blob: f7e759743f9f586a9d1db10a62fd102864a97117 [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 */
2986static int stats_emit_raw_data_field(struct chunk *out, const struct field *f)
2987{
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 Tarreaubf95cba2016-01-11 18:27:29 +01002999/* Dump all fields from <info> into <out> using the "show info" format (name: value) */
3000static int stats_dump_info_fields(struct chunk *out, const struct field *info)
3001{
3002 int field;
3003
3004 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
3005 if (!field_format(info, field))
3006 continue;
3007
3008 if (!chunk_appendf(out, "%s: ", info_field_names[field]))
3009 return 0;
3010 if (!stats_emit_raw_data_field(out, &info[field]))
3011 return 0;
3012 if (!chunk_strcat(out, "\n"))
3013 return 0;
3014 }
3015 return 1;
3016}
3017
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003018/* This function dumps information onto the stream interface's read buffer.
3019 * It returns 0 as long as it does not complete, non-zero upon completion.
3020 * No state is used.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003021 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003022static int stats_dump_info_to_buffer(struct stream_interface *si)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003023{
3024 unsigned int up = (now.tv_sec - start_date.tv_sec);
Willy Tarreau4529c072016-01-11 18:17:07 +01003025 struct chunk *out = get_trash_chunk();
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003026
Willy Tarreau0c9c2722014-05-28 12:28:58 +02003027#ifdef USE_OPENSSL
3028 int ssl_sess_rate = read_freq_ctr(&global.ssl_per_sec);
3029 int ssl_key_rate = read_freq_ctr(&global.ssl_fe_keys_per_sec);
3030 int ssl_reuse = 0;
3031
3032 if (ssl_key_rate < ssl_sess_rate) {
3033 /* count the ssl reuse ratio and avoid overflows in both directions */
3034 ssl_reuse = 100 - (100 * ssl_key_rate + (ssl_sess_rate - 1) / 2) / ssl_sess_rate;
3035 }
3036#endif
3037
Willy Tarreau4529c072016-01-11 18:17:07 +01003038 chunk_reset(out);
3039 memset(&info, 0, sizeof(info));
3040
3041 info[INF_NAME] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, PRODUCT_NAME);
3042 info[INF_VERSION] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, HAPROXY_VERSION);
3043 info[INF_RELEASE_DATE] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, HAPROXY_DATE);
3044
3045 info[INF_NBPROC] = mkf_u32(FO_CONFIG|FS_SERVICE, global.nbproc);
3046 info[INF_PROCESS_NUM] = mkf_u32(FO_KEY, relative_pid);
3047 info[INF_PID] = mkf_u32(FO_STATUS, pid);
3048
3049 info[INF_UPTIME] = mkf_str(FN_DURATION, chunk_newstr(out));
3050 chunk_appendf(out, "%ud %uh%02um%02us", up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60));
3051
3052 info[INF_UPTIME_SEC] = mkf_u32(FN_DURATION, up);
3053 info[INF_MEMMAX_MB] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_memmax);
3054 info[INF_POOL_ALLOC_MB] = mkf_u32(0, (unsigned)(pool_total_allocated() / 1048576L));
3055 info[INF_POOL_USED_MB] = mkf_u32(0, (unsigned)(pool_total_used() / 1048576L));
3056 info[INF_POOL_FAILED] = mkf_u32(FN_COUNTER, pool_total_failures());
3057 info[INF_ULIMIT_N] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_nofile);
3058 info[INF_MAXSOCK] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxsock);
3059 info[INF_MAXCONN] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxconn);
3060 info[INF_HARD_MAXCONN] = mkf_u32(FO_CONFIG|FN_LIMIT, global.hardmaxconn);
3061 info[INF_CURR_CONN] = mkf_u32(0, actconn);
3062 info[INF_CUM_CONN] = mkf_u32(FN_COUNTER, totalconn);
3063 info[INF_CUM_REQ] = mkf_u32(FN_COUNTER, global.req_count);
3064#ifdef USE_OPENSSL
3065 info[INF_MAX_SSL_CONNS] = mkf_u32(FN_MAX, global.maxsslconn);
3066 info[INF_CURR_SSL_CONNS] = mkf_u32(0, sslconns);
3067 info[INF_CUM_SSL_CONNS] = mkf_u32(FN_COUNTER, totalsslconns);
3068#endif
3069 info[INF_MAXPIPES] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxpipes);
3070 info[INF_PIPES_USED] = mkf_u32(0, pipes_used);
3071 info[INF_PIPES_FREE] = mkf_u32(0, pipes_free);
3072 info[INF_CONN_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.conn_per_sec));
3073 info[INF_CONN_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.cps_lim);
3074 info[INF_MAX_CONN_RATE] = mkf_u32(FN_MAX, global.cps_max);
3075 info[INF_SESS_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.sess_per_sec));
3076 info[INF_SESS_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.sps_lim);
3077 info[INF_MAX_SESS_RATE] = mkf_u32(FN_RATE, global.sps_max);
3078
3079#ifdef USE_OPENSSL
3080 info[INF_SSL_RATE] = mkf_u32(FN_RATE, ssl_sess_rate);
3081 info[INF_SSL_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.ssl_lim);
3082 info[INF_MAX_SSL_RATE] = mkf_u32(FN_MAX, global.ssl_max);
3083 info[INF_SSL_FRONTEND_KEY_RATE] = mkf_u32(0, ssl_key_rate);
3084 info[INF_SSL_FRONTEND_MAX_KEY_RATE] = mkf_u32(FN_MAX, global.ssl_fe_keys_max);
3085 info[INF_SSL_FRONTEND_SESSION_REUSE_PCT] = mkf_u32(0, ssl_reuse);
3086 info[INF_SSL_BACKEND_KEY_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.ssl_be_keys_per_sec));
3087 info[INF_SSL_BACKEND_MAX_KEY_RATE] = mkf_u32(FN_MAX, global.ssl_be_keys_max);
3088 info[INF_SSL_CACHE_LOOKUPS] = mkf_u32(FN_COUNTER, global.shctx_lookups);
3089 info[INF_SSL_CACHE_MISSES] = mkf_u32(FN_COUNTER, global.shctx_misses);
3090#endif
3091 info[INF_COMPRESS_BPS_IN] = mkf_u32(FN_RATE, read_freq_ctr(&global.comp_bps_in));
3092 info[INF_COMPRESS_BPS_OUT] = mkf_u32(FN_RATE, read_freq_ctr(&global.comp_bps_out));
3093 info[INF_COMPRESS_BPS_RATE_LIM] = mkf_u32(FO_CONFIG|FN_LIMIT, global.comp_rate_lim);
3094#ifdef USE_ZLIB
3095 info[INF_ZLIB_MEM_USAGE] = mkf_u32(0, zlib_used_memory);
3096 info[INF_MAX_ZLIB_MEM_USAGE] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxzlibmem);
3097#endif
3098 info[INF_TASKS] = mkf_u32(0, nb_tasks_cur);
3099 info[INF_RUN_QUEUE] = mkf_u32(0, run_queue_cur);
3100 info[INF_IDLE_PCT] = mkf_u32(FN_AVG, idle_pct);
3101 info[INF_NODE] = mkf_str(FO_CONFIG|FN_OUTPUT|FS_SERVICE, global.node);
3102 if (global.desc)
3103 info[INF_DESCRIPTION] = mkf_str(FO_CONFIG|FN_OUTPUT|FS_SERVICE, global.desc);
3104
Willy Tarreau1b4ba1e2016-01-11 18:29:04 +01003105 chunk_reset(&trash);
3106 stats_dump_info_fields(&trash, info);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003107
Willy Tarreaubc18da12015-03-13 14:00:47 +01003108 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02003109 si_applet_cant_put(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003110 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01003111 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003112
3113 return 1;
3114}
3115
Baptiste Assmann28289462015-07-03 08:01:20 +02003116/* dumps server state information into <buf> for all the servers found in <backend>
3117 * These information are all the parameters which may change during HAProxy runtime.
3118 * By default, we only export to the last known server state file format.
3119 * These information can be used at next startup to recover same level of server state.
3120 */
3121static void dump_servers_state(struct proxy *backend, struct chunk *buf)
3122{
3123 struct server *srv;
3124 char srv_addr[INET6_ADDRSTRLEN + 1];
3125 time_t srv_time_since_last_change;
3126 int bk_f_forced_id, srv_f_forced_id;
3127
3128 /* we don't want to report any state if the backend is not enabled on this process */
3129 if (backend->bind_proc && !(backend->bind_proc & (1UL << (relative_pid - 1))))
3130 return;
3131
3132 srv = backend->srv;
3133
3134 while (srv) {
3135 srv_addr[0] = '\0';
3136 srv_time_since_last_change = 0;
3137 bk_f_forced_id = 0;
3138 srv_f_forced_id = 0;
3139
3140 switch (srv->addr.ss_family) {
3141 case AF_INET:
3142 inet_ntop(srv->addr.ss_family, &((struct sockaddr_in *)&srv->addr)->sin_addr,
3143 srv_addr, INET_ADDRSTRLEN + 1);
3144 break;
3145 case AF_INET6:
3146 inet_ntop(srv->addr.ss_family, &((struct sockaddr_in6 *)&srv->addr)->sin6_addr,
3147 srv_addr, INET6_ADDRSTRLEN + 1);
3148 break;
3149 }
3150 srv_time_since_last_change = now.tv_sec - srv->last_change;
3151 bk_f_forced_id = backend->options & PR_O_FORCED_ID ? 1 : 0;
3152 srv_f_forced_id = srv->flags & SRV_F_FORCED_ID ? 1 : 0;
3153
3154 chunk_appendf(buf,
3155 "%d %s "
3156 "%d %s %s "
3157 "%d %d %d %d %ld "
3158 "%d %d %d %d %d "
3159 "%d %d"
3160 "\n",
3161 backend->uuid, backend->id,
3162 srv->puid, srv->id, srv_addr,
David Carlier081b3362015-11-18 06:10:22 +00003163 srv->state, srv->admin, srv->uweight, srv->iweight, (long int)srv_time_since_last_change,
Baptiste Assmann28289462015-07-03 08:01:20 +02003164 srv->check.status, srv->check.result, srv->check.health, srv->check.state, srv->agent.state,
3165 bk_f_forced_id, srv_f_forced_id);
3166
3167 srv = srv->next;
3168 }
3169}
3170
Baptiste Assmann9b6857e2015-09-18 14:49:12 +02003171/* Parses backend list and simply report backend names */
3172static int stats_dump_backend_to_buffer(struct stream_interface *si)
3173{
3174 extern struct proxy *proxy;
3175 struct proxy *curproxy;
3176
3177 chunk_reset(&trash);
3178 chunk_printf(&trash, "# name\n");
3179
3180 for (curproxy = proxy; curproxy != NULL; curproxy = curproxy->next) {
3181 /* looking for backends only */
3182 if (!(curproxy->cap & PR_CAP_BE))
3183 continue;
3184
3185 /* we don't want to list a backend which is bound to this process */
3186 if (curproxy->bind_proc && !(curproxy->bind_proc & (1UL << (relative_pid - 1))))
3187 continue;
3188
3189 chunk_appendf(&trash, "%s\n", curproxy->id);
3190 }
3191
3192 if (bi_putchk(si_ic(si), &trash) == -1) {
3193 si_applet_cant_put(si);
3194 return 0;
3195 }
3196
3197 return 1;
3198}
3199
Baptiste Assmann28289462015-07-03 08:01:20 +02003200/* Parses backend list or simply use backend name provided by the user to return
3201 * states of servers to stdout.
3202 */
3203static int stats_dump_servers_state_to_buffer(struct stream_interface *si)
3204{
3205 struct appctx *appctx = __objt_appctx(si->end);
3206 extern struct proxy *proxy;
3207 struct proxy *curproxy;
3208
3209 chunk_reset(&trash);
3210
3211 chunk_printf(&trash, "%d\n# %s\n", SRV_STATE_FILE_VERSION, SRV_STATE_FILE_FIELD_NAMES);
3212
3213 if (appctx->ctx.server_state.backend) {
3214 dump_servers_state(appctx->ctx.server_state.backend, &trash);
3215 }
3216 else {
3217 for (curproxy = proxy; curproxy != NULL; curproxy = curproxy->next) {
3218 /* servers are only in backends */
3219 if (!(curproxy->cap & PR_CAP_BE))
3220 continue;
3221
3222 dump_servers_state(curproxy, &trash);
3223 }
3224 }
3225
3226 if (bi_putchk(si_ic(si), &trash) == -1) {
3227 si_applet_cant_put(si);
3228 return 0;
3229 }
3230
3231 return 1;
3232}
3233
Willy Tarreau12833bb2014-01-28 16:49:56 +01003234/* This function dumps memory usage information onto the stream interface's
3235 * read buffer. It returns 0 as long as it does not complete, non-zero upon
3236 * completion. No state is used.
3237 */
3238static int stats_dump_pools_to_buffer(struct stream_interface *si)
3239{
3240 dump_pools_to_trash();
Willy Tarreaubc18da12015-03-13 14:00:47 +01003241 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02003242 si_applet_cant_put(si);
Willy Tarreau12833bb2014-01-28 16:49:56 +01003243 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01003244 }
Willy Tarreau82a86022016-01-04 19:04:18 +01003245 return 1;
3246}
3247
3248/* Dump all fields from <stats> into <out> using CSV format */
3249static int stats_dump_fields_csv(struct chunk *out, const struct field *stats)
3250{
3251 int field;
3252
3253 for (field = 0; field < ST_F_TOTAL_FIELDS; field++) {
3254 if (!stats_emit_raw_data_field(out, &stats[field]))
3255 return 0;
3256 if (!chunk_strcat(out, ","))
3257 return 0;
3258 }
3259 chunk_strcat(&trash, "\n");
Willy Tarreau12833bb2014-01-28 16:49:56 +01003260 return 1;
3261}
3262
Willy Tarreau36090d22016-01-11 15:22:55 +01003263/* Dump all fields from <stats> into trash using the HTML format.
Willy Tarreaub5f66b82016-01-08 17:15:39 +01003264 * A column is reserved for the checkbox is <admin> is non-null. The caller's
3265 * flags may be passed in <flags> (eg: SHLGNDS to show the legends).
Cyril Bonté70be45d2010-10-12 00:14:35 +02003266 */
Willy Tarreau36090d22016-01-11 15:22:55 +01003267static int stats_dump_fields_html(const struct field *stats, int admin, unsigned int flags)
Cyril Bonté70be45d2010-10-12 00:14:35 +02003268{
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003269 struct chunk src;
3270
Willy Tarreaub5f66b82016-01-08 17:15:39 +01003271 if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_FE) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003272 chunk_appendf(&trash,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003273 /* name, queue */
3274 "<tr class=\"frontend\">");
Cyril Bonté70be45d2010-10-12 00:14:35 +02003275
Willy Tarreaub5f66b82016-01-08 17:15:39 +01003276 if (admin) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003277 /* Column sub-heading for Enable or Disable server */
3278 chunk_appendf(&trash, "<td></td>");
3279 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02003280
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003281 chunk_appendf(&trash,
3282 "<td class=ac>"
3283 "<a name=\"%s/Frontend\"></a>"
3284 "<a class=lfsb href=\"#%s/Frontend\">Frontend</a></td>"
3285 "<td colspan=3></td>"
3286 "",
Willy Tarreaud72c9172016-01-06 15:26:40 +01003287 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_PXNAME));
Cyril Bonté70be45d2010-10-12 00:14:35 +02003288
Willy Tarreau466c9b52012-12-23 02:25:03 +01003289 chunk_appendf(&trash,
3290 /* sessions rate : current */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003291 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01003292 "<tr><th>Current connection rate:</th><td>%s/s</td></tr>"
3293 "<tr><th>Current session rate:</th><td>%s/s</td></tr>"
3294 "",
Willy Tarreaud72c9172016-01-06 15:26:40 +01003295 U2H(stats[ST_F_RATE].u.u32),
Willy Tarreauc73810f2016-01-11 13:52:04 +01003296 U2H(stats[ST_F_CONN_RATE].u.u32),
Willy Tarreaud72c9172016-01-06 15:26:40 +01003297 U2H(stats[ST_F_RATE].u.u32));
Willy Tarreau466c9b52012-12-23 02:25:03 +01003298
Willy Tarreauf8211df2016-01-11 14:09:38 +01003299 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003300 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01003301 "<tr><th>Current request rate:</th><td>%s/s</td></tr>",
Willy Tarreaud72c9172016-01-06 15:26:40 +01003302 U2H(stats[ST_F_REQ_RATE].u.u32));
Willy Tarreau466c9b52012-12-23 02:25:03 +01003303
3304 chunk_appendf(&trash,
3305 "</table></div></u></td>"
3306 /* sessions rate : max */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003307 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01003308 "<tr><th>Max connection rate:</th><td>%s/s</td></tr>"
3309 "<tr><th>Max session rate:</th><td>%s/s</td></tr>"
3310 "",
Willy Tarreaud72c9172016-01-06 15:26:40 +01003311 U2H(stats[ST_F_RATE_MAX].u.u32),
Willy Tarreauc73810f2016-01-11 13:52:04 +01003312 U2H(stats[ST_F_CONN_RATE_MAX].u.u32),
Willy Tarreaud72c9172016-01-06 15:26:40 +01003313 U2H(stats[ST_F_RATE_MAX].u.u32));
Willy Tarreau466c9b52012-12-23 02:25:03 +01003314
Willy Tarreauf8211df2016-01-11 14:09:38 +01003315 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003316 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01003317 "<tr><th>Max request rate:</th><td>%s/s</td></tr>",
Willy Tarreaud72c9172016-01-06 15:26:40 +01003318 U2H(stats[ST_F_REQ_RATE_MAX].u.u32));
Willy Tarreau466c9b52012-12-23 02:25:03 +01003319
3320 chunk_appendf(&trash,
3321 "</table></div></u></td>"
3322 /* sessions rate : limit */
3323 "<td>%s</td>",
Willy Tarreaud72c9172016-01-06 15:26:40 +01003324 LIM2A(stats[ST_F_RATE_LIM].u.u32, "-"));
Cyril Bonté70be45d2010-10-12 00:14:35 +02003325
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003326 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003327 /* sessions: current, max, limit, total */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003328 "<td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003329 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01003330 "<tr><th>Cum. connections:</th><td>%s</td></tr>"
3331 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003332 "",
Willy Tarreauc73810f2016-01-11 13:52:04 +01003333 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 +01003334 U2H(stats[ST_F_STOT].u.u64),
Willy Tarreauc73810f2016-01-11 13:52:04 +01003335 U2H(stats[ST_F_CONN_TOT].u.u64),
Willy Tarreaud72c9172016-01-06 15:26:40 +01003336 U2H(stats[ST_F_STOT].u.u64));
Cyril Bonté70be45d2010-10-12 00:14:35 +02003337
Willy Tarreau466c9b52012-12-23 02:25:03 +01003338 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
Willy Tarreauf8211df2016-01-11 14:09:38 +01003339 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
Willy Tarreau466c9b52012-12-23 02:25:03 +01003340 chunk_appendf(&trash,
3341 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
3342 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
3343 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
3344 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
3345 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
3346 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
3347 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
3348 "<tr><th>- other responses:</th><td>%s</td></tr>"
3349 "<tr><th>Intercepted requests:</th><td>%s</td></tr>"
3350 "",
Willy Tarreaud72c9172016-01-06 15:26:40 +01003351 U2H(stats[ST_F_REQ_TOT].u.u64),
3352 U2H(stats[ST_F_HRSP_1XX].u.u64),
3353 U2H(stats[ST_F_HRSP_2XX].u.u64),
3354 U2H(stats[ST_F_COMP_RSP].u.u64),
3355 stats[ST_F_HRSP_2XX].u.u64 ?
3356 (int)(100 * stats[ST_F_COMP_RSP].u.u64 / stats[ST_F_HRSP_2XX].u.u64) : 0,
3357 U2H(stats[ST_F_HRSP_3XX].u.u64),
3358 U2H(stats[ST_F_HRSP_4XX].u.u64),
3359 U2H(stats[ST_F_HRSP_5XX].u.u64),
3360 U2H(stats[ST_F_HRSP_OTHER].u.u64),
Willy Tarreau5b9bdff2016-01-11 14:40:47 +01003361 U2H(stats[ST_F_INTERCEPTED].u.u64));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003362 }
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02003363
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003364 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01003365 "</table></div></u></td>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05003366 /* sessions: lbtot, lastsess */
3367 "<td></td><td></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003368 /* bytes : in */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003369 "<td>%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003370 "",
Willy Tarreaud72c9172016-01-06 15:26:40 +01003371 U2H(stats[ST_F_BIN].u.u64));
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02003372
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003373 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003374 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
Willy Tarreau4e5d58e2015-07-04 14:35:15 +02003375 "<td>%s%s<div class=tips><table class=det>"
3376 "<tr><th>Response bytes in:</th><td>%s</td></tr>"
3377 "<tr><th>Compression in:</th><td>%s</td></tr>"
3378 "<tr><th>Compression out:</th><td>%s</td><td>(%d%%)</td></tr>"
3379 "<tr><th>Compression bypass:</th><td>%s</td></tr>"
3380 "<tr><th>Total bytes saved:</th><td>%s</td><td>(%d%%)</td></tr>"
3381 "</table></div>%s</td>",
Willy Tarreaud72c9172016-01-06 15:26:40 +01003382 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "<u>":"",
3383 U2H(stats[ST_F_BOUT].u.u64),
3384 U2H(stats[ST_F_BOUT].u.u64),
3385 U2H(stats[ST_F_COMP_IN].u.u64),
3386 U2H(stats[ST_F_COMP_OUT].u.u64),
3387 stats[ST_F_COMP_IN].u.u64 ? (int)(stats[ST_F_COMP_OUT].u.u64 * 100 / stats[ST_F_COMP_IN].u.u64) : 0,
3388 U2H(stats[ST_F_COMP_BYP].u.u64),
3389 U2H(stats[ST_F_COMP_IN].u.u64 - stats[ST_F_COMP_OUT].u.u64),
3390 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,
3391 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "</u>":"");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02003392
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003393 chunk_appendf(&trash,
3394 /* denied: req, resp */
3395 "<td>%s</td><td>%s</td>"
3396 /* errors : request, connect, response */
3397 "<td>%s</td><td></td><td></td>"
3398 /* warnings: retries, redispatches */
3399 "<td></td><td></td>"
3400 /* server status : reflect frontend status */
3401 "<td class=ac>%s</td>"
3402 /* rest of server: nothing */
3403 "<td class=ac colspan=8></td></tr>"
3404 "",
Willy Tarreaud72c9172016-01-06 15:26:40 +01003405 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
3406 U2H(stats[ST_F_EREQ].u.u64),
3407 field_str(stats, ST_F_STATUS));
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02003408 }
Willy Tarreaufa9512f2016-01-08 17:20:51 +01003409 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_SO) {
3410 chunk_appendf(&trash, "<tr class=socket>");
3411 if (admin) {
3412 /* Column sub-heading for Enable or Disable server */
3413 chunk_appendf(&trash, "<td></td>");
3414 }
3415
3416 chunk_appendf(&trash,
3417 /* frontend name, listener name */
3418 "<td class=ac><a name=\"%s/+%s\"></a>%s"
3419 "<a class=lfsb href=\"#%s/+%s\">%s</a>"
3420 "",
3421 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME),
3422 (flags & ST_SHLGNDS)?"<u>":"",
3423 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME), field_str(stats, ST_F_SVNAME));
3424
3425 if (flags & ST_SHLGNDS) {
3426 chunk_appendf(&trash, "<div class=tips>");
3427
3428 if (isdigit(*field_str(stats, ST_F_ADDR)))
3429 chunk_appendf(&trash, "IPv4: %s, ", field_str(stats, ST_F_ADDR));
3430 else if (*field_str(stats, ST_F_ADDR) == '[')
3431 chunk_appendf(&trash, "IPv6: %s, ", field_str(stats, ST_F_ADDR));
3432 else if (*field_str(stats, ST_F_ADDR))
3433 chunk_appendf(&trash, "%s, ", field_str(stats, ST_F_ADDR));
3434
3435 /* id */
3436 chunk_appendf(&trash, "id: %d</div>", stats[ST_F_SID].u.u32);
3437 }
3438
3439 chunk_appendf(&trash,
3440 /* queue */
3441 "%s</td><td colspan=3></td>"
3442 /* sessions rate: current, max, limit */
3443 "<td colspan=3>&nbsp;</td>"
3444 /* sessions: current, max, limit, total, lbtot, lastsess */
3445 "<td>%s</td><td>%s</td><td>%s</td>"
3446 "<td>%s</td><td>&nbsp;</td><td>&nbsp;</td>"
3447 /* bytes: in, out */
3448 "<td>%s</td><td>%s</td>"
3449 "",
3450 (flags & ST_SHLGNDS)?"</u>":"",
3451 U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SMAX].u.u32), U2H(stats[ST_F_SLIM].u.u32),
3452 U2H(stats[ST_F_STOT].u.u64), U2H(stats[ST_F_BIN].u.u64), U2H(stats[ST_F_BOUT].u.u64));
3453
3454 chunk_appendf(&trash,
3455 /* denied: req, resp */
3456 "<td>%s</td><td>%s</td>"
3457 /* errors: request, connect, response */
3458 "<td>%s</td><td></td><td></td>"
3459 /* warnings: retries, redispatches */
3460 "<td></td><td></td>"
3461 /* server status: reflect listener status */
3462 "<td class=ac>%s</td>"
3463 /* rest of server: nothing */
3464 "<td class=ac colspan=8></td></tr>"
3465 "",
3466 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
3467 U2H(stats[ST_F_EREQ].u.u64),
3468 field_str(stats, ST_F_STATUS));
3469 }
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003470 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_SV) {
3471 const char *style;
Willy Tarreaub5f66b82016-01-08 17:15:39 +01003472
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003473 /* determine the style to use depending on the server's state,
3474 * its health and weight. There isn't a 1-to-1 mapping between
3475 * state and styles for the cases where the server is (still)
3476 * up. The reason is that we don't want to report nolb and
3477 * drain with the same color.
3478 */
Willy Tarreaub5f66b82016-01-08 17:15:39 +01003479
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003480 if (strcmp(field_str(stats, ST_F_STATUS), "DOWN") == 0 ||
3481 strcmp(field_str(stats, ST_F_STATUS), "DOWN (agent)") == 0) {
3482 style = "down";
3483 }
3484 else if (strcmp(field_str(stats, ST_F_STATUS), "DOWN ") == 0) {
3485 style = "going_up";
3486 }
3487 else if (strcmp(field_str(stats, ST_F_STATUS), "NOLB ") == 0) {
3488 style = "going_down";
3489 }
3490 else if (strcmp(field_str(stats, ST_F_STATUS), "NOLB") == 0) {
3491 style = "nolb";
3492 }
3493 else if (strcmp(field_str(stats, ST_F_STATUS), "no check") == 0) {
3494 style = "no_check";
3495 }
3496 else if (!stats[ST_F_CHKFAIL].type ||
3497 stats[ST_F_CHECK_HEALTH].u.u32 == stats[ST_F_CHECK_RISE].u.u32 + stats[ST_F_CHECK_FALL].u.u32 - 1) {
3498 /* no check or max health = UP */
3499 if (stats[ST_F_WEIGHT].u.u32)
3500 style = "up";
3501 else
3502 style = "draining";
3503 }
3504 else {
3505 style = "going_down";
3506 }
Willy Tarreaub5f66b82016-01-08 17:15:39 +01003507
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003508 if (memcmp(field_str(stats, ST_F_STATUS), "MAINT", 5) == 0)
3509 chunk_appendf(&trash, "<tr class=\"maintain\">");
3510 else
3511 chunk_appendf(&trash,
3512 "<tr class=\"%s_%s\">",
3513 (stats[ST_F_BCK].u.u32) ? "backup" : "active", style);
Willy Tarreaub5f66b82016-01-08 17:15:39 +01003514
Willy Tarreaub5f66b82016-01-08 17:15:39 +01003515
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003516 if (admin)
3517 chunk_appendf(&trash,
3518 "<td><input type=\"checkbox\" name=\"s\" value=\"%s\"></td>",
3519 field_str(stats, ST_F_SVNAME));
Willy Tarreaub5f66b82016-01-08 17:15:39 +01003520
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003521 chunk_appendf(&trash,
3522 "<td class=ac><a name=\"%s/%s\"></a>%s"
3523 "<a class=lfsb href=\"#%s/%s\">%s</a>"
3524 "",
3525 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME),
3526 (flags & ST_SHLGNDS) ? "<u>" : "",
3527 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 +01003528
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003529 if (flags & ST_SHLGNDS) {
3530 chunk_appendf(&trash, "<div class=tips>");
Willy Tarreaub5f66b82016-01-08 17:15:39 +01003531
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003532 if (isdigit(*field_str(stats, ST_F_ADDR)))
3533 chunk_appendf(&trash, "IPv4: %s, ", field_str(stats, ST_F_ADDR));
3534 else if (*field_str(stats, ST_F_ADDR) == '[')
3535 chunk_appendf(&trash, "IPv6: %s, ", field_str(stats, ST_F_ADDR));
3536 else if (*field_str(stats, ST_F_ADDR))
3537 chunk_appendf(&trash, "%s, ", field_str(stats, ST_F_ADDR));
Willy Tarreaub5f66b82016-01-08 17:15:39 +01003538
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003539 /* id */
3540 chunk_appendf(&trash, "id: %d", stats[ST_F_SID].u.u32);
Willy Tarreaub5f66b82016-01-08 17:15:39 +01003541
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003542 /* cookie */
3543 if (stats[ST_F_COOKIE].type) {
3544 chunk_appendf(&trash, ", cookie: '");
3545 chunk_initstr(&src, field_str(stats, ST_F_COOKIE));
3546 chunk_htmlencode(&trash, &src);
3547 chunk_appendf(&trash, "'");
3548 }
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02003549
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003550 chunk_appendf(&trash, "</div>");
3551 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003552
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003553 chunk_appendf(&trash,
3554 /* queue : current, max, limit */
3555 "%s</td><td>%s</td><td>%s</td><td>%s</td>"
3556 /* sessions rate : current, max, limit */
3557 "<td>%s</td><td>%s</td><td></td>"
3558 "",
3559 (flags & ST_SHLGNDS) ? "</u>" : "",
3560 U2H(stats[ST_F_QCUR].u.u32), U2H(stats[ST_F_QMAX].u.u32), LIM2A(stats[ST_F_QLIMIT].u.u32, "-"),
3561 U2H(stats[ST_F_RATE].u.u32), U2H(stats[ST_F_RATE_MAX].u.u32));
Willy Tarreau4607fad2016-01-06 15:41:29 +01003562
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003563 chunk_appendf(&trash,
3564 /* sessions: current, max, limit, total */
3565 "<td>%s</td><td>%s</td><td>%s</td>"
3566 "<td><u>%s<div class=tips><table class=det>"
3567 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
3568 "",
3569 U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SMAX].u.u32), LIM2A(stats[ST_F_SLIM].u.u32, "-"),
3570 U2H(stats[ST_F_STOT].u.u64),
3571 U2H(stats[ST_F_STOT].u.u64));
Willy Tarreau4607fad2016-01-06 15:41:29 +01003572
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003573 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
Willy Tarreauf8211df2016-01-11 14:09:38 +01003574 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003575 unsigned long long tot;
Willy Tarreaua6f5a732016-01-08 16:59:56 +01003576
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003577 tot = stats[ST_F_HRSP_OTHER].u.u64;
3578 tot += stats[ST_F_HRSP_1XX].u.u64;
3579 tot += stats[ST_F_HRSP_2XX].u.u64;
3580 tot += stats[ST_F_HRSP_3XX].u.u64;
3581 tot += stats[ST_F_HRSP_4XX].u.u64;
3582 tot += stats[ST_F_HRSP_5XX].u.u64;
3583
3584 chunk_appendf(&trash,
3585 "<tr><th>Cum. HTTP responses:</th><td>%s</td></tr>"
3586 "<tr><th>- HTTP 1xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
3587 "<tr><th>- HTTP 2xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
3588 "<tr><th>- HTTP 3xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
3589 "<tr><th>- HTTP 4xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
3590 "<tr><th>- HTTP 5xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
3591 "<tr><th>- other responses:</th><td>%s</td><td>(%d%%)</td></tr>"
3592 "",
3593 U2H(tot),
3594 U2H(stats[ST_F_HRSP_1XX].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_1XX].u.u64 / tot) : 0,
3595 U2H(stats[ST_F_HRSP_2XX].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_2XX].u.u64 / tot) : 0,
3596 U2H(stats[ST_F_HRSP_3XX].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_3XX].u.u64 / tot) : 0,
3597 U2H(stats[ST_F_HRSP_4XX].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_4XX].u.u64 / tot) : 0,
3598 U2H(stats[ST_F_HRSP_5XX].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_5XX].u.u64 / tot) : 0,
3599 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 +01003600 }
Willy Tarreaua6f5a732016-01-08 16:59:56 +01003601
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003602 chunk_appendf(&trash, "<tr><th colspan=3>Avg over last 1024 success. conn.</th></tr>");
3603 chunk_appendf(&trash, "<tr><th>- Queue time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_QTIME].u.u32));
3604 chunk_appendf(&trash, "<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 +01003605 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003606 chunk_appendf(&trash, "<tr><th>- Response time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_RTIME].u.u32));
3607 chunk_appendf(&trash, "<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 +02003608
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003609 chunk_appendf(&trash,
3610 "</table></div></u></td>"
3611 /* sessions: lbtot, last */
3612 "<td>%s</td><td>%s</td>",
3613 U2H(stats[ST_F_LBTOT].u.u64),
3614 human_time(stats[ST_F_LASTSESS].u.s32, 1));
Willy Tarreau91861262007-10-17 17:06:05 +02003615
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003616 chunk_appendf(&trash,
3617 /* bytes : in, out */
3618 "<td>%s</td><td>%s</td>"
3619 /* denied: req, resp */
3620 "<td></td><td>%s</td>"
3621 /* errors : request, connect */
3622 "<td></td><td>%s</td>"
3623 /* errors : response */
3624 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
3625 /* warnings: retries, redispatches */
3626 "<td>%lld</td><td>%lld</td>"
3627 "",
3628 U2H(stats[ST_F_BIN].u.u64), U2H(stats[ST_F_BOUT].u.u64),
3629 U2H(stats[ST_F_DRESP].u.u64),
3630 U2H(stats[ST_F_ECON].u.u64),
3631 U2H(stats[ST_F_ERESP].u.u64),
3632 (long long)stats[ST_F_CLI_ABRT].u.u64,
3633 (long long)stats[ST_F_SRV_ABRT].u.u64,
3634 (long long)stats[ST_F_WRETR].u.u64,
3635 (long long)stats[ST_F_WREDIS].u.u64);
3636
3637 /* status, last change */
3638 chunk_appendf(&trash, "<td class=ac>");
3639
3640 /* FIXME!!!!
3641 * LASTCHG should contain the last change for *this* server and must be computed
3642 * properly above, as was done below, ie: this server if maint, otherwise ref server
3643 * if tracking. Note that ref is either local or remote depending on tracking.
3644 */
3645
3646
3647 if (memcmp(field_str(stats, ST_F_STATUS), "MAINT", 5) == 0) {
3648 chunk_appendf(&trash, "%s MAINT", human_time(stats[ST_F_LASTCHG].u.u32, 1));
3649 }
3650 else if (memcmp(field_str(stats, ST_F_STATUS), "no check", 5) == 0) {
3651 chunk_strcat(&trash, "<i>no check</i>");
3652 }
3653 else {
3654 chunk_appendf(&trash, "%s %s", human_time(stats[ST_F_LASTCHG].u.u32, 1), field_str(stats, ST_F_STATUS));
3655 if (memcmp(field_str(stats, ST_F_STATUS), "DOWN", 4) == 0) {
3656 if (stats[ST_F_CHECK_HEALTH].u.u32)
3657 chunk_strcat(&trash, " &uarr;");
3658 }
3659 else if (stats[ST_F_CHECK_HEALTH].u.u32 < stats[ST_F_CHECK_RISE].u.u32 + stats[ST_F_CHECK_FALL].u.u32 - 1)
3660 chunk_strcat(&trash, " &darr;");
3661 }
3662
3663 if (memcmp(field_str(stats, ST_F_STATUS), "DOWN", 4) == 0 &&
3664 stats[ST_F_AGENT_STATUS].type && !stats[ST_F_AGENT_HEALTH].u.u32) {
3665 chunk_appendf(&trash,
3666 "</td><td class=ac><u> %s",
3667 field_str(stats, ST_F_AGENT_STATUS));
3668
3669 if (stats[ST_F_AGENT_CODE].type)
3670 chunk_appendf(&trash, "/%d", stats[ST_F_AGENT_CODE].u.u32);
3671
3672 if (stats[ST_F_AGENT_DURATION].type && stats[ST_F_AGENT_DURATION].u.u64 >= 0)
3673 chunk_appendf(&trash, " in %lums", (long)stats[ST_F_AGENT_DURATION].u.u64);
3674
3675 chunk_appendf(&trash, "<div class=tips>%s", field_str(stats, ST_F_AGENT_DESC));
3676
3677 if (*field_str(stats, ST_F_LAST_AGT)) {
3678 chunk_appendf(&trash, ": ");
3679 chunk_initstr(&src, field_str(stats, ST_F_LAST_AGT));
3680 chunk_htmlencode(&trash, &src);
3681 }
3682 chunk_appendf(&trash, "</div></u>");
3683 }
3684 else if (stats[ST_F_CHECK_STATUS].type) {
3685 chunk_appendf(&trash,
3686 "</td><td class=ac><u> %s",
3687 field_str(stats, ST_F_CHECK_STATUS));
3688
3689 if (stats[ST_F_CHECK_CODE].type)
3690 chunk_appendf(&trash, "/%d", stats[ST_F_CHECK_CODE].u.u32);
3691
3692 if (stats[ST_F_CHECK_DURATION].type && stats[ST_F_CHECK_DURATION].u.u64 >= 0)
3693 chunk_appendf(&trash, " in %lums", (long)stats[ST_F_CHECK_DURATION].u.u64);
3694
3695 chunk_appendf(&trash, "<div class=tips>%s", field_str(stats, ST_F_CHECK_DESC));
3696
3697 if (*field_str(stats, ST_F_LAST_CHK)) {
3698 chunk_appendf(&trash, ": ");
3699 chunk_initstr(&src, field_str(stats, ST_F_LAST_CHK));
3700 chunk_htmlencode(&trash, &src);
3701 }
3702 chunk_appendf(&trash, "</div></u>");
3703 }
3704 else
3705 chunk_appendf(&trash, "</td><td>");
3706
3707 chunk_appendf(&trash,
3708 /* weight */
3709 "</td><td class=ac>%d</td>"
3710 /* act, bck */
3711 "<td class=ac>%s</td><td class=ac>%s</td>"
3712 "",
3713 stats[ST_F_WEIGHT].u.u32,
3714 stats[ST_F_BCK].u.u32 ? "-" : "Y",
3715 stats[ST_F_BCK].u.u32 ? "Y" : "-");
3716
3717 /* check failures: unique, fatal, down time */
3718 if (stats[ST_F_CHKFAIL].type) {
3719 chunk_appendf(&trash, "<td><u>%lld", (long long)stats[ST_F_CHKFAIL].u.u64);
3720
3721 if (stats[ST_F_HANAFAIL].type)
3722 chunk_appendf(&trash, "/%lld", (long long)stats[ST_F_HANAFAIL].u.u64);
3723
3724 chunk_appendf(&trash,
3725 "<div class=tips>Failed Health Checks%s</div></u></td>"
3726 "<td>%lld</td><td>%s</td>"
3727 "",
3728 stats[ST_F_HANAFAIL].type ? "/Health Analyses" : "",
3729 (long long)stats[ST_F_CHKDOWN].u.u64, human_time(stats[ST_F_DOWNTIME].u.u32, 1));
3730 }
3731 else if (strcmp(field_str(stats, ST_F_STATUS), "MAINT") != 0 && field_format(stats, ST_F_TRACKED) == FF_STR) {
3732 /* tracking a server (hence inherited maint would appear as "MAINT (via...)" */
3733 chunk_appendf(&trash,
3734 "<td class=ac colspan=3><a class=lfsb href=\"#%s\">via %s</a></td>",
3735 field_str(stats, ST_F_TRACKED), field_str(stats, ST_F_TRACKED));
3736 }
3737 else
3738 chunk_appendf(&trash, "<td colspan=3></td>");
3739
3740 /* throttle */
3741 if (stats[ST_F_THROTTLE].type)
3742 chunk_appendf(&trash, "<td class=ac>%d %%</td></tr>\n", stats[ST_F_THROTTLE].u.u32);
3743 else
3744 chunk_appendf(&trash, "<td class=ac>-</td></tr>\n");
3745 }
Willy Tarreaubbf84502016-01-08 17:25:50 +01003746 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_BE) {
3747 chunk_appendf(&trash, "<tr class=\"backend\">");
3748 if (admin) {
3749 /* Column sub-heading for Enable or Disable server */
3750 chunk_appendf(&trash, "<td></td>");
3751 }
3752 chunk_appendf(&trash,
3753 "<td class=ac>"
3754 /* name */
3755 "%s<a name=\"%s/Backend\"></a>"
3756 "<a class=lfsb href=\"#%s/Backend\">Backend</a>"
3757 "",
3758 (flags & ST_SHLGNDS)?"<u>":"",
3759 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_PXNAME));
3760
3761 if (flags & ST_SHLGNDS) {
3762 /* balancing */
3763 chunk_appendf(&trash, "<div class=tips>balancing: %s",
Willy Tarreauf1516d92016-01-11 14:48:36 +01003764 field_str(stats, ST_F_ALGO));
Willy Tarreaubbf84502016-01-08 17:25:50 +01003765
3766 /* cookie */
3767 if (stats[ST_F_COOKIE].type) {
3768 chunk_appendf(&trash, ", cookie: '");
3769 chunk_initstr(&src, field_str(stats, ST_F_COOKIE));
3770 chunk_htmlencode(&trash, &src);
3771 chunk_appendf(&trash, "'");
3772 }
3773 chunk_appendf(&trash, "</div>");
3774 }
3775
3776 chunk_appendf(&trash,
3777 "%s</td>"
3778 /* queue : current, max */
3779 "<td>%s</td><td>%s</td><td></td>"
3780 /* sessions rate : current, max, limit */
3781 "<td>%s</td><td>%s</td><td></td>"
3782 "",
3783 (flags & ST_SHLGNDS)?"</u>":"",
3784 U2H(stats[ST_F_QCUR].u.u32), U2H(stats[ST_F_QMAX].u.u32),
3785 U2H(stats[ST_F_RATE].u.u32), U2H(stats[ST_F_RATE_MAX].u.u32));
3786
3787 chunk_appendf(&trash,
3788 /* sessions: current, max, limit, total */
3789 "<td>%s</td><td>%s</td><td>%s</td>"
3790 "<td><u>%s<div class=tips><table class=det>"
3791 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
3792 "",
3793 U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SLIM].u.u32),
3794 U2H(stats[ST_F_STOT].u.u64),
3795 U2H(stats[ST_F_STOT].u.u64));
3796
3797 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
Willy Tarreauf8211df2016-01-11 14:09:38 +01003798 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
Willy Tarreaubbf84502016-01-08 17:25:50 +01003799 chunk_appendf(&trash,
3800 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
3801 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
3802 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
3803 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
3804 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
3805 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
3806 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
3807 "<tr><th>- other responses:</th><td>%s</td></tr>"
3808 "<tr><th>Intercepted requests:</th><td>%s</td></tr>"
3809 "<tr><th colspan=3>Avg over last 1024 success. conn.</th></tr>"
3810 "",
3811 U2H(stats[ST_F_REQ_TOT].u.u64),
3812 U2H(stats[ST_F_HRSP_1XX].u.u64),
3813 U2H(stats[ST_F_HRSP_2XX].u.u64),
3814 U2H(stats[ST_F_COMP_RSP].u.u64),
3815 stats[ST_F_HRSP_2XX].u.u64 ?
3816 (int)(100 * stats[ST_F_COMP_RSP].u.u64 / stats[ST_F_HRSP_2XX].u.u64) : 0,
3817 U2H(stats[ST_F_HRSP_3XX].u.u64),
3818 U2H(stats[ST_F_HRSP_4XX].u.u64),
3819 U2H(stats[ST_F_HRSP_5XX].u.u64),
3820 U2H(stats[ST_F_HRSP_OTHER].u.u64),
Willy Tarreau5b9bdff2016-01-11 14:40:47 +01003821 U2H(stats[ST_F_INTERCEPTED].u.u64));
Willy Tarreaubbf84502016-01-08 17:25:50 +01003822 }
3823
3824 chunk_appendf(&trash, "<tr><th>- Queue time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_QTIME].u.u32));
3825 chunk_appendf(&trash, "<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 +01003826 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
Willy Tarreaubbf84502016-01-08 17:25:50 +01003827 chunk_appendf(&trash, "<tr><th>- Response time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_RTIME].u.u32));
3828 chunk_appendf(&trash, "<tr><th>- Total time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_TTIME].u.u32));
3829
3830 chunk_appendf(&trash,
3831 "</table></div></u></td>"
3832 /* sessions: lbtot, last */
3833 "<td>%s</td><td>%s</td>"
3834 /* bytes: in */
3835 "<td>%s</td>"
3836 "",
3837 U2H(stats[ST_F_LBTOT].u.u64),
3838 human_time(stats[ST_F_LASTSESS].u.s32, 1),
3839 U2H(stats[ST_F_BIN].u.u64));
3840
3841 chunk_appendf(&trash,
3842 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
3843 "<td>%s%s<div class=tips><table class=det>"
3844 "<tr><th>Response bytes in:</th><td>%s</td></tr>"
3845 "<tr><th>Compression in:</th><td>%s</td></tr>"
3846 "<tr><th>Compression out:</th><td>%s</td><td>(%d%%)</td></tr>"
3847 "<tr><th>Compression bypass:</th><td>%s</td></tr>"
3848 "<tr><th>Total bytes saved:</th><td>%s</td><td>(%d%%)</td></tr>"
3849 "</table></div>%s</td>",
3850 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "<u>":"",
3851 U2H(stats[ST_F_BOUT].u.u64),
3852 U2H(stats[ST_F_BOUT].u.u64),
3853 U2H(stats[ST_F_COMP_IN].u.u64),
3854 U2H(stats[ST_F_COMP_OUT].u.u64),
3855 stats[ST_F_COMP_IN].u.u64 ? (int)(stats[ST_F_COMP_OUT].u.u64 * 100 / stats[ST_F_COMP_IN].u.u64) : 0,
3856 U2H(stats[ST_F_COMP_BYP].u.u64),
3857 U2H(stats[ST_F_COMP_IN].u.u64 - stats[ST_F_COMP_BYP].u.u64),
3858 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,
3859 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "</u>":"");
3860
3861 chunk_appendf(&trash,
3862 /* denied: req, resp */
3863 "<td>%s</td><td>%s</td>"
3864 /* errors : request, connect */
3865 "<td></td><td>%s</td>"
3866 /* errors : response */
3867 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
3868 /* warnings: retries, redispatches */
3869 "<td>%lld</td><td>%lld</td>"
3870 /* backend status: reflect backend status (up/down): we display UP
3871 * if the backend has known working servers or if it has no server at
3872 * all (eg: for stats). Then we display the total weight, number of
3873 * active and backups. */
3874 "<td class=ac>%s %s</td><td class=ac>&nbsp;</td><td class=ac>%d</td>"
3875 "<td class=ac>%d</td><td class=ac>%d</td>"
3876 "",
3877 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
3878 U2H(stats[ST_F_ECON].u.u64),
3879 U2H(stats[ST_F_ERESP].u.u64),
3880 (long long)stats[ST_F_CLI_ABRT].u.u64,
3881 (long long)stats[ST_F_SRV_ABRT].u.u64,
3882 (long long)stats[ST_F_WRETR].u.u64, (long long)stats[ST_F_WREDIS].u.u64,
3883 human_time(stats[ST_F_LASTCHG].u.u32, 1),
3884 strcmp(field_str(stats, ST_F_STATUS), "DOWN") ? field_str(stats, ST_F_STATUS) : "<font color=\"red\"><b>DOWN</b></font>",
3885 stats[ST_F_WEIGHT].u.u32,
3886 stats[ST_F_ACT].u.u32, stats[ST_F_BCK].u.u32);
3887
3888 chunk_appendf(&trash,
3889 /* rest of backend: nothing, down transitions, total downtime, throttle */
3890 "<td class=ac>&nbsp;</td><td>%d</td>"
3891 "<td>%s</td>"
3892 "<td></td>"
3893 "</tr>",
3894 stats[ST_F_CHKDOWN].u.u32,
3895 stats[ST_F_DOWNTIME].type ? human_time(stats[ST_F_DOWNTIME].u.u32, 1) : "&nbsp;");
3896 }
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003897 return 1;
3898}
3899
Willy Tarreau501f6022016-01-08 17:37:22 +01003900static int stats_dump_one_line(const struct field *stats, unsigned int flags, struct proxy *px, struct appctx *appctx)
3901{
3902 int admin;
3903
3904 admin = (px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN);
3905
3906 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
Willy Tarreau36090d22016-01-11 15:22:55 +01003907 return stats_dump_fields_html(stats, admin, flags);
Willy Tarreau501f6022016-01-08 17:37:22 +01003908 else
3909 return stats_dump_fields_csv(&trash, stats);
3910}
3911
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003912/* Dumps a frontend's line to the trash for the current proxy <px> and uses
3913 * the state from stream interface <si>. The caller is responsible for clearing
3914 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
3915 */
3916static int stats_dump_fe_stats(struct stream_interface *si, struct proxy *px)
3917{
3918 struct appctx *appctx = __objt_appctx(si->end);
3919
3920 if (!(px->cap & PR_CAP_FE))
3921 return 0;
3922
3923 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_FE)))
3924 return 0;
3925
3926 memset(&stats, 0, sizeof(stats));
3927
3928 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
3929 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, "FRONTEND");
Willy Tarreauf8211df2016-01-11 14:09:38 +01003930 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003931 stats[ST_F_SCUR] = mkf_u32(0, px->feconn);
3932 stats[ST_F_SMAX] = mkf_u32(FN_MAX, px->fe_counters.conn_max);
3933 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, px->maxconn);
3934 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, px->fe_counters.cum_sess);
3935 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, px->fe_counters.bytes_in);
3936 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, px->fe_counters.bytes_out);
3937 stats[ST_F_DREQ] = mkf_u64(FN_COUNTER, px->fe_counters.denied_req);
3938 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, px->fe_counters.denied_resp);
3939 stats[ST_F_EREQ] = mkf_u64(FN_COUNTER, px->fe_counters.failed_req);
3940 stats[ST_F_STATUS] = mkf_str(FO_STATUS, px->state == PR_STREADY ? "OPEN" : px->state == PR_STFULL ? "FULL" : "STOP");
3941 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
3942 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
3943 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, 0);
3944 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_FE);
3945 stats[ST_F_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_sess_per_sec));
3946 stats[ST_F_RATE_LIM] = mkf_u32(FO_CONFIG|FN_LIMIT, px->fe_sps_lim);
3947 stats[ST_F_RATE_MAX] = mkf_u32(FN_MAX, px->fe_counters.sps_max);
3948
3949 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
3950 if (px->mode == PR_MODE_HTTP) {
3951 stats[ST_F_HRSP_1XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[1]);
3952 stats[ST_F_HRSP_2XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[2]);
3953 stats[ST_F_HRSP_3XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[3]);
3954 stats[ST_F_HRSP_4XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[4]);
3955 stats[ST_F_HRSP_5XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[5]);
3956 stats[ST_F_HRSP_OTHER] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[0]);
Willy Tarreau5b9bdff2016-01-11 14:40:47 +01003957 stats[ST_F_INTERCEPTED] = mkf_u64(FN_COUNTER, px->fe_counters.intercepted_req);
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003958 }
3959
3960 /* requests : req_rate, req_rate_max, req_tot, */
3961 stats[ST_F_REQ_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_req_per_sec));
3962 stats[ST_F_REQ_RATE_MAX] = mkf_u32(FN_MAX, px->fe_counters.p.http.rps_max);
3963 stats[ST_F_REQ_TOT] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cum_req);
3964
3965 /* compression: in, out, bypassed, responses */
3966 stats[ST_F_COMP_IN] = mkf_u64(FN_COUNTER, px->fe_counters.comp_in);
3967 stats[ST_F_COMP_OUT] = mkf_u64(FN_COUNTER, px->fe_counters.comp_out);
3968 stats[ST_F_COMP_BYP] = mkf_u64(FN_COUNTER, px->fe_counters.comp_byp);
3969 stats[ST_F_COMP_RSP] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.comp_rsp);
3970
Willy Tarreauc73810f2016-01-11 13:52:04 +01003971 /* connections : conn_rate, conn_rate_max, conn_tot, conn_max */
3972 stats[ST_F_CONN_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_conn_per_sec));
3973 stats[ST_F_CONN_RATE_MAX] = mkf_u32(FN_MAX, px->fe_counters.cps_max);
3974 stats[ST_F_CONN_TOT] = mkf_u64(FN_COUNTER, px->fe_counters.cum_conn);
3975
Willy Tarreau501f6022016-01-08 17:37:22 +01003976 return stats_dump_one_line(stats, 0, px, appctx);
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003977}
3978
3979/* Dumps a line for listener <l> and proxy <px> to the trash and uses the state
3980 * from stream interface <si>, and stats flags <flags>. The caller is responsible
3981 * for clearing the trash if needed. Returns non-zero if it emits anything, zero
3982 * otherwise.
3983 */
3984static int stats_dump_li_stats(struct stream_interface *si, struct proxy *px, struct listener *l, int flags)
3985{
3986 struct appctx *appctx = __objt_appctx(si->end);
3987 struct chunk *out = get_trash_chunk();
3988
3989 chunk_reset(out);
3990 memset(&stats, 0, sizeof(stats));
3991
3992 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
3993 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, l->name);
Willy Tarreauf8211df2016-01-11 14:09:38 +01003994 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003995 stats[ST_F_SCUR] = mkf_u32(0, l->nbconn);
3996 stats[ST_F_SMAX] = mkf_u32(FN_MAX, l->counters->conn_max);
3997 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, l->maxconn);
3998 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, l->counters->cum_conn);
3999 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, l->counters->bytes_in);
4000 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, l->counters->bytes_out);
4001 stats[ST_F_DREQ] = mkf_u64(FN_COUNTER, l->counters->denied_req);
4002 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, l->counters->denied_resp);
4003 stats[ST_F_EREQ] = mkf_u64(FN_COUNTER, l->counters->failed_req);
4004 stats[ST_F_STATUS] = mkf_str(FO_STATUS, (l->nbconn < l->maxconn) ? (l->state == LI_LIMITED) ? "WAITING" : "OPEN" : "FULL");
4005 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
4006 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
4007 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, l->luid);
4008 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_SO);
4009
4010 if (flags & ST_SHLGNDS) {
4011 char str[INET6_ADDRSTRLEN];
4012 int port;
4013
4014 port = get_host_port(&l->addr);
4015 switch (addr_to_str(&l->addr, str, sizeof(str))) {
4016 case AF_INET:
4017 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
4018 chunk_appendf(out, "%s:%d", str, port);
4019 break;
4020 case AF_INET6:
4021 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
4022 chunk_appendf(out, "[%s]:%d", str, port);
4023 break;
4024 case AF_UNIX:
4025 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, "unix");
4026 break;
4027 case -1:
4028 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
4029 chunk_strcat(out, strerror(errno));
4030 break;
4031 default: /* address family not supported */
4032 break;
4033 }
4034 }
4035
Willy Tarreau501f6022016-01-08 17:37:22 +01004036 return stats_dump_one_line(stats, flags, px, appctx);
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004037}
4038
4039enum srv_stats_state {
4040 SRV_STATS_STATE_DOWN = 0,
4041 SRV_STATS_STATE_DOWN_AGENT,
4042 SRV_STATS_STATE_GOING_UP,
4043 SRV_STATS_STATE_UP_GOING_DOWN,
4044 SRV_STATS_STATE_UP,
4045 SRV_STATS_STATE_NOLB_GOING_DOWN,
Simon Horman4d2eab62015-04-23 14:51:26 +09004046 SRV_STATS_STATE_NOLB,
4047 SRV_STATS_STATE_DRAIN_GOING_DOWN,
4048 SRV_STATS_STATE_DRAIN,
Simon Hormanb167b6b2015-04-23 14:51:29 +09004049 SRV_STATS_STATE_DRAIN_AGENT,
Simon Horman4d2eab62015-04-23 14:51:26 +09004050 SRV_STATS_STATE_NO_CHECK,
4051
4052 SRV_STATS_STATE_COUNT, /* Must be last */
4053};
4054
Willy Tarreau2b96cf12016-01-06 19:25:38 +01004055static const char *srv_hlt_st[SRV_STATS_STATE_COUNT] = {
4056 [SRV_STATS_STATE_DOWN] = "DOWN",
4057 [SRV_STATS_STATE_DOWN_AGENT] = "DOWN (agent)",
4058 [SRV_STATS_STATE_GOING_UP] = "DOWN %d/%d",
4059 [SRV_STATS_STATE_UP_GOING_DOWN] = "UP %d/%d",
4060 [SRV_STATS_STATE_UP] = "UP",
4061 [SRV_STATS_STATE_NOLB_GOING_DOWN] = "NOLB %d/%d",
4062 [SRV_STATS_STATE_NOLB] = "NOLB",
4063 [SRV_STATS_STATE_DRAIN_GOING_DOWN] = "DRAIN %d/%d",
4064 [SRV_STATS_STATE_DRAIN] = "DRAIN",
4065 [SRV_STATS_STATE_DRAIN_AGENT] = "DRAIN (agent)",
4066 [SRV_STATS_STATE_NO_CHECK] = "no check"
4067};
4068
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004069/* Dumps a line for server <sv> and proxy <px> to the trash and uses the state
4070 * from stream interface <si>, stats flags <flags>, and server state <state>.
4071 * The caller is responsible for clearing the trash if needed. Returns non-zero
Simon Horman4d2eab62015-04-23 14:51:26 +09004072 * if it emits anything, zero otherwise.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004073 */
Willy Tarreauba2f2642016-01-07 09:54:40 +01004074static int stats_dump_sv_stats(struct stream_interface *si, struct proxy *px, int flags, struct server *sv)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004075{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004076 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau32091232014-05-16 13:52:00 +02004077 struct server *via, *ref;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004078 char str[INET6_ADDRSTRLEN];
Willy Tarreau2b96cf12016-01-06 19:25:38 +01004079 struct chunk *out = get_trash_chunk();
Willy Tarreauba2f2642016-01-07 09:54:40 +01004080 enum srv_stats_state state;
Willy Tarreau2b96cf12016-01-06 19:25:38 +01004081 char *fld_status;
Willy Tarreau32091232014-05-16 13:52:00 +02004082 /* we have "via" which is the tracked server as described in the configuration,
4083 * and "ref" which is the checked server and the end of the chain.
4084 */
4085 via = sv->track ? sv->track : sv;
4086 ref = via;
4087 while (ref->track)
4088 ref = ref->track;
4089
Willy Tarreauba2f2642016-01-07 09:54:40 +01004090 if (sv->state == SRV_ST_RUNNING || sv->state == SRV_ST_STARTING) {
4091 if ((ref->check.state & CHK_ST_ENABLED) &&
4092 (ref->check.health < ref->check.rise + ref->check.fall - 1)) {
4093 state = SRV_STATS_STATE_UP_GOING_DOWN;
Willy Tarreauba2f2642016-01-07 09:54:40 +01004094 } else {
4095 state = SRV_STATS_STATE_UP;
Willy Tarreauba2f2642016-01-07 09:54:40 +01004096 }
4097
Willy Tarreauba2f2642016-01-07 09:54:40 +01004098 if (sv->admin & SRV_ADMF_DRAIN) {
4099 if (ref->agent.state & CHK_ST_ENABLED)
4100 state = SRV_STATS_STATE_DRAIN_AGENT;
4101 else if (state == SRV_STATS_STATE_UP_GOING_DOWN)
4102 state = SRV_STATS_STATE_DRAIN_GOING_DOWN;
4103 else
4104 state = SRV_STATS_STATE_DRAIN;
4105 }
4106
4107 if (state == SRV_STATS_STATE_UP && !(ref->check.state & CHK_ST_ENABLED)) {
4108 state = SRV_STATS_STATE_NO_CHECK;
Willy Tarreauba2f2642016-01-07 09:54:40 +01004109 }
4110 }
4111 else if (sv->state == SRV_ST_STOPPING) {
4112 if ((!(sv->check.state & CHK_ST_ENABLED) && !sv->track) ||
4113 (ref->check.health == ref->check.rise + ref->check.fall - 1)) {
4114 state = SRV_STATS_STATE_NOLB;
Willy Tarreauba2f2642016-01-07 09:54:40 +01004115 } else {
4116 state = SRV_STATS_STATE_NOLB_GOING_DOWN;
Willy Tarreauba2f2642016-01-07 09:54:40 +01004117 }
4118 }
4119 else { /* stopped */
4120 if ((ref->agent.state & CHK_ST_ENABLED) && !ref->agent.health) {
4121 state = SRV_STATS_STATE_DOWN_AGENT;
Willy Tarreauba2f2642016-01-07 09:54:40 +01004122 } else if ((ref->check.state & CHK_ST_ENABLED) && !ref->check.health) {
4123 state = SRV_STATS_STATE_DOWN; /* DOWN */
Willy Tarreauba2f2642016-01-07 09:54:40 +01004124 } else if ((ref->agent.state & CHK_ST_ENABLED) || (ref->check.state & CHK_ST_ENABLED)) {
4125 state = SRV_STATS_STATE_GOING_UP;
Willy Tarreauba2f2642016-01-07 09:54:40 +01004126 } else {
4127 state = SRV_STATS_STATE_DOWN; /* DOWN, unchecked */
Willy Tarreauba2f2642016-01-07 09:54:40 +01004128 }
4129 }
4130
Willy Tarreau2b96cf12016-01-06 19:25:38 +01004131 chunk_reset(out);
4132 memset(&stats, 0, sizeof(stats));
4133
4134 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
4135 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, sv->id);
Willy Tarreauf8211df2016-01-11 14:09:38 +01004136 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
Willy Tarreau2b96cf12016-01-06 19:25:38 +01004137 stats[ST_F_QCUR] = mkf_u32(0, sv->nbpend);
4138 stats[ST_F_QMAX] = mkf_u32(FN_MAX, sv->counters.nbpend_max);
4139 stats[ST_F_SCUR] = mkf_u32(0, sv->cur_sess);
4140 stats[ST_F_SMAX] = mkf_u32(FN_MAX, sv->counters.cur_sess_max);
4141
4142 if (sv->maxconn)
4143 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, sv->maxconn);
4144
4145 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, sv->counters.cum_sess);
4146 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, sv->counters.bytes_in);
4147 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, sv->counters.bytes_out);
4148 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, sv->counters.failed_secu);
4149 stats[ST_F_ECON] = mkf_u64(FN_COUNTER, sv->counters.failed_conns);
4150 stats[ST_F_ERESP] = mkf_u64(FN_COUNTER, sv->counters.failed_resp);
4151 stats[ST_F_WRETR] = mkf_u64(FN_COUNTER, sv->counters.retries);
4152 stats[ST_F_WREDIS] = mkf_u64(FN_COUNTER, sv->counters.redispatches);
4153
4154 /* status */
4155 fld_status = chunk_newstr(out);
4156 if (sv->admin & SRV_ADMF_IMAINT)
4157 chunk_appendf(out, "MAINT (via %s/%s)", via->proxy->id, via->id);
4158 else if (sv->admin & SRV_ADMF_MAINT)
4159 chunk_appendf(out, "MAINT");
4160 else
4161 chunk_appendf(out,
4162 srv_hlt_st[state],
4163 (ref->state != SRV_ST_STOPPED) ? (ref->check.health - ref->check.rise + 1) : (ref->check.health),
4164 (ref->state != SRV_ST_STOPPED) ? (ref->check.fall) : (ref->check.rise));
4165
4166 stats[ST_F_STATUS] = mkf_str(FO_STATUS, fld_status);
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004167 stats[ST_F_LASTCHG] = mkf_u32(FN_AGE, now.tv_sec - sv->last_change);
4168 stats[ST_F_WEIGHT] = mkf_u32(FN_AVG, (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv);
4169 stats[ST_F_ACT] = mkf_u32(FO_STATUS, (sv->flags & SRV_F_BACKUP) ? 0 : 1);
4170 stats[ST_F_BCK] = mkf_u32(FO_STATUS, (sv->flags & SRV_F_BACKUP) ? 1 : 0);
Willy Tarreau91861262007-10-17 17:06:05 +02004171
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004172 /* check failures: unique, fatal; last change, total downtime */
4173 if (sv->check.state & CHK_ST_ENABLED) {
4174 stats[ST_F_CHKFAIL] = mkf_u64(FN_COUNTER, sv->counters.failed_checks);
4175 stats[ST_F_CHKDOWN] = mkf_u64(FN_COUNTER, sv->counters.down_trans);
4176 stats[ST_F_DOWNTIME] = mkf_u32(FN_COUNTER, srv_downtime(sv));
4177 }
Willy Tarreau164d4a92016-01-06 19:48:21 +01004178
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004179 if (sv->maxqueue)
4180 stats[ST_F_QLIMIT] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->maxqueue);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004181
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004182 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
4183 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
4184 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, sv->puid);
Willy Tarreau91861262007-10-17 17:06:05 +02004185
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004186 if (sv->state == SRV_ST_STARTING && !server_is_draining(sv))
4187 stats[ST_F_THROTTLE] = mkf_u32(FN_AVG, server_throttle_rate(sv));
Willy Tarreauf5b1cc32014-06-17 12:20:59 +02004188
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004189 stats[ST_F_LBTOT] = mkf_u64(FN_COUNTER, sv->counters.cum_lbconn);
Willy Tarreau91861262007-10-17 17:06:05 +02004190
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004191 if (sv->track) {
4192 char *fld_track = chunk_newstr(out);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004193
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004194 chunk_appendf(out, "%s/%s", sv->track->proxy->id, sv->track->id);
4195 stats[ST_F_TRACKED] = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, fld_track);
4196 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004197
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004198 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_SV);
4199 stats[ST_F_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&sv->sess_per_sec));
4200 stats[ST_F_RATE_MAX] = mkf_u32(FN_MAX, sv->counters.sps_max);
Willy Tarreau164d4a92016-01-06 19:48:21 +01004201
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004202 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) {
4203 const char *fld_chksts;
Willy Tarreau164d4a92016-01-06 19:48:21 +01004204
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004205 fld_chksts = chunk_newstr(out);
4206 chunk_strcat(out, "* "); // for check in progress
4207 chunk_strcat(out, get_check_status_info(sv->check.status));
4208 if (!(sv->check.state & CHK_ST_INPROGRESS))
4209 fld_chksts += 2; // skip "* "
4210 stats[ST_F_CHECK_STATUS] = mkf_str(FN_OUTPUT, fld_chksts);
Willy Tarreau91861262007-10-17 17:06:05 +02004211
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004212 if (sv->check.status >= HCHK_STATUS_L57DATA)
4213 stats[ST_F_CHECK_CODE] = mkf_u32(FN_OUTPUT, sv->check.code);
Willy Tarreaucf2924b2014-05-23 12:15:15 +02004214
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004215 if (sv->check.status >= HCHK_STATUS_CHECKED)
4216 stats[ST_F_CHECK_DURATION] = mkf_u64(FN_DURATION, sv->check.duration);
Willy Tarreaucf2924b2014-05-23 12:15:15 +02004217
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004218 stats[ST_F_CHECK_DESC] = mkf_str(FN_OUTPUT, get_check_status_description(sv->check.status));
4219 stats[ST_F_LAST_CHK] = mkf_str(FN_OUTPUT, sv->check.desc);
4220 stats[ST_F_CHECK_RISE] = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.rise);
4221 stats[ST_F_CHECK_FALL] = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.fall);
4222 stats[ST_F_CHECK_HEALTH] = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.health);
4223 }
Willy Tarreaucf2924b2014-05-23 12:15:15 +02004224
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004225 if ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) {
4226 const char *fld_chksts;
Willy Tarreaudd7354b2016-01-08 13:47:26 +01004227
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004228 fld_chksts = chunk_newstr(out);
4229 chunk_strcat(out, "* "); // for check in progress
4230 chunk_strcat(out, get_check_status_info(sv->agent.status));
4231 if (!(sv->agent.state & CHK_ST_INPROGRESS))
4232 fld_chksts += 2; // skip "* "
4233 stats[ST_F_AGENT_STATUS] = mkf_str(FN_OUTPUT, fld_chksts);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004234
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004235 if (sv->agent.status >= HCHK_STATUS_L57DATA)
4236 stats[ST_F_AGENT_CODE] = mkf_u32(FN_OUTPUT, sv->agent.code);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004237
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004238 if (sv->agent.status >= HCHK_STATUS_CHECKED)
4239 stats[ST_F_AGENT_DURATION] = mkf_u64(FN_DURATION, sv->agent.duration);
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01004240
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004241 stats[ST_F_AGENT_DESC] = mkf_str(FN_OUTPUT, get_check_status_description(sv->agent.status));
4242 stats[ST_F_LAST_AGT] = mkf_str(FN_OUTPUT, sv->agent.desc);
4243 stats[ST_F_AGENT_RISE] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.rise);
4244 stats[ST_F_AGENT_FALL] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.fall);
4245 stats[ST_F_AGENT_HEALTH] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.health);
4246 }
Willy Tarreaudd7354b2016-01-08 13:47:26 +01004247
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004248 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
4249 if (px->mode == PR_MODE_HTTP) {
4250 stats[ST_F_HRSP_1XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[1]);
4251 stats[ST_F_HRSP_2XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[2]);
4252 stats[ST_F_HRSP_3XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[3]);
4253 stats[ST_F_HRSP_4XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[4]);
4254 stats[ST_F_HRSP_5XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[5]);
4255 stats[ST_F_HRSP_OTHER] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[0]);
4256 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004257
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004258 if (ref->observe)
4259 stats[ST_F_HANAFAIL] = mkf_u64(FN_COUNTER, sv->counters.failed_hana);
Willy Tarreau164d4a92016-01-06 19:48:21 +01004260
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004261 stats[ST_F_CLI_ABRT] = mkf_u64(FN_COUNTER, sv->counters.cli_aborts);
4262 stats[ST_F_SRV_ABRT] = mkf_u64(FN_COUNTER, sv->counters.srv_aborts);
4263 stats[ST_F_LASTSESS] = mkf_s32(FN_AGE, srv_lastsession(sv));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004264
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004265 stats[ST_F_QTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.q_time, TIME_STATS_SAMPLES));
4266 stats[ST_F_CTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.c_time, TIME_STATS_SAMPLES));
4267 stats[ST_F_RTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.d_time, TIME_STATS_SAMPLES));
4268 stats[ST_F_TTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.t_time, TIME_STATS_SAMPLES));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004269
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004270 if (flags & ST_SHLGNDS) {
4271 switch (addr_to_str(&sv->addr, str, sizeof(str))) {
4272 case AF_INET:
4273 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
4274 chunk_appendf(out, "%s:%d", str, get_host_port(&sv->addr));
4275 break;
4276 case AF_INET6:
4277 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
4278 chunk_appendf(out, "[%s]:%d", str, get_host_port(&sv->addr));
4279 break;
4280 case AF_UNIX:
4281 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, "unix");
4282 break;
4283 case -1:
4284 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
4285 chunk_strcat(out, strerror(errno));
4286 break;
4287 default: /* address family not supported */
4288 break;
Willy Tarreauf4659942013-11-28 10:50:06 +01004289 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004290
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004291 if (sv->cookie)
4292 stats[ST_F_COOKIE] = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, sv->cookie);
4293 }
4294
Willy Tarreau501f6022016-01-08 17:37:22 +01004295 return stats_dump_one_line(stats, flags, px, appctx);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004296}
4297
4298/* Dumps a line for backend <px> to the trash for and uses the state from stream
4299 * interface <si> and stats flags <flags>. The caller is responsible for clearing
4300 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
4301 */
4302static int stats_dump_be_stats(struct stream_interface *si, struct proxy *px, int flags)
4303{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004304 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004305
4306 if (!(px->cap & PR_CAP_BE))
4307 return 0;
4308
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004309 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_BE)))
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004310 return 0;
4311
Willy Tarreauf6eecbe2016-01-06 16:14:50 +01004312 memset(&stats, 0, sizeof(stats));
4313
4314 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
4315 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, "BACKEND");
Willy Tarreauf8211df2016-01-11 14:09:38 +01004316 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
Willy Tarreauf6eecbe2016-01-06 16:14:50 +01004317 stats[ST_F_QCUR] = mkf_u32(0, px->nbpend);
4318 stats[ST_F_QMAX] = mkf_u32(FN_MAX, px->be_counters.nbpend_max);
4319 stats[ST_F_SCUR] = mkf_u32(FO_CONFIG|FN_LIMIT, px->beconn);
4320 stats[ST_F_SMAX] = mkf_u32(FN_MAX, px->be_counters.conn_max);
4321 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, px->fullconn);
4322 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, px->be_counters.cum_conn);
4323 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, px->be_counters.bytes_in);
4324 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, px->be_counters.bytes_out);
4325 stats[ST_F_DREQ] = mkf_u64(FN_COUNTER, px->be_counters.denied_req);
4326 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, px->be_counters.denied_resp);
4327 stats[ST_F_ECON] = mkf_u64(FN_COUNTER, px->be_counters.failed_conns);
4328 stats[ST_F_ERESP] = mkf_u64(FN_COUNTER, px->be_counters.failed_resp);
4329 stats[ST_F_WRETR] = mkf_u64(FN_COUNTER, px->be_counters.retries);
4330 stats[ST_F_WREDIS] = mkf_u64(FN_COUNTER, px->be_counters.redispatches);
4331 stats[ST_F_STATUS] = mkf_str(FO_STATUS, (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" : "DOWN");
4332 stats[ST_F_WEIGHT] = mkf_u32(FN_AVG, (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv);
4333 stats[ST_F_ACT] = mkf_u32(0, px->srv_act);
4334 stats[ST_F_BCK] = mkf_u32(0, px->srv_bck);
4335 stats[ST_F_CHKDOWN] = mkf_u64(FN_COUNTER, px->down_trans);
4336 stats[ST_F_LASTCHG] = mkf_u32(FN_AGE, now.tv_sec - px->last_change);
Willy Tarreau7344f472016-01-11 12:04:02 +01004337 if (px->srv)
4338 stats[ST_F_DOWNTIME] = mkf_u32(FN_COUNTER, be_downtime(px));
4339
Willy Tarreauf6eecbe2016-01-06 16:14:50 +01004340 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
4341 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
4342 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, 0);
4343 stats[ST_F_LBTOT] = mkf_u64(FN_COUNTER, px->be_counters.cum_lbconn);
4344 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_BE);
4345 stats[ST_F_RATE] = mkf_u32(0, read_freq_ctr(&px->be_sess_per_sec));
4346 stats[ST_F_RATE_MAX] = mkf_u32(0, px->be_counters.sps_max);
Willy Tarreauf1516d92016-01-11 14:48:36 +01004347
Willy Tarreaue4847c62016-01-08 15:43:54 +01004348 if (flags & ST_SHLGNDS) {
4349 if (px->cookie_name)
4350 stats[ST_F_COOKIE] = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, px->cookie_name);
Willy Tarreauf1516d92016-01-11 14:48:36 +01004351 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 +01004352 }
Willy Tarreauf6eecbe2016-01-06 16:14:50 +01004353
4354 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
4355 if (px->mode == PR_MODE_HTTP) {
4356 stats[ST_F_REQ_TOT] = mkf_u64(FN_COUNTER, px->be_counters.p.http.cum_req);
4357 stats[ST_F_HRSP_1XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[1]);
4358 stats[ST_F_HRSP_2XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[2]);
4359 stats[ST_F_HRSP_3XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[3]);
4360 stats[ST_F_HRSP_4XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[4]);
4361 stats[ST_F_HRSP_5XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[5]);
4362 stats[ST_F_HRSP_OTHER] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[0]);
Willy Tarreau5b9bdff2016-01-11 14:40:47 +01004363 stats[ST_F_INTERCEPTED] = mkf_u64(FN_COUNTER, px->be_counters.intercepted_req);
Willy Tarreauf6eecbe2016-01-06 16:14:50 +01004364 }
4365
4366 stats[ST_F_CLI_ABRT] = mkf_u64(FN_COUNTER, px->be_counters.cli_aborts);
4367 stats[ST_F_SRV_ABRT] = mkf_u64(FN_COUNTER, px->be_counters.srv_aborts);
4368
4369 /* compression: in, out, bypassed, responses */
4370 stats[ST_F_COMP_IN] = mkf_u64(FN_COUNTER, px->be_counters.comp_in);
4371 stats[ST_F_COMP_OUT] = mkf_u64(FN_COUNTER, px->be_counters.comp_out);
4372 stats[ST_F_COMP_BYP] = mkf_u64(FN_COUNTER, px->be_counters.comp_byp);
4373 stats[ST_F_COMP_RSP] = mkf_u64(FN_COUNTER, px->be_counters.p.http.comp_rsp);
4374 stats[ST_F_LASTSESS] = mkf_s32(FN_AGE, be_lastsession(px));
4375
4376 stats[ST_F_QTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.q_time, TIME_STATS_SAMPLES));
4377 stats[ST_F_CTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.c_time, TIME_STATS_SAMPLES));
4378 stats[ST_F_RTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.d_time, TIME_STATS_SAMPLES));
4379 stats[ST_F_TTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.t_time, TIME_STATS_SAMPLES));
4380
Willy Tarreau501f6022016-01-08 17:37:22 +01004381 return stats_dump_one_line(stats, flags, px, appctx);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004382}
4383
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004384/* Dumps the HTML table header for proxy <px> to the trash for and uses the state from
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004385 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004386 * for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004387 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004388static void stats_dump_html_px_hdr(struct stream_interface *si, struct proxy *px, struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004389{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004390 struct appctx *appctx = __objt_appctx(si->end);
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004391 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
4392
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004393 if (px->cap & PR_CAP_BE && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004394 /* A form to enable/disable this proxy servers */
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004395
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004396 /* 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 +02004397 scope_txt[0] = 0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004398 if (appctx->ctx.stats.scope_len) {
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004399 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau4e4292b2014-11-28 12:18:45 +01004400 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 +01004401 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004402 }
4403
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004404 chunk_appendf(&trash,
Jeff Buchbinder2dbbf4d2014-08-29 15:10:08 -05004405 "<form method=\"post\">");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004406 }
4407
4408 /* print a new table */
4409 chunk_appendf(&trash,
4410 "<table class=\"tbl\" width=\"100%%\">\n"
4411 "<tr class=\"titre\">"
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01004412 "<th class=\"pxname\" width=\"10%%\">");
4413
4414 chunk_appendf(&trash,
4415 "<a name=\"%s\"></a>%s"
4416 "<a class=px href=\"#%s\">%s</a>",
4417 px->id,
4418 (uri->flags & ST_SHLGNDS) ? "<u>":"",
4419 px->id, px->id);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004420
4421 if (uri->flags & ST_SHLGNDS) {
4422 /* cap, mode, id */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02004423 chunk_appendf(&trash, "<div class=tips>cap: %s, mode: %s, id: %d",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004424 proxy_cap_str(px->cap), proxy_mode_str(px->mode),
4425 px->uuid);
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01004426 chunk_appendf(&trash, "</div>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004427 }
4428
4429 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01004430 "%s</th>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004431 "<th class=\"%s\" width=\"90%%\">%s</th>"
4432 "</tr>\n"
4433 "</table>\n"
4434 "<table class=\"tbl\" width=\"100%%\">\n"
4435 "<tr class=\"titre\">",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004436 (uri->flags & ST_SHLGNDS) ? "</u>":"",
4437 px->desc ? "desc" : "empty", px->desc ? px->desc : "");
4438
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004439 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004440 /* Column heading for Enable or Disable server */
4441 chunk_appendf(&trash, "<th rowspan=2 width=1></th>");
Willy Tarreau91861262007-10-17 17:06:05 +02004442 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004443
4444 chunk_appendf(&trash,
4445 "<th rowspan=2></th>"
4446 "<th colspan=3>Queue</th>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05004447 "<th colspan=3>Session rate</th><th colspan=6>Sessions</th>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004448 "<th colspan=2>Bytes</th><th colspan=2>Denied</th>"
4449 "<th colspan=3>Errors</th><th colspan=2>Warnings</th>"
4450 "<th colspan=9>Server</th>"
4451 "</tr>\n"
4452 "<tr class=\"titre\">"
4453 "<th>Cur</th><th>Max</th><th>Limit</th>"
4454 "<th>Cur</th><th>Max</th><th>Limit</th><th>Cur</th><th>Max</th>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05004455 "<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 +01004456 "<th>Req</th><th>Resp</th><th>Req</th><th>Conn</th>"
4457 "<th>Resp</th><th>Retr</th><th>Redis</th>"
4458 "<th>Status</th><th>LastChk</th><th>Wght</th><th>Act</th>"
4459 "<th>Bck</th><th>Chk</th><th>Dwn</th><th>Dwntme</th>"
4460 "<th>Thrtle</th>\n"
4461 "</tr>");
Willy Tarreau91861262007-10-17 17:06:05 +02004462}
4463
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004464/* Dumps the HTML table trailer for proxy <px> to the trash for and uses the state from
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004465 * stream interface <si>. The caller is responsible for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004466 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004467static void stats_dump_html_px_end(struct stream_interface *si, struct proxy *px)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004468{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004469 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004470 chunk_appendf(&trash, "</table>");
4471
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004472 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004473 /* close the form used to enable/disable this proxy servers */
4474 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004475 "Choose the action to perform on the checked servers : "
4476 "<select name=action>"
4477 "<option value=\"\"></option>"
Willy Tarreaued7df902014-05-22 18:04:49 +02004478 "<option value=\"ready\">Set state to READY</option>"
4479 "<option value=\"drain\">Set state to DRAIN</option>"
Marco Corte8c27bca2014-07-02 17:49:34 +02004480 "<option value=\"maint\">Set state to MAINT</option>"
Willy Tarreau248a60e2014-05-23 14:59:48 +02004481 "<option value=\"dhlth\">Health: disable checks</option>"
4482 "<option value=\"ehlth\">Health: enable checks</option>"
4483 "<option value=\"hrunn\">Health: force UP</option>"
4484 "<option value=\"hnolb\">Health: force NOLB</option>"
4485 "<option value=\"hdown\">Health: force DOWN</option>"
4486 "<option value=\"dagent\">Agent: disable checks</option>"
4487 "<option value=\"eagent\">Agent: enable checks</option>"
4488 "<option value=\"arunn\">Agent: force UP</option>"
4489 "<option value=\"adown\">Agent: force DOWN</option>"
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004490 "<option value=\"shutdown\">Kill Sessions</option>"
4491 "</select>"
4492 "<input type=\"hidden\" name=\"b\" value=\"#%d\">"
4493 "&nbsp;<input type=\"submit\" value=\"Apply\">"
4494 "</form>",
4495 px->uuid);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004496 }
4497
4498 chunk_appendf(&trash, "<p>\n");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004499}
Willy Tarreau91861262007-10-17 17:06:05 +02004500
4501/*
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004502 * Dumps statistics for a proxy. The output is sent to the stream interface's
4503 * input buffer. Returns 0 if it had to stop dumping data because of lack of
4504 * buffer space, or non-zero if everything completed. This function is used
4505 * both by the CLI and the HTTP entry points, and is able to dump the output
4506 * in HTML or CSV formats. If the later, <uri> must be NULL.
Willy Tarreau91861262007-10-17 17:06:05 +02004507 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004508static int stats_dump_proxy_to_buffer(struct stream_interface *si, struct proxy *px, struct uri_auth *uri)
Willy Tarreau91861262007-10-17 17:06:05 +02004509{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004510 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau87b09662015-04-03 00:22:06 +02004511 struct stream *s = si_strm(si);
Willy Tarreau2bb4a962014-11-28 11:11:05 +01004512 struct channel *rep = si_ic(si);
Willy Tarreau44267702011-10-28 15:35:33 +02004513 struct server *sv, *svs; /* server and server-state, server-state=server or server->track */
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004514 struct listener *l;
Willy Tarreau0deb85a2016-01-08 15:33:18 +01004515 unsigned int flags;
4516
4517 if (uri)
4518 flags = uri->flags;
4519 else if (strm_li(s)->bind_conf->level >= ACCESS_LVL_OPER)
4520 flags = ST_SHLGNDS | ST_SHNODE | ST_SHDESC;
4521 else
4522 flags = ST_SHNODE | ST_SHDESC;
Willy Tarreau91861262007-10-17 17:06:05 +02004523
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004524 chunk_reset(&trash);
Willy Tarreau91861262007-10-17 17:06:05 +02004525
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004526 switch (appctx->ctx.stats.px_st) {
Willy Tarreau295a8372011-03-10 11:25:07 +01004527 case STAT_PX_ST_INIT:
Willy Tarreau91861262007-10-17 17:06:05 +02004528 /* we are on a new proxy */
Willy Tarreau91861262007-10-17 17:06:05 +02004529 if (uri && uri->scope) {
4530 /* we have a limited scope, we have to check the proxy name */
4531 struct stat_scope *scope;
4532 int len;
4533
4534 len = strlen(px->id);
4535 scope = uri->scope;
4536
4537 while (scope) {
4538 /* match exact proxy name */
4539 if (scope->px_len == len && !memcmp(px->id, scope->px_id, len))
4540 break;
4541
4542 /* match '.' which means 'self' proxy */
Willy Tarreau1388a3a2007-10-18 16:38:37 +02004543 if (!strcmp(scope->px_id, ".") && px == s->be)
Willy Tarreau91861262007-10-17 17:06:05 +02004544 break;
4545 scope = scope->next;
4546 }
4547
4548 /* proxy name not found : don't dump anything */
4549 if (scope == NULL)
4550 return 1;
4551 }
4552
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004553 /* if the user has requested a limited output and the proxy
4554 * name does not match, skip it.
4555 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004556 if (appctx->ctx.stats.scope_len &&
Willy Tarreau4e4292b2014-11-28 12:18:45 +01004557 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 +02004558 return 1;
4559
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004560 if ((appctx->ctx.stats.flags & STAT_BOUND) &&
4561 (appctx->ctx.stats.iid != -1) &&
4562 (px->uuid != appctx->ctx.stats.iid))
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01004563 return 1;
4564
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004565 appctx->ctx.stats.px_st = STAT_PX_ST_TH;
Willy Tarreau91861262007-10-17 17:06:05 +02004566 /* fall through */
4567
Willy Tarreau295a8372011-03-10 11:25:07 +01004568 case STAT_PX_ST_TH:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004569 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004570 stats_dump_html_px_hdr(si, px, uri);
Willy Tarreaubc18da12015-03-13 14:00:47 +01004571 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004572 si_applet_cant_put(si);
Willy Tarreau55bb8452007-10-17 18:44:57 +02004573 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01004574 }
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004575 }
Willy Tarreau91861262007-10-17 17:06:05 +02004576
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004577 appctx->ctx.stats.px_st = STAT_PX_ST_FE;
Willy Tarreau91861262007-10-17 17:06:05 +02004578 /* fall through */
4579
Willy Tarreau295a8372011-03-10 11:25:07 +01004580 case STAT_PX_ST_FE:
Willy Tarreau91861262007-10-17 17:06:05 +02004581 /* print the frontend */
Willy Tarreaubc18da12015-03-13 14:00:47 +01004582 if (stats_dump_fe_stats(si, px)) {
4583 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004584 si_applet_cant_put(si);
Willy Tarreau91861262007-10-17 17:06:05 +02004585 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01004586 }
4587 }
Willy Tarreau91861262007-10-17 17:06:05 +02004588
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004589 appctx->ctx.stats.l = px->conf.listeners.n;
4590 appctx->ctx.stats.px_st = STAT_PX_ST_LI;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004591 /* fall through */
4592
Willy Tarreau295a8372011-03-10 11:25:07 +01004593 case STAT_PX_ST_LI:
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004594 /* stats.l has been initialized above */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004595 for (; appctx->ctx.stats.l != &px->conf.listeners; appctx->ctx.stats.l = l->by_fe.n) {
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01004596 if (buffer_almost_full(rep->buf)) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004597 si_applet_cant_put(si);
Willy Tarreau4e33d862009-10-11 23:35:10 +02004598 return 0;
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01004599 }
Willy Tarreau4e33d862009-10-11 23:35:10 +02004600
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004601 l = LIST_ELEM(appctx->ctx.stats.l, struct listener *, by_fe);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004602 if (!l->counters)
4603 continue;
4604
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004605 if (appctx->ctx.stats.flags & STAT_BOUND) {
4606 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SO)))
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004607 break;
4608
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004609 if (appctx->ctx.stats.sid != -1 && l->luid != appctx->ctx.stats.sid)
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004610 continue;
4611 }
4612
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004613 /* print the frontend */
Willy Tarreau0deb85a2016-01-08 15:33:18 +01004614 if (stats_dump_li_stats(si, px, l, flags)) {
Willy Tarreaubc18da12015-03-13 14:00:47 +01004615 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004616 si_applet_cant_put(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004617 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01004618 }
4619 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004620 }
4621
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004622 appctx->ctx.stats.sv = px->srv; /* may be NULL */
4623 appctx->ctx.stats.px_st = STAT_PX_ST_SV;
Willy Tarreau91861262007-10-17 17:06:05 +02004624 /* fall through */
4625
Willy Tarreau295a8372011-03-10 11:25:07 +01004626 case STAT_PX_ST_SV:
Willy Tarreau91861262007-10-17 17:06:05 +02004627 /* stats.sv has been initialized above */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004628 for (; appctx->ctx.stats.sv != NULL; appctx->ctx.stats.sv = sv->next) {
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01004629 if (buffer_almost_full(rep->buf)) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004630 si_applet_cant_put(si);
Willy Tarreau4e33d862009-10-11 23:35:10 +02004631 return 0;
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01004632 }
Willy Tarreau4e33d862009-10-11 23:35:10 +02004633
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004634 sv = appctx->ctx.stats.sv;
Willy Tarreau91861262007-10-17 17:06:05 +02004635
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004636 if (appctx->ctx.stats.flags & STAT_BOUND) {
4637 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SV)))
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01004638 break;
4639
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004640 if (appctx->ctx.stats.sid != -1 && sv->puid != appctx->ctx.stats.sid)
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01004641 continue;
4642 }
4643
Willy Tarreau32091232014-05-16 13:52:00 +02004644 svs = sv;
4645 while (svs->track)
4646 svs = svs->track;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01004647
Willy Tarreauba2f2642016-01-07 09:54:40 +01004648 /* do not report servers which are DOWN and not changing state */
4649 if ((appctx->ctx.stats.flags & STAT_HIDE_DOWN) &&
4650 ((sv->admin & SRV_ADMF_MAINT) || /* server is in maintenance */
4651 (sv->state == SRV_ST_STOPPED && /* server is down */
4652 (!((svs->agent.state | svs->check.state) & CHK_ST_ENABLED) ||
4653 ((svs->agent.state & CHK_ST_ENABLED) && !svs->agent.health) ||
4654 ((svs->check.state & CHK_ST_ENABLED) && !svs->check.health))))) {
Willy Tarreau91861262007-10-17 17:06:05 +02004655 continue;
4656 }
4657
Willy Tarreau0deb85a2016-01-08 15:33:18 +01004658 if (stats_dump_sv_stats(si, px, flags, sv)) {
Willy Tarreaubc18da12015-03-13 14:00:47 +01004659 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004660 si_applet_cant_put(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004661 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01004662 }
4663 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004664 } /* for sv */
Cyril Bonté474be412010-10-12 00:14:36 +02004665
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004666 appctx->ctx.stats.px_st = STAT_PX_ST_BE;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004667 /* fall through */
Cyril Bonté70be45d2010-10-12 00:14:35 +02004668
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004669 case STAT_PX_ST_BE:
4670 /* print the backend */
Willy Tarreau0deb85a2016-01-08 15:33:18 +01004671 if (stats_dump_be_stats(si, px, flags)) {
Willy Tarreaubc18da12015-03-13 14:00:47 +01004672 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004673 si_applet_cant_put(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004674 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01004675 }
4676 }
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01004677
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004678 appctx->ctx.stats.px_st = STAT_PX_ST_END;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004679 /* fall through */
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01004680
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004681 case STAT_PX_ST_END:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004682 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004683 stats_dump_html_px_end(si, px);
Willy Tarreaubc18da12015-03-13 14:00:47 +01004684 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004685 si_applet_cant_put(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004686 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01004687 }
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004688 }
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01004689
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004690 appctx->ctx.stats.px_st = STAT_PX_ST_FIN;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004691 /* fall through */
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01004692
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004693 case STAT_PX_ST_FIN:
4694 return 1;
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01004695
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004696 default:
4697 /* unknown state, we should put an abort() here ! */
4698 return 1;
4699 }
4700}
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01004701
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004702/* Dumps the HTTP stats head block to the trash for and uses the per-uri
4703 * parameters <uri>. The caller is responsible for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004704 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004705static void stats_dump_html_head(struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004706{
4707 /* WARNING! This must fit in the first buffer !!! */
4708 chunk_appendf(&trash,
4709 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n"
4710 "\"http://www.w3.org/TR/html4/loose.dtd\">\n"
4711 "<html><head><title>Statistics Report for " PRODUCT_NAME "%s%s</title>\n"
4712 "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n"
4713 "<style type=\"text/css\"><!--\n"
4714 "body {"
4715 " font-family: arial, helvetica, sans-serif;"
4716 " font-size: 12px;"
4717 " font-weight: normal;"
4718 " color: black;"
4719 " background: white;"
4720 "}\n"
4721 "th,td {"
4722 " font-size: 10px;"
4723 "}\n"
4724 "h1 {"
4725 " font-size: x-large;"
4726 " margin-bottom: 0.5em;"
4727 "}\n"
4728 "h2 {"
4729 " font-family: helvetica, arial;"
4730 " font-size: x-large;"
4731 " font-weight: bold;"
4732 " font-style: italic;"
4733 " color: #6020a0;"
4734 " margin-top: 0em;"
4735 " margin-bottom: 0em;"
4736 "}\n"
4737 "h3 {"
4738 " font-family: helvetica, arial;"
4739 " font-size: 16px;"
4740 " font-weight: bold;"
4741 " color: #b00040;"
4742 " background: #e8e8d0;"
4743 " margin-top: 0em;"
4744 " margin-bottom: 0em;"
4745 "}\n"
4746 "li {"
4747 " margin-top: 0.25em;"
4748 " margin-right: 2em;"
4749 "}\n"
4750 ".hr {margin-top: 0.25em;"
4751 " border-color: black;"
4752 " border-bottom-style: solid;"
4753 "}\n"
4754 ".titre {background: #20D0D0;color: #000000; font-weight: bold; text-align: center;}\n"
4755 ".total {background: #20D0D0;color: #ffff80;}\n"
4756 ".frontend {background: #e8e8d0;}\n"
4757 ".socket {background: #d0d0d0;}\n"
4758 ".backend {background: #e8e8d0;}\n"
Simon Horman837bfa72015-04-23 14:51:27 +09004759 ".active_down {background: #ff9090;}\n"
4760 ".active_going_up {background: #ffd020;}\n"
4761 ".active_going_down {background: #ffffa0;}\n"
4762 ".active_up {background: #c0ffc0;}\n"
4763 ".active_nolb {background: #20a0ff;}\n"
4764 ".active_draining {background: #20a0FF;}\n"
4765 ".active_no_check {background: #e0e0e0;}\n"
4766 ".backup_down {background: #ff9090;}\n"
4767 ".backup_going_up {background: #ff80ff;}\n"
4768 ".backup_going_down {background: #c060ff;}\n"
4769 ".backup_up {background: #b0d0ff;}\n"
4770 ".backup_nolb {background: #90b0e0;}\n"
4771 ".backup_draining {background: #cc9900;}\n"
4772 ".backup_no_check {background: #e0e0e0;}\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004773 ".maintain {background: #c07820;}\n"
4774 ".rls {letter-spacing: 0.2em; margin-right: 1px;}\n" /* right letter spacing (used for grouping digits) */
4775 "\n"
4776 "a.px:link {color: #ffff40; text-decoration: none;}"
4777 "a.px:visited {color: #ffff40; text-decoration: none;}"
4778 "a.px:hover {color: #ffffff; text-decoration: none;}"
4779 "a.lfsb:link {color: #000000; text-decoration: none;}"
4780 "a.lfsb:visited {color: #000000; text-decoration: none;}"
4781 "a.lfsb:hover {color: #505050; text-decoration: none;}"
4782 "\n"
4783 "table.tbl { border-collapse: collapse; border-style: none;}\n"
4784 "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"
4785 "table.tbl td.ac { text-align: center;}\n"
4786 "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}\n"
4787 "table.tbl th.pxname { background: #b00040; color: #ffff40; font-weight: bold; border-style: solid solid none solid; padding: 2px 3px; white-space: nowrap;}\n"
4788 "table.tbl th.empty { border-style: none; empty-cells: hide; background: white;}\n"
4789 "table.tbl th.desc { background: white; border-style: solid solid none solid; text-align: left; padding: 2px 3px;}\n"
4790 "\n"
4791 "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n"
4792 "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n"
4793 "table.lgd td.noborder { border-style: none; padding: 2px; white-space: nowrap;}\n"
Willy Tarreau466c9b52012-12-23 02:25:03 +01004794 "table.det { border-collapse: collapse; border-style: none; }\n"
4795 "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 +01004796 "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 +01004797 "u {text-decoration:none; border-bottom: 1px dotted black;}\n"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02004798 "div.tips {\n"
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01004799 " display:block;\n"
4800 " visibility:hidden;\n"
4801 " z-index:2147483647;\n"
4802 " position:absolute;\n"
4803 " padding:2px 4px 3px;\n"
4804 " background:#f0f060; color:#000000;\n"
4805 " border:1px solid #7040c0;\n"
4806 " white-space:nowrap;\n"
4807 " font-style:normal;font-size:11px;font-weight:normal;\n"
4808 " -moz-border-radius:3px;-webkit-border-radius:3px;border-radius:3px;\n"
4809 " -moz-box-shadow:gray 2px 2px 3px;-webkit-box-shadow:gray 2px 2px 3px;box-shadow:gray 2px 2px 3px;\n"
4810 "}\n"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02004811 "u:hover div.tips {visibility:visible;}\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004812 "-->\n"
4813 "</style></head>\n",
4814 (uri->flags & ST_SHNODE) ? " on " : "",
4815 (uri->flags & ST_SHNODE) ? (uri->node ? uri->node : global.node) : ""
4816 );
4817}
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01004818
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004819/* Dumps the HTML stats information block to the trash for and uses the state from
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004820 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004821 * for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004822 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004823static void stats_dump_html_info(struct stream_interface *si, struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004824{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004825 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004826 unsigned int up = (now.tv_sec - start_date.tv_sec);
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004827 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01004828
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004829 /* WARNING! this has to fit the first packet too.
4830 * We are around 3.5 kB, add adding entries will
4831 * become tricky if we want to support 4kB buffers !
4832 */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004833 chunk_appendf(&trash,
4834 "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">"
4835 PRODUCT_NAME "%s</a></h1>\n"
4836 "<h2>Statistics Report for pid %d%s%s%s%s</h2>\n"
4837 "<hr width=\"100%%\" class=\"hr\">\n"
4838 "<h3>&gt; General process information</h3>\n"
4839 "<table border=0><tr><td align=\"left\" nowrap width=\"1%%\">\n"
4840 "<p><b>pid = </b> %d (process #%d, nbproc = %d)<br>\n"
4841 "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n"
4842 "<b>system limits:</b> memmax = %s%s; ulimit-n = %d<br>\n"
4843 "<b>maxsock = </b> %d; <b>maxconn = </b> %d; <b>maxpipes = </b> %d<br>\n"
4844 "current conns = %d; current pipes = %d/%d; conn rate = %d/sec<br>\n"
4845 "Running tasks: %d/%d; idle = %d %%<br>\n"
4846 "</td><td align=\"center\" nowrap>\n"
4847 "<table class=\"lgd\"><tr>\n"
Simon Horman837bfa72015-04-23 14:51:27 +09004848 "<td class=\"active_up\">&nbsp;</td><td class=\"noborder\">active UP </td>"
4849 "<td class=\"backup_up\">&nbsp;</td><td class=\"noborder\">backup UP </td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004850 "</tr><tr>\n"
Simon Horman837bfa72015-04-23 14:51:27 +09004851 "<td class=\"active_going_down\"></td><td class=\"noborder\">active UP, going down </td>"
4852 "<td class=\"backup_going_down\"></td><td class=\"noborder\">backup UP, going down </td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004853 "</tr><tr>\n"
Simon Horman837bfa72015-04-23 14:51:27 +09004854 "<td class=\"active_going_up\"></td><td class=\"noborder\">active DOWN, going up </td>"
4855 "<td class=\"backup_going_up\"></td><td class=\"noborder\">backup DOWN, going up </td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004856 "</tr><tr>\n"
Simon Horman837bfa72015-04-23 14:51:27 +09004857 "<td class=\"active_down\"></td><td class=\"noborder\">active or backup DOWN &nbsp;</td>"
4858 "<td class=\"active_no_check\"></td><td class=\"noborder\">not checked </td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004859 "</tr><tr>\n"
4860 "<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 -07004861 "</tr><tr>\n"
Simon Horman837bfa72015-04-23 14:51:27 +09004862 "<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 +01004863 "</tr></table>\n"
Willy Tarreau6b7764a2013-12-04 00:43:21 +01004864 "Note: \"NOLB\"/\"DRAIN\" = UP with load-balancing disabled."
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004865 "</td>"
4866 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
4867 "<b>Display option:</b><ul style=\"margin-top: 0.25em;\">"
4868 "",
4869 (uri->flags & ST_HIDEVER) ? "" : (STATS_VERSION_STRING),
4870 pid, (uri->flags & ST_SHNODE) ? " on " : "",
4871 (uri->flags & ST_SHNODE) ? (uri->node ? uri->node : global.node) : "",
4872 (uri->flags & ST_SHDESC) ? ": " : "",
4873 (uri->flags & ST_SHDESC) ? (uri->desc ? uri->desc : global.desc) : "",
4874 pid, relative_pid, global.nbproc,
4875 up / 86400, (up % 86400) / 3600,
4876 (up % 3600) / 60, (up % 60),
4877 global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited",
4878 global.rlimit_memmax ? " MB" : "",
4879 global.rlimit_nofile,
4880 global.maxsock, global.maxconn, global.maxpipes,
4881 actconn, pipes_used, pipes_used+pipes_free, read_freq_ctr(&global.conn_per_sec),
4882 run_queue_cur, nb_tasks_cur, idle_pct
4883 );
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004884
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004885 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Willy Tarreau4e4292b2014-11-28 12:18:45 +01004886 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 +01004887 scope_txt[appctx->ctx.stats.scope_len] = '\0';
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004888
4889 chunk_appendf(&trash,
Jeff Buchbinder2dbbf4d2014-08-29 15:10:08 -05004890 "<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 +01004891 (appctx->ctx.stats.scope_len > 0) ? scope_txt : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004892 STAT_SCOPE_TXT_MAXLEN);
4893
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004894 /* 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 +02004895 scope_txt[0] = 0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004896 if (appctx->ctx.stats.scope_len) {
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004897 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau4e4292b2014-11-28 12:18:45 +01004898 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 +01004899 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004900 }
4901
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004902 if (appctx->ctx.stats.flags & STAT_HIDE_DOWN)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004903 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004904 "<li><a href=\"%s%s%s%s\">Show all servers</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004905 uri->uri_prefix,
4906 "",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004907 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004908 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004909 else
4910 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004911 "<li><a href=\"%s%s%s%s\">Hide 'DOWN' servers</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004912 uri->uri_prefix,
4913 ";up",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004914 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004915 scope_txt);
Willy Tarreau91861262007-10-17 17:06:05 +02004916
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004917 if (uri->refresh > 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004918 if (appctx->ctx.stats.flags & STAT_NO_REFRESH)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004919 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004920 "<li><a href=\"%s%s%s%s\">Enable refresh</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004921 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004922 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004923 "",
4924 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004925 else
4926 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004927 "<li><a href=\"%s%s%s%s\">Disable refresh</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004928 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004929 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004930 ";norefresh",
4931 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004932 }
Willy Tarreau55bb8452007-10-17 18:44:57 +02004933
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004934 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004935 "<li><a href=\"%s%s%s%s\">Refresh now</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004936 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004937 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4938 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004939 scope_txt);
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02004940
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004941 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004942 "<li><a href=\"%s;csv%s%s\">CSV export</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004943 uri->uri_prefix,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004944 (uri->refresh > 0) ? ";norefresh" : "",
4945 scope_txt);
Willy Tarreau4bab24d2007-11-30 18:16:29 +01004946
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004947 chunk_appendf(&trash,
4948 "</ul></td>"
4949 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
4950 "<b>External resources:</b><ul style=\"margin-top: 0.25em;\">\n"
4951 "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>\n"
4952 "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>\n"
4953 "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>\n"
4954 "</ul>"
4955 "</td>"
4956 "</tr></table>\n"
4957 ""
4958 );
Willy Tarreau4bab24d2007-11-30 18:16:29 +01004959
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004960 if (appctx->ctx.stats.st_code) {
4961 switch (appctx->ctx.stats.st_code) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004962 case STAT_STATUS_DONE:
4963 chunk_appendf(&trash,
Simon Horman837bfa72015-04-23 14:51:27 +09004964 "<p><div class=active_up>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004965 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004966 "Action processed successfully."
Willy Tarreauba6be982013-04-19 12:16:55 +02004967 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004968 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4969 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02004970 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004971 break;
4972 case STAT_STATUS_NONE:
4973 chunk_appendf(&trash,
Simon Horman837bfa72015-04-23 14:51:27 +09004974 "<p><div class=active_going_down>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004975 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004976 "Nothing has changed."
Willy Tarreauba6be982013-04-19 12:16:55 +02004977 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004978 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4979 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02004980 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004981 break;
4982 case STAT_STATUS_PART:
4983 chunk_appendf(&trash,
Simon Horman837bfa72015-04-23 14:51:27 +09004984 "<p><div class=active_going_down>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004985 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004986 "Action partially processed.<br>"
4987 "Some server names are probably unknown or ambiguous (duplicated names in the backend)."
Willy Tarreauba6be982013-04-19 12:16:55 +02004988 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004989 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4990 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02004991 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004992 break;
4993 case STAT_STATUS_ERRP:
4994 chunk_appendf(&trash,
Simon Horman837bfa72015-04-23 14:51:27 +09004995 "<p><div class=active_down>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004996 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004997 "Action not processed because of invalid parameters."
4998 "<ul>"
4999 "<li>The action is maybe unknown.</li>"
5000 "<li>The backend name is probably unknown or ambiguous (duplicated names).</li>"
5001 "<li>Some server names are probably unknown or ambiguous (duplicated names in the backend).</li>"
5002 "</ul>"
Willy Tarreauba6be982013-04-19 12:16:55 +02005003 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005004 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
5005 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02005006 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005007 break;
5008 case STAT_STATUS_EXCD:
5009 chunk_appendf(&trash,
Simon Horman837bfa72015-04-23 14:51:27 +09005010 "<p><div class=active_down>"
Willy Tarreauba6be982013-04-19 12:16:55 +02005011 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005012 "<b>Action not processed : the buffer couldn't store all the data.<br>"
5013 "You should retry with less servers at a time.</b>"
Willy Tarreauba6be982013-04-19 12:16:55 +02005014 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005015 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
5016 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02005017 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005018 break;
5019 case STAT_STATUS_DENY:
5020 chunk_appendf(&trash,
Simon Horman837bfa72015-04-23 14:51:27 +09005021 "<p><div class=active_down>"
Willy Tarreauba6be982013-04-19 12:16:55 +02005022 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005023 "<b>Action denied.</b>"
Willy Tarreauba6be982013-04-19 12:16:55 +02005024 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005025 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
5026 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02005027 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005028 break;
5029 default:
5030 chunk_appendf(&trash,
Simon Horman837bfa72015-04-23 14:51:27 +09005031 "<p><div class=active_no_check>"
Willy Tarreauba6be982013-04-19 12:16:55 +02005032 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005033 "Unexpected result."
Willy Tarreauba6be982013-04-19 12:16:55 +02005034 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005035 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
5036 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02005037 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005038 }
5039 chunk_appendf(&trash, "<p>\n");
5040 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005041}
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01005042
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01005043/* Dumps the HTML stats trailer block to the trash. The caller is responsible
5044 * for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005045 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01005046static void stats_dump_html_end()
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005047{
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005048 chunk_appendf(&trash, "</body></html>\n");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005049}
Willy Tarreau7f062c42009-03-05 18:43:00 +01005050
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01005051/* This function dumps statistics onto the stream interface's read buffer in
5052 * either CSV or HTML format. <uri> contains some HTML-specific parameters that
Willy Tarreau306f8302013-07-08 15:53:06 +02005053 * are ignored for CSV format (hence <uri> may be NULL there). It returns 0 if
5054 * 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 +02005055 * and the stream must be closed, or -1 in case of any error. This function is
Willy Tarreau306f8302013-07-08 15:53:06 +02005056 * used by both the CLI and the HTTP handlers.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005057 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01005058static int stats_dump_stat_to_buffer(struct stream_interface *si, struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005059{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005060 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005061 struct channel *rep = si_ic(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005062 struct proxy *px;
Willy Tarreau7f062c42009-03-05 18:43:00 +01005063
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005064 chunk_reset(&trash);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02005065
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005066 switch (appctx->st2) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005067 case STAT_ST_INIT:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005068 appctx->st2 = STAT_ST_HEAD; /* let's start producing data */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005069 /* fall through */
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005070
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005071 case STAT_ST_HEAD:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005072 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01005073 stats_dump_html_head(uri);
Willy Tarreau354898b2012-12-23 18:15:23 +01005074 else
5075 stats_dump_csv_header();
Willy Tarreaud9b587f2010-02-26 10:05:55 +01005076
Willy Tarreaubc18da12015-03-13 14:00:47 +01005077 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02005078 si_applet_cant_put(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005079 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01005080 }
Willy Tarreauae526782010-03-04 20:34:23 +01005081
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005082 appctx->st2 = STAT_ST_INFO;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005083 /* fall through */
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01005084
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005085 case STAT_ST_INFO:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005086 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01005087 stats_dump_html_info(si, uri);
Willy Tarreaubc18da12015-03-13 14:00:47 +01005088 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02005089 si_applet_cant_put(si);
Willy Tarreau91861262007-10-17 17:06:05 +02005090 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01005091 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005092 }
Willy Tarreau91861262007-10-17 17:06:05 +02005093
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005094 appctx->ctx.stats.px = proxy;
5095 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
5096 appctx->st2 = STAT_ST_LIST;
Willy Tarreau91861262007-10-17 17:06:05 +02005097 /* fall through */
5098
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005099 case STAT_ST_LIST:
5100 /* dump proxies */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005101 while (appctx->ctx.stats.px) {
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01005102 if (buffer_almost_full(rep->buf)) {
Willy Tarreaufe127932015-04-21 19:23:39 +02005103 si_applet_cant_put(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005104 return 0;
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01005105 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005106
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005107 px = appctx->ctx.stats.px;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005108 /* skip the disabled proxies, global frontend and non-networked ones */
5109 if (px->state != PR_STSTOPPED && px->uuid > 0 && (px->cap & (PR_CAP_FE | PR_CAP_BE)))
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01005110 if (stats_dump_proxy_to_buffer(si, px, uri) == 0)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005111 return 0;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005112
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005113 appctx->ctx.stats.px = px->next;
5114 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005115 }
5116 /* here, we just have reached the last proxy */
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005117
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005118 appctx->st2 = STAT_ST_END;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005119 /* fall through */
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005120
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005121 case STAT_ST_END:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005122 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01005123 stats_dump_html_end();
Willy Tarreaubc18da12015-03-13 14:00:47 +01005124 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02005125 si_applet_cant_put(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005126 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01005127 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005128 }
Willy Tarreau55058a72012-11-21 08:27:21 +01005129
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005130 appctx->st2 = STAT_ST_FIN;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005131 /* fall through */
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02005132
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005133 case STAT_ST_FIN:
5134 return 1;
Willy Tarreau55058a72012-11-21 08:27:21 +01005135
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005136 default:
5137 /* unknown state ! */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005138 appctx->st2 = STAT_ST_FIN;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005139 return -1;
5140 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005141}
Willy Tarreauae526782010-03-04 20:34:23 +01005142
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005143/* We reached the stats page through a POST request. The appctx is
5144 * expected to have already been allocated by the caller.
Willy Tarreau347a35d2013-11-22 17:51:09 +01005145 * Parse the posted data and enable/disable servers if necessary.
5146 * Returns 1 if request was parsed or zero if it needs more data.
5147 */
5148static int stats_process_http_post(struct stream_interface *si)
5149{
Willy Tarreau87b09662015-04-03 00:22:06 +02005150 struct stream *s = si_strm(si);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005151 struct appctx *appctx = objt_appctx(si->end);
Willy Tarreau347a35d2013-11-22 17:51:09 +01005152
5153 struct proxy *px = NULL;
5154 struct server *sv = NULL;
5155
5156 char key[LINESIZE];
5157 int action = ST_ADM_ACTION_NONE;
5158 int reprocess = 0;
5159
5160 int total_servers = 0;
5161 int altered_servers = 0;
5162
5163 char *first_param, *cur_param, *next_param, *end_params;
5164 char *st_cur_param = NULL;
5165 char *st_next_param = NULL;
5166
5167 struct chunk *temp;
5168 int reql;
5169
5170 temp = get_trash_chunk();
Willy Tarreaueee5b512015-04-03 23:46:31 +02005171 if (temp->size < s->txn->req.body_len) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01005172 /* too large request */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005173 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005174 goto out;
5175 }
5176
Willy Tarreaueee5b512015-04-03 23:46:31 +02005177 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 +01005178 if (reql <= 0) {
5179 /* we need more data */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005180 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005181 return 0;
5182 }
5183
5184 first_param = temp->str;
5185 end_params = temp->str + reql;
5186 cur_param = next_param = end_params;
5187 *end_params = '\0';
5188
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005189 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005190
5191 /*
5192 * Parse the parameters in reverse order to only store the last value.
5193 * From the html form, the backend and the action are at the end.
5194 */
5195 while (cur_param > first_param) {
5196 char *value;
5197 int poffset, plen;
5198
5199 cur_param--;
5200
5201 if ((*cur_param == '&') || (cur_param == first_param)) {
5202 reprocess_servers:
5203 /* Parse the key */
5204 poffset = (cur_param != first_param ? 1 : 0);
5205 plen = next_param - cur_param + (cur_param == first_param ? 1 : 0);
5206 if ((plen > 0) && (plen <= sizeof(key))) {
5207 strncpy(key, cur_param + poffset, plen);
5208 key[plen - 1] = '\0';
5209 } else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005210 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005211 goto out;
5212 }
5213
5214 /* Parse the value */
5215 value = key;
5216 while (*value != '\0' && *value != '=') {
5217 value++;
5218 }
5219 if (*value == '=') {
5220 /* Ok, a value is found, we can mark the end of the key */
5221 *value++ = '\0';
5222 }
5223 if (url_decode(key) < 0 || url_decode(value) < 0)
5224 break;
5225
5226 /* Now we can check the key to see what to do */
5227 if (!px && (strcmp(key, "b") == 0)) {
Willy Tarreau9e0bb102015-05-26 11:24:42 +02005228 if ((px = proxy_be_by_name(value)) == NULL) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01005229 /* the backend name is unknown or ambiguous (duplicate names) */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005230 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005231 goto out;
5232 }
5233 }
5234 else if (!action && (strcmp(key, "action") == 0)) {
Willy Tarreaued7df902014-05-22 18:04:49 +02005235 if (strcmp(value, "ready") == 0) {
5236 action = ST_ADM_ACTION_READY;
5237 }
5238 else if (strcmp(value, "drain") == 0) {
5239 action = ST_ADM_ACTION_DRAIN;
5240 }
5241 else if (strcmp(value, "maint") == 0) {
5242 action = ST_ADM_ACTION_MAINT;
5243 }
5244 else if (strcmp(value, "shutdown") == 0) {
5245 action = ST_ADM_ACTION_SHUTDOWN;
5246 }
Willy Tarreau248a60e2014-05-23 14:59:48 +02005247 else if (strcmp(value, "dhlth") == 0) {
5248 action = ST_ADM_ACTION_DHLTH;
5249 }
5250 else if (strcmp(value, "ehlth") == 0) {
5251 action = ST_ADM_ACTION_EHLTH;
5252 }
5253 else if (strcmp(value, "hrunn") == 0) {
5254 action = ST_ADM_ACTION_HRUNN;
5255 }
5256 else if (strcmp(value, "hnolb") == 0) {
5257 action = ST_ADM_ACTION_HNOLB;
5258 }
5259 else if (strcmp(value, "hdown") == 0) {
5260 action = ST_ADM_ACTION_HDOWN;
5261 }
5262 else if (strcmp(value, "dagent") == 0) {
5263 action = ST_ADM_ACTION_DAGENT;
5264 }
5265 else if (strcmp(value, "eagent") == 0) {
5266 action = ST_ADM_ACTION_EAGENT;
5267 }
5268 else if (strcmp(value, "arunn") == 0) {
5269 action = ST_ADM_ACTION_ARUNN;
5270 }
5271 else if (strcmp(value, "adown") == 0) {
5272 action = ST_ADM_ACTION_ADOWN;
5273 }
Willy Tarreaued7df902014-05-22 18:04:49 +02005274 /* else these are the old supported methods */
5275 else if (strcmp(value, "disable") == 0) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01005276 action = ST_ADM_ACTION_DISABLE;
5277 }
5278 else if (strcmp(value, "enable") == 0) {
5279 action = ST_ADM_ACTION_ENABLE;
5280 }
5281 else if (strcmp(value, "stop") == 0) {
5282 action = ST_ADM_ACTION_STOP;
5283 }
5284 else if (strcmp(value, "start") == 0) {
5285 action = ST_ADM_ACTION_START;
5286 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01005287 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005288 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005289 goto out;
5290 }
5291 }
5292 else if (strcmp(key, "s") == 0) {
5293 if (!(px && action)) {
5294 /*
5295 * Indicates that we'll need to reprocess the parameters
5296 * as soon as backend and action are known
5297 */
5298 if (!reprocess) {
5299 st_cur_param = cur_param;
5300 st_next_param = next_param;
5301 }
5302 reprocess = 1;
5303 }
5304 else if ((sv = findserver(px, value)) != NULL) {
5305 switch (action) {
5306 case ST_ADM_ACTION_DISABLE:
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02005307 if (!(sv->admin & SRV_ADMF_FMAINT)) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01005308 altered_servers++;
5309 total_servers++;
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02005310 srv_set_admin_flag(sv, SRV_ADMF_FMAINT);
Willy Tarreau347a35d2013-11-22 17:51:09 +01005311 }
5312 break;
5313 case ST_ADM_ACTION_ENABLE:
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02005314 if (sv->admin & SRV_ADMF_FMAINT) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01005315 altered_servers++;
5316 total_servers++;
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02005317 srv_clr_admin_flag(sv, SRV_ADMF_FMAINT);
Willy Tarreau347a35d2013-11-22 17:51:09 +01005318 }
5319 break;
5320 case ST_ADM_ACTION_STOP:
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02005321 if (!(sv->admin & SRV_ADMF_FDRAIN)) {
5322 srv_set_admin_flag(sv, SRV_ADMF_FDRAIN);
5323 altered_servers++;
5324 total_servers++;
5325 }
5326 break;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005327 case ST_ADM_ACTION_START:
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02005328 if (sv->admin & SRV_ADMF_FDRAIN) {
5329 srv_clr_admin_flag(sv, SRV_ADMF_FDRAIN);
5330 altered_servers++;
5331 total_servers++;
5332 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01005333 break;
Willy Tarreau248a60e2014-05-23 14:59:48 +02005334 case ST_ADM_ACTION_DHLTH:
5335 if (sv->check.state & CHK_ST_CONFIGURED) {
5336 sv->check.state &= ~CHK_ST_ENABLED;
5337 altered_servers++;
5338 total_servers++;
5339 }
5340 break;
5341 case ST_ADM_ACTION_EHLTH:
5342 if (sv->check.state & CHK_ST_CONFIGURED) {
5343 sv->check.state |= CHK_ST_ENABLED;
5344 altered_servers++;
5345 total_servers++;
5346 }
5347 break;
5348 case ST_ADM_ACTION_HRUNN:
5349 if (!(sv->track)) {
5350 sv->check.health = sv->check.rise + sv->check.fall - 1;
5351 srv_set_running(sv, "changed from Web interface");
5352 altered_servers++;
5353 total_servers++;
5354 }
5355 break;
5356 case ST_ADM_ACTION_HNOLB:
5357 if (!(sv->track)) {
5358 sv->check.health = sv->check.rise + sv->check.fall - 1;
5359 srv_set_stopping(sv, "changed from Web interface");
5360 altered_servers++;
5361 total_servers++;
5362 }
5363 break;
5364 case ST_ADM_ACTION_HDOWN:
5365 if (!(sv->track)) {
5366 sv->check.health = 0;
5367 srv_set_stopped(sv, "changed from Web interface");
5368 altered_servers++;
5369 total_servers++;
5370 }
5371 break;
5372 case ST_ADM_ACTION_DAGENT:
5373 if (sv->agent.state & CHK_ST_CONFIGURED) {
5374 sv->agent.state &= ~CHK_ST_ENABLED;
5375 altered_servers++;
5376 total_servers++;
5377 }
5378 break;
5379 case ST_ADM_ACTION_EAGENT:
5380 if (sv->agent.state & CHK_ST_CONFIGURED) {
5381 sv->agent.state |= CHK_ST_ENABLED;
5382 altered_servers++;
5383 total_servers++;
5384 }
5385 break;
5386 case ST_ADM_ACTION_ARUNN:
5387 if (sv->agent.state & CHK_ST_ENABLED) {
5388 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
5389 srv_set_running(sv, "changed from Web interface");
5390 altered_servers++;
5391 total_servers++;
5392 }
5393 break;
5394 case ST_ADM_ACTION_ADOWN:
5395 if (sv->agent.state & CHK_ST_ENABLED) {
5396 sv->agent.health = 0;
5397 srv_set_stopped(sv, "changed from Web interface");
5398 altered_servers++;
5399 total_servers++;
5400 }
5401 break;
Willy Tarreaued7df902014-05-22 18:04:49 +02005402 case ST_ADM_ACTION_READY:
5403 srv_adm_set_ready(sv);
5404 altered_servers++;
5405 total_servers++;
5406 break;
5407 case ST_ADM_ACTION_DRAIN:
5408 srv_adm_set_drain(sv);
5409 altered_servers++;
5410 total_servers++;
5411 break;
5412 case ST_ADM_ACTION_MAINT:
5413 srv_adm_set_maint(sv);
5414 altered_servers++;
5415 total_servers++;
5416 break;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005417 case ST_ADM_ACTION_SHUTDOWN:
5418 if (px->state != PR_STSTOPPED) {
Willy Tarreau87b09662015-04-03 00:22:06 +02005419 struct stream *sess, *sess_bck;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005420
5421 list_for_each_entry_safe(sess, sess_bck, &sv->actconns, by_srv)
5422 if (sess->srv_conn == sv)
Willy Tarreaue7dff022015-04-03 01:14:29 +02005423 stream_shutdown(sess, SF_ERR_KILLED);
Willy Tarreau347a35d2013-11-22 17:51:09 +01005424
5425 altered_servers++;
5426 total_servers++;
5427 }
5428 break;
5429 }
5430 } else {
5431 /* the server name is unknown or ambiguous (duplicate names) */
5432 total_servers++;
5433 }
5434 }
5435 if (reprocess && px && action) {
5436 /* Now, we know the backend and the action chosen by the user.
5437 * We can safely restart from the first server parameter
5438 * to reprocess them
5439 */
5440 cur_param = st_cur_param;
5441 next_param = st_next_param;
5442 reprocess = 0;
5443 goto reprocess_servers;
5444 }
5445
5446 next_param = cur_param;
5447 }
5448 }
5449
5450 if (total_servers == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005451 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005452 }
5453 else if (altered_servers == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005454 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005455 }
5456 else if (altered_servers == total_servers) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005457 appctx->ctx.stats.st_code = STAT_STATUS_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005458 }
5459 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005460 appctx->ctx.stats.st_code = STAT_STATUS_PART;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005461 }
5462 out:
5463 return 1;
5464}
5465
5466
5467static int stats_send_http_headers(struct stream_interface *si)
5468{
Willy Tarreau87b09662015-04-03 00:22:06 +02005469 struct stream *s = si_strm(si);
Willy Tarreau347a35d2013-11-22 17:51:09 +01005470 struct uri_auth *uri = s->be->uri_auth;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005471 struct appctx *appctx = objt_appctx(si->end);
Willy Tarreau347a35d2013-11-22 17:51:09 +01005472
5473 chunk_printf(&trash,
Willy Tarreau8b8995f2014-04-24 22:51:54 +02005474 "HTTP/1.1 200 OK\r\n"
Willy Tarreau347a35d2013-11-22 17:51:09 +01005475 "Cache-Control: no-cache\r\n"
5476 "Connection: close\r\n"
5477 "Content-Type: %s\r\n",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005478 (appctx->ctx.stats.flags & STAT_FMT_HTML) ? "text/html" : "text/plain");
Willy Tarreau347a35d2013-11-22 17:51:09 +01005479
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005480 if (uri->refresh > 0 && !(appctx->ctx.stats.flags & STAT_NO_REFRESH))
Willy Tarreau347a35d2013-11-22 17:51:09 +01005481 chunk_appendf(&trash, "Refresh: %d\r\n",
5482 uri->refresh);
5483
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005484 /* we don't send the CRLF in chunked mode, it will be sent with the first chunk's size */
5485
5486 if (appctx->ctx.stats.flags & STAT_CHUNKED)
5487 chunk_appendf(&trash, "Transfer-Encoding: chunked\r\n");
5488 else
5489 chunk_appendf(&trash, "\r\n");
Willy Tarreau347a35d2013-11-22 17:51:09 +01005490
Willy Tarreaueee5b512015-04-03 23:46:31 +02005491 s->txn->status = 200;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005492 s->logs.tv_request = now;
5493
Willy Tarreaubc18da12015-03-13 14:00:47 +01005494 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02005495 si_applet_cant_put(si);
Willy Tarreau347a35d2013-11-22 17:51:09 +01005496 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01005497 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01005498
5499 return 1;
5500}
5501
5502static int stats_send_http_redirect(struct stream_interface *si)
5503{
5504 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
Willy Tarreau87b09662015-04-03 00:22:06 +02005505 struct stream *s = si_strm(si);
Willy Tarreau347a35d2013-11-22 17:51:09 +01005506 struct uri_auth *uri = s->be->uri_auth;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005507 struct appctx *appctx = objt_appctx(si->end);
Willy Tarreau347a35d2013-11-22 17:51:09 +01005508
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005509 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Willy Tarreau347a35d2013-11-22 17:51:09 +01005510 scope_txt[0] = 0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005511 if (appctx->ctx.stats.scope_len) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01005512 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau4e4292b2014-11-28 12:18:45 +01005513 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 +01005514 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005515 }
5516
5517 /* We don't want to land on the posted stats page because a refresh will
5518 * repost the data. We don't want this to happen on accident so we redirect
5519 * the browse to the stats page with a GET.
5520 */
5521 chunk_printf(&trash,
5522 "HTTP/1.1 303 See Other\r\n"
5523 "Cache-Control: no-cache\r\n"
5524 "Content-Type: text/plain\r\n"
5525 "Connection: close\r\n"
5526 "Location: %s;st=%s%s%s%s\r\n"
Willy Tarreaufdfcc9d2016-01-26 13:57:29 +01005527 "Content-length: 0\r\n"
Willy Tarreau347a35d2013-11-22 17:51:09 +01005528 "\r\n",
5529 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005530 ((appctx->ctx.stats.st_code > STAT_STATUS_INIT) &&
5531 (appctx->ctx.stats.st_code < STAT_STATUS_SIZE) &&
5532 stat_status_codes[appctx->ctx.stats.st_code]) ?
5533 stat_status_codes[appctx->ctx.stats.st_code] :
Willy Tarreau347a35d2013-11-22 17:51:09 +01005534 stat_status_codes[STAT_STATUS_UNKN],
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005535 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
5536 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreau347a35d2013-11-22 17:51:09 +01005537 scope_txt);
5538
Willy Tarreaueee5b512015-04-03 23:46:31 +02005539 s->txn->status = 303;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005540 s->logs.tv_request = now;
5541
Willy Tarreaubc18da12015-03-13 14:00:47 +01005542 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02005543 si_applet_cant_put(si);
Willy Tarreau347a35d2013-11-22 17:51:09 +01005544 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01005545 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01005546
5547 return 1;
5548}
5549
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005550/* This I/O handler runs as an applet embedded in a stream interface. It is
5551 * used to send HTTP stats over a TCP socket. The mechanism is very simple.
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005552 * appctx->st0 contains the operation in progress (dump, done). The handler
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005553 * automatically unregisters itself once transfer is complete.
5554 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02005555static void http_stats_io_handler(struct appctx *appctx)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005556{
Willy Tarreau00a37f02015-04-13 12:05:19 +02005557 struct stream_interface *si = appctx->owner;
Willy Tarreau87b09662015-04-03 00:22:06 +02005558 struct stream *s = si_strm(si);
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005559 struct channel *req = si_oc(si);
5560 struct channel *res = si_ic(si);
Willy Tarreau55058a72012-11-21 08:27:21 +01005561
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005562 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
5563 goto out;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01005564
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005565 /* check that the output is not closed */
5566 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005567 appctx->st0 = STAT_HTTP_DONE;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005568
Willy Tarreau347a35d2013-11-22 17:51:09 +01005569 /* all states are processed in sequence */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005570 if (appctx->st0 == STAT_HTTP_HEAD) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01005571 if (stats_send_http_headers(si)) {
Willy Tarreaueee5b512015-04-03 23:46:31 +02005572 if (s->txn->meth == HTTP_METH_HEAD)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005573 appctx->st0 = STAT_HTTP_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005574 else
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005575 appctx->st0 = STAT_HTTP_DUMP;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005576 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01005577 }
5578
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005579 if (appctx->st0 == STAT_HTTP_DUMP) {
Willy Tarreau4e4292b2014-11-28 12:18:45 +01005580 unsigned int prev_len = si_ib(si)->i;
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005581 unsigned int data_len;
5582 unsigned int last_len;
Willy Tarreaucce36482014-04-24 20:26:41 +02005583 unsigned int last_fwd = 0;
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005584
5585 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
5586 /* One difficulty we're facing is that we must prevent
5587 * the input data from being automatically forwarded to
5588 * the output area. For this, we temporarily disable
5589 * forwarding on the channel.
5590 */
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005591 last_fwd = si_ic(si)->to_forward;
5592 si_ic(si)->to_forward = 0;
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005593 chunk_printf(&trash, "\r\n000000\r\n");
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005594 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02005595 si_applet_cant_put(si);
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005596 si_ic(si)->to_forward = last_fwd;
Willy Tarreau828824a2015-04-19 17:20:03 +02005597 goto out;
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005598 }
5599 }
5600
Willy Tarreau4e4292b2014-11-28 12:18:45 +01005601 data_len = si_ib(si)->i;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005602 if (stats_dump_stat_to_buffer(si, s->be->uri_auth))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005603 appctx->st0 = STAT_HTTP_DONE;
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005604
Willy Tarreau4e4292b2014-11-28 12:18:45 +01005605 last_len = si_ib(si)->i;
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005606
5607 /* Now we must either adjust or remove the chunk size. This is
5608 * not easy because the chunk size might wrap at the end of the
5609 * buffer, so we pretend we have nothing in the buffer, we write
5610 * the size, then restore the buffer's contents. Note that we can
5611 * only do that because no forwarding is scheduled on the stats
5612 * applet.
5613 */
5614 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
Willy Tarreau4e4292b2014-11-28 12:18:45 +01005615 si_ic(si)->total -= (last_len - prev_len);
5616 si_ib(si)->i -= (last_len - prev_len);
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005617
5618 if (last_len != data_len) {
5619 chunk_printf(&trash, "\r\n%06x\r\n", (last_len - data_len));
Willy Tarreaubc18da12015-03-13 14:00:47 +01005620 if (bi_putchk(si_ic(si), &trash) == -1)
Willy Tarreaufe127932015-04-21 19:23:39 +02005621 si_applet_cant_put(si);
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005622
Willy Tarreau4e4292b2014-11-28 12:18:45 +01005623 si_ic(si)->total += (last_len - data_len);
5624 si_ib(si)->i += (last_len - data_len);
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005625 }
5626 /* now re-enable forwarding */
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005627 channel_forward(si_ic(si), last_fwd);
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005628 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005629 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02005630
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005631 if (appctx->st0 == STAT_HTTP_POST) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01005632 if (stats_process_http_post(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005633 appctx->st0 = STAT_HTTP_LAST;
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005634 else if (si_oc(si)->flags & CF_SHUTR)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005635 appctx->st0 = STAT_HTTP_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005636 }
5637
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005638 if (appctx->st0 == STAT_HTTP_LAST) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01005639 if (stats_send_http_redirect(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005640 appctx->st0 = STAT_HTTP_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005641 }
5642
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005643 if (appctx->st0 == STAT_HTTP_DONE) {
5644 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
5645 chunk_printf(&trash, "\r\n0\r\n\r\n");
Willy Tarreaubc18da12015-03-13 14:00:47 +01005646 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02005647 si_applet_cant_put(si);
Willy Tarreau828824a2015-04-19 17:20:03 +02005648 goto out;
Willy Tarreaubc18da12015-03-13 14:00:47 +01005649 }
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005650 }
5651 /* eat the whole request */
Willy Tarreau4e4292b2014-11-28 12:18:45 +01005652 bo_skip(si_oc(si), si_ob(si)->o);
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005653 res->flags |= CF_READ_NULL;
5654 si_shutr(si);
5655 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01005656
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005657 if ((res->flags & CF_SHUTR) && (si->state == SI_ST_EST))
5658 si_shutw(si);
Willy Tarreau91861262007-10-17 17:06:05 +02005659
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005660 if (appctx->st0 == STAT_HTTP_DONE) {
Willy Tarreau96d44912013-11-22 12:25:24 +01005661 if ((req->flags & CF_SHUTW) && (si->state == SI_ST_EST)) {
5662 si_shutr(si);
5663 res->flags |= CF_READ_NULL;
5664 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005665 }
Willy Tarreau828824a2015-04-19 17:20:03 +02005666 out:
Willy Tarreaud4da1962015-04-20 01:31:23 +02005667 /* just to make gcc happy */ ;
Willy Tarreau91861262007-10-17 17:06:05 +02005668}
5669
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005670
Willy Tarreau909d5172012-11-26 03:04:41 +01005671static inline const char *get_conn_ctrl_name(const struct connection *conn)
5672{
Willy Tarreau3c728722014-01-23 13:50:42 +01005673 if (!conn_ctrl_ready(conn))
Willy Tarreau909d5172012-11-26 03:04:41 +01005674 return "NONE";
5675 return conn->ctrl->name;
5676}
5677
5678static inline const char *get_conn_xprt_name(const struct connection *conn)
5679{
5680 static char ptr[17];
5681
Willy Tarreauaad69382014-01-23 14:21:42 +01005682 if (!conn_xprt_ready(conn))
Willy Tarreau909d5172012-11-26 03:04:41 +01005683 return "NONE";
5684
5685 if (conn->xprt == &raw_sock)
5686 return "RAW";
5687
5688#ifdef USE_OPENSSL
5689 if (conn->xprt == &ssl_sock)
5690 return "SSL";
5691#endif
5692 snprintf(ptr, sizeof(ptr), "%p", conn->xprt);
5693 return ptr;
5694}
5695
5696static inline const char *get_conn_data_name(const struct connection *conn)
5697{
5698 static char ptr[17];
5699
5700 if (!conn->data)
5701 return "NONE";
5702
5703 if (conn->data == &sess_conn_cb)
5704 return "SESS";
5705
5706 if (conn->data == &si_conn_cb)
5707 return "STRM";
5708
5709 if (conn->data == &check_conn_cb)
5710 return "CHCK";
5711
5712 snprintf(ptr, sizeof(ptr), "%p", conn->data);
5713 return ptr;
5714}
5715
Willy Tarreau87b09662015-04-03 00:22:06 +02005716/* This function dumps a complete stream state onto the stream interface's
5717 * read buffer. The stream has to be set in sess->target. It returns
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01005718 * 0 if the output buffer is full and it needs to be called again, otherwise
5719 * non-zero. It is designed to be called from stats_dump_sess_to_buffer() below.
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005720 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005721static int stats_dump_full_sess_to_buffer(struct stream_interface *si, struct stream *sess)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005722{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005723 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005724 struct tm tm;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005725 extern const char *monthname[12];
5726 char pn[INET6_ADDRSTRLEN];
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005727 struct connection *conn;
Willy Tarreau284ddbf2013-12-01 20:45:00 +01005728 struct appctx *tmpctx;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005729
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005730 chunk_reset(&trash);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005731
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005732 if (appctx->ctx.sess.section > 0 && appctx->ctx.sess.uid != sess->uniq_id) {
Willy Tarreau87b09662015-04-03 00:22:06 +02005733 /* stream changed, no need to go any further */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005734 chunk_appendf(&trash, " *** session terminated while we were watching it ***\n");
Willy Tarreaubc18da12015-03-13 14:00:47 +01005735 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02005736 si_applet_cant_put(si);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005737 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01005738 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005739 appctx->ctx.sess.uid = 0;
5740 appctx->ctx.sess.section = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005741 return 1;
5742 }
5743
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005744 switch (appctx->ctx.sess.section) {
Willy Tarreau87b09662015-04-03 00:22:06 +02005745 case 0: /* main status of the stream */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005746 appctx->ctx.sess.uid = sess->uniq_id;
5747 appctx->ctx.sess.section = 1;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005748 /* fall through */
5749
5750 case 1:
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005751 get_localtime(sess->logs.accept_date.tv_sec, &tm);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005752 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005753 "%p: [%02d/%s/%04d:%02d:%02d:%02d.%06d] id=%u proto=%s",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005754 sess,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005755 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
5756 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(sess->logs.accept_date.tv_usec),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005757 sess->uniq_id,
Willy Tarreau666f5042015-06-17 19:49:52 +02005758 strm_li(sess) ? strm_li(sess)->proto->name : "?");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005759
Willy Tarreaud0d8da92015-04-04 02:10:38 +02005760 conn = objt_conn(strm_orig(sess));
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005761 switch (conn ? addr_to_str(&conn->addr.from, pn, sizeof(pn)) : AF_UNSPEC) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005762 case AF_INET:
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005763 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005764 chunk_appendf(&trash, " source=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005765 pn, get_host_port(&conn->addr.from));
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005766 break;
5767 case AF_UNIX:
Willy Tarreaud0d8da92015-04-04 02:10:38 +02005768 chunk_appendf(&trash, " source=unix:%d\n", strm_li(sess)->luid);
Emeric Brun837ca522010-10-22 16:19:01 +02005769 break;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005770 default:
5771 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005772 chunk_appendf(&trash, "\n");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005773 break;
5774 }
5775
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005776 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005777 " flags=0x%x, conn_retries=%d, srv_conn=%p, pend_pos=%p\n",
Willy Tarreauee28de02010-06-01 09:51:00 +02005778 sess->flags, sess->si[1].conn_retries, sess->srv_conn, sess->pend_pos);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005779
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005780 chunk_appendf(&trash,
Daniel Schultze90690c72012-03-23 10:53:36 -07005781 " frontend=%s (id=%u mode=%s), listener=%s (id=%u)",
Willy Tarreaud0d8da92015-04-04 02:10:38 +02005782 strm_fe(sess)->id, strm_fe(sess)->uuid, strm_fe(sess)->mode ? "http" : "tcp",
5783 strm_li(sess) ? strm_li(sess)->name ? strm_li(sess)->name : "?" : "?",
5784 strm_li(sess) ? strm_li(sess)->luid : 0);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005785
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005786 if (conn)
5787 conn_get_to_addr(conn);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005788
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005789 switch (conn ? addr_to_str(&conn->addr.to, pn, sizeof(pn)) : AF_UNSPEC) {
Daniel Schultze90690c72012-03-23 10:53:36 -07005790 case AF_INET:
5791 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005792 chunk_appendf(&trash, " addr=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005793 pn, get_host_port(&conn->addr.to));
Daniel Schultze90690c72012-03-23 10:53:36 -07005794 break;
5795 case AF_UNIX:
Willy Tarreaud0d8da92015-04-04 02:10:38 +02005796 chunk_appendf(&trash, " addr=unix:%d\n", strm_li(sess)->luid);
Daniel Schultze90690c72012-03-23 10:53:36 -07005797 break;
5798 default:
5799 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005800 chunk_appendf(&trash, "\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07005801 break;
5802 }
5803
Willy Tarreau50943332011-09-02 17:33:05 +02005804 if (sess->be->cap & PR_CAP_BE)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005805 chunk_appendf(&trash,
Daniel Schultze90690c72012-03-23 10:53:36 -07005806 " backend=%s (id=%u mode=%s)",
Willy Tarreau50943332011-09-02 17:33:05 +02005807 sess->be->id,
Daniel Schultze90690c72012-03-23 10:53:36 -07005808 sess->be->uuid, sess->be->mode ? "http" : "tcp");
5809 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005810 chunk_appendf(&trash, " backend=<NONE> (id=-1 mode=-)");
Daniel Schultze90690c72012-03-23 10:53:36 -07005811
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005812 conn = objt_conn(sess->si[1].end);
5813 if (conn)
5814 conn_get_from_addr(conn);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005815
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005816 switch (conn ? addr_to_str(&conn->addr.from, pn, sizeof(pn)) : AF_UNSPEC) {
Daniel Schultze90690c72012-03-23 10:53:36 -07005817 case AF_INET:
5818 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005819 chunk_appendf(&trash, " addr=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005820 pn, get_host_port(&conn->addr.from));
Daniel Schultze90690c72012-03-23 10:53:36 -07005821 break;
5822 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005823 chunk_appendf(&trash, " addr=unix\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07005824 break;
5825 default:
5826 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005827 chunk_appendf(&trash, "\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07005828 break;
5829 }
5830
5831 if (sess->be->cap & PR_CAP_BE)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005832 chunk_appendf(&trash,
Daniel Schultze90690c72012-03-23 10:53:36 -07005833 " server=%s (id=%u)",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005834 objt_server(sess->target) ? objt_server(sess->target)->id : "<none>",
5835 objt_server(sess->target) ? objt_server(sess->target)->puid : 0);
Willy Tarreau50943332011-09-02 17:33:05 +02005836 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005837 chunk_appendf(&trash, " server=<NONE> (id=-1)");
Daniel Schultze90690c72012-03-23 10:53:36 -07005838
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005839 if (conn)
5840 conn_get_to_addr(conn);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005841
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005842 switch (conn ? addr_to_str(&conn->addr.to, pn, sizeof(pn)) : AF_UNSPEC) {
Daniel Schultze90690c72012-03-23 10:53:36 -07005843 case AF_INET:
5844 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005845 chunk_appendf(&trash, " addr=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005846 pn, get_host_port(&conn->addr.to));
Daniel Schultze90690c72012-03-23 10:53:36 -07005847 break;
5848 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005849 chunk_appendf(&trash, " addr=unix\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07005850 break;
5851 default:
5852 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005853 chunk_appendf(&trash, "\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07005854 break;
5855 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005856
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005857 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005858 " task=%p (state=0x%02x nice=%d calls=%d exp=%s%s",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005859 sess->task,
5860 sess->task->state,
5861 sess->task->nice, sess->task->calls,
5862 sess->task->expire ?
5863 tick_is_expired(sess->task->expire, now_ms) ? "<PAST>" :
5864 human_time(TICKS_TO_MS(sess->task->expire - now_ms),
5865 TICKS_TO_MS(1000)) : "<NEVER>",
5866 task_in_rq(sess->task) ? ", running" : "");
5867
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005868 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005869 " age=%s)\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005870 human_time(now.tv_sec - sess->logs.accept_date.tv_sec, 1));
5871
Willy Tarreaueee5b512015-04-03 23:46:31 +02005872 if (sess->txn)
5873 chunk_appendf(&trash,
Willy Tarreau98410192014-11-26 18:05:38 +01005874 " 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 +02005875 sess->txn, sess->txn->flags, sess->txn->meth, sess->txn->status,
5876 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 +01005877
5878 chunk_appendf(&trash,
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02005879 " si[0]=%p (state=%s flags=0x%02x endp0=%s:%p exp=%s, et=0x%03x)\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005880 &sess->si[0],
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005881 si_state_str(sess->si[0].state),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005882 sess->si[0].flags,
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005883 obj_type_name(sess->si[0].end),
5884 obj_base_ptr(sess->si[0].end),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005885 sess->si[0].exp ?
5886 tick_is_expired(sess->si[0].exp, now_ms) ? "<PAST>" :
5887 human_time(TICKS_TO_MS(sess->si[0].exp - now_ms),
5888 TICKS_TO_MS(1000)) : "<NEVER>",
5889 sess->si[0].err_type);
5890
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005891 chunk_appendf(&trash,
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02005892 " si[1]=%p (state=%s flags=0x%02x endp1=%s:%p exp=%s, et=0x%03x)\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005893 &sess->si[1],
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005894 si_state_str(sess->si[1].state),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005895 sess->si[1].flags,
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005896 obj_type_name(sess->si[1].end),
5897 obj_base_ptr(sess->si[1].end),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005898 sess->si[1].exp ?
5899 tick_is_expired(sess->si[1].exp, now_ms) ? "<PAST>" :
5900 human_time(TICKS_TO_MS(sess->si[1].exp - now_ms),
5901 TICKS_TO_MS(1000)) : "<NEVER>",
5902 sess->si[1].err_type);
5903
Willy Tarreau284ddbf2013-12-01 20:45:00 +01005904 if ((conn = objt_conn(sess->si[0].end)) != NULL) {
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005905 chunk_appendf(&trash,
5906 " co0=%p ctrl=%s xprt=%s data=%s target=%s:%p\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005907 conn,
5908 get_conn_ctrl_name(conn),
5909 get_conn_xprt_name(conn),
5910 get_conn_data_name(conn),
5911 obj_type_name(conn->target),
5912 obj_base_ptr(conn->target));
Willy Tarreau909d5172012-11-26 03:04:41 +01005913
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005914 chunk_appendf(&trash,
Willy Tarreau16f649c2014-01-25 19:10:48 +01005915 " flags=0x%08x fd=%d fd.state=%02x fd.cache=%d updt=%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005916 conn->flags,
5917 conn->t.sock.fd,
Willy Tarreau69a41fa2014-01-20 11:02:59 +01005918 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].state : 0,
Willy Tarreau15a4dec2014-01-20 11:09:39 +01005919 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].cache : 0,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005920 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].updated : 0);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005921 }
Willy Tarreau284ddbf2013-12-01 20:45:00 +01005922 else if ((tmpctx = objt_appctx(sess->si[0].end)) != NULL) {
5923 chunk_appendf(&trash,
5924 " app0=%p st0=%d st1=%d st2=%d applet=%s\n",
5925 tmpctx,
5926 tmpctx->st0,
5927 tmpctx->st1,
5928 tmpctx->st2,
5929 tmpctx->applet->name);
5930 }
Willy Tarreaubc174aa2012-11-19 16:10:32 +01005931
Willy Tarreau284ddbf2013-12-01 20:45:00 +01005932 if ((conn = objt_conn(sess->si[1].end)) != NULL) {
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005933 chunk_appendf(&trash,
5934 " co1=%p ctrl=%s xprt=%s data=%s target=%s:%p\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005935 conn,
5936 get_conn_ctrl_name(conn),
5937 get_conn_xprt_name(conn),
5938 get_conn_data_name(conn),
5939 obj_type_name(conn->target),
5940 obj_base_ptr(conn->target));
Willy Tarreau909d5172012-11-26 03:04:41 +01005941
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005942 chunk_appendf(&trash,
Willy Tarreauceeafb52016-01-25 15:27:17 +01005943 " flags=0x%08x fd=%d fd.state=%02x fd.cache=%d updt=%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005944 conn->flags,
5945 conn->t.sock.fd,
Willy Tarreau69a41fa2014-01-20 11:02:59 +01005946 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].state : 0,
Willy Tarreau15a4dec2014-01-20 11:09:39 +01005947 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].cache : 0,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005948 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].updated : 0);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005949 }
Willy Tarreau284ddbf2013-12-01 20:45:00 +01005950 else if ((tmpctx = objt_appctx(sess->si[1].end)) != NULL) {
5951 chunk_appendf(&trash,
5952 " app1=%p st0=%d st1=%d st2=%d applet=%s\n",
5953 tmpctx,
5954 tmpctx->st0,
5955 tmpctx->st1,
5956 tmpctx->st2,
5957 tmpctx->applet->name);
5958 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005959
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005960 chunk_appendf(&trash,
Willy Tarreau909d5172012-11-26 03:04:41 +01005961 " req=%p (f=0x%06x an=0x%x pipe=%d tofwd=%d total=%lld)\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005962 " an_exp=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005963 &sess->req,
5964 sess->req.flags, sess->req.analysers,
5965 sess->req.pipe ? sess->req.pipe->data : 0,
5966 sess->req.to_forward, sess->req.total,
5967 sess->req.analyse_exp ?
5968 human_time(TICKS_TO_MS(sess->req.analyse_exp - now_ms),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005969 TICKS_TO_MS(1000)) : "<NEVER>");
5970
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005971 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005972 " rex=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005973 sess->req.rex ?
5974 human_time(TICKS_TO_MS(sess->req.rex - now_ms),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005975 TICKS_TO_MS(1000)) : "<NEVER>");
5976
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005977 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005978 " wex=%s\n"
Willy Tarreau909d5172012-11-26 03:04:41 +01005979 " buf=%p data=%p o=%d p=%d req.next=%d i=%d size=%d\n",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005980 sess->req.wex ?
5981 human_time(TICKS_TO_MS(sess->req.wex - now_ms),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005982 TICKS_TO_MS(1000)) : "<NEVER>",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005983 sess->req.buf,
5984 sess->req.buf->data, sess->req.buf->o,
5985 (int)(sess->req.buf->p - sess->req.buf->data),
Willy Tarreaueee5b512015-04-03 23:46:31 +02005986 sess->txn ? sess->txn->req.next : 0, sess->req.buf->i,
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005987 sess->req.buf->size);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005988
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005989 chunk_appendf(&trash,
Willy Tarreau909d5172012-11-26 03:04:41 +01005990 " res=%p (f=0x%06x an=0x%x pipe=%d tofwd=%d total=%lld)\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005991 " an_exp=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005992 &sess->res,
5993 sess->res.flags, sess->res.analysers,
5994 sess->res.pipe ? sess->res.pipe->data : 0,
5995 sess->res.to_forward, sess->res.total,
5996 sess->res.analyse_exp ?
5997 human_time(TICKS_TO_MS(sess->res.analyse_exp - now_ms),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005998 TICKS_TO_MS(1000)) : "<NEVER>");
5999
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006000 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006001 " rex=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006002 sess->res.rex ?
6003 human_time(TICKS_TO_MS(sess->res.rex - now_ms),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006004 TICKS_TO_MS(1000)) : "<NEVER>");
6005
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006006 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006007 " wex=%s\n"
Willy Tarreau909d5172012-11-26 03:04:41 +01006008 " buf=%p data=%p o=%d p=%d rsp.next=%d i=%d size=%d\n",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006009 sess->res.wex ?
6010 human_time(TICKS_TO_MS(sess->res.wex - now_ms),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006011 TICKS_TO_MS(1000)) : "<NEVER>",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006012 sess->res.buf,
6013 sess->res.buf->data, sess->res.buf->o,
6014 (int)(sess->res.buf->p - sess->res.buf->data),
Willy Tarreaueee5b512015-04-03 23:46:31 +02006015 sess->txn ? sess->txn->rsp.next : 0, sess->res.buf->i,
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006016 sess->res.buf->size);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006017
Willy Tarreaubc18da12015-03-13 14:00:47 +01006018 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02006019 si_applet_cant_put(si);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006020 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01006021 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006022
6023 /* use other states to dump the contents */
6024 }
6025 /* end of dump */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006026 appctx->ctx.sess.uid = 0;
6027 appctx->ctx.sess.section = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006028 return 1;
6029}
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006030
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02006031#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
6032static int stats_tlskeys_list(struct stream_interface *si) {
6033 struct appctx *appctx = __objt_appctx(si->end);
6034
6035 switch (appctx->st2) {
6036 case STAT_ST_INIT:
6037 /* Display the column headers. If the message cannot be sent,
6038 * quit the fucntion with returning 0. The function is called
6039 * later and restart at the state "STAT_ST_INIT".
6040 */
6041 chunk_reset(&trash);
6042 chunk_appendf(&trash, "# id (file)\n");
6043 if (bi_putchk(si_ic(si), &trash) == -1) {
6044 si_applet_cant_put(si);
6045 return 0;
6046 }
6047
6048 /* Now, we start the browsing of the references lists.
6049 * Note that the following call to LIST_ELEM return bad pointer. The only
6050 * avalaible field of this pointer is <list>. It is used with the function
6051 * tlskeys_list_get_next() for retruning the first avalaible entry
6052 */
6053 appctx->ctx.tlskeys.ref = LIST_ELEM(&tlskeys_reference, struct tls_keys_ref *, list);
6054 appctx->ctx.tlskeys.ref = tlskeys_list_get_next(appctx->ctx.tlskeys.ref, &tlskeys_reference);
6055
6056 appctx->st2 = STAT_ST_LIST;
6057 /* fall through */
6058
6059 case STAT_ST_LIST:
6060 while (appctx->ctx.tlskeys.ref) {
6061 chunk_reset(&trash);
6062
6063 chunk_appendf(&trash, "%d (%s)\n", appctx->ctx.tlskeys.ref->unique_id,
6064 appctx->ctx.tlskeys.ref->filename);
6065
6066 if (bi_putchk(si_ic(si), &trash) == -1) {
6067 /* let's try again later from this stream. We add ourselves into
6068 * this stream's users so that it can remove us upon termination.
6069 */
6070 si_applet_cant_put(si);
6071 return 0;
6072 }
6073
6074 /* get next list entry and check the end of the list */
6075 appctx->ctx.tlskeys.ref = tlskeys_list_get_next(appctx->ctx.tlskeys.ref, &tlskeys_reference);
6076 }
6077
6078 appctx->st2 = STAT_ST_FIN;
6079 /* fall through */
6080
6081 default:
6082 appctx->st2 = STAT_ST_FIN;
6083 return 1;
6084 }
6085 return 0;
6086}
6087#endif
6088
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006089static int stats_pats_list(struct stream_interface *si)
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006090{
6091 struct appctx *appctx = __objt_appctx(si->end);
6092
6093 switch (appctx->st2) {
6094 case STAT_ST_INIT:
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01006095 /* Display the column headers. If the message cannot be sent,
6096 * quit the fucntion with returning 0. The function is called
6097 * later and restart at the state "STAT_ST_INIT".
6098 */
6099 chunk_reset(&trash);
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01006100 chunk_appendf(&trash, "# id (file) description\n");
Willy Tarreaubc18da12015-03-13 14:00:47 +01006101 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02006102 si_applet_cant_put(si);
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01006103 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01006104 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006105
6106 /* Now, we start the browsing of the references lists.
6107 * Note that the following call to LIST_ELEM return bad pointer. The only
6108 * avalaible field of this pointer is <list>. It is used with the function
6109 * pat_list_get_next() for retruning the first avalaible entry
6110 */
6111 appctx->ctx.map.ref = LIST_ELEM(&pattern_reference, struct pat_ref *, list);
6112 appctx->ctx.map.ref = pat_list_get_next(appctx->ctx.map.ref, &pattern_reference,
6113 appctx->ctx.map.display_flags);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006114 appctx->st2 = STAT_ST_LIST;
6115 /* fall through */
6116
6117 case STAT_ST_LIST:
6118 while (appctx->ctx.map.ref) {
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006119 chunk_reset(&trash);
6120
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006121 /* Build messages. If the reference is used by another category than
6122 * the listed categorie, display the information in the massage.
6123 */
Thierry FOURNIERf7e04e92014-03-20 11:45:47 +01006124 chunk_appendf(&trash, "%d (%s) %s\n", appctx->ctx.map.ref->unique_id,
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01006125 appctx->ctx.map.ref->reference ? appctx->ctx.map.ref->reference : "",
6126 appctx->ctx.map.ref->display);
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01006127
Willy Tarreau2bb4a962014-11-28 11:11:05 +01006128 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau87b09662015-04-03 00:22:06 +02006129 /* let's try again later from this stream. We add ourselves into
6130 * this stream's users so that it can remove us upon termination.
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006131 */
Willy Tarreaufe127932015-04-21 19:23:39 +02006132 si_applet_cant_put(si);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006133 return 0;
6134 }
6135
6136 /* get next list entry and check the end of the list */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006137 appctx->ctx.map.ref = pat_list_get_next(appctx->ctx.map.ref, &pattern_reference,
6138 appctx->ctx.map.display_flags);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006139 }
6140
6141 appctx->st2 = STAT_ST_FIN;
6142 /* fall through */
6143
6144 default:
6145 appctx->st2 = STAT_ST_FIN;
6146 return 1;
6147 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006148 return 0;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006149}
6150
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006151static int stats_map_lookup(struct stream_interface *si)
6152{
6153 struct appctx *appctx = __objt_appctx(si->end);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006154 struct sample sample;
6155 struct pattern *pat;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006156 int match_method;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006157
6158 switch (appctx->st2) {
6159 case STAT_ST_INIT:
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006160 /* Init to the first entry. The list cannot be change */
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01006161 appctx->ctx.map.expr = LIST_ELEM(&appctx->ctx.map.ref->pat, struct pattern_expr *, list);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006162 appctx->ctx.map.expr = pat_expr_get_next(appctx->ctx.map.expr, &appctx->ctx.map.ref->pat);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006163 appctx->st2 = STAT_ST_LIST;
6164 /* fall through */
6165
6166 case STAT_ST_LIST:
6167 /* for each lookup type */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006168 while (appctx->ctx.map.expr) {
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006169 /* initialise chunk to build new message */
6170 chunk_reset(&trash);
6171
6172 /* execute pattern matching */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02006173 sample.data.type = SMP_T_STR;
Andreas Seltenreich9727cf42016-03-03 19:32:25 +01006174 sample.flags = SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02006175 sample.data.u.str.len = appctx->ctx.map.chunk.len;
6176 sample.data.u.str.str = appctx->ctx.map.chunk.str;
Thierry FOURNIER5d344082014-01-27 14:19:53 +01006177 if (appctx->ctx.map.expr->pat_head->match &&
6178 sample_convert(&sample, appctx->ctx.map.expr->pat_head->expect_type))
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006179 pat = appctx->ctx.map.expr->pat_head->match(&sample, appctx->ctx.map.expr, 1);
6180 else
6181 pat = NULL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006182
6183 /* build return message: set type of match */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006184 for (match_method=0; match_method<PAT_MATCH_NUM; match_method++)
6185 if (appctx->ctx.map.expr->pat_head->match == pat_match_fcts[match_method])
6186 break;
6187 if (match_method >= PAT_MATCH_NUM)
6188 chunk_appendf(&trash, "type=unknown(%p)", appctx->ctx.map.expr->pat_head->match);
6189 else
6190 chunk_appendf(&trash, "type=%s", pat_match_names[match_method]);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006191
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02006192 /* case sensitive */
6193 if (appctx->ctx.map.expr->mflags & PAT_MF_IGNORE_CASE)
6194 chunk_appendf(&trash, ", case=insensitive");
6195 else
6196 chunk_appendf(&trash, ", case=sensitive");
6197
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006198 /* Display no match, and set default value */
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01006199 if (!pat) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006200 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
6201 chunk_appendf(&trash, ", found=no");
6202 else
6203 chunk_appendf(&trash, ", match=no");
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006204 }
6205
6206 /* Display match and match info */
6207 else {
6208 /* display match */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006209 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
6210 chunk_appendf(&trash, ", found=yes");
6211 else
6212 chunk_appendf(&trash, ", match=yes");
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006213
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01006214 /* display index mode */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02006215 if (pat->sflags & PAT_SF_TREE)
Thierry FOURNIERb9903842014-03-11 18:48:17 +01006216 chunk_appendf(&trash, ", idx=tree");
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006217 else
Thierry FOURNIERb9903842014-03-11 18:48:17 +01006218 chunk_appendf(&trash, ", idx=list");
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006219
Thierry FOURNIER86db66a2014-03-11 18:37:58 +01006220 /* display pattern */
6221 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
6222 if (pat->ref && pat->ref->pattern)
6223 chunk_appendf(&trash, ", key=\"%s\"", pat->ref->pattern);
6224 else
6225 chunk_appendf(&trash, ", key=unknown");
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006226 }
6227 else {
Thierry FOURNIER86db66a2014-03-11 18:37:58 +01006228 if (pat->ref && pat->ref->pattern)
6229 chunk_appendf(&trash, ", pattern=\"%s\"", pat->ref->pattern);
6230 else
6231 chunk_appendf(&trash, ", pattern=unknown");
6232 }
6233
6234 /* display return value */
6235 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
Thierry FOURNIER503bb092015-08-19 08:35:43 +02006236 if (pat->data && pat->ref && pat->ref->sample)
Thierry FOURNIER03d0e452015-07-23 18:33:41 +02006237 chunk_appendf(&trash, ", value=\"%s\", type=\"%s\"", pat->ref->sample,
Thierry FOURNIER7d4335c2015-08-19 09:05:25 +02006238 smp_to_type[pat->data->type]);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006239 else
Thierry FOURNIER86db66a2014-03-11 18:37:58 +01006240 chunk_appendf(&trash, ", value=none");
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006241 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006242 }
6243
Thierry FOURNIERb9903842014-03-11 18:48:17 +01006244 chunk_appendf(&trash, "\n");
6245
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006246 /* display response */
Willy Tarreau2bb4a962014-11-28 11:11:05 +01006247 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau87b09662015-04-03 00:22:06 +02006248 /* let's try again later from this stream. We add ourselves into
6249 * this stream's users so that it can remove us upon termination.
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006250 */
Willy Tarreaufe127932015-04-21 19:23:39 +02006251 si_applet_cant_put(si);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006252 return 0;
6253 }
6254
6255 /* get next entry */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006256 appctx->ctx.map.expr = pat_expr_get_next(appctx->ctx.map.expr,
6257 &appctx->ctx.map.ref->pat);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006258 }
6259
6260 appctx->st2 = STAT_ST_FIN;
6261 /* fall through */
6262
6263 default:
6264 appctx->st2 = STAT_ST_FIN;
6265 free(appctx->ctx.map.chunk.str);
6266 return 1;
6267 }
6268}
6269
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006270static int stats_pat_list(struct stream_interface *si)
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006271{
6272 struct appctx *appctx = __objt_appctx(si->end);
6273
6274 switch (appctx->st2) {
6275
6276 case STAT_ST_INIT:
6277 /* Init to the first entry. The list cannot be change */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006278 appctx->ctx.map.elt = LIST_NEXT(&appctx->ctx.map.ref->head,
6279 struct pat_ref_elt *, list);
6280 if (&appctx->ctx.map.elt->list == &appctx->ctx.map.ref->head)
6281 appctx->ctx.map.elt = NULL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006282 appctx->st2 = STAT_ST_LIST;
6283 /* fall through */
6284
6285 case STAT_ST_LIST:
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006286 while (appctx->ctx.map.elt) {
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006287 chunk_reset(&trash);
6288
6289 /* build messages */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006290 if (appctx->ctx.map.elt->sample)
Thierry FOURNIER9356c682014-01-28 15:55:37 +01006291 chunk_appendf(&trash, "%p %s %s\n",
6292 appctx->ctx.map.elt, appctx->ctx.map.elt->pattern,
6293 appctx->ctx.map.elt->sample);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006294 else
Thierry FOURNIER9356c682014-01-28 15:55:37 +01006295 chunk_appendf(&trash, "%p %s\n",
6296 appctx->ctx.map.elt, appctx->ctx.map.elt->pattern);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006297
Willy Tarreau2bb4a962014-11-28 11:11:05 +01006298 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau87b09662015-04-03 00:22:06 +02006299 /* let's try again later from this stream. We add ourselves into
6300 * this stream's users so that it can remove us upon termination.
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006301 */
Willy Tarreaufe127932015-04-21 19:23:39 +02006302 si_applet_cant_put(si);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006303 return 0;
6304 }
6305
6306 /* get next list entry and check the end of the list */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006307 appctx->ctx.map.elt = LIST_NEXT(&appctx->ctx.map.elt->list,
6308 struct pat_ref_elt *, list);
6309 if (&appctx->ctx.map.elt->list == &appctx->ctx.map.ref->head)
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006310 break;
6311 }
6312
6313 appctx->st2 = STAT_ST_FIN;
6314 /* fall through */
6315
6316 default:
6317 appctx->st2 = STAT_ST_FIN;
6318 return 1;
6319 }
6320}
6321
Willy Tarreau87b09662015-04-03 00:22:06 +02006322/* This function dumps all streams' states onto the stream interface's
Willy Tarreau306f8302013-07-08 15:53:06 +02006323 * read buffer. It returns 0 if the output buffer is full and it needs
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01006324 * to be called again, otherwise non-zero. It is designed to be called
6325 * from stats_dump_sess_to_buffer() below.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006326 */
Simon Horman9bd2c732011-06-15 15:18:44 +09006327static int stats_dump_sess_to_buffer(struct stream_interface *si)
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006328{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006329 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006330 struct connection *conn;
6331
Willy Tarreau2bb4a962014-11-28 11:11:05 +01006332 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW))) {
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006333 /* If we're forced to shut down, we might have to remove our
Willy Tarreau87b09662015-04-03 00:22:06 +02006334 * reference to the last stream being dumped.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006335 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006336 if (appctx->st2 == STAT_ST_LIST) {
6337 if (!LIST_ISEMPTY(&appctx->ctx.sess.bref.users)) {
6338 LIST_DEL(&appctx->ctx.sess.bref.users);
6339 LIST_INIT(&appctx->ctx.sess.bref.users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +01006340 }
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006341 }
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02006342 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006343 }
6344
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006345 chunk_reset(&trash);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006346
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006347 switch (appctx->st2) {
Willy Tarreau295a8372011-03-10 11:25:07 +01006348 case STAT_ST_INIT:
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006349 /* the function had not been called yet, let's prepare the
Willy Tarreau87b09662015-04-03 00:22:06 +02006350 * buffer for a response. We initialize the current stream
Willy Tarreaufd3828e2009-02-22 15:17:24 +01006351 * pointer to the first in the global list. When a target
Willy Tarreau87b09662015-04-03 00:22:06 +02006352 * stream is being destroyed, it is responsible for updating
Willy Tarreaufd3828e2009-02-22 15:17:24 +01006353 * this pointer. We know we have reached the end when this
Willy Tarreau87b09662015-04-03 00:22:06 +02006354 * pointer points back to the head of the streams list.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006355 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006356 LIST_INIT(&appctx->ctx.sess.bref.users);
Willy Tarreau87b09662015-04-03 00:22:06 +02006357 appctx->ctx.sess.bref.ref = streams.n;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006358 appctx->st2 = STAT_ST_LIST;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006359 /* fall through */
6360
Willy Tarreau295a8372011-03-10 11:25:07 +01006361 case STAT_ST_LIST:
Willy Tarreau87b09662015-04-03 00:22:06 +02006362 /* first, let's detach the back-ref from a possible previous stream */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006363 if (!LIST_ISEMPTY(&appctx->ctx.sess.bref.users)) {
6364 LIST_DEL(&appctx->ctx.sess.bref.users);
6365 LIST_INIT(&appctx->ctx.sess.bref.users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +01006366 }
6367
6368 /* and start from where we stopped */
Willy Tarreau87b09662015-04-03 00:22:06 +02006369 while (appctx->ctx.sess.bref.ref != &streams) {
Cyril Bontéacd7d632010-11-01 19:26:02 +01006370 char pn[INET6_ADDRSTRLEN];
Willy Tarreau87b09662015-04-03 00:22:06 +02006371 struct stream *curr_sess;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006372
Willy Tarreau87b09662015-04-03 00:22:06 +02006373 curr_sess = LIST_ELEM(appctx->ctx.sess.bref.ref, struct stream *, list);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006374
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006375 if (appctx->ctx.sess.target) {
6376 if (appctx->ctx.sess.target != (void *)-1 && appctx->ctx.sess.target != curr_sess)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006377 goto next_sess;
6378
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006379 LIST_ADDQ(&curr_sess->back_refs, &appctx->ctx.sess.bref.users);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006380 /* call the proper dump() function and return if we're missing space */
Willy Tarreau76153662012-11-26 01:16:39 +01006381 if (!stats_dump_full_sess_to_buffer(si, curr_sess))
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006382 return 0;
6383
Willy Tarreau87b09662015-04-03 00:22:06 +02006384 /* stream dump complete */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006385 LIST_DEL(&appctx->ctx.sess.bref.users);
6386 LIST_INIT(&appctx->ctx.sess.bref.users);
6387 if (appctx->ctx.sess.target != (void *)-1) {
6388 appctx->ctx.sess.target = NULL;
Willy Tarreau76153662012-11-26 01:16:39 +01006389 break;
6390 }
6391 else
6392 goto next_sess;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006393 }
6394
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006395 chunk_appendf(&trash,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006396 "%p: proto=%s",
6397 curr_sess,
Willy Tarreauf1e02122015-09-23 12:16:43 +02006398 strm_li(curr_sess) ? strm_li(curr_sess)->proto->name : "?");
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006399
Willy Tarreaud0d8da92015-04-04 02:10:38 +02006400 conn = objt_conn(strm_orig(curr_sess));
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006401 switch (conn ? addr_to_str(&conn->addr.from, pn, sizeof(pn)) : AF_UNSPEC) {
Willy Tarreau631f01c2011-09-05 00:36:48 +02006402 case AF_INET:
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006403 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006404 chunk_appendf(&trash,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006405 " src=%s:%d fe=%s be=%s srv=%s",
6406 pn,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006407 get_host_port(&conn->addr.from),
Willy Tarreaud0d8da92015-04-04 02:10:38 +02006408 strm_fe(curr_sess)->id,
Willy Tarreau50943332011-09-02 17:33:05 +02006409 (curr_sess->be->cap & PR_CAP_BE) ? curr_sess->be->id : "<NONE>",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006410 objt_server(curr_sess->target) ? objt_server(curr_sess->target)->id : "<none>"
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006411 );
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006412 break;
6413 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006414 chunk_appendf(&trash,
Emeric Brun837ca522010-10-22 16:19:01 +02006415 " src=unix:%d fe=%s be=%s srv=%s",
Willy Tarreaud0d8da92015-04-04 02:10:38 +02006416 strm_li(curr_sess)->luid,
6417 strm_fe(curr_sess)->id,
Willy Tarreau50943332011-09-02 17:33:05 +02006418 (curr_sess->be->cap & PR_CAP_BE) ? curr_sess->be->id : "<NONE>",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006419 objt_server(curr_sess->target) ? objt_server(curr_sess->target)->id : "<none>"
Emeric Brun837ca522010-10-22 16:19:01 +02006420 );
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006421 break;
6422 }
6423
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006424 chunk_appendf(&trash,
Willy Tarreau65671ab2009-10-04 14:24:59 +02006425 " ts=%02x age=%s calls=%d",
6426 curr_sess->task->state,
Willy Tarreau3884cba2009-03-28 17:54:35 +01006427 human_time(now.tv_sec - curr_sess->logs.tv_accept.tv_sec, 1),
6428 curr_sess->task->calls);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006429
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006430 chunk_appendf(&trash,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01006431 " rq[f=%06xh,i=%d,an=%02xh,rx=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006432 curr_sess->req.flags,
6433 curr_sess->req.buf->i,
6434 curr_sess->req.analysers,
6435 curr_sess->req.rex ?
6436 human_time(TICKS_TO_MS(curr_sess->req.rex - now_ms),
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006437 TICKS_TO_MS(1000)) : "");
6438
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006439 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006440 ",wx=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006441 curr_sess->req.wex ?
6442 human_time(TICKS_TO_MS(curr_sess->req.wex - now_ms),
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006443 TICKS_TO_MS(1000)) : "");
6444
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006445 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006446 ",ax=%s]",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006447 curr_sess->req.analyse_exp ?
6448 human_time(TICKS_TO_MS(curr_sess->req.analyse_exp - now_ms),
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006449 TICKS_TO_MS(1000)) : "");
6450
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006451 chunk_appendf(&trash,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01006452 " rp[f=%06xh,i=%d,an=%02xh,rx=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006453 curr_sess->res.flags,
6454 curr_sess->res.buf->i,
6455 curr_sess->res.analysers,
6456 curr_sess->res.rex ?
6457 human_time(TICKS_TO_MS(curr_sess->res.rex - now_ms),
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006458 TICKS_TO_MS(1000)) : "");
6459
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006460 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006461 ",wx=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006462 curr_sess->res.wex ?
6463 human_time(TICKS_TO_MS(curr_sess->res.wex - now_ms),
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006464 TICKS_TO_MS(1000)) : "");
6465
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006466 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006467 ",ax=%s]",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006468 curr_sess->res.analyse_exp ?
6469 human_time(TICKS_TO_MS(curr_sess->res.analyse_exp - now_ms),
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006470 TICKS_TO_MS(1000)) : "");
6471
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006472 conn = objt_conn(curr_sess->si[0].end);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006473 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006474 " s0=[%d,%1xh,fd=%d,ex=%s]",
6475 curr_sess->si[0].state,
6476 curr_sess->si[0].flags,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006477 conn ? conn->t.sock.fd : -1,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006478 curr_sess->si[0].exp ?
6479 human_time(TICKS_TO_MS(curr_sess->si[0].exp - now_ms),
6480 TICKS_TO_MS(1000)) : "");
6481
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006482 conn = objt_conn(curr_sess->si[1].end);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006483 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006484 " s1=[%d,%1xh,fd=%d,ex=%s]",
6485 curr_sess->si[1].state,
6486 curr_sess->si[1].flags,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006487 conn ? conn->t.sock.fd : -1,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006488 curr_sess->si[1].exp ?
6489 human_time(TICKS_TO_MS(curr_sess->si[1].exp - now_ms),
6490 TICKS_TO_MS(1000)) : "");
6491
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006492 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006493 " exp=%s",
6494 curr_sess->task->expire ?
6495 human_time(TICKS_TO_MS(curr_sess->task->expire - now_ms),
6496 TICKS_TO_MS(1000)) : "");
Willy Tarreau4726f532009-03-07 17:25:21 +01006497 if (task_in_rq(curr_sess->task))
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006498 chunk_appendf(&trash, " run(nice=%d)", curr_sess->task->nice);
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006499
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006500 chunk_appendf(&trash, "\n");
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006501
Willy Tarreau2bb4a962014-11-28 11:11:05 +01006502 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau87b09662015-04-03 00:22:06 +02006503 /* let's try again later from this stream. We add ourselves into
6504 * this stream's users so that it can remove us upon termination.
Willy Tarreaufd3828e2009-02-22 15:17:24 +01006505 */
Willy Tarreaufe127932015-04-21 19:23:39 +02006506 si_applet_cant_put(si);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006507 LIST_ADDQ(&curr_sess->back_refs, &appctx->ctx.sess.bref.users);
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02006508 return 0;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006509 }
6510
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006511 next_sess:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006512 appctx->ctx.sess.bref.ref = curr_sess->list.n;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006513 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006514
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006515 if (appctx->ctx.sess.target && appctx->ctx.sess.target != (void *)-1) {
Willy Tarreau87b09662015-04-03 00:22:06 +02006516 /* specified stream not found */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006517 if (appctx->ctx.sess.section > 0)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006518 chunk_appendf(&trash, " *** session terminated while we were watching it ***\n");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006519 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006520 chunk_appendf(&trash, "Session not found.\n");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006521
Willy Tarreaubc18da12015-03-13 14:00:47 +01006522 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02006523 si_applet_cant_put(si);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006524 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01006525 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006526
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006527 appctx->ctx.sess.target = NULL;
6528 appctx->ctx.sess.uid = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006529 return 1;
6530 }
6531
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006532 appctx->st2 = STAT_ST_FIN;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006533 /* fall through */
6534
6535 default:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006536 appctx->st2 = STAT_ST_FIN;
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02006537 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006538 }
Emeric Brun1e029aa2010-09-23 18:12:53 +02006539}
6540
Willy Tarreau5f9a8772012-11-26 02:22:40 +01006541/* This is called when the stream interface is closed. For instance, upon an
6542 * external abort, we won't call the i/o handler anymore so we may need to
Willy Tarreau87b09662015-04-03 00:22:06 +02006543 * remove back references to the stream currently being dumped.
Willy Tarreau5f9a8772012-11-26 02:22:40 +01006544 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02006545static void cli_release_handler(struct appctx *appctx)
Willy Tarreau5f9a8772012-11-26 02:22:40 +01006546{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006547 if (appctx->st0 == STAT_CLI_O_SESS && appctx->st2 == STAT_ST_LIST) {
6548 if (!LIST_ISEMPTY(&appctx->ctx.sess.bref.users))
6549 LIST_DEL(&appctx->ctx.sess.bref.users);
Willy Tarreau5f9a8772012-11-26 02:22:40 +01006550 }
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01006551 else if (appctx->st0 == STAT_CLI_PRINT_FREE) {
6552 free(appctx->ctx.cli.err);
Willy Tarreau6457d0f2015-10-07 20:00:24 +02006553 appctx->ctx.cli.err = NULL;
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01006554 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006555 else if (appctx->st0 == STAT_CLI_O_MLOOK) {
6556 free(appctx->ctx.map.chunk.str);
Willy Tarreau6457d0f2015-10-07 20:00:24 +02006557 appctx->ctx.map.chunk.str = NULL;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006558 }
Willy Tarreau5f9a8772012-11-26 02:22:40 +01006559}
6560
Willy Tarreau20e99322013-04-13 09:22:25 +02006561/* This function is used to either dump tables states (when action is set
6562 * to STAT_CLI_O_TAB) or clear tables (when action is STAT_CLI_O_CLR).
Willy Tarreau20e99322013-04-13 09:22:25 +02006563 * It returns 0 if the output buffer is full and it needs to be called
6564 * again, otherwise non-zero.
Willy Tarreau69f58c82010-07-12 17:55:33 +02006565 */
Willy Tarreau20e99322013-04-13 09:22:25 +02006566static int stats_table_request(struct stream_interface *si, int action)
Willy Tarreau69f58c82010-07-12 17:55:33 +02006567{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006568 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau87b09662015-04-03 00:22:06 +02006569 struct stream *s = si_strm(si);
Willy Tarreau69f58c82010-07-12 17:55:33 +02006570 struct ebmb_node *eb;
6571 int dt;
Willy Tarreau44455022012-12-05 23:01:12 +01006572 int skip_entry;
Willy Tarreau20e99322013-04-13 09:22:25 +02006573 int show = action == STAT_CLI_O_TAB;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006574
6575 /*
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006576 * We have 3 possible states in appctx->st2 :
Willy Tarreau295a8372011-03-10 11:25:07 +01006577 * - STAT_ST_INIT : the first call
6578 * - STAT_ST_INFO : the proxy pointer points to the next table to
Willy Tarreau69f58c82010-07-12 17:55:33 +02006579 * dump, the entry pointer is NULL ;
Willy Tarreau295a8372011-03-10 11:25:07 +01006580 * - STAT_ST_LIST : the proxy pointer points to the current table
Willy Tarreau69f58c82010-07-12 17:55:33 +02006581 * and the entry pointer points to the next entry to be dumped,
6582 * and the refcount on the next entry is held ;
Willy Tarreau295a8372011-03-10 11:25:07 +01006583 * - STAT_ST_END : nothing left to dump, the buffer may contain some
Willy Tarreau69f58c82010-07-12 17:55:33 +02006584 * data though.
6585 */
6586
Willy Tarreau2bb4a962014-11-28 11:11:05 +01006587 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW))) {
Willy Tarreau69f58c82010-07-12 17:55:33 +02006588 /* in case of abort, remove any refcount we might have set on an entry */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006589 if (appctx->st2 == STAT_ST_LIST) {
6590 appctx->ctx.table.entry->ref_cnt--;
6591 stksess_kill_if_expired(&appctx->ctx.table.proxy->table, appctx->ctx.table.entry);
Willy Tarreauf6efda12010-08-03 20:34:06 +02006592 }
Willy Tarreau69f58c82010-07-12 17:55:33 +02006593 return 1;
6594 }
6595
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006596 chunk_reset(&trash);
Willy Tarreau69f58c82010-07-12 17:55:33 +02006597
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006598 while (appctx->st2 != STAT_ST_FIN) {
6599 switch (appctx->st2) {
Willy Tarreau295a8372011-03-10 11:25:07 +01006600 case STAT_ST_INIT:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006601 appctx->ctx.table.proxy = appctx->ctx.table.target;
6602 if (!appctx->ctx.table.proxy)
6603 appctx->ctx.table.proxy = proxy;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006604
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006605 appctx->ctx.table.entry = NULL;
6606 appctx->st2 = STAT_ST_INFO;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006607 break;
6608
Willy Tarreau295a8372011-03-10 11:25:07 +01006609 case STAT_ST_INFO:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006610 if (!appctx->ctx.table.proxy ||
6611 (appctx->ctx.table.target &&
6612 appctx->ctx.table.proxy != appctx->ctx.table.target)) {
6613 appctx->st2 = STAT_ST_END;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006614 break;
6615 }
6616
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006617 if (appctx->ctx.table.proxy->table.size) {
6618 if (show && !stats_dump_table_head_to_buffer(&trash, si, appctx->ctx.table.proxy,
6619 appctx->ctx.table.target))
Willy Tarreau69f58c82010-07-12 17:55:33 +02006620 return 0;
6621
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006622 if (appctx->ctx.table.target &&
Willy Tarreaud0d8da92015-04-04 02:10:38 +02006623 strm_li(s)->bind_conf->level >= ACCESS_LVL_OPER) {
Willy Tarreau69f58c82010-07-12 17:55:33 +02006624 /* dump entries only if table explicitly requested */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006625 eb = ebmb_first(&appctx->ctx.table.proxy->table.keys);
Willy Tarreau69f58c82010-07-12 17:55:33 +02006626 if (eb) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006627 appctx->ctx.table.entry = ebmb_entry(eb, struct stksess, key);
6628 appctx->ctx.table.entry->ref_cnt++;
6629 appctx->st2 = STAT_ST_LIST;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006630 break;
6631 }
6632 }
6633 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006634 appctx->ctx.table.proxy = appctx->ctx.table.proxy->next;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006635 break;
6636
Willy Tarreau295a8372011-03-10 11:25:07 +01006637 case STAT_ST_LIST:
Willy Tarreau44455022012-12-05 23:01:12 +01006638 skip_entry = 0;
Simon Hormanc88b8872011-06-15 15:18:49 +09006639
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006640 if (appctx->ctx.table.data_type >= 0) {
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02006641 /* we're filtering on some data contents */
6642 void *ptr;
6643 long long data;
6644
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006645 dt = appctx->ctx.table.data_type;
6646 ptr = stktable_data_ptr(&appctx->ctx.table.proxy->table,
6647 appctx->ctx.table.entry,
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02006648 dt);
6649
6650 data = 0;
6651 switch (stktable_data_types[dt].std_type) {
6652 case STD_T_SINT:
6653 data = stktable_data_cast(ptr, std_t_sint);
6654 break;
6655 case STD_T_UINT:
6656 data = stktable_data_cast(ptr, std_t_uint);
6657 break;
6658 case STD_T_ULL:
6659 data = stktable_data_cast(ptr, std_t_ull);
6660 break;
6661 case STD_T_FRQP:
6662 data = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006663 appctx->ctx.table.proxy->table.data_arg[dt].u);
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02006664 break;
6665 }
6666
6667 /* skip the entry if the data does not match the test and the value */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006668 if ((data < appctx->ctx.table.value &&
6669 (appctx->ctx.table.data_op == STD_OP_EQ ||
6670 appctx->ctx.table.data_op == STD_OP_GT ||
6671 appctx->ctx.table.data_op == STD_OP_GE)) ||
6672 (data == appctx->ctx.table.value &&
6673 (appctx->ctx.table.data_op == STD_OP_NE ||
6674 appctx->ctx.table.data_op == STD_OP_GT ||
6675 appctx->ctx.table.data_op == STD_OP_LT)) ||
6676 (data > appctx->ctx.table.value &&
6677 (appctx->ctx.table.data_op == STD_OP_EQ ||
6678 appctx->ctx.table.data_op == STD_OP_LT ||
6679 appctx->ctx.table.data_op == STD_OP_LE)))
Willy Tarreau44455022012-12-05 23:01:12 +01006680 skip_entry = 1;
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02006681 }
6682
Simon Hormanc88b8872011-06-15 15:18:49 +09006683 if (show && !skip_entry &&
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006684 !stats_dump_table_entry_to_buffer(&trash, si, appctx->ctx.table.proxy,
6685 appctx->ctx.table.entry))
Simon Hormand9366582011-06-15 15:18:45 +09006686 return 0;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006687
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006688 appctx->ctx.table.entry->ref_cnt--;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006689
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006690 eb = ebmb_next(&appctx->ctx.table.entry->key);
Willy Tarreau69f58c82010-07-12 17:55:33 +02006691 if (eb) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006692 struct stksess *old = appctx->ctx.table.entry;
6693 appctx->ctx.table.entry = ebmb_entry(eb, struct stksess, key);
Willy Tarreau8fa52f42012-01-09 11:50:03 +01006694 if (show)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006695 stksess_kill_if_expired(&appctx->ctx.table.proxy->table, old);
6696 else if (!skip_entry && !appctx->ctx.table.entry->ref_cnt)
6697 stksess_kill(&appctx->ctx.table.proxy->table, old);
6698 appctx->ctx.table.entry->ref_cnt++;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006699 break;
6700 }
6701
Simon Hormanc88b8872011-06-15 15:18:49 +09006702
6703 if (show)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006704 stksess_kill_if_expired(&appctx->ctx.table.proxy->table, appctx->ctx.table.entry);
6705 else if (!skip_entry && !appctx->ctx.table.entry->ref_cnt)
6706 stksess_kill(&appctx->ctx.table.proxy->table, appctx->ctx.table.entry);
Simon Hormanc88b8872011-06-15 15:18:49 +09006707
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006708 appctx->ctx.table.proxy = appctx->ctx.table.proxy->next;
6709 appctx->st2 = STAT_ST_INFO;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006710 break;
6711
Willy Tarreau295a8372011-03-10 11:25:07 +01006712 case STAT_ST_END:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006713 appctx->st2 = STAT_ST_FIN;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006714 break;
6715 }
6716 }
6717 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006718}
6719
Willy Tarreaud426a182010-03-05 14:58:26 +01006720/* print a line of text buffer (limited to 70 bytes) to <out>. The format is :
Willy Tarreau74808cb2009-03-04 15:53:18 +01006721 * <2 spaces> <offset=5 digits> <space or plus> <space> <70 chars max> <\n>
6722 * which is 60 chars per line. Non-printable chars \t, \n, \r and \e are
6723 * encoded in C format. Other non-printable chars are encoded "\xHH". Original
6724 * lines are respected within the limit of 70 output chars. Lines that are
6725 * continuation of a previous truncated line begin with "+" instead of " "
6726 * after the offset. The new pointer is returned.
6727 */
Willy Tarreaud426a182010-03-05 14:58:26 +01006728static int dump_text_line(struct chunk *out, const char *buf, int bsize, int len,
6729 int *line, int ptr)
Willy Tarreau74808cb2009-03-04 15:53:18 +01006730{
6731 int end;
6732 unsigned char c;
6733
6734 end = out->len + 80;
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02006735 if (end > out->size)
Willy Tarreau74808cb2009-03-04 15:53:18 +01006736 return ptr;
6737
Willy Tarreau77804732012-10-29 16:14:26 +01006738 chunk_appendf(out, " %05d%c ", ptr, (ptr == *line) ? ' ' : '+');
Willy Tarreau74808cb2009-03-04 15:53:18 +01006739
Willy Tarreaud426a182010-03-05 14:58:26 +01006740 while (ptr < len && ptr < bsize) {
6741 c = buf[ptr];
Willy Tarreau787bbd92009-03-12 08:18:33 +01006742 if (isprint(c) && isascii(c) && c != '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006743 if (out->len > end - 2)
6744 break;
6745 out->str[out->len++] = c;
Willy Tarreau787bbd92009-03-12 08:18:33 +01006746 } else if (c == '\t' || c == '\n' || c == '\r' || c == '\e' || c == '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006747 if (out->len > end - 3)
6748 break;
6749 out->str[out->len++] = '\\';
6750 switch (c) {
6751 case '\t': c = 't'; break;
6752 case '\n': c = 'n'; break;
6753 case '\r': c = 'r'; break;
6754 case '\e': c = 'e'; break;
Willy Tarreau787bbd92009-03-12 08:18:33 +01006755 case '\\': c = '\\'; break;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006756 }
6757 out->str[out->len++] = c;
6758 } else {
6759 if (out->len > end - 5)
6760 break;
6761 out->str[out->len++] = '\\';
6762 out->str[out->len++] = 'x';
6763 out->str[out->len++] = hextab[(c >> 4) & 0xF];
6764 out->str[out->len++] = hextab[c & 0xF];
6765 }
Willy Tarreaud426a182010-03-05 14:58:26 +01006766 if (buf[ptr++] == '\n') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006767 /* we had a line break, let's return now */
6768 out->str[out->len++] = '\n';
6769 *line = ptr;
6770 return ptr;
6771 }
6772 }
6773 /* we have an incomplete line, we return it as-is */
6774 out->str[out->len++] = '\n';
6775 return ptr;
6776}
6777
Baptiste Assmann3863f972015-05-17 00:33:24 +02006778/* This function dumps counters from all resolvers section and associated name servers.
6779 * It returns 0 if the output buffer is full and it needs
6780 * to be called again, otherwise non-zero.
6781 */
6782static int stats_dump_resolvers_to_buffer(struct stream_interface *si)
6783{
6784 struct appctx *appctx = __objt_appctx(si->end);
6785 struct dns_resolvers *presolvers;
6786 struct dns_nameserver *pnameserver;
6787
6788 chunk_reset(&trash);
6789
6790 switch (appctx->st2) {
6791 case STAT_ST_INIT:
6792 appctx->st2 = STAT_ST_LIST; /* let's start producing data */
6793 /* fall through */
6794
6795 case STAT_ST_LIST:
Andrew Hayworth68d05342015-10-02 20:33:01 +00006796 if (LIST_ISEMPTY(&dns_resolvers)) {
6797 chunk_appendf(&trash, "No resolvers found\n");
6798 }
6799 else {
6800 list_for_each_entry(presolvers, &dns_resolvers, list) {
6801 if (appctx->ctx.resolvers.ptr != NULL && appctx->ctx.resolvers.ptr != presolvers)
6802 continue;
6803
6804 chunk_appendf(&trash, "Resolvers section %s\n", presolvers->id);
6805 list_for_each_entry(pnameserver, &presolvers->nameserver_list, list) {
6806 chunk_appendf(&trash, " nameserver %s:\n", pnameserver->id);
6807 chunk_appendf(&trash, " sent: %ld\n", pnameserver->counters.sent);
6808 chunk_appendf(&trash, " valid: %ld\n", pnameserver->counters.valid);
6809 chunk_appendf(&trash, " update: %ld\n", pnameserver->counters.update);
6810 chunk_appendf(&trash, " cname: %ld\n", pnameserver->counters.cname);
6811 chunk_appendf(&trash, " cname_error: %ld\n", pnameserver->counters.cname_error);
6812 chunk_appendf(&trash, " any_err: %ld\n", pnameserver->counters.any_err);
6813 chunk_appendf(&trash, " nx: %ld\n", pnameserver->counters.nx);
6814 chunk_appendf(&trash, " timeout: %ld\n", pnameserver->counters.timeout);
6815 chunk_appendf(&trash, " refused: %ld\n", pnameserver->counters.refused);
6816 chunk_appendf(&trash, " other: %ld\n", pnameserver->counters.other);
6817 chunk_appendf(&trash, " invalid: %ld\n", pnameserver->counters.invalid);
6818 chunk_appendf(&trash, " too_big: %ld\n", pnameserver->counters.too_big);
6819 chunk_appendf(&trash, " truncated: %ld\n", pnameserver->counters.truncated);
6820 chunk_appendf(&trash, " outdated: %ld\n", pnameserver->counters.outdated);
6821 }
6822 }
Baptiste Assmann3863f972015-05-17 00:33:24 +02006823 }
6824
6825 /* display response */
6826 if (bi_putchk(si_ic(si), &trash) == -1) {
6827 /* let's try again later from this session. We add ourselves into
6828 * this session's users so that it can remove us upon termination.
6829 */
6830 si->flags |= SI_FL_WAIT_ROOM;
6831 return 0;
6832 }
6833
6834 appctx->st2 = STAT_ST_FIN;
6835 /* fall through */
6836
6837 default:
6838 appctx->st2 = STAT_ST_FIN;
6839 return 1;
6840 }
6841}
6842
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02006843/* This function dumps all captured errors onto the stream interface's
Willy Tarreau306f8302013-07-08 15:53:06 +02006844 * read buffer. It returns 0 if the output buffer is full and it needs
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01006845 * to be called again, otherwise non-zero.
Willy Tarreau74808cb2009-03-04 15:53:18 +01006846 */
Simon Horman9bd2c732011-06-15 15:18:44 +09006847static int stats_dump_errors_to_buffer(struct stream_interface *si)
Willy Tarreau74808cb2009-03-04 15:53:18 +01006848{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006849 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau74808cb2009-03-04 15:53:18 +01006850 extern const char *monthname[12];
Willy Tarreau74808cb2009-03-04 15:53:18 +01006851
Willy Tarreau2bb4a962014-11-28 11:11:05 +01006852 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
Willy Tarreau61b34732009-10-03 23:49:35 +02006853 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006854
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006855 chunk_reset(&trash);
Willy Tarreau74808cb2009-03-04 15:53:18 +01006856
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006857 if (!appctx->ctx.errors.px) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006858 /* the function had not been called yet, let's prepare the
6859 * buffer for a response.
6860 */
Willy Tarreau10479e42010-12-12 14:00:34 +01006861 struct tm tm;
6862
6863 get_localtime(date.tv_sec, &tm);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006864 chunk_appendf(&trash, "Total events captured on [%02d/%s/%04d:%02d:%02d:%02d.%03d] : %u\n",
Willy Tarreau10479e42010-12-12 14:00:34 +01006865 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
6866 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(date.tv_usec/1000),
6867 error_snapshot_id);
6868
Willy Tarreau2bb4a962014-11-28 11:11:05 +01006869 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau10479e42010-12-12 14:00:34 +01006870 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreaufe127932015-04-21 19:23:39 +02006871 si_applet_cant_put(si);
Willy Tarreau10479e42010-12-12 14:00:34 +01006872 return 0;
6873 }
6874
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006875 appctx->ctx.errors.px = proxy;
6876 appctx->ctx.errors.buf = 0;
6877 appctx->ctx.errors.bol = 0;
6878 appctx->ctx.errors.ptr = -1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006879 }
6880
6881 /* we have two inner loops here, one for the proxy, the other one for
6882 * the buffer.
6883 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006884 while (appctx->ctx.errors.px) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006885 struct error_snapshot *es;
6886
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006887 if (appctx->ctx.errors.buf == 0)
6888 es = &appctx->ctx.errors.px->invalid_req;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006889 else
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006890 es = &appctx->ctx.errors.px->invalid_rep;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006891
6892 if (!es->when.tv_sec)
6893 goto next;
6894
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006895 if (appctx->ctx.errors.iid >= 0 &&
6896 appctx->ctx.errors.px->uuid != appctx->ctx.errors.iid &&
6897 es->oe->uuid != appctx->ctx.errors.iid)
Willy Tarreau74808cb2009-03-04 15:53:18 +01006898 goto next;
6899
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006900 if (appctx->ctx.errors.ptr < 0) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006901 /* just print headers now */
6902
6903 char pn[INET6_ADDRSTRLEN];
6904 struct tm tm;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02006905 int port;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006906
6907 get_localtime(es->when.tv_sec, &tm);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006908 chunk_appendf(&trash, " \n[%02d/%s/%04d:%02d:%02d:%02d.%03d]",
Willy Tarreau74808cb2009-03-04 15:53:18 +01006909 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
Willy Tarreau1772ece2009-04-03 14:49:12 +02006910 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(es->when.tv_usec/1000));
Willy Tarreau74808cb2009-03-04 15:53:18 +01006911
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02006912 switch (addr_to_str(&es->src, pn, sizeof(pn))) {
6913 case AF_INET:
6914 case AF_INET6:
6915 port = get_host_port(&es->src);
6916 break;
6917 default:
6918 port = 0;
6919 }
6920
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006921 switch (appctx->ctx.errors.buf) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006922 case 0:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006923 chunk_appendf(&trash,
Willy Tarreau74808cb2009-03-04 15:53:18 +01006924 " frontend %s (#%d): invalid request\n"
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02006925 " backend %s (#%d)",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006926 appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02006927 (es->oe->cap & PR_CAP_BE) ? es->oe->id : "<NONE>",
6928 (es->oe->cap & PR_CAP_BE) ? es->oe->uuid : -1);
Willy Tarreau74808cb2009-03-04 15:53:18 +01006929 break;
6930 case 1:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006931 chunk_appendf(&trash,
Olivier Doucet08afdcb2014-09-08 11:23:00 +02006932 " backend %s (#%d): invalid response\n"
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02006933 " frontend %s (#%d)",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006934 appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02006935 es->oe->id, es->oe->uuid);
Willy Tarreau74808cb2009-03-04 15:53:18 +01006936 break;
6937 }
6938
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006939 chunk_appendf(&trash,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02006940 ", server %s (#%d), event #%u\n"
6941 " src %s:%d, session #%d, session flags 0x%08x\n"
6942 " HTTP msg state %d, msg flags 0x%08x, tx flags 0x%08x\n"
6943 " HTTP chunk len %lld bytes, HTTP body len %lld bytes\n"
6944 " buffer flags 0x%08x, out %d bytes, total %lld bytes\n"
6945 " pending %d bytes, wrapping at %d, error at position %d:\n \n",
6946 es->srv ? es->srv->id : "<NONE>", es->srv ? es->srv->puid : -1,
6947 es->ev_id,
6948 pn, port, es->sid, es->s_flags,
6949 es->state, es->m_flags, es->t_flags,
6950 es->m_clen, es->m_blen,
6951 es->b_flags, es->b_out, es->b_tot,
6952 es->len, es->b_wrap, es->pos);
6953
Willy Tarreau2bb4a962014-11-28 11:11:05 +01006954 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006955 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreaufe127932015-04-21 19:23:39 +02006956 si_applet_cant_put(si);
Willy Tarreau61b34732009-10-03 23:49:35 +02006957 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006958 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006959 appctx->ctx.errors.ptr = 0;
6960 appctx->ctx.errors.sid = es->sid;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006961 }
6962
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006963 if (appctx->ctx.errors.sid != es->sid) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006964 /* the snapshot changed while we were dumping it */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006965 chunk_appendf(&trash,
Willy Tarreau74808cb2009-03-04 15:53:18 +01006966 " WARNING! update detected on this snapshot, dump interrupted. Please re-check!\n");
Willy Tarreaubc18da12015-03-13 14:00:47 +01006967 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02006968 si_applet_cant_put(si);
Willy Tarreau61b34732009-10-03 23:49:35 +02006969 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01006970 }
Willy Tarreau74808cb2009-03-04 15:53:18 +01006971 goto next;
6972 }
6973
6974 /* OK, ptr >= 0, so we have to dump the current line */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006975 while (appctx->ctx.errors.ptr < es->len && appctx->ctx.errors.ptr < sizeof(es->buf)) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006976 int newptr;
6977 int newline;
6978
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006979 newline = appctx->ctx.errors.bol;
6980 newptr = dump_text_line(&trash, es->buf, sizeof(es->buf), es->len, &newline, appctx->ctx.errors.ptr);
6981 if (newptr == appctx->ctx.errors.ptr)
Willy Tarreau61b34732009-10-03 23:49:35 +02006982 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006983
Willy Tarreau2bb4a962014-11-28 11:11:05 +01006984 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006985 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreaufe127932015-04-21 19:23:39 +02006986 si_applet_cant_put(si);
Willy Tarreau61b34732009-10-03 23:49:35 +02006987 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006988 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006989 appctx->ctx.errors.ptr = newptr;
6990 appctx->ctx.errors.bol = newline;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006991 };
6992 next:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006993 appctx->ctx.errors.bol = 0;
6994 appctx->ctx.errors.ptr = -1;
6995 appctx->ctx.errors.buf++;
6996 if (appctx->ctx.errors.buf > 1) {
6997 appctx->ctx.errors.buf = 0;
6998 appctx->ctx.errors.px = appctx->ctx.errors.px->next;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006999 }
Willy Tarreauae795722016-02-16 11:27:28 +01007000 }
7001
7002 /* dump complete */
7003 return 1;
7004}
7005
7006/* This function dumps all environmnent variables to the buffer. It returns 0
7007 * if the output buffer is full and it needs to be called again, otherwise
7008 * non-zero. Dumps only one entry if st2 == STAT_ST_END.
7009 */
7010static int stats_dump_env_to_buffer(struct stream_interface *si)
7011{
7012 struct appctx *appctx = __objt_appctx(si->end);
7013
7014 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
7015 return 1;
7016
7017 chunk_reset(&trash);
7018
7019 /* we have two inner loops here, one for the proxy, the other one for
7020 * the buffer.
7021 */
7022 while (*appctx->ctx.env.var) {
7023 chunk_printf(&trash, "%s\n", *appctx->ctx.env.var);
7024
7025 if (bi_putchk(si_ic(si), &trash) == -1) {
7026 si_applet_cant_put(si);
7027 return 0;
7028 }
7029 if (appctx->st2 == STAT_ST_END)
7030 break;
7031 appctx->ctx.env.var++;
Willy Tarreau74808cb2009-03-04 15:53:18 +01007032 }
7033
7034 /* dump complete */
Willy Tarreau61b34732009-10-03 23:49:35 +02007035 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01007036}
7037
Willy Tarreaud5781202012-09-22 19:32:35 +02007038/* parse the "level" argument on the bind lines */
7039static int bind_parse_level(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
7040{
7041 if (!*args[cur_arg + 1]) {
7042 memprintf(err, "'%s' : missing level", args[cur_arg]);
7043 return ERR_ALERT | ERR_FATAL;
7044 }
7045
7046 if (!strcmp(args[cur_arg+1], "user"))
7047 conf->level = ACCESS_LVL_USER;
7048 else if (!strcmp(args[cur_arg+1], "operator"))
7049 conf->level = ACCESS_LVL_OPER;
7050 else if (!strcmp(args[cur_arg+1], "admin"))
7051 conf->level = ACCESS_LVL_ADMIN;
7052 else {
7053 memprintf(err, "'%s' only supports 'user', 'operator', and 'admin' (got '%s')",
7054 args[cur_arg], args[cur_arg+1]);
7055 return ERR_ALERT | ERR_FATAL;
7056 }
7057
7058 return 0;
7059}
7060
Willy Tarreau30576452015-04-13 13:50:30 +02007061struct applet http_stats_applet = {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007062 .obj_type = OBJ_TYPE_APPLET,
Willy Tarreaub24281b2011-02-13 13:16:36 +01007063 .name = "<STATS>", /* used for logging */
7064 .fct = http_stats_io_handler,
Aman Gupta9a13e842012-04-02 18:57:53 -07007065 .release = NULL,
Willy Tarreaub24281b2011-02-13 13:16:36 +01007066};
7067
Willy Tarreau30576452015-04-13 13:50:30 +02007068static struct applet cli_applet = {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007069 .obj_type = OBJ_TYPE_APPLET,
Willy Tarreaub24281b2011-02-13 13:16:36 +01007070 .name = "<CLI>", /* used for logging */
7071 .fct = cli_io_handler,
Willy Tarreau5f9a8772012-11-26 02:22:40 +01007072 .release = cli_release_handler,
Willy Tarreaub24281b2011-02-13 13:16:36 +01007073};
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01007074
Willy Tarreaudc13c112013-06-21 23:16:39 +02007075static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreau10522fd2008-07-09 20:12:41 +02007076 { CFG_GLOBAL, "stats", stats_parse_global },
7077 { 0, NULL, NULL },
7078}};
7079
Willy Tarreaud5781202012-09-22 19:32:35 +02007080static struct bind_kw_list bind_kws = { "STAT", { }, {
7081 { "level", bind_parse_level, 1 }, /* set the unix socket admin level */
7082 { NULL, NULL, 0 },
7083}};
7084
Willy Tarreau10522fd2008-07-09 20:12:41 +02007085__attribute__((constructor))
7086static void __dumpstats_module_init(void)
7087{
7088 cfg_register_keywords(&cfg_kws);
Willy Tarreaud5781202012-09-22 19:32:35 +02007089 bind_register_keywords(&bind_kws);
Willy Tarreau10522fd2008-07-09 20:12:41 +02007090}
7091
Willy Tarreau91861262007-10-17 17:06:05 +02007092/*
7093 * Local variables:
7094 * c-indent-level: 8
7095 * c-basic-offset: 8
7096 * End:
7097 */