blob: b9f85dafaa83caeaa322ed415f660545a25d95d4 [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 */
Cyril Bontédb98eb32016-05-06 12:18:50 +020089 STAT_CLI_O_PATS, /* list all pattern reference available */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +010090 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 */
William Lallemand1d0b36a2016-05-20 17:40:26 +020094 STAT_CLI_O_TLSK_ENT, /* list all TLS ticket keys entries for a reference */
Baptiste Assmann3863f972015-05-17 00:33:24 +020095 STAT_CLI_O_RESOLVERS,/* dump a resolver's section nameservers counters */
Baptiste Assmann28289462015-07-03 08:01:20 +020096 STAT_CLI_O_SERVERS_STATE, /* dump server state and changing information */
Baptiste Assmann9b6857e2015-09-18 14:49:12 +020097 STAT_CLI_O_BACKEND, /* dump backend list */
Willy Tarreauae795722016-02-16 11:27:28 +010098 STAT_CLI_O_ENV, /* dump environment */
Willy Tarreau91b843d2014-01-28 16:27:17 +010099};
100
Willy Tarreaued7df902014-05-22 18:04:49 +0200101/* Actions available for the stats admin forms */
102enum {
103 ST_ADM_ACTION_NONE = 0,
Willy Tarreau248a60e2014-05-23 14:59:48 +0200104
105 /* enable/disable health checks */
106 ST_ADM_ACTION_DHLTH,
107 ST_ADM_ACTION_EHLTH,
108
109 /* force health check status */
110 ST_ADM_ACTION_HRUNN,
111 ST_ADM_ACTION_HNOLB,
112 ST_ADM_ACTION_HDOWN,
113
114 /* enable/disable agent checks */
115 ST_ADM_ACTION_DAGENT,
116 ST_ADM_ACTION_EAGENT,
117
118 /* force agent check status */
119 ST_ADM_ACTION_ARUNN,
120 ST_ADM_ACTION_ADOWN,
121
122 /* set admin state */
Willy Tarreaued7df902014-05-22 18:04:49 +0200123 ST_ADM_ACTION_READY,
124 ST_ADM_ACTION_DRAIN,
125 ST_ADM_ACTION_MAINT,
126 ST_ADM_ACTION_SHUTDOWN,
127 /* these are the ancient actions, still available for compatibility */
128 ST_ADM_ACTION_DISABLE,
129 ST_ADM_ACTION_ENABLE,
130 ST_ADM_ACTION_STOP,
131 ST_ADM_ACTION_START,
132};
133
Willy Tarreauc4832de2016-01-11 18:12:26 +0100134
Willy Tarreauc4832de2016-01-11 18:12:26 +0100135/* These are the field names for each INF_* field position. Please pay attention
136 * to always use the exact same name except that the strings for new names must
137 * be lower case or CamelCase while the enum entries must be upper case.
138 */
139const char *info_field_names[INF_TOTAL_FIELDS] = {
140 [INF_NAME] = "Name",
141 [INF_VERSION] = "Version",
142 [INF_RELEASE_DATE] = "Release_date",
143 [INF_NBPROC] = "Nbproc",
144 [INF_PROCESS_NUM] = "Process_num",
145 [INF_PID] = "Pid",
146 [INF_UPTIME] = "Uptime",
147 [INF_UPTIME_SEC] = "Uptime_sec",
148 [INF_MEMMAX_MB] = "Memmax_MB",
149 [INF_POOL_ALLOC_MB] = "PoolAlloc_MB",
150 [INF_POOL_USED_MB] = "PoolUsed_MB",
151 [INF_POOL_FAILED] = "PoolFailed",
152 [INF_ULIMIT_N] = "Ulimit-n",
153 [INF_MAXSOCK] = "Maxsock",
154 [INF_MAXCONN] = "Maxconn",
155 [INF_HARD_MAXCONN] = "Hard_maxconn",
156 [INF_CURR_CONN] = "CurrConns",
157 [INF_CUM_CONN] = "CumConns",
158 [INF_CUM_REQ] = "CumReq",
159 [INF_MAX_SSL_CONNS] = "MaxSslConns",
160 [INF_CURR_SSL_CONNS] = "CurrSslConns",
161 [INF_CUM_SSL_CONNS] = "CumSslConns",
162 [INF_MAXPIPES] = "Maxpipes",
163 [INF_PIPES_USED] = "PipesUsed",
164 [INF_PIPES_FREE] = "PipesFree",
165 [INF_CONN_RATE] = "ConnRate",
166 [INF_CONN_RATE_LIMIT] = "ConnRateLimit",
167 [INF_MAX_CONN_RATE] = "MaxConnRate",
168 [INF_SESS_RATE] = "SessRate",
169 [INF_SESS_RATE_LIMIT] = "SessRateLimit",
170 [INF_MAX_SESS_RATE] = "MaxSessRate",
171 [INF_SSL_RATE] = "SslRate",
172 [INF_SSL_RATE_LIMIT] = "SslRateLimit",
173 [INF_MAX_SSL_RATE] = "MaxSslRate",
174 [INF_SSL_FRONTEND_KEY_RATE] = "SslFrontendKeyRate",
175 [INF_SSL_FRONTEND_MAX_KEY_RATE] = "SslFrontendMaxKeyRate",
176 [INF_SSL_FRONTEND_SESSION_REUSE_PCT] = "SslFrontendSessionReuse_pct",
177 [INF_SSL_BACKEND_KEY_RATE] = "SslBackendKeyRate",
178 [INF_SSL_BACKEND_MAX_KEY_RATE] = "SslBackendMaxKeyRate",
179 [INF_SSL_CACHE_LOOKUPS] = "SslCacheLookups",
180 [INF_SSL_CACHE_MISSES] = "SslCacheMisses",
181 [INF_COMPRESS_BPS_IN] = "CompressBpsIn",
182 [INF_COMPRESS_BPS_OUT] = "CompressBpsOut",
183 [INF_COMPRESS_BPS_RATE_LIM] = "CompressBpsRateLim",
184 [INF_ZLIB_MEM_USAGE] = "ZlibMemUsage",
185 [INF_MAX_ZLIB_MEM_USAGE] = "MaxZlibMemUsage",
186 [INF_TASKS] = "Tasks",
187 [INF_RUN_QUEUE] = "Run_queue",
188 [INF_IDLE_PCT] = "Idle_pct",
189 [INF_NODE] = "node",
190 [INF_DESCRIPTION] = "description",
191};
192
193/* one line of stats */
194static struct field info[INF_TOTAL_FIELDS];
195
Willy Tarreau8e205142016-01-04 17:23:25 +0100196/* These are the field names for each ST_F_* field position. Please pay attention
197 * to always use the exact same name except that the strings must be lower case
198 * while the enum entries must be upper case.
199 */
200const char *stat_field_names[ST_F_TOTAL_FIELDS] = {
201 [ST_F_PXNAME] = "pxname",
202 [ST_F_SVNAME] = "svname",
203 [ST_F_QCUR] = "qcur",
204 [ST_F_QMAX] = "qmax",
205 [ST_F_SCUR] = "scur",
206 [ST_F_SMAX] = "smax",
207 [ST_F_SLIM] = "slim",
208 [ST_F_STOT] = "stot",
209 [ST_F_BIN] = "bin",
210 [ST_F_BOUT] = "bout",
211 [ST_F_DREQ] = "dreq",
212 [ST_F_DRESP] = "dresp",
213 [ST_F_EREQ] = "ereq",
214 [ST_F_ECON] = "econ",
215 [ST_F_ERESP] = "eresp",
216 [ST_F_WRETR] = "wretr",
217 [ST_F_WREDIS] = "wredis",
218 [ST_F_STATUS] = "status",
219 [ST_F_WEIGHT] = "weight",
220 [ST_F_ACT] = "act",
221 [ST_F_BCK] = "bck",
222 [ST_F_CHKFAIL] = "chkfail",
223 [ST_F_CHKDOWN] = "chkdown",
224 [ST_F_LASTCHG] = "lastchg",
225 [ST_F_DOWNTIME] = "downtime",
226 [ST_F_QLIMIT] = "qlimit",
227 [ST_F_PID] = "pid",
228 [ST_F_IID] = "iid",
229 [ST_F_SID] = "sid",
230 [ST_F_THROTTLE] = "throttle",
231 [ST_F_LBTOT] = "lbtot",
232 [ST_F_TRACKED] = "tracked",
233 [ST_F_TYPE] = "type",
234 [ST_F_RATE] = "rate",
235 [ST_F_RATE_LIM] = "rate_lim",
236 [ST_F_RATE_MAX] = "rate_max",
237 [ST_F_CHECK_STATUS] = "check_status",
238 [ST_F_CHECK_CODE] = "check_code",
239 [ST_F_CHECK_DURATION] = "check_duration",
240 [ST_F_HRSP_1XX] = "hrsp_1xx",
241 [ST_F_HRSP_2XX] = "hrsp_2xx",
242 [ST_F_HRSP_3XX] = "hrsp_3xx",
243 [ST_F_HRSP_4XX] = "hrsp_4xx",
244 [ST_F_HRSP_5XX] = "hrsp_5xx",
245 [ST_F_HRSP_OTHER] = "hrsp_other",
246 [ST_F_HANAFAIL] = "hanafail",
247 [ST_F_REQ_RATE] = "req_rate",
248 [ST_F_REQ_RATE_MAX] = "req_rate_max",
249 [ST_F_REQ_TOT] = "req_tot",
250 [ST_F_CLI_ABRT] = "cli_abrt",
251 [ST_F_SRV_ABRT] = "srv_abrt",
252 [ST_F_COMP_IN] = "comp_in",
253 [ST_F_COMP_OUT] = "comp_out",
254 [ST_F_COMP_BYP] = "comp_byp",
255 [ST_F_COMP_RSP] = "comp_rsp",
256 [ST_F_LASTSESS] = "lastsess",
257 [ST_F_LAST_CHK] = "last_chk",
258 [ST_F_LAST_AGT] = "last_agt",
259 [ST_F_QTIME] = "qtime",
260 [ST_F_CTIME] = "ctime",
261 [ST_F_RTIME] = "rtime",
262 [ST_F_TTIME] = "ttime",
Willy Tarreau7f618842016-01-08 11:40:03 +0100263 [ST_F_AGENT_STATUS] = "agent_status",
264 [ST_F_AGENT_CODE] = "agent_code",
265 [ST_F_AGENT_DURATION] = "agent_duration",
Willy Tarreaudd7354b2016-01-08 13:47:26 +0100266 [ST_F_CHECK_DESC] = "check_desc",
267 [ST_F_AGENT_DESC] = "agent_desc",
Willy Tarreau3141f592016-01-08 14:25:28 +0100268 [ST_F_CHECK_RISE] = "check_rise",
269 [ST_F_CHECK_FALL] = "check_fall",
270 [ST_F_CHECK_HEALTH] = "check_health",
271 [ST_F_AGENT_RISE] = "agent_rise",
272 [ST_F_AGENT_FALL] = "agent_fall",
273 [ST_F_AGENT_HEALTH] = "agent_health",
Willy Tarreau3a4ec3a2016-01-08 15:35:43 +0100274 [ST_F_ADDR] = "addr",
Willy Tarreaue4847c62016-01-08 15:43:54 +0100275 [ST_F_COOKIE] = "cookie",
Willy Tarreauf8211df2016-01-11 14:09:38 +0100276 [ST_F_MODE] = "mode",
Willy Tarreauf1516d92016-01-11 14:48:36 +0100277 [ST_F_ALGO] = "algo",
Willy Tarreauc73810f2016-01-11 13:52:04 +0100278 [ST_F_CONN_RATE] = "conn_rate",
279 [ST_F_CONN_RATE_MAX] = "conn_rate_max",
280 [ST_F_CONN_TOT] = "conn_tot",
Willy Tarreau5b9bdff2016-01-11 14:40:47 +0100281 [ST_F_INTERCEPTED] = "intercepted",
Willy Tarreau8e205142016-01-04 17:23:25 +0100282};
283
Willy Tarreau82a86022016-01-04 19:04:18 +0100284/* one line of stats */
285static struct field stats[ST_F_TOTAL_FIELDS];
286
Baptiste Assmann9b6857e2015-09-18 14:49:12 +0200287static int stats_dump_backend_to_buffer(struct stream_interface *si);
Willy Tarreauae795722016-02-16 11:27:28 +0100288static int stats_dump_env_to_buffer(struct stream_interface *si);
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +0100289static int stats_dump_info_to_buffer(struct stream_interface *si);
Baptiste Assmann28289462015-07-03 08:01:20 +0200290static int stats_dump_servers_state_to_buffer(struct stream_interface *si);
Willy Tarreau12833bb2014-01-28 16:49:56 +0100291static int stats_dump_pools_to_buffer(struct stream_interface *si);
Willy Tarreau87b09662015-04-03 00:22:06 +0200292static int stats_dump_full_sess_to_buffer(struct stream_interface *si, struct stream *sess);
Simon Horman9bd2c732011-06-15 15:18:44 +0900293static int stats_dump_sess_to_buffer(struct stream_interface *si);
294static int stats_dump_errors_to_buffer(struct stream_interface *si);
Willy Tarreau44455022012-12-05 23:01:12 +0100295static int stats_table_request(struct stream_interface *si, int show);
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +0100296static int stats_dump_proxy_to_buffer(struct stream_interface *si, struct proxy *px, struct uri_auth *uri);
297static int stats_dump_stat_to_buffer(struct stream_interface *si, struct uri_auth *uri);
Baptiste Assmann3863f972015-05-17 00:33:24 +0200298static int stats_dump_resolvers_to_buffer(struct stream_interface *si);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100299static int stats_pats_list(struct stream_interface *si);
300static int stats_pat_list(struct stream_interface *si);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +0100301static int stats_map_lookup(struct stream_interface *si);
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +0200302#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
303static int stats_tlskeys_list(struct stream_interface *si);
304#endif
Willy Tarreau00a37f02015-04-13 12:05:19 +0200305static void cli_release_handler(struct appctx *appctx);
Simon Horman9bd2c732011-06-15 15:18:44 +0900306
Cyril Bonté76a99782016-05-06 12:18:48 +0200307static int dump_servers_state(struct stream_interface *si, struct chunk *buf);
Baptiste Assmann28289462015-07-03 08:01:20 +0200308
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100309/*
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +0100310 * cli_io_handler()
311 * -> stats_dump_sess_to_buffer() // "show sess"
312 * -> stats_dump_errors_to_buffer() // "show errors"
313 * -> stats_dump_info_to_buffer() // "show info"
Baptiste Assmann9b6857e2015-09-18 14:49:12 +0200314 * -> stats_dump_backend_to_buffer() // "show backend"
Baptiste Assmann28289462015-07-03 08:01:20 +0200315 * -> stats_dump_servers_state_to_buffer() // "show servers state [<backend name>]"
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +0100316 * -> stats_dump_stat_to_buffer() // "show stat"
Cyril Bontéa8322f92016-05-06 12:18:51 +0200317 * -> stats_dump_resolvers_to_buffer() // "show stat resolvers <id>"
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +0100318 * -> stats_dump_csv_header()
319 * -> stats_dump_proxy_to_buffer()
320 * -> stats_dump_fe_stats()
321 * -> stats_dump_li_stats()
322 * -> stats_dump_sv_stats()
323 * -> stats_dump_be_stats()
324 *
325 * http_stats_io_handler()
326 * -> stats_dump_stat_to_buffer() // same as above, but used for CSV or HTML
327 * -> stats_dump_csv_header() // emits the CSV headers (same as above)
328 * -> stats_dump_html_head() // emits the HTML headers
329 * -> stats_dump_html_info() // emits the equivalent of "show info" at the top
330 * -> stats_dump_proxy_to_buffer() // same as above, valid for CSV and HTML
331 * -> stats_dump_html_px_hdr()
332 * -> stats_dump_fe_stats()
333 * -> stats_dump_li_stats()
334 * -> stats_dump_sv_stats()
335 * -> stats_dump_be_stats()
336 * -> stats_dump_html_px_end()
337 * -> stats_dump_html_end() // emits HTML trailer
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100338 */
339
Willy Tarreau30576452015-04-13 13:50:30 +0200340static struct applet cli_applet;
Simon Horman9bd2c732011-06-15 15:18:44 +0900341
342static const char stats_sock_usage_msg[] =
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200343 "Unknown command. Please enter one of the following commands only :\n"
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200344 " clear counters : clear max statistics counters (add 'all' for all counters)\n"
Willy Tarreau88ee3972010-07-13 13:48:00 +0200345 " clear table : remove an entry from a table\n"
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200346 " help : this message\n"
347 " prompt : toggle interactive mode with prompt\n"
348 " quit : disconnect\n"
Baptiste Assmann9b6857e2015-09-18 14:49:12 +0200349 " show backend : list backends in the current running config\n"
Willy Tarreauae795722016-02-16 11:27:28 +0100350 " show env [var] : dump environment variables known to the process\n"
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200351 " show info : report information about the running process\n"
Willy Tarreau12833bb2014-01-28 16:49:56 +0100352 " show pools : report information about the memory pools usage\n"
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200353 " show stat : report counters for each proxy and server\n"
Cyril Bontéa8322f92016-05-06 12:18:51 +0200354 " show stat resolvers [id]: dumps counters from all resolvers section and\n"
355 " associated name servers\n"
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200356 " show errors : report last request and response errors for each proxy\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +0100357 " show sess [id] : report the list of current sessions or dump this session\n"
Willy Tarreau69f58c82010-07-12 17:55:33 +0200358 " show table [id]: report table usage stats or dump this table's contents\n"
Baptiste Assmann28289462015-07-03 08:01:20 +0200359 " show servers state [id]: dump volatile server information (for backend <id>)\n"
Willy Tarreau38338fa2009-10-10 18:37:29 +0200360 " get weight : report a server's current weight\n"
Willy Tarreau4483d432009-10-10 19:30:08 +0200361 " set weight : change a server's weight\n"
Baptiste Assmann3d8f8312015-04-13 22:54:33 +0200362 " set server : change a server's state, weight or address\n"
Willy Tarreau654694e2012-06-07 01:03:16 +0200363 " set table [id] : update or create a table entry's data\n"
Willy Tarreau7aabd112010-01-26 10:59:06 +0100364 " set timeout : change a timeout setting\n"
Willy Tarreau2a0f4d22011-08-02 11:49:05 +0200365 " set maxconn : change a maxconn setting\n"
Willy Tarreauf5b22872011-09-07 16:13:44 +0200366 " set rate-limit : change a rate limiting value\n"
Willy Tarreaua295edc2011-09-07 23:21:03 +0200367 " disable : put a server or frontend in maintenance mode\n"
368 " enable : re-enable a server or frontend which is in maintenance mode\n"
369 " shutdown : kill a session or a frontend (eg:to release listening ports)\n"
Cyril Bontédb98eb32016-05-06 12:18:50 +0200370 " show acl [id] : report available acls or dump an acl's contents\n"
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100371 " get acl : reports the patterns matching a sample for an ACL\n"
372 " add acl : add acl entry\n"
373 " del acl : delete acl entry\n"
374 " clear acl <id> : clear the content of this acl\n"
Cyril Bontédb98eb32016-05-06 12:18:50 +0200375 " show map [id] : report available maps or dump a map's contents\n"
Thierry FOURNIER1432a0c2014-03-11 13:42:38 +0100376 " get map : reports the keys and values matching a sample for a map\n"
377 " set map : modify map entry\n"
378 " add map : add map entry\n"
379 " del map : delete map entry\n"
380 " clear map <id> : clear the content of this map\n"
Emeric Brun4147b2e2014-06-16 18:36:30 +0200381 " set ssl <stmt> : set statement for ssl\n"
William Lallemand1d0b36a2016-05-20 17:40:26 +0200382#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
383 " show tls-keys [id|*]: show tls keys references or dump tls ticket keys when id specified\n"
384#endif
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200385 "";
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200386
Simon Horman9bd2c732011-06-15 15:18:44 +0900387static const char stats_permission_denied_msg[] =
Willy Tarreau6162db22009-10-10 17:13:00 +0200388 "Permission denied\n"
389 "";
390
Willy Tarreau295a8372011-03-10 11:25:07 +0100391/* data transmission states for the stats responses */
392enum {
393 STAT_ST_INIT = 0,
394 STAT_ST_HEAD,
395 STAT_ST_INFO,
396 STAT_ST_LIST,
397 STAT_ST_END,
398 STAT_ST_FIN,
399};
400
401/* data transmission states for the stats responses inside a proxy */
402enum {
403 STAT_PX_ST_INIT = 0,
404 STAT_PX_ST_TH,
405 STAT_PX_ST_FE,
406 STAT_PX_ST_LI,
407 STAT_PX_ST_SV,
408 STAT_PX_ST_BE,
409 STAT_PX_ST_END,
410 STAT_PX_ST_FIN,
411};
412
Cyril Bonté19979e12012-04-04 12:57:21 +0200413extern const char *stat_status_codes[];
414
Willy Tarreau07e9e642010-08-17 21:48:17 +0200415/* allocate a new stats frontend named <name>, and return it
416 * (or NULL in case of lack of memory).
417 */
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200418static struct proxy *alloc_stats_fe(const char *name, const char *file, int line)
Willy Tarreau07e9e642010-08-17 21:48:17 +0200419{
420 struct proxy *fe;
421
Vincent Bernat02779b62016-04-03 13:48:43 +0200422 fe = calloc(1, sizeof(*fe));
Willy Tarreau07e9e642010-08-17 21:48:17 +0200423 if (!fe)
424 return NULL;
425
Willy Tarreau237250c2011-07-29 01:49:03 +0200426 init_new_proxy(fe);
Willy Tarreau050536d2012-10-04 08:47:34 +0200427 fe->next = proxy;
428 proxy = fe;
Willy Tarreau07e9e642010-08-17 21:48:17 +0200429 fe->last_change = now.tv_sec;
430 fe->id = strdup("GLOBAL");
431 fe->cap = PR_CAP_FE;
Willy Tarreauc2adf8b2011-09-07 12:13:34 +0200432 fe->maxconn = 10; /* default to 10 concurrent connections */
433 fe->timeout.client = MS_TO_TICKS(10000); /* default timeout of 10 seconds */
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200434 fe->conf.file = strdup(file);
435 fe->conf.line = line;
Willy Tarreau32b60d42015-03-13 16:14:57 +0100436 fe->accept = frontend_accept;
Willy Tarreauf87ab942015-03-13 15:55:16 +0100437 fe->default_target = &cli_applet.obj_type;
Willy Tarreau050536d2012-10-04 08:47:34 +0200438
439 /* the stats frontend is the only one able to assign ID #0 */
440 fe->conf.id.key = fe->uuid = 0;
441 eb32_insert(&used_proxy_id, &fe->conf.id);
Willy Tarreau07e9e642010-08-17 21:48:17 +0200442 return fe;
443}
444
Willy Tarreaufbee7132007-10-18 13:53:22 +0200445/* This function parses a "stats" statement in the "global" section. It returns
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200446 * -1 if there is any error, otherwise zero. If it returns -1, it will write an
447 * error message into the <err> buffer which will be preallocated. The trailing
448 * '\n' must not be written. The function must be called with <args> pointing to
449 * the first word after "stats".
Willy Tarreaufbee7132007-10-18 13:53:22 +0200450 */
Willy Tarreau10522fd2008-07-09 20:12:41 +0200451static int stats_parse_global(char **args, int section_type, struct proxy *curpx,
Willy Tarreau28a47d62012-09-18 20:02:48 +0200452 struct proxy *defpx, const char *file, int line,
453 char **err)
Willy Tarreaufbee7132007-10-18 13:53:22 +0200454{
Willy Tarreau4348fad2012-09-20 16:48:07 +0200455 struct bind_conf *bind_conf;
Willy Tarreauc53d4222012-09-20 20:19:28 +0200456 struct listener *l;
Willy Tarreau4348fad2012-09-20 16:48:07 +0200457
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200458 if (!strcmp(args[1], "socket")) {
Willy Tarreaufbee7132007-10-18 13:53:22 +0200459 int cur_arg;
460
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200461 if (*args[2] == 0) {
Willy Tarreauc53d4222012-09-20 20:19:28 +0200462 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 +0200463 return -1;
464 }
465
Willy Tarreau89a63132009-08-16 17:41:45 +0200466 if (!global.stats_fe) {
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200467 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200468 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
Willy Tarreau89a63132009-08-16 17:41:45 +0200469 return -1;
470 }
Willy Tarreau89a63132009-08-16 17:41:45 +0200471 }
472
Willy Tarreau4348fad2012-09-20 16:48:07 +0200473 bind_conf = bind_conf_alloc(&global.stats_fe->conf.bind, file, line, args[2]);
Willy Tarreau290e63a2012-09-20 18:07:14 +0200474 bind_conf->level = ACCESS_LVL_OPER; /* default access level */
Willy Tarreau4348fad2012-09-20 16:48:07 +0200475
Willy Tarreauc53d4222012-09-20 20:19:28 +0200476 if (!str2listener(args[2], global.stats_fe, bind_conf, file, line, err)) {
477 memprintf(err, "parsing [%s:%d] : '%s %s' : %s\n",
478 file, line, args[0], args[1], err && *err ? *err : "error");
479 return -1;
480 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200481
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200482 cur_arg = 3;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200483 while (*args[cur_arg]) {
Willy Tarreaud5781202012-09-22 19:32:35 +0200484 static int bind_dumped;
485 struct bind_kw *kw;
486
487 kw = bind_find_kw(args[cur_arg]);
488 if (kw) {
489 if (!kw->parse) {
490 memprintf(err, "'%s %s' : '%s' option is not implemented in this version (check build options).",
491 args[0], args[1], args[cur_arg]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200492 return -1;
493 }
Willy Tarreaud5781202012-09-22 19:32:35 +0200494
Willy Tarreau061b5de2015-10-13 15:06:57 +0200495 if (kw->parse(args, cur_arg, global.stats_fe, bind_conf, err) != 0) {
Willy Tarreaud5781202012-09-22 19:32:35 +0200496 if (err && *err)
497 memprintf(err, "'%s %s' : '%s'", args[0], args[1], *err);
498 else
499 memprintf(err, "'%s %s' : error encountered while processing '%s'",
500 args[0], args[1], args[cur_arg]);
Willy Tarreau6162db22009-10-10 17:13:00 +0200501 return -1;
502 }
Willy Tarreaud5781202012-09-22 19:32:35 +0200503
504 cur_arg += 1 + kw->skip;
505 continue;
Willy Tarreau6162db22009-10-10 17:13:00 +0200506 }
Willy Tarreaud5781202012-09-22 19:32:35 +0200507
508 if (!bind_dumped) {
509 bind_dump_kws(err);
510 indent_msg(err, 4);
511 bind_dumped = 1;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200512 }
Willy Tarreaud5781202012-09-22 19:32:35 +0200513
514 memprintf(err, "'%s %s' : unknown keyword '%s'.%s%s",
515 args[0], args[1], args[cur_arg],
516 err && *err ? " Registered keywords :" : "", err && *err ? *err : "");
517 return -1;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200518 }
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100519
Willy Tarreauc53d4222012-09-20 20:19:28 +0200520 list_for_each_entry(l, &bind_conf->listeners, by_bind) {
521 l->maxconn = global.stats_fe->maxconn;
522 l->backlog = global.stats_fe->backlog;
Willy Tarreau9903f0e2015-04-04 18:50:31 +0200523 l->accept = session_accept_fd;
Willy Tarreau87b09662015-04-03 00:22:06 +0200524 l->handler = process_stream;
Willy Tarreau10b688f2015-03-13 16:43:12 +0100525 l->default_target = global.stats_fe->default_target;
Willy Tarreauc53d4222012-09-20 20:19:28 +0200526 l->options |= LI_O_UNLIMITED; /* don't make the peers subject to global limits */
527 l->nice = -64; /* we want to boost priority for local stats */
528 global.maxsock += l->maxconn;
529 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200530 }
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200531 else if (!strcmp(args[1], "timeout")) {
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100532 unsigned timeout;
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200533 const char *res = parse_time_err(args[2], &timeout, TIME_UNIT_MS);
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100534
535 if (res) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200536 memprintf(err, "'%s %s' : unexpected character '%c'", args[0], args[1], *res);
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100537 return -1;
538 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200539
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100540 if (!timeout) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200541 memprintf(err, "'%s %s' expects a positive value", args[0], args[1]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200542 return -1;
543 }
Willy Tarreau07e9e642010-08-17 21:48:17 +0200544 if (!global.stats_fe) {
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200545 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200546 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
Willy Tarreau07e9e642010-08-17 21:48:17 +0200547 return -1;
548 }
549 }
Willy Tarreau89a63132009-08-16 17:41:45 +0200550 global.stats_fe->timeout.client = MS_TO_TICKS(timeout);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200551 }
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200552 else if (!strcmp(args[1], "maxconn")) {
553 int maxconn = atol(args[2]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200554
555 if (maxconn <= 0) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200556 memprintf(err, "'%s %s' expects a positive value", args[0], args[1]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200557 return -1;
558 }
Willy Tarreauc2adf8b2011-09-07 12:13:34 +0200559
560 if (!global.stats_fe) {
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200561 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200562 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
Willy Tarreauc2adf8b2011-09-07 12:13:34 +0200563 return -1;
564 }
565 }
566 global.stats_fe->maxconn = maxconn;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200567 }
Willy Tarreau35b7b162012-10-22 23:17:18 +0200568 else if (!strcmp(args[1], "bind-process")) { /* enable the socket only on some processes */
569 int cur_arg = 2;
Willy Tarreaua9db57e2013-01-18 11:29:29 +0100570 unsigned long set = 0;
Willy Tarreau35b7b162012-10-22 23:17:18 +0200571
Willy Tarreau91319572013-04-20 09:48:50 +0200572 if (!global.stats_fe) {
573 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
574 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
575 return -1;
576 }
577 }
578
Willy Tarreau35b7b162012-10-22 23:17:18 +0200579 while (*args[cur_arg]) {
Willy Tarreau110ecc12012-11-15 17:50:01 +0100580 unsigned int low, high;
581
Willy Tarreau35b7b162012-10-22 23:17:18 +0200582 if (strcmp(args[cur_arg], "all") == 0) {
583 set = 0;
584 break;
585 }
586 else if (strcmp(args[cur_arg], "odd") == 0) {
Willy Tarreaua9db57e2013-01-18 11:29:29 +0100587 set |= ~0UL/3UL; /* 0x555....555 */
Willy Tarreau35b7b162012-10-22 23:17:18 +0200588 }
589 else if (strcmp(args[cur_arg], "even") == 0) {
Willy Tarreaua9db57e2013-01-18 11:29:29 +0100590 set |= (~0UL/3UL) << 1; /* 0xAAA...AAA */
Willy Tarreau35b7b162012-10-22 23:17:18 +0200591 }
Willy Tarreau83d84cf2012-11-22 01:04:31 +0100592 else if (isdigit((int)*args[cur_arg])) {
Willy Tarreau110ecc12012-11-15 17:50:01 +0100593 char *dash = strchr(args[cur_arg], '-');
594
595 low = high = str2uic(args[cur_arg]);
596 if (dash)
597 high = str2uic(dash + 1);
598
599 if (high < low) {
600 unsigned int swap = low;
601 low = high;
602 high = swap;
603 }
604
Willy Tarreaua9db57e2013-01-18 11:29:29 +0100605 if (low < 1 || high > LONGBITS) {
606 memprintf(err, "'%s %s' supports process numbers from 1 to %d.\n",
607 args[0], args[1], LONGBITS);
Willy Tarreau35b7b162012-10-22 23:17:18 +0200608 return -1;
609 }
Willy Tarreau110ecc12012-11-15 17:50:01 +0100610 while (low <= high)
Willy Tarreaua9db57e2013-01-18 11:29:29 +0100611 set |= 1UL << (low++ - 1);
Willy Tarreau110ecc12012-11-15 17:50:01 +0100612 }
613 else {
614 memprintf(err,
Willy Tarreaua9db57e2013-01-18 11:29:29 +0100615 "'%s %s' expects 'all', 'odd', 'even', or a list of process ranges with numbers from 1 to %d.\n",
616 args[0], args[1], LONGBITS);
Willy Tarreau110ecc12012-11-15 17:50:01 +0100617 return -1;
Willy Tarreau35b7b162012-10-22 23:17:18 +0200618 }
619 cur_arg++;
620 }
621 global.stats_fe->bind_proc = set;
622 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200623 else {
Willy Tarreau35b7b162012-10-22 23:17:18 +0200624 memprintf(err, "'%s' only supports 'socket', 'maxconn', 'bind-process' and 'timeout' (got '%s')", args[0], args[1]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200625 return -1;
626 }
627 return 0;
628}
629
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100630/* Dumps the stats CSV header to the trash buffer which. The caller is responsible
631 * for clearing it if needed.
Willy Tarreauf522f3d2014-02-10 22:22:49 +0100632 * NOTE: Some tools happen to rely on the field position instead of its name,
633 * so please only append new fields at the end, never in the middle.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100634 */
635static void stats_dump_csv_header()
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100636{
Willy Tarreauf6142292016-01-04 17:24:29 +0100637 int field;
638
639 chunk_appendf(&trash, "# ");
640 for (field = 0; field < ST_F_TOTAL_FIELDS; field++)
641 chunk_appendf(&trash, "%s,", stat_field_names[field]);
642
643 chunk_appendf(&trash, "\n");
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100644}
645
Simon Hormand9366582011-06-15 15:18:45 +0900646/* print a string of text buffer to <out>. The format is :
647 * Non-printable chars \t, \n, \r and \e are * encoded in C format.
648 * Other non-printable chars are encoded "\xHH". Space and '\' are also escaped.
649 * Print stopped if null char or <bsize> is reached, or if no more place in the chunk.
650 */
651static int dump_text(struct chunk *out, const char *buf, int bsize)
652{
653 unsigned char c;
654 int ptr = 0;
655
656 while (buf[ptr] && ptr < bsize) {
657 c = buf[ptr];
658 if (isprint(c) && isascii(c) && c != '\\' && c != ' ') {
659 if (out->len > out->size - 1)
660 break;
661 out->str[out->len++] = c;
662 }
663 else if (c == '\t' || c == '\n' || c == '\r' || c == '\e' || c == '\\' || c == ' ') {
664 if (out->len > out->size - 2)
665 break;
666 out->str[out->len++] = '\\';
667 switch (c) {
668 case ' ': c = ' '; break;
669 case '\t': c = 't'; break;
670 case '\n': c = 'n'; break;
671 case '\r': c = 'r'; break;
672 case '\e': c = 'e'; break;
673 case '\\': c = '\\'; break;
674 }
675 out->str[out->len++] = c;
676 }
677 else {
678 if (out->len > out->size - 4)
679 break;
680 out->str[out->len++] = '\\';
681 out->str[out->len++] = 'x';
682 out->str[out->len++] = hextab[(c >> 4) & 0xF];
683 out->str[out->len++] = hextab[c & 0xF];
684 }
685 ptr++;
686 }
687
688 return ptr;
689}
690
691/* print a buffer in hexa.
692 * Print stopped if <bsize> is reached, or if no more place in the chunk.
693 */
694static int dump_binary(struct chunk *out, const char *buf, int bsize)
695{
696 unsigned char c;
697 int ptr = 0;
698
699 while (ptr < bsize) {
700 c = buf[ptr];
701
702 if (out->len > out->size - 2)
703 break;
704 out->str[out->len++] = hextab[(c >> 4) & 0xF];
705 out->str[out->len++] = hextab[c & 0xF];
706
707 ptr++;
708 }
709 return ptr;
710}
711
712/* Dump the status of a table to a stream interface's
713 * read buffer. It returns 0 if the output buffer is full
714 * and needs to be called again, otherwise non-zero.
715 */
716static int stats_dump_table_head_to_buffer(struct chunk *msg, struct stream_interface *si,
717 struct proxy *proxy, struct proxy *target)
718{
Willy Tarreau87b09662015-04-03 00:22:06 +0200719 struct stream *s = si_strm(si);
Simon Hormand9366582011-06-15 15:18:45 +0900720
Willy Tarreau77804732012-10-29 16:14:26 +0100721 chunk_appendf(msg, "# table: %s, type: %s, size:%d, used:%d\n",
Simon Hormand9366582011-06-15 15:18:45 +0900722 proxy->id, stktable_types[proxy->table.type].kw, proxy->table.size, proxy->table.current);
723
724 /* any other information should be dumped here */
725
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200726 if (target && strm_li(s)->bind_conf->level < ACCESS_LVL_OPER)
Willy Tarreau77804732012-10-29 16:14:26 +0100727 chunk_appendf(msg, "# contents not dumped due to insufficient privileges\n");
Simon Hormand9366582011-06-15 15:18:45 +0900728
Willy Tarreaubc18da12015-03-13 14:00:47 +0100729 if (bi_putchk(si_ic(si), msg) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +0200730 si_applet_cant_put(si);
Simon Hormand9366582011-06-15 15:18:45 +0900731 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +0100732 }
Simon Hormand9366582011-06-15 15:18:45 +0900733
734 return 1;
735}
736
737/* Dump the a table entry to a stream interface's
738 * read buffer. It returns 0 if the output buffer is full
739 * and needs to be called again, otherwise non-zero.
740 */
741static int stats_dump_table_entry_to_buffer(struct chunk *msg, struct stream_interface *si,
742 struct proxy *proxy, struct stksess *entry)
743{
744 int dt;
745
Willy Tarreau77804732012-10-29 16:14:26 +0100746 chunk_appendf(msg, "%p:", entry);
Simon Hormand9366582011-06-15 15:18:45 +0900747
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200748 if (proxy->table.type == SMP_T_IPV4) {
Simon Hormand9366582011-06-15 15:18:45 +0900749 char addr[INET_ADDRSTRLEN];
750 inet_ntop(AF_INET, (const void *)&entry->key.key, addr, sizeof(addr));
Willy Tarreau77804732012-10-29 16:14:26 +0100751 chunk_appendf(msg, " key=%s", addr);
Simon Hormand9366582011-06-15 15:18:45 +0900752 }
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200753 else if (proxy->table.type == SMP_T_IPV6) {
Simon Hormand9366582011-06-15 15:18:45 +0900754 char addr[INET6_ADDRSTRLEN];
755 inet_ntop(AF_INET6, (const void *)&entry->key.key, addr, sizeof(addr));
Willy Tarreau77804732012-10-29 16:14:26 +0100756 chunk_appendf(msg, " key=%s", addr);
Simon Hormand9366582011-06-15 15:18:45 +0900757 }
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200758 else if (proxy->table.type == SMP_T_SINT) {
Willy Tarreau77804732012-10-29 16:14:26 +0100759 chunk_appendf(msg, " key=%u", *(unsigned int *)entry->key.key);
Simon Hormand9366582011-06-15 15:18:45 +0900760 }
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200761 else if (proxy->table.type == SMP_T_STR) {
Willy Tarreau77804732012-10-29 16:14:26 +0100762 chunk_appendf(msg, " key=");
Simon Hormand9366582011-06-15 15:18:45 +0900763 dump_text(msg, (const char *)entry->key.key, proxy->table.key_size);
764 }
765 else {
Willy Tarreau77804732012-10-29 16:14:26 +0100766 chunk_appendf(msg, " key=");
Simon Hormand9366582011-06-15 15:18:45 +0900767 dump_binary(msg, (const char *)entry->key.key, proxy->table.key_size);
768 }
769
Willy Tarreau77804732012-10-29 16:14:26 +0100770 chunk_appendf(msg, " use=%d exp=%d", entry->ref_cnt - 1, tick_remain(now_ms, entry->expire));
Simon Hormand9366582011-06-15 15:18:45 +0900771
772 for (dt = 0; dt < STKTABLE_DATA_TYPES; dt++) {
773 void *ptr;
774
775 if (proxy->table.data_ofs[dt] == 0)
776 continue;
777 if (stktable_data_types[dt].arg_type == ARG_T_DELAY)
Willy Tarreau77804732012-10-29 16:14:26 +0100778 chunk_appendf(msg, " %s(%d)=", stktable_data_types[dt].name, proxy->table.data_arg[dt].u);
Simon Hormand9366582011-06-15 15:18:45 +0900779 else
Willy Tarreau77804732012-10-29 16:14:26 +0100780 chunk_appendf(msg, " %s=", stktable_data_types[dt].name);
Simon Hormand9366582011-06-15 15:18:45 +0900781
782 ptr = stktable_data_ptr(&proxy->table, entry, dt);
783 switch (stktable_data_types[dt].std_type) {
784 case STD_T_SINT:
Willy Tarreau77804732012-10-29 16:14:26 +0100785 chunk_appendf(msg, "%d", stktable_data_cast(ptr, std_t_sint));
Simon Hormand9366582011-06-15 15:18:45 +0900786 break;
787 case STD_T_UINT:
Willy Tarreau77804732012-10-29 16:14:26 +0100788 chunk_appendf(msg, "%u", stktable_data_cast(ptr, std_t_uint));
Simon Hormand9366582011-06-15 15:18:45 +0900789 break;
790 case STD_T_ULL:
Willy Tarreau77804732012-10-29 16:14:26 +0100791 chunk_appendf(msg, "%lld", stktable_data_cast(ptr, std_t_ull));
Simon Hormand9366582011-06-15 15:18:45 +0900792 break;
793 case STD_T_FRQP:
Willy Tarreau77804732012-10-29 16:14:26 +0100794 chunk_appendf(msg, "%d",
Simon Hormand9366582011-06-15 15:18:45 +0900795 read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
796 proxy->table.data_arg[dt].u));
797 break;
798 }
799 }
Willy Tarreau77804732012-10-29 16:14:26 +0100800 chunk_appendf(msg, "\n");
Simon Hormand9366582011-06-15 15:18:45 +0900801
Willy Tarreaubc18da12015-03-13 14:00:47 +0100802 if (bi_putchk(si_ic(si), msg) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +0200803 si_applet_cant_put(si);
Simon Hormand9366582011-06-15 15:18:45 +0900804 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +0100805 }
Simon Hormand9366582011-06-15 15:18:45 +0900806
807 return 1;
808}
809
Willy Tarreaudec98142012-06-06 23:37:08 +0200810static void stats_sock_table_key_request(struct stream_interface *si, char **args, int action)
Simon Horman121f3052011-06-15 15:18:46 +0900811{
Willy Tarreau87b09662015-04-03 00:22:06 +0200812 struct stream *s = si_strm(si);
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100813 struct appctx *appctx = __objt_appctx(si->end);
814 struct proxy *px = appctx->ctx.table.target;
Simon Horman121f3052011-06-15 15:18:46 +0900815 struct stksess *ts;
Simon Hormancec9a222011-06-15 15:18:51 +0900816 uint32_t uint32_key;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900817 unsigned char ip6_key[sizeof(struct in6_addr)];
Willy Tarreau654694e2012-06-07 01:03:16 +0200818 long long value;
819 int data_type;
Willy Tarreau47060b62013-08-01 21:11:42 +0200820 int cur_arg;
Willy Tarreau654694e2012-06-07 01:03:16 +0200821 void *ptr;
822 struct freq_ctr_period *frqp;
Simon Horman121f3052011-06-15 15:18:46 +0900823
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100824 appctx->st0 = STAT_CLI_OUTPUT;
Simon Horman121f3052011-06-15 15:18:46 +0900825
826 if (!*args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100827 appctx->ctx.cli.msg = "Key value expected\n";
828 appctx->st0 = STAT_CLI_PRINT;
Simon Horman121f3052011-06-15 15:18:46 +0900829 return;
830 }
831
Simon Hormanc5b89f62011-06-15 15:18:50 +0900832 switch (px->table.type) {
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200833 case SMP_T_IPV4:
Simon Hormancec9a222011-06-15 15:18:51 +0900834 uint32_key = htonl(inetaddr_host(args[4]));
Willy Tarreau07115412012-10-29 21:56:59 +0100835 static_table_key->key = &uint32_key;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900836 break;
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200837 case SMP_T_IPV6:
Simon Hormanc5b89f62011-06-15 15:18:50 +0900838 inet_pton(AF_INET6, args[4], ip6_key);
Willy Tarreau07115412012-10-29 21:56:59 +0100839 static_table_key->key = &ip6_key;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900840 break;
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200841 case SMP_T_SINT:
Simon Hormancec9a222011-06-15 15:18:51 +0900842 {
843 char *endptr;
844 unsigned long val;
845 errno = 0;
846 val = strtoul(args[4], &endptr, 10);
847 if ((errno == ERANGE && val == ULONG_MAX) ||
848 (errno != 0 && val == 0) || endptr == args[4] ||
849 val > 0xffffffff) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100850 appctx->ctx.cli.msg = "Invalid key\n";
851 appctx->st0 = STAT_CLI_PRINT;
Simon Hormancec9a222011-06-15 15:18:51 +0900852 return;
853 }
854 uint32_key = (uint32_t) val;
Willy Tarreau07115412012-10-29 21:56:59 +0100855 static_table_key->key = &uint32_key;
Simon Hormancec9a222011-06-15 15:18:51 +0900856 break;
857 }
858 break;
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200859 case SMP_T_STR:
Willy Tarreau07115412012-10-29 21:56:59 +0100860 static_table_key->key = args[4];
861 static_table_key->key_len = strlen(args[4]);
Simon Horman619e3cc2011-06-15 15:18:52 +0900862 break;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900863 default:
Willy Tarreaudec98142012-06-06 23:37:08 +0200864 switch (action) {
865 case STAT_CLI_O_TAB:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100866 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 +0200867 break;
868 case STAT_CLI_O_CLR:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100869 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 +0200870 break;
871 default:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100872 appctx->ctx.cli.msg = "Unknown action\n";
Willy Tarreaudec98142012-06-06 23:37:08 +0200873 break;
874 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100875 appctx->st0 = STAT_CLI_PRINT;
Simon Horman121f3052011-06-15 15:18:46 +0900876 return;
877 }
878
879 /* check permissions */
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200880 if (strm_li(s)->bind_conf->level < ACCESS_LVL_OPER) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100881 appctx->ctx.cli.msg = stats_permission_denied_msg;
882 appctx->st0 = STAT_CLI_PRINT;
Simon Horman121f3052011-06-15 15:18:46 +0900883 return;
884 }
885
Willy Tarreau07115412012-10-29 21:56:59 +0100886 ts = stktable_lookup_key(&px->table, static_table_key);
Simon Horman17bce342011-06-15 15:18:47 +0900887
Willy Tarreaudec98142012-06-06 23:37:08 +0200888 switch (action) {
889 case STAT_CLI_O_TAB:
890 if (!ts)
891 return;
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100892 chunk_reset(&trash);
893 if (!stats_dump_table_head_to_buffer(&trash, si, px, px))
Simon Horman17bce342011-06-15 15:18:47 +0900894 return;
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100895 stats_dump_table_entry_to_buffer(&trash, si, px, ts);
Simon Horman121f3052011-06-15 15:18:46 +0900896 return;
Willy Tarreaudec98142012-06-06 23:37:08 +0200897
898 case STAT_CLI_O_CLR:
899 if (!ts)
900 return;
901 if (ts->ref_cnt) {
902 /* don't delete an entry which is currently referenced */
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100903 appctx->ctx.cli.msg = "Entry currently in use, cannot remove\n";
904 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaudec98142012-06-06 23:37:08 +0200905 return;
906 }
907 stksess_kill(&px->table, ts);
908 break;
Simon Horman17bce342011-06-15 15:18:47 +0900909
Willy Tarreau654694e2012-06-07 01:03:16 +0200910 case STAT_CLI_O_SET:
Willy Tarreau654694e2012-06-07 01:03:16 +0200911 if (ts)
912 stktable_touch(&px->table, ts, 1);
913 else {
Willy Tarreau07115412012-10-29 21:56:59 +0100914 ts = stksess_new(&px->table, static_table_key);
Willy Tarreau654694e2012-06-07 01:03:16 +0200915 if (!ts) {
916 /* don't delete an entry which is currently referenced */
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100917 appctx->ctx.cli.msg = "Unable to allocate a new entry\n";
918 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau654694e2012-06-07 01:03:16 +0200919 return;
920 }
921 stktable_store(&px->table, ts, 1);
922 }
923
Willy Tarreau47060b62013-08-01 21:11:42 +0200924 for (cur_arg = 5; *args[cur_arg]; cur_arg += 2) {
925 if (strncmp(args[cur_arg], "data.", 5) != 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100926 appctx->ctx.cli.msg = "\"data.<type>\" followed by a value expected\n";
927 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau47060b62013-08-01 21:11:42 +0200928 return;
929 }
930
931 data_type = stktable_get_data_type(args[cur_arg] + 5);
932 if (data_type < 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100933 appctx->ctx.cli.msg = "Unknown data type\n";
934 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau47060b62013-08-01 21:11:42 +0200935 return;
936 }
937
938 if (!px->table.data_ofs[data_type]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100939 appctx->ctx.cli.msg = "Data type not stored in this table\n";
940 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau47060b62013-08-01 21:11:42 +0200941 return;
942 }
943
944 if (!*args[cur_arg+1] || strl2llrc(args[cur_arg+1], strlen(args[cur_arg+1]), &value) != 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100945 appctx->ctx.cli.msg = "Require a valid integer value to store\n";
946 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau47060b62013-08-01 21:11:42 +0200947 return;
948 }
949
950 ptr = stktable_data_ptr(&px->table, ts, data_type);
951
952 switch (stktable_data_types[data_type].std_type) {
953 case STD_T_SINT:
954 stktable_data_cast(ptr, std_t_sint) = value;
955 break;
956 case STD_T_UINT:
957 stktable_data_cast(ptr, std_t_uint) = value;
958 break;
959 case STD_T_ULL:
960 stktable_data_cast(ptr, std_t_ull) = value;
961 break;
962 case STD_T_FRQP:
963 /* We set both the current and previous values. That way
964 * the reported frequency is stable during all the period
965 * then slowly fades out. This allows external tools to
966 * push measures without having to update them too often.
967 */
968 frqp = &stktable_data_cast(ptr, std_t_frqp);
969 frqp->curr_tick = now_ms;
970 frqp->prev_ctr = 0;
971 frqp->curr_ctr = value;
972 break;
973 }
Willy Tarreau654694e2012-06-07 01:03:16 +0200974 }
975 break;
976
Willy Tarreaudec98142012-06-06 23:37:08 +0200977 default:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100978 appctx->ctx.cli.msg = "Unknown action\n";
979 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaudec98142012-06-06 23:37:08 +0200980 break;
Simon Horman121f3052011-06-15 15:18:46 +0900981 }
Simon Horman121f3052011-06-15 15:18:46 +0900982}
983
Willy Tarreau654694e2012-06-07 01:03:16 +0200984static void stats_sock_table_data_request(struct stream_interface *si, char **args, int action)
Simon Hormand5b9fd92011-06-15 15:18:48 +0900985{
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100986 struct appctx *appctx = __objt_appctx(si->end);
987
Willy Tarreau04b3a192013-04-13 09:41:37 +0200988 if (action != STAT_CLI_O_TAB && action != STAT_CLI_O_CLR) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100989 appctx->ctx.cli.msg = "content-based lookup is only supported with the \"show\" and \"clear\" actions";
990 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau654694e2012-06-07 01:03:16 +0200991 return;
992 }
993
Simon Hormand5b9fd92011-06-15 15:18:48 +0900994 /* condition on stored data value */
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100995 appctx->ctx.table.data_type = stktable_get_data_type(args[3] + 5);
996 if (appctx->ctx.table.data_type < 0) {
997 appctx->ctx.cli.msg = "Unknown data type\n";
998 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900999 return;
1000 }
1001
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001002 if (!((struct proxy *)appctx->ctx.table.target)->table.data_ofs[appctx->ctx.table.data_type]) {
1003 appctx->ctx.cli.msg = "Data type not stored in this table\n";
1004 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +09001005 return;
1006 }
1007
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001008 appctx->ctx.table.data_op = get_std_op(args[4]);
1009 if (appctx->ctx.table.data_op < 0) {
1010 appctx->ctx.cli.msg = "Require and operator among \"eq\", \"ne\", \"le\", \"ge\", \"lt\", \"gt\"\n";
1011 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +09001012 return;
1013 }
1014
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001015 if (!*args[5] || strl2llrc(args[5], strlen(args[5]), &appctx->ctx.table.value) != 0) {
1016 appctx->ctx.cli.msg = "Require a valid integer value to compare against\n";
1017 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +09001018 return;
1019 }
1020}
1021
Willy Tarreaudec98142012-06-06 23:37:08 +02001022static void stats_sock_table_request(struct stream_interface *si, char **args, int action)
Simon Hormand5b9fd92011-06-15 15:18:48 +09001023{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001024 struct appctx *appctx = __objt_appctx(si->end);
1025
1026 appctx->ctx.table.data_type = -1;
1027 appctx->st2 = STAT_ST_INIT;
1028 appctx->ctx.table.target = NULL;
1029 appctx->ctx.table.proxy = NULL;
1030 appctx->ctx.table.entry = NULL;
1031 appctx->st0 = action;
Simon Hormand5b9fd92011-06-15 15:18:48 +09001032
1033 if (*args[2]) {
Willy Tarreaue2dc1fa2015-05-26 12:08:07 +02001034 appctx->ctx.table.target = proxy_tbl_by_name(args[2]);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001035 if (!appctx->ctx.table.target) {
1036 appctx->ctx.cli.msg = "No such table\n";
1037 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +09001038 return;
1039 }
1040 }
1041 else {
Willy Tarreaudec98142012-06-06 23:37:08 +02001042 if (action != STAT_CLI_O_TAB)
Simon Hormand5b9fd92011-06-15 15:18:48 +09001043 goto err_args;
1044 return;
1045 }
1046
1047 if (strcmp(args[3], "key") == 0)
Willy Tarreaudec98142012-06-06 23:37:08 +02001048 stats_sock_table_key_request(si, args, action);
Simon Hormanc88b8872011-06-15 15:18:49 +09001049 else if (strncmp(args[3], "data.", 5) == 0)
Willy Tarreau654694e2012-06-07 01:03:16 +02001050 stats_sock_table_data_request(si, args, action);
Simon Hormanc88b8872011-06-15 15:18:49 +09001051 else if (*args[3])
Simon Hormand5b9fd92011-06-15 15:18:48 +09001052 goto err_args;
1053
1054 return;
1055
1056err_args:
Willy Tarreaudec98142012-06-06 23:37:08 +02001057 switch (action) {
1058 case STAT_CLI_O_TAB:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001059 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 +02001060 break;
1061 case STAT_CLI_O_CLR:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001062 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 +02001063 break;
1064 default:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001065 appctx->ctx.cli.msg = "Unknown action\n";
Willy Tarreaudec98142012-06-06 23:37:08 +02001066 break;
1067 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001068 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +09001069}
1070
Willy Tarreau532a4502011-09-07 22:37:44 +02001071/* Expects to find a frontend named <arg> and returns it, otherwise displays various
Willy Tarreau87b09662015-04-03 00:22:06 +02001072 * adequate error messages and returns NULL. This function also expects the stream
Willy Tarreau532a4502011-09-07 22:37:44 +02001073 * level to be admin.
1074 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001075static struct proxy *expect_frontend_admin(struct stream *s, struct stream_interface *si, const char *arg)
Willy Tarreau532a4502011-09-07 22:37:44 +02001076{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001077 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau532a4502011-09-07 22:37:44 +02001078 struct proxy *px;
1079
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001080 if (strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001081 appctx->ctx.cli.msg = stats_permission_denied_msg;
1082 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001083 return NULL;
1084 }
1085
1086 if (!*arg) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001087 appctx->ctx.cli.msg = "A frontend name is expected.\n";
1088 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001089 return NULL;
1090 }
1091
Willy Tarreau9e0bb102015-05-26 11:24:42 +02001092 px = proxy_fe_by_name(arg);
Willy Tarreau532a4502011-09-07 22:37:44 +02001093 if (!px) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001094 appctx->ctx.cli.msg = "No such frontend.\n";
1095 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001096 return NULL;
1097 }
1098 return px;
1099}
1100
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001101/* Expects to find a backend and a server in <arg> under the form <backend>/<server>,
1102 * and returns the pointer to the server. Otherwise, display adequate error messages
Willy Tarreau87b09662015-04-03 00:22:06 +02001103 * and returns NULL. This function also expects the stream level to be admin. Note:
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001104 * the <arg> is modified to remove the '/'.
1105 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001106static struct server *expect_server_admin(struct stream *s, struct stream_interface *si, char *arg)
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001107{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001108 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001109 struct proxy *px;
1110 struct server *sv;
1111 char *line;
1112
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001113 if (strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001114 appctx->ctx.cli.msg = stats_permission_denied_msg;
1115 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001116 return NULL;
1117 }
1118
1119 /* split "backend/server" and make <line> point to server */
1120 for (line = arg; *line; line++)
1121 if (*line == '/') {
1122 *line++ = '\0';
1123 break;
1124 }
1125
1126 if (!*line || !*arg) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001127 appctx->ctx.cli.msg = "Require 'backend/server'.\n";
1128 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001129 return NULL;
1130 }
1131
1132 if (!get_backend_server(arg, line, &px, &sv)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001133 appctx->ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
1134 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001135 return NULL;
1136 }
1137
1138 if (px->state == PR_STSTOPPED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001139 appctx->ctx.cli.msg = "Proxy is disabled.\n";
1140 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001141 return NULL;
1142 }
1143
1144 return sv;
1145}
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02001146
1147/* This function is used with TLS ticket keys management. It permits to browse
1148 * each reference. The variable <getnext> must contain the current node,
1149 * <end> point to the root node.
1150 */
1151#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
1152static inline
1153struct tls_keys_ref *tlskeys_list_get_next(struct tls_keys_ref *getnext, struct list *end)
1154{
1155 struct tls_keys_ref *ref = getnext;
1156
1157 while (1) {
1158
1159 /* Get next list entry. */
1160 ref = LIST_NEXT(&ref->list, struct tls_keys_ref *, list);
1161
1162 /* If the entry is the last of the list, return NULL. */
1163 if (&ref->list == end)
1164 return NULL;
1165
1166 return ref;
1167 }
1168}
1169
1170static inline
1171struct tls_keys_ref *tlskeys_ref_lookup_ref(const char *reference)
1172{
1173 int id;
1174 char *error;
1175
1176 /* If the reference starts by a '#', this is numeric id. */
1177 if (reference[0] == '#') {
1178 /* Try to convert the numeric id. If the conversion fails, the lookup fails. */
1179 id = strtol(reference + 1, &error, 10);
1180 if (*error != '\0')
1181 return NULL;
1182
1183 /* Perform the unique id lookup. */
1184 return tlskeys_ref_lookupid(id);
1185 }
1186
1187 /* Perform the string lookup. */
1188 return tlskeys_ref_lookup(reference);
1189}
1190#endif
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001191
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001192/* This function is used with map and acl management. It permits to browse
1193 * each reference. The variable <getnext> must contain the current node,
1194 * <end> point to the root node and the <flags> permit to filter required
1195 * nodes.
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001196 */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001197static inline
1198struct pat_ref *pat_list_get_next(struct pat_ref *getnext, struct list *end,
1199 unsigned int flags)
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001200{
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001201 struct pat_ref *ref = getnext;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001202
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001203 while (1) {
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001204
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001205 /* Get next list entry. */
1206 ref = LIST_NEXT(&ref->list, struct pat_ref *, list);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001207
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001208 /* If the entry is the last of the list, return NULL. */
1209 if (&ref->list == end)
1210 return NULL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001211
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001212 /* If the entry match the flag, return it. */
1213 if (ref->flags & flags)
1214 return ref;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001215 }
1216}
1217
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001218static inline
1219struct pat_ref *pat_ref_lookup_ref(const char *reference)
1220{
1221 int id;
1222 char *error;
1223
1224 /* If the reference starts by a '#', this is numeric id. */
1225 if (reference[0] == '#') {
1226 /* Try to convert the numeric id. If the conversion fails, the lookup fails. */
1227 id = strtol(reference + 1, &error, 10);
1228 if (*error != '\0')
1229 return NULL;
1230
1231 /* Perform the unique id lookup. */
1232 return pat_ref_lookupid(id);
1233 }
1234
1235 /* Perform the string lookup. */
1236 return pat_ref_lookup(reference);
1237}
1238
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001239/* This function is used with map and acl management. It permits to browse
1240 * each reference.
1241 */
1242static inline
1243struct pattern_expr *pat_expr_get_next(struct pattern_expr *getnext, struct list *end)
1244{
1245 struct pattern_expr *expr;
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01001246 expr = LIST_NEXT(&getnext->list, struct pattern_expr *, list);
1247 if (&expr->list == end)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001248 return NULL;
1249 return expr;
1250}
1251
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001252/* Processes the stats interpreter on the statistics socket. This function is
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001253 * called from an applet running in a stream interface. The function returns 1
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001254 * if the request was understood, otherwise zero. It sets appctx->st0 to a value
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02001255 * designating the function which will have to process the request, which can
1256 * also be the print function to display the return message set into cli.msg.
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001257 */
Simon Horman9bd2c732011-06-15 15:18:44 +09001258static int stats_sock_parse_request(struct stream_interface *si, char *line)
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001259{
Willy Tarreau87b09662015-04-03 00:22:06 +02001260 struct stream *s = si_strm(si);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001261 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001262 char *args[MAX_STATS_ARGS + 1];
1263 int arg;
Thierry FOURNIER48bcfda2013-12-10 18:54:58 +01001264 int i, j;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001265
1266 while (isspace((unsigned char)*line))
1267 line++;
1268
1269 arg = 0;
1270 args[arg] = line;
1271
1272 while (*line && arg < MAX_STATS_ARGS) {
Thierry FOURNIER48bcfda2013-12-10 18:54:58 +01001273 if (*line == '\\') {
1274 line++;
1275 if (*line == '\0')
1276 break;
1277 }
1278 else if (isspace((unsigned char)*line)) {
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001279 *line++ = '\0';
1280
1281 while (isspace((unsigned char)*line))
1282 line++;
1283
1284 args[++arg] = line;
1285 continue;
1286 }
1287
1288 line++;
1289 }
1290
1291 while (++arg <= MAX_STATS_ARGS)
1292 args[arg] = line;
1293
Thierry FOURNIER48bcfda2013-12-10 18:54:58 +01001294 /* remove \ */
1295 arg = 0;
1296 while (*args[arg] != '\0') {
1297 j = 0;
1298 for (i=0; args[arg][i] != '\0'; i++) {
1299 if (args[arg][i] == '\\')
1300 continue;
1301 args[arg][j] = args[arg][i];
1302 j++;
1303 }
1304 args[arg][j] = '\0';
1305 arg++;
1306 }
1307
Willy Tarreau6bcb95d2015-05-04 18:07:56 +02001308 appctx->ctx.stats.scope_str = 0;
1309 appctx->ctx.stats.scope_len = 0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001310 appctx->ctx.stats.flags = 0;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001311 if (strcmp(args[0], "show") == 0) {
Baptiste Assmann9b6857e2015-09-18 14:49:12 +02001312 if (strcmp(args[1], "backend") == 0) {
Cyril Bonté6ca9e012016-05-06 12:18:49 +02001313 appctx->ctx.be.px = NULL;
Baptiste Assmann9b6857e2015-09-18 14:49:12 +02001314 appctx->st2 = STAT_ST_INIT;
1315 appctx->st0 = STAT_CLI_O_BACKEND;
1316 }
Willy Tarreauae795722016-02-16 11:27:28 +01001317 else if (strcmp(args[1], "env") == 0) {
1318 extern char **environ;
1319
1320 if (strm_li(s)->bind_conf->level < ACCESS_LVL_OPER) {
1321 appctx->ctx.cli.msg = stats_permission_denied_msg;
1322 appctx->st0 = STAT_CLI_PRINT;
1323 return 1;
1324 }
1325 appctx->ctx.env.var = environ;
1326 appctx->st2 = STAT_ST_INIT;
1327 appctx->st0 = STAT_CLI_O_ENV; // stats_dump_env_to_buffer
1328
1329 if (*args[2]) {
1330 int len = strlen(args[2]);
1331
1332 for (; *appctx->ctx.env.var; appctx->ctx.env.var++) {
1333 if (strncmp(*appctx->ctx.env.var, args[2], len) == 0 &&
1334 (*appctx->ctx.env.var)[len] == '=')
1335 break;
1336 }
1337 if (!*appctx->ctx.env.var) {
1338 appctx->ctx.cli.msg = "Variable not found\n";
1339 appctx->st0 = STAT_CLI_PRINT;
1340 return 1;
1341 }
1342 appctx->st2 = STAT_ST_END;
1343 }
1344 }
1345 else if (strcmp(args[1], "stat") == 0) {
Baptiste Assmann3863f972015-05-17 00:33:24 +02001346 if (strcmp(args[2], "resolvers") == 0) {
1347 struct dns_resolvers *presolvers;
1348
Andrew Hayworth68d05342015-10-02 20:33:01 +00001349 if (*args[3]) {
1350 appctx->ctx.resolvers.ptr = NULL;
1351 list_for_each_entry(presolvers, &dns_resolvers, list) {
1352 if (strcmp(presolvers->id, args[3]) == 0) {
1353 appctx->ctx.resolvers.ptr = presolvers;
1354 break;
1355 }
Baptiste Assmann3863f972015-05-17 00:33:24 +02001356 }
Andrew Hayworth68d05342015-10-02 20:33:01 +00001357 if (appctx->ctx.resolvers.ptr == NULL) {
1358 appctx->ctx.cli.msg = "Can't find that resolvers section\n";
1359 appctx->st0 = STAT_CLI_PRINT;
1360 return 1;
1361 }
Baptiste Assmann3863f972015-05-17 00:33:24 +02001362 }
1363
1364 appctx->st2 = STAT_ST_INIT;
1365 appctx->st0 = STAT_CLI_O_RESOLVERS;
1366 return 1;
1367 }
1368 else if (*args[2] && *args[3] && *args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001369 appctx->ctx.stats.flags |= STAT_BOUND;
1370 appctx->ctx.stats.iid = atoi(args[2]);
1371 appctx->ctx.stats.type = atoi(args[3]);
1372 appctx->ctx.stats.sid = atoi(args[4]);
Willy Tarreau1e62df92016-01-11 18:57:53 +01001373 if (strcmp(args[5], "typed") == 0)
1374 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001375 }
Willy Tarreau1e62df92016-01-11 18:57:53 +01001376 else if (strcmp(args[2], "typed") == 0)
1377 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001378
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001379 appctx->st2 = STAT_ST_INIT;
1380 appctx->st0 = STAT_CLI_O_STAT; // stats_dump_stat_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001381 }
1382 else if (strcmp(args[1], "info") == 0) {
Willy Tarreaucb809122016-01-11 20:08:42 +01001383 if (strcmp(args[2], "typed") == 0)
1384 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001385 appctx->st2 = STAT_ST_INIT;
1386 appctx->st0 = STAT_CLI_O_INFO; // stats_dump_info_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001387 }
Baptiste Assmann28289462015-07-03 08:01:20 +02001388 else if (strcmp(args[1], "servers") == 0 && strcmp(args[2], "state") == 0) {
Cyril Bonté76a99782016-05-06 12:18:48 +02001389 appctx->ctx.server_state.iid = 0;
1390 appctx->ctx.server_state.px = NULL;
1391 appctx->ctx.server_state.sv = NULL;
Baptiste Assmann28289462015-07-03 08:01:20 +02001392
1393 /* check if a backend name has been provided */
1394 if (*args[3]) {
1395 /* read server state from local file */
Cyril Bonté76a99782016-05-06 12:18:48 +02001396 appctx->ctx.server_state.px = proxy_be_by_name(args[3]);
Baptiste Assmann28289462015-07-03 08:01:20 +02001397
Cyril Bonté76a99782016-05-06 12:18:48 +02001398 if (!appctx->ctx.server_state.px) {
Baptiste Assmann28289462015-07-03 08:01:20 +02001399 appctx->ctx.cli.msg = "Can't find backend.\n";
1400 appctx->st0 = STAT_CLI_PRINT;
1401 return 1;
1402 }
Cyril Bonté76a99782016-05-06 12:18:48 +02001403 appctx->ctx.server_state.iid = appctx->ctx.server_state.px->uuid;
Baptiste Assmann28289462015-07-03 08:01:20 +02001404 }
1405 appctx->st2 = STAT_ST_INIT;
1406 appctx->st0 = STAT_CLI_O_SERVERS_STATE; // stats_dump_servers_state_to_buffer
1407 return 1;
1408 }
Willy Tarreau12833bb2014-01-28 16:49:56 +01001409 else if (strcmp(args[1], "pools") == 0) {
1410 appctx->st2 = STAT_ST_INIT;
1411 appctx->st0 = STAT_CLI_O_POOLS; // stats_dump_pools_to_buffer
1412 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001413 else if (strcmp(args[1], "sess") == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001414 appctx->st2 = STAT_ST_INIT;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001415 if (strm_li(s)->bind_conf->level < ACCESS_LVL_OPER) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001416 appctx->ctx.cli.msg = stats_permission_denied_msg;
1417 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +02001418 return 1;
1419 }
Willy Tarreau76153662012-11-26 01:16:39 +01001420 if (*args[2] && strcmp(args[2], "all") == 0)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001421 appctx->ctx.sess.target = (void *)-1;
Willy Tarreau76153662012-11-26 01:16:39 +01001422 else if (*args[2])
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001423 appctx->ctx.sess.target = (void *)strtoul(args[2], NULL, 0);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01001424 else
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001425 appctx->ctx.sess.target = NULL;
Willy Tarreau87b09662015-04-03 00:22:06 +02001426 appctx->ctx.sess.section = 0; /* start with stream status */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001427 appctx->ctx.sess.pos = 0;
1428 appctx->st0 = STAT_CLI_O_SESS; // stats_dump_sess_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001429 }
1430 else if (strcmp(args[1], "errors") == 0) {
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001431 if (strm_li(s)->bind_conf->level < ACCESS_LVL_OPER) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001432 appctx->ctx.cli.msg = stats_permission_denied_msg;
1433 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +02001434 return 1;
1435 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001436 if (*args[2])
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001437 appctx->ctx.errors.iid = atoi(args[2]);
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001438 else
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001439 appctx->ctx.errors.iid = -1;
1440 appctx->ctx.errors.px = NULL;
1441 appctx->st2 = STAT_ST_INIT;
1442 appctx->st0 = STAT_CLI_O_ERR; // stats_dump_errors_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001443 }
Willy Tarreau69f58c82010-07-12 17:55:33 +02001444 else if (strcmp(args[1], "table") == 0) {
Willy Tarreaudec98142012-06-06 23:37:08 +02001445 stats_sock_table_request(si, args, STAT_CLI_O_TAB);
Willy Tarreau69f58c82010-07-12 17:55:33 +02001446 }
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02001447 else if (strcmp(args[1], "tls-keys") == 0) {
1448#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
William Lallemandcf9e7882016-06-14 17:45:18 +02001449 appctx->ctx.tlskeys.dump_all = 0;
William Lallemand1d0b36a2016-05-20 17:40:26 +02001450 /* no parameter, shows only file list */
1451 if (!*args[2]) {
William Lallemandcf9e7882016-06-14 17:45:18 +02001452 appctx->ctx.tlskeys.dump_all = 1;
William Lallemand1d0b36a2016-05-20 17:40:26 +02001453 appctx->st2 = STAT_ST_INIT;
1454 appctx->st0 = STAT_CLI_O_TLSK;
1455 return 1;
1456 }
1457
1458 if (args[2][0] == '*') {
1459 /* list every TLS ticket keys */
1460 appctx->ctx.tlskeys.ref = NULL;
William Lallemandcf9e7882016-06-14 17:45:18 +02001461 appctx->ctx.tlskeys.dump_all = 1;
William Lallemand1d0b36a2016-05-20 17:40:26 +02001462 } else {
1463 appctx->ctx.tlskeys.ref = tlskeys_ref_lookup_ref(args[2]);
1464 if(!appctx->ctx.tlskeys.ref) {
1465 appctx->ctx.cli.msg = "'show tls-keys' unable to locate referenced filename\n";
1466 appctx->st0 = STAT_CLI_PRINT;
1467 return 1;
1468 }
1469 }
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02001470 appctx->st2 = STAT_ST_INIT;
William Lallemand1d0b36a2016-05-20 17:40:26 +02001471 appctx->st0 = STAT_CLI_O_TLSK_ENT;
1472
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02001473#else
1474 appctx->ctx.cli.msg = "HAProxy was compiled against a version of OpenSSL "
1475 "that doesn't support specifying TLS ticket keys\n";
1476 appctx->st0 = STAT_CLI_PRINT;
1477#endif
1478 return 1;
1479 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001480 else if (strcmp(args[1], "map") == 0 ||
1481 strcmp(args[1], "acl") == 0) {
1482
1483 /* Set ACL or MAP flags. */
1484 if (args[1][0] == 'm')
1485 appctx->ctx.map.display_flags = PAT_REF_MAP;
1486 else
1487 appctx->ctx.map.display_flags = PAT_REF_ACL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001488
Cyril Bontédb98eb32016-05-06 12:18:50 +02001489 /* no parameter: display all map available */
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001490 if (!*args[2]) {
1491 appctx->st2 = STAT_ST_INIT;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001492 appctx->st0 = STAT_CLI_O_PATS;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001493 return 1;
1494 }
1495
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001496 /* lookup into the refs and check the map flag */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001497 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001498 if (!appctx->ctx.map.ref ||
1499 !(appctx->ctx.map.ref->flags & appctx->ctx.map.display_flags)) {
1500 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001501 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001502 else
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001503 appctx->ctx.cli.msg = "Unknown ACL identifier. Please use #<id> or <file>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001504 appctx->st0 = STAT_CLI_PRINT;
1505 return 1;
1506 }
1507 appctx->st2 = STAT_ST_INIT;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001508 appctx->st0 = STAT_CLI_O_PAT;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001509 }
Aman Guptaceafb4a2012-04-02 18:57:54 -07001510 else { /* neither "stat" nor "info" nor "sess" nor "errors" nor "table" */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001511 return 0;
1512 }
1513 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001514 else if (strcmp(args[0], "clear") == 0) {
1515 if (strcmp(args[1], "counters") == 0) {
1516 struct proxy *px;
1517 struct server *sv;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001518 struct listener *li;
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001519 int clrall = 0;
1520
1521 if (strcmp(args[2], "all") == 0)
1522 clrall = 1;
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001523
Willy Tarreau6162db22009-10-10 17:13:00 +02001524 /* check permissions */
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001525 if (strm_li(s)->bind_conf->level < ACCESS_LVL_OPER ||
1526 (clrall && strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001527 appctx->ctx.cli.msg = stats_permission_denied_msg;
1528 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +02001529 return 1;
1530 }
1531
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001532 for (px = proxy; px; px = px->next) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001533 if (clrall) {
1534 memset(&px->be_counters, 0, sizeof(px->be_counters));
1535 memset(&px->fe_counters, 0, sizeof(px->fe_counters));
1536 }
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001537 else {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001538 px->be_counters.conn_max = 0;
1539 px->be_counters.p.http.rps_max = 0;
1540 px->be_counters.sps_max = 0;
1541 px->be_counters.cps_max = 0;
1542 px->be_counters.nbpend_max = 0;
1543
1544 px->fe_counters.conn_max = 0;
1545 px->fe_counters.p.http.rps_max = 0;
1546 px->fe_counters.sps_max = 0;
1547 px->fe_counters.cps_max = 0;
1548 px->fe_counters.nbpend_max = 0;
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001549 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001550
1551 for (sv = px->srv; sv; sv = sv->next)
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001552 if (clrall)
1553 memset(&sv->counters, 0, sizeof(sv->counters));
1554 else {
1555 sv->counters.cur_sess_max = 0;
1556 sv->counters.nbpend_max = 0;
1557 sv->counters.sps_max = 0;
1558 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001559
Willy Tarreaua7944ad2012-09-26 21:03:11 +02001560 list_for_each_entry(li, &px->conf.listeners, by_fe)
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001561 if (li->counters) {
1562 if (clrall)
1563 memset(li->counters, 0, sizeof(*li->counters));
1564 else
1565 li->counters->conn_max = 0;
1566 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001567 }
1568
Willy Tarreau81c25d02011-09-07 15:17:21 +02001569 global.cps_max = 0;
Willy Tarreau93e7c002013-10-07 18:51:07 +02001570 global.sps_max = 0;
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001571 return 1;
1572 }
Willy Tarreau88ee3972010-07-13 13:48:00 +02001573 else if (strcmp(args[1], "table") == 0) {
Willy Tarreaudec98142012-06-06 23:37:08 +02001574 stats_sock_table_request(si, args, STAT_CLI_O_CLR);
Willy Tarreau88ee3972010-07-13 13:48:00 +02001575 /* end of processing */
1576 return 1;
1577 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001578 else if (strcmp(args[1], "map") == 0 || strcmp(args[1], "acl") == 0) {
1579 /* Set ACL or MAP flags. */
1580 if (args[1][0] == 'm')
1581 appctx->ctx.map.display_flags = PAT_REF_MAP;
1582 else
1583 appctx->ctx.map.display_flags = PAT_REF_ACL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001584
1585 /* no parameter */
1586 if (!*args[2]) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001587 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
1588 appctx->ctx.cli.msg = "Missing map identifier.\n";
1589 else
1590 appctx->ctx.cli.msg = "Missing ACL identifier.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001591 appctx->st0 = STAT_CLI_PRINT;
1592 return 1;
1593 }
1594
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001595 /* lookup into the refs and check the map flag */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001596 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001597 if (!appctx->ctx.map.ref ||
1598 !(appctx->ctx.map.ref->flags & appctx->ctx.map.display_flags)) {
1599 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001600 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001601 else
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001602 appctx->ctx.cli.msg = "Unknown ACL identifier. Please use #<id> or <file>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001603 appctx->st0 = STAT_CLI_PRINT;
1604 return 1;
1605 }
1606
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001607 /* Clear all. */
1608 pat_ref_prune(appctx->ctx.map.ref);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001609
1610 /* return response */
Thierry FOURNIER07e78c52014-12-18 15:28:01 +01001611 appctx->st0 = STAT_CLI_PROMPT;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001612 return 1;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001613 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001614 else {
Willy Tarreau88ee3972010-07-13 13:48:00 +02001615 /* unknown "clear" argument */
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001616 return 0;
1617 }
1618 }
Willy Tarreau38338fa2009-10-10 18:37:29 +02001619 else if (strcmp(args[0], "get") == 0) {
1620 if (strcmp(args[1], "weight") == 0) {
1621 struct proxy *px;
1622 struct server *sv;
1623
1624 /* split "backend/server" and make <line> point to server */
1625 for (line = args[2]; *line; line++)
1626 if (*line == '/') {
1627 *line++ = '\0';
1628 break;
1629 }
1630
1631 if (!*line) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001632 appctx->ctx.cli.msg = "Require 'backend/server'.\n";
1633 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau38338fa2009-10-10 18:37:29 +02001634 return 1;
1635 }
1636
1637 if (!get_backend_server(args[2], line, &px, &sv)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001638 appctx->ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
1639 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau38338fa2009-10-10 18:37:29 +02001640 return 1;
1641 }
1642
1643 /* return server's effective weight at the moment */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001644 snprintf(trash.str, trash.size, "%d (initial %d)\n", sv->uweight, sv->iweight);
Willy Tarreaubc18da12015-03-13 14:00:47 +01001645 if (bi_putstr(si_ic(si), trash.str) == -1)
Willy Tarreaufe127932015-04-21 19:23:39 +02001646 si_applet_cant_put(si);
Willy Tarreaubc18da12015-03-13 14:00:47 +01001647
Willy Tarreau38338fa2009-10-10 18:37:29 +02001648 return 1;
1649 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001650 else if (strcmp(args[1], "map") == 0 || strcmp(args[1], "acl") == 0) {
1651 /* Set flags. */
1652 if (args[1][0] == 'm')
1653 appctx->ctx.map.display_flags = PAT_REF_MAP;
1654 else
1655 appctx->ctx.map.display_flags = PAT_REF_ACL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001656
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001657 /* No parameter. */
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001658 if (!*args[2] || !*args[3]) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001659 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
1660 appctx->ctx.cli.msg = "Missing map identifier and/or key.\n";
1661 else
1662 appctx->ctx.cli.msg = "Missing ACL identifier and/or key.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001663 appctx->st0 = STAT_CLI_PRINT;
1664 return 1;
1665 }
1666
1667 /* lookup into the maps */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001668 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001669 if (!appctx->ctx.map.ref) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001670 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001671 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001672 else
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001673 appctx->ctx.cli.msg = "Unknown ACL identifier. Please use #<id> or <file>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001674 appctx->st0 = STAT_CLI_PRINT;
1675 return 1;
1676 }
1677
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001678 /* copy input string. The string must be allocated because
1679 * it may be used over multiple iterations. It's released
1680 * at the end and upon abort anyway.
1681 */
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001682 appctx->ctx.map.chunk.len = strlen(args[3]);
1683 appctx->ctx.map.chunk.size = appctx->ctx.map.chunk.len + 1;
1684 appctx->ctx.map.chunk.str = strdup(args[3]);
1685 if (!appctx->ctx.map.chunk.str) {
1686 appctx->ctx.cli.msg = "Out of memory error.\n";
1687 appctx->st0 = STAT_CLI_PRINT;
1688 return 1;
1689 }
1690
1691 /* prepare response */
1692 appctx->st2 = STAT_ST_INIT;
1693 appctx->st0 = STAT_CLI_O_MLOOK;
1694 }
Willy Tarreau38338fa2009-10-10 18:37:29 +02001695 else { /* not "get weight" */
1696 return 0;
1697 }
1698 }
Willy Tarreau4483d432009-10-10 19:30:08 +02001699 else if (strcmp(args[0], "set") == 0) {
1700 if (strcmp(args[1], "weight") == 0) {
Willy Tarreau4483d432009-10-10 19:30:08 +02001701 struct server *sv;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001702 const char *warning;
Willy Tarreau4483d432009-10-10 19:30:08 +02001703
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001704 sv = expect_server_admin(s, si, args[2]);
1705 if (!sv)
Willy Tarreau4483d432009-10-10 19:30:08 +02001706 return 1;
Willy Tarreau4483d432009-10-10 19:30:08 +02001707
Simon Horman7d09b9a2013-02-12 10:45:51 +09001708 warning = server_parse_weight_change_request(sv, args[3]);
1709 if (warning) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001710 appctx->ctx.cli.msg = warning;
1711 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +02001712 }
Willy Tarreau4483d432009-10-10 19:30:08 +02001713 return 1;
1714 }
Willy Tarreau2a4b70f2014-05-22 18:42:35 +02001715 else if (strcmp(args[1], "server") == 0) {
1716 struct server *sv;
1717 const char *warning;
1718
1719 sv = expect_server_admin(s, si, args[2]);
1720 if (!sv)
1721 return 1;
1722
1723 if (strcmp(args[3], "weight") == 0) {
1724 warning = server_parse_weight_change_request(sv, args[4]);
1725 if (warning) {
1726 appctx->ctx.cli.msg = warning;
1727 appctx->st0 = STAT_CLI_PRINT;
1728 }
1729 }
1730 else if (strcmp(args[3], "state") == 0) {
1731 if (strcmp(args[4], "ready") == 0)
1732 srv_adm_set_ready(sv);
1733 else if (strcmp(args[4], "drain") == 0)
1734 srv_adm_set_drain(sv);
1735 else if (strcmp(args[4], "maint") == 0)
1736 srv_adm_set_maint(sv);
1737 else {
1738 appctx->ctx.cli.msg = "'set server <srv> state' expects 'ready', 'drain' and 'maint'.\n";
1739 appctx->st0 = STAT_CLI_PRINT;
1740 }
1741 }
1742 else if (strcmp(args[3], "health") == 0) {
1743 if (sv->track) {
1744 appctx->ctx.cli.msg = "cannot change health on a tracking server.\n";
1745 appctx->st0 = STAT_CLI_PRINT;
1746 }
1747 else if (strcmp(args[4], "up") == 0) {
1748 sv->check.health = sv->check.rise + sv->check.fall - 1;
1749 srv_set_running(sv, "changed from CLI");
1750 }
1751 else if (strcmp(args[4], "stopping") == 0) {
1752 sv->check.health = sv->check.rise + sv->check.fall - 1;
1753 srv_set_stopping(sv, "changed from CLI");
1754 }
1755 else if (strcmp(args[4], "down") == 0) {
1756 sv->check.health = 0;
1757 srv_set_stopped(sv, "changed from CLI");
1758 }
1759 else {
1760 appctx->ctx.cli.msg = "'set server <srv> health' expects 'up', 'stopping', or 'down'.\n";
1761 appctx->st0 = STAT_CLI_PRINT;
1762 }
1763 }
1764 else if (strcmp(args[3], "agent") == 0) {
1765 if (!(sv->agent.state & CHK_ST_ENABLED)) {
1766 appctx->ctx.cli.msg = "agent checks are not enabled on this server.\n";
1767 appctx->st0 = STAT_CLI_PRINT;
1768 }
1769 else if (strcmp(args[4], "up") == 0) {
1770 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
1771 srv_set_running(sv, "changed from CLI");
1772 }
1773 else if (strcmp(args[4], "down") == 0) {
1774 sv->agent.health = 0;
1775 srv_set_stopped(sv, "changed from CLI");
1776 }
1777 else {
1778 appctx->ctx.cli.msg = "'set server <srv> agent' expects 'up' or 'down'.\n";
1779 appctx->st0 = STAT_CLI_PRINT;
1780 }
1781 }
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001782 else if (strcmp(args[3], "addr") == 0) {
Thierry Fournier09a91782016-02-24 08:25:39 +01001783 warning = server_parse_addr_change_request(sv, args[4], "stats command");
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001784 if (warning) {
1785 appctx->ctx.cli.msg = warning;
1786 appctx->st0 = STAT_CLI_PRINT;
1787 }
1788 }
Willy Tarreau2a4b70f2014-05-22 18:42:35 +02001789 else {
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001790 appctx->ctx.cli.msg = "'set server <srv>' only supports 'agent', 'health', 'state', 'weight' and 'addr'.\n";
Willy Tarreau2a4b70f2014-05-22 18:42:35 +02001791 appctx->st0 = STAT_CLI_PRINT;
1792 }
1793 return 1;
1794 }
Willy Tarreau7aabd112010-01-26 10:59:06 +01001795 else if (strcmp(args[1], "timeout") == 0) {
1796 if (strcmp(args[2], "cli") == 0) {
1797 unsigned timeout;
1798 const char *res;
1799
1800 if (!*args[3]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001801 appctx->ctx.cli.msg = "Expects an integer value.\n";
1802 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001803 return 1;
1804 }
1805
1806 res = parse_time_err(args[3], &timeout, TIME_UNIT_S);
1807 if (res || timeout < 1) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001808 appctx->ctx.cli.msg = "Invalid timeout value.\n";
1809 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001810 return 1;
1811 }
1812
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001813 s->req.rto = s->res.wto = 1 + MS_TO_TICKS(timeout*1000);
Willy Tarreau7aabd112010-01-26 10:59:06 +01001814 return 1;
1815 }
1816 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001817 appctx->ctx.cli.msg = "'set timeout' only supports 'cli'.\n";
1818 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001819 return 1;
1820 }
1821 }
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001822 else if (strcmp(args[1], "maxconn") == 0) {
1823 if (strcmp(args[2], "frontend") == 0) {
1824 struct proxy *px;
1825 struct listener *l;
1826 int v;
1827
Willy Tarreau532a4502011-09-07 22:37:44 +02001828 px = expect_frontend_admin(s, si, args[3]);
1829 if (!px)
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001830 return 1;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001831
1832 if (!*args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001833 appctx->ctx.cli.msg = "Integer value expected.\n";
1834 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001835 return 1;
1836 }
1837
1838 v = atoi(args[4]);
Willy Tarreau3c7a79d2012-09-26 21:07:15 +02001839 if (v < 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001840 appctx->ctx.cli.msg = "Value out of range.\n";
1841 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001842 return 1;
1843 }
1844
1845 /* OK, the value is fine, so we assign it to the proxy and to all of
1846 * its listeners. The blocked ones will be dequeued.
1847 */
1848 px->maxconn = v;
Willy Tarreaua7944ad2012-09-26 21:03:11 +02001849 list_for_each_entry(l, &px->conf.listeners, by_fe) {
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001850 l->maxconn = v;
1851 if (l->state == LI_FULL)
1852 resume_listener(l);
1853 }
1854
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001855 if (px->maxconn > px->feconn && !LIST_ISEMPTY(&strm_fe(s)->listener_queue))
1856 dequeue_all_listeners(&strm_fe(s)->listener_queue);
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001857
1858 return 1;
1859 }
Andrew Hayworthedb93a72015-10-27 21:46:25 +00001860 else if (strcmp(args[2], "server") == 0) {
1861 struct server *sv;
Nenad Merdanovic5c3ed342016-04-24 23:10:07 +02001862 const char *warning;
Andrew Hayworthedb93a72015-10-27 21:46:25 +00001863
1864 sv = expect_server_admin(s, si, args[3]);
1865 if (!sv)
1866 return 1;
1867
Nenad Merdanovic5c3ed342016-04-24 23:10:07 +02001868 warning = server_parse_maxconn_change_request(sv, args[4]);
1869 if (warning) {
1870 appctx->ctx.cli.msg = warning;
Andrew Hayworthedb93a72015-10-27 21:46:25 +00001871 appctx->st0 = STAT_CLI_PRINT;
Andrew Hayworthedb93a72015-10-27 21:46:25 +00001872 }
1873
Andrew Hayworthedb93a72015-10-27 21:46:25 +00001874 return 1;
1875 }
Willy Tarreau91886b62011-09-07 14:38:31 +02001876 else if (strcmp(args[2], "global") == 0) {
1877 int v;
1878
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001879 if (strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001880 appctx->ctx.cli.msg = stats_permission_denied_msg;
1881 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau91886b62011-09-07 14:38:31 +02001882 return 1;
1883 }
1884
1885 if (!*args[3]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001886 appctx->ctx.cli.msg = "Expects an integer value.\n";
1887 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau91886b62011-09-07 14:38:31 +02001888 return 1;
1889 }
1890
1891 v = atoi(args[3]);
1892 if (v > global.hardmaxconn) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001893 appctx->ctx.cli.msg = "Value out of range.\n";
1894 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau91886b62011-09-07 14:38:31 +02001895 return 1;
1896 }
1897
1898 /* check for unlimited values */
1899 if (v <= 0)
1900 v = global.hardmaxconn;
1901
1902 global.maxconn = v;
1903
1904 /* Dequeues all of the listeners waiting for a resource */
1905 if (!LIST_ISEMPTY(&global_listener_queue))
1906 dequeue_all_listeners(&global_listener_queue);
1907
1908 return 1;
1909 }
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001910 else {
Andrew Hayworthedb93a72015-10-27 21:46:25 +00001911 appctx->ctx.cli.msg = "'set maxconn' only supports 'frontend', 'server', and 'global'.\n";
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001912 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001913 return 1;
1914 }
1915 }
Willy Tarreauf5b22872011-09-07 16:13:44 +02001916 else if (strcmp(args[1], "rate-limit") == 0) {
1917 if (strcmp(args[2], "connections") == 0) {
1918 if (strcmp(args[3], "global") == 0) {
1919 int v;
1920
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001921 if (strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001922 appctx->ctx.cli.msg = stats_permission_denied_msg;
1923 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001924 return 1;
1925 }
1926
1927 if (!*args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001928 appctx->ctx.cli.msg = "Expects an integer value.\n";
1929 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001930 return 1;
1931 }
1932
1933 v = atoi(args[4]);
1934 if (v < 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001935 appctx->ctx.cli.msg = "Value out of range.\n";
1936 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001937 return 1;
1938 }
1939
1940 global.cps_lim = v;
1941
1942 /* Dequeues all of the listeners waiting for a resource */
1943 if (!LIST_ISEMPTY(&global_listener_queue))
1944 dequeue_all_listeners(&global_listener_queue);
1945
1946 return 1;
1947 }
1948 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001949 appctx->ctx.cli.msg = "'set rate-limit connections' only supports 'global'.\n";
1950 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001951 return 1;
1952 }
1953 }
Willy Tarreau93e7c002013-10-07 18:51:07 +02001954 else if (strcmp(args[2], "sessions") == 0) {
1955 if (strcmp(args[3], "global") == 0) {
1956 int v;
1957
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001958 if (strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau93e7c002013-10-07 18:51:07 +02001959 appctx->ctx.cli.msg = stats_permission_denied_msg;
1960 appctx->st0 = STAT_CLI_PRINT;
1961 return 1;
1962 }
1963
1964 if (!*args[4]) {
1965 appctx->ctx.cli.msg = "Expects an integer value.\n";
1966 appctx->st0 = STAT_CLI_PRINT;
1967 return 1;
1968 }
1969
1970 v = atoi(args[4]);
1971 if (v < 0) {
1972 appctx->ctx.cli.msg = "Value out of range.\n";
1973 appctx->st0 = STAT_CLI_PRINT;
1974 return 1;
1975 }
1976
1977 global.sps_lim = v;
1978
1979 /* Dequeues all of the listeners waiting for a resource */
1980 if (!LIST_ISEMPTY(&global_listener_queue))
1981 dequeue_all_listeners(&global_listener_queue);
1982
1983 return 1;
1984 }
1985 else {
1986 appctx->ctx.cli.msg = "'set rate-limit sessions' only supports 'global'.\n";
1987 appctx->st0 = STAT_CLI_PRINT;
1988 return 1;
1989 }
1990 }
Willy Tarreaue43d5322013-10-07 20:01:52 +02001991#ifdef USE_OPENSSL
1992 else if (strcmp(args[2], "ssl-sessions") == 0) {
1993 if (strcmp(args[3], "global") == 0) {
1994 int v;
1995
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001996 if (strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreaue43d5322013-10-07 20:01:52 +02001997 appctx->ctx.cli.msg = stats_permission_denied_msg;
1998 appctx->st0 = STAT_CLI_PRINT;
1999 return 1;
2000 }
2001
2002 if (!*args[4]) {
2003 appctx->ctx.cli.msg = "Expects an integer value.\n";
2004 appctx->st0 = STAT_CLI_PRINT;
2005 return 1;
2006 }
2007
2008 v = atoi(args[4]);
2009 if (v < 0) {
2010 appctx->ctx.cli.msg = "Value out of range.\n";
2011 appctx->st0 = STAT_CLI_PRINT;
2012 return 1;
2013 }
2014
2015 global.ssl_lim = v;
2016
2017 /* Dequeues all of the listeners waiting for a resource */
2018 if (!LIST_ISEMPTY(&global_listener_queue))
2019 dequeue_all_listeners(&global_listener_queue);
2020
2021 return 1;
2022 }
2023 else {
2024 appctx->ctx.cli.msg = "'set rate-limit ssl-sessions' only supports 'global'.\n";
2025 appctx->st0 = STAT_CLI_PRINT;
2026 return 1;
2027 }
2028 }
2029#endif
William Lallemandd85f9172012-11-09 17:05:39 +01002030 else if (strcmp(args[2], "http-compression") == 0) {
2031 if (strcmp(args[3], "global") == 0) {
2032 int v;
2033
Willy Tarreaua1c2b2c2015-11-26 18:32:39 +01002034 if (strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN) {
2035 appctx->ctx.cli.msg = stats_permission_denied_msg;
2036 appctx->st0 = STAT_CLI_PRINT;
2037 return 1;
2038 }
2039
Willy Tarreau85d47f92012-11-21 00:29:50 +01002040 if (!*args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002041 appctx->ctx.cli.msg = "Expects a maximum input byte rate in kB/s.\n";
2042 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau85d47f92012-11-21 00:29:50 +01002043 return 1;
2044 }
2045
William Lallemandd85f9172012-11-09 17:05:39 +01002046 v = atoi(args[4]);
2047 global.comp_rate_lim = v * 1024; /* Kilo to bytes. */
2048 }
2049 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002050 appctx->ctx.cli.msg = "'set rate-limit http-compression' only supports 'global'.\n";
2051 appctx->st0 = STAT_CLI_PRINT;
William Lallemandd85f9172012-11-09 17:05:39 +01002052 return 1;
2053 }
2054 }
Willy Tarreauf5b22872011-09-07 16:13:44 +02002055 else {
Willy Tarreaue43d5322013-10-07 20:01:52 +02002056 appctx->ctx.cli.msg = "'set rate-limit' supports 'connections', 'sessions', 'ssl-sessions', and 'http-compression'.\n";
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002057 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02002058 return 1;
2059 }
2060 }
Willy Tarreau654694e2012-06-07 01:03:16 +02002061 else if (strcmp(args[1], "table") == 0) {
2062 stats_sock_table_request(si, args, STAT_CLI_O_SET);
2063 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002064 else if (strcmp(args[1], "map") == 0) {
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002065 char *err;
2066
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002067 /* Set flags. */
2068 appctx->ctx.map.display_flags = PAT_REF_MAP;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002069
2070 /* Expect three parameters: map name, key and new value. */
2071 if (!*args[2] || !*args[3] || !*args[4]) {
Thierry FOURNIERd5723432014-03-11 13:52:44 +01002072 appctx->ctx.cli.msg = "'set map' expects three parameters: map identifier, key and value.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002073 appctx->st0 = STAT_CLI_PRINT;
2074 return 1;
2075 }
2076
2077 /* Lookup the reference in the maps. */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002078 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002079 if (!appctx->ctx.map.ref) {
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01002080 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002081 appctx->st0 = STAT_CLI_PRINT;
2082 return 1;
2083 }
2084
Thierry FOURNIER9356c682014-01-28 15:55:37 +01002085 /* If the entry identifier start with a '#', it is considered as
2086 * pointer id
2087 */
2088 if (args[3][0] == '#' && args[3][1] == '0' && args[3][2] == 'x') {
2089 struct pat_ref_elt *ref;
2090 long long int conv;
2091 char *error;
2092
2093 /* Convert argument to integer value. */
2094 conv = strtoll(&args[3][1], &error, 16);
2095 if (*error != '\0') {
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01002096 appctx->ctx.cli.msg = "Malformed identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER9356c682014-01-28 15:55:37 +01002097 appctx->st0 = STAT_CLI_PRINT;
2098 return 1;
2099 }
2100
2101 /* Convert and check integer to pointer. */
2102 ref = (struct pat_ref_elt *)(long)conv;
2103 if ((long long int)(long)ref != conv) {
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01002104 appctx->ctx.cli.msg = "Malformed identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER9356c682014-01-28 15:55:37 +01002105 appctx->st0 = STAT_CLI_PRINT;
2106 return 1;
2107 }
2108
2109 /* Try to delete the entry. */
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002110 err = NULL;
2111 if (!pat_ref_set_by_id(appctx->ctx.map.ref, ref, args[4], &err)) {
2112 if (err)
2113 memprintf(&err, "%s.\n", err);
2114 appctx->ctx.cli.err = err;
2115 appctx->st0 = STAT_CLI_PRINT_FREE;
Thierry FOURNIER9356c682014-01-28 15:55:37 +01002116 return 1;
2117 }
2118 }
2119 else {
2120 /* Else, use the entry identifier as pattern
2121 * string, and update the value.
2122 */
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002123 err = NULL;
2124 if (!pat_ref_set(appctx->ctx.map.ref, args[3], args[4], &err)) {
2125 if (err)
2126 memprintf(&err, "%s.\n", err);
2127 appctx->ctx.cli.err = err;
2128 appctx->st0 = STAT_CLI_PRINT_FREE;
Thierry FOURNIER9356c682014-01-28 15:55:37 +01002129 return 1;
2130 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002131 }
2132
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002133 /* The set is done, send message. */
Thierry FOURNIER07e78c52014-12-18 15:28:01 +01002134 appctx->st0 = STAT_CLI_PROMPT;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002135 return 1;
2136 }
Emeric Brun4147b2e2014-06-16 18:36:30 +02002137#ifdef USE_OPENSSL
2138 else if (strcmp(args[1], "ssl") == 0) {
2139 if (strcmp(args[2], "ocsp-response") == 0) {
Lukas Tribuse4e30f72014-12-09 16:32:51 +01002140#if (defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP)
Emeric Brun4147b2e2014-06-16 18:36:30 +02002141 char *err = NULL;
2142
Emeric Brunaf4ef742014-06-19 14:10:45 +02002143 /* Expect one parameter: the new response in base64 encoding */
Emeric Brun4147b2e2014-06-16 18:36:30 +02002144 if (!*args[3]) {
2145 appctx->ctx.cli.msg = "'set ssl ocsp-response' expects response in base64 encoding.\n";
2146 appctx->st0 = STAT_CLI_PRINT;
2147 return 1;
2148 }
2149
2150 trash.len = base64dec(args[3], strlen(args[3]), trash.str, trash.size);
2151 if (trash.len < 0) {
2152 appctx->ctx.cli.msg = "'set ssl ocsp-response' received invalid base64 encoded response.\n";
2153 appctx->st0 = STAT_CLI_PRINT;
2154 return 1;
2155 }
2156
2157 if (ssl_sock_update_ocsp_response(&trash, &err)) {
2158 if (err) {
2159 memprintf(&err, "%s.\n", err);
2160 appctx->ctx.cli.err = err;
2161 appctx->st0 = STAT_CLI_PRINT_FREE;
2162 }
2163 return 1;
2164 }
2165 appctx->ctx.cli.msg = "OCSP Response updated!";
2166 appctx->st0 = STAT_CLI_PRINT;
2167 return 1;
2168#else
2169 appctx->ctx.cli.msg = "HAProxy was compiled against a version of OpenSSL that doesn't support OCSP stapling.\n";
2170 appctx->st0 = STAT_CLI_PRINT;
2171 return 1;
2172#endif
2173 }
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02002174 else if (strcmp(args[2], "tls-key") == 0) {
2175#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
2176 /* Expect two parameters: the filename and the new new TLS key in encoding */
2177 if (!*args[3] || !*args[4]) {
2178 appctx->ctx.cli.msg = "'set ssl tls-key' expects a filename and the new TLS key in base64 encoding.\n";
2179 appctx->st0 = STAT_CLI_PRINT;
2180 return 1;
2181 }
2182
2183 appctx->ctx.tlskeys.ref = tlskeys_ref_lookup_ref(args[3]);
2184 if(!appctx->ctx.tlskeys.ref) {
2185 appctx->ctx.cli.msg = "'set ssl tls-key' unable to locate referenced filename\n";
2186 appctx->st0 = STAT_CLI_PRINT;
2187 return 1;
2188 }
2189
2190 trash.len = base64dec(args[4], strlen(args[4]), trash.str, trash.size);
2191 if (trash.len != sizeof(struct tls_sess_key)) {
2192 appctx->ctx.cli.msg = "'set ssl tls-key' received invalid base64 encoded TLS key.\n";
2193 appctx->st0 = STAT_CLI_PRINT;
2194 return 1;
2195 }
2196
Pradeep Jindalcc79b002015-08-20 18:25:17 +05302197 memcpy(appctx->ctx.tlskeys.ref->tlskeys + ((appctx->ctx.tlskeys.ref->tls_ticket_enc_index + 2) % TLS_TICKETS_NO), trash.str, trash.len);
2198 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 +02002199
2200 appctx->ctx.cli.msg = "TLS ticket key updated!";
2201 appctx->st0 = STAT_CLI_PRINT;
2202 return 1;
2203#else
2204 appctx->ctx.cli.msg = "HAProxy was compiled against a version of OpenSSL "
2205 "that doesn't support specifying TLS ticket keys\n";
2206 appctx->st0 = STAT_CLI_PRINT;
2207 return 1;
2208#endif
2209 }
Emeric Brun4147b2e2014-06-16 18:36:30 +02002210 else {
2211 appctx->ctx.cli.msg = "'set ssl' only supports 'ocsp-response'.\n";
2212 appctx->st0 = STAT_CLI_PRINT;
2213 return 1;
2214 }
2215 }
2216#endif
Willy Tarreau7aabd112010-01-26 10:59:06 +01002217 else { /* unknown "set" parameter */
Willy Tarreau4483d432009-10-10 19:30:08 +02002218 return 0;
2219 }
2220 }
Cyril Bontécd19e512010-01-31 22:34:03 +01002221 else if (strcmp(args[0], "enable") == 0) {
Simon Horman671b6f02013-11-25 10:46:39 +09002222 if (strcmp(args[1], "agent") == 0) {
2223 struct server *sv;
2224
2225 sv = expect_server_admin(s, si, args[2]);
2226 if (!sv)
2227 return 1;
2228
Willy Tarreau2e10f5a2013-12-11 20:11:55 +01002229 if (!(sv->agent.state & CHK_ST_CONFIGURED)) {
2230 appctx->ctx.cli.msg = "Agent was not configured on this server, cannot enable.\n";
2231 appctx->st0 = STAT_CLI_PRINT;
2232 return 1;
2233 }
2234
2235 sv->agent.state |= CHK_ST_ENABLED;
Simon Horman671b6f02013-11-25 10:46:39 +09002236 return 1;
2237 }
Willy Tarreau9b5aecd2014-05-23 11:53:10 +02002238 else if (strcmp(args[1], "health") == 0) {
Cyril Bontécd19e512010-01-31 22:34:03 +01002239 struct server *sv;
2240
Willy Tarreaud52c41e2011-09-07 23:41:01 +02002241 sv = expect_server_admin(s, si, args[2]);
2242 if (!sv)
Cyril Bonté613f0df2011-03-03 20:49:04 +01002243 return 1;
Cyril Bonté613f0df2011-03-03 20:49:04 +01002244
Willy Tarreau9b5aecd2014-05-23 11:53:10 +02002245 if (!(sv->check.state & CHK_ST_CONFIGURED)) {
2246 appctx->ctx.cli.msg = "Health checks are not configured on this server, cannot enable.\n";
2247 appctx->st0 = STAT_CLI_PRINT;
2248 return 1;
2249 }
2250
2251 sv->check.state |= CHK_ST_ENABLED;
2252 return 1;
2253 }
2254 else if (strcmp(args[1], "server") == 0) {
2255 struct server *sv;
2256
2257 sv = expect_server_admin(s, si, args[2]);
2258 if (!sv)
2259 return 1;
2260
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02002261 srv_adm_set_ready(sv);
Willy Tarreau532a4502011-09-07 22:37:44 +02002262 return 1;
2263 }
2264 else if (strcmp(args[1], "frontend") == 0) {
2265 struct proxy *px;
2266
2267 px = expect_frontend_admin(s, si, args[2]);
2268 if (!px)
2269 return 1;
2270
2271 if (px->state == PR_STSTOPPED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002272 appctx->ctx.cli.msg = "Frontend was previously shut down, cannot enable.\n";
2273 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002274 return 1;
2275 }
2276
2277 if (px->state != PR_STPAUSED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002278 appctx->ctx.cli.msg = "Frontend is already enabled.\n";
2279 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002280 return 1;
2281 }
2282
2283 if (!resume_proxy(px)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002284 appctx->ctx.cli.msg = "Failed to resume frontend, check logs for precise cause (port conflict?).\n";
2285 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002286 return 1;
2287 }
Cyril Bontécd19e512010-01-31 22:34:03 +01002288 return 1;
2289 }
2290 else { /* unknown "enable" parameter */
Willy Tarreau9b5aecd2014-05-23 11:53:10 +02002291 appctx->ctx.cli.msg = "'enable' only supports 'agent', 'frontend', 'health', and 'server'.\n";
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002292 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002293 return 1;
Cyril Bontécd19e512010-01-31 22:34:03 +01002294 }
2295 }
2296 else if (strcmp(args[0], "disable") == 0) {
Simon Horman671b6f02013-11-25 10:46:39 +09002297 if (strcmp(args[1], "agent") == 0) {
2298 struct server *sv;
2299
2300 sv = expect_server_admin(s, si, args[2]);
2301 if (!sv)
2302 return 1;
2303
Willy Tarreau2e10f5a2013-12-11 20:11:55 +01002304 sv->agent.state &= ~CHK_ST_ENABLED;
Simon Horman671b6f02013-11-25 10:46:39 +09002305 return 1;
2306 }
Willy Tarreau9b5aecd2014-05-23 11:53:10 +02002307 else if (strcmp(args[1], "health") == 0) {
2308 struct server *sv;
2309
2310 sv = expect_server_admin(s, si, args[2]);
2311 if (!sv)
2312 return 1;
2313
2314 sv->check.state &= ~CHK_ST_ENABLED;
2315 return 1;
2316 }
Simon Horman671b6f02013-11-25 10:46:39 +09002317 else if (strcmp(args[1], "server") == 0) {
Cyril Bontécd19e512010-01-31 22:34:03 +01002318 struct server *sv;
2319
Willy Tarreaud52c41e2011-09-07 23:41:01 +02002320 sv = expect_server_admin(s, si, args[2]);
2321 if (!sv)
Cyril Bonté613f0df2011-03-03 20:49:04 +01002322 return 1;
Cyril Bonté613f0df2011-03-03 20:49:04 +01002323
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02002324 srv_adm_set_maint(sv);
Willy Tarreau532a4502011-09-07 22:37:44 +02002325 return 1;
2326 }
2327 else if (strcmp(args[1], "frontend") == 0) {
2328 struct proxy *px;
2329
2330 px = expect_frontend_admin(s, si, args[2]);
2331 if (!px)
2332 return 1;
2333
2334 if (px->state == PR_STSTOPPED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002335 appctx->ctx.cli.msg = "Frontend was previously shut down, cannot disable.\n";
2336 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002337 return 1;
2338 }
2339
2340 if (px->state == PR_STPAUSED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002341 appctx->ctx.cli.msg = "Frontend is already disabled.\n";
2342 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002343 return 1;
2344 }
2345
2346 if (!pause_proxy(px)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002347 appctx->ctx.cli.msg = "Failed to pause frontend, check logs for precise cause.\n";
2348 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002349 return 1;
2350 }
Cyril Bontécd19e512010-01-31 22:34:03 +01002351 return 1;
2352 }
2353 else { /* unknown "disable" parameter */
Willy Tarreau9b5aecd2014-05-23 11:53:10 +02002354 appctx->ctx.cli.msg = "'disable' only supports 'agent', 'frontend', 'health', and 'server'.\n";
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002355 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002356 return 1;
2357 }
2358 }
2359 else if (strcmp(args[0], "shutdown") == 0) {
2360 if (strcmp(args[1], "frontend") == 0) {
2361 struct proxy *px;
2362
2363 px = expect_frontend_admin(s, si, args[2]);
2364 if (!px)
2365 return 1;
2366
2367 if (px->state == PR_STSTOPPED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002368 appctx->ctx.cli.msg = "Frontend was already shut down.\n";
2369 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002370 return 1;
2371 }
2372
2373 Warning("Proxy %s stopped (FE: %lld conns, BE: %lld conns).\n",
2374 px->id, px->fe_counters.cum_conn, px->be_counters.cum_conn);
2375 send_log(px, LOG_WARNING, "Proxy %s stopped (FE: %lld conns, BE: %lld conns).\n",
2376 px->id, px->fe_counters.cum_conn, px->be_counters.cum_conn);
2377 stop_proxy(px);
2378 return 1;
2379 }
Willy Tarreaua295edc2011-09-07 23:21:03 +02002380 else if (strcmp(args[1], "session") == 0) {
Willy Tarreau87b09662015-04-03 00:22:06 +02002381 struct stream *sess, *ptr;
Willy Tarreaua295edc2011-09-07 23:21:03 +02002382
Willy Tarreaud0d8da92015-04-04 02:10:38 +02002383 if (strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002384 appctx->ctx.cli.msg = stats_permission_denied_msg;
2385 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaua295edc2011-09-07 23:21:03 +02002386 return 1;
2387 }
2388
2389 if (!*args[2]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002390 appctx->ctx.cli.msg = "Session pointer expected (use 'show sess').\n";
2391 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaua295edc2011-09-07 23:21:03 +02002392 return 1;
2393 }
2394
2395 ptr = (void *)strtoul(args[2], NULL, 0);
2396
Willy Tarreau87b09662015-04-03 00:22:06 +02002397 /* first, look for the requested stream in the stream table */
2398 list_for_each_entry(sess, &streams, list) {
Willy Tarreaua295edc2011-09-07 23:21:03 +02002399 if (sess == ptr)
2400 break;
2401 }
2402
Willy Tarreau87b09662015-04-03 00:22:06 +02002403 /* do we have the stream ? */
Willy Tarreaua295edc2011-09-07 23:21:03 +02002404 if (sess != ptr) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002405 appctx->ctx.cli.msg = "No such session (use 'show sess').\n";
2406 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaua295edc2011-09-07 23:21:03 +02002407 return 1;
2408 }
2409
Willy Tarreaue7dff022015-04-03 01:14:29 +02002410 stream_shutdown(sess, SF_ERR_KILLED);
Willy Tarreaua295edc2011-09-07 23:21:03 +02002411 return 1;
2412 }
Willy Tarreau52b2d222011-09-07 23:48:48 +02002413 else if (strcmp(args[1], "sessions") == 0) {
2414 if (strcmp(args[2], "server") == 0) {
2415 struct server *sv;
Willy Tarreau87b09662015-04-03 00:22:06 +02002416 struct stream *sess, *sess_bck;
Willy Tarreau52b2d222011-09-07 23:48:48 +02002417
2418 sv = expect_server_admin(s, si, args[3]);
2419 if (!sv)
2420 return 1;
2421
Willy Tarreau87b09662015-04-03 00:22:06 +02002422 /* kill all the stream that are on this server */
Willy Tarreau52b2d222011-09-07 23:48:48 +02002423 list_for_each_entry_safe(sess, sess_bck, &sv->actconns, by_srv)
2424 if (sess->srv_conn == sv)
Willy Tarreaue7dff022015-04-03 01:14:29 +02002425 stream_shutdown(sess, SF_ERR_KILLED);
Willy Tarreau52b2d222011-09-07 23:48:48 +02002426
2427 return 1;
2428 }
2429 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002430 appctx->ctx.cli.msg = "'shutdown sessions' only supports 'server'.\n";
2431 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau52b2d222011-09-07 23:48:48 +02002432 return 1;
2433 }
2434 }
Willy Tarreau532a4502011-09-07 22:37:44 +02002435 else { /* unknown "disable" parameter */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002436 appctx->ctx.cli.msg = "'shutdown' only supports 'frontend', 'session' and 'sessions'.\n";
2437 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002438 return 1;
Cyril Bontécd19e512010-01-31 22:34:03 +01002439 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002440 }
2441 else if (strcmp(args[0], "del") == 0) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002442 if (strcmp(args[1], "map") == 0 || strcmp(args[1], "acl") == 0) {
2443 if (args[1][0] == 'm')
2444 appctx->ctx.map.display_flags = PAT_REF_MAP;
2445 else
2446 appctx->ctx.map.display_flags = PAT_REF_ACL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002447
2448 /* Expect two parameters: map name and key. */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002449 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
2450 if (!*args[2] || !*args[3]) {
2451 appctx->ctx.cli.msg = "This command expects two parameters: map identifier and key.\n";
2452 appctx->st0 = STAT_CLI_PRINT;
2453 return 1;
2454 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002455 }
2456
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002457 else {
2458 if (!*args[2] || !*args[3]) {
2459 appctx->ctx.cli.msg = "This command expects two parameters: ACL identifier and key.\n";
2460 appctx->st0 = STAT_CLI_PRINT;
2461 return 1;
2462 }
2463 }
2464
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002465 /* Lookup the reference in the maps. */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002466 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002467 if (!appctx->ctx.map.ref ||
2468 !(appctx->ctx.map.ref->flags & appctx->ctx.map.display_flags)) {
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01002469 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002470 appctx->st0 = STAT_CLI_PRINT;
2471 return 1;
2472 }
2473
Thierry FOURNIER9356c682014-01-28 15:55:37 +01002474 /* If the entry identifier start with a '#', it is considered as
2475 * pointer id
2476 */
2477 if (args[3][0] == '#' && args[3][1] == '0' && args[3][2] == 'x') {
2478 struct pat_ref_elt *ref;
2479 long long int conv;
2480 char *error;
2481
2482 /* Convert argument to integer value. */
2483 conv = strtoll(&args[3][1], &error, 16);
2484 if (*error != '\0') {
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01002485 appctx->ctx.cli.msg = "Malformed identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER9356c682014-01-28 15:55:37 +01002486 appctx->st0 = STAT_CLI_PRINT;
2487 return 1;
2488 }
2489
2490 /* Convert and check integer to pointer. */
2491 ref = (struct pat_ref_elt *)(long)conv;
2492 if ((long long int)(long)ref != conv) {
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01002493 appctx->ctx.cli.msg = "Malformed identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER9356c682014-01-28 15:55:37 +01002494 appctx->st0 = STAT_CLI_PRINT;
2495 return 1;
2496 }
2497
2498 /* Try to delete the entry. */
2499 if (!pat_ref_delete_by_id(appctx->ctx.map.ref, ref)) {
2500 /* The entry is not found, send message. */
2501 appctx->ctx.cli.msg = "Key not found.\n";
2502 appctx->st0 = STAT_CLI_PRINT;
2503 return 1;
2504 }
2505 }
2506 else {
2507 /* Else, use the entry identifier as pattern
2508 * string and try to delete the entry.
2509 */
2510 if (!pat_ref_delete(appctx->ctx.map.ref, args[3])) {
2511 /* The entry is not found, send message. */
2512 appctx->ctx.cli.msg = "Key not found.\n";
2513 appctx->st0 = STAT_CLI_PRINT;
2514 return 1;
2515 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002516 }
2517
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002518 /* The deletion is done, send message. */
Thierry FOURNIER07e78c52014-12-18 15:28:01 +01002519 appctx->st0 = STAT_CLI_PROMPT;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002520 return 1;
2521 }
2522 else { /* unknown "del" parameter */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002523 appctx->ctx.cli.msg = "'del' only supports 'map' or 'acl'.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002524 appctx->st0 = STAT_CLI_PRINT;
2525 return 1;
2526 }
2527 }
2528 else if (strcmp(args[0], "add") == 0) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002529 if (strcmp(args[1], "map") == 0 ||
2530 strcmp(args[1], "acl") == 0) {
2531 int ret;
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002532 char *err;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002533
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002534 /* Set flags. */
2535 if (args[1][0] == 'm')
2536 appctx->ctx.map.display_flags = PAT_REF_MAP;
2537 else
2538 appctx->ctx.map.display_flags = PAT_REF_ACL;
2539
2540 /* If the keywork is "map", we expect three parameters, if it
2541 * is "acl", we expect only two parameters
2542 */
2543 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
2544 if (!*args[2] || !*args[3] || !*args[4]) {
2545 appctx->ctx.cli.msg = "'add map' expects three parameters: map identifier, key and value.\n";
2546 appctx->st0 = STAT_CLI_PRINT;
2547 return 1;
2548 }
2549 }
2550 else {
2551 if (!*args[2] || !*args[3]) {
2552 appctx->ctx.cli.msg = "'add acl' expects two parameters: ACL identifier and pattern.\n";
2553 appctx->st0 = STAT_CLI_PRINT;
2554 return 1;
2555 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002556 }
2557
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002558 /* Lookup for the reference. */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002559 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002560 if (!appctx->ctx.map.ref) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002561 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01002562 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002563 else
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01002564 appctx->ctx.cli.msg = "Unknown ACL identifier. Please use #<id> or <file>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002565 appctx->st0 = STAT_CLI_PRINT;
2566 return 1;
2567 }
2568
Thierry FOURNIER64c585f2014-01-29 20:02:36 +01002569 /* The command "add acl" is prohibited if the reference
2570 * use samples.
2571 */
2572 if ((appctx->ctx.map.display_flags & PAT_REF_ACL) &&
2573 (appctx->ctx.map.ref->flags & PAT_REF_SMP)) {
2574 appctx->ctx.cli.msg = "This ACL is shared with a map containing samples. "
2575 "You must use the command 'add map' to add values.\n";
2576 appctx->st0 = STAT_CLI_PRINT;
2577 return 1;
2578 }
2579
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002580 /* Add value. */
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002581 err = NULL;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002582 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02002583 ret = pat_ref_add(appctx->ctx.map.ref, args[3], args[4], &err);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002584 else
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02002585 ret = pat_ref_add(appctx->ctx.map.ref, args[3], NULL, &err);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002586 if (!ret) {
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002587 if (err)
2588 memprintf(&err, "%s.\n", err);
2589 appctx->ctx.cli.err = err;
2590 appctx->st0 = STAT_CLI_PRINT_FREE;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002591 return 1;
2592 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002593
2594 /* The add is done, send message. */
Thierry FOURNIER07e78c52014-12-18 15:28:01 +01002595 appctx->st0 = STAT_CLI_PROMPT;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002596 return 1;
2597 }
2598 else { /* unknown "del" parameter */
2599 appctx->ctx.cli.msg = "'add' only supports 'map'.\n";
2600 appctx->st0 = STAT_CLI_PRINT;
2601 return 1;
2602 }
Cyril Bontécd19e512010-01-31 22:34:03 +01002603 }
2604 else { /* not "show" nor "clear" nor "get" nor "set" nor "enable" nor "disable" */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002605 return 0;
2606 }
2607 return 1;
2608}
2609
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002610/* This I/O handler runs as an applet embedded in a stream interface. It is
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002611 * used to processes I/O from/to the stats unix socket. The system relies on a
2612 * state machine handling requests and various responses. We read a request,
2613 * then we process it and send the response, and we possibly display a prompt.
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002614 * Then we can read again. The state is stored in appctx->st0 and is one of the
2615 * STAT_CLI_* constants. appctx->st1 is used to indicate whether prompt is enabled
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002616 * or not.
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002617 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02002618static void cli_io_handler(struct appctx *appctx)
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002619{
Willy Tarreau00a37f02015-04-13 12:05:19 +02002620 struct stream_interface *si = appctx->owner;
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002621 struct channel *req = si_oc(si);
2622 struct channel *res = si_ic(si);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002623 int reql;
2624 int len;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002625
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002626 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
2627 goto out;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002628
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002629 while (1) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002630 if (appctx->st0 == STAT_CLI_INIT) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002631 /* Stats output not initialized yet */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002632 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
2633 appctx->st0 = STAT_CLI_GETREQ;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002634 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002635 else if (appctx->st0 == STAT_CLI_END) {
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002636 /* Let's close for real now. We just close the request
2637 * side, the conditions below will complete if needed.
2638 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02002639 si_shutw(si);
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002640 break;
2641 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002642 else if (appctx->st0 == STAT_CLI_GETREQ) {
Willy Tarreau4e33d862009-10-11 23:35:10 +02002643 /* ensure we have some output room left in the event we
2644 * would want to return some info right after parsing.
2645 */
Willy Tarreau4e4292b2014-11-28 12:18:45 +01002646 if (buffer_almost_full(si_ib(si))) {
Willy Tarreaufe127932015-04-21 19:23:39 +02002647 si_applet_cant_put(si);
Willy Tarreau4e33d862009-10-11 23:35:10 +02002648 break;
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01002649 }
Willy Tarreau4e33d862009-10-11 23:35:10 +02002650
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002651 reql = bo_getline(si_oc(si), trash.str, trash.size);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002652 if (reql <= 0) { /* closed or EOL not found */
2653 if (reql == 0)
2654 break;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002655 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002656 continue;
2657 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002658
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002659 /* seek for a possible semi-colon. If we find one, we
2660 * replace it with an LF and skip only this part.
2661 */
2662 for (len = 0; len < reql; len++)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002663 if (trash.str[len] == ';') {
2664 trash.str[len] = '\n';
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002665 reql = len + 1;
2666 break;
2667 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002668
Willy Tarreau816fc222009-10-04 07:36:58 +02002669 /* now it is time to check that we have a full line,
2670 * remove the trailing \n and possibly \r, then cut the
2671 * line.
2672 */
2673 len = reql - 1;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002674 if (trash.str[len] != '\n') {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002675 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002676 continue;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002677 }
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002678
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002679 if (len && trash.str[len-1] == '\r')
Willy Tarreau816fc222009-10-04 07:36:58 +02002680 len--;
2681
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002682 trash.str[len] = '\0';
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002683
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002684 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002685 if (len) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002686 if (strcmp(trash.str, "quit") == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002687 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002688 continue;
2689 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002690 else if (strcmp(trash.str, "prompt") == 0)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002691 appctx->st1 = !appctx->st1;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002692 else if (strcmp(trash.str, "help") == 0 ||
2693 !stats_sock_parse_request(si, trash.str)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002694 appctx->ctx.cli.msg = stats_sock_usage_msg;
2695 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02002696 }
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002697 /* NB: stats_sock_parse_request() may have put
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002698 * another STAT_CLI_O_* into appctx->st0.
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002699 */
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002700 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002701 else if (!appctx->st1) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002702 /* if prompt is disabled, print help on empty lines,
2703 * so that the user at least knows how to enable
2704 * prompt and find help.
2705 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002706 appctx->ctx.cli.msg = stats_sock_usage_msg;
2707 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002708 }
2709
2710 /* re-adjust req buffer */
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002711 bo_skip(si_oc(si), reql);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002712 req->flags |= CF_READ_DONTWAIT; /* we plan to read small requests */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002713 }
Willy Tarreau68c00c72015-09-25 19:21:19 +02002714 else { /* output functions */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002715 switch (appctx->st0) {
Andrew Hayworthe32d1862015-10-02 15:08:10 +00002716 case STAT_CLI_PROMPT:
2717 break;
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02002718 case STAT_CLI_PRINT:
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002719 if (bi_putstr(si_ic(si), appctx->ctx.cli.msg) != -1)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002720 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreaubc18da12015-03-13 14:00:47 +01002721 else
Willy Tarreaufe127932015-04-21 19:23:39 +02002722 si_applet_cant_put(si);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002723 break;
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002724 case STAT_CLI_PRINT_FREE:
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002725 if (bi_putstr(si_ic(si), appctx->ctx.cli.err) != -1) {
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002726 free(appctx->ctx.cli.err);
2727 appctx->st0 = STAT_CLI_PROMPT;
2728 }
Willy Tarreaubc18da12015-03-13 14:00:47 +01002729 else
Willy Tarreaufe127932015-04-21 19:23:39 +02002730 si_applet_cant_put(si);
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002731 break;
Baptiste Assmann9b6857e2015-09-18 14:49:12 +02002732 case STAT_CLI_O_BACKEND:
2733 if (stats_dump_backend_to_buffer(si))
2734 appctx->st0 = STAT_CLI_PROMPT;
2735 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002736 case STAT_CLI_O_INFO:
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002737 if (stats_dump_info_to_buffer(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002738 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002739 break;
Baptiste Assmann28289462015-07-03 08:01:20 +02002740 case STAT_CLI_O_SERVERS_STATE:
2741 if (stats_dump_servers_state_to_buffer(si))
2742 appctx->st0 = STAT_CLI_PROMPT;
2743 break;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002744 case STAT_CLI_O_STAT:
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002745 if (stats_dump_stat_to_buffer(si, NULL))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002746 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002747 break;
Baptiste Assmann3863f972015-05-17 00:33:24 +02002748 case STAT_CLI_O_RESOLVERS:
2749 if (stats_dump_resolvers_to_buffer(si))
2750 appctx->st0 = STAT_CLI_PROMPT;
2751 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002752 case STAT_CLI_O_SESS:
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01002753 if (stats_dump_sess_to_buffer(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002754 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002755 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002756 case STAT_CLI_O_ERR: /* errors dump */
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01002757 if (stats_dump_errors_to_buffer(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002758 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002759 break;
Willy Tarreau69f58c82010-07-12 17:55:33 +02002760 case STAT_CLI_O_TAB:
Simon Hormanc88b8872011-06-15 15:18:49 +09002761 case STAT_CLI_O_CLR:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002762 if (stats_table_request(si, appctx->st0))
2763 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau69f58c82010-07-12 17:55:33 +02002764 break;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002765 case STAT_CLI_O_PATS:
2766 if (stats_pats_list(si))
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002767 appctx->st0 = STAT_CLI_PROMPT;
2768 break;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002769 case STAT_CLI_O_PAT:
2770 if (stats_pat_list(si))
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002771 appctx->st0 = STAT_CLI_PROMPT;
2772 break;
2773 case STAT_CLI_O_MLOOK:
2774 if (stats_map_lookup(si))
2775 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau4efb3532014-01-29 12:13:39 +01002776 break;
Willy Tarreau12833bb2014-01-28 16:49:56 +01002777 case STAT_CLI_O_POOLS:
2778 if (stats_dump_pools_to_buffer(si))
2779 appctx->st0 = STAT_CLI_PROMPT;
2780 break;
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02002781#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
2782 case STAT_CLI_O_TLSK:
2783 if (stats_tlskeys_list(si))
2784 appctx->st0 = STAT_CLI_PROMPT;
2785 break;
William Lallemand1d0b36a2016-05-20 17:40:26 +02002786 case STAT_CLI_O_TLSK_ENT:
2787 if (stats_tlskeys_list(si))
2788 appctx->st0 = STAT_CLI_PROMPT;
2789 break;
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02002790#endif
Willy Tarreauae795722016-02-16 11:27:28 +01002791 case STAT_CLI_O_ENV: /* environment dump */
2792 if (stats_dump_env_to_buffer(si))
2793 appctx->st0 = STAT_CLI_PROMPT;
2794 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002795 default: /* abnormal state */
Willy Tarreau5cfa3bc2015-09-25 20:08:51 +02002796 si->flags |= SI_FL_ERR;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002797 break;
2798 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002799
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002800 /* The post-command prompt is either LF alone or LF + '> ' in interactive mode */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002801 if (appctx->st0 == STAT_CLI_PROMPT) {
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002802 if (bi_putstr(si_ic(si), appctx->st1 ? "\n> " : "\n") != -1)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002803 appctx->st0 = STAT_CLI_GETREQ;
Willy Tarreaubc18da12015-03-13 14:00:47 +01002804 else
Willy Tarreaufe127932015-04-21 19:23:39 +02002805 si_applet_cant_put(si);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002806 }
2807
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002808 /* If the output functions are still there, it means they require more room. */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002809 if (appctx->st0 >= STAT_CLI_OUTPUT)
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002810 break;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002811
2812 /* Now we close the output if one of the writers did so,
2813 * or if we're not in interactive mode and the request
2814 * buffer is empty. This still allows pipelined requests
2815 * to be sent in non-interactive mode.
2816 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002817 if ((res->flags & (CF_SHUTW|CF_SHUTW_NOW)) || (!appctx->st1 && !req->buf->o)) {
2818 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002819 continue;
2820 }
2821
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002822 /* switch state back to GETREQ to read next requests */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002823 appctx->st0 = STAT_CLI_GETREQ;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002824 }
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002825 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002826
Willy Tarreauc9e930a2015-09-25 20:06:08 +02002827 if ((res->flags & CF_SHUTR) && (si->state == SI_ST_EST)) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002828 DPRINTF(stderr, "%s@%d: si to buf closed. req=%08x, res=%08x, st=%d\n",
2829 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
Aman Guptaceafb4a2012-04-02 18:57:54 -07002830 /* Other side has closed, let's abort if we have no more processing to do
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002831 * and nothing more to consume. This is comparable to a broken pipe, so
2832 * we forward the close to the request side so that it flows upstream to
2833 * the client.
2834 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02002835 si_shutw(si);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002836 }
2837
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002838 if ((req->flags & CF_SHUTW) && (si->state == SI_ST_EST) && (appctx->st0 < STAT_CLI_OUTPUT)) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002839 DPRINTF(stderr, "%s@%d: buf to si closed. req=%08x, res=%08x, st=%d\n",
2840 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
2841 /* We have no more processing to do, and nothing more to send, and
2842 * the client side has closed. So we'll forward this state downstream
2843 * on the response buffer.
2844 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02002845 si_shutr(si);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002846 res->flags |= CF_READ_NULL;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002847 }
2848
Willy Tarreau828824a2015-04-19 17:20:03 +02002849 out:
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01002850 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 +02002851 __FUNCTION__, __LINE__,
Willy Tarreau9b28e032012-10-12 23:49:43 +02002852 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 +02002853}
2854
Willy Tarreau638d40a2016-02-24 23:11:01 +01002855/* Emits a stats field without any surrounding element and properly encoded to
2856 * resist CSV output. Returns non-zero on success, 0 if the buffer is full.
2857 */
Willy Tarreaub47785f2016-02-24 23:28:31 +01002858int stats_emit_raw_data_field(struct chunk *out, const struct field *f)
Willy Tarreau638d40a2016-02-24 23:11:01 +01002859{
2860 switch (field_format(f, 0)) {
2861 case FF_EMPTY: return 1;
2862 case FF_S32: return chunk_appendf(out, "%d", f->u.s32);
2863 case FF_U32: return chunk_appendf(out, "%u", f->u.u32);
2864 case FF_S64: return chunk_appendf(out, "%lld", (long long)f->u.s64);
2865 case FF_U64: return chunk_appendf(out, "%llu", (unsigned long long)f->u.u64);
2866 case FF_STR: return csv_enc_append(field_str(f, 0), 1, out) != NULL;
2867 default: return chunk_appendf(out, "[INCORRECT_FIELD_TYPE_%08x]", f->type);
2868 }
2869}
2870
Willy Tarreaub47785f2016-02-24 23:28:31 +01002871/* Emits a stats field prefixed with its type. No CSV encoding is prepared, the
2872 * output is supposed to be used on its own line. Returns non-zero on success, 0
2873 * if the buffer is full.
2874 */
2875int stats_emit_typed_data_field(struct chunk *out, const struct field *f)
2876{
2877 switch (field_format(f, 0)) {
2878 case FF_EMPTY: return 1;
2879 case FF_S32: return chunk_appendf(out, "s32:%d", f->u.s32);
2880 case FF_U32: return chunk_appendf(out, "u32:%u", f->u.u32);
2881 case FF_S64: return chunk_appendf(out, "s64:%lld", (long long)f->u.s64);
2882 case FF_U64: return chunk_appendf(out, "u64:%llu", (unsigned long long)f->u.u64);
2883 case FF_STR: return chunk_appendf(out, "str:%s", field_str(f, 0));
2884 default: return chunk_appendf(out, "%08x:?", f->type);
2885 }
2886}
2887
2888/* Emits an encoding of the field type on 3 characters followed by a delimiter.
2889 * Returns non-zero on success, 0 if the buffer is full.
2890 */
2891int stats_emit_field_tags(struct chunk *out, const struct field *f, char delim)
2892{
2893 char origin, nature, scope;
2894
2895 switch (field_origin(f, 0)) {
2896 case FO_METRIC: origin = 'M'; break;
2897 case FO_STATUS: origin = 'S'; break;
2898 case FO_KEY: origin = 'K'; break;
2899 case FO_CONFIG: origin = 'C'; break;
2900 case FO_PRODUCT: origin = 'P'; break;
2901 default: origin = '?'; break;
2902 }
2903
2904 switch (field_nature(f, 0)) {
2905 case FN_GAUGE: nature = 'G'; break;
2906 case FN_LIMIT: nature = 'L'; break;
2907 case FN_MIN: nature = 'm'; break;
2908 case FN_MAX: nature = 'M'; break;
2909 case FN_RATE: nature = 'R'; break;
2910 case FN_COUNTER: nature = 'C'; break;
2911 case FN_DURATION: nature = 'D'; break;
2912 case FN_AGE: nature = 'A'; break;
2913 case FN_TIME: nature = 'T'; break;
2914 case FN_NAME: nature = 'N'; break;
2915 case FN_OUTPUT: nature = 'O'; break;
2916 case FN_AVG: nature = 'a'; break;
2917 default: nature = '?'; break;
2918 }
2919
2920 switch (field_scope(f, 0)) {
2921 case FS_PROCESS: scope = 'P'; break;
2922 case FS_SERVICE: scope = 'S'; break;
2923 case FS_SYSTEM: scope = 's'; break;
2924 case FS_CLUSTER: scope = 'C'; break;
2925 default: scope = '?'; break;
2926 }
2927
2928 return chunk_appendf(out, "%c%c%c%c", origin, nature, scope, delim);
2929}
2930
Willy Tarreaubf95cba2016-01-11 18:27:29 +01002931/* Dump all fields from <info> into <out> using the "show info" format (name: value) */
2932static int stats_dump_info_fields(struct chunk *out, const struct field *info)
2933{
2934 int field;
2935
2936 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
2937 if (!field_format(info, field))
2938 continue;
2939
2940 if (!chunk_appendf(out, "%s: ", info_field_names[field]))
2941 return 0;
2942 if (!stats_emit_raw_data_field(out, &info[field]))
2943 return 0;
2944 if (!chunk_strcat(out, "\n"))
2945 return 0;
2946 }
2947 return 1;
2948}
2949
Willy Tarreaucb809122016-01-11 20:08:42 +01002950/* Dump all fields from <info> into <out> using the "show info typed" format */
2951static int stats_dump_typed_info_fields(struct chunk *out, const struct field *info)
2952{
2953 int field;
2954
2955 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
2956 if (!field_format(info, field))
2957 continue;
2958
2959 if (!chunk_appendf(out, "%d.%s.%u:", field, info_field_names[field], info[INF_PROCESS_NUM].u.u32))
2960 return 0;
2961 if (!stats_emit_field_tags(out, &info[field], ':'))
2962 return 0;
2963 if (!stats_emit_typed_data_field(out, &info[field]))
2964 return 0;
2965 if (!chunk_strcat(out, "\n"))
2966 return 0;
2967 }
2968 return 1;
2969}
2970
Thierry Fourniercb2c7672016-03-25 08:19:23 +01002971/* Fill <info> with HAProxy global info. <info> is preallocated
2972 * array of length <len>. The length of the aray must be
2973 * INF_TOTAL_FIELDS. If this length is less then this value, the
2974 * function returns 0, otherwise, it returns 1.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002975 */
Thierry Fourniercb2c7672016-03-25 08:19:23 +01002976int stats_fill_info(struct field *info, int len)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002977{
2978 unsigned int up = (now.tv_sec - start_date.tv_sec);
Willy Tarreau4529c072016-01-11 18:17:07 +01002979 struct chunk *out = get_trash_chunk();
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002980
Willy Tarreau0c9c2722014-05-28 12:28:58 +02002981#ifdef USE_OPENSSL
2982 int ssl_sess_rate = read_freq_ctr(&global.ssl_per_sec);
2983 int ssl_key_rate = read_freq_ctr(&global.ssl_fe_keys_per_sec);
2984 int ssl_reuse = 0;
2985
2986 if (ssl_key_rate < ssl_sess_rate) {
2987 /* count the ssl reuse ratio and avoid overflows in both directions */
2988 ssl_reuse = 100 - (100 * ssl_key_rate + (ssl_sess_rate - 1) / 2) / ssl_sess_rate;
2989 }
2990#endif
2991
Thierry Fourniercb2c7672016-03-25 08:19:23 +01002992 if (len < INF_TOTAL_FIELDS)
2993 return 0;
2994
Willy Tarreau4529c072016-01-11 18:17:07 +01002995 chunk_reset(out);
Thierry Fourniercb2c7672016-03-25 08:19:23 +01002996 memset(info, 0, sizeof(*info) * len);
Willy Tarreau4529c072016-01-11 18:17:07 +01002997
2998 info[INF_NAME] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, PRODUCT_NAME);
2999 info[INF_VERSION] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, HAPROXY_VERSION);
3000 info[INF_RELEASE_DATE] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, HAPROXY_DATE);
3001
3002 info[INF_NBPROC] = mkf_u32(FO_CONFIG|FS_SERVICE, global.nbproc);
3003 info[INF_PROCESS_NUM] = mkf_u32(FO_KEY, relative_pid);
3004 info[INF_PID] = mkf_u32(FO_STATUS, pid);
3005
3006 info[INF_UPTIME] = mkf_str(FN_DURATION, chunk_newstr(out));
3007 chunk_appendf(out, "%ud %uh%02um%02us", up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60));
3008
3009 info[INF_UPTIME_SEC] = mkf_u32(FN_DURATION, up);
3010 info[INF_MEMMAX_MB] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_memmax);
3011 info[INF_POOL_ALLOC_MB] = mkf_u32(0, (unsigned)(pool_total_allocated() / 1048576L));
3012 info[INF_POOL_USED_MB] = mkf_u32(0, (unsigned)(pool_total_used() / 1048576L));
3013 info[INF_POOL_FAILED] = mkf_u32(FN_COUNTER, pool_total_failures());
3014 info[INF_ULIMIT_N] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_nofile);
3015 info[INF_MAXSOCK] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxsock);
3016 info[INF_MAXCONN] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxconn);
3017 info[INF_HARD_MAXCONN] = mkf_u32(FO_CONFIG|FN_LIMIT, global.hardmaxconn);
3018 info[INF_CURR_CONN] = mkf_u32(0, actconn);
3019 info[INF_CUM_CONN] = mkf_u32(FN_COUNTER, totalconn);
3020 info[INF_CUM_REQ] = mkf_u32(FN_COUNTER, global.req_count);
3021#ifdef USE_OPENSSL
3022 info[INF_MAX_SSL_CONNS] = mkf_u32(FN_MAX, global.maxsslconn);
3023 info[INF_CURR_SSL_CONNS] = mkf_u32(0, sslconns);
3024 info[INF_CUM_SSL_CONNS] = mkf_u32(FN_COUNTER, totalsslconns);
3025#endif
3026 info[INF_MAXPIPES] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxpipes);
3027 info[INF_PIPES_USED] = mkf_u32(0, pipes_used);
3028 info[INF_PIPES_FREE] = mkf_u32(0, pipes_free);
3029 info[INF_CONN_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.conn_per_sec));
3030 info[INF_CONN_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.cps_lim);
3031 info[INF_MAX_CONN_RATE] = mkf_u32(FN_MAX, global.cps_max);
3032 info[INF_SESS_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.sess_per_sec));
3033 info[INF_SESS_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.sps_lim);
3034 info[INF_MAX_SESS_RATE] = mkf_u32(FN_RATE, global.sps_max);
3035
3036#ifdef USE_OPENSSL
3037 info[INF_SSL_RATE] = mkf_u32(FN_RATE, ssl_sess_rate);
3038 info[INF_SSL_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.ssl_lim);
3039 info[INF_MAX_SSL_RATE] = mkf_u32(FN_MAX, global.ssl_max);
3040 info[INF_SSL_FRONTEND_KEY_RATE] = mkf_u32(0, ssl_key_rate);
3041 info[INF_SSL_FRONTEND_MAX_KEY_RATE] = mkf_u32(FN_MAX, global.ssl_fe_keys_max);
3042 info[INF_SSL_FRONTEND_SESSION_REUSE_PCT] = mkf_u32(0, ssl_reuse);
3043 info[INF_SSL_BACKEND_KEY_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.ssl_be_keys_per_sec));
3044 info[INF_SSL_BACKEND_MAX_KEY_RATE] = mkf_u32(FN_MAX, global.ssl_be_keys_max);
3045 info[INF_SSL_CACHE_LOOKUPS] = mkf_u32(FN_COUNTER, global.shctx_lookups);
3046 info[INF_SSL_CACHE_MISSES] = mkf_u32(FN_COUNTER, global.shctx_misses);
3047#endif
3048 info[INF_COMPRESS_BPS_IN] = mkf_u32(FN_RATE, read_freq_ctr(&global.comp_bps_in));
3049 info[INF_COMPRESS_BPS_OUT] = mkf_u32(FN_RATE, read_freq_ctr(&global.comp_bps_out));
3050 info[INF_COMPRESS_BPS_RATE_LIM] = mkf_u32(FO_CONFIG|FN_LIMIT, global.comp_rate_lim);
3051#ifdef USE_ZLIB
3052 info[INF_ZLIB_MEM_USAGE] = mkf_u32(0, zlib_used_memory);
3053 info[INF_MAX_ZLIB_MEM_USAGE] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxzlibmem);
3054#endif
3055 info[INF_TASKS] = mkf_u32(0, nb_tasks_cur);
3056 info[INF_RUN_QUEUE] = mkf_u32(0, run_queue_cur);
3057 info[INF_IDLE_PCT] = mkf_u32(FN_AVG, idle_pct);
3058 info[INF_NODE] = mkf_str(FO_CONFIG|FN_OUTPUT|FS_SERVICE, global.node);
3059 if (global.desc)
3060 info[INF_DESCRIPTION] = mkf_str(FO_CONFIG|FN_OUTPUT|FS_SERVICE, global.desc);
3061
Thierry Fourniercb2c7672016-03-25 08:19:23 +01003062 return 1;
3063}
3064
3065/* This function dumps information onto the stream interface's read buffer.
3066 * It returns 0 as long as it does not complete, non-zero upon completion.
3067 * No state is used.
3068 */
3069static int stats_dump_info_to_buffer(struct stream_interface *si)
3070{
3071 struct appctx *appctx = __objt_appctx(si->end);
3072
3073 if (!stats_fill_info(info, INF_TOTAL_FIELDS))
3074 return 0;
3075
Willy Tarreau1b4ba1e2016-01-11 18:29:04 +01003076 chunk_reset(&trash);
Willy Tarreaucb809122016-01-11 20:08:42 +01003077
3078 if (appctx->ctx.stats.flags & STAT_FMT_TYPED)
3079 stats_dump_typed_info_fields(&trash, info);
3080 else
3081 stats_dump_info_fields(&trash, info);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003082
Willy Tarreaubc18da12015-03-13 14:00:47 +01003083 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02003084 si_applet_cant_put(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003085 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01003086 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003087
3088 return 1;
3089}
3090
Baptiste Assmann28289462015-07-03 08:01:20 +02003091/* dumps server state information into <buf> for all the servers found in <backend>
3092 * These information are all the parameters which may change during HAProxy runtime.
3093 * By default, we only export to the last known server state file format.
3094 * These information can be used at next startup to recover same level of server state.
3095 */
Cyril Bonté76a99782016-05-06 12:18:48 +02003096static int dump_servers_state(struct stream_interface *si, struct chunk *buf)
Baptiste Assmann28289462015-07-03 08:01:20 +02003097{
Cyril Bonté76a99782016-05-06 12:18:48 +02003098 struct appctx *appctx = __objt_appctx(si->end);
Baptiste Assmann28289462015-07-03 08:01:20 +02003099 struct server *srv;
3100 char srv_addr[INET6_ADDRSTRLEN + 1];
3101 time_t srv_time_since_last_change;
3102 int bk_f_forced_id, srv_f_forced_id;
3103
Baptiste Assmann28289462015-07-03 08:01:20 +02003104
Cyril Bonté76a99782016-05-06 12:18:48 +02003105 /* we don't want to report any state if the backend is not enabled on this process */
3106 if (appctx->ctx.server_state.px->bind_proc && !(appctx->ctx.server_state.px->bind_proc & (1UL << (relative_pid - 1))))
3107 return 1;
Baptiste Assmann28289462015-07-03 08:01:20 +02003108
Cyril Bontéd55bd7a2016-05-27 00:06:45 +02003109 if (!appctx->ctx.server_state.sv)
3110 appctx->ctx.server_state.sv = appctx->ctx.server_state.px->srv;
3111
Cyril Bonté76a99782016-05-06 12:18:48 +02003112 for (; appctx->ctx.server_state.sv != NULL; appctx->ctx.server_state.sv = srv->next) {
3113 srv = appctx->ctx.server_state.sv;
Baptiste Assmann28289462015-07-03 08:01:20 +02003114 srv_addr[0] = '\0';
3115 srv_time_since_last_change = 0;
3116 bk_f_forced_id = 0;
3117 srv_f_forced_id = 0;
3118
3119 switch (srv->addr.ss_family) {
3120 case AF_INET:
3121 inet_ntop(srv->addr.ss_family, &((struct sockaddr_in *)&srv->addr)->sin_addr,
3122 srv_addr, INET_ADDRSTRLEN + 1);
3123 break;
3124 case AF_INET6:
3125 inet_ntop(srv->addr.ss_family, &((struct sockaddr_in6 *)&srv->addr)->sin6_addr,
3126 srv_addr, INET6_ADDRSTRLEN + 1);
3127 break;
3128 }
3129 srv_time_since_last_change = now.tv_sec - srv->last_change;
Cyril Bonté76a99782016-05-06 12:18:48 +02003130 bk_f_forced_id = appctx->ctx.server_state.px->options & PR_O_FORCED_ID ? 1 : 0;
Baptiste Assmann28289462015-07-03 08:01:20 +02003131 srv_f_forced_id = srv->flags & SRV_F_FORCED_ID ? 1 : 0;
3132
3133 chunk_appendf(buf,
3134 "%d %s "
3135 "%d %s %s "
3136 "%d %d %d %d %ld "
3137 "%d %d %d %d %d "
3138 "%d %d"
3139 "\n",
Cyril Bonté76a99782016-05-06 12:18:48 +02003140 appctx->ctx.server_state.px->uuid, appctx->ctx.server_state.px->id,
Baptiste Assmann28289462015-07-03 08:01:20 +02003141 srv->puid, srv->id, srv_addr,
David Carlier081b3362015-11-18 06:10:22 +00003142 srv->state, srv->admin, srv->uweight, srv->iweight, (long int)srv_time_since_last_change,
Baptiste Assmann28289462015-07-03 08:01:20 +02003143 srv->check.status, srv->check.result, srv->check.health, srv->check.state, srv->agent.state,
3144 bk_f_forced_id, srv_f_forced_id);
Cyril Bonté76a99782016-05-06 12:18:48 +02003145 if (bi_putchk(si_ic(si), &trash) == -1) {
3146 si_applet_cant_put(si);
3147 return 0;
3148 }
Baptiste Assmann28289462015-07-03 08:01:20 +02003149 }
Cyril Bonté76a99782016-05-06 12:18:48 +02003150 return 1;
Baptiste Assmann28289462015-07-03 08:01:20 +02003151}
3152
Baptiste Assmann9b6857e2015-09-18 14:49:12 +02003153/* Parses backend list and simply report backend names */
3154static int stats_dump_backend_to_buffer(struct stream_interface *si)
3155{
Cyril Bonté6ca9e012016-05-06 12:18:49 +02003156 struct appctx *appctx = __objt_appctx(si->end);
Baptiste Assmann9b6857e2015-09-18 14:49:12 +02003157 extern struct proxy *proxy;
3158 struct proxy *curproxy;
3159
3160 chunk_reset(&trash);
Baptiste Assmann9b6857e2015-09-18 14:49:12 +02003161
Cyril Bonté6ca9e012016-05-06 12:18:49 +02003162 if (!appctx->ctx.be.px) {
3163 chunk_printf(&trash, "# name\n");
3164 if (bi_putchk(si_ic(si), &trash) == -1) {
3165 si_applet_cant_put(si);
3166 return 0;
3167 }
3168 appctx->ctx.be.px = proxy;
3169 }
3170
3171 for (; appctx->ctx.be.px != NULL; appctx->ctx.be.px = curproxy->next) {
3172 curproxy = appctx->ctx.be.px;
3173
Baptiste Assmann9b6857e2015-09-18 14:49:12 +02003174 /* looking for backends only */
3175 if (!(curproxy->cap & PR_CAP_BE))
3176 continue;
3177
3178 /* we don't want to list a backend which is bound to this process */
3179 if (curproxy->bind_proc && !(curproxy->bind_proc & (1UL << (relative_pid - 1))))
3180 continue;
3181
3182 chunk_appendf(&trash, "%s\n", curproxy->id);
Cyril Bonté6ca9e012016-05-06 12:18:49 +02003183 if (bi_putchk(si_ic(si), &trash) == -1) {
3184 si_applet_cant_put(si);
3185 return 0;
3186 }
Baptiste Assmann9b6857e2015-09-18 14:49:12 +02003187 }
3188
3189 return 1;
3190}
3191
Baptiste Assmann28289462015-07-03 08:01:20 +02003192/* Parses backend list or simply use backend name provided by the user to return
3193 * states of servers to stdout.
3194 */
3195static int stats_dump_servers_state_to_buffer(struct stream_interface *si)
3196{
3197 struct appctx *appctx = __objt_appctx(si->end);
3198 extern struct proxy *proxy;
3199 struct proxy *curproxy;
3200
3201 chunk_reset(&trash);
3202
Cyril Bonté76a99782016-05-06 12:18:48 +02003203 if (!appctx->ctx.server_state.px) {
3204 chunk_printf(&trash, "%d\n# %s\n", SRV_STATE_FILE_VERSION, SRV_STATE_FILE_FIELD_NAMES);
3205 if (bi_putchk(si_ic(si), &trash) == -1) {
3206 si_applet_cant_put(si);
3207 return 0;
Baptiste Assmann28289462015-07-03 08:01:20 +02003208 }
Cyril Bonté76a99782016-05-06 12:18:48 +02003209 appctx->ctx.server_state.px = proxy;
Baptiste Assmann28289462015-07-03 08:01:20 +02003210 }
3211
Cyril Bonté76a99782016-05-06 12:18:48 +02003212 for (; appctx->ctx.server_state.px != NULL; appctx->ctx.server_state.px = curproxy->next) {
3213 curproxy = appctx->ctx.server_state.px;
Cyril Bonté76a99782016-05-06 12:18:48 +02003214 /* servers are only in backends */
3215 if (curproxy->cap & PR_CAP_BE) {
3216 if (!dump_servers_state(si, &trash))
3217 return 0;
3218
3219 if (bi_putchk(si_ic(si), &trash) == -1) {
3220 si_applet_cant_put(si);
3221 return 0;
3222 }
3223 }
3224 /* only the selected proxy is dumped */
3225 if (appctx->ctx.server_state.iid)
3226 break;
Baptiste Assmann28289462015-07-03 08:01:20 +02003227 }
3228
3229 return 1;
3230}
3231
Willy Tarreau12833bb2014-01-28 16:49:56 +01003232/* This function dumps memory usage information onto the stream interface's
3233 * read buffer. It returns 0 as long as it does not complete, non-zero upon
3234 * completion. No state is used.
3235 */
3236static int stats_dump_pools_to_buffer(struct stream_interface *si)
3237{
3238 dump_pools_to_trash();
Willy Tarreaubc18da12015-03-13 14:00:47 +01003239 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02003240 si_applet_cant_put(si);
Willy Tarreau12833bb2014-01-28 16:49:56 +01003241 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01003242 }
Willy Tarreau82a86022016-01-04 19:04:18 +01003243 return 1;
3244}
3245
3246/* Dump all fields from <stats> into <out> using CSV format */
3247static int stats_dump_fields_csv(struct chunk *out, const struct field *stats)
3248{
3249 int field;
3250
3251 for (field = 0; field < ST_F_TOTAL_FIELDS; field++) {
3252 if (!stats_emit_raw_data_field(out, &stats[field]))
3253 return 0;
3254 if (!chunk_strcat(out, ","))
3255 return 0;
3256 }
Conrad Hoffmann692c9382016-04-01 20:40:58 +02003257 chunk_strcat(out, "\n");
Willy Tarreau12833bb2014-01-28 16:49:56 +01003258 return 1;
3259}
3260
Willy Tarreau1e62df92016-01-11 18:57:53 +01003261/* Dump all fields from <stats> into <out> using a typed "field:desc:type:value" format */
3262static int stats_dump_fields_typed(struct chunk *out, const struct field *stats)
3263{
3264 int field;
3265
3266 for (field = 0; field < ST_F_TOTAL_FIELDS; field++) {
3267 if (!stats[field].type)
3268 continue;
3269
3270 chunk_appendf(out, "%c.%u.%u.%d.%s.%u:",
3271 stats[ST_F_TYPE].u.u32 == STATS_TYPE_FE ? 'F' :
3272 stats[ST_F_TYPE].u.u32 == STATS_TYPE_BE ? 'B' :
3273 stats[ST_F_TYPE].u.u32 == STATS_TYPE_SO ? 'L' :
3274 stats[ST_F_TYPE].u.u32 == STATS_TYPE_SV ? 'S' :
3275 '?',
3276 stats[ST_F_IID].u.u32, stats[ST_F_SID].u.u32,
3277 field, stat_field_names[field], stats[ST_F_PID].u.u32);
3278
3279 if (!stats_emit_field_tags(out, &stats[field], ':'))
3280 return 0;
3281 if (!stats_emit_typed_data_field(out, &stats[field]))
3282 return 0;
3283 if (!chunk_strcat(out, "\n"))
3284 return 0;
3285 }
3286 return 1;
3287}
3288
Willy Tarreau60600742016-01-11 15:32:30 +01003289/* Dump all fields from <stats> into <out> using the HTML format. A column is
Willy Tarreau508a63f2016-01-11 15:28:40 +01003290 * reserved for the checkbox is ST_SHOWADMIN is set in <flags>. Some extra info
3291 * are provided if ST_SHLGNDS is present in <flags>.
Cyril Bonté70be45d2010-10-12 00:14:35 +02003292 */
Willy Tarreau60600742016-01-11 15:32:30 +01003293static int stats_dump_fields_html(struct chunk *out, const struct field *stats, unsigned int flags)
Cyril Bonté70be45d2010-10-12 00:14:35 +02003294{
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003295 struct chunk src;
3296
Willy Tarreaub5f66b82016-01-08 17:15:39 +01003297 if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_FE) {
Willy Tarreau60600742016-01-11 15:32:30 +01003298 chunk_appendf(out,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003299 /* name, queue */
3300 "<tr class=\"frontend\">");
Cyril Bonté70be45d2010-10-12 00:14:35 +02003301
Willy Tarreau508a63f2016-01-11 15:28:40 +01003302 if (flags & ST_SHOWADMIN) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003303 /* Column sub-heading for Enable or Disable server */
Willy Tarreau60600742016-01-11 15:32:30 +01003304 chunk_appendf(out, "<td></td>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003305 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02003306
Willy Tarreau60600742016-01-11 15:32:30 +01003307 chunk_appendf(out,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003308 "<td class=ac>"
3309 "<a name=\"%s/Frontend\"></a>"
3310 "<a class=lfsb href=\"#%s/Frontend\">Frontend</a></td>"
3311 "<td colspan=3></td>"
3312 "",
Willy Tarreaud72c9172016-01-06 15:26:40 +01003313 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_PXNAME));
Cyril Bonté70be45d2010-10-12 00:14:35 +02003314
Willy Tarreau60600742016-01-11 15:32:30 +01003315 chunk_appendf(out,
Willy Tarreau466c9b52012-12-23 02:25:03 +01003316 /* sessions rate : current */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003317 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01003318 "<tr><th>Current connection rate:</th><td>%s/s</td></tr>"
3319 "<tr><th>Current session rate:</th><td>%s/s</td></tr>"
3320 "",
Willy Tarreaud72c9172016-01-06 15:26:40 +01003321 U2H(stats[ST_F_RATE].u.u32),
Willy Tarreauc73810f2016-01-11 13:52:04 +01003322 U2H(stats[ST_F_CONN_RATE].u.u32),
Willy Tarreaud72c9172016-01-06 15:26:40 +01003323 U2H(stats[ST_F_RATE].u.u32));
Willy Tarreau466c9b52012-12-23 02:25:03 +01003324
Willy Tarreauf8211df2016-01-11 14:09:38 +01003325 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
Willy Tarreau60600742016-01-11 15:32:30 +01003326 chunk_appendf(out,
Willy Tarreau466c9b52012-12-23 02:25:03 +01003327 "<tr><th>Current request rate:</th><td>%s/s</td></tr>",
Willy Tarreaud72c9172016-01-06 15:26:40 +01003328 U2H(stats[ST_F_REQ_RATE].u.u32));
Willy Tarreau466c9b52012-12-23 02:25:03 +01003329
Willy Tarreau60600742016-01-11 15:32:30 +01003330 chunk_appendf(out,
Willy Tarreau466c9b52012-12-23 02:25:03 +01003331 "</table></div></u></td>"
3332 /* sessions rate : max */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003333 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01003334 "<tr><th>Max connection rate:</th><td>%s/s</td></tr>"
3335 "<tr><th>Max session rate:</th><td>%s/s</td></tr>"
3336 "",
Willy Tarreaud72c9172016-01-06 15:26:40 +01003337 U2H(stats[ST_F_RATE_MAX].u.u32),
Willy Tarreauc73810f2016-01-11 13:52:04 +01003338 U2H(stats[ST_F_CONN_RATE_MAX].u.u32),
Willy Tarreaud72c9172016-01-06 15:26:40 +01003339 U2H(stats[ST_F_RATE_MAX].u.u32));
Willy Tarreau466c9b52012-12-23 02:25:03 +01003340
Willy Tarreauf8211df2016-01-11 14:09:38 +01003341 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
Willy Tarreau60600742016-01-11 15:32:30 +01003342 chunk_appendf(out,
Willy Tarreau466c9b52012-12-23 02:25:03 +01003343 "<tr><th>Max request rate:</th><td>%s/s</td></tr>",
Willy Tarreaud72c9172016-01-06 15:26:40 +01003344 U2H(stats[ST_F_REQ_RATE_MAX].u.u32));
Willy Tarreau466c9b52012-12-23 02:25:03 +01003345
Willy Tarreau60600742016-01-11 15:32:30 +01003346 chunk_appendf(out,
Willy Tarreau466c9b52012-12-23 02:25:03 +01003347 "</table></div></u></td>"
3348 /* sessions rate : limit */
3349 "<td>%s</td>",
Willy Tarreaud72c9172016-01-06 15:26:40 +01003350 LIM2A(stats[ST_F_RATE_LIM].u.u32, "-"));
Cyril Bonté70be45d2010-10-12 00:14:35 +02003351
Willy Tarreau60600742016-01-11 15:32:30 +01003352 chunk_appendf(out,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003353 /* sessions: current, max, limit, total */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003354 "<td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003355 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01003356 "<tr><th>Cum. connections:</th><td>%s</td></tr>"
3357 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003358 "",
Willy Tarreauc73810f2016-01-11 13:52:04 +01003359 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 +01003360 U2H(stats[ST_F_STOT].u.u64),
Willy Tarreauc73810f2016-01-11 13:52:04 +01003361 U2H(stats[ST_F_CONN_TOT].u.u64),
Willy Tarreaud72c9172016-01-06 15:26:40 +01003362 U2H(stats[ST_F_STOT].u.u64));
Cyril Bonté70be45d2010-10-12 00:14:35 +02003363
Willy Tarreau466c9b52012-12-23 02:25:03 +01003364 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
Willy Tarreauf8211df2016-01-11 14:09:38 +01003365 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
Willy Tarreau60600742016-01-11 15:32:30 +01003366 chunk_appendf(out,
Willy Tarreau466c9b52012-12-23 02:25:03 +01003367 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
3368 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
3369 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
3370 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
3371 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
3372 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
3373 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
3374 "<tr><th>- other responses:</th><td>%s</td></tr>"
3375 "<tr><th>Intercepted requests:</th><td>%s</td></tr>"
3376 "",
Willy Tarreaud72c9172016-01-06 15:26:40 +01003377 U2H(stats[ST_F_REQ_TOT].u.u64),
3378 U2H(stats[ST_F_HRSP_1XX].u.u64),
3379 U2H(stats[ST_F_HRSP_2XX].u.u64),
3380 U2H(stats[ST_F_COMP_RSP].u.u64),
3381 stats[ST_F_HRSP_2XX].u.u64 ?
3382 (int)(100 * stats[ST_F_COMP_RSP].u.u64 / stats[ST_F_HRSP_2XX].u.u64) : 0,
3383 U2H(stats[ST_F_HRSP_3XX].u.u64),
3384 U2H(stats[ST_F_HRSP_4XX].u.u64),
3385 U2H(stats[ST_F_HRSP_5XX].u.u64),
3386 U2H(stats[ST_F_HRSP_OTHER].u.u64),
Willy Tarreau5b9bdff2016-01-11 14:40:47 +01003387 U2H(stats[ST_F_INTERCEPTED].u.u64));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003388 }
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02003389
Willy Tarreau60600742016-01-11 15:32:30 +01003390 chunk_appendf(out,
Willy Tarreau466c9b52012-12-23 02:25:03 +01003391 "</table></div></u></td>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05003392 /* sessions: lbtot, lastsess */
3393 "<td></td><td></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003394 /* bytes : in */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003395 "<td>%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003396 "",
Willy Tarreaud72c9172016-01-06 15:26:40 +01003397 U2H(stats[ST_F_BIN].u.u64));
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02003398
Willy Tarreau60600742016-01-11 15:32:30 +01003399 chunk_appendf(out,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003400 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
Willy Tarreau4e5d58e2015-07-04 14:35:15 +02003401 "<td>%s%s<div class=tips><table class=det>"
3402 "<tr><th>Response bytes in:</th><td>%s</td></tr>"
3403 "<tr><th>Compression in:</th><td>%s</td></tr>"
3404 "<tr><th>Compression out:</th><td>%s</td><td>(%d%%)</td></tr>"
3405 "<tr><th>Compression bypass:</th><td>%s</td></tr>"
3406 "<tr><th>Total bytes saved:</th><td>%s</td><td>(%d%%)</td></tr>"
3407 "</table></div>%s</td>",
Willy Tarreaud72c9172016-01-06 15:26:40 +01003408 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "<u>":"",
3409 U2H(stats[ST_F_BOUT].u.u64),
3410 U2H(stats[ST_F_BOUT].u.u64),
3411 U2H(stats[ST_F_COMP_IN].u.u64),
3412 U2H(stats[ST_F_COMP_OUT].u.u64),
3413 stats[ST_F_COMP_IN].u.u64 ? (int)(stats[ST_F_COMP_OUT].u.u64 * 100 / stats[ST_F_COMP_IN].u.u64) : 0,
3414 U2H(stats[ST_F_COMP_BYP].u.u64),
3415 U2H(stats[ST_F_COMP_IN].u.u64 - stats[ST_F_COMP_OUT].u.u64),
3416 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,
3417 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "</u>":"");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02003418
Willy Tarreau60600742016-01-11 15:32:30 +01003419 chunk_appendf(out,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003420 /* denied: req, resp */
3421 "<td>%s</td><td>%s</td>"
3422 /* errors : request, connect, response */
3423 "<td>%s</td><td></td><td></td>"
3424 /* warnings: retries, redispatches */
3425 "<td></td><td></td>"
3426 /* server status : reflect frontend status */
3427 "<td class=ac>%s</td>"
3428 /* rest of server: nothing */
3429 "<td class=ac colspan=8></td></tr>"
3430 "",
Willy Tarreaud72c9172016-01-06 15:26:40 +01003431 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
3432 U2H(stats[ST_F_EREQ].u.u64),
3433 field_str(stats, ST_F_STATUS));
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02003434 }
Willy Tarreaufa9512f2016-01-08 17:20:51 +01003435 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_SO) {
Willy Tarreau60600742016-01-11 15:32:30 +01003436 chunk_appendf(out, "<tr class=socket>");
Willy Tarreau508a63f2016-01-11 15:28:40 +01003437 if (flags & ST_SHOWADMIN) {
Willy Tarreaufa9512f2016-01-08 17:20:51 +01003438 /* Column sub-heading for Enable or Disable server */
Willy Tarreau60600742016-01-11 15:32:30 +01003439 chunk_appendf(out, "<td></td>");
Willy Tarreaufa9512f2016-01-08 17:20:51 +01003440 }
3441
Willy Tarreau60600742016-01-11 15:32:30 +01003442 chunk_appendf(out,
Willy Tarreaufa9512f2016-01-08 17:20:51 +01003443 /* frontend name, listener name */
3444 "<td class=ac><a name=\"%s/+%s\"></a>%s"
3445 "<a class=lfsb href=\"#%s/+%s\">%s</a>"
3446 "",
3447 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME),
3448 (flags & ST_SHLGNDS)?"<u>":"",
3449 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME), field_str(stats, ST_F_SVNAME));
3450
3451 if (flags & ST_SHLGNDS) {
Willy Tarreau60600742016-01-11 15:32:30 +01003452 chunk_appendf(out, "<div class=tips>");
Willy Tarreaufa9512f2016-01-08 17:20:51 +01003453
3454 if (isdigit(*field_str(stats, ST_F_ADDR)))
Willy Tarreau60600742016-01-11 15:32:30 +01003455 chunk_appendf(out, "IPv4: %s, ", field_str(stats, ST_F_ADDR));
Willy Tarreaufa9512f2016-01-08 17:20:51 +01003456 else if (*field_str(stats, ST_F_ADDR) == '[')
Willy Tarreau60600742016-01-11 15:32:30 +01003457 chunk_appendf(out, "IPv6: %s, ", field_str(stats, ST_F_ADDR));
Willy Tarreaufa9512f2016-01-08 17:20:51 +01003458 else if (*field_str(stats, ST_F_ADDR))
Willy Tarreau60600742016-01-11 15:32:30 +01003459 chunk_appendf(out, "%s, ", field_str(stats, ST_F_ADDR));
Willy Tarreaufa9512f2016-01-08 17:20:51 +01003460
3461 /* id */
Willy Tarreau60600742016-01-11 15:32:30 +01003462 chunk_appendf(out, "id: %d</div>", stats[ST_F_SID].u.u32);
Willy Tarreaufa9512f2016-01-08 17:20:51 +01003463 }
3464
Willy Tarreau60600742016-01-11 15:32:30 +01003465 chunk_appendf(out,
Willy Tarreaufa9512f2016-01-08 17:20:51 +01003466 /* queue */
3467 "%s</td><td colspan=3></td>"
3468 /* sessions rate: current, max, limit */
3469 "<td colspan=3>&nbsp;</td>"
3470 /* sessions: current, max, limit, total, lbtot, lastsess */
3471 "<td>%s</td><td>%s</td><td>%s</td>"
3472 "<td>%s</td><td>&nbsp;</td><td>&nbsp;</td>"
3473 /* bytes: in, out */
3474 "<td>%s</td><td>%s</td>"
3475 "",
3476 (flags & ST_SHLGNDS)?"</u>":"",
3477 U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SMAX].u.u32), U2H(stats[ST_F_SLIM].u.u32),
3478 U2H(stats[ST_F_STOT].u.u64), U2H(stats[ST_F_BIN].u.u64), U2H(stats[ST_F_BOUT].u.u64));
3479
Willy Tarreau60600742016-01-11 15:32:30 +01003480 chunk_appendf(out,
Willy Tarreaufa9512f2016-01-08 17:20:51 +01003481 /* denied: req, resp */
3482 "<td>%s</td><td>%s</td>"
3483 /* errors: request, connect, response */
3484 "<td>%s</td><td></td><td></td>"
3485 /* warnings: retries, redispatches */
3486 "<td></td><td></td>"
3487 /* server status: reflect listener status */
3488 "<td class=ac>%s</td>"
3489 /* rest of server: nothing */
3490 "<td class=ac colspan=8></td></tr>"
3491 "",
3492 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
3493 U2H(stats[ST_F_EREQ].u.u64),
3494 field_str(stats, ST_F_STATUS));
3495 }
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003496 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_SV) {
3497 const char *style;
Willy Tarreaub5f66b82016-01-08 17:15:39 +01003498
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003499 /* determine the style to use depending on the server's state,
3500 * its health and weight. There isn't a 1-to-1 mapping between
3501 * state and styles for the cases where the server is (still)
3502 * up. The reason is that we don't want to report nolb and
3503 * drain with the same color.
3504 */
Willy Tarreaub5f66b82016-01-08 17:15:39 +01003505
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003506 if (strcmp(field_str(stats, ST_F_STATUS), "DOWN") == 0 ||
3507 strcmp(field_str(stats, ST_F_STATUS), "DOWN (agent)") == 0) {
3508 style = "down";
3509 }
3510 else if (strcmp(field_str(stats, ST_F_STATUS), "DOWN ") == 0) {
3511 style = "going_up";
3512 }
3513 else if (strcmp(field_str(stats, ST_F_STATUS), "NOLB ") == 0) {
3514 style = "going_down";
3515 }
3516 else if (strcmp(field_str(stats, ST_F_STATUS), "NOLB") == 0) {
3517 style = "nolb";
3518 }
3519 else if (strcmp(field_str(stats, ST_F_STATUS), "no check") == 0) {
3520 style = "no_check";
3521 }
3522 else if (!stats[ST_F_CHKFAIL].type ||
3523 stats[ST_F_CHECK_HEALTH].u.u32 == stats[ST_F_CHECK_RISE].u.u32 + stats[ST_F_CHECK_FALL].u.u32 - 1) {
3524 /* no check or max health = UP */
3525 if (stats[ST_F_WEIGHT].u.u32)
3526 style = "up";
3527 else
3528 style = "draining";
3529 }
3530 else {
3531 style = "going_down";
3532 }
Willy Tarreaub5f66b82016-01-08 17:15:39 +01003533
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003534 if (memcmp(field_str(stats, ST_F_STATUS), "MAINT", 5) == 0)
Willy Tarreau60600742016-01-11 15:32:30 +01003535 chunk_appendf(out, "<tr class=\"maintain\">");
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003536 else
Willy Tarreau60600742016-01-11 15:32:30 +01003537 chunk_appendf(out,
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003538 "<tr class=\"%s_%s\">",
3539 (stats[ST_F_BCK].u.u32) ? "backup" : "active", style);
Willy Tarreaub5f66b82016-01-08 17:15:39 +01003540
Willy Tarreaub5f66b82016-01-08 17:15:39 +01003541
Willy Tarreau508a63f2016-01-11 15:28:40 +01003542 if (flags & ST_SHOWADMIN)
Willy Tarreau60600742016-01-11 15:32:30 +01003543 chunk_appendf(out,
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003544 "<td><input type=\"checkbox\" name=\"s\" value=\"%s\"></td>",
3545 field_str(stats, ST_F_SVNAME));
Willy Tarreaub5f66b82016-01-08 17:15:39 +01003546
Willy Tarreau60600742016-01-11 15:32:30 +01003547 chunk_appendf(out,
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003548 "<td class=ac><a name=\"%s/%s\"></a>%s"
3549 "<a class=lfsb href=\"#%s/%s\">%s</a>"
3550 "",
3551 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME),
3552 (flags & ST_SHLGNDS) ? "<u>" : "",
3553 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 +01003554
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003555 if (flags & ST_SHLGNDS) {
Willy Tarreau60600742016-01-11 15:32:30 +01003556 chunk_appendf(out, "<div class=tips>");
Willy Tarreaub5f66b82016-01-08 17:15:39 +01003557
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003558 if (isdigit(*field_str(stats, ST_F_ADDR)))
Willy Tarreau60600742016-01-11 15:32:30 +01003559 chunk_appendf(out, "IPv4: %s, ", field_str(stats, ST_F_ADDR));
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003560 else if (*field_str(stats, ST_F_ADDR) == '[')
Willy Tarreau60600742016-01-11 15:32:30 +01003561 chunk_appendf(out, "IPv6: %s, ", field_str(stats, ST_F_ADDR));
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003562 else if (*field_str(stats, ST_F_ADDR))
Willy Tarreau60600742016-01-11 15:32:30 +01003563 chunk_appendf(out, "%s, ", field_str(stats, ST_F_ADDR));
Willy Tarreaub5f66b82016-01-08 17:15:39 +01003564
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003565 /* id */
Willy Tarreau60600742016-01-11 15:32:30 +01003566 chunk_appendf(out, "id: %d", stats[ST_F_SID].u.u32);
Willy Tarreaub5f66b82016-01-08 17:15:39 +01003567
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003568 /* cookie */
3569 if (stats[ST_F_COOKIE].type) {
Willy Tarreau60600742016-01-11 15:32:30 +01003570 chunk_appendf(out, ", cookie: '");
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003571 chunk_initstr(&src, field_str(stats, ST_F_COOKIE));
Willy Tarreau60600742016-01-11 15:32:30 +01003572 chunk_htmlencode(out, &src);
3573 chunk_appendf(out, "'");
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003574 }
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02003575
Willy Tarreau60600742016-01-11 15:32:30 +01003576 chunk_appendf(out, "</div>");
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003577 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003578
Willy Tarreau60600742016-01-11 15:32:30 +01003579 chunk_appendf(out,
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003580 /* queue : current, max, limit */
3581 "%s</td><td>%s</td><td>%s</td><td>%s</td>"
3582 /* sessions rate : current, max, limit */
3583 "<td>%s</td><td>%s</td><td></td>"
3584 "",
3585 (flags & ST_SHLGNDS) ? "</u>" : "",
3586 U2H(stats[ST_F_QCUR].u.u32), U2H(stats[ST_F_QMAX].u.u32), LIM2A(stats[ST_F_QLIMIT].u.u32, "-"),
3587 U2H(stats[ST_F_RATE].u.u32), U2H(stats[ST_F_RATE_MAX].u.u32));
Willy Tarreau4607fad2016-01-06 15:41:29 +01003588
Willy Tarreau60600742016-01-11 15:32:30 +01003589 chunk_appendf(out,
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003590 /* sessions: current, max, limit, total */
3591 "<td>%s</td><td>%s</td><td>%s</td>"
3592 "<td><u>%s<div class=tips><table class=det>"
3593 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
3594 "",
3595 U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SMAX].u.u32), LIM2A(stats[ST_F_SLIM].u.u32, "-"),
3596 U2H(stats[ST_F_STOT].u.u64),
3597 U2H(stats[ST_F_STOT].u.u64));
Willy Tarreau4607fad2016-01-06 15:41:29 +01003598
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003599 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
Willy Tarreauf8211df2016-01-11 14:09:38 +01003600 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003601 unsigned long long tot;
Willy Tarreaua6f5a732016-01-08 16:59:56 +01003602
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003603 tot = stats[ST_F_HRSP_OTHER].u.u64;
3604 tot += stats[ST_F_HRSP_1XX].u.u64;
3605 tot += stats[ST_F_HRSP_2XX].u.u64;
3606 tot += stats[ST_F_HRSP_3XX].u.u64;
3607 tot += stats[ST_F_HRSP_4XX].u.u64;
3608 tot += stats[ST_F_HRSP_5XX].u.u64;
3609
Willy Tarreau60600742016-01-11 15:32:30 +01003610 chunk_appendf(out,
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003611 "<tr><th>Cum. HTTP responses:</th><td>%s</td></tr>"
3612 "<tr><th>- HTTP 1xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
3613 "<tr><th>- HTTP 2xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
3614 "<tr><th>- HTTP 3xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
3615 "<tr><th>- HTTP 4xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
3616 "<tr><th>- HTTP 5xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
3617 "<tr><th>- other responses:</th><td>%s</td><td>(%d%%)</td></tr>"
3618 "",
3619 U2H(tot),
3620 U2H(stats[ST_F_HRSP_1XX].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_1XX].u.u64 / tot) : 0,
3621 U2H(stats[ST_F_HRSP_2XX].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_2XX].u.u64 / tot) : 0,
3622 U2H(stats[ST_F_HRSP_3XX].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_3XX].u.u64 / tot) : 0,
3623 U2H(stats[ST_F_HRSP_4XX].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_4XX].u.u64 / tot) : 0,
3624 U2H(stats[ST_F_HRSP_5XX].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_5XX].u.u64 / tot) : 0,
3625 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 +01003626 }
Willy Tarreaua6f5a732016-01-08 16:59:56 +01003627
Willy Tarreau60600742016-01-11 15:32:30 +01003628 chunk_appendf(out, "<tr><th colspan=3>Avg over last 1024 success. conn.</th></tr>");
3629 chunk_appendf(out, "<tr><th>- Queue time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_QTIME].u.u32));
3630 chunk_appendf(out, "<tr><th>- Connect time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_CTIME].u.u32));
Willy Tarreauf8211df2016-01-11 14:09:38 +01003631 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
Willy Tarreau60600742016-01-11 15:32:30 +01003632 chunk_appendf(out, "<tr><th>- Response time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_RTIME].u.u32));
3633 chunk_appendf(out, "<tr><th>- Total time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_TTIME].u.u32));
Willy Tarreau56a560a2009-09-22 19:27:35 +02003634
Willy Tarreau60600742016-01-11 15:32:30 +01003635 chunk_appendf(out,
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003636 "</table></div></u></td>"
3637 /* sessions: lbtot, last */
3638 "<td>%s</td><td>%s</td>",
3639 U2H(stats[ST_F_LBTOT].u.u64),
3640 human_time(stats[ST_F_LASTSESS].u.s32, 1));
Willy Tarreau91861262007-10-17 17:06:05 +02003641
Willy Tarreau60600742016-01-11 15:32:30 +01003642 chunk_appendf(out,
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003643 /* bytes : in, out */
3644 "<td>%s</td><td>%s</td>"
3645 /* denied: req, resp */
3646 "<td></td><td>%s</td>"
3647 /* errors : request, connect */
3648 "<td></td><td>%s</td>"
3649 /* errors : response */
3650 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
3651 /* warnings: retries, redispatches */
3652 "<td>%lld</td><td>%lld</td>"
3653 "",
3654 U2H(stats[ST_F_BIN].u.u64), U2H(stats[ST_F_BOUT].u.u64),
3655 U2H(stats[ST_F_DRESP].u.u64),
3656 U2H(stats[ST_F_ECON].u.u64),
3657 U2H(stats[ST_F_ERESP].u.u64),
3658 (long long)stats[ST_F_CLI_ABRT].u.u64,
3659 (long long)stats[ST_F_SRV_ABRT].u.u64,
3660 (long long)stats[ST_F_WRETR].u.u64,
3661 (long long)stats[ST_F_WREDIS].u.u64);
3662
3663 /* status, last change */
Willy Tarreau60600742016-01-11 15:32:30 +01003664 chunk_appendf(out, "<td class=ac>");
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003665
3666 /* FIXME!!!!
3667 * LASTCHG should contain the last change for *this* server and must be computed
3668 * properly above, as was done below, ie: this server if maint, otherwise ref server
3669 * if tracking. Note that ref is either local or remote depending on tracking.
3670 */
3671
3672
3673 if (memcmp(field_str(stats, ST_F_STATUS), "MAINT", 5) == 0) {
Willy Tarreau60600742016-01-11 15:32:30 +01003674 chunk_appendf(out, "%s MAINT", human_time(stats[ST_F_LASTCHG].u.u32, 1));
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003675 }
3676 else if (memcmp(field_str(stats, ST_F_STATUS), "no check", 5) == 0) {
Willy Tarreau60600742016-01-11 15:32:30 +01003677 chunk_strcat(out, "<i>no check</i>");
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003678 }
3679 else {
Willy Tarreau60600742016-01-11 15:32:30 +01003680 chunk_appendf(out, "%s %s", human_time(stats[ST_F_LASTCHG].u.u32, 1), field_str(stats, ST_F_STATUS));
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003681 if (memcmp(field_str(stats, ST_F_STATUS), "DOWN", 4) == 0) {
3682 if (stats[ST_F_CHECK_HEALTH].u.u32)
Willy Tarreau60600742016-01-11 15:32:30 +01003683 chunk_strcat(out, " &uarr;");
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003684 }
3685 else if (stats[ST_F_CHECK_HEALTH].u.u32 < stats[ST_F_CHECK_RISE].u.u32 + stats[ST_F_CHECK_FALL].u.u32 - 1)
Willy Tarreau60600742016-01-11 15:32:30 +01003686 chunk_strcat(out, " &darr;");
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003687 }
3688
3689 if (memcmp(field_str(stats, ST_F_STATUS), "DOWN", 4) == 0 &&
3690 stats[ST_F_AGENT_STATUS].type && !stats[ST_F_AGENT_HEALTH].u.u32) {
Willy Tarreau60600742016-01-11 15:32:30 +01003691 chunk_appendf(out,
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003692 "</td><td class=ac><u> %s",
3693 field_str(stats, ST_F_AGENT_STATUS));
3694
3695 if (stats[ST_F_AGENT_CODE].type)
Willy Tarreau60600742016-01-11 15:32:30 +01003696 chunk_appendf(out, "/%d", stats[ST_F_AGENT_CODE].u.u32);
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003697
3698 if (stats[ST_F_AGENT_DURATION].type && stats[ST_F_AGENT_DURATION].u.u64 >= 0)
Willy Tarreau60600742016-01-11 15:32:30 +01003699 chunk_appendf(out, " in %lums", (long)stats[ST_F_AGENT_DURATION].u.u64);
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003700
Willy Tarreau60600742016-01-11 15:32:30 +01003701 chunk_appendf(out, "<div class=tips>%s", field_str(stats, ST_F_AGENT_DESC));
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003702
3703 if (*field_str(stats, ST_F_LAST_AGT)) {
Willy Tarreau60600742016-01-11 15:32:30 +01003704 chunk_appendf(out, ": ");
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003705 chunk_initstr(&src, field_str(stats, ST_F_LAST_AGT));
Willy Tarreau60600742016-01-11 15:32:30 +01003706 chunk_htmlencode(out, &src);
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003707 }
Willy Tarreau60600742016-01-11 15:32:30 +01003708 chunk_appendf(out, "</div></u>");
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003709 }
3710 else if (stats[ST_F_CHECK_STATUS].type) {
Willy Tarreau60600742016-01-11 15:32:30 +01003711 chunk_appendf(out,
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003712 "</td><td class=ac><u> %s",
3713 field_str(stats, ST_F_CHECK_STATUS));
3714
3715 if (stats[ST_F_CHECK_CODE].type)
Willy Tarreau60600742016-01-11 15:32:30 +01003716 chunk_appendf(out, "/%d", stats[ST_F_CHECK_CODE].u.u32);
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003717
3718 if (stats[ST_F_CHECK_DURATION].type && stats[ST_F_CHECK_DURATION].u.u64 >= 0)
Willy Tarreau60600742016-01-11 15:32:30 +01003719 chunk_appendf(out, " in %lums", (long)stats[ST_F_CHECK_DURATION].u.u64);
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003720
Willy Tarreau60600742016-01-11 15:32:30 +01003721 chunk_appendf(out, "<div class=tips>%s", field_str(stats, ST_F_CHECK_DESC));
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003722
3723 if (*field_str(stats, ST_F_LAST_CHK)) {
Willy Tarreau60600742016-01-11 15:32:30 +01003724 chunk_appendf(out, ": ");
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003725 chunk_initstr(&src, field_str(stats, ST_F_LAST_CHK));
Willy Tarreau60600742016-01-11 15:32:30 +01003726 chunk_htmlencode(out, &src);
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003727 }
Willy Tarreau60600742016-01-11 15:32:30 +01003728 chunk_appendf(out, "</div></u>");
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003729 }
3730 else
Willy Tarreau60600742016-01-11 15:32:30 +01003731 chunk_appendf(out, "</td><td>");
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003732
Willy Tarreau60600742016-01-11 15:32:30 +01003733 chunk_appendf(out,
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003734 /* weight */
3735 "</td><td class=ac>%d</td>"
3736 /* act, bck */
3737 "<td class=ac>%s</td><td class=ac>%s</td>"
3738 "",
3739 stats[ST_F_WEIGHT].u.u32,
3740 stats[ST_F_BCK].u.u32 ? "-" : "Y",
3741 stats[ST_F_BCK].u.u32 ? "Y" : "-");
3742
3743 /* check failures: unique, fatal, down time */
3744 if (stats[ST_F_CHKFAIL].type) {
Willy Tarreau60600742016-01-11 15:32:30 +01003745 chunk_appendf(out, "<td><u>%lld", (long long)stats[ST_F_CHKFAIL].u.u64);
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003746
3747 if (stats[ST_F_HANAFAIL].type)
Willy Tarreau60600742016-01-11 15:32:30 +01003748 chunk_appendf(out, "/%lld", (long long)stats[ST_F_HANAFAIL].u.u64);
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003749
Willy Tarreau60600742016-01-11 15:32:30 +01003750 chunk_appendf(out,
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003751 "<div class=tips>Failed Health Checks%s</div></u></td>"
3752 "<td>%lld</td><td>%s</td>"
3753 "",
3754 stats[ST_F_HANAFAIL].type ? "/Health Analyses" : "",
3755 (long long)stats[ST_F_CHKDOWN].u.u64, human_time(stats[ST_F_DOWNTIME].u.u32, 1));
3756 }
3757 else if (strcmp(field_str(stats, ST_F_STATUS), "MAINT") != 0 && field_format(stats, ST_F_TRACKED) == FF_STR) {
3758 /* tracking a server (hence inherited maint would appear as "MAINT (via...)" */
Willy Tarreau60600742016-01-11 15:32:30 +01003759 chunk_appendf(out,
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003760 "<td class=ac colspan=3><a class=lfsb href=\"#%s\">via %s</a></td>",
3761 field_str(stats, ST_F_TRACKED), field_str(stats, ST_F_TRACKED));
3762 }
3763 else
Willy Tarreau60600742016-01-11 15:32:30 +01003764 chunk_appendf(out, "<td colspan=3></td>");
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003765
3766 /* throttle */
3767 if (stats[ST_F_THROTTLE].type)
Willy Tarreau60600742016-01-11 15:32:30 +01003768 chunk_appendf(out, "<td class=ac>%d %%</td></tr>\n", stats[ST_F_THROTTLE].u.u32);
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003769 else
Willy Tarreau60600742016-01-11 15:32:30 +01003770 chunk_appendf(out, "<td class=ac>-</td></tr>\n");
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003771 }
Willy Tarreaubbf84502016-01-08 17:25:50 +01003772 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_BE) {
Willy Tarreau60600742016-01-11 15:32:30 +01003773 chunk_appendf(out, "<tr class=\"backend\">");
Willy Tarreau508a63f2016-01-11 15:28:40 +01003774 if (flags & ST_SHOWADMIN) {
Willy Tarreaubbf84502016-01-08 17:25:50 +01003775 /* Column sub-heading for Enable or Disable server */
Willy Tarreau60600742016-01-11 15:32:30 +01003776 chunk_appendf(out, "<td></td>");
Willy Tarreaubbf84502016-01-08 17:25:50 +01003777 }
Willy Tarreau60600742016-01-11 15:32:30 +01003778 chunk_appendf(out,
Willy Tarreaubbf84502016-01-08 17:25:50 +01003779 "<td class=ac>"
3780 /* name */
3781 "%s<a name=\"%s/Backend\"></a>"
3782 "<a class=lfsb href=\"#%s/Backend\">Backend</a>"
3783 "",
3784 (flags & ST_SHLGNDS)?"<u>":"",
3785 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_PXNAME));
3786
3787 if (flags & ST_SHLGNDS) {
3788 /* balancing */
Willy Tarreau60600742016-01-11 15:32:30 +01003789 chunk_appendf(out, "<div class=tips>balancing: %s",
Willy Tarreauf1516d92016-01-11 14:48:36 +01003790 field_str(stats, ST_F_ALGO));
Willy Tarreaubbf84502016-01-08 17:25:50 +01003791
3792 /* cookie */
3793 if (stats[ST_F_COOKIE].type) {
Willy Tarreau60600742016-01-11 15:32:30 +01003794 chunk_appendf(out, ", cookie: '");
Willy Tarreaubbf84502016-01-08 17:25:50 +01003795 chunk_initstr(&src, field_str(stats, ST_F_COOKIE));
Willy Tarreau60600742016-01-11 15:32:30 +01003796 chunk_htmlencode(out, &src);
3797 chunk_appendf(out, "'");
Willy Tarreaubbf84502016-01-08 17:25:50 +01003798 }
Willy Tarreau60600742016-01-11 15:32:30 +01003799 chunk_appendf(out, "</div>");
Willy Tarreaubbf84502016-01-08 17:25:50 +01003800 }
3801
Willy Tarreau60600742016-01-11 15:32:30 +01003802 chunk_appendf(out,
Willy Tarreaubbf84502016-01-08 17:25:50 +01003803 "%s</td>"
3804 /* queue : current, max */
3805 "<td>%s</td><td>%s</td><td></td>"
3806 /* sessions rate : current, max, limit */
3807 "<td>%s</td><td>%s</td><td></td>"
3808 "",
3809 (flags & ST_SHLGNDS)?"</u>":"",
3810 U2H(stats[ST_F_QCUR].u.u32), U2H(stats[ST_F_QMAX].u.u32),
3811 U2H(stats[ST_F_RATE].u.u32), U2H(stats[ST_F_RATE_MAX].u.u32));
3812
Willy Tarreau60600742016-01-11 15:32:30 +01003813 chunk_appendf(out,
Willy Tarreaubbf84502016-01-08 17:25:50 +01003814 /* sessions: current, max, limit, total */
3815 "<td>%s</td><td>%s</td><td>%s</td>"
3816 "<td><u>%s<div class=tips><table class=det>"
3817 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
3818 "",
3819 U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SLIM].u.u32),
3820 U2H(stats[ST_F_STOT].u.u64),
3821 U2H(stats[ST_F_STOT].u.u64));
3822
3823 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
Willy Tarreauf8211df2016-01-11 14:09:38 +01003824 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
Willy Tarreau60600742016-01-11 15:32:30 +01003825 chunk_appendf(out,
Willy Tarreaubbf84502016-01-08 17:25:50 +01003826 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
3827 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
3828 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
3829 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
3830 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
3831 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
3832 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
3833 "<tr><th>- other responses:</th><td>%s</td></tr>"
3834 "<tr><th>Intercepted requests:</th><td>%s</td></tr>"
3835 "<tr><th colspan=3>Avg over last 1024 success. conn.</th></tr>"
3836 "",
3837 U2H(stats[ST_F_REQ_TOT].u.u64),
3838 U2H(stats[ST_F_HRSP_1XX].u.u64),
3839 U2H(stats[ST_F_HRSP_2XX].u.u64),
3840 U2H(stats[ST_F_COMP_RSP].u.u64),
3841 stats[ST_F_HRSP_2XX].u.u64 ?
3842 (int)(100 * stats[ST_F_COMP_RSP].u.u64 / stats[ST_F_HRSP_2XX].u.u64) : 0,
3843 U2H(stats[ST_F_HRSP_3XX].u.u64),
3844 U2H(stats[ST_F_HRSP_4XX].u.u64),
3845 U2H(stats[ST_F_HRSP_5XX].u.u64),
3846 U2H(stats[ST_F_HRSP_OTHER].u.u64),
Willy Tarreau5b9bdff2016-01-11 14:40:47 +01003847 U2H(stats[ST_F_INTERCEPTED].u.u64));
Willy Tarreaubbf84502016-01-08 17:25:50 +01003848 }
3849
Willy Tarreau60600742016-01-11 15:32:30 +01003850 chunk_appendf(out, "<tr><th>- Queue time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_QTIME].u.u32));
3851 chunk_appendf(out, "<tr><th>- Connect time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_QTIME].u.u32));
Willy Tarreauf8211df2016-01-11 14:09:38 +01003852 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
Willy Tarreau60600742016-01-11 15:32:30 +01003853 chunk_appendf(out, "<tr><th>- Response time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_RTIME].u.u32));
3854 chunk_appendf(out, "<tr><th>- Total time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_TTIME].u.u32));
Willy Tarreaubbf84502016-01-08 17:25:50 +01003855
Willy Tarreau60600742016-01-11 15:32:30 +01003856 chunk_appendf(out,
Willy Tarreaubbf84502016-01-08 17:25:50 +01003857 "</table></div></u></td>"
3858 /* sessions: lbtot, last */
3859 "<td>%s</td><td>%s</td>"
3860 /* bytes: in */
3861 "<td>%s</td>"
3862 "",
3863 U2H(stats[ST_F_LBTOT].u.u64),
3864 human_time(stats[ST_F_LASTSESS].u.s32, 1),
3865 U2H(stats[ST_F_BIN].u.u64));
3866
Willy Tarreau60600742016-01-11 15:32:30 +01003867 chunk_appendf(out,
Willy Tarreaubbf84502016-01-08 17:25:50 +01003868 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
3869 "<td>%s%s<div class=tips><table class=det>"
3870 "<tr><th>Response bytes in:</th><td>%s</td></tr>"
3871 "<tr><th>Compression in:</th><td>%s</td></tr>"
3872 "<tr><th>Compression out:</th><td>%s</td><td>(%d%%)</td></tr>"
3873 "<tr><th>Compression bypass:</th><td>%s</td></tr>"
3874 "<tr><th>Total bytes saved:</th><td>%s</td><td>(%d%%)</td></tr>"
3875 "</table></div>%s</td>",
3876 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "<u>":"",
3877 U2H(stats[ST_F_BOUT].u.u64),
3878 U2H(stats[ST_F_BOUT].u.u64),
3879 U2H(stats[ST_F_COMP_IN].u.u64),
3880 U2H(stats[ST_F_COMP_OUT].u.u64),
3881 stats[ST_F_COMP_IN].u.u64 ? (int)(stats[ST_F_COMP_OUT].u.u64 * 100 / stats[ST_F_COMP_IN].u.u64) : 0,
3882 U2H(stats[ST_F_COMP_BYP].u.u64),
Christopher Faulet0b64f622016-04-28 15:09:31 +02003883 U2H(stats[ST_F_COMP_IN].u.u64 - stats[ST_F_COMP_OUT].u.u64),
Willy Tarreaubbf84502016-01-08 17:25:50 +01003884 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,
3885 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "</u>":"");
3886
Willy Tarreau60600742016-01-11 15:32:30 +01003887 chunk_appendf(out,
Willy Tarreaubbf84502016-01-08 17:25:50 +01003888 /* denied: req, resp */
3889 "<td>%s</td><td>%s</td>"
3890 /* errors : request, connect */
3891 "<td></td><td>%s</td>"
3892 /* errors : response */
3893 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
3894 /* warnings: retries, redispatches */
3895 "<td>%lld</td><td>%lld</td>"
3896 /* backend status: reflect backend status (up/down): we display UP
3897 * if the backend has known working servers or if it has no server at
3898 * all (eg: for stats). Then we display the total weight, number of
3899 * active and backups. */
3900 "<td class=ac>%s %s</td><td class=ac>&nbsp;</td><td class=ac>%d</td>"
3901 "<td class=ac>%d</td><td class=ac>%d</td>"
3902 "",
3903 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
3904 U2H(stats[ST_F_ECON].u.u64),
3905 U2H(stats[ST_F_ERESP].u.u64),
3906 (long long)stats[ST_F_CLI_ABRT].u.u64,
3907 (long long)stats[ST_F_SRV_ABRT].u.u64,
3908 (long long)stats[ST_F_WRETR].u.u64, (long long)stats[ST_F_WREDIS].u.u64,
3909 human_time(stats[ST_F_LASTCHG].u.u32, 1),
3910 strcmp(field_str(stats, ST_F_STATUS), "DOWN") ? field_str(stats, ST_F_STATUS) : "<font color=\"red\"><b>DOWN</b></font>",
3911 stats[ST_F_WEIGHT].u.u32,
3912 stats[ST_F_ACT].u.u32, stats[ST_F_BCK].u.u32);
3913
Willy Tarreau60600742016-01-11 15:32:30 +01003914 chunk_appendf(out,
Willy Tarreaubbf84502016-01-08 17:25:50 +01003915 /* rest of backend: nothing, down transitions, total downtime, throttle */
3916 "<td class=ac>&nbsp;</td><td>%d</td>"
3917 "<td>%s</td>"
3918 "<td></td>"
3919 "</tr>",
3920 stats[ST_F_CHKDOWN].u.u32,
3921 stats[ST_F_DOWNTIME].type ? human_time(stats[ST_F_DOWNTIME].u.u32, 1) : "&nbsp;");
3922 }
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003923 return 1;
3924}
3925
Willy Tarreau501f6022016-01-08 17:37:22 +01003926static int stats_dump_one_line(const struct field *stats, unsigned int flags, struct proxy *px, struct appctx *appctx)
3927{
Willy Tarreau508a63f2016-01-11 15:28:40 +01003928 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN))
3929 flags |= ST_SHOWADMIN;
Willy Tarreau501f6022016-01-08 17:37:22 +01003930
3931 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
Willy Tarreau60600742016-01-11 15:32:30 +01003932 return stats_dump_fields_html(&trash, stats, flags);
Willy Tarreau1e62df92016-01-11 18:57:53 +01003933 else if (appctx->ctx.stats.flags & STAT_FMT_TYPED)
3934 return stats_dump_fields_typed(&trash, stats);
Willy Tarreau501f6022016-01-08 17:37:22 +01003935 else
3936 return stats_dump_fields_csv(&trash, stats);
3937}
3938
Thierry Fournier23d2d642016-03-25 08:20:11 +01003939/* Fill <stats> with the frontend statistics. <stats> is
3940 * preallocated array of length <len>. The length of the array
3941 * must be at least ST_F_TOTAL_FIELDS. If this length is less then
3942 * this value, the function returns 0, otherwise, it returns 1.
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003943 */
Thierry Fournier23d2d642016-03-25 08:20:11 +01003944int stats_fill_fe_stats(struct proxy *px, struct field *stats, int len)
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003945{
Thierry Fournier23d2d642016-03-25 08:20:11 +01003946 if (len < ST_F_TOTAL_FIELDS)
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003947 return 0;
3948
Thierry Fournier23d2d642016-03-25 08:20:11 +01003949 memset(stats, 0, sizeof(*stats) * len);
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003950
3951 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
3952 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, "FRONTEND");
Willy Tarreauf8211df2016-01-11 14:09:38 +01003953 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003954 stats[ST_F_SCUR] = mkf_u32(0, px->feconn);
3955 stats[ST_F_SMAX] = mkf_u32(FN_MAX, px->fe_counters.conn_max);
3956 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, px->maxconn);
3957 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, px->fe_counters.cum_sess);
3958 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, px->fe_counters.bytes_in);
3959 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, px->fe_counters.bytes_out);
3960 stats[ST_F_DREQ] = mkf_u64(FN_COUNTER, px->fe_counters.denied_req);
3961 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, px->fe_counters.denied_resp);
3962 stats[ST_F_EREQ] = mkf_u64(FN_COUNTER, px->fe_counters.failed_req);
3963 stats[ST_F_STATUS] = mkf_str(FO_STATUS, px->state == PR_STREADY ? "OPEN" : px->state == PR_STFULL ? "FULL" : "STOP");
3964 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
3965 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
3966 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, 0);
3967 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_FE);
3968 stats[ST_F_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_sess_per_sec));
3969 stats[ST_F_RATE_LIM] = mkf_u32(FO_CONFIG|FN_LIMIT, px->fe_sps_lim);
3970 stats[ST_F_RATE_MAX] = mkf_u32(FN_MAX, px->fe_counters.sps_max);
3971
3972 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
3973 if (px->mode == PR_MODE_HTTP) {
3974 stats[ST_F_HRSP_1XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[1]);
3975 stats[ST_F_HRSP_2XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[2]);
3976 stats[ST_F_HRSP_3XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[3]);
3977 stats[ST_F_HRSP_4XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[4]);
3978 stats[ST_F_HRSP_5XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[5]);
3979 stats[ST_F_HRSP_OTHER] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[0]);
Willy Tarreau5b9bdff2016-01-11 14:40:47 +01003980 stats[ST_F_INTERCEPTED] = mkf_u64(FN_COUNTER, px->fe_counters.intercepted_req);
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003981 }
3982
3983 /* requests : req_rate, req_rate_max, req_tot, */
3984 stats[ST_F_REQ_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_req_per_sec));
3985 stats[ST_F_REQ_RATE_MAX] = mkf_u32(FN_MAX, px->fe_counters.p.http.rps_max);
3986 stats[ST_F_REQ_TOT] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cum_req);
3987
3988 /* compression: in, out, bypassed, responses */
3989 stats[ST_F_COMP_IN] = mkf_u64(FN_COUNTER, px->fe_counters.comp_in);
3990 stats[ST_F_COMP_OUT] = mkf_u64(FN_COUNTER, px->fe_counters.comp_out);
3991 stats[ST_F_COMP_BYP] = mkf_u64(FN_COUNTER, px->fe_counters.comp_byp);
3992 stats[ST_F_COMP_RSP] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.comp_rsp);
3993
Willy Tarreauc73810f2016-01-11 13:52:04 +01003994 /* connections : conn_rate, conn_rate_max, conn_tot, conn_max */
3995 stats[ST_F_CONN_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_conn_per_sec));
3996 stats[ST_F_CONN_RATE_MAX] = mkf_u32(FN_MAX, px->fe_counters.cps_max);
3997 stats[ST_F_CONN_TOT] = mkf_u64(FN_COUNTER, px->fe_counters.cum_conn);
3998
Thierry Fournier23d2d642016-03-25 08:20:11 +01003999 return 1;
4000}
4001
4002/* Dumps a frontend's line to the trash for the current proxy <px> and uses
4003 * the state from stream interface <si>. The caller is responsible for clearing
4004 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
4005 */
4006static int stats_dump_fe_stats(struct stream_interface *si, struct proxy *px)
4007{
4008 struct appctx *appctx = __objt_appctx(si->end);
4009
4010 if (!(px->cap & PR_CAP_FE))
4011 return 0;
4012
4013 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_FE)))
4014 return 0;
4015
4016 if (!stats_fill_fe_stats(px, stats, ST_F_TOTAL_FIELDS))
4017 return 0;
4018
Willy Tarreau501f6022016-01-08 17:37:22 +01004019 return stats_dump_one_line(stats, 0, px, appctx);
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004020}
4021
Thierry Fournierc4456852016-03-25 08:20:49 +01004022/* Fill <stats> with the listener statistics. <stats> is
4023 * preallocated array of length <len>. The length of the array
4024 * must be at least ST_F_TOTAL_FIELDS. If this length is less
4025 * then this value, the function returns 0, otherwise, it
4026 * returns 1. <flags> can take the value ST_SHLGNDS.
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004027 */
Thierry Fournierc4456852016-03-25 08:20:49 +01004028int stats_fill_li_stats(struct proxy *px, struct listener *l, int flags,
4029 struct field *stats, int len)
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004030{
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004031 struct chunk *out = get_trash_chunk();
4032
Thierry Fournierc4456852016-03-25 08:20:49 +01004033 if (len < ST_F_TOTAL_FIELDS)
4034 return 0;
4035
Thierry Fournierac9d4672016-03-25 08:43:46 +01004036 if (!l->counters)
4037 return 0;
4038
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004039 chunk_reset(out);
Thierry Fournierc4456852016-03-25 08:20:49 +01004040 memset(stats, 0, sizeof(*stats) * len);
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004041
4042 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
4043 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, l->name);
Willy Tarreauf8211df2016-01-11 14:09:38 +01004044 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004045 stats[ST_F_SCUR] = mkf_u32(0, l->nbconn);
4046 stats[ST_F_SMAX] = mkf_u32(FN_MAX, l->counters->conn_max);
4047 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, l->maxconn);
4048 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, l->counters->cum_conn);
4049 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, l->counters->bytes_in);
4050 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, l->counters->bytes_out);
4051 stats[ST_F_DREQ] = mkf_u64(FN_COUNTER, l->counters->denied_req);
4052 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, l->counters->denied_resp);
4053 stats[ST_F_EREQ] = mkf_u64(FN_COUNTER, l->counters->failed_req);
4054 stats[ST_F_STATUS] = mkf_str(FO_STATUS, (l->nbconn < l->maxconn) ? (l->state == LI_LIMITED) ? "WAITING" : "OPEN" : "FULL");
4055 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
4056 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
4057 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, l->luid);
4058 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_SO);
4059
4060 if (flags & ST_SHLGNDS) {
4061 char str[INET6_ADDRSTRLEN];
4062 int port;
4063
4064 port = get_host_port(&l->addr);
4065 switch (addr_to_str(&l->addr, str, sizeof(str))) {
4066 case AF_INET:
4067 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
4068 chunk_appendf(out, "%s:%d", str, port);
4069 break;
4070 case AF_INET6:
4071 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
4072 chunk_appendf(out, "[%s]:%d", str, port);
4073 break;
4074 case AF_UNIX:
4075 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, "unix");
4076 break;
4077 case -1:
4078 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
4079 chunk_strcat(out, strerror(errno));
4080 break;
4081 default: /* address family not supported */
4082 break;
4083 }
4084 }
4085
Thierry Fournierc4456852016-03-25 08:20:49 +01004086 return 1;
4087}
4088
4089/* Dumps a line for listener <l> and proxy <px> to the trash and uses the state
4090 * from stream interface <si>, and stats flags <flags>. The caller is responsible
4091 * for clearing the trash if needed. Returns non-zero if it emits anything, zero
4092 * otherwise.
4093 */
4094static int stats_dump_li_stats(struct stream_interface *si, struct proxy *px, struct listener *l, int flags)
4095{
4096 struct appctx *appctx = __objt_appctx(si->end);
4097
4098 if (!stats_fill_li_stats(px, l, flags, stats, ST_F_TOTAL_FIELDS))
4099 return 0;
4100
Willy Tarreau501f6022016-01-08 17:37:22 +01004101 return stats_dump_one_line(stats, flags, px, appctx);
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004102}
4103
4104enum srv_stats_state {
4105 SRV_STATS_STATE_DOWN = 0,
4106 SRV_STATS_STATE_DOWN_AGENT,
4107 SRV_STATS_STATE_GOING_UP,
4108 SRV_STATS_STATE_UP_GOING_DOWN,
4109 SRV_STATS_STATE_UP,
4110 SRV_STATS_STATE_NOLB_GOING_DOWN,
Simon Horman4d2eab62015-04-23 14:51:26 +09004111 SRV_STATS_STATE_NOLB,
4112 SRV_STATS_STATE_DRAIN_GOING_DOWN,
4113 SRV_STATS_STATE_DRAIN,
Simon Hormanb167b6b2015-04-23 14:51:29 +09004114 SRV_STATS_STATE_DRAIN_AGENT,
Simon Horman4d2eab62015-04-23 14:51:26 +09004115 SRV_STATS_STATE_NO_CHECK,
4116
4117 SRV_STATS_STATE_COUNT, /* Must be last */
4118};
4119
Willy Tarreau2b96cf12016-01-06 19:25:38 +01004120static const char *srv_hlt_st[SRV_STATS_STATE_COUNT] = {
4121 [SRV_STATS_STATE_DOWN] = "DOWN",
4122 [SRV_STATS_STATE_DOWN_AGENT] = "DOWN (agent)",
4123 [SRV_STATS_STATE_GOING_UP] = "DOWN %d/%d",
4124 [SRV_STATS_STATE_UP_GOING_DOWN] = "UP %d/%d",
4125 [SRV_STATS_STATE_UP] = "UP",
4126 [SRV_STATS_STATE_NOLB_GOING_DOWN] = "NOLB %d/%d",
4127 [SRV_STATS_STATE_NOLB] = "NOLB",
4128 [SRV_STATS_STATE_DRAIN_GOING_DOWN] = "DRAIN %d/%d",
4129 [SRV_STATS_STATE_DRAIN] = "DRAIN",
4130 [SRV_STATS_STATE_DRAIN_AGENT] = "DRAIN (agent)",
4131 [SRV_STATS_STATE_NO_CHECK] = "no check"
4132};
4133
Thierry Fournier61fe6c02016-03-25 08:21:21 +01004134/* Fill <stats> with the server statistics. <stats> is
4135 * preallocated array of length <len>. The length of the array
4136 * must be at least ST_F_TOTAL_FIELDS. If this length is less
4137 * then this value, the function returns 0, otherwise, it
4138 * returns 1. <flags> can take the value ST_SHLGNDS.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004139 */
Thierry Fournier61fe6c02016-03-25 08:21:21 +01004140int stats_fill_sv_stats(struct proxy *px, struct server *sv, int flags,
4141 struct field *stats, int len)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004142{
Willy Tarreau32091232014-05-16 13:52:00 +02004143 struct server *via, *ref;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004144 char str[INET6_ADDRSTRLEN];
Willy Tarreau2b96cf12016-01-06 19:25:38 +01004145 struct chunk *out = get_trash_chunk();
Willy Tarreauba2f2642016-01-07 09:54:40 +01004146 enum srv_stats_state state;
Willy Tarreau2b96cf12016-01-06 19:25:38 +01004147 char *fld_status;
Thierry Fournier61fe6c02016-03-25 08:21:21 +01004148
4149 if (len < ST_F_TOTAL_FIELDS)
4150 return 0;
4151
4152 memset(stats, 0, sizeof(*stats) * len);
4153
Willy Tarreau32091232014-05-16 13:52:00 +02004154 /* we have "via" which is the tracked server as described in the configuration,
4155 * and "ref" which is the checked server and the end of the chain.
4156 */
4157 via = sv->track ? sv->track : sv;
4158 ref = via;
4159 while (ref->track)
4160 ref = ref->track;
4161
Willy Tarreauba2f2642016-01-07 09:54:40 +01004162 if (sv->state == SRV_ST_RUNNING || sv->state == SRV_ST_STARTING) {
4163 if ((ref->check.state & CHK_ST_ENABLED) &&
4164 (ref->check.health < ref->check.rise + ref->check.fall - 1)) {
4165 state = SRV_STATS_STATE_UP_GOING_DOWN;
Willy Tarreauba2f2642016-01-07 09:54:40 +01004166 } else {
4167 state = SRV_STATS_STATE_UP;
Willy Tarreauba2f2642016-01-07 09:54:40 +01004168 }
4169
Willy Tarreauba2f2642016-01-07 09:54:40 +01004170 if (sv->admin & SRV_ADMF_DRAIN) {
4171 if (ref->agent.state & CHK_ST_ENABLED)
4172 state = SRV_STATS_STATE_DRAIN_AGENT;
4173 else if (state == SRV_STATS_STATE_UP_GOING_DOWN)
4174 state = SRV_STATS_STATE_DRAIN_GOING_DOWN;
4175 else
4176 state = SRV_STATS_STATE_DRAIN;
4177 }
4178
4179 if (state == SRV_STATS_STATE_UP && !(ref->check.state & CHK_ST_ENABLED)) {
4180 state = SRV_STATS_STATE_NO_CHECK;
Willy Tarreauba2f2642016-01-07 09:54:40 +01004181 }
4182 }
4183 else if (sv->state == SRV_ST_STOPPING) {
4184 if ((!(sv->check.state & CHK_ST_ENABLED) && !sv->track) ||
4185 (ref->check.health == ref->check.rise + ref->check.fall - 1)) {
4186 state = SRV_STATS_STATE_NOLB;
Willy Tarreauba2f2642016-01-07 09:54:40 +01004187 } else {
4188 state = SRV_STATS_STATE_NOLB_GOING_DOWN;
Willy Tarreauba2f2642016-01-07 09:54:40 +01004189 }
4190 }
4191 else { /* stopped */
4192 if ((ref->agent.state & CHK_ST_ENABLED) && !ref->agent.health) {
4193 state = SRV_STATS_STATE_DOWN_AGENT;
Willy Tarreauba2f2642016-01-07 09:54:40 +01004194 } else if ((ref->check.state & CHK_ST_ENABLED) && !ref->check.health) {
4195 state = SRV_STATS_STATE_DOWN; /* DOWN */
Willy Tarreauba2f2642016-01-07 09:54:40 +01004196 } else if ((ref->agent.state & CHK_ST_ENABLED) || (ref->check.state & CHK_ST_ENABLED)) {
4197 state = SRV_STATS_STATE_GOING_UP;
Willy Tarreauba2f2642016-01-07 09:54:40 +01004198 } else {
4199 state = SRV_STATS_STATE_DOWN; /* DOWN, unchecked */
Willy Tarreauba2f2642016-01-07 09:54:40 +01004200 }
4201 }
4202
Willy Tarreau2b96cf12016-01-06 19:25:38 +01004203 chunk_reset(out);
Willy Tarreau2b96cf12016-01-06 19:25:38 +01004204
4205 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
4206 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, sv->id);
Willy Tarreauf8211df2016-01-11 14:09:38 +01004207 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
Willy Tarreau2b96cf12016-01-06 19:25:38 +01004208 stats[ST_F_QCUR] = mkf_u32(0, sv->nbpend);
4209 stats[ST_F_QMAX] = mkf_u32(FN_MAX, sv->counters.nbpend_max);
4210 stats[ST_F_SCUR] = mkf_u32(0, sv->cur_sess);
4211 stats[ST_F_SMAX] = mkf_u32(FN_MAX, sv->counters.cur_sess_max);
4212
4213 if (sv->maxconn)
4214 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, sv->maxconn);
4215
4216 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, sv->counters.cum_sess);
4217 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, sv->counters.bytes_in);
4218 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, sv->counters.bytes_out);
4219 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, sv->counters.failed_secu);
4220 stats[ST_F_ECON] = mkf_u64(FN_COUNTER, sv->counters.failed_conns);
4221 stats[ST_F_ERESP] = mkf_u64(FN_COUNTER, sv->counters.failed_resp);
4222 stats[ST_F_WRETR] = mkf_u64(FN_COUNTER, sv->counters.retries);
4223 stats[ST_F_WREDIS] = mkf_u64(FN_COUNTER, sv->counters.redispatches);
4224
4225 /* status */
4226 fld_status = chunk_newstr(out);
4227 if (sv->admin & SRV_ADMF_IMAINT)
4228 chunk_appendf(out, "MAINT (via %s/%s)", via->proxy->id, via->id);
4229 else if (sv->admin & SRV_ADMF_MAINT)
4230 chunk_appendf(out, "MAINT");
4231 else
4232 chunk_appendf(out,
4233 srv_hlt_st[state],
4234 (ref->state != SRV_ST_STOPPED) ? (ref->check.health - ref->check.rise + 1) : (ref->check.health),
4235 (ref->state != SRV_ST_STOPPED) ? (ref->check.fall) : (ref->check.rise));
4236
4237 stats[ST_F_STATUS] = mkf_str(FO_STATUS, fld_status);
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004238 stats[ST_F_LASTCHG] = mkf_u32(FN_AGE, now.tv_sec - sv->last_change);
4239 stats[ST_F_WEIGHT] = mkf_u32(FN_AVG, (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv);
4240 stats[ST_F_ACT] = mkf_u32(FO_STATUS, (sv->flags & SRV_F_BACKUP) ? 0 : 1);
4241 stats[ST_F_BCK] = mkf_u32(FO_STATUS, (sv->flags & SRV_F_BACKUP) ? 1 : 0);
Willy Tarreau91861262007-10-17 17:06:05 +02004242
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004243 /* check failures: unique, fatal; last change, total downtime */
4244 if (sv->check.state & CHK_ST_ENABLED) {
4245 stats[ST_F_CHKFAIL] = mkf_u64(FN_COUNTER, sv->counters.failed_checks);
4246 stats[ST_F_CHKDOWN] = mkf_u64(FN_COUNTER, sv->counters.down_trans);
4247 stats[ST_F_DOWNTIME] = mkf_u32(FN_COUNTER, srv_downtime(sv));
4248 }
Willy Tarreau164d4a92016-01-06 19:48:21 +01004249
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004250 if (sv->maxqueue)
4251 stats[ST_F_QLIMIT] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->maxqueue);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004252
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004253 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
4254 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
4255 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, sv->puid);
Willy Tarreau91861262007-10-17 17:06:05 +02004256
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004257 if (sv->state == SRV_ST_STARTING && !server_is_draining(sv))
4258 stats[ST_F_THROTTLE] = mkf_u32(FN_AVG, server_throttle_rate(sv));
Willy Tarreauf5b1cc32014-06-17 12:20:59 +02004259
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004260 stats[ST_F_LBTOT] = mkf_u64(FN_COUNTER, sv->counters.cum_lbconn);
Willy Tarreau91861262007-10-17 17:06:05 +02004261
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004262 if (sv->track) {
4263 char *fld_track = chunk_newstr(out);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004264
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004265 chunk_appendf(out, "%s/%s", sv->track->proxy->id, sv->track->id);
4266 stats[ST_F_TRACKED] = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, fld_track);
4267 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004268
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004269 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_SV);
4270 stats[ST_F_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&sv->sess_per_sec));
4271 stats[ST_F_RATE_MAX] = mkf_u32(FN_MAX, sv->counters.sps_max);
Willy Tarreau164d4a92016-01-06 19:48:21 +01004272
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004273 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) {
4274 const char *fld_chksts;
Willy Tarreau164d4a92016-01-06 19:48:21 +01004275
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004276 fld_chksts = chunk_newstr(out);
4277 chunk_strcat(out, "* "); // for check in progress
4278 chunk_strcat(out, get_check_status_info(sv->check.status));
4279 if (!(sv->check.state & CHK_ST_INPROGRESS))
4280 fld_chksts += 2; // skip "* "
4281 stats[ST_F_CHECK_STATUS] = mkf_str(FN_OUTPUT, fld_chksts);
Willy Tarreau91861262007-10-17 17:06:05 +02004282
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004283 if (sv->check.status >= HCHK_STATUS_L57DATA)
4284 stats[ST_F_CHECK_CODE] = mkf_u32(FN_OUTPUT, sv->check.code);
Willy Tarreaucf2924b2014-05-23 12:15:15 +02004285
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004286 if (sv->check.status >= HCHK_STATUS_CHECKED)
4287 stats[ST_F_CHECK_DURATION] = mkf_u64(FN_DURATION, sv->check.duration);
Willy Tarreaucf2924b2014-05-23 12:15:15 +02004288
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004289 stats[ST_F_CHECK_DESC] = mkf_str(FN_OUTPUT, get_check_status_description(sv->check.status));
4290 stats[ST_F_LAST_CHK] = mkf_str(FN_OUTPUT, sv->check.desc);
4291 stats[ST_F_CHECK_RISE] = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.rise);
4292 stats[ST_F_CHECK_FALL] = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.fall);
4293 stats[ST_F_CHECK_HEALTH] = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.health);
4294 }
Willy Tarreaucf2924b2014-05-23 12:15:15 +02004295
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004296 if ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) {
4297 const char *fld_chksts;
Willy Tarreaudd7354b2016-01-08 13:47:26 +01004298
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004299 fld_chksts = chunk_newstr(out);
4300 chunk_strcat(out, "* "); // for check in progress
4301 chunk_strcat(out, get_check_status_info(sv->agent.status));
4302 if (!(sv->agent.state & CHK_ST_INPROGRESS))
4303 fld_chksts += 2; // skip "* "
4304 stats[ST_F_AGENT_STATUS] = mkf_str(FN_OUTPUT, fld_chksts);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004305
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004306 if (sv->agent.status >= HCHK_STATUS_L57DATA)
4307 stats[ST_F_AGENT_CODE] = mkf_u32(FN_OUTPUT, sv->agent.code);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004308
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004309 if (sv->agent.status >= HCHK_STATUS_CHECKED)
4310 stats[ST_F_AGENT_DURATION] = mkf_u64(FN_DURATION, sv->agent.duration);
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01004311
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004312 stats[ST_F_AGENT_DESC] = mkf_str(FN_OUTPUT, get_check_status_description(sv->agent.status));
4313 stats[ST_F_LAST_AGT] = mkf_str(FN_OUTPUT, sv->agent.desc);
4314 stats[ST_F_AGENT_RISE] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.rise);
4315 stats[ST_F_AGENT_FALL] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.fall);
4316 stats[ST_F_AGENT_HEALTH] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.health);
4317 }
Willy Tarreaudd7354b2016-01-08 13:47:26 +01004318
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004319 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
4320 if (px->mode == PR_MODE_HTTP) {
4321 stats[ST_F_HRSP_1XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[1]);
4322 stats[ST_F_HRSP_2XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[2]);
4323 stats[ST_F_HRSP_3XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[3]);
4324 stats[ST_F_HRSP_4XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[4]);
4325 stats[ST_F_HRSP_5XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[5]);
4326 stats[ST_F_HRSP_OTHER] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[0]);
4327 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004328
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004329 if (ref->observe)
4330 stats[ST_F_HANAFAIL] = mkf_u64(FN_COUNTER, sv->counters.failed_hana);
Willy Tarreau164d4a92016-01-06 19:48:21 +01004331
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004332 stats[ST_F_CLI_ABRT] = mkf_u64(FN_COUNTER, sv->counters.cli_aborts);
4333 stats[ST_F_SRV_ABRT] = mkf_u64(FN_COUNTER, sv->counters.srv_aborts);
4334 stats[ST_F_LASTSESS] = mkf_s32(FN_AGE, srv_lastsession(sv));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004335
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004336 stats[ST_F_QTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.q_time, TIME_STATS_SAMPLES));
4337 stats[ST_F_CTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.c_time, TIME_STATS_SAMPLES));
4338 stats[ST_F_RTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.d_time, TIME_STATS_SAMPLES));
4339 stats[ST_F_TTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.t_time, TIME_STATS_SAMPLES));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004340
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004341 if (flags & ST_SHLGNDS) {
4342 switch (addr_to_str(&sv->addr, str, sizeof(str))) {
4343 case AF_INET:
4344 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
4345 chunk_appendf(out, "%s:%d", str, get_host_port(&sv->addr));
4346 break;
4347 case AF_INET6:
4348 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
4349 chunk_appendf(out, "[%s]:%d", str, get_host_port(&sv->addr));
4350 break;
4351 case AF_UNIX:
4352 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, "unix");
4353 break;
4354 case -1:
4355 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
4356 chunk_strcat(out, strerror(errno));
4357 break;
4358 default: /* address family not supported */
4359 break;
Willy Tarreauf4659942013-11-28 10:50:06 +01004360 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004361
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004362 if (sv->cookie)
4363 stats[ST_F_COOKIE] = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, sv->cookie);
4364 }
4365
Thierry Fournier61fe6c02016-03-25 08:21:21 +01004366 return 1;
4367}
4368
4369/* Dumps a line for server <sv> and proxy <px> to the trash and uses the state
4370 * from stream interface <si>, stats flags <flags>, and server state <state>.
4371 * The caller is responsible for clearing the trash if needed. Returns non-zero
4372 * if it emits anything, zero otherwise.
4373 */
4374static int stats_dump_sv_stats(struct stream_interface *si, struct proxy *px, int flags, struct server *sv)
4375{
4376 struct appctx *appctx = __objt_appctx(si->end);
4377
4378 if (!stats_fill_sv_stats(px, sv, flags, stats, ST_F_TOTAL_FIELDS))
4379 return 0;
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
Thierry Fournierd0a56c22016-03-25 08:21:51 +01004384/* Fill <stats> with the backend statistics. <stats> is
4385 * preallocated array of length <len>. The length of the array
4386 * must be at least ST_F_TOTAL_FIELDS. If this length is less
4387 * then this value, the function returns 0, otherwise, it
4388 * returns 1. <flags> can take the value ST_SHLGNDS.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004389 */
Thierry Fournierd0a56c22016-03-25 08:21:51 +01004390int stats_fill_be_stats(struct proxy *px, int flags, struct field *stats, int len)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004391{
Thierry Fournierd0a56c22016-03-25 08:21:51 +01004392 if (len < ST_F_TOTAL_FIELDS)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004393 return 0;
4394
Thierry Fournierd0a56c22016-03-25 08:21:51 +01004395 memset(stats, 0, sizeof(*stats) * len);
Willy Tarreauf6eecbe2016-01-06 16:14:50 +01004396
4397 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
4398 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, "BACKEND");
Willy Tarreauf8211df2016-01-11 14:09:38 +01004399 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
Willy Tarreauf6eecbe2016-01-06 16:14:50 +01004400 stats[ST_F_QCUR] = mkf_u32(0, px->nbpend);
4401 stats[ST_F_QMAX] = mkf_u32(FN_MAX, px->be_counters.nbpend_max);
4402 stats[ST_F_SCUR] = mkf_u32(FO_CONFIG|FN_LIMIT, px->beconn);
4403 stats[ST_F_SMAX] = mkf_u32(FN_MAX, px->be_counters.conn_max);
4404 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, px->fullconn);
4405 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, px->be_counters.cum_conn);
4406 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, px->be_counters.bytes_in);
4407 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, px->be_counters.bytes_out);
4408 stats[ST_F_DREQ] = mkf_u64(FN_COUNTER, px->be_counters.denied_req);
4409 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, px->be_counters.denied_resp);
4410 stats[ST_F_ECON] = mkf_u64(FN_COUNTER, px->be_counters.failed_conns);
4411 stats[ST_F_ERESP] = mkf_u64(FN_COUNTER, px->be_counters.failed_resp);
4412 stats[ST_F_WRETR] = mkf_u64(FN_COUNTER, px->be_counters.retries);
4413 stats[ST_F_WREDIS] = mkf_u64(FN_COUNTER, px->be_counters.redispatches);
4414 stats[ST_F_STATUS] = mkf_str(FO_STATUS, (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" : "DOWN");
4415 stats[ST_F_WEIGHT] = mkf_u32(FN_AVG, (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv);
4416 stats[ST_F_ACT] = mkf_u32(0, px->srv_act);
4417 stats[ST_F_BCK] = mkf_u32(0, px->srv_bck);
4418 stats[ST_F_CHKDOWN] = mkf_u64(FN_COUNTER, px->down_trans);
4419 stats[ST_F_LASTCHG] = mkf_u32(FN_AGE, now.tv_sec - px->last_change);
Willy Tarreau7344f472016-01-11 12:04:02 +01004420 if (px->srv)
4421 stats[ST_F_DOWNTIME] = mkf_u32(FN_COUNTER, be_downtime(px));
4422
Willy Tarreauf6eecbe2016-01-06 16:14:50 +01004423 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
4424 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
4425 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, 0);
4426 stats[ST_F_LBTOT] = mkf_u64(FN_COUNTER, px->be_counters.cum_lbconn);
4427 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_BE);
4428 stats[ST_F_RATE] = mkf_u32(0, read_freq_ctr(&px->be_sess_per_sec));
4429 stats[ST_F_RATE_MAX] = mkf_u32(0, px->be_counters.sps_max);
Willy Tarreauf1516d92016-01-11 14:48:36 +01004430
Willy Tarreaue4847c62016-01-08 15:43:54 +01004431 if (flags & ST_SHLGNDS) {
4432 if (px->cookie_name)
4433 stats[ST_F_COOKIE] = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, px->cookie_name);
Willy Tarreauf1516d92016-01-11 14:48:36 +01004434 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 +01004435 }
Willy Tarreauf6eecbe2016-01-06 16:14:50 +01004436
4437 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
4438 if (px->mode == PR_MODE_HTTP) {
4439 stats[ST_F_REQ_TOT] = mkf_u64(FN_COUNTER, px->be_counters.p.http.cum_req);
4440 stats[ST_F_HRSP_1XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[1]);
4441 stats[ST_F_HRSP_2XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[2]);
4442 stats[ST_F_HRSP_3XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[3]);
4443 stats[ST_F_HRSP_4XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[4]);
4444 stats[ST_F_HRSP_5XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[5]);
4445 stats[ST_F_HRSP_OTHER] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[0]);
Willy Tarreau5b9bdff2016-01-11 14:40:47 +01004446 stats[ST_F_INTERCEPTED] = mkf_u64(FN_COUNTER, px->be_counters.intercepted_req);
Willy Tarreauf6eecbe2016-01-06 16:14:50 +01004447 }
4448
4449 stats[ST_F_CLI_ABRT] = mkf_u64(FN_COUNTER, px->be_counters.cli_aborts);
4450 stats[ST_F_SRV_ABRT] = mkf_u64(FN_COUNTER, px->be_counters.srv_aborts);
4451
4452 /* compression: in, out, bypassed, responses */
4453 stats[ST_F_COMP_IN] = mkf_u64(FN_COUNTER, px->be_counters.comp_in);
4454 stats[ST_F_COMP_OUT] = mkf_u64(FN_COUNTER, px->be_counters.comp_out);
4455 stats[ST_F_COMP_BYP] = mkf_u64(FN_COUNTER, px->be_counters.comp_byp);
4456 stats[ST_F_COMP_RSP] = mkf_u64(FN_COUNTER, px->be_counters.p.http.comp_rsp);
4457 stats[ST_F_LASTSESS] = mkf_s32(FN_AGE, be_lastsession(px));
4458
4459 stats[ST_F_QTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.q_time, TIME_STATS_SAMPLES));
4460 stats[ST_F_CTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.c_time, TIME_STATS_SAMPLES));
4461 stats[ST_F_RTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.d_time, TIME_STATS_SAMPLES));
4462 stats[ST_F_TTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.t_time, TIME_STATS_SAMPLES));
4463
Thierry Fournierd0a56c22016-03-25 08:21:51 +01004464 return 1;
4465}
4466
4467/* Dumps a line for backend <px> to the trash for and uses the state from stream
4468 * interface <si> and stats flags <flags>. The caller is responsible for clearing
4469 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
4470 */
4471static int stats_dump_be_stats(struct stream_interface *si, struct proxy *px, int flags)
4472{
4473 struct appctx *appctx = __objt_appctx(si->end);
4474
4475 if (!(px->cap & PR_CAP_BE))
4476 return 0;
4477
4478 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_BE)))
4479 return 0;
4480
4481 if (!stats_fill_be_stats(px, flags, stats, ST_F_TOTAL_FIELDS))
4482 return 0;
4483
Willy Tarreau501f6022016-01-08 17:37:22 +01004484 return stats_dump_one_line(stats, flags, px, appctx);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004485}
4486
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004487/* Dumps the HTML table header for proxy <px> to the trash for and uses the state from
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004488 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004489 * for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004490 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004491static void stats_dump_html_px_hdr(struct stream_interface *si, struct proxy *px, struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004492{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004493 struct appctx *appctx = __objt_appctx(si->end);
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004494 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
4495
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004496 if (px->cap & PR_CAP_BE && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004497 /* A form to enable/disable this proxy servers */
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004498
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004499 /* 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 +02004500 scope_txt[0] = 0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004501 if (appctx->ctx.stats.scope_len) {
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004502 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau4e4292b2014-11-28 12:18:45 +01004503 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 +01004504 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004505 }
4506
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004507 chunk_appendf(&trash,
Jeff Buchbinder2dbbf4d2014-08-29 15:10:08 -05004508 "<form method=\"post\">");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004509 }
4510
4511 /* print a new table */
4512 chunk_appendf(&trash,
4513 "<table class=\"tbl\" width=\"100%%\">\n"
4514 "<tr class=\"titre\">"
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01004515 "<th class=\"pxname\" width=\"10%%\">");
4516
4517 chunk_appendf(&trash,
4518 "<a name=\"%s\"></a>%s"
4519 "<a class=px href=\"#%s\">%s</a>",
4520 px->id,
4521 (uri->flags & ST_SHLGNDS) ? "<u>":"",
4522 px->id, px->id);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004523
4524 if (uri->flags & ST_SHLGNDS) {
4525 /* cap, mode, id */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02004526 chunk_appendf(&trash, "<div class=tips>cap: %s, mode: %s, id: %d",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004527 proxy_cap_str(px->cap), proxy_mode_str(px->mode),
4528 px->uuid);
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01004529 chunk_appendf(&trash, "</div>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004530 }
4531
4532 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01004533 "%s</th>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004534 "<th class=\"%s\" width=\"90%%\">%s</th>"
4535 "</tr>\n"
4536 "</table>\n"
4537 "<table class=\"tbl\" width=\"100%%\">\n"
4538 "<tr class=\"titre\">",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004539 (uri->flags & ST_SHLGNDS) ? "</u>":"",
4540 px->desc ? "desc" : "empty", px->desc ? px->desc : "");
4541
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004542 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004543 /* Column heading for Enable or Disable server */
4544 chunk_appendf(&trash, "<th rowspan=2 width=1></th>");
Willy Tarreau91861262007-10-17 17:06:05 +02004545 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004546
4547 chunk_appendf(&trash,
4548 "<th rowspan=2></th>"
4549 "<th colspan=3>Queue</th>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05004550 "<th colspan=3>Session rate</th><th colspan=6>Sessions</th>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004551 "<th colspan=2>Bytes</th><th colspan=2>Denied</th>"
4552 "<th colspan=3>Errors</th><th colspan=2>Warnings</th>"
4553 "<th colspan=9>Server</th>"
4554 "</tr>\n"
4555 "<tr class=\"titre\">"
4556 "<th>Cur</th><th>Max</th><th>Limit</th>"
4557 "<th>Cur</th><th>Max</th><th>Limit</th><th>Cur</th><th>Max</th>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05004558 "<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 +01004559 "<th>Req</th><th>Resp</th><th>Req</th><th>Conn</th>"
4560 "<th>Resp</th><th>Retr</th><th>Redis</th>"
4561 "<th>Status</th><th>LastChk</th><th>Wght</th><th>Act</th>"
4562 "<th>Bck</th><th>Chk</th><th>Dwn</th><th>Dwntme</th>"
4563 "<th>Thrtle</th>\n"
4564 "</tr>");
Willy Tarreau91861262007-10-17 17:06:05 +02004565}
4566
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004567/* Dumps the HTML table trailer for proxy <px> to the trash for and uses the state from
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004568 * stream interface <si>. The caller is responsible for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004569 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004570static void stats_dump_html_px_end(struct stream_interface *si, struct proxy *px)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004571{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004572 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004573 chunk_appendf(&trash, "</table>");
4574
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004575 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004576 /* close the form used to enable/disable this proxy servers */
4577 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004578 "Choose the action to perform on the checked servers : "
4579 "<select name=action>"
4580 "<option value=\"\"></option>"
Willy Tarreaued7df902014-05-22 18:04:49 +02004581 "<option value=\"ready\">Set state to READY</option>"
4582 "<option value=\"drain\">Set state to DRAIN</option>"
Marco Corte8c27bca2014-07-02 17:49:34 +02004583 "<option value=\"maint\">Set state to MAINT</option>"
Willy Tarreau248a60e2014-05-23 14:59:48 +02004584 "<option value=\"dhlth\">Health: disable checks</option>"
4585 "<option value=\"ehlth\">Health: enable checks</option>"
4586 "<option value=\"hrunn\">Health: force UP</option>"
4587 "<option value=\"hnolb\">Health: force NOLB</option>"
4588 "<option value=\"hdown\">Health: force DOWN</option>"
4589 "<option value=\"dagent\">Agent: disable checks</option>"
4590 "<option value=\"eagent\">Agent: enable checks</option>"
4591 "<option value=\"arunn\">Agent: force UP</option>"
4592 "<option value=\"adown\">Agent: force DOWN</option>"
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004593 "<option value=\"shutdown\">Kill Sessions</option>"
4594 "</select>"
4595 "<input type=\"hidden\" name=\"b\" value=\"#%d\">"
4596 "&nbsp;<input type=\"submit\" value=\"Apply\">"
4597 "</form>",
4598 px->uuid);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004599 }
4600
4601 chunk_appendf(&trash, "<p>\n");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004602}
Willy Tarreau91861262007-10-17 17:06:05 +02004603
4604/*
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004605 * Dumps statistics for a proxy. The output is sent to the stream interface's
4606 * input buffer. Returns 0 if it had to stop dumping data because of lack of
4607 * buffer space, or non-zero if everything completed. This function is used
4608 * both by the CLI and the HTTP entry points, and is able to dump the output
4609 * in HTML or CSV formats. If the later, <uri> must be NULL.
Willy Tarreau91861262007-10-17 17:06:05 +02004610 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004611static int stats_dump_proxy_to_buffer(struct stream_interface *si, struct proxy *px, struct uri_auth *uri)
Willy Tarreau91861262007-10-17 17:06:05 +02004612{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004613 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau87b09662015-04-03 00:22:06 +02004614 struct stream *s = si_strm(si);
Willy Tarreau2bb4a962014-11-28 11:11:05 +01004615 struct channel *rep = si_ic(si);
Willy Tarreau44267702011-10-28 15:35:33 +02004616 struct server *sv, *svs; /* server and server-state, server-state=server or server->track */
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004617 struct listener *l;
Willy Tarreau0deb85a2016-01-08 15:33:18 +01004618 unsigned int flags;
4619
4620 if (uri)
4621 flags = uri->flags;
4622 else if (strm_li(s)->bind_conf->level >= ACCESS_LVL_OPER)
4623 flags = ST_SHLGNDS | ST_SHNODE | ST_SHDESC;
4624 else
4625 flags = ST_SHNODE | ST_SHDESC;
Willy Tarreau91861262007-10-17 17:06:05 +02004626
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004627 chunk_reset(&trash);
Willy Tarreau91861262007-10-17 17:06:05 +02004628
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004629 switch (appctx->ctx.stats.px_st) {
Willy Tarreau295a8372011-03-10 11:25:07 +01004630 case STAT_PX_ST_INIT:
Willy Tarreau91861262007-10-17 17:06:05 +02004631 /* we are on a new proxy */
Willy Tarreau91861262007-10-17 17:06:05 +02004632 if (uri && uri->scope) {
4633 /* we have a limited scope, we have to check the proxy name */
4634 struct stat_scope *scope;
4635 int len;
4636
4637 len = strlen(px->id);
4638 scope = uri->scope;
4639
4640 while (scope) {
4641 /* match exact proxy name */
4642 if (scope->px_len == len && !memcmp(px->id, scope->px_id, len))
4643 break;
4644
4645 /* match '.' which means 'self' proxy */
Willy Tarreau1388a3a2007-10-18 16:38:37 +02004646 if (!strcmp(scope->px_id, ".") && px == s->be)
Willy Tarreau91861262007-10-17 17:06:05 +02004647 break;
4648 scope = scope->next;
4649 }
4650
4651 /* proxy name not found : don't dump anything */
4652 if (scope == NULL)
4653 return 1;
4654 }
4655
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004656 /* if the user has requested a limited output and the proxy
4657 * name does not match, skip it.
4658 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004659 if (appctx->ctx.stats.scope_len &&
Willy Tarreau4e4292b2014-11-28 12:18:45 +01004660 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 +02004661 return 1;
4662
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004663 if ((appctx->ctx.stats.flags & STAT_BOUND) &&
4664 (appctx->ctx.stats.iid != -1) &&
4665 (px->uuid != appctx->ctx.stats.iid))
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01004666 return 1;
4667
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004668 appctx->ctx.stats.px_st = STAT_PX_ST_TH;
Willy Tarreau91861262007-10-17 17:06:05 +02004669 /* fall through */
4670
Willy Tarreau295a8372011-03-10 11:25:07 +01004671 case STAT_PX_ST_TH:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004672 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004673 stats_dump_html_px_hdr(si, px, uri);
Willy Tarreaubc18da12015-03-13 14:00:47 +01004674 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004675 si_applet_cant_put(si);
Willy Tarreau55bb8452007-10-17 18:44:57 +02004676 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01004677 }
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004678 }
Willy Tarreau91861262007-10-17 17:06:05 +02004679
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004680 appctx->ctx.stats.px_st = STAT_PX_ST_FE;
Willy Tarreau91861262007-10-17 17:06:05 +02004681 /* fall through */
4682
Willy Tarreau295a8372011-03-10 11:25:07 +01004683 case STAT_PX_ST_FE:
Willy Tarreau91861262007-10-17 17:06:05 +02004684 /* print the frontend */
Willy Tarreaubc18da12015-03-13 14:00:47 +01004685 if (stats_dump_fe_stats(si, px)) {
4686 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004687 si_applet_cant_put(si);
Willy Tarreau91861262007-10-17 17:06:05 +02004688 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01004689 }
4690 }
Willy Tarreau91861262007-10-17 17:06:05 +02004691
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004692 appctx->ctx.stats.l = px->conf.listeners.n;
4693 appctx->ctx.stats.px_st = STAT_PX_ST_LI;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004694 /* fall through */
4695
Willy Tarreau295a8372011-03-10 11:25:07 +01004696 case STAT_PX_ST_LI:
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004697 /* stats.l has been initialized above */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004698 for (; appctx->ctx.stats.l != &px->conf.listeners; appctx->ctx.stats.l = l->by_fe.n) {
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01004699 if (buffer_almost_full(rep->buf)) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004700 si_applet_cant_put(si);
Willy Tarreau4e33d862009-10-11 23:35:10 +02004701 return 0;
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01004702 }
Willy Tarreau4e33d862009-10-11 23:35:10 +02004703
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004704 l = LIST_ELEM(appctx->ctx.stats.l, struct listener *, by_fe);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004705 if (!l->counters)
4706 continue;
4707
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004708 if (appctx->ctx.stats.flags & STAT_BOUND) {
4709 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SO)))
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004710 break;
4711
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004712 if (appctx->ctx.stats.sid != -1 && l->luid != appctx->ctx.stats.sid)
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004713 continue;
4714 }
4715
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004716 /* print the frontend */
Willy Tarreau0deb85a2016-01-08 15:33:18 +01004717 if (stats_dump_li_stats(si, px, l, flags)) {
Willy Tarreaubc18da12015-03-13 14:00:47 +01004718 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004719 si_applet_cant_put(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004720 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01004721 }
4722 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004723 }
4724
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004725 appctx->ctx.stats.sv = px->srv; /* may be NULL */
4726 appctx->ctx.stats.px_st = STAT_PX_ST_SV;
Willy Tarreau91861262007-10-17 17:06:05 +02004727 /* fall through */
4728
Willy Tarreau295a8372011-03-10 11:25:07 +01004729 case STAT_PX_ST_SV:
Willy Tarreau91861262007-10-17 17:06:05 +02004730 /* stats.sv has been initialized above */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004731 for (; appctx->ctx.stats.sv != NULL; appctx->ctx.stats.sv = sv->next) {
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01004732 if (buffer_almost_full(rep->buf)) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004733 si_applet_cant_put(si);
Willy Tarreau4e33d862009-10-11 23:35:10 +02004734 return 0;
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01004735 }
Willy Tarreau4e33d862009-10-11 23:35:10 +02004736
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004737 sv = appctx->ctx.stats.sv;
Willy Tarreau91861262007-10-17 17:06:05 +02004738
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004739 if (appctx->ctx.stats.flags & STAT_BOUND) {
4740 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SV)))
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01004741 break;
4742
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004743 if (appctx->ctx.stats.sid != -1 && sv->puid != appctx->ctx.stats.sid)
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01004744 continue;
4745 }
4746
Willy Tarreau32091232014-05-16 13:52:00 +02004747 svs = sv;
4748 while (svs->track)
4749 svs = svs->track;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01004750
Willy Tarreauba2f2642016-01-07 09:54:40 +01004751 /* do not report servers which are DOWN and not changing state */
4752 if ((appctx->ctx.stats.flags & STAT_HIDE_DOWN) &&
4753 ((sv->admin & SRV_ADMF_MAINT) || /* server is in maintenance */
4754 (sv->state == SRV_ST_STOPPED && /* server is down */
4755 (!((svs->agent.state | svs->check.state) & CHK_ST_ENABLED) ||
4756 ((svs->agent.state & CHK_ST_ENABLED) && !svs->agent.health) ||
4757 ((svs->check.state & CHK_ST_ENABLED) && !svs->check.health))))) {
Willy Tarreau91861262007-10-17 17:06:05 +02004758 continue;
4759 }
4760
Willy Tarreau0deb85a2016-01-08 15:33:18 +01004761 if (stats_dump_sv_stats(si, px, flags, sv)) {
Willy Tarreaubc18da12015-03-13 14:00:47 +01004762 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004763 si_applet_cant_put(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004764 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01004765 }
4766 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004767 } /* for sv */
Cyril Bonté474be412010-10-12 00:14:36 +02004768
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004769 appctx->ctx.stats.px_st = STAT_PX_ST_BE;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004770 /* fall through */
Cyril Bonté70be45d2010-10-12 00:14:35 +02004771
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004772 case STAT_PX_ST_BE:
4773 /* print the backend */
Willy Tarreau0deb85a2016-01-08 15:33:18 +01004774 if (stats_dump_be_stats(si, px, flags)) {
Willy Tarreaubc18da12015-03-13 14:00:47 +01004775 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004776 si_applet_cant_put(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004777 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01004778 }
4779 }
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01004780
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004781 appctx->ctx.stats.px_st = STAT_PX_ST_END;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004782 /* fall through */
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01004783
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004784 case STAT_PX_ST_END:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004785 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004786 stats_dump_html_px_end(si, px);
Willy Tarreaubc18da12015-03-13 14:00:47 +01004787 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004788 si_applet_cant_put(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004789 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01004790 }
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004791 }
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01004792
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004793 appctx->ctx.stats.px_st = STAT_PX_ST_FIN;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004794 /* fall through */
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01004795
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004796 case STAT_PX_ST_FIN:
4797 return 1;
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01004798
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004799 default:
4800 /* unknown state, we should put an abort() here ! */
4801 return 1;
4802 }
4803}
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01004804
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004805/* Dumps the HTTP stats head block to the trash for and uses the per-uri
4806 * parameters <uri>. The caller is responsible for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004807 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004808static void stats_dump_html_head(struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004809{
4810 /* WARNING! This must fit in the first buffer !!! */
4811 chunk_appendf(&trash,
4812 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n"
4813 "\"http://www.w3.org/TR/html4/loose.dtd\">\n"
4814 "<html><head><title>Statistics Report for " PRODUCT_NAME "%s%s</title>\n"
4815 "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n"
4816 "<style type=\"text/css\"><!--\n"
4817 "body {"
4818 " font-family: arial, helvetica, sans-serif;"
4819 " font-size: 12px;"
4820 " font-weight: normal;"
4821 " color: black;"
4822 " background: white;"
4823 "}\n"
4824 "th,td {"
4825 " font-size: 10px;"
4826 "}\n"
4827 "h1 {"
4828 " font-size: x-large;"
4829 " margin-bottom: 0.5em;"
4830 "}\n"
4831 "h2 {"
4832 " font-family: helvetica, arial;"
4833 " font-size: x-large;"
4834 " font-weight: bold;"
4835 " font-style: italic;"
4836 " color: #6020a0;"
4837 " margin-top: 0em;"
4838 " margin-bottom: 0em;"
4839 "}\n"
4840 "h3 {"
4841 " font-family: helvetica, arial;"
4842 " font-size: 16px;"
4843 " font-weight: bold;"
4844 " color: #b00040;"
4845 " background: #e8e8d0;"
4846 " margin-top: 0em;"
4847 " margin-bottom: 0em;"
4848 "}\n"
4849 "li {"
4850 " margin-top: 0.25em;"
4851 " margin-right: 2em;"
4852 "}\n"
4853 ".hr {margin-top: 0.25em;"
4854 " border-color: black;"
4855 " border-bottom-style: solid;"
4856 "}\n"
4857 ".titre {background: #20D0D0;color: #000000; font-weight: bold; text-align: center;}\n"
4858 ".total {background: #20D0D0;color: #ffff80;}\n"
4859 ".frontend {background: #e8e8d0;}\n"
4860 ".socket {background: #d0d0d0;}\n"
4861 ".backend {background: #e8e8d0;}\n"
Simon Horman837bfa72015-04-23 14:51:27 +09004862 ".active_down {background: #ff9090;}\n"
4863 ".active_going_up {background: #ffd020;}\n"
4864 ".active_going_down {background: #ffffa0;}\n"
4865 ".active_up {background: #c0ffc0;}\n"
4866 ".active_nolb {background: #20a0ff;}\n"
4867 ".active_draining {background: #20a0FF;}\n"
4868 ".active_no_check {background: #e0e0e0;}\n"
4869 ".backup_down {background: #ff9090;}\n"
4870 ".backup_going_up {background: #ff80ff;}\n"
4871 ".backup_going_down {background: #c060ff;}\n"
4872 ".backup_up {background: #b0d0ff;}\n"
4873 ".backup_nolb {background: #90b0e0;}\n"
4874 ".backup_draining {background: #cc9900;}\n"
4875 ".backup_no_check {background: #e0e0e0;}\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004876 ".maintain {background: #c07820;}\n"
4877 ".rls {letter-spacing: 0.2em; margin-right: 1px;}\n" /* right letter spacing (used for grouping digits) */
4878 "\n"
4879 "a.px:link {color: #ffff40; text-decoration: none;}"
4880 "a.px:visited {color: #ffff40; text-decoration: none;}"
4881 "a.px:hover {color: #ffffff; text-decoration: none;}"
4882 "a.lfsb:link {color: #000000; text-decoration: none;}"
4883 "a.lfsb:visited {color: #000000; text-decoration: none;}"
4884 "a.lfsb:hover {color: #505050; text-decoration: none;}"
4885 "\n"
4886 "table.tbl { border-collapse: collapse; border-style: none;}\n"
4887 "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"
4888 "table.tbl td.ac { text-align: center;}\n"
4889 "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}\n"
4890 "table.tbl th.pxname { background: #b00040; color: #ffff40; font-weight: bold; border-style: solid solid none solid; padding: 2px 3px; white-space: nowrap;}\n"
4891 "table.tbl th.empty { border-style: none; empty-cells: hide; background: white;}\n"
4892 "table.tbl th.desc { background: white; border-style: solid solid none solid; text-align: left; padding: 2px 3px;}\n"
4893 "\n"
4894 "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n"
4895 "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n"
4896 "table.lgd td.noborder { border-style: none; padding: 2px; white-space: nowrap;}\n"
Willy Tarreau466c9b52012-12-23 02:25:03 +01004897 "table.det { border-collapse: collapse; border-style: none; }\n"
4898 "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 +01004899 "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 +01004900 "u {text-decoration:none; border-bottom: 1px dotted black;}\n"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02004901 "div.tips {\n"
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01004902 " display:block;\n"
4903 " visibility:hidden;\n"
4904 " z-index:2147483647;\n"
4905 " position:absolute;\n"
4906 " padding:2px 4px 3px;\n"
4907 " background:#f0f060; color:#000000;\n"
4908 " border:1px solid #7040c0;\n"
4909 " white-space:nowrap;\n"
4910 " font-style:normal;font-size:11px;font-weight:normal;\n"
4911 " -moz-border-radius:3px;-webkit-border-radius:3px;border-radius:3px;\n"
4912 " -moz-box-shadow:gray 2px 2px 3px;-webkit-box-shadow:gray 2px 2px 3px;box-shadow:gray 2px 2px 3px;\n"
4913 "}\n"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02004914 "u:hover div.tips {visibility:visible;}\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004915 "-->\n"
4916 "</style></head>\n",
4917 (uri->flags & ST_SHNODE) ? " on " : "",
4918 (uri->flags & ST_SHNODE) ? (uri->node ? uri->node : global.node) : ""
4919 );
4920}
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01004921
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004922/* Dumps the HTML stats information block to the trash for and uses the state from
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004923 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004924 * for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004925 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004926static void stats_dump_html_info(struct stream_interface *si, struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004927{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004928 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004929 unsigned int up = (now.tv_sec - start_date.tv_sec);
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004930 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01004931
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004932 /* WARNING! this has to fit the first packet too.
4933 * We are around 3.5 kB, add adding entries will
4934 * become tricky if we want to support 4kB buffers !
4935 */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004936 chunk_appendf(&trash,
4937 "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">"
4938 PRODUCT_NAME "%s</a></h1>\n"
4939 "<h2>Statistics Report for pid %d%s%s%s%s</h2>\n"
4940 "<hr width=\"100%%\" class=\"hr\">\n"
4941 "<h3>&gt; General process information</h3>\n"
4942 "<table border=0><tr><td align=\"left\" nowrap width=\"1%%\">\n"
4943 "<p><b>pid = </b> %d (process #%d, nbproc = %d)<br>\n"
4944 "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n"
4945 "<b>system limits:</b> memmax = %s%s; ulimit-n = %d<br>\n"
4946 "<b>maxsock = </b> %d; <b>maxconn = </b> %d; <b>maxpipes = </b> %d<br>\n"
4947 "current conns = %d; current pipes = %d/%d; conn rate = %d/sec<br>\n"
4948 "Running tasks: %d/%d; idle = %d %%<br>\n"
4949 "</td><td align=\"center\" nowrap>\n"
4950 "<table class=\"lgd\"><tr>\n"
Simon Horman837bfa72015-04-23 14:51:27 +09004951 "<td class=\"active_up\">&nbsp;</td><td class=\"noborder\">active UP </td>"
4952 "<td class=\"backup_up\">&nbsp;</td><td class=\"noborder\">backup UP </td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004953 "</tr><tr>\n"
Simon Horman837bfa72015-04-23 14:51:27 +09004954 "<td class=\"active_going_down\"></td><td class=\"noborder\">active UP, going down </td>"
4955 "<td class=\"backup_going_down\"></td><td class=\"noborder\">backup UP, going down </td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004956 "</tr><tr>\n"
Simon Horman837bfa72015-04-23 14:51:27 +09004957 "<td class=\"active_going_up\"></td><td class=\"noborder\">active DOWN, going up </td>"
4958 "<td class=\"backup_going_up\"></td><td class=\"noborder\">backup DOWN, going up </td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004959 "</tr><tr>\n"
Simon Horman837bfa72015-04-23 14:51:27 +09004960 "<td class=\"active_down\"></td><td class=\"noborder\">active or backup DOWN &nbsp;</td>"
4961 "<td class=\"active_no_check\"></td><td class=\"noborder\">not checked </td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004962 "</tr><tr>\n"
4963 "<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 -07004964 "</tr><tr>\n"
Simon Horman837bfa72015-04-23 14:51:27 +09004965 "<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 +01004966 "</tr></table>\n"
Willy Tarreau6b7764a2013-12-04 00:43:21 +01004967 "Note: \"NOLB\"/\"DRAIN\" = UP with load-balancing disabled."
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004968 "</td>"
4969 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
4970 "<b>Display option:</b><ul style=\"margin-top: 0.25em;\">"
4971 "",
4972 (uri->flags & ST_HIDEVER) ? "" : (STATS_VERSION_STRING),
4973 pid, (uri->flags & ST_SHNODE) ? " on " : "",
4974 (uri->flags & ST_SHNODE) ? (uri->node ? uri->node : global.node) : "",
4975 (uri->flags & ST_SHDESC) ? ": " : "",
4976 (uri->flags & ST_SHDESC) ? (uri->desc ? uri->desc : global.desc) : "",
4977 pid, relative_pid, global.nbproc,
4978 up / 86400, (up % 86400) / 3600,
4979 (up % 3600) / 60, (up % 60),
4980 global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited",
4981 global.rlimit_memmax ? " MB" : "",
4982 global.rlimit_nofile,
4983 global.maxsock, global.maxconn, global.maxpipes,
4984 actconn, pipes_used, pipes_used+pipes_free, read_freq_ctr(&global.conn_per_sec),
4985 run_queue_cur, nb_tasks_cur, idle_pct
4986 );
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004987
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004988 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Willy Tarreau4e4292b2014-11-28 12:18:45 +01004989 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 +01004990 scope_txt[appctx->ctx.stats.scope_len] = '\0';
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004991
4992 chunk_appendf(&trash,
Jeff Buchbinder2dbbf4d2014-08-29 15:10:08 -05004993 "<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 +01004994 (appctx->ctx.stats.scope_len > 0) ? scope_txt : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004995 STAT_SCOPE_TXT_MAXLEN);
4996
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004997 /* 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 +02004998 scope_txt[0] = 0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004999 if (appctx->ctx.stats.scope_len) {
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02005000 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau4e4292b2014-11-28 12:18:45 +01005001 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 +01005002 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02005003 }
5004
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005005 if (appctx->ctx.stats.flags & STAT_HIDE_DOWN)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005006 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02005007 "<li><a href=\"%s%s%s%s\">Show all servers</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005008 uri->uri_prefix,
5009 "",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005010 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02005011 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005012 else
5013 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02005014 "<li><a href=\"%s%s%s%s\">Hide 'DOWN' servers</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005015 uri->uri_prefix,
5016 ";up",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005017 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02005018 scope_txt);
Willy Tarreau91861262007-10-17 17:06:05 +02005019
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005020 if (uri->refresh > 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005021 if (appctx->ctx.stats.flags & STAT_NO_REFRESH)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005022 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02005023 "<li><a href=\"%s%s%s%s\">Enable refresh</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005024 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005025 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02005026 "",
5027 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005028 else
5029 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02005030 "<li><a href=\"%s%s%s%s\">Disable refresh</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005031 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005032 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02005033 ";norefresh",
5034 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005035 }
Willy Tarreau55bb8452007-10-17 18:44:57 +02005036
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005037 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02005038 "<li><a href=\"%s%s%s%s\">Refresh now</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005039 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005040 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
5041 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02005042 scope_txt);
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02005043
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005044 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02005045 "<li><a href=\"%s;csv%s%s\">CSV export</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005046 uri->uri_prefix,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02005047 (uri->refresh > 0) ? ";norefresh" : "",
5048 scope_txt);
Willy Tarreau4bab24d2007-11-30 18:16:29 +01005049
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005050 chunk_appendf(&trash,
5051 "</ul></td>"
5052 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
5053 "<b>External resources:</b><ul style=\"margin-top: 0.25em;\">\n"
5054 "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>\n"
5055 "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>\n"
5056 "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>\n"
5057 "</ul>"
5058 "</td>"
5059 "</tr></table>\n"
5060 ""
5061 );
Willy Tarreau4bab24d2007-11-30 18:16:29 +01005062
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005063 if (appctx->ctx.stats.st_code) {
5064 switch (appctx->ctx.stats.st_code) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005065 case STAT_STATUS_DONE:
5066 chunk_appendf(&trash,
Simon Horman837bfa72015-04-23 14:51:27 +09005067 "<p><div class=active_up>"
Willy Tarreauba6be982013-04-19 12:16:55 +02005068 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005069 "Action processed successfully."
Willy Tarreauba6be982013-04-19 12:16:55 +02005070 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005071 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
5072 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02005073 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005074 break;
5075 case STAT_STATUS_NONE:
5076 chunk_appendf(&trash,
Simon Horman837bfa72015-04-23 14:51:27 +09005077 "<p><div class=active_going_down>"
Willy Tarreauba6be982013-04-19 12:16:55 +02005078 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005079 "Nothing has changed."
Willy Tarreauba6be982013-04-19 12:16:55 +02005080 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005081 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
5082 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02005083 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005084 break;
5085 case STAT_STATUS_PART:
5086 chunk_appendf(&trash,
Simon Horman837bfa72015-04-23 14:51:27 +09005087 "<p><div class=active_going_down>"
Willy Tarreauba6be982013-04-19 12:16:55 +02005088 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005089 "Action partially processed.<br>"
5090 "Some server names are probably unknown or ambiguous (duplicated names in the backend)."
Willy Tarreauba6be982013-04-19 12:16:55 +02005091 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005092 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
5093 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02005094 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005095 break;
5096 case STAT_STATUS_ERRP:
5097 chunk_appendf(&trash,
Simon Horman837bfa72015-04-23 14:51:27 +09005098 "<p><div class=active_down>"
Willy Tarreauba6be982013-04-19 12:16:55 +02005099 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005100 "Action not processed because of invalid parameters."
5101 "<ul>"
5102 "<li>The action is maybe unknown.</li>"
5103 "<li>The backend name is probably unknown or ambiguous (duplicated names).</li>"
5104 "<li>Some server names are probably unknown or ambiguous (duplicated names in the backend).</li>"
5105 "</ul>"
Willy Tarreauba6be982013-04-19 12:16:55 +02005106 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005107 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
5108 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02005109 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005110 break;
5111 case STAT_STATUS_EXCD:
5112 chunk_appendf(&trash,
Simon Horman837bfa72015-04-23 14:51:27 +09005113 "<p><div class=active_down>"
Willy Tarreauba6be982013-04-19 12:16:55 +02005114 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005115 "<b>Action not processed : the buffer couldn't store all the data.<br>"
5116 "You should retry with less servers at a time.</b>"
Willy Tarreauba6be982013-04-19 12:16:55 +02005117 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005118 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
5119 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02005120 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005121 break;
5122 case STAT_STATUS_DENY:
5123 chunk_appendf(&trash,
Simon Horman837bfa72015-04-23 14:51:27 +09005124 "<p><div class=active_down>"
Willy Tarreauba6be982013-04-19 12:16:55 +02005125 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005126 "<b>Action denied.</b>"
Willy Tarreauba6be982013-04-19 12:16:55 +02005127 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005128 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
5129 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02005130 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005131 break;
5132 default:
5133 chunk_appendf(&trash,
Simon Horman837bfa72015-04-23 14:51:27 +09005134 "<p><div class=active_no_check>"
Willy Tarreauba6be982013-04-19 12:16:55 +02005135 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005136 "Unexpected result."
Willy Tarreauba6be982013-04-19 12:16:55 +02005137 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005138 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
5139 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02005140 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005141 }
5142 chunk_appendf(&trash, "<p>\n");
5143 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005144}
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01005145
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01005146/* Dumps the HTML stats trailer block to the trash. The caller is responsible
5147 * for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005148 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01005149static void stats_dump_html_end()
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005150{
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005151 chunk_appendf(&trash, "</body></html>\n");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005152}
Willy Tarreau7f062c42009-03-05 18:43:00 +01005153
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01005154/* This function dumps statistics onto the stream interface's read buffer in
5155 * either CSV or HTML format. <uri> contains some HTML-specific parameters that
Willy Tarreau306f8302013-07-08 15:53:06 +02005156 * are ignored for CSV format (hence <uri> may be NULL there). It returns 0 if
5157 * 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 +02005158 * and the stream must be closed, or -1 in case of any error. This function is
Willy Tarreau306f8302013-07-08 15:53:06 +02005159 * used by both the CLI and the HTTP handlers.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005160 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01005161static int stats_dump_stat_to_buffer(struct stream_interface *si, struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005162{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005163 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005164 struct channel *rep = si_ic(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005165 struct proxy *px;
Willy Tarreau7f062c42009-03-05 18:43:00 +01005166
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005167 chunk_reset(&trash);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02005168
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005169 switch (appctx->st2) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005170 case STAT_ST_INIT:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005171 appctx->st2 = STAT_ST_HEAD; /* let's start producing data */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005172 /* fall through */
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005173
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005174 case STAT_ST_HEAD:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005175 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01005176 stats_dump_html_head(uri);
Willy Tarreau1e62df92016-01-11 18:57:53 +01005177 else if (!(appctx->ctx.stats.flags & STAT_FMT_TYPED))
Willy Tarreau354898b2012-12-23 18:15:23 +01005178 stats_dump_csv_header();
Willy Tarreaud9b587f2010-02-26 10:05:55 +01005179
Willy Tarreaubc18da12015-03-13 14:00:47 +01005180 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02005181 si_applet_cant_put(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005182 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01005183 }
Willy Tarreauae526782010-03-04 20:34:23 +01005184
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005185 appctx->st2 = STAT_ST_INFO;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005186 /* fall through */
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01005187
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005188 case STAT_ST_INFO:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005189 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01005190 stats_dump_html_info(si, uri);
Willy Tarreaubc18da12015-03-13 14:00:47 +01005191 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02005192 si_applet_cant_put(si);
Willy Tarreau91861262007-10-17 17:06:05 +02005193 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01005194 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005195 }
Willy Tarreau91861262007-10-17 17:06:05 +02005196
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005197 appctx->ctx.stats.px = proxy;
5198 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
5199 appctx->st2 = STAT_ST_LIST;
Willy Tarreau91861262007-10-17 17:06:05 +02005200 /* fall through */
5201
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005202 case STAT_ST_LIST:
5203 /* dump proxies */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005204 while (appctx->ctx.stats.px) {
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01005205 if (buffer_almost_full(rep->buf)) {
Willy Tarreaufe127932015-04-21 19:23:39 +02005206 si_applet_cant_put(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005207 return 0;
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01005208 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005209
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005210 px = appctx->ctx.stats.px;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005211 /* skip the disabled proxies, global frontend and non-networked ones */
5212 if (px->state != PR_STSTOPPED && px->uuid > 0 && (px->cap & (PR_CAP_FE | PR_CAP_BE)))
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01005213 if (stats_dump_proxy_to_buffer(si, px, uri) == 0)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005214 return 0;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005215
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005216 appctx->ctx.stats.px = px->next;
5217 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005218 }
5219 /* here, we just have reached the last proxy */
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005220
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005221 appctx->st2 = STAT_ST_END;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005222 /* fall through */
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005223
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005224 case STAT_ST_END:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005225 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01005226 stats_dump_html_end();
Willy Tarreaubc18da12015-03-13 14:00:47 +01005227 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02005228 si_applet_cant_put(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005229 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01005230 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005231 }
Willy Tarreau55058a72012-11-21 08:27:21 +01005232
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005233 appctx->st2 = STAT_ST_FIN;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005234 /* fall through */
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02005235
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005236 case STAT_ST_FIN:
5237 return 1;
Willy Tarreau55058a72012-11-21 08:27:21 +01005238
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005239 default:
5240 /* unknown state ! */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005241 appctx->st2 = STAT_ST_FIN;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005242 return -1;
5243 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005244}
Willy Tarreauae526782010-03-04 20:34:23 +01005245
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005246/* We reached the stats page through a POST request. The appctx is
5247 * expected to have already been allocated by the caller.
Willy Tarreau347a35d2013-11-22 17:51:09 +01005248 * Parse the posted data and enable/disable servers if necessary.
5249 * Returns 1 if request was parsed or zero if it needs more data.
5250 */
5251static int stats_process_http_post(struct stream_interface *si)
5252{
Willy Tarreau87b09662015-04-03 00:22:06 +02005253 struct stream *s = si_strm(si);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005254 struct appctx *appctx = objt_appctx(si->end);
Willy Tarreau347a35d2013-11-22 17:51:09 +01005255
5256 struct proxy *px = NULL;
5257 struct server *sv = NULL;
5258
5259 char key[LINESIZE];
5260 int action = ST_ADM_ACTION_NONE;
5261 int reprocess = 0;
5262
5263 int total_servers = 0;
5264 int altered_servers = 0;
5265
5266 char *first_param, *cur_param, *next_param, *end_params;
5267 char *st_cur_param = NULL;
5268 char *st_next_param = NULL;
5269
5270 struct chunk *temp;
5271 int reql;
5272
5273 temp = get_trash_chunk();
Willy Tarreaueee5b512015-04-03 23:46:31 +02005274 if (temp->size < s->txn->req.body_len) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01005275 /* too large request */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005276 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005277 goto out;
5278 }
5279
Willy Tarreaueee5b512015-04-03 23:46:31 +02005280 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 +01005281 if (reql <= 0) {
5282 /* we need more data */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005283 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005284 return 0;
5285 }
5286
5287 first_param = temp->str;
5288 end_params = temp->str + reql;
5289 cur_param = next_param = end_params;
5290 *end_params = '\0';
5291
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005292 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005293
5294 /*
5295 * Parse the parameters in reverse order to only store the last value.
5296 * From the html form, the backend and the action are at the end.
5297 */
5298 while (cur_param > first_param) {
5299 char *value;
5300 int poffset, plen;
5301
5302 cur_param--;
5303
5304 if ((*cur_param == '&') || (cur_param == first_param)) {
5305 reprocess_servers:
5306 /* Parse the key */
5307 poffset = (cur_param != first_param ? 1 : 0);
5308 plen = next_param - cur_param + (cur_param == first_param ? 1 : 0);
5309 if ((plen > 0) && (plen <= sizeof(key))) {
5310 strncpy(key, cur_param + poffset, plen);
5311 key[plen - 1] = '\0';
5312 } else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005313 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005314 goto out;
5315 }
5316
5317 /* Parse the value */
5318 value = key;
5319 while (*value != '\0' && *value != '=') {
5320 value++;
5321 }
5322 if (*value == '=') {
5323 /* Ok, a value is found, we can mark the end of the key */
5324 *value++ = '\0';
5325 }
5326 if (url_decode(key) < 0 || url_decode(value) < 0)
5327 break;
5328
5329 /* Now we can check the key to see what to do */
5330 if (!px && (strcmp(key, "b") == 0)) {
Willy Tarreau9e0bb102015-05-26 11:24:42 +02005331 if ((px = proxy_be_by_name(value)) == NULL) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01005332 /* the backend name is unknown or ambiguous (duplicate names) */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005333 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005334 goto out;
5335 }
5336 }
5337 else if (!action && (strcmp(key, "action") == 0)) {
Willy Tarreaued7df902014-05-22 18:04:49 +02005338 if (strcmp(value, "ready") == 0) {
5339 action = ST_ADM_ACTION_READY;
5340 }
5341 else if (strcmp(value, "drain") == 0) {
5342 action = ST_ADM_ACTION_DRAIN;
5343 }
5344 else if (strcmp(value, "maint") == 0) {
5345 action = ST_ADM_ACTION_MAINT;
5346 }
5347 else if (strcmp(value, "shutdown") == 0) {
5348 action = ST_ADM_ACTION_SHUTDOWN;
5349 }
Willy Tarreau248a60e2014-05-23 14:59:48 +02005350 else if (strcmp(value, "dhlth") == 0) {
5351 action = ST_ADM_ACTION_DHLTH;
5352 }
5353 else if (strcmp(value, "ehlth") == 0) {
5354 action = ST_ADM_ACTION_EHLTH;
5355 }
5356 else if (strcmp(value, "hrunn") == 0) {
5357 action = ST_ADM_ACTION_HRUNN;
5358 }
5359 else if (strcmp(value, "hnolb") == 0) {
5360 action = ST_ADM_ACTION_HNOLB;
5361 }
5362 else if (strcmp(value, "hdown") == 0) {
5363 action = ST_ADM_ACTION_HDOWN;
5364 }
5365 else if (strcmp(value, "dagent") == 0) {
5366 action = ST_ADM_ACTION_DAGENT;
5367 }
5368 else if (strcmp(value, "eagent") == 0) {
5369 action = ST_ADM_ACTION_EAGENT;
5370 }
5371 else if (strcmp(value, "arunn") == 0) {
5372 action = ST_ADM_ACTION_ARUNN;
5373 }
5374 else if (strcmp(value, "adown") == 0) {
5375 action = ST_ADM_ACTION_ADOWN;
5376 }
Willy Tarreaued7df902014-05-22 18:04:49 +02005377 /* else these are the old supported methods */
5378 else if (strcmp(value, "disable") == 0) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01005379 action = ST_ADM_ACTION_DISABLE;
5380 }
5381 else if (strcmp(value, "enable") == 0) {
5382 action = ST_ADM_ACTION_ENABLE;
5383 }
5384 else if (strcmp(value, "stop") == 0) {
5385 action = ST_ADM_ACTION_STOP;
5386 }
5387 else if (strcmp(value, "start") == 0) {
5388 action = ST_ADM_ACTION_START;
5389 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01005390 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005391 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005392 goto out;
5393 }
5394 }
5395 else if (strcmp(key, "s") == 0) {
5396 if (!(px && action)) {
5397 /*
5398 * Indicates that we'll need to reprocess the parameters
5399 * as soon as backend and action are known
5400 */
5401 if (!reprocess) {
5402 st_cur_param = cur_param;
5403 st_next_param = next_param;
5404 }
5405 reprocess = 1;
5406 }
5407 else if ((sv = findserver(px, value)) != NULL) {
5408 switch (action) {
5409 case ST_ADM_ACTION_DISABLE:
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02005410 if (!(sv->admin & SRV_ADMF_FMAINT)) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01005411 altered_servers++;
5412 total_servers++;
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02005413 srv_set_admin_flag(sv, SRV_ADMF_FMAINT);
Willy Tarreau347a35d2013-11-22 17:51:09 +01005414 }
5415 break;
5416 case ST_ADM_ACTION_ENABLE:
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02005417 if (sv->admin & SRV_ADMF_FMAINT) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01005418 altered_servers++;
5419 total_servers++;
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02005420 srv_clr_admin_flag(sv, SRV_ADMF_FMAINT);
Willy Tarreau347a35d2013-11-22 17:51:09 +01005421 }
5422 break;
5423 case ST_ADM_ACTION_STOP:
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02005424 if (!(sv->admin & SRV_ADMF_FDRAIN)) {
5425 srv_set_admin_flag(sv, SRV_ADMF_FDRAIN);
5426 altered_servers++;
5427 total_servers++;
5428 }
5429 break;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005430 case ST_ADM_ACTION_START:
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02005431 if (sv->admin & SRV_ADMF_FDRAIN) {
5432 srv_clr_admin_flag(sv, SRV_ADMF_FDRAIN);
5433 altered_servers++;
5434 total_servers++;
5435 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01005436 break;
Willy Tarreau248a60e2014-05-23 14:59:48 +02005437 case ST_ADM_ACTION_DHLTH:
5438 if (sv->check.state & CHK_ST_CONFIGURED) {
5439 sv->check.state &= ~CHK_ST_ENABLED;
5440 altered_servers++;
5441 total_servers++;
5442 }
5443 break;
5444 case ST_ADM_ACTION_EHLTH:
5445 if (sv->check.state & CHK_ST_CONFIGURED) {
5446 sv->check.state |= CHK_ST_ENABLED;
5447 altered_servers++;
5448 total_servers++;
5449 }
5450 break;
5451 case ST_ADM_ACTION_HRUNN:
5452 if (!(sv->track)) {
5453 sv->check.health = sv->check.rise + sv->check.fall - 1;
5454 srv_set_running(sv, "changed from Web interface");
5455 altered_servers++;
5456 total_servers++;
5457 }
5458 break;
5459 case ST_ADM_ACTION_HNOLB:
5460 if (!(sv->track)) {
5461 sv->check.health = sv->check.rise + sv->check.fall - 1;
5462 srv_set_stopping(sv, "changed from Web interface");
5463 altered_servers++;
5464 total_servers++;
5465 }
5466 break;
5467 case ST_ADM_ACTION_HDOWN:
5468 if (!(sv->track)) {
5469 sv->check.health = 0;
5470 srv_set_stopped(sv, "changed from Web interface");
5471 altered_servers++;
5472 total_servers++;
5473 }
5474 break;
5475 case ST_ADM_ACTION_DAGENT:
5476 if (sv->agent.state & CHK_ST_CONFIGURED) {
5477 sv->agent.state &= ~CHK_ST_ENABLED;
5478 altered_servers++;
5479 total_servers++;
5480 }
5481 break;
5482 case ST_ADM_ACTION_EAGENT:
5483 if (sv->agent.state & CHK_ST_CONFIGURED) {
5484 sv->agent.state |= CHK_ST_ENABLED;
5485 altered_servers++;
5486 total_servers++;
5487 }
5488 break;
5489 case ST_ADM_ACTION_ARUNN:
5490 if (sv->agent.state & CHK_ST_ENABLED) {
5491 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
5492 srv_set_running(sv, "changed from Web interface");
5493 altered_servers++;
5494 total_servers++;
5495 }
5496 break;
5497 case ST_ADM_ACTION_ADOWN:
5498 if (sv->agent.state & CHK_ST_ENABLED) {
5499 sv->agent.health = 0;
5500 srv_set_stopped(sv, "changed from Web interface");
5501 altered_servers++;
5502 total_servers++;
5503 }
5504 break;
Willy Tarreaued7df902014-05-22 18:04:49 +02005505 case ST_ADM_ACTION_READY:
5506 srv_adm_set_ready(sv);
5507 altered_servers++;
5508 total_servers++;
5509 break;
5510 case ST_ADM_ACTION_DRAIN:
5511 srv_adm_set_drain(sv);
5512 altered_servers++;
5513 total_servers++;
5514 break;
5515 case ST_ADM_ACTION_MAINT:
5516 srv_adm_set_maint(sv);
5517 altered_servers++;
5518 total_servers++;
5519 break;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005520 case ST_ADM_ACTION_SHUTDOWN:
5521 if (px->state != PR_STSTOPPED) {
Willy Tarreau87b09662015-04-03 00:22:06 +02005522 struct stream *sess, *sess_bck;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005523
5524 list_for_each_entry_safe(sess, sess_bck, &sv->actconns, by_srv)
5525 if (sess->srv_conn == sv)
Willy Tarreaue7dff022015-04-03 01:14:29 +02005526 stream_shutdown(sess, SF_ERR_KILLED);
Willy Tarreau347a35d2013-11-22 17:51:09 +01005527
5528 altered_servers++;
5529 total_servers++;
5530 }
5531 break;
5532 }
5533 } else {
5534 /* the server name is unknown or ambiguous (duplicate names) */
5535 total_servers++;
5536 }
5537 }
5538 if (reprocess && px && action) {
5539 /* Now, we know the backend and the action chosen by the user.
5540 * We can safely restart from the first server parameter
5541 * to reprocess them
5542 */
5543 cur_param = st_cur_param;
5544 next_param = st_next_param;
5545 reprocess = 0;
5546 goto reprocess_servers;
5547 }
5548
5549 next_param = cur_param;
5550 }
5551 }
5552
5553 if (total_servers == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005554 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005555 }
5556 else if (altered_servers == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005557 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005558 }
5559 else if (altered_servers == total_servers) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005560 appctx->ctx.stats.st_code = STAT_STATUS_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005561 }
5562 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005563 appctx->ctx.stats.st_code = STAT_STATUS_PART;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005564 }
5565 out:
5566 return 1;
5567}
5568
5569
5570static int stats_send_http_headers(struct stream_interface *si)
5571{
Willy Tarreau87b09662015-04-03 00:22:06 +02005572 struct stream *s = si_strm(si);
Willy Tarreau347a35d2013-11-22 17:51:09 +01005573 struct uri_auth *uri = s->be->uri_auth;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005574 struct appctx *appctx = objt_appctx(si->end);
Willy Tarreau347a35d2013-11-22 17:51:09 +01005575
5576 chunk_printf(&trash,
Willy Tarreau8b8995f2014-04-24 22:51:54 +02005577 "HTTP/1.1 200 OK\r\n"
Willy Tarreau347a35d2013-11-22 17:51:09 +01005578 "Cache-Control: no-cache\r\n"
5579 "Connection: close\r\n"
5580 "Content-Type: %s\r\n",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005581 (appctx->ctx.stats.flags & STAT_FMT_HTML) ? "text/html" : "text/plain");
Willy Tarreau347a35d2013-11-22 17:51:09 +01005582
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005583 if (uri->refresh > 0 && !(appctx->ctx.stats.flags & STAT_NO_REFRESH))
Willy Tarreau347a35d2013-11-22 17:51:09 +01005584 chunk_appendf(&trash, "Refresh: %d\r\n",
5585 uri->refresh);
5586
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005587 /* we don't send the CRLF in chunked mode, it will be sent with the first chunk's size */
5588
5589 if (appctx->ctx.stats.flags & STAT_CHUNKED)
5590 chunk_appendf(&trash, "Transfer-Encoding: chunked\r\n");
5591 else
5592 chunk_appendf(&trash, "\r\n");
Willy Tarreau347a35d2013-11-22 17:51:09 +01005593
Willy Tarreaueee5b512015-04-03 23:46:31 +02005594 s->txn->status = 200;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005595 s->logs.tv_request = now;
5596
Willy Tarreaubc18da12015-03-13 14:00:47 +01005597 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02005598 si_applet_cant_put(si);
Willy Tarreau347a35d2013-11-22 17:51:09 +01005599 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01005600 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01005601
5602 return 1;
5603}
5604
5605static int stats_send_http_redirect(struct stream_interface *si)
5606{
5607 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
Willy Tarreau87b09662015-04-03 00:22:06 +02005608 struct stream *s = si_strm(si);
Willy Tarreau347a35d2013-11-22 17:51:09 +01005609 struct uri_auth *uri = s->be->uri_auth;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005610 struct appctx *appctx = objt_appctx(si->end);
Willy Tarreau347a35d2013-11-22 17:51:09 +01005611
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005612 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Willy Tarreau347a35d2013-11-22 17:51:09 +01005613 scope_txt[0] = 0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005614 if (appctx->ctx.stats.scope_len) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01005615 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau4e4292b2014-11-28 12:18:45 +01005616 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 +01005617 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005618 }
5619
5620 /* We don't want to land on the posted stats page because a refresh will
5621 * repost the data. We don't want this to happen on accident so we redirect
5622 * the browse to the stats page with a GET.
5623 */
5624 chunk_printf(&trash,
5625 "HTTP/1.1 303 See Other\r\n"
5626 "Cache-Control: no-cache\r\n"
5627 "Content-Type: text/plain\r\n"
5628 "Connection: close\r\n"
5629 "Location: %s;st=%s%s%s%s\r\n"
Willy Tarreaufdfcc9d2016-01-26 13:57:29 +01005630 "Content-length: 0\r\n"
Willy Tarreau347a35d2013-11-22 17:51:09 +01005631 "\r\n",
5632 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005633 ((appctx->ctx.stats.st_code > STAT_STATUS_INIT) &&
5634 (appctx->ctx.stats.st_code < STAT_STATUS_SIZE) &&
5635 stat_status_codes[appctx->ctx.stats.st_code]) ?
5636 stat_status_codes[appctx->ctx.stats.st_code] :
Willy Tarreau347a35d2013-11-22 17:51:09 +01005637 stat_status_codes[STAT_STATUS_UNKN],
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005638 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
5639 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreau347a35d2013-11-22 17:51:09 +01005640 scope_txt);
5641
Willy Tarreaueee5b512015-04-03 23:46:31 +02005642 s->txn->status = 303;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005643 s->logs.tv_request = now;
5644
Willy Tarreaubc18da12015-03-13 14:00:47 +01005645 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02005646 si_applet_cant_put(si);
Willy Tarreau347a35d2013-11-22 17:51:09 +01005647 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01005648 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01005649
5650 return 1;
5651}
5652
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005653/* This I/O handler runs as an applet embedded in a stream interface. It is
5654 * used to send HTTP stats over a TCP socket. The mechanism is very simple.
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005655 * appctx->st0 contains the operation in progress (dump, done). The handler
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005656 * automatically unregisters itself once transfer is complete.
5657 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02005658static void http_stats_io_handler(struct appctx *appctx)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005659{
Willy Tarreau00a37f02015-04-13 12:05:19 +02005660 struct stream_interface *si = appctx->owner;
Willy Tarreau87b09662015-04-03 00:22:06 +02005661 struct stream *s = si_strm(si);
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005662 struct channel *req = si_oc(si);
5663 struct channel *res = si_ic(si);
Willy Tarreau55058a72012-11-21 08:27:21 +01005664
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005665 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
5666 goto out;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01005667
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005668 /* check that the output is not closed */
5669 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005670 appctx->st0 = STAT_HTTP_DONE;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005671
Willy Tarreau347a35d2013-11-22 17:51:09 +01005672 /* all states are processed in sequence */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005673 if (appctx->st0 == STAT_HTTP_HEAD) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01005674 if (stats_send_http_headers(si)) {
Willy Tarreaueee5b512015-04-03 23:46:31 +02005675 if (s->txn->meth == HTTP_METH_HEAD)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005676 appctx->st0 = STAT_HTTP_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005677 else
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005678 appctx->st0 = STAT_HTTP_DUMP;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005679 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01005680 }
5681
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005682 if (appctx->st0 == STAT_HTTP_DUMP) {
Willy Tarreau4e4292b2014-11-28 12:18:45 +01005683 unsigned int prev_len = si_ib(si)->i;
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005684 unsigned int data_len;
5685 unsigned int last_len;
Willy Tarreaucce36482014-04-24 20:26:41 +02005686 unsigned int last_fwd = 0;
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005687
5688 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
5689 /* One difficulty we're facing is that we must prevent
5690 * the input data from being automatically forwarded to
5691 * the output area. For this, we temporarily disable
5692 * forwarding on the channel.
5693 */
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005694 last_fwd = si_ic(si)->to_forward;
5695 si_ic(si)->to_forward = 0;
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005696 chunk_printf(&trash, "\r\n000000\r\n");
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005697 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02005698 si_applet_cant_put(si);
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005699 si_ic(si)->to_forward = last_fwd;
Willy Tarreau828824a2015-04-19 17:20:03 +02005700 goto out;
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005701 }
5702 }
5703
Willy Tarreau4e4292b2014-11-28 12:18:45 +01005704 data_len = si_ib(si)->i;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005705 if (stats_dump_stat_to_buffer(si, s->be->uri_auth))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005706 appctx->st0 = STAT_HTTP_DONE;
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005707
Willy Tarreau4e4292b2014-11-28 12:18:45 +01005708 last_len = si_ib(si)->i;
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005709
5710 /* Now we must either adjust or remove the chunk size. This is
5711 * not easy because the chunk size might wrap at the end of the
5712 * buffer, so we pretend we have nothing in the buffer, we write
5713 * the size, then restore the buffer's contents. Note that we can
5714 * only do that because no forwarding is scheduled on the stats
5715 * applet.
5716 */
5717 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
Willy Tarreau4e4292b2014-11-28 12:18:45 +01005718 si_ic(si)->total -= (last_len - prev_len);
5719 si_ib(si)->i -= (last_len - prev_len);
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005720
5721 if (last_len != data_len) {
5722 chunk_printf(&trash, "\r\n%06x\r\n", (last_len - data_len));
Willy Tarreaubc18da12015-03-13 14:00:47 +01005723 if (bi_putchk(si_ic(si), &trash) == -1)
Willy Tarreaufe127932015-04-21 19:23:39 +02005724 si_applet_cant_put(si);
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005725
Willy Tarreau4e4292b2014-11-28 12:18:45 +01005726 si_ic(si)->total += (last_len - data_len);
5727 si_ib(si)->i += (last_len - data_len);
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005728 }
5729 /* now re-enable forwarding */
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005730 channel_forward(si_ic(si), last_fwd);
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005731 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005732 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02005733
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005734 if (appctx->st0 == STAT_HTTP_POST) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01005735 if (stats_process_http_post(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005736 appctx->st0 = STAT_HTTP_LAST;
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005737 else if (si_oc(si)->flags & CF_SHUTR)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005738 appctx->st0 = STAT_HTTP_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005739 }
5740
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005741 if (appctx->st0 == STAT_HTTP_LAST) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01005742 if (stats_send_http_redirect(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005743 appctx->st0 = STAT_HTTP_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005744 }
5745
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005746 if (appctx->st0 == STAT_HTTP_DONE) {
5747 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
5748 chunk_printf(&trash, "\r\n0\r\n\r\n");
Willy Tarreaubc18da12015-03-13 14:00:47 +01005749 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02005750 si_applet_cant_put(si);
Willy Tarreau828824a2015-04-19 17:20:03 +02005751 goto out;
Willy Tarreaubc18da12015-03-13 14:00:47 +01005752 }
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005753 }
5754 /* eat the whole request */
Willy Tarreau4e4292b2014-11-28 12:18:45 +01005755 bo_skip(si_oc(si), si_ob(si)->o);
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005756 res->flags |= CF_READ_NULL;
5757 si_shutr(si);
5758 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01005759
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005760 if ((res->flags & CF_SHUTR) && (si->state == SI_ST_EST))
5761 si_shutw(si);
Willy Tarreau91861262007-10-17 17:06:05 +02005762
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005763 if (appctx->st0 == STAT_HTTP_DONE) {
Willy Tarreau96d44912013-11-22 12:25:24 +01005764 if ((req->flags & CF_SHUTW) && (si->state == SI_ST_EST)) {
5765 si_shutr(si);
5766 res->flags |= CF_READ_NULL;
5767 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005768 }
Willy Tarreau828824a2015-04-19 17:20:03 +02005769 out:
Willy Tarreaud4da1962015-04-20 01:31:23 +02005770 /* just to make gcc happy */ ;
Willy Tarreau91861262007-10-17 17:06:05 +02005771}
5772
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005773
Willy Tarreau909d5172012-11-26 03:04:41 +01005774static inline const char *get_conn_ctrl_name(const struct connection *conn)
5775{
Willy Tarreau3c728722014-01-23 13:50:42 +01005776 if (!conn_ctrl_ready(conn))
Willy Tarreau909d5172012-11-26 03:04:41 +01005777 return "NONE";
5778 return conn->ctrl->name;
5779}
5780
5781static inline const char *get_conn_xprt_name(const struct connection *conn)
5782{
5783 static char ptr[17];
5784
Willy Tarreauaad69382014-01-23 14:21:42 +01005785 if (!conn_xprt_ready(conn))
Willy Tarreau909d5172012-11-26 03:04:41 +01005786 return "NONE";
5787
5788 if (conn->xprt == &raw_sock)
5789 return "RAW";
5790
5791#ifdef USE_OPENSSL
5792 if (conn->xprt == &ssl_sock)
5793 return "SSL";
5794#endif
5795 snprintf(ptr, sizeof(ptr), "%p", conn->xprt);
5796 return ptr;
5797}
5798
5799static inline const char *get_conn_data_name(const struct connection *conn)
5800{
5801 static char ptr[17];
5802
5803 if (!conn->data)
5804 return "NONE";
5805
5806 if (conn->data == &sess_conn_cb)
5807 return "SESS";
5808
5809 if (conn->data == &si_conn_cb)
5810 return "STRM";
5811
5812 if (conn->data == &check_conn_cb)
5813 return "CHCK";
5814
5815 snprintf(ptr, sizeof(ptr), "%p", conn->data);
5816 return ptr;
5817}
5818
Willy Tarreau87b09662015-04-03 00:22:06 +02005819/* This function dumps a complete stream state onto the stream interface's
5820 * read buffer. The stream has to be set in sess->target. It returns
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01005821 * 0 if the output buffer is full and it needs to be called again, otherwise
5822 * non-zero. It is designed to be called from stats_dump_sess_to_buffer() below.
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005823 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005824static int stats_dump_full_sess_to_buffer(struct stream_interface *si, struct stream *sess)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005825{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005826 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005827 struct tm tm;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005828 extern const char *monthname[12];
5829 char pn[INET6_ADDRSTRLEN];
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005830 struct connection *conn;
Willy Tarreau284ddbf2013-12-01 20:45:00 +01005831 struct appctx *tmpctx;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005832
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005833 chunk_reset(&trash);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005834
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005835 if (appctx->ctx.sess.section > 0 && appctx->ctx.sess.uid != sess->uniq_id) {
Willy Tarreau87b09662015-04-03 00:22:06 +02005836 /* stream changed, no need to go any further */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005837 chunk_appendf(&trash, " *** session terminated while we were watching it ***\n");
Willy Tarreaubc18da12015-03-13 14:00:47 +01005838 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02005839 si_applet_cant_put(si);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005840 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01005841 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005842 appctx->ctx.sess.uid = 0;
5843 appctx->ctx.sess.section = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005844 return 1;
5845 }
5846
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005847 switch (appctx->ctx.sess.section) {
Willy Tarreau87b09662015-04-03 00:22:06 +02005848 case 0: /* main status of the stream */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005849 appctx->ctx.sess.uid = sess->uniq_id;
5850 appctx->ctx.sess.section = 1;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005851 /* fall through */
5852
5853 case 1:
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005854 get_localtime(sess->logs.accept_date.tv_sec, &tm);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005855 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005856 "%p: [%02d/%s/%04d:%02d:%02d:%02d.%06d] id=%u proto=%s",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005857 sess,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005858 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
5859 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(sess->logs.accept_date.tv_usec),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005860 sess->uniq_id,
Willy Tarreau666f5042015-06-17 19:49:52 +02005861 strm_li(sess) ? strm_li(sess)->proto->name : "?");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005862
Willy Tarreaud0d8da92015-04-04 02:10:38 +02005863 conn = objt_conn(strm_orig(sess));
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005864 switch (conn ? addr_to_str(&conn->addr.from, pn, sizeof(pn)) : AF_UNSPEC) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005865 case AF_INET:
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005866 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005867 chunk_appendf(&trash, " source=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005868 pn, get_host_port(&conn->addr.from));
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005869 break;
5870 case AF_UNIX:
Willy Tarreaud0d8da92015-04-04 02:10:38 +02005871 chunk_appendf(&trash, " source=unix:%d\n", strm_li(sess)->luid);
Emeric Brun837ca522010-10-22 16:19:01 +02005872 break;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005873 default:
5874 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005875 chunk_appendf(&trash, "\n");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005876 break;
5877 }
5878
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005879 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005880 " flags=0x%x, conn_retries=%d, srv_conn=%p, pend_pos=%p\n",
Willy Tarreauee28de02010-06-01 09:51:00 +02005881 sess->flags, sess->si[1].conn_retries, sess->srv_conn, sess->pend_pos);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005882
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005883 chunk_appendf(&trash,
Daniel Schultze90690c72012-03-23 10:53:36 -07005884 " frontend=%s (id=%u mode=%s), listener=%s (id=%u)",
Willy Tarreaud0d8da92015-04-04 02:10:38 +02005885 strm_fe(sess)->id, strm_fe(sess)->uuid, strm_fe(sess)->mode ? "http" : "tcp",
5886 strm_li(sess) ? strm_li(sess)->name ? strm_li(sess)->name : "?" : "?",
5887 strm_li(sess) ? strm_li(sess)->luid : 0);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005888
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005889 if (conn)
5890 conn_get_to_addr(conn);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005891
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005892 switch (conn ? addr_to_str(&conn->addr.to, pn, sizeof(pn)) : AF_UNSPEC) {
Daniel Schultze90690c72012-03-23 10:53:36 -07005893 case AF_INET:
5894 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005895 chunk_appendf(&trash, " addr=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005896 pn, get_host_port(&conn->addr.to));
Daniel Schultze90690c72012-03-23 10:53:36 -07005897 break;
5898 case AF_UNIX:
Willy Tarreaud0d8da92015-04-04 02:10:38 +02005899 chunk_appendf(&trash, " addr=unix:%d\n", strm_li(sess)->luid);
Daniel Schultze90690c72012-03-23 10:53:36 -07005900 break;
5901 default:
5902 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005903 chunk_appendf(&trash, "\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07005904 break;
5905 }
5906
Willy Tarreau50943332011-09-02 17:33:05 +02005907 if (sess->be->cap & PR_CAP_BE)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005908 chunk_appendf(&trash,
Daniel Schultze90690c72012-03-23 10:53:36 -07005909 " backend=%s (id=%u mode=%s)",
Willy Tarreau50943332011-09-02 17:33:05 +02005910 sess->be->id,
Daniel Schultze90690c72012-03-23 10:53:36 -07005911 sess->be->uuid, sess->be->mode ? "http" : "tcp");
5912 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005913 chunk_appendf(&trash, " backend=<NONE> (id=-1 mode=-)");
Daniel Schultze90690c72012-03-23 10:53:36 -07005914
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005915 conn = objt_conn(sess->si[1].end);
5916 if (conn)
5917 conn_get_from_addr(conn);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005918
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005919 switch (conn ? addr_to_str(&conn->addr.from, pn, sizeof(pn)) : AF_UNSPEC) {
Daniel Schultze90690c72012-03-23 10:53:36 -07005920 case AF_INET:
5921 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005922 chunk_appendf(&trash, " addr=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005923 pn, get_host_port(&conn->addr.from));
Daniel Schultze90690c72012-03-23 10:53:36 -07005924 break;
5925 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005926 chunk_appendf(&trash, " addr=unix\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07005927 break;
5928 default:
5929 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005930 chunk_appendf(&trash, "\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07005931 break;
5932 }
5933
5934 if (sess->be->cap & PR_CAP_BE)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005935 chunk_appendf(&trash,
Daniel Schultze90690c72012-03-23 10:53:36 -07005936 " server=%s (id=%u)",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005937 objt_server(sess->target) ? objt_server(sess->target)->id : "<none>",
5938 objt_server(sess->target) ? objt_server(sess->target)->puid : 0);
Willy Tarreau50943332011-09-02 17:33:05 +02005939 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005940 chunk_appendf(&trash, " server=<NONE> (id=-1)");
Daniel Schultze90690c72012-03-23 10:53:36 -07005941
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005942 if (conn)
5943 conn_get_to_addr(conn);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005944
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005945 switch (conn ? addr_to_str(&conn->addr.to, pn, sizeof(pn)) : AF_UNSPEC) {
Daniel Schultze90690c72012-03-23 10:53:36 -07005946 case AF_INET:
5947 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005948 chunk_appendf(&trash, " addr=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005949 pn, get_host_port(&conn->addr.to));
Daniel Schultze90690c72012-03-23 10:53:36 -07005950 break;
5951 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005952 chunk_appendf(&trash, " addr=unix\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07005953 break;
5954 default:
5955 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005956 chunk_appendf(&trash, "\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07005957 break;
5958 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005959
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005960 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005961 " task=%p (state=0x%02x nice=%d calls=%d exp=%s%s",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005962 sess->task,
5963 sess->task->state,
5964 sess->task->nice, sess->task->calls,
5965 sess->task->expire ?
5966 tick_is_expired(sess->task->expire, now_ms) ? "<PAST>" :
5967 human_time(TICKS_TO_MS(sess->task->expire - now_ms),
5968 TICKS_TO_MS(1000)) : "<NEVER>",
5969 task_in_rq(sess->task) ? ", running" : "");
5970
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005971 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005972 " age=%s)\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005973 human_time(now.tv_sec - sess->logs.accept_date.tv_sec, 1));
5974
Willy Tarreaueee5b512015-04-03 23:46:31 +02005975 if (sess->txn)
5976 chunk_appendf(&trash,
Willy Tarreau98410192014-11-26 18:05:38 +01005977 " 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 +02005978 sess->txn, sess->txn->flags, sess->txn->meth, sess->txn->status,
5979 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 +01005980
5981 chunk_appendf(&trash,
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02005982 " si[0]=%p (state=%s flags=0x%02x endp0=%s:%p exp=%s, et=0x%03x)\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005983 &sess->si[0],
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005984 si_state_str(sess->si[0].state),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005985 sess->si[0].flags,
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005986 obj_type_name(sess->si[0].end),
5987 obj_base_ptr(sess->si[0].end),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005988 sess->si[0].exp ?
5989 tick_is_expired(sess->si[0].exp, now_ms) ? "<PAST>" :
5990 human_time(TICKS_TO_MS(sess->si[0].exp - now_ms),
5991 TICKS_TO_MS(1000)) : "<NEVER>",
5992 sess->si[0].err_type);
5993
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005994 chunk_appendf(&trash,
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02005995 " si[1]=%p (state=%s flags=0x%02x endp1=%s:%p exp=%s, et=0x%03x)\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005996 &sess->si[1],
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005997 si_state_str(sess->si[1].state),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005998 sess->si[1].flags,
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005999 obj_type_name(sess->si[1].end),
6000 obj_base_ptr(sess->si[1].end),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006001 sess->si[1].exp ?
6002 tick_is_expired(sess->si[1].exp, now_ms) ? "<PAST>" :
6003 human_time(TICKS_TO_MS(sess->si[1].exp - now_ms),
6004 TICKS_TO_MS(1000)) : "<NEVER>",
6005 sess->si[1].err_type);
6006
Willy Tarreau284ddbf2013-12-01 20:45:00 +01006007 if ((conn = objt_conn(sess->si[0].end)) != NULL) {
Willy Tarreaucf644ed2013-09-29 17:19:56 +02006008 chunk_appendf(&trash,
6009 " co0=%p ctrl=%s xprt=%s data=%s target=%s:%p\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006010 conn,
6011 get_conn_ctrl_name(conn),
6012 get_conn_xprt_name(conn),
6013 get_conn_data_name(conn),
6014 obj_type_name(conn->target),
6015 obj_base_ptr(conn->target));
Willy Tarreau909d5172012-11-26 03:04:41 +01006016
Willy Tarreaucf644ed2013-09-29 17:19:56 +02006017 chunk_appendf(&trash,
Willy Tarreau16f649c2014-01-25 19:10:48 +01006018 " flags=0x%08x fd=%d fd.state=%02x fd.cache=%d updt=%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006019 conn->flags,
6020 conn->t.sock.fd,
Willy Tarreau69a41fa2014-01-20 11:02:59 +01006021 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].state : 0,
Willy Tarreau15a4dec2014-01-20 11:09:39 +01006022 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].cache : 0,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006023 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].updated : 0);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02006024 }
Willy Tarreau284ddbf2013-12-01 20:45:00 +01006025 else if ((tmpctx = objt_appctx(sess->si[0].end)) != NULL) {
6026 chunk_appendf(&trash,
6027 " app0=%p st0=%d st1=%d st2=%d applet=%s\n",
6028 tmpctx,
6029 tmpctx->st0,
6030 tmpctx->st1,
6031 tmpctx->st2,
6032 tmpctx->applet->name);
6033 }
Willy Tarreaubc174aa2012-11-19 16:10:32 +01006034
Willy Tarreau284ddbf2013-12-01 20:45:00 +01006035 if ((conn = objt_conn(sess->si[1].end)) != NULL) {
Willy Tarreaucf644ed2013-09-29 17:19:56 +02006036 chunk_appendf(&trash,
6037 " co1=%p ctrl=%s xprt=%s data=%s target=%s:%p\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006038 conn,
6039 get_conn_ctrl_name(conn),
6040 get_conn_xprt_name(conn),
6041 get_conn_data_name(conn),
6042 obj_type_name(conn->target),
6043 obj_base_ptr(conn->target));
Willy Tarreau909d5172012-11-26 03:04:41 +01006044
Willy Tarreaucf644ed2013-09-29 17:19:56 +02006045 chunk_appendf(&trash,
Willy Tarreauceeafb52016-01-25 15:27:17 +01006046 " flags=0x%08x fd=%d fd.state=%02x fd.cache=%d updt=%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006047 conn->flags,
6048 conn->t.sock.fd,
Willy Tarreau69a41fa2014-01-20 11:02:59 +01006049 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].state : 0,
Willy Tarreau15a4dec2014-01-20 11:09:39 +01006050 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].cache : 0,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006051 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].updated : 0);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02006052 }
Willy Tarreau284ddbf2013-12-01 20:45:00 +01006053 else if ((tmpctx = objt_appctx(sess->si[1].end)) != NULL) {
6054 chunk_appendf(&trash,
6055 " app1=%p st0=%d st1=%d st2=%d applet=%s\n",
6056 tmpctx,
6057 tmpctx->st0,
6058 tmpctx->st1,
6059 tmpctx->st2,
6060 tmpctx->applet->name);
6061 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006062
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006063 chunk_appendf(&trash,
Willy Tarreau909d5172012-11-26 03:04:41 +01006064 " req=%p (f=0x%06x an=0x%x pipe=%d tofwd=%d total=%lld)\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006065 " an_exp=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006066 &sess->req,
6067 sess->req.flags, sess->req.analysers,
6068 sess->req.pipe ? sess->req.pipe->data : 0,
6069 sess->req.to_forward, sess->req.total,
6070 sess->req.analyse_exp ?
6071 human_time(TICKS_TO_MS(sess->req.analyse_exp - now_ms),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006072 TICKS_TO_MS(1000)) : "<NEVER>");
6073
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006074 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006075 " rex=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006076 sess->req.rex ?
6077 human_time(TICKS_TO_MS(sess->req.rex - now_ms),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006078 TICKS_TO_MS(1000)) : "<NEVER>");
6079
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006080 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006081 " wex=%s\n"
Willy Tarreau909d5172012-11-26 03:04:41 +01006082 " buf=%p data=%p o=%d p=%d req.next=%d i=%d size=%d\n",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006083 sess->req.wex ?
6084 human_time(TICKS_TO_MS(sess->req.wex - now_ms),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006085 TICKS_TO_MS(1000)) : "<NEVER>",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006086 sess->req.buf,
6087 sess->req.buf->data, sess->req.buf->o,
6088 (int)(sess->req.buf->p - sess->req.buf->data),
Willy Tarreaueee5b512015-04-03 23:46:31 +02006089 sess->txn ? sess->txn->req.next : 0, sess->req.buf->i,
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006090 sess->req.buf->size);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006091
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006092 chunk_appendf(&trash,
Willy Tarreau909d5172012-11-26 03:04:41 +01006093 " res=%p (f=0x%06x an=0x%x pipe=%d tofwd=%d total=%lld)\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006094 " an_exp=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006095 &sess->res,
6096 sess->res.flags, sess->res.analysers,
6097 sess->res.pipe ? sess->res.pipe->data : 0,
6098 sess->res.to_forward, sess->res.total,
6099 sess->res.analyse_exp ?
6100 human_time(TICKS_TO_MS(sess->res.analyse_exp - now_ms),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006101 TICKS_TO_MS(1000)) : "<NEVER>");
6102
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006103 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006104 " rex=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006105 sess->res.rex ?
6106 human_time(TICKS_TO_MS(sess->res.rex - now_ms),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006107 TICKS_TO_MS(1000)) : "<NEVER>");
6108
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006109 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006110 " wex=%s\n"
Willy Tarreau909d5172012-11-26 03:04:41 +01006111 " buf=%p data=%p o=%d p=%d rsp.next=%d i=%d size=%d\n",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006112 sess->res.wex ?
6113 human_time(TICKS_TO_MS(sess->res.wex - now_ms),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006114 TICKS_TO_MS(1000)) : "<NEVER>",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006115 sess->res.buf,
6116 sess->res.buf->data, sess->res.buf->o,
6117 (int)(sess->res.buf->p - sess->res.buf->data),
Willy Tarreaueee5b512015-04-03 23:46:31 +02006118 sess->txn ? sess->txn->rsp.next : 0, sess->res.buf->i,
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006119 sess->res.buf->size);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006120
Willy Tarreaubc18da12015-03-13 14:00:47 +01006121 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02006122 si_applet_cant_put(si);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006123 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01006124 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006125
6126 /* use other states to dump the contents */
6127 }
6128 /* end of dump */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006129 appctx->ctx.sess.uid = 0;
6130 appctx->ctx.sess.section = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006131 return 1;
6132}
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006133
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02006134#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
6135static int stats_tlskeys_list(struct stream_interface *si) {
6136 struct appctx *appctx = __objt_appctx(si->end);
6137
6138 switch (appctx->st2) {
6139 case STAT_ST_INIT:
6140 /* Display the column headers. If the message cannot be sent,
6141 * quit the fucntion with returning 0. The function is called
6142 * later and restart at the state "STAT_ST_INIT".
6143 */
6144 chunk_reset(&trash);
William Lallemand1d0b36a2016-05-20 17:40:26 +02006145
6146 if (appctx->st0 == STAT_CLI_O_TLSK_ENT)
6147 chunk_appendf(&trash, "# id secret\n");
6148 else
6149 chunk_appendf(&trash, "# id (file)\n");
6150
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02006151 if (bi_putchk(si_ic(si), &trash) == -1) {
6152 si_applet_cant_put(si);
6153 return 0;
6154 }
6155
William Lallemand1d0b36a2016-05-20 17:40:26 +02006156
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02006157 /* Now, we start the browsing of the references lists.
6158 * Note that the following call to LIST_ELEM return bad pointer. The only
Cyril Bontédb98eb32016-05-06 12:18:50 +02006159 * available field of this pointer is <list>. It is used with the function
6160 * tlskeys_list_get_next() for retruning the first available entry
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02006161 */
William Lallemandcf9e7882016-06-14 17:45:18 +02006162 if (appctx->ctx.tlskeys.ref == NULL) {
6163 appctx->ctx.tlskeys.ref = LIST_ELEM(&tlskeys_reference, struct tls_keys_ref *, list);
6164 appctx->ctx.tlskeys.ref = tlskeys_list_get_next(appctx->ctx.tlskeys.ref, &tlskeys_reference);
William Lallemand1d0b36a2016-05-20 17:40:26 +02006165 }
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02006166
6167 appctx->st2 = STAT_ST_LIST;
6168 /* fall through */
6169
6170 case STAT_ST_LIST:
William Lallemandcf9e7882016-06-14 17:45:18 +02006171 while (appctx->ctx.tlskeys.ref) {
William Lallemand1d0b36a2016-05-20 17:40:26 +02006172 int i;
William Lallemandcf9e7882016-06-14 17:45:18 +02006173 int head = appctx->ctx.tlskeys.ref->tls_ticket_enc_index;
William Lallemand1d0b36a2016-05-20 17:40:26 +02006174
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02006175 chunk_reset(&trash);
William Lallemand1d0b36a2016-05-20 17:40:26 +02006176 if (appctx->st0 == STAT_CLI_O_TLSK_ENT)
6177 chunk_appendf(&trash, "# ");
William Lallemandcf9e7882016-06-14 17:45:18 +02006178 chunk_appendf(&trash, "%d (%s)\n", appctx->ctx.tlskeys.ref->unique_id,
6179 appctx->ctx.tlskeys.ref->filename);
William Lallemand1d0b36a2016-05-20 17:40:26 +02006180 if (appctx->st0 == STAT_CLI_O_TLSK_ENT) {
6181 for (i = 0; i < TLS_TICKETS_NO; i++) {
6182 struct chunk *t2 = get_trash_chunk();
6183 int b64_len;
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02006184
William Lallemand1d0b36a2016-05-20 17:40:26 +02006185 chunk_reset(t2);
William Lallemandcf9e7882016-06-14 17:45:18 +02006186 b64_len = a2base64((char *)(appctx->ctx.tlskeys.ref->tlskeys + (head + 2 + i) % TLS_TICKETS_NO),
William Lallemand1d0b36a2016-05-20 17:40:26 +02006187 sizeof(struct tls_sess_key), t2->str, t2->size);
6188 if (b64_len < 0)
6189 return 0;
6190 t2->len = b64_len;
William Lallemandcf9e7882016-06-14 17:45:18 +02006191 chunk_appendf(&trash, "%d.%d %s\n", appctx->ctx.tlskeys.ref->unique_id, i, t2->str);
William Lallemand1d0b36a2016-05-20 17:40:26 +02006192 }
6193 }
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02006194 if (bi_putchk(si_ic(si), &trash) == -1) {
6195 /* let's try again later from this stream. We add ourselves into
6196 * this stream's users so that it can remove us upon termination.
6197 */
6198 si_applet_cant_put(si);
6199 return 0;
6200 }
6201
William Lallemandcf9e7882016-06-14 17:45:18 +02006202 if (appctx->ctx.tlskeys.dump_all == 0) /* don't display everything if not necessary */
William Lallemand1d0b36a2016-05-20 17:40:26 +02006203 break;
6204
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02006205 /* get next list entry and check the end of the list */
William Lallemandcf9e7882016-06-14 17:45:18 +02006206 appctx->ctx.tlskeys.ref = tlskeys_list_get_next(appctx->ctx.tlskeys.ref, &tlskeys_reference);
William Lallemand1d0b36a2016-05-20 17:40:26 +02006207
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02006208 }
6209
6210 appctx->st2 = STAT_ST_FIN;
6211 /* fall through */
6212
6213 default:
6214 appctx->st2 = STAT_ST_FIN;
6215 return 1;
6216 }
6217 return 0;
6218}
6219#endif
6220
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006221static int stats_pats_list(struct stream_interface *si)
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006222{
6223 struct appctx *appctx = __objt_appctx(si->end);
6224
6225 switch (appctx->st2) {
6226 case STAT_ST_INIT:
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01006227 /* Display the column headers. If the message cannot be sent,
6228 * quit the fucntion with returning 0. The function is called
6229 * later and restart at the state "STAT_ST_INIT".
6230 */
6231 chunk_reset(&trash);
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01006232 chunk_appendf(&trash, "# id (file) description\n");
Willy Tarreaubc18da12015-03-13 14:00:47 +01006233 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02006234 si_applet_cant_put(si);
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01006235 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01006236 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006237
6238 /* Now, we start the browsing of the references lists.
6239 * Note that the following call to LIST_ELEM return bad pointer. The only
Cyril Bontédb98eb32016-05-06 12:18:50 +02006240 * available field of this pointer is <list>. It is used with the function
6241 * pat_list_get_next() for retruning the first available entry
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006242 */
6243 appctx->ctx.map.ref = LIST_ELEM(&pattern_reference, struct pat_ref *, list);
6244 appctx->ctx.map.ref = pat_list_get_next(appctx->ctx.map.ref, &pattern_reference,
6245 appctx->ctx.map.display_flags);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006246 appctx->st2 = STAT_ST_LIST;
6247 /* fall through */
6248
6249 case STAT_ST_LIST:
6250 while (appctx->ctx.map.ref) {
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006251 chunk_reset(&trash);
6252
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006253 /* Build messages. If the reference is used by another category than
6254 * the listed categorie, display the information in the massage.
6255 */
Thierry FOURNIERf7e04e92014-03-20 11:45:47 +01006256 chunk_appendf(&trash, "%d (%s) %s\n", appctx->ctx.map.ref->unique_id,
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01006257 appctx->ctx.map.ref->reference ? appctx->ctx.map.ref->reference : "",
6258 appctx->ctx.map.ref->display);
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01006259
Willy Tarreau2bb4a962014-11-28 11:11:05 +01006260 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau87b09662015-04-03 00:22:06 +02006261 /* let's try again later from this stream. We add ourselves into
6262 * this stream's users so that it can remove us upon termination.
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006263 */
Willy Tarreaufe127932015-04-21 19:23:39 +02006264 si_applet_cant_put(si);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006265 return 0;
6266 }
6267
6268 /* get next list entry and check the end of the list */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006269 appctx->ctx.map.ref = pat_list_get_next(appctx->ctx.map.ref, &pattern_reference,
6270 appctx->ctx.map.display_flags);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006271 }
6272
6273 appctx->st2 = STAT_ST_FIN;
6274 /* fall through */
6275
6276 default:
6277 appctx->st2 = STAT_ST_FIN;
6278 return 1;
6279 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006280 return 0;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006281}
6282
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006283static int stats_map_lookup(struct stream_interface *si)
6284{
6285 struct appctx *appctx = __objt_appctx(si->end);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006286 struct sample sample;
6287 struct pattern *pat;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006288 int match_method;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006289
6290 switch (appctx->st2) {
6291 case STAT_ST_INIT:
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006292 /* Init to the first entry. The list cannot be change */
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01006293 appctx->ctx.map.expr = LIST_ELEM(&appctx->ctx.map.ref->pat, struct pattern_expr *, list);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006294 appctx->ctx.map.expr = pat_expr_get_next(appctx->ctx.map.expr, &appctx->ctx.map.ref->pat);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006295 appctx->st2 = STAT_ST_LIST;
6296 /* fall through */
6297
6298 case STAT_ST_LIST:
6299 /* for each lookup type */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006300 while (appctx->ctx.map.expr) {
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006301 /* initialise chunk to build new message */
6302 chunk_reset(&trash);
6303
6304 /* execute pattern matching */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02006305 sample.data.type = SMP_T_STR;
Andreas Seltenreich9727cf42016-03-03 19:32:25 +01006306 sample.flags = SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02006307 sample.data.u.str.len = appctx->ctx.map.chunk.len;
6308 sample.data.u.str.str = appctx->ctx.map.chunk.str;
Thierry FOURNIER5d344082014-01-27 14:19:53 +01006309 if (appctx->ctx.map.expr->pat_head->match &&
6310 sample_convert(&sample, appctx->ctx.map.expr->pat_head->expect_type))
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006311 pat = appctx->ctx.map.expr->pat_head->match(&sample, appctx->ctx.map.expr, 1);
6312 else
6313 pat = NULL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006314
6315 /* build return message: set type of match */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006316 for (match_method=0; match_method<PAT_MATCH_NUM; match_method++)
6317 if (appctx->ctx.map.expr->pat_head->match == pat_match_fcts[match_method])
6318 break;
6319 if (match_method >= PAT_MATCH_NUM)
6320 chunk_appendf(&trash, "type=unknown(%p)", appctx->ctx.map.expr->pat_head->match);
6321 else
6322 chunk_appendf(&trash, "type=%s", pat_match_names[match_method]);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006323
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02006324 /* case sensitive */
6325 if (appctx->ctx.map.expr->mflags & PAT_MF_IGNORE_CASE)
6326 chunk_appendf(&trash, ", case=insensitive");
6327 else
6328 chunk_appendf(&trash, ", case=sensitive");
6329
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006330 /* Display no match, and set default value */
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01006331 if (!pat) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006332 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
6333 chunk_appendf(&trash, ", found=no");
6334 else
6335 chunk_appendf(&trash, ", match=no");
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006336 }
6337
6338 /* Display match and match info */
6339 else {
6340 /* display match */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006341 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
6342 chunk_appendf(&trash, ", found=yes");
6343 else
6344 chunk_appendf(&trash, ", match=yes");
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006345
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01006346 /* display index mode */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02006347 if (pat->sflags & PAT_SF_TREE)
Thierry FOURNIERb9903842014-03-11 18:48:17 +01006348 chunk_appendf(&trash, ", idx=tree");
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006349 else
Thierry FOURNIERb9903842014-03-11 18:48:17 +01006350 chunk_appendf(&trash, ", idx=list");
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006351
Thierry FOURNIER86db66a2014-03-11 18:37:58 +01006352 /* display pattern */
6353 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
6354 if (pat->ref && pat->ref->pattern)
6355 chunk_appendf(&trash, ", key=\"%s\"", pat->ref->pattern);
6356 else
6357 chunk_appendf(&trash, ", key=unknown");
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006358 }
6359 else {
Thierry FOURNIER86db66a2014-03-11 18:37:58 +01006360 if (pat->ref && pat->ref->pattern)
6361 chunk_appendf(&trash, ", pattern=\"%s\"", pat->ref->pattern);
6362 else
6363 chunk_appendf(&trash, ", pattern=unknown");
6364 }
6365
6366 /* display return value */
6367 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
Thierry FOURNIER503bb092015-08-19 08:35:43 +02006368 if (pat->data && pat->ref && pat->ref->sample)
Thierry FOURNIER03d0e452015-07-23 18:33:41 +02006369 chunk_appendf(&trash, ", value=\"%s\", type=\"%s\"", pat->ref->sample,
Thierry FOURNIER7d4335c2015-08-19 09:05:25 +02006370 smp_to_type[pat->data->type]);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006371 else
Thierry FOURNIER86db66a2014-03-11 18:37:58 +01006372 chunk_appendf(&trash, ", value=none");
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006373 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006374 }
6375
Thierry FOURNIERb9903842014-03-11 18:48:17 +01006376 chunk_appendf(&trash, "\n");
6377
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006378 /* display response */
Willy Tarreau2bb4a962014-11-28 11:11:05 +01006379 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau87b09662015-04-03 00:22:06 +02006380 /* let's try again later from this stream. We add ourselves into
6381 * this stream's users so that it can remove us upon termination.
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006382 */
Willy Tarreaufe127932015-04-21 19:23:39 +02006383 si_applet_cant_put(si);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006384 return 0;
6385 }
6386
6387 /* get next entry */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006388 appctx->ctx.map.expr = pat_expr_get_next(appctx->ctx.map.expr,
6389 &appctx->ctx.map.ref->pat);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006390 }
6391
6392 appctx->st2 = STAT_ST_FIN;
6393 /* fall through */
6394
6395 default:
6396 appctx->st2 = STAT_ST_FIN;
6397 free(appctx->ctx.map.chunk.str);
6398 return 1;
6399 }
6400}
6401
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006402static int stats_pat_list(struct stream_interface *si)
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006403{
6404 struct appctx *appctx = __objt_appctx(si->end);
6405
6406 switch (appctx->st2) {
6407
6408 case STAT_ST_INIT:
6409 /* Init to the first entry. The list cannot be change */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006410 appctx->ctx.map.elt = LIST_NEXT(&appctx->ctx.map.ref->head,
6411 struct pat_ref_elt *, list);
6412 if (&appctx->ctx.map.elt->list == &appctx->ctx.map.ref->head)
6413 appctx->ctx.map.elt = NULL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006414 appctx->st2 = STAT_ST_LIST;
6415 /* fall through */
6416
6417 case STAT_ST_LIST:
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006418 while (appctx->ctx.map.elt) {
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006419 chunk_reset(&trash);
6420
6421 /* build messages */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006422 if (appctx->ctx.map.elt->sample)
Thierry FOURNIER9356c682014-01-28 15:55:37 +01006423 chunk_appendf(&trash, "%p %s %s\n",
6424 appctx->ctx.map.elt, appctx->ctx.map.elt->pattern,
6425 appctx->ctx.map.elt->sample);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006426 else
Thierry FOURNIER9356c682014-01-28 15:55:37 +01006427 chunk_appendf(&trash, "%p %s\n",
6428 appctx->ctx.map.elt, appctx->ctx.map.elt->pattern);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006429
Willy Tarreau2bb4a962014-11-28 11:11:05 +01006430 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau87b09662015-04-03 00:22:06 +02006431 /* let's try again later from this stream. We add ourselves into
6432 * this stream's users so that it can remove us upon termination.
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006433 */
Willy Tarreaufe127932015-04-21 19:23:39 +02006434 si_applet_cant_put(si);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006435 return 0;
6436 }
6437
6438 /* get next list entry and check the end of the list */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006439 appctx->ctx.map.elt = LIST_NEXT(&appctx->ctx.map.elt->list,
6440 struct pat_ref_elt *, list);
6441 if (&appctx->ctx.map.elt->list == &appctx->ctx.map.ref->head)
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006442 break;
6443 }
6444
6445 appctx->st2 = STAT_ST_FIN;
6446 /* fall through */
6447
6448 default:
6449 appctx->st2 = STAT_ST_FIN;
6450 return 1;
6451 }
6452}
6453
Willy Tarreau87b09662015-04-03 00:22:06 +02006454/* This function dumps all streams' states onto the stream interface's
Willy Tarreau306f8302013-07-08 15:53:06 +02006455 * read buffer. It returns 0 if the output buffer is full and it needs
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01006456 * to be called again, otherwise non-zero. It is designed to be called
6457 * from stats_dump_sess_to_buffer() below.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006458 */
Simon Horman9bd2c732011-06-15 15:18:44 +09006459static int stats_dump_sess_to_buffer(struct stream_interface *si)
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006460{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006461 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006462 struct connection *conn;
6463
Willy Tarreau2bb4a962014-11-28 11:11:05 +01006464 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW))) {
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006465 /* If we're forced to shut down, we might have to remove our
Willy Tarreau87b09662015-04-03 00:22:06 +02006466 * reference to the last stream being dumped.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006467 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006468 if (appctx->st2 == STAT_ST_LIST) {
6469 if (!LIST_ISEMPTY(&appctx->ctx.sess.bref.users)) {
6470 LIST_DEL(&appctx->ctx.sess.bref.users);
6471 LIST_INIT(&appctx->ctx.sess.bref.users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +01006472 }
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006473 }
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02006474 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006475 }
6476
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006477 chunk_reset(&trash);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006478
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006479 switch (appctx->st2) {
Willy Tarreau295a8372011-03-10 11:25:07 +01006480 case STAT_ST_INIT:
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006481 /* the function had not been called yet, let's prepare the
Willy Tarreau87b09662015-04-03 00:22:06 +02006482 * buffer for a response. We initialize the current stream
Willy Tarreaufd3828e2009-02-22 15:17:24 +01006483 * pointer to the first in the global list. When a target
Willy Tarreau87b09662015-04-03 00:22:06 +02006484 * stream is being destroyed, it is responsible for updating
Willy Tarreaufd3828e2009-02-22 15:17:24 +01006485 * this pointer. We know we have reached the end when this
Willy Tarreau87b09662015-04-03 00:22:06 +02006486 * pointer points back to the head of the streams list.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006487 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006488 LIST_INIT(&appctx->ctx.sess.bref.users);
Willy Tarreau87b09662015-04-03 00:22:06 +02006489 appctx->ctx.sess.bref.ref = streams.n;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006490 appctx->st2 = STAT_ST_LIST;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006491 /* fall through */
6492
Willy Tarreau295a8372011-03-10 11:25:07 +01006493 case STAT_ST_LIST:
Willy Tarreau87b09662015-04-03 00:22:06 +02006494 /* first, let's detach the back-ref from a possible previous stream */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006495 if (!LIST_ISEMPTY(&appctx->ctx.sess.bref.users)) {
6496 LIST_DEL(&appctx->ctx.sess.bref.users);
6497 LIST_INIT(&appctx->ctx.sess.bref.users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +01006498 }
6499
6500 /* and start from where we stopped */
Willy Tarreau87b09662015-04-03 00:22:06 +02006501 while (appctx->ctx.sess.bref.ref != &streams) {
Cyril Bontéacd7d632010-11-01 19:26:02 +01006502 char pn[INET6_ADDRSTRLEN];
Willy Tarreau87b09662015-04-03 00:22:06 +02006503 struct stream *curr_sess;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006504
Willy Tarreau87b09662015-04-03 00:22:06 +02006505 curr_sess = LIST_ELEM(appctx->ctx.sess.bref.ref, struct stream *, list);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006506
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006507 if (appctx->ctx.sess.target) {
6508 if (appctx->ctx.sess.target != (void *)-1 && appctx->ctx.sess.target != curr_sess)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006509 goto next_sess;
6510
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006511 LIST_ADDQ(&curr_sess->back_refs, &appctx->ctx.sess.bref.users);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006512 /* call the proper dump() function and return if we're missing space */
Willy Tarreau76153662012-11-26 01:16:39 +01006513 if (!stats_dump_full_sess_to_buffer(si, curr_sess))
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006514 return 0;
6515
Willy Tarreau87b09662015-04-03 00:22:06 +02006516 /* stream dump complete */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006517 LIST_DEL(&appctx->ctx.sess.bref.users);
6518 LIST_INIT(&appctx->ctx.sess.bref.users);
6519 if (appctx->ctx.sess.target != (void *)-1) {
6520 appctx->ctx.sess.target = NULL;
Willy Tarreau76153662012-11-26 01:16:39 +01006521 break;
6522 }
6523 else
6524 goto next_sess;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006525 }
6526
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006527 chunk_appendf(&trash,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006528 "%p: proto=%s",
6529 curr_sess,
Willy Tarreauf1e02122015-09-23 12:16:43 +02006530 strm_li(curr_sess) ? strm_li(curr_sess)->proto->name : "?");
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006531
Willy Tarreaud0d8da92015-04-04 02:10:38 +02006532 conn = objt_conn(strm_orig(curr_sess));
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006533 switch (conn ? addr_to_str(&conn->addr.from, pn, sizeof(pn)) : AF_UNSPEC) {
Willy Tarreau631f01c2011-09-05 00:36:48 +02006534 case AF_INET:
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006535 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006536 chunk_appendf(&trash,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006537 " src=%s:%d fe=%s be=%s srv=%s",
6538 pn,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006539 get_host_port(&conn->addr.from),
Willy Tarreaud0d8da92015-04-04 02:10:38 +02006540 strm_fe(curr_sess)->id,
Willy Tarreau50943332011-09-02 17:33:05 +02006541 (curr_sess->be->cap & PR_CAP_BE) ? curr_sess->be->id : "<NONE>",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006542 objt_server(curr_sess->target) ? objt_server(curr_sess->target)->id : "<none>"
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006543 );
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006544 break;
6545 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006546 chunk_appendf(&trash,
Emeric Brun837ca522010-10-22 16:19:01 +02006547 " src=unix:%d fe=%s be=%s srv=%s",
Willy Tarreaud0d8da92015-04-04 02:10:38 +02006548 strm_li(curr_sess)->luid,
6549 strm_fe(curr_sess)->id,
Willy Tarreau50943332011-09-02 17:33:05 +02006550 (curr_sess->be->cap & PR_CAP_BE) ? curr_sess->be->id : "<NONE>",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006551 objt_server(curr_sess->target) ? objt_server(curr_sess->target)->id : "<none>"
Emeric Brun837ca522010-10-22 16:19:01 +02006552 );
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006553 break;
6554 }
6555
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006556 chunk_appendf(&trash,
Willy Tarreau65671ab2009-10-04 14:24:59 +02006557 " ts=%02x age=%s calls=%d",
6558 curr_sess->task->state,
Willy Tarreau3884cba2009-03-28 17:54:35 +01006559 human_time(now.tv_sec - curr_sess->logs.tv_accept.tv_sec, 1),
6560 curr_sess->task->calls);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006561
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006562 chunk_appendf(&trash,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01006563 " rq[f=%06xh,i=%d,an=%02xh,rx=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006564 curr_sess->req.flags,
6565 curr_sess->req.buf->i,
6566 curr_sess->req.analysers,
6567 curr_sess->req.rex ?
6568 human_time(TICKS_TO_MS(curr_sess->req.rex - now_ms),
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006569 TICKS_TO_MS(1000)) : "");
6570
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006571 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006572 ",wx=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006573 curr_sess->req.wex ?
6574 human_time(TICKS_TO_MS(curr_sess->req.wex - now_ms),
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006575 TICKS_TO_MS(1000)) : "");
6576
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006577 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006578 ",ax=%s]",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006579 curr_sess->req.analyse_exp ?
6580 human_time(TICKS_TO_MS(curr_sess->req.analyse_exp - now_ms),
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006581 TICKS_TO_MS(1000)) : "");
6582
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006583 chunk_appendf(&trash,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01006584 " rp[f=%06xh,i=%d,an=%02xh,rx=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006585 curr_sess->res.flags,
6586 curr_sess->res.buf->i,
6587 curr_sess->res.analysers,
6588 curr_sess->res.rex ?
6589 human_time(TICKS_TO_MS(curr_sess->res.rex - now_ms),
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006590 TICKS_TO_MS(1000)) : "");
6591
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006592 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006593 ",wx=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006594 curr_sess->res.wex ?
6595 human_time(TICKS_TO_MS(curr_sess->res.wex - now_ms),
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006596 TICKS_TO_MS(1000)) : "");
6597
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006598 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006599 ",ax=%s]",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006600 curr_sess->res.analyse_exp ?
6601 human_time(TICKS_TO_MS(curr_sess->res.analyse_exp - now_ms),
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006602 TICKS_TO_MS(1000)) : "");
6603
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006604 conn = objt_conn(curr_sess->si[0].end);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006605 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006606 " s0=[%d,%1xh,fd=%d,ex=%s]",
6607 curr_sess->si[0].state,
6608 curr_sess->si[0].flags,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006609 conn ? conn->t.sock.fd : -1,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006610 curr_sess->si[0].exp ?
6611 human_time(TICKS_TO_MS(curr_sess->si[0].exp - now_ms),
6612 TICKS_TO_MS(1000)) : "");
6613
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006614 conn = objt_conn(curr_sess->si[1].end);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006615 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006616 " s1=[%d,%1xh,fd=%d,ex=%s]",
6617 curr_sess->si[1].state,
6618 curr_sess->si[1].flags,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006619 conn ? conn->t.sock.fd : -1,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006620 curr_sess->si[1].exp ?
6621 human_time(TICKS_TO_MS(curr_sess->si[1].exp - now_ms),
6622 TICKS_TO_MS(1000)) : "");
6623
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006624 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006625 " exp=%s",
6626 curr_sess->task->expire ?
6627 human_time(TICKS_TO_MS(curr_sess->task->expire - now_ms),
6628 TICKS_TO_MS(1000)) : "");
Willy Tarreau4726f532009-03-07 17:25:21 +01006629 if (task_in_rq(curr_sess->task))
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006630 chunk_appendf(&trash, " run(nice=%d)", curr_sess->task->nice);
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006631
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006632 chunk_appendf(&trash, "\n");
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006633
Willy Tarreau2bb4a962014-11-28 11:11:05 +01006634 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau87b09662015-04-03 00:22:06 +02006635 /* let's try again later from this stream. We add ourselves into
6636 * this stream's users so that it can remove us upon termination.
Willy Tarreaufd3828e2009-02-22 15:17:24 +01006637 */
Willy Tarreaufe127932015-04-21 19:23:39 +02006638 si_applet_cant_put(si);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006639 LIST_ADDQ(&curr_sess->back_refs, &appctx->ctx.sess.bref.users);
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02006640 return 0;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006641 }
6642
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006643 next_sess:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006644 appctx->ctx.sess.bref.ref = curr_sess->list.n;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006645 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006646
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006647 if (appctx->ctx.sess.target && appctx->ctx.sess.target != (void *)-1) {
Willy Tarreau87b09662015-04-03 00:22:06 +02006648 /* specified stream not found */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006649 if (appctx->ctx.sess.section > 0)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006650 chunk_appendf(&trash, " *** session terminated while we were watching it ***\n");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006651 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006652 chunk_appendf(&trash, "Session not found.\n");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006653
Willy Tarreaubc18da12015-03-13 14:00:47 +01006654 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02006655 si_applet_cant_put(si);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006656 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01006657 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006658
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006659 appctx->ctx.sess.target = NULL;
6660 appctx->ctx.sess.uid = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006661 return 1;
6662 }
6663
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006664 appctx->st2 = STAT_ST_FIN;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006665 /* fall through */
6666
6667 default:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006668 appctx->st2 = STAT_ST_FIN;
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02006669 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006670 }
Emeric Brun1e029aa2010-09-23 18:12:53 +02006671}
6672
Willy Tarreau5f9a8772012-11-26 02:22:40 +01006673/* This is called when the stream interface is closed. For instance, upon an
6674 * external abort, we won't call the i/o handler anymore so we may need to
Willy Tarreau87b09662015-04-03 00:22:06 +02006675 * remove back references to the stream currently being dumped.
Willy Tarreau5f9a8772012-11-26 02:22:40 +01006676 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02006677static void cli_release_handler(struct appctx *appctx)
Willy Tarreau5f9a8772012-11-26 02:22:40 +01006678{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006679 if (appctx->st0 == STAT_CLI_O_SESS && appctx->st2 == STAT_ST_LIST) {
6680 if (!LIST_ISEMPTY(&appctx->ctx.sess.bref.users))
6681 LIST_DEL(&appctx->ctx.sess.bref.users);
Willy Tarreau5f9a8772012-11-26 02:22:40 +01006682 }
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01006683 else if (appctx->st0 == STAT_CLI_PRINT_FREE) {
6684 free(appctx->ctx.cli.err);
Willy Tarreau6457d0f2015-10-07 20:00:24 +02006685 appctx->ctx.cli.err = NULL;
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01006686 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006687 else if (appctx->st0 == STAT_CLI_O_MLOOK) {
6688 free(appctx->ctx.map.chunk.str);
Willy Tarreau6457d0f2015-10-07 20:00:24 +02006689 appctx->ctx.map.chunk.str = NULL;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006690 }
Willy Tarreau5f9a8772012-11-26 02:22:40 +01006691}
6692
Willy Tarreau20e99322013-04-13 09:22:25 +02006693/* This function is used to either dump tables states (when action is set
6694 * to STAT_CLI_O_TAB) or clear tables (when action is STAT_CLI_O_CLR).
Willy Tarreau20e99322013-04-13 09:22:25 +02006695 * It returns 0 if the output buffer is full and it needs to be called
6696 * again, otherwise non-zero.
Willy Tarreau69f58c82010-07-12 17:55:33 +02006697 */
Willy Tarreau20e99322013-04-13 09:22:25 +02006698static int stats_table_request(struct stream_interface *si, int action)
Willy Tarreau69f58c82010-07-12 17:55:33 +02006699{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006700 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau87b09662015-04-03 00:22:06 +02006701 struct stream *s = si_strm(si);
Willy Tarreau69f58c82010-07-12 17:55:33 +02006702 struct ebmb_node *eb;
6703 int dt;
Willy Tarreau44455022012-12-05 23:01:12 +01006704 int skip_entry;
Willy Tarreau20e99322013-04-13 09:22:25 +02006705 int show = action == STAT_CLI_O_TAB;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006706
6707 /*
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006708 * We have 3 possible states in appctx->st2 :
Willy Tarreau295a8372011-03-10 11:25:07 +01006709 * - STAT_ST_INIT : the first call
6710 * - STAT_ST_INFO : the proxy pointer points to the next table to
Willy Tarreau69f58c82010-07-12 17:55:33 +02006711 * dump, the entry pointer is NULL ;
Willy Tarreau295a8372011-03-10 11:25:07 +01006712 * - STAT_ST_LIST : the proxy pointer points to the current table
Willy Tarreau69f58c82010-07-12 17:55:33 +02006713 * and the entry pointer points to the next entry to be dumped,
6714 * and the refcount on the next entry is held ;
Willy Tarreau295a8372011-03-10 11:25:07 +01006715 * - STAT_ST_END : nothing left to dump, the buffer may contain some
Willy Tarreau69f58c82010-07-12 17:55:33 +02006716 * data though.
6717 */
6718
Willy Tarreau2bb4a962014-11-28 11:11:05 +01006719 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW))) {
Willy Tarreau69f58c82010-07-12 17:55:33 +02006720 /* in case of abort, remove any refcount we might have set on an entry */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006721 if (appctx->st2 == STAT_ST_LIST) {
6722 appctx->ctx.table.entry->ref_cnt--;
6723 stksess_kill_if_expired(&appctx->ctx.table.proxy->table, appctx->ctx.table.entry);
Willy Tarreauf6efda12010-08-03 20:34:06 +02006724 }
Willy Tarreau69f58c82010-07-12 17:55:33 +02006725 return 1;
6726 }
6727
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006728 chunk_reset(&trash);
Willy Tarreau69f58c82010-07-12 17:55:33 +02006729
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006730 while (appctx->st2 != STAT_ST_FIN) {
6731 switch (appctx->st2) {
Willy Tarreau295a8372011-03-10 11:25:07 +01006732 case STAT_ST_INIT:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006733 appctx->ctx.table.proxy = appctx->ctx.table.target;
6734 if (!appctx->ctx.table.proxy)
6735 appctx->ctx.table.proxy = proxy;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006736
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006737 appctx->ctx.table.entry = NULL;
6738 appctx->st2 = STAT_ST_INFO;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006739 break;
6740
Willy Tarreau295a8372011-03-10 11:25:07 +01006741 case STAT_ST_INFO:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006742 if (!appctx->ctx.table.proxy ||
6743 (appctx->ctx.table.target &&
6744 appctx->ctx.table.proxy != appctx->ctx.table.target)) {
6745 appctx->st2 = STAT_ST_END;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006746 break;
6747 }
6748
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006749 if (appctx->ctx.table.proxy->table.size) {
6750 if (show && !stats_dump_table_head_to_buffer(&trash, si, appctx->ctx.table.proxy,
6751 appctx->ctx.table.target))
Willy Tarreau69f58c82010-07-12 17:55:33 +02006752 return 0;
6753
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006754 if (appctx->ctx.table.target &&
Willy Tarreaud0d8da92015-04-04 02:10:38 +02006755 strm_li(s)->bind_conf->level >= ACCESS_LVL_OPER) {
Willy Tarreau69f58c82010-07-12 17:55:33 +02006756 /* dump entries only if table explicitly requested */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006757 eb = ebmb_first(&appctx->ctx.table.proxy->table.keys);
Willy Tarreau69f58c82010-07-12 17:55:33 +02006758 if (eb) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006759 appctx->ctx.table.entry = ebmb_entry(eb, struct stksess, key);
6760 appctx->ctx.table.entry->ref_cnt++;
6761 appctx->st2 = STAT_ST_LIST;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006762 break;
6763 }
6764 }
6765 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006766 appctx->ctx.table.proxy = appctx->ctx.table.proxy->next;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006767 break;
6768
Willy Tarreau295a8372011-03-10 11:25:07 +01006769 case STAT_ST_LIST:
Willy Tarreau44455022012-12-05 23:01:12 +01006770 skip_entry = 0;
Simon Hormanc88b8872011-06-15 15:18:49 +09006771
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006772 if (appctx->ctx.table.data_type >= 0) {
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02006773 /* we're filtering on some data contents */
6774 void *ptr;
6775 long long data;
6776
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006777 dt = appctx->ctx.table.data_type;
6778 ptr = stktable_data_ptr(&appctx->ctx.table.proxy->table,
6779 appctx->ctx.table.entry,
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02006780 dt);
6781
6782 data = 0;
6783 switch (stktable_data_types[dt].std_type) {
6784 case STD_T_SINT:
6785 data = stktable_data_cast(ptr, std_t_sint);
6786 break;
6787 case STD_T_UINT:
6788 data = stktable_data_cast(ptr, std_t_uint);
6789 break;
6790 case STD_T_ULL:
6791 data = stktable_data_cast(ptr, std_t_ull);
6792 break;
6793 case STD_T_FRQP:
6794 data = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006795 appctx->ctx.table.proxy->table.data_arg[dt].u);
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02006796 break;
6797 }
6798
6799 /* skip the entry if the data does not match the test and the value */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006800 if ((data < appctx->ctx.table.value &&
6801 (appctx->ctx.table.data_op == STD_OP_EQ ||
6802 appctx->ctx.table.data_op == STD_OP_GT ||
6803 appctx->ctx.table.data_op == STD_OP_GE)) ||
6804 (data == appctx->ctx.table.value &&
6805 (appctx->ctx.table.data_op == STD_OP_NE ||
6806 appctx->ctx.table.data_op == STD_OP_GT ||
6807 appctx->ctx.table.data_op == STD_OP_LT)) ||
6808 (data > appctx->ctx.table.value &&
6809 (appctx->ctx.table.data_op == STD_OP_EQ ||
6810 appctx->ctx.table.data_op == STD_OP_LT ||
6811 appctx->ctx.table.data_op == STD_OP_LE)))
Willy Tarreau44455022012-12-05 23:01:12 +01006812 skip_entry = 1;
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02006813 }
6814
Simon Hormanc88b8872011-06-15 15:18:49 +09006815 if (show && !skip_entry &&
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006816 !stats_dump_table_entry_to_buffer(&trash, si, appctx->ctx.table.proxy,
6817 appctx->ctx.table.entry))
Simon Hormand9366582011-06-15 15:18:45 +09006818 return 0;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006819
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006820 appctx->ctx.table.entry->ref_cnt--;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006821
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006822 eb = ebmb_next(&appctx->ctx.table.entry->key);
Willy Tarreau69f58c82010-07-12 17:55:33 +02006823 if (eb) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006824 struct stksess *old = appctx->ctx.table.entry;
6825 appctx->ctx.table.entry = ebmb_entry(eb, struct stksess, key);
Willy Tarreau8fa52f42012-01-09 11:50:03 +01006826 if (show)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006827 stksess_kill_if_expired(&appctx->ctx.table.proxy->table, old);
6828 else if (!skip_entry && !appctx->ctx.table.entry->ref_cnt)
6829 stksess_kill(&appctx->ctx.table.proxy->table, old);
6830 appctx->ctx.table.entry->ref_cnt++;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006831 break;
6832 }
6833
Simon Hormanc88b8872011-06-15 15:18:49 +09006834
6835 if (show)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006836 stksess_kill_if_expired(&appctx->ctx.table.proxy->table, appctx->ctx.table.entry);
6837 else if (!skip_entry && !appctx->ctx.table.entry->ref_cnt)
6838 stksess_kill(&appctx->ctx.table.proxy->table, appctx->ctx.table.entry);
Simon Hormanc88b8872011-06-15 15:18:49 +09006839
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006840 appctx->ctx.table.proxy = appctx->ctx.table.proxy->next;
6841 appctx->st2 = STAT_ST_INFO;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006842 break;
6843
Willy Tarreau295a8372011-03-10 11:25:07 +01006844 case STAT_ST_END:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006845 appctx->st2 = STAT_ST_FIN;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006846 break;
6847 }
6848 }
6849 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006850}
6851
Willy Tarreaud426a182010-03-05 14:58:26 +01006852/* print a line of text buffer (limited to 70 bytes) to <out>. The format is :
Willy Tarreau74808cb2009-03-04 15:53:18 +01006853 * <2 spaces> <offset=5 digits> <space or plus> <space> <70 chars max> <\n>
6854 * which is 60 chars per line. Non-printable chars \t, \n, \r and \e are
6855 * encoded in C format. Other non-printable chars are encoded "\xHH". Original
6856 * lines are respected within the limit of 70 output chars. Lines that are
6857 * continuation of a previous truncated line begin with "+" instead of " "
6858 * after the offset. The new pointer is returned.
6859 */
Willy Tarreaud426a182010-03-05 14:58:26 +01006860static int dump_text_line(struct chunk *out, const char *buf, int bsize, int len,
6861 int *line, int ptr)
Willy Tarreau74808cb2009-03-04 15:53:18 +01006862{
6863 int end;
6864 unsigned char c;
6865
6866 end = out->len + 80;
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02006867 if (end > out->size)
Willy Tarreau74808cb2009-03-04 15:53:18 +01006868 return ptr;
6869
Willy Tarreau77804732012-10-29 16:14:26 +01006870 chunk_appendf(out, " %05d%c ", ptr, (ptr == *line) ? ' ' : '+');
Willy Tarreau74808cb2009-03-04 15:53:18 +01006871
Willy Tarreaud426a182010-03-05 14:58:26 +01006872 while (ptr < len && ptr < bsize) {
6873 c = buf[ptr];
Willy Tarreau787bbd92009-03-12 08:18:33 +01006874 if (isprint(c) && isascii(c) && c != '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006875 if (out->len > end - 2)
6876 break;
6877 out->str[out->len++] = c;
Willy Tarreau787bbd92009-03-12 08:18:33 +01006878 } else if (c == '\t' || c == '\n' || c == '\r' || c == '\e' || c == '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006879 if (out->len > end - 3)
6880 break;
6881 out->str[out->len++] = '\\';
6882 switch (c) {
6883 case '\t': c = 't'; break;
6884 case '\n': c = 'n'; break;
6885 case '\r': c = 'r'; break;
6886 case '\e': c = 'e'; break;
Willy Tarreau787bbd92009-03-12 08:18:33 +01006887 case '\\': c = '\\'; break;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006888 }
6889 out->str[out->len++] = c;
6890 } else {
6891 if (out->len > end - 5)
6892 break;
6893 out->str[out->len++] = '\\';
6894 out->str[out->len++] = 'x';
6895 out->str[out->len++] = hextab[(c >> 4) & 0xF];
6896 out->str[out->len++] = hextab[c & 0xF];
6897 }
Willy Tarreaud426a182010-03-05 14:58:26 +01006898 if (buf[ptr++] == '\n') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006899 /* we had a line break, let's return now */
6900 out->str[out->len++] = '\n';
6901 *line = ptr;
6902 return ptr;
6903 }
6904 }
6905 /* we have an incomplete line, we return it as-is */
6906 out->str[out->len++] = '\n';
6907 return ptr;
6908}
6909
Baptiste Assmann3863f972015-05-17 00:33:24 +02006910/* This function dumps counters from all resolvers section and associated name servers.
6911 * It returns 0 if the output buffer is full and it needs
6912 * to be called again, otherwise non-zero.
6913 */
6914static int stats_dump_resolvers_to_buffer(struct stream_interface *si)
6915{
6916 struct appctx *appctx = __objt_appctx(si->end);
6917 struct dns_resolvers *presolvers;
6918 struct dns_nameserver *pnameserver;
6919
6920 chunk_reset(&trash);
6921
6922 switch (appctx->st2) {
6923 case STAT_ST_INIT:
6924 appctx->st2 = STAT_ST_LIST; /* let's start producing data */
6925 /* fall through */
6926
6927 case STAT_ST_LIST:
Andrew Hayworth68d05342015-10-02 20:33:01 +00006928 if (LIST_ISEMPTY(&dns_resolvers)) {
6929 chunk_appendf(&trash, "No resolvers found\n");
6930 }
6931 else {
6932 list_for_each_entry(presolvers, &dns_resolvers, list) {
6933 if (appctx->ctx.resolvers.ptr != NULL && appctx->ctx.resolvers.ptr != presolvers)
6934 continue;
6935
6936 chunk_appendf(&trash, "Resolvers section %s\n", presolvers->id);
6937 list_for_each_entry(pnameserver, &presolvers->nameserver_list, list) {
6938 chunk_appendf(&trash, " nameserver %s:\n", pnameserver->id);
6939 chunk_appendf(&trash, " sent: %ld\n", pnameserver->counters.sent);
6940 chunk_appendf(&trash, " valid: %ld\n", pnameserver->counters.valid);
6941 chunk_appendf(&trash, " update: %ld\n", pnameserver->counters.update);
6942 chunk_appendf(&trash, " cname: %ld\n", pnameserver->counters.cname);
6943 chunk_appendf(&trash, " cname_error: %ld\n", pnameserver->counters.cname_error);
6944 chunk_appendf(&trash, " any_err: %ld\n", pnameserver->counters.any_err);
6945 chunk_appendf(&trash, " nx: %ld\n", pnameserver->counters.nx);
6946 chunk_appendf(&trash, " timeout: %ld\n", pnameserver->counters.timeout);
6947 chunk_appendf(&trash, " refused: %ld\n", pnameserver->counters.refused);
6948 chunk_appendf(&trash, " other: %ld\n", pnameserver->counters.other);
6949 chunk_appendf(&trash, " invalid: %ld\n", pnameserver->counters.invalid);
6950 chunk_appendf(&trash, " too_big: %ld\n", pnameserver->counters.too_big);
6951 chunk_appendf(&trash, " truncated: %ld\n", pnameserver->counters.truncated);
6952 chunk_appendf(&trash, " outdated: %ld\n", pnameserver->counters.outdated);
6953 }
6954 }
Baptiste Assmann3863f972015-05-17 00:33:24 +02006955 }
6956
6957 /* display response */
6958 if (bi_putchk(si_ic(si), &trash) == -1) {
6959 /* let's try again later from this session. We add ourselves into
6960 * this session's users so that it can remove us upon termination.
6961 */
6962 si->flags |= SI_FL_WAIT_ROOM;
6963 return 0;
6964 }
6965
6966 appctx->st2 = STAT_ST_FIN;
6967 /* fall through */
6968
6969 default:
6970 appctx->st2 = STAT_ST_FIN;
6971 return 1;
6972 }
6973}
6974
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02006975/* This function dumps all captured errors onto the stream interface's
Willy Tarreau306f8302013-07-08 15:53:06 +02006976 * read buffer. It returns 0 if the output buffer is full and it needs
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01006977 * to be called again, otherwise non-zero.
Willy Tarreau74808cb2009-03-04 15:53:18 +01006978 */
Simon Horman9bd2c732011-06-15 15:18:44 +09006979static int stats_dump_errors_to_buffer(struct stream_interface *si)
Willy Tarreau74808cb2009-03-04 15:53:18 +01006980{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006981 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau74808cb2009-03-04 15:53:18 +01006982 extern const char *monthname[12];
Willy Tarreau74808cb2009-03-04 15:53:18 +01006983
Willy Tarreau2bb4a962014-11-28 11:11:05 +01006984 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
Willy Tarreau61b34732009-10-03 23:49:35 +02006985 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006986
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006987 chunk_reset(&trash);
Willy Tarreau74808cb2009-03-04 15:53:18 +01006988
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006989 if (!appctx->ctx.errors.px) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006990 /* the function had not been called yet, let's prepare the
6991 * buffer for a response.
6992 */
Willy Tarreau10479e42010-12-12 14:00:34 +01006993 struct tm tm;
6994
6995 get_localtime(date.tv_sec, &tm);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006996 chunk_appendf(&trash, "Total events captured on [%02d/%s/%04d:%02d:%02d:%02d.%03d] : %u\n",
Willy Tarreau10479e42010-12-12 14:00:34 +01006997 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
6998 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(date.tv_usec/1000),
6999 error_snapshot_id);
7000
Willy Tarreau2bb4a962014-11-28 11:11:05 +01007001 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau10479e42010-12-12 14:00:34 +01007002 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreaufe127932015-04-21 19:23:39 +02007003 si_applet_cant_put(si);
Willy Tarreau10479e42010-12-12 14:00:34 +01007004 return 0;
7005 }
7006
Willy Tarreau7b4b4992013-12-01 09:15:12 +01007007 appctx->ctx.errors.px = proxy;
7008 appctx->ctx.errors.buf = 0;
7009 appctx->ctx.errors.bol = 0;
7010 appctx->ctx.errors.ptr = -1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01007011 }
7012
7013 /* we have two inner loops here, one for the proxy, the other one for
7014 * the buffer.
7015 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01007016 while (appctx->ctx.errors.px) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01007017 struct error_snapshot *es;
7018
Willy Tarreau7b4b4992013-12-01 09:15:12 +01007019 if (appctx->ctx.errors.buf == 0)
7020 es = &appctx->ctx.errors.px->invalid_req;
Willy Tarreau74808cb2009-03-04 15:53:18 +01007021 else
Willy Tarreau7b4b4992013-12-01 09:15:12 +01007022 es = &appctx->ctx.errors.px->invalid_rep;
Willy Tarreau74808cb2009-03-04 15:53:18 +01007023
7024 if (!es->when.tv_sec)
7025 goto next;
7026
Willy Tarreau7b4b4992013-12-01 09:15:12 +01007027 if (appctx->ctx.errors.iid >= 0 &&
7028 appctx->ctx.errors.px->uuid != appctx->ctx.errors.iid &&
7029 es->oe->uuid != appctx->ctx.errors.iid)
Willy Tarreau74808cb2009-03-04 15:53:18 +01007030 goto next;
7031
Willy Tarreau7b4b4992013-12-01 09:15:12 +01007032 if (appctx->ctx.errors.ptr < 0) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01007033 /* just print headers now */
7034
7035 char pn[INET6_ADDRSTRLEN];
7036 struct tm tm;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02007037 int port;
Willy Tarreau74808cb2009-03-04 15:53:18 +01007038
7039 get_localtime(es->when.tv_sec, &tm);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007040 chunk_appendf(&trash, " \n[%02d/%s/%04d:%02d:%02d:%02d.%03d]",
Willy Tarreau74808cb2009-03-04 15:53:18 +01007041 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
Willy Tarreau1772ece2009-04-03 14:49:12 +02007042 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(es->when.tv_usec/1000));
Willy Tarreau74808cb2009-03-04 15:53:18 +01007043
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02007044 switch (addr_to_str(&es->src, pn, sizeof(pn))) {
7045 case AF_INET:
7046 case AF_INET6:
7047 port = get_host_port(&es->src);
7048 break;
7049 default:
7050 port = 0;
7051 }
7052
Willy Tarreau7b4b4992013-12-01 09:15:12 +01007053 switch (appctx->ctx.errors.buf) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01007054 case 0:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007055 chunk_appendf(&trash,
Willy Tarreau74808cb2009-03-04 15:53:18 +01007056 " frontend %s (#%d): invalid request\n"
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02007057 " backend %s (#%d)",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01007058 appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02007059 (es->oe->cap & PR_CAP_BE) ? es->oe->id : "<NONE>",
7060 (es->oe->cap & PR_CAP_BE) ? es->oe->uuid : -1);
Willy Tarreau74808cb2009-03-04 15:53:18 +01007061 break;
7062 case 1:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007063 chunk_appendf(&trash,
Olivier Doucet08afdcb2014-09-08 11:23:00 +02007064 " backend %s (#%d): invalid response\n"
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02007065 " frontend %s (#%d)",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01007066 appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02007067 es->oe->id, es->oe->uuid);
Willy Tarreau74808cb2009-03-04 15:53:18 +01007068 break;
7069 }
7070
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007071 chunk_appendf(&trash,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02007072 ", server %s (#%d), event #%u\n"
7073 " src %s:%d, session #%d, session flags 0x%08x\n"
7074 " HTTP msg state %d, msg flags 0x%08x, tx flags 0x%08x\n"
7075 " HTTP chunk len %lld bytes, HTTP body len %lld bytes\n"
7076 " buffer flags 0x%08x, out %d bytes, total %lld bytes\n"
7077 " pending %d bytes, wrapping at %d, error at position %d:\n \n",
7078 es->srv ? es->srv->id : "<NONE>", es->srv ? es->srv->puid : -1,
7079 es->ev_id,
7080 pn, port, es->sid, es->s_flags,
7081 es->state, es->m_flags, es->t_flags,
7082 es->m_clen, es->m_blen,
7083 es->b_flags, es->b_out, es->b_tot,
7084 es->len, es->b_wrap, es->pos);
7085
Willy Tarreau2bb4a962014-11-28 11:11:05 +01007086 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01007087 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreaufe127932015-04-21 19:23:39 +02007088 si_applet_cant_put(si);
Willy Tarreau61b34732009-10-03 23:49:35 +02007089 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01007090 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01007091 appctx->ctx.errors.ptr = 0;
7092 appctx->ctx.errors.sid = es->sid;
Willy Tarreau74808cb2009-03-04 15:53:18 +01007093 }
7094
Willy Tarreau7b4b4992013-12-01 09:15:12 +01007095 if (appctx->ctx.errors.sid != es->sid) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01007096 /* the snapshot changed while we were dumping it */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007097 chunk_appendf(&trash,
Willy Tarreau74808cb2009-03-04 15:53:18 +01007098 " WARNING! update detected on this snapshot, dump interrupted. Please re-check!\n");
Willy Tarreaubc18da12015-03-13 14:00:47 +01007099 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02007100 si_applet_cant_put(si);
Willy Tarreau61b34732009-10-03 23:49:35 +02007101 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01007102 }
Willy Tarreau74808cb2009-03-04 15:53:18 +01007103 goto next;
7104 }
7105
7106 /* OK, ptr >= 0, so we have to dump the current line */
Willy Tarreauf3764b72016-03-31 13:45:10 +02007107 while (es->buf && appctx->ctx.errors.ptr < es->len && appctx->ctx.errors.ptr < global.tune.bufsize) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01007108 int newptr;
7109 int newline;
7110
Willy Tarreau7b4b4992013-12-01 09:15:12 +01007111 newline = appctx->ctx.errors.bol;
Willy Tarreauf3764b72016-03-31 13:45:10 +02007112 newptr = dump_text_line(&trash, es->buf, global.tune.bufsize, es->len, &newline, appctx->ctx.errors.ptr);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01007113 if (newptr == appctx->ctx.errors.ptr)
Willy Tarreau61b34732009-10-03 23:49:35 +02007114 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01007115
Willy Tarreau2bb4a962014-11-28 11:11:05 +01007116 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01007117 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreaufe127932015-04-21 19:23:39 +02007118 si_applet_cant_put(si);
Willy Tarreau61b34732009-10-03 23:49:35 +02007119 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01007120 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01007121 appctx->ctx.errors.ptr = newptr;
7122 appctx->ctx.errors.bol = newline;
Willy Tarreau74808cb2009-03-04 15:53:18 +01007123 };
7124 next:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01007125 appctx->ctx.errors.bol = 0;
7126 appctx->ctx.errors.ptr = -1;
7127 appctx->ctx.errors.buf++;
7128 if (appctx->ctx.errors.buf > 1) {
7129 appctx->ctx.errors.buf = 0;
7130 appctx->ctx.errors.px = appctx->ctx.errors.px->next;
Willy Tarreau74808cb2009-03-04 15:53:18 +01007131 }
Willy Tarreauae795722016-02-16 11:27:28 +01007132 }
7133
7134 /* dump complete */
7135 return 1;
7136}
7137
7138/* This function dumps all environmnent variables to the buffer. It returns 0
7139 * if the output buffer is full and it needs to be called again, otherwise
7140 * non-zero. Dumps only one entry if st2 == STAT_ST_END.
7141 */
7142static int stats_dump_env_to_buffer(struct stream_interface *si)
7143{
7144 struct appctx *appctx = __objt_appctx(si->end);
7145
7146 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
7147 return 1;
7148
7149 chunk_reset(&trash);
7150
7151 /* we have two inner loops here, one for the proxy, the other one for
7152 * the buffer.
7153 */
7154 while (*appctx->ctx.env.var) {
7155 chunk_printf(&trash, "%s\n", *appctx->ctx.env.var);
7156
7157 if (bi_putchk(si_ic(si), &trash) == -1) {
7158 si_applet_cant_put(si);
7159 return 0;
7160 }
7161 if (appctx->st2 == STAT_ST_END)
7162 break;
7163 appctx->ctx.env.var++;
Willy Tarreau74808cb2009-03-04 15:53:18 +01007164 }
7165
7166 /* dump complete */
Willy Tarreau61b34732009-10-03 23:49:35 +02007167 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01007168}
7169
Willy Tarreaud5781202012-09-22 19:32:35 +02007170/* parse the "level" argument on the bind lines */
7171static int bind_parse_level(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
7172{
7173 if (!*args[cur_arg + 1]) {
7174 memprintf(err, "'%s' : missing level", args[cur_arg]);
7175 return ERR_ALERT | ERR_FATAL;
7176 }
7177
7178 if (!strcmp(args[cur_arg+1], "user"))
7179 conf->level = ACCESS_LVL_USER;
7180 else if (!strcmp(args[cur_arg+1], "operator"))
7181 conf->level = ACCESS_LVL_OPER;
7182 else if (!strcmp(args[cur_arg+1], "admin"))
7183 conf->level = ACCESS_LVL_ADMIN;
7184 else {
7185 memprintf(err, "'%s' only supports 'user', 'operator', and 'admin' (got '%s')",
7186 args[cur_arg], args[cur_arg+1]);
7187 return ERR_ALERT | ERR_FATAL;
7188 }
7189
7190 return 0;
7191}
7192
Willy Tarreau30576452015-04-13 13:50:30 +02007193struct applet http_stats_applet = {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007194 .obj_type = OBJ_TYPE_APPLET,
Willy Tarreaub24281b2011-02-13 13:16:36 +01007195 .name = "<STATS>", /* used for logging */
7196 .fct = http_stats_io_handler,
Aman Gupta9a13e842012-04-02 18:57:53 -07007197 .release = NULL,
Willy Tarreaub24281b2011-02-13 13:16:36 +01007198};
7199
Willy Tarreau30576452015-04-13 13:50:30 +02007200static struct applet cli_applet = {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007201 .obj_type = OBJ_TYPE_APPLET,
Willy Tarreaub24281b2011-02-13 13:16:36 +01007202 .name = "<CLI>", /* used for logging */
7203 .fct = cli_io_handler,
Willy Tarreau5f9a8772012-11-26 02:22:40 +01007204 .release = cli_release_handler,
Willy Tarreaub24281b2011-02-13 13:16:36 +01007205};
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01007206
Willy Tarreaudc13c112013-06-21 23:16:39 +02007207static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreau10522fd2008-07-09 20:12:41 +02007208 { CFG_GLOBAL, "stats", stats_parse_global },
7209 { 0, NULL, NULL },
7210}};
7211
Willy Tarreaud5781202012-09-22 19:32:35 +02007212static struct bind_kw_list bind_kws = { "STAT", { }, {
7213 { "level", bind_parse_level, 1 }, /* set the unix socket admin level */
7214 { NULL, NULL, 0 },
7215}};
7216
Willy Tarreau10522fd2008-07-09 20:12:41 +02007217__attribute__((constructor))
7218static void __dumpstats_module_init(void)
7219{
7220 cfg_register_keywords(&cfg_kws);
Willy Tarreaud5781202012-09-22 19:32:35 +02007221 bind_register_keywords(&bind_kws);
Willy Tarreau10522fd2008-07-09 20:12:41 +02007222}
7223
Willy Tarreau91861262007-10-17 17:06:05 +02007224/*
7225 * Local variables:
7226 * c-indent-level: 8
7227 * c-basic-offset: 8
7228 * End:
7229 */