blob: c88d0acbd039608216740d325ae257f32592e117 [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
Willy Tarreaua58c4352016-06-22 14:51:40 +02003203 if (appctx->st2 == STAT_ST_INIT) {
3204 if (!appctx->ctx.server_state.px)
3205 appctx->ctx.server_state.px = proxy;
3206 appctx->st2 = STAT_ST_HEAD;
3207 }
3208
3209 if (appctx->st2 == STAT_ST_HEAD) {
Cyril Bonté76a99782016-05-06 12:18:48 +02003210 chunk_printf(&trash, "%d\n# %s\n", SRV_STATE_FILE_VERSION, SRV_STATE_FILE_FIELD_NAMES);
3211 if (bi_putchk(si_ic(si), &trash) == -1) {
3212 si_applet_cant_put(si);
3213 return 0;
Baptiste Assmann28289462015-07-03 08:01:20 +02003214 }
Willy Tarreaua58c4352016-06-22 14:51:40 +02003215 appctx->st2 = STAT_ST_INFO;
Baptiste Assmann28289462015-07-03 08:01:20 +02003216 }
3217
Willy Tarreaua58c4352016-06-22 14:51:40 +02003218 /* STAT_ST_INFO */
Cyril Bonté76a99782016-05-06 12:18:48 +02003219 for (; appctx->ctx.server_state.px != NULL; appctx->ctx.server_state.px = curproxy->next) {
3220 curproxy = appctx->ctx.server_state.px;
Cyril Bonté76a99782016-05-06 12:18:48 +02003221 /* servers are only in backends */
3222 if (curproxy->cap & PR_CAP_BE) {
3223 if (!dump_servers_state(si, &trash))
3224 return 0;
3225
3226 if (bi_putchk(si_ic(si), &trash) == -1) {
3227 si_applet_cant_put(si);
3228 return 0;
3229 }
3230 }
3231 /* only the selected proxy is dumped */
3232 if (appctx->ctx.server_state.iid)
3233 break;
Baptiste Assmann28289462015-07-03 08:01:20 +02003234 }
3235
3236 return 1;
3237}
3238
Willy Tarreau12833bb2014-01-28 16:49:56 +01003239/* This function dumps memory usage information onto the stream interface's
3240 * read buffer. It returns 0 as long as it does not complete, non-zero upon
3241 * completion. No state is used.
3242 */
3243static int stats_dump_pools_to_buffer(struct stream_interface *si)
3244{
3245 dump_pools_to_trash();
Willy Tarreaubc18da12015-03-13 14:00:47 +01003246 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02003247 si_applet_cant_put(si);
Willy Tarreau12833bb2014-01-28 16:49:56 +01003248 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01003249 }
Willy Tarreau82a86022016-01-04 19:04:18 +01003250 return 1;
3251}
3252
3253/* Dump all fields from <stats> into <out> using CSV format */
3254static int stats_dump_fields_csv(struct chunk *out, const struct field *stats)
3255{
3256 int field;
3257
3258 for (field = 0; field < ST_F_TOTAL_FIELDS; field++) {
3259 if (!stats_emit_raw_data_field(out, &stats[field]))
3260 return 0;
3261 if (!chunk_strcat(out, ","))
3262 return 0;
3263 }
Conrad Hoffmann692c9382016-04-01 20:40:58 +02003264 chunk_strcat(out, "\n");
Willy Tarreau12833bb2014-01-28 16:49:56 +01003265 return 1;
3266}
3267
Willy Tarreau1e62df92016-01-11 18:57:53 +01003268/* Dump all fields from <stats> into <out> using a typed "field:desc:type:value" format */
3269static int stats_dump_fields_typed(struct chunk *out, const struct field *stats)
3270{
3271 int field;
3272
3273 for (field = 0; field < ST_F_TOTAL_FIELDS; field++) {
3274 if (!stats[field].type)
3275 continue;
3276
3277 chunk_appendf(out, "%c.%u.%u.%d.%s.%u:",
3278 stats[ST_F_TYPE].u.u32 == STATS_TYPE_FE ? 'F' :
3279 stats[ST_F_TYPE].u.u32 == STATS_TYPE_BE ? 'B' :
3280 stats[ST_F_TYPE].u.u32 == STATS_TYPE_SO ? 'L' :
3281 stats[ST_F_TYPE].u.u32 == STATS_TYPE_SV ? 'S' :
3282 '?',
3283 stats[ST_F_IID].u.u32, stats[ST_F_SID].u.u32,
3284 field, stat_field_names[field], stats[ST_F_PID].u.u32);
3285
3286 if (!stats_emit_field_tags(out, &stats[field], ':'))
3287 return 0;
3288 if (!stats_emit_typed_data_field(out, &stats[field]))
3289 return 0;
3290 if (!chunk_strcat(out, "\n"))
3291 return 0;
3292 }
3293 return 1;
3294}
3295
Willy Tarreau60600742016-01-11 15:32:30 +01003296/* Dump all fields from <stats> into <out> using the HTML format. A column is
Willy Tarreau508a63f2016-01-11 15:28:40 +01003297 * reserved for the checkbox is ST_SHOWADMIN is set in <flags>. Some extra info
3298 * are provided if ST_SHLGNDS is present in <flags>.
Cyril Bonté70be45d2010-10-12 00:14:35 +02003299 */
Willy Tarreau60600742016-01-11 15:32:30 +01003300static int stats_dump_fields_html(struct chunk *out, const struct field *stats, unsigned int flags)
Cyril Bonté70be45d2010-10-12 00:14:35 +02003301{
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003302 struct chunk src;
3303
Willy Tarreaub5f66b82016-01-08 17:15:39 +01003304 if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_FE) {
Willy Tarreau60600742016-01-11 15:32:30 +01003305 chunk_appendf(out,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003306 /* name, queue */
3307 "<tr class=\"frontend\">");
Cyril Bonté70be45d2010-10-12 00:14:35 +02003308
Willy Tarreau508a63f2016-01-11 15:28:40 +01003309 if (flags & ST_SHOWADMIN) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003310 /* Column sub-heading for Enable or Disable server */
Willy Tarreau60600742016-01-11 15:32:30 +01003311 chunk_appendf(out, "<td></td>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003312 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02003313
Willy Tarreau60600742016-01-11 15:32:30 +01003314 chunk_appendf(out,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003315 "<td class=ac>"
3316 "<a name=\"%s/Frontend\"></a>"
3317 "<a class=lfsb href=\"#%s/Frontend\">Frontend</a></td>"
3318 "<td colspan=3></td>"
3319 "",
Willy Tarreaud72c9172016-01-06 15:26:40 +01003320 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_PXNAME));
Cyril Bonté70be45d2010-10-12 00:14:35 +02003321
Willy Tarreau60600742016-01-11 15:32:30 +01003322 chunk_appendf(out,
Willy Tarreau466c9b52012-12-23 02:25:03 +01003323 /* sessions rate : current */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003324 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01003325 "<tr><th>Current connection rate:</th><td>%s/s</td></tr>"
3326 "<tr><th>Current session rate:</th><td>%s/s</td></tr>"
3327 "",
Willy Tarreaud72c9172016-01-06 15:26:40 +01003328 U2H(stats[ST_F_RATE].u.u32),
Willy Tarreauc73810f2016-01-11 13:52:04 +01003329 U2H(stats[ST_F_CONN_RATE].u.u32),
Willy Tarreaud72c9172016-01-06 15:26:40 +01003330 U2H(stats[ST_F_RATE].u.u32));
Willy Tarreau466c9b52012-12-23 02:25:03 +01003331
Willy Tarreauf8211df2016-01-11 14:09:38 +01003332 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
Willy Tarreau60600742016-01-11 15:32:30 +01003333 chunk_appendf(out,
Willy Tarreau466c9b52012-12-23 02:25:03 +01003334 "<tr><th>Current request rate:</th><td>%s/s</td></tr>",
Willy Tarreaud72c9172016-01-06 15:26:40 +01003335 U2H(stats[ST_F_REQ_RATE].u.u32));
Willy Tarreau466c9b52012-12-23 02:25:03 +01003336
Willy Tarreau60600742016-01-11 15:32:30 +01003337 chunk_appendf(out,
Willy Tarreau466c9b52012-12-23 02:25:03 +01003338 "</table></div></u></td>"
3339 /* sessions rate : max */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003340 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01003341 "<tr><th>Max connection rate:</th><td>%s/s</td></tr>"
3342 "<tr><th>Max session rate:</th><td>%s/s</td></tr>"
3343 "",
Willy Tarreaud72c9172016-01-06 15:26:40 +01003344 U2H(stats[ST_F_RATE_MAX].u.u32),
Willy Tarreauc73810f2016-01-11 13:52:04 +01003345 U2H(stats[ST_F_CONN_RATE_MAX].u.u32),
Willy Tarreaud72c9172016-01-06 15:26:40 +01003346 U2H(stats[ST_F_RATE_MAX].u.u32));
Willy Tarreau466c9b52012-12-23 02:25:03 +01003347
Willy Tarreauf8211df2016-01-11 14:09:38 +01003348 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
Willy Tarreau60600742016-01-11 15:32:30 +01003349 chunk_appendf(out,
Willy Tarreau466c9b52012-12-23 02:25:03 +01003350 "<tr><th>Max request rate:</th><td>%s/s</td></tr>",
Willy Tarreaud72c9172016-01-06 15:26:40 +01003351 U2H(stats[ST_F_REQ_RATE_MAX].u.u32));
Willy Tarreau466c9b52012-12-23 02:25:03 +01003352
Willy Tarreau60600742016-01-11 15:32:30 +01003353 chunk_appendf(out,
Willy Tarreau466c9b52012-12-23 02:25:03 +01003354 "</table></div></u></td>"
3355 /* sessions rate : limit */
3356 "<td>%s</td>",
Willy Tarreaud72c9172016-01-06 15:26:40 +01003357 LIM2A(stats[ST_F_RATE_LIM].u.u32, "-"));
Cyril Bonté70be45d2010-10-12 00:14:35 +02003358
Willy Tarreau60600742016-01-11 15:32:30 +01003359 chunk_appendf(out,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003360 /* sessions: current, max, limit, total */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003361 "<td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003362 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01003363 "<tr><th>Cum. connections:</th><td>%s</td></tr>"
3364 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003365 "",
Willy Tarreauc73810f2016-01-11 13:52:04 +01003366 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 +01003367 U2H(stats[ST_F_STOT].u.u64),
Willy Tarreauc73810f2016-01-11 13:52:04 +01003368 U2H(stats[ST_F_CONN_TOT].u.u64),
Willy Tarreaud72c9172016-01-06 15:26:40 +01003369 U2H(stats[ST_F_STOT].u.u64));
Cyril Bonté70be45d2010-10-12 00:14:35 +02003370
Willy Tarreau466c9b52012-12-23 02:25:03 +01003371 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
Willy Tarreauf8211df2016-01-11 14:09:38 +01003372 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
Willy Tarreau60600742016-01-11 15:32:30 +01003373 chunk_appendf(out,
Willy Tarreau466c9b52012-12-23 02:25:03 +01003374 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
3375 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
3376 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
3377 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
3378 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
3379 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
3380 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
3381 "<tr><th>- other responses:</th><td>%s</td></tr>"
3382 "<tr><th>Intercepted requests:</th><td>%s</td></tr>"
3383 "",
Willy Tarreaud72c9172016-01-06 15:26:40 +01003384 U2H(stats[ST_F_REQ_TOT].u.u64),
3385 U2H(stats[ST_F_HRSP_1XX].u.u64),
3386 U2H(stats[ST_F_HRSP_2XX].u.u64),
3387 U2H(stats[ST_F_COMP_RSP].u.u64),
3388 stats[ST_F_HRSP_2XX].u.u64 ?
3389 (int)(100 * stats[ST_F_COMP_RSP].u.u64 / stats[ST_F_HRSP_2XX].u.u64) : 0,
3390 U2H(stats[ST_F_HRSP_3XX].u.u64),
3391 U2H(stats[ST_F_HRSP_4XX].u.u64),
3392 U2H(stats[ST_F_HRSP_5XX].u.u64),
3393 U2H(stats[ST_F_HRSP_OTHER].u.u64),
Willy Tarreau5b9bdff2016-01-11 14:40:47 +01003394 U2H(stats[ST_F_INTERCEPTED].u.u64));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003395 }
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02003396
Willy Tarreau60600742016-01-11 15:32:30 +01003397 chunk_appendf(out,
Willy Tarreau466c9b52012-12-23 02:25:03 +01003398 "</table></div></u></td>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05003399 /* sessions: lbtot, lastsess */
3400 "<td></td><td></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003401 /* bytes : in */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003402 "<td>%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003403 "",
Willy Tarreaud72c9172016-01-06 15:26:40 +01003404 U2H(stats[ST_F_BIN].u.u64));
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02003405
Willy Tarreau60600742016-01-11 15:32:30 +01003406 chunk_appendf(out,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003407 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
Willy Tarreau4e5d58e2015-07-04 14:35:15 +02003408 "<td>%s%s<div class=tips><table class=det>"
3409 "<tr><th>Response bytes in:</th><td>%s</td></tr>"
3410 "<tr><th>Compression in:</th><td>%s</td></tr>"
3411 "<tr><th>Compression out:</th><td>%s</td><td>(%d%%)</td></tr>"
3412 "<tr><th>Compression bypass:</th><td>%s</td></tr>"
3413 "<tr><th>Total bytes saved:</th><td>%s</td><td>(%d%%)</td></tr>"
3414 "</table></div>%s</td>",
Willy Tarreaud72c9172016-01-06 15:26:40 +01003415 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "<u>":"",
3416 U2H(stats[ST_F_BOUT].u.u64),
3417 U2H(stats[ST_F_BOUT].u.u64),
3418 U2H(stats[ST_F_COMP_IN].u.u64),
3419 U2H(stats[ST_F_COMP_OUT].u.u64),
3420 stats[ST_F_COMP_IN].u.u64 ? (int)(stats[ST_F_COMP_OUT].u.u64 * 100 / stats[ST_F_COMP_IN].u.u64) : 0,
3421 U2H(stats[ST_F_COMP_BYP].u.u64),
3422 U2H(stats[ST_F_COMP_IN].u.u64 - stats[ST_F_COMP_OUT].u.u64),
3423 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,
3424 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "</u>":"");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02003425
Willy Tarreau60600742016-01-11 15:32:30 +01003426 chunk_appendf(out,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003427 /* denied: req, resp */
3428 "<td>%s</td><td>%s</td>"
3429 /* errors : request, connect, response */
3430 "<td>%s</td><td></td><td></td>"
3431 /* warnings: retries, redispatches */
3432 "<td></td><td></td>"
3433 /* server status : reflect frontend status */
3434 "<td class=ac>%s</td>"
3435 /* rest of server: nothing */
3436 "<td class=ac colspan=8></td></tr>"
3437 "",
Willy Tarreaud72c9172016-01-06 15:26:40 +01003438 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
3439 U2H(stats[ST_F_EREQ].u.u64),
3440 field_str(stats, ST_F_STATUS));
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02003441 }
Willy Tarreaufa9512f2016-01-08 17:20:51 +01003442 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_SO) {
Willy Tarreau60600742016-01-11 15:32:30 +01003443 chunk_appendf(out, "<tr class=socket>");
Willy Tarreau508a63f2016-01-11 15:28:40 +01003444 if (flags & ST_SHOWADMIN) {
Willy Tarreaufa9512f2016-01-08 17:20:51 +01003445 /* Column sub-heading for Enable or Disable server */
Willy Tarreau60600742016-01-11 15:32:30 +01003446 chunk_appendf(out, "<td></td>");
Willy Tarreaufa9512f2016-01-08 17:20:51 +01003447 }
3448
Willy Tarreau60600742016-01-11 15:32:30 +01003449 chunk_appendf(out,
Willy Tarreaufa9512f2016-01-08 17:20:51 +01003450 /* frontend name, listener name */
3451 "<td class=ac><a name=\"%s/+%s\"></a>%s"
3452 "<a class=lfsb href=\"#%s/+%s\">%s</a>"
3453 "",
3454 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME),
3455 (flags & ST_SHLGNDS)?"<u>":"",
3456 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME), field_str(stats, ST_F_SVNAME));
3457
3458 if (flags & ST_SHLGNDS) {
Willy Tarreau60600742016-01-11 15:32:30 +01003459 chunk_appendf(out, "<div class=tips>");
Willy Tarreaufa9512f2016-01-08 17:20:51 +01003460
3461 if (isdigit(*field_str(stats, ST_F_ADDR)))
Willy Tarreau60600742016-01-11 15:32:30 +01003462 chunk_appendf(out, "IPv4: %s, ", field_str(stats, ST_F_ADDR));
Willy Tarreaufa9512f2016-01-08 17:20:51 +01003463 else if (*field_str(stats, ST_F_ADDR) == '[')
Willy Tarreau60600742016-01-11 15:32:30 +01003464 chunk_appendf(out, "IPv6: %s, ", field_str(stats, ST_F_ADDR));
Willy Tarreaufa9512f2016-01-08 17:20:51 +01003465 else if (*field_str(stats, ST_F_ADDR))
Willy Tarreau60600742016-01-11 15:32:30 +01003466 chunk_appendf(out, "%s, ", field_str(stats, ST_F_ADDR));
Willy Tarreaufa9512f2016-01-08 17:20:51 +01003467
3468 /* id */
Willy Tarreau60600742016-01-11 15:32:30 +01003469 chunk_appendf(out, "id: %d</div>", stats[ST_F_SID].u.u32);
Willy Tarreaufa9512f2016-01-08 17:20:51 +01003470 }
3471
Willy Tarreau60600742016-01-11 15:32:30 +01003472 chunk_appendf(out,
Willy Tarreaufa9512f2016-01-08 17:20:51 +01003473 /* queue */
3474 "%s</td><td colspan=3></td>"
3475 /* sessions rate: current, max, limit */
3476 "<td colspan=3>&nbsp;</td>"
3477 /* sessions: current, max, limit, total, lbtot, lastsess */
3478 "<td>%s</td><td>%s</td><td>%s</td>"
3479 "<td>%s</td><td>&nbsp;</td><td>&nbsp;</td>"
3480 /* bytes: in, out */
3481 "<td>%s</td><td>%s</td>"
3482 "",
3483 (flags & ST_SHLGNDS)?"</u>":"",
3484 U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SMAX].u.u32), U2H(stats[ST_F_SLIM].u.u32),
3485 U2H(stats[ST_F_STOT].u.u64), U2H(stats[ST_F_BIN].u.u64), U2H(stats[ST_F_BOUT].u.u64));
3486
Willy Tarreau60600742016-01-11 15:32:30 +01003487 chunk_appendf(out,
Willy Tarreaufa9512f2016-01-08 17:20:51 +01003488 /* denied: req, resp */
3489 "<td>%s</td><td>%s</td>"
3490 /* errors: request, connect, response */
3491 "<td>%s</td><td></td><td></td>"
3492 /* warnings: retries, redispatches */
3493 "<td></td><td></td>"
3494 /* server status: reflect listener status */
3495 "<td class=ac>%s</td>"
3496 /* rest of server: nothing */
3497 "<td class=ac colspan=8></td></tr>"
3498 "",
3499 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
3500 U2H(stats[ST_F_EREQ].u.u64),
3501 field_str(stats, ST_F_STATUS));
3502 }
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003503 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_SV) {
3504 const char *style;
Willy Tarreaub5f66b82016-01-08 17:15:39 +01003505
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003506 /* determine the style to use depending on the server's state,
3507 * its health and weight. There isn't a 1-to-1 mapping between
3508 * state and styles for the cases where the server is (still)
3509 * up. The reason is that we don't want to report nolb and
3510 * drain with the same color.
3511 */
Willy Tarreaub5f66b82016-01-08 17:15:39 +01003512
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003513 if (strcmp(field_str(stats, ST_F_STATUS), "DOWN") == 0 ||
3514 strcmp(field_str(stats, ST_F_STATUS), "DOWN (agent)") == 0) {
3515 style = "down";
3516 }
3517 else if (strcmp(field_str(stats, ST_F_STATUS), "DOWN ") == 0) {
3518 style = "going_up";
3519 }
3520 else if (strcmp(field_str(stats, ST_F_STATUS), "NOLB ") == 0) {
3521 style = "going_down";
3522 }
3523 else if (strcmp(field_str(stats, ST_F_STATUS), "NOLB") == 0) {
3524 style = "nolb";
3525 }
3526 else if (strcmp(field_str(stats, ST_F_STATUS), "no check") == 0) {
3527 style = "no_check";
3528 }
3529 else if (!stats[ST_F_CHKFAIL].type ||
3530 stats[ST_F_CHECK_HEALTH].u.u32 == stats[ST_F_CHECK_RISE].u.u32 + stats[ST_F_CHECK_FALL].u.u32 - 1) {
3531 /* no check or max health = UP */
3532 if (stats[ST_F_WEIGHT].u.u32)
3533 style = "up";
3534 else
3535 style = "draining";
3536 }
3537 else {
3538 style = "going_down";
3539 }
Willy Tarreaub5f66b82016-01-08 17:15:39 +01003540
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003541 if (memcmp(field_str(stats, ST_F_STATUS), "MAINT", 5) == 0)
Willy Tarreau60600742016-01-11 15:32:30 +01003542 chunk_appendf(out, "<tr class=\"maintain\">");
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003543 else
Willy Tarreau60600742016-01-11 15:32:30 +01003544 chunk_appendf(out,
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003545 "<tr class=\"%s_%s\">",
3546 (stats[ST_F_BCK].u.u32) ? "backup" : "active", style);
Willy Tarreaub5f66b82016-01-08 17:15:39 +01003547
Willy Tarreaub5f66b82016-01-08 17:15:39 +01003548
Willy Tarreau508a63f2016-01-11 15:28:40 +01003549 if (flags & ST_SHOWADMIN)
Willy Tarreau60600742016-01-11 15:32:30 +01003550 chunk_appendf(out,
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003551 "<td><input type=\"checkbox\" name=\"s\" value=\"%s\"></td>",
3552 field_str(stats, ST_F_SVNAME));
Willy Tarreaub5f66b82016-01-08 17:15:39 +01003553
Willy Tarreau60600742016-01-11 15:32:30 +01003554 chunk_appendf(out,
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003555 "<td class=ac><a name=\"%s/%s\"></a>%s"
3556 "<a class=lfsb href=\"#%s/%s\">%s</a>"
3557 "",
3558 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME),
3559 (flags & ST_SHLGNDS) ? "<u>" : "",
3560 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 +01003561
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003562 if (flags & ST_SHLGNDS) {
Willy Tarreau60600742016-01-11 15:32:30 +01003563 chunk_appendf(out, "<div class=tips>");
Willy Tarreaub5f66b82016-01-08 17:15:39 +01003564
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003565 if (isdigit(*field_str(stats, ST_F_ADDR)))
Willy Tarreau60600742016-01-11 15:32:30 +01003566 chunk_appendf(out, "IPv4: %s, ", field_str(stats, ST_F_ADDR));
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003567 else if (*field_str(stats, ST_F_ADDR) == '[')
Willy Tarreau60600742016-01-11 15:32:30 +01003568 chunk_appendf(out, "IPv6: %s, ", field_str(stats, ST_F_ADDR));
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003569 else if (*field_str(stats, ST_F_ADDR))
Willy Tarreau60600742016-01-11 15:32:30 +01003570 chunk_appendf(out, "%s, ", field_str(stats, ST_F_ADDR));
Willy Tarreaub5f66b82016-01-08 17:15:39 +01003571
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003572 /* id */
Willy Tarreau60600742016-01-11 15:32:30 +01003573 chunk_appendf(out, "id: %d", stats[ST_F_SID].u.u32);
Willy Tarreaub5f66b82016-01-08 17:15:39 +01003574
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003575 /* cookie */
3576 if (stats[ST_F_COOKIE].type) {
Willy Tarreau60600742016-01-11 15:32:30 +01003577 chunk_appendf(out, ", cookie: '");
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003578 chunk_initstr(&src, field_str(stats, ST_F_COOKIE));
Willy Tarreau60600742016-01-11 15:32:30 +01003579 chunk_htmlencode(out, &src);
3580 chunk_appendf(out, "'");
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003581 }
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02003582
Willy Tarreau60600742016-01-11 15:32:30 +01003583 chunk_appendf(out, "</div>");
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003584 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003585
Willy Tarreau60600742016-01-11 15:32:30 +01003586 chunk_appendf(out,
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003587 /* queue : current, max, limit */
3588 "%s</td><td>%s</td><td>%s</td><td>%s</td>"
3589 /* sessions rate : current, max, limit */
3590 "<td>%s</td><td>%s</td><td></td>"
3591 "",
3592 (flags & ST_SHLGNDS) ? "</u>" : "",
3593 U2H(stats[ST_F_QCUR].u.u32), U2H(stats[ST_F_QMAX].u.u32), LIM2A(stats[ST_F_QLIMIT].u.u32, "-"),
3594 U2H(stats[ST_F_RATE].u.u32), U2H(stats[ST_F_RATE_MAX].u.u32));
Willy Tarreau4607fad2016-01-06 15:41:29 +01003595
Willy Tarreau60600742016-01-11 15:32:30 +01003596 chunk_appendf(out,
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003597 /* sessions: current, max, limit, total */
3598 "<td>%s</td><td>%s</td><td>%s</td>"
3599 "<td><u>%s<div class=tips><table class=det>"
3600 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
3601 "",
3602 U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SMAX].u.u32), LIM2A(stats[ST_F_SLIM].u.u32, "-"),
3603 U2H(stats[ST_F_STOT].u.u64),
3604 U2H(stats[ST_F_STOT].u.u64));
Willy Tarreau4607fad2016-01-06 15:41:29 +01003605
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003606 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
Willy Tarreauf8211df2016-01-11 14:09:38 +01003607 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003608 unsigned long long tot;
Willy Tarreaua6f5a732016-01-08 16:59:56 +01003609
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003610 tot = stats[ST_F_HRSP_OTHER].u.u64;
3611 tot += stats[ST_F_HRSP_1XX].u.u64;
3612 tot += stats[ST_F_HRSP_2XX].u.u64;
3613 tot += stats[ST_F_HRSP_3XX].u.u64;
3614 tot += stats[ST_F_HRSP_4XX].u.u64;
3615 tot += stats[ST_F_HRSP_5XX].u.u64;
3616
Willy Tarreau60600742016-01-11 15:32:30 +01003617 chunk_appendf(out,
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003618 "<tr><th>Cum. HTTP responses:</th><td>%s</td></tr>"
3619 "<tr><th>- HTTP 1xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
3620 "<tr><th>- HTTP 2xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
3621 "<tr><th>- HTTP 3xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
3622 "<tr><th>- HTTP 4xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
3623 "<tr><th>- HTTP 5xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
3624 "<tr><th>- other responses:</th><td>%s</td><td>(%d%%)</td></tr>"
3625 "",
3626 U2H(tot),
3627 U2H(stats[ST_F_HRSP_1XX].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_1XX].u.u64 / tot) : 0,
3628 U2H(stats[ST_F_HRSP_2XX].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_2XX].u.u64 / tot) : 0,
3629 U2H(stats[ST_F_HRSP_3XX].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_3XX].u.u64 / tot) : 0,
3630 U2H(stats[ST_F_HRSP_4XX].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_4XX].u.u64 / tot) : 0,
3631 U2H(stats[ST_F_HRSP_5XX].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_5XX].u.u64 / tot) : 0,
3632 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 +01003633 }
Willy Tarreaua6f5a732016-01-08 16:59:56 +01003634
Willy Tarreau60600742016-01-11 15:32:30 +01003635 chunk_appendf(out, "<tr><th colspan=3>Avg over last 1024 success. conn.</th></tr>");
3636 chunk_appendf(out, "<tr><th>- Queue time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_QTIME].u.u32));
3637 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 +01003638 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
Willy Tarreau60600742016-01-11 15:32:30 +01003639 chunk_appendf(out, "<tr><th>- Response time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_RTIME].u.u32));
3640 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 +02003641
Willy Tarreau60600742016-01-11 15:32:30 +01003642 chunk_appendf(out,
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003643 "</table></div></u></td>"
3644 /* sessions: lbtot, last */
3645 "<td>%s</td><td>%s</td>",
3646 U2H(stats[ST_F_LBTOT].u.u64),
3647 human_time(stats[ST_F_LASTSESS].u.s32, 1));
Willy Tarreau91861262007-10-17 17:06:05 +02003648
Willy Tarreau60600742016-01-11 15:32:30 +01003649 chunk_appendf(out,
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003650 /* bytes : in, out */
3651 "<td>%s</td><td>%s</td>"
3652 /* denied: req, resp */
3653 "<td></td><td>%s</td>"
3654 /* errors : request, connect */
3655 "<td></td><td>%s</td>"
3656 /* errors : response */
3657 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
3658 /* warnings: retries, redispatches */
3659 "<td>%lld</td><td>%lld</td>"
3660 "",
3661 U2H(stats[ST_F_BIN].u.u64), U2H(stats[ST_F_BOUT].u.u64),
3662 U2H(stats[ST_F_DRESP].u.u64),
3663 U2H(stats[ST_F_ECON].u.u64),
3664 U2H(stats[ST_F_ERESP].u.u64),
3665 (long long)stats[ST_F_CLI_ABRT].u.u64,
3666 (long long)stats[ST_F_SRV_ABRT].u.u64,
3667 (long long)stats[ST_F_WRETR].u.u64,
3668 (long long)stats[ST_F_WREDIS].u.u64);
3669
3670 /* status, last change */
Willy Tarreau60600742016-01-11 15:32:30 +01003671 chunk_appendf(out, "<td class=ac>");
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003672
3673 /* FIXME!!!!
3674 * LASTCHG should contain the last change for *this* server and must be computed
3675 * properly above, as was done below, ie: this server if maint, otherwise ref server
3676 * if tracking. Note that ref is either local or remote depending on tracking.
3677 */
3678
3679
3680 if (memcmp(field_str(stats, ST_F_STATUS), "MAINT", 5) == 0) {
Willy Tarreau60600742016-01-11 15:32:30 +01003681 chunk_appendf(out, "%s MAINT", human_time(stats[ST_F_LASTCHG].u.u32, 1));
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003682 }
3683 else if (memcmp(field_str(stats, ST_F_STATUS), "no check", 5) == 0) {
Willy Tarreau60600742016-01-11 15:32:30 +01003684 chunk_strcat(out, "<i>no check</i>");
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003685 }
3686 else {
Willy Tarreau60600742016-01-11 15:32:30 +01003687 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 +01003688 if (memcmp(field_str(stats, ST_F_STATUS), "DOWN", 4) == 0) {
3689 if (stats[ST_F_CHECK_HEALTH].u.u32)
Willy Tarreau60600742016-01-11 15:32:30 +01003690 chunk_strcat(out, " &uarr;");
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003691 }
3692 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 +01003693 chunk_strcat(out, " &darr;");
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003694 }
3695
3696 if (memcmp(field_str(stats, ST_F_STATUS), "DOWN", 4) == 0 &&
3697 stats[ST_F_AGENT_STATUS].type && !stats[ST_F_AGENT_HEALTH].u.u32) {
Willy Tarreau60600742016-01-11 15:32:30 +01003698 chunk_appendf(out,
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003699 "</td><td class=ac><u> %s",
3700 field_str(stats, ST_F_AGENT_STATUS));
3701
3702 if (stats[ST_F_AGENT_CODE].type)
Willy Tarreau60600742016-01-11 15:32:30 +01003703 chunk_appendf(out, "/%d", stats[ST_F_AGENT_CODE].u.u32);
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003704
David Carlier08d35de2016-06-27 14:12:59 +01003705 if (stats[ST_F_AGENT_DURATION].type)
Willy Tarreau60600742016-01-11 15:32:30 +01003706 chunk_appendf(out, " in %lums", (long)stats[ST_F_AGENT_DURATION].u.u64);
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003707
Willy Tarreau60600742016-01-11 15:32:30 +01003708 chunk_appendf(out, "<div class=tips>%s", field_str(stats, ST_F_AGENT_DESC));
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003709
3710 if (*field_str(stats, ST_F_LAST_AGT)) {
Willy Tarreau60600742016-01-11 15:32:30 +01003711 chunk_appendf(out, ": ");
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003712 chunk_initstr(&src, field_str(stats, ST_F_LAST_AGT));
Willy Tarreau60600742016-01-11 15:32:30 +01003713 chunk_htmlencode(out, &src);
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003714 }
Willy Tarreau60600742016-01-11 15:32:30 +01003715 chunk_appendf(out, "</div></u>");
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003716 }
3717 else if (stats[ST_F_CHECK_STATUS].type) {
Willy Tarreau60600742016-01-11 15:32:30 +01003718 chunk_appendf(out,
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003719 "</td><td class=ac><u> %s",
3720 field_str(stats, ST_F_CHECK_STATUS));
3721
3722 if (stats[ST_F_CHECK_CODE].type)
Willy Tarreau60600742016-01-11 15:32:30 +01003723 chunk_appendf(out, "/%d", stats[ST_F_CHECK_CODE].u.u32);
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003724
David Carlier08d35de2016-06-27 14:12:59 +01003725 if (stats[ST_F_CHECK_DURATION].type)
Willy Tarreau60600742016-01-11 15:32:30 +01003726 chunk_appendf(out, " in %lums", (long)stats[ST_F_CHECK_DURATION].u.u64);
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003727
Willy Tarreau60600742016-01-11 15:32:30 +01003728 chunk_appendf(out, "<div class=tips>%s", field_str(stats, ST_F_CHECK_DESC));
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003729
3730 if (*field_str(stats, ST_F_LAST_CHK)) {
Willy Tarreau60600742016-01-11 15:32:30 +01003731 chunk_appendf(out, ": ");
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003732 chunk_initstr(&src, field_str(stats, ST_F_LAST_CHK));
Willy Tarreau60600742016-01-11 15:32:30 +01003733 chunk_htmlencode(out, &src);
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003734 }
Willy Tarreau60600742016-01-11 15:32:30 +01003735 chunk_appendf(out, "</div></u>");
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003736 }
3737 else
Willy Tarreau60600742016-01-11 15:32:30 +01003738 chunk_appendf(out, "</td><td>");
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003739
Willy Tarreau60600742016-01-11 15:32:30 +01003740 chunk_appendf(out,
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003741 /* weight */
3742 "</td><td class=ac>%d</td>"
3743 /* act, bck */
3744 "<td class=ac>%s</td><td class=ac>%s</td>"
3745 "",
3746 stats[ST_F_WEIGHT].u.u32,
3747 stats[ST_F_BCK].u.u32 ? "-" : "Y",
3748 stats[ST_F_BCK].u.u32 ? "Y" : "-");
3749
3750 /* check failures: unique, fatal, down time */
3751 if (stats[ST_F_CHKFAIL].type) {
Willy Tarreau60600742016-01-11 15:32:30 +01003752 chunk_appendf(out, "<td><u>%lld", (long long)stats[ST_F_CHKFAIL].u.u64);
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003753
3754 if (stats[ST_F_HANAFAIL].type)
Willy Tarreau60600742016-01-11 15:32:30 +01003755 chunk_appendf(out, "/%lld", (long long)stats[ST_F_HANAFAIL].u.u64);
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003756
Willy Tarreau60600742016-01-11 15:32:30 +01003757 chunk_appendf(out,
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003758 "<div class=tips>Failed Health Checks%s</div></u></td>"
3759 "<td>%lld</td><td>%s</td>"
3760 "",
3761 stats[ST_F_HANAFAIL].type ? "/Health Analyses" : "",
3762 (long long)stats[ST_F_CHKDOWN].u.u64, human_time(stats[ST_F_DOWNTIME].u.u32, 1));
3763 }
3764 else if (strcmp(field_str(stats, ST_F_STATUS), "MAINT") != 0 && field_format(stats, ST_F_TRACKED) == FF_STR) {
3765 /* tracking a server (hence inherited maint would appear as "MAINT (via...)" */
Willy Tarreau60600742016-01-11 15:32:30 +01003766 chunk_appendf(out,
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003767 "<td class=ac colspan=3><a class=lfsb href=\"#%s\">via %s</a></td>",
3768 field_str(stats, ST_F_TRACKED), field_str(stats, ST_F_TRACKED));
3769 }
3770 else
Willy Tarreau60600742016-01-11 15:32:30 +01003771 chunk_appendf(out, "<td colspan=3></td>");
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003772
3773 /* throttle */
3774 if (stats[ST_F_THROTTLE].type)
Willy Tarreau60600742016-01-11 15:32:30 +01003775 chunk_appendf(out, "<td class=ac>%d %%</td></tr>\n", stats[ST_F_THROTTLE].u.u32);
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003776 else
Willy Tarreau60600742016-01-11 15:32:30 +01003777 chunk_appendf(out, "<td class=ac>-</td></tr>\n");
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003778 }
Willy Tarreaubbf84502016-01-08 17:25:50 +01003779 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_BE) {
Willy Tarreau60600742016-01-11 15:32:30 +01003780 chunk_appendf(out, "<tr class=\"backend\">");
Willy Tarreau508a63f2016-01-11 15:28:40 +01003781 if (flags & ST_SHOWADMIN) {
Willy Tarreaubbf84502016-01-08 17:25:50 +01003782 /* Column sub-heading for Enable or Disable server */
Willy Tarreau60600742016-01-11 15:32:30 +01003783 chunk_appendf(out, "<td></td>");
Willy Tarreaubbf84502016-01-08 17:25:50 +01003784 }
Willy Tarreau60600742016-01-11 15:32:30 +01003785 chunk_appendf(out,
Willy Tarreaubbf84502016-01-08 17:25:50 +01003786 "<td class=ac>"
3787 /* name */
3788 "%s<a name=\"%s/Backend\"></a>"
3789 "<a class=lfsb href=\"#%s/Backend\">Backend</a>"
3790 "",
3791 (flags & ST_SHLGNDS)?"<u>":"",
3792 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_PXNAME));
3793
3794 if (flags & ST_SHLGNDS) {
3795 /* balancing */
Willy Tarreau60600742016-01-11 15:32:30 +01003796 chunk_appendf(out, "<div class=tips>balancing: %s",
Willy Tarreauf1516d92016-01-11 14:48:36 +01003797 field_str(stats, ST_F_ALGO));
Willy Tarreaubbf84502016-01-08 17:25:50 +01003798
3799 /* cookie */
3800 if (stats[ST_F_COOKIE].type) {
Willy Tarreau60600742016-01-11 15:32:30 +01003801 chunk_appendf(out, ", cookie: '");
Willy Tarreaubbf84502016-01-08 17:25:50 +01003802 chunk_initstr(&src, field_str(stats, ST_F_COOKIE));
Willy Tarreau60600742016-01-11 15:32:30 +01003803 chunk_htmlencode(out, &src);
3804 chunk_appendf(out, "'");
Willy Tarreaubbf84502016-01-08 17:25:50 +01003805 }
Willy Tarreau60600742016-01-11 15:32:30 +01003806 chunk_appendf(out, "</div>");
Willy Tarreaubbf84502016-01-08 17:25:50 +01003807 }
3808
Willy Tarreau60600742016-01-11 15:32:30 +01003809 chunk_appendf(out,
Willy Tarreaubbf84502016-01-08 17:25:50 +01003810 "%s</td>"
3811 /* queue : current, max */
3812 "<td>%s</td><td>%s</td><td></td>"
3813 /* sessions rate : current, max, limit */
3814 "<td>%s</td><td>%s</td><td></td>"
3815 "",
3816 (flags & ST_SHLGNDS)?"</u>":"",
3817 U2H(stats[ST_F_QCUR].u.u32), U2H(stats[ST_F_QMAX].u.u32),
3818 U2H(stats[ST_F_RATE].u.u32), U2H(stats[ST_F_RATE_MAX].u.u32));
3819
Willy Tarreau60600742016-01-11 15:32:30 +01003820 chunk_appendf(out,
Willy Tarreaubbf84502016-01-08 17:25:50 +01003821 /* sessions: current, max, limit, total */
3822 "<td>%s</td><td>%s</td><td>%s</td>"
3823 "<td><u>%s<div class=tips><table class=det>"
3824 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
3825 "",
3826 U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SLIM].u.u32),
3827 U2H(stats[ST_F_STOT].u.u64),
3828 U2H(stats[ST_F_STOT].u.u64));
3829
3830 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
Willy Tarreauf8211df2016-01-11 14:09:38 +01003831 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
Willy Tarreau60600742016-01-11 15:32:30 +01003832 chunk_appendf(out,
Willy Tarreaubbf84502016-01-08 17:25:50 +01003833 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
3834 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
3835 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
3836 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
3837 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
3838 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
3839 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
3840 "<tr><th>- other responses:</th><td>%s</td></tr>"
3841 "<tr><th>Intercepted requests:</th><td>%s</td></tr>"
3842 "<tr><th colspan=3>Avg over last 1024 success. conn.</th></tr>"
3843 "",
3844 U2H(stats[ST_F_REQ_TOT].u.u64),
3845 U2H(stats[ST_F_HRSP_1XX].u.u64),
3846 U2H(stats[ST_F_HRSP_2XX].u.u64),
3847 U2H(stats[ST_F_COMP_RSP].u.u64),
3848 stats[ST_F_HRSP_2XX].u.u64 ?
3849 (int)(100 * stats[ST_F_COMP_RSP].u.u64 / stats[ST_F_HRSP_2XX].u.u64) : 0,
3850 U2H(stats[ST_F_HRSP_3XX].u.u64),
3851 U2H(stats[ST_F_HRSP_4XX].u.u64),
3852 U2H(stats[ST_F_HRSP_5XX].u.u64),
3853 U2H(stats[ST_F_HRSP_OTHER].u.u64),
Willy Tarreau5b9bdff2016-01-11 14:40:47 +01003854 U2H(stats[ST_F_INTERCEPTED].u.u64));
Willy Tarreaubbf84502016-01-08 17:25:50 +01003855 }
3856
Willy Tarreau60600742016-01-11 15:32:30 +01003857 chunk_appendf(out, "<tr><th>- Queue time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_QTIME].u.u32));
3858 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 +01003859 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
Willy Tarreau60600742016-01-11 15:32:30 +01003860 chunk_appendf(out, "<tr><th>- Response time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_RTIME].u.u32));
3861 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 +01003862
Willy Tarreau60600742016-01-11 15:32:30 +01003863 chunk_appendf(out,
Willy Tarreaubbf84502016-01-08 17:25:50 +01003864 "</table></div></u></td>"
3865 /* sessions: lbtot, last */
3866 "<td>%s</td><td>%s</td>"
3867 /* bytes: in */
3868 "<td>%s</td>"
3869 "",
3870 U2H(stats[ST_F_LBTOT].u.u64),
3871 human_time(stats[ST_F_LASTSESS].u.s32, 1),
3872 U2H(stats[ST_F_BIN].u.u64));
3873
Willy Tarreau60600742016-01-11 15:32:30 +01003874 chunk_appendf(out,
Willy Tarreaubbf84502016-01-08 17:25:50 +01003875 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
3876 "<td>%s%s<div class=tips><table class=det>"
3877 "<tr><th>Response bytes in:</th><td>%s</td></tr>"
3878 "<tr><th>Compression in:</th><td>%s</td></tr>"
3879 "<tr><th>Compression out:</th><td>%s</td><td>(%d%%)</td></tr>"
3880 "<tr><th>Compression bypass:</th><td>%s</td></tr>"
3881 "<tr><th>Total bytes saved:</th><td>%s</td><td>(%d%%)</td></tr>"
3882 "</table></div>%s</td>",
3883 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "<u>":"",
3884 U2H(stats[ST_F_BOUT].u.u64),
3885 U2H(stats[ST_F_BOUT].u.u64),
3886 U2H(stats[ST_F_COMP_IN].u.u64),
3887 U2H(stats[ST_F_COMP_OUT].u.u64),
3888 stats[ST_F_COMP_IN].u.u64 ? (int)(stats[ST_F_COMP_OUT].u.u64 * 100 / stats[ST_F_COMP_IN].u.u64) : 0,
3889 U2H(stats[ST_F_COMP_BYP].u.u64),
Christopher Faulet0b64f622016-04-28 15:09:31 +02003890 U2H(stats[ST_F_COMP_IN].u.u64 - stats[ST_F_COMP_OUT].u.u64),
Willy Tarreaubbf84502016-01-08 17:25:50 +01003891 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,
3892 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "</u>":"");
3893
Willy Tarreau60600742016-01-11 15:32:30 +01003894 chunk_appendf(out,
Willy Tarreaubbf84502016-01-08 17:25:50 +01003895 /* denied: req, resp */
3896 "<td>%s</td><td>%s</td>"
3897 /* errors : request, connect */
3898 "<td></td><td>%s</td>"
3899 /* errors : response */
3900 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
3901 /* warnings: retries, redispatches */
3902 "<td>%lld</td><td>%lld</td>"
3903 /* backend status: reflect backend status (up/down): we display UP
3904 * if the backend has known working servers or if it has no server at
3905 * all (eg: for stats). Then we display the total weight, number of
3906 * active and backups. */
3907 "<td class=ac>%s %s</td><td class=ac>&nbsp;</td><td class=ac>%d</td>"
3908 "<td class=ac>%d</td><td class=ac>%d</td>"
3909 "",
3910 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
3911 U2H(stats[ST_F_ECON].u.u64),
3912 U2H(stats[ST_F_ERESP].u.u64),
3913 (long long)stats[ST_F_CLI_ABRT].u.u64,
3914 (long long)stats[ST_F_SRV_ABRT].u.u64,
3915 (long long)stats[ST_F_WRETR].u.u64, (long long)stats[ST_F_WREDIS].u.u64,
3916 human_time(stats[ST_F_LASTCHG].u.u32, 1),
3917 strcmp(field_str(stats, ST_F_STATUS), "DOWN") ? field_str(stats, ST_F_STATUS) : "<font color=\"red\"><b>DOWN</b></font>",
3918 stats[ST_F_WEIGHT].u.u32,
3919 stats[ST_F_ACT].u.u32, stats[ST_F_BCK].u.u32);
3920
Willy Tarreau60600742016-01-11 15:32:30 +01003921 chunk_appendf(out,
Willy Tarreaubbf84502016-01-08 17:25:50 +01003922 /* rest of backend: nothing, down transitions, total downtime, throttle */
3923 "<td class=ac>&nbsp;</td><td>%d</td>"
3924 "<td>%s</td>"
3925 "<td></td>"
3926 "</tr>",
3927 stats[ST_F_CHKDOWN].u.u32,
3928 stats[ST_F_DOWNTIME].type ? human_time(stats[ST_F_DOWNTIME].u.u32, 1) : "&nbsp;");
3929 }
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003930 return 1;
3931}
3932
Willy Tarreau501f6022016-01-08 17:37:22 +01003933static int stats_dump_one_line(const struct field *stats, unsigned int flags, struct proxy *px, struct appctx *appctx)
3934{
Willy Tarreau508a63f2016-01-11 15:28:40 +01003935 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN))
3936 flags |= ST_SHOWADMIN;
Willy Tarreau501f6022016-01-08 17:37:22 +01003937
3938 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
Willy Tarreau60600742016-01-11 15:32:30 +01003939 return stats_dump_fields_html(&trash, stats, flags);
Willy Tarreau1e62df92016-01-11 18:57:53 +01003940 else if (appctx->ctx.stats.flags & STAT_FMT_TYPED)
3941 return stats_dump_fields_typed(&trash, stats);
Willy Tarreau501f6022016-01-08 17:37:22 +01003942 else
3943 return stats_dump_fields_csv(&trash, stats);
3944}
3945
Thierry Fournier23d2d642016-03-25 08:20:11 +01003946/* Fill <stats> with the frontend statistics. <stats> is
3947 * preallocated array of length <len>. The length of the array
3948 * must be at least ST_F_TOTAL_FIELDS. If this length is less then
3949 * this value, the function returns 0, otherwise, it returns 1.
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003950 */
Thierry Fournier23d2d642016-03-25 08:20:11 +01003951int stats_fill_fe_stats(struct proxy *px, struct field *stats, int len)
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003952{
Thierry Fournier23d2d642016-03-25 08:20:11 +01003953 if (len < ST_F_TOTAL_FIELDS)
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003954 return 0;
3955
Thierry Fournier23d2d642016-03-25 08:20:11 +01003956 memset(stats, 0, sizeof(*stats) * len);
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003957
3958 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
3959 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, "FRONTEND");
Willy Tarreauf8211df2016-01-11 14:09:38 +01003960 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003961 stats[ST_F_SCUR] = mkf_u32(0, px->feconn);
3962 stats[ST_F_SMAX] = mkf_u32(FN_MAX, px->fe_counters.conn_max);
3963 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, px->maxconn);
3964 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, px->fe_counters.cum_sess);
3965 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, px->fe_counters.bytes_in);
3966 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, px->fe_counters.bytes_out);
3967 stats[ST_F_DREQ] = mkf_u64(FN_COUNTER, px->fe_counters.denied_req);
3968 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, px->fe_counters.denied_resp);
3969 stats[ST_F_EREQ] = mkf_u64(FN_COUNTER, px->fe_counters.failed_req);
3970 stats[ST_F_STATUS] = mkf_str(FO_STATUS, px->state == PR_STREADY ? "OPEN" : px->state == PR_STFULL ? "FULL" : "STOP");
3971 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
3972 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
3973 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, 0);
3974 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_FE);
3975 stats[ST_F_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_sess_per_sec));
3976 stats[ST_F_RATE_LIM] = mkf_u32(FO_CONFIG|FN_LIMIT, px->fe_sps_lim);
3977 stats[ST_F_RATE_MAX] = mkf_u32(FN_MAX, px->fe_counters.sps_max);
3978
3979 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
3980 if (px->mode == PR_MODE_HTTP) {
3981 stats[ST_F_HRSP_1XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[1]);
3982 stats[ST_F_HRSP_2XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[2]);
3983 stats[ST_F_HRSP_3XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[3]);
3984 stats[ST_F_HRSP_4XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[4]);
3985 stats[ST_F_HRSP_5XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[5]);
3986 stats[ST_F_HRSP_OTHER] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[0]);
Willy Tarreau5b9bdff2016-01-11 14:40:47 +01003987 stats[ST_F_INTERCEPTED] = mkf_u64(FN_COUNTER, px->fe_counters.intercepted_req);
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003988 }
3989
3990 /* requests : req_rate, req_rate_max, req_tot, */
3991 stats[ST_F_REQ_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_req_per_sec));
3992 stats[ST_F_REQ_RATE_MAX] = mkf_u32(FN_MAX, px->fe_counters.p.http.rps_max);
3993 stats[ST_F_REQ_TOT] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cum_req);
3994
3995 /* compression: in, out, bypassed, responses */
3996 stats[ST_F_COMP_IN] = mkf_u64(FN_COUNTER, px->fe_counters.comp_in);
3997 stats[ST_F_COMP_OUT] = mkf_u64(FN_COUNTER, px->fe_counters.comp_out);
3998 stats[ST_F_COMP_BYP] = mkf_u64(FN_COUNTER, px->fe_counters.comp_byp);
3999 stats[ST_F_COMP_RSP] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.comp_rsp);
4000
Willy Tarreauc73810f2016-01-11 13:52:04 +01004001 /* connections : conn_rate, conn_rate_max, conn_tot, conn_max */
4002 stats[ST_F_CONN_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_conn_per_sec));
4003 stats[ST_F_CONN_RATE_MAX] = mkf_u32(FN_MAX, px->fe_counters.cps_max);
4004 stats[ST_F_CONN_TOT] = mkf_u64(FN_COUNTER, px->fe_counters.cum_conn);
4005
Thierry Fournier23d2d642016-03-25 08:20:11 +01004006 return 1;
4007}
4008
4009/* Dumps a frontend's line to the trash for the current proxy <px> and uses
4010 * the state from stream interface <si>. The caller is responsible for clearing
4011 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
4012 */
4013static int stats_dump_fe_stats(struct stream_interface *si, struct proxy *px)
4014{
4015 struct appctx *appctx = __objt_appctx(si->end);
4016
4017 if (!(px->cap & PR_CAP_FE))
4018 return 0;
4019
4020 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_FE)))
4021 return 0;
4022
4023 if (!stats_fill_fe_stats(px, stats, ST_F_TOTAL_FIELDS))
4024 return 0;
4025
Willy Tarreau501f6022016-01-08 17:37:22 +01004026 return stats_dump_one_line(stats, 0, px, appctx);
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004027}
4028
Thierry Fournierc4456852016-03-25 08:20:49 +01004029/* Fill <stats> with the listener statistics. <stats> is
4030 * preallocated array of length <len>. The length of the array
4031 * must be at least ST_F_TOTAL_FIELDS. If this length is less
4032 * then this value, the function returns 0, otherwise, it
4033 * returns 1. <flags> can take the value ST_SHLGNDS.
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004034 */
Thierry Fournierc4456852016-03-25 08:20:49 +01004035int stats_fill_li_stats(struct proxy *px, struct listener *l, int flags,
4036 struct field *stats, int len)
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004037{
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004038 struct chunk *out = get_trash_chunk();
4039
Thierry Fournierc4456852016-03-25 08:20:49 +01004040 if (len < ST_F_TOTAL_FIELDS)
4041 return 0;
4042
Thierry Fournierac9d4672016-03-25 08:43:46 +01004043 if (!l->counters)
4044 return 0;
4045
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004046 chunk_reset(out);
Thierry Fournierc4456852016-03-25 08:20:49 +01004047 memset(stats, 0, sizeof(*stats) * len);
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004048
4049 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
4050 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, l->name);
Willy Tarreauf8211df2016-01-11 14:09:38 +01004051 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004052 stats[ST_F_SCUR] = mkf_u32(0, l->nbconn);
4053 stats[ST_F_SMAX] = mkf_u32(FN_MAX, l->counters->conn_max);
4054 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, l->maxconn);
4055 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, l->counters->cum_conn);
4056 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, l->counters->bytes_in);
4057 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, l->counters->bytes_out);
4058 stats[ST_F_DREQ] = mkf_u64(FN_COUNTER, l->counters->denied_req);
4059 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, l->counters->denied_resp);
4060 stats[ST_F_EREQ] = mkf_u64(FN_COUNTER, l->counters->failed_req);
4061 stats[ST_F_STATUS] = mkf_str(FO_STATUS, (l->nbconn < l->maxconn) ? (l->state == LI_LIMITED) ? "WAITING" : "OPEN" : "FULL");
4062 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
4063 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
4064 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, l->luid);
4065 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_SO);
4066
4067 if (flags & ST_SHLGNDS) {
4068 char str[INET6_ADDRSTRLEN];
4069 int port;
4070
4071 port = get_host_port(&l->addr);
4072 switch (addr_to_str(&l->addr, str, sizeof(str))) {
4073 case AF_INET:
4074 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
4075 chunk_appendf(out, "%s:%d", str, port);
4076 break;
4077 case AF_INET6:
4078 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
4079 chunk_appendf(out, "[%s]:%d", str, port);
4080 break;
4081 case AF_UNIX:
4082 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, "unix");
4083 break;
4084 case -1:
4085 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
4086 chunk_strcat(out, strerror(errno));
4087 break;
4088 default: /* address family not supported */
4089 break;
4090 }
4091 }
4092
Thierry Fournierc4456852016-03-25 08:20:49 +01004093 return 1;
4094}
4095
4096/* Dumps a line for listener <l> and proxy <px> to the trash and uses the state
4097 * from stream interface <si>, and stats flags <flags>. The caller is responsible
4098 * for clearing the trash if needed. Returns non-zero if it emits anything, zero
4099 * otherwise.
4100 */
4101static int stats_dump_li_stats(struct stream_interface *si, struct proxy *px, struct listener *l, int flags)
4102{
4103 struct appctx *appctx = __objt_appctx(si->end);
4104
4105 if (!stats_fill_li_stats(px, l, flags, stats, ST_F_TOTAL_FIELDS))
4106 return 0;
4107
Willy Tarreau501f6022016-01-08 17:37:22 +01004108 return stats_dump_one_line(stats, flags, px, appctx);
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004109}
4110
4111enum srv_stats_state {
4112 SRV_STATS_STATE_DOWN = 0,
4113 SRV_STATS_STATE_DOWN_AGENT,
4114 SRV_STATS_STATE_GOING_UP,
4115 SRV_STATS_STATE_UP_GOING_DOWN,
4116 SRV_STATS_STATE_UP,
4117 SRV_STATS_STATE_NOLB_GOING_DOWN,
Simon Horman4d2eab62015-04-23 14:51:26 +09004118 SRV_STATS_STATE_NOLB,
4119 SRV_STATS_STATE_DRAIN_GOING_DOWN,
4120 SRV_STATS_STATE_DRAIN,
Simon Hormanb167b6b2015-04-23 14:51:29 +09004121 SRV_STATS_STATE_DRAIN_AGENT,
Simon Horman4d2eab62015-04-23 14:51:26 +09004122 SRV_STATS_STATE_NO_CHECK,
4123
4124 SRV_STATS_STATE_COUNT, /* Must be last */
4125};
4126
Willy Tarreau2b96cf12016-01-06 19:25:38 +01004127static const char *srv_hlt_st[SRV_STATS_STATE_COUNT] = {
4128 [SRV_STATS_STATE_DOWN] = "DOWN",
4129 [SRV_STATS_STATE_DOWN_AGENT] = "DOWN (agent)",
4130 [SRV_STATS_STATE_GOING_UP] = "DOWN %d/%d",
4131 [SRV_STATS_STATE_UP_GOING_DOWN] = "UP %d/%d",
4132 [SRV_STATS_STATE_UP] = "UP",
4133 [SRV_STATS_STATE_NOLB_GOING_DOWN] = "NOLB %d/%d",
4134 [SRV_STATS_STATE_NOLB] = "NOLB",
4135 [SRV_STATS_STATE_DRAIN_GOING_DOWN] = "DRAIN %d/%d",
4136 [SRV_STATS_STATE_DRAIN] = "DRAIN",
4137 [SRV_STATS_STATE_DRAIN_AGENT] = "DRAIN (agent)",
4138 [SRV_STATS_STATE_NO_CHECK] = "no check"
4139};
4140
Thierry Fournier61fe6c02016-03-25 08:21:21 +01004141/* Fill <stats> with the server statistics. <stats> is
4142 * preallocated array of length <len>. The length of the array
4143 * must be at least ST_F_TOTAL_FIELDS. If this length is less
4144 * then this value, the function returns 0, otherwise, it
4145 * returns 1. <flags> can take the value ST_SHLGNDS.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004146 */
Thierry Fournier61fe6c02016-03-25 08:21:21 +01004147int stats_fill_sv_stats(struct proxy *px, struct server *sv, int flags,
4148 struct field *stats, int len)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004149{
Willy Tarreau32091232014-05-16 13:52:00 +02004150 struct server *via, *ref;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004151 char str[INET6_ADDRSTRLEN];
Willy Tarreau2b96cf12016-01-06 19:25:38 +01004152 struct chunk *out = get_trash_chunk();
Willy Tarreauba2f2642016-01-07 09:54:40 +01004153 enum srv_stats_state state;
Willy Tarreau2b96cf12016-01-06 19:25:38 +01004154 char *fld_status;
Thierry Fournier61fe6c02016-03-25 08:21:21 +01004155
4156 if (len < ST_F_TOTAL_FIELDS)
4157 return 0;
4158
4159 memset(stats, 0, sizeof(*stats) * len);
4160
Willy Tarreau32091232014-05-16 13:52:00 +02004161 /* we have "via" which is the tracked server as described in the configuration,
4162 * and "ref" which is the checked server and the end of the chain.
4163 */
4164 via = sv->track ? sv->track : sv;
4165 ref = via;
4166 while (ref->track)
4167 ref = ref->track;
4168
Willy Tarreauba2f2642016-01-07 09:54:40 +01004169 if (sv->state == SRV_ST_RUNNING || sv->state == SRV_ST_STARTING) {
4170 if ((ref->check.state & CHK_ST_ENABLED) &&
4171 (ref->check.health < ref->check.rise + ref->check.fall - 1)) {
4172 state = SRV_STATS_STATE_UP_GOING_DOWN;
Willy Tarreauba2f2642016-01-07 09:54:40 +01004173 } else {
4174 state = SRV_STATS_STATE_UP;
Willy Tarreauba2f2642016-01-07 09:54:40 +01004175 }
4176
Willy Tarreauba2f2642016-01-07 09:54:40 +01004177 if (sv->admin & SRV_ADMF_DRAIN) {
4178 if (ref->agent.state & CHK_ST_ENABLED)
4179 state = SRV_STATS_STATE_DRAIN_AGENT;
4180 else if (state == SRV_STATS_STATE_UP_GOING_DOWN)
4181 state = SRV_STATS_STATE_DRAIN_GOING_DOWN;
4182 else
4183 state = SRV_STATS_STATE_DRAIN;
4184 }
4185
4186 if (state == SRV_STATS_STATE_UP && !(ref->check.state & CHK_ST_ENABLED)) {
4187 state = SRV_STATS_STATE_NO_CHECK;
Willy Tarreauba2f2642016-01-07 09:54:40 +01004188 }
4189 }
4190 else if (sv->state == SRV_ST_STOPPING) {
4191 if ((!(sv->check.state & CHK_ST_ENABLED) && !sv->track) ||
4192 (ref->check.health == ref->check.rise + ref->check.fall - 1)) {
4193 state = SRV_STATS_STATE_NOLB;
Willy Tarreauba2f2642016-01-07 09:54:40 +01004194 } else {
4195 state = SRV_STATS_STATE_NOLB_GOING_DOWN;
Willy Tarreauba2f2642016-01-07 09:54:40 +01004196 }
4197 }
4198 else { /* stopped */
4199 if ((ref->agent.state & CHK_ST_ENABLED) && !ref->agent.health) {
4200 state = SRV_STATS_STATE_DOWN_AGENT;
Willy Tarreauba2f2642016-01-07 09:54:40 +01004201 } else if ((ref->check.state & CHK_ST_ENABLED) && !ref->check.health) {
4202 state = SRV_STATS_STATE_DOWN; /* DOWN */
Willy Tarreauba2f2642016-01-07 09:54:40 +01004203 } else if ((ref->agent.state & CHK_ST_ENABLED) || (ref->check.state & CHK_ST_ENABLED)) {
4204 state = SRV_STATS_STATE_GOING_UP;
Willy Tarreauba2f2642016-01-07 09:54:40 +01004205 } else {
4206 state = SRV_STATS_STATE_DOWN; /* DOWN, unchecked */
Willy Tarreauba2f2642016-01-07 09:54:40 +01004207 }
4208 }
4209
Willy Tarreau2b96cf12016-01-06 19:25:38 +01004210 chunk_reset(out);
Willy Tarreau2b96cf12016-01-06 19:25:38 +01004211
4212 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
4213 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, sv->id);
Willy Tarreauf8211df2016-01-11 14:09:38 +01004214 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
Willy Tarreau2b96cf12016-01-06 19:25:38 +01004215 stats[ST_F_QCUR] = mkf_u32(0, sv->nbpend);
4216 stats[ST_F_QMAX] = mkf_u32(FN_MAX, sv->counters.nbpend_max);
4217 stats[ST_F_SCUR] = mkf_u32(0, sv->cur_sess);
4218 stats[ST_F_SMAX] = mkf_u32(FN_MAX, sv->counters.cur_sess_max);
4219
4220 if (sv->maxconn)
4221 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, sv->maxconn);
4222
4223 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, sv->counters.cum_sess);
4224 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, sv->counters.bytes_in);
4225 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, sv->counters.bytes_out);
4226 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, sv->counters.failed_secu);
4227 stats[ST_F_ECON] = mkf_u64(FN_COUNTER, sv->counters.failed_conns);
4228 stats[ST_F_ERESP] = mkf_u64(FN_COUNTER, sv->counters.failed_resp);
4229 stats[ST_F_WRETR] = mkf_u64(FN_COUNTER, sv->counters.retries);
4230 stats[ST_F_WREDIS] = mkf_u64(FN_COUNTER, sv->counters.redispatches);
4231
4232 /* status */
4233 fld_status = chunk_newstr(out);
4234 if (sv->admin & SRV_ADMF_IMAINT)
4235 chunk_appendf(out, "MAINT (via %s/%s)", via->proxy->id, via->id);
4236 else if (sv->admin & SRV_ADMF_MAINT)
4237 chunk_appendf(out, "MAINT");
4238 else
4239 chunk_appendf(out,
4240 srv_hlt_st[state],
4241 (ref->state != SRV_ST_STOPPED) ? (ref->check.health - ref->check.rise + 1) : (ref->check.health),
4242 (ref->state != SRV_ST_STOPPED) ? (ref->check.fall) : (ref->check.rise));
4243
4244 stats[ST_F_STATUS] = mkf_str(FO_STATUS, fld_status);
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004245 stats[ST_F_LASTCHG] = mkf_u32(FN_AGE, now.tv_sec - sv->last_change);
4246 stats[ST_F_WEIGHT] = mkf_u32(FN_AVG, (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv);
4247 stats[ST_F_ACT] = mkf_u32(FO_STATUS, (sv->flags & SRV_F_BACKUP) ? 0 : 1);
4248 stats[ST_F_BCK] = mkf_u32(FO_STATUS, (sv->flags & SRV_F_BACKUP) ? 1 : 0);
Willy Tarreau91861262007-10-17 17:06:05 +02004249
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004250 /* check failures: unique, fatal; last change, total downtime */
4251 if (sv->check.state & CHK_ST_ENABLED) {
4252 stats[ST_F_CHKFAIL] = mkf_u64(FN_COUNTER, sv->counters.failed_checks);
4253 stats[ST_F_CHKDOWN] = mkf_u64(FN_COUNTER, sv->counters.down_trans);
4254 stats[ST_F_DOWNTIME] = mkf_u32(FN_COUNTER, srv_downtime(sv));
4255 }
Willy Tarreau164d4a92016-01-06 19:48:21 +01004256
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004257 if (sv->maxqueue)
4258 stats[ST_F_QLIMIT] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->maxqueue);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004259
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004260 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
4261 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
4262 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, sv->puid);
Willy Tarreau91861262007-10-17 17:06:05 +02004263
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004264 if (sv->state == SRV_ST_STARTING && !server_is_draining(sv))
4265 stats[ST_F_THROTTLE] = mkf_u32(FN_AVG, server_throttle_rate(sv));
Willy Tarreauf5b1cc32014-06-17 12:20:59 +02004266
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004267 stats[ST_F_LBTOT] = mkf_u64(FN_COUNTER, sv->counters.cum_lbconn);
Willy Tarreau91861262007-10-17 17:06:05 +02004268
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004269 if (sv->track) {
4270 char *fld_track = chunk_newstr(out);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004271
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004272 chunk_appendf(out, "%s/%s", sv->track->proxy->id, sv->track->id);
4273 stats[ST_F_TRACKED] = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, fld_track);
4274 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004275
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004276 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_SV);
4277 stats[ST_F_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&sv->sess_per_sec));
4278 stats[ST_F_RATE_MAX] = mkf_u32(FN_MAX, sv->counters.sps_max);
Willy Tarreau164d4a92016-01-06 19:48:21 +01004279
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004280 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) {
4281 const char *fld_chksts;
Willy Tarreau164d4a92016-01-06 19:48:21 +01004282
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004283 fld_chksts = chunk_newstr(out);
4284 chunk_strcat(out, "* "); // for check in progress
4285 chunk_strcat(out, get_check_status_info(sv->check.status));
4286 if (!(sv->check.state & CHK_ST_INPROGRESS))
4287 fld_chksts += 2; // skip "* "
4288 stats[ST_F_CHECK_STATUS] = mkf_str(FN_OUTPUT, fld_chksts);
Willy Tarreau91861262007-10-17 17:06:05 +02004289
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004290 if (sv->check.status >= HCHK_STATUS_L57DATA)
4291 stats[ST_F_CHECK_CODE] = mkf_u32(FN_OUTPUT, sv->check.code);
Willy Tarreaucf2924b2014-05-23 12:15:15 +02004292
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004293 if (sv->check.status >= HCHK_STATUS_CHECKED)
4294 stats[ST_F_CHECK_DURATION] = mkf_u64(FN_DURATION, sv->check.duration);
Willy Tarreaucf2924b2014-05-23 12:15:15 +02004295
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004296 stats[ST_F_CHECK_DESC] = mkf_str(FN_OUTPUT, get_check_status_description(sv->check.status));
4297 stats[ST_F_LAST_CHK] = mkf_str(FN_OUTPUT, sv->check.desc);
4298 stats[ST_F_CHECK_RISE] = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.rise);
4299 stats[ST_F_CHECK_FALL] = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.fall);
4300 stats[ST_F_CHECK_HEALTH] = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.health);
4301 }
Willy Tarreaucf2924b2014-05-23 12:15:15 +02004302
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004303 if ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) {
4304 const char *fld_chksts;
Willy Tarreaudd7354b2016-01-08 13:47:26 +01004305
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004306 fld_chksts = chunk_newstr(out);
4307 chunk_strcat(out, "* "); // for check in progress
4308 chunk_strcat(out, get_check_status_info(sv->agent.status));
4309 if (!(sv->agent.state & CHK_ST_INPROGRESS))
4310 fld_chksts += 2; // skip "* "
4311 stats[ST_F_AGENT_STATUS] = mkf_str(FN_OUTPUT, fld_chksts);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004312
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004313 if (sv->agent.status >= HCHK_STATUS_L57DATA)
4314 stats[ST_F_AGENT_CODE] = mkf_u32(FN_OUTPUT, sv->agent.code);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004315
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004316 if (sv->agent.status >= HCHK_STATUS_CHECKED)
4317 stats[ST_F_AGENT_DURATION] = mkf_u64(FN_DURATION, sv->agent.duration);
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01004318
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004319 stats[ST_F_AGENT_DESC] = mkf_str(FN_OUTPUT, get_check_status_description(sv->agent.status));
4320 stats[ST_F_LAST_AGT] = mkf_str(FN_OUTPUT, sv->agent.desc);
4321 stats[ST_F_AGENT_RISE] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.rise);
4322 stats[ST_F_AGENT_FALL] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.fall);
4323 stats[ST_F_AGENT_HEALTH] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.health);
4324 }
Willy Tarreaudd7354b2016-01-08 13:47:26 +01004325
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004326 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
4327 if (px->mode == PR_MODE_HTTP) {
4328 stats[ST_F_HRSP_1XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[1]);
4329 stats[ST_F_HRSP_2XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[2]);
4330 stats[ST_F_HRSP_3XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[3]);
4331 stats[ST_F_HRSP_4XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[4]);
4332 stats[ST_F_HRSP_5XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[5]);
4333 stats[ST_F_HRSP_OTHER] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[0]);
4334 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004335
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004336 if (ref->observe)
4337 stats[ST_F_HANAFAIL] = mkf_u64(FN_COUNTER, sv->counters.failed_hana);
Willy Tarreau164d4a92016-01-06 19:48:21 +01004338
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004339 stats[ST_F_CLI_ABRT] = mkf_u64(FN_COUNTER, sv->counters.cli_aborts);
4340 stats[ST_F_SRV_ABRT] = mkf_u64(FN_COUNTER, sv->counters.srv_aborts);
4341 stats[ST_F_LASTSESS] = mkf_s32(FN_AGE, srv_lastsession(sv));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004342
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004343 stats[ST_F_QTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.q_time, TIME_STATS_SAMPLES));
4344 stats[ST_F_CTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.c_time, TIME_STATS_SAMPLES));
4345 stats[ST_F_RTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.d_time, TIME_STATS_SAMPLES));
4346 stats[ST_F_TTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.t_time, TIME_STATS_SAMPLES));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004347
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004348 if (flags & ST_SHLGNDS) {
4349 switch (addr_to_str(&sv->addr, str, sizeof(str))) {
4350 case AF_INET:
4351 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
4352 chunk_appendf(out, "%s:%d", str, get_host_port(&sv->addr));
4353 break;
4354 case AF_INET6:
4355 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
4356 chunk_appendf(out, "[%s]:%d", str, get_host_port(&sv->addr));
4357 break;
4358 case AF_UNIX:
4359 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, "unix");
4360 break;
4361 case -1:
4362 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
4363 chunk_strcat(out, strerror(errno));
4364 break;
4365 default: /* address family not supported */
4366 break;
Willy Tarreauf4659942013-11-28 10:50:06 +01004367 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004368
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004369 if (sv->cookie)
4370 stats[ST_F_COOKIE] = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, sv->cookie);
4371 }
4372
Thierry Fournier61fe6c02016-03-25 08:21:21 +01004373 return 1;
4374}
4375
4376/* Dumps a line for server <sv> and proxy <px> to the trash and uses the state
4377 * from stream interface <si>, stats flags <flags>, and server state <state>.
4378 * The caller is responsible for clearing the trash if needed. Returns non-zero
4379 * if it emits anything, zero otherwise.
4380 */
4381static int stats_dump_sv_stats(struct stream_interface *si, struct proxy *px, int flags, struct server *sv)
4382{
4383 struct appctx *appctx = __objt_appctx(si->end);
4384
4385 if (!stats_fill_sv_stats(px, sv, flags, stats, ST_F_TOTAL_FIELDS))
4386 return 0;
4387
Willy Tarreau501f6022016-01-08 17:37:22 +01004388 return stats_dump_one_line(stats, flags, px, appctx);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004389}
4390
Thierry Fournierd0a56c22016-03-25 08:21:51 +01004391/* Fill <stats> with the backend statistics. <stats> is
4392 * preallocated array of length <len>. The length of the array
4393 * must be at least ST_F_TOTAL_FIELDS. If this length is less
4394 * then this value, the function returns 0, otherwise, it
4395 * returns 1. <flags> can take the value ST_SHLGNDS.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004396 */
Thierry Fournierd0a56c22016-03-25 08:21:51 +01004397int stats_fill_be_stats(struct proxy *px, int flags, struct field *stats, int len)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004398{
Thierry Fournierd0a56c22016-03-25 08:21:51 +01004399 if (len < ST_F_TOTAL_FIELDS)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004400 return 0;
4401
Thierry Fournierd0a56c22016-03-25 08:21:51 +01004402 memset(stats, 0, sizeof(*stats) * len);
Willy Tarreauf6eecbe2016-01-06 16:14:50 +01004403
4404 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
4405 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, "BACKEND");
Willy Tarreauf8211df2016-01-11 14:09:38 +01004406 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
Willy Tarreauf6eecbe2016-01-06 16:14:50 +01004407 stats[ST_F_QCUR] = mkf_u32(0, px->nbpend);
4408 stats[ST_F_QMAX] = mkf_u32(FN_MAX, px->be_counters.nbpend_max);
4409 stats[ST_F_SCUR] = mkf_u32(FO_CONFIG|FN_LIMIT, px->beconn);
4410 stats[ST_F_SMAX] = mkf_u32(FN_MAX, px->be_counters.conn_max);
4411 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, px->fullconn);
4412 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, px->be_counters.cum_conn);
4413 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, px->be_counters.bytes_in);
4414 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, px->be_counters.bytes_out);
4415 stats[ST_F_DREQ] = mkf_u64(FN_COUNTER, px->be_counters.denied_req);
4416 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, px->be_counters.denied_resp);
4417 stats[ST_F_ECON] = mkf_u64(FN_COUNTER, px->be_counters.failed_conns);
4418 stats[ST_F_ERESP] = mkf_u64(FN_COUNTER, px->be_counters.failed_resp);
4419 stats[ST_F_WRETR] = mkf_u64(FN_COUNTER, px->be_counters.retries);
4420 stats[ST_F_WREDIS] = mkf_u64(FN_COUNTER, px->be_counters.redispatches);
4421 stats[ST_F_STATUS] = mkf_str(FO_STATUS, (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" : "DOWN");
4422 stats[ST_F_WEIGHT] = mkf_u32(FN_AVG, (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv);
4423 stats[ST_F_ACT] = mkf_u32(0, px->srv_act);
4424 stats[ST_F_BCK] = mkf_u32(0, px->srv_bck);
4425 stats[ST_F_CHKDOWN] = mkf_u64(FN_COUNTER, px->down_trans);
4426 stats[ST_F_LASTCHG] = mkf_u32(FN_AGE, now.tv_sec - px->last_change);
Willy Tarreau7344f472016-01-11 12:04:02 +01004427 if (px->srv)
4428 stats[ST_F_DOWNTIME] = mkf_u32(FN_COUNTER, be_downtime(px));
4429
Willy Tarreauf6eecbe2016-01-06 16:14:50 +01004430 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
4431 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
4432 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, 0);
4433 stats[ST_F_LBTOT] = mkf_u64(FN_COUNTER, px->be_counters.cum_lbconn);
4434 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_BE);
4435 stats[ST_F_RATE] = mkf_u32(0, read_freq_ctr(&px->be_sess_per_sec));
4436 stats[ST_F_RATE_MAX] = mkf_u32(0, px->be_counters.sps_max);
Willy Tarreauf1516d92016-01-11 14:48:36 +01004437
Willy Tarreaue4847c62016-01-08 15:43:54 +01004438 if (flags & ST_SHLGNDS) {
4439 if (px->cookie_name)
4440 stats[ST_F_COOKIE] = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, px->cookie_name);
Willy Tarreauf1516d92016-01-11 14:48:36 +01004441 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 +01004442 }
Willy Tarreauf6eecbe2016-01-06 16:14:50 +01004443
4444 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
4445 if (px->mode == PR_MODE_HTTP) {
4446 stats[ST_F_REQ_TOT] = mkf_u64(FN_COUNTER, px->be_counters.p.http.cum_req);
4447 stats[ST_F_HRSP_1XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[1]);
4448 stats[ST_F_HRSP_2XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[2]);
4449 stats[ST_F_HRSP_3XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[3]);
4450 stats[ST_F_HRSP_4XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[4]);
4451 stats[ST_F_HRSP_5XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[5]);
4452 stats[ST_F_HRSP_OTHER] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[0]);
Willy Tarreau5b9bdff2016-01-11 14:40:47 +01004453 stats[ST_F_INTERCEPTED] = mkf_u64(FN_COUNTER, px->be_counters.intercepted_req);
Willy Tarreauf6eecbe2016-01-06 16:14:50 +01004454 }
4455
4456 stats[ST_F_CLI_ABRT] = mkf_u64(FN_COUNTER, px->be_counters.cli_aborts);
4457 stats[ST_F_SRV_ABRT] = mkf_u64(FN_COUNTER, px->be_counters.srv_aborts);
4458
4459 /* compression: in, out, bypassed, responses */
4460 stats[ST_F_COMP_IN] = mkf_u64(FN_COUNTER, px->be_counters.comp_in);
4461 stats[ST_F_COMP_OUT] = mkf_u64(FN_COUNTER, px->be_counters.comp_out);
4462 stats[ST_F_COMP_BYP] = mkf_u64(FN_COUNTER, px->be_counters.comp_byp);
4463 stats[ST_F_COMP_RSP] = mkf_u64(FN_COUNTER, px->be_counters.p.http.comp_rsp);
4464 stats[ST_F_LASTSESS] = mkf_s32(FN_AGE, be_lastsession(px));
4465
4466 stats[ST_F_QTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.q_time, TIME_STATS_SAMPLES));
4467 stats[ST_F_CTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.c_time, TIME_STATS_SAMPLES));
4468 stats[ST_F_RTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.d_time, TIME_STATS_SAMPLES));
4469 stats[ST_F_TTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.t_time, TIME_STATS_SAMPLES));
4470
Thierry Fournierd0a56c22016-03-25 08:21:51 +01004471 return 1;
4472}
4473
4474/* Dumps a line for backend <px> to the trash for and uses the state from stream
4475 * interface <si> and stats flags <flags>. The caller is responsible for clearing
4476 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
4477 */
4478static int stats_dump_be_stats(struct stream_interface *si, struct proxy *px, int flags)
4479{
4480 struct appctx *appctx = __objt_appctx(si->end);
4481
4482 if (!(px->cap & PR_CAP_BE))
4483 return 0;
4484
4485 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_BE)))
4486 return 0;
4487
4488 if (!stats_fill_be_stats(px, flags, stats, ST_F_TOTAL_FIELDS))
4489 return 0;
4490
Willy Tarreau501f6022016-01-08 17:37:22 +01004491 return stats_dump_one_line(stats, flags, px, appctx);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004492}
4493
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004494/* Dumps the HTML table header for proxy <px> to the trash for and uses the state from
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004495 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004496 * for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004497 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004498static void stats_dump_html_px_hdr(struct stream_interface *si, struct proxy *px, struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004499{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004500 struct appctx *appctx = __objt_appctx(si->end);
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004501 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
4502
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004503 if (px->cap & PR_CAP_BE && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004504 /* A form to enable/disable this proxy servers */
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004505
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004506 /* 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 +02004507 scope_txt[0] = 0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004508 if (appctx->ctx.stats.scope_len) {
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004509 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau4e4292b2014-11-28 12:18:45 +01004510 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 +01004511 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004512 }
4513
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004514 chunk_appendf(&trash,
Jeff Buchbinder2dbbf4d2014-08-29 15:10:08 -05004515 "<form method=\"post\">");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004516 }
4517
4518 /* print a new table */
4519 chunk_appendf(&trash,
4520 "<table class=\"tbl\" width=\"100%%\">\n"
4521 "<tr class=\"titre\">"
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01004522 "<th class=\"pxname\" width=\"10%%\">");
4523
4524 chunk_appendf(&trash,
4525 "<a name=\"%s\"></a>%s"
4526 "<a class=px href=\"#%s\">%s</a>",
4527 px->id,
4528 (uri->flags & ST_SHLGNDS) ? "<u>":"",
4529 px->id, px->id);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004530
4531 if (uri->flags & ST_SHLGNDS) {
4532 /* cap, mode, id */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02004533 chunk_appendf(&trash, "<div class=tips>cap: %s, mode: %s, id: %d",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004534 proxy_cap_str(px->cap), proxy_mode_str(px->mode),
4535 px->uuid);
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01004536 chunk_appendf(&trash, "</div>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004537 }
4538
4539 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01004540 "%s</th>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004541 "<th class=\"%s\" width=\"90%%\">%s</th>"
4542 "</tr>\n"
4543 "</table>\n"
4544 "<table class=\"tbl\" width=\"100%%\">\n"
4545 "<tr class=\"titre\">",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004546 (uri->flags & ST_SHLGNDS) ? "</u>":"",
4547 px->desc ? "desc" : "empty", px->desc ? px->desc : "");
4548
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004549 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004550 /* Column heading for Enable or Disable server */
4551 chunk_appendf(&trash, "<th rowspan=2 width=1></th>");
Willy Tarreau91861262007-10-17 17:06:05 +02004552 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004553
4554 chunk_appendf(&trash,
4555 "<th rowspan=2></th>"
4556 "<th colspan=3>Queue</th>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05004557 "<th colspan=3>Session rate</th><th colspan=6>Sessions</th>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004558 "<th colspan=2>Bytes</th><th colspan=2>Denied</th>"
4559 "<th colspan=3>Errors</th><th colspan=2>Warnings</th>"
4560 "<th colspan=9>Server</th>"
4561 "</tr>\n"
4562 "<tr class=\"titre\">"
4563 "<th>Cur</th><th>Max</th><th>Limit</th>"
4564 "<th>Cur</th><th>Max</th><th>Limit</th><th>Cur</th><th>Max</th>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05004565 "<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 +01004566 "<th>Req</th><th>Resp</th><th>Req</th><th>Conn</th>"
4567 "<th>Resp</th><th>Retr</th><th>Redis</th>"
4568 "<th>Status</th><th>LastChk</th><th>Wght</th><th>Act</th>"
4569 "<th>Bck</th><th>Chk</th><th>Dwn</th><th>Dwntme</th>"
4570 "<th>Thrtle</th>\n"
4571 "</tr>");
Willy Tarreau91861262007-10-17 17:06:05 +02004572}
4573
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004574/* Dumps the HTML table trailer for proxy <px> to the trash for and uses the state from
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004575 * stream interface <si>. The caller is responsible for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004576 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004577static void stats_dump_html_px_end(struct stream_interface *si, struct proxy *px)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004578{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004579 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004580 chunk_appendf(&trash, "</table>");
4581
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004582 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004583 /* close the form used to enable/disable this proxy servers */
4584 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004585 "Choose the action to perform on the checked servers : "
4586 "<select name=action>"
4587 "<option value=\"\"></option>"
Willy Tarreaued7df902014-05-22 18:04:49 +02004588 "<option value=\"ready\">Set state to READY</option>"
4589 "<option value=\"drain\">Set state to DRAIN</option>"
Marco Corte8c27bca2014-07-02 17:49:34 +02004590 "<option value=\"maint\">Set state to MAINT</option>"
Willy Tarreau248a60e2014-05-23 14:59:48 +02004591 "<option value=\"dhlth\">Health: disable checks</option>"
4592 "<option value=\"ehlth\">Health: enable checks</option>"
4593 "<option value=\"hrunn\">Health: force UP</option>"
4594 "<option value=\"hnolb\">Health: force NOLB</option>"
4595 "<option value=\"hdown\">Health: force DOWN</option>"
4596 "<option value=\"dagent\">Agent: disable checks</option>"
4597 "<option value=\"eagent\">Agent: enable checks</option>"
4598 "<option value=\"arunn\">Agent: force UP</option>"
4599 "<option value=\"adown\">Agent: force DOWN</option>"
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004600 "<option value=\"shutdown\">Kill Sessions</option>"
4601 "</select>"
4602 "<input type=\"hidden\" name=\"b\" value=\"#%d\">"
4603 "&nbsp;<input type=\"submit\" value=\"Apply\">"
4604 "</form>",
4605 px->uuid);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004606 }
4607
4608 chunk_appendf(&trash, "<p>\n");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004609}
Willy Tarreau91861262007-10-17 17:06:05 +02004610
4611/*
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004612 * Dumps statistics for a proxy. The output is sent to the stream interface's
4613 * input buffer. Returns 0 if it had to stop dumping data because of lack of
4614 * buffer space, or non-zero if everything completed. This function is used
4615 * both by the CLI and the HTTP entry points, and is able to dump the output
4616 * in HTML or CSV formats. If the later, <uri> must be NULL.
Willy Tarreau91861262007-10-17 17:06:05 +02004617 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004618static int stats_dump_proxy_to_buffer(struct stream_interface *si, struct proxy *px, struct uri_auth *uri)
Willy Tarreau91861262007-10-17 17:06:05 +02004619{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004620 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau87b09662015-04-03 00:22:06 +02004621 struct stream *s = si_strm(si);
Willy Tarreau2bb4a962014-11-28 11:11:05 +01004622 struct channel *rep = si_ic(si);
Willy Tarreau44267702011-10-28 15:35:33 +02004623 struct server *sv, *svs; /* server and server-state, server-state=server or server->track */
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004624 struct listener *l;
Willy Tarreau0deb85a2016-01-08 15:33:18 +01004625 unsigned int flags;
4626
4627 if (uri)
4628 flags = uri->flags;
4629 else if (strm_li(s)->bind_conf->level >= ACCESS_LVL_OPER)
4630 flags = ST_SHLGNDS | ST_SHNODE | ST_SHDESC;
4631 else
4632 flags = ST_SHNODE | ST_SHDESC;
Willy Tarreau91861262007-10-17 17:06:05 +02004633
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004634 chunk_reset(&trash);
Willy Tarreau91861262007-10-17 17:06:05 +02004635
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004636 switch (appctx->ctx.stats.px_st) {
Willy Tarreau295a8372011-03-10 11:25:07 +01004637 case STAT_PX_ST_INIT:
Willy Tarreau91861262007-10-17 17:06:05 +02004638 /* we are on a new proxy */
Willy Tarreau91861262007-10-17 17:06:05 +02004639 if (uri && uri->scope) {
4640 /* we have a limited scope, we have to check the proxy name */
4641 struct stat_scope *scope;
4642 int len;
4643
4644 len = strlen(px->id);
4645 scope = uri->scope;
4646
4647 while (scope) {
4648 /* match exact proxy name */
4649 if (scope->px_len == len && !memcmp(px->id, scope->px_id, len))
4650 break;
4651
4652 /* match '.' which means 'self' proxy */
Willy Tarreau1388a3a2007-10-18 16:38:37 +02004653 if (!strcmp(scope->px_id, ".") && px == s->be)
Willy Tarreau91861262007-10-17 17:06:05 +02004654 break;
4655 scope = scope->next;
4656 }
4657
4658 /* proxy name not found : don't dump anything */
4659 if (scope == NULL)
4660 return 1;
4661 }
4662
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004663 /* if the user has requested a limited output and the proxy
4664 * name does not match, skip it.
4665 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004666 if (appctx->ctx.stats.scope_len &&
Willy Tarreau4e4292b2014-11-28 12:18:45 +01004667 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 +02004668 return 1;
4669
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004670 if ((appctx->ctx.stats.flags & STAT_BOUND) &&
4671 (appctx->ctx.stats.iid != -1) &&
4672 (px->uuid != appctx->ctx.stats.iid))
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01004673 return 1;
4674
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004675 appctx->ctx.stats.px_st = STAT_PX_ST_TH;
Willy Tarreau91861262007-10-17 17:06:05 +02004676 /* fall through */
4677
Willy Tarreau295a8372011-03-10 11:25:07 +01004678 case STAT_PX_ST_TH:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004679 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004680 stats_dump_html_px_hdr(si, px, uri);
Willy Tarreaubc18da12015-03-13 14:00:47 +01004681 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004682 si_applet_cant_put(si);
Willy Tarreau55bb8452007-10-17 18:44:57 +02004683 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01004684 }
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004685 }
Willy Tarreau91861262007-10-17 17:06:05 +02004686
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004687 appctx->ctx.stats.px_st = STAT_PX_ST_FE;
Willy Tarreau91861262007-10-17 17:06:05 +02004688 /* fall through */
4689
Willy Tarreau295a8372011-03-10 11:25:07 +01004690 case STAT_PX_ST_FE:
Willy Tarreau91861262007-10-17 17:06:05 +02004691 /* print the frontend */
Willy Tarreaubc18da12015-03-13 14:00:47 +01004692 if (stats_dump_fe_stats(si, px)) {
4693 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004694 si_applet_cant_put(si);
Willy Tarreau91861262007-10-17 17:06:05 +02004695 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01004696 }
4697 }
Willy Tarreau91861262007-10-17 17:06:05 +02004698
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004699 appctx->ctx.stats.l = px->conf.listeners.n;
4700 appctx->ctx.stats.px_st = STAT_PX_ST_LI;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004701 /* fall through */
4702
Willy Tarreau295a8372011-03-10 11:25:07 +01004703 case STAT_PX_ST_LI:
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004704 /* stats.l has been initialized above */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004705 for (; appctx->ctx.stats.l != &px->conf.listeners; appctx->ctx.stats.l = l->by_fe.n) {
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01004706 if (buffer_almost_full(rep->buf)) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004707 si_applet_cant_put(si);
Willy Tarreau4e33d862009-10-11 23:35:10 +02004708 return 0;
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01004709 }
Willy Tarreau4e33d862009-10-11 23:35:10 +02004710
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004711 l = LIST_ELEM(appctx->ctx.stats.l, struct listener *, by_fe);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004712 if (!l->counters)
4713 continue;
4714
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004715 if (appctx->ctx.stats.flags & STAT_BOUND) {
4716 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SO)))
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004717 break;
4718
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004719 if (appctx->ctx.stats.sid != -1 && l->luid != appctx->ctx.stats.sid)
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004720 continue;
4721 }
4722
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004723 /* print the frontend */
Willy Tarreau0deb85a2016-01-08 15:33:18 +01004724 if (stats_dump_li_stats(si, px, l, flags)) {
Willy Tarreaubc18da12015-03-13 14:00:47 +01004725 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004726 si_applet_cant_put(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004727 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01004728 }
4729 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004730 }
4731
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004732 appctx->ctx.stats.sv = px->srv; /* may be NULL */
4733 appctx->ctx.stats.px_st = STAT_PX_ST_SV;
Willy Tarreau91861262007-10-17 17:06:05 +02004734 /* fall through */
4735
Willy Tarreau295a8372011-03-10 11:25:07 +01004736 case STAT_PX_ST_SV:
Willy Tarreau91861262007-10-17 17:06:05 +02004737 /* stats.sv has been initialized above */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004738 for (; appctx->ctx.stats.sv != NULL; appctx->ctx.stats.sv = sv->next) {
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01004739 if (buffer_almost_full(rep->buf)) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004740 si_applet_cant_put(si);
Willy Tarreau4e33d862009-10-11 23:35:10 +02004741 return 0;
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01004742 }
Willy Tarreau4e33d862009-10-11 23:35:10 +02004743
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004744 sv = appctx->ctx.stats.sv;
Willy Tarreau91861262007-10-17 17:06:05 +02004745
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004746 if (appctx->ctx.stats.flags & STAT_BOUND) {
4747 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SV)))
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01004748 break;
4749
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004750 if (appctx->ctx.stats.sid != -1 && sv->puid != appctx->ctx.stats.sid)
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01004751 continue;
4752 }
4753
Willy Tarreau32091232014-05-16 13:52:00 +02004754 svs = sv;
4755 while (svs->track)
4756 svs = svs->track;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01004757
Willy Tarreauba2f2642016-01-07 09:54:40 +01004758 /* do not report servers which are DOWN and not changing state */
4759 if ((appctx->ctx.stats.flags & STAT_HIDE_DOWN) &&
4760 ((sv->admin & SRV_ADMF_MAINT) || /* server is in maintenance */
4761 (sv->state == SRV_ST_STOPPED && /* server is down */
4762 (!((svs->agent.state | svs->check.state) & CHK_ST_ENABLED) ||
4763 ((svs->agent.state & CHK_ST_ENABLED) && !svs->agent.health) ||
4764 ((svs->check.state & CHK_ST_ENABLED) && !svs->check.health))))) {
Willy Tarreau91861262007-10-17 17:06:05 +02004765 continue;
4766 }
4767
Willy Tarreau0deb85a2016-01-08 15:33:18 +01004768 if (stats_dump_sv_stats(si, px, flags, sv)) {
Willy Tarreaubc18da12015-03-13 14:00:47 +01004769 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004770 si_applet_cant_put(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004771 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01004772 }
4773 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004774 } /* for sv */
Cyril Bonté474be412010-10-12 00:14:36 +02004775
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004776 appctx->ctx.stats.px_st = STAT_PX_ST_BE;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004777 /* fall through */
Cyril Bonté70be45d2010-10-12 00:14:35 +02004778
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004779 case STAT_PX_ST_BE:
4780 /* print the backend */
Willy Tarreau0deb85a2016-01-08 15:33:18 +01004781 if (stats_dump_be_stats(si, px, flags)) {
Willy Tarreaubc18da12015-03-13 14:00:47 +01004782 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004783 si_applet_cant_put(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004784 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01004785 }
4786 }
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01004787
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004788 appctx->ctx.stats.px_st = STAT_PX_ST_END;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004789 /* fall through */
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01004790
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004791 case STAT_PX_ST_END:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004792 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004793 stats_dump_html_px_end(si, px);
Willy Tarreaubc18da12015-03-13 14:00:47 +01004794 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004795 si_applet_cant_put(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004796 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01004797 }
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004798 }
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01004799
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004800 appctx->ctx.stats.px_st = STAT_PX_ST_FIN;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004801 /* fall through */
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01004802
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004803 case STAT_PX_ST_FIN:
4804 return 1;
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01004805
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004806 default:
4807 /* unknown state, we should put an abort() here ! */
4808 return 1;
4809 }
4810}
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01004811
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004812/* Dumps the HTTP stats head block to the trash for and uses the per-uri
4813 * parameters <uri>. The caller is responsible for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004814 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004815static void stats_dump_html_head(struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004816{
4817 /* WARNING! This must fit in the first buffer !!! */
4818 chunk_appendf(&trash,
4819 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n"
4820 "\"http://www.w3.org/TR/html4/loose.dtd\">\n"
4821 "<html><head><title>Statistics Report for " PRODUCT_NAME "%s%s</title>\n"
4822 "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n"
4823 "<style type=\"text/css\"><!--\n"
4824 "body {"
4825 " font-family: arial, helvetica, sans-serif;"
4826 " font-size: 12px;"
4827 " font-weight: normal;"
4828 " color: black;"
4829 " background: white;"
4830 "}\n"
4831 "th,td {"
4832 " font-size: 10px;"
4833 "}\n"
4834 "h1 {"
4835 " font-size: x-large;"
4836 " margin-bottom: 0.5em;"
4837 "}\n"
4838 "h2 {"
4839 " font-family: helvetica, arial;"
4840 " font-size: x-large;"
4841 " font-weight: bold;"
4842 " font-style: italic;"
4843 " color: #6020a0;"
4844 " margin-top: 0em;"
4845 " margin-bottom: 0em;"
4846 "}\n"
4847 "h3 {"
4848 " font-family: helvetica, arial;"
4849 " font-size: 16px;"
4850 " font-weight: bold;"
4851 " color: #b00040;"
4852 " background: #e8e8d0;"
4853 " margin-top: 0em;"
4854 " margin-bottom: 0em;"
4855 "}\n"
4856 "li {"
4857 " margin-top: 0.25em;"
4858 " margin-right: 2em;"
4859 "}\n"
4860 ".hr {margin-top: 0.25em;"
4861 " border-color: black;"
4862 " border-bottom-style: solid;"
4863 "}\n"
4864 ".titre {background: #20D0D0;color: #000000; font-weight: bold; text-align: center;}\n"
4865 ".total {background: #20D0D0;color: #ffff80;}\n"
4866 ".frontend {background: #e8e8d0;}\n"
4867 ".socket {background: #d0d0d0;}\n"
4868 ".backend {background: #e8e8d0;}\n"
Simon Horman837bfa72015-04-23 14:51:27 +09004869 ".active_down {background: #ff9090;}\n"
4870 ".active_going_up {background: #ffd020;}\n"
4871 ".active_going_down {background: #ffffa0;}\n"
4872 ".active_up {background: #c0ffc0;}\n"
4873 ".active_nolb {background: #20a0ff;}\n"
4874 ".active_draining {background: #20a0FF;}\n"
4875 ".active_no_check {background: #e0e0e0;}\n"
4876 ".backup_down {background: #ff9090;}\n"
4877 ".backup_going_up {background: #ff80ff;}\n"
4878 ".backup_going_down {background: #c060ff;}\n"
4879 ".backup_up {background: #b0d0ff;}\n"
4880 ".backup_nolb {background: #90b0e0;}\n"
4881 ".backup_draining {background: #cc9900;}\n"
4882 ".backup_no_check {background: #e0e0e0;}\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004883 ".maintain {background: #c07820;}\n"
4884 ".rls {letter-spacing: 0.2em; margin-right: 1px;}\n" /* right letter spacing (used for grouping digits) */
4885 "\n"
4886 "a.px:link {color: #ffff40; text-decoration: none;}"
4887 "a.px:visited {color: #ffff40; text-decoration: none;}"
4888 "a.px:hover {color: #ffffff; text-decoration: none;}"
4889 "a.lfsb:link {color: #000000; text-decoration: none;}"
4890 "a.lfsb:visited {color: #000000; text-decoration: none;}"
4891 "a.lfsb:hover {color: #505050; text-decoration: none;}"
4892 "\n"
4893 "table.tbl { border-collapse: collapse; border-style: none;}\n"
4894 "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"
4895 "table.tbl td.ac { text-align: center;}\n"
4896 "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}\n"
4897 "table.tbl th.pxname { background: #b00040; color: #ffff40; font-weight: bold; border-style: solid solid none solid; padding: 2px 3px; white-space: nowrap;}\n"
4898 "table.tbl th.empty { border-style: none; empty-cells: hide; background: white;}\n"
4899 "table.tbl th.desc { background: white; border-style: solid solid none solid; text-align: left; padding: 2px 3px;}\n"
4900 "\n"
4901 "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n"
4902 "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n"
4903 "table.lgd td.noborder { border-style: none; padding: 2px; white-space: nowrap;}\n"
Willy Tarreau466c9b52012-12-23 02:25:03 +01004904 "table.det { border-collapse: collapse; border-style: none; }\n"
4905 "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 +01004906 "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 +01004907 "u {text-decoration:none; border-bottom: 1px dotted black;}\n"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02004908 "div.tips {\n"
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01004909 " display:block;\n"
4910 " visibility:hidden;\n"
4911 " z-index:2147483647;\n"
4912 " position:absolute;\n"
4913 " padding:2px 4px 3px;\n"
4914 " background:#f0f060; color:#000000;\n"
4915 " border:1px solid #7040c0;\n"
4916 " white-space:nowrap;\n"
4917 " font-style:normal;font-size:11px;font-weight:normal;\n"
4918 " -moz-border-radius:3px;-webkit-border-radius:3px;border-radius:3px;\n"
4919 " -moz-box-shadow:gray 2px 2px 3px;-webkit-box-shadow:gray 2px 2px 3px;box-shadow:gray 2px 2px 3px;\n"
4920 "}\n"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02004921 "u:hover div.tips {visibility:visible;}\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004922 "-->\n"
4923 "</style></head>\n",
4924 (uri->flags & ST_SHNODE) ? " on " : "",
4925 (uri->flags & ST_SHNODE) ? (uri->node ? uri->node : global.node) : ""
4926 );
4927}
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01004928
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004929/* Dumps the HTML stats information block to the trash for and uses the state from
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004930 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004931 * for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004932 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004933static void stats_dump_html_info(struct stream_interface *si, struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004934{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004935 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004936 unsigned int up = (now.tv_sec - start_date.tv_sec);
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004937 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01004938
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004939 /* WARNING! this has to fit the first packet too.
4940 * We are around 3.5 kB, add adding entries will
4941 * become tricky if we want to support 4kB buffers !
4942 */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004943 chunk_appendf(&trash,
4944 "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">"
4945 PRODUCT_NAME "%s</a></h1>\n"
4946 "<h2>Statistics Report for pid %d%s%s%s%s</h2>\n"
4947 "<hr width=\"100%%\" class=\"hr\">\n"
4948 "<h3>&gt; General process information</h3>\n"
4949 "<table border=0><tr><td align=\"left\" nowrap width=\"1%%\">\n"
4950 "<p><b>pid = </b> %d (process #%d, nbproc = %d)<br>\n"
4951 "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n"
4952 "<b>system limits:</b> memmax = %s%s; ulimit-n = %d<br>\n"
4953 "<b>maxsock = </b> %d; <b>maxconn = </b> %d; <b>maxpipes = </b> %d<br>\n"
4954 "current conns = %d; current pipes = %d/%d; conn rate = %d/sec<br>\n"
4955 "Running tasks: %d/%d; idle = %d %%<br>\n"
4956 "</td><td align=\"center\" nowrap>\n"
4957 "<table class=\"lgd\"><tr>\n"
Simon Horman837bfa72015-04-23 14:51:27 +09004958 "<td class=\"active_up\">&nbsp;</td><td class=\"noborder\">active UP </td>"
4959 "<td class=\"backup_up\">&nbsp;</td><td class=\"noborder\">backup UP </td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004960 "</tr><tr>\n"
Simon Horman837bfa72015-04-23 14:51:27 +09004961 "<td class=\"active_going_down\"></td><td class=\"noborder\">active UP, going down </td>"
4962 "<td class=\"backup_going_down\"></td><td class=\"noborder\">backup UP, going down </td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004963 "</tr><tr>\n"
Simon Horman837bfa72015-04-23 14:51:27 +09004964 "<td class=\"active_going_up\"></td><td class=\"noborder\">active DOWN, going up </td>"
4965 "<td class=\"backup_going_up\"></td><td class=\"noborder\">backup DOWN, going up </td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004966 "</tr><tr>\n"
Simon Horman837bfa72015-04-23 14:51:27 +09004967 "<td class=\"active_down\"></td><td class=\"noborder\">active or backup DOWN &nbsp;</td>"
4968 "<td class=\"active_no_check\"></td><td class=\"noborder\">not checked </td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004969 "</tr><tr>\n"
4970 "<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 -07004971 "</tr><tr>\n"
Simon Horman837bfa72015-04-23 14:51:27 +09004972 "<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 +01004973 "</tr></table>\n"
Willy Tarreau6b7764a2013-12-04 00:43:21 +01004974 "Note: \"NOLB\"/\"DRAIN\" = UP with load-balancing disabled."
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004975 "</td>"
4976 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
4977 "<b>Display option:</b><ul style=\"margin-top: 0.25em;\">"
4978 "",
4979 (uri->flags & ST_HIDEVER) ? "" : (STATS_VERSION_STRING),
4980 pid, (uri->flags & ST_SHNODE) ? " on " : "",
4981 (uri->flags & ST_SHNODE) ? (uri->node ? uri->node : global.node) : "",
4982 (uri->flags & ST_SHDESC) ? ": " : "",
4983 (uri->flags & ST_SHDESC) ? (uri->desc ? uri->desc : global.desc) : "",
4984 pid, relative_pid, global.nbproc,
4985 up / 86400, (up % 86400) / 3600,
4986 (up % 3600) / 60, (up % 60),
4987 global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited",
4988 global.rlimit_memmax ? " MB" : "",
4989 global.rlimit_nofile,
4990 global.maxsock, global.maxconn, global.maxpipes,
4991 actconn, pipes_used, pipes_used+pipes_free, read_freq_ctr(&global.conn_per_sec),
4992 run_queue_cur, nb_tasks_cur, idle_pct
4993 );
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004994
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004995 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Willy Tarreau4e4292b2014-11-28 12:18:45 +01004996 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 +01004997 scope_txt[appctx->ctx.stats.scope_len] = '\0';
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004998
4999 chunk_appendf(&trash,
Jeff Buchbinder2dbbf4d2014-08-29 15:10:08 -05005000 "<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 +01005001 (appctx->ctx.stats.scope_len > 0) ? scope_txt : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02005002 STAT_SCOPE_TXT_MAXLEN);
5003
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005004 /* 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 +02005005 scope_txt[0] = 0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005006 if (appctx->ctx.stats.scope_len) {
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02005007 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau4e4292b2014-11-28 12:18:45 +01005008 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 +01005009 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02005010 }
5011
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005012 if (appctx->ctx.stats.flags & STAT_HIDE_DOWN)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005013 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02005014 "<li><a href=\"%s%s%s%s\">Show all servers</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005015 uri->uri_prefix,
5016 "",
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 Tarreaud9bdcd52012-12-22 20:31:10 +01005019 else
5020 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02005021 "<li><a href=\"%s%s%s%s\">Hide 'DOWN' servers</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005022 uri->uri_prefix,
5023 ";up",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005024 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02005025 scope_txt);
Willy Tarreau91861262007-10-17 17:06:05 +02005026
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005027 if (uri->refresh > 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005028 if (appctx->ctx.stats.flags & STAT_NO_REFRESH)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005029 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02005030 "<li><a href=\"%s%s%s%s\">Enable 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 "",
5034 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005035 else
5036 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02005037 "<li><a href=\"%s%s%s%s\">Disable refresh</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005038 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005039 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02005040 ";norefresh",
5041 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005042 }
Willy Tarreau55bb8452007-10-17 18:44:57 +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%s%s%s\">Refresh now</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005046 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005047 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
5048 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02005049 scope_txt);
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02005050
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005051 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02005052 "<li><a href=\"%s;csv%s%s\">CSV export</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005053 uri->uri_prefix,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02005054 (uri->refresh > 0) ? ";norefresh" : "",
5055 scope_txt);
Willy Tarreau4bab24d2007-11-30 18:16:29 +01005056
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005057 chunk_appendf(&trash,
5058 "</ul></td>"
5059 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
5060 "<b>External resources:</b><ul style=\"margin-top: 0.25em;\">\n"
5061 "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>\n"
5062 "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>\n"
5063 "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>\n"
5064 "</ul>"
5065 "</td>"
5066 "</tr></table>\n"
5067 ""
5068 );
Willy Tarreau4bab24d2007-11-30 18:16:29 +01005069
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005070 if (appctx->ctx.stats.st_code) {
5071 switch (appctx->ctx.stats.st_code) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005072 case STAT_STATUS_DONE:
5073 chunk_appendf(&trash,
Simon Horman837bfa72015-04-23 14:51:27 +09005074 "<p><div class=active_up>"
Willy Tarreauba6be982013-04-19 12:16:55 +02005075 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005076 "Action processed successfully."
Willy Tarreauba6be982013-04-19 12:16:55 +02005077 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005078 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
5079 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02005080 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005081 break;
5082 case STAT_STATUS_NONE:
5083 chunk_appendf(&trash,
Simon Horman837bfa72015-04-23 14:51:27 +09005084 "<p><div class=active_going_down>"
Willy Tarreauba6be982013-04-19 12:16:55 +02005085 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005086 "Nothing has changed."
Willy Tarreauba6be982013-04-19 12:16:55 +02005087 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005088 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
5089 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02005090 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005091 break;
5092 case STAT_STATUS_PART:
5093 chunk_appendf(&trash,
Simon Horman837bfa72015-04-23 14:51:27 +09005094 "<p><div class=active_going_down>"
Willy Tarreauba6be982013-04-19 12:16:55 +02005095 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005096 "Action partially processed.<br>"
5097 "Some server names are probably unknown or ambiguous (duplicated names in the backend)."
Willy Tarreauba6be982013-04-19 12:16:55 +02005098 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005099 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
5100 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02005101 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005102 break;
5103 case STAT_STATUS_ERRP:
5104 chunk_appendf(&trash,
Simon Horman837bfa72015-04-23 14:51:27 +09005105 "<p><div class=active_down>"
Willy Tarreauba6be982013-04-19 12:16:55 +02005106 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005107 "Action not processed because of invalid parameters."
5108 "<ul>"
5109 "<li>The action is maybe unknown.</li>"
5110 "<li>The backend name is probably unknown or ambiguous (duplicated names).</li>"
5111 "<li>Some server names are probably unknown or ambiguous (duplicated names in the backend).</li>"
5112 "</ul>"
Willy Tarreauba6be982013-04-19 12:16:55 +02005113 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005114 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
5115 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02005116 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005117 break;
5118 case STAT_STATUS_EXCD:
5119 chunk_appendf(&trash,
Simon Horman837bfa72015-04-23 14:51:27 +09005120 "<p><div class=active_down>"
Willy Tarreauba6be982013-04-19 12:16:55 +02005121 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005122 "<b>Action not processed : the buffer couldn't store all the data.<br>"
5123 "You should retry with less servers at a time.</b>"
Willy Tarreauba6be982013-04-19 12:16:55 +02005124 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005125 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
5126 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02005127 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005128 break;
5129 case STAT_STATUS_DENY:
5130 chunk_appendf(&trash,
Simon Horman837bfa72015-04-23 14:51:27 +09005131 "<p><div class=active_down>"
Willy Tarreauba6be982013-04-19 12:16:55 +02005132 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005133 "<b>Action denied.</b>"
Willy Tarreauba6be982013-04-19 12:16:55 +02005134 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005135 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
5136 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02005137 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005138 break;
5139 default:
5140 chunk_appendf(&trash,
Simon Horman837bfa72015-04-23 14:51:27 +09005141 "<p><div class=active_no_check>"
Willy Tarreauba6be982013-04-19 12:16:55 +02005142 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005143 "Unexpected result."
Willy Tarreauba6be982013-04-19 12:16:55 +02005144 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005145 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
5146 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02005147 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005148 }
5149 chunk_appendf(&trash, "<p>\n");
5150 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005151}
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01005152
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01005153/* Dumps the HTML stats trailer block to the trash. The caller is responsible
5154 * for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005155 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01005156static void stats_dump_html_end()
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005157{
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005158 chunk_appendf(&trash, "</body></html>\n");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005159}
Willy Tarreau7f062c42009-03-05 18:43:00 +01005160
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01005161/* This function dumps statistics onto the stream interface's read buffer in
5162 * either CSV or HTML format. <uri> contains some HTML-specific parameters that
Willy Tarreau306f8302013-07-08 15:53:06 +02005163 * are ignored for CSV format (hence <uri> may be NULL there). It returns 0 if
5164 * 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 +02005165 * and the stream must be closed, or -1 in case of any error. This function is
Willy Tarreau306f8302013-07-08 15:53:06 +02005166 * used by both the CLI and the HTTP handlers.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005167 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01005168static int stats_dump_stat_to_buffer(struct stream_interface *si, struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005169{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005170 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005171 struct channel *rep = si_ic(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005172 struct proxy *px;
Willy Tarreau7f062c42009-03-05 18:43:00 +01005173
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005174 chunk_reset(&trash);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02005175
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005176 switch (appctx->st2) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005177 case STAT_ST_INIT:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005178 appctx->st2 = STAT_ST_HEAD; /* let's start producing data */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005179 /* fall through */
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005180
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005181 case STAT_ST_HEAD:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005182 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01005183 stats_dump_html_head(uri);
Willy Tarreau1e62df92016-01-11 18:57:53 +01005184 else if (!(appctx->ctx.stats.flags & STAT_FMT_TYPED))
Willy Tarreau354898b2012-12-23 18:15:23 +01005185 stats_dump_csv_header();
Willy Tarreaud9b587f2010-02-26 10:05:55 +01005186
Willy Tarreaubc18da12015-03-13 14:00:47 +01005187 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02005188 si_applet_cant_put(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005189 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01005190 }
Willy Tarreauae526782010-03-04 20:34:23 +01005191
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005192 appctx->st2 = STAT_ST_INFO;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005193 /* fall through */
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01005194
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005195 case STAT_ST_INFO:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005196 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01005197 stats_dump_html_info(si, uri);
Willy Tarreaubc18da12015-03-13 14:00:47 +01005198 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02005199 si_applet_cant_put(si);
Willy Tarreau91861262007-10-17 17:06:05 +02005200 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01005201 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005202 }
Willy Tarreau91861262007-10-17 17:06:05 +02005203
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005204 appctx->ctx.stats.px = proxy;
5205 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
5206 appctx->st2 = STAT_ST_LIST;
Willy Tarreau91861262007-10-17 17:06:05 +02005207 /* fall through */
5208
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005209 case STAT_ST_LIST:
5210 /* dump proxies */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005211 while (appctx->ctx.stats.px) {
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01005212 if (buffer_almost_full(rep->buf)) {
Willy Tarreaufe127932015-04-21 19:23:39 +02005213 si_applet_cant_put(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005214 return 0;
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01005215 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005216
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005217 px = appctx->ctx.stats.px;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005218 /* skip the disabled proxies, global frontend and non-networked ones */
5219 if (px->state != PR_STSTOPPED && px->uuid > 0 && (px->cap & (PR_CAP_FE | PR_CAP_BE)))
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01005220 if (stats_dump_proxy_to_buffer(si, px, uri) == 0)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005221 return 0;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005222
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005223 appctx->ctx.stats.px = px->next;
5224 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005225 }
5226 /* here, we just have reached the last proxy */
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005227
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005228 appctx->st2 = STAT_ST_END;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005229 /* fall through */
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005230
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005231 case STAT_ST_END:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005232 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01005233 stats_dump_html_end();
Willy Tarreaubc18da12015-03-13 14:00:47 +01005234 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02005235 si_applet_cant_put(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005236 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01005237 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005238 }
Willy Tarreau55058a72012-11-21 08:27:21 +01005239
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005240 appctx->st2 = STAT_ST_FIN;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005241 /* fall through */
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02005242
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005243 case STAT_ST_FIN:
5244 return 1;
Willy Tarreau55058a72012-11-21 08:27:21 +01005245
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005246 default:
5247 /* unknown state ! */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005248 appctx->st2 = STAT_ST_FIN;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005249 return -1;
5250 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005251}
Willy Tarreauae526782010-03-04 20:34:23 +01005252
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005253/* We reached the stats page through a POST request. The appctx is
5254 * expected to have already been allocated by the caller.
Willy Tarreau347a35d2013-11-22 17:51:09 +01005255 * Parse the posted data and enable/disable servers if necessary.
5256 * Returns 1 if request was parsed or zero if it needs more data.
5257 */
5258static int stats_process_http_post(struct stream_interface *si)
5259{
Willy Tarreau87b09662015-04-03 00:22:06 +02005260 struct stream *s = si_strm(si);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005261 struct appctx *appctx = objt_appctx(si->end);
Willy Tarreau347a35d2013-11-22 17:51:09 +01005262
5263 struct proxy *px = NULL;
5264 struct server *sv = NULL;
5265
5266 char key[LINESIZE];
5267 int action = ST_ADM_ACTION_NONE;
5268 int reprocess = 0;
5269
5270 int total_servers = 0;
5271 int altered_servers = 0;
5272
5273 char *first_param, *cur_param, *next_param, *end_params;
5274 char *st_cur_param = NULL;
5275 char *st_next_param = NULL;
5276
5277 struct chunk *temp;
5278 int reql;
5279
5280 temp = get_trash_chunk();
Willy Tarreaueee5b512015-04-03 23:46:31 +02005281 if (temp->size < s->txn->req.body_len) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01005282 /* too large request */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005283 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005284 goto out;
5285 }
5286
Willy Tarreaueee5b512015-04-03 23:46:31 +02005287 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 +01005288 if (reql <= 0) {
5289 /* we need more data */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005290 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005291 return 0;
5292 }
5293
5294 first_param = temp->str;
5295 end_params = temp->str + reql;
5296 cur_param = next_param = end_params;
5297 *end_params = '\0';
5298
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005299 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005300
5301 /*
5302 * Parse the parameters in reverse order to only store the last value.
5303 * From the html form, the backend and the action are at the end.
5304 */
5305 while (cur_param > first_param) {
5306 char *value;
5307 int poffset, plen;
5308
5309 cur_param--;
5310
5311 if ((*cur_param == '&') || (cur_param == first_param)) {
5312 reprocess_servers:
5313 /* Parse the key */
5314 poffset = (cur_param != first_param ? 1 : 0);
5315 plen = next_param - cur_param + (cur_param == first_param ? 1 : 0);
5316 if ((plen > 0) && (plen <= sizeof(key))) {
5317 strncpy(key, cur_param + poffset, plen);
5318 key[plen - 1] = '\0';
5319 } else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005320 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005321 goto out;
5322 }
5323
5324 /* Parse the value */
5325 value = key;
5326 while (*value != '\0' && *value != '=') {
5327 value++;
5328 }
5329 if (*value == '=') {
5330 /* Ok, a value is found, we can mark the end of the key */
5331 *value++ = '\0';
5332 }
5333 if (url_decode(key) < 0 || url_decode(value) < 0)
5334 break;
5335
5336 /* Now we can check the key to see what to do */
5337 if (!px && (strcmp(key, "b") == 0)) {
Willy Tarreau9e0bb102015-05-26 11:24:42 +02005338 if ((px = proxy_be_by_name(value)) == NULL) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01005339 /* the backend name is unknown or ambiguous (duplicate names) */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005340 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005341 goto out;
5342 }
5343 }
5344 else if (!action && (strcmp(key, "action") == 0)) {
Willy Tarreaued7df902014-05-22 18:04:49 +02005345 if (strcmp(value, "ready") == 0) {
5346 action = ST_ADM_ACTION_READY;
5347 }
5348 else if (strcmp(value, "drain") == 0) {
5349 action = ST_ADM_ACTION_DRAIN;
5350 }
5351 else if (strcmp(value, "maint") == 0) {
5352 action = ST_ADM_ACTION_MAINT;
5353 }
5354 else if (strcmp(value, "shutdown") == 0) {
5355 action = ST_ADM_ACTION_SHUTDOWN;
5356 }
Willy Tarreau248a60e2014-05-23 14:59:48 +02005357 else if (strcmp(value, "dhlth") == 0) {
5358 action = ST_ADM_ACTION_DHLTH;
5359 }
5360 else if (strcmp(value, "ehlth") == 0) {
5361 action = ST_ADM_ACTION_EHLTH;
5362 }
5363 else if (strcmp(value, "hrunn") == 0) {
5364 action = ST_ADM_ACTION_HRUNN;
5365 }
5366 else if (strcmp(value, "hnolb") == 0) {
5367 action = ST_ADM_ACTION_HNOLB;
5368 }
5369 else if (strcmp(value, "hdown") == 0) {
5370 action = ST_ADM_ACTION_HDOWN;
5371 }
5372 else if (strcmp(value, "dagent") == 0) {
5373 action = ST_ADM_ACTION_DAGENT;
5374 }
5375 else if (strcmp(value, "eagent") == 0) {
5376 action = ST_ADM_ACTION_EAGENT;
5377 }
5378 else if (strcmp(value, "arunn") == 0) {
5379 action = ST_ADM_ACTION_ARUNN;
5380 }
5381 else if (strcmp(value, "adown") == 0) {
5382 action = ST_ADM_ACTION_ADOWN;
5383 }
Willy Tarreaued7df902014-05-22 18:04:49 +02005384 /* else these are the old supported methods */
5385 else if (strcmp(value, "disable") == 0) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01005386 action = ST_ADM_ACTION_DISABLE;
5387 }
5388 else if (strcmp(value, "enable") == 0) {
5389 action = ST_ADM_ACTION_ENABLE;
5390 }
5391 else if (strcmp(value, "stop") == 0) {
5392 action = ST_ADM_ACTION_STOP;
5393 }
5394 else if (strcmp(value, "start") == 0) {
5395 action = ST_ADM_ACTION_START;
5396 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01005397 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005398 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005399 goto out;
5400 }
5401 }
5402 else if (strcmp(key, "s") == 0) {
5403 if (!(px && action)) {
5404 /*
5405 * Indicates that we'll need to reprocess the parameters
5406 * as soon as backend and action are known
5407 */
5408 if (!reprocess) {
5409 st_cur_param = cur_param;
5410 st_next_param = next_param;
5411 }
5412 reprocess = 1;
5413 }
5414 else if ((sv = findserver(px, value)) != NULL) {
5415 switch (action) {
5416 case ST_ADM_ACTION_DISABLE:
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_set_admin_flag(sv, SRV_ADMF_FMAINT);
Willy Tarreau347a35d2013-11-22 17:51:09 +01005421 }
5422 break;
5423 case ST_ADM_ACTION_ENABLE:
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02005424 if (sv->admin & SRV_ADMF_FMAINT) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01005425 altered_servers++;
5426 total_servers++;
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02005427 srv_clr_admin_flag(sv, SRV_ADMF_FMAINT);
Willy Tarreau347a35d2013-11-22 17:51:09 +01005428 }
5429 break;
5430 case ST_ADM_ACTION_STOP:
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02005431 if (!(sv->admin & SRV_ADMF_FDRAIN)) {
5432 srv_set_admin_flag(sv, SRV_ADMF_FDRAIN);
5433 altered_servers++;
5434 total_servers++;
5435 }
5436 break;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005437 case ST_ADM_ACTION_START:
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02005438 if (sv->admin & SRV_ADMF_FDRAIN) {
5439 srv_clr_admin_flag(sv, SRV_ADMF_FDRAIN);
5440 altered_servers++;
5441 total_servers++;
5442 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01005443 break;
Willy Tarreau248a60e2014-05-23 14:59:48 +02005444 case ST_ADM_ACTION_DHLTH:
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_EHLTH:
5452 if (sv->check.state & CHK_ST_CONFIGURED) {
5453 sv->check.state |= CHK_ST_ENABLED;
5454 altered_servers++;
5455 total_servers++;
5456 }
5457 break;
5458 case ST_ADM_ACTION_HRUNN:
5459 if (!(sv->track)) {
5460 sv->check.health = sv->check.rise + sv->check.fall - 1;
5461 srv_set_running(sv, "changed from Web interface");
5462 altered_servers++;
5463 total_servers++;
5464 }
5465 break;
5466 case ST_ADM_ACTION_HNOLB:
5467 if (!(sv->track)) {
5468 sv->check.health = sv->check.rise + sv->check.fall - 1;
5469 srv_set_stopping(sv, "changed from Web interface");
5470 altered_servers++;
5471 total_servers++;
5472 }
5473 break;
5474 case ST_ADM_ACTION_HDOWN:
5475 if (!(sv->track)) {
5476 sv->check.health = 0;
5477 srv_set_stopped(sv, "changed from Web interface");
5478 altered_servers++;
5479 total_servers++;
5480 }
5481 break;
5482 case ST_ADM_ACTION_DAGENT:
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_EAGENT:
5490 if (sv->agent.state & CHK_ST_CONFIGURED) {
5491 sv->agent.state |= CHK_ST_ENABLED;
5492 altered_servers++;
5493 total_servers++;
5494 }
5495 break;
5496 case ST_ADM_ACTION_ARUNN:
5497 if (sv->agent.state & CHK_ST_ENABLED) {
5498 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
5499 srv_set_running(sv, "changed from Web interface");
5500 altered_servers++;
5501 total_servers++;
5502 }
5503 break;
5504 case ST_ADM_ACTION_ADOWN:
5505 if (sv->agent.state & CHK_ST_ENABLED) {
5506 sv->agent.health = 0;
5507 srv_set_stopped(sv, "changed from Web interface");
5508 altered_servers++;
5509 total_servers++;
5510 }
5511 break;
Willy Tarreaued7df902014-05-22 18:04:49 +02005512 case ST_ADM_ACTION_READY:
5513 srv_adm_set_ready(sv);
5514 altered_servers++;
5515 total_servers++;
5516 break;
5517 case ST_ADM_ACTION_DRAIN:
5518 srv_adm_set_drain(sv);
5519 altered_servers++;
5520 total_servers++;
5521 break;
5522 case ST_ADM_ACTION_MAINT:
5523 srv_adm_set_maint(sv);
5524 altered_servers++;
5525 total_servers++;
5526 break;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005527 case ST_ADM_ACTION_SHUTDOWN:
5528 if (px->state != PR_STSTOPPED) {
Willy Tarreau87b09662015-04-03 00:22:06 +02005529 struct stream *sess, *sess_bck;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005530
5531 list_for_each_entry_safe(sess, sess_bck, &sv->actconns, by_srv)
5532 if (sess->srv_conn == sv)
Willy Tarreaue7dff022015-04-03 01:14:29 +02005533 stream_shutdown(sess, SF_ERR_KILLED);
Willy Tarreau347a35d2013-11-22 17:51:09 +01005534
5535 altered_servers++;
5536 total_servers++;
5537 }
5538 break;
5539 }
5540 } else {
5541 /* the server name is unknown or ambiguous (duplicate names) */
5542 total_servers++;
5543 }
5544 }
5545 if (reprocess && px && action) {
5546 /* Now, we know the backend and the action chosen by the user.
5547 * We can safely restart from the first server parameter
5548 * to reprocess them
5549 */
5550 cur_param = st_cur_param;
5551 next_param = st_next_param;
5552 reprocess = 0;
5553 goto reprocess_servers;
5554 }
5555
5556 next_param = cur_param;
5557 }
5558 }
5559
5560 if (total_servers == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005561 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005562 }
5563 else if (altered_servers == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005564 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005565 }
5566 else if (altered_servers == total_servers) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005567 appctx->ctx.stats.st_code = STAT_STATUS_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005568 }
5569 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005570 appctx->ctx.stats.st_code = STAT_STATUS_PART;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005571 }
5572 out:
5573 return 1;
5574}
5575
5576
5577static int stats_send_http_headers(struct stream_interface *si)
5578{
Willy Tarreau87b09662015-04-03 00:22:06 +02005579 struct stream *s = si_strm(si);
Willy Tarreau347a35d2013-11-22 17:51:09 +01005580 struct uri_auth *uri = s->be->uri_auth;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005581 struct appctx *appctx = objt_appctx(si->end);
Willy Tarreau347a35d2013-11-22 17:51:09 +01005582
5583 chunk_printf(&trash,
Willy Tarreau8b8995f2014-04-24 22:51:54 +02005584 "HTTP/1.1 200 OK\r\n"
Willy Tarreau347a35d2013-11-22 17:51:09 +01005585 "Cache-Control: no-cache\r\n"
5586 "Connection: close\r\n"
5587 "Content-Type: %s\r\n",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005588 (appctx->ctx.stats.flags & STAT_FMT_HTML) ? "text/html" : "text/plain");
Willy Tarreau347a35d2013-11-22 17:51:09 +01005589
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005590 if (uri->refresh > 0 && !(appctx->ctx.stats.flags & STAT_NO_REFRESH))
Willy Tarreau347a35d2013-11-22 17:51:09 +01005591 chunk_appendf(&trash, "Refresh: %d\r\n",
5592 uri->refresh);
5593
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005594 /* we don't send the CRLF in chunked mode, it will be sent with the first chunk's size */
5595
5596 if (appctx->ctx.stats.flags & STAT_CHUNKED)
5597 chunk_appendf(&trash, "Transfer-Encoding: chunked\r\n");
5598 else
5599 chunk_appendf(&trash, "\r\n");
Willy Tarreau347a35d2013-11-22 17:51:09 +01005600
Willy Tarreaueee5b512015-04-03 23:46:31 +02005601 s->txn->status = 200;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005602 s->logs.tv_request = now;
5603
Willy Tarreaubc18da12015-03-13 14:00:47 +01005604 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02005605 si_applet_cant_put(si);
Willy Tarreau347a35d2013-11-22 17:51:09 +01005606 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01005607 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01005608
5609 return 1;
5610}
5611
5612static int stats_send_http_redirect(struct stream_interface *si)
5613{
5614 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
Willy Tarreau87b09662015-04-03 00:22:06 +02005615 struct stream *s = si_strm(si);
Willy Tarreau347a35d2013-11-22 17:51:09 +01005616 struct uri_auth *uri = s->be->uri_auth;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005617 struct appctx *appctx = objt_appctx(si->end);
Willy Tarreau347a35d2013-11-22 17:51:09 +01005618
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005619 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Willy Tarreau347a35d2013-11-22 17:51:09 +01005620 scope_txt[0] = 0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005621 if (appctx->ctx.stats.scope_len) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01005622 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau4e4292b2014-11-28 12:18:45 +01005623 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 +01005624 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005625 }
5626
5627 /* We don't want to land on the posted stats page because a refresh will
5628 * repost the data. We don't want this to happen on accident so we redirect
5629 * the browse to the stats page with a GET.
5630 */
5631 chunk_printf(&trash,
5632 "HTTP/1.1 303 See Other\r\n"
5633 "Cache-Control: no-cache\r\n"
5634 "Content-Type: text/plain\r\n"
5635 "Connection: close\r\n"
5636 "Location: %s;st=%s%s%s%s\r\n"
Willy Tarreaufdfcc9d2016-01-26 13:57:29 +01005637 "Content-length: 0\r\n"
Willy Tarreau347a35d2013-11-22 17:51:09 +01005638 "\r\n",
5639 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005640 ((appctx->ctx.stats.st_code > STAT_STATUS_INIT) &&
5641 (appctx->ctx.stats.st_code < STAT_STATUS_SIZE) &&
5642 stat_status_codes[appctx->ctx.stats.st_code]) ?
5643 stat_status_codes[appctx->ctx.stats.st_code] :
Willy Tarreau347a35d2013-11-22 17:51:09 +01005644 stat_status_codes[STAT_STATUS_UNKN],
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005645 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
5646 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreau347a35d2013-11-22 17:51:09 +01005647 scope_txt);
5648
Willy Tarreaueee5b512015-04-03 23:46:31 +02005649 s->txn->status = 303;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005650 s->logs.tv_request = now;
5651
Willy Tarreaubc18da12015-03-13 14:00:47 +01005652 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02005653 si_applet_cant_put(si);
Willy Tarreau347a35d2013-11-22 17:51:09 +01005654 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01005655 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01005656
5657 return 1;
5658}
5659
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005660/* This I/O handler runs as an applet embedded in a stream interface. It is
5661 * used to send HTTP stats over a TCP socket. The mechanism is very simple.
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005662 * appctx->st0 contains the operation in progress (dump, done). The handler
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005663 * automatically unregisters itself once transfer is complete.
5664 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02005665static void http_stats_io_handler(struct appctx *appctx)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005666{
Willy Tarreau00a37f02015-04-13 12:05:19 +02005667 struct stream_interface *si = appctx->owner;
Willy Tarreau87b09662015-04-03 00:22:06 +02005668 struct stream *s = si_strm(si);
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005669 struct channel *req = si_oc(si);
5670 struct channel *res = si_ic(si);
Willy Tarreau55058a72012-11-21 08:27:21 +01005671
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005672 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
5673 goto out;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01005674
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005675 /* check that the output is not closed */
5676 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005677 appctx->st0 = STAT_HTTP_DONE;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005678
Willy Tarreau347a35d2013-11-22 17:51:09 +01005679 /* all states are processed in sequence */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005680 if (appctx->st0 == STAT_HTTP_HEAD) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01005681 if (stats_send_http_headers(si)) {
Willy Tarreaueee5b512015-04-03 23:46:31 +02005682 if (s->txn->meth == HTTP_METH_HEAD)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005683 appctx->st0 = STAT_HTTP_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005684 else
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005685 appctx->st0 = STAT_HTTP_DUMP;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005686 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01005687 }
5688
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005689 if (appctx->st0 == STAT_HTTP_DUMP) {
Willy Tarreau4e4292b2014-11-28 12:18:45 +01005690 unsigned int prev_len = si_ib(si)->i;
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005691 unsigned int data_len;
5692 unsigned int last_len;
Willy Tarreaucce36482014-04-24 20:26:41 +02005693 unsigned int last_fwd = 0;
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005694
5695 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
5696 /* One difficulty we're facing is that we must prevent
5697 * the input data from being automatically forwarded to
5698 * the output area. For this, we temporarily disable
5699 * forwarding on the channel.
5700 */
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005701 last_fwd = si_ic(si)->to_forward;
5702 si_ic(si)->to_forward = 0;
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005703 chunk_printf(&trash, "\r\n000000\r\n");
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005704 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02005705 si_applet_cant_put(si);
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005706 si_ic(si)->to_forward = last_fwd;
Willy Tarreau828824a2015-04-19 17:20:03 +02005707 goto out;
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005708 }
5709 }
5710
Willy Tarreau4e4292b2014-11-28 12:18:45 +01005711 data_len = si_ib(si)->i;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005712 if (stats_dump_stat_to_buffer(si, s->be->uri_auth))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005713 appctx->st0 = STAT_HTTP_DONE;
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005714
Willy Tarreau4e4292b2014-11-28 12:18:45 +01005715 last_len = si_ib(si)->i;
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005716
5717 /* Now we must either adjust or remove the chunk size. This is
5718 * not easy because the chunk size might wrap at the end of the
5719 * buffer, so we pretend we have nothing in the buffer, we write
5720 * the size, then restore the buffer's contents. Note that we can
5721 * only do that because no forwarding is scheduled on the stats
5722 * applet.
5723 */
5724 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
Willy Tarreau4e4292b2014-11-28 12:18:45 +01005725 si_ic(si)->total -= (last_len - prev_len);
5726 si_ib(si)->i -= (last_len - prev_len);
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005727
5728 if (last_len != data_len) {
5729 chunk_printf(&trash, "\r\n%06x\r\n", (last_len - data_len));
Willy Tarreaubc18da12015-03-13 14:00:47 +01005730 if (bi_putchk(si_ic(si), &trash) == -1)
Willy Tarreaufe127932015-04-21 19:23:39 +02005731 si_applet_cant_put(si);
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005732
Willy Tarreau4e4292b2014-11-28 12:18:45 +01005733 si_ic(si)->total += (last_len - data_len);
5734 si_ib(si)->i += (last_len - data_len);
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005735 }
5736 /* now re-enable forwarding */
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005737 channel_forward(si_ic(si), last_fwd);
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005738 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005739 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02005740
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005741 if (appctx->st0 == STAT_HTTP_POST) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01005742 if (stats_process_http_post(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005743 appctx->st0 = STAT_HTTP_LAST;
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005744 else if (si_oc(si)->flags & CF_SHUTR)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005745 appctx->st0 = STAT_HTTP_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005746 }
5747
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005748 if (appctx->st0 == STAT_HTTP_LAST) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01005749 if (stats_send_http_redirect(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005750 appctx->st0 = STAT_HTTP_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005751 }
5752
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005753 if (appctx->st0 == STAT_HTTP_DONE) {
5754 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
5755 chunk_printf(&trash, "\r\n0\r\n\r\n");
Willy Tarreaubc18da12015-03-13 14:00:47 +01005756 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02005757 si_applet_cant_put(si);
Willy Tarreau828824a2015-04-19 17:20:03 +02005758 goto out;
Willy Tarreaubc18da12015-03-13 14:00:47 +01005759 }
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005760 }
5761 /* eat the whole request */
Willy Tarreau4e4292b2014-11-28 12:18:45 +01005762 bo_skip(si_oc(si), si_ob(si)->o);
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005763 res->flags |= CF_READ_NULL;
5764 si_shutr(si);
5765 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01005766
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005767 if ((res->flags & CF_SHUTR) && (si->state == SI_ST_EST))
5768 si_shutw(si);
Willy Tarreau91861262007-10-17 17:06:05 +02005769
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005770 if (appctx->st0 == STAT_HTTP_DONE) {
Willy Tarreau96d44912013-11-22 12:25:24 +01005771 if ((req->flags & CF_SHUTW) && (si->state == SI_ST_EST)) {
5772 si_shutr(si);
5773 res->flags |= CF_READ_NULL;
5774 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005775 }
Willy Tarreau828824a2015-04-19 17:20:03 +02005776 out:
Willy Tarreaud4da1962015-04-20 01:31:23 +02005777 /* just to make gcc happy */ ;
Willy Tarreau91861262007-10-17 17:06:05 +02005778}
5779
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005780
Willy Tarreau909d5172012-11-26 03:04:41 +01005781static inline const char *get_conn_ctrl_name(const struct connection *conn)
5782{
Willy Tarreau3c728722014-01-23 13:50:42 +01005783 if (!conn_ctrl_ready(conn))
Willy Tarreau909d5172012-11-26 03:04:41 +01005784 return "NONE";
5785 return conn->ctrl->name;
5786}
5787
5788static inline const char *get_conn_xprt_name(const struct connection *conn)
5789{
5790 static char ptr[17];
5791
Willy Tarreauaad69382014-01-23 14:21:42 +01005792 if (!conn_xprt_ready(conn))
Willy Tarreau909d5172012-11-26 03:04:41 +01005793 return "NONE";
5794
5795 if (conn->xprt == &raw_sock)
5796 return "RAW";
5797
5798#ifdef USE_OPENSSL
5799 if (conn->xprt == &ssl_sock)
5800 return "SSL";
5801#endif
5802 snprintf(ptr, sizeof(ptr), "%p", conn->xprt);
5803 return ptr;
5804}
5805
5806static inline const char *get_conn_data_name(const struct connection *conn)
5807{
5808 static char ptr[17];
5809
5810 if (!conn->data)
5811 return "NONE";
5812
5813 if (conn->data == &sess_conn_cb)
5814 return "SESS";
5815
5816 if (conn->data == &si_conn_cb)
5817 return "STRM";
5818
5819 if (conn->data == &check_conn_cb)
5820 return "CHCK";
5821
5822 snprintf(ptr, sizeof(ptr), "%p", conn->data);
5823 return ptr;
5824}
5825
Willy Tarreau87b09662015-04-03 00:22:06 +02005826/* This function dumps a complete stream state onto the stream interface's
5827 * read buffer. The stream has to be set in sess->target. It returns
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01005828 * 0 if the output buffer is full and it needs to be called again, otherwise
5829 * non-zero. It is designed to be called from stats_dump_sess_to_buffer() below.
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005830 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005831static int stats_dump_full_sess_to_buffer(struct stream_interface *si, struct stream *sess)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005832{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005833 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005834 struct tm tm;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005835 extern const char *monthname[12];
5836 char pn[INET6_ADDRSTRLEN];
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005837 struct connection *conn;
Willy Tarreau284ddbf2013-12-01 20:45:00 +01005838 struct appctx *tmpctx;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005839
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005840 chunk_reset(&trash);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005841
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005842 if (appctx->ctx.sess.section > 0 && appctx->ctx.sess.uid != sess->uniq_id) {
Willy Tarreau87b09662015-04-03 00:22:06 +02005843 /* stream changed, no need to go any further */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005844 chunk_appendf(&trash, " *** session terminated while we were watching it ***\n");
Willy Tarreaubc18da12015-03-13 14:00:47 +01005845 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02005846 si_applet_cant_put(si);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005847 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01005848 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005849 appctx->ctx.sess.uid = 0;
5850 appctx->ctx.sess.section = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005851 return 1;
5852 }
5853
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005854 switch (appctx->ctx.sess.section) {
Willy Tarreau87b09662015-04-03 00:22:06 +02005855 case 0: /* main status of the stream */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005856 appctx->ctx.sess.uid = sess->uniq_id;
5857 appctx->ctx.sess.section = 1;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005858 /* fall through */
5859
5860 case 1:
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005861 get_localtime(sess->logs.accept_date.tv_sec, &tm);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005862 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005863 "%p: [%02d/%s/%04d:%02d:%02d:%02d.%06d] id=%u proto=%s",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005864 sess,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005865 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
5866 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(sess->logs.accept_date.tv_usec),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005867 sess->uniq_id,
Willy Tarreau666f5042015-06-17 19:49:52 +02005868 strm_li(sess) ? strm_li(sess)->proto->name : "?");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005869
Willy Tarreaud0d8da92015-04-04 02:10:38 +02005870 conn = objt_conn(strm_orig(sess));
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005871 switch (conn ? addr_to_str(&conn->addr.from, pn, sizeof(pn)) : AF_UNSPEC) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005872 case AF_INET:
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005873 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005874 chunk_appendf(&trash, " source=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005875 pn, get_host_port(&conn->addr.from));
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005876 break;
5877 case AF_UNIX:
Willy Tarreaud0d8da92015-04-04 02:10:38 +02005878 chunk_appendf(&trash, " source=unix:%d\n", strm_li(sess)->luid);
Emeric Brun837ca522010-10-22 16:19:01 +02005879 break;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005880 default:
5881 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005882 chunk_appendf(&trash, "\n");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005883 break;
5884 }
5885
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005886 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005887 " flags=0x%x, conn_retries=%d, srv_conn=%p, pend_pos=%p\n",
Willy Tarreauee28de02010-06-01 09:51:00 +02005888 sess->flags, sess->si[1].conn_retries, sess->srv_conn, sess->pend_pos);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005889
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005890 chunk_appendf(&trash,
Daniel Schultze90690c72012-03-23 10:53:36 -07005891 " frontend=%s (id=%u mode=%s), listener=%s (id=%u)",
Willy Tarreaud0d8da92015-04-04 02:10:38 +02005892 strm_fe(sess)->id, strm_fe(sess)->uuid, strm_fe(sess)->mode ? "http" : "tcp",
5893 strm_li(sess) ? strm_li(sess)->name ? strm_li(sess)->name : "?" : "?",
5894 strm_li(sess) ? strm_li(sess)->luid : 0);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005895
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005896 if (conn)
5897 conn_get_to_addr(conn);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005898
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005899 switch (conn ? addr_to_str(&conn->addr.to, pn, sizeof(pn)) : AF_UNSPEC) {
Daniel Schultze90690c72012-03-23 10:53:36 -07005900 case AF_INET:
5901 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005902 chunk_appendf(&trash, " addr=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005903 pn, get_host_port(&conn->addr.to));
Daniel Schultze90690c72012-03-23 10:53:36 -07005904 break;
5905 case AF_UNIX:
Willy Tarreaud0d8da92015-04-04 02:10:38 +02005906 chunk_appendf(&trash, " addr=unix:%d\n", strm_li(sess)->luid);
Daniel Schultze90690c72012-03-23 10:53:36 -07005907 break;
5908 default:
5909 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005910 chunk_appendf(&trash, "\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07005911 break;
5912 }
5913
Willy Tarreau50943332011-09-02 17:33:05 +02005914 if (sess->be->cap & PR_CAP_BE)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005915 chunk_appendf(&trash,
Daniel Schultze90690c72012-03-23 10:53:36 -07005916 " backend=%s (id=%u mode=%s)",
Willy Tarreau50943332011-09-02 17:33:05 +02005917 sess->be->id,
Daniel Schultze90690c72012-03-23 10:53:36 -07005918 sess->be->uuid, sess->be->mode ? "http" : "tcp");
5919 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005920 chunk_appendf(&trash, " backend=<NONE> (id=-1 mode=-)");
Daniel Schultze90690c72012-03-23 10:53:36 -07005921
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005922 conn = objt_conn(sess->si[1].end);
5923 if (conn)
5924 conn_get_from_addr(conn);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005925
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005926 switch (conn ? addr_to_str(&conn->addr.from, pn, sizeof(pn)) : AF_UNSPEC) {
Daniel Schultze90690c72012-03-23 10:53:36 -07005927 case AF_INET:
5928 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005929 chunk_appendf(&trash, " addr=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005930 pn, get_host_port(&conn->addr.from));
Daniel Schultze90690c72012-03-23 10:53:36 -07005931 break;
5932 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005933 chunk_appendf(&trash, " addr=unix\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07005934 break;
5935 default:
5936 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005937 chunk_appendf(&trash, "\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07005938 break;
5939 }
5940
5941 if (sess->be->cap & PR_CAP_BE)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005942 chunk_appendf(&trash,
Daniel Schultze90690c72012-03-23 10:53:36 -07005943 " server=%s (id=%u)",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005944 objt_server(sess->target) ? objt_server(sess->target)->id : "<none>",
5945 objt_server(sess->target) ? objt_server(sess->target)->puid : 0);
Willy Tarreau50943332011-09-02 17:33:05 +02005946 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005947 chunk_appendf(&trash, " server=<NONE> (id=-1)");
Daniel Schultze90690c72012-03-23 10:53:36 -07005948
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005949 if (conn)
5950 conn_get_to_addr(conn);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005951
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005952 switch (conn ? addr_to_str(&conn->addr.to, pn, sizeof(pn)) : AF_UNSPEC) {
Daniel Schultze90690c72012-03-23 10:53:36 -07005953 case AF_INET:
5954 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005955 chunk_appendf(&trash, " addr=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005956 pn, get_host_port(&conn->addr.to));
Daniel Schultze90690c72012-03-23 10:53:36 -07005957 break;
5958 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005959 chunk_appendf(&trash, " addr=unix\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07005960 break;
5961 default:
5962 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005963 chunk_appendf(&trash, "\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07005964 break;
5965 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005966
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005967 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005968 " task=%p (state=0x%02x nice=%d calls=%d exp=%s%s",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005969 sess->task,
5970 sess->task->state,
5971 sess->task->nice, sess->task->calls,
5972 sess->task->expire ?
5973 tick_is_expired(sess->task->expire, now_ms) ? "<PAST>" :
5974 human_time(TICKS_TO_MS(sess->task->expire - now_ms),
5975 TICKS_TO_MS(1000)) : "<NEVER>",
5976 task_in_rq(sess->task) ? ", running" : "");
5977
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005978 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005979 " age=%s)\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005980 human_time(now.tv_sec - sess->logs.accept_date.tv_sec, 1));
5981
Willy Tarreaueee5b512015-04-03 23:46:31 +02005982 if (sess->txn)
5983 chunk_appendf(&trash,
Willy Tarreau98410192014-11-26 18:05:38 +01005984 " 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 +02005985 sess->txn, sess->txn->flags, sess->txn->meth, sess->txn->status,
5986 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 +01005987
5988 chunk_appendf(&trash,
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02005989 " si[0]=%p (state=%s flags=0x%02x endp0=%s:%p exp=%s, et=0x%03x)\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005990 &sess->si[0],
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005991 si_state_str(sess->si[0].state),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005992 sess->si[0].flags,
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005993 obj_type_name(sess->si[0].end),
5994 obj_base_ptr(sess->si[0].end),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005995 sess->si[0].exp ?
5996 tick_is_expired(sess->si[0].exp, now_ms) ? "<PAST>" :
5997 human_time(TICKS_TO_MS(sess->si[0].exp - now_ms),
5998 TICKS_TO_MS(1000)) : "<NEVER>",
5999 sess->si[0].err_type);
6000
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006001 chunk_appendf(&trash,
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02006002 " si[1]=%p (state=%s flags=0x%02x endp1=%s:%p exp=%s, et=0x%03x)\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006003 &sess->si[1],
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01006004 si_state_str(sess->si[1].state),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006005 sess->si[1].flags,
Willy Tarreaucf644ed2013-09-29 17:19:56 +02006006 obj_type_name(sess->si[1].end),
6007 obj_base_ptr(sess->si[1].end),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006008 sess->si[1].exp ?
6009 tick_is_expired(sess->si[1].exp, now_ms) ? "<PAST>" :
6010 human_time(TICKS_TO_MS(sess->si[1].exp - now_ms),
6011 TICKS_TO_MS(1000)) : "<NEVER>",
6012 sess->si[1].err_type);
6013
Willy Tarreau284ddbf2013-12-01 20:45:00 +01006014 if ((conn = objt_conn(sess->si[0].end)) != NULL) {
Willy Tarreaucf644ed2013-09-29 17:19:56 +02006015 chunk_appendf(&trash,
6016 " co0=%p ctrl=%s xprt=%s data=%s target=%s:%p\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006017 conn,
6018 get_conn_ctrl_name(conn),
6019 get_conn_xprt_name(conn),
6020 get_conn_data_name(conn),
6021 obj_type_name(conn->target),
6022 obj_base_ptr(conn->target));
Willy Tarreau909d5172012-11-26 03:04:41 +01006023
Willy Tarreaucf644ed2013-09-29 17:19:56 +02006024 chunk_appendf(&trash,
Willy Tarreau16f649c2014-01-25 19:10:48 +01006025 " flags=0x%08x fd=%d fd.state=%02x fd.cache=%d updt=%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006026 conn->flags,
6027 conn->t.sock.fd,
Willy Tarreau69a41fa2014-01-20 11:02:59 +01006028 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].state : 0,
Willy Tarreau15a4dec2014-01-20 11:09:39 +01006029 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].cache : 0,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006030 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].updated : 0);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02006031 }
Willy Tarreau284ddbf2013-12-01 20:45:00 +01006032 else if ((tmpctx = objt_appctx(sess->si[0].end)) != NULL) {
6033 chunk_appendf(&trash,
6034 " app0=%p st0=%d st1=%d st2=%d applet=%s\n",
6035 tmpctx,
6036 tmpctx->st0,
6037 tmpctx->st1,
6038 tmpctx->st2,
6039 tmpctx->applet->name);
6040 }
Willy Tarreaubc174aa2012-11-19 16:10:32 +01006041
Willy Tarreau284ddbf2013-12-01 20:45:00 +01006042 if ((conn = objt_conn(sess->si[1].end)) != NULL) {
Willy Tarreaucf644ed2013-09-29 17:19:56 +02006043 chunk_appendf(&trash,
6044 " co1=%p ctrl=%s xprt=%s data=%s target=%s:%p\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006045 conn,
6046 get_conn_ctrl_name(conn),
6047 get_conn_xprt_name(conn),
6048 get_conn_data_name(conn),
6049 obj_type_name(conn->target),
6050 obj_base_ptr(conn->target));
Willy Tarreau909d5172012-11-26 03:04:41 +01006051
Willy Tarreaucf644ed2013-09-29 17:19:56 +02006052 chunk_appendf(&trash,
Willy Tarreauceeafb52016-01-25 15:27:17 +01006053 " flags=0x%08x fd=%d fd.state=%02x fd.cache=%d updt=%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006054 conn->flags,
6055 conn->t.sock.fd,
Willy Tarreau69a41fa2014-01-20 11:02:59 +01006056 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].state : 0,
Willy Tarreau15a4dec2014-01-20 11:09:39 +01006057 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].cache : 0,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006058 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].updated : 0);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02006059 }
Willy Tarreau284ddbf2013-12-01 20:45:00 +01006060 else if ((tmpctx = objt_appctx(sess->si[1].end)) != NULL) {
6061 chunk_appendf(&trash,
6062 " app1=%p st0=%d st1=%d st2=%d applet=%s\n",
6063 tmpctx,
6064 tmpctx->st0,
6065 tmpctx->st1,
6066 tmpctx->st2,
6067 tmpctx->applet->name);
6068 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006069
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006070 chunk_appendf(&trash,
Willy Tarreau909d5172012-11-26 03:04:41 +01006071 " req=%p (f=0x%06x an=0x%x pipe=%d tofwd=%d total=%lld)\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006072 " an_exp=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006073 &sess->req,
6074 sess->req.flags, sess->req.analysers,
6075 sess->req.pipe ? sess->req.pipe->data : 0,
6076 sess->req.to_forward, sess->req.total,
6077 sess->req.analyse_exp ?
6078 human_time(TICKS_TO_MS(sess->req.analyse_exp - now_ms),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006079 TICKS_TO_MS(1000)) : "<NEVER>");
6080
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006081 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006082 " rex=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006083 sess->req.rex ?
6084 human_time(TICKS_TO_MS(sess->req.rex - now_ms),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006085 TICKS_TO_MS(1000)) : "<NEVER>");
6086
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006087 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006088 " wex=%s\n"
Willy Tarreau909d5172012-11-26 03:04:41 +01006089 " buf=%p data=%p o=%d p=%d req.next=%d i=%d size=%d\n",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006090 sess->req.wex ?
6091 human_time(TICKS_TO_MS(sess->req.wex - now_ms),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006092 TICKS_TO_MS(1000)) : "<NEVER>",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006093 sess->req.buf,
6094 sess->req.buf->data, sess->req.buf->o,
6095 (int)(sess->req.buf->p - sess->req.buf->data),
Willy Tarreaueee5b512015-04-03 23:46:31 +02006096 sess->txn ? sess->txn->req.next : 0, sess->req.buf->i,
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006097 sess->req.buf->size);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006098
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006099 chunk_appendf(&trash,
Willy Tarreau909d5172012-11-26 03:04:41 +01006100 " res=%p (f=0x%06x an=0x%x pipe=%d tofwd=%d total=%lld)\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006101 " an_exp=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006102 &sess->res,
6103 sess->res.flags, sess->res.analysers,
6104 sess->res.pipe ? sess->res.pipe->data : 0,
6105 sess->res.to_forward, sess->res.total,
6106 sess->res.analyse_exp ?
6107 human_time(TICKS_TO_MS(sess->res.analyse_exp - now_ms),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006108 TICKS_TO_MS(1000)) : "<NEVER>");
6109
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006110 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006111 " rex=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006112 sess->res.rex ?
6113 human_time(TICKS_TO_MS(sess->res.rex - now_ms),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006114 TICKS_TO_MS(1000)) : "<NEVER>");
6115
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006116 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006117 " wex=%s\n"
Willy Tarreau909d5172012-11-26 03:04:41 +01006118 " buf=%p data=%p o=%d p=%d rsp.next=%d i=%d size=%d\n",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006119 sess->res.wex ?
6120 human_time(TICKS_TO_MS(sess->res.wex - now_ms),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006121 TICKS_TO_MS(1000)) : "<NEVER>",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006122 sess->res.buf,
6123 sess->res.buf->data, sess->res.buf->o,
6124 (int)(sess->res.buf->p - sess->res.buf->data),
Willy Tarreaueee5b512015-04-03 23:46:31 +02006125 sess->txn ? sess->txn->rsp.next : 0, sess->res.buf->i,
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006126 sess->res.buf->size);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006127
Willy Tarreaubc18da12015-03-13 14:00:47 +01006128 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02006129 si_applet_cant_put(si);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006130 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01006131 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006132
6133 /* use other states to dump the contents */
6134 }
6135 /* end of dump */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006136 appctx->ctx.sess.uid = 0;
6137 appctx->ctx.sess.section = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006138 return 1;
6139}
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006140
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02006141#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
6142static int stats_tlskeys_list(struct stream_interface *si) {
6143 struct appctx *appctx = __objt_appctx(si->end);
6144
6145 switch (appctx->st2) {
6146 case STAT_ST_INIT:
6147 /* Display the column headers. If the message cannot be sent,
6148 * quit the fucntion with returning 0. The function is called
6149 * later and restart at the state "STAT_ST_INIT".
6150 */
6151 chunk_reset(&trash);
William Lallemand1d0b36a2016-05-20 17:40:26 +02006152
6153 if (appctx->st0 == STAT_CLI_O_TLSK_ENT)
6154 chunk_appendf(&trash, "# id secret\n");
6155 else
6156 chunk_appendf(&trash, "# id (file)\n");
6157
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02006158 if (bi_putchk(si_ic(si), &trash) == -1) {
6159 si_applet_cant_put(si);
6160 return 0;
6161 }
6162
William Lallemand72a8a182016-06-14 18:58:55 +02006163 appctx->ctx.tlskeys.dump_keys_index = 0;
William Lallemand1d0b36a2016-05-20 17:40:26 +02006164
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02006165 /* Now, we start the browsing of the references lists.
6166 * Note that the following call to LIST_ELEM return bad pointer. The only
Cyril Bontédb98eb32016-05-06 12:18:50 +02006167 * available field of this pointer is <list>. It is used with the function
6168 * tlskeys_list_get_next() for retruning the first available entry
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02006169 */
William Lallemandcf9e7882016-06-14 17:45:18 +02006170 if (appctx->ctx.tlskeys.ref == NULL) {
6171 appctx->ctx.tlskeys.ref = LIST_ELEM(&tlskeys_reference, struct tls_keys_ref *, list);
6172 appctx->ctx.tlskeys.ref = tlskeys_list_get_next(appctx->ctx.tlskeys.ref, &tlskeys_reference);
William Lallemand1d0b36a2016-05-20 17:40:26 +02006173 }
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02006174
6175 appctx->st2 = STAT_ST_LIST;
6176 /* fall through */
6177
6178 case STAT_ST_LIST:
William Lallemandcf9e7882016-06-14 17:45:18 +02006179 while (appctx->ctx.tlskeys.ref) {
William Lallemandcf9e7882016-06-14 17:45:18 +02006180 int head = appctx->ctx.tlskeys.ref->tls_ticket_enc_index;
William Lallemand1d0b36a2016-05-20 17:40:26 +02006181
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02006182 chunk_reset(&trash);
William Lallemand72a8a182016-06-14 18:58:55 +02006183 if (appctx->st0 == STAT_CLI_O_TLSK_ENT && appctx->ctx.tlskeys.dump_keys_index == 0)
William Lallemand1d0b36a2016-05-20 17:40:26 +02006184 chunk_appendf(&trash, "# ");
William Lallemand72a8a182016-06-14 18:58:55 +02006185 if (appctx->ctx.tlskeys.dump_keys_index == 0)
6186 chunk_appendf(&trash, "%d (%s)\n", appctx->ctx.tlskeys.ref->unique_id,
6187 appctx->ctx.tlskeys.ref->filename);
William Lallemand1d0b36a2016-05-20 17:40:26 +02006188 if (appctx->st0 == STAT_CLI_O_TLSK_ENT) {
William Lallemand72a8a182016-06-14 18:58:55 +02006189 while (appctx->ctx.tlskeys.dump_keys_index < TLS_TICKETS_NO) {
William Lallemand1d0b36a2016-05-20 17:40:26 +02006190 struct chunk *t2 = get_trash_chunk();
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02006191
William Lallemand1d0b36a2016-05-20 17:40:26 +02006192 chunk_reset(t2);
William Lallemand72a8a182016-06-14 18:58:55 +02006193 /* should never fail here because we dump only a key in the t2 buffer */
6194 t2->len = a2base64((char *)(appctx->ctx.tlskeys.ref->tlskeys + (head + 2 + appctx->ctx.tlskeys.dump_keys_index) % TLS_TICKETS_NO),
William Lallemand1d0b36a2016-05-20 17:40:26 +02006195 sizeof(struct tls_sess_key), t2->str, t2->size);
William Lallemand72a8a182016-06-14 18:58:55 +02006196 chunk_appendf(&trash, "%d.%d %s\n", appctx->ctx.tlskeys.ref->unique_id, appctx->ctx.tlskeys.dump_keys_index, t2->str);
6197
6198 if (bi_putchk(si_ic(si), &trash) == -1) {
6199 /* let's try again later from this stream. We add ourselves into
6200 * this stream's users so that it can remove us upon termination.
6201 */
6202 si_applet_cant_put(si);
William Lallemand1d0b36a2016-05-20 17:40:26 +02006203 return 0;
William Lallemand72a8a182016-06-14 18:58:55 +02006204 }
6205 appctx->ctx.tlskeys.dump_keys_index++;
William Lallemand1d0b36a2016-05-20 17:40:26 +02006206 }
William Lallemand72a8a182016-06-14 18:58:55 +02006207 appctx->ctx.tlskeys.dump_keys_index = 0;
William Lallemand1d0b36a2016-05-20 17:40:26 +02006208 }
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02006209 if (bi_putchk(si_ic(si), &trash) == -1) {
6210 /* let's try again later from this stream. We add ourselves into
6211 * this stream's users so that it can remove us upon termination.
6212 */
6213 si_applet_cant_put(si);
6214 return 0;
6215 }
6216
William Lallemandcf9e7882016-06-14 17:45:18 +02006217 if (appctx->ctx.tlskeys.dump_all == 0) /* don't display everything if not necessary */
William Lallemand1d0b36a2016-05-20 17:40:26 +02006218 break;
6219
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02006220 /* get next list entry and check the end of the list */
William Lallemandcf9e7882016-06-14 17:45:18 +02006221 appctx->ctx.tlskeys.ref = tlskeys_list_get_next(appctx->ctx.tlskeys.ref, &tlskeys_reference);
William Lallemand1d0b36a2016-05-20 17:40:26 +02006222
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02006223 }
6224
6225 appctx->st2 = STAT_ST_FIN;
6226 /* fall through */
6227
6228 default:
6229 appctx->st2 = STAT_ST_FIN;
6230 return 1;
6231 }
6232 return 0;
6233}
6234#endif
6235
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006236static int stats_pats_list(struct stream_interface *si)
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006237{
6238 struct appctx *appctx = __objt_appctx(si->end);
6239
6240 switch (appctx->st2) {
6241 case STAT_ST_INIT:
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01006242 /* Display the column headers. If the message cannot be sent,
6243 * quit the fucntion with returning 0. The function is called
6244 * later and restart at the state "STAT_ST_INIT".
6245 */
6246 chunk_reset(&trash);
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01006247 chunk_appendf(&trash, "# id (file) description\n");
Willy Tarreaubc18da12015-03-13 14:00:47 +01006248 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02006249 si_applet_cant_put(si);
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01006250 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01006251 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006252
6253 /* Now, we start the browsing of the references lists.
6254 * Note that the following call to LIST_ELEM return bad pointer. The only
Cyril Bontédb98eb32016-05-06 12:18:50 +02006255 * available field of this pointer is <list>. It is used with the function
6256 * pat_list_get_next() for retruning the first available entry
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006257 */
6258 appctx->ctx.map.ref = LIST_ELEM(&pattern_reference, struct pat_ref *, list);
6259 appctx->ctx.map.ref = pat_list_get_next(appctx->ctx.map.ref, &pattern_reference,
6260 appctx->ctx.map.display_flags);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006261 appctx->st2 = STAT_ST_LIST;
6262 /* fall through */
6263
6264 case STAT_ST_LIST:
6265 while (appctx->ctx.map.ref) {
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006266 chunk_reset(&trash);
6267
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006268 /* Build messages. If the reference is used by another category than
6269 * the listed categorie, display the information in the massage.
6270 */
Thierry FOURNIERf7e04e92014-03-20 11:45:47 +01006271 chunk_appendf(&trash, "%d (%s) %s\n", appctx->ctx.map.ref->unique_id,
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01006272 appctx->ctx.map.ref->reference ? appctx->ctx.map.ref->reference : "",
6273 appctx->ctx.map.ref->display);
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01006274
Willy Tarreau2bb4a962014-11-28 11:11:05 +01006275 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau87b09662015-04-03 00:22:06 +02006276 /* let's try again later from this stream. We add ourselves into
6277 * this stream's users so that it can remove us upon termination.
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006278 */
Willy Tarreaufe127932015-04-21 19:23:39 +02006279 si_applet_cant_put(si);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006280 return 0;
6281 }
6282
6283 /* get next list entry and check the end of the list */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006284 appctx->ctx.map.ref = pat_list_get_next(appctx->ctx.map.ref, &pattern_reference,
6285 appctx->ctx.map.display_flags);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006286 }
6287
6288 appctx->st2 = STAT_ST_FIN;
6289 /* fall through */
6290
6291 default:
6292 appctx->st2 = STAT_ST_FIN;
6293 return 1;
6294 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006295 return 0;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006296}
6297
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006298static int stats_map_lookup(struct stream_interface *si)
6299{
6300 struct appctx *appctx = __objt_appctx(si->end);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006301 struct sample sample;
6302 struct pattern *pat;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006303 int match_method;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006304
6305 switch (appctx->st2) {
6306 case STAT_ST_INIT:
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006307 /* Init to the first entry. The list cannot be change */
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01006308 appctx->ctx.map.expr = LIST_ELEM(&appctx->ctx.map.ref->pat, struct pattern_expr *, list);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006309 appctx->ctx.map.expr = pat_expr_get_next(appctx->ctx.map.expr, &appctx->ctx.map.ref->pat);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006310 appctx->st2 = STAT_ST_LIST;
6311 /* fall through */
6312
6313 case STAT_ST_LIST:
6314 /* for each lookup type */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006315 while (appctx->ctx.map.expr) {
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006316 /* initialise chunk to build new message */
6317 chunk_reset(&trash);
6318
6319 /* execute pattern matching */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02006320 sample.data.type = SMP_T_STR;
Andreas Seltenreich9727cf42016-03-03 19:32:25 +01006321 sample.flags = SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02006322 sample.data.u.str.len = appctx->ctx.map.chunk.len;
6323 sample.data.u.str.str = appctx->ctx.map.chunk.str;
Thierry FOURNIER5d344082014-01-27 14:19:53 +01006324 if (appctx->ctx.map.expr->pat_head->match &&
6325 sample_convert(&sample, appctx->ctx.map.expr->pat_head->expect_type))
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006326 pat = appctx->ctx.map.expr->pat_head->match(&sample, appctx->ctx.map.expr, 1);
6327 else
6328 pat = NULL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006329
6330 /* build return message: set type of match */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006331 for (match_method=0; match_method<PAT_MATCH_NUM; match_method++)
6332 if (appctx->ctx.map.expr->pat_head->match == pat_match_fcts[match_method])
6333 break;
6334 if (match_method >= PAT_MATCH_NUM)
6335 chunk_appendf(&trash, "type=unknown(%p)", appctx->ctx.map.expr->pat_head->match);
6336 else
6337 chunk_appendf(&trash, "type=%s", pat_match_names[match_method]);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006338
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02006339 /* case sensitive */
6340 if (appctx->ctx.map.expr->mflags & PAT_MF_IGNORE_CASE)
6341 chunk_appendf(&trash, ", case=insensitive");
6342 else
6343 chunk_appendf(&trash, ", case=sensitive");
6344
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006345 /* Display no match, and set default value */
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01006346 if (!pat) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006347 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
6348 chunk_appendf(&trash, ", found=no");
6349 else
6350 chunk_appendf(&trash, ", match=no");
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006351 }
6352
6353 /* Display match and match info */
6354 else {
6355 /* display match */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006356 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
6357 chunk_appendf(&trash, ", found=yes");
6358 else
6359 chunk_appendf(&trash, ", match=yes");
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006360
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01006361 /* display index mode */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02006362 if (pat->sflags & PAT_SF_TREE)
Thierry FOURNIERb9903842014-03-11 18:48:17 +01006363 chunk_appendf(&trash, ", idx=tree");
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006364 else
Thierry FOURNIERb9903842014-03-11 18:48:17 +01006365 chunk_appendf(&trash, ", idx=list");
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006366
Thierry FOURNIER86db66a2014-03-11 18:37:58 +01006367 /* display pattern */
6368 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
6369 if (pat->ref && pat->ref->pattern)
6370 chunk_appendf(&trash, ", key=\"%s\"", pat->ref->pattern);
6371 else
6372 chunk_appendf(&trash, ", key=unknown");
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006373 }
6374 else {
Thierry FOURNIER86db66a2014-03-11 18:37:58 +01006375 if (pat->ref && pat->ref->pattern)
6376 chunk_appendf(&trash, ", pattern=\"%s\"", pat->ref->pattern);
6377 else
6378 chunk_appendf(&trash, ", pattern=unknown");
6379 }
6380
6381 /* display return value */
6382 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
Thierry FOURNIER503bb092015-08-19 08:35:43 +02006383 if (pat->data && pat->ref && pat->ref->sample)
Thierry FOURNIER03d0e452015-07-23 18:33:41 +02006384 chunk_appendf(&trash, ", value=\"%s\", type=\"%s\"", pat->ref->sample,
Thierry FOURNIER7d4335c2015-08-19 09:05:25 +02006385 smp_to_type[pat->data->type]);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006386 else
Thierry FOURNIER86db66a2014-03-11 18:37:58 +01006387 chunk_appendf(&trash, ", value=none");
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006388 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006389 }
6390
Thierry FOURNIERb9903842014-03-11 18:48:17 +01006391 chunk_appendf(&trash, "\n");
6392
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006393 /* display response */
Willy Tarreau2bb4a962014-11-28 11:11:05 +01006394 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau87b09662015-04-03 00:22:06 +02006395 /* let's try again later from this stream. We add ourselves into
6396 * this stream's users so that it can remove us upon termination.
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006397 */
Willy Tarreaufe127932015-04-21 19:23:39 +02006398 si_applet_cant_put(si);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006399 return 0;
6400 }
6401
6402 /* get next entry */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006403 appctx->ctx.map.expr = pat_expr_get_next(appctx->ctx.map.expr,
6404 &appctx->ctx.map.ref->pat);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006405 }
6406
6407 appctx->st2 = STAT_ST_FIN;
6408 /* fall through */
6409
6410 default:
6411 appctx->st2 = STAT_ST_FIN;
6412 free(appctx->ctx.map.chunk.str);
6413 return 1;
6414 }
6415}
6416
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006417static int stats_pat_list(struct stream_interface *si)
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006418{
6419 struct appctx *appctx = __objt_appctx(si->end);
6420
6421 switch (appctx->st2) {
6422
6423 case STAT_ST_INIT:
6424 /* Init to the first entry. The list cannot be change */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006425 appctx->ctx.map.elt = LIST_NEXT(&appctx->ctx.map.ref->head,
6426 struct pat_ref_elt *, list);
6427 if (&appctx->ctx.map.elt->list == &appctx->ctx.map.ref->head)
6428 appctx->ctx.map.elt = NULL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006429 appctx->st2 = STAT_ST_LIST;
6430 /* fall through */
6431
6432 case STAT_ST_LIST:
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006433 while (appctx->ctx.map.elt) {
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006434 chunk_reset(&trash);
6435
6436 /* build messages */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006437 if (appctx->ctx.map.elt->sample)
Thierry FOURNIER9356c682014-01-28 15:55:37 +01006438 chunk_appendf(&trash, "%p %s %s\n",
6439 appctx->ctx.map.elt, appctx->ctx.map.elt->pattern,
6440 appctx->ctx.map.elt->sample);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006441 else
Thierry FOURNIER9356c682014-01-28 15:55:37 +01006442 chunk_appendf(&trash, "%p %s\n",
6443 appctx->ctx.map.elt, appctx->ctx.map.elt->pattern);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006444
Willy Tarreau2bb4a962014-11-28 11:11:05 +01006445 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau87b09662015-04-03 00:22:06 +02006446 /* let's try again later from this stream. We add ourselves into
6447 * this stream's users so that it can remove us upon termination.
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006448 */
Willy Tarreaufe127932015-04-21 19:23:39 +02006449 si_applet_cant_put(si);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006450 return 0;
6451 }
6452
6453 /* get next list entry and check the end of the list */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006454 appctx->ctx.map.elt = LIST_NEXT(&appctx->ctx.map.elt->list,
6455 struct pat_ref_elt *, list);
6456 if (&appctx->ctx.map.elt->list == &appctx->ctx.map.ref->head)
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006457 break;
6458 }
6459
6460 appctx->st2 = STAT_ST_FIN;
6461 /* fall through */
6462
6463 default:
6464 appctx->st2 = STAT_ST_FIN;
6465 return 1;
6466 }
6467}
6468
Willy Tarreau87b09662015-04-03 00:22:06 +02006469/* This function dumps all streams' states onto the stream interface's
Willy Tarreau306f8302013-07-08 15:53:06 +02006470 * read buffer. It returns 0 if the output buffer is full and it needs
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01006471 * to be called again, otherwise non-zero. It is designed to be called
6472 * from stats_dump_sess_to_buffer() below.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006473 */
Simon Horman9bd2c732011-06-15 15:18:44 +09006474static int stats_dump_sess_to_buffer(struct stream_interface *si)
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006475{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006476 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006477 struct connection *conn;
6478
Willy Tarreau2bb4a962014-11-28 11:11:05 +01006479 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW))) {
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006480 /* If we're forced to shut down, we might have to remove our
Willy Tarreau87b09662015-04-03 00:22:06 +02006481 * reference to the last stream being dumped.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006482 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006483 if (appctx->st2 == STAT_ST_LIST) {
6484 if (!LIST_ISEMPTY(&appctx->ctx.sess.bref.users)) {
6485 LIST_DEL(&appctx->ctx.sess.bref.users);
6486 LIST_INIT(&appctx->ctx.sess.bref.users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +01006487 }
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006488 }
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02006489 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006490 }
6491
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006492 chunk_reset(&trash);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006493
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006494 switch (appctx->st2) {
Willy Tarreau295a8372011-03-10 11:25:07 +01006495 case STAT_ST_INIT:
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006496 /* the function had not been called yet, let's prepare the
Willy Tarreau87b09662015-04-03 00:22:06 +02006497 * buffer for a response. We initialize the current stream
Willy Tarreaufd3828e2009-02-22 15:17:24 +01006498 * pointer to the first in the global list. When a target
Willy Tarreau87b09662015-04-03 00:22:06 +02006499 * stream is being destroyed, it is responsible for updating
Willy Tarreaufd3828e2009-02-22 15:17:24 +01006500 * this pointer. We know we have reached the end when this
Willy Tarreau87b09662015-04-03 00:22:06 +02006501 * pointer points back to the head of the streams list.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006502 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006503 LIST_INIT(&appctx->ctx.sess.bref.users);
Willy Tarreau87b09662015-04-03 00:22:06 +02006504 appctx->ctx.sess.bref.ref = streams.n;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006505 appctx->st2 = STAT_ST_LIST;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006506 /* fall through */
6507
Willy Tarreau295a8372011-03-10 11:25:07 +01006508 case STAT_ST_LIST:
Willy Tarreau87b09662015-04-03 00:22:06 +02006509 /* first, let's detach the back-ref from a possible previous stream */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006510 if (!LIST_ISEMPTY(&appctx->ctx.sess.bref.users)) {
6511 LIST_DEL(&appctx->ctx.sess.bref.users);
6512 LIST_INIT(&appctx->ctx.sess.bref.users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +01006513 }
6514
6515 /* and start from where we stopped */
Willy Tarreau87b09662015-04-03 00:22:06 +02006516 while (appctx->ctx.sess.bref.ref != &streams) {
Cyril Bontéacd7d632010-11-01 19:26:02 +01006517 char pn[INET6_ADDRSTRLEN];
Willy Tarreau87b09662015-04-03 00:22:06 +02006518 struct stream *curr_sess;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006519
Willy Tarreau87b09662015-04-03 00:22:06 +02006520 curr_sess = LIST_ELEM(appctx->ctx.sess.bref.ref, struct stream *, list);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006521
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006522 if (appctx->ctx.sess.target) {
6523 if (appctx->ctx.sess.target != (void *)-1 && appctx->ctx.sess.target != curr_sess)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006524 goto next_sess;
6525
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006526 LIST_ADDQ(&curr_sess->back_refs, &appctx->ctx.sess.bref.users);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006527 /* call the proper dump() function and return if we're missing space */
Willy Tarreau76153662012-11-26 01:16:39 +01006528 if (!stats_dump_full_sess_to_buffer(si, curr_sess))
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006529 return 0;
6530
Willy Tarreau87b09662015-04-03 00:22:06 +02006531 /* stream dump complete */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006532 LIST_DEL(&appctx->ctx.sess.bref.users);
6533 LIST_INIT(&appctx->ctx.sess.bref.users);
6534 if (appctx->ctx.sess.target != (void *)-1) {
6535 appctx->ctx.sess.target = NULL;
Willy Tarreau76153662012-11-26 01:16:39 +01006536 break;
6537 }
6538 else
6539 goto next_sess;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006540 }
6541
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006542 chunk_appendf(&trash,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006543 "%p: proto=%s",
6544 curr_sess,
Willy Tarreauf1e02122015-09-23 12:16:43 +02006545 strm_li(curr_sess) ? strm_li(curr_sess)->proto->name : "?");
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006546
Willy Tarreaud0d8da92015-04-04 02:10:38 +02006547 conn = objt_conn(strm_orig(curr_sess));
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006548 switch (conn ? addr_to_str(&conn->addr.from, pn, sizeof(pn)) : AF_UNSPEC) {
Willy Tarreau631f01c2011-09-05 00:36:48 +02006549 case AF_INET:
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006550 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006551 chunk_appendf(&trash,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006552 " src=%s:%d fe=%s be=%s srv=%s",
6553 pn,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006554 get_host_port(&conn->addr.from),
Willy Tarreaud0d8da92015-04-04 02:10:38 +02006555 strm_fe(curr_sess)->id,
Willy Tarreau50943332011-09-02 17:33:05 +02006556 (curr_sess->be->cap & PR_CAP_BE) ? curr_sess->be->id : "<NONE>",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006557 objt_server(curr_sess->target) ? objt_server(curr_sess->target)->id : "<none>"
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006558 );
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006559 break;
6560 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006561 chunk_appendf(&trash,
Emeric Brun837ca522010-10-22 16:19:01 +02006562 " src=unix:%d fe=%s be=%s srv=%s",
Willy Tarreaud0d8da92015-04-04 02:10:38 +02006563 strm_li(curr_sess)->luid,
6564 strm_fe(curr_sess)->id,
Willy Tarreau50943332011-09-02 17:33:05 +02006565 (curr_sess->be->cap & PR_CAP_BE) ? curr_sess->be->id : "<NONE>",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006566 objt_server(curr_sess->target) ? objt_server(curr_sess->target)->id : "<none>"
Emeric Brun837ca522010-10-22 16:19:01 +02006567 );
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006568 break;
6569 }
6570
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006571 chunk_appendf(&trash,
Willy Tarreau65671ab2009-10-04 14:24:59 +02006572 " ts=%02x age=%s calls=%d",
6573 curr_sess->task->state,
Willy Tarreau3884cba2009-03-28 17:54:35 +01006574 human_time(now.tv_sec - curr_sess->logs.tv_accept.tv_sec, 1),
6575 curr_sess->task->calls);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006576
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006577 chunk_appendf(&trash,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01006578 " rq[f=%06xh,i=%d,an=%02xh,rx=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006579 curr_sess->req.flags,
6580 curr_sess->req.buf->i,
6581 curr_sess->req.analysers,
6582 curr_sess->req.rex ?
6583 human_time(TICKS_TO_MS(curr_sess->req.rex - now_ms),
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006584 TICKS_TO_MS(1000)) : "");
6585
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006586 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006587 ",wx=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006588 curr_sess->req.wex ?
6589 human_time(TICKS_TO_MS(curr_sess->req.wex - 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 ",ax=%s]",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006594 curr_sess->req.analyse_exp ?
6595 human_time(TICKS_TO_MS(curr_sess->req.analyse_exp - 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 Tarreau02d6cfc2012-03-01 18:19:58 +01006599 " rp[f=%06xh,i=%d,an=%02xh,rx=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006600 curr_sess->res.flags,
6601 curr_sess->res.buf->i,
6602 curr_sess->res.analysers,
6603 curr_sess->res.rex ?
6604 human_time(TICKS_TO_MS(curr_sess->res.rex - now_ms),
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006605 TICKS_TO_MS(1000)) : "");
6606
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006607 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006608 ",wx=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006609 curr_sess->res.wex ?
6610 human_time(TICKS_TO_MS(curr_sess->res.wex - now_ms),
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006611 TICKS_TO_MS(1000)) : "");
6612
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006613 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006614 ",ax=%s]",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006615 curr_sess->res.analyse_exp ?
6616 human_time(TICKS_TO_MS(curr_sess->res.analyse_exp - now_ms),
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006617 TICKS_TO_MS(1000)) : "");
6618
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006619 conn = objt_conn(curr_sess->si[0].end);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006620 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006621 " s0=[%d,%1xh,fd=%d,ex=%s]",
6622 curr_sess->si[0].state,
6623 curr_sess->si[0].flags,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006624 conn ? conn->t.sock.fd : -1,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006625 curr_sess->si[0].exp ?
6626 human_time(TICKS_TO_MS(curr_sess->si[0].exp - now_ms),
6627 TICKS_TO_MS(1000)) : "");
6628
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006629 conn = objt_conn(curr_sess->si[1].end);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006630 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006631 " s1=[%d,%1xh,fd=%d,ex=%s]",
6632 curr_sess->si[1].state,
6633 curr_sess->si[1].flags,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006634 conn ? conn->t.sock.fd : -1,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006635 curr_sess->si[1].exp ?
6636 human_time(TICKS_TO_MS(curr_sess->si[1].exp - now_ms),
6637 TICKS_TO_MS(1000)) : "");
6638
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006639 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006640 " exp=%s",
6641 curr_sess->task->expire ?
6642 human_time(TICKS_TO_MS(curr_sess->task->expire - now_ms),
6643 TICKS_TO_MS(1000)) : "");
Willy Tarreau4726f532009-03-07 17:25:21 +01006644 if (task_in_rq(curr_sess->task))
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006645 chunk_appendf(&trash, " run(nice=%d)", curr_sess->task->nice);
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006646
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006647 chunk_appendf(&trash, "\n");
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006648
Willy Tarreau2bb4a962014-11-28 11:11:05 +01006649 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau87b09662015-04-03 00:22:06 +02006650 /* let's try again later from this stream. We add ourselves into
6651 * this stream's users so that it can remove us upon termination.
Willy Tarreaufd3828e2009-02-22 15:17:24 +01006652 */
Willy Tarreaufe127932015-04-21 19:23:39 +02006653 si_applet_cant_put(si);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006654 LIST_ADDQ(&curr_sess->back_refs, &appctx->ctx.sess.bref.users);
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02006655 return 0;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006656 }
6657
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006658 next_sess:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006659 appctx->ctx.sess.bref.ref = curr_sess->list.n;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006660 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006661
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006662 if (appctx->ctx.sess.target && appctx->ctx.sess.target != (void *)-1) {
Willy Tarreau87b09662015-04-03 00:22:06 +02006663 /* specified stream not found */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006664 if (appctx->ctx.sess.section > 0)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006665 chunk_appendf(&trash, " *** session terminated while we were watching it ***\n");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006666 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006667 chunk_appendf(&trash, "Session not found.\n");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006668
Willy Tarreaubc18da12015-03-13 14:00:47 +01006669 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02006670 si_applet_cant_put(si);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006671 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01006672 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006673
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006674 appctx->ctx.sess.target = NULL;
6675 appctx->ctx.sess.uid = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006676 return 1;
6677 }
6678
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006679 appctx->st2 = STAT_ST_FIN;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006680 /* fall through */
6681
6682 default:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006683 appctx->st2 = STAT_ST_FIN;
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02006684 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006685 }
Emeric Brun1e029aa2010-09-23 18:12:53 +02006686}
6687
Willy Tarreau5f9a8772012-11-26 02:22:40 +01006688/* This is called when the stream interface is closed. For instance, upon an
6689 * external abort, we won't call the i/o handler anymore so we may need to
Willy Tarreau87b09662015-04-03 00:22:06 +02006690 * remove back references to the stream currently being dumped.
Willy Tarreau5f9a8772012-11-26 02:22:40 +01006691 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02006692static void cli_release_handler(struct appctx *appctx)
Willy Tarreau5f9a8772012-11-26 02:22:40 +01006693{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006694 if (appctx->st0 == STAT_CLI_O_SESS && appctx->st2 == STAT_ST_LIST) {
6695 if (!LIST_ISEMPTY(&appctx->ctx.sess.bref.users))
6696 LIST_DEL(&appctx->ctx.sess.bref.users);
Willy Tarreau5f9a8772012-11-26 02:22:40 +01006697 }
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01006698 else if (appctx->st0 == STAT_CLI_PRINT_FREE) {
6699 free(appctx->ctx.cli.err);
Willy Tarreau6457d0f2015-10-07 20:00:24 +02006700 appctx->ctx.cli.err = NULL;
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01006701 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006702 else if (appctx->st0 == STAT_CLI_O_MLOOK) {
6703 free(appctx->ctx.map.chunk.str);
Willy Tarreau6457d0f2015-10-07 20:00:24 +02006704 appctx->ctx.map.chunk.str = NULL;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006705 }
Willy Tarreau5f9a8772012-11-26 02:22:40 +01006706}
6707
Willy Tarreau20e99322013-04-13 09:22:25 +02006708/* This function is used to either dump tables states (when action is set
6709 * to STAT_CLI_O_TAB) or clear tables (when action is STAT_CLI_O_CLR).
Willy Tarreau20e99322013-04-13 09:22:25 +02006710 * It returns 0 if the output buffer is full and it needs to be called
6711 * again, otherwise non-zero.
Willy Tarreau69f58c82010-07-12 17:55:33 +02006712 */
Willy Tarreau20e99322013-04-13 09:22:25 +02006713static int stats_table_request(struct stream_interface *si, int action)
Willy Tarreau69f58c82010-07-12 17:55:33 +02006714{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006715 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau87b09662015-04-03 00:22:06 +02006716 struct stream *s = si_strm(si);
Willy Tarreau69f58c82010-07-12 17:55:33 +02006717 struct ebmb_node *eb;
6718 int dt;
Willy Tarreau44455022012-12-05 23:01:12 +01006719 int skip_entry;
Willy Tarreau20e99322013-04-13 09:22:25 +02006720 int show = action == STAT_CLI_O_TAB;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006721
6722 /*
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006723 * We have 3 possible states in appctx->st2 :
Willy Tarreau295a8372011-03-10 11:25:07 +01006724 * - STAT_ST_INIT : the first call
6725 * - STAT_ST_INFO : the proxy pointer points to the next table to
Willy Tarreau69f58c82010-07-12 17:55:33 +02006726 * dump, the entry pointer is NULL ;
Willy Tarreau295a8372011-03-10 11:25:07 +01006727 * - STAT_ST_LIST : the proxy pointer points to the current table
Willy Tarreau69f58c82010-07-12 17:55:33 +02006728 * and the entry pointer points to the next entry to be dumped,
6729 * and the refcount on the next entry is held ;
Willy Tarreau295a8372011-03-10 11:25:07 +01006730 * - STAT_ST_END : nothing left to dump, the buffer may contain some
Willy Tarreau69f58c82010-07-12 17:55:33 +02006731 * data though.
6732 */
6733
Willy Tarreau2bb4a962014-11-28 11:11:05 +01006734 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW))) {
Willy Tarreau69f58c82010-07-12 17:55:33 +02006735 /* in case of abort, remove any refcount we might have set on an entry */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006736 if (appctx->st2 == STAT_ST_LIST) {
6737 appctx->ctx.table.entry->ref_cnt--;
6738 stksess_kill_if_expired(&appctx->ctx.table.proxy->table, appctx->ctx.table.entry);
Willy Tarreauf6efda12010-08-03 20:34:06 +02006739 }
Willy Tarreau69f58c82010-07-12 17:55:33 +02006740 return 1;
6741 }
6742
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006743 chunk_reset(&trash);
Willy Tarreau69f58c82010-07-12 17:55:33 +02006744
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006745 while (appctx->st2 != STAT_ST_FIN) {
6746 switch (appctx->st2) {
Willy Tarreau295a8372011-03-10 11:25:07 +01006747 case STAT_ST_INIT:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006748 appctx->ctx.table.proxy = appctx->ctx.table.target;
6749 if (!appctx->ctx.table.proxy)
6750 appctx->ctx.table.proxy = proxy;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006751
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006752 appctx->ctx.table.entry = NULL;
6753 appctx->st2 = STAT_ST_INFO;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006754 break;
6755
Willy Tarreau295a8372011-03-10 11:25:07 +01006756 case STAT_ST_INFO:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006757 if (!appctx->ctx.table.proxy ||
6758 (appctx->ctx.table.target &&
6759 appctx->ctx.table.proxy != appctx->ctx.table.target)) {
6760 appctx->st2 = STAT_ST_END;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006761 break;
6762 }
6763
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006764 if (appctx->ctx.table.proxy->table.size) {
6765 if (show && !stats_dump_table_head_to_buffer(&trash, si, appctx->ctx.table.proxy,
6766 appctx->ctx.table.target))
Willy Tarreau69f58c82010-07-12 17:55:33 +02006767 return 0;
6768
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006769 if (appctx->ctx.table.target &&
Willy Tarreaud0d8da92015-04-04 02:10:38 +02006770 strm_li(s)->bind_conf->level >= ACCESS_LVL_OPER) {
Willy Tarreau69f58c82010-07-12 17:55:33 +02006771 /* dump entries only if table explicitly requested */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006772 eb = ebmb_first(&appctx->ctx.table.proxy->table.keys);
Willy Tarreau69f58c82010-07-12 17:55:33 +02006773 if (eb) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006774 appctx->ctx.table.entry = ebmb_entry(eb, struct stksess, key);
6775 appctx->ctx.table.entry->ref_cnt++;
6776 appctx->st2 = STAT_ST_LIST;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006777 break;
6778 }
6779 }
6780 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006781 appctx->ctx.table.proxy = appctx->ctx.table.proxy->next;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006782 break;
6783
Willy Tarreau295a8372011-03-10 11:25:07 +01006784 case STAT_ST_LIST:
Willy Tarreau44455022012-12-05 23:01:12 +01006785 skip_entry = 0;
Simon Hormanc88b8872011-06-15 15:18:49 +09006786
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006787 if (appctx->ctx.table.data_type >= 0) {
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02006788 /* we're filtering on some data contents */
6789 void *ptr;
6790 long long data;
6791
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006792 dt = appctx->ctx.table.data_type;
6793 ptr = stktable_data_ptr(&appctx->ctx.table.proxy->table,
6794 appctx->ctx.table.entry,
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02006795 dt);
6796
6797 data = 0;
6798 switch (stktable_data_types[dt].std_type) {
6799 case STD_T_SINT:
6800 data = stktable_data_cast(ptr, std_t_sint);
6801 break;
6802 case STD_T_UINT:
6803 data = stktable_data_cast(ptr, std_t_uint);
6804 break;
6805 case STD_T_ULL:
6806 data = stktable_data_cast(ptr, std_t_ull);
6807 break;
6808 case STD_T_FRQP:
6809 data = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006810 appctx->ctx.table.proxy->table.data_arg[dt].u);
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02006811 break;
6812 }
6813
6814 /* skip the entry if the data does not match the test and the value */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006815 if ((data < appctx->ctx.table.value &&
6816 (appctx->ctx.table.data_op == STD_OP_EQ ||
6817 appctx->ctx.table.data_op == STD_OP_GT ||
6818 appctx->ctx.table.data_op == STD_OP_GE)) ||
6819 (data == appctx->ctx.table.value &&
6820 (appctx->ctx.table.data_op == STD_OP_NE ||
6821 appctx->ctx.table.data_op == STD_OP_GT ||
6822 appctx->ctx.table.data_op == STD_OP_LT)) ||
6823 (data > appctx->ctx.table.value &&
6824 (appctx->ctx.table.data_op == STD_OP_EQ ||
6825 appctx->ctx.table.data_op == STD_OP_LT ||
6826 appctx->ctx.table.data_op == STD_OP_LE)))
Willy Tarreau44455022012-12-05 23:01:12 +01006827 skip_entry = 1;
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02006828 }
6829
Simon Hormanc88b8872011-06-15 15:18:49 +09006830 if (show && !skip_entry &&
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006831 !stats_dump_table_entry_to_buffer(&trash, si, appctx->ctx.table.proxy,
6832 appctx->ctx.table.entry))
Simon Hormand9366582011-06-15 15:18:45 +09006833 return 0;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006834
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006835 appctx->ctx.table.entry->ref_cnt--;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006836
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006837 eb = ebmb_next(&appctx->ctx.table.entry->key);
Willy Tarreau69f58c82010-07-12 17:55:33 +02006838 if (eb) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006839 struct stksess *old = appctx->ctx.table.entry;
6840 appctx->ctx.table.entry = ebmb_entry(eb, struct stksess, key);
Willy Tarreau8fa52f42012-01-09 11:50:03 +01006841 if (show)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006842 stksess_kill_if_expired(&appctx->ctx.table.proxy->table, old);
6843 else if (!skip_entry && !appctx->ctx.table.entry->ref_cnt)
6844 stksess_kill(&appctx->ctx.table.proxy->table, old);
6845 appctx->ctx.table.entry->ref_cnt++;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006846 break;
6847 }
6848
Simon Hormanc88b8872011-06-15 15:18:49 +09006849
6850 if (show)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006851 stksess_kill_if_expired(&appctx->ctx.table.proxy->table, appctx->ctx.table.entry);
6852 else if (!skip_entry && !appctx->ctx.table.entry->ref_cnt)
6853 stksess_kill(&appctx->ctx.table.proxy->table, appctx->ctx.table.entry);
Simon Hormanc88b8872011-06-15 15:18:49 +09006854
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006855 appctx->ctx.table.proxy = appctx->ctx.table.proxy->next;
6856 appctx->st2 = STAT_ST_INFO;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006857 break;
6858
Willy Tarreau295a8372011-03-10 11:25:07 +01006859 case STAT_ST_END:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006860 appctx->st2 = STAT_ST_FIN;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006861 break;
6862 }
6863 }
6864 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006865}
6866
Willy Tarreaud426a182010-03-05 14:58:26 +01006867/* print a line of text buffer (limited to 70 bytes) to <out>. The format is :
Willy Tarreau74808cb2009-03-04 15:53:18 +01006868 * <2 spaces> <offset=5 digits> <space or plus> <space> <70 chars max> <\n>
6869 * which is 60 chars per line. Non-printable chars \t, \n, \r and \e are
6870 * encoded in C format. Other non-printable chars are encoded "\xHH". Original
6871 * lines are respected within the limit of 70 output chars. Lines that are
6872 * continuation of a previous truncated line begin with "+" instead of " "
6873 * after the offset. The new pointer is returned.
6874 */
Willy Tarreaud426a182010-03-05 14:58:26 +01006875static int dump_text_line(struct chunk *out, const char *buf, int bsize, int len,
6876 int *line, int ptr)
Willy Tarreau74808cb2009-03-04 15:53:18 +01006877{
6878 int end;
6879 unsigned char c;
6880
6881 end = out->len + 80;
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02006882 if (end > out->size)
Willy Tarreau74808cb2009-03-04 15:53:18 +01006883 return ptr;
6884
Willy Tarreau77804732012-10-29 16:14:26 +01006885 chunk_appendf(out, " %05d%c ", ptr, (ptr == *line) ? ' ' : '+');
Willy Tarreau74808cb2009-03-04 15:53:18 +01006886
Willy Tarreaud426a182010-03-05 14:58:26 +01006887 while (ptr < len && ptr < bsize) {
6888 c = buf[ptr];
Willy Tarreau787bbd92009-03-12 08:18:33 +01006889 if (isprint(c) && isascii(c) && c != '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006890 if (out->len > end - 2)
6891 break;
6892 out->str[out->len++] = c;
Willy Tarreau787bbd92009-03-12 08:18:33 +01006893 } else if (c == '\t' || c == '\n' || c == '\r' || c == '\e' || c == '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006894 if (out->len > end - 3)
6895 break;
6896 out->str[out->len++] = '\\';
6897 switch (c) {
6898 case '\t': c = 't'; break;
6899 case '\n': c = 'n'; break;
6900 case '\r': c = 'r'; break;
6901 case '\e': c = 'e'; break;
Willy Tarreau787bbd92009-03-12 08:18:33 +01006902 case '\\': c = '\\'; break;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006903 }
6904 out->str[out->len++] = c;
6905 } else {
6906 if (out->len > end - 5)
6907 break;
6908 out->str[out->len++] = '\\';
6909 out->str[out->len++] = 'x';
6910 out->str[out->len++] = hextab[(c >> 4) & 0xF];
6911 out->str[out->len++] = hextab[c & 0xF];
6912 }
Willy Tarreaud426a182010-03-05 14:58:26 +01006913 if (buf[ptr++] == '\n') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006914 /* we had a line break, let's return now */
6915 out->str[out->len++] = '\n';
6916 *line = ptr;
6917 return ptr;
6918 }
6919 }
6920 /* we have an incomplete line, we return it as-is */
6921 out->str[out->len++] = '\n';
6922 return ptr;
6923}
6924
Baptiste Assmann3863f972015-05-17 00:33:24 +02006925/* This function dumps counters from all resolvers section and associated name servers.
6926 * It returns 0 if the output buffer is full and it needs
6927 * to be called again, otherwise non-zero.
6928 */
6929static int stats_dump_resolvers_to_buffer(struct stream_interface *si)
6930{
6931 struct appctx *appctx = __objt_appctx(si->end);
6932 struct dns_resolvers *presolvers;
6933 struct dns_nameserver *pnameserver;
6934
6935 chunk_reset(&trash);
6936
6937 switch (appctx->st2) {
6938 case STAT_ST_INIT:
6939 appctx->st2 = STAT_ST_LIST; /* let's start producing data */
6940 /* fall through */
6941
6942 case STAT_ST_LIST:
Andrew Hayworth68d05342015-10-02 20:33:01 +00006943 if (LIST_ISEMPTY(&dns_resolvers)) {
6944 chunk_appendf(&trash, "No resolvers found\n");
6945 }
6946 else {
6947 list_for_each_entry(presolvers, &dns_resolvers, list) {
6948 if (appctx->ctx.resolvers.ptr != NULL && appctx->ctx.resolvers.ptr != presolvers)
6949 continue;
6950
6951 chunk_appendf(&trash, "Resolvers section %s\n", presolvers->id);
6952 list_for_each_entry(pnameserver, &presolvers->nameserver_list, list) {
6953 chunk_appendf(&trash, " nameserver %s:\n", pnameserver->id);
6954 chunk_appendf(&trash, " sent: %ld\n", pnameserver->counters.sent);
6955 chunk_appendf(&trash, " valid: %ld\n", pnameserver->counters.valid);
6956 chunk_appendf(&trash, " update: %ld\n", pnameserver->counters.update);
6957 chunk_appendf(&trash, " cname: %ld\n", pnameserver->counters.cname);
6958 chunk_appendf(&trash, " cname_error: %ld\n", pnameserver->counters.cname_error);
6959 chunk_appendf(&trash, " any_err: %ld\n", pnameserver->counters.any_err);
6960 chunk_appendf(&trash, " nx: %ld\n", pnameserver->counters.nx);
6961 chunk_appendf(&trash, " timeout: %ld\n", pnameserver->counters.timeout);
6962 chunk_appendf(&trash, " refused: %ld\n", pnameserver->counters.refused);
6963 chunk_appendf(&trash, " other: %ld\n", pnameserver->counters.other);
6964 chunk_appendf(&trash, " invalid: %ld\n", pnameserver->counters.invalid);
6965 chunk_appendf(&trash, " too_big: %ld\n", pnameserver->counters.too_big);
6966 chunk_appendf(&trash, " truncated: %ld\n", pnameserver->counters.truncated);
6967 chunk_appendf(&trash, " outdated: %ld\n", pnameserver->counters.outdated);
6968 }
6969 }
Baptiste Assmann3863f972015-05-17 00:33:24 +02006970 }
6971
6972 /* display response */
6973 if (bi_putchk(si_ic(si), &trash) == -1) {
6974 /* let's try again later from this session. We add ourselves into
6975 * this session's users so that it can remove us upon termination.
6976 */
6977 si->flags |= SI_FL_WAIT_ROOM;
6978 return 0;
6979 }
6980
6981 appctx->st2 = STAT_ST_FIN;
6982 /* fall through */
6983
6984 default:
6985 appctx->st2 = STAT_ST_FIN;
6986 return 1;
6987 }
6988}
6989
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02006990/* This function dumps all captured errors onto the stream interface's
Willy Tarreau306f8302013-07-08 15:53:06 +02006991 * read buffer. It returns 0 if the output buffer is full and it needs
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01006992 * to be called again, otherwise non-zero.
Willy Tarreau74808cb2009-03-04 15:53:18 +01006993 */
Simon Horman9bd2c732011-06-15 15:18:44 +09006994static int stats_dump_errors_to_buffer(struct stream_interface *si)
Willy Tarreau74808cb2009-03-04 15:53:18 +01006995{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006996 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau74808cb2009-03-04 15:53:18 +01006997 extern const char *monthname[12];
Willy Tarreau74808cb2009-03-04 15:53:18 +01006998
Willy Tarreau2bb4a962014-11-28 11:11:05 +01006999 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
Willy Tarreau61b34732009-10-03 23:49:35 +02007000 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01007001
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007002 chunk_reset(&trash);
Willy Tarreau74808cb2009-03-04 15:53:18 +01007003
Willy Tarreau7b4b4992013-12-01 09:15:12 +01007004 if (!appctx->ctx.errors.px) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01007005 /* the function had not been called yet, let's prepare the
7006 * buffer for a response.
7007 */
Willy Tarreau10479e42010-12-12 14:00:34 +01007008 struct tm tm;
7009
7010 get_localtime(date.tv_sec, &tm);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007011 chunk_appendf(&trash, "Total events captured on [%02d/%s/%04d:%02d:%02d:%02d.%03d] : %u\n",
Willy Tarreau10479e42010-12-12 14:00:34 +01007012 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
7013 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(date.tv_usec/1000),
7014 error_snapshot_id);
7015
Willy Tarreau2bb4a962014-11-28 11:11:05 +01007016 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau10479e42010-12-12 14:00:34 +01007017 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreaufe127932015-04-21 19:23:39 +02007018 si_applet_cant_put(si);
Willy Tarreau10479e42010-12-12 14:00:34 +01007019 return 0;
7020 }
7021
Willy Tarreau7b4b4992013-12-01 09:15:12 +01007022 appctx->ctx.errors.px = proxy;
7023 appctx->ctx.errors.buf = 0;
7024 appctx->ctx.errors.bol = 0;
7025 appctx->ctx.errors.ptr = -1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01007026 }
7027
7028 /* we have two inner loops here, one for the proxy, the other one for
7029 * the buffer.
7030 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01007031 while (appctx->ctx.errors.px) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01007032 struct error_snapshot *es;
7033
Willy Tarreau7b4b4992013-12-01 09:15:12 +01007034 if (appctx->ctx.errors.buf == 0)
7035 es = &appctx->ctx.errors.px->invalid_req;
Willy Tarreau74808cb2009-03-04 15:53:18 +01007036 else
Willy Tarreau7b4b4992013-12-01 09:15:12 +01007037 es = &appctx->ctx.errors.px->invalid_rep;
Willy Tarreau74808cb2009-03-04 15:53:18 +01007038
7039 if (!es->when.tv_sec)
7040 goto next;
7041
Willy Tarreau7b4b4992013-12-01 09:15:12 +01007042 if (appctx->ctx.errors.iid >= 0 &&
7043 appctx->ctx.errors.px->uuid != appctx->ctx.errors.iid &&
7044 es->oe->uuid != appctx->ctx.errors.iid)
Willy Tarreau74808cb2009-03-04 15:53:18 +01007045 goto next;
7046
Willy Tarreau7b4b4992013-12-01 09:15:12 +01007047 if (appctx->ctx.errors.ptr < 0) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01007048 /* just print headers now */
7049
7050 char pn[INET6_ADDRSTRLEN];
7051 struct tm tm;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02007052 int port;
Willy Tarreau74808cb2009-03-04 15:53:18 +01007053
7054 get_localtime(es->when.tv_sec, &tm);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007055 chunk_appendf(&trash, " \n[%02d/%s/%04d:%02d:%02d:%02d.%03d]",
Willy Tarreau74808cb2009-03-04 15:53:18 +01007056 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
Willy Tarreau1772ece2009-04-03 14:49:12 +02007057 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(es->when.tv_usec/1000));
Willy Tarreau74808cb2009-03-04 15:53:18 +01007058
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02007059 switch (addr_to_str(&es->src, pn, sizeof(pn))) {
7060 case AF_INET:
7061 case AF_INET6:
7062 port = get_host_port(&es->src);
7063 break;
7064 default:
7065 port = 0;
7066 }
7067
Willy Tarreau7b4b4992013-12-01 09:15:12 +01007068 switch (appctx->ctx.errors.buf) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01007069 case 0:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007070 chunk_appendf(&trash,
Willy Tarreau74808cb2009-03-04 15:53:18 +01007071 " frontend %s (#%d): invalid request\n"
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02007072 " backend %s (#%d)",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01007073 appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02007074 (es->oe->cap & PR_CAP_BE) ? es->oe->id : "<NONE>",
7075 (es->oe->cap & PR_CAP_BE) ? es->oe->uuid : -1);
Willy Tarreau74808cb2009-03-04 15:53:18 +01007076 break;
7077 case 1:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007078 chunk_appendf(&trash,
Olivier Doucet08afdcb2014-09-08 11:23:00 +02007079 " backend %s (#%d): invalid response\n"
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02007080 " frontend %s (#%d)",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01007081 appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02007082 es->oe->id, es->oe->uuid);
Willy Tarreau74808cb2009-03-04 15:53:18 +01007083 break;
7084 }
7085
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007086 chunk_appendf(&trash,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02007087 ", server %s (#%d), event #%u\n"
7088 " src %s:%d, session #%d, session flags 0x%08x\n"
7089 " HTTP msg state %d, msg flags 0x%08x, tx flags 0x%08x\n"
7090 " HTTP chunk len %lld bytes, HTTP body len %lld bytes\n"
7091 " buffer flags 0x%08x, out %d bytes, total %lld bytes\n"
7092 " pending %d bytes, wrapping at %d, error at position %d:\n \n",
7093 es->srv ? es->srv->id : "<NONE>", es->srv ? es->srv->puid : -1,
7094 es->ev_id,
7095 pn, port, es->sid, es->s_flags,
7096 es->state, es->m_flags, es->t_flags,
7097 es->m_clen, es->m_blen,
7098 es->b_flags, es->b_out, es->b_tot,
7099 es->len, es->b_wrap, es->pos);
7100
Willy Tarreau2bb4a962014-11-28 11:11:05 +01007101 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01007102 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreaufe127932015-04-21 19:23:39 +02007103 si_applet_cant_put(si);
Willy Tarreau61b34732009-10-03 23:49:35 +02007104 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01007105 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01007106 appctx->ctx.errors.ptr = 0;
7107 appctx->ctx.errors.sid = es->sid;
Willy Tarreau74808cb2009-03-04 15:53:18 +01007108 }
7109
Willy Tarreau7b4b4992013-12-01 09:15:12 +01007110 if (appctx->ctx.errors.sid != es->sid) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01007111 /* the snapshot changed while we were dumping it */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007112 chunk_appendf(&trash,
Willy Tarreau74808cb2009-03-04 15:53:18 +01007113 " WARNING! update detected on this snapshot, dump interrupted. Please re-check!\n");
Willy Tarreaubc18da12015-03-13 14:00:47 +01007114 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02007115 si_applet_cant_put(si);
Willy Tarreau61b34732009-10-03 23:49:35 +02007116 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01007117 }
Willy Tarreau74808cb2009-03-04 15:53:18 +01007118 goto next;
7119 }
7120
7121 /* OK, ptr >= 0, so we have to dump the current line */
Willy Tarreauf3764b72016-03-31 13:45:10 +02007122 while (es->buf && appctx->ctx.errors.ptr < es->len && appctx->ctx.errors.ptr < global.tune.bufsize) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01007123 int newptr;
7124 int newline;
7125
Willy Tarreau7b4b4992013-12-01 09:15:12 +01007126 newline = appctx->ctx.errors.bol;
Willy Tarreauf3764b72016-03-31 13:45:10 +02007127 newptr = dump_text_line(&trash, es->buf, global.tune.bufsize, es->len, &newline, appctx->ctx.errors.ptr);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01007128 if (newptr == appctx->ctx.errors.ptr)
Willy Tarreau61b34732009-10-03 23:49:35 +02007129 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01007130
Willy Tarreau2bb4a962014-11-28 11:11:05 +01007131 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01007132 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreaufe127932015-04-21 19:23:39 +02007133 si_applet_cant_put(si);
Willy Tarreau61b34732009-10-03 23:49:35 +02007134 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01007135 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01007136 appctx->ctx.errors.ptr = newptr;
7137 appctx->ctx.errors.bol = newline;
Willy Tarreau74808cb2009-03-04 15:53:18 +01007138 };
7139 next:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01007140 appctx->ctx.errors.bol = 0;
7141 appctx->ctx.errors.ptr = -1;
7142 appctx->ctx.errors.buf++;
7143 if (appctx->ctx.errors.buf > 1) {
7144 appctx->ctx.errors.buf = 0;
7145 appctx->ctx.errors.px = appctx->ctx.errors.px->next;
Willy Tarreau74808cb2009-03-04 15:53:18 +01007146 }
Willy Tarreauae795722016-02-16 11:27:28 +01007147 }
7148
7149 /* dump complete */
7150 return 1;
7151}
7152
7153/* This function dumps all environmnent variables to the buffer. It returns 0
7154 * if the output buffer is full and it needs to be called again, otherwise
7155 * non-zero. Dumps only one entry if st2 == STAT_ST_END.
7156 */
7157static int stats_dump_env_to_buffer(struct stream_interface *si)
7158{
7159 struct appctx *appctx = __objt_appctx(si->end);
7160
7161 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
7162 return 1;
7163
7164 chunk_reset(&trash);
7165
7166 /* we have two inner loops here, one for the proxy, the other one for
7167 * the buffer.
7168 */
7169 while (*appctx->ctx.env.var) {
7170 chunk_printf(&trash, "%s\n", *appctx->ctx.env.var);
7171
7172 if (bi_putchk(si_ic(si), &trash) == -1) {
7173 si_applet_cant_put(si);
7174 return 0;
7175 }
7176 if (appctx->st2 == STAT_ST_END)
7177 break;
7178 appctx->ctx.env.var++;
Willy Tarreau74808cb2009-03-04 15:53:18 +01007179 }
7180
7181 /* dump complete */
Willy Tarreau61b34732009-10-03 23:49:35 +02007182 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01007183}
7184
Willy Tarreaud5781202012-09-22 19:32:35 +02007185/* parse the "level" argument on the bind lines */
7186static int bind_parse_level(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
7187{
7188 if (!*args[cur_arg + 1]) {
7189 memprintf(err, "'%s' : missing level", args[cur_arg]);
7190 return ERR_ALERT | ERR_FATAL;
7191 }
7192
7193 if (!strcmp(args[cur_arg+1], "user"))
7194 conf->level = ACCESS_LVL_USER;
7195 else if (!strcmp(args[cur_arg+1], "operator"))
7196 conf->level = ACCESS_LVL_OPER;
7197 else if (!strcmp(args[cur_arg+1], "admin"))
7198 conf->level = ACCESS_LVL_ADMIN;
7199 else {
7200 memprintf(err, "'%s' only supports 'user', 'operator', and 'admin' (got '%s')",
7201 args[cur_arg], args[cur_arg+1]);
7202 return ERR_ALERT | ERR_FATAL;
7203 }
7204
7205 return 0;
7206}
7207
Willy Tarreau30576452015-04-13 13:50:30 +02007208struct applet http_stats_applet = {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007209 .obj_type = OBJ_TYPE_APPLET,
Willy Tarreaub24281b2011-02-13 13:16:36 +01007210 .name = "<STATS>", /* used for logging */
7211 .fct = http_stats_io_handler,
Aman Gupta9a13e842012-04-02 18:57:53 -07007212 .release = NULL,
Willy Tarreaub24281b2011-02-13 13:16:36 +01007213};
7214
Willy Tarreau30576452015-04-13 13:50:30 +02007215static struct applet cli_applet = {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007216 .obj_type = OBJ_TYPE_APPLET,
Willy Tarreaub24281b2011-02-13 13:16:36 +01007217 .name = "<CLI>", /* used for logging */
7218 .fct = cli_io_handler,
Willy Tarreau5f9a8772012-11-26 02:22:40 +01007219 .release = cli_release_handler,
Willy Tarreaub24281b2011-02-13 13:16:36 +01007220};
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01007221
Willy Tarreaudc13c112013-06-21 23:16:39 +02007222static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreau10522fd2008-07-09 20:12:41 +02007223 { CFG_GLOBAL, "stats", stats_parse_global },
7224 { 0, NULL, NULL },
7225}};
7226
Willy Tarreaud5781202012-09-22 19:32:35 +02007227static struct bind_kw_list bind_kws = { "STAT", { }, {
7228 { "level", bind_parse_level, 1 }, /* set the unix socket admin level */
7229 { NULL, NULL, 0 },
7230}};
7231
Willy Tarreau10522fd2008-07-09 20:12:41 +02007232__attribute__((constructor))
7233static void __dumpstats_module_init(void)
7234{
7235 cfg_register_keywords(&cfg_kws);
Willy Tarreaud5781202012-09-22 19:32:35 +02007236 bind_register_keywords(&bind_kws);
Willy Tarreau10522fd2008-07-09 20:12:41 +02007237}
7238
Willy Tarreau91861262007-10-17 17:06:05 +02007239/*
7240 * Local variables:
7241 * c-indent-level: 8
7242 * c-basic-offset: 8
7243 * End:
7244 */