blob: 694ed2dd37248581283e641bb4c938862a80ab2a [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 Tarreaue4847c62016-01-08 15:43:54 +0100337 ST_F_COOKIE,
Willy Tarreauf8211df2016-01-11 14:09:38 +0100338 ST_F_MODE,
Willy Tarreauf1516d92016-01-11 14:48:36 +0100339 ST_F_ALGO,
Willy Tarreauc73810f2016-01-11 13:52:04 +0100340 ST_F_CONN_RATE,
341 ST_F_CONN_RATE_MAX,
342 ST_F_CONN_TOT,
Willy Tarreau5b9bdff2016-01-11 14:40:47 +0100343 ST_F_INTERCEPTED,
Willy Tarreau8e205142016-01-04 17:23:25 +0100344
345 /* must always be the last one */
346 ST_F_TOTAL_FIELDS
347};
348
349/* These are the field names for each ST_F_* field position. Please pay attention
350 * to always use the exact same name except that the strings must be lower case
351 * while the enum entries must be upper case.
352 */
353const char *stat_field_names[ST_F_TOTAL_FIELDS] = {
354 [ST_F_PXNAME] = "pxname",
355 [ST_F_SVNAME] = "svname",
356 [ST_F_QCUR] = "qcur",
357 [ST_F_QMAX] = "qmax",
358 [ST_F_SCUR] = "scur",
359 [ST_F_SMAX] = "smax",
360 [ST_F_SLIM] = "slim",
361 [ST_F_STOT] = "stot",
362 [ST_F_BIN] = "bin",
363 [ST_F_BOUT] = "bout",
364 [ST_F_DREQ] = "dreq",
365 [ST_F_DRESP] = "dresp",
366 [ST_F_EREQ] = "ereq",
367 [ST_F_ECON] = "econ",
368 [ST_F_ERESP] = "eresp",
369 [ST_F_WRETR] = "wretr",
370 [ST_F_WREDIS] = "wredis",
371 [ST_F_STATUS] = "status",
372 [ST_F_WEIGHT] = "weight",
373 [ST_F_ACT] = "act",
374 [ST_F_BCK] = "bck",
375 [ST_F_CHKFAIL] = "chkfail",
376 [ST_F_CHKDOWN] = "chkdown",
377 [ST_F_LASTCHG] = "lastchg",
378 [ST_F_DOWNTIME] = "downtime",
379 [ST_F_QLIMIT] = "qlimit",
380 [ST_F_PID] = "pid",
381 [ST_F_IID] = "iid",
382 [ST_F_SID] = "sid",
383 [ST_F_THROTTLE] = "throttle",
384 [ST_F_LBTOT] = "lbtot",
385 [ST_F_TRACKED] = "tracked",
386 [ST_F_TYPE] = "type",
387 [ST_F_RATE] = "rate",
388 [ST_F_RATE_LIM] = "rate_lim",
389 [ST_F_RATE_MAX] = "rate_max",
390 [ST_F_CHECK_STATUS] = "check_status",
391 [ST_F_CHECK_CODE] = "check_code",
392 [ST_F_CHECK_DURATION] = "check_duration",
393 [ST_F_HRSP_1XX] = "hrsp_1xx",
394 [ST_F_HRSP_2XX] = "hrsp_2xx",
395 [ST_F_HRSP_3XX] = "hrsp_3xx",
396 [ST_F_HRSP_4XX] = "hrsp_4xx",
397 [ST_F_HRSP_5XX] = "hrsp_5xx",
398 [ST_F_HRSP_OTHER] = "hrsp_other",
399 [ST_F_HANAFAIL] = "hanafail",
400 [ST_F_REQ_RATE] = "req_rate",
401 [ST_F_REQ_RATE_MAX] = "req_rate_max",
402 [ST_F_REQ_TOT] = "req_tot",
403 [ST_F_CLI_ABRT] = "cli_abrt",
404 [ST_F_SRV_ABRT] = "srv_abrt",
405 [ST_F_COMP_IN] = "comp_in",
406 [ST_F_COMP_OUT] = "comp_out",
407 [ST_F_COMP_BYP] = "comp_byp",
408 [ST_F_COMP_RSP] = "comp_rsp",
409 [ST_F_LASTSESS] = "lastsess",
410 [ST_F_LAST_CHK] = "last_chk",
411 [ST_F_LAST_AGT] = "last_agt",
412 [ST_F_QTIME] = "qtime",
413 [ST_F_CTIME] = "ctime",
414 [ST_F_RTIME] = "rtime",
415 [ST_F_TTIME] = "ttime",
Willy Tarreau7f618842016-01-08 11:40:03 +0100416 [ST_F_AGENT_STATUS] = "agent_status",
417 [ST_F_AGENT_CODE] = "agent_code",
418 [ST_F_AGENT_DURATION] = "agent_duration",
Willy Tarreaudd7354b2016-01-08 13:47:26 +0100419 [ST_F_CHECK_DESC] = "check_desc",
420 [ST_F_AGENT_DESC] = "agent_desc",
Willy Tarreau3141f592016-01-08 14:25:28 +0100421 [ST_F_CHECK_RISE] = "check_rise",
422 [ST_F_CHECK_FALL] = "check_fall",
423 [ST_F_CHECK_HEALTH] = "check_health",
424 [ST_F_AGENT_RISE] = "agent_rise",
425 [ST_F_AGENT_FALL] = "agent_fall",
426 [ST_F_AGENT_HEALTH] = "agent_health",
Willy Tarreau3a4ec3a2016-01-08 15:35:43 +0100427 [ST_F_ADDR] = "addr",
Willy Tarreaue4847c62016-01-08 15:43:54 +0100428 [ST_F_COOKIE] = "cookie",
Willy Tarreauf8211df2016-01-11 14:09:38 +0100429 [ST_F_MODE] = "mode",
Willy Tarreauf1516d92016-01-11 14:48:36 +0100430 [ST_F_ALGO] = "algo",
Willy Tarreauc73810f2016-01-11 13:52:04 +0100431 [ST_F_CONN_RATE] = "conn_rate",
432 [ST_F_CONN_RATE_MAX] = "conn_rate_max",
433 [ST_F_CONN_TOT] = "conn_tot",
Willy Tarreau5b9bdff2016-01-11 14:40:47 +0100434 [ST_F_INTERCEPTED] = "intercepted",
Willy Tarreau8e205142016-01-04 17:23:25 +0100435};
436
Willy Tarreau82a86022016-01-04 19:04:18 +0100437/* one line of stats */
438static struct field stats[ST_F_TOTAL_FIELDS];
439
Baptiste Assmann9b6857e2015-09-18 14:49:12 +0200440static int stats_dump_backend_to_buffer(struct stream_interface *si);
Willy Tarreauae795722016-02-16 11:27:28 +0100441static int stats_dump_env_to_buffer(struct stream_interface *si);
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +0100442static int stats_dump_info_to_buffer(struct stream_interface *si);
Baptiste Assmann28289462015-07-03 08:01:20 +0200443static int stats_dump_servers_state_to_buffer(struct stream_interface *si);
Willy Tarreau12833bb2014-01-28 16:49:56 +0100444static int stats_dump_pools_to_buffer(struct stream_interface *si);
Willy Tarreau87b09662015-04-03 00:22:06 +0200445static int stats_dump_full_sess_to_buffer(struct stream_interface *si, struct stream *sess);
Simon Horman9bd2c732011-06-15 15:18:44 +0900446static int stats_dump_sess_to_buffer(struct stream_interface *si);
447static int stats_dump_errors_to_buffer(struct stream_interface *si);
Willy Tarreau44455022012-12-05 23:01:12 +0100448static int stats_table_request(struct stream_interface *si, int show);
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +0100449static int stats_dump_proxy_to_buffer(struct stream_interface *si, struct proxy *px, struct uri_auth *uri);
450static int stats_dump_stat_to_buffer(struct stream_interface *si, struct uri_auth *uri);
Baptiste Assmann3863f972015-05-17 00:33:24 +0200451static int stats_dump_resolvers_to_buffer(struct stream_interface *si);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100452static int stats_pats_list(struct stream_interface *si);
453static int stats_pat_list(struct stream_interface *si);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +0100454static int stats_map_lookup(struct stream_interface *si);
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +0200455#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
456static int stats_tlskeys_list(struct stream_interface *si);
457#endif
Willy Tarreau00a37f02015-04-13 12:05:19 +0200458static void cli_release_handler(struct appctx *appctx);
Simon Horman9bd2c732011-06-15 15:18:44 +0900459
Baptiste Assmann28289462015-07-03 08:01:20 +0200460static void dump_servers_state(struct proxy *backend, struct chunk *buf);
461
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100462/*
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +0100463 * cli_io_handler()
464 * -> stats_dump_sess_to_buffer() // "show sess"
465 * -> stats_dump_errors_to_buffer() // "show errors"
466 * -> stats_dump_info_to_buffer() // "show info"
Baptiste Assmann9b6857e2015-09-18 14:49:12 +0200467 * -> stats_dump_backend_to_buffer() // "show backend"
Baptiste Assmann28289462015-07-03 08:01:20 +0200468 * -> stats_dump_servers_state_to_buffer() // "show servers state [<backend name>]"
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +0100469 * -> stats_dump_stat_to_buffer() // "show stat"
Baptiste Assmann3863f972015-05-17 00:33:24 +0200470 * -> stats_dump_resolvers_to_buffer() // "show stat resolver <id>"
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +0100471 * -> stats_dump_csv_header()
472 * -> stats_dump_proxy_to_buffer()
473 * -> stats_dump_fe_stats()
474 * -> stats_dump_li_stats()
475 * -> stats_dump_sv_stats()
476 * -> stats_dump_be_stats()
477 *
478 * http_stats_io_handler()
479 * -> stats_dump_stat_to_buffer() // same as above, but used for CSV or HTML
480 * -> stats_dump_csv_header() // emits the CSV headers (same as above)
481 * -> stats_dump_html_head() // emits the HTML headers
482 * -> stats_dump_html_info() // emits the equivalent of "show info" at the top
483 * -> stats_dump_proxy_to_buffer() // same as above, valid for CSV and HTML
484 * -> stats_dump_html_px_hdr()
485 * -> stats_dump_fe_stats()
486 * -> stats_dump_li_stats()
487 * -> stats_dump_sv_stats()
488 * -> stats_dump_be_stats()
489 * -> stats_dump_html_px_end()
490 * -> stats_dump_html_end() // emits HTML trailer
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100491 */
492
Willy Tarreau30576452015-04-13 13:50:30 +0200493static struct applet cli_applet;
Simon Horman9bd2c732011-06-15 15:18:44 +0900494
495static const char stats_sock_usage_msg[] =
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200496 "Unknown command. Please enter one of the following commands only :\n"
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200497 " clear counters : clear max statistics counters (add 'all' for all counters)\n"
Willy Tarreau88ee3972010-07-13 13:48:00 +0200498 " clear table : remove an entry from a table\n"
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200499 " help : this message\n"
500 " prompt : toggle interactive mode with prompt\n"
501 " quit : disconnect\n"
Baptiste Assmann9b6857e2015-09-18 14:49:12 +0200502 " show backend : list backends in the current running config\n"
Willy Tarreauae795722016-02-16 11:27:28 +0100503 " show env [var] : dump environment variables known to the process\n"
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200504 " show info : report information about the running process\n"
Willy Tarreau12833bb2014-01-28 16:49:56 +0100505 " show pools : report information about the memory pools usage\n"
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200506 " show stat : report counters for each proxy and server\n"
507 " show errors : report last request and response errors for each proxy\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +0100508 " show sess [id] : report the list of current sessions or dump this session\n"
Willy Tarreau69f58c82010-07-12 17:55:33 +0200509 " show table [id]: report table usage stats or dump this table's contents\n"
Baptiste Assmann28289462015-07-03 08:01:20 +0200510 " show servers state [id]: dump volatile server information (for backend <id>)\n"
Willy Tarreau38338fa2009-10-10 18:37:29 +0200511 " get weight : report a server's current weight\n"
Willy Tarreau4483d432009-10-10 19:30:08 +0200512 " set weight : change a server's weight\n"
Baptiste Assmann3d8f8312015-04-13 22:54:33 +0200513 " set server : change a server's state, weight or address\n"
Willy Tarreau654694e2012-06-07 01:03:16 +0200514 " set table [id] : update or create a table entry's data\n"
Willy Tarreau7aabd112010-01-26 10:59:06 +0100515 " set timeout : change a timeout setting\n"
Willy Tarreau2a0f4d22011-08-02 11:49:05 +0200516 " set maxconn : change a maxconn setting\n"
Willy Tarreauf5b22872011-09-07 16:13:44 +0200517 " set rate-limit : change a rate limiting value\n"
Willy Tarreaua295edc2011-09-07 23:21:03 +0200518 " disable : put a server or frontend in maintenance mode\n"
519 " enable : re-enable a server or frontend which is in maintenance mode\n"
520 " shutdown : kill a session or a frontend (eg:to release listening ports)\n"
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100521 " show acl [id] : report avalaible acls or dump an acl's contents\n"
522 " get acl : reports the patterns matching a sample for an ACL\n"
523 " add acl : add acl entry\n"
524 " del acl : delete acl entry\n"
525 " clear acl <id> : clear the content of this acl\n"
Thierry FOURNIER1432a0c2014-03-11 13:42:38 +0100526 " show map [id] : report avalaible maps or dump a map's contents\n"
527 " get map : reports the keys and values matching a sample for a map\n"
528 " set map : modify map entry\n"
529 " add map : add map entry\n"
530 " del map : delete map entry\n"
531 " clear map <id> : clear the content of this map\n"
Emeric Brun4147b2e2014-06-16 18:36:30 +0200532 " set ssl <stmt> : set statement for ssl\n"
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200533 "";
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200534
Simon Horman9bd2c732011-06-15 15:18:44 +0900535static const char stats_permission_denied_msg[] =
Willy Tarreau6162db22009-10-10 17:13:00 +0200536 "Permission denied\n"
537 "";
538
Willy Tarreau295a8372011-03-10 11:25:07 +0100539/* data transmission states for the stats responses */
540enum {
541 STAT_ST_INIT = 0,
542 STAT_ST_HEAD,
543 STAT_ST_INFO,
544 STAT_ST_LIST,
545 STAT_ST_END,
546 STAT_ST_FIN,
547};
548
549/* data transmission states for the stats responses inside a proxy */
550enum {
551 STAT_PX_ST_INIT = 0,
552 STAT_PX_ST_TH,
553 STAT_PX_ST_FE,
554 STAT_PX_ST_LI,
555 STAT_PX_ST_SV,
556 STAT_PX_ST_BE,
557 STAT_PX_ST_END,
558 STAT_PX_ST_FIN,
559};
560
Cyril Bonté19979e12012-04-04 12:57:21 +0200561extern const char *stat_status_codes[];
562
Willy Tarreau07e9e642010-08-17 21:48:17 +0200563/* allocate a new stats frontend named <name>, and return it
564 * (or NULL in case of lack of memory).
565 */
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200566static struct proxy *alloc_stats_fe(const char *name, const char *file, int line)
Willy Tarreau07e9e642010-08-17 21:48:17 +0200567{
568 struct proxy *fe;
569
570 fe = (struct proxy *)calloc(1, sizeof(struct proxy));
571 if (!fe)
572 return NULL;
573
Willy Tarreau237250c2011-07-29 01:49:03 +0200574 init_new_proxy(fe);
Willy Tarreau050536d2012-10-04 08:47:34 +0200575 fe->next = proxy;
576 proxy = fe;
Willy Tarreau07e9e642010-08-17 21:48:17 +0200577 fe->last_change = now.tv_sec;
578 fe->id = strdup("GLOBAL");
579 fe->cap = PR_CAP_FE;
Willy Tarreauc2adf8b2011-09-07 12:13:34 +0200580 fe->maxconn = 10; /* default to 10 concurrent connections */
581 fe->timeout.client = MS_TO_TICKS(10000); /* default timeout of 10 seconds */
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200582 fe->conf.file = strdup(file);
583 fe->conf.line = line;
Willy Tarreau32b60d42015-03-13 16:14:57 +0100584 fe->accept = frontend_accept;
Willy Tarreauf87ab942015-03-13 15:55:16 +0100585 fe->default_target = &cli_applet.obj_type;
Willy Tarreau050536d2012-10-04 08:47:34 +0200586
587 /* the stats frontend is the only one able to assign ID #0 */
588 fe->conf.id.key = fe->uuid = 0;
589 eb32_insert(&used_proxy_id, &fe->conf.id);
Willy Tarreau07e9e642010-08-17 21:48:17 +0200590 return fe;
591}
592
Willy Tarreaufbee7132007-10-18 13:53:22 +0200593/* This function parses a "stats" statement in the "global" section. It returns
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200594 * -1 if there is any error, otherwise zero. If it returns -1, it will write an
595 * error message into the <err> buffer which will be preallocated. The trailing
596 * '\n' must not be written. The function must be called with <args> pointing to
597 * the first word after "stats".
Willy Tarreaufbee7132007-10-18 13:53:22 +0200598 */
Willy Tarreau10522fd2008-07-09 20:12:41 +0200599static int stats_parse_global(char **args, int section_type, struct proxy *curpx,
Willy Tarreau28a47d62012-09-18 20:02:48 +0200600 struct proxy *defpx, const char *file, int line,
601 char **err)
Willy Tarreaufbee7132007-10-18 13:53:22 +0200602{
Willy Tarreau4348fad2012-09-20 16:48:07 +0200603 struct bind_conf *bind_conf;
Willy Tarreauc53d4222012-09-20 20:19:28 +0200604 struct listener *l;
Willy Tarreau4348fad2012-09-20 16:48:07 +0200605
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200606 if (!strcmp(args[1], "socket")) {
Willy Tarreaufbee7132007-10-18 13:53:22 +0200607 int cur_arg;
608
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200609 if (*args[2] == 0) {
Willy Tarreauc53d4222012-09-20 20:19:28 +0200610 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 +0200611 return -1;
612 }
613
Willy Tarreau89a63132009-08-16 17:41:45 +0200614 if (!global.stats_fe) {
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200615 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200616 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
Willy Tarreau89a63132009-08-16 17:41:45 +0200617 return -1;
618 }
Willy Tarreau89a63132009-08-16 17:41:45 +0200619 }
620
Willy Tarreau4348fad2012-09-20 16:48:07 +0200621 bind_conf = bind_conf_alloc(&global.stats_fe->conf.bind, file, line, args[2]);
Willy Tarreau290e63a2012-09-20 18:07:14 +0200622 bind_conf->level = ACCESS_LVL_OPER; /* default access level */
Willy Tarreau4348fad2012-09-20 16:48:07 +0200623
Willy Tarreauc53d4222012-09-20 20:19:28 +0200624 if (!str2listener(args[2], global.stats_fe, bind_conf, file, line, err)) {
625 memprintf(err, "parsing [%s:%d] : '%s %s' : %s\n",
626 file, line, args[0], args[1], err && *err ? *err : "error");
627 return -1;
628 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200629
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200630 cur_arg = 3;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200631 while (*args[cur_arg]) {
Willy Tarreaud5781202012-09-22 19:32:35 +0200632 static int bind_dumped;
633 struct bind_kw *kw;
634
635 kw = bind_find_kw(args[cur_arg]);
636 if (kw) {
637 if (!kw->parse) {
638 memprintf(err, "'%s %s' : '%s' option is not implemented in this version (check build options).",
639 args[0], args[1], args[cur_arg]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200640 return -1;
641 }
Willy Tarreaud5781202012-09-22 19:32:35 +0200642
Willy Tarreau061b5de2015-10-13 15:06:57 +0200643 if (kw->parse(args, cur_arg, global.stats_fe, bind_conf, err) != 0) {
Willy Tarreaud5781202012-09-22 19:32:35 +0200644 if (err && *err)
645 memprintf(err, "'%s %s' : '%s'", args[0], args[1], *err);
646 else
647 memprintf(err, "'%s %s' : error encountered while processing '%s'",
648 args[0], args[1], args[cur_arg]);
Willy Tarreau6162db22009-10-10 17:13:00 +0200649 return -1;
650 }
Willy Tarreaud5781202012-09-22 19:32:35 +0200651
652 cur_arg += 1 + kw->skip;
653 continue;
Willy Tarreau6162db22009-10-10 17:13:00 +0200654 }
Willy Tarreaud5781202012-09-22 19:32:35 +0200655
656 if (!bind_dumped) {
657 bind_dump_kws(err);
658 indent_msg(err, 4);
659 bind_dumped = 1;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200660 }
Willy Tarreaud5781202012-09-22 19:32:35 +0200661
662 memprintf(err, "'%s %s' : unknown keyword '%s'.%s%s",
663 args[0], args[1], args[cur_arg],
664 err && *err ? " Registered keywords :" : "", err && *err ? *err : "");
665 return -1;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200666 }
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100667
Willy Tarreauc53d4222012-09-20 20:19:28 +0200668 list_for_each_entry(l, &bind_conf->listeners, by_bind) {
669 l->maxconn = global.stats_fe->maxconn;
670 l->backlog = global.stats_fe->backlog;
Willy Tarreau9903f0e2015-04-04 18:50:31 +0200671 l->accept = session_accept_fd;
Willy Tarreau87b09662015-04-03 00:22:06 +0200672 l->handler = process_stream;
Willy Tarreau10b688f2015-03-13 16:43:12 +0100673 l->default_target = global.stats_fe->default_target;
Willy Tarreauc53d4222012-09-20 20:19:28 +0200674 l->options |= LI_O_UNLIMITED; /* don't make the peers subject to global limits */
675 l->nice = -64; /* we want to boost priority for local stats */
676 global.maxsock += l->maxconn;
677 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200678 }
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200679 else if (!strcmp(args[1], "timeout")) {
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100680 unsigned timeout;
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200681 const char *res = parse_time_err(args[2], &timeout, TIME_UNIT_MS);
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100682
683 if (res) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200684 memprintf(err, "'%s %s' : unexpected character '%c'", args[0], args[1], *res);
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100685 return -1;
686 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200687
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100688 if (!timeout) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200689 memprintf(err, "'%s %s' expects a positive value", args[0], args[1]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200690 return -1;
691 }
Willy Tarreau07e9e642010-08-17 21:48:17 +0200692 if (!global.stats_fe) {
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200693 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200694 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
Willy Tarreau07e9e642010-08-17 21:48:17 +0200695 return -1;
696 }
697 }
Willy Tarreau89a63132009-08-16 17:41:45 +0200698 global.stats_fe->timeout.client = MS_TO_TICKS(timeout);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200699 }
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200700 else if (!strcmp(args[1], "maxconn")) {
701 int maxconn = atol(args[2]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200702
703 if (maxconn <= 0) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200704 memprintf(err, "'%s %s' expects a positive value", args[0], args[1]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200705 return -1;
706 }
Willy Tarreauc2adf8b2011-09-07 12:13:34 +0200707
708 if (!global.stats_fe) {
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200709 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200710 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
Willy Tarreauc2adf8b2011-09-07 12:13:34 +0200711 return -1;
712 }
713 }
714 global.stats_fe->maxconn = maxconn;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200715 }
Willy Tarreau35b7b162012-10-22 23:17:18 +0200716 else if (!strcmp(args[1], "bind-process")) { /* enable the socket only on some processes */
717 int cur_arg = 2;
Willy Tarreaua9db57e2013-01-18 11:29:29 +0100718 unsigned long set = 0;
Willy Tarreau35b7b162012-10-22 23:17:18 +0200719
Willy Tarreau91319572013-04-20 09:48:50 +0200720 if (!global.stats_fe) {
721 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
722 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
723 return -1;
724 }
725 }
726
Willy Tarreau35b7b162012-10-22 23:17:18 +0200727 while (*args[cur_arg]) {
Willy Tarreau110ecc12012-11-15 17:50:01 +0100728 unsigned int low, high;
729
Willy Tarreau35b7b162012-10-22 23:17:18 +0200730 if (strcmp(args[cur_arg], "all") == 0) {
731 set = 0;
732 break;
733 }
734 else if (strcmp(args[cur_arg], "odd") == 0) {
Willy Tarreaua9db57e2013-01-18 11:29:29 +0100735 set |= ~0UL/3UL; /* 0x555....555 */
Willy Tarreau35b7b162012-10-22 23:17:18 +0200736 }
737 else if (strcmp(args[cur_arg], "even") == 0) {
Willy Tarreaua9db57e2013-01-18 11:29:29 +0100738 set |= (~0UL/3UL) << 1; /* 0xAAA...AAA */
Willy Tarreau35b7b162012-10-22 23:17:18 +0200739 }
Willy Tarreau83d84cf2012-11-22 01:04:31 +0100740 else if (isdigit((int)*args[cur_arg])) {
Willy Tarreau110ecc12012-11-15 17:50:01 +0100741 char *dash = strchr(args[cur_arg], '-');
742
743 low = high = str2uic(args[cur_arg]);
744 if (dash)
745 high = str2uic(dash + 1);
746
747 if (high < low) {
748 unsigned int swap = low;
749 low = high;
750 high = swap;
751 }
752
Willy Tarreaua9db57e2013-01-18 11:29:29 +0100753 if (low < 1 || high > LONGBITS) {
754 memprintf(err, "'%s %s' supports process numbers from 1 to %d.\n",
755 args[0], args[1], LONGBITS);
Willy Tarreau35b7b162012-10-22 23:17:18 +0200756 return -1;
757 }
Willy Tarreau110ecc12012-11-15 17:50:01 +0100758 while (low <= high)
Willy Tarreaua9db57e2013-01-18 11:29:29 +0100759 set |= 1UL << (low++ - 1);
Willy Tarreau110ecc12012-11-15 17:50:01 +0100760 }
761 else {
762 memprintf(err,
Willy Tarreaua9db57e2013-01-18 11:29:29 +0100763 "'%s %s' expects 'all', 'odd', 'even', or a list of process ranges with numbers from 1 to %d.\n",
764 args[0], args[1], LONGBITS);
Willy Tarreau110ecc12012-11-15 17:50:01 +0100765 return -1;
Willy Tarreau35b7b162012-10-22 23:17:18 +0200766 }
767 cur_arg++;
768 }
769 global.stats_fe->bind_proc = set;
770 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200771 else {
Willy Tarreau35b7b162012-10-22 23:17:18 +0200772 memprintf(err, "'%s' only supports 'socket', 'maxconn', 'bind-process' and 'timeout' (got '%s')", args[0], args[1]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200773 return -1;
774 }
775 return 0;
776}
777
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100778/* Dumps the stats CSV header to the trash buffer which. The caller is responsible
779 * for clearing it if needed.
Willy Tarreauf522f3d2014-02-10 22:22:49 +0100780 * NOTE: Some tools happen to rely on the field position instead of its name,
781 * so please only append new fields at the end, never in the middle.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100782 */
783static void stats_dump_csv_header()
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100784{
Willy Tarreauf6142292016-01-04 17:24:29 +0100785 int field;
786
787 chunk_appendf(&trash, "# ");
788 for (field = 0; field < ST_F_TOTAL_FIELDS; field++)
789 chunk_appendf(&trash, "%s,", stat_field_names[field]);
790
791 chunk_appendf(&trash, "\n");
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100792}
793
Simon Hormand9366582011-06-15 15:18:45 +0900794/* print a string of text buffer to <out>. The format is :
795 * Non-printable chars \t, \n, \r and \e are * encoded in C format.
796 * Other non-printable chars are encoded "\xHH". Space and '\' are also escaped.
797 * Print stopped if null char or <bsize> is reached, or if no more place in the chunk.
798 */
799static int dump_text(struct chunk *out, const char *buf, int bsize)
800{
801 unsigned char c;
802 int ptr = 0;
803
804 while (buf[ptr] && ptr < bsize) {
805 c = buf[ptr];
806 if (isprint(c) && isascii(c) && c != '\\' && c != ' ') {
807 if (out->len > out->size - 1)
808 break;
809 out->str[out->len++] = c;
810 }
811 else if (c == '\t' || c == '\n' || c == '\r' || c == '\e' || c == '\\' || c == ' ') {
812 if (out->len > out->size - 2)
813 break;
814 out->str[out->len++] = '\\';
815 switch (c) {
816 case ' ': c = ' '; break;
817 case '\t': c = 't'; break;
818 case '\n': c = 'n'; break;
819 case '\r': c = 'r'; break;
820 case '\e': c = 'e'; break;
821 case '\\': c = '\\'; break;
822 }
823 out->str[out->len++] = c;
824 }
825 else {
826 if (out->len > out->size - 4)
827 break;
828 out->str[out->len++] = '\\';
829 out->str[out->len++] = 'x';
830 out->str[out->len++] = hextab[(c >> 4) & 0xF];
831 out->str[out->len++] = hextab[c & 0xF];
832 }
833 ptr++;
834 }
835
836 return ptr;
837}
838
839/* print a buffer in hexa.
840 * Print stopped if <bsize> is reached, or if no more place in the chunk.
841 */
842static int dump_binary(struct chunk *out, const char *buf, int bsize)
843{
844 unsigned char c;
845 int ptr = 0;
846
847 while (ptr < bsize) {
848 c = buf[ptr];
849
850 if (out->len > out->size - 2)
851 break;
852 out->str[out->len++] = hextab[(c >> 4) & 0xF];
853 out->str[out->len++] = hextab[c & 0xF];
854
855 ptr++;
856 }
857 return ptr;
858}
859
860/* Dump the status of a table to a stream interface's
861 * read buffer. It returns 0 if the output buffer is full
862 * and needs to be called again, otherwise non-zero.
863 */
864static int stats_dump_table_head_to_buffer(struct chunk *msg, struct stream_interface *si,
865 struct proxy *proxy, struct proxy *target)
866{
Willy Tarreau87b09662015-04-03 00:22:06 +0200867 struct stream *s = si_strm(si);
Simon Hormand9366582011-06-15 15:18:45 +0900868
Willy Tarreau77804732012-10-29 16:14:26 +0100869 chunk_appendf(msg, "# table: %s, type: %s, size:%d, used:%d\n",
Simon Hormand9366582011-06-15 15:18:45 +0900870 proxy->id, stktable_types[proxy->table.type].kw, proxy->table.size, proxy->table.current);
871
872 /* any other information should be dumped here */
873
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200874 if (target && strm_li(s)->bind_conf->level < ACCESS_LVL_OPER)
Willy Tarreau77804732012-10-29 16:14:26 +0100875 chunk_appendf(msg, "# contents not dumped due to insufficient privileges\n");
Simon Hormand9366582011-06-15 15:18:45 +0900876
Willy Tarreaubc18da12015-03-13 14:00:47 +0100877 if (bi_putchk(si_ic(si), msg) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +0200878 si_applet_cant_put(si);
Simon Hormand9366582011-06-15 15:18:45 +0900879 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +0100880 }
Simon Hormand9366582011-06-15 15:18:45 +0900881
882 return 1;
883}
884
885/* Dump the a table entry to a stream interface's
886 * read buffer. It returns 0 if the output buffer is full
887 * and needs to be called again, otherwise non-zero.
888 */
889static int stats_dump_table_entry_to_buffer(struct chunk *msg, struct stream_interface *si,
890 struct proxy *proxy, struct stksess *entry)
891{
892 int dt;
893
Willy Tarreau77804732012-10-29 16:14:26 +0100894 chunk_appendf(msg, "%p:", entry);
Simon Hormand9366582011-06-15 15:18:45 +0900895
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200896 if (proxy->table.type == SMP_T_IPV4) {
Simon Hormand9366582011-06-15 15:18:45 +0900897 char addr[INET_ADDRSTRLEN];
898 inet_ntop(AF_INET, (const void *)&entry->key.key, addr, sizeof(addr));
Willy Tarreau77804732012-10-29 16:14:26 +0100899 chunk_appendf(msg, " key=%s", addr);
Simon Hormand9366582011-06-15 15:18:45 +0900900 }
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200901 else if (proxy->table.type == SMP_T_IPV6) {
Simon Hormand9366582011-06-15 15:18:45 +0900902 char addr[INET6_ADDRSTRLEN];
903 inet_ntop(AF_INET6, (const void *)&entry->key.key, addr, sizeof(addr));
Willy Tarreau77804732012-10-29 16:14:26 +0100904 chunk_appendf(msg, " key=%s", addr);
Simon Hormand9366582011-06-15 15:18:45 +0900905 }
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200906 else if (proxy->table.type == SMP_T_SINT) {
Willy Tarreau77804732012-10-29 16:14:26 +0100907 chunk_appendf(msg, " key=%u", *(unsigned int *)entry->key.key);
Simon Hormand9366582011-06-15 15:18:45 +0900908 }
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200909 else if (proxy->table.type == SMP_T_STR) {
Willy Tarreau77804732012-10-29 16:14:26 +0100910 chunk_appendf(msg, " key=");
Simon Hormand9366582011-06-15 15:18:45 +0900911 dump_text(msg, (const char *)entry->key.key, proxy->table.key_size);
912 }
913 else {
Willy Tarreau77804732012-10-29 16:14:26 +0100914 chunk_appendf(msg, " key=");
Simon Hormand9366582011-06-15 15:18:45 +0900915 dump_binary(msg, (const char *)entry->key.key, proxy->table.key_size);
916 }
917
Willy Tarreau77804732012-10-29 16:14:26 +0100918 chunk_appendf(msg, " use=%d exp=%d", entry->ref_cnt - 1, tick_remain(now_ms, entry->expire));
Simon Hormand9366582011-06-15 15:18:45 +0900919
920 for (dt = 0; dt < STKTABLE_DATA_TYPES; dt++) {
921 void *ptr;
922
923 if (proxy->table.data_ofs[dt] == 0)
924 continue;
925 if (stktable_data_types[dt].arg_type == ARG_T_DELAY)
Willy Tarreau77804732012-10-29 16:14:26 +0100926 chunk_appendf(msg, " %s(%d)=", stktable_data_types[dt].name, proxy->table.data_arg[dt].u);
Simon Hormand9366582011-06-15 15:18:45 +0900927 else
Willy Tarreau77804732012-10-29 16:14:26 +0100928 chunk_appendf(msg, " %s=", stktable_data_types[dt].name);
Simon Hormand9366582011-06-15 15:18:45 +0900929
930 ptr = stktable_data_ptr(&proxy->table, entry, dt);
931 switch (stktable_data_types[dt].std_type) {
932 case STD_T_SINT:
Willy Tarreau77804732012-10-29 16:14:26 +0100933 chunk_appendf(msg, "%d", stktable_data_cast(ptr, std_t_sint));
Simon Hormand9366582011-06-15 15:18:45 +0900934 break;
935 case STD_T_UINT:
Willy Tarreau77804732012-10-29 16:14:26 +0100936 chunk_appendf(msg, "%u", stktable_data_cast(ptr, std_t_uint));
Simon Hormand9366582011-06-15 15:18:45 +0900937 break;
938 case STD_T_ULL:
Willy Tarreau77804732012-10-29 16:14:26 +0100939 chunk_appendf(msg, "%lld", stktable_data_cast(ptr, std_t_ull));
Simon Hormand9366582011-06-15 15:18:45 +0900940 break;
941 case STD_T_FRQP:
Willy Tarreau77804732012-10-29 16:14:26 +0100942 chunk_appendf(msg, "%d",
Simon Hormand9366582011-06-15 15:18:45 +0900943 read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
944 proxy->table.data_arg[dt].u));
945 break;
946 }
947 }
Willy Tarreau77804732012-10-29 16:14:26 +0100948 chunk_appendf(msg, "\n");
Simon Hormand9366582011-06-15 15:18:45 +0900949
Willy Tarreaubc18da12015-03-13 14:00:47 +0100950 if (bi_putchk(si_ic(si), msg) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +0200951 si_applet_cant_put(si);
Simon Hormand9366582011-06-15 15:18:45 +0900952 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +0100953 }
Simon Hormand9366582011-06-15 15:18:45 +0900954
955 return 1;
956}
957
Willy Tarreaudec98142012-06-06 23:37:08 +0200958static void stats_sock_table_key_request(struct stream_interface *si, char **args, int action)
Simon Horman121f3052011-06-15 15:18:46 +0900959{
Willy Tarreau87b09662015-04-03 00:22:06 +0200960 struct stream *s = si_strm(si);
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100961 struct appctx *appctx = __objt_appctx(si->end);
962 struct proxy *px = appctx->ctx.table.target;
Simon Horman121f3052011-06-15 15:18:46 +0900963 struct stksess *ts;
Simon Hormancec9a222011-06-15 15:18:51 +0900964 uint32_t uint32_key;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900965 unsigned char ip6_key[sizeof(struct in6_addr)];
Willy Tarreau654694e2012-06-07 01:03:16 +0200966 long long value;
967 int data_type;
Willy Tarreau47060b62013-08-01 21:11:42 +0200968 int cur_arg;
Willy Tarreau654694e2012-06-07 01:03:16 +0200969 void *ptr;
970 struct freq_ctr_period *frqp;
Simon Horman121f3052011-06-15 15:18:46 +0900971
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100972 appctx->st0 = STAT_CLI_OUTPUT;
Simon Horman121f3052011-06-15 15:18:46 +0900973
974 if (!*args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100975 appctx->ctx.cli.msg = "Key value expected\n";
976 appctx->st0 = STAT_CLI_PRINT;
Simon Horman121f3052011-06-15 15:18:46 +0900977 return;
978 }
979
Simon Hormanc5b89f62011-06-15 15:18:50 +0900980 switch (px->table.type) {
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200981 case SMP_T_IPV4:
Simon Hormancec9a222011-06-15 15:18:51 +0900982 uint32_key = htonl(inetaddr_host(args[4]));
Willy Tarreau07115412012-10-29 21:56:59 +0100983 static_table_key->key = &uint32_key;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900984 break;
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200985 case SMP_T_IPV6:
Simon Hormanc5b89f62011-06-15 15:18:50 +0900986 inet_pton(AF_INET6, args[4], ip6_key);
Willy Tarreau07115412012-10-29 21:56:59 +0100987 static_table_key->key = &ip6_key;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900988 break;
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200989 case SMP_T_SINT:
Simon Hormancec9a222011-06-15 15:18:51 +0900990 {
991 char *endptr;
992 unsigned long val;
993 errno = 0;
994 val = strtoul(args[4], &endptr, 10);
995 if ((errno == ERANGE && val == ULONG_MAX) ||
996 (errno != 0 && val == 0) || endptr == args[4] ||
997 val > 0xffffffff) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100998 appctx->ctx.cli.msg = "Invalid key\n";
999 appctx->st0 = STAT_CLI_PRINT;
Simon Hormancec9a222011-06-15 15:18:51 +09001000 return;
1001 }
1002 uint32_key = (uint32_t) val;
Willy Tarreau07115412012-10-29 21:56:59 +01001003 static_table_key->key = &uint32_key;
Simon Hormancec9a222011-06-15 15:18:51 +09001004 break;
1005 }
1006 break;
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +02001007 case SMP_T_STR:
Willy Tarreau07115412012-10-29 21:56:59 +01001008 static_table_key->key = args[4];
1009 static_table_key->key_len = strlen(args[4]);
Simon Horman619e3cc2011-06-15 15:18:52 +09001010 break;
Simon Hormanc5b89f62011-06-15 15:18:50 +09001011 default:
Willy Tarreaudec98142012-06-06 23:37:08 +02001012 switch (action) {
1013 case STAT_CLI_O_TAB:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001014 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 +02001015 break;
1016 case STAT_CLI_O_CLR:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001017 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 +02001018 break;
1019 default:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001020 appctx->ctx.cli.msg = "Unknown action\n";
Willy Tarreaudec98142012-06-06 23:37:08 +02001021 break;
1022 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001023 appctx->st0 = STAT_CLI_PRINT;
Simon Horman121f3052011-06-15 15:18:46 +09001024 return;
1025 }
1026
1027 /* check permissions */
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001028 if (strm_li(s)->bind_conf->level < ACCESS_LVL_OPER) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001029 appctx->ctx.cli.msg = stats_permission_denied_msg;
1030 appctx->st0 = STAT_CLI_PRINT;
Simon Horman121f3052011-06-15 15:18:46 +09001031 return;
1032 }
1033
Willy Tarreau07115412012-10-29 21:56:59 +01001034 ts = stktable_lookup_key(&px->table, static_table_key);
Simon Horman17bce342011-06-15 15:18:47 +09001035
Willy Tarreaudec98142012-06-06 23:37:08 +02001036 switch (action) {
1037 case STAT_CLI_O_TAB:
1038 if (!ts)
1039 return;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001040 chunk_reset(&trash);
1041 if (!stats_dump_table_head_to_buffer(&trash, si, px, px))
Simon Horman17bce342011-06-15 15:18:47 +09001042 return;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001043 stats_dump_table_entry_to_buffer(&trash, si, px, ts);
Simon Horman121f3052011-06-15 15:18:46 +09001044 return;
Willy Tarreaudec98142012-06-06 23:37:08 +02001045
1046 case STAT_CLI_O_CLR:
1047 if (!ts)
1048 return;
1049 if (ts->ref_cnt) {
1050 /* don't delete an entry which is currently referenced */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001051 appctx->ctx.cli.msg = "Entry currently in use, cannot remove\n";
1052 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaudec98142012-06-06 23:37:08 +02001053 return;
1054 }
1055 stksess_kill(&px->table, ts);
1056 break;
Simon Horman17bce342011-06-15 15:18:47 +09001057
Willy Tarreau654694e2012-06-07 01:03:16 +02001058 case STAT_CLI_O_SET:
Willy Tarreau654694e2012-06-07 01:03:16 +02001059 if (ts)
1060 stktable_touch(&px->table, ts, 1);
1061 else {
Willy Tarreau07115412012-10-29 21:56:59 +01001062 ts = stksess_new(&px->table, static_table_key);
Willy Tarreau654694e2012-06-07 01:03:16 +02001063 if (!ts) {
1064 /* don't delete an entry which is currently referenced */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001065 appctx->ctx.cli.msg = "Unable to allocate a new entry\n";
1066 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau654694e2012-06-07 01:03:16 +02001067 return;
1068 }
1069 stktable_store(&px->table, ts, 1);
1070 }
1071
Willy Tarreau47060b62013-08-01 21:11:42 +02001072 for (cur_arg = 5; *args[cur_arg]; cur_arg += 2) {
1073 if (strncmp(args[cur_arg], "data.", 5) != 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001074 appctx->ctx.cli.msg = "\"data.<type>\" followed by a value expected\n";
1075 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau47060b62013-08-01 21:11:42 +02001076 return;
1077 }
1078
1079 data_type = stktable_get_data_type(args[cur_arg] + 5);
1080 if (data_type < 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001081 appctx->ctx.cli.msg = "Unknown data type\n";
1082 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau47060b62013-08-01 21:11:42 +02001083 return;
1084 }
1085
1086 if (!px->table.data_ofs[data_type]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001087 appctx->ctx.cli.msg = "Data type not stored in this table\n";
1088 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau47060b62013-08-01 21:11:42 +02001089 return;
1090 }
1091
1092 if (!*args[cur_arg+1] || strl2llrc(args[cur_arg+1], strlen(args[cur_arg+1]), &value) != 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001093 appctx->ctx.cli.msg = "Require a valid integer value to store\n";
1094 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau47060b62013-08-01 21:11:42 +02001095 return;
1096 }
1097
1098 ptr = stktable_data_ptr(&px->table, ts, data_type);
1099
1100 switch (stktable_data_types[data_type].std_type) {
1101 case STD_T_SINT:
1102 stktable_data_cast(ptr, std_t_sint) = value;
1103 break;
1104 case STD_T_UINT:
1105 stktable_data_cast(ptr, std_t_uint) = value;
1106 break;
1107 case STD_T_ULL:
1108 stktable_data_cast(ptr, std_t_ull) = value;
1109 break;
1110 case STD_T_FRQP:
1111 /* We set both the current and previous values. That way
1112 * the reported frequency is stable during all the period
1113 * then slowly fades out. This allows external tools to
1114 * push measures without having to update them too often.
1115 */
1116 frqp = &stktable_data_cast(ptr, std_t_frqp);
1117 frqp->curr_tick = now_ms;
1118 frqp->prev_ctr = 0;
1119 frqp->curr_ctr = value;
1120 break;
1121 }
Willy Tarreau654694e2012-06-07 01:03:16 +02001122 }
1123 break;
1124
Willy Tarreaudec98142012-06-06 23:37:08 +02001125 default:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001126 appctx->ctx.cli.msg = "Unknown action\n";
1127 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaudec98142012-06-06 23:37:08 +02001128 break;
Simon Horman121f3052011-06-15 15:18:46 +09001129 }
Simon Horman121f3052011-06-15 15:18:46 +09001130}
1131
Willy Tarreau654694e2012-06-07 01:03:16 +02001132static void stats_sock_table_data_request(struct stream_interface *si, char **args, int action)
Simon Hormand5b9fd92011-06-15 15:18:48 +09001133{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001134 struct appctx *appctx = __objt_appctx(si->end);
1135
Willy Tarreau04b3a192013-04-13 09:41:37 +02001136 if (action != STAT_CLI_O_TAB && action != STAT_CLI_O_CLR) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001137 appctx->ctx.cli.msg = "content-based lookup is only supported with the \"show\" and \"clear\" actions";
1138 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau654694e2012-06-07 01:03:16 +02001139 return;
1140 }
1141
Simon Hormand5b9fd92011-06-15 15:18:48 +09001142 /* condition on stored data value */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001143 appctx->ctx.table.data_type = stktable_get_data_type(args[3] + 5);
1144 if (appctx->ctx.table.data_type < 0) {
1145 appctx->ctx.cli.msg = "Unknown data type\n";
1146 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +09001147 return;
1148 }
1149
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001150 if (!((struct proxy *)appctx->ctx.table.target)->table.data_ofs[appctx->ctx.table.data_type]) {
1151 appctx->ctx.cli.msg = "Data type not stored in this table\n";
1152 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +09001153 return;
1154 }
1155
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001156 appctx->ctx.table.data_op = get_std_op(args[4]);
1157 if (appctx->ctx.table.data_op < 0) {
1158 appctx->ctx.cli.msg = "Require and operator among \"eq\", \"ne\", \"le\", \"ge\", \"lt\", \"gt\"\n";
1159 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +09001160 return;
1161 }
1162
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001163 if (!*args[5] || strl2llrc(args[5], strlen(args[5]), &appctx->ctx.table.value) != 0) {
1164 appctx->ctx.cli.msg = "Require a valid integer value to compare against\n";
1165 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +09001166 return;
1167 }
1168}
1169
Willy Tarreaudec98142012-06-06 23:37:08 +02001170static void stats_sock_table_request(struct stream_interface *si, char **args, int action)
Simon Hormand5b9fd92011-06-15 15:18:48 +09001171{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001172 struct appctx *appctx = __objt_appctx(si->end);
1173
1174 appctx->ctx.table.data_type = -1;
1175 appctx->st2 = STAT_ST_INIT;
1176 appctx->ctx.table.target = NULL;
1177 appctx->ctx.table.proxy = NULL;
1178 appctx->ctx.table.entry = NULL;
1179 appctx->st0 = action;
Simon Hormand5b9fd92011-06-15 15:18:48 +09001180
1181 if (*args[2]) {
Willy Tarreaue2dc1fa2015-05-26 12:08:07 +02001182 appctx->ctx.table.target = proxy_tbl_by_name(args[2]);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001183 if (!appctx->ctx.table.target) {
1184 appctx->ctx.cli.msg = "No such table\n";
1185 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +09001186 return;
1187 }
1188 }
1189 else {
Willy Tarreaudec98142012-06-06 23:37:08 +02001190 if (action != STAT_CLI_O_TAB)
Simon Hormand5b9fd92011-06-15 15:18:48 +09001191 goto err_args;
1192 return;
1193 }
1194
1195 if (strcmp(args[3], "key") == 0)
Willy Tarreaudec98142012-06-06 23:37:08 +02001196 stats_sock_table_key_request(si, args, action);
Simon Hormanc88b8872011-06-15 15:18:49 +09001197 else if (strncmp(args[3], "data.", 5) == 0)
Willy Tarreau654694e2012-06-07 01:03:16 +02001198 stats_sock_table_data_request(si, args, action);
Simon Hormanc88b8872011-06-15 15:18:49 +09001199 else if (*args[3])
Simon Hormand5b9fd92011-06-15 15:18:48 +09001200 goto err_args;
1201
1202 return;
1203
1204err_args:
Willy Tarreaudec98142012-06-06 23:37:08 +02001205 switch (action) {
1206 case STAT_CLI_O_TAB:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001207 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 +02001208 break;
1209 case STAT_CLI_O_CLR:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001210 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 +02001211 break;
1212 default:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001213 appctx->ctx.cli.msg = "Unknown action\n";
Willy Tarreaudec98142012-06-06 23:37:08 +02001214 break;
1215 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001216 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +09001217}
1218
Willy Tarreau532a4502011-09-07 22:37:44 +02001219/* Expects to find a frontend named <arg> and returns it, otherwise displays various
Willy Tarreau87b09662015-04-03 00:22:06 +02001220 * adequate error messages and returns NULL. This function also expects the stream
Willy Tarreau532a4502011-09-07 22:37:44 +02001221 * level to be admin.
1222 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001223static struct proxy *expect_frontend_admin(struct stream *s, struct stream_interface *si, const char *arg)
Willy Tarreau532a4502011-09-07 22:37:44 +02001224{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001225 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau532a4502011-09-07 22:37:44 +02001226 struct proxy *px;
1227
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001228 if (strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001229 appctx->ctx.cli.msg = stats_permission_denied_msg;
1230 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001231 return NULL;
1232 }
1233
1234 if (!*arg) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001235 appctx->ctx.cli.msg = "A frontend name is expected.\n";
1236 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001237 return NULL;
1238 }
1239
Willy Tarreau9e0bb102015-05-26 11:24:42 +02001240 px = proxy_fe_by_name(arg);
Willy Tarreau532a4502011-09-07 22:37:44 +02001241 if (!px) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001242 appctx->ctx.cli.msg = "No such frontend.\n";
1243 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001244 return NULL;
1245 }
1246 return px;
1247}
1248
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001249/* Expects to find a backend and a server in <arg> under the form <backend>/<server>,
1250 * and returns the pointer to the server. Otherwise, display adequate error messages
Willy Tarreau87b09662015-04-03 00:22:06 +02001251 * and returns NULL. This function also expects the stream level to be admin. Note:
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001252 * the <arg> is modified to remove the '/'.
1253 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001254static struct server *expect_server_admin(struct stream *s, struct stream_interface *si, char *arg)
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001255{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001256 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001257 struct proxy *px;
1258 struct server *sv;
1259 char *line;
1260
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001261 if (strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001262 appctx->ctx.cli.msg = stats_permission_denied_msg;
1263 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001264 return NULL;
1265 }
1266
1267 /* split "backend/server" and make <line> point to server */
1268 for (line = arg; *line; line++)
1269 if (*line == '/') {
1270 *line++ = '\0';
1271 break;
1272 }
1273
1274 if (!*line || !*arg) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001275 appctx->ctx.cli.msg = "Require 'backend/server'.\n";
1276 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001277 return NULL;
1278 }
1279
1280 if (!get_backend_server(arg, line, &px, &sv)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001281 appctx->ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
1282 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001283 return NULL;
1284 }
1285
1286 if (px->state == PR_STSTOPPED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001287 appctx->ctx.cli.msg = "Proxy is disabled.\n";
1288 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001289 return NULL;
1290 }
1291
1292 return sv;
1293}
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02001294
1295/* This function is used with TLS ticket keys management. It permits to browse
1296 * each reference. The variable <getnext> must contain the current node,
1297 * <end> point to the root node.
1298 */
1299#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
1300static inline
1301struct tls_keys_ref *tlskeys_list_get_next(struct tls_keys_ref *getnext, struct list *end)
1302{
1303 struct tls_keys_ref *ref = getnext;
1304
1305 while (1) {
1306
1307 /* Get next list entry. */
1308 ref = LIST_NEXT(&ref->list, struct tls_keys_ref *, list);
1309
1310 /* If the entry is the last of the list, return NULL. */
1311 if (&ref->list == end)
1312 return NULL;
1313
1314 return ref;
1315 }
1316}
1317
1318static inline
1319struct tls_keys_ref *tlskeys_ref_lookup_ref(const char *reference)
1320{
1321 int id;
1322 char *error;
1323
1324 /* If the reference starts by a '#', this is numeric id. */
1325 if (reference[0] == '#') {
1326 /* Try to convert the numeric id. If the conversion fails, the lookup fails. */
1327 id = strtol(reference + 1, &error, 10);
1328 if (*error != '\0')
1329 return NULL;
1330
1331 /* Perform the unique id lookup. */
1332 return tlskeys_ref_lookupid(id);
1333 }
1334
1335 /* Perform the string lookup. */
1336 return tlskeys_ref_lookup(reference);
1337}
1338#endif
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001339
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001340/* This function is used with map and acl management. It permits to browse
1341 * each reference. The variable <getnext> must contain the current node,
1342 * <end> point to the root node and the <flags> permit to filter required
1343 * nodes.
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001344 */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001345static inline
1346struct pat_ref *pat_list_get_next(struct pat_ref *getnext, struct list *end,
1347 unsigned int flags)
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001348{
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001349 struct pat_ref *ref = getnext;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001350
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001351 while (1) {
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001352
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001353 /* Get next list entry. */
1354 ref = LIST_NEXT(&ref->list, struct pat_ref *, list);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001355
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001356 /* If the entry is the last of the list, return NULL. */
1357 if (&ref->list == end)
1358 return NULL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001359
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001360 /* If the entry match the flag, return it. */
1361 if (ref->flags & flags)
1362 return ref;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001363 }
1364}
1365
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001366static inline
1367struct pat_ref *pat_ref_lookup_ref(const char *reference)
1368{
1369 int id;
1370 char *error;
1371
1372 /* If the reference starts by a '#', this is numeric id. */
1373 if (reference[0] == '#') {
1374 /* Try to convert the numeric id. If the conversion fails, the lookup fails. */
1375 id = strtol(reference + 1, &error, 10);
1376 if (*error != '\0')
1377 return NULL;
1378
1379 /* Perform the unique id lookup. */
1380 return pat_ref_lookupid(id);
1381 }
1382
1383 /* Perform the string lookup. */
1384 return pat_ref_lookup(reference);
1385}
1386
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001387/* This function is used with map and acl management. It permits to browse
1388 * each reference.
1389 */
1390static inline
1391struct pattern_expr *pat_expr_get_next(struct pattern_expr *getnext, struct list *end)
1392{
1393 struct pattern_expr *expr;
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01001394 expr = LIST_NEXT(&getnext->list, struct pattern_expr *, list);
1395 if (&expr->list == end)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001396 return NULL;
1397 return expr;
1398}
1399
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001400/* Processes the stats interpreter on the statistics socket. This function is
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001401 * called from an applet running in a stream interface. The function returns 1
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001402 * if the request was understood, otherwise zero. It sets appctx->st0 to a value
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02001403 * designating the function which will have to process the request, which can
1404 * also be the print function to display the return message set into cli.msg.
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001405 */
Simon Horman9bd2c732011-06-15 15:18:44 +09001406static int stats_sock_parse_request(struct stream_interface *si, char *line)
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001407{
Willy Tarreau87b09662015-04-03 00:22:06 +02001408 struct stream *s = si_strm(si);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001409 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001410 char *args[MAX_STATS_ARGS + 1];
1411 int arg;
Thierry FOURNIER48bcfda2013-12-10 18:54:58 +01001412 int i, j;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001413
1414 while (isspace((unsigned char)*line))
1415 line++;
1416
1417 arg = 0;
1418 args[arg] = line;
1419
1420 while (*line && arg < MAX_STATS_ARGS) {
Thierry FOURNIER48bcfda2013-12-10 18:54:58 +01001421 if (*line == '\\') {
1422 line++;
1423 if (*line == '\0')
1424 break;
1425 }
1426 else if (isspace((unsigned char)*line)) {
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001427 *line++ = '\0';
1428
1429 while (isspace((unsigned char)*line))
1430 line++;
1431
1432 args[++arg] = line;
1433 continue;
1434 }
1435
1436 line++;
1437 }
1438
1439 while (++arg <= MAX_STATS_ARGS)
1440 args[arg] = line;
1441
Thierry FOURNIER48bcfda2013-12-10 18:54:58 +01001442 /* remove \ */
1443 arg = 0;
1444 while (*args[arg] != '\0') {
1445 j = 0;
1446 for (i=0; args[arg][i] != '\0'; i++) {
1447 if (args[arg][i] == '\\')
1448 continue;
1449 args[arg][j] = args[arg][i];
1450 j++;
1451 }
1452 args[arg][j] = '\0';
1453 arg++;
1454 }
1455
Willy Tarreau6bcb95d2015-05-04 18:07:56 +02001456 appctx->ctx.stats.scope_str = 0;
1457 appctx->ctx.stats.scope_len = 0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001458 appctx->ctx.stats.flags = 0;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001459 if (strcmp(args[0], "show") == 0) {
Baptiste Assmann9b6857e2015-09-18 14:49:12 +02001460 if (strcmp(args[1], "backend") == 0) {
1461 appctx->st2 = STAT_ST_INIT;
1462 appctx->st0 = STAT_CLI_O_BACKEND;
1463 }
Willy Tarreauae795722016-02-16 11:27:28 +01001464 else if (strcmp(args[1], "env") == 0) {
1465 extern char **environ;
1466
1467 if (strm_li(s)->bind_conf->level < ACCESS_LVL_OPER) {
1468 appctx->ctx.cli.msg = stats_permission_denied_msg;
1469 appctx->st0 = STAT_CLI_PRINT;
1470 return 1;
1471 }
1472 appctx->ctx.env.var = environ;
1473 appctx->st2 = STAT_ST_INIT;
1474 appctx->st0 = STAT_CLI_O_ENV; // stats_dump_env_to_buffer
1475
1476 if (*args[2]) {
1477 int len = strlen(args[2]);
1478
1479 for (; *appctx->ctx.env.var; appctx->ctx.env.var++) {
1480 if (strncmp(*appctx->ctx.env.var, args[2], len) == 0 &&
1481 (*appctx->ctx.env.var)[len] == '=')
1482 break;
1483 }
1484 if (!*appctx->ctx.env.var) {
1485 appctx->ctx.cli.msg = "Variable not found\n";
1486 appctx->st0 = STAT_CLI_PRINT;
1487 return 1;
1488 }
1489 appctx->st2 = STAT_ST_END;
1490 }
1491 }
1492 else if (strcmp(args[1], "stat") == 0) {
Baptiste Assmann3863f972015-05-17 00:33:24 +02001493 if (strcmp(args[2], "resolvers") == 0) {
1494 struct dns_resolvers *presolvers;
1495
Andrew Hayworth68d05342015-10-02 20:33:01 +00001496 if (*args[3]) {
1497 appctx->ctx.resolvers.ptr = NULL;
1498 list_for_each_entry(presolvers, &dns_resolvers, list) {
1499 if (strcmp(presolvers->id, args[3]) == 0) {
1500 appctx->ctx.resolvers.ptr = presolvers;
1501 break;
1502 }
Baptiste Assmann3863f972015-05-17 00:33:24 +02001503 }
Andrew Hayworth68d05342015-10-02 20:33:01 +00001504 if (appctx->ctx.resolvers.ptr == NULL) {
1505 appctx->ctx.cli.msg = "Can't find that resolvers section\n";
1506 appctx->st0 = STAT_CLI_PRINT;
1507 return 1;
1508 }
Baptiste Assmann3863f972015-05-17 00:33:24 +02001509 }
1510
1511 appctx->st2 = STAT_ST_INIT;
1512 appctx->st0 = STAT_CLI_O_RESOLVERS;
1513 return 1;
1514 }
1515 else if (*args[2] && *args[3] && *args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001516 appctx->ctx.stats.flags |= STAT_BOUND;
1517 appctx->ctx.stats.iid = atoi(args[2]);
1518 appctx->ctx.stats.type = atoi(args[3]);
1519 appctx->ctx.stats.sid = atoi(args[4]);
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001520 }
1521
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001522 appctx->st2 = STAT_ST_INIT;
1523 appctx->st0 = STAT_CLI_O_STAT; // stats_dump_stat_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001524 }
1525 else if (strcmp(args[1], "info") == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001526 appctx->st2 = STAT_ST_INIT;
1527 appctx->st0 = STAT_CLI_O_INFO; // stats_dump_info_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001528 }
Baptiste Assmann28289462015-07-03 08:01:20 +02001529 else if (strcmp(args[1], "servers") == 0 && strcmp(args[2], "state") == 0) {
1530 appctx->ctx.server_state.backend = NULL;
1531
1532 /* check if a backend name has been provided */
1533 if (*args[3]) {
1534 /* read server state from local file */
1535 appctx->ctx.server_state.backend = proxy_be_by_name(args[3]);
1536
1537 if (appctx->ctx.server_state.backend == NULL) {
1538 appctx->ctx.cli.msg = "Can't find backend.\n";
1539 appctx->st0 = STAT_CLI_PRINT;
1540 return 1;
1541 }
1542 }
1543 appctx->st2 = STAT_ST_INIT;
1544 appctx->st0 = STAT_CLI_O_SERVERS_STATE; // stats_dump_servers_state_to_buffer
1545 return 1;
1546 }
Willy Tarreau12833bb2014-01-28 16:49:56 +01001547 else if (strcmp(args[1], "pools") == 0) {
1548 appctx->st2 = STAT_ST_INIT;
1549 appctx->st0 = STAT_CLI_O_POOLS; // stats_dump_pools_to_buffer
1550 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001551 else if (strcmp(args[1], "sess") == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001552 appctx->st2 = STAT_ST_INIT;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001553 if (strm_li(s)->bind_conf->level < ACCESS_LVL_OPER) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001554 appctx->ctx.cli.msg = stats_permission_denied_msg;
1555 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +02001556 return 1;
1557 }
Willy Tarreau76153662012-11-26 01:16:39 +01001558 if (*args[2] && strcmp(args[2], "all") == 0)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001559 appctx->ctx.sess.target = (void *)-1;
Willy Tarreau76153662012-11-26 01:16:39 +01001560 else if (*args[2])
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001561 appctx->ctx.sess.target = (void *)strtoul(args[2], NULL, 0);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01001562 else
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001563 appctx->ctx.sess.target = NULL;
Willy Tarreau87b09662015-04-03 00:22:06 +02001564 appctx->ctx.sess.section = 0; /* start with stream status */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001565 appctx->ctx.sess.pos = 0;
1566 appctx->st0 = STAT_CLI_O_SESS; // stats_dump_sess_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001567 }
1568 else if (strcmp(args[1], "errors") == 0) {
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001569 if (strm_li(s)->bind_conf->level < ACCESS_LVL_OPER) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001570 appctx->ctx.cli.msg = stats_permission_denied_msg;
1571 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +02001572 return 1;
1573 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001574 if (*args[2])
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001575 appctx->ctx.errors.iid = atoi(args[2]);
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001576 else
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001577 appctx->ctx.errors.iid = -1;
1578 appctx->ctx.errors.px = NULL;
1579 appctx->st2 = STAT_ST_INIT;
1580 appctx->st0 = STAT_CLI_O_ERR; // stats_dump_errors_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001581 }
Willy Tarreau69f58c82010-07-12 17:55:33 +02001582 else if (strcmp(args[1], "table") == 0) {
Willy Tarreaudec98142012-06-06 23:37:08 +02001583 stats_sock_table_request(si, args, STAT_CLI_O_TAB);
Willy Tarreau69f58c82010-07-12 17:55:33 +02001584 }
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02001585 else if (strcmp(args[1], "tls-keys") == 0) {
1586#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
1587 appctx->st2 = STAT_ST_INIT;
1588 appctx->st0 = STAT_CLI_O_TLSK;
1589#else
1590 appctx->ctx.cli.msg = "HAProxy was compiled against a version of OpenSSL "
1591 "that doesn't support specifying TLS ticket keys\n";
1592 appctx->st0 = STAT_CLI_PRINT;
1593#endif
1594 return 1;
1595 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001596 else if (strcmp(args[1], "map") == 0 ||
1597 strcmp(args[1], "acl") == 0) {
1598
1599 /* Set ACL or MAP flags. */
1600 if (args[1][0] == 'm')
1601 appctx->ctx.map.display_flags = PAT_REF_MAP;
1602 else
1603 appctx->ctx.map.display_flags = PAT_REF_ACL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001604
1605 /* no parameter: display all map avalaible */
1606 if (!*args[2]) {
1607 appctx->st2 = STAT_ST_INIT;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001608 appctx->st0 = STAT_CLI_O_PATS;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001609 return 1;
1610 }
1611
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001612 /* lookup into the refs and check the map flag */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001613 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001614 if (!appctx->ctx.map.ref ||
1615 !(appctx->ctx.map.ref->flags & appctx->ctx.map.display_flags)) {
1616 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001617 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001618 else
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001619 appctx->ctx.cli.msg = "Unknown ACL identifier. Please use #<id> or <file>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001620 appctx->st0 = STAT_CLI_PRINT;
1621 return 1;
1622 }
1623 appctx->st2 = STAT_ST_INIT;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001624 appctx->st0 = STAT_CLI_O_PAT;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001625 }
Aman Guptaceafb4a2012-04-02 18:57:54 -07001626 else { /* neither "stat" nor "info" nor "sess" nor "errors" nor "table" */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001627 return 0;
1628 }
1629 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001630 else if (strcmp(args[0], "clear") == 0) {
1631 if (strcmp(args[1], "counters") == 0) {
1632 struct proxy *px;
1633 struct server *sv;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001634 struct listener *li;
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001635 int clrall = 0;
1636
1637 if (strcmp(args[2], "all") == 0)
1638 clrall = 1;
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001639
Willy Tarreau6162db22009-10-10 17:13:00 +02001640 /* check permissions */
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001641 if (strm_li(s)->bind_conf->level < ACCESS_LVL_OPER ||
1642 (clrall && strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001643 appctx->ctx.cli.msg = stats_permission_denied_msg;
1644 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +02001645 return 1;
1646 }
1647
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001648 for (px = proxy; px; px = px->next) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001649 if (clrall) {
1650 memset(&px->be_counters, 0, sizeof(px->be_counters));
1651 memset(&px->fe_counters, 0, sizeof(px->fe_counters));
1652 }
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001653 else {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001654 px->be_counters.conn_max = 0;
1655 px->be_counters.p.http.rps_max = 0;
1656 px->be_counters.sps_max = 0;
1657 px->be_counters.cps_max = 0;
1658 px->be_counters.nbpend_max = 0;
1659
1660 px->fe_counters.conn_max = 0;
1661 px->fe_counters.p.http.rps_max = 0;
1662 px->fe_counters.sps_max = 0;
1663 px->fe_counters.cps_max = 0;
1664 px->fe_counters.nbpend_max = 0;
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001665 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001666
1667 for (sv = px->srv; sv; sv = sv->next)
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001668 if (clrall)
1669 memset(&sv->counters, 0, sizeof(sv->counters));
1670 else {
1671 sv->counters.cur_sess_max = 0;
1672 sv->counters.nbpend_max = 0;
1673 sv->counters.sps_max = 0;
1674 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001675
Willy Tarreaua7944ad2012-09-26 21:03:11 +02001676 list_for_each_entry(li, &px->conf.listeners, by_fe)
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001677 if (li->counters) {
1678 if (clrall)
1679 memset(li->counters, 0, sizeof(*li->counters));
1680 else
1681 li->counters->conn_max = 0;
1682 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001683 }
1684
Willy Tarreau81c25d02011-09-07 15:17:21 +02001685 global.cps_max = 0;
Willy Tarreau93e7c002013-10-07 18:51:07 +02001686 global.sps_max = 0;
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001687 return 1;
1688 }
Willy Tarreau88ee3972010-07-13 13:48:00 +02001689 else if (strcmp(args[1], "table") == 0) {
Willy Tarreaudec98142012-06-06 23:37:08 +02001690 stats_sock_table_request(si, args, STAT_CLI_O_CLR);
Willy Tarreau88ee3972010-07-13 13:48:00 +02001691 /* end of processing */
1692 return 1;
1693 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001694 else if (strcmp(args[1], "map") == 0 || strcmp(args[1], "acl") == 0) {
1695 /* Set ACL or MAP flags. */
1696 if (args[1][0] == 'm')
1697 appctx->ctx.map.display_flags = PAT_REF_MAP;
1698 else
1699 appctx->ctx.map.display_flags = PAT_REF_ACL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001700
1701 /* no parameter */
1702 if (!*args[2]) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001703 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
1704 appctx->ctx.cli.msg = "Missing map identifier.\n";
1705 else
1706 appctx->ctx.cli.msg = "Missing ACL identifier.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001707 appctx->st0 = STAT_CLI_PRINT;
1708 return 1;
1709 }
1710
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001711 /* lookup into the refs and check the map flag */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001712 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001713 if (!appctx->ctx.map.ref ||
1714 !(appctx->ctx.map.ref->flags & appctx->ctx.map.display_flags)) {
1715 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001716 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001717 else
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001718 appctx->ctx.cli.msg = "Unknown ACL identifier. Please use #<id> or <file>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001719 appctx->st0 = STAT_CLI_PRINT;
1720 return 1;
1721 }
1722
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001723 /* Clear all. */
1724 pat_ref_prune(appctx->ctx.map.ref);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001725
1726 /* return response */
Thierry FOURNIER07e78c52014-12-18 15:28:01 +01001727 appctx->st0 = STAT_CLI_PROMPT;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001728 return 1;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001729 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001730 else {
Willy Tarreau88ee3972010-07-13 13:48:00 +02001731 /* unknown "clear" argument */
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001732 return 0;
1733 }
1734 }
Willy Tarreau38338fa2009-10-10 18:37:29 +02001735 else if (strcmp(args[0], "get") == 0) {
1736 if (strcmp(args[1], "weight") == 0) {
1737 struct proxy *px;
1738 struct server *sv;
1739
1740 /* split "backend/server" and make <line> point to server */
1741 for (line = args[2]; *line; line++)
1742 if (*line == '/') {
1743 *line++ = '\0';
1744 break;
1745 }
1746
1747 if (!*line) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001748 appctx->ctx.cli.msg = "Require 'backend/server'.\n";
1749 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau38338fa2009-10-10 18:37:29 +02001750 return 1;
1751 }
1752
1753 if (!get_backend_server(args[2], line, &px, &sv)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001754 appctx->ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
1755 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau38338fa2009-10-10 18:37:29 +02001756 return 1;
1757 }
1758
1759 /* return server's effective weight at the moment */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001760 snprintf(trash.str, trash.size, "%d (initial %d)\n", sv->uweight, sv->iweight);
Willy Tarreaubc18da12015-03-13 14:00:47 +01001761 if (bi_putstr(si_ic(si), trash.str) == -1)
Willy Tarreaufe127932015-04-21 19:23:39 +02001762 si_applet_cant_put(si);
Willy Tarreaubc18da12015-03-13 14:00:47 +01001763
Willy Tarreau38338fa2009-10-10 18:37:29 +02001764 return 1;
1765 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001766 else if (strcmp(args[1], "map") == 0 || strcmp(args[1], "acl") == 0) {
1767 /* Set flags. */
1768 if (args[1][0] == 'm')
1769 appctx->ctx.map.display_flags = PAT_REF_MAP;
1770 else
1771 appctx->ctx.map.display_flags = PAT_REF_ACL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001772
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001773 /* No parameter. */
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001774 if (!*args[2] || !*args[3]) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001775 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
1776 appctx->ctx.cli.msg = "Missing map identifier and/or key.\n";
1777 else
1778 appctx->ctx.cli.msg = "Missing ACL identifier and/or key.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001779 appctx->st0 = STAT_CLI_PRINT;
1780 return 1;
1781 }
1782
1783 /* lookup into the maps */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001784 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001785 if (!appctx->ctx.map.ref) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001786 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001787 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001788 else
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001789 appctx->ctx.cli.msg = "Unknown ACL identifier. Please use #<id> or <file>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001790 appctx->st0 = STAT_CLI_PRINT;
1791 return 1;
1792 }
1793
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001794 /* copy input string. The string must be allocated because
1795 * it may be used over multiple iterations. It's released
1796 * at the end and upon abort anyway.
1797 */
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001798 appctx->ctx.map.chunk.len = strlen(args[3]);
1799 appctx->ctx.map.chunk.size = appctx->ctx.map.chunk.len + 1;
1800 appctx->ctx.map.chunk.str = strdup(args[3]);
1801 if (!appctx->ctx.map.chunk.str) {
1802 appctx->ctx.cli.msg = "Out of memory error.\n";
1803 appctx->st0 = STAT_CLI_PRINT;
1804 return 1;
1805 }
1806
1807 /* prepare response */
1808 appctx->st2 = STAT_ST_INIT;
1809 appctx->st0 = STAT_CLI_O_MLOOK;
1810 }
Willy Tarreau38338fa2009-10-10 18:37:29 +02001811 else { /* not "get weight" */
1812 return 0;
1813 }
1814 }
Willy Tarreau4483d432009-10-10 19:30:08 +02001815 else if (strcmp(args[0], "set") == 0) {
1816 if (strcmp(args[1], "weight") == 0) {
Willy Tarreau4483d432009-10-10 19:30:08 +02001817 struct server *sv;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001818 const char *warning;
Willy Tarreau4483d432009-10-10 19:30:08 +02001819
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001820 sv = expect_server_admin(s, si, args[2]);
1821 if (!sv)
Willy Tarreau4483d432009-10-10 19:30:08 +02001822 return 1;
Willy Tarreau4483d432009-10-10 19:30:08 +02001823
Simon Horman7d09b9a2013-02-12 10:45:51 +09001824 warning = server_parse_weight_change_request(sv, args[3]);
1825 if (warning) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001826 appctx->ctx.cli.msg = warning;
1827 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +02001828 }
Willy Tarreau4483d432009-10-10 19:30:08 +02001829 return 1;
1830 }
Willy Tarreau2a4b70f2014-05-22 18:42:35 +02001831 else if (strcmp(args[1], "server") == 0) {
1832 struct server *sv;
1833 const char *warning;
1834
1835 sv = expect_server_admin(s, si, args[2]);
1836 if (!sv)
1837 return 1;
1838
1839 if (strcmp(args[3], "weight") == 0) {
1840 warning = server_parse_weight_change_request(sv, args[4]);
1841 if (warning) {
1842 appctx->ctx.cli.msg = warning;
1843 appctx->st0 = STAT_CLI_PRINT;
1844 }
1845 }
1846 else if (strcmp(args[3], "state") == 0) {
1847 if (strcmp(args[4], "ready") == 0)
1848 srv_adm_set_ready(sv);
1849 else if (strcmp(args[4], "drain") == 0)
1850 srv_adm_set_drain(sv);
1851 else if (strcmp(args[4], "maint") == 0)
1852 srv_adm_set_maint(sv);
1853 else {
1854 appctx->ctx.cli.msg = "'set server <srv> state' expects 'ready', 'drain' and 'maint'.\n";
1855 appctx->st0 = STAT_CLI_PRINT;
1856 }
1857 }
1858 else if (strcmp(args[3], "health") == 0) {
1859 if (sv->track) {
1860 appctx->ctx.cli.msg = "cannot change health on a tracking server.\n";
1861 appctx->st0 = STAT_CLI_PRINT;
1862 }
1863 else if (strcmp(args[4], "up") == 0) {
1864 sv->check.health = sv->check.rise + sv->check.fall - 1;
1865 srv_set_running(sv, "changed from CLI");
1866 }
1867 else if (strcmp(args[4], "stopping") == 0) {
1868 sv->check.health = sv->check.rise + sv->check.fall - 1;
1869 srv_set_stopping(sv, "changed from CLI");
1870 }
1871 else if (strcmp(args[4], "down") == 0) {
1872 sv->check.health = 0;
1873 srv_set_stopped(sv, "changed from CLI");
1874 }
1875 else {
1876 appctx->ctx.cli.msg = "'set server <srv> health' expects 'up', 'stopping', or 'down'.\n";
1877 appctx->st0 = STAT_CLI_PRINT;
1878 }
1879 }
1880 else if (strcmp(args[3], "agent") == 0) {
1881 if (!(sv->agent.state & CHK_ST_ENABLED)) {
1882 appctx->ctx.cli.msg = "agent checks are not enabled on this server.\n";
1883 appctx->st0 = STAT_CLI_PRINT;
1884 }
1885 else if (strcmp(args[4], "up") == 0) {
1886 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
1887 srv_set_running(sv, "changed from CLI");
1888 }
1889 else if (strcmp(args[4], "down") == 0) {
1890 sv->agent.health = 0;
1891 srv_set_stopped(sv, "changed from CLI");
1892 }
1893 else {
1894 appctx->ctx.cli.msg = "'set server <srv> agent' expects 'up' or 'down'.\n";
1895 appctx->st0 = STAT_CLI_PRINT;
1896 }
1897 }
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001898 else if (strcmp(args[3], "addr") == 0) {
Thierry Fournier09a91782016-02-24 08:25:39 +01001899 warning = server_parse_addr_change_request(sv, args[4], "stats command");
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001900 if (warning) {
1901 appctx->ctx.cli.msg = warning;
1902 appctx->st0 = STAT_CLI_PRINT;
1903 }
1904 }
Willy Tarreau2a4b70f2014-05-22 18:42:35 +02001905 else {
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001906 appctx->ctx.cli.msg = "'set server <srv>' only supports 'agent', 'health', 'state', 'weight' and 'addr'.\n";
Willy Tarreau2a4b70f2014-05-22 18:42:35 +02001907 appctx->st0 = STAT_CLI_PRINT;
1908 }
1909 return 1;
1910 }
Willy Tarreau7aabd112010-01-26 10:59:06 +01001911 else if (strcmp(args[1], "timeout") == 0) {
1912 if (strcmp(args[2], "cli") == 0) {
1913 unsigned timeout;
1914 const char *res;
1915
1916 if (!*args[3]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001917 appctx->ctx.cli.msg = "Expects an integer value.\n";
1918 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001919 return 1;
1920 }
1921
1922 res = parse_time_err(args[3], &timeout, TIME_UNIT_S);
1923 if (res || timeout < 1) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001924 appctx->ctx.cli.msg = "Invalid timeout value.\n";
1925 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001926 return 1;
1927 }
1928
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001929 s->req.rto = s->res.wto = 1 + MS_TO_TICKS(timeout*1000);
Willy Tarreau7aabd112010-01-26 10:59:06 +01001930 return 1;
1931 }
1932 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001933 appctx->ctx.cli.msg = "'set timeout' only supports 'cli'.\n";
1934 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001935 return 1;
1936 }
1937 }
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001938 else if (strcmp(args[1], "maxconn") == 0) {
1939 if (strcmp(args[2], "frontend") == 0) {
1940 struct proxy *px;
1941 struct listener *l;
1942 int v;
1943
Willy Tarreau532a4502011-09-07 22:37:44 +02001944 px = expect_frontend_admin(s, si, args[3]);
1945 if (!px)
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001946 return 1;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001947
1948 if (!*args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001949 appctx->ctx.cli.msg = "Integer value expected.\n";
1950 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001951 return 1;
1952 }
1953
1954 v = atoi(args[4]);
Willy Tarreau3c7a79d2012-09-26 21:07:15 +02001955 if (v < 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001956 appctx->ctx.cli.msg = "Value out of range.\n";
1957 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001958 return 1;
1959 }
1960
1961 /* OK, the value is fine, so we assign it to the proxy and to all of
1962 * its listeners. The blocked ones will be dequeued.
1963 */
1964 px->maxconn = v;
Willy Tarreaua7944ad2012-09-26 21:03:11 +02001965 list_for_each_entry(l, &px->conf.listeners, by_fe) {
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001966 l->maxconn = v;
1967 if (l->state == LI_FULL)
1968 resume_listener(l);
1969 }
1970
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001971 if (px->maxconn > px->feconn && !LIST_ISEMPTY(&strm_fe(s)->listener_queue))
1972 dequeue_all_listeners(&strm_fe(s)->listener_queue);
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001973
1974 return 1;
1975 }
Andrew Hayworthedb93a72015-10-27 21:46:25 +00001976 else if (strcmp(args[2], "server") == 0) {
1977 struct server *sv;
1978 int v;
1979
1980 sv = expect_server_admin(s, si, args[3]);
1981 if (!sv)
1982 return 1;
1983
1984 if (!*args[4]) {
1985 appctx->ctx.cli.msg = "Integer value expected.\n";
1986 appctx->st0 = STAT_CLI_PRINT;
1987 return 1;
1988 }
1989
1990 v = atoi(args[4]);
1991 if (v < 0) {
1992 appctx->ctx.cli.msg = "Value out of range.\n";
1993 appctx->st0 = STAT_CLI_PRINT;
1994 return 1;
1995 }
1996
1997 if (sv->maxconn == sv->minconn) { // static maxconn
1998 sv->maxconn = sv->minconn = v;
1999 } else { // dynamic maxconn
2000 sv->maxconn = v;
2001 }
2002
2003 if (may_dequeue_tasks(sv, sv->proxy))
2004 process_srv_queue(sv);
2005
2006 return 1;
2007 }
Willy Tarreau91886b62011-09-07 14:38:31 +02002008 else if (strcmp(args[2], "global") == 0) {
2009 int v;
2010
Willy Tarreaud0d8da92015-04-04 02:10:38 +02002011 if (strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002012 appctx->ctx.cli.msg = stats_permission_denied_msg;
2013 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau91886b62011-09-07 14:38:31 +02002014 return 1;
2015 }
2016
2017 if (!*args[3]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002018 appctx->ctx.cli.msg = "Expects an integer value.\n";
2019 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau91886b62011-09-07 14:38:31 +02002020 return 1;
2021 }
2022
2023 v = atoi(args[3]);
2024 if (v > global.hardmaxconn) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002025 appctx->ctx.cli.msg = "Value out of range.\n";
2026 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau91886b62011-09-07 14:38:31 +02002027 return 1;
2028 }
2029
2030 /* check for unlimited values */
2031 if (v <= 0)
2032 v = global.hardmaxconn;
2033
2034 global.maxconn = v;
2035
2036 /* Dequeues all of the listeners waiting for a resource */
2037 if (!LIST_ISEMPTY(&global_listener_queue))
2038 dequeue_all_listeners(&global_listener_queue);
2039
2040 return 1;
2041 }
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02002042 else {
Andrew Hayworthedb93a72015-10-27 21:46:25 +00002043 appctx->ctx.cli.msg = "'set maxconn' only supports 'frontend', 'server', and 'global'.\n";
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002044 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02002045 return 1;
2046 }
2047 }
Willy Tarreauf5b22872011-09-07 16:13:44 +02002048 else if (strcmp(args[1], "rate-limit") == 0) {
2049 if (strcmp(args[2], "connections") == 0) {
2050 if (strcmp(args[3], "global") == 0) {
2051 int v;
2052
Willy Tarreaud0d8da92015-04-04 02:10:38 +02002053 if (strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002054 appctx->ctx.cli.msg = stats_permission_denied_msg;
2055 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02002056 return 1;
2057 }
2058
2059 if (!*args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002060 appctx->ctx.cli.msg = "Expects an integer value.\n";
2061 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02002062 return 1;
2063 }
2064
2065 v = atoi(args[4]);
2066 if (v < 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002067 appctx->ctx.cli.msg = "Value out of range.\n";
2068 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02002069 return 1;
2070 }
2071
2072 global.cps_lim = v;
2073
2074 /* Dequeues all of the listeners waiting for a resource */
2075 if (!LIST_ISEMPTY(&global_listener_queue))
2076 dequeue_all_listeners(&global_listener_queue);
2077
2078 return 1;
2079 }
2080 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002081 appctx->ctx.cli.msg = "'set rate-limit connections' only supports 'global'.\n";
2082 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02002083 return 1;
2084 }
2085 }
Willy Tarreau93e7c002013-10-07 18:51:07 +02002086 else if (strcmp(args[2], "sessions") == 0) {
2087 if (strcmp(args[3], "global") == 0) {
2088 int v;
2089
Willy Tarreaud0d8da92015-04-04 02:10:38 +02002090 if (strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau93e7c002013-10-07 18:51:07 +02002091 appctx->ctx.cli.msg = stats_permission_denied_msg;
2092 appctx->st0 = STAT_CLI_PRINT;
2093 return 1;
2094 }
2095
2096 if (!*args[4]) {
2097 appctx->ctx.cli.msg = "Expects an integer value.\n";
2098 appctx->st0 = STAT_CLI_PRINT;
2099 return 1;
2100 }
2101
2102 v = atoi(args[4]);
2103 if (v < 0) {
2104 appctx->ctx.cli.msg = "Value out of range.\n";
2105 appctx->st0 = STAT_CLI_PRINT;
2106 return 1;
2107 }
2108
2109 global.sps_lim = v;
2110
2111 /* Dequeues all of the listeners waiting for a resource */
2112 if (!LIST_ISEMPTY(&global_listener_queue))
2113 dequeue_all_listeners(&global_listener_queue);
2114
2115 return 1;
2116 }
2117 else {
2118 appctx->ctx.cli.msg = "'set rate-limit sessions' only supports 'global'.\n";
2119 appctx->st0 = STAT_CLI_PRINT;
2120 return 1;
2121 }
2122 }
Willy Tarreaue43d5322013-10-07 20:01:52 +02002123#ifdef USE_OPENSSL
2124 else if (strcmp(args[2], "ssl-sessions") == 0) {
2125 if (strcmp(args[3], "global") == 0) {
2126 int v;
2127
Willy Tarreaud0d8da92015-04-04 02:10:38 +02002128 if (strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreaue43d5322013-10-07 20:01:52 +02002129 appctx->ctx.cli.msg = stats_permission_denied_msg;
2130 appctx->st0 = STAT_CLI_PRINT;
2131 return 1;
2132 }
2133
2134 if (!*args[4]) {
2135 appctx->ctx.cli.msg = "Expects an integer value.\n";
2136 appctx->st0 = STAT_CLI_PRINT;
2137 return 1;
2138 }
2139
2140 v = atoi(args[4]);
2141 if (v < 0) {
2142 appctx->ctx.cli.msg = "Value out of range.\n";
2143 appctx->st0 = STAT_CLI_PRINT;
2144 return 1;
2145 }
2146
2147 global.ssl_lim = v;
2148
2149 /* Dequeues all of the listeners waiting for a resource */
2150 if (!LIST_ISEMPTY(&global_listener_queue))
2151 dequeue_all_listeners(&global_listener_queue);
2152
2153 return 1;
2154 }
2155 else {
2156 appctx->ctx.cli.msg = "'set rate-limit ssl-sessions' only supports 'global'.\n";
2157 appctx->st0 = STAT_CLI_PRINT;
2158 return 1;
2159 }
2160 }
2161#endif
William Lallemandd85f9172012-11-09 17:05:39 +01002162 else if (strcmp(args[2], "http-compression") == 0) {
2163 if (strcmp(args[3], "global") == 0) {
2164 int v;
2165
Willy Tarreaua1c2b2c2015-11-26 18:32:39 +01002166 if (strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN) {
2167 appctx->ctx.cli.msg = stats_permission_denied_msg;
2168 appctx->st0 = STAT_CLI_PRINT;
2169 return 1;
2170 }
2171
Willy Tarreau85d47f92012-11-21 00:29:50 +01002172 if (!*args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002173 appctx->ctx.cli.msg = "Expects a maximum input byte rate in kB/s.\n";
2174 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau85d47f92012-11-21 00:29:50 +01002175 return 1;
2176 }
2177
William Lallemandd85f9172012-11-09 17:05:39 +01002178 v = atoi(args[4]);
2179 global.comp_rate_lim = v * 1024; /* Kilo to bytes. */
2180 }
2181 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002182 appctx->ctx.cli.msg = "'set rate-limit http-compression' only supports 'global'.\n";
2183 appctx->st0 = STAT_CLI_PRINT;
William Lallemandd85f9172012-11-09 17:05:39 +01002184 return 1;
2185 }
2186 }
Willy Tarreauf5b22872011-09-07 16:13:44 +02002187 else {
Willy Tarreaue43d5322013-10-07 20:01:52 +02002188 appctx->ctx.cli.msg = "'set rate-limit' supports 'connections', 'sessions', 'ssl-sessions', and 'http-compression'.\n";
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002189 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02002190 return 1;
2191 }
2192 }
Willy Tarreau654694e2012-06-07 01:03:16 +02002193 else if (strcmp(args[1], "table") == 0) {
2194 stats_sock_table_request(si, args, STAT_CLI_O_SET);
2195 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002196 else if (strcmp(args[1], "map") == 0) {
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002197 char *err;
2198
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002199 /* Set flags. */
2200 appctx->ctx.map.display_flags = PAT_REF_MAP;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002201
2202 /* Expect three parameters: map name, key and new value. */
2203 if (!*args[2] || !*args[3] || !*args[4]) {
Thierry FOURNIERd5723432014-03-11 13:52:44 +01002204 appctx->ctx.cli.msg = "'set map' expects three parameters: map identifier, key and value.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002205 appctx->st0 = STAT_CLI_PRINT;
2206 return 1;
2207 }
2208
2209 /* Lookup the reference in the maps. */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002210 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002211 if (!appctx->ctx.map.ref) {
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01002212 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002213 appctx->st0 = STAT_CLI_PRINT;
2214 return 1;
2215 }
2216
Thierry FOURNIER9356c682014-01-28 15:55:37 +01002217 /* If the entry identifier start with a '#', it is considered as
2218 * pointer id
2219 */
2220 if (args[3][0] == '#' && args[3][1] == '0' && args[3][2] == 'x') {
2221 struct pat_ref_elt *ref;
2222 long long int conv;
2223 char *error;
2224
2225 /* Convert argument to integer value. */
2226 conv = strtoll(&args[3][1], &error, 16);
2227 if (*error != '\0') {
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01002228 appctx->ctx.cli.msg = "Malformed identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER9356c682014-01-28 15:55:37 +01002229 appctx->st0 = STAT_CLI_PRINT;
2230 return 1;
2231 }
2232
2233 /* Convert and check integer to pointer. */
2234 ref = (struct pat_ref_elt *)(long)conv;
2235 if ((long long int)(long)ref != conv) {
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01002236 appctx->ctx.cli.msg = "Malformed identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER9356c682014-01-28 15:55:37 +01002237 appctx->st0 = STAT_CLI_PRINT;
2238 return 1;
2239 }
2240
2241 /* Try to delete the entry. */
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002242 err = NULL;
2243 if (!pat_ref_set_by_id(appctx->ctx.map.ref, ref, args[4], &err)) {
2244 if (err)
2245 memprintf(&err, "%s.\n", err);
2246 appctx->ctx.cli.err = err;
2247 appctx->st0 = STAT_CLI_PRINT_FREE;
Thierry FOURNIER9356c682014-01-28 15:55:37 +01002248 return 1;
2249 }
2250 }
2251 else {
2252 /* Else, use the entry identifier as pattern
2253 * string, and update the value.
2254 */
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002255 err = NULL;
2256 if (!pat_ref_set(appctx->ctx.map.ref, args[3], args[4], &err)) {
2257 if (err)
2258 memprintf(&err, "%s.\n", err);
2259 appctx->ctx.cli.err = err;
2260 appctx->st0 = STAT_CLI_PRINT_FREE;
Thierry FOURNIER9356c682014-01-28 15:55:37 +01002261 return 1;
2262 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002263 }
2264
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002265 /* The set is done, send message. */
Thierry FOURNIER07e78c52014-12-18 15:28:01 +01002266 appctx->st0 = STAT_CLI_PROMPT;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002267 return 1;
2268 }
Emeric Brun4147b2e2014-06-16 18:36:30 +02002269#ifdef USE_OPENSSL
2270 else if (strcmp(args[1], "ssl") == 0) {
2271 if (strcmp(args[2], "ocsp-response") == 0) {
Lukas Tribuse4e30f72014-12-09 16:32:51 +01002272#if (defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP)
Emeric Brun4147b2e2014-06-16 18:36:30 +02002273 char *err = NULL;
2274
Emeric Brunaf4ef742014-06-19 14:10:45 +02002275 /* Expect one parameter: the new response in base64 encoding */
Emeric Brun4147b2e2014-06-16 18:36:30 +02002276 if (!*args[3]) {
2277 appctx->ctx.cli.msg = "'set ssl ocsp-response' expects response in base64 encoding.\n";
2278 appctx->st0 = STAT_CLI_PRINT;
2279 return 1;
2280 }
2281
2282 trash.len = base64dec(args[3], strlen(args[3]), trash.str, trash.size);
2283 if (trash.len < 0) {
2284 appctx->ctx.cli.msg = "'set ssl ocsp-response' received invalid base64 encoded response.\n";
2285 appctx->st0 = STAT_CLI_PRINT;
2286 return 1;
2287 }
2288
2289 if (ssl_sock_update_ocsp_response(&trash, &err)) {
2290 if (err) {
2291 memprintf(&err, "%s.\n", err);
2292 appctx->ctx.cli.err = err;
2293 appctx->st0 = STAT_CLI_PRINT_FREE;
2294 }
2295 return 1;
2296 }
2297 appctx->ctx.cli.msg = "OCSP Response updated!";
2298 appctx->st0 = STAT_CLI_PRINT;
2299 return 1;
2300#else
2301 appctx->ctx.cli.msg = "HAProxy was compiled against a version of OpenSSL that doesn't support OCSP stapling.\n";
2302 appctx->st0 = STAT_CLI_PRINT;
2303 return 1;
2304#endif
2305 }
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02002306 else if (strcmp(args[2], "tls-key") == 0) {
2307#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
2308 /* Expect two parameters: the filename and the new new TLS key in encoding */
2309 if (!*args[3] || !*args[4]) {
2310 appctx->ctx.cli.msg = "'set ssl tls-key' expects a filename and the new TLS key in base64 encoding.\n";
2311 appctx->st0 = STAT_CLI_PRINT;
2312 return 1;
2313 }
2314
2315 appctx->ctx.tlskeys.ref = tlskeys_ref_lookup_ref(args[3]);
2316 if(!appctx->ctx.tlskeys.ref) {
2317 appctx->ctx.cli.msg = "'set ssl tls-key' unable to locate referenced filename\n";
2318 appctx->st0 = STAT_CLI_PRINT;
2319 return 1;
2320 }
2321
2322 trash.len = base64dec(args[4], strlen(args[4]), trash.str, trash.size);
2323 if (trash.len != sizeof(struct tls_sess_key)) {
2324 appctx->ctx.cli.msg = "'set ssl tls-key' received invalid base64 encoded TLS key.\n";
2325 appctx->st0 = STAT_CLI_PRINT;
2326 return 1;
2327 }
2328
Pradeep Jindalcc79b002015-08-20 18:25:17 +05302329 memcpy(appctx->ctx.tlskeys.ref->tlskeys + ((appctx->ctx.tlskeys.ref->tls_ticket_enc_index + 2) % TLS_TICKETS_NO), trash.str, trash.len);
2330 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 +02002331
2332 appctx->ctx.cli.msg = "TLS ticket key updated!";
2333 appctx->st0 = STAT_CLI_PRINT;
2334 return 1;
2335#else
2336 appctx->ctx.cli.msg = "HAProxy was compiled against a version of OpenSSL "
2337 "that doesn't support specifying TLS ticket keys\n";
2338 appctx->st0 = STAT_CLI_PRINT;
2339 return 1;
2340#endif
2341 }
Emeric Brun4147b2e2014-06-16 18:36:30 +02002342 else {
2343 appctx->ctx.cli.msg = "'set ssl' only supports 'ocsp-response'.\n";
2344 appctx->st0 = STAT_CLI_PRINT;
2345 return 1;
2346 }
2347 }
2348#endif
Willy Tarreau7aabd112010-01-26 10:59:06 +01002349 else { /* unknown "set" parameter */
Willy Tarreau4483d432009-10-10 19:30:08 +02002350 return 0;
2351 }
2352 }
Cyril Bontécd19e512010-01-31 22:34:03 +01002353 else if (strcmp(args[0], "enable") == 0) {
Simon Horman671b6f02013-11-25 10:46:39 +09002354 if (strcmp(args[1], "agent") == 0) {
2355 struct server *sv;
2356
2357 sv = expect_server_admin(s, si, args[2]);
2358 if (!sv)
2359 return 1;
2360
Willy Tarreau2e10f5a2013-12-11 20:11:55 +01002361 if (!(sv->agent.state & CHK_ST_CONFIGURED)) {
2362 appctx->ctx.cli.msg = "Agent was not configured on this server, cannot enable.\n";
2363 appctx->st0 = STAT_CLI_PRINT;
2364 return 1;
2365 }
2366
2367 sv->agent.state |= CHK_ST_ENABLED;
Simon Horman671b6f02013-11-25 10:46:39 +09002368 return 1;
2369 }
Willy Tarreau9b5aecd2014-05-23 11:53:10 +02002370 else if (strcmp(args[1], "health") == 0) {
Cyril Bontécd19e512010-01-31 22:34:03 +01002371 struct server *sv;
2372
Willy Tarreaud52c41e2011-09-07 23:41:01 +02002373 sv = expect_server_admin(s, si, args[2]);
2374 if (!sv)
Cyril Bonté613f0df2011-03-03 20:49:04 +01002375 return 1;
Cyril Bonté613f0df2011-03-03 20:49:04 +01002376
Willy Tarreau9b5aecd2014-05-23 11:53:10 +02002377 if (!(sv->check.state & CHK_ST_CONFIGURED)) {
2378 appctx->ctx.cli.msg = "Health checks are not configured on this server, cannot enable.\n";
2379 appctx->st0 = STAT_CLI_PRINT;
2380 return 1;
2381 }
2382
2383 sv->check.state |= CHK_ST_ENABLED;
2384 return 1;
2385 }
2386 else if (strcmp(args[1], "server") == 0) {
2387 struct server *sv;
2388
2389 sv = expect_server_admin(s, si, args[2]);
2390 if (!sv)
2391 return 1;
2392
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02002393 srv_adm_set_ready(sv);
Willy Tarreau532a4502011-09-07 22:37:44 +02002394 return 1;
2395 }
2396 else if (strcmp(args[1], "frontend") == 0) {
2397 struct proxy *px;
2398
2399 px = expect_frontend_admin(s, si, args[2]);
2400 if (!px)
2401 return 1;
2402
2403 if (px->state == PR_STSTOPPED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002404 appctx->ctx.cli.msg = "Frontend was previously shut down, cannot enable.\n";
2405 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002406 return 1;
2407 }
2408
2409 if (px->state != PR_STPAUSED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002410 appctx->ctx.cli.msg = "Frontend is already enabled.\n";
2411 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002412 return 1;
2413 }
2414
2415 if (!resume_proxy(px)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002416 appctx->ctx.cli.msg = "Failed to resume frontend, check logs for precise cause (port conflict?).\n";
2417 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002418 return 1;
2419 }
Cyril Bontécd19e512010-01-31 22:34:03 +01002420 return 1;
2421 }
2422 else { /* unknown "enable" parameter */
Willy Tarreau9b5aecd2014-05-23 11:53:10 +02002423 appctx->ctx.cli.msg = "'enable' only supports 'agent', 'frontend', 'health', and 'server'.\n";
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002424 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002425 return 1;
Cyril Bontécd19e512010-01-31 22:34:03 +01002426 }
2427 }
2428 else if (strcmp(args[0], "disable") == 0) {
Simon Horman671b6f02013-11-25 10:46:39 +09002429 if (strcmp(args[1], "agent") == 0) {
2430 struct server *sv;
2431
2432 sv = expect_server_admin(s, si, args[2]);
2433 if (!sv)
2434 return 1;
2435
Willy Tarreau2e10f5a2013-12-11 20:11:55 +01002436 sv->agent.state &= ~CHK_ST_ENABLED;
Simon Horman671b6f02013-11-25 10:46:39 +09002437 return 1;
2438 }
Willy Tarreau9b5aecd2014-05-23 11:53:10 +02002439 else if (strcmp(args[1], "health") == 0) {
2440 struct server *sv;
2441
2442 sv = expect_server_admin(s, si, args[2]);
2443 if (!sv)
2444 return 1;
2445
2446 sv->check.state &= ~CHK_ST_ENABLED;
2447 return 1;
2448 }
Simon Horman671b6f02013-11-25 10:46:39 +09002449 else if (strcmp(args[1], "server") == 0) {
Cyril Bontécd19e512010-01-31 22:34:03 +01002450 struct server *sv;
2451
Willy Tarreaud52c41e2011-09-07 23:41:01 +02002452 sv = expect_server_admin(s, si, args[2]);
2453 if (!sv)
Cyril Bonté613f0df2011-03-03 20:49:04 +01002454 return 1;
Cyril Bonté613f0df2011-03-03 20:49:04 +01002455
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02002456 srv_adm_set_maint(sv);
Willy Tarreau532a4502011-09-07 22:37:44 +02002457 return 1;
2458 }
2459 else if (strcmp(args[1], "frontend") == 0) {
2460 struct proxy *px;
2461
2462 px = expect_frontend_admin(s, si, args[2]);
2463 if (!px)
2464 return 1;
2465
2466 if (px->state == PR_STSTOPPED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002467 appctx->ctx.cli.msg = "Frontend was previously shut down, cannot disable.\n";
2468 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002469 return 1;
2470 }
2471
2472 if (px->state == PR_STPAUSED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002473 appctx->ctx.cli.msg = "Frontend is already disabled.\n";
2474 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002475 return 1;
2476 }
2477
2478 if (!pause_proxy(px)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002479 appctx->ctx.cli.msg = "Failed to pause frontend, check logs for precise cause.\n";
2480 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002481 return 1;
2482 }
Cyril Bontécd19e512010-01-31 22:34:03 +01002483 return 1;
2484 }
2485 else { /* unknown "disable" parameter */
Willy Tarreau9b5aecd2014-05-23 11:53:10 +02002486 appctx->ctx.cli.msg = "'disable' only supports 'agent', 'frontend', 'health', and 'server'.\n";
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002487 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002488 return 1;
2489 }
2490 }
2491 else if (strcmp(args[0], "shutdown") == 0) {
2492 if (strcmp(args[1], "frontend") == 0) {
2493 struct proxy *px;
2494
2495 px = expect_frontend_admin(s, si, args[2]);
2496 if (!px)
2497 return 1;
2498
2499 if (px->state == PR_STSTOPPED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002500 appctx->ctx.cli.msg = "Frontend was already shut down.\n";
2501 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002502 return 1;
2503 }
2504
2505 Warning("Proxy %s stopped (FE: %lld conns, BE: %lld conns).\n",
2506 px->id, px->fe_counters.cum_conn, px->be_counters.cum_conn);
2507 send_log(px, LOG_WARNING, "Proxy %s stopped (FE: %lld conns, BE: %lld conns).\n",
2508 px->id, px->fe_counters.cum_conn, px->be_counters.cum_conn);
2509 stop_proxy(px);
2510 return 1;
2511 }
Willy Tarreaua295edc2011-09-07 23:21:03 +02002512 else if (strcmp(args[1], "session") == 0) {
Willy Tarreau87b09662015-04-03 00:22:06 +02002513 struct stream *sess, *ptr;
Willy Tarreaua295edc2011-09-07 23:21:03 +02002514
Willy Tarreaud0d8da92015-04-04 02:10:38 +02002515 if (strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002516 appctx->ctx.cli.msg = stats_permission_denied_msg;
2517 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaua295edc2011-09-07 23:21:03 +02002518 return 1;
2519 }
2520
2521 if (!*args[2]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002522 appctx->ctx.cli.msg = "Session pointer expected (use 'show sess').\n";
2523 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaua295edc2011-09-07 23:21:03 +02002524 return 1;
2525 }
2526
2527 ptr = (void *)strtoul(args[2], NULL, 0);
2528
Willy Tarreau87b09662015-04-03 00:22:06 +02002529 /* first, look for the requested stream in the stream table */
2530 list_for_each_entry(sess, &streams, list) {
Willy Tarreaua295edc2011-09-07 23:21:03 +02002531 if (sess == ptr)
2532 break;
2533 }
2534
Willy Tarreau87b09662015-04-03 00:22:06 +02002535 /* do we have the stream ? */
Willy Tarreaua295edc2011-09-07 23:21:03 +02002536 if (sess != ptr) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002537 appctx->ctx.cli.msg = "No such session (use 'show sess').\n";
2538 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaua295edc2011-09-07 23:21:03 +02002539 return 1;
2540 }
2541
Willy Tarreaue7dff022015-04-03 01:14:29 +02002542 stream_shutdown(sess, SF_ERR_KILLED);
Willy Tarreaua295edc2011-09-07 23:21:03 +02002543 return 1;
2544 }
Willy Tarreau52b2d222011-09-07 23:48:48 +02002545 else if (strcmp(args[1], "sessions") == 0) {
2546 if (strcmp(args[2], "server") == 0) {
2547 struct server *sv;
Willy Tarreau87b09662015-04-03 00:22:06 +02002548 struct stream *sess, *sess_bck;
Willy Tarreau52b2d222011-09-07 23:48:48 +02002549
2550 sv = expect_server_admin(s, si, args[3]);
2551 if (!sv)
2552 return 1;
2553
Willy Tarreau87b09662015-04-03 00:22:06 +02002554 /* kill all the stream that are on this server */
Willy Tarreau52b2d222011-09-07 23:48:48 +02002555 list_for_each_entry_safe(sess, sess_bck, &sv->actconns, by_srv)
2556 if (sess->srv_conn == sv)
Willy Tarreaue7dff022015-04-03 01:14:29 +02002557 stream_shutdown(sess, SF_ERR_KILLED);
Willy Tarreau52b2d222011-09-07 23:48:48 +02002558
2559 return 1;
2560 }
2561 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002562 appctx->ctx.cli.msg = "'shutdown sessions' only supports 'server'.\n";
2563 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau52b2d222011-09-07 23:48:48 +02002564 return 1;
2565 }
2566 }
Willy Tarreau532a4502011-09-07 22:37:44 +02002567 else { /* unknown "disable" parameter */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002568 appctx->ctx.cli.msg = "'shutdown' only supports 'frontend', 'session' and 'sessions'.\n";
2569 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002570 return 1;
Cyril Bontécd19e512010-01-31 22:34:03 +01002571 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002572 }
2573 else if (strcmp(args[0], "del") == 0) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002574 if (strcmp(args[1], "map") == 0 || strcmp(args[1], "acl") == 0) {
2575 if (args[1][0] == 'm')
2576 appctx->ctx.map.display_flags = PAT_REF_MAP;
2577 else
2578 appctx->ctx.map.display_flags = PAT_REF_ACL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002579
2580 /* Expect two parameters: map name and key. */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002581 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
2582 if (!*args[2] || !*args[3]) {
2583 appctx->ctx.cli.msg = "This command expects two parameters: map identifier and key.\n";
2584 appctx->st0 = STAT_CLI_PRINT;
2585 return 1;
2586 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002587 }
2588
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002589 else {
2590 if (!*args[2] || !*args[3]) {
2591 appctx->ctx.cli.msg = "This command expects two parameters: ACL identifier and key.\n";
2592 appctx->st0 = STAT_CLI_PRINT;
2593 return 1;
2594 }
2595 }
2596
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002597 /* Lookup the reference in the maps. */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002598 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002599 if (!appctx->ctx.map.ref ||
2600 !(appctx->ctx.map.ref->flags & appctx->ctx.map.display_flags)) {
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01002601 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002602 appctx->st0 = STAT_CLI_PRINT;
2603 return 1;
2604 }
2605
Thierry FOURNIER9356c682014-01-28 15:55:37 +01002606 /* If the entry identifier start with a '#', it is considered as
2607 * pointer id
2608 */
2609 if (args[3][0] == '#' && args[3][1] == '0' && args[3][2] == 'x') {
2610 struct pat_ref_elt *ref;
2611 long long int conv;
2612 char *error;
2613
2614 /* Convert argument to integer value. */
2615 conv = strtoll(&args[3][1], &error, 16);
2616 if (*error != '\0') {
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01002617 appctx->ctx.cli.msg = "Malformed identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER9356c682014-01-28 15:55:37 +01002618 appctx->st0 = STAT_CLI_PRINT;
2619 return 1;
2620 }
2621
2622 /* Convert and check integer to pointer. */
2623 ref = (struct pat_ref_elt *)(long)conv;
2624 if ((long long int)(long)ref != conv) {
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01002625 appctx->ctx.cli.msg = "Malformed identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER9356c682014-01-28 15:55:37 +01002626 appctx->st0 = STAT_CLI_PRINT;
2627 return 1;
2628 }
2629
2630 /* Try to delete the entry. */
2631 if (!pat_ref_delete_by_id(appctx->ctx.map.ref, ref)) {
2632 /* The entry is not found, send message. */
2633 appctx->ctx.cli.msg = "Key not found.\n";
2634 appctx->st0 = STAT_CLI_PRINT;
2635 return 1;
2636 }
2637 }
2638 else {
2639 /* Else, use the entry identifier as pattern
2640 * string and try to delete the entry.
2641 */
2642 if (!pat_ref_delete(appctx->ctx.map.ref, args[3])) {
2643 /* The entry is not found, send message. */
2644 appctx->ctx.cli.msg = "Key not found.\n";
2645 appctx->st0 = STAT_CLI_PRINT;
2646 return 1;
2647 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002648 }
2649
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002650 /* The deletion is done, send message. */
Thierry FOURNIER07e78c52014-12-18 15:28:01 +01002651 appctx->st0 = STAT_CLI_PROMPT;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002652 return 1;
2653 }
2654 else { /* unknown "del" parameter */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002655 appctx->ctx.cli.msg = "'del' only supports 'map' or 'acl'.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002656 appctx->st0 = STAT_CLI_PRINT;
2657 return 1;
2658 }
2659 }
2660 else if (strcmp(args[0], "add") == 0) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002661 if (strcmp(args[1], "map") == 0 ||
2662 strcmp(args[1], "acl") == 0) {
2663 int ret;
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002664 char *err;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002665
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002666 /* Set flags. */
2667 if (args[1][0] == 'm')
2668 appctx->ctx.map.display_flags = PAT_REF_MAP;
2669 else
2670 appctx->ctx.map.display_flags = PAT_REF_ACL;
2671
2672 /* If the keywork is "map", we expect three parameters, if it
2673 * is "acl", we expect only two parameters
2674 */
2675 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
2676 if (!*args[2] || !*args[3] || !*args[4]) {
2677 appctx->ctx.cli.msg = "'add map' expects three parameters: map identifier, key and value.\n";
2678 appctx->st0 = STAT_CLI_PRINT;
2679 return 1;
2680 }
2681 }
2682 else {
2683 if (!*args[2] || !*args[3]) {
2684 appctx->ctx.cli.msg = "'add acl' expects two parameters: ACL identifier and pattern.\n";
2685 appctx->st0 = STAT_CLI_PRINT;
2686 return 1;
2687 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002688 }
2689
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002690 /* Lookup for the reference. */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002691 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002692 if (!appctx->ctx.map.ref) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002693 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01002694 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002695 else
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01002696 appctx->ctx.cli.msg = "Unknown ACL identifier. Please use #<id> or <file>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002697 appctx->st0 = STAT_CLI_PRINT;
2698 return 1;
2699 }
2700
Thierry FOURNIER64c585f2014-01-29 20:02:36 +01002701 /* The command "add acl" is prohibited if the reference
2702 * use samples.
2703 */
2704 if ((appctx->ctx.map.display_flags & PAT_REF_ACL) &&
2705 (appctx->ctx.map.ref->flags & PAT_REF_SMP)) {
2706 appctx->ctx.cli.msg = "This ACL is shared with a map containing samples. "
2707 "You must use the command 'add map' to add values.\n";
2708 appctx->st0 = STAT_CLI_PRINT;
2709 return 1;
2710 }
2711
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002712 /* Add value. */
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002713 err = NULL;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002714 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02002715 ret = pat_ref_add(appctx->ctx.map.ref, args[3], args[4], &err);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002716 else
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02002717 ret = pat_ref_add(appctx->ctx.map.ref, args[3], NULL, &err);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002718 if (!ret) {
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002719 if (err)
2720 memprintf(&err, "%s.\n", err);
2721 appctx->ctx.cli.err = err;
2722 appctx->st0 = STAT_CLI_PRINT_FREE;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002723 return 1;
2724 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002725
2726 /* The add is done, send message. */
Thierry FOURNIER07e78c52014-12-18 15:28:01 +01002727 appctx->st0 = STAT_CLI_PROMPT;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002728 return 1;
2729 }
2730 else { /* unknown "del" parameter */
2731 appctx->ctx.cli.msg = "'add' only supports 'map'.\n";
2732 appctx->st0 = STAT_CLI_PRINT;
2733 return 1;
2734 }
Cyril Bontécd19e512010-01-31 22:34:03 +01002735 }
2736 else { /* not "show" nor "clear" nor "get" nor "set" nor "enable" nor "disable" */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002737 return 0;
2738 }
2739 return 1;
2740}
2741
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002742/* This I/O handler runs as an applet embedded in a stream interface. It is
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002743 * used to processes I/O from/to the stats unix socket. The system relies on a
2744 * state machine handling requests and various responses. We read a request,
2745 * then we process it and send the response, and we possibly display a prompt.
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002746 * Then we can read again. The state is stored in appctx->st0 and is one of the
2747 * STAT_CLI_* constants. appctx->st1 is used to indicate whether prompt is enabled
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002748 * or not.
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002749 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02002750static void cli_io_handler(struct appctx *appctx)
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002751{
Willy Tarreau00a37f02015-04-13 12:05:19 +02002752 struct stream_interface *si = appctx->owner;
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002753 struct channel *req = si_oc(si);
2754 struct channel *res = si_ic(si);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002755 int reql;
2756 int len;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002757
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002758 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
2759 goto out;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002760
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002761 while (1) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002762 if (appctx->st0 == STAT_CLI_INIT) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002763 /* Stats output not initialized yet */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002764 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
2765 appctx->st0 = STAT_CLI_GETREQ;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002766 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002767 else if (appctx->st0 == STAT_CLI_END) {
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002768 /* Let's close for real now. We just close the request
2769 * side, the conditions below will complete if needed.
2770 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02002771 si_shutw(si);
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002772 break;
2773 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002774 else if (appctx->st0 == STAT_CLI_GETREQ) {
Willy Tarreau4e33d862009-10-11 23:35:10 +02002775 /* ensure we have some output room left in the event we
2776 * would want to return some info right after parsing.
2777 */
Willy Tarreau4e4292b2014-11-28 12:18:45 +01002778 if (buffer_almost_full(si_ib(si))) {
Willy Tarreaufe127932015-04-21 19:23:39 +02002779 si_applet_cant_put(si);
Willy Tarreau4e33d862009-10-11 23:35:10 +02002780 break;
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01002781 }
Willy Tarreau4e33d862009-10-11 23:35:10 +02002782
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002783 reql = bo_getline(si_oc(si), trash.str, trash.size);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002784 if (reql <= 0) { /* closed or EOL not found */
2785 if (reql == 0)
2786 break;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002787 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002788 continue;
2789 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002790
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002791 /* seek for a possible semi-colon. If we find one, we
2792 * replace it with an LF and skip only this part.
2793 */
2794 for (len = 0; len < reql; len++)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002795 if (trash.str[len] == ';') {
2796 trash.str[len] = '\n';
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002797 reql = len + 1;
2798 break;
2799 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002800
Willy Tarreau816fc222009-10-04 07:36:58 +02002801 /* now it is time to check that we have a full line,
2802 * remove the trailing \n and possibly \r, then cut the
2803 * line.
2804 */
2805 len = reql - 1;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002806 if (trash.str[len] != '\n') {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002807 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002808 continue;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002809 }
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002810
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002811 if (len && trash.str[len-1] == '\r')
Willy Tarreau816fc222009-10-04 07:36:58 +02002812 len--;
2813
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002814 trash.str[len] = '\0';
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002815
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002816 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002817 if (len) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002818 if (strcmp(trash.str, "quit") == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002819 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002820 continue;
2821 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002822 else if (strcmp(trash.str, "prompt") == 0)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002823 appctx->st1 = !appctx->st1;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002824 else if (strcmp(trash.str, "help") == 0 ||
2825 !stats_sock_parse_request(si, trash.str)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002826 appctx->ctx.cli.msg = stats_sock_usage_msg;
2827 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02002828 }
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002829 /* NB: stats_sock_parse_request() may have put
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002830 * another STAT_CLI_O_* into appctx->st0.
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002831 */
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002832 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002833 else if (!appctx->st1) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002834 /* if prompt is disabled, print help on empty lines,
2835 * so that the user at least knows how to enable
2836 * prompt and find help.
2837 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002838 appctx->ctx.cli.msg = stats_sock_usage_msg;
2839 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002840 }
2841
2842 /* re-adjust req buffer */
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002843 bo_skip(si_oc(si), reql);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002844 req->flags |= CF_READ_DONTWAIT; /* we plan to read small requests */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002845 }
Willy Tarreau68c00c72015-09-25 19:21:19 +02002846 else { /* output functions */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002847 switch (appctx->st0) {
Andrew Hayworthe32d1862015-10-02 15:08:10 +00002848 case STAT_CLI_PROMPT:
2849 break;
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02002850 case STAT_CLI_PRINT:
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002851 if (bi_putstr(si_ic(si), appctx->ctx.cli.msg) != -1)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002852 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreaubc18da12015-03-13 14:00:47 +01002853 else
Willy Tarreaufe127932015-04-21 19:23:39 +02002854 si_applet_cant_put(si);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002855 break;
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002856 case STAT_CLI_PRINT_FREE:
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002857 if (bi_putstr(si_ic(si), appctx->ctx.cli.err) != -1) {
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002858 free(appctx->ctx.cli.err);
2859 appctx->st0 = STAT_CLI_PROMPT;
2860 }
Willy Tarreaubc18da12015-03-13 14:00:47 +01002861 else
Willy Tarreaufe127932015-04-21 19:23:39 +02002862 si_applet_cant_put(si);
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002863 break;
Baptiste Assmann9b6857e2015-09-18 14:49:12 +02002864 case STAT_CLI_O_BACKEND:
2865 if (stats_dump_backend_to_buffer(si))
2866 appctx->st0 = STAT_CLI_PROMPT;
2867 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002868 case STAT_CLI_O_INFO:
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002869 if (stats_dump_info_to_buffer(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002870 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002871 break;
Baptiste Assmann28289462015-07-03 08:01:20 +02002872 case STAT_CLI_O_SERVERS_STATE:
2873 if (stats_dump_servers_state_to_buffer(si))
2874 appctx->st0 = STAT_CLI_PROMPT;
2875 break;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002876 case STAT_CLI_O_STAT:
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002877 if (stats_dump_stat_to_buffer(si, NULL))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002878 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002879 break;
Baptiste Assmann3863f972015-05-17 00:33:24 +02002880 case STAT_CLI_O_RESOLVERS:
2881 if (stats_dump_resolvers_to_buffer(si))
2882 appctx->st0 = STAT_CLI_PROMPT;
2883 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002884 case STAT_CLI_O_SESS:
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01002885 if (stats_dump_sess_to_buffer(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002886 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002887 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002888 case STAT_CLI_O_ERR: /* errors dump */
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01002889 if (stats_dump_errors_to_buffer(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002890 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002891 break;
Willy Tarreau69f58c82010-07-12 17:55:33 +02002892 case STAT_CLI_O_TAB:
Simon Hormanc88b8872011-06-15 15:18:49 +09002893 case STAT_CLI_O_CLR:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002894 if (stats_table_request(si, appctx->st0))
2895 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau69f58c82010-07-12 17:55:33 +02002896 break;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002897 case STAT_CLI_O_PATS:
2898 if (stats_pats_list(si))
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002899 appctx->st0 = STAT_CLI_PROMPT;
2900 break;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002901 case STAT_CLI_O_PAT:
2902 if (stats_pat_list(si))
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002903 appctx->st0 = STAT_CLI_PROMPT;
2904 break;
2905 case STAT_CLI_O_MLOOK:
2906 if (stats_map_lookup(si))
2907 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau4efb3532014-01-29 12:13:39 +01002908 break;
Willy Tarreau12833bb2014-01-28 16:49:56 +01002909 case STAT_CLI_O_POOLS:
2910 if (stats_dump_pools_to_buffer(si))
2911 appctx->st0 = STAT_CLI_PROMPT;
2912 break;
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02002913#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
2914 case STAT_CLI_O_TLSK:
2915 if (stats_tlskeys_list(si))
2916 appctx->st0 = STAT_CLI_PROMPT;
2917 break;
2918#endif
Willy Tarreauae795722016-02-16 11:27:28 +01002919 case STAT_CLI_O_ENV: /* environment dump */
2920 if (stats_dump_env_to_buffer(si))
2921 appctx->st0 = STAT_CLI_PROMPT;
2922 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002923 default: /* abnormal state */
Willy Tarreau5cfa3bc2015-09-25 20:08:51 +02002924 si->flags |= SI_FL_ERR;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002925 break;
2926 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002927
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002928 /* The post-command prompt is either LF alone or LF + '> ' in interactive mode */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002929 if (appctx->st0 == STAT_CLI_PROMPT) {
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002930 if (bi_putstr(si_ic(si), appctx->st1 ? "\n> " : "\n") != -1)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002931 appctx->st0 = STAT_CLI_GETREQ;
Willy Tarreaubc18da12015-03-13 14:00:47 +01002932 else
Willy Tarreaufe127932015-04-21 19:23:39 +02002933 si_applet_cant_put(si);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002934 }
2935
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002936 /* If the output functions are still there, it means they require more room. */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002937 if (appctx->st0 >= STAT_CLI_OUTPUT)
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002938 break;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002939
2940 /* Now we close the output if one of the writers did so,
2941 * or if we're not in interactive mode and the request
2942 * buffer is empty. This still allows pipelined requests
2943 * to be sent in non-interactive mode.
2944 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002945 if ((res->flags & (CF_SHUTW|CF_SHUTW_NOW)) || (!appctx->st1 && !req->buf->o)) {
2946 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002947 continue;
2948 }
2949
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002950 /* switch state back to GETREQ to read next requests */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002951 appctx->st0 = STAT_CLI_GETREQ;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002952 }
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002953 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002954
Willy Tarreauc9e930a2015-09-25 20:06:08 +02002955 if ((res->flags & CF_SHUTR) && (si->state == SI_ST_EST)) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002956 DPRINTF(stderr, "%s@%d: si to buf closed. req=%08x, res=%08x, st=%d\n",
2957 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
Aman Guptaceafb4a2012-04-02 18:57:54 -07002958 /* Other side has closed, let's abort if we have no more processing to do
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002959 * and nothing more to consume. This is comparable to a broken pipe, so
2960 * we forward the close to the request side so that it flows upstream to
2961 * the client.
2962 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02002963 si_shutw(si);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002964 }
2965
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002966 if ((req->flags & CF_SHUTW) && (si->state == SI_ST_EST) && (appctx->st0 < STAT_CLI_OUTPUT)) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002967 DPRINTF(stderr, "%s@%d: buf to si closed. req=%08x, res=%08x, st=%d\n",
2968 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
2969 /* We have no more processing to do, and nothing more to send, and
2970 * the client side has closed. So we'll forward this state downstream
2971 * on the response buffer.
2972 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02002973 si_shutr(si);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002974 res->flags |= CF_READ_NULL;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002975 }
2976
Willy Tarreau828824a2015-04-19 17:20:03 +02002977 out:
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01002978 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 +02002979 __FUNCTION__, __LINE__,
Willy Tarreau9b28e032012-10-12 23:49:43 +02002980 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 +02002981}
2982
Willy Tarreau638d40a2016-02-24 23:11:01 +01002983/* Emits a stats field without any surrounding element and properly encoded to
2984 * resist CSV output. Returns non-zero on success, 0 if the buffer is full.
2985 */
Willy Tarreaub47785f2016-02-24 23:28:31 +01002986int stats_emit_raw_data_field(struct chunk *out, const struct field *f)
Willy Tarreau638d40a2016-02-24 23:11:01 +01002987{
2988 switch (field_format(f, 0)) {
2989 case FF_EMPTY: return 1;
2990 case FF_S32: return chunk_appendf(out, "%d", f->u.s32);
2991 case FF_U32: return chunk_appendf(out, "%u", f->u.u32);
2992 case FF_S64: return chunk_appendf(out, "%lld", (long long)f->u.s64);
2993 case FF_U64: return chunk_appendf(out, "%llu", (unsigned long long)f->u.u64);
2994 case FF_STR: return csv_enc_append(field_str(f, 0), 1, out) != NULL;
2995 default: return chunk_appendf(out, "[INCORRECT_FIELD_TYPE_%08x]", f->type);
2996 }
2997}
2998
Willy Tarreaub47785f2016-02-24 23:28:31 +01002999/* Emits a stats field prefixed with its type. No CSV encoding is prepared, the
3000 * output is supposed to be used on its own line. Returns non-zero on success, 0
3001 * if the buffer is full.
3002 */
3003int stats_emit_typed_data_field(struct chunk *out, const struct field *f)
3004{
3005 switch (field_format(f, 0)) {
3006 case FF_EMPTY: return 1;
3007 case FF_S32: return chunk_appendf(out, "s32:%d", f->u.s32);
3008 case FF_U32: return chunk_appendf(out, "u32:%u", f->u.u32);
3009 case FF_S64: return chunk_appendf(out, "s64:%lld", (long long)f->u.s64);
3010 case FF_U64: return chunk_appendf(out, "u64:%llu", (unsigned long long)f->u.u64);
3011 case FF_STR: return chunk_appendf(out, "str:%s", field_str(f, 0));
3012 default: return chunk_appendf(out, "%08x:?", f->type);
3013 }
3014}
3015
3016/* Emits an encoding of the field type on 3 characters followed by a delimiter.
3017 * Returns non-zero on success, 0 if the buffer is full.
3018 */
3019int stats_emit_field_tags(struct chunk *out, const struct field *f, char delim)
3020{
3021 char origin, nature, scope;
3022
3023 switch (field_origin(f, 0)) {
3024 case FO_METRIC: origin = 'M'; break;
3025 case FO_STATUS: origin = 'S'; break;
3026 case FO_KEY: origin = 'K'; break;
3027 case FO_CONFIG: origin = 'C'; break;
3028 case FO_PRODUCT: origin = 'P'; break;
3029 default: origin = '?'; break;
3030 }
3031
3032 switch (field_nature(f, 0)) {
3033 case FN_GAUGE: nature = 'G'; break;
3034 case FN_LIMIT: nature = 'L'; break;
3035 case FN_MIN: nature = 'm'; break;
3036 case FN_MAX: nature = 'M'; break;
3037 case FN_RATE: nature = 'R'; break;
3038 case FN_COUNTER: nature = 'C'; break;
3039 case FN_DURATION: nature = 'D'; break;
3040 case FN_AGE: nature = 'A'; break;
3041 case FN_TIME: nature = 'T'; break;
3042 case FN_NAME: nature = 'N'; break;
3043 case FN_OUTPUT: nature = 'O'; break;
3044 case FN_AVG: nature = 'a'; break;
3045 default: nature = '?'; break;
3046 }
3047
3048 switch (field_scope(f, 0)) {
3049 case FS_PROCESS: scope = 'P'; break;
3050 case FS_SERVICE: scope = 'S'; break;
3051 case FS_SYSTEM: scope = 's'; break;
3052 case FS_CLUSTER: scope = 'C'; break;
3053 default: scope = '?'; break;
3054 }
3055
3056 return chunk_appendf(out, "%c%c%c%c", origin, nature, scope, delim);
3057}
3058
Willy Tarreaubf95cba2016-01-11 18:27:29 +01003059/* Dump all fields from <info> into <out> using the "show info" format (name: value) */
3060static int stats_dump_info_fields(struct chunk *out, const struct field *info)
3061{
3062 int field;
3063
3064 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
3065 if (!field_format(info, field))
3066 continue;
3067
3068 if (!chunk_appendf(out, "%s: ", info_field_names[field]))
3069 return 0;
3070 if (!stats_emit_raw_data_field(out, &info[field]))
3071 return 0;
3072 if (!chunk_strcat(out, "\n"))
3073 return 0;
3074 }
3075 return 1;
3076}
3077
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003078/* This function dumps information onto the stream interface's read buffer.
3079 * It returns 0 as long as it does not complete, non-zero upon completion.
3080 * No state is used.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003081 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003082static int stats_dump_info_to_buffer(struct stream_interface *si)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003083{
3084 unsigned int up = (now.tv_sec - start_date.tv_sec);
Willy Tarreau4529c072016-01-11 18:17:07 +01003085 struct chunk *out = get_trash_chunk();
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003086
Willy Tarreau0c9c2722014-05-28 12:28:58 +02003087#ifdef USE_OPENSSL
3088 int ssl_sess_rate = read_freq_ctr(&global.ssl_per_sec);
3089 int ssl_key_rate = read_freq_ctr(&global.ssl_fe_keys_per_sec);
3090 int ssl_reuse = 0;
3091
3092 if (ssl_key_rate < ssl_sess_rate) {
3093 /* count the ssl reuse ratio and avoid overflows in both directions */
3094 ssl_reuse = 100 - (100 * ssl_key_rate + (ssl_sess_rate - 1) / 2) / ssl_sess_rate;
3095 }
3096#endif
3097
Willy Tarreau4529c072016-01-11 18:17:07 +01003098 chunk_reset(out);
3099 memset(&info, 0, sizeof(info));
3100
3101 info[INF_NAME] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, PRODUCT_NAME);
3102 info[INF_VERSION] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, HAPROXY_VERSION);
3103 info[INF_RELEASE_DATE] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, HAPROXY_DATE);
3104
3105 info[INF_NBPROC] = mkf_u32(FO_CONFIG|FS_SERVICE, global.nbproc);
3106 info[INF_PROCESS_NUM] = mkf_u32(FO_KEY, relative_pid);
3107 info[INF_PID] = mkf_u32(FO_STATUS, pid);
3108
3109 info[INF_UPTIME] = mkf_str(FN_DURATION, chunk_newstr(out));
3110 chunk_appendf(out, "%ud %uh%02um%02us", up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60));
3111
3112 info[INF_UPTIME_SEC] = mkf_u32(FN_DURATION, up);
3113 info[INF_MEMMAX_MB] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_memmax);
3114 info[INF_POOL_ALLOC_MB] = mkf_u32(0, (unsigned)(pool_total_allocated() / 1048576L));
3115 info[INF_POOL_USED_MB] = mkf_u32(0, (unsigned)(pool_total_used() / 1048576L));
3116 info[INF_POOL_FAILED] = mkf_u32(FN_COUNTER, pool_total_failures());
3117 info[INF_ULIMIT_N] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_nofile);
3118 info[INF_MAXSOCK] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxsock);
3119 info[INF_MAXCONN] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxconn);
3120 info[INF_HARD_MAXCONN] = mkf_u32(FO_CONFIG|FN_LIMIT, global.hardmaxconn);
3121 info[INF_CURR_CONN] = mkf_u32(0, actconn);
3122 info[INF_CUM_CONN] = mkf_u32(FN_COUNTER, totalconn);
3123 info[INF_CUM_REQ] = mkf_u32(FN_COUNTER, global.req_count);
3124#ifdef USE_OPENSSL
3125 info[INF_MAX_SSL_CONNS] = mkf_u32(FN_MAX, global.maxsslconn);
3126 info[INF_CURR_SSL_CONNS] = mkf_u32(0, sslconns);
3127 info[INF_CUM_SSL_CONNS] = mkf_u32(FN_COUNTER, totalsslconns);
3128#endif
3129 info[INF_MAXPIPES] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxpipes);
3130 info[INF_PIPES_USED] = mkf_u32(0, pipes_used);
3131 info[INF_PIPES_FREE] = mkf_u32(0, pipes_free);
3132 info[INF_CONN_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.conn_per_sec));
3133 info[INF_CONN_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.cps_lim);
3134 info[INF_MAX_CONN_RATE] = mkf_u32(FN_MAX, global.cps_max);
3135 info[INF_SESS_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.sess_per_sec));
3136 info[INF_SESS_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.sps_lim);
3137 info[INF_MAX_SESS_RATE] = mkf_u32(FN_RATE, global.sps_max);
3138
3139#ifdef USE_OPENSSL
3140 info[INF_SSL_RATE] = mkf_u32(FN_RATE, ssl_sess_rate);
3141 info[INF_SSL_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.ssl_lim);
3142 info[INF_MAX_SSL_RATE] = mkf_u32(FN_MAX, global.ssl_max);
3143 info[INF_SSL_FRONTEND_KEY_RATE] = mkf_u32(0, ssl_key_rate);
3144 info[INF_SSL_FRONTEND_MAX_KEY_RATE] = mkf_u32(FN_MAX, global.ssl_fe_keys_max);
3145 info[INF_SSL_FRONTEND_SESSION_REUSE_PCT] = mkf_u32(0, ssl_reuse);
3146 info[INF_SSL_BACKEND_KEY_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.ssl_be_keys_per_sec));
3147 info[INF_SSL_BACKEND_MAX_KEY_RATE] = mkf_u32(FN_MAX, global.ssl_be_keys_max);
3148 info[INF_SSL_CACHE_LOOKUPS] = mkf_u32(FN_COUNTER, global.shctx_lookups);
3149 info[INF_SSL_CACHE_MISSES] = mkf_u32(FN_COUNTER, global.shctx_misses);
3150#endif
3151 info[INF_COMPRESS_BPS_IN] = mkf_u32(FN_RATE, read_freq_ctr(&global.comp_bps_in));
3152 info[INF_COMPRESS_BPS_OUT] = mkf_u32(FN_RATE, read_freq_ctr(&global.comp_bps_out));
3153 info[INF_COMPRESS_BPS_RATE_LIM] = mkf_u32(FO_CONFIG|FN_LIMIT, global.comp_rate_lim);
3154#ifdef USE_ZLIB
3155 info[INF_ZLIB_MEM_USAGE] = mkf_u32(0, zlib_used_memory);
3156 info[INF_MAX_ZLIB_MEM_USAGE] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxzlibmem);
3157#endif
3158 info[INF_TASKS] = mkf_u32(0, nb_tasks_cur);
3159 info[INF_RUN_QUEUE] = mkf_u32(0, run_queue_cur);
3160 info[INF_IDLE_PCT] = mkf_u32(FN_AVG, idle_pct);
3161 info[INF_NODE] = mkf_str(FO_CONFIG|FN_OUTPUT|FS_SERVICE, global.node);
3162 if (global.desc)
3163 info[INF_DESCRIPTION] = mkf_str(FO_CONFIG|FN_OUTPUT|FS_SERVICE, global.desc);
3164
Willy Tarreau1b4ba1e2016-01-11 18:29:04 +01003165 chunk_reset(&trash);
3166 stats_dump_info_fields(&trash, info);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003167
Willy Tarreaubc18da12015-03-13 14:00:47 +01003168 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02003169 si_applet_cant_put(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003170 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01003171 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003172
3173 return 1;
3174}
3175
Baptiste Assmann28289462015-07-03 08:01:20 +02003176/* dumps server state information into <buf> for all the servers found in <backend>
3177 * These information are all the parameters which may change during HAProxy runtime.
3178 * By default, we only export to the last known server state file format.
3179 * These information can be used at next startup to recover same level of server state.
3180 */
3181static void dump_servers_state(struct proxy *backend, struct chunk *buf)
3182{
3183 struct server *srv;
3184 char srv_addr[INET6_ADDRSTRLEN + 1];
3185 time_t srv_time_since_last_change;
3186 int bk_f_forced_id, srv_f_forced_id;
3187
3188 /* we don't want to report any state if the backend is not enabled on this process */
3189 if (backend->bind_proc && !(backend->bind_proc & (1UL << (relative_pid - 1))))
3190 return;
3191
3192 srv = backend->srv;
3193
3194 while (srv) {
3195 srv_addr[0] = '\0';
3196 srv_time_since_last_change = 0;
3197 bk_f_forced_id = 0;
3198 srv_f_forced_id = 0;
3199
3200 switch (srv->addr.ss_family) {
3201 case AF_INET:
3202 inet_ntop(srv->addr.ss_family, &((struct sockaddr_in *)&srv->addr)->sin_addr,
3203 srv_addr, INET_ADDRSTRLEN + 1);
3204 break;
3205 case AF_INET6:
3206 inet_ntop(srv->addr.ss_family, &((struct sockaddr_in6 *)&srv->addr)->sin6_addr,
3207 srv_addr, INET6_ADDRSTRLEN + 1);
3208 break;
3209 }
3210 srv_time_since_last_change = now.tv_sec - srv->last_change;
3211 bk_f_forced_id = backend->options & PR_O_FORCED_ID ? 1 : 0;
3212 srv_f_forced_id = srv->flags & SRV_F_FORCED_ID ? 1 : 0;
3213
3214 chunk_appendf(buf,
3215 "%d %s "
3216 "%d %s %s "
3217 "%d %d %d %d %ld "
3218 "%d %d %d %d %d "
3219 "%d %d"
3220 "\n",
3221 backend->uuid, backend->id,
3222 srv->puid, srv->id, srv_addr,
David Carlier081b3362015-11-18 06:10:22 +00003223 srv->state, srv->admin, srv->uweight, srv->iweight, (long int)srv_time_since_last_change,
Baptiste Assmann28289462015-07-03 08:01:20 +02003224 srv->check.status, srv->check.result, srv->check.health, srv->check.state, srv->agent.state,
3225 bk_f_forced_id, srv_f_forced_id);
3226
3227 srv = srv->next;
3228 }
3229}
3230
Baptiste Assmann9b6857e2015-09-18 14:49:12 +02003231/* Parses backend list and simply report backend names */
3232static int stats_dump_backend_to_buffer(struct stream_interface *si)
3233{
3234 extern struct proxy *proxy;
3235 struct proxy *curproxy;
3236
3237 chunk_reset(&trash);
3238 chunk_printf(&trash, "# name\n");
3239
3240 for (curproxy = proxy; curproxy != NULL; curproxy = curproxy->next) {
3241 /* looking for backends only */
3242 if (!(curproxy->cap & PR_CAP_BE))
3243 continue;
3244
3245 /* we don't want to list a backend which is bound to this process */
3246 if (curproxy->bind_proc && !(curproxy->bind_proc & (1UL << (relative_pid - 1))))
3247 continue;
3248
3249 chunk_appendf(&trash, "%s\n", curproxy->id);
3250 }
3251
3252 if (bi_putchk(si_ic(si), &trash) == -1) {
3253 si_applet_cant_put(si);
3254 return 0;
3255 }
3256
3257 return 1;
3258}
3259
Baptiste Assmann28289462015-07-03 08:01:20 +02003260/* Parses backend list or simply use backend name provided by the user to return
3261 * states of servers to stdout.
3262 */
3263static int stats_dump_servers_state_to_buffer(struct stream_interface *si)
3264{
3265 struct appctx *appctx = __objt_appctx(si->end);
3266 extern struct proxy *proxy;
3267 struct proxy *curproxy;
3268
3269 chunk_reset(&trash);
3270
3271 chunk_printf(&trash, "%d\n# %s\n", SRV_STATE_FILE_VERSION, SRV_STATE_FILE_FIELD_NAMES);
3272
3273 if (appctx->ctx.server_state.backend) {
3274 dump_servers_state(appctx->ctx.server_state.backend, &trash);
3275 }
3276 else {
3277 for (curproxy = proxy; curproxy != NULL; curproxy = curproxy->next) {
3278 /* servers are only in backends */
3279 if (!(curproxy->cap & PR_CAP_BE))
3280 continue;
3281
3282 dump_servers_state(curproxy, &trash);
3283 }
3284 }
3285
3286 if (bi_putchk(si_ic(si), &trash) == -1) {
3287 si_applet_cant_put(si);
3288 return 0;
3289 }
3290
3291 return 1;
3292}
3293
Willy Tarreau12833bb2014-01-28 16:49:56 +01003294/* This function dumps memory usage information onto the stream interface's
3295 * read buffer. It returns 0 as long as it does not complete, non-zero upon
3296 * completion. No state is used.
3297 */
3298static int stats_dump_pools_to_buffer(struct stream_interface *si)
3299{
3300 dump_pools_to_trash();
Willy Tarreaubc18da12015-03-13 14:00:47 +01003301 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02003302 si_applet_cant_put(si);
Willy Tarreau12833bb2014-01-28 16:49:56 +01003303 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01003304 }
Willy Tarreau82a86022016-01-04 19:04:18 +01003305 return 1;
3306}
3307
3308/* Dump all fields from <stats> into <out> using CSV format */
3309static int stats_dump_fields_csv(struct chunk *out, const struct field *stats)
3310{
3311 int field;
3312
3313 for (field = 0; field < ST_F_TOTAL_FIELDS; field++) {
3314 if (!stats_emit_raw_data_field(out, &stats[field]))
3315 return 0;
3316 if (!chunk_strcat(out, ","))
3317 return 0;
3318 }
3319 chunk_strcat(&trash, "\n");
Willy Tarreau12833bb2014-01-28 16:49:56 +01003320 return 1;
3321}
3322
Willy Tarreau60600742016-01-11 15:32:30 +01003323/* Dump all fields from <stats> into <out> using the HTML format. A column is
Willy Tarreau508a63f2016-01-11 15:28:40 +01003324 * reserved for the checkbox is ST_SHOWADMIN is set in <flags>. Some extra info
3325 * are provided if ST_SHLGNDS is present in <flags>.
Cyril Bonté70be45d2010-10-12 00:14:35 +02003326 */
Willy Tarreau60600742016-01-11 15:32:30 +01003327static int stats_dump_fields_html(struct chunk *out, const struct field *stats, unsigned int flags)
Cyril Bonté70be45d2010-10-12 00:14:35 +02003328{
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003329 struct chunk src;
3330
Willy Tarreaub5f66b82016-01-08 17:15:39 +01003331 if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_FE) {
Willy Tarreau60600742016-01-11 15:32:30 +01003332 chunk_appendf(out,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003333 /* name, queue */
3334 "<tr class=\"frontend\">");
Cyril Bonté70be45d2010-10-12 00:14:35 +02003335
Willy Tarreau508a63f2016-01-11 15:28:40 +01003336 if (flags & ST_SHOWADMIN) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003337 /* Column sub-heading for Enable or Disable server */
Willy Tarreau60600742016-01-11 15:32:30 +01003338 chunk_appendf(out, "<td></td>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003339 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02003340
Willy Tarreau60600742016-01-11 15:32:30 +01003341 chunk_appendf(out,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003342 "<td class=ac>"
3343 "<a name=\"%s/Frontend\"></a>"
3344 "<a class=lfsb href=\"#%s/Frontend\">Frontend</a></td>"
3345 "<td colspan=3></td>"
3346 "",
Willy Tarreaud72c9172016-01-06 15:26:40 +01003347 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_PXNAME));
Cyril Bonté70be45d2010-10-12 00:14:35 +02003348
Willy Tarreau60600742016-01-11 15:32:30 +01003349 chunk_appendf(out,
Willy Tarreau466c9b52012-12-23 02:25:03 +01003350 /* sessions rate : current */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003351 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01003352 "<tr><th>Current connection rate:</th><td>%s/s</td></tr>"
3353 "<tr><th>Current session rate:</th><td>%s/s</td></tr>"
3354 "",
Willy Tarreaud72c9172016-01-06 15:26:40 +01003355 U2H(stats[ST_F_RATE].u.u32),
Willy Tarreauc73810f2016-01-11 13:52:04 +01003356 U2H(stats[ST_F_CONN_RATE].u.u32),
Willy Tarreaud72c9172016-01-06 15:26:40 +01003357 U2H(stats[ST_F_RATE].u.u32));
Willy Tarreau466c9b52012-12-23 02:25:03 +01003358
Willy Tarreauf8211df2016-01-11 14:09:38 +01003359 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
Willy Tarreau60600742016-01-11 15:32:30 +01003360 chunk_appendf(out,
Willy Tarreau466c9b52012-12-23 02:25:03 +01003361 "<tr><th>Current request rate:</th><td>%s/s</td></tr>",
Willy Tarreaud72c9172016-01-06 15:26:40 +01003362 U2H(stats[ST_F_REQ_RATE].u.u32));
Willy Tarreau466c9b52012-12-23 02:25:03 +01003363
Willy Tarreau60600742016-01-11 15:32:30 +01003364 chunk_appendf(out,
Willy Tarreau466c9b52012-12-23 02:25:03 +01003365 "</table></div></u></td>"
3366 /* sessions rate : max */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003367 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01003368 "<tr><th>Max connection rate:</th><td>%s/s</td></tr>"
3369 "<tr><th>Max session rate:</th><td>%s/s</td></tr>"
3370 "",
Willy Tarreaud72c9172016-01-06 15:26:40 +01003371 U2H(stats[ST_F_RATE_MAX].u.u32),
Willy Tarreauc73810f2016-01-11 13:52:04 +01003372 U2H(stats[ST_F_CONN_RATE_MAX].u.u32),
Willy Tarreaud72c9172016-01-06 15:26:40 +01003373 U2H(stats[ST_F_RATE_MAX].u.u32));
Willy Tarreau466c9b52012-12-23 02:25:03 +01003374
Willy Tarreauf8211df2016-01-11 14:09:38 +01003375 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
Willy Tarreau60600742016-01-11 15:32:30 +01003376 chunk_appendf(out,
Willy Tarreau466c9b52012-12-23 02:25:03 +01003377 "<tr><th>Max request rate:</th><td>%s/s</td></tr>",
Willy Tarreaud72c9172016-01-06 15:26:40 +01003378 U2H(stats[ST_F_REQ_RATE_MAX].u.u32));
Willy Tarreau466c9b52012-12-23 02:25:03 +01003379
Willy Tarreau60600742016-01-11 15:32:30 +01003380 chunk_appendf(out,
Willy Tarreau466c9b52012-12-23 02:25:03 +01003381 "</table></div></u></td>"
3382 /* sessions rate : limit */
3383 "<td>%s</td>",
Willy Tarreaud72c9172016-01-06 15:26:40 +01003384 LIM2A(stats[ST_F_RATE_LIM].u.u32, "-"));
Cyril Bonté70be45d2010-10-12 00:14:35 +02003385
Willy Tarreau60600742016-01-11 15:32:30 +01003386 chunk_appendf(out,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003387 /* sessions: current, max, limit, total */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003388 "<td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003389 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01003390 "<tr><th>Cum. connections:</th><td>%s</td></tr>"
3391 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003392 "",
Willy Tarreauc73810f2016-01-11 13:52:04 +01003393 U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SMAX].u.u32), U2H(stats[ST_F_SLIM].u.u32),
Willy Tarreaud72c9172016-01-06 15:26:40 +01003394 U2H(stats[ST_F_STOT].u.u64),
Willy Tarreauc73810f2016-01-11 13:52:04 +01003395 U2H(stats[ST_F_CONN_TOT].u.u64),
Willy Tarreaud72c9172016-01-06 15:26:40 +01003396 U2H(stats[ST_F_STOT].u.u64));
Cyril Bonté70be45d2010-10-12 00:14:35 +02003397
Willy Tarreau466c9b52012-12-23 02:25:03 +01003398 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
Willy Tarreauf8211df2016-01-11 14:09:38 +01003399 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
Willy Tarreau60600742016-01-11 15:32:30 +01003400 chunk_appendf(out,
Willy Tarreau466c9b52012-12-23 02:25:03 +01003401 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
3402 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
3403 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
3404 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
3405 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
3406 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
3407 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
3408 "<tr><th>- other responses:</th><td>%s</td></tr>"
3409 "<tr><th>Intercepted requests:</th><td>%s</td></tr>"
3410 "",
Willy Tarreaud72c9172016-01-06 15:26:40 +01003411 U2H(stats[ST_F_REQ_TOT].u.u64),
3412 U2H(stats[ST_F_HRSP_1XX].u.u64),
3413 U2H(stats[ST_F_HRSP_2XX].u.u64),
3414 U2H(stats[ST_F_COMP_RSP].u.u64),
3415 stats[ST_F_HRSP_2XX].u.u64 ?
3416 (int)(100 * stats[ST_F_COMP_RSP].u.u64 / stats[ST_F_HRSP_2XX].u.u64) : 0,
3417 U2H(stats[ST_F_HRSP_3XX].u.u64),
3418 U2H(stats[ST_F_HRSP_4XX].u.u64),
3419 U2H(stats[ST_F_HRSP_5XX].u.u64),
3420 U2H(stats[ST_F_HRSP_OTHER].u.u64),
Willy Tarreau5b9bdff2016-01-11 14:40:47 +01003421 U2H(stats[ST_F_INTERCEPTED].u.u64));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003422 }
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02003423
Willy Tarreau60600742016-01-11 15:32:30 +01003424 chunk_appendf(out,
Willy Tarreau466c9b52012-12-23 02:25:03 +01003425 "</table></div></u></td>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05003426 /* sessions: lbtot, lastsess */
3427 "<td></td><td></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003428 /* bytes : in */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003429 "<td>%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003430 "",
Willy Tarreaud72c9172016-01-06 15:26:40 +01003431 U2H(stats[ST_F_BIN].u.u64));
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02003432
Willy Tarreau60600742016-01-11 15:32:30 +01003433 chunk_appendf(out,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003434 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
Willy Tarreau4e5d58e2015-07-04 14:35:15 +02003435 "<td>%s%s<div class=tips><table class=det>"
3436 "<tr><th>Response bytes in:</th><td>%s</td></tr>"
3437 "<tr><th>Compression in:</th><td>%s</td></tr>"
3438 "<tr><th>Compression out:</th><td>%s</td><td>(%d%%)</td></tr>"
3439 "<tr><th>Compression bypass:</th><td>%s</td></tr>"
3440 "<tr><th>Total bytes saved:</th><td>%s</td><td>(%d%%)</td></tr>"
3441 "</table></div>%s</td>",
Willy Tarreaud72c9172016-01-06 15:26:40 +01003442 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "<u>":"",
3443 U2H(stats[ST_F_BOUT].u.u64),
3444 U2H(stats[ST_F_BOUT].u.u64),
3445 U2H(stats[ST_F_COMP_IN].u.u64),
3446 U2H(stats[ST_F_COMP_OUT].u.u64),
3447 stats[ST_F_COMP_IN].u.u64 ? (int)(stats[ST_F_COMP_OUT].u.u64 * 100 / stats[ST_F_COMP_IN].u.u64) : 0,
3448 U2H(stats[ST_F_COMP_BYP].u.u64),
3449 U2H(stats[ST_F_COMP_IN].u.u64 - stats[ST_F_COMP_OUT].u.u64),
3450 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,
3451 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "</u>":"");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02003452
Willy Tarreau60600742016-01-11 15:32:30 +01003453 chunk_appendf(out,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003454 /* denied: req, resp */
3455 "<td>%s</td><td>%s</td>"
3456 /* errors : request, connect, response */
3457 "<td>%s</td><td></td><td></td>"
3458 /* warnings: retries, redispatches */
3459 "<td></td><td></td>"
3460 /* server status : reflect frontend status */
3461 "<td class=ac>%s</td>"
3462 /* rest of server: nothing */
3463 "<td class=ac colspan=8></td></tr>"
3464 "",
Willy Tarreaud72c9172016-01-06 15:26:40 +01003465 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
3466 U2H(stats[ST_F_EREQ].u.u64),
3467 field_str(stats, ST_F_STATUS));
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02003468 }
Willy Tarreaufa9512f2016-01-08 17:20:51 +01003469 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_SO) {
Willy Tarreau60600742016-01-11 15:32:30 +01003470 chunk_appendf(out, "<tr class=socket>");
Willy Tarreau508a63f2016-01-11 15:28:40 +01003471 if (flags & ST_SHOWADMIN) {
Willy Tarreaufa9512f2016-01-08 17:20:51 +01003472 /* Column sub-heading for Enable or Disable server */
Willy Tarreau60600742016-01-11 15:32:30 +01003473 chunk_appendf(out, "<td></td>");
Willy Tarreaufa9512f2016-01-08 17:20:51 +01003474 }
3475
Willy Tarreau60600742016-01-11 15:32:30 +01003476 chunk_appendf(out,
Willy Tarreaufa9512f2016-01-08 17:20:51 +01003477 /* frontend name, listener name */
3478 "<td class=ac><a name=\"%s/+%s\"></a>%s"
3479 "<a class=lfsb href=\"#%s/+%s\">%s</a>"
3480 "",
3481 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME),
3482 (flags & ST_SHLGNDS)?"<u>":"",
3483 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME), field_str(stats, ST_F_SVNAME));
3484
3485 if (flags & ST_SHLGNDS) {
Willy Tarreau60600742016-01-11 15:32:30 +01003486 chunk_appendf(out, "<div class=tips>");
Willy Tarreaufa9512f2016-01-08 17:20:51 +01003487
3488 if (isdigit(*field_str(stats, ST_F_ADDR)))
Willy Tarreau60600742016-01-11 15:32:30 +01003489 chunk_appendf(out, "IPv4: %s, ", field_str(stats, ST_F_ADDR));
Willy Tarreaufa9512f2016-01-08 17:20:51 +01003490 else if (*field_str(stats, ST_F_ADDR) == '[')
Willy Tarreau60600742016-01-11 15:32:30 +01003491 chunk_appendf(out, "IPv6: %s, ", field_str(stats, ST_F_ADDR));
Willy Tarreaufa9512f2016-01-08 17:20:51 +01003492 else if (*field_str(stats, ST_F_ADDR))
Willy Tarreau60600742016-01-11 15:32:30 +01003493 chunk_appendf(out, "%s, ", field_str(stats, ST_F_ADDR));
Willy Tarreaufa9512f2016-01-08 17:20:51 +01003494
3495 /* id */
Willy Tarreau60600742016-01-11 15:32:30 +01003496 chunk_appendf(out, "id: %d</div>", stats[ST_F_SID].u.u32);
Willy Tarreaufa9512f2016-01-08 17:20:51 +01003497 }
3498
Willy Tarreau60600742016-01-11 15:32:30 +01003499 chunk_appendf(out,
Willy Tarreaufa9512f2016-01-08 17:20:51 +01003500 /* queue */
3501 "%s</td><td colspan=3></td>"
3502 /* sessions rate: current, max, limit */
3503 "<td colspan=3>&nbsp;</td>"
3504 /* sessions: current, max, limit, total, lbtot, lastsess */
3505 "<td>%s</td><td>%s</td><td>%s</td>"
3506 "<td>%s</td><td>&nbsp;</td><td>&nbsp;</td>"
3507 /* bytes: in, out */
3508 "<td>%s</td><td>%s</td>"
3509 "",
3510 (flags & ST_SHLGNDS)?"</u>":"",
3511 U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SMAX].u.u32), U2H(stats[ST_F_SLIM].u.u32),
3512 U2H(stats[ST_F_STOT].u.u64), U2H(stats[ST_F_BIN].u.u64), U2H(stats[ST_F_BOUT].u.u64));
3513
Willy Tarreau60600742016-01-11 15:32:30 +01003514 chunk_appendf(out,
Willy Tarreaufa9512f2016-01-08 17:20:51 +01003515 /* denied: req, resp */
3516 "<td>%s</td><td>%s</td>"
3517 /* errors: request, connect, response */
3518 "<td>%s</td><td></td><td></td>"
3519 /* warnings: retries, redispatches */
3520 "<td></td><td></td>"
3521 /* server status: reflect listener status */
3522 "<td class=ac>%s</td>"
3523 /* rest of server: nothing */
3524 "<td class=ac colspan=8></td></tr>"
3525 "",
3526 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
3527 U2H(stats[ST_F_EREQ].u.u64),
3528 field_str(stats, ST_F_STATUS));
3529 }
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003530 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_SV) {
3531 const char *style;
Willy Tarreaub5f66b82016-01-08 17:15:39 +01003532
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003533 /* determine the style to use depending on the server's state,
3534 * its health and weight. There isn't a 1-to-1 mapping between
3535 * state and styles for the cases where the server is (still)
3536 * up. The reason is that we don't want to report nolb and
3537 * drain with the same color.
3538 */
Willy Tarreaub5f66b82016-01-08 17:15:39 +01003539
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003540 if (strcmp(field_str(stats, ST_F_STATUS), "DOWN") == 0 ||
3541 strcmp(field_str(stats, ST_F_STATUS), "DOWN (agent)") == 0) {
3542 style = "down";
3543 }
3544 else if (strcmp(field_str(stats, ST_F_STATUS), "DOWN ") == 0) {
3545 style = "going_up";
3546 }
3547 else if (strcmp(field_str(stats, ST_F_STATUS), "NOLB ") == 0) {
3548 style = "going_down";
3549 }
3550 else if (strcmp(field_str(stats, ST_F_STATUS), "NOLB") == 0) {
3551 style = "nolb";
3552 }
3553 else if (strcmp(field_str(stats, ST_F_STATUS), "no check") == 0) {
3554 style = "no_check";
3555 }
3556 else if (!stats[ST_F_CHKFAIL].type ||
3557 stats[ST_F_CHECK_HEALTH].u.u32 == stats[ST_F_CHECK_RISE].u.u32 + stats[ST_F_CHECK_FALL].u.u32 - 1) {
3558 /* no check or max health = UP */
3559 if (stats[ST_F_WEIGHT].u.u32)
3560 style = "up";
3561 else
3562 style = "draining";
3563 }
3564 else {
3565 style = "going_down";
3566 }
Willy Tarreaub5f66b82016-01-08 17:15:39 +01003567
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003568 if (memcmp(field_str(stats, ST_F_STATUS), "MAINT", 5) == 0)
Willy Tarreau60600742016-01-11 15:32:30 +01003569 chunk_appendf(out, "<tr class=\"maintain\">");
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003570 else
Willy Tarreau60600742016-01-11 15:32:30 +01003571 chunk_appendf(out,
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003572 "<tr class=\"%s_%s\">",
3573 (stats[ST_F_BCK].u.u32) ? "backup" : "active", style);
Willy Tarreaub5f66b82016-01-08 17:15:39 +01003574
Willy Tarreaub5f66b82016-01-08 17:15:39 +01003575
Willy Tarreau508a63f2016-01-11 15:28:40 +01003576 if (flags & ST_SHOWADMIN)
Willy Tarreau60600742016-01-11 15:32:30 +01003577 chunk_appendf(out,
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003578 "<td><input type=\"checkbox\" name=\"s\" value=\"%s\"></td>",
3579 field_str(stats, ST_F_SVNAME));
Willy Tarreaub5f66b82016-01-08 17:15:39 +01003580
Willy Tarreau60600742016-01-11 15:32:30 +01003581 chunk_appendf(out,
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003582 "<td class=ac><a name=\"%s/%s\"></a>%s"
3583 "<a class=lfsb href=\"#%s/%s\">%s</a>"
3584 "",
3585 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME),
3586 (flags & ST_SHLGNDS) ? "<u>" : "",
3587 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME), field_str(stats, ST_F_SVNAME));
Willy Tarreaub5f66b82016-01-08 17:15:39 +01003588
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003589 if (flags & ST_SHLGNDS) {
Willy Tarreau60600742016-01-11 15:32:30 +01003590 chunk_appendf(out, "<div class=tips>");
Willy Tarreaub5f66b82016-01-08 17:15:39 +01003591
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003592 if (isdigit(*field_str(stats, ST_F_ADDR)))
Willy Tarreau60600742016-01-11 15:32:30 +01003593 chunk_appendf(out, "IPv4: %s, ", field_str(stats, ST_F_ADDR));
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003594 else if (*field_str(stats, ST_F_ADDR) == '[')
Willy Tarreau60600742016-01-11 15:32:30 +01003595 chunk_appendf(out, "IPv6: %s, ", field_str(stats, ST_F_ADDR));
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003596 else if (*field_str(stats, ST_F_ADDR))
Willy Tarreau60600742016-01-11 15:32:30 +01003597 chunk_appendf(out, "%s, ", field_str(stats, ST_F_ADDR));
Willy Tarreaub5f66b82016-01-08 17:15:39 +01003598
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003599 /* id */
Willy Tarreau60600742016-01-11 15:32:30 +01003600 chunk_appendf(out, "id: %d", stats[ST_F_SID].u.u32);
Willy Tarreaub5f66b82016-01-08 17:15:39 +01003601
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003602 /* cookie */
3603 if (stats[ST_F_COOKIE].type) {
Willy Tarreau60600742016-01-11 15:32:30 +01003604 chunk_appendf(out, ", cookie: '");
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003605 chunk_initstr(&src, field_str(stats, ST_F_COOKIE));
Willy Tarreau60600742016-01-11 15:32:30 +01003606 chunk_htmlencode(out, &src);
3607 chunk_appendf(out, "'");
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003608 }
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02003609
Willy Tarreau60600742016-01-11 15:32:30 +01003610 chunk_appendf(out, "</div>");
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003611 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003612
Willy Tarreau60600742016-01-11 15:32:30 +01003613 chunk_appendf(out,
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003614 /* queue : current, max, limit */
3615 "%s</td><td>%s</td><td>%s</td><td>%s</td>"
3616 /* sessions rate : current, max, limit */
3617 "<td>%s</td><td>%s</td><td></td>"
3618 "",
3619 (flags & ST_SHLGNDS) ? "</u>" : "",
3620 U2H(stats[ST_F_QCUR].u.u32), U2H(stats[ST_F_QMAX].u.u32), LIM2A(stats[ST_F_QLIMIT].u.u32, "-"),
3621 U2H(stats[ST_F_RATE].u.u32), U2H(stats[ST_F_RATE_MAX].u.u32));
Willy Tarreau4607fad2016-01-06 15:41:29 +01003622
Willy Tarreau60600742016-01-11 15:32:30 +01003623 chunk_appendf(out,
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003624 /* sessions: current, max, limit, total */
3625 "<td>%s</td><td>%s</td><td>%s</td>"
3626 "<td><u>%s<div class=tips><table class=det>"
3627 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
3628 "",
3629 U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SMAX].u.u32), LIM2A(stats[ST_F_SLIM].u.u32, "-"),
3630 U2H(stats[ST_F_STOT].u.u64),
3631 U2H(stats[ST_F_STOT].u.u64));
Willy Tarreau4607fad2016-01-06 15:41:29 +01003632
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003633 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
Willy Tarreauf8211df2016-01-11 14:09:38 +01003634 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003635 unsigned long long tot;
Willy Tarreaua6f5a732016-01-08 16:59:56 +01003636
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003637 tot = stats[ST_F_HRSP_OTHER].u.u64;
3638 tot += stats[ST_F_HRSP_1XX].u.u64;
3639 tot += stats[ST_F_HRSP_2XX].u.u64;
3640 tot += stats[ST_F_HRSP_3XX].u.u64;
3641 tot += stats[ST_F_HRSP_4XX].u.u64;
3642 tot += stats[ST_F_HRSP_5XX].u.u64;
3643
Willy Tarreau60600742016-01-11 15:32:30 +01003644 chunk_appendf(out,
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003645 "<tr><th>Cum. HTTP responses:</th><td>%s</td></tr>"
3646 "<tr><th>- HTTP 1xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
3647 "<tr><th>- HTTP 2xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
3648 "<tr><th>- HTTP 3xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
3649 "<tr><th>- HTTP 4xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
3650 "<tr><th>- HTTP 5xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
3651 "<tr><th>- other responses:</th><td>%s</td><td>(%d%%)</td></tr>"
3652 "",
3653 U2H(tot),
3654 U2H(stats[ST_F_HRSP_1XX].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_1XX].u.u64 / tot) : 0,
3655 U2H(stats[ST_F_HRSP_2XX].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_2XX].u.u64 / tot) : 0,
3656 U2H(stats[ST_F_HRSP_3XX].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_3XX].u.u64 / tot) : 0,
3657 U2H(stats[ST_F_HRSP_4XX].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_4XX].u.u64 / tot) : 0,
3658 U2H(stats[ST_F_HRSP_5XX].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_5XX].u.u64 / tot) : 0,
3659 U2H(stats[ST_F_HRSP_OTHER].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_OTHER].u.u64 / tot) : 0);
Willy Tarreaua6f5a732016-01-08 16:59:56 +01003660 }
Willy Tarreaua6f5a732016-01-08 16:59:56 +01003661
Willy Tarreau60600742016-01-11 15:32:30 +01003662 chunk_appendf(out, "<tr><th colspan=3>Avg over last 1024 success. conn.</th></tr>");
3663 chunk_appendf(out, "<tr><th>- Queue time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_QTIME].u.u32));
3664 chunk_appendf(out, "<tr><th>- Connect time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_CTIME].u.u32));
Willy Tarreauf8211df2016-01-11 14:09:38 +01003665 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
Willy Tarreau60600742016-01-11 15:32:30 +01003666 chunk_appendf(out, "<tr><th>- Response time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_RTIME].u.u32));
3667 chunk_appendf(out, "<tr><th>- Total time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_TTIME].u.u32));
Willy Tarreau56a560a2009-09-22 19:27:35 +02003668
Willy Tarreau60600742016-01-11 15:32:30 +01003669 chunk_appendf(out,
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003670 "</table></div></u></td>"
3671 /* sessions: lbtot, last */
3672 "<td>%s</td><td>%s</td>",
3673 U2H(stats[ST_F_LBTOT].u.u64),
3674 human_time(stats[ST_F_LASTSESS].u.s32, 1));
Willy Tarreau91861262007-10-17 17:06:05 +02003675
Willy Tarreau60600742016-01-11 15:32:30 +01003676 chunk_appendf(out,
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003677 /* bytes : in, out */
3678 "<td>%s</td><td>%s</td>"
3679 /* denied: req, resp */
3680 "<td></td><td>%s</td>"
3681 /* errors : request, connect */
3682 "<td></td><td>%s</td>"
3683 /* errors : response */
3684 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
3685 /* warnings: retries, redispatches */
3686 "<td>%lld</td><td>%lld</td>"
3687 "",
3688 U2H(stats[ST_F_BIN].u.u64), U2H(stats[ST_F_BOUT].u.u64),
3689 U2H(stats[ST_F_DRESP].u.u64),
3690 U2H(stats[ST_F_ECON].u.u64),
3691 U2H(stats[ST_F_ERESP].u.u64),
3692 (long long)stats[ST_F_CLI_ABRT].u.u64,
3693 (long long)stats[ST_F_SRV_ABRT].u.u64,
3694 (long long)stats[ST_F_WRETR].u.u64,
3695 (long long)stats[ST_F_WREDIS].u.u64);
3696
3697 /* status, last change */
Willy Tarreau60600742016-01-11 15:32:30 +01003698 chunk_appendf(out, "<td class=ac>");
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003699
3700 /* FIXME!!!!
3701 * LASTCHG should contain the last change for *this* server and must be computed
3702 * properly above, as was done below, ie: this server if maint, otherwise ref server
3703 * if tracking. Note that ref is either local or remote depending on tracking.
3704 */
3705
3706
3707 if (memcmp(field_str(stats, ST_F_STATUS), "MAINT", 5) == 0) {
Willy Tarreau60600742016-01-11 15:32:30 +01003708 chunk_appendf(out, "%s MAINT", human_time(stats[ST_F_LASTCHG].u.u32, 1));
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003709 }
3710 else if (memcmp(field_str(stats, ST_F_STATUS), "no check", 5) == 0) {
Willy Tarreau60600742016-01-11 15:32:30 +01003711 chunk_strcat(out, "<i>no check</i>");
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003712 }
3713 else {
Willy Tarreau60600742016-01-11 15:32:30 +01003714 chunk_appendf(out, "%s %s", human_time(stats[ST_F_LASTCHG].u.u32, 1), field_str(stats, ST_F_STATUS));
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003715 if (memcmp(field_str(stats, ST_F_STATUS), "DOWN", 4) == 0) {
3716 if (stats[ST_F_CHECK_HEALTH].u.u32)
Willy Tarreau60600742016-01-11 15:32:30 +01003717 chunk_strcat(out, " &uarr;");
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003718 }
3719 else if (stats[ST_F_CHECK_HEALTH].u.u32 < stats[ST_F_CHECK_RISE].u.u32 + stats[ST_F_CHECK_FALL].u.u32 - 1)
Willy Tarreau60600742016-01-11 15:32:30 +01003720 chunk_strcat(out, " &darr;");
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003721 }
3722
3723 if (memcmp(field_str(stats, ST_F_STATUS), "DOWN", 4) == 0 &&
3724 stats[ST_F_AGENT_STATUS].type && !stats[ST_F_AGENT_HEALTH].u.u32) {
Willy Tarreau60600742016-01-11 15:32:30 +01003725 chunk_appendf(out,
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003726 "</td><td class=ac><u> %s",
3727 field_str(stats, ST_F_AGENT_STATUS));
3728
3729 if (stats[ST_F_AGENT_CODE].type)
Willy Tarreau60600742016-01-11 15:32:30 +01003730 chunk_appendf(out, "/%d", stats[ST_F_AGENT_CODE].u.u32);
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003731
3732 if (stats[ST_F_AGENT_DURATION].type && stats[ST_F_AGENT_DURATION].u.u64 >= 0)
Willy Tarreau60600742016-01-11 15:32:30 +01003733 chunk_appendf(out, " in %lums", (long)stats[ST_F_AGENT_DURATION].u.u64);
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003734
Willy Tarreau60600742016-01-11 15:32:30 +01003735 chunk_appendf(out, "<div class=tips>%s", field_str(stats, ST_F_AGENT_DESC));
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003736
3737 if (*field_str(stats, ST_F_LAST_AGT)) {
Willy Tarreau60600742016-01-11 15:32:30 +01003738 chunk_appendf(out, ": ");
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003739 chunk_initstr(&src, field_str(stats, ST_F_LAST_AGT));
Willy Tarreau60600742016-01-11 15:32:30 +01003740 chunk_htmlencode(out, &src);
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003741 }
Willy Tarreau60600742016-01-11 15:32:30 +01003742 chunk_appendf(out, "</div></u>");
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003743 }
3744 else if (stats[ST_F_CHECK_STATUS].type) {
Willy Tarreau60600742016-01-11 15:32:30 +01003745 chunk_appendf(out,
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003746 "</td><td class=ac><u> %s",
3747 field_str(stats, ST_F_CHECK_STATUS));
3748
3749 if (stats[ST_F_CHECK_CODE].type)
Willy Tarreau60600742016-01-11 15:32:30 +01003750 chunk_appendf(out, "/%d", stats[ST_F_CHECK_CODE].u.u32);
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003751
3752 if (stats[ST_F_CHECK_DURATION].type && stats[ST_F_CHECK_DURATION].u.u64 >= 0)
Willy Tarreau60600742016-01-11 15:32:30 +01003753 chunk_appendf(out, " in %lums", (long)stats[ST_F_CHECK_DURATION].u.u64);
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003754
Willy Tarreau60600742016-01-11 15:32:30 +01003755 chunk_appendf(out, "<div class=tips>%s", field_str(stats, ST_F_CHECK_DESC));
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003756
3757 if (*field_str(stats, ST_F_LAST_CHK)) {
Willy Tarreau60600742016-01-11 15:32:30 +01003758 chunk_appendf(out, ": ");
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003759 chunk_initstr(&src, field_str(stats, ST_F_LAST_CHK));
Willy Tarreau60600742016-01-11 15:32:30 +01003760 chunk_htmlencode(out, &src);
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003761 }
Willy Tarreau60600742016-01-11 15:32:30 +01003762 chunk_appendf(out, "</div></u>");
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003763 }
3764 else
Willy Tarreau60600742016-01-11 15:32:30 +01003765 chunk_appendf(out, "</td><td>");
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003766
Willy Tarreau60600742016-01-11 15:32:30 +01003767 chunk_appendf(out,
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003768 /* weight */
3769 "</td><td class=ac>%d</td>"
3770 /* act, bck */
3771 "<td class=ac>%s</td><td class=ac>%s</td>"
3772 "",
3773 stats[ST_F_WEIGHT].u.u32,
3774 stats[ST_F_BCK].u.u32 ? "-" : "Y",
3775 stats[ST_F_BCK].u.u32 ? "Y" : "-");
3776
3777 /* check failures: unique, fatal, down time */
3778 if (stats[ST_F_CHKFAIL].type) {
Willy Tarreau60600742016-01-11 15:32:30 +01003779 chunk_appendf(out, "<td><u>%lld", (long long)stats[ST_F_CHKFAIL].u.u64);
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003780
3781 if (stats[ST_F_HANAFAIL].type)
Willy Tarreau60600742016-01-11 15:32:30 +01003782 chunk_appendf(out, "/%lld", (long long)stats[ST_F_HANAFAIL].u.u64);
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003783
Willy Tarreau60600742016-01-11 15:32:30 +01003784 chunk_appendf(out,
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003785 "<div class=tips>Failed Health Checks%s</div></u></td>"
3786 "<td>%lld</td><td>%s</td>"
3787 "",
3788 stats[ST_F_HANAFAIL].type ? "/Health Analyses" : "",
3789 (long long)stats[ST_F_CHKDOWN].u.u64, human_time(stats[ST_F_DOWNTIME].u.u32, 1));
3790 }
3791 else if (strcmp(field_str(stats, ST_F_STATUS), "MAINT") != 0 && field_format(stats, ST_F_TRACKED) == FF_STR) {
3792 /* tracking a server (hence inherited maint would appear as "MAINT (via...)" */
Willy Tarreau60600742016-01-11 15:32:30 +01003793 chunk_appendf(out,
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003794 "<td class=ac colspan=3><a class=lfsb href=\"#%s\">via %s</a></td>",
3795 field_str(stats, ST_F_TRACKED), field_str(stats, ST_F_TRACKED));
3796 }
3797 else
Willy Tarreau60600742016-01-11 15:32:30 +01003798 chunk_appendf(out, "<td colspan=3></td>");
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003799
3800 /* throttle */
3801 if (stats[ST_F_THROTTLE].type)
Willy Tarreau60600742016-01-11 15:32:30 +01003802 chunk_appendf(out, "<td class=ac>%d %%</td></tr>\n", stats[ST_F_THROTTLE].u.u32);
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003803 else
Willy Tarreau60600742016-01-11 15:32:30 +01003804 chunk_appendf(out, "<td class=ac>-</td></tr>\n");
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003805 }
Willy Tarreaubbf84502016-01-08 17:25:50 +01003806 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_BE) {
Willy Tarreau60600742016-01-11 15:32:30 +01003807 chunk_appendf(out, "<tr class=\"backend\">");
Willy Tarreau508a63f2016-01-11 15:28:40 +01003808 if (flags & ST_SHOWADMIN) {
Willy Tarreaubbf84502016-01-08 17:25:50 +01003809 /* Column sub-heading for Enable or Disable server */
Willy Tarreau60600742016-01-11 15:32:30 +01003810 chunk_appendf(out, "<td></td>");
Willy Tarreaubbf84502016-01-08 17:25:50 +01003811 }
Willy Tarreau60600742016-01-11 15:32:30 +01003812 chunk_appendf(out,
Willy Tarreaubbf84502016-01-08 17:25:50 +01003813 "<td class=ac>"
3814 /* name */
3815 "%s<a name=\"%s/Backend\"></a>"
3816 "<a class=lfsb href=\"#%s/Backend\">Backend</a>"
3817 "",
3818 (flags & ST_SHLGNDS)?"<u>":"",
3819 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_PXNAME));
3820
3821 if (flags & ST_SHLGNDS) {
3822 /* balancing */
Willy Tarreau60600742016-01-11 15:32:30 +01003823 chunk_appendf(out, "<div class=tips>balancing: %s",
Willy Tarreauf1516d92016-01-11 14:48:36 +01003824 field_str(stats, ST_F_ALGO));
Willy Tarreaubbf84502016-01-08 17:25:50 +01003825
3826 /* cookie */
3827 if (stats[ST_F_COOKIE].type) {
Willy Tarreau60600742016-01-11 15:32:30 +01003828 chunk_appendf(out, ", cookie: '");
Willy Tarreaubbf84502016-01-08 17:25:50 +01003829 chunk_initstr(&src, field_str(stats, ST_F_COOKIE));
Willy Tarreau60600742016-01-11 15:32:30 +01003830 chunk_htmlencode(out, &src);
3831 chunk_appendf(out, "'");
Willy Tarreaubbf84502016-01-08 17:25:50 +01003832 }
Willy Tarreau60600742016-01-11 15:32:30 +01003833 chunk_appendf(out, "</div>");
Willy Tarreaubbf84502016-01-08 17:25:50 +01003834 }
3835
Willy Tarreau60600742016-01-11 15:32:30 +01003836 chunk_appendf(out,
Willy Tarreaubbf84502016-01-08 17:25:50 +01003837 "%s</td>"
3838 /* queue : current, max */
3839 "<td>%s</td><td>%s</td><td></td>"
3840 /* sessions rate : current, max, limit */
3841 "<td>%s</td><td>%s</td><td></td>"
3842 "",
3843 (flags & ST_SHLGNDS)?"</u>":"",
3844 U2H(stats[ST_F_QCUR].u.u32), U2H(stats[ST_F_QMAX].u.u32),
3845 U2H(stats[ST_F_RATE].u.u32), U2H(stats[ST_F_RATE_MAX].u.u32));
3846
Willy Tarreau60600742016-01-11 15:32:30 +01003847 chunk_appendf(out,
Willy Tarreaubbf84502016-01-08 17:25:50 +01003848 /* sessions: current, max, limit, total */
3849 "<td>%s</td><td>%s</td><td>%s</td>"
3850 "<td><u>%s<div class=tips><table class=det>"
3851 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
3852 "",
3853 U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SLIM].u.u32),
3854 U2H(stats[ST_F_STOT].u.u64),
3855 U2H(stats[ST_F_STOT].u.u64));
3856
3857 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
Willy Tarreauf8211df2016-01-11 14:09:38 +01003858 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
Willy Tarreau60600742016-01-11 15:32:30 +01003859 chunk_appendf(out,
Willy Tarreaubbf84502016-01-08 17:25:50 +01003860 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
3861 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
3862 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
3863 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
3864 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
3865 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
3866 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
3867 "<tr><th>- other responses:</th><td>%s</td></tr>"
3868 "<tr><th>Intercepted requests:</th><td>%s</td></tr>"
3869 "<tr><th colspan=3>Avg over last 1024 success. conn.</th></tr>"
3870 "",
3871 U2H(stats[ST_F_REQ_TOT].u.u64),
3872 U2H(stats[ST_F_HRSP_1XX].u.u64),
3873 U2H(stats[ST_F_HRSP_2XX].u.u64),
3874 U2H(stats[ST_F_COMP_RSP].u.u64),
3875 stats[ST_F_HRSP_2XX].u.u64 ?
3876 (int)(100 * stats[ST_F_COMP_RSP].u.u64 / stats[ST_F_HRSP_2XX].u.u64) : 0,
3877 U2H(stats[ST_F_HRSP_3XX].u.u64),
3878 U2H(stats[ST_F_HRSP_4XX].u.u64),
3879 U2H(stats[ST_F_HRSP_5XX].u.u64),
3880 U2H(stats[ST_F_HRSP_OTHER].u.u64),
Willy Tarreau5b9bdff2016-01-11 14:40:47 +01003881 U2H(stats[ST_F_INTERCEPTED].u.u64));
Willy Tarreaubbf84502016-01-08 17:25:50 +01003882 }
3883
Willy Tarreau60600742016-01-11 15:32:30 +01003884 chunk_appendf(out, "<tr><th>- Queue time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_QTIME].u.u32));
3885 chunk_appendf(out, "<tr><th>- Connect time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_QTIME].u.u32));
Willy Tarreauf8211df2016-01-11 14:09:38 +01003886 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
Willy Tarreau60600742016-01-11 15:32:30 +01003887 chunk_appendf(out, "<tr><th>- Response time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_RTIME].u.u32));
3888 chunk_appendf(out, "<tr><th>- Total time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_TTIME].u.u32));
Willy Tarreaubbf84502016-01-08 17:25:50 +01003889
Willy Tarreau60600742016-01-11 15:32:30 +01003890 chunk_appendf(out,
Willy Tarreaubbf84502016-01-08 17:25:50 +01003891 "</table></div></u></td>"
3892 /* sessions: lbtot, last */
3893 "<td>%s</td><td>%s</td>"
3894 /* bytes: in */
3895 "<td>%s</td>"
3896 "",
3897 U2H(stats[ST_F_LBTOT].u.u64),
3898 human_time(stats[ST_F_LASTSESS].u.s32, 1),
3899 U2H(stats[ST_F_BIN].u.u64));
3900
Willy Tarreau60600742016-01-11 15:32:30 +01003901 chunk_appendf(out,
Willy Tarreaubbf84502016-01-08 17:25:50 +01003902 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
3903 "<td>%s%s<div class=tips><table class=det>"
3904 "<tr><th>Response bytes in:</th><td>%s</td></tr>"
3905 "<tr><th>Compression in:</th><td>%s</td></tr>"
3906 "<tr><th>Compression out:</th><td>%s</td><td>(%d%%)</td></tr>"
3907 "<tr><th>Compression bypass:</th><td>%s</td></tr>"
3908 "<tr><th>Total bytes saved:</th><td>%s</td><td>(%d%%)</td></tr>"
3909 "</table></div>%s</td>",
3910 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "<u>":"",
3911 U2H(stats[ST_F_BOUT].u.u64),
3912 U2H(stats[ST_F_BOUT].u.u64),
3913 U2H(stats[ST_F_COMP_IN].u.u64),
3914 U2H(stats[ST_F_COMP_OUT].u.u64),
3915 stats[ST_F_COMP_IN].u.u64 ? (int)(stats[ST_F_COMP_OUT].u.u64 * 100 / stats[ST_F_COMP_IN].u.u64) : 0,
3916 U2H(stats[ST_F_COMP_BYP].u.u64),
3917 U2H(stats[ST_F_COMP_IN].u.u64 - stats[ST_F_COMP_BYP].u.u64),
3918 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,
3919 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "</u>":"");
3920
Willy Tarreau60600742016-01-11 15:32:30 +01003921 chunk_appendf(out,
Willy Tarreaubbf84502016-01-08 17:25:50 +01003922 /* denied: req, resp */
3923 "<td>%s</td><td>%s</td>"
3924 /* errors : request, connect */
3925 "<td></td><td>%s</td>"
3926 /* errors : response */
3927 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
3928 /* warnings: retries, redispatches */
3929 "<td>%lld</td><td>%lld</td>"
3930 /* backend status: reflect backend status (up/down): we display UP
3931 * if the backend has known working servers or if it has no server at
3932 * all (eg: for stats). Then we display the total weight, number of
3933 * active and backups. */
3934 "<td class=ac>%s %s</td><td class=ac>&nbsp;</td><td class=ac>%d</td>"
3935 "<td class=ac>%d</td><td class=ac>%d</td>"
3936 "",
3937 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
3938 U2H(stats[ST_F_ECON].u.u64),
3939 U2H(stats[ST_F_ERESP].u.u64),
3940 (long long)stats[ST_F_CLI_ABRT].u.u64,
3941 (long long)stats[ST_F_SRV_ABRT].u.u64,
3942 (long long)stats[ST_F_WRETR].u.u64, (long long)stats[ST_F_WREDIS].u.u64,
3943 human_time(stats[ST_F_LASTCHG].u.u32, 1),
3944 strcmp(field_str(stats, ST_F_STATUS), "DOWN") ? field_str(stats, ST_F_STATUS) : "<font color=\"red\"><b>DOWN</b></font>",
3945 stats[ST_F_WEIGHT].u.u32,
3946 stats[ST_F_ACT].u.u32, stats[ST_F_BCK].u.u32);
3947
Willy Tarreau60600742016-01-11 15:32:30 +01003948 chunk_appendf(out,
Willy Tarreaubbf84502016-01-08 17:25:50 +01003949 /* rest of backend: nothing, down transitions, total downtime, throttle */
3950 "<td class=ac>&nbsp;</td><td>%d</td>"
3951 "<td>%s</td>"
3952 "<td></td>"
3953 "</tr>",
3954 stats[ST_F_CHKDOWN].u.u32,
3955 stats[ST_F_DOWNTIME].type ? human_time(stats[ST_F_DOWNTIME].u.u32, 1) : "&nbsp;");
3956 }
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003957 return 1;
3958}
3959
Willy Tarreau501f6022016-01-08 17:37:22 +01003960static int stats_dump_one_line(const struct field *stats, unsigned int flags, struct proxy *px, struct appctx *appctx)
3961{
Willy Tarreau508a63f2016-01-11 15:28:40 +01003962 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN))
3963 flags |= ST_SHOWADMIN;
Willy Tarreau501f6022016-01-08 17:37:22 +01003964
3965 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
Willy Tarreau60600742016-01-11 15:32:30 +01003966 return stats_dump_fields_html(&trash, stats, flags);
Willy Tarreau501f6022016-01-08 17:37:22 +01003967 else
3968 return stats_dump_fields_csv(&trash, stats);
3969}
3970
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003971/* Dumps a frontend's line to the trash for the current proxy <px> and uses
3972 * the state from stream interface <si>. The caller is responsible for clearing
3973 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
3974 */
3975static int stats_dump_fe_stats(struct stream_interface *si, struct proxy *px)
3976{
3977 struct appctx *appctx = __objt_appctx(si->end);
3978
3979 if (!(px->cap & PR_CAP_FE))
3980 return 0;
3981
3982 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_FE)))
3983 return 0;
3984
3985 memset(&stats, 0, sizeof(stats));
3986
3987 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
3988 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, "FRONTEND");
Willy Tarreauf8211df2016-01-11 14:09:38 +01003989 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003990 stats[ST_F_SCUR] = mkf_u32(0, px->feconn);
3991 stats[ST_F_SMAX] = mkf_u32(FN_MAX, px->fe_counters.conn_max);
3992 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, px->maxconn);
3993 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, px->fe_counters.cum_sess);
3994 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, px->fe_counters.bytes_in);
3995 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, px->fe_counters.bytes_out);
3996 stats[ST_F_DREQ] = mkf_u64(FN_COUNTER, px->fe_counters.denied_req);
3997 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, px->fe_counters.denied_resp);
3998 stats[ST_F_EREQ] = mkf_u64(FN_COUNTER, px->fe_counters.failed_req);
3999 stats[ST_F_STATUS] = mkf_str(FO_STATUS, px->state == PR_STREADY ? "OPEN" : px->state == PR_STFULL ? "FULL" : "STOP");
4000 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
4001 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
4002 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, 0);
4003 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_FE);
4004 stats[ST_F_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_sess_per_sec));
4005 stats[ST_F_RATE_LIM] = mkf_u32(FO_CONFIG|FN_LIMIT, px->fe_sps_lim);
4006 stats[ST_F_RATE_MAX] = mkf_u32(FN_MAX, px->fe_counters.sps_max);
4007
4008 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
4009 if (px->mode == PR_MODE_HTTP) {
4010 stats[ST_F_HRSP_1XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[1]);
4011 stats[ST_F_HRSP_2XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[2]);
4012 stats[ST_F_HRSP_3XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[3]);
4013 stats[ST_F_HRSP_4XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[4]);
4014 stats[ST_F_HRSP_5XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[5]);
4015 stats[ST_F_HRSP_OTHER] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[0]);
Willy Tarreau5b9bdff2016-01-11 14:40:47 +01004016 stats[ST_F_INTERCEPTED] = mkf_u64(FN_COUNTER, px->fe_counters.intercepted_req);
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004017 }
4018
4019 /* requests : req_rate, req_rate_max, req_tot, */
4020 stats[ST_F_REQ_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_req_per_sec));
4021 stats[ST_F_REQ_RATE_MAX] = mkf_u32(FN_MAX, px->fe_counters.p.http.rps_max);
4022 stats[ST_F_REQ_TOT] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cum_req);
4023
4024 /* compression: in, out, bypassed, responses */
4025 stats[ST_F_COMP_IN] = mkf_u64(FN_COUNTER, px->fe_counters.comp_in);
4026 stats[ST_F_COMP_OUT] = mkf_u64(FN_COUNTER, px->fe_counters.comp_out);
4027 stats[ST_F_COMP_BYP] = mkf_u64(FN_COUNTER, px->fe_counters.comp_byp);
4028 stats[ST_F_COMP_RSP] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.comp_rsp);
4029
Willy Tarreauc73810f2016-01-11 13:52:04 +01004030 /* connections : conn_rate, conn_rate_max, conn_tot, conn_max */
4031 stats[ST_F_CONN_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_conn_per_sec));
4032 stats[ST_F_CONN_RATE_MAX] = mkf_u32(FN_MAX, px->fe_counters.cps_max);
4033 stats[ST_F_CONN_TOT] = mkf_u64(FN_COUNTER, px->fe_counters.cum_conn);
4034
Willy Tarreau501f6022016-01-08 17:37:22 +01004035 return stats_dump_one_line(stats, 0, px, appctx);
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004036}
4037
4038/* Dumps a line for listener <l> and proxy <px> to the trash and uses the state
4039 * from stream interface <si>, and stats flags <flags>. The caller is responsible
4040 * for clearing the trash if needed. Returns non-zero if it emits anything, zero
4041 * otherwise.
4042 */
4043static int stats_dump_li_stats(struct stream_interface *si, struct proxy *px, struct listener *l, int flags)
4044{
4045 struct appctx *appctx = __objt_appctx(si->end);
4046 struct chunk *out = get_trash_chunk();
4047
4048 chunk_reset(out);
4049 memset(&stats, 0, sizeof(stats));
4050
4051 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
4052 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, l->name);
Willy Tarreauf8211df2016-01-11 14:09:38 +01004053 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004054 stats[ST_F_SCUR] = mkf_u32(0, l->nbconn);
4055 stats[ST_F_SMAX] = mkf_u32(FN_MAX, l->counters->conn_max);
4056 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, l->maxconn);
4057 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, l->counters->cum_conn);
4058 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, l->counters->bytes_in);
4059 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, l->counters->bytes_out);
4060 stats[ST_F_DREQ] = mkf_u64(FN_COUNTER, l->counters->denied_req);
4061 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, l->counters->denied_resp);
4062 stats[ST_F_EREQ] = mkf_u64(FN_COUNTER, l->counters->failed_req);
4063 stats[ST_F_STATUS] = mkf_str(FO_STATUS, (l->nbconn < l->maxconn) ? (l->state == LI_LIMITED) ? "WAITING" : "OPEN" : "FULL");
4064 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
4065 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
4066 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, l->luid);
4067 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_SO);
4068
4069 if (flags & ST_SHLGNDS) {
4070 char str[INET6_ADDRSTRLEN];
4071 int port;
4072
4073 port = get_host_port(&l->addr);
4074 switch (addr_to_str(&l->addr, str, sizeof(str))) {
4075 case AF_INET:
4076 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
4077 chunk_appendf(out, "%s:%d", str, port);
4078 break;
4079 case AF_INET6:
4080 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
4081 chunk_appendf(out, "[%s]:%d", str, port);
4082 break;
4083 case AF_UNIX:
4084 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, "unix");
4085 break;
4086 case -1:
4087 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
4088 chunk_strcat(out, strerror(errno));
4089 break;
4090 default: /* address family not supported */
4091 break;
4092 }
4093 }
4094
Willy Tarreau501f6022016-01-08 17:37:22 +01004095 return stats_dump_one_line(stats, flags, px, appctx);
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004096}
4097
4098enum srv_stats_state {
4099 SRV_STATS_STATE_DOWN = 0,
4100 SRV_STATS_STATE_DOWN_AGENT,
4101 SRV_STATS_STATE_GOING_UP,
4102 SRV_STATS_STATE_UP_GOING_DOWN,
4103 SRV_STATS_STATE_UP,
4104 SRV_STATS_STATE_NOLB_GOING_DOWN,
Simon Horman4d2eab62015-04-23 14:51:26 +09004105 SRV_STATS_STATE_NOLB,
4106 SRV_STATS_STATE_DRAIN_GOING_DOWN,
4107 SRV_STATS_STATE_DRAIN,
Simon Hormanb167b6b2015-04-23 14:51:29 +09004108 SRV_STATS_STATE_DRAIN_AGENT,
Simon Horman4d2eab62015-04-23 14:51:26 +09004109 SRV_STATS_STATE_NO_CHECK,
4110
4111 SRV_STATS_STATE_COUNT, /* Must be last */
4112};
4113
Willy Tarreau2b96cf12016-01-06 19:25:38 +01004114static const char *srv_hlt_st[SRV_STATS_STATE_COUNT] = {
4115 [SRV_STATS_STATE_DOWN] = "DOWN",
4116 [SRV_STATS_STATE_DOWN_AGENT] = "DOWN (agent)",
4117 [SRV_STATS_STATE_GOING_UP] = "DOWN %d/%d",
4118 [SRV_STATS_STATE_UP_GOING_DOWN] = "UP %d/%d",
4119 [SRV_STATS_STATE_UP] = "UP",
4120 [SRV_STATS_STATE_NOLB_GOING_DOWN] = "NOLB %d/%d",
4121 [SRV_STATS_STATE_NOLB] = "NOLB",
4122 [SRV_STATS_STATE_DRAIN_GOING_DOWN] = "DRAIN %d/%d",
4123 [SRV_STATS_STATE_DRAIN] = "DRAIN",
4124 [SRV_STATS_STATE_DRAIN_AGENT] = "DRAIN (agent)",
4125 [SRV_STATS_STATE_NO_CHECK] = "no check"
4126};
4127
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004128/* Dumps a line for server <sv> and proxy <px> to the trash and uses the state
4129 * from stream interface <si>, stats flags <flags>, and server state <state>.
4130 * The caller is responsible for clearing the trash if needed. Returns non-zero
Simon Horman4d2eab62015-04-23 14:51:26 +09004131 * if it emits anything, zero otherwise.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004132 */
Willy Tarreauba2f2642016-01-07 09:54:40 +01004133static int stats_dump_sv_stats(struct stream_interface *si, struct proxy *px, int flags, struct server *sv)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004134{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004135 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau32091232014-05-16 13:52:00 +02004136 struct server *via, *ref;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004137 char str[INET6_ADDRSTRLEN];
Willy Tarreau2b96cf12016-01-06 19:25:38 +01004138 struct chunk *out = get_trash_chunk();
Willy Tarreauba2f2642016-01-07 09:54:40 +01004139 enum srv_stats_state state;
Willy Tarreau2b96cf12016-01-06 19:25:38 +01004140 char *fld_status;
Willy Tarreau32091232014-05-16 13:52:00 +02004141 /* we have "via" which is the tracked server as described in the configuration,
4142 * and "ref" which is the checked server and the end of the chain.
4143 */
4144 via = sv->track ? sv->track : sv;
4145 ref = via;
4146 while (ref->track)
4147 ref = ref->track;
4148
Willy Tarreauba2f2642016-01-07 09:54:40 +01004149 if (sv->state == SRV_ST_RUNNING || sv->state == SRV_ST_STARTING) {
4150 if ((ref->check.state & CHK_ST_ENABLED) &&
4151 (ref->check.health < ref->check.rise + ref->check.fall - 1)) {
4152 state = SRV_STATS_STATE_UP_GOING_DOWN;
Willy Tarreauba2f2642016-01-07 09:54:40 +01004153 } else {
4154 state = SRV_STATS_STATE_UP;
Willy Tarreauba2f2642016-01-07 09:54:40 +01004155 }
4156
Willy Tarreauba2f2642016-01-07 09:54:40 +01004157 if (sv->admin & SRV_ADMF_DRAIN) {
4158 if (ref->agent.state & CHK_ST_ENABLED)
4159 state = SRV_STATS_STATE_DRAIN_AGENT;
4160 else if (state == SRV_STATS_STATE_UP_GOING_DOWN)
4161 state = SRV_STATS_STATE_DRAIN_GOING_DOWN;
4162 else
4163 state = SRV_STATS_STATE_DRAIN;
4164 }
4165
4166 if (state == SRV_STATS_STATE_UP && !(ref->check.state & CHK_ST_ENABLED)) {
4167 state = SRV_STATS_STATE_NO_CHECK;
Willy Tarreauba2f2642016-01-07 09:54:40 +01004168 }
4169 }
4170 else if (sv->state == SRV_ST_STOPPING) {
4171 if ((!(sv->check.state & CHK_ST_ENABLED) && !sv->track) ||
4172 (ref->check.health == ref->check.rise + ref->check.fall - 1)) {
4173 state = SRV_STATS_STATE_NOLB;
Willy Tarreauba2f2642016-01-07 09:54:40 +01004174 } else {
4175 state = SRV_STATS_STATE_NOLB_GOING_DOWN;
Willy Tarreauba2f2642016-01-07 09:54:40 +01004176 }
4177 }
4178 else { /* stopped */
4179 if ((ref->agent.state & CHK_ST_ENABLED) && !ref->agent.health) {
4180 state = SRV_STATS_STATE_DOWN_AGENT;
Willy Tarreauba2f2642016-01-07 09:54:40 +01004181 } else if ((ref->check.state & CHK_ST_ENABLED) && !ref->check.health) {
4182 state = SRV_STATS_STATE_DOWN; /* DOWN */
Willy Tarreauba2f2642016-01-07 09:54:40 +01004183 } else if ((ref->agent.state & CHK_ST_ENABLED) || (ref->check.state & CHK_ST_ENABLED)) {
4184 state = SRV_STATS_STATE_GOING_UP;
Willy Tarreauba2f2642016-01-07 09:54:40 +01004185 } else {
4186 state = SRV_STATS_STATE_DOWN; /* DOWN, unchecked */
Willy Tarreauba2f2642016-01-07 09:54:40 +01004187 }
4188 }
4189
Willy Tarreau2b96cf12016-01-06 19:25:38 +01004190 chunk_reset(out);
4191 memset(&stats, 0, sizeof(stats));
4192
4193 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
4194 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, sv->id);
Willy Tarreauf8211df2016-01-11 14:09:38 +01004195 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
Willy Tarreau2b96cf12016-01-06 19:25:38 +01004196 stats[ST_F_QCUR] = mkf_u32(0, sv->nbpend);
4197 stats[ST_F_QMAX] = mkf_u32(FN_MAX, sv->counters.nbpend_max);
4198 stats[ST_F_SCUR] = mkf_u32(0, sv->cur_sess);
4199 stats[ST_F_SMAX] = mkf_u32(FN_MAX, sv->counters.cur_sess_max);
4200
4201 if (sv->maxconn)
4202 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, sv->maxconn);
4203
4204 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, sv->counters.cum_sess);
4205 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, sv->counters.bytes_in);
4206 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, sv->counters.bytes_out);
4207 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, sv->counters.failed_secu);
4208 stats[ST_F_ECON] = mkf_u64(FN_COUNTER, sv->counters.failed_conns);
4209 stats[ST_F_ERESP] = mkf_u64(FN_COUNTER, sv->counters.failed_resp);
4210 stats[ST_F_WRETR] = mkf_u64(FN_COUNTER, sv->counters.retries);
4211 stats[ST_F_WREDIS] = mkf_u64(FN_COUNTER, sv->counters.redispatches);
4212
4213 /* status */
4214 fld_status = chunk_newstr(out);
4215 if (sv->admin & SRV_ADMF_IMAINT)
4216 chunk_appendf(out, "MAINT (via %s/%s)", via->proxy->id, via->id);
4217 else if (sv->admin & SRV_ADMF_MAINT)
4218 chunk_appendf(out, "MAINT");
4219 else
4220 chunk_appendf(out,
4221 srv_hlt_st[state],
4222 (ref->state != SRV_ST_STOPPED) ? (ref->check.health - ref->check.rise + 1) : (ref->check.health),
4223 (ref->state != SRV_ST_STOPPED) ? (ref->check.fall) : (ref->check.rise));
4224
4225 stats[ST_F_STATUS] = mkf_str(FO_STATUS, fld_status);
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004226 stats[ST_F_LASTCHG] = mkf_u32(FN_AGE, now.tv_sec - sv->last_change);
4227 stats[ST_F_WEIGHT] = mkf_u32(FN_AVG, (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv);
4228 stats[ST_F_ACT] = mkf_u32(FO_STATUS, (sv->flags & SRV_F_BACKUP) ? 0 : 1);
4229 stats[ST_F_BCK] = mkf_u32(FO_STATUS, (sv->flags & SRV_F_BACKUP) ? 1 : 0);
Willy Tarreau91861262007-10-17 17:06:05 +02004230
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004231 /* check failures: unique, fatal; last change, total downtime */
4232 if (sv->check.state & CHK_ST_ENABLED) {
4233 stats[ST_F_CHKFAIL] = mkf_u64(FN_COUNTER, sv->counters.failed_checks);
4234 stats[ST_F_CHKDOWN] = mkf_u64(FN_COUNTER, sv->counters.down_trans);
4235 stats[ST_F_DOWNTIME] = mkf_u32(FN_COUNTER, srv_downtime(sv));
4236 }
Willy Tarreau164d4a92016-01-06 19:48:21 +01004237
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004238 if (sv->maxqueue)
4239 stats[ST_F_QLIMIT] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->maxqueue);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004240
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004241 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
4242 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
4243 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, sv->puid);
Willy Tarreau91861262007-10-17 17:06:05 +02004244
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004245 if (sv->state == SRV_ST_STARTING && !server_is_draining(sv))
4246 stats[ST_F_THROTTLE] = mkf_u32(FN_AVG, server_throttle_rate(sv));
Willy Tarreauf5b1cc32014-06-17 12:20:59 +02004247
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004248 stats[ST_F_LBTOT] = mkf_u64(FN_COUNTER, sv->counters.cum_lbconn);
Willy Tarreau91861262007-10-17 17:06:05 +02004249
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004250 if (sv->track) {
4251 char *fld_track = chunk_newstr(out);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004252
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004253 chunk_appendf(out, "%s/%s", sv->track->proxy->id, sv->track->id);
4254 stats[ST_F_TRACKED] = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, fld_track);
4255 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004256
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004257 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_SV);
4258 stats[ST_F_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&sv->sess_per_sec));
4259 stats[ST_F_RATE_MAX] = mkf_u32(FN_MAX, sv->counters.sps_max);
Willy Tarreau164d4a92016-01-06 19:48:21 +01004260
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004261 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) {
4262 const char *fld_chksts;
Willy Tarreau164d4a92016-01-06 19:48:21 +01004263
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004264 fld_chksts = chunk_newstr(out);
4265 chunk_strcat(out, "* "); // for check in progress
4266 chunk_strcat(out, get_check_status_info(sv->check.status));
4267 if (!(sv->check.state & CHK_ST_INPROGRESS))
4268 fld_chksts += 2; // skip "* "
4269 stats[ST_F_CHECK_STATUS] = mkf_str(FN_OUTPUT, fld_chksts);
Willy Tarreau91861262007-10-17 17:06:05 +02004270
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004271 if (sv->check.status >= HCHK_STATUS_L57DATA)
4272 stats[ST_F_CHECK_CODE] = mkf_u32(FN_OUTPUT, sv->check.code);
Willy Tarreaucf2924b2014-05-23 12:15:15 +02004273
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004274 if (sv->check.status >= HCHK_STATUS_CHECKED)
4275 stats[ST_F_CHECK_DURATION] = mkf_u64(FN_DURATION, sv->check.duration);
Willy Tarreaucf2924b2014-05-23 12:15:15 +02004276
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004277 stats[ST_F_CHECK_DESC] = mkf_str(FN_OUTPUT, get_check_status_description(sv->check.status));
4278 stats[ST_F_LAST_CHK] = mkf_str(FN_OUTPUT, sv->check.desc);
4279 stats[ST_F_CHECK_RISE] = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.rise);
4280 stats[ST_F_CHECK_FALL] = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.fall);
4281 stats[ST_F_CHECK_HEALTH] = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.health);
4282 }
Willy Tarreaucf2924b2014-05-23 12:15:15 +02004283
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004284 if ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) {
4285 const char *fld_chksts;
Willy Tarreaudd7354b2016-01-08 13:47:26 +01004286
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004287 fld_chksts = chunk_newstr(out);
4288 chunk_strcat(out, "* "); // for check in progress
4289 chunk_strcat(out, get_check_status_info(sv->agent.status));
4290 if (!(sv->agent.state & CHK_ST_INPROGRESS))
4291 fld_chksts += 2; // skip "* "
4292 stats[ST_F_AGENT_STATUS] = mkf_str(FN_OUTPUT, fld_chksts);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004293
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004294 if (sv->agent.status >= HCHK_STATUS_L57DATA)
4295 stats[ST_F_AGENT_CODE] = mkf_u32(FN_OUTPUT, sv->agent.code);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004296
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004297 if (sv->agent.status >= HCHK_STATUS_CHECKED)
4298 stats[ST_F_AGENT_DURATION] = mkf_u64(FN_DURATION, sv->agent.duration);
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01004299
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004300 stats[ST_F_AGENT_DESC] = mkf_str(FN_OUTPUT, get_check_status_description(sv->agent.status));
4301 stats[ST_F_LAST_AGT] = mkf_str(FN_OUTPUT, sv->agent.desc);
4302 stats[ST_F_AGENT_RISE] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.rise);
4303 stats[ST_F_AGENT_FALL] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.fall);
4304 stats[ST_F_AGENT_HEALTH] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.health);
4305 }
Willy Tarreaudd7354b2016-01-08 13:47:26 +01004306
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004307 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
4308 if (px->mode == PR_MODE_HTTP) {
4309 stats[ST_F_HRSP_1XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[1]);
4310 stats[ST_F_HRSP_2XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[2]);
4311 stats[ST_F_HRSP_3XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[3]);
4312 stats[ST_F_HRSP_4XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[4]);
4313 stats[ST_F_HRSP_5XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[5]);
4314 stats[ST_F_HRSP_OTHER] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[0]);
4315 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004316
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004317 if (ref->observe)
4318 stats[ST_F_HANAFAIL] = mkf_u64(FN_COUNTER, sv->counters.failed_hana);
Willy Tarreau164d4a92016-01-06 19:48:21 +01004319
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004320 stats[ST_F_CLI_ABRT] = mkf_u64(FN_COUNTER, sv->counters.cli_aborts);
4321 stats[ST_F_SRV_ABRT] = mkf_u64(FN_COUNTER, sv->counters.srv_aborts);
4322 stats[ST_F_LASTSESS] = mkf_s32(FN_AGE, srv_lastsession(sv));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004323
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004324 stats[ST_F_QTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.q_time, TIME_STATS_SAMPLES));
4325 stats[ST_F_CTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.c_time, TIME_STATS_SAMPLES));
4326 stats[ST_F_RTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.d_time, TIME_STATS_SAMPLES));
4327 stats[ST_F_TTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.t_time, TIME_STATS_SAMPLES));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004328
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004329 if (flags & ST_SHLGNDS) {
4330 switch (addr_to_str(&sv->addr, str, sizeof(str))) {
4331 case AF_INET:
4332 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
4333 chunk_appendf(out, "%s:%d", str, get_host_port(&sv->addr));
4334 break;
4335 case AF_INET6:
4336 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
4337 chunk_appendf(out, "[%s]:%d", str, get_host_port(&sv->addr));
4338 break;
4339 case AF_UNIX:
4340 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, "unix");
4341 break;
4342 case -1:
4343 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
4344 chunk_strcat(out, strerror(errno));
4345 break;
4346 default: /* address family not supported */
4347 break;
Willy Tarreauf4659942013-11-28 10:50:06 +01004348 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004349
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004350 if (sv->cookie)
4351 stats[ST_F_COOKIE] = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, sv->cookie);
4352 }
4353
Willy Tarreau501f6022016-01-08 17:37:22 +01004354 return stats_dump_one_line(stats, flags, px, appctx);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004355}
4356
4357/* Dumps a line for backend <px> to the trash for and uses the state from stream
4358 * interface <si> and stats flags <flags>. The caller is responsible for clearing
4359 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
4360 */
4361static int stats_dump_be_stats(struct stream_interface *si, struct proxy *px, int flags)
4362{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004363 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004364
4365 if (!(px->cap & PR_CAP_BE))
4366 return 0;
4367
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004368 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_BE)))
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004369 return 0;
4370
Willy Tarreauf6eecbe2016-01-06 16:14:50 +01004371 memset(&stats, 0, sizeof(stats));
4372
4373 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
4374 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, "BACKEND");
Willy Tarreauf8211df2016-01-11 14:09:38 +01004375 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
Willy Tarreauf6eecbe2016-01-06 16:14:50 +01004376 stats[ST_F_QCUR] = mkf_u32(0, px->nbpend);
4377 stats[ST_F_QMAX] = mkf_u32(FN_MAX, px->be_counters.nbpend_max);
4378 stats[ST_F_SCUR] = mkf_u32(FO_CONFIG|FN_LIMIT, px->beconn);
4379 stats[ST_F_SMAX] = mkf_u32(FN_MAX, px->be_counters.conn_max);
4380 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, px->fullconn);
4381 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, px->be_counters.cum_conn);
4382 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, px->be_counters.bytes_in);
4383 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, px->be_counters.bytes_out);
4384 stats[ST_F_DREQ] = mkf_u64(FN_COUNTER, px->be_counters.denied_req);
4385 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, px->be_counters.denied_resp);
4386 stats[ST_F_ECON] = mkf_u64(FN_COUNTER, px->be_counters.failed_conns);
4387 stats[ST_F_ERESP] = mkf_u64(FN_COUNTER, px->be_counters.failed_resp);
4388 stats[ST_F_WRETR] = mkf_u64(FN_COUNTER, px->be_counters.retries);
4389 stats[ST_F_WREDIS] = mkf_u64(FN_COUNTER, px->be_counters.redispatches);
4390 stats[ST_F_STATUS] = mkf_str(FO_STATUS, (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" : "DOWN");
4391 stats[ST_F_WEIGHT] = mkf_u32(FN_AVG, (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv);
4392 stats[ST_F_ACT] = mkf_u32(0, px->srv_act);
4393 stats[ST_F_BCK] = mkf_u32(0, px->srv_bck);
4394 stats[ST_F_CHKDOWN] = mkf_u64(FN_COUNTER, px->down_trans);
4395 stats[ST_F_LASTCHG] = mkf_u32(FN_AGE, now.tv_sec - px->last_change);
Willy Tarreau7344f472016-01-11 12:04:02 +01004396 if (px->srv)
4397 stats[ST_F_DOWNTIME] = mkf_u32(FN_COUNTER, be_downtime(px));
4398
Willy Tarreauf6eecbe2016-01-06 16:14:50 +01004399 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
4400 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
4401 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, 0);
4402 stats[ST_F_LBTOT] = mkf_u64(FN_COUNTER, px->be_counters.cum_lbconn);
4403 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_BE);
4404 stats[ST_F_RATE] = mkf_u32(0, read_freq_ctr(&px->be_sess_per_sec));
4405 stats[ST_F_RATE_MAX] = mkf_u32(0, px->be_counters.sps_max);
Willy Tarreauf1516d92016-01-11 14:48:36 +01004406
Willy Tarreaue4847c62016-01-08 15:43:54 +01004407 if (flags & ST_SHLGNDS) {
4408 if (px->cookie_name)
4409 stats[ST_F_COOKIE] = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, px->cookie_name);
Willy Tarreauf1516d92016-01-11 14:48:36 +01004410 stats[ST_F_ALGO] = mkf_str(FO_CONFIG|FS_SERVICE, backend_lb_algo_str(px->lbprm.algo & BE_LB_ALGO));
Willy Tarreaue4847c62016-01-08 15:43:54 +01004411 }
Willy Tarreauf6eecbe2016-01-06 16:14:50 +01004412
4413 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
4414 if (px->mode == PR_MODE_HTTP) {
4415 stats[ST_F_REQ_TOT] = mkf_u64(FN_COUNTER, px->be_counters.p.http.cum_req);
4416 stats[ST_F_HRSP_1XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[1]);
4417 stats[ST_F_HRSP_2XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[2]);
4418 stats[ST_F_HRSP_3XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[3]);
4419 stats[ST_F_HRSP_4XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[4]);
4420 stats[ST_F_HRSP_5XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[5]);
4421 stats[ST_F_HRSP_OTHER] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[0]);
Willy Tarreau5b9bdff2016-01-11 14:40:47 +01004422 stats[ST_F_INTERCEPTED] = mkf_u64(FN_COUNTER, px->be_counters.intercepted_req);
Willy Tarreauf6eecbe2016-01-06 16:14:50 +01004423 }
4424
4425 stats[ST_F_CLI_ABRT] = mkf_u64(FN_COUNTER, px->be_counters.cli_aborts);
4426 stats[ST_F_SRV_ABRT] = mkf_u64(FN_COUNTER, px->be_counters.srv_aborts);
4427
4428 /* compression: in, out, bypassed, responses */
4429 stats[ST_F_COMP_IN] = mkf_u64(FN_COUNTER, px->be_counters.comp_in);
4430 stats[ST_F_COMP_OUT] = mkf_u64(FN_COUNTER, px->be_counters.comp_out);
4431 stats[ST_F_COMP_BYP] = mkf_u64(FN_COUNTER, px->be_counters.comp_byp);
4432 stats[ST_F_COMP_RSP] = mkf_u64(FN_COUNTER, px->be_counters.p.http.comp_rsp);
4433 stats[ST_F_LASTSESS] = mkf_s32(FN_AGE, be_lastsession(px));
4434
4435 stats[ST_F_QTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.q_time, TIME_STATS_SAMPLES));
4436 stats[ST_F_CTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.c_time, TIME_STATS_SAMPLES));
4437 stats[ST_F_RTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.d_time, TIME_STATS_SAMPLES));
4438 stats[ST_F_TTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.t_time, TIME_STATS_SAMPLES));
4439
Willy Tarreau501f6022016-01-08 17:37:22 +01004440 return stats_dump_one_line(stats, flags, px, appctx);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004441}
4442
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004443/* Dumps the HTML table header for proxy <px> to the trash for and uses the state from
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004444 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004445 * for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004446 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004447static void stats_dump_html_px_hdr(struct stream_interface *si, struct proxy *px, struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004448{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004449 struct appctx *appctx = __objt_appctx(si->end);
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004450 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
4451
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004452 if (px->cap & PR_CAP_BE && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004453 /* A form to enable/disable this proxy servers */
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004454
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004455 /* 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 +02004456 scope_txt[0] = 0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004457 if (appctx->ctx.stats.scope_len) {
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004458 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau4e4292b2014-11-28 12:18:45 +01004459 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 +01004460 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004461 }
4462
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004463 chunk_appendf(&trash,
Jeff Buchbinder2dbbf4d2014-08-29 15:10:08 -05004464 "<form method=\"post\">");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004465 }
4466
4467 /* print a new table */
4468 chunk_appendf(&trash,
4469 "<table class=\"tbl\" width=\"100%%\">\n"
4470 "<tr class=\"titre\">"
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01004471 "<th class=\"pxname\" width=\"10%%\">");
4472
4473 chunk_appendf(&trash,
4474 "<a name=\"%s\"></a>%s"
4475 "<a class=px href=\"#%s\">%s</a>",
4476 px->id,
4477 (uri->flags & ST_SHLGNDS) ? "<u>":"",
4478 px->id, px->id);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004479
4480 if (uri->flags & ST_SHLGNDS) {
4481 /* cap, mode, id */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02004482 chunk_appendf(&trash, "<div class=tips>cap: %s, mode: %s, id: %d",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004483 proxy_cap_str(px->cap), proxy_mode_str(px->mode),
4484 px->uuid);
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01004485 chunk_appendf(&trash, "</div>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004486 }
4487
4488 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01004489 "%s</th>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004490 "<th class=\"%s\" width=\"90%%\">%s</th>"
4491 "</tr>\n"
4492 "</table>\n"
4493 "<table class=\"tbl\" width=\"100%%\">\n"
4494 "<tr class=\"titre\">",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004495 (uri->flags & ST_SHLGNDS) ? "</u>":"",
4496 px->desc ? "desc" : "empty", px->desc ? px->desc : "");
4497
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004498 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004499 /* Column heading for Enable or Disable server */
4500 chunk_appendf(&trash, "<th rowspan=2 width=1></th>");
Willy Tarreau91861262007-10-17 17:06:05 +02004501 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004502
4503 chunk_appendf(&trash,
4504 "<th rowspan=2></th>"
4505 "<th colspan=3>Queue</th>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05004506 "<th colspan=3>Session rate</th><th colspan=6>Sessions</th>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004507 "<th colspan=2>Bytes</th><th colspan=2>Denied</th>"
4508 "<th colspan=3>Errors</th><th colspan=2>Warnings</th>"
4509 "<th colspan=9>Server</th>"
4510 "</tr>\n"
4511 "<tr class=\"titre\">"
4512 "<th>Cur</th><th>Max</th><th>Limit</th>"
4513 "<th>Cur</th><th>Max</th><th>Limit</th><th>Cur</th><th>Max</th>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05004514 "<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 +01004515 "<th>Req</th><th>Resp</th><th>Req</th><th>Conn</th>"
4516 "<th>Resp</th><th>Retr</th><th>Redis</th>"
4517 "<th>Status</th><th>LastChk</th><th>Wght</th><th>Act</th>"
4518 "<th>Bck</th><th>Chk</th><th>Dwn</th><th>Dwntme</th>"
4519 "<th>Thrtle</th>\n"
4520 "</tr>");
Willy Tarreau91861262007-10-17 17:06:05 +02004521}
4522
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004523/* Dumps the HTML table trailer for proxy <px> to the trash for and uses the state from
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004524 * stream interface <si>. The caller is responsible for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004525 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004526static void stats_dump_html_px_end(struct stream_interface *si, struct proxy *px)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004527{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004528 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004529 chunk_appendf(&trash, "</table>");
4530
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004531 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004532 /* close the form used to enable/disable this proxy servers */
4533 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004534 "Choose the action to perform on the checked servers : "
4535 "<select name=action>"
4536 "<option value=\"\"></option>"
Willy Tarreaued7df902014-05-22 18:04:49 +02004537 "<option value=\"ready\">Set state to READY</option>"
4538 "<option value=\"drain\">Set state to DRAIN</option>"
Marco Corte8c27bca2014-07-02 17:49:34 +02004539 "<option value=\"maint\">Set state to MAINT</option>"
Willy Tarreau248a60e2014-05-23 14:59:48 +02004540 "<option value=\"dhlth\">Health: disable checks</option>"
4541 "<option value=\"ehlth\">Health: enable checks</option>"
4542 "<option value=\"hrunn\">Health: force UP</option>"
4543 "<option value=\"hnolb\">Health: force NOLB</option>"
4544 "<option value=\"hdown\">Health: force DOWN</option>"
4545 "<option value=\"dagent\">Agent: disable checks</option>"
4546 "<option value=\"eagent\">Agent: enable checks</option>"
4547 "<option value=\"arunn\">Agent: force UP</option>"
4548 "<option value=\"adown\">Agent: force DOWN</option>"
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004549 "<option value=\"shutdown\">Kill Sessions</option>"
4550 "</select>"
4551 "<input type=\"hidden\" name=\"b\" value=\"#%d\">"
4552 "&nbsp;<input type=\"submit\" value=\"Apply\">"
4553 "</form>",
4554 px->uuid);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004555 }
4556
4557 chunk_appendf(&trash, "<p>\n");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004558}
Willy Tarreau91861262007-10-17 17:06:05 +02004559
4560/*
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004561 * Dumps statistics for a proxy. The output is sent to the stream interface's
4562 * input buffer. Returns 0 if it had to stop dumping data because of lack of
4563 * buffer space, or non-zero if everything completed. This function is used
4564 * both by the CLI and the HTTP entry points, and is able to dump the output
4565 * in HTML or CSV formats. If the later, <uri> must be NULL.
Willy Tarreau91861262007-10-17 17:06:05 +02004566 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004567static int stats_dump_proxy_to_buffer(struct stream_interface *si, struct proxy *px, struct uri_auth *uri)
Willy Tarreau91861262007-10-17 17:06:05 +02004568{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004569 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau87b09662015-04-03 00:22:06 +02004570 struct stream *s = si_strm(si);
Willy Tarreau2bb4a962014-11-28 11:11:05 +01004571 struct channel *rep = si_ic(si);
Willy Tarreau44267702011-10-28 15:35:33 +02004572 struct server *sv, *svs; /* server and server-state, server-state=server or server->track */
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004573 struct listener *l;
Willy Tarreau0deb85a2016-01-08 15:33:18 +01004574 unsigned int flags;
4575
4576 if (uri)
4577 flags = uri->flags;
4578 else if (strm_li(s)->bind_conf->level >= ACCESS_LVL_OPER)
4579 flags = ST_SHLGNDS | ST_SHNODE | ST_SHDESC;
4580 else
4581 flags = ST_SHNODE | ST_SHDESC;
Willy Tarreau91861262007-10-17 17:06:05 +02004582
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004583 chunk_reset(&trash);
Willy Tarreau91861262007-10-17 17:06:05 +02004584
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004585 switch (appctx->ctx.stats.px_st) {
Willy Tarreau295a8372011-03-10 11:25:07 +01004586 case STAT_PX_ST_INIT:
Willy Tarreau91861262007-10-17 17:06:05 +02004587 /* we are on a new proxy */
Willy Tarreau91861262007-10-17 17:06:05 +02004588 if (uri && uri->scope) {
4589 /* we have a limited scope, we have to check the proxy name */
4590 struct stat_scope *scope;
4591 int len;
4592
4593 len = strlen(px->id);
4594 scope = uri->scope;
4595
4596 while (scope) {
4597 /* match exact proxy name */
4598 if (scope->px_len == len && !memcmp(px->id, scope->px_id, len))
4599 break;
4600
4601 /* match '.' which means 'self' proxy */
Willy Tarreau1388a3a2007-10-18 16:38:37 +02004602 if (!strcmp(scope->px_id, ".") && px == s->be)
Willy Tarreau91861262007-10-17 17:06:05 +02004603 break;
4604 scope = scope->next;
4605 }
4606
4607 /* proxy name not found : don't dump anything */
4608 if (scope == NULL)
4609 return 1;
4610 }
4611
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004612 /* if the user has requested a limited output and the proxy
4613 * name does not match, skip it.
4614 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004615 if (appctx->ctx.stats.scope_len &&
Willy Tarreau4e4292b2014-11-28 12:18:45 +01004616 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 +02004617 return 1;
4618
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004619 if ((appctx->ctx.stats.flags & STAT_BOUND) &&
4620 (appctx->ctx.stats.iid != -1) &&
4621 (px->uuid != appctx->ctx.stats.iid))
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01004622 return 1;
4623
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004624 appctx->ctx.stats.px_st = STAT_PX_ST_TH;
Willy Tarreau91861262007-10-17 17:06:05 +02004625 /* fall through */
4626
Willy Tarreau295a8372011-03-10 11:25:07 +01004627 case STAT_PX_ST_TH:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004628 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004629 stats_dump_html_px_hdr(si, px, uri);
Willy Tarreaubc18da12015-03-13 14:00:47 +01004630 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004631 si_applet_cant_put(si);
Willy Tarreau55bb8452007-10-17 18:44:57 +02004632 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01004633 }
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004634 }
Willy Tarreau91861262007-10-17 17:06:05 +02004635
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004636 appctx->ctx.stats.px_st = STAT_PX_ST_FE;
Willy Tarreau91861262007-10-17 17:06:05 +02004637 /* fall through */
4638
Willy Tarreau295a8372011-03-10 11:25:07 +01004639 case STAT_PX_ST_FE:
Willy Tarreau91861262007-10-17 17:06:05 +02004640 /* print the frontend */
Willy Tarreaubc18da12015-03-13 14:00:47 +01004641 if (stats_dump_fe_stats(si, px)) {
4642 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004643 si_applet_cant_put(si);
Willy Tarreau91861262007-10-17 17:06:05 +02004644 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01004645 }
4646 }
Willy Tarreau91861262007-10-17 17:06:05 +02004647
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004648 appctx->ctx.stats.l = px->conf.listeners.n;
4649 appctx->ctx.stats.px_st = STAT_PX_ST_LI;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004650 /* fall through */
4651
Willy Tarreau295a8372011-03-10 11:25:07 +01004652 case STAT_PX_ST_LI:
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004653 /* stats.l has been initialized above */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004654 for (; appctx->ctx.stats.l != &px->conf.listeners; appctx->ctx.stats.l = l->by_fe.n) {
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01004655 if (buffer_almost_full(rep->buf)) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004656 si_applet_cant_put(si);
Willy Tarreau4e33d862009-10-11 23:35:10 +02004657 return 0;
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01004658 }
Willy Tarreau4e33d862009-10-11 23:35:10 +02004659
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004660 l = LIST_ELEM(appctx->ctx.stats.l, struct listener *, by_fe);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004661 if (!l->counters)
4662 continue;
4663
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004664 if (appctx->ctx.stats.flags & STAT_BOUND) {
4665 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SO)))
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004666 break;
4667
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004668 if (appctx->ctx.stats.sid != -1 && l->luid != appctx->ctx.stats.sid)
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004669 continue;
4670 }
4671
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004672 /* print the frontend */
Willy Tarreau0deb85a2016-01-08 15:33:18 +01004673 if (stats_dump_li_stats(si, px, l, flags)) {
Willy Tarreaubc18da12015-03-13 14:00:47 +01004674 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004675 si_applet_cant_put(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004676 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01004677 }
4678 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004679 }
4680
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004681 appctx->ctx.stats.sv = px->srv; /* may be NULL */
4682 appctx->ctx.stats.px_st = STAT_PX_ST_SV;
Willy Tarreau91861262007-10-17 17:06:05 +02004683 /* fall through */
4684
Willy Tarreau295a8372011-03-10 11:25:07 +01004685 case STAT_PX_ST_SV:
Willy Tarreau91861262007-10-17 17:06:05 +02004686 /* stats.sv has been initialized above */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004687 for (; appctx->ctx.stats.sv != NULL; appctx->ctx.stats.sv = sv->next) {
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01004688 if (buffer_almost_full(rep->buf)) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004689 si_applet_cant_put(si);
Willy Tarreau4e33d862009-10-11 23:35:10 +02004690 return 0;
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01004691 }
Willy Tarreau4e33d862009-10-11 23:35:10 +02004692
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004693 sv = appctx->ctx.stats.sv;
Willy Tarreau91861262007-10-17 17:06:05 +02004694
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004695 if (appctx->ctx.stats.flags & STAT_BOUND) {
4696 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SV)))
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01004697 break;
4698
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004699 if (appctx->ctx.stats.sid != -1 && sv->puid != appctx->ctx.stats.sid)
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01004700 continue;
4701 }
4702
Willy Tarreau32091232014-05-16 13:52:00 +02004703 svs = sv;
4704 while (svs->track)
4705 svs = svs->track;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01004706
Willy Tarreauba2f2642016-01-07 09:54:40 +01004707 /* do not report servers which are DOWN and not changing state */
4708 if ((appctx->ctx.stats.flags & STAT_HIDE_DOWN) &&
4709 ((sv->admin & SRV_ADMF_MAINT) || /* server is in maintenance */
4710 (sv->state == SRV_ST_STOPPED && /* server is down */
4711 (!((svs->agent.state | svs->check.state) & CHK_ST_ENABLED) ||
4712 ((svs->agent.state & CHK_ST_ENABLED) && !svs->agent.health) ||
4713 ((svs->check.state & CHK_ST_ENABLED) && !svs->check.health))))) {
Willy Tarreau91861262007-10-17 17:06:05 +02004714 continue;
4715 }
4716
Willy Tarreau0deb85a2016-01-08 15:33:18 +01004717 if (stats_dump_sv_stats(si, px, flags, sv)) {
Willy Tarreaubc18da12015-03-13 14:00:47 +01004718 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004719 si_applet_cant_put(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004720 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01004721 }
4722 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004723 } /* for sv */
Cyril Bonté474be412010-10-12 00:14:36 +02004724
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004725 appctx->ctx.stats.px_st = STAT_PX_ST_BE;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004726 /* fall through */
Cyril Bonté70be45d2010-10-12 00:14:35 +02004727
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004728 case STAT_PX_ST_BE:
4729 /* print the backend */
Willy Tarreau0deb85a2016-01-08 15:33:18 +01004730 if (stats_dump_be_stats(si, px, flags)) {
Willy Tarreaubc18da12015-03-13 14:00:47 +01004731 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004732 si_applet_cant_put(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004733 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01004734 }
4735 }
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01004736
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004737 appctx->ctx.stats.px_st = STAT_PX_ST_END;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004738 /* fall through */
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01004739
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004740 case STAT_PX_ST_END:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004741 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004742 stats_dump_html_px_end(si, px);
Willy Tarreaubc18da12015-03-13 14:00:47 +01004743 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004744 si_applet_cant_put(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004745 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01004746 }
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004747 }
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01004748
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004749 appctx->ctx.stats.px_st = STAT_PX_ST_FIN;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004750 /* fall through */
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01004751
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004752 case STAT_PX_ST_FIN:
4753 return 1;
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01004754
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004755 default:
4756 /* unknown state, we should put an abort() here ! */
4757 return 1;
4758 }
4759}
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01004760
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004761/* Dumps the HTTP stats head block to the trash for and uses the per-uri
4762 * parameters <uri>. The caller is responsible for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004763 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004764static void stats_dump_html_head(struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004765{
4766 /* WARNING! This must fit in the first buffer !!! */
4767 chunk_appendf(&trash,
4768 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n"
4769 "\"http://www.w3.org/TR/html4/loose.dtd\">\n"
4770 "<html><head><title>Statistics Report for " PRODUCT_NAME "%s%s</title>\n"
4771 "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n"
4772 "<style type=\"text/css\"><!--\n"
4773 "body {"
4774 " font-family: arial, helvetica, sans-serif;"
4775 " font-size: 12px;"
4776 " font-weight: normal;"
4777 " color: black;"
4778 " background: white;"
4779 "}\n"
4780 "th,td {"
4781 " font-size: 10px;"
4782 "}\n"
4783 "h1 {"
4784 " font-size: x-large;"
4785 " margin-bottom: 0.5em;"
4786 "}\n"
4787 "h2 {"
4788 " font-family: helvetica, arial;"
4789 " font-size: x-large;"
4790 " font-weight: bold;"
4791 " font-style: italic;"
4792 " color: #6020a0;"
4793 " margin-top: 0em;"
4794 " margin-bottom: 0em;"
4795 "}\n"
4796 "h3 {"
4797 " font-family: helvetica, arial;"
4798 " font-size: 16px;"
4799 " font-weight: bold;"
4800 " color: #b00040;"
4801 " background: #e8e8d0;"
4802 " margin-top: 0em;"
4803 " margin-bottom: 0em;"
4804 "}\n"
4805 "li {"
4806 " margin-top: 0.25em;"
4807 " margin-right: 2em;"
4808 "}\n"
4809 ".hr {margin-top: 0.25em;"
4810 " border-color: black;"
4811 " border-bottom-style: solid;"
4812 "}\n"
4813 ".titre {background: #20D0D0;color: #000000; font-weight: bold; text-align: center;}\n"
4814 ".total {background: #20D0D0;color: #ffff80;}\n"
4815 ".frontend {background: #e8e8d0;}\n"
4816 ".socket {background: #d0d0d0;}\n"
4817 ".backend {background: #e8e8d0;}\n"
Simon Horman837bfa72015-04-23 14:51:27 +09004818 ".active_down {background: #ff9090;}\n"
4819 ".active_going_up {background: #ffd020;}\n"
4820 ".active_going_down {background: #ffffa0;}\n"
4821 ".active_up {background: #c0ffc0;}\n"
4822 ".active_nolb {background: #20a0ff;}\n"
4823 ".active_draining {background: #20a0FF;}\n"
4824 ".active_no_check {background: #e0e0e0;}\n"
4825 ".backup_down {background: #ff9090;}\n"
4826 ".backup_going_up {background: #ff80ff;}\n"
4827 ".backup_going_down {background: #c060ff;}\n"
4828 ".backup_up {background: #b0d0ff;}\n"
4829 ".backup_nolb {background: #90b0e0;}\n"
4830 ".backup_draining {background: #cc9900;}\n"
4831 ".backup_no_check {background: #e0e0e0;}\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004832 ".maintain {background: #c07820;}\n"
4833 ".rls {letter-spacing: 0.2em; margin-right: 1px;}\n" /* right letter spacing (used for grouping digits) */
4834 "\n"
4835 "a.px:link {color: #ffff40; text-decoration: none;}"
4836 "a.px:visited {color: #ffff40; text-decoration: none;}"
4837 "a.px:hover {color: #ffffff; text-decoration: none;}"
4838 "a.lfsb:link {color: #000000; text-decoration: none;}"
4839 "a.lfsb:visited {color: #000000; text-decoration: none;}"
4840 "a.lfsb:hover {color: #505050; text-decoration: none;}"
4841 "\n"
4842 "table.tbl { border-collapse: collapse; border-style: none;}\n"
4843 "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"
4844 "table.tbl td.ac { text-align: center;}\n"
4845 "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}\n"
4846 "table.tbl th.pxname { background: #b00040; color: #ffff40; font-weight: bold; border-style: solid solid none solid; padding: 2px 3px; white-space: nowrap;}\n"
4847 "table.tbl th.empty { border-style: none; empty-cells: hide; background: white;}\n"
4848 "table.tbl th.desc { background: white; border-style: solid solid none solid; text-align: left; padding: 2px 3px;}\n"
4849 "\n"
4850 "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n"
4851 "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n"
4852 "table.lgd td.noborder { border-style: none; padding: 2px; white-space: nowrap;}\n"
Willy Tarreau466c9b52012-12-23 02:25:03 +01004853 "table.det { border-collapse: collapse; border-style: none; }\n"
4854 "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 +01004855 "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 +01004856 "u {text-decoration:none; border-bottom: 1px dotted black;}\n"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02004857 "div.tips {\n"
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01004858 " display:block;\n"
4859 " visibility:hidden;\n"
4860 " z-index:2147483647;\n"
4861 " position:absolute;\n"
4862 " padding:2px 4px 3px;\n"
4863 " background:#f0f060; color:#000000;\n"
4864 " border:1px solid #7040c0;\n"
4865 " white-space:nowrap;\n"
4866 " font-style:normal;font-size:11px;font-weight:normal;\n"
4867 " -moz-border-radius:3px;-webkit-border-radius:3px;border-radius:3px;\n"
4868 " -moz-box-shadow:gray 2px 2px 3px;-webkit-box-shadow:gray 2px 2px 3px;box-shadow:gray 2px 2px 3px;\n"
4869 "}\n"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02004870 "u:hover div.tips {visibility:visible;}\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004871 "-->\n"
4872 "</style></head>\n",
4873 (uri->flags & ST_SHNODE) ? " on " : "",
4874 (uri->flags & ST_SHNODE) ? (uri->node ? uri->node : global.node) : ""
4875 );
4876}
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01004877
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004878/* Dumps the HTML stats information block to the trash for and uses the state from
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004879 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004880 * for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004881 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004882static void stats_dump_html_info(struct stream_interface *si, struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004883{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004884 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004885 unsigned int up = (now.tv_sec - start_date.tv_sec);
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004886 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01004887
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004888 /* WARNING! this has to fit the first packet too.
4889 * We are around 3.5 kB, add adding entries will
4890 * become tricky if we want to support 4kB buffers !
4891 */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004892 chunk_appendf(&trash,
4893 "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">"
4894 PRODUCT_NAME "%s</a></h1>\n"
4895 "<h2>Statistics Report for pid %d%s%s%s%s</h2>\n"
4896 "<hr width=\"100%%\" class=\"hr\">\n"
4897 "<h3>&gt; General process information</h3>\n"
4898 "<table border=0><tr><td align=\"left\" nowrap width=\"1%%\">\n"
4899 "<p><b>pid = </b> %d (process #%d, nbproc = %d)<br>\n"
4900 "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n"
4901 "<b>system limits:</b> memmax = %s%s; ulimit-n = %d<br>\n"
4902 "<b>maxsock = </b> %d; <b>maxconn = </b> %d; <b>maxpipes = </b> %d<br>\n"
4903 "current conns = %d; current pipes = %d/%d; conn rate = %d/sec<br>\n"
4904 "Running tasks: %d/%d; idle = %d %%<br>\n"
4905 "</td><td align=\"center\" nowrap>\n"
4906 "<table class=\"lgd\"><tr>\n"
Simon Horman837bfa72015-04-23 14:51:27 +09004907 "<td class=\"active_up\">&nbsp;</td><td class=\"noborder\">active UP </td>"
4908 "<td class=\"backup_up\">&nbsp;</td><td class=\"noborder\">backup UP </td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004909 "</tr><tr>\n"
Simon Horman837bfa72015-04-23 14:51:27 +09004910 "<td class=\"active_going_down\"></td><td class=\"noborder\">active UP, going down </td>"
4911 "<td class=\"backup_going_down\"></td><td class=\"noborder\">backup UP, going down </td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004912 "</tr><tr>\n"
Simon Horman837bfa72015-04-23 14:51:27 +09004913 "<td class=\"active_going_up\"></td><td class=\"noborder\">active DOWN, going up </td>"
4914 "<td class=\"backup_going_up\"></td><td class=\"noborder\">backup DOWN, going up </td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004915 "</tr><tr>\n"
Simon Horman837bfa72015-04-23 14:51:27 +09004916 "<td class=\"active_down\"></td><td class=\"noborder\">active or backup DOWN &nbsp;</td>"
4917 "<td class=\"active_no_check\"></td><td class=\"noborder\">not checked </td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004918 "</tr><tr>\n"
4919 "<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 -07004920 "</tr><tr>\n"
Simon Horman837bfa72015-04-23 14:51:27 +09004921 "<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 +01004922 "</tr></table>\n"
Willy Tarreau6b7764a2013-12-04 00:43:21 +01004923 "Note: \"NOLB\"/\"DRAIN\" = UP with load-balancing disabled."
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004924 "</td>"
4925 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
4926 "<b>Display option:</b><ul style=\"margin-top: 0.25em;\">"
4927 "",
4928 (uri->flags & ST_HIDEVER) ? "" : (STATS_VERSION_STRING),
4929 pid, (uri->flags & ST_SHNODE) ? " on " : "",
4930 (uri->flags & ST_SHNODE) ? (uri->node ? uri->node : global.node) : "",
4931 (uri->flags & ST_SHDESC) ? ": " : "",
4932 (uri->flags & ST_SHDESC) ? (uri->desc ? uri->desc : global.desc) : "",
4933 pid, relative_pid, global.nbproc,
4934 up / 86400, (up % 86400) / 3600,
4935 (up % 3600) / 60, (up % 60),
4936 global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited",
4937 global.rlimit_memmax ? " MB" : "",
4938 global.rlimit_nofile,
4939 global.maxsock, global.maxconn, global.maxpipes,
4940 actconn, pipes_used, pipes_used+pipes_free, read_freq_ctr(&global.conn_per_sec),
4941 run_queue_cur, nb_tasks_cur, idle_pct
4942 );
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004943
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004944 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Willy Tarreau4e4292b2014-11-28 12:18:45 +01004945 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 +01004946 scope_txt[appctx->ctx.stats.scope_len] = '\0';
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004947
4948 chunk_appendf(&trash,
Jeff Buchbinder2dbbf4d2014-08-29 15:10:08 -05004949 "<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 +01004950 (appctx->ctx.stats.scope_len > 0) ? scope_txt : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004951 STAT_SCOPE_TXT_MAXLEN);
4952
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004953 /* 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 +02004954 scope_txt[0] = 0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004955 if (appctx->ctx.stats.scope_len) {
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004956 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau4e4292b2014-11-28 12:18:45 +01004957 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 +01004958 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004959 }
4960
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004961 if (appctx->ctx.stats.flags & STAT_HIDE_DOWN)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004962 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004963 "<li><a href=\"%s%s%s%s\">Show all servers</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004964 uri->uri_prefix,
4965 "",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004966 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004967 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004968 else
4969 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004970 "<li><a href=\"%s%s%s%s\">Hide 'DOWN' servers</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004971 uri->uri_prefix,
4972 ";up",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004973 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004974 scope_txt);
Willy Tarreau91861262007-10-17 17:06:05 +02004975
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004976 if (uri->refresh > 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004977 if (appctx->ctx.stats.flags & STAT_NO_REFRESH)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004978 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004979 "<li><a href=\"%s%s%s%s\">Enable refresh</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004980 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004981 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004982 "",
4983 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004984 else
4985 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004986 "<li><a href=\"%s%s%s%s\">Disable refresh</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004987 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004988 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004989 ";norefresh",
4990 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004991 }
Willy Tarreau55bb8452007-10-17 18:44:57 +02004992
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004993 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004994 "<li><a href=\"%s%s%s%s\">Refresh now</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004995 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004996 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4997 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004998 scope_txt);
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02004999
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005000 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02005001 "<li><a href=\"%s;csv%s%s\">CSV export</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005002 uri->uri_prefix,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02005003 (uri->refresh > 0) ? ";norefresh" : "",
5004 scope_txt);
Willy Tarreau4bab24d2007-11-30 18:16:29 +01005005
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005006 chunk_appendf(&trash,
5007 "</ul></td>"
5008 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
5009 "<b>External resources:</b><ul style=\"margin-top: 0.25em;\">\n"
5010 "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>\n"
5011 "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>\n"
5012 "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>\n"
5013 "</ul>"
5014 "</td>"
5015 "</tr></table>\n"
5016 ""
5017 );
Willy Tarreau4bab24d2007-11-30 18:16:29 +01005018
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005019 if (appctx->ctx.stats.st_code) {
5020 switch (appctx->ctx.stats.st_code) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005021 case STAT_STATUS_DONE:
5022 chunk_appendf(&trash,
Simon Horman837bfa72015-04-23 14:51:27 +09005023 "<p><div class=active_up>"
Willy Tarreauba6be982013-04-19 12:16:55 +02005024 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005025 "Action processed successfully."
Willy Tarreauba6be982013-04-19 12:16:55 +02005026 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005027 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
5028 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02005029 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005030 break;
5031 case STAT_STATUS_NONE:
5032 chunk_appendf(&trash,
Simon Horman837bfa72015-04-23 14:51:27 +09005033 "<p><div class=active_going_down>"
Willy Tarreauba6be982013-04-19 12:16:55 +02005034 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005035 "Nothing has changed."
Willy Tarreauba6be982013-04-19 12:16:55 +02005036 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005037 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
5038 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02005039 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005040 break;
5041 case STAT_STATUS_PART:
5042 chunk_appendf(&trash,
Simon Horman837bfa72015-04-23 14:51:27 +09005043 "<p><div class=active_going_down>"
Willy Tarreauba6be982013-04-19 12:16:55 +02005044 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005045 "Action partially processed.<br>"
5046 "Some server names are probably unknown or ambiguous (duplicated names in the backend)."
Willy Tarreauba6be982013-04-19 12:16:55 +02005047 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005048 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
5049 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02005050 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005051 break;
5052 case STAT_STATUS_ERRP:
5053 chunk_appendf(&trash,
Simon Horman837bfa72015-04-23 14:51:27 +09005054 "<p><div class=active_down>"
Willy Tarreauba6be982013-04-19 12:16:55 +02005055 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005056 "Action not processed because of invalid parameters."
5057 "<ul>"
5058 "<li>The action is maybe unknown.</li>"
5059 "<li>The backend name is probably unknown or ambiguous (duplicated names).</li>"
5060 "<li>Some server names are probably unknown or ambiguous (duplicated names in the backend).</li>"
5061 "</ul>"
Willy Tarreauba6be982013-04-19 12:16:55 +02005062 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005063 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
5064 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02005065 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005066 break;
5067 case STAT_STATUS_EXCD:
5068 chunk_appendf(&trash,
Simon Horman837bfa72015-04-23 14:51:27 +09005069 "<p><div class=active_down>"
Willy Tarreauba6be982013-04-19 12:16:55 +02005070 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005071 "<b>Action not processed : the buffer couldn't store all the data.<br>"
5072 "You should retry with less servers at a time.</b>"
Willy Tarreauba6be982013-04-19 12:16:55 +02005073 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005074 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
5075 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02005076 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005077 break;
5078 case STAT_STATUS_DENY:
5079 chunk_appendf(&trash,
Simon Horman837bfa72015-04-23 14:51:27 +09005080 "<p><div class=active_down>"
Willy Tarreauba6be982013-04-19 12:16:55 +02005081 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005082 "<b>Action denied.</b>"
Willy Tarreauba6be982013-04-19 12:16:55 +02005083 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005084 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
5085 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02005086 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005087 break;
5088 default:
5089 chunk_appendf(&trash,
Simon Horman837bfa72015-04-23 14:51:27 +09005090 "<p><div class=active_no_check>"
Willy Tarreauba6be982013-04-19 12:16:55 +02005091 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005092 "Unexpected result."
Willy Tarreauba6be982013-04-19 12:16:55 +02005093 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005094 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
5095 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02005096 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005097 }
5098 chunk_appendf(&trash, "<p>\n");
5099 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005100}
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01005101
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01005102/* Dumps the HTML stats trailer block to the trash. The caller is responsible
5103 * for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005104 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01005105static void stats_dump_html_end()
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005106{
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005107 chunk_appendf(&trash, "</body></html>\n");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005108}
Willy Tarreau7f062c42009-03-05 18:43:00 +01005109
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01005110/* This function dumps statistics onto the stream interface's read buffer in
5111 * either CSV or HTML format. <uri> contains some HTML-specific parameters that
Willy Tarreau306f8302013-07-08 15:53:06 +02005112 * are ignored for CSV format (hence <uri> may be NULL there). It returns 0 if
5113 * 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 +02005114 * and the stream must be closed, or -1 in case of any error. This function is
Willy Tarreau306f8302013-07-08 15:53:06 +02005115 * used by both the CLI and the HTTP handlers.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005116 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01005117static int stats_dump_stat_to_buffer(struct stream_interface *si, struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005118{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005119 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005120 struct channel *rep = si_ic(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005121 struct proxy *px;
Willy Tarreau7f062c42009-03-05 18:43:00 +01005122
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005123 chunk_reset(&trash);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02005124
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005125 switch (appctx->st2) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005126 case STAT_ST_INIT:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005127 appctx->st2 = STAT_ST_HEAD; /* let's start producing data */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005128 /* fall through */
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005129
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005130 case STAT_ST_HEAD:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005131 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01005132 stats_dump_html_head(uri);
Willy Tarreau354898b2012-12-23 18:15:23 +01005133 else
5134 stats_dump_csv_header();
Willy Tarreaud9b587f2010-02-26 10:05:55 +01005135
Willy Tarreaubc18da12015-03-13 14:00:47 +01005136 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02005137 si_applet_cant_put(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005138 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01005139 }
Willy Tarreauae526782010-03-04 20:34:23 +01005140
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005141 appctx->st2 = STAT_ST_INFO;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005142 /* fall through */
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01005143
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005144 case STAT_ST_INFO:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005145 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01005146 stats_dump_html_info(si, uri);
Willy Tarreaubc18da12015-03-13 14:00:47 +01005147 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02005148 si_applet_cant_put(si);
Willy Tarreau91861262007-10-17 17:06:05 +02005149 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01005150 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005151 }
Willy Tarreau91861262007-10-17 17:06:05 +02005152
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005153 appctx->ctx.stats.px = proxy;
5154 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
5155 appctx->st2 = STAT_ST_LIST;
Willy Tarreau91861262007-10-17 17:06:05 +02005156 /* fall through */
5157
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005158 case STAT_ST_LIST:
5159 /* dump proxies */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005160 while (appctx->ctx.stats.px) {
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01005161 if (buffer_almost_full(rep->buf)) {
Willy Tarreaufe127932015-04-21 19:23:39 +02005162 si_applet_cant_put(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005163 return 0;
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01005164 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005165
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005166 px = appctx->ctx.stats.px;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005167 /* skip the disabled proxies, global frontend and non-networked ones */
5168 if (px->state != PR_STSTOPPED && px->uuid > 0 && (px->cap & (PR_CAP_FE | PR_CAP_BE)))
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01005169 if (stats_dump_proxy_to_buffer(si, px, uri) == 0)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005170 return 0;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005171
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005172 appctx->ctx.stats.px = px->next;
5173 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005174 }
5175 /* here, we just have reached the last proxy */
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005176
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005177 appctx->st2 = STAT_ST_END;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005178 /* fall through */
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005179
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005180 case STAT_ST_END:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005181 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01005182 stats_dump_html_end();
Willy Tarreaubc18da12015-03-13 14:00:47 +01005183 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02005184 si_applet_cant_put(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005185 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01005186 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005187 }
Willy Tarreau55058a72012-11-21 08:27:21 +01005188
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005189 appctx->st2 = STAT_ST_FIN;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005190 /* fall through */
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02005191
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005192 case STAT_ST_FIN:
5193 return 1;
Willy Tarreau55058a72012-11-21 08:27:21 +01005194
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005195 default:
5196 /* unknown state ! */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005197 appctx->st2 = STAT_ST_FIN;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005198 return -1;
5199 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005200}
Willy Tarreauae526782010-03-04 20:34:23 +01005201
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005202/* We reached the stats page through a POST request. The appctx is
5203 * expected to have already been allocated by the caller.
Willy Tarreau347a35d2013-11-22 17:51:09 +01005204 * Parse the posted data and enable/disable servers if necessary.
5205 * Returns 1 if request was parsed or zero if it needs more data.
5206 */
5207static int stats_process_http_post(struct stream_interface *si)
5208{
Willy Tarreau87b09662015-04-03 00:22:06 +02005209 struct stream *s = si_strm(si);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005210 struct appctx *appctx = objt_appctx(si->end);
Willy Tarreau347a35d2013-11-22 17:51:09 +01005211
5212 struct proxy *px = NULL;
5213 struct server *sv = NULL;
5214
5215 char key[LINESIZE];
5216 int action = ST_ADM_ACTION_NONE;
5217 int reprocess = 0;
5218
5219 int total_servers = 0;
5220 int altered_servers = 0;
5221
5222 char *first_param, *cur_param, *next_param, *end_params;
5223 char *st_cur_param = NULL;
5224 char *st_next_param = NULL;
5225
5226 struct chunk *temp;
5227 int reql;
5228
5229 temp = get_trash_chunk();
Willy Tarreaueee5b512015-04-03 23:46:31 +02005230 if (temp->size < s->txn->req.body_len) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01005231 /* too large request */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005232 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005233 goto out;
5234 }
5235
Willy Tarreaueee5b512015-04-03 23:46:31 +02005236 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 +01005237 if (reql <= 0) {
5238 /* we need more data */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005239 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005240 return 0;
5241 }
5242
5243 first_param = temp->str;
5244 end_params = temp->str + reql;
5245 cur_param = next_param = end_params;
5246 *end_params = '\0';
5247
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005248 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005249
5250 /*
5251 * Parse the parameters in reverse order to only store the last value.
5252 * From the html form, the backend and the action are at the end.
5253 */
5254 while (cur_param > first_param) {
5255 char *value;
5256 int poffset, plen;
5257
5258 cur_param--;
5259
5260 if ((*cur_param == '&') || (cur_param == first_param)) {
5261 reprocess_servers:
5262 /* Parse the key */
5263 poffset = (cur_param != first_param ? 1 : 0);
5264 plen = next_param - cur_param + (cur_param == first_param ? 1 : 0);
5265 if ((plen > 0) && (plen <= sizeof(key))) {
5266 strncpy(key, cur_param + poffset, plen);
5267 key[plen - 1] = '\0';
5268 } else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005269 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005270 goto out;
5271 }
5272
5273 /* Parse the value */
5274 value = key;
5275 while (*value != '\0' && *value != '=') {
5276 value++;
5277 }
5278 if (*value == '=') {
5279 /* Ok, a value is found, we can mark the end of the key */
5280 *value++ = '\0';
5281 }
5282 if (url_decode(key) < 0 || url_decode(value) < 0)
5283 break;
5284
5285 /* Now we can check the key to see what to do */
5286 if (!px && (strcmp(key, "b") == 0)) {
Willy Tarreau9e0bb102015-05-26 11:24:42 +02005287 if ((px = proxy_be_by_name(value)) == NULL) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01005288 /* the backend name is unknown or ambiguous (duplicate names) */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005289 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005290 goto out;
5291 }
5292 }
5293 else if (!action && (strcmp(key, "action") == 0)) {
Willy Tarreaued7df902014-05-22 18:04:49 +02005294 if (strcmp(value, "ready") == 0) {
5295 action = ST_ADM_ACTION_READY;
5296 }
5297 else if (strcmp(value, "drain") == 0) {
5298 action = ST_ADM_ACTION_DRAIN;
5299 }
5300 else if (strcmp(value, "maint") == 0) {
5301 action = ST_ADM_ACTION_MAINT;
5302 }
5303 else if (strcmp(value, "shutdown") == 0) {
5304 action = ST_ADM_ACTION_SHUTDOWN;
5305 }
Willy Tarreau248a60e2014-05-23 14:59:48 +02005306 else if (strcmp(value, "dhlth") == 0) {
5307 action = ST_ADM_ACTION_DHLTH;
5308 }
5309 else if (strcmp(value, "ehlth") == 0) {
5310 action = ST_ADM_ACTION_EHLTH;
5311 }
5312 else if (strcmp(value, "hrunn") == 0) {
5313 action = ST_ADM_ACTION_HRUNN;
5314 }
5315 else if (strcmp(value, "hnolb") == 0) {
5316 action = ST_ADM_ACTION_HNOLB;
5317 }
5318 else if (strcmp(value, "hdown") == 0) {
5319 action = ST_ADM_ACTION_HDOWN;
5320 }
5321 else if (strcmp(value, "dagent") == 0) {
5322 action = ST_ADM_ACTION_DAGENT;
5323 }
5324 else if (strcmp(value, "eagent") == 0) {
5325 action = ST_ADM_ACTION_EAGENT;
5326 }
5327 else if (strcmp(value, "arunn") == 0) {
5328 action = ST_ADM_ACTION_ARUNN;
5329 }
5330 else if (strcmp(value, "adown") == 0) {
5331 action = ST_ADM_ACTION_ADOWN;
5332 }
Willy Tarreaued7df902014-05-22 18:04:49 +02005333 /* else these are the old supported methods */
5334 else if (strcmp(value, "disable") == 0) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01005335 action = ST_ADM_ACTION_DISABLE;
5336 }
5337 else if (strcmp(value, "enable") == 0) {
5338 action = ST_ADM_ACTION_ENABLE;
5339 }
5340 else if (strcmp(value, "stop") == 0) {
5341 action = ST_ADM_ACTION_STOP;
5342 }
5343 else if (strcmp(value, "start") == 0) {
5344 action = ST_ADM_ACTION_START;
5345 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01005346 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005347 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005348 goto out;
5349 }
5350 }
5351 else if (strcmp(key, "s") == 0) {
5352 if (!(px && action)) {
5353 /*
5354 * Indicates that we'll need to reprocess the parameters
5355 * as soon as backend and action are known
5356 */
5357 if (!reprocess) {
5358 st_cur_param = cur_param;
5359 st_next_param = next_param;
5360 }
5361 reprocess = 1;
5362 }
5363 else if ((sv = findserver(px, value)) != NULL) {
5364 switch (action) {
5365 case ST_ADM_ACTION_DISABLE:
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02005366 if (!(sv->admin & SRV_ADMF_FMAINT)) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01005367 altered_servers++;
5368 total_servers++;
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02005369 srv_set_admin_flag(sv, SRV_ADMF_FMAINT);
Willy Tarreau347a35d2013-11-22 17:51:09 +01005370 }
5371 break;
5372 case ST_ADM_ACTION_ENABLE:
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02005373 if (sv->admin & SRV_ADMF_FMAINT) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01005374 altered_servers++;
5375 total_servers++;
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02005376 srv_clr_admin_flag(sv, SRV_ADMF_FMAINT);
Willy Tarreau347a35d2013-11-22 17:51:09 +01005377 }
5378 break;
5379 case ST_ADM_ACTION_STOP:
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02005380 if (!(sv->admin & SRV_ADMF_FDRAIN)) {
5381 srv_set_admin_flag(sv, SRV_ADMF_FDRAIN);
5382 altered_servers++;
5383 total_servers++;
5384 }
5385 break;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005386 case ST_ADM_ACTION_START:
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02005387 if (sv->admin & SRV_ADMF_FDRAIN) {
5388 srv_clr_admin_flag(sv, SRV_ADMF_FDRAIN);
5389 altered_servers++;
5390 total_servers++;
5391 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01005392 break;
Willy Tarreau248a60e2014-05-23 14:59:48 +02005393 case ST_ADM_ACTION_DHLTH:
5394 if (sv->check.state & CHK_ST_CONFIGURED) {
5395 sv->check.state &= ~CHK_ST_ENABLED;
5396 altered_servers++;
5397 total_servers++;
5398 }
5399 break;
5400 case ST_ADM_ACTION_EHLTH:
5401 if (sv->check.state & CHK_ST_CONFIGURED) {
5402 sv->check.state |= CHK_ST_ENABLED;
5403 altered_servers++;
5404 total_servers++;
5405 }
5406 break;
5407 case ST_ADM_ACTION_HRUNN:
5408 if (!(sv->track)) {
5409 sv->check.health = sv->check.rise + sv->check.fall - 1;
5410 srv_set_running(sv, "changed from Web interface");
5411 altered_servers++;
5412 total_servers++;
5413 }
5414 break;
5415 case ST_ADM_ACTION_HNOLB:
5416 if (!(sv->track)) {
5417 sv->check.health = sv->check.rise + sv->check.fall - 1;
5418 srv_set_stopping(sv, "changed from Web interface");
5419 altered_servers++;
5420 total_servers++;
5421 }
5422 break;
5423 case ST_ADM_ACTION_HDOWN:
5424 if (!(sv->track)) {
5425 sv->check.health = 0;
5426 srv_set_stopped(sv, "changed from Web interface");
5427 altered_servers++;
5428 total_servers++;
5429 }
5430 break;
5431 case ST_ADM_ACTION_DAGENT:
5432 if (sv->agent.state & CHK_ST_CONFIGURED) {
5433 sv->agent.state &= ~CHK_ST_ENABLED;
5434 altered_servers++;
5435 total_servers++;
5436 }
5437 break;
5438 case ST_ADM_ACTION_EAGENT:
5439 if (sv->agent.state & CHK_ST_CONFIGURED) {
5440 sv->agent.state |= CHK_ST_ENABLED;
5441 altered_servers++;
5442 total_servers++;
5443 }
5444 break;
5445 case ST_ADM_ACTION_ARUNN:
5446 if (sv->agent.state & CHK_ST_ENABLED) {
5447 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
5448 srv_set_running(sv, "changed from Web interface");
5449 altered_servers++;
5450 total_servers++;
5451 }
5452 break;
5453 case ST_ADM_ACTION_ADOWN:
5454 if (sv->agent.state & CHK_ST_ENABLED) {
5455 sv->agent.health = 0;
5456 srv_set_stopped(sv, "changed from Web interface");
5457 altered_servers++;
5458 total_servers++;
5459 }
5460 break;
Willy Tarreaued7df902014-05-22 18:04:49 +02005461 case ST_ADM_ACTION_READY:
5462 srv_adm_set_ready(sv);
5463 altered_servers++;
5464 total_servers++;
5465 break;
5466 case ST_ADM_ACTION_DRAIN:
5467 srv_adm_set_drain(sv);
5468 altered_servers++;
5469 total_servers++;
5470 break;
5471 case ST_ADM_ACTION_MAINT:
5472 srv_adm_set_maint(sv);
5473 altered_servers++;
5474 total_servers++;
5475 break;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005476 case ST_ADM_ACTION_SHUTDOWN:
5477 if (px->state != PR_STSTOPPED) {
Willy Tarreau87b09662015-04-03 00:22:06 +02005478 struct stream *sess, *sess_bck;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005479
5480 list_for_each_entry_safe(sess, sess_bck, &sv->actconns, by_srv)
5481 if (sess->srv_conn == sv)
Willy Tarreaue7dff022015-04-03 01:14:29 +02005482 stream_shutdown(sess, SF_ERR_KILLED);
Willy Tarreau347a35d2013-11-22 17:51:09 +01005483
5484 altered_servers++;
5485 total_servers++;
5486 }
5487 break;
5488 }
5489 } else {
5490 /* the server name is unknown or ambiguous (duplicate names) */
5491 total_servers++;
5492 }
5493 }
5494 if (reprocess && px && action) {
5495 /* Now, we know the backend and the action chosen by the user.
5496 * We can safely restart from the first server parameter
5497 * to reprocess them
5498 */
5499 cur_param = st_cur_param;
5500 next_param = st_next_param;
5501 reprocess = 0;
5502 goto reprocess_servers;
5503 }
5504
5505 next_param = cur_param;
5506 }
5507 }
5508
5509 if (total_servers == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005510 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005511 }
5512 else if (altered_servers == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005513 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005514 }
5515 else if (altered_servers == total_servers) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005516 appctx->ctx.stats.st_code = STAT_STATUS_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005517 }
5518 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005519 appctx->ctx.stats.st_code = STAT_STATUS_PART;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005520 }
5521 out:
5522 return 1;
5523}
5524
5525
5526static int stats_send_http_headers(struct stream_interface *si)
5527{
Willy Tarreau87b09662015-04-03 00:22:06 +02005528 struct stream *s = si_strm(si);
Willy Tarreau347a35d2013-11-22 17:51:09 +01005529 struct uri_auth *uri = s->be->uri_auth;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005530 struct appctx *appctx = objt_appctx(si->end);
Willy Tarreau347a35d2013-11-22 17:51:09 +01005531
5532 chunk_printf(&trash,
Willy Tarreau8b8995f2014-04-24 22:51:54 +02005533 "HTTP/1.1 200 OK\r\n"
Willy Tarreau347a35d2013-11-22 17:51:09 +01005534 "Cache-Control: no-cache\r\n"
5535 "Connection: close\r\n"
5536 "Content-Type: %s\r\n",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005537 (appctx->ctx.stats.flags & STAT_FMT_HTML) ? "text/html" : "text/plain");
Willy Tarreau347a35d2013-11-22 17:51:09 +01005538
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005539 if (uri->refresh > 0 && !(appctx->ctx.stats.flags & STAT_NO_REFRESH))
Willy Tarreau347a35d2013-11-22 17:51:09 +01005540 chunk_appendf(&trash, "Refresh: %d\r\n",
5541 uri->refresh);
5542
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005543 /* we don't send the CRLF in chunked mode, it will be sent with the first chunk's size */
5544
5545 if (appctx->ctx.stats.flags & STAT_CHUNKED)
5546 chunk_appendf(&trash, "Transfer-Encoding: chunked\r\n");
5547 else
5548 chunk_appendf(&trash, "\r\n");
Willy Tarreau347a35d2013-11-22 17:51:09 +01005549
Willy Tarreaueee5b512015-04-03 23:46:31 +02005550 s->txn->status = 200;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005551 s->logs.tv_request = now;
5552
Willy Tarreaubc18da12015-03-13 14:00:47 +01005553 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02005554 si_applet_cant_put(si);
Willy Tarreau347a35d2013-11-22 17:51:09 +01005555 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01005556 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01005557
5558 return 1;
5559}
5560
5561static int stats_send_http_redirect(struct stream_interface *si)
5562{
5563 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
Willy Tarreau87b09662015-04-03 00:22:06 +02005564 struct stream *s = si_strm(si);
Willy Tarreau347a35d2013-11-22 17:51:09 +01005565 struct uri_auth *uri = s->be->uri_auth;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005566 struct appctx *appctx = objt_appctx(si->end);
Willy Tarreau347a35d2013-11-22 17:51:09 +01005567
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005568 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Willy Tarreau347a35d2013-11-22 17:51:09 +01005569 scope_txt[0] = 0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005570 if (appctx->ctx.stats.scope_len) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01005571 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau4e4292b2014-11-28 12:18:45 +01005572 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 +01005573 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005574 }
5575
5576 /* We don't want to land on the posted stats page because a refresh will
5577 * repost the data. We don't want this to happen on accident so we redirect
5578 * the browse to the stats page with a GET.
5579 */
5580 chunk_printf(&trash,
5581 "HTTP/1.1 303 See Other\r\n"
5582 "Cache-Control: no-cache\r\n"
5583 "Content-Type: text/plain\r\n"
5584 "Connection: close\r\n"
5585 "Location: %s;st=%s%s%s%s\r\n"
Willy Tarreaufdfcc9d2016-01-26 13:57:29 +01005586 "Content-length: 0\r\n"
Willy Tarreau347a35d2013-11-22 17:51:09 +01005587 "\r\n",
5588 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005589 ((appctx->ctx.stats.st_code > STAT_STATUS_INIT) &&
5590 (appctx->ctx.stats.st_code < STAT_STATUS_SIZE) &&
5591 stat_status_codes[appctx->ctx.stats.st_code]) ?
5592 stat_status_codes[appctx->ctx.stats.st_code] :
Willy Tarreau347a35d2013-11-22 17:51:09 +01005593 stat_status_codes[STAT_STATUS_UNKN],
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005594 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
5595 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreau347a35d2013-11-22 17:51:09 +01005596 scope_txt);
5597
Willy Tarreaueee5b512015-04-03 23:46:31 +02005598 s->txn->status = 303;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005599 s->logs.tv_request = now;
5600
Willy Tarreaubc18da12015-03-13 14:00:47 +01005601 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02005602 si_applet_cant_put(si);
Willy Tarreau347a35d2013-11-22 17:51:09 +01005603 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01005604 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01005605
5606 return 1;
5607}
5608
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005609/* This I/O handler runs as an applet embedded in a stream interface. It is
5610 * used to send HTTP stats over a TCP socket. The mechanism is very simple.
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005611 * appctx->st0 contains the operation in progress (dump, done). The handler
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005612 * automatically unregisters itself once transfer is complete.
5613 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02005614static void http_stats_io_handler(struct appctx *appctx)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005615{
Willy Tarreau00a37f02015-04-13 12:05:19 +02005616 struct stream_interface *si = appctx->owner;
Willy Tarreau87b09662015-04-03 00:22:06 +02005617 struct stream *s = si_strm(si);
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005618 struct channel *req = si_oc(si);
5619 struct channel *res = si_ic(si);
Willy Tarreau55058a72012-11-21 08:27:21 +01005620
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005621 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
5622 goto out;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01005623
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005624 /* check that the output is not closed */
5625 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005626 appctx->st0 = STAT_HTTP_DONE;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005627
Willy Tarreau347a35d2013-11-22 17:51:09 +01005628 /* all states are processed in sequence */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005629 if (appctx->st0 == STAT_HTTP_HEAD) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01005630 if (stats_send_http_headers(si)) {
Willy Tarreaueee5b512015-04-03 23:46:31 +02005631 if (s->txn->meth == HTTP_METH_HEAD)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005632 appctx->st0 = STAT_HTTP_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005633 else
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005634 appctx->st0 = STAT_HTTP_DUMP;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005635 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01005636 }
5637
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005638 if (appctx->st0 == STAT_HTTP_DUMP) {
Willy Tarreau4e4292b2014-11-28 12:18:45 +01005639 unsigned int prev_len = si_ib(si)->i;
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005640 unsigned int data_len;
5641 unsigned int last_len;
Willy Tarreaucce36482014-04-24 20:26:41 +02005642 unsigned int last_fwd = 0;
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005643
5644 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
5645 /* One difficulty we're facing is that we must prevent
5646 * the input data from being automatically forwarded to
5647 * the output area. For this, we temporarily disable
5648 * forwarding on the channel.
5649 */
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005650 last_fwd = si_ic(si)->to_forward;
5651 si_ic(si)->to_forward = 0;
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005652 chunk_printf(&trash, "\r\n000000\r\n");
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005653 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02005654 si_applet_cant_put(si);
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005655 si_ic(si)->to_forward = last_fwd;
Willy Tarreau828824a2015-04-19 17:20:03 +02005656 goto out;
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005657 }
5658 }
5659
Willy Tarreau4e4292b2014-11-28 12:18:45 +01005660 data_len = si_ib(si)->i;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005661 if (stats_dump_stat_to_buffer(si, s->be->uri_auth))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005662 appctx->st0 = STAT_HTTP_DONE;
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005663
Willy Tarreau4e4292b2014-11-28 12:18:45 +01005664 last_len = si_ib(si)->i;
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005665
5666 /* Now we must either adjust or remove the chunk size. This is
5667 * not easy because the chunk size might wrap at the end of the
5668 * buffer, so we pretend we have nothing in the buffer, we write
5669 * the size, then restore the buffer's contents. Note that we can
5670 * only do that because no forwarding is scheduled on the stats
5671 * applet.
5672 */
5673 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
Willy Tarreau4e4292b2014-11-28 12:18:45 +01005674 si_ic(si)->total -= (last_len - prev_len);
5675 si_ib(si)->i -= (last_len - prev_len);
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005676
5677 if (last_len != data_len) {
5678 chunk_printf(&trash, "\r\n%06x\r\n", (last_len - data_len));
Willy Tarreaubc18da12015-03-13 14:00:47 +01005679 if (bi_putchk(si_ic(si), &trash) == -1)
Willy Tarreaufe127932015-04-21 19:23:39 +02005680 si_applet_cant_put(si);
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005681
Willy Tarreau4e4292b2014-11-28 12:18:45 +01005682 si_ic(si)->total += (last_len - data_len);
5683 si_ib(si)->i += (last_len - data_len);
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005684 }
5685 /* now re-enable forwarding */
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005686 channel_forward(si_ic(si), last_fwd);
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005687 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005688 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02005689
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005690 if (appctx->st0 == STAT_HTTP_POST) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01005691 if (stats_process_http_post(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005692 appctx->st0 = STAT_HTTP_LAST;
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005693 else if (si_oc(si)->flags & CF_SHUTR)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005694 appctx->st0 = STAT_HTTP_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005695 }
5696
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005697 if (appctx->st0 == STAT_HTTP_LAST) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01005698 if (stats_send_http_redirect(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005699 appctx->st0 = STAT_HTTP_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005700 }
5701
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005702 if (appctx->st0 == STAT_HTTP_DONE) {
5703 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
5704 chunk_printf(&trash, "\r\n0\r\n\r\n");
Willy Tarreaubc18da12015-03-13 14:00:47 +01005705 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02005706 si_applet_cant_put(si);
Willy Tarreau828824a2015-04-19 17:20:03 +02005707 goto out;
Willy Tarreaubc18da12015-03-13 14:00:47 +01005708 }
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005709 }
5710 /* eat the whole request */
Willy Tarreau4e4292b2014-11-28 12:18:45 +01005711 bo_skip(si_oc(si), si_ob(si)->o);
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005712 res->flags |= CF_READ_NULL;
5713 si_shutr(si);
5714 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01005715
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005716 if ((res->flags & CF_SHUTR) && (si->state == SI_ST_EST))
5717 si_shutw(si);
Willy Tarreau91861262007-10-17 17:06:05 +02005718
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005719 if (appctx->st0 == STAT_HTTP_DONE) {
Willy Tarreau96d44912013-11-22 12:25:24 +01005720 if ((req->flags & CF_SHUTW) && (si->state == SI_ST_EST)) {
5721 si_shutr(si);
5722 res->flags |= CF_READ_NULL;
5723 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005724 }
Willy Tarreau828824a2015-04-19 17:20:03 +02005725 out:
Willy Tarreaud4da1962015-04-20 01:31:23 +02005726 /* just to make gcc happy */ ;
Willy Tarreau91861262007-10-17 17:06:05 +02005727}
5728
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005729
Willy Tarreau909d5172012-11-26 03:04:41 +01005730static inline const char *get_conn_ctrl_name(const struct connection *conn)
5731{
Willy Tarreau3c728722014-01-23 13:50:42 +01005732 if (!conn_ctrl_ready(conn))
Willy Tarreau909d5172012-11-26 03:04:41 +01005733 return "NONE";
5734 return conn->ctrl->name;
5735}
5736
5737static inline const char *get_conn_xprt_name(const struct connection *conn)
5738{
5739 static char ptr[17];
5740
Willy Tarreauaad69382014-01-23 14:21:42 +01005741 if (!conn_xprt_ready(conn))
Willy Tarreau909d5172012-11-26 03:04:41 +01005742 return "NONE";
5743
5744 if (conn->xprt == &raw_sock)
5745 return "RAW";
5746
5747#ifdef USE_OPENSSL
5748 if (conn->xprt == &ssl_sock)
5749 return "SSL";
5750#endif
5751 snprintf(ptr, sizeof(ptr), "%p", conn->xprt);
5752 return ptr;
5753}
5754
5755static inline const char *get_conn_data_name(const struct connection *conn)
5756{
5757 static char ptr[17];
5758
5759 if (!conn->data)
5760 return "NONE";
5761
5762 if (conn->data == &sess_conn_cb)
5763 return "SESS";
5764
5765 if (conn->data == &si_conn_cb)
5766 return "STRM";
5767
5768 if (conn->data == &check_conn_cb)
5769 return "CHCK";
5770
5771 snprintf(ptr, sizeof(ptr), "%p", conn->data);
5772 return ptr;
5773}
5774
Willy Tarreau87b09662015-04-03 00:22:06 +02005775/* This function dumps a complete stream state onto the stream interface's
5776 * read buffer. The stream has to be set in sess->target. It returns
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01005777 * 0 if the output buffer is full and it needs to be called again, otherwise
5778 * non-zero. It is designed to be called from stats_dump_sess_to_buffer() below.
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005779 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005780static int stats_dump_full_sess_to_buffer(struct stream_interface *si, struct stream *sess)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005781{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005782 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005783 struct tm tm;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005784 extern const char *monthname[12];
5785 char pn[INET6_ADDRSTRLEN];
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005786 struct connection *conn;
Willy Tarreau284ddbf2013-12-01 20:45:00 +01005787 struct appctx *tmpctx;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005788
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005789 chunk_reset(&trash);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005790
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005791 if (appctx->ctx.sess.section > 0 && appctx->ctx.sess.uid != sess->uniq_id) {
Willy Tarreau87b09662015-04-03 00:22:06 +02005792 /* stream changed, no need to go any further */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005793 chunk_appendf(&trash, " *** session terminated while we were watching it ***\n");
Willy Tarreaubc18da12015-03-13 14:00:47 +01005794 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02005795 si_applet_cant_put(si);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005796 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01005797 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005798 appctx->ctx.sess.uid = 0;
5799 appctx->ctx.sess.section = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005800 return 1;
5801 }
5802
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005803 switch (appctx->ctx.sess.section) {
Willy Tarreau87b09662015-04-03 00:22:06 +02005804 case 0: /* main status of the stream */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005805 appctx->ctx.sess.uid = sess->uniq_id;
5806 appctx->ctx.sess.section = 1;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005807 /* fall through */
5808
5809 case 1:
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005810 get_localtime(sess->logs.accept_date.tv_sec, &tm);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005811 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005812 "%p: [%02d/%s/%04d:%02d:%02d:%02d.%06d] id=%u proto=%s",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005813 sess,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005814 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
5815 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(sess->logs.accept_date.tv_usec),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005816 sess->uniq_id,
Willy Tarreau666f5042015-06-17 19:49:52 +02005817 strm_li(sess) ? strm_li(sess)->proto->name : "?");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005818
Willy Tarreaud0d8da92015-04-04 02:10:38 +02005819 conn = objt_conn(strm_orig(sess));
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005820 switch (conn ? addr_to_str(&conn->addr.from, pn, sizeof(pn)) : AF_UNSPEC) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005821 case AF_INET:
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005822 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005823 chunk_appendf(&trash, " source=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005824 pn, get_host_port(&conn->addr.from));
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005825 break;
5826 case AF_UNIX:
Willy Tarreaud0d8da92015-04-04 02:10:38 +02005827 chunk_appendf(&trash, " source=unix:%d\n", strm_li(sess)->luid);
Emeric Brun837ca522010-10-22 16:19:01 +02005828 break;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005829 default:
5830 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005831 chunk_appendf(&trash, "\n");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005832 break;
5833 }
5834
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005835 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005836 " flags=0x%x, conn_retries=%d, srv_conn=%p, pend_pos=%p\n",
Willy Tarreauee28de02010-06-01 09:51:00 +02005837 sess->flags, sess->si[1].conn_retries, sess->srv_conn, sess->pend_pos);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005838
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005839 chunk_appendf(&trash,
Daniel Schultze90690c72012-03-23 10:53:36 -07005840 " frontend=%s (id=%u mode=%s), listener=%s (id=%u)",
Willy Tarreaud0d8da92015-04-04 02:10:38 +02005841 strm_fe(sess)->id, strm_fe(sess)->uuid, strm_fe(sess)->mode ? "http" : "tcp",
5842 strm_li(sess) ? strm_li(sess)->name ? strm_li(sess)->name : "?" : "?",
5843 strm_li(sess) ? strm_li(sess)->luid : 0);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005844
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005845 if (conn)
5846 conn_get_to_addr(conn);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005847
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005848 switch (conn ? addr_to_str(&conn->addr.to, pn, sizeof(pn)) : AF_UNSPEC) {
Daniel Schultze90690c72012-03-23 10:53:36 -07005849 case AF_INET:
5850 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005851 chunk_appendf(&trash, " addr=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005852 pn, get_host_port(&conn->addr.to));
Daniel Schultze90690c72012-03-23 10:53:36 -07005853 break;
5854 case AF_UNIX:
Willy Tarreaud0d8da92015-04-04 02:10:38 +02005855 chunk_appendf(&trash, " addr=unix:%d\n", strm_li(sess)->luid);
Daniel Schultze90690c72012-03-23 10:53:36 -07005856 break;
5857 default:
5858 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005859 chunk_appendf(&trash, "\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07005860 break;
5861 }
5862
Willy Tarreau50943332011-09-02 17:33:05 +02005863 if (sess->be->cap & PR_CAP_BE)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005864 chunk_appendf(&trash,
Daniel Schultze90690c72012-03-23 10:53:36 -07005865 " backend=%s (id=%u mode=%s)",
Willy Tarreau50943332011-09-02 17:33:05 +02005866 sess->be->id,
Daniel Schultze90690c72012-03-23 10:53:36 -07005867 sess->be->uuid, sess->be->mode ? "http" : "tcp");
5868 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005869 chunk_appendf(&trash, " backend=<NONE> (id=-1 mode=-)");
Daniel Schultze90690c72012-03-23 10:53:36 -07005870
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005871 conn = objt_conn(sess->si[1].end);
5872 if (conn)
5873 conn_get_from_addr(conn);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005874
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005875 switch (conn ? addr_to_str(&conn->addr.from, pn, sizeof(pn)) : AF_UNSPEC) {
Daniel Schultze90690c72012-03-23 10:53:36 -07005876 case AF_INET:
5877 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005878 chunk_appendf(&trash, " addr=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005879 pn, get_host_port(&conn->addr.from));
Daniel Schultze90690c72012-03-23 10:53:36 -07005880 break;
5881 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005882 chunk_appendf(&trash, " addr=unix\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07005883 break;
5884 default:
5885 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005886 chunk_appendf(&trash, "\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07005887 break;
5888 }
5889
5890 if (sess->be->cap & PR_CAP_BE)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005891 chunk_appendf(&trash,
Daniel Schultze90690c72012-03-23 10:53:36 -07005892 " server=%s (id=%u)",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005893 objt_server(sess->target) ? objt_server(sess->target)->id : "<none>",
5894 objt_server(sess->target) ? objt_server(sess->target)->puid : 0);
Willy Tarreau50943332011-09-02 17:33:05 +02005895 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005896 chunk_appendf(&trash, " server=<NONE> (id=-1)");
Daniel Schultze90690c72012-03-23 10:53:36 -07005897
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005898 if (conn)
5899 conn_get_to_addr(conn);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005900
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005901 switch (conn ? addr_to_str(&conn->addr.to, pn, sizeof(pn)) : AF_UNSPEC) {
Daniel Schultze90690c72012-03-23 10:53:36 -07005902 case AF_INET:
5903 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005904 chunk_appendf(&trash, " addr=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005905 pn, get_host_port(&conn->addr.to));
Daniel Schultze90690c72012-03-23 10:53:36 -07005906 break;
5907 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005908 chunk_appendf(&trash, " addr=unix\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07005909 break;
5910 default:
5911 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005912 chunk_appendf(&trash, "\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07005913 break;
5914 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005915
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005916 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005917 " task=%p (state=0x%02x nice=%d calls=%d exp=%s%s",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005918 sess->task,
5919 sess->task->state,
5920 sess->task->nice, sess->task->calls,
5921 sess->task->expire ?
5922 tick_is_expired(sess->task->expire, now_ms) ? "<PAST>" :
5923 human_time(TICKS_TO_MS(sess->task->expire - now_ms),
5924 TICKS_TO_MS(1000)) : "<NEVER>",
5925 task_in_rq(sess->task) ? ", running" : "");
5926
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005927 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005928 " age=%s)\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005929 human_time(now.tv_sec - sess->logs.accept_date.tv_sec, 1));
5930
Willy Tarreaueee5b512015-04-03 23:46:31 +02005931 if (sess->txn)
5932 chunk_appendf(&trash,
Willy Tarreau98410192014-11-26 18:05:38 +01005933 " 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 +02005934 sess->txn, sess->txn->flags, sess->txn->meth, sess->txn->status,
5935 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 +01005936
5937 chunk_appendf(&trash,
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02005938 " si[0]=%p (state=%s flags=0x%02x endp0=%s:%p exp=%s, et=0x%03x)\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005939 &sess->si[0],
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005940 si_state_str(sess->si[0].state),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005941 sess->si[0].flags,
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005942 obj_type_name(sess->si[0].end),
5943 obj_base_ptr(sess->si[0].end),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005944 sess->si[0].exp ?
5945 tick_is_expired(sess->si[0].exp, now_ms) ? "<PAST>" :
5946 human_time(TICKS_TO_MS(sess->si[0].exp - now_ms),
5947 TICKS_TO_MS(1000)) : "<NEVER>",
5948 sess->si[0].err_type);
5949
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005950 chunk_appendf(&trash,
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02005951 " si[1]=%p (state=%s flags=0x%02x endp1=%s:%p exp=%s, et=0x%03x)\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005952 &sess->si[1],
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005953 si_state_str(sess->si[1].state),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005954 sess->si[1].flags,
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005955 obj_type_name(sess->si[1].end),
5956 obj_base_ptr(sess->si[1].end),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005957 sess->si[1].exp ?
5958 tick_is_expired(sess->si[1].exp, now_ms) ? "<PAST>" :
5959 human_time(TICKS_TO_MS(sess->si[1].exp - now_ms),
5960 TICKS_TO_MS(1000)) : "<NEVER>",
5961 sess->si[1].err_type);
5962
Willy Tarreau284ddbf2013-12-01 20:45:00 +01005963 if ((conn = objt_conn(sess->si[0].end)) != NULL) {
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005964 chunk_appendf(&trash,
5965 " co0=%p ctrl=%s xprt=%s data=%s target=%s:%p\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005966 conn,
5967 get_conn_ctrl_name(conn),
5968 get_conn_xprt_name(conn),
5969 get_conn_data_name(conn),
5970 obj_type_name(conn->target),
5971 obj_base_ptr(conn->target));
Willy Tarreau909d5172012-11-26 03:04:41 +01005972
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005973 chunk_appendf(&trash,
Willy Tarreau16f649c2014-01-25 19:10:48 +01005974 " flags=0x%08x fd=%d fd.state=%02x fd.cache=%d updt=%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005975 conn->flags,
5976 conn->t.sock.fd,
Willy Tarreau69a41fa2014-01-20 11:02:59 +01005977 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].state : 0,
Willy Tarreau15a4dec2014-01-20 11:09:39 +01005978 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].cache : 0,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005979 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].updated : 0);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005980 }
Willy Tarreau284ddbf2013-12-01 20:45:00 +01005981 else if ((tmpctx = objt_appctx(sess->si[0].end)) != NULL) {
5982 chunk_appendf(&trash,
5983 " app0=%p st0=%d st1=%d st2=%d applet=%s\n",
5984 tmpctx,
5985 tmpctx->st0,
5986 tmpctx->st1,
5987 tmpctx->st2,
5988 tmpctx->applet->name);
5989 }
Willy Tarreaubc174aa2012-11-19 16:10:32 +01005990
Willy Tarreau284ddbf2013-12-01 20:45:00 +01005991 if ((conn = objt_conn(sess->si[1].end)) != NULL) {
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005992 chunk_appendf(&trash,
5993 " co1=%p ctrl=%s xprt=%s data=%s target=%s:%p\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005994 conn,
5995 get_conn_ctrl_name(conn),
5996 get_conn_xprt_name(conn),
5997 get_conn_data_name(conn),
5998 obj_type_name(conn->target),
5999 obj_base_ptr(conn->target));
Willy Tarreau909d5172012-11-26 03:04:41 +01006000
Willy Tarreaucf644ed2013-09-29 17:19:56 +02006001 chunk_appendf(&trash,
Willy Tarreauceeafb52016-01-25 15:27:17 +01006002 " flags=0x%08x fd=%d fd.state=%02x fd.cache=%d updt=%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006003 conn->flags,
6004 conn->t.sock.fd,
Willy Tarreau69a41fa2014-01-20 11:02:59 +01006005 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].state : 0,
Willy Tarreau15a4dec2014-01-20 11:09:39 +01006006 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].cache : 0,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006007 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].updated : 0);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02006008 }
Willy Tarreau284ddbf2013-12-01 20:45:00 +01006009 else if ((tmpctx = objt_appctx(sess->si[1].end)) != NULL) {
6010 chunk_appendf(&trash,
6011 " app1=%p st0=%d st1=%d st2=%d applet=%s\n",
6012 tmpctx,
6013 tmpctx->st0,
6014 tmpctx->st1,
6015 tmpctx->st2,
6016 tmpctx->applet->name);
6017 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006018
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006019 chunk_appendf(&trash,
Willy Tarreau909d5172012-11-26 03:04:41 +01006020 " req=%p (f=0x%06x an=0x%x pipe=%d tofwd=%d total=%lld)\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006021 " an_exp=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006022 &sess->req,
6023 sess->req.flags, sess->req.analysers,
6024 sess->req.pipe ? sess->req.pipe->data : 0,
6025 sess->req.to_forward, sess->req.total,
6026 sess->req.analyse_exp ?
6027 human_time(TICKS_TO_MS(sess->req.analyse_exp - now_ms),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006028 TICKS_TO_MS(1000)) : "<NEVER>");
6029
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006030 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006031 " rex=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006032 sess->req.rex ?
6033 human_time(TICKS_TO_MS(sess->req.rex - now_ms),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006034 TICKS_TO_MS(1000)) : "<NEVER>");
6035
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006036 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006037 " wex=%s\n"
Willy Tarreau909d5172012-11-26 03:04:41 +01006038 " buf=%p data=%p o=%d p=%d req.next=%d i=%d size=%d\n",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006039 sess->req.wex ?
6040 human_time(TICKS_TO_MS(sess->req.wex - now_ms),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006041 TICKS_TO_MS(1000)) : "<NEVER>",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006042 sess->req.buf,
6043 sess->req.buf->data, sess->req.buf->o,
6044 (int)(sess->req.buf->p - sess->req.buf->data),
Willy Tarreaueee5b512015-04-03 23:46:31 +02006045 sess->txn ? sess->txn->req.next : 0, sess->req.buf->i,
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006046 sess->req.buf->size);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006047
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006048 chunk_appendf(&trash,
Willy Tarreau909d5172012-11-26 03:04:41 +01006049 " res=%p (f=0x%06x an=0x%x pipe=%d tofwd=%d total=%lld)\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006050 " an_exp=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006051 &sess->res,
6052 sess->res.flags, sess->res.analysers,
6053 sess->res.pipe ? sess->res.pipe->data : 0,
6054 sess->res.to_forward, sess->res.total,
6055 sess->res.analyse_exp ?
6056 human_time(TICKS_TO_MS(sess->res.analyse_exp - now_ms),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006057 TICKS_TO_MS(1000)) : "<NEVER>");
6058
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006059 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006060 " rex=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006061 sess->res.rex ?
6062 human_time(TICKS_TO_MS(sess->res.rex - now_ms),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006063 TICKS_TO_MS(1000)) : "<NEVER>");
6064
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006065 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006066 " wex=%s\n"
Willy Tarreau909d5172012-11-26 03:04:41 +01006067 " buf=%p data=%p o=%d p=%d rsp.next=%d i=%d size=%d\n",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006068 sess->res.wex ?
6069 human_time(TICKS_TO_MS(sess->res.wex - now_ms),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006070 TICKS_TO_MS(1000)) : "<NEVER>",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006071 sess->res.buf,
6072 sess->res.buf->data, sess->res.buf->o,
6073 (int)(sess->res.buf->p - sess->res.buf->data),
Willy Tarreaueee5b512015-04-03 23:46:31 +02006074 sess->txn ? sess->txn->rsp.next : 0, sess->res.buf->i,
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006075 sess->res.buf->size);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006076
Willy Tarreaubc18da12015-03-13 14:00:47 +01006077 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02006078 si_applet_cant_put(si);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006079 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01006080 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006081
6082 /* use other states to dump the contents */
6083 }
6084 /* end of dump */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006085 appctx->ctx.sess.uid = 0;
6086 appctx->ctx.sess.section = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006087 return 1;
6088}
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006089
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02006090#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
6091static int stats_tlskeys_list(struct stream_interface *si) {
6092 struct appctx *appctx = __objt_appctx(si->end);
6093
6094 switch (appctx->st2) {
6095 case STAT_ST_INIT:
6096 /* Display the column headers. If the message cannot be sent,
6097 * quit the fucntion with returning 0. The function is called
6098 * later and restart at the state "STAT_ST_INIT".
6099 */
6100 chunk_reset(&trash);
6101 chunk_appendf(&trash, "# id (file)\n");
6102 if (bi_putchk(si_ic(si), &trash) == -1) {
6103 si_applet_cant_put(si);
6104 return 0;
6105 }
6106
6107 /* Now, we start the browsing of the references lists.
6108 * Note that the following call to LIST_ELEM return bad pointer. The only
6109 * avalaible field of this pointer is <list>. It is used with the function
6110 * tlskeys_list_get_next() for retruning the first avalaible entry
6111 */
6112 appctx->ctx.tlskeys.ref = LIST_ELEM(&tlskeys_reference, struct tls_keys_ref *, list);
6113 appctx->ctx.tlskeys.ref = tlskeys_list_get_next(appctx->ctx.tlskeys.ref, &tlskeys_reference);
6114
6115 appctx->st2 = STAT_ST_LIST;
6116 /* fall through */
6117
6118 case STAT_ST_LIST:
6119 while (appctx->ctx.tlskeys.ref) {
6120 chunk_reset(&trash);
6121
6122 chunk_appendf(&trash, "%d (%s)\n", appctx->ctx.tlskeys.ref->unique_id,
6123 appctx->ctx.tlskeys.ref->filename);
6124
6125 if (bi_putchk(si_ic(si), &trash) == -1) {
6126 /* let's try again later from this stream. We add ourselves into
6127 * this stream's users so that it can remove us upon termination.
6128 */
6129 si_applet_cant_put(si);
6130 return 0;
6131 }
6132
6133 /* get next list entry and check the end of the list */
6134 appctx->ctx.tlskeys.ref = tlskeys_list_get_next(appctx->ctx.tlskeys.ref, &tlskeys_reference);
6135 }
6136
6137 appctx->st2 = STAT_ST_FIN;
6138 /* fall through */
6139
6140 default:
6141 appctx->st2 = STAT_ST_FIN;
6142 return 1;
6143 }
6144 return 0;
6145}
6146#endif
6147
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006148static int stats_pats_list(struct stream_interface *si)
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006149{
6150 struct appctx *appctx = __objt_appctx(si->end);
6151
6152 switch (appctx->st2) {
6153 case STAT_ST_INIT:
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01006154 /* Display the column headers. If the message cannot be sent,
6155 * quit the fucntion with returning 0. The function is called
6156 * later and restart at the state "STAT_ST_INIT".
6157 */
6158 chunk_reset(&trash);
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01006159 chunk_appendf(&trash, "# id (file) description\n");
Willy Tarreaubc18da12015-03-13 14:00:47 +01006160 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02006161 si_applet_cant_put(si);
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01006162 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01006163 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006164
6165 /* Now, we start the browsing of the references lists.
6166 * Note that the following call to LIST_ELEM return bad pointer. The only
6167 * avalaible field of this pointer is <list>. It is used with the function
6168 * pat_list_get_next() for retruning the first avalaible entry
6169 */
6170 appctx->ctx.map.ref = LIST_ELEM(&pattern_reference, struct pat_ref *, list);
6171 appctx->ctx.map.ref = pat_list_get_next(appctx->ctx.map.ref, &pattern_reference,
6172 appctx->ctx.map.display_flags);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006173 appctx->st2 = STAT_ST_LIST;
6174 /* fall through */
6175
6176 case STAT_ST_LIST:
6177 while (appctx->ctx.map.ref) {
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006178 chunk_reset(&trash);
6179
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006180 /* Build messages. If the reference is used by another category than
6181 * the listed categorie, display the information in the massage.
6182 */
Thierry FOURNIERf7e04e92014-03-20 11:45:47 +01006183 chunk_appendf(&trash, "%d (%s) %s\n", appctx->ctx.map.ref->unique_id,
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01006184 appctx->ctx.map.ref->reference ? appctx->ctx.map.ref->reference : "",
6185 appctx->ctx.map.ref->display);
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01006186
Willy Tarreau2bb4a962014-11-28 11:11:05 +01006187 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau87b09662015-04-03 00:22:06 +02006188 /* let's try again later from this stream. We add ourselves into
6189 * this stream's users so that it can remove us upon termination.
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006190 */
Willy Tarreaufe127932015-04-21 19:23:39 +02006191 si_applet_cant_put(si);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006192 return 0;
6193 }
6194
6195 /* get next list entry and check the end of the list */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006196 appctx->ctx.map.ref = pat_list_get_next(appctx->ctx.map.ref, &pattern_reference,
6197 appctx->ctx.map.display_flags);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006198 }
6199
6200 appctx->st2 = STAT_ST_FIN;
6201 /* fall through */
6202
6203 default:
6204 appctx->st2 = STAT_ST_FIN;
6205 return 1;
6206 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006207 return 0;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006208}
6209
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006210static int stats_map_lookup(struct stream_interface *si)
6211{
6212 struct appctx *appctx = __objt_appctx(si->end);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006213 struct sample sample;
6214 struct pattern *pat;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006215 int match_method;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006216
6217 switch (appctx->st2) {
6218 case STAT_ST_INIT:
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006219 /* Init to the first entry. The list cannot be change */
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01006220 appctx->ctx.map.expr = LIST_ELEM(&appctx->ctx.map.ref->pat, struct pattern_expr *, list);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006221 appctx->ctx.map.expr = pat_expr_get_next(appctx->ctx.map.expr, &appctx->ctx.map.ref->pat);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006222 appctx->st2 = STAT_ST_LIST;
6223 /* fall through */
6224
6225 case STAT_ST_LIST:
6226 /* for each lookup type */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006227 while (appctx->ctx.map.expr) {
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006228 /* initialise chunk to build new message */
6229 chunk_reset(&trash);
6230
6231 /* execute pattern matching */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02006232 sample.data.type = SMP_T_STR;
Andreas Seltenreich9727cf42016-03-03 19:32:25 +01006233 sample.flags = SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02006234 sample.data.u.str.len = appctx->ctx.map.chunk.len;
6235 sample.data.u.str.str = appctx->ctx.map.chunk.str;
Thierry FOURNIER5d344082014-01-27 14:19:53 +01006236 if (appctx->ctx.map.expr->pat_head->match &&
6237 sample_convert(&sample, appctx->ctx.map.expr->pat_head->expect_type))
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006238 pat = appctx->ctx.map.expr->pat_head->match(&sample, appctx->ctx.map.expr, 1);
6239 else
6240 pat = NULL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006241
6242 /* build return message: set type of match */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006243 for (match_method=0; match_method<PAT_MATCH_NUM; match_method++)
6244 if (appctx->ctx.map.expr->pat_head->match == pat_match_fcts[match_method])
6245 break;
6246 if (match_method >= PAT_MATCH_NUM)
6247 chunk_appendf(&trash, "type=unknown(%p)", appctx->ctx.map.expr->pat_head->match);
6248 else
6249 chunk_appendf(&trash, "type=%s", pat_match_names[match_method]);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006250
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02006251 /* case sensitive */
6252 if (appctx->ctx.map.expr->mflags & PAT_MF_IGNORE_CASE)
6253 chunk_appendf(&trash, ", case=insensitive");
6254 else
6255 chunk_appendf(&trash, ", case=sensitive");
6256
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006257 /* Display no match, and set default value */
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01006258 if (!pat) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006259 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
6260 chunk_appendf(&trash, ", found=no");
6261 else
6262 chunk_appendf(&trash, ", match=no");
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006263 }
6264
6265 /* Display match and match info */
6266 else {
6267 /* display match */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006268 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
6269 chunk_appendf(&trash, ", found=yes");
6270 else
6271 chunk_appendf(&trash, ", match=yes");
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006272
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01006273 /* display index mode */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02006274 if (pat->sflags & PAT_SF_TREE)
Thierry FOURNIERb9903842014-03-11 18:48:17 +01006275 chunk_appendf(&trash, ", idx=tree");
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006276 else
Thierry FOURNIERb9903842014-03-11 18:48:17 +01006277 chunk_appendf(&trash, ", idx=list");
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006278
Thierry FOURNIER86db66a2014-03-11 18:37:58 +01006279 /* display pattern */
6280 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
6281 if (pat->ref && pat->ref->pattern)
6282 chunk_appendf(&trash, ", key=\"%s\"", pat->ref->pattern);
6283 else
6284 chunk_appendf(&trash, ", key=unknown");
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006285 }
6286 else {
Thierry FOURNIER86db66a2014-03-11 18:37:58 +01006287 if (pat->ref && pat->ref->pattern)
6288 chunk_appendf(&trash, ", pattern=\"%s\"", pat->ref->pattern);
6289 else
6290 chunk_appendf(&trash, ", pattern=unknown");
6291 }
6292
6293 /* display return value */
6294 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
Thierry FOURNIER503bb092015-08-19 08:35:43 +02006295 if (pat->data && pat->ref && pat->ref->sample)
Thierry FOURNIER03d0e452015-07-23 18:33:41 +02006296 chunk_appendf(&trash, ", value=\"%s\", type=\"%s\"", pat->ref->sample,
Thierry FOURNIER7d4335c2015-08-19 09:05:25 +02006297 smp_to_type[pat->data->type]);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006298 else
Thierry FOURNIER86db66a2014-03-11 18:37:58 +01006299 chunk_appendf(&trash, ", value=none");
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006300 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006301 }
6302
Thierry FOURNIERb9903842014-03-11 18:48:17 +01006303 chunk_appendf(&trash, "\n");
6304
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006305 /* display response */
Willy Tarreau2bb4a962014-11-28 11:11:05 +01006306 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau87b09662015-04-03 00:22:06 +02006307 /* let's try again later from this stream. We add ourselves into
6308 * this stream's users so that it can remove us upon termination.
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006309 */
Willy Tarreaufe127932015-04-21 19:23:39 +02006310 si_applet_cant_put(si);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006311 return 0;
6312 }
6313
6314 /* get next entry */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006315 appctx->ctx.map.expr = pat_expr_get_next(appctx->ctx.map.expr,
6316 &appctx->ctx.map.ref->pat);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006317 }
6318
6319 appctx->st2 = STAT_ST_FIN;
6320 /* fall through */
6321
6322 default:
6323 appctx->st2 = STAT_ST_FIN;
6324 free(appctx->ctx.map.chunk.str);
6325 return 1;
6326 }
6327}
6328
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006329static int stats_pat_list(struct stream_interface *si)
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006330{
6331 struct appctx *appctx = __objt_appctx(si->end);
6332
6333 switch (appctx->st2) {
6334
6335 case STAT_ST_INIT:
6336 /* Init to the first entry. The list cannot be change */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006337 appctx->ctx.map.elt = LIST_NEXT(&appctx->ctx.map.ref->head,
6338 struct pat_ref_elt *, list);
6339 if (&appctx->ctx.map.elt->list == &appctx->ctx.map.ref->head)
6340 appctx->ctx.map.elt = NULL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006341 appctx->st2 = STAT_ST_LIST;
6342 /* fall through */
6343
6344 case STAT_ST_LIST:
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006345 while (appctx->ctx.map.elt) {
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006346 chunk_reset(&trash);
6347
6348 /* build messages */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006349 if (appctx->ctx.map.elt->sample)
Thierry FOURNIER9356c682014-01-28 15:55:37 +01006350 chunk_appendf(&trash, "%p %s %s\n",
6351 appctx->ctx.map.elt, appctx->ctx.map.elt->pattern,
6352 appctx->ctx.map.elt->sample);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006353 else
Thierry FOURNIER9356c682014-01-28 15:55:37 +01006354 chunk_appendf(&trash, "%p %s\n",
6355 appctx->ctx.map.elt, appctx->ctx.map.elt->pattern);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006356
Willy Tarreau2bb4a962014-11-28 11:11:05 +01006357 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau87b09662015-04-03 00:22:06 +02006358 /* let's try again later from this stream. We add ourselves into
6359 * this stream's users so that it can remove us upon termination.
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006360 */
Willy Tarreaufe127932015-04-21 19:23:39 +02006361 si_applet_cant_put(si);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006362 return 0;
6363 }
6364
6365 /* get next list entry and check the end of the list */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006366 appctx->ctx.map.elt = LIST_NEXT(&appctx->ctx.map.elt->list,
6367 struct pat_ref_elt *, list);
6368 if (&appctx->ctx.map.elt->list == &appctx->ctx.map.ref->head)
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006369 break;
6370 }
6371
6372 appctx->st2 = STAT_ST_FIN;
6373 /* fall through */
6374
6375 default:
6376 appctx->st2 = STAT_ST_FIN;
6377 return 1;
6378 }
6379}
6380
Willy Tarreau87b09662015-04-03 00:22:06 +02006381/* This function dumps all streams' states onto the stream interface's
Willy Tarreau306f8302013-07-08 15:53:06 +02006382 * read buffer. It returns 0 if the output buffer is full and it needs
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01006383 * to be called again, otherwise non-zero. It is designed to be called
6384 * from stats_dump_sess_to_buffer() below.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006385 */
Simon Horman9bd2c732011-06-15 15:18:44 +09006386static int stats_dump_sess_to_buffer(struct stream_interface *si)
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006387{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006388 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006389 struct connection *conn;
6390
Willy Tarreau2bb4a962014-11-28 11:11:05 +01006391 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW))) {
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006392 /* If we're forced to shut down, we might have to remove our
Willy Tarreau87b09662015-04-03 00:22:06 +02006393 * reference to the last stream being dumped.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006394 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006395 if (appctx->st2 == STAT_ST_LIST) {
6396 if (!LIST_ISEMPTY(&appctx->ctx.sess.bref.users)) {
6397 LIST_DEL(&appctx->ctx.sess.bref.users);
6398 LIST_INIT(&appctx->ctx.sess.bref.users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +01006399 }
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006400 }
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02006401 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006402 }
6403
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006404 chunk_reset(&trash);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006405
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006406 switch (appctx->st2) {
Willy Tarreau295a8372011-03-10 11:25:07 +01006407 case STAT_ST_INIT:
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006408 /* the function had not been called yet, let's prepare the
Willy Tarreau87b09662015-04-03 00:22:06 +02006409 * buffer for a response. We initialize the current stream
Willy Tarreaufd3828e2009-02-22 15:17:24 +01006410 * pointer to the first in the global list. When a target
Willy Tarreau87b09662015-04-03 00:22:06 +02006411 * stream is being destroyed, it is responsible for updating
Willy Tarreaufd3828e2009-02-22 15:17:24 +01006412 * this pointer. We know we have reached the end when this
Willy Tarreau87b09662015-04-03 00:22:06 +02006413 * pointer points back to the head of the streams list.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006414 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006415 LIST_INIT(&appctx->ctx.sess.bref.users);
Willy Tarreau87b09662015-04-03 00:22:06 +02006416 appctx->ctx.sess.bref.ref = streams.n;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006417 appctx->st2 = STAT_ST_LIST;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006418 /* fall through */
6419
Willy Tarreau295a8372011-03-10 11:25:07 +01006420 case STAT_ST_LIST:
Willy Tarreau87b09662015-04-03 00:22:06 +02006421 /* first, let's detach the back-ref from a possible previous stream */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006422 if (!LIST_ISEMPTY(&appctx->ctx.sess.bref.users)) {
6423 LIST_DEL(&appctx->ctx.sess.bref.users);
6424 LIST_INIT(&appctx->ctx.sess.bref.users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +01006425 }
6426
6427 /* and start from where we stopped */
Willy Tarreau87b09662015-04-03 00:22:06 +02006428 while (appctx->ctx.sess.bref.ref != &streams) {
Cyril Bontéacd7d632010-11-01 19:26:02 +01006429 char pn[INET6_ADDRSTRLEN];
Willy Tarreau87b09662015-04-03 00:22:06 +02006430 struct stream *curr_sess;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006431
Willy Tarreau87b09662015-04-03 00:22:06 +02006432 curr_sess = LIST_ELEM(appctx->ctx.sess.bref.ref, struct stream *, list);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006433
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006434 if (appctx->ctx.sess.target) {
6435 if (appctx->ctx.sess.target != (void *)-1 && appctx->ctx.sess.target != curr_sess)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006436 goto next_sess;
6437
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006438 LIST_ADDQ(&curr_sess->back_refs, &appctx->ctx.sess.bref.users);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006439 /* call the proper dump() function and return if we're missing space */
Willy Tarreau76153662012-11-26 01:16:39 +01006440 if (!stats_dump_full_sess_to_buffer(si, curr_sess))
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006441 return 0;
6442
Willy Tarreau87b09662015-04-03 00:22:06 +02006443 /* stream dump complete */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006444 LIST_DEL(&appctx->ctx.sess.bref.users);
6445 LIST_INIT(&appctx->ctx.sess.bref.users);
6446 if (appctx->ctx.sess.target != (void *)-1) {
6447 appctx->ctx.sess.target = NULL;
Willy Tarreau76153662012-11-26 01:16:39 +01006448 break;
6449 }
6450 else
6451 goto next_sess;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006452 }
6453
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006454 chunk_appendf(&trash,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006455 "%p: proto=%s",
6456 curr_sess,
Willy Tarreauf1e02122015-09-23 12:16:43 +02006457 strm_li(curr_sess) ? strm_li(curr_sess)->proto->name : "?");
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006458
Willy Tarreaud0d8da92015-04-04 02:10:38 +02006459 conn = objt_conn(strm_orig(curr_sess));
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006460 switch (conn ? addr_to_str(&conn->addr.from, pn, sizeof(pn)) : AF_UNSPEC) {
Willy Tarreau631f01c2011-09-05 00:36:48 +02006461 case AF_INET:
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006462 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006463 chunk_appendf(&trash,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006464 " src=%s:%d fe=%s be=%s srv=%s",
6465 pn,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006466 get_host_port(&conn->addr.from),
Willy Tarreaud0d8da92015-04-04 02:10:38 +02006467 strm_fe(curr_sess)->id,
Willy Tarreau50943332011-09-02 17:33:05 +02006468 (curr_sess->be->cap & PR_CAP_BE) ? curr_sess->be->id : "<NONE>",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006469 objt_server(curr_sess->target) ? objt_server(curr_sess->target)->id : "<none>"
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006470 );
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006471 break;
6472 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006473 chunk_appendf(&trash,
Emeric Brun837ca522010-10-22 16:19:01 +02006474 " src=unix:%d fe=%s be=%s srv=%s",
Willy Tarreaud0d8da92015-04-04 02:10:38 +02006475 strm_li(curr_sess)->luid,
6476 strm_fe(curr_sess)->id,
Willy Tarreau50943332011-09-02 17:33:05 +02006477 (curr_sess->be->cap & PR_CAP_BE) ? curr_sess->be->id : "<NONE>",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006478 objt_server(curr_sess->target) ? objt_server(curr_sess->target)->id : "<none>"
Emeric Brun837ca522010-10-22 16:19:01 +02006479 );
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006480 break;
6481 }
6482
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006483 chunk_appendf(&trash,
Willy Tarreau65671ab2009-10-04 14:24:59 +02006484 " ts=%02x age=%s calls=%d",
6485 curr_sess->task->state,
Willy Tarreau3884cba2009-03-28 17:54:35 +01006486 human_time(now.tv_sec - curr_sess->logs.tv_accept.tv_sec, 1),
6487 curr_sess->task->calls);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006488
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006489 chunk_appendf(&trash,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01006490 " rq[f=%06xh,i=%d,an=%02xh,rx=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006491 curr_sess->req.flags,
6492 curr_sess->req.buf->i,
6493 curr_sess->req.analysers,
6494 curr_sess->req.rex ?
6495 human_time(TICKS_TO_MS(curr_sess->req.rex - now_ms),
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006496 TICKS_TO_MS(1000)) : "");
6497
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006498 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006499 ",wx=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006500 curr_sess->req.wex ?
6501 human_time(TICKS_TO_MS(curr_sess->req.wex - now_ms),
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006502 TICKS_TO_MS(1000)) : "");
6503
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006504 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006505 ",ax=%s]",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006506 curr_sess->req.analyse_exp ?
6507 human_time(TICKS_TO_MS(curr_sess->req.analyse_exp - now_ms),
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006508 TICKS_TO_MS(1000)) : "");
6509
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006510 chunk_appendf(&trash,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01006511 " rp[f=%06xh,i=%d,an=%02xh,rx=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006512 curr_sess->res.flags,
6513 curr_sess->res.buf->i,
6514 curr_sess->res.analysers,
6515 curr_sess->res.rex ?
6516 human_time(TICKS_TO_MS(curr_sess->res.rex - now_ms),
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006517 TICKS_TO_MS(1000)) : "");
6518
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006519 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006520 ",wx=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006521 curr_sess->res.wex ?
6522 human_time(TICKS_TO_MS(curr_sess->res.wex - now_ms),
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006523 TICKS_TO_MS(1000)) : "");
6524
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006525 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006526 ",ax=%s]",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006527 curr_sess->res.analyse_exp ?
6528 human_time(TICKS_TO_MS(curr_sess->res.analyse_exp - now_ms),
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006529 TICKS_TO_MS(1000)) : "");
6530
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006531 conn = objt_conn(curr_sess->si[0].end);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006532 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006533 " s0=[%d,%1xh,fd=%d,ex=%s]",
6534 curr_sess->si[0].state,
6535 curr_sess->si[0].flags,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006536 conn ? conn->t.sock.fd : -1,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006537 curr_sess->si[0].exp ?
6538 human_time(TICKS_TO_MS(curr_sess->si[0].exp - now_ms),
6539 TICKS_TO_MS(1000)) : "");
6540
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006541 conn = objt_conn(curr_sess->si[1].end);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006542 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006543 " s1=[%d,%1xh,fd=%d,ex=%s]",
6544 curr_sess->si[1].state,
6545 curr_sess->si[1].flags,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006546 conn ? conn->t.sock.fd : -1,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006547 curr_sess->si[1].exp ?
6548 human_time(TICKS_TO_MS(curr_sess->si[1].exp - now_ms),
6549 TICKS_TO_MS(1000)) : "");
6550
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006551 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006552 " exp=%s",
6553 curr_sess->task->expire ?
6554 human_time(TICKS_TO_MS(curr_sess->task->expire - now_ms),
6555 TICKS_TO_MS(1000)) : "");
Willy Tarreau4726f532009-03-07 17:25:21 +01006556 if (task_in_rq(curr_sess->task))
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006557 chunk_appendf(&trash, " run(nice=%d)", curr_sess->task->nice);
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006558
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006559 chunk_appendf(&trash, "\n");
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006560
Willy Tarreau2bb4a962014-11-28 11:11:05 +01006561 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau87b09662015-04-03 00:22:06 +02006562 /* let's try again later from this stream. We add ourselves into
6563 * this stream's users so that it can remove us upon termination.
Willy Tarreaufd3828e2009-02-22 15:17:24 +01006564 */
Willy Tarreaufe127932015-04-21 19:23:39 +02006565 si_applet_cant_put(si);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006566 LIST_ADDQ(&curr_sess->back_refs, &appctx->ctx.sess.bref.users);
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02006567 return 0;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006568 }
6569
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006570 next_sess:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006571 appctx->ctx.sess.bref.ref = curr_sess->list.n;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006572 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006573
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006574 if (appctx->ctx.sess.target && appctx->ctx.sess.target != (void *)-1) {
Willy Tarreau87b09662015-04-03 00:22:06 +02006575 /* specified stream not found */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006576 if (appctx->ctx.sess.section > 0)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006577 chunk_appendf(&trash, " *** session terminated while we were watching it ***\n");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006578 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006579 chunk_appendf(&trash, "Session not found.\n");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006580
Willy Tarreaubc18da12015-03-13 14:00:47 +01006581 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02006582 si_applet_cant_put(si);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006583 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01006584 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006585
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006586 appctx->ctx.sess.target = NULL;
6587 appctx->ctx.sess.uid = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006588 return 1;
6589 }
6590
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006591 appctx->st2 = STAT_ST_FIN;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006592 /* fall through */
6593
6594 default:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006595 appctx->st2 = STAT_ST_FIN;
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02006596 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006597 }
Emeric Brun1e029aa2010-09-23 18:12:53 +02006598}
6599
Willy Tarreau5f9a8772012-11-26 02:22:40 +01006600/* This is called when the stream interface is closed. For instance, upon an
6601 * external abort, we won't call the i/o handler anymore so we may need to
Willy Tarreau87b09662015-04-03 00:22:06 +02006602 * remove back references to the stream currently being dumped.
Willy Tarreau5f9a8772012-11-26 02:22:40 +01006603 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02006604static void cli_release_handler(struct appctx *appctx)
Willy Tarreau5f9a8772012-11-26 02:22:40 +01006605{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006606 if (appctx->st0 == STAT_CLI_O_SESS && appctx->st2 == STAT_ST_LIST) {
6607 if (!LIST_ISEMPTY(&appctx->ctx.sess.bref.users))
6608 LIST_DEL(&appctx->ctx.sess.bref.users);
Willy Tarreau5f9a8772012-11-26 02:22:40 +01006609 }
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01006610 else if (appctx->st0 == STAT_CLI_PRINT_FREE) {
6611 free(appctx->ctx.cli.err);
Willy Tarreau6457d0f2015-10-07 20:00:24 +02006612 appctx->ctx.cli.err = NULL;
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01006613 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006614 else if (appctx->st0 == STAT_CLI_O_MLOOK) {
6615 free(appctx->ctx.map.chunk.str);
Willy Tarreau6457d0f2015-10-07 20:00:24 +02006616 appctx->ctx.map.chunk.str = NULL;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006617 }
Willy Tarreau5f9a8772012-11-26 02:22:40 +01006618}
6619
Willy Tarreau20e99322013-04-13 09:22:25 +02006620/* This function is used to either dump tables states (when action is set
6621 * to STAT_CLI_O_TAB) or clear tables (when action is STAT_CLI_O_CLR).
Willy Tarreau20e99322013-04-13 09:22:25 +02006622 * It returns 0 if the output buffer is full and it needs to be called
6623 * again, otherwise non-zero.
Willy Tarreau69f58c82010-07-12 17:55:33 +02006624 */
Willy Tarreau20e99322013-04-13 09:22:25 +02006625static int stats_table_request(struct stream_interface *si, int action)
Willy Tarreau69f58c82010-07-12 17:55:33 +02006626{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006627 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau87b09662015-04-03 00:22:06 +02006628 struct stream *s = si_strm(si);
Willy Tarreau69f58c82010-07-12 17:55:33 +02006629 struct ebmb_node *eb;
6630 int dt;
Willy Tarreau44455022012-12-05 23:01:12 +01006631 int skip_entry;
Willy Tarreau20e99322013-04-13 09:22:25 +02006632 int show = action == STAT_CLI_O_TAB;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006633
6634 /*
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006635 * We have 3 possible states in appctx->st2 :
Willy Tarreau295a8372011-03-10 11:25:07 +01006636 * - STAT_ST_INIT : the first call
6637 * - STAT_ST_INFO : the proxy pointer points to the next table to
Willy Tarreau69f58c82010-07-12 17:55:33 +02006638 * dump, the entry pointer is NULL ;
Willy Tarreau295a8372011-03-10 11:25:07 +01006639 * - STAT_ST_LIST : the proxy pointer points to the current table
Willy Tarreau69f58c82010-07-12 17:55:33 +02006640 * and the entry pointer points to the next entry to be dumped,
6641 * and the refcount on the next entry is held ;
Willy Tarreau295a8372011-03-10 11:25:07 +01006642 * - STAT_ST_END : nothing left to dump, the buffer may contain some
Willy Tarreau69f58c82010-07-12 17:55:33 +02006643 * data though.
6644 */
6645
Willy Tarreau2bb4a962014-11-28 11:11:05 +01006646 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW))) {
Willy Tarreau69f58c82010-07-12 17:55:33 +02006647 /* in case of abort, remove any refcount we might have set on an entry */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006648 if (appctx->st2 == STAT_ST_LIST) {
6649 appctx->ctx.table.entry->ref_cnt--;
6650 stksess_kill_if_expired(&appctx->ctx.table.proxy->table, appctx->ctx.table.entry);
Willy Tarreauf6efda12010-08-03 20:34:06 +02006651 }
Willy Tarreau69f58c82010-07-12 17:55:33 +02006652 return 1;
6653 }
6654
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006655 chunk_reset(&trash);
Willy Tarreau69f58c82010-07-12 17:55:33 +02006656
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006657 while (appctx->st2 != STAT_ST_FIN) {
6658 switch (appctx->st2) {
Willy Tarreau295a8372011-03-10 11:25:07 +01006659 case STAT_ST_INIT:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006660 appctx->ctx.table.proxy = appctx->ctx.table.target;
6661 if (!appctx->ctx.table.proxy)
6662 appctx->ctx.table.proxy = proxy;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006663
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006664 appctx->ctx.table.entry = NULL;
6665 appctx->st2 = STAT_ST_INFO;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006666 break;
6667
Willy Tarreau295a8372011-03-10 11:25:07 +01006668 case STAT_ST_INFO:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006669 if (!appctx->ctx.table.proxy ||
6670 (appctx->ctx.table.target &&
6671 appctx->ctx.table.proxy != appctx->ctx.table.target)) {
6672 appctx->st2 = STAT_ST_END;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006673 break;
6674 }
6675
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006676 if (appctx->ctx.table.proxy->table.size) {
6677 if (show && !stats_dump_table_head_to_buffer(&trash, si, appctx->ctx.table.proxy,
6678 appctx->ctx.table.target))
Willy Tarreau69f58c82010-07-12 17:55:33 +02006679 return 0;
6680
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006681 if (appctx->ctx.table.target &&
Willy Tarreaud0d8da92015-04-04 02:10:38 +02006682 strm_li(s)->bind_conf->level >= ACCESS_LVL_OPER) {
Willy Tarreau69f58c82010-07-12 17:55:33 +02006683 /* dump entries only if table explicitly requested */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006684 eb = ebmb_first(&appctx->ctx.table.proxy->table.keys);
Willy Tarreau69f58c82010-07-12 17:55:33 +02006685 if (eb) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006686 appctx->ctx.table.entry = ebmb_entry(eb, struct stksess, key);
6687 appctx->ctx.table.entry->ref_cnt++;
6688 appctx->st2 = STAT_ST_LIST;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006689 break;
6690 }
6691 }
6692 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006693 appctx->ctx.table.proxy = appctx->ctx.table.proxy->next;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006694 break;
6695
Willy Tarreau295a8372011-03-10 11:25:07 +01006696 case STAT_ST_LIST:
Willy Tarreau44455022012-12-05 23:01:12 +01006697 skip_entry = 0;
Simon Hormanc88b8872011-06-15 15:18:49 +09006698
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006699 if (appctx->ctx.table.data_type >= 0) {
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02006700 /* we're filtering on some data contents */
6701 void *ptr;
6702 long long data;
6703
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006704 dt = appctx->ctx.table.data_type;
6705 ptr = stktable_data_ptr(&appctx->ctx.table.proxy->table,
6706 appctx->ctx.table.entry,
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02006707 dt);
6708
6709 data = 0;
6710 switch (stktable_data_types[dt].std_type) {
6711 case STD_T_SINT:
6712 data = stktable_data_cast(ptr, std_t_sint);
6713 break;
6714 case STD_T_UINT:
6715 data = stktable_data_cast(ptr, std_t_uint);
6716 break;
6717 case STD_T_ULL:
6718 data = stktable_data_cast(ptr, std_t_ull);
6719 break;
6720 case STD_T_FRQP:
6721 data = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006722 appctx->ctx.table.proxy->table.data_arg[dt].u);
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02006723 break;
6724 }
6725
6726 /* skip the entry if the data does not match the test and the value */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006727 if ((data < appctx->ctx.table.value &&
6728 (appctx->ctx.table.data_op == STD_OP_EQ ||
6729 appctx->ctx.table.data_op == STD_OP_GT ||
6730 appctx->ctx.table.data_op == STD_OP_GE)) ||
6731 (data == appctx->ctx.table.value &&
6732 (appctx->ctx.table.data_op == STD_OP_NE ||
6733 appctx->ctx.table.data_op == STD_OP_GT ||
6734 appctx->ctx.table.data_op == STD_OP_LT)) ||
6735 (data > appctx->ctx.table.value &&
6736 (appctx->ctx.table.data_op == STD_OP_EQ ||
6737 appctx->ctx.table.data_op == STD_OP_LT ||
6738 appctx->ctx.table.data_op == STD_OP_LE)))
Willy Tarreau44455022012-12-05 23:01:12 +01006739 skip_entry = 1;
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02006740 }
6741
Simon Hormanc88b8872011-06-15 15:18:49 +09006742 if (show && !skip_entry &&
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006743 !stats_dump_table_entry_to_buffer(&trash, si, appctx->ctx.table.proxy,
6744 appctx->ctx.table.entry))
Simon Hormand9366582011-06-15 15:18:45 +09006745 return 0;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006746
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006747 appctx->ctx.table.entry->ref_cnt--;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006748
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006749 eb = ebmb_next(&appctx->ctx.table.entry->key);
Willy Tarreau69f58c82010-07-12 17:55:33 +02006750 if (eb) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006751 struct stksess *old = appctx->ctx.table.entry;
6752 appctx->ctx.table.entry = ebmb_entry(eb, struct stksess, key);
Willy Tarreau8fa52f42012-01-09 11:50:03 +01006753 if (show)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006754 stksess_kill_if_expired(&appctx->ctx.table.proxy->table, old);
6755 else if (!skip_entry && !appctx->ctx.table.entry->ref_cnt)
6756 stksess_kill(&appctx->ctx.table.proxy->table, old);
6757 appctx->ctx.table.entry->ref_cnt++;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006758 break;
6759 }
6760
Simon Hormanc88b8872011-06-15 15:18:49 +09006761
6762 if (show)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006763 stksess_kill_if_expired(&appctx->ctx.table.proxy->table, appctx->ctx.table.entry);
6764 else if (!skip_entry && !appctx->ctx.table.entry->ref_cnt)
6765 stksess_kill(&appctx->ctx.table.proxy->table, appctx->ctx.table.entry);
Simon Hormanc88b8872011-06-15 15:18:49 +09006766
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006767 appctx->ctx.table.proxy = appctx->ctx.table.proxy->next;
6768 appctx->st2 = STAT_ST_INFO;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006769 break;
6770
Willy Tarreau295a8372011-03-10 11:25:07 +01006771 case STAT_ST_END:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006772 appctx->st2 = STAT_ST_FIN;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006773 break;
6774 }
6775 }
6776 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006777}
6778
Willy Tarreaud426a182010-03-05 14:58:26 +01006779/* print a line of text buffer (limited to 70 bytes) to <out>. The format is :
Willy Tarreau74808cb2009-03-04 15:53:18 +01006780 * <2 spaces> <offset=5 digits> <space or plus> <space> <70 chars max> <\n>
6781 * which is 60 chars per line. Non-printable chars \t, \n, \r and \e are
6782 * encoded in C format. Other non-printable chars are encoded "\xHH". Original
6783 * lines are respected within the limit of 70 output chars. Lines that are
6784 * continuation of a previous truncated line begin with "+" instead of " "
6785 * after the offset. The new pointer is returned.
6786 */
Willy Tarreaud426a182010-03-05 14:58:26 +01006787static int dump_text_line(struct chunk *out, const char *buf, int bsize, int len,
6788 int *line, int ptr)
Willy Tarreau74808cb2009-03-04 15:53:18 +01006789{
6790 int end;
6791 unsigned char c;
6792
6793 end = out->len + 80;
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02006794 if (end > out->size)
Willy Tarreau74808cb2009-03-04 15:53:18 +01006795 return ptr;
6796
Willy Tarreau77804732012-10-29 16:14:26 +01006797 chunk_appendf(out, " %05d%c ", ptr, (ptr == *line) ? ' ' : '+');
Willy Tarreau74808cb2009-03-04 15:53:18 +01006798
Willy Tarreaud426a182010-03-05 14:58:26 +01006799 while (ptr < len && ptr < bsize) {
6800 c = buf[ptr];
Willy Tarreau787bbd92009-03-12 08:18:33 +01006801 if (isprint(c) && isascii(c) && c != '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006802 if (out->len > end - 2)
6803 break;
6804 out->str[out->len++] = c;
Willy Tarreau787bbd92009-03-12 08:18:33 +01006805 } else if (c == '\t' || c == '\n' || c == '\r' || c == '\e' || c == '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006806 if (out->len > end - 3)
6807 break;
6808 out->str[out->len++] = '\\';
6809 switch (c) {
6810 case '\t': c = 't'; break;
6811 case '\n': c = 'n'; break;
6812 case '\r': c = 'r'; break;
6813 case '\e': c = 'e'; break;
Willy Tarreau787bbd92009-03-12 08:18:33 +01006814 case '\\': c = '\\'; break;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006815 }
6816 out->str[out->len++] = c;
6817 } else {
6818 if (out->len > end - 5)
6819 break;
6820 out->str[out->len++] = '\\';
6821 out->str[out->len++] = 'x';
6822 out->str[out->len++] = hextab[(c >> 4) & 0xF];
6823 out->str[out->len++] = hextab[c & 0xF];
6824 }
Willy Tarreaud426a182010-03-05 14:58:26 +01006825 if (buf[ptr++] == '\n') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006826 /* we had a line break, let's return now */
6827 out->str[out->len++] = '\n';
6828 *line = ptr;
6829 return ptr;
6830 }
6831 }
6832 /* we have an incomplete line, we return it as-is */
6833 out->str[out->len++] = '\n';
6834 return ptr;
6835}
6836
Baptiste Assmann3863f972015-05-17 00:33:24 +02006837/* This function dumps counters from all resolvers section and associated name servers.
6838 * It returns 0 if the output buffer is full and it needs
6839 * to be called again, otherwise non-zero.
6840 */
6841static int stats_dump_resolvers_to_buffer(struct stream_interface *si)
6842{
6843 struct appctx *appctx = __objt_appctx(si->end);
6844 struct dns_resolvers *presolvers;
6845 struct dns_nameserver *pnameserver;
6846
6847 chunk_reset(&trash);
6848
6849 switch (appctx->st2) {
6850 case STAT_ST_INIT:
6851 appctx->st2 = STAT_ST_LIST; /* let's start producing data */
6852 /* fall through */
6853
6854 case STAT_ST_LIST:
Andrew Hayworth68d05342015-10-02 20:33:01 +00006855 if (LIST_ISEMPTY(&dns_resolvers)) {
6856 chunk_appendf(&trash, "No resolvers found\n");
6857 }
6858 else {
6859 list_for_each_entry(presolvers, &dns_resolvers, list) {
6860 if (appctx->ctx.resolvers.ptr != NULL && appctx->ctx.resolvers.ptr != presolvers)
6861 continue;
6862
6863 chunk_appendf(&trash, "Resolvers section %s\n", presolvers->id);
6864 list_for_each_entry(pnameserver, &presolvers->nameserver_list, list) {
6865 chunk_appendf(&trash, " nameserver %s:\n", pnameserver->id);
6866 chunk_appendf(&trash, " sent: %ld\n", pnameserver->counters.sent);
6867 chunk_appendf(&trash, " valid: %ld\n", pnameserver->counters.valid);
6868 chunk_appendf(&trash, " update: %ld\n", pnameserver->counters.update);
6869 chunk_appendf(&trash, " cname: %ld\n", pnameserver->counters.cname);
6870 chunk_appendf(&trash, " cname_error: %ld\n", pnameserver->counters.cname_error);
6871 chunk_appendf(&trash, " any_err: %ld\n", pnameserver->counters.any_err);
6872 chunk_appendf(&trash, " nx: %ld\n", pnameserver->counters.nx);
6873 chunk_appendf(&trash, " timeout: %ld\n", pnameserver->counters.timeout);
6874 chunk_appendf(&trash, " refused: %ld\n", pnameserver->counters.refused);
6875 chunk_appendf(&trash, " other: %ld\n", pnameserver->counters.other);
6876 chunk_appendf(&trash, " invalid: %ld\n", pnameserver->counters.invalid);
6877 chunk_appendf(&trash, " too_big: %ld\n", pnameserver->counters.too_big);
6878 chunk_appendf(&trash, " truncated: %ld\n", pnameserver->counters.truncated);
6879 chunk_appendf(&trash, " outdated: %ld\n", pnameserver->counters.outdated);
6880 }
6881 }
Baptiste Assmann3863f972015-05-17 00:33:24 +02006882 }
6883
6884 /* display response */
6885 if (bi_putchk(si_ic(si), &trash) == -1) {
6886 /* let's try again later from this session. We add ourselves into
6887 * this session's users so that it can remove us upon termination.
6888 */
6889 si->flags |= SI_FL_WAIT_ROOM;
6890 return 0;
6891 }
6892
6893 appctx->st2 = STAT_ST_FIN;
6894 /* fall through */
6895
6896 default:
6897 appctx->st2 = STAT_ST_FIN;
6898 return 1;
6899 }
6900}
6901
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02006902/* This function dumps all captured errors onto the stream interface's
Willy Tarreau306f8302013-07-08 15:53:06 +02006903 * read buffer. It returns 0 if the output buffer is full and it needs
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01006904 * to be called again, otherwise non-zero.
Willy Tarreau74808cb2009-03-04 15:53:18 +01006905 */
Simon Horman9bd2c732011-06-15 15:18:44 +09006906static int stats_dump_errors_to_buffer(struct stream_interface *si)
Willy Tarreau74808cb2009-03-04 15:53:18 +01006907{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006908 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau74808cb2009-03-04 15:53:18 +01006909 extern const char *monthname[12];
Willy Tarreau74808cb2009-03-04 15:53:18 +01006910
Willy Tarreau2bb4a962014-11-28 11:11:05 +01006911 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
Willy Tarreau61b34732009-10-03 23:49:35 +02006912 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006913
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006914 chunk_reset(&trash);
Willy Tarreau74808cb2009-03-04 15:53:18 +01006915
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006916 if (!appctx->ctx.errors.px) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006917 /* the function had not been called yet, let's prepare the
6918 * buffer for a response.
6919 */
Willy Tarreau10479e42010-12-12 14:00:34 +01006920 struct tm tm;
6921
6922 get_localtime(date.tv_sec, &tm);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006923 chunk_appendf(&trash, "Total events captured on [%02d/%s/%04d:%02d:%02d:%02d.%03d] : %u\n",
Willy Tarreau10479e42010-12-12 14:00:34 +01006924 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
6925 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(date.tv_usec/1000),
6926 error_snapshot_id);
6927
Willy Tarreau2bb4a962014-11-28 11:11:05 +01006928 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau10479e42010-12-12 14:00:34 +01006929 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreaufe127932015-04-21 19:23:39 +02006930 si_applet_cant_put(si);
Willy Tarreau10479e42010-12-12 14:00:34 +01006931 return 0;
6932 }
6933
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006934 appctx->ctx.errors.px = proxy;
6935 appctx->ctx.errors.buf = 0;
6936 appctx->ctx.errors.bol = 0;
6937 appctx->ctx.errors.ptr = -1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006938 }
6939
6940 /* we have two inner loops here, one for the proxy, the other one for
6941 * the buffer.
6942 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006943 while (appctx->ctx.errors.px) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006944 struct error_snapshot *es;
6945
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006946 if (appctx->ctx.errors.buf == 0)
6947 es = &appctx->ctx.errors.px->invalid_req;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006948 else
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006949 es = &appctx->ctx.errors.px->invalid_rep;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006950
6951 if (!es->when.tv_sec)
6952 goto next;
6953
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006954 if (appctx->ctx.errors.iid >= 0 &&
6955 appctx->ctx.errors.px->uuid != appctx->ctx.errors.iid &&
6956 es->oe->uuid != appctx->ctx.errors.iid)
Willy Tarreau74808cb2009-03-04 15:53:18 +01006957 goto next;
6958
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006959 if (appctx->ctx.errors.ptr < 0) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006960 /* just print headers now */
6961
6962 char pn[INET6_ADDRSTRLEN];
6963 struct tm tm;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02006964 int port;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006965
6966 get_localtime(es->when.tv_sec, &tm);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006967 chunk_appendf(&trash, " \n[%02d/%s/%04d:%02d:%02d:%02d.%03d]",
Willy Tarreau74808cb2009-03-04 15:53:18 +01006968 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
Willy Tarreau1772ece2009-04-03 14:49:12 +02006969 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(es->when.tv_usec/1000));
Willy Tarreau74808cb2009-03-04 15:53:18 +01006970
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02006971 switch (addr_to_str(&es->src, pn, sizeof(pn))) {
6972 case AF_INET:
6973 case AF_INET6:
6974 port = get_host_port(&es->src);
6975 break;
6976 default:
6977 port = 0;
6978 }
6979
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006980 switch (appctx->ctx.errors.buf) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006981 case 0:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006982 chunk_appendf(&trash,
Willy Tarreau74808cb2009-03-04 15:53:18 +01006983 " frontend %s (#%d): invalid request\n"
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02006984 " backend %s (#%d)",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006985 appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02006986 (es->oe->cap & PR_CAP_BE) ? es->oe->id : "<NONE>",
6987 (es->oe->cap & PR_CAP_BE) ? es->oe->uuid : -1);
Willy Tarreau74808cb2009-03-04 15:53:18 +01006988 break;
6989 case 1:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006990 chunk_appendf(&trash,
Olivier Doucet08afdcb2014-09-08 11:23:00 +02006991 " backend %s (#%d): invalid response\n"
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02006992 " frontend %s (#%d)",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006993 appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02006994 es->oe->id, es->oe->uuid);
Willy Tarreau74808cb2009-03-04 15:53:18 +01006995 break;
6996 }
6997
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006998 chunk_appendf(&trash,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02006999 ", server %s (#%d), event #%u\n"
7000 " src %s:%d, session #%d, session flags 0x%08x\n"
7001 " HTTP msg state %d, msg flags 0x%08x, tx flags 0x%08x\n"
7002 " HTTP chunk len %lld bytes, HTTP body len %lld bytes\n"
7003 " buffer flags 0x%08x, out %d bytes, total %lld bytes\n"
7004 " pending %d bytes, wrapping at %d, error at position %d:\n \n",
7005 es->srv ? es->srv->id : "<NONE>", es->srv ? es->srv->puid : -1,
7006 es->ev_id,
7007 pn, port, es->sid, es->s_flags,
7008 es->state, es->m_flags, es->t_flags,
7009 es->m_clen, es->m_blen,
7010 es->b_flags, es->b_out, es->b_tot,
7011 es->len, es->b_wrap, es->pos);
7012
Willy Tarreau2bb4a962014-11-28 11:11:05 +01007013 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01007014 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreaufe127932015-04-21 19:23:39 +02007015 si_applet_cant_put(si);
Willy Tarreau61b34732009-10-03 23:49:35 +02007016 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01007017 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01007018 appctx->ctx.errors.ptr = 0;
7019 appctx->ctx.errors.sid = es->sid;
Willy Tarreau74808cb2009-03-04 15:53:18 +01007020 }
7021
Willy Tarreau7b4b4992013-12-01 09:15:12 +01007022 if (appctx->ctx.errors.sid != es->sid) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01007023 /* the snapshot changed while we were dumping it */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007024 chunk_appendf(&trash,
Willy Tarreau74808cb2009-03-04 15:53:18 +01007025 " WARNING! update detected on this snapshot, dump interrupted. Please re-check!\n");
Willy Tarreaubc18da12015-03-13 14:00:47 +01007026 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02007027 si_applet_cant_put(si);
Willy Tarreau61b34732009-10-03 23:49:35 +02007028 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01007029 }
Willy Tarreau74808cb2009-03-04 15:53:18 +01007030 goto next;
7031 }
7032
7033 /* OK, ptr >= 0, so we have to dump the current line */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01007034 while (appctx->ctx.errors.ptr < es->len && appctx->ctx.errors.ptr < sizeof(es->buf)) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01007035 int newptr;
7036 int newline;
7037
Willy Tarreau7b4b4992013-12-01 09:15:12 +01007038 newline = appctx->ctx.errors.bol;
7039 newptr = dump_text_line(&trash, es->buf, sizeof(es->buf), es->len, &newline, appctx->ctx.errors.ptr);
7040 if (newptr == appctx->ctx.errors.ptr)
Willy Tarreau61b34732009-10-03 23:49:35 +02007041 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01007042
Willy Tarreau2bb4a962014-11-28 11:11:05 +01007043 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01007044 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreaufe127932015-04-21 19:23:39 +02007045 si_applet_cant_put(si);
Willy Tarreau61b34732009-10-03 23:49:35 +02007046 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01007047 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01007048 appctx->ctx.errors.ptr = newptr;
7049 appctx->ctx.errors.bol = newline;
Willy Tarreau74808cb2009-03-04 15:53:18 +01007050 };
7051 next:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01007052 appctx->ctx.errors.bol = 0;
7053 appctx->ctx.errors.ptr = -1;
7054 appctx->ctx.errors.buf++;
7055 if (appctx->ctx.errors.buf > 1) {
7056 appctx->ctx.errors.buf = 0;
7057 appctx->ctx.errors.px = appctx->ctx.errors.px->next;
Willy Tarreau74808cb2009-03-04 15:53:18 +01007058 }
Willy Tarreauae795722016-02-16 11:27:28 +01007059 }
7060
7061 /* dump complete */
7062 return 1;
7063}
7064
7065/* This function dumps all environmnent variables to the buffer. It returns 0
7066 * if the output buffer is full and it needs to be called again, otherwise
7067 * non-zero. Dumps only one entry if st2 == STAT_ST_END.
7068 */
7069static int stats_dump_env_to_buffer(struct stream_interface *si)
7070{
7071 struct appctx *appctx = __objt_appctx(si->end);
7072
7073 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
7074 return 1;
7075
7076 chunk_reset(&trash);
7077
7078 /* we have two inner loops here, one for the proxy, the other one for
7079 * the buffer.
7080 */
7081 while (*appctx->ctx.env.var) {
7082 chunk_printf(&trash, "%s\n", *appctx->ctx.env.var);
7083
7084 if (bi_putchk(si_ic(si), &trash) == -1) {
7085 si_applet_cant_put(si);
7086 return 0;
7087 }
7088 if (appctx->st2 == STAT_ST_END)
7089 break;
7090 appctx->ctx.env.var++;
Willy Tarreau74808cb2009-03-04 15:53:18 +01007091 }
7092
7093 /* dump complete */
Willy Tarreau61b34732009-10-03 23:49:35 +02007094 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01007095}
7096
Willy Tarreaud5781202012-09-22 19:32:35 +02007097/* parse the "level" argument on the bind lines */
7098static int bind_parse_level(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
7099{
7100 if (!*args[cur_arg + 1]) {
7101 memprintf(err, "'%s' : missing level", args[cur_arg]);
7102 return ERR_ALERT | ERR_FATAL;
7103 }
7104
7105 if (!strcmp(args[cur_arg+1], "user"))
7106 conf->level = ACCESS_LVL_USER;
7107 else if (!strcmp(args[cur_arg+1], "operator"))
7108 conf->level = ACCESS_LVL_OPER;
7109 else if (!strcmp(args[cur_arg+1], "admin"))
7110 conf->level = ACCESS_LVL_ADMIN;
7111 else {
7112 memprintf(err, "'%s' only supports 'user', 'operator', and 'admin' (got '%s')",
7113 args[cur_arg], args[cur_arg+1]);
7114 return ERR_ALERT | ERR_FATAL;
7115 }
7116
7117 return 0;
7118}
7119
Willy Tarreau30576452015-04-13 13:50:30 +02007120struct applet http_stats_applet = {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007121 .obj_type = OBJ_TYPE_APPLET,
Willy Tarreaub24281b2011-02-13 13:16:36 +01007122 .name = "<STATS>", /* used for logging */
7123 .fct = http_stats_io_handler,
Aman Gupta9a13e842012-04-02 18:57:53 -07007124 .release = NULL,
Willy Tarreaub24281b2011-02-13 13:16:36 +01007125};
7126
Willy Tarreau30576452015-04-13 13:50:30 +02007127static struct applet cli_applet = {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007128 .obj_type = OBJ_TYPE_APPLET,
Willy Tarreaub24281b2011-02-13 13:16:36 +01007129 .name = "<CLI>", /* used for logging */
7130 .fct = cli_io_handler,
Willy Tarreau5f9a8772012-11-26 02:22:40 +01007131 .release = cli_release_handler,
Willy Tarreaub24281b2011-02-13 13:16:36 +01007132};
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01007133
Willy Tarreaudc13c112013-06-21 23:16:39 +02007134static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreau10522fd2008-07-09 20:12:41 +02007135 { CFG_GLOBAL, "stats", stats_parse_global },
7136 { 0, NULL, NULL },
7137}};
7138
Willy Tarreaud5781202012-09-22 19:32:35 +02007139static struct bind_kw_list bind_kws = { "STAT", { }, {
7140 { "level", bind_parse_level, 1 }, /* set the unix socket admin level */
7141 { NULL, NULL, 0 },
7142}};
7143
Willy Tarreau10522fd2008-07-09 20:12:41 +02007144__attribute__((constructor))
7145static void __dumpstats_module_init(void)
7146{
7147 cfg_register_keywords(&cfg_kws);
Willy Tarreaud5781202012-09-22 19:32:35 +02007148 bind_register_keywords(&bind_kws);
Willy Tarreau10522fd2008-07-09 20:12:41 +02007149}
7150
Willy Tarreau91861262007-10-17 17:06:05 +02007151/*
7152 * Local variables:
7153 * c-indent-level: 8
7154 * c-basic-offset: 8
7155 * End:
7156 */