blob: 0a047b84bfcc4d088a6bcc29b5ac52de1938d894 [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 Assmann50946562016-08-31 23:26:29 +02001782 else if (strcmp(args[3], "check-port") == 0) {
1783 int i = 0;
1784 if (strl2irc(args[4], strlen(args[4]), &i) != 0) {
1785 appctx->ctx.cli.msg = "'set server <srv> check-port' expects an integer as argument.\n";
1786 appctx->st0 = STAT_CLI_PRINT;
1787 }
1788 if ((i < 0) || (i > 65535)) {
1789 appctx->ctx.cli.msg = "provided port is not valid.\n";
1790 appctx->st0 = STAT_CLI_PRINT;
1791 }
1792 /* prevent the update of port to 0 if MAPPORTS are in use */
1793 if ((sv->flags & SRV_F_MAPPORTS) && (i == 0)) {
1794 appctx->ctx.cli.msg = "can't unset 'port' since MAPPORTS is in use.\n";
1795 appctx->st0 = STAT_CLI_PRINT;
1796 return 1;
1797 }
1798 sv->check.port = i;
1799 appctx->ctx.cli.msg = "health check port updated.\n";
1800 appctx->st0 = STAT_CLI_PRINT;
1801 }
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001802 else if (strcmp(args[3], "addr") == 0) {
Baptiste Assmann3749ebf2016-08-03 22:34:12 +02001803 char *addr = NULL;
1804 char *port = NULL;
1805 if (strlen(args[4]) == 0) {
1806 appctx->ctx.cli.msg = "set server <b>/<s> requires an <addr> .\n";
1807 appctx->st0 = STAT_CLI_PRINT;
1808 return 1;
1809 }
1810 else {
1811 addr = args[4];
1812 }
1813 if (strcmp(args[5], "port") == 0) {
1814 port = args[6];
1815 }
1816 warning = update_server_addr_port(sv, addr, port, "stats socket command");
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001817 if (warning) {
1818 appctx->ctx.cli.msg = warning;
1819 appctx->st0 = STAT_CLI_PRINT;
1820 }
1821 }
Willy Tarreau2a4b70f2014-05-22 18:42:35 +02001822 else {
Baptiste Assmann50946562016-08-31 23:26:29 +02001823 appctx->ctx.cli.msg = "'set server <srv>' only supports 'agent', 'health', 'state', 'weight', 'addr' and 'check-port'.\n";
Willy Tarreau2a4b70f2014-05-22 18:42:35 +02001824 appctx->st0 = STAT_CLI_PRINT;
1825 }
1826 return 1;
1827 }
Willy Tarreau7aabd112010-01-26 10:59:06 +01001828 else if (strcmp(args[1], "timeout") == 0) {
1829 if (strcmp(args[2], "cli") == 0) {
1830 unsigned timeout;
1831 const char *res;
1832
1833 if (!*args[3]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001834 appctx->ctx.cli.msg = "Expects an integer value.\n";
1835 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001836 return 1;
1837 }
1838
1839 res = parse_time_err(args[3], &timeout, TIME_UNIT_S);
1840 if (res || timeout < 1) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001841 appctx->ctx.cli.msg = "Invalid timeout value.\n";
1842 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001843 return 1;
1844 }
1845
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001846 s->req.rto = s->res.wto = 1 + MS_TO_TICKS(timeout*1000);
Willy Tarreau7aabd112010-01-26 10:59:06 +01001847 return 1;
1848 }
1849 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001850 appctx->ctx.cli.msg = "'set timeout' only supports 'cli'.\n";
1851 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001852 return 1;
1853 }
1854 }
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001855 else if (strcmp(args[1], "maxconn") == 0) {
1856 if (strcmp(args[2], "frontend") == 0) {
1857 struct proxy *px;
1858 struct listener *l;
1859 int v;
1860
Willy Tarreau532a4502011-09-07 22:37:44 +02001861 px = expect_frontend_admin(s, si, args[3]);
1862 if (!px)
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001863 return 1;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001864
1865 if (!*args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001866 appctx->ctx.cli.msg = "Integer value expected.\n";
1867 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001868 return 1;
1869 }
1870
1871 v = atoi(args[4]);
Willy Tarreau3c7a79d2012-09-26 21:07:15 +02001872 if (v < 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001873 appctx->ctx.cli.msg = "Value out of range.\n";
1874 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001875 return 1;
1876 }
1877
1878 /* OK, the value is fine, so we assign it to the proxy and to all of
1879 * its listeners. The blocked ones will be dequeued.
1880 */
1881 px->maxconn = v;
Willy Tarreaua7944ad2012-09-26 21:03:11 +02001882 list_for_each_entry(l, &px->conf.listeners, by_fe) {
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001883 l->maxconn = v;
1884 if (l->state == LI_FULL)
1885 resume_listener(l);
1886 }
1887
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001888 if (px->maxconn > px->feconn && !LIST_ISEMPTY(&strm_fe(s)->listener_queue))
1889 dequeue_all_listeners(&strm_fe(s)->listener_queue);
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001890
1891 return 1;
1892 }
Andrew Hayworthedb93a72015-10-27 21:46:25 +00001893 else if (strcmp(args[2], "server") == 0) {
1894 struct server *sv;
Nenad Merdanovic5c3ed342016-04-24 23:10:07 +02001895 const char *warning;
Andrew Hayworthedb93a72015-10-27 21:46:25 +00001896
1897 sv = expect_server_admin(s, si, args[3]);
1898 if (!sv)
1899 return 1;
1900
Nenad Merdanovic5c3ed342016-04-24 23:10:07 +02001901 warning = server_parse_maxconn_change_request(sv, args[4]);
1902 if (warning) {
1903 appctx->ctx.cli.msg = warning;
Andrew Hayworthedb93a72015-10-27 21:46:25 +00001904 appctx->st0 = STAT_CLI_PRINT;
Andrew Hayworthedb93a72015-10-27 21:46:25 +00001905 }
1906
Andrew Hayworthedb93a72015-10-27 21:46:25 +00001907 return 1;
1908 }
Willy Tarreau91886b62011-09-07 14:38:31 +02001909 else if (strcmp(args[2], "global") == 0) {
1910 int v;
1911
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001912 if (strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001913 appctx->ctx.cli.msg = stats_permission_denied_msg;
1914 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau91886b62011-09-07 14:38:31 +02001915 return 1;
1916 }
1917
1918 if (!*args[3]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001919 appctx->ctx.cli.msg = "Expects an integer value.\n";
1920 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau91886b62011-09-07 14:38:31 +02001921 return 1;
1922 }
1923
1924 v = atoi(args[3]);
1925 if (v > global.hardmaxconn) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001926 appctx->ctx.cli.msg = "Value out of range.\n";
1927 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau91886b62011-09-07 14:38:31 +02001928 return 1;
1929 }
1930
1931 /* check for unlimited values */
1932 if (v <= 0)
1933 v = global.hardmaxconn;
1934
1935 global.maxconn = v;
1936
1937 /* Dequeues all of the listeners waiting for a resource */
1938 if (!LIST_ISEMPTY(&global_listener_queue))
1939 dequeue_all_listeners(&global_listener_queue);
1940
1941 return 1;
1942 }
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001943 else {
Andrew Hayworthedb93a72015-10-27 21:46:25 +00001944 appctx->ctx.cli.msg = "'set maxconn' only supports 'frontend', 'server', and 'global'.\n";
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001945 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001946 return 1;
1947 }
1948 }
Willy Tarreauf5b22872011-09-07 16:13:44 +02001949 else if (strcmp(args[1], "rate-limit") == 0) {
1950 if (strcmp(args[2], "connections") == 0) {
1951 if (strcmp(args[3], "global") == 0) {
1952 int v;
1953
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001954 if (strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001955 appctx->ctx.cli.msg = stats_permission_denied_msg;
1956 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001957 return 1;
1958 }
1959
1960 if (!*args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001961 appctx->ctx.cli.msg = "Expects an integer value.\n";
1962 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001963 return 1;
1964 }
1965
1966 v = atoi(args[4]);
1967 if (v < 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001968 appctx->ctx.cli.msg = "Value out of range.\n";
1969 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001970 return 1;
1971 }
1972
1973 global.cps_lim = v;
1974
1975 /* Dequeues all of the listeners waiting for a resource */
1976 if (!LIST_ISEMPTY(&global_listener_queue))
1977 dequeue_all_listeners(&global_listener_queue);
1978
1979 return 1;
1980 }
1981 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001982 appctx->ctx.cli.msg = "'set rate-limit connections' only supports 'global'.\n";
1983 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001984 return 1;
1985 }
1986 }
Willy Tarreau93e7c002013-10-07 18:51:07 +02001987 else if (strcmp(args[2], "sessions") == 0) {
1988 if (strcmp(args[3], "global") == 0) {
1989 int v;
1990
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001991 if (strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau93e7c002013-10-07 18:51:07 +02001992 appctx->ctx.cli.msg = stats_permission_denied_msg;
1993 appctx->st0 = STAT_CLI_PRINT;
1994 return 1;
1995 }
1996
1997 if (!*args[4]) {
1998 appctx->ctx.cli.msg = "Expects an integer value.\n";
1999 appctx->st0 = STAT_CLI_PRINT;
2000 return 1;
2001 }
2002
2003 v = atoi(args[4]);
2004 if (v < 0) {
2005 appctx->ctx.cli.msg = "Value out of range.\n";
2006 appctx->st0 = STAT_CLI_PRINT;
2007 return 1;
2008 }
2009
2010 global.sps_lim = v;
2011
2012 /* Dequeues all of the listeners waiting for a resource */
2013 if (!LIST_ISEMPTY(&global_listener_queue))
2014 dequeue_all_listeners(&global_listener_queue);
2015
2016 return 1;
2017 }
2018 else {
2019 appctx->ctx.cli.msg = "'set rate-limit sessions' only supports 'global'.\n";
2020 appctx->st0 = STAT_CLI_PRINT;
2021 return 1;
2022 }
2023 }
Willy Tarreaue43d5322013-10-07 20:01:52 +02002024#ifdef USE_OPENSSL
2025 else if (strcmp(args[2], "ssl-sessions") == 0) {
2026 if (strcmp(args[3], "global") == 0) {
2027 int v;
2028
Willy Tarreaud0d8da92015-04-04 02:10:38 +02002029 if (strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreaue43d5322013-10-07 20:01:52 +02002030 appctx->ctx.cli.msg = stats_permission_denied_msg;
2031 appctx->st0 = STAT_CLI_PRINT;
2032 return 1;
2033 }
2034
2035 if (!*args[4]) {
2036 appctx->ctx.cli.msg = "Expects an integer value.\n";
2037 appctx->st0 = STAT_CLI_PRINT;
2038 return 1;
2039 }
2040
2041 v = atoi(args[4]);
2042 if (v < 0) {
2043 appctx->ctx.cli.msg = "Value out of range.\n";
2044 appctx->st0 = STAT_CLI_PRINT;
2045 return 1;
2046 }
2047
2048 global.ssl_lim = v;
2049
2050 /* Dequeues all of the listeners waiting for a resource */
2051 if (!LIST_ISEMPTY(&global_listener_queue))
2052 dequeue_all_listeners(&global_listener_queue);
2053
2054 return 1;
2055 }
2056 else {
2057 appctx->ctx.cli.msg = "'set rate-limit ssl-sessions' only supports 'global'.\n";
2058 appctx->st0 = STAT_CLI_PRINT;
2059 return 1;
2060 }
2061 }
2062#endif
William Lallemandd85f9172012-11-09 17:05:39 +01002063 else if (strcmp(args[2], "http-compression") == 0) {
2064 if (strcmp(args[3], "global") == 0) {
2065 int v;
2066
Willy Tarreaua1c2b2c2015-11-26 18:32:39 +01002067 if (strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN) {
2068 appctx->ctx.cli.msg = stats_permission_denied_msg;
2069 appctx->st0 = STAT_CLI_PRINT;
2070 return 1;
2071 }
2072
Willy Tarreau85d47f92012-11-21 00:29:50 +01002073 if (!*args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002074 appctx->ctx.cli.msg = "Expects a maximum input byte rate in kB/s.\n";
2075 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau85d47f92012-11-21 00:29:50 +01002076 return 1;
2077 }
2078
William Lallemandd85f9172012-11-09 17:05:39 +01002079 v = atoi(args[4]);
2080 global.comp_rate_lim = v * 1024; /* Kilo to bytes. */
2081 }
2082 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002083 appctx->ctx.cli.msg = "'set rate-limit http-compression' only supports 'global'.\n";
2084 appctx->st0 = STAT_CLI_PRINT;
William Lallemandd85f9172012-11-09 17:05:39 +01002085 return 1;
2086 }
2087 }
Willy Tarreauf5b22872011-09-07 16:13:44 +02002088 else {
Willy Tarreaue43d5322013-10-07 20:01:52 +02002089 appctx->ctx.cli.msg = "'set rate-limit' supports 'connections', 'sessions', 'ssl-sessions', and 'http-compression'.\n";
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002090 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02002091 return 1;
2092 }
2093 }
Willy Tarreau654694e2012-06-07 01:03:16 +02002094 else if (strcmp(args[1], "table") == 0) {
2095 stats_sock_table_request(si, args, STAT_CLI_O_SET);
2096 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002097 else if (strcmp(args[1], "map") == 0) {
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002098 char *err;
2099
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002100 /* Set flags. */
2101 appctx->ctx.map.display_flags = PAT_REF_MAP;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002102
2103 /* Expect three parameters: map name, key and new value. */
2104 if (!*args[2] || !*args[3] || !*args[4]) {
Thierry FOURNIERd5723432014-03-11 13:52:44 +01002105 appctx->ctx.cli.msg = "'set map' expects three parameters: map identifier, key and value.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002106 appctx->st0 = STAT_CLI_PRINT;
2107 return 1;
2108 }
2109
2110 /* Lookup the reference in the maps. */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002111 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002112 if (!appctx->ctx.map.ref) {
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01002113 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002114 appctx->st0 = STAT_CLI_PRINT;
2115 return 1;
2116 }
2117
Thierry FOURNIER9356c682014-01-28 15:55:37 +01002118 /* If the entry identifier start with a '#', it is considered as
2119 * pointer id
2120 */
2121 if (args[3][0] == '#' && args[3][1] == '0' && args[3][2] == 'x') {
2122 struct pat_ref_elt *ref;
2123 long long int conv;
2124 char *error;
2125
2126 /* Convert argument to integer value. */
2127 conv = strtoll(&args[3][1], &error, 16);
2128 if (*error != '\0') {
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01002129 appctx->ctx.cli.msg = "Malformed identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER9356c682014-01-28 15:55:37 +01002130 appctx->st0 = STAT_CLI_PRINT;
2131 return 1;
2132 }
2133
2134 /* Convert and check integer to pointer. */
2135 ref = (struct pat_ref_elt *)(long)conv;
2136 if ((long long int)(long)ref != conv) {
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01002137 appctx->ctx.cli.msg = "Malformed identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER9356c682014-01-28 15:55:37 +01002138 appctx->st0 = STAT_CLI_PRINT;
2139 return 1;
2140 }
2141
2142 /* Try to delete the entry. */
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002143 err = NULL;
2144 if (!pat_ref_set_by_id(appctx->ctx.map.ref, ref, args[4], &err)) {
2145 if (err)
2146 memprintf(&err, "%s.\n", err);
2147 appctx->ctx.cli.err = err;
2148 appctx->st0 = STAT_CLI_PRINT_FREE;
Thierry FOURNIER9356c682014-01-28 15:55:37 +01002149 return 1;
2150 }
2151 }
2152 else {
2153 /* Else, use the entry identifier as pattern
2154 * string, and update the value.
2155 */
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002156 err = NULL;
2157 if (!pat_ref_set(appctx->ctx.map.ref, args[3], args[4], &err)) {
2158 if (err)
2159 memprintf(&err, "%s.\n", err);
2160 appctx->ctx.cli.err = err;
2161 appctx->st0 = STAT_CLI_PRINT_FREE;
Thierry FOURNIER9356c682014-01-28 15:55:37 +01002162 return 1;
2163 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002164 }
2165
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002166 /* The set is done, send message. */
Thierry FOURNIER07e78c52014-12-18 15:28:01 +01002167 appctx->st0 = STAT_CLI_PROMPT;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002168 return 1;
2169 }
Emeric Brun4147b2e2014-06-16 18:36:30 +02002170#ifdef USE_OPENSSL
2171 else if (strcmp(args[1], "ssl") == 0) {
2172 if (strcmp(args[2], "ocsp-response") == 0) {
Lukas Tribuse4e30f72014-12-09 16:32:51 +01002173#if (defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP)
Emeric Brun4147b2e2014-06-16 18:36:30 +02002174 char *err = NULL;
2175
Emeric Brunaf4ef742014-06-19 14:10:45 +02002176 /* Expect one parameter: the new response in base64 encoding */
Emeric Brun4147b2e2014-06-16 18:36:30 +02002177 if (!*args[3]) {
2178 appctx->ctx.cli.msg = "'set ssl ocsp-response' expects response in base64 encoding.\n";
2179 appctx->st0 = STAT_CLI_PRINT;
2180 return 1;
2181 }
2182
2183 trash.len = base64dec(args[3], strlen(args[3]), trash.str, trash.size);
2184 if (trash.len < 0) {
2185 appctx->ctx.cli.msg = "'set ssl ocsp-response' received invalid base64 encoded response.\n";
2186 appctx->st0 = STAT_CLI_PRINT;
2187 return 1;
2188 }
2189
2190 if (ssl_sock_update_ocsp_response(&trash, &err)) {
2191 if (err) {
2192 memprintf(&err, "%s.\n", err);
2193 appctx->ctx.cli.err = err;
2194 appctx->st0 = STAT_CLI_PRINT_FREE;
2195 }
2196 return 1;
2197 }
2198 appctx->ctx.cli.msg = "OCSP Response updated!";
2199 appctx->st0 = STAT_CLI_PRINT;
2200 return 1;
2201#else
2202 appctx->ctx.cli.msg = "HAProxy was compiled against a version of OpenSSL that doesn't support OCSP stapling.\n";
2203 appctx->st0 = STAT_CLI_PRINT;
2204 return 1;
2205#endif
2206 }
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02002207 else if (strcmp(args[2], "tls-key") == 0) {
2208#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
2209 /* Expect two parameters: the filename and the new new TLS key in encoding */
2210 if (!*args[3] || !*args[4]) {
2211 appctx->ctx.cli.msg = "'set ssl tls-key' expects a filename and the new TLS key in base64 encoding.\n";
2212 appctx->st0 = STAT_CLI_PRINT;
2213 return 1;
2214 }
2215
2216 appctx->ctx.tlskeys.ref = tlskeys_ref_lookup_ref(args[3]);
2217 if(!appctx->ctx.tlskeys.ref) {
2218 appctx->ctx.cli.msg = "'set ssl tls-key' unable to locate referenced filename\n";
2219 appctx->st0 = STAT_CLI_PRINT;
2220 return 1;
2221 }
2222
2223 trash.len = base64dec(args[4], strlen(args[4]), trash.str, trash.size);
2224 if (trash.len != sizeof(struct tls_sess_key)) {
2225 appctx->ctx.cli.msg = "'set ssl tls-key' received invalid base64 encoded TLS key.\n";
2226 appctx->st0 = STAT_CLI_PRINT;
2227 return 1;
2228 }
2229
Pradeep Jindalcc79b002015-08-20 18:25:17 +05302230 memcpy(appctx->ctx.tlskeys.ref->tlskeys + ((appctx->ctx.tlskeys.ref->tls_ticket_enc_index + 2) % TLS_TICKETS_NO), trash.str, trash.len);
2231 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 +02002232
2233 appctx->ctx.cli.msg = "TLS ticket key updated!";
2234 appctx->st0 = STAT_CLI_PRINT;
2235 return 1;
2236#else
2237 appctx->ctx.cli.msg = "HAProxy was compiled against a version of OpenSSL "
2238 "that doesn't support specifying TLS ticket keys\n";
2239 appctx->st0 = STAT_CLI_PRINT;
2240 return 1;
2241#endif
2242 }
Emeric Brun4147b2e2014-06-16 18:36:30 +02002243 else {
2244 appctx->ctx.cli.msg = "'set ssl' only supports 'ocsp-response'.\n";
2245 appctx->st0 = STAT_CLI_PRINT;
2246 return 1;
2247 }
2248 }
2249#endif
Willy Tarreau7aabd112010-01-26 10:59:06 +01002250 else { /* unknown "set" parameter */
Willy Tarreau4483d432009-10-10 19:30:08 +02002251 return 0;
2252 }
2253 }
Cyril Bontécd19e512010-01-31 22:34:03 +01002254 else if (strcmp(args[0], "enable") == 0) {
Simon Horman671b6f02013-11-25 10:46:39 +09002255 if (strcmp(args[1], "agent") == 0) {
2256 struct server *sv;
2257
2258 sv = expect_server_admin(s, si, args[2]);
2259 if (!sv)
2260 return 1;
2261
Willy Tarreau2e10f5a2013-12-11 20:11:55 +01002262 if (!(sv->agent.state & CHK_ST_CONFIGURED)) {
2263 appctx->ctx.cli.msg = "Agent was not configured on this server, cannot enable.\n";
2264 appctx->st0 = STAT_CLI_PRINT;
2265 return 1;
2266 }
2267
2268 sv->agent.state |= CHK_ST_ENABLED;
Simon Horman671b6f02013-11-25 10:46:39 +09002269 return 1;
2270 }
Willy Tarreau9b5aecd2014-05-23 11:53:10 +02002271 else if (strcmp(args[1], "health") == 0) {
Cyril Bontécd19e512010-01-31 22:34:03 +01002272 struct server *sv;
2273
Willy Tarreaud52c41e2011-09-07 23:41:01 +02002274 sv = expect_server_admin(s, si, args[2]);
2275 if (!sv)
Cyril Bonté613f0df2011-03-03 20:49:04 +01002276 return 1;
Cyril Bonté613f0df2011-03-03 20:49:04 +01002277
Willy Tarreau9b5aecd2014-05-23 11:53:10 +02002278 if (!(sv->check.state & CHK_ST_CONFIGURED)) {
2279 appctx->ctx.cli.msg = "Health checks are not configured on this server, cannot enable.\n";
2280 appctx->st0 = STAT_CLI_PRINT;
2281 return 1;
2282 }
2283
2284 sv->check.state |= CHK_ST_ENABLED;
2285 return 1;
2286 }
2287 else if (strcmp(args[1], "server") == 0) {
2288 struct server *sv;
2289
2290 sv = expect_server_admin(s, si, args[2]);
2291 if (!sv)
2292 return 1;
2293
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02002294 srv_adm_set_ready(sv);
Willy Tarreau532a4502011-09-07 22:37:44 +02002295 return 1;
2296 }
2297 else if (strcmp(args[1], "frontend") == 0) {
2298 struct proxy *px;
2299
2300 px = expect_frontend_admin(s, si, args[2]);
2301 if (!px)
2302 return 1;
2303
2304 if (px->state == PR_STSTOPPED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002305 appctx->ctx.cli.msg = "Frontend was previously shut down, cannot enable.\n";
2306 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002307 return 1;
2308 }
2309
2310 if (px->state != PR_STPAUSED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002311 appctx->ctx.cli.msg = "Frontend is already enabled.\n";
2312 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002313 return 1;
2314 }
2315
2316 if (!resume_proxy(px)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002317 appctx->ctx.cli.msg = "Failed to resume frontend, check logs for precise cause (port conflict?).\n";
2318 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002319 return 1;
2320 }
Cyril Bontécd19e512010-01-31 22:34:03 +01002321 return 1;
2322 }
2323 else { /* unknown "enable" parameter */
Willy Tarreau9b5aecd2014-05-23 11:53:10 +02002324 appctx->ctx.cli.msg = "'enable' only supports 'agent', 'frontend', 'health', and 'server'.\n";
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002325 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002326 return 1;
Cyril Bontécd19e512010-01-31 22:34:03 +01002327 }
2328 }
2329 else if (strcmp(args[0], "disable") == 0) {
Simon Horman671b6f02013-11-25 10:46:39 +09002330 if (strcmp(args[1], "agent") == 0) {
2331 struct server *sv;
2332
2333 sv = expect_server_admin(s, si, args[2]);
2334 if (!sv)
2335 return 1;
2336
Willy Tarreau2e10f5a2013-12-11 20:11:55 +01002337 sv->agent.state &= ~CHK_ST_ENABLED;
Simon Horman671b6f02013-11-25 10:46:39 +09002338 return 1;
2339 }
Willy Tarreau9b5aecd2014-05-23 11:53:10 +02002340 else if (strcmp(args[1], "health") == 0) {
2341 struct server *sv;
2342
2343 sv = expect_server_admin(s, si, args[2]);
2344 if (!sv)
2345 return 1;
2346
2347 sv->check.state &= ~CHK_ST_ENABLED;
2348 return 1;
2349 }
Simon Horman671b6f02013-11-25 10:46:39 +09002350 else if (strcmp(args[1], "server") == 0) {
Cyril Bontécd19e512010-01-31 22:34:03 +01002351 struct server *sv;
2352
Willy Tarreaud52c41e2011-09-07 23:41:01 +02002353 sv = expect_server_admin(s, si, args[2]);
2354 if (!sv)
Cyril Bonté613f0df2011-03-03 20:49:04 +01002355 return 1;
Cyril Bonté613f0df2011-03-03 20:49:04 +01002356
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02002357 srv_adm_set_maint(sv);
Willy Tarreau532a4502011-09-07 22:37:44 +02002358 return 1;
2359 }
2360 else 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 previously shut down, cannot disable.\n";
2369 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002370 return 1;
2371 }
2372
2373 if (px->state == PR_STPAUSED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002374 appctx->ctx.cli.msg = "Frontend is already disabled.\n";
2375 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002376 return 1;
2377 }
2378
2379 if (!pause_proxy(px)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002380 appctx->ctx.cli.msg = "Failed to pause frontend, check logs for precise cause.\n";
2381 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002382 return 1;
2383 }
Cyril Bontécd19e512010-01-31 22:34:03 +01002384 return 1;
2385 }
2386 else { /* unknown "disable" parameter */
Willy Tarreau9b5aecd2014-05-23 11:53:10 +02002387 appctx->ctx.cli.msg = "'disable' only supports 'agent', 'frontend', 'health', and 'server'.\n";
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002388 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002389 return 1;
2390 }
2391 }
2392 else if (strcmp(args[0], "shutdown") == 0) {
2393 if (strcmp(args[1], "frontend") == 0) {
2394 struct proxy *px;
2395
2396 px = expect_frontend_admin(s, si, args[2]);
2397 if (!px)
2398 return 1;
2399
2400 if (px->state == PR_STSTOPPED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002401 appctx->ctx.cli.msg = "Frontend was already shut down.\n";
2402 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002403 return 1;
2404 }
2405
2406 Warning("Proxy %s stopped (FE: %lld conns, BE: %lld conns).\n",
2407 px->id, px->fe_counters.cum_conn, px->be_counters.cum_conn);
2408 send_log(px, LOG_WARNING, "Proxy %s stopped (FE: %lld conns, BE: %lld conns).\n",
2409 px->id, px->fe_counters.cum_conn, px->be_counters.cum_conn);
2410 stop_proxy(px);
2411 return 1;
2412 }
Willy Tarreaua295edc2011-09-07 23:21:03 +02002413 else if (strcmp(args[1], "session") == 0) {
Willy Tarreau87b09662015-04-03 00:22:06 +02002414 struct stream *sess, *ptr;
Willy Tarreaua295edc2011-09-07 23:21:03 +02002415
Willy Tarreaud0d8da92015-04-04 02:10:38 +02002416 if (strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002417 appctx->ctx.cli.msg = stats_permission_denied_msg;
2418 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaua295edc2011-09-07 23:21:03 +02002419 return 1;
2420 }
2421
2422 if (!*args[2]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002423 appctx->ctx.cli.msg = "Session pointer expected (use 'show sess').\n";
2424 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaua295edc2011-09-07 23:21:03 +02002425 return 1;
2426 }
2427
2428 ptr = (void *)strtoul(args[2], NULL, 0);
2429
Willy Tarreau87b09662015-04-03 00:22:06 +02002430 /* first, look for the requested stream in the stream table */
2431 list_for_each_entry(sess, &streams, list) {
Willy Tarreaua295edc2011-09-07 23:21:03 +02002432 if (sess == ptr)
2433 break;
2434 }
2435
Willy Tarreau87b09662015-04-03 00:22:06 +02002436 /* do we have the stream ? */
Willy Tarreaua295edc2011-09-07 23:21:03 +02002437 if (sess != ptr) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002438 appctx->ctx.cli.msg = "No such session (use 'show sess').\n";
2439 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaua295edc2011-09-07 23:21:03 +02002440 return 1;
2441 }
2442
Willy Tarreaue7dff022015-04-03 01:14:29 +02002443 stream_shutdown(sess, SF_ERR_KILLED);
Willy Tarreaua295edc2011-09-07 23:21:03 +02002444 return 1;
2445 }
Willy Tarreau52b2d222011-09-07 23:48:48 +02002446 else if (strcmp(args[1], "sessions") == 0) {
2447 if (strcmp(args[2], "server") == 0) {
2448 struct server *sv;
Willy Tarreau87b09662015-04-03 00:22:06 +02002449 struct stream *sess, *sess_bck;
Willy Tarreau52b2d222011-09-07 23:48:48 +02002450
2451 sv = expect_server_admin(s, si, args[3]);
2452 if (!sv)
2453 return 1;
2454
Willy Tarreau87b09662015-04-03 00:22:06 +02002455 /* kill all the stream that are on this server */
Willy Tarreau52b2d222011-09-07 23:48:48 +02002456 list_for_each_entry_safe(sess, sess_bck, &sv->actconns, by_srv)
2457 if (sess->srv_conn == sv)
Willy Tarreaue7dff022015-04-03 01:14:29 +02002458 stream_shutdown(sess, SF_ERR_KILLED);
Willy Tarreau52b2d222011-09-07 23:48:48 +02002459
2460 return 1;
2461 }
2462 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002463 appctx->ctx.cli.msg = "'shutdown sessions' only supports 'server'.\n";
2464 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau52b2d222011-09-07 23:48:48 +02002465 return 1;
2466 }
2467 }
Willy Tarreau532a4502011-09-07 22:37:44 +02002468 else { /* unknown "disable" parameter */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002469 appctx->ctx.cli.msg = "'shutdown' only supports 'frontend', 'session' and 'sessions'.\n";
2470 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002471 return 1;
Cyril Bontécd19e512010-01-31 22:34:03 +01002472 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002473 }
2474 else if (strcmp(args[0], "del") == 0) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002475 if (strcmp(args[1], "map") == 0 || strcmp(args[1], "acl") == 0) {
2476 if (args[1][0] == 'm')
2477 appctx->ctx.map.display_flags = PAT_REF_MAP;
2478 else
2479 appctx->ctx.map.display_flags = PAT_REF_ACL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002480
2481 /* Expect two parameters: map name and key. */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002482 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
2483 if (!*args[2] || !*args[3]) {
2484 appctx->ctx.cli.msg = "This command expects two parameters: map identifier and key.\n";
2485 appctx->st0 = STAT_CLI_PRINT;
2486 return 1;
2487 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002488 }
2489
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002490 else {
2491 if (!*args[2] || !*args[3]) {
2492 appctx->ctx.cli.msg = "This command expects two parameters: ACL identifier and key.\n";
2493 appctx->st0 = STAT_CLI_PRINT;
2494 return 1;
2495 }
2496 }
2497
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002498 /* Lookup the reference in the maps. */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002499 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002500 if (!appctx->ctx.map.ref ||
2501 !(appctx->ctx.map.ref->flags & appctx->ctx.map.display_flags)) {
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01002502 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002503 appctx->st0 = STAT_CLI_PRINT;
2504 return 1;
2505 }
2506
Thierry FOURNIER9356c682014-01-28 15:55:37 +01002507 /* If the entry identifier start with a '#', it is considered as
2508 * pointer id
2509 */
2510 if (args[3][0] == '#' && args[3][1] == '0' && args[3][2] == 'x') {
2511 struct pat_ref_elt *ref;
2512 long long int conv;
2513 char *error;
2514
2515 /* Convert argument to integer value. */
2516 conv = strtoll(&args[3][1], &error, 16);
2517 if (*error != '\0') {
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01002518 appctx->ctx.cli.msg = "Malformed identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER9356c682014-01-28 15:55:37 +01002519 appctx->st0 = STAT_CLI_PRINT;
2520 return 1;
2521 }
2522
2523 /* Convert and check integer to pointer. */
2524 ref = (struct pat_ref_elt *)(long)conv;
2525 if ((long long int)(long)ref != conv) {
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01002526 appctx->ctx.cli.msg = "Malformed identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER9356c682014-01-28 15:55:37 +01002527 appctx->st0 = STAT_CLI_PRINT;
2528 return 1;
2529 }
2530
2531 /* Try to delete the entry. */
2532 if (!pat_ref_delete_by_id(appctx->ctx.map.ref, ref)) {
2533 /* The entry is not found, send message. */
2534 appctx->ctx.cli.msg = "Key not found.\n";
2535 appctx->st0 = STAT_CLI_PRINT;
2536 return 1;
2537 }
2538 }
2539 else {
2540 /* Else, use the entry identifier as pattern
2541 * string and try to delete the entry.
2542 */
2543 if (!pat_ref_delete(appctx->ctx.map.ref, args[3])) {
2544 /* The entry is not found, send message. */
2545 appctx->ctx.cli.msg = "Key not found.\n";
2546 appctx->st0 = STAT_CLI_PRINT;
2547 return 1;
2548 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002549 }
2550
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002551 /* The deletion is done, send message. */
Thierry FOURNIER07e78c52014-12-18 15:28:01 +01002552 appctx->st0 = STAT_CLI_PROMPT;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002553 return 1;
2554 }
2555 else { /* unknown "del" parameter */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002556 appctx->ctx.cli.msg = "'del' only supports 'map' or 'acl'.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002557 appctx->st0 = STAT_CLI_PRINT;
2558 return 1;
2559 }
2560 }
2561 else if (strcmp(args[0], "add") == 0) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002562 if (strcmp(args[1], "map") == 0 ||
2563 strcmp(args[1], "acl") == 0) {
2564 int ret;
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002565 char *err;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002566
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002567 /* Set flags. */
2568 if (args[1][0] == 'm')
2569 appctx->ctx.map.display_flags = PAT_REF_MAP;
2570 else
2571 appctx->ctx.map.display_flags = PAT_REF_ACL;
2572
2573 /* If the keywork is "map", we expect three parameters, if it
2574 * is "acl", we expect only two parameters
2575 */
2576 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
2577 if (!*args[2] || !*args[3] || !*args[4]) {
2578 appctx->ctx.cli.msg = "'add map' expects three parameters: map identifier, key and value.\n";
2579 appctx->st0 = STAT_CLI_PRINT;
2580 return 1;
2581 }
2582 }
2583 else {
2584 if (!*args[2] || !*args[3]) {
2585 appctx->ctx.cli.msg = "'add acl' expects two parameters: ACL identifier and pattern.\n";
2586 appctx->st0 = STAT_CLI_PRINT;
2587 return 1;
2588 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002589 }
2590
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002591 /* Lookup for the reference. */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002592 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002593 if (!appctx->ctx.map.ref) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002594 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01002595 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002596 else
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01002597 appctx->ctx.cli.msg = "Unknown ACL identifier. Please use #<id> or <file>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002598 appctx->st0 = STAT_CLI_PRINT;
2599 return 1;
2600 }
2601
Thierry FOURNIER64c585f2014-01-29 20:02:36 +01002602 /* The command "add acl" is prohibited if the reference
2603 * use samples.
2604 */
2605 if ((appctx->ctx.map.display_flags & PAT_REF_ACL) &&
2606 (appctx->ctx.map.ref->flags & PAT_REF_SMP)) {
2607 appctx->ctx.cli.msg = "This ACL is shared with a map containing samples. "
2608 "You must use the command 'add map' to add values.\n";
2609 appctx->st0 = STAT_CLI_PRINT;
2610 return 1;
2611 }
2612
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002613 /* Add value. */
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002614 err = NULL;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002615 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02002616 ret = pat_ref_add(appctx->ctx.map.ref, args[3], args[4], &err);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002617 else
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02002618 ret = pat_ref_add(appctx->ctx.map.ref, args[3], NULL, &err);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002619 if (!ret) {
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002620 if (err)
2621 memprintf(&err, "%s.\n", err);
2622 appctx->ctx.cli.err = err;
2623 appctx->st0 = STAT_CLI_PRINT_FREE;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002624 return 1;
2625 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002626
2627 /* The add is done, send message. */
Thierry FOURNIER07e78c52014-12-18 15:28:01 +01002628 appctx->st0 = STAT_CLI_PROMPT;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002629 return 1;
2630 }
2631 else { /* unknown "del" parameter */
2632 appctx->ctx.cli.msg = "'add' only supports 'map'.\n";
2633 appctx->st0 = STAT_CLI_PRINT;
2634 return 1;
2635 }
Cyril Bontécd19e512010-01-31 22:34:03 +01002636 }
2637 else { /* not "show" nor "clear" nor "get" nor "set" nor "enable" nor "disable" */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002638 return 0;
2639 }
2640 return 1;
2641}
2642
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002643/* This I/O handler runs as an applet embedded in a stream interface. It is
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002644 * used to processes I/O from/to the stats unix socket. The system relies on a
2645 * state machine handling requests and various responses. We read a request,
2646 * then we process it and send the response, and we possibly display a prompt.
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002647 * Then we can read again. The state is stored in appctx->st0 and is one of the
2648 * STAT_CLI_* constants. appctx->st1 is used to indicate whether prompt is enabled
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002649 * or not.
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002650 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02002651static void cli_io_handler(struct appctx *appctx)
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002652{
Willy Tarreau00a37f02015-04-13 12:05:19 +02002653 struct stream_interface *si = appctx->owner;
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002654 struct channel *req = si_oc(si);
2655 struct channel *res = si_ic(si);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002656 int reql;
2657 int len;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002658
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002659 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
2660 goto out;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002661
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002662 while (1) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002663 if (appctx->st0 == STAT_CLI_INIT) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002664 /* Stats output not initialized yet */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002665 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
2666 appctx->st0 = STAT_CLI_GETREQ;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002667 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002668 else if (appctx->st0 == STAT_CLI_END) {
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002669 /* Let's close for real now. We just close the request
2670 * side, the conditions below will complete if needed.
2671 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02002672 si_shutw(si);
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002673 break;
2674 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002675 else if (appctx->st0 == STAT_CLI_GETREQ) {
Willy Tarreau4e33d862009-10-11 23:35:10 +02002676 /* ensure we have some output room left in the event we
2677 * would want to return some info right after parsing.
2678 */
Willy Tarreau4e4292b2014-11-28 12:18:45 +01002679 if (buffer_almost_full(si_ib(si))) {
Willy Tarreaufe127932015-04-21 19:23:39 +02002680 si_applet_cant_put(si);
Willy Tarreau4e33d862009-10-11 23:35:10 +02002681 break;
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01002682 }
Willy Tarreau4e33d862009-10-11 23:35:10 +02002683
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002684 reql = bo_getline(si_oc(si), trash.str, trash.size);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002685 if (reql <= 0) { /* closed or EOL not found */
2686 if (reql == 0)
2687 break;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002688 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002689 continue;
2690 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002691
Chad Lavoiee3f50312016-05-26 16:42:25 -04002692 /* seek for a possible unescaped semi-colon. If we find
2693 * one, we replace it with an LF and skip only this part.
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002694 */
Chad Lavoiee3f50312016-05-26 16:42:25 -04002695 for (len = 0; len < reql; len++) {
2696 if (trash.str[len] == '\\') {
2697 len++;
2698 continue;
2699 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002700 if (trash.str[len] == ';') {
2701 trash.str[len] = '\n';
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002702 reql = len + 1;
2703 break;
2704 }
Chad Lavoiee3f50312016-05-26 16:42:25 -04002705 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002706
Willy Tarreau816fc222009-10-04 07:36:58 +02002707 /* now it is time to check that we have a full line,
2708 * remove the trailing \n and possibly \r, then cut the
2709 * line.
2710 */
2711 len = reql - 1;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002712 if (trash.str[len] != '\n') {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002713 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002714 continue;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002715 }
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002716
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002717 if (len && trash.str[len-1] == '\r')
Willy Tarreau816fc222009-10-04 07:36:58 +02002718 len--;
2719
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002720 trash.str[len] = '\0';
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002721
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002722 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002723 if (len) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002724 if (strcmp(trash.str, "quit") == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002725 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002726 continue;
2727 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002728 else if (strcmp(trash.str, "prompt") == 0)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002729 appctx->st1 = !appctx->st1;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002730 else if (strcmp(trash.str, "help") == 0 ||
2731 !stats_sock_parse_request(si, trash.str)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002732 appctx->ctx.cli.msg = stats_sock_usage_msg;
2733 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02002734 }
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002735 /* NB: stats_sock_parse_request() may have put
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002736 * another STAT_CLI_O_* into appctx->st0.
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002737 */
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002738 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002739 else if (!appctx->st1) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002740 /* if prompt is disabled, print help on empty lines,
2741 * so that the user at least knows how to enable
2742 * prompt and find help.
2743 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002744 appctx->ctx.cli.msg = stats_sock_usage_msg;
2745 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002746 }
2747
2748 /* re-adjust req buffer */
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002749 bo_skip(si_oc(si), reql);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002750 req->flags |= CF_READ_DONTWAIT; /* we plan to read small requests */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002751 }
Willy Tarreau68c00c72015-09-25 19:21:19 +02002752 else { /* output functions */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002753 switch (appctx->st0) {
Andrew Hayworthe32d1862015-10-02 15:08:10 +00002754 case STAT_CLI_PROMPT:
2755 break;
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02002756 case STAT_CLI_PRINT:
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002757 if (bi_putstr(si_ic(si), appctx->ctx.cli.msg) != -1)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002758 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreaubc18da12015-03-13 14:00:47 +01002759 else
Willy Tarreaufe127932015-04-21 19:23:39 +02002760 si_applet_cant_put(si);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002761 break;
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002762 case STAT_CLI_PRINT_FREE:
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002763 if (bi_putstr(si_ic(si), appctx->ctx.cli.err) != -1) {
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002764 free(appctx->ctx.cli.err);
2765 appctx->st0 = STAT_CLI_PROMPT;
2766 }
Willy Tarreaubc18da12015-03-13 14:00:47 +01002767 else
Willy Tarreaufe127932015-04-21 19:23:39 +02002768 si_applet_cant_put(si);
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002769 break;
Baptiste Assmann9b6857e2015-09-18 14:49:12 +02002770 case STAT_CLI_O_BACKEND:
2771 if (stats_dump_backend_to_buffer(si))
2772 appctx->st0 = STAT_CLI_PROMPT;
2773 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002774 case STAT_CLI_O_INFO:
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002775 if (stats_dump_info_to_buffer(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002776 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002777 break;
Baptiste Assmann28289462015-07-03 08:01:20 +02002778 case STAT_CLI_O_SERVERS_STATE:
2779 if (stats_dump_servers_state_to_buffer(si))
2780 appctx->st0 = STAT_CLI_PROMPT;
2781 break;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002782 case STAT_CLI_O_STAT:
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002783 if (stats_dump_stat_to_buffer(si, NULL))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002784 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002785 break;
Baptiste Assmann3863f972015-05-17 00:33:24 +02002786 case STAT_CLI_O_RESOLVERS:
2787 if (stats_dump_resolvers_to_buffer(si))
2788 appctx->st0 = STAT_CLI_PROMPT;
2789 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002790 case STAT_CLI_O_SESS:
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01002791 if (stats_dump_sess_to_buffer(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002792 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002793 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002794 case STAT_CLI_O_ERR: /* errors dump */
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01002795 if (stats_dump_errors_to_buffer(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002796 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002797 break;
Willy Tarreau69f58c82010-07-12 17:55:33 +02002798 case STAT_CLI_O_TAB:
Simon Hormanc88b8872011-06-15 15:18:49 +09002799 case STAT_CLI_O_CLR:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002800 if (stats_table_request(si, appctx->st0))
2801 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau69f58c82010-07-12 17:55:33 +02002802 break;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002803 case STAT_CLI_O_PATS:
2804 if (stats_pats_list(si))
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002805 appctx->st0 = STAT_CLI_PROMPT;
2806 break;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002807 case STAT_CLI_O_PAT:
2808 if (stats_pat_list(si))
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002809 appctx->st0 = STAT_CLI_PROMPT;
2810 break;
2811 case STAT_CLI_O_MLOOK:
2812 if (stats_map_lookup(si))
2813 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau4efb3532014-01-29 12:13:39 +01002814 break;
Willy Tarreau12833bb2014-01-28 16:49:56 +01002815 case STAT_CLI_O_POOLS:
2816 if (stats_dump_pools_to_buffer(si))
2817 appctx->st0 = STAT_CLI_PROMPT;
2818 break;
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02002819#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
2820 case STAT_CLI_O_TLSK:
2821 if (stats_tlskeys_list(si))
2822 appctx->st0 = STAT_CLI_PROMPT;
2823 break;
William Lallemand1d0b36a2016-05-20 17:40:26 +02002824 case STAT_CLI_O_TLSK_ENT:
2825 if (stats_tlskeys_list(si))
2826 appctx->st0 = STAT_CLI_PROMPT;
2827 break;
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02002828#endif
Willy Tarreauae795722016-02-16 11:27:28 +01002829 case STAT_CLI_O_ENV: /* environment dump */
2830 if (stats_dump_env_to_buffer(si))
2831 appctx->st0 = STAT_CLI_PROMPT;
2832 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002833 default: /* abnormal state */
Willy Tarreau5cfa3bc2015-09-25 20:08:51 +02002834 si->flags |= SI_FL_ERR;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002835 break;
2836 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002837
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002838 /* The post-command prompt is either LF alone or LF + '> ' in interactive mode */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002839 if (appctx->st0 == STAT_CLI_PROMPT) {
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002840 if (bi_putstr(si_ic(si), appctx->st1 ? "\n> " : "\n") != -1)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002841 appctx->st0 = STAT_CLI_GETREQ;
Willy Tarreaubc18da12015-03-13 14:00:47 +01002842 else
Willy Tarreaufe127932015-04-21 19:23:39 +02002843 si_applet_cant_put(si);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002844 }
2845
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002846 /* If the output functions are still there, it means they require more room. */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002847 if (appctx->st0 >= STAT_CLI_OUTPUT)
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002848 break;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002849
2850 /* Now we close the output if one of the writers did so,
2851 * or if we're not in interactive mode and the request
2852 * buffer is empty. This still allows pipelined requests
2853 * to be sent in non-interactive mode.
2854 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002855 if ((res->flags & (CF_SHUTW|CF_SHUTW_NOW)) || (!appctx->st1 && !req->buf->o)) {
2856 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002857 continue;
2858 }
2859
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002860 /* switch state back to GETREQ to read next requests */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002861 appctx->st0 = STAT_CLI_GETREQ;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002862 }
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002863 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002864
Willy Tarreauc9e930a2015-09-25 20:06:08 +02002865 if ((res->flags & CF_SHUTR) && (si->state == SI_ST_EST)) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002866 DPRINTF(stderr, "%s@%d: si to buf closed. req=%08x, res=%08x, st=%d\n",
2867 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
Aman Guptaceafb4a2012-04-02 18:57:54 -07002868 /* Other side has closed, let's abort if we have no more processing to do
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002869 * and nothing more to consume. This is comparable to a broken pipe, so
2870 * we forward the close to the request side so that it flows upstream to
2871 * the client.
2872 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02002873 si_shutw(si);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002874 }
2875
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002876 if ((req->flags & CF_SHUTW) && (si->state == SI_ST_EST) && (appctx->st0 < STAT_CLI_OUTPUT)) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002877 DPRINTF(stderr, "%s@%d: buf to si closed. req=%08x, res=%08x, st=%d\n",
2878 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
2879 /* We have no more processing to do, and nothing more to send, and
2880 * the client side has closed. So we'll forward this state downstream
2881 * on the response buffer.
2882 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02002883 si_shutr(si);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002884 res->flags |= CF_READ_NULL;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002885 }
2886
Willy Tarreau828824a2015-04-19 17:20:03 +02002887 out:
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01002888 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 +02002889 __FUNCTION__, __LINE__,
Willy Tarreau9b28e032012-10-12 23:49:43 +02002890 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 +02002891}
2892
Willy Tarreau638d40a2016-02-24 23:11:01 +01002893/* Emits a stats field without any surrounding element and properly encoded to
2894 * resist CSV output. Returns non-zero on success, 0 if the buffer is full.
2895 */
Willy Tarreaub47785f2016-02-24 23:28:31 +01002896int stats_emit_raw_data_field(struct chunk *out, const struct field *f)
Willy Tarreau638d40a2016-02-24 23:11:01 +01002897{
2898 switch (field_format(f, 0)) {
2899 case FF_EMPTY: return 1;
2900 case FF_S32: return chunk_appendf(out, "%d", f->u.s32);
2901 case FF_U32: return chunk_appendf(out, "%u", f->u.u32);
2902 case FF_S64: return chunk_appendf(out, "%lld", (long long)f->u.s64);
2903 case FF_U64: return chunk_appendf(out, "%llu", (unsigned long long)f->u.u64);
2904 case FF_STR: return csv_enc_append(field_str(f, 0), 1, out) != NULL;
2905 default: return chunk_appendf(out, "[INCORRECT_FIELD_TYPE_%08x]", f->type);
2906 }
2907}
2908
Willy Tarreaub47785f2016-02-24 23:28:31 +01002909/* Emits a stats field prefixed with its type. No CSV encoding is prepared, the
2910 * output is supposed to be used on its own line. Returns non-zero on success, 0
2911 * if the buffer is full.
2912 */
2913int stats_emit_typed_data_field(struct chunk *out, const struct field *f)
2914{
2915 switch (field_format(f, 0)) {
2916 case FF_EMPTY: return 1;
2917 case FF_S32: return chunk_appendf(out, "s32:%d", f->u.s32);
2918 case FF_U32: return chunk_appendf(out, "u32:%u", f->u.u32);
2919 case FF_S64: return chunk_appendf(out, "s64:%lld", (long long)f->u.s64);
2920 case FF_U64: return chunk_appendf(out, "u64:%llu", (unsigned long long)f->u.u64);
2921 case FF_STR: return chunk_appendf(out, "str:%s", field_str(f, 0));
2922 default: return chunk_appendf(out, "%08x:?", f->type);
2923 }
2924}
2925
2926/* Emits an encoding of the field type on 3 characters followed by a delimiter.
2927 * Returns non-zero on success, 0 if the buffer is full.
2928 */
2929int stats_emit_field_tags(struct chunk *out, const struct field *f, char delim)
2930{
2931 char origin, nature, scope;
2932
2933 switch (field_origin(f, 0)) {
2934 case FO_METRIC: origin = 'M'; break;
2935 case FO_STATUS: origin = 'S'; break;
2936 case FO_KEY: origin = 'K'; break;
2937 case FO_CONFIG: origin = 'C'; break;
2938 case FO_PRODUCT: origin = 'P'; break;
2939 default: origin = '?'; break;
2940 }
2941
2942 switch (field_nature(f, 0)) {
2943 case FN_GAUGE: nature = 'G'; break;
2944 case FN_LIMIT: nature = 'L'; break;
2945 case FN_MIN: nature = 'm'; break;
2946 case FN_MAX: nature = 'M'; break;
2947 case FN_RATE: nature = 'R'; break;
2948 case FN_COUNTER: nature = 'C'; break;
2949 case FN_DURATION: nature = 'D'; break;
2950 case FN_AGE: nature = 'A'; break;
2951 case FN_TIME: nature = 'T'; break;
2952 case FN_NAME: nature = 'N'; break;
2953 case FN_OUTPUT: nature = 'O'; break;
2954 case FN_AVG: nature = 'a'; break;
2955 default: nature = '?'; break;
2956 }
2957
2958 switch (field_scope(f, 0)) {
2959 case FS_PROCESS: scope = 'P'; break;
2960 case FS_SERVICE: scope = 'S'; break;
2961 case FS_SYSTEM: scope = 's'; break;
2962 case FS_CLUSTER: scope = 'C'; break;
2963 default: scope = '?'; break;
2964 }
2965
2966 return chunk_appendf(out, "%c%c%c%c", origin, nature, scope, delim);
2967}
2968
Willy Tarreaubf95cba2016-01-11 18:27:29 +01002969/* Dump all fields from <info> into <out> using the "show info" format (name: value) */
2970static int stats_dump_info_fields(struct chunk *out, const struct field *info)
2971{
2972 int field;
2973
2974 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
2975 if (!field_format(info, field))
2976 continue;
2977
2978 if (!chunk_appendf(out, "%s: ", info_field_names[field]))
2979 return 0;
2980 if (!stats_emit_raw_data_field(out, &info[field]))
2981 return 0;
2982 if (!chunk_strcat(out, "\n"))
2983 return 0;
2984 }
2985 return 1;
2986}
2987
Willy Tarreaucb809122016-01-11 20:08:42 +01002988/* Dump all fields from <info> into <out> using the "show info typed" format */
2989static int stats_dump_typed_info_fields(struct chunk *out, const struct field *info)
2990{
2991 int field;
2992
2993 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
2994 if (!field_format(info, field))
2995 continue;
2996
2997 if (!chunk_appendf(out, "%d.%s.%u:", field, info_field_names[field], info[INF_PROCESS_NUM].u.u32))
2998 return 0;
2999 if (!stats_emit_field_tags(out, &info[field], ':'))
3000 return 0;
3001 if (!stats_emit_typed_data_field(out, &info[field]))
3002 return 0;
3003 if (!chunk_strcat(out, "\n"))
3004 return 0;
3005 }
3006 return 1;
3007}
3008
Thierry Fourniercb2c7672016-03-25 08:19:23 +01003009/* Fill <info> with HAProxy global info. <info> is preallocated
3010 * array of length <len>. The length of the aray must be
3011 * INF_TOTAL_FIELDS. If this length is less then this value, the
3012 * function returns 0, otherwise, it returns 1.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003013 */
Thierry Fourniercb2c7672016-03-25 08:19:23 +01003014int stats_fill_info(struct field *info, int len)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003015{
3016 unsigned int up = (now.tv_sec - start_date.tv_sec);
Willy Tarreau4529c072016-01-11 18:17:07 +01003017 struct chunk *out = get_trash_chunk();
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003018
Willy Tarreau0c9c2722014-05-28 12:28:58 +02003019#ifdef USE_OPENSSL
3020 int ssl_sess_rate = read_freq_ctr(&global.ssl_per_sec);
3021 int ssl_key_rate = read_freq_ctr(&global.ssl_fe_keys_per_sec);
3022 int ssl_reuse = 0;
3023
3024 if (ssl_key_rate < ssl_sess_rate) {
3025 /* count the ssl reuse ratio and avoid overflows in both directions */
3026 ssl_reuse = 100 - (100 * ssl_key_rate + (ssl_sess_rate - 1) / 2) / ssl_sess_rate;
3027 }
3028#endif
3029
Thierry Fourniercb2c7672016-03-25 08:19:23 +01003030 if (len < INF_TOTAL_FIELDS)
3031 return 0;
3032
Willy Tarreau4529c072016-01-11 18:17:07 +01003033 chunk_reset(out);
Thierry Fourniercb2c7672016-03-25 08:19:23 +01003034 memset(info, 0, sizeof(*info) * len);
Willy Tarreau4529c072016-01-11 18:17:07 +01003035
3036 info[INF_NAME] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, PRODUCT_NAME);
3037 info[INF_VERSION] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, HAPROXY_VERSION);
3038 info[INF_RELEASE_DATE] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, HAPROXY_DATE);
3039
3040 info[INF_NBPROC] = mkf_u32(FO_CONFIG|FS_SERVICE, global.nbproc);
3041 info[INF_PROCESS_NUM] = mkf_u32(FO_KEY, relative_pid);
3042 info[INF_PID] = mkf_u32(FO_STATUS, pid);
3043
3044 info[INF_UPTIME] = mkf_str(FN_DURATION, chunk_newstr(out));
3045 chunk_appendf(out, "%ud %uh%02um%02us", up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60));
3046
3047 info[INF_UPTIME_SEC] = mkf_u32(FN_DURATION, up);
3048 info[INF_MEMMAX_MB] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_memmax);
3049 info[INF_POOL_ALLOC_MB] = mkf_u32(0, (unsigned)(pool_total_allocated() / 1048576L));
3050 info[INF_POOL_USED_MB] = mkf_u32(0, (unsigned)(pool_total_used() / 1048576L));
3051 info[INF_POOL_FAILED] = mkf_u32(FN_COUNTER, pool_total_failures());
3052 info[INF_ULIMIT_N] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_nofile);
3053 info[INF_MAXSOCK] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxsock);
3054 info[INF_MAXCONN] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxconn);
3055 info[INF_HARD_MAXCONN] = mkf_u32(FO_CONFIG|FN_LIMIT, global.hardmaxconn);
3056 info[INF_CURR_CONN] = mkf_u32(0, actconn);
3057 info[INF_CUM_CONN] = mkf_u32(FN_COUNTER, totalconn);
3058 info[INF_CUM_REQ] = mkf_u32(FN_COUNTER, global.req_count);
3059#ifdef USE_OPENSSL
3060 info[INF_MAX_SSL_CONNS] = mkf_u32(FN_MAX, global.maxsslconn);
3061 info[INF_CURR_SSL_CONNS] = mkf_u32(0, sslconns);
3062 info[INF_CUM_SSL_CONNS] = mkf_u32(FN_COUNTER, totalsslconns);
3063#endif
3064 info[INF_MAXPIPES] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxpipes);
3065 info[INF_PIPES_USED] = mkf_u32(0, pipes_used);
3066 info[INF_PIPES_FREE] = mkf_u32(0, pipes_free);
3067 info[INF_CONN_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.conn_per_sec));
3068 info[INF_CONN_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.cps_lim);
3069 info[INF_MAX_CONN_RATE] = mkf_u32(FN_MAX, global.cps_max);
3070 info[INF_SESS_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.sess_per_sec));
3071 info[INF_SESS_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.sps_lim);
3072 info[INF_MAX_SESS_RATE] = mkf_u32(FN_RATE, global.sps_max);
3073
3074#ifdef USE_OPENSSL
3075 info[INF_SSL_RATE] = mkf_u32(FN_RATE, ssl_sess_rate);
3076 info[INF_SSL_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.ssl_lim);
3077 info[INF_MAX_SSL_RATE] = mkf_u32(FN_MAX, global.ssl_max);
3078 info[INF_SSL_FRONTEND_KEY_RATE] = mkf_u32(0, ssl_key_rate);
3079 info[INF_SSL_FRONTEND_MAX_KEY_RATE] = mkf_u32(FN_MAX, global.ssl_fe_keys_max);
3080 info[INF_SSL_FRONTEND_SESSION_REUSE_PCT] = mkf_u32(0, ssl_reuse);
3081 info[INF_SSL_BACKEND_KEY_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.ssl_be_keys_per_sec));
3082 info[INF_SSL_BACKEND_MAX_KEY_RATE] = mkf_u32(FN_MAX, global.ssl_be_keys_max);
3083 info[INF_SSL_CACHE_LOOKUPS] = mkf_u32(FN_COUNTER, global.shctx_lookups);
3084 info[INF_SSL_CACHE_MISSES] = mkf_u32(FN_COUNTER, global.shctx_misses);
3085#endif
3086 info[INF_COMPRESS_BPS_IN] = mkf_u32(FN_RATE, read_freq_ctr(&global.comp_bps_in));
3087 info[INF_COMPRESS_BPS_OUT] = mkf_u32(FN_RATE, read_freq_ctr(&global.comp_bps_out));
3088 info[INF_COMPRESS_BPS_RATE_LIM] = mkf_u32(FO_CONFIG|FN_LIMIT, global.comp_rate_lim);
3089#ifdef USE_ZLIB
3090 info[INF_ZLIB_MEM_USAGE] = mkf_u32(0, zlib_used_memory);
3091 info[INF_MAX_ZLIB_MEM_USAGE] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxzlibmem);
3092#endif
3093 info[INF_TASKS] = mkf_u32(0, nb_tasks_cur);
3094 info[INF_RUN_QUEUE] = mkf_u32(0, run_queue_cur);
3095 info[INF_IDLE_PCT] = mkf_u32(FN_AVG, idle_pct);
3096 info[INF_NODE] = mkf_str(FO_CONFIG|FN_OUTPUT|FS_SERVICE, global.node);
3097 if (global.desc)
3098 info[INF_DESCRIPTION] = mkf_str(FO_CONFIG|FN_OUTPUT|FS_SERVICE, global.desc);
3099
Thierry Fourniercb2c7672016-03-25 08:19:23 +01003100 return 1;
3101}
3102
3103/* This function dumps information onto the stream interface's read buffer.
3104 * It returns 0 as long as it does not complete, non-zero upon completion.
3105 * No state is used.
3106 */
3107static int stats_dump_info_to_buffer(struct stream_interface *si)
3108{
3109 struct appctx *appctx = __objt_appctx(si->end);
3110
3111 if (!stats_fill_info(info, INF_TOTAL_FIELDS))
3112 return 0;
3113
Willy Tarreau1b4ba1e2016-01-11 18:29:04 +01003114 chunk_reset(&trash);
Willy Tarreaucb809122016-01-11 20:08:42 +01003115
3116 if (appctx->ctx.stats.flags & STAT_FMT_TYPED)
3117 stats_dump_typed_info_fields(&trash, info);
3118 else
3119 stats_dump_info_fields(&trash, info);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003120
Willy Tarreaubc18da12015-03-13 14:00:47 +01003121 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02003122 si_applet_cant_put(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003123 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01003124 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003125
3126 return 1;
3127}
3128
Baptiste Assmann28289462015-07-03 08:01:20 +02003129/* dumps server state information into <buf> for all the servers found in <backend>
3130 * These information are all the parameters which may change during HAProxy runtime.
3131 * By default, we only export to the last known server state file format.
3132 * These information can be used at next startup to recover same level of server state.
3133 */
Cyril Bonté76a99782016-05-06 12:18:48 +02003134static int dump_servers_state(struct stream_interface *si, struct chunk *buf)
Baptiste Assmann28289462015-07-03 08:01:20 +02003135{
Cyril Bonté76a99782016-05-06 12:18:48 +02003136 struct appctx *appctx = __objt_appctx(si->end);
Baptiste Assmann28289462015-07-03 08:01:20 +02003137 struct server *srv;
3138 char srv_addr[INET6_ADDRSTRLEN + 1];
3139 time_t srv_time_since_last_change;
3140 int bk_f_forced_id, srv_f_forced_id;
3141
Baptiste Assmann28289462015-07-03 08:01:20 +02003142
Cyril Bonté76a99782016-05-06 12:18:48 +02003143 /* we don't want to report any state if the backend is not enabled on this process */
3144 if (appctx->ctx.server_state.px->bind_proc && !(appctx->ctx.server_state.px->bind_proc & (1UL << (relative_pid - 1))))
3145 return 1;
Baptiste Assmann28289462015-07-03 08:01:20 +02003146
Cyril Bontéd55bd7a2016-05-27 00:06:45 +02003147 if (!appctx->ctx.server_state.sv)
3148 appctx->ctx.server_state.sv = appctx->ctx.server_state.px->srv;
3149
Cyril Bonté76a99782016-05-06 12:18:48 +02003150 for (; appctx->ctx.server_state.sv != NULL; appctx->ctx.server_state.sv = srv->next) {
3151 srv = appctx->ctx.server_state.sv;
Baptiste Assmann28289462015-07-03 08:01:20 +02003152 srv_addr[0] = '\0';
Baptiste Assmann28289462015-07-03 08:01:20 +02003153
3154 switch (srv->addr.ss_family) {
3155 case AF_INET:
3156 inet_ntop(srv->addr.ss_family, &((struct sockaddr_in *)&srv->addr)->sin_addr,
3157 srv_addr, INET_ADDRSTRLEN + 1);
3158 break;
3159 case AF_INET6:
3160 inet_ntop(srv->addr.ss_family, &((struct sockaddr_in6 *)&srv->addr)->sin6_addr,
3161 srv_addr, INET6_ADDRSTRLEN + 1);
3162 break;
3163 }
3164 srv_time_since_last_change = now.tv_sec - srv->last_change;
Cyril Bonté76a99782016-05-06 12:18:48 +02003165 bk_f_forced_id = appctx->ctx.server_state.px->options & PR_O_FORCED_ID ? 1 : 0;
Baptiste Assmann28289462015-07-03 08:01:20 +02003166 srv_f_forced_id = srv->flags & SRV_F_FORCED_ID ? 1 : 0;
3167
3168 chunk_appendf(buf,
3169 "%d %s "
3170 "%d %s %s "
3171 "%d %d %d %d %ld "
3172 "%d %d %d %d %d "
3173 "%d %d"
3174 "\n",
Cyril Bonté76a99782016-05-06 12:18:48 +02003175 appctx->ctx.server_state.px->uuid, appctx->ctx.server_state.px->id,
Baptiste Assmann28289462015-07-03 08:01:20 +02003176 srv->puid, srv->id, srv_addr,
David Carlier081b3362015-11-18 06:10:22 +00003177 srv->state, srv->admin, srv->uweight, srv->iweight, (long int)srv_time_since_last_change,
Baptiste Assmann28289462015-07-03 08:01:20 +02003178 srv->check.status, srv->check.result, srv->check.health, srv->check.state, srv->agent.state,
3179 bk_f_forced_id, srv_f_forced_id);
Cyril Bonté76a99782016-05-06 12:18:48 +02003180 if (bi_putchk(si_ic(si), &trash) == -1) {
3181 si_applet_cant_put(si);
3182 return 0;
3183 }
Baptiste Assmann28289462015-07-03 08:01:20 +02003184 }
Cyril Bonté76a99782016-05-06 12:18:48 +02003185 return 1;
Baptiste Assmann28289462015-07-03 08:01:20 +02003186}
3187
Baptiste Assmann9b6857e2015-09-18 14:49:12 +02003188/* Parses backend list and simply report backend names */
3189static int stats_dump_backend_to_buffer(struct stream_interface *si)
3190{
Cyril Bonté6ca9e012016-05-06 12:18:49 +02003191 struct appctx *appctx = __objt_appctx(si->end);
Baptiste Assmann9b6857e2015-09-18 14:49:12 +02003192 extern struct proxy *proxy;
3193 struct proxy *curproxy;
3194
3195 chunk_reset(&trash);
Baptiste Assmann9b6857e2015-09-18 14:49:12 +02003196
Cyril Bonté6ca9e012016-05-06 12:18:49 +02003197 if (!appctx->ctx.be.px) {
3198 chunk_printf(&trash, "# name\n");
3199 if (bi_putchk(si_ic(si), &trash) == -1) {
3200 si_applet_cant_put(si);
3201 return 0;
3202 }
3203 appctx->ctx.be.px = proxy;
3204 }
3205
3206 for (; appctx->ctx.be.px != NULL; appctx->ctx.be.px = curproxy->next) {
3207 curproxy = appctx->ctx.be.px;
3208
Baptiste Assmann9b6857e2015-09-18 14:49:12 +02003209 /* looking for backends only */
3210 if (!(curproxy->cap & PR_CAP_BE))
3211 continue;
3212
3213 /* we don't want to list a backend which is bound to this process */
3214 if (curproxy->bind_proc && !(curproxy->bind_proc & (1UL << (relative_pid - 1))))
3215 continue;
3216
3217 chunk_appendf(&trash, "%s\n", curproxy->id);
Cyril Bonté6ca9e012016-05-06 12:18:49 +02003218 if (bi_putchk(si_ic(si), &trash) == -1) {
3219 si_applet_cant_put(si);
3220 return 0;
3221 }
Baptiste Assmann9b6857e2015-09-18 14:49:12 +02003222 }
3223
3224 return 1;
3225}
3226
Baptiste Assmann28289462015-07-03 08:01:20 +02003227/* Parses backend list or simply use backend name provided by the user to return
3228 * states of servers to stdout.
3229 */
3230static int stats_dump_servers_state_to_buffer(struct stream_interface *si)
3231{
3232 struct appctx *appctx = __objt_appctx(si->end);
3233 extern struct proxy *proxy;
3234 struct proxy *curproxy;
3235
3236 chunk_reset(&trash);
3237
Willy Tarreaua58c4352016-06-22 14:51:40 +02003238 if (appctx->st2 == STAT_ST_INIT) {
3239 if (!appctx->ctx.server_state.px)
3240 appctx->ctx.server_state.px = proxy;
3241 appctx->st2 = STAT_ST_HEAD;
3242 }
3243
3244 if (appctx->st2 == STAT_ST_HEAD) {
Cyril Bonté76a99782016-05-06 12:18:48 +02003245 chunk_printf(&trash, "%d\n# %s\n", SRV_STATE_FILE_VERSION, SRV_STATE_FILE_FIELD_NAMES);
3246 if (bi_putchk(si_ic(si), &trash) == -1) {
3247 si_applet_cant_put(si);
3248 return 0;
Baptiste Assmann28289462015-07-03 08:01:20 +02003249 }
Willy Tarreaua58c4352016-06-22 14:51:40 +02003250 appctx->st2 = STAT_ST_INFO;
Baptiste Assmann28289462015-07-03 08:01:20 +02003251 }
3252
Willy Tarreaua58c4352016-06-22 14:51:40 +02003253 /* STAT_ST_INFO */
Cyril Bonté76a99782016-05-06 12:18:48 +02003254 for (; appctx->ctx.server_state.px != NULL; appctx->ctx.server_state.px = curproxy->next) {
3255 curproxy = appctx->ctx.server_state.px;
Cyril Bonté76a99782016-05-06 12:18:48 +02003256 /* servers are only in backends */
3257 if (curproxy->cap & PR_CAP_BE) {
3258 if (!dump_servers_state(si, &trash))
3259 return 0;
3260
3261 if (bi_putchk(si_ic(si), &trash) == -1) {
3262 si_applet_cant_put(si);
3263 return 0;
3264 }
3265 }
3266 /* only the selected proxy is dumped */
3267 if (appctx->ctx.server_state.iid)
3268 break;
Baptiste Assmann28289462015-07-03 08:01:20 +02003269 }
3270
3271 return 1;
3272}
3273
Willy Tarreau12833bb2014-01-28 16:49:56 +01003274/* This function dumps memory usage information onto the stream interface's
3275 * read buffer. It returns 0 as long as it does not complete, non-zero upon
3276 * completion. No state is used.
3277 */
3278static int stats_dump_pools_to_buffer(struct stream_interface *si)
3279{
3280 dump_pools_to_trash();
Willy Tarreaubc18da12015-03-13 14:00:47 +01003281 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02003282 si_applet_cant_put(si);
Willy Tarreau12833bb2014-01-28 16:49:56 +01003283 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01003284 }
Willy Tarreau82a86022016-01-04 19:04:18 +01003285 return 1;
3286}
3287
3288/* Dump all fields from <stats> into <out> using CSV format */
3289static int stats_dump_fields_csv(struct chunk *out, const struct field *stats)
3290{
3291 int field;
3292
3293 for (field = 0; field < ST_F_TOTAL_FIELDS; field++) {
3294 if (!stats_emit_raw_data_field(out, &stats[field]))
3295 return 0;
3296 if (!chunk_strcat(out, ","))
3297 return 0;
3298 }
Conrad Hoffmann692c9382016-04-01 20:40:58 +02003299 chunk_strcat(out, "\n");
Willy Tarreau12833bb2014-01-28 16:49:56 +01003300 return 1;
3301}
3302
Willy Tarreau1e62df92016-01-11 18:57:53 +01003303/* Dump all fields from <stats> into <out> using a typed "field:desc:type:value" format */
3304static int stats_dump_fields_typed(struct chunk *out, const struct field *stats)
3305{
3306 int field;
3307
3308 for (field = 0; field < ST_F_TOTAL_FIELDS; field++) {
3309 if (!stats[field].type)
3310 continue;
3311
3312 chunk_appendf(out, "%c.%u.%u.%d.%s.%u:",
3313 stats[ST_F_TYPE].u.u32 == STATS_TYPE_FE ? 'F' :
3314 stats[ST_F_TYPE].u.u32 == STATS_TYPE_BE ? 'B' :
3315 stats[ST_F_TYPE].u.u32 == STATS_TYPE_SO ? 'L' :
3316 stats[ST_F_TYPE].u.u32 == STATS_TYPE_SV ? 'S' :
3317 '?',
3318 stats[ST_F_IID].u.u32, stats[ST_F_SID].u.u32,
3319 field, stat_field_names[field], stats[ST_F_PID].u.u32);
3320
3321 if (!stats_emit_field_tags(out, &stats[field], ':'))
3322 return 0;
3323 if (!stats_emit_typed_data_field(out, &stats[field]))
3324 return 0;
3325 if (!chunk_strcat(out, "\n"))
3326 return 0;
3327 }
3328 return 1;
3329}
3330
Willy Tarreau60600742016-01-11 15:32:30 +01003331/* Dump all fields from <stats> into <out> using the HTML format. A column is
Willy Tarreau508a63f2016-01-11 15:28:40 +01003332 * reserved for the checkbox is ST_SHOWADMIN is set in <flags>. Some extra info
3333 * are provided if ST_SHLGNDS is present in <flags>.
Cyril Bonté70be45d2010-10-12 00:14:35 +02003334 */
Willy Tarreau60600742016-01-11 15:32:30 +01003335static int stats_dump_fields_html(struct chunk *out, const struct field *stats, unsigned int flags)
Cyril Bonté70be45d2010-10-12 00:14:35 +02003336{
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003337 struct chunk src;
3338
Willy Tarreaub5f66b82016-01-08 17:15:39 +01003339 if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_FE) {
Willy Tarreau60600742016-01-11 15:32:30 +01003340 chunk_appendf(out,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003341 /* name, queue */
3342 "<tr class=\"frontend\">");
Cyril Bonté70be45d2010-10-12 00:14:35 +02003343
Willy Tarreau508a63f2016-01-11 15:28:40 +01003344 if (flags & ST_SHOWADMIN) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003345 /* Column sub-heading for Enable or Disable server */
Willy Tarreau60600742016-01-11 15:32:30 +01003346 chunk_appendf(out, "<td></td>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003347 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02003348
Willy Tarreau60600742016-01-11 15:32:30 +01003349 chunk_appendf(out,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003350 "<td class=ac>"
3351 "<a name=\"%s/Frontend\"></a>"
3352 "<a class=lfsb href=\"#%s/Frontend\">Frontend</a></td>"
3353 "<td colspan=3></td>"
3354 "",
Willy Tarreaud72c9172016-01-06 15:26:40 +01003355 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_PXNAME));
Cyril Bonté70be45d2010-10-12 00:14:35 +02003356
Willy Tarreau60600742016-01-11 15:32:30 +01003357 chunk_appendf(out,
Willy Tarreau466c9b52012-12-23 02:25:03 +01003358 /* sessions rate : current */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003359 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01003360 "<tr><th>Current connection rate:</th><td>%s/s</td></tr>"
3361 "<tr><th>Current session rate:</th><td>%s/s</td></tr>"
3362 "",
Willy Tarreaud72c9172016-01-06 15:26:40 +01003363 U2H(stats[ST_F_RATE].u.u32),
Willy Tarreauc73810f2016-01-11 13:52:04 +01003364 U2H(stats[ST_F_CONN_RATE].u.u32),
Willy Tarreaud72c9172016-01-06 15:26:40 +01003365 U2H(stats[ST_F_RATE].u.u32));
Willy Tarreau466c9b52012-12-23 02:25:03 +01003366
Willy Tarreauf8211df2016-01-11 14:09:38 +01003367 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
Willy Tarreau60600742016-01-11 15:32:30 +01003368 chunk_appendf(out,
Willy Tarreau466c9b52012-12-23 02:25:03 +01003369 "<tr><th>Current request rate:</th><td>%s/s</td></tr>",
Willy Tarreaud72c9172016-01-06 15:26:40 +01003370 U2H(stats[ST_F_REQ_RATE].u.u32));
Willy Tarreau466c9b52012-12-23 02:25:03 +01003371
Willy Tarreau60600742016-01-11 15:32:30 +01003372 chunk_appendf(out,
Willy Tarreau466c9b52012-12-23 02:25:03 +01003373 "</table></div></u></td>"
3374 /* sessions rate : max */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003375 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01003376 "<tr><th>Max connection rate:</th><td>%s/s</td></tr>"
3377 "<tr><th>Max session rate:</th><td>%s/s</td></tr>"
3378 "",
Willy Tarreaud72c9172016-01-06 15:26:40 +01003379 U2H(stats[ST_F_RATE_MAX].u.u32),
Willy Tarreauc73810f2016-01-11 13:52:04 +01003380 U2H(stats[ST_F_CONN_RATE_MAX].u.u32),
Willy Tarreaud72c9172016-01-06 15:26:40 +01003381 U2H(stats[ST_F_RATE_MAX].u.u32));
Willy Tarreau466c9b52012-12-23 02:25:03 +01003382
Willy Tarreauf8211df2016-01-11 14:09:38 +01003383 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
Willy Tarreau60600742016-01-11 15:32:30 +01003384 chunk_appendf(out,
Willy Tarreau466c9b52012-12-23 02:25:03 +01003385 "<tr><th>Max request rate:</th><td>%s/s</td></tr>",
Willy Tarreaud72c9172016-01-06 15:26:40 +01003386 U2H(stats[ST_F_REQ_RATE_MAX].u.u32));
Willy Tarreau466c9b52012-12-23 02:25:03 +01003387
Willy Tarreau60600742016-01-11 15:32:30 +01003388 chunk_appendf(out,
Willy Tarreau466c9b52012-12-23 02:25:03 +01003389 "</table></div></u></td>"
3390 /* sessions rate : limit */
3391 "<td>%s</td>",
Willy Tarreaud72c9172016-01-06 15:26:40 +01003392 LIM2A(stats[ST_F_RATE_LIM].u.u32, "-"));
Cyril Bonté70be45d2010-10-12 00:14:35 +02003393
Willy Tarreau60600742016-01-11 15:32:30 +01003394 chunk_appendf(out,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003395 /* sessions: current, max, limit, total */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003396 "<td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003397 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01003398 "<tr><th>Cum. connections:</th><td>%s</td></tr>"
3399 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003400 "",
Willy Tarreauc73810f2016-01-11 13:52:04 +01003401 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 +01003402 U2H(stats[ST_F_STOT].u.u64),
Willy Tarreauc73810f2016-01-11 13:52:04 +01003403 U2H(stats[ST_F_CONN_TOT].u.u64),
Willy Tarreaud72c9172016-01-06 15:26:40 +01003404 U2H(stats[ST_F_STOT].u.u64));
Cyril Bonté70be45d2010-10-12 00:14:35 +02003405
Willy Tarreau466c9b52012-12-23 02:25:03 +01003406 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
Willy Tarreauf8211df2016-01-11 14:09:38 +01003407 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
Willy Tarreau60600742016-01-11 15:32:30 +01003408 chunk_appendf(out,
Willy Tarreau466c9b52012-12-23 02:25:03 +01003409 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
3410 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
3411 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
3412 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
3413 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
3414 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
3415 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
3416 "<tr><th>- other responses:</th><td>%s</td></tr>"
3417 "<tr><th>Intercepted requests:</th><td>%s</td></tr>"
3418 "",
Willy Tarreaud72c9172016-01-06 15:26:40 +01003419 U2H(stats[ST_F_REQ_TOT].u.u64),
3420 U2H(stats[ST_F_HRSP_1XX].u.u64),
3421 U2H(stats[ST_F_HRSP_2XX].u.u64),
3422 U2H(stats[ST_F_COMP_RSP].u.u64),
3423 stats[ST_F_HRSP_2XX].u.u64 ?
3424 (int)(100 * stats[ST_F_COMP_RSP].u.u64 / stats[ST_F_HRSP_2XX].u.u64) : 0,
3425 U2H(stats[ST_F_HRSP_3XX].u.u64),
3426 U2H(stats[ST_F_HRSP_4XX].u.u64),
3427 U2H(stats[ST_F_HRSP_5XX].u.u64),
3428 U2H(stats[ST_F_HRSP_OTHER].u.u64),
Willy Tarreau5b9bdff2016-01-11 14:40:47 +01003429 U2H(stats[ST_F_INTERCEPTED].u.u64));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003430 }
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02003431
Willy Tarreau60600742016-01-11 15:32:30 +01003432 chunk_appendf(out,
Willy Tarreau466c9b52012-12-23 02:25:03 +01003433 "</table></div></u></td>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05003434 /* sessions: lbtot, lastsess */
3435 "<td></td><td></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003436 /* bytes : in */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003437 "<td>%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003438 "",
Willy Tarreaud72c9172016-01-06 15:26:40 +01003439 U2H(stats[ST_F_BIN].u.u64));
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02003440
Willy Tarreau60600742016-01-11 15:32:30 +01003441 chunk_appendf(out,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003442 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
Willy Tarreau4e5d58e2015-07-04 14:35:15 +02003443 "<td>%s%s<div class=tips><table class=det>"
3444 "<tr><th>Response bytes in:</th><td>%s</td></tr>"
3445 "<tr><th>Compression in:</th><td>%s</td></tr>"
3446 "<tr><th>Compression out:</th><td>%s</td><td>(%d%%)</td></tr>"
3447 "<tr><th>Compression bypass:</th><td>%s</td></tr>"
3448 "<tr><th>Total bytes saved:</th><td>%s</td><td>(%d%%)</td></tr>"
3449 "</table></div>%s</td>",
Willy Tarreaud72c9172016-01-06 15:26:40 +01003450 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "<u>":"",
3451 U2H(stats[ST_F_BOUT].u.u64),
3452 U2H(stats[ST_F_BOUT].u.u64),
3453 U2H(stats[ST_F_COMP_IN].u.u64),
3454 U2H(stats[ST_F_COMP_OUT].u.u64),
3455 stats[ST_F_COMP_IN].u.u64 ? (int)(stats[ST_F_COMP_OUT].u.u64 * 100 / stats[ST_F_COMP_IN].u.u64) : 0,
3456 U2H(stats[ST_F_COMP_BYP].u.u64),
3457 U2H(stats[ST_F_COMP_IN].u.u64 - stats[ST_F_COMP_OUT].u.u64),
3458 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,
3459 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "</u>":"");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02003460
Willy Tarreau60600742016-01-11 15:32:30 +01003461 chunk_appendf(out,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003462 /* denied: req, resp */
3463 "<td>%s</td><td>%s</td>"
3464 /* errors : request, connect, response */
3465 "<td>%s</td><td></td><td></td>"
3466 /* warnings: retries, redispatches */
3467 "<td></td><td></td>"
3468 /* server status : reflect frontend status */
3469 "<td class=ac>%s</td>"
3470 /* rest of server: nothing */
3471 "<td class=ac colspan=8></td></tr>"
3472 "",
Willy Tarreaud72c9172016-01-06 15:26:40 +01003473 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
3474 U2H(stats[ST_F_EREQ].u.u64),
3475 field_str(stats, ST_F_STATUS));
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02003476 }
Willy Tarreaufa9512f2016-01-08 17:20:51 +01003477 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_SO) {
Willy Tarreau60600742016-01-11 15:32:30 +01003478 chunk_appendf(out, "<tr class=socket>");
Willy Tarreau508a63f2016-01-11 15:28:40 +01003479 if (flags & ST_SHOWADMIN) {
Willy Tarreaufa9512f2016-01-08 17:20:51 +01003480 /* Column sub-heading for Enable or Disable server */
Willy Tarreau60600742016-01-11 15:32:30 +01003481 chunk_appendf(out, "<td></td>");
Willy Tarreaufa9512f2016-01-08 17:20:51 +01003482 }
3483
Willy Tarreau60600742016-01-11 15:32:30 +01003484 chunk_appendf(out,
Willy Tarreaufa9512f2016-01-08 17:20:51 +01003485 /* frontend name, listener name */
3486 "<td class=ac><a name=\"%s/+%s\"></a>%s"
3487 "<a class=lfsb href=\"#%s/+%s\">%s</a>"
3488 "",
3489 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME),
3490 (flags & ST_SHLGNDS)?"<u>":"",
3491 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME), field_str(stats, ST_F_SVNAME));
3492
3493 if (flags & ST_SHLGNDS) {
Willy Tarreau60600742016-01-11 15:32:30 +01003494 chunk_appendf(out, "<div class=tips>");
Willy Tarreaufa9512f2016-01-08 17:20:51 +01003495
3496 if (isdigit(*field_str(stats, ST_F_ADDR)))
Willy Tarreau60600742016-01-11 15:32:30 +01003497 chunk_appendf(out, "IPv4: %s, ", field_str(stats, ST_F_ADDR));
Willy Tarreaufa9512f2016-01-08 17:20:51 +01003498 else if (*field_str(stats, ST_F_ADDR) == '[')
Willy Tarreau60600742016-01-11 15:32:30 +01003499 chunk_appendf(out, "IPv6: %s, ", field_str(stats, ST_F_ADDR));
Willy Tarreaufa9512f2016-01-08 17:20:51 +01003500 else if (*field_str(stats, ST_F_ADDR))
Willy Tarreau60600742016-01-11 15:32:30 +01003501 chunk_appendf(out, "%s, ", field_str(stats, ST_F_ADDR));
Willy Tarreaufa9512f2016-01-08 17:20:51 +01003502
3503 /* id */
Willy Tarreau60600742016-01-11 15:32:30 +01003504 chunk_appendf(out, "id: %d</div>", stats[ST_F_SID].u.u32);
Willy Tarreaufa9512f2016-01-08 17:20:51 +01003505 }
3506
Willy Tarreau60600742016-01-11 15:32:30 +01003507 chunk_appendf(out,
Willy Tarreaufa9512f2016-01-08 17:20:51 +01003508 /* queue */
3509 "%s</td><td colspan=3></td>"
3510 /* sessions rate: current, max, limit */
3511 "<td colspan=3>&nbsp;</td>"
3512 /* sessions: current, max, limit, total, lbtot, lastsess */
3513 "<td>%s</td><td>%s</td><td>%s</td>"
3514 "<td>%s</td><td>&nbsp;</td><td>&nbsp;</td>"
3515 /* bytes: in, out */
3516 "<td>%s</td><td>%s</td>"
3517 "",
3518 (flags & ST_SHLGNDS)?"</u>":"",
3519 U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SMAX].u.u32), U2H(stats[ST_F_SLIM].u.u32),
3520 U2H(stats[ST_F_STOT].u.u64), U2H(stats[ST_F_BIN].u.u64), U2H(stats[ST_F_BOUT].u.u64));
3521
Willy Tarreau60600742016-01-11 15:32:30 +01003522 chunk_appendf(out,
Willy Tarreaufa9512f2016-01-08 17:20:51 +01003523 /* denied: req, resp */
3524 "<td>%s</td><td>%s</td>"
3525 /* errors: request, connect, response */
3526 "<td>%s</td><td></td><td></td>"
3527 /* warnings: retries, redispatches */
3528 "<td></td><td></td>"
3529 /* server status: reflect listener status */
3530 "<td class=ac>%s</td>"
3531 /* rest of server: nothing */
3532 "<td class=ac colspan=8></td></tr>"
3533 "",
3534 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
3535 U2H(stats[ST_F_EREQ].u.u64),
3536 field_str(stats, ST_F_STATUS));
3537 }
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003538 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_SV) {
3539 const char *style;
Willy Tarreaub5f66b82016-01-08 17:15:39 +01003540
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003541 /* determine the style to use depending on the server's state,
3542 * its health and weight. There isn't a 1-to-1 mapping between
3543 * state and styles for the cases where the server is (still)
3544 * up. The reason is that we don't want to report nolb and
3545 * drain with the same color.
3546 */
Willy Tarreaub5f66b82016-01-08 17:15:39 +01003547
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003548 if (strcmp(field_str(stats, ST_F_STATUS), "DOWN") == 0 ||
3549 strcmp(field_str(stats, ST_F_STATUS), "DOWN (agent)") == 0) {
3550 style = "down";
3551 }
3552 else if (strcmp(field_str(stats, ST_F_STATUS), "DOWN ") == 0) {
3553 style = "going_up";
3554 }
3555 else if (strcmp(field_str(stats, ST_F_STATUS), "NOLB ") == 0) {
3556 style = "going_down";
3557 }
3558 else if (strcmp(field_str(stats, ST_F_STATUS), "NOLB") == 0) {
3559 style = "nolb";
3560 }
3561 else if (strcmp(field_str(stats, ST_F_STATUS), "no check") == 0) {
3562 style = "no_check";
3563 }
3564 else if (!stats[ST_F_CHKFAIL].type ||
3565 stats[ST_F_CHECK_HEALTH].u.u32 == stats[ST_F_CHECK_RISE].u.u32 + stats[ST_F_CHECK_FALL].u.u32 - 1) {
3566 /* no check or max health = UP */
3567 if (stats[ST_F_WEIGHT].u.u32)
3568 style = "up";
3569 else
3570 style = "draining";
3571 }
3572 else {
3573 style = "going_down";
3574 }
Willy Tarreaub5f66b82016-01-08 17:15:39 +01003575
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003576 if (memcmp(field_str(stats, ST_F_STATUS), "MAINT", 5) == 0)
Willy Tarreau60600742016-01-11 15:32:30 +01003577 chunk_appendf(out, "<tr class=\"maintain\">");
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003578 else
Willy Tarreau60600742016-01-11 15:32:30 +01003579 chunk_appendf(out,
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003580 "<tr class=\"%s_%s\">",
3581 (stats[ST_F_BCK].u.u32) ? "backup" : "active", style);
Willy Tarreaub5f66b82016-01-08 17:15:39 +01003582
Willy Tarreaub5f66b82016-01-08 17:15:39 +01003583
Willy Tarreau508a63f2016-01-11 15:28:40 +01003584 if (flags & ST_SHOWADMIN)
Willy Tarreau60600742016-01-11 15:32:30 +01003585 chunk_appendf(out,
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003586 "<td><input type=\"checkbox\" name=\"s\" value=\"%s\"></td>",
3587 field_str(stats, ST_F_SVNAME));
Willy Tarreaub5f66b82016-01-08 17:15:39 +01003588
Willy Tarreau60600742016-01-11 15:32:30 +01003589 chunk_appendf(out,
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003590 "<td class=ac><a name=\"%s/%s\"></a>%s"
3591 "<a class=lfsb href=\"#%s/%s\">%s</a>"
3592 "",
3593 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME),
3594 (flags & ST_SHLGNDS) ? "<u>" : "",
3595 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 +01003596
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003597 if (flags & ST_SHLGNDS) {
Willy Tarreau60600742016-01-11 15:32:30 +01003598 chunk_appendf(out, "<div class=tips>");
Willy Tarreaub5f66b82016-01-08 17:15:39 +01003599
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003600 if (isdigit(*field_str(stats, ST_F_ADDR)))
Willy Tarreau60600742016-01-11 15:32:30 +01003601 chunk_appendf(out, "IPv4: %s, ", field_str(stats, ST_F_ADDR));
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003602 else if (*field_str(stats, ST_F_ADDR) == '[')
Willy Tarreau60600742016-01-11 15:32:30 +01003603 chunk_appendf(out, "IPv6: %s, ", field_str(stats, ST_F_ADDR));
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003604 else if (*field_str(stats, ST_F_ADDR))
Willy Tarreau60600742016-01-11 15:32:30 +01003605 chunk_appendf(out, "%s, ", field_str(stats, ST_F_ADDR));
Willy Tarreaub5f66b82016-01-08 17:15:39 +01003606
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003607 /* id */
Willy Tarreau60600742016-01-11 15:32:30 +01003608 chunk_appendf(out, "id: %d", stats[ST_F_SID].u.u32);
Willy Tarreaub5f66b82016-01-08 17:15:39 +01003609
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003610 /* cookie */
3611 if (stats[ST_F_COOKIE].type) {
Willy Tarreau60600742016-01-11 15:32:30 +01003612 chunk_appendf(out, ", cookie: '");
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003613 chunk_initstr(&src, field_str(stats, ST_F_COOKIE));
Willy Tarreau60600742016-01-11 15:32:30 +01003614 chunk_htmlencode(out, &src);
3615 chunk_appendf(out, "'");
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003616 }
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02003617
Willy Tarreau60600742016-01-11 15:32:30 +01003618 chunk_appendf(out, "</div>");
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003619 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003620
Willy Tarreau60600742016-01-11 15:32:30 +01003621 chunk_appendf(out,
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003622 /* queue : current, max, limit */
3623 "%s</td><td>%s</td><td>%s</td><td>%s</td>"
3624 /* sessions rate : current, max, limit */
3625 "<td>%s</td><td>%s</td><td></td>"
3626 "",
3627 (flags & ST_SHLGNDS) ? "</u>" : "",
3628 U2H(stats[ST_F_QCUR].u.u32), U2H(stats[ST_F_QMAX].u.u32), LIM2A(stats[ST_F_QLIMIT].u.u32, "-"),
3629 U2H(stats[ST_F_RATE].u.u32), U2H(stats[ST_F_RATE_MAX].u.u32));
Willy Tarreau4607fad2016-01-06 15:41:29 +01003630
Willy Tarreau60600742016-01-11 15:32:30 +01003631 chunk_appendf(out,
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003632 /* sessions: current, max, limit, total */
3633 "<td>%s</td><td>%s</td><td>%s</td>"
3634 "<td><u>%s<div class=tips><table class=det>"
3635 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
3636 "",
3637 U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SMAX].u.u32), LIM2A(stats[ST_F_SLIM].u.u32, "-"),
3638 U2H(stats[ST_F_STOT].u.u64),
3639 U2H(stats[ST_F_STOT].u.u64));
Willy Tarreau4607fad2016-01-06 15:41:29 +01003640
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003641 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
Willy Tarreauf8211df2016-01-11 14:09:38 +01003642 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003643 unsigned long long tot;
Willy Tarreaua6f5a732016-01-08 16:59:56 +01003644
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003645 tot = stats[ST_F_HRSP_OTHER].u.u64;
3646 tot += stats[ST_F_HRSP_1XX].u.u64;
3647 tot += stats[ST_F_HRSP_2XX].u.u64;
3648 tot += stats[ST_F_HRSP_3XX].u.u64;
3649 tot += stats[ST_F_HRSP_4XX].u.u64;
3650 tot += stats[ST_F_HRSP_5XX].u.u64;
3651
Willy Tarreau60600742016-01-11 15:32:30 +01003652 chunk_appendf(out,
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003653 "<tr><th>Cum. HTTP responses:</th><td>%s</td></tr>"
3654 "<tr><th>- HTTP 1xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
3655 "<tr><th>- HTTP 2xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
3656 "<tr><th>- HTTP 3xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
3657 "<tr><th>- HTTP 4xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
3658 "<tr><th>- HTTP 5xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
3659 "<tr><th>- other responses:</th><td>%s</td><td>(%d%%)</td></tr>"
3660 "",
3661 U2H(tot),
3662 U2H(stats[ST_F_HRSP_1XX].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_1XX].u.u64 / tot) : 0,
3663 U2H(stats[ST_F_HRSP_2XX].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_2XX].u.u64 / tot) : 0,
3664 U2H(stats[ST_F_HRSP_3XX].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_3XX].u.u64 / tot) : 0,
3665 U2H(stats[ST_F_HRSP_4XX].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_4XX].u.u64 / tot) : 0,
3666 U2H(stats[ST_F_HRSP_5XX].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_5XX].u.u64 / tot) : 0,
3667 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 +01003668 }
Willy Tarreaua6f5a732016-01-08 16:59:56 +01003669
Willy Tarreau60600742016-01-11 15:32:30 +01003670 chunk_appendf(out, "<tr><th colspan=3>Avg over last 1024 success. conn.</th></tr>");
3671 chunk_appendf(out, "<tr><th>- Queue time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_QTIME].u.u32));
3672 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 +01003673 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
Willy Tarreau60600742016-01-11 15:32:30 +01003674 chunk_appendf(out, "<tr><th>- Response time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_RTIME].u.u32));
3675 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 +02003676
Willy Tarreau60600742016-01-11 15:32:30 +01003677 chunk_appendf(out,
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003678 "</table></div></u></td>"
3679 /* sessions: lbtot, last */
3680 "<td>%s</td><td>%s</td>",
3681 U2H(stats[ST_F_LBTOT].u.u64),
3682 human_time(stats[ST_F_LASTSESS].u.s32, 1));
Willy Tarreau91861262007-10-17 17:06:05 +02003683
Willy Tarreau60600742016-01-11 15:32:30 +01003684 chunk_appendf(out,
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003685 /* bytes : in, out */
3686 "<td>%s</td><td>%s</td>"
3687 /* denied: req, resp */
3688 "<td></td><td>%s</td>"
3689 /* errors : request, connect */
3690 "<td></td><td>%s</td>"
3691 /* errors : response */
3692 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
3693 /* warnings: retries, redispatches */
3694 "<td>%lld</td><td>%lld</td>"
3695 "",
3696 U2H(stats[ST_F_BIN].u.u64), U2H(stats[ST_F_BOUT].u.u64),
3697 U2H(stats[ST_F_DRESP].u.u64),
3698 U2H(stats[ST_F_ECON].u.u64),
3699 U2H(stats[ST_F_ERESP].u.u64),
3700 (long long)stats[ST_F_CLI_ABRT].u.u64,
3701 (long long)stats[ST_F_SRV_ABRT].u.u64,
3702 (long long)stats[ST_F_WRETR].u.u64,
3703 (long long)stats[ST_F_WREDIS].u.u64);
3704
3705 /* status, last change */
Willy Tarreau60600742016-01-11 15:32:30 +01003706 chunk_appendf(out, "<td class=ac>");
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003707
3708 /* FIXME!!!!
3709 * LASTCHG should contain the last change for *this* server and must be computed
3710 * properly above, as was done below, ie: this server if maint, otherwise ref server
3711 * if tracking. Note that ref is either local or remote depending on tracking.
3712 */
3713
3714
3715 if (memcmp(field_str(stats, ST_F_STATUS), "MAINT", 5) == 0) {
Willy Tarreau60600742016-01-11 15:32:30 +01003716 chunk_appendf(out, "%s MAINT", human_time(stats[ST_F_LASTCHG].u.u32, 1));
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003717 }
3718 else if (memcmp(field_str(stats, ST_F_STATUS), "no check", 5) == 0) {
Willy Tarreau60600742016-01-11 15:32:30 +01003719 chunk_strcat(out, "<i>no check</i>");
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003720 }
3721 else {
Willy Tarreau60600742016-01-11 15:32:30 +01003722 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 +01003723 if (memcmp(field_str(stats, ST_F_STATUS), "DOWN", 4) == 0) {
3724 if (stats[ST_F_CHECK_HEALTH].u.u32)
Willy Tarreau60600742016-01-11 15:32:30 +01003725 chunk_strcat(out, " &uarr;");
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003726 }
3727 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 +01003728 chunk_strcat(out, " &darr;");
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003729 }
3730
3731 if (memcmp(field_str(stats, ST_F_STATUS), "DOWN", 4) == 0 &&
3732 stats[ST_F_AGENT_STATUS].type && !stats[ST_F_AGENT_HEALTH].u.u32) {
Willy Tarreau60600742016-01-11 15:32:30 +01003733 chunk_appendf(out,
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003734 "</td><td class=ac><u> %s",
3735 field_str(stats, ST_F_AGENT_STATUS));
3736
3737 if (stats[ST_F_AGENT_CODE].type)
Willy Tarreau60600742016-01-11 15:32:30 +01003738 chunk_appendf(out, "/%d", stats[ST_F_AGENT_CODE].u.u32);
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003739
David Carlier08d35de2016-06-27 14:12:59 +01003740 if (stats[ST_F_AGENT_DURATION].type)
Willy Tarreau60600742016-01-11 15:32:30 +01003741 chunk_appendf(out, " in %lums", (long)stats[ST_F_AGENT_DURATION].u.u64);
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003742
Willy Tarreau60600742016-01-11 15:32:30 +01003743 chunk_appendf(out, "<div class=tips>%s", field_str(stats, ST_F_AGENT_DESC));
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003744
3745 if (*field_str(stats, ST_F_LAST_AGT)) {
Willy Tarreau60600742016-01-11 15:32:30 +01003746 chunk_appendf(out, ": ");
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003747 chunk_initstr(&src, field_str(stats, ST_F_LAST_AGT));
Willy Tarreau60600742016-01-11 15:32:30 +01003748 chunk_htmlencode(out, &src);
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003749 }
Willy Tarreau60600742016-01-11 15:32:30 +01003750 chunk_appendf(out, "</div></u>");
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003751 }
3752 else if (stats[ST_F_CHECK_STATUS].type) {
Willy Tarreau60600742016-01-11 15:32:30 +01003753 chunk_appendf(out,
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003754 "</td><td class=ac><u> %s",
3755 field_str(stats, ST_F_CHECK_STATUS));
3756
3757 if (stats[ST_F_CHECK_CODE].type)
Willy Tarreau60600742016-01-11 15:32:30 +01003758 chunk_appendf(out, "/%d", stats[ST_F_CHECK_CODE].u.u32);
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003759
David Carlier08d35de2016-06-27 14:12:59 +01003760 if (stats[ST_F_CHECK_DURATION].type)
Willy Tarreau60600742016-01-11 15:32:30 +01003761 chunk_appendf(out, " in %lums", (long)stats[ST_F_CHECK_DURATION].u.u64);
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003762
Willy Tarreau60600742016-01-11 15:32:30 +01003763 chunk_appendf(out, "<div class=tips>%s", field_str(stats, ST_F_CHECK_DESC));
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003764
3765 if (*field_str(stats, ST_F_LAST_CHK)) {
Willy Tarreau60600742016-01-11 15:32:30 +01003766 chunk_appendf(out, ": ");
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003767 chunk_initstr(&src, field_str(stats, ST_F_LAST_CHK));
Willy Tarreau60600742016-01-11 15:32:30 +01003768 chunk_htmlencode(out, &src);
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003769 }
Willy Tarreau60600742016-01-11 15:32:30 +01003770 chunk_appendf(out, "</div></u>");
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003771 }
3772 else
Willy Tarreau60600742016-01-11 15:32:30 +01003773 chunk_appendf(out, "</td><td>");
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003774
Willy Tarreau60600742016-01-11 15:32:30 +01003775 chunk_appendf(out,
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003776 /* weight */
3777 "</td><td class=ac>%d</td>"
3778 /* act, bck */
3779 "<td class=ac>%s</td><td class=ac>%s</td>"
3780 "",
3781 stats[ST_F_WEIGHT].u.u32,
3782 stats[ST_F_BCK].u.u32 ? "-" : "Y",
3783 stats[ST_F_BCK].u.u32 ? "Y" : "-");
3784
3785 /* check failures: unique, fatal, down time */
3786 if (stats[ST_F_CHKFAIL].type) {
Willy Tarreau60600742016-01-11 15:32:30 +01003787 chunk_appendf(out, "<td><u>%lld", (long long)stats[ST_F_CHKFAIL].u.u64);
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003788
3789 if (stats[ST_F_HANAFAIL].type)
Willy Tarreau60600742016-01-11 15:32:30 +01003790 chunk_appendf(out, "/%lld", (long long)stats[ST_F_HANAFAIL].u.u64);
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003791
Willy Tarreau60600742016-01-11 15:32:30 +01003792 chunk_appendf(out,
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003793 "<div class=tips>Failed Health Checks%s</div></u></td>"
3794 "<td>%lld</td><td>%s</td>"
3795 "",
3796 stats[ST_F_HANAFAIL].type ? "/Health Analyses" : "",
3797 (long long)stats[ST_F_CHKDOWN].u.u64, human_time(stats[ST_F_DOWNTIME].u.u32, 1));
3798 }
3799 else if (strcmp(field_str(stats, ST_F_STATUS), "MAINT") != 0 && field_format(stats, ST_F_TRACKED) == FF_STR) {
3800 /* tracking a server (hence inherited maint would appear as "MAINT (via...)" */
Willy Tarreau60600742016-01-11 15:32:30 +01003801 chunk_appendf(out,
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003802 "<td class=ac colspan=3><a class=lfsb href=\"#%s\">via %s</a></td>",
3803 field_str(stats, ST_F_TRACKED), field_str(stats, ST_F_TRACKED));
3804 }
3805 else
Willy Tarreau60600742016-01-11 15:32:30 +01003806 chunk_appendf(out, "<td colspan=3></td>");
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003807
3808 /* throttle */
3809 if (stats[ST_F_THROTTLE].type)
Willy Tarreau60600742016-01-11 15:32:30 +01003810 chunk_appendf(out, "<td class=ac>%d %%</td></tr>\n", stats[ST_F_THROTTLE].u.u32);
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003811 else
Willy Tarreau60600742016-01-11 15:32:30 +01003812 chunk_appendf(out, "<td class=ac>-</td></tr>\n");
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003813 }
Willy Tarreaubbf84502016-01-08 17:25:50 +01003814 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_BE) {
Willy Tarreau60600742016-01-11 15:32:30 +01003815 chunk_appendf(out, "<tr class=\"backend\">");
Willy Tarreau508a63f2016-01-11 15:28:40 +01003816 if (flags & ST_SHOWADMIN) {
Willy Tarreaubbf84502016-01-08 17:25:50 +01003817 /* Column sub-heading for Enable or Disable server */
Willy Tarreau60600742016-01-11 15:32:30 +01003818 chunk_appendf(out, "<td></td>");
Willy Tarreaubbf84502016-01-08 17:25:50 +01003819 }
Willy Tarreau60600742016-01-11 15:32:30 +01003820 chunk_appendf(out,
Willy Tarreaubbf84502016-01-08 17:25:50 +01003821 "<td class=ac>"
3822 /* name */
3823 "%s<a name=\"%s/Backend\"></a>"
3824 "<a class=lfsb href=\"#%s/Backend\">Backend</a>"
3825 "",
3826 (flags & ST_SHLGNDS)?"<u>":"",
3827 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_PXNAME));
3828
3829 if (flags & ST_SHLGNDS) {
3830 /* balancing */
Willy Tarreau60600742016-01-11 15:32:30 +01003831 chunk_appendf(out, "<div class=tips>balancing: %s",
Willy Tarreauf1516d92016-01-11 14:48:36 +01003832 field_str(stats, ST_F_ALGO));
Willy Tarreaubbf84502016-01-08 17:25:50 +01003833
3834 /* cookie */
3835 if (stats[ST_F_COOKIE].type) {
Willy Tarreau60600742016-01-11 15:32:30 +01003836 chunk_appendf(out, ", cookie: '");
Willy Tarreaubbf84502016-01-08 17:25:50 +01003837 chunk_initstr(&src, field_str(stats, ST_F_COOKIE));
Willy Tarreau60600742016-01-11 15:32:30 +01003838 chunk_htmlencode(out, &src);
3839 chunk_appendf(out, "'");
Willy Tarreaubbf84502016-01-08 17:25:50 +01003840 }
Willy Tarreau60600742016-01-11 15:32:30 +01003841 chunk_appendf(out, "</div>");
Willy Tarreaubbf84502016-01-08 17:25:50 +01003842 }
3843
Willy Tarreau60600742016-01-11 15:32:30 +01003844 chunk_appendf(out,
Willy Tarreaubbf84502016-01-08 17:25:50 +01003845 "%s</td>"
3846 /* queue : current, max */
3847 "<td>%s</td><td>%s</td><td></td>"
3848 /* sessions rate : current, max, limit */
3849 "<td>%s</td><td>%s</td><td></td>"
3850 "",
3851 (flags & ST_SHLGNDS)?"</u>":"",
3852 U2H(stats[ST_F_QCUR].u.u32), U2H(stats[ST_F_QMAX].u.u32),
3853 U2H(stats[ST_F_RATE].u.u32), U2H(stats[ST_F_RATE_MAX].u.u32));
3854
Willy Tarreau60600742016-01-11 15:32:30 +01003855 chunk_appendf(out,
Willy Tarreaubbf84502016-01-08 17:25:50 +01003856 /* sessions: current, max, limit, total */
3857 "<td>%s</td><td>%s</td><td>%s</td>"
3858 "<td><u>%s<div class=tips><table class=det>"
3859 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
3860 "",
3861 U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SLIM].u.u32),
3862 U2H(stats[ST_F_STOT].u.u64),
3863 U2H(stats[ST_F_STOT].u.u64));
3864
3865 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
Willy Tarreauf8211df2016-01-11 14:09:38 +01003866 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
Willy Tarreau60600742016-01-11 15:32:30 +01003867 chunk_appendf(out,
Willy Tarreaubbf84502016-01-08 17:25:50 +01003868 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
3869 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
3870 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
3871 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
3872 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
3873 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
3874 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
3875 "<tr><th>- other responses:</th><td>%s</td></tr>"
3876 "<tr><th>Intercepted requests:</th><td>%s</td></tr>"
3877 "<tr><th colspan=3>Avg over last 1024 success. conn.</th></tr>"
3878 "",
3879 U2H(stats[ST_F_REQ_TOT].u.u64),
3880 U2H(stats[ST_F_HRSP_1XX].u.u64),
3881 U2H(stats[ST_F_HRSP_2XX].u.u64),
3882 U2H(stats[ST_F_COMP_RSP].u.u64),
3883 stats[ST_F_HRSP_2XX].u.u64 ?
3884 (int)(100 * stats[ST_F_COMP_RSP].u.u64 / stats[ST_F_HRSP_2XX].u.u64) : 0,
3885 U2H(stats[ST_F_HRSP_3XX].u.u64),
3886 U2H(stats[ST_F_HRSP_4XX].u.u64),
3887 U2H(stats[ST_F_HRSP_5XX].u.u64),
3888 U2H(stats[ST_F_HRSP_OTHER].u.u64),
Willy Tarreau5b9bdff2016-01-11 14:40:47 +01003889 U2H(stats[ST_F_INTERCEPTED].u.u64));
Willy Tarreaubbf84502016-01-08 17:25:50 +01003890 }
3891
Willy Tarreau60600742016-01-11 15:32:30 +01003892 chunk_appendf(out, "<tr><th>- Queue time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_QTIME].u.u32));
3893 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 +01003894 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
Willy Tarreau60600742016-01-11 15:32:30 +01003895 chunk_appendf(out, "<tr><th>- Response time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_RTIME].u.u32));
3896 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 +01003897
Willy Tarreau60600742016-01-11 15:32:30 +01003898 chunk_appendf(out,
Willy Tarreaubbf84502016-01-08 17:25:50 +01003899 "</table></div></u></td>"
3900 /* sessions: lbtot, last */
3901 "<td>%s</td><td>%s</td>"
3902 /* bytes: in */
3903 "<td>%s</td>"
3904 "",
3905 U2H(stats[ST_F_LBTOT].u.u64),
3906 human_time(stats[ST_F_LASTSESS].u.s32, 1),
3907 U2H(stats[ST_F_BIN].u.u64));
3908
Willy Tarreau60600742016-01-11 15:32:30 +01003909 chunk_appendf(out,
Willy Tarreaubbf84502016-01-08 17:25:50 +01003910 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
3911 "<td>%s%s<div class=tips><table class=det>"
3912 "<tr><th>Response bytes in:</th><td>%s</td></tr>"
3913 "<tr><th>Compression in:</th><td>%s</td></tr>"
3914 "<tr><th>Compression out:</th><td>%s</td><td>(%d%%)</td></tr>"
3915 "<tr><th>Compression bypass:</th><td>%s</td></tr>"
3916 "<tr><th>Total bytes saved:</th><td>%s</td><td>(%d%%)</td></tr>"
3917 "</table></div>%s</td>",
3918 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "<u>":"",
3919 U2H(stats[ST_F_BOUT].u.u64),
3920 U2H(stats[ST_F_BOUT].u.u64),
3921 U2H(stats[ST_F_COMP_IN].u.u64),
3922 U2H(stats[ST_F_COMP_OUT].u.u64),
3923 stats[ST_F_COMP_IN].u.u64 ? (int)(stats[ST_F_COMP_OUT].u.u64 * 100 / stats[ST_F_COMP_IN].u.u64) : 0,
3924 U2H(stats[ST_F_COMP_BYP].u.u64),
Christopher Faulet0b64f622016-04-28 15:09:31 +02003925 U2H(stats[ST_F_COMP_IN].u.u64 - stats[ST_F_COMP_OUT].u.u64),
Willy Tarreaubbf84502016-01-08 17:25:50 +01003926 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,
3927 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "</u>":"");
3928
Willy Tarreau60600742016-01-11 15:32:30 +01003929 chunk_appendf(out,
Willy Tarreaubbf84502016-01-08 17:25:50 +01003930 /* denied: req, resp */
3931 "<td>%s</td><td>%s</td>"
3932 /* errors : request, connect */
3933 "<td></td><td>%s</td>"
3934 /* errors : response */
3935 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
3936 /* warnings: retries, redispatches */
3937 "<td>%lld</td><td>%lld</td>"
3938 /* backend status: reflect backend status (up/down): we display UP
3939 * if the backend has known working servers or if it has no server at
3940 * all (eg: for stats). Then we display the total weight, number of
3941 * active and backups. */
3942 "<td class=ac>%s %s</td><td class=ac>&nbsp;</td><td class=ac>%d</td>"
3943 "<td class=ac>%d</td><td class=ac>%d</td>"
3944 "",
3945 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
3946 U2H(stats[ST_F_ECON].u.u64),
3947 U2H(stats[ST_F_ERESP].u.u64),
3948 (long long)stats[ST_F_CLI_ABRT].u.u64,
3949 (long long)stats[ST_F_SRV_ABRT].u.u64,
3950 (long long)stats[ST_F_WRETR].u.u64, (long long)stats[ST_F_WREDIS].u.u64,
3951 human_time(stats[ST_F_LASTCHG].u.u32, 1),
3952 strcmp(field_str(stats, ST_F_STATUS), "DOWN") ? field_str(stats, ST_F_STATUS) : "<font color=\"red\"><b>DOWN</b></font>",
3953 stats[ST_F_WEIGHT].u.u32,
3954 stats[ST_F_ACT].u.u32, stats[ST_F_BCK].u.u32);
3955
Willy Tarreau60600742016-01-11 15:32:30 +01003956 chunk_appendf(out,
Willy Tarreaubbf84502016-01-08 17:25:50 +01003957 /* rest of backend: nothing, down transitions, total downtime, throttle */
3958 "<td class=ac>&nbsp;</td><td>%d</td>"
3959 "<td>%s</td>"
3960 "<td></td>"
3961 "</tr>",
3962 stats[ST_F_CHKDOWN].u.u32,
3963 stats[ST_F_DOWNTIME].type ? human_time(stats[ST_F_DOWNTIME].u.u32, 1) : "&nbsp;");
3964 }
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003965 return 1;
3966}
3967
Willy Tarreau501f6022016-01-08 17:37:22 +01003968static int stats_dump_one_line(const struct field *stats, unsigned int flags, struct proxy *px, struct appctx *appctx)
3969{
Willy Tarreau508a63f2016-01-11 15:28:40 +01003970 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN))
3971 flags |= ST_SHOWADMIN;
Willy Tarreau501f6022016-01-08 17:37:22 +01003972
3973 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
Willy Tarreau60600742016-01-11 15:32:30 +01003974 return stats_dump_fields_html(&trash, stats, flags);
Willy Tarreau1e62df92016-01-11 18:57:53 +01003975 else if (appctx->ctx.stats.flags & STAT_FMT_TYPED)
3976 return stats_dump_fields_typed(&trash, stats);
Willy Tarreau501f6022016-01-08 17:37:22 +01003977 else
3978 return stats_dump_fields_csv(&trash, stats);
3979}
3980
Thierry Fournier23d2d642016-03-25 08:20:11 +01003981/* Fill <stats> with the frontend statistics. <stats> is
3982 * preallocated array of length <len>. The length of the array
3983 * must be at least ST_F_TOTAL_FIELDS. If this length is less then
3984 * this value, the function returns 0, otherwise, it returns 1.
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003985 */
Thierry Fournier23d2d642016-03-25 08:20:11 +01003986int stats_fill_fe_stats(struct proxy *px, struct field *stats, int len)
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003987{
Thierry Fournier23d2d642016-03-25 08:20:11 +01003988 if (len < ST_F_TOTAL_FIELDS)
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003989 return 0;
3990
Thierry Fournier23d2d642016-03-25 08:20:11 +01003991 memset(stats, 0, sizeof(*stats) * len);
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003992
3993 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
3994 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, "FRONTEND");
Willy Tarreauf8211df2016-01-11 14:09:38 +01003995 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003996 stats[ST_F_SCUR] = mkf_u32(0, px->feconn);
3997 stats[ST_F_SMAX] = mkf_u32(FN_MAX, px->fe_counters.conn_max);
3998 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, px->maxconn);
3999 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, px->fe_counters.cum_sess);
4000 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, px->fe_counters.bytes_in);
4001 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, px->fe_counters.bytes_out);
4002 stats[ST_F_DREQ] = mkf_u64(FN_COUNTER, px->fe_counters.denied_req);
4003 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, px->fe_counters.denied_resp);
4004 stats[ST_F_EREQ] = mkf_u64(FN_COUNTER, px->fe_counters.failed_req);
4005 stats[ST_F_STATUS] = mkf_str(FO_STATUS, px->state == PR_STREADY ? "OPEN" : px->state == PR_STFULL ? "FULL" : "STOP");
4006 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
4007 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
4008 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, 0);
4009 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_FE);
4010 stats[ST_F_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_sess_per_sec));
4011 stats[ST_F_RATE_LIM] = mkf_u32(FO_CONFIG|FN_LIMIT, px->fe_sps_lim);
4012 stats[ST_F_RATE_MAX] = mkf_u32(FN_MAX, px->fe_counters.sps_max);
4013
4014 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
4015 if (px->mode == PR_MODE_HTTP) {
4016 stats[ST_F_HRSP_1XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[1]);
4017 stats[ST_F_HRSP_2XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[2]);
4018 stats[ST_F_HRSP_3XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[3]);
4019 stats[ST_F_HRSP_4XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[4]);
4020 stats[ST_F_HRSP_5XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[5]);
4021 stats[ST_F_HRSP_OTHER] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[0]);
Willy Tarreau5b9bdff2016-01-11 14:40:47 +01004022 stats[ST_F_INTERCEPTED] = mkf_u64(FN_COUNTER, px->fe_counters.intercepted_req);
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004023 }
4024
4025 /* requests : req_rate, req_rate_max, req_tot, */
4026 stats[ST_F_REQ_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_req_per_sec));
4027 stats[ST_F_REQ_RATE_MAX] = mkf_u32(FN_MAX, px->fe_counters.p.http.rps_max);
4028 stats[ST_F_REQ_TOT] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cum_req);
4029
4030 /* compression: in, out, bypassed, responses */
4031 stats[ST_F_COMP_IN] = mkf_u64(FN_COUNTER, px->fe_counters.comp_in);
4032 stats[ST_F_COMP_OUT] = mkf_u64(FN_COUNTER, px->fe_counters.comp_out);
4033 stats[ST_F_COMP_BYP] = mkf_u64(FN_COUNTER, px->fe_counters.comp_byp);
4034 stats[ST_F_COMP_RSP] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.comp_rsp);
4035
Willy Tarreauc73810f2016-01-11 13:52:04 +01004036 /* connections : conn_rate, conn_rate_max, conn_tot, conn_max */
4037 stats[ST_F_CONN_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_conn_per_sec));
4038 stats[ST_F_CONN_RATE_MAX] = mkf_u32(FN_MAX, px->fe_counters.cps_max);
4039 stats[ST_F_CONN_TOT] = mkf_u64(FN_COUNTER, px->fe_counters.cum_conn);
4040
Thierry Fournier23d2d642016-03-25 08:20:11 +01004041 return 1;
4042}
4043
4044/* Dumps a frontend's line to the trash for the current proxy <px> and uses
4045 * the state from stream interface <si>. The caller is responsible for clearing
4046 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
4047 */
4048static int stats_dump_fe_stats(struct stream_interface *si, struct proxy *px)
4049{
4050 struct appctx *appctx = __objt_appctx(si->end);
4051
4052 if (!(px->cap & PR_CAP_FE))
4053 return 0;
4054
4055 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_FE)))
4056 return 0;
4057
4058 if (!stats_fill_fe_stats(px, stats, ST_F_TOTAL_FIELDS))
4059 return 0;
4060
Willy Tarreau501f6022016-01-08 17:37:22 +01004061 return stats_dump_one_line(stats, 0, px, appctx);
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004062}
4063
Thierry Fournierc4456852016-03-25 08:20:49 +01004064/* Fill <stats> with the listener statistics. <stats> is
4065 * preallocated array of length <len>. The length of the array
4066 * must be at least ST_F_TOTAL_FIELDS. If this length is less
4067 * then this value, the function returns 0, otherwise, it
4068 * returns 1. <flags> can take the value ST_SHLGNDS.
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004069 */
Thierry Fournierc4456852016-03-25 08:20:49 +01004070int stats_fill_li_stats(struct proxy *px, struct listener *l, int flags,
4071 struct field *stats, int len)
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004072{
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004073 struct chunk *out = get_trash_chunk();
4074
Thierry Fournierc4456852016-03-25 08:20:49 +01004075 if (len < ST_F_TOTAL_FIELDS)
4076 return 0;
4077
Thierry Fournierac9d4672016-03-25 08:43:46 +01004078 if (!l->counters)
4079 return 0;
4080
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004081 chunk_reset(out);
Thierry Fournierc4456852016-03-25 08:20:49 +01004082 memset(stats, 0, sizeof(*stats) * len);
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004083
4084 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
4085 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, l->name);
Willy Tarreauf8211df2016-01-11 14:09:38 +01004086 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004087 stats[ST_F_SCUR] = mkf_u32(0, l->nbconn);
4088 stats[ST_F_SMAX] = mkf_u32(FN_MAX, l->counters->conn_max);
4089 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, l->maxconn);
4090 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, l->counters->cum_conn);
4091 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, l->counters->bytes_in);
4092 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, l->counters->bytes_out);
4093 stats[ST_F_DREQ] = mkf_u64(FN_COUNTER, l->counters->denied_req);
4094 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, l->counters->denied_resp);
4095 stats[ST_F_EREQ] = mkf_u64(FN_COUNTER, l->counters->failed_req);
4096 stats[ST_F_STATUS] = mkf_str(FO_STATUS, (l->nbconn < l->maxconn) ? (l->state == LI_LIMITED) ? "WAITING" : "OPEN" : "FULL");
4097 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
4098 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
4099 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, l->luid);
4100 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_SO);
4101
4102 if (flags & ST_SHLGNDS) {
4103 char str[INET6_ADDRSTRLEN];
4104 int port;
4105
4106 port = get_host_port(&l->addr);
4107 switch (addr_to_str(&l->addr, str, sizeof(str))) {
4108 case AF_INET:
4109 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
4110 chunk_appendf(out, "%s:%d", str, port);
4111 break;
4112 case AF_INET6:
4113 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
4114 chunk_appendf(out, "[%s]:%d", str, port);
4115 break;
4116 case AF_UNIX:
4117 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, "unix");
4118 break;
4119 case -1:
4120 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
4121 chunk_strcat(out, strerror(errno));
4122 break;
4123 default: /* address family not supported */
4124 break;
4125 }
4126 }
4127
Thierry Fournierc4456852016-03-25 08:20:49 +01004128 return 1;
4129}
4130
4131/* Dumps a line for listener <l> and proxy <px> to the trash and uses the state
4132 * from stream interface <si>, and stats flags <flags>. The caller is responsible
4133 * for clearing the trash if needed. Returns non-zero if it emits anything, zero
4134 * otherwise.
4135 */
4136static int stats_dump_li_stats(struct stream_interface *si, struct proxy *px, struct listener *l, int flags)
4137{
4138 struct appctx *appctx = __objt_appctx(si->end);
4139
4140 if (!stats_fill_li_stats(px, l, flags, stats, ST_F_TOTAL_FIELDS))
4141 return 0;
4142
Willy Tarreau501f6022016-01-08 17:37:22 +01004143 return stats_dump_one_line(stats, flags, px, appctx);
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004144}
4145
4146enum srv_stats_state {
4147 SRV_STATS_STATE_DOWN = 0,
4148 SRV_STATS_STATE_DOWN_AGENT,
4149 SRV_STATS_STATE_GOING_UP,
4150 SRV_STATS_STATE_UP_GOING_DOWN,
4151 SRV_STATS_STATE_UP,
4152 SRV_STATS_STATE_NOLB_GOING_DOWN,
Simon Horman4d2eab62015-04-23 14:51:26 +09004153 SRV_STATS_STATE_NOLB,
4154 SRV_STATS_STATE_DRAIN_GOING_DOWN,
4155 SRV_STATS_STATE_DRAIN,
Simon Hormanb167b6b2015-04-23 14:51:29 +09004156 SRV_STATS_STATE_DRAIN_AGENT,
Simon Horman4d2eab62015-04-23 14:51:26 +09004157 SRV_STATS_STATE_NO_CHECK,
4158
4159 SRV_STATS_STATE_COUNT, /* Must be last */
4160};
4161
Willy Tarreau2b96cf12016-01-06 19:25:38 +01004162static const char *srv_hlt_st[SRV_STATS_STATE_COUNT] = {
4163 [SRV_STATS_STATE_DOWN] = "DOWN",
4164 [SRV_STATS_STATE_DOWN_AGENT] = "DOWN (agent)",
4165 [SRV_STATS_STATE_GOING_UP] = "DOWN %d/%d",
4166 [SRV_STATS_STATE_UP_GOING_DOWN] = "UP %d/%d",
4167 [SRV_STATS_STATE_UP] = "UP",
4168 [SRV_STATS_STATE_NOLB_GOING_DOWN] = "NOLB %d/%d",
4169 [SRV_STATS_STATE_NOLB] = "NOLB",
4170 [SRV_STATS_STATE_DRAIN_GOING_DOWN] = "DRAIN %d/%d",
4171 [SRV_STATS_STATE_DRAIN] = "DRAIN",
4172 [SRV_STATS_STATE_DRAIN_AGENT] = "DRAIN (agent)",
4173 [SRV_STATS_STATE_NO_CHECK] = "no check"
4174};
4175
Thierry Fournier61fe6c02016-03-25 08:21:21 +01004176/* Fill <stats> with the server statistics. <stats> is
4177 * preallocated array of length <len>. The length of the array
4178 * must be at least ST_F_TOTAL_FIELDS. If this length is less
4179 * then this value, the function returns 0, otherwise, it
4180 * returns 1. <flags> can take the value ST_SHLGNDS.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004181 */
Thierry Fournier61fe6c02016-03-25 08:21:21 +01004182int stats_fill_sv_stats(struct proxy *px, struct server *sv, int flags,
4183 struct field *stats, int len)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004184{
Willy Tarreau32091232014-05-16 13:52:00 +02004185 struct server *via, *ref;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004186 char str[INET6_ADDRSTRLEN];
Willy Tarreau2b96cf12016-01-06 19:25:38 +01004187 struct chunk *out = get_trash_chunk();
Willy Tarreauba2f2642016-01-07 09:54:40 +01004188 enum srv_stats_state state;
Willy Tarreau2b96cf12016-01-06 19:25:38 +01004189 char *fld_status;
Thierry Fournier61fe6c02016-03-25 08:21:21 +01004190
4191 if (len < ST_F_TOTAL_FIELDS)
4192 return 0;
4193
4194 memset(stats, 0, sizeof(*stats) * len);
4195
Willy Tarreau32091232014-05-16 13:52:00 +02004196 /* we have "via" which is the tracked server as described in the configuration,
4197 * and "ref" which is the checked server and the end of the chain.
4198 */
4199 via = sv->track ? sv->track : sv;
4200 ref = via;
4201 while (ref->track)
4202 ref = ref->track;
4203
Willy Tarreauba2f2642016-01-07 09:54:40 +01004204 if (sv->state == SRV_ST_RUNNING || sv->state == SRV_ST_STARTING) {
4205 if ((ref->check.state & CHK_ST_ENABLED) &&
4206 (ref->check.health < ref->check.rise + ref->check.fall - 1)) {
4207 state = SRV_STATS_STATE_UP_GOING_DOWN;
Willy Tarreauba2f2642016-01-07 09:54:40 +01004208 } else {
4209 state = SRV_STATS_STATE_UP;
Willy Tarreauba2f2642016-01-07 09:54:40 +01004210 }
4211
Willy Tarreauba2f2642016-01-07 09:54:40 +01004212 if (sv->admin & SRV_ADMF_DRAIN) {
4213 if (ref->agent.state & CHK_ST_ENABLED)
4214 state = SRV_STATS_STATE_DRAIN_AGENT;
4215 else if (state == SRV_STATS_STATE_UP_GOING_DOWN)
4216 state = SRV_STATS_STATE_DRAIN_GOING_DOWN;
4217 else
4218 state = SRV_STATS_STATE_DRAIN;
4219 }
4220
4221 if (state == SRV_STATS_STATE_UP && !(ref->check.state & CHK_ST_ENABLED)) {
4222 state = SRV_STATS_STATE_NO_CHECK;
Willy Tarreauba2f2642016-01-07 09:54:40 +01004223 }
4224 }
4225 else if (sv->state == SRV_ST_STOPPING) {
4226 if ((!(sv->check.state & CHK_ST_ENABLED) && !sv->track) ||
4227 (ref->check.health == ref->check.rise + ref->check.fall - 1)) {
4228 state = SRV_STATS_STATE_NOLB;
Willy Tarreauba2f2642016-01-07 09:54:40 +01004229 } else {
4230 state = SRV_STATS_STATE_NOLB_GOING_DOWN;
Willy Tarreauba2f2642016-01-07 09:54:40 +01004231 }
4232 }
4233 else { /* stopped */
4234 if ((ref->agent.state & CHK_ST_ENABLED) && !ref->agent.health) {
4235 state = SRV_STATS_STATE_DOWN_AGENT;
Willy Tarreauba2f2642016-01-07 09:54:40 +01004236 } else if ((ref->check.state & CHK_ST_ENABLED) && !ref->check.health) {
4237 state = SRV_STATS_STATE_DOWN; /* DOWN */
Willy Tarreauba2f2642016-01-07 09:54:40 +01004238 } else if ((ref->agent.state & CHK_ST_ENABLED) || (ref->check.state & CHK_ST_ENABLED)) {
4239 state = SRV_STATS_STATE_GOING_UP;
Willy Tarreauba2f2642016-01-07 09:54:40 +01004240 } else {
4241 state = SRV_STATS_STATE_DOWN; /* DOWN, unchecked */
Willy Tarreauba2f2642016-01-07 09:54:40 +01004242 }
4243 }
4244
Willy Tarreau2b96cf12016-01-06 19:25:38 +01004245 chunk_reset(out);
Willy Tarreau2b96cf12016-01-06 19:25:38 +01004246
4247 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
4248 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, sv->id);
Willy Tarreauf8211df2016-01-11 14:09:38 +01004249 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
Willy Tarreau2b96cf12016-01-06 19:25:38 +01004250 stats[ST_F_QCUR] = mkf_u32(0, sv->nbpend);
4251 stats[ST_F_QMAX] = mkf_u32(FN_MAX, sv->counters.nbpend_max);
4252 stats[ST_F_SCUR] = mkf_u32(0, sv->cur_sess);
4253 stats[ST_F_SMAX] = mkf_u32(FN_MAX, sv->counters.cur_sess_max);
4254
4255 if (sv->maxconn)
4256 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, sv->maxconn);
4257
4258 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, sv->counters.cum_sess);
4259 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, sv->counters.bytes_in);
4260 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, sv->counters.bytes_out);
4261 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, sv->counters.failed_secu);
4262 stats[ST_F_ECON] = mkf_u64(FN_COUNTER, sv->counters.failed_conns);
4263 stats[ST_F_ERESP] = mkf_u64(FN_COUNTER, sv->counters.failed_resp);
4264 stats[ST_F_WRETR] = mkf_u64(FN_COUNTER, sv->counters.retries);
4265 stats[ST_F_WREDIS] = mkf_u64(FN_COUNTER, sv->counters.redispatches);
4266
4267 /* status */
4268 fld_status = chunk_newstr(out);
4269 if (sv->admin & SRV_ADMF_IMAINT)
4270 chunk_appendf(out, "MAINT (via %s/%s)", via->proxy->id, via->id);
4271 else if (sv->admin & SRV_ADMF_MAINT)
4272 chunk_appendf(out, "MAINT");
4273 else
4274 chunk_appendf(out,
4275 srv_hlt_st[state],
4276 (ref->state != SRV_ST_STOPPED) ? (ref->check.health - ref->check.rise + 1) : (ref->check.health),
4277 (ref->state != SRV_ST_STOPPED) ? (ref->check.fall) : (ref->check.rise));
4278
4279 stats[ST_F_STATUS] = mkf_str(FO_STATUS, fld_status);
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004280 stats[ST_F_LASTCHG] = mkf_u32(FN_AGE, now.tv_sec - sv->last_change);
4281 stats[ST_F_WEIGHT] = mkf_u32(FN_AVG, (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv);
4282 stats[ST_F_ACT] = mkf_u32(FO_STATUS, (sv->flags & SRV_F_BACKUP) ? 0 : 1);
4283 stats[ST_F_BCK] = mkf_u32(FO_STATUS, (sv->flags & SRV_F_BACKUP) ? 1 : 0);
Willy Tarreau91861262007-10-17 17:06:05 +02004284
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004285 /* check failures: unique, fatal; last change, total downtime */
4286 if (sv->check.state & CHK_ST_ENABLED) {
4287 stats[ST_F_CHKFAIL] = mkf_u64(FN_COUNTER, sv->counters.failed_checks);
4288 stats[ST_F_CHKDOWN] = mkf_u64(FN_COUNTER, sv->counters.down_trans);
4289 stats[ST_F_DOWNTIME] = mkf_u32(FN_COUNTER, srv_downtime(sv));
4290 }
Willy Tarreau164d4a92016-01-06 19:48:21 +01004291
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004292 if (sv->maxqueue)
4293 stats[ST_F_QLIMIT] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->maxqueue);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004294
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004295 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
4296 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
4297 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, sv->puid);
Willy Tarreau91861262007-10-17 17:06:05 +02004298
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004299 if (sv->state == SRV_ST_STARTING && !server_is_draining(sv))
4300 stats[ST_F_THROTTLE] = mkf_u32(FN_AVG, server_throttle_rate(sv));
Willy Tarreauf5b1cc32014-06-17 12:20:59 +02004301
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004302 stats[ST_F_LBTOT] = mkf_u64(FN_COUNTER, sv->counters.cum_lbconn);
Willy Tarreau91861262007-10-17 17:06:05 +02004303
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004304 if (sv->track) {
4305 char *fld_track = chunk_newstr(out);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004306
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004307 chunk_appendf(out, "%s/%s", sv->track->proxy->id, sv->track->id);
4308 stats[ST_F_TRACKED] = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, fld_track);
4309 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004310
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004311 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_SV);
4312 stats[ST_F_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&sv->sess_per_sec));
4313 stats[ST_F_RATE_MAX] = mkf_u32(FN_MAX, sv->counters.sps_max);
Willy Tarreau164d4a92016-01-06 19:48:21 +01004314
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004315 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) {
4316 const char *fld_chksts;
Willy Tarreau164d4a92016-01-06 19:48:21 +01004317
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004318 fld_chksts = chunk_newstr(out);
4319 chunk_strcat(out, "* "); // for check in progress
4320 chunk_strcat(out, get_check_status_info(sv->check.status));
4321 if (!(sv->check.state & CHK_ST_INPROGRESS))
4322 fld_chksts += 2; // skip "* "
4323 stats[ST_F_CHECK_STATUS] = mkf_str(FN_OUTPUT, fld_chksts);
Willy Tarreau91861262007-10-17 17:06:05 +02004324
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004325 if (sv->check.status >= HCHK_STATUS_L57DATA)
4326 stats[ST_F_CHECK_CODE] = mkf_u32(FN_OUTPUT, sv->check.code);
Willy Tarreaucf2924b2014-05-23 12:15:15 +02004327
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004328 if (sv->check.status >= HCHK_STATUS_CHECKED)
4329 stats[ST_F_CHECK_DURATION] = mkf_u64(FN_DURATION, sv->check.duration);
Willy Tarreaucf2924b2014-05-23 12:15:15 +02004330
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004331 stats[ST_F_CHECK_DESC] = mkf_str(FN_OUTPUT, get_check_status_description(sv->check.status));
4332 stats[ST_F_LAST_CHK] = mkf_str(FN_OUTPUT, sv->check.desc);
4333 stats[ST_F_CHECK_RISE] = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.rise);
4334 stats[ST_F_CHECK_FALL] = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.fall);
4335 stats[ST_F_CHECK_HEALTH] = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.health);
4336 }
Willy Tarreaucf2924b2014-05-23 12:15:15 +02004337
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004338 if ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) {
4339 const char *fld_chksts;
Willy Tarreaudd7354b2016-01-08 13:47:26 +01004340
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004341 fld_chksts = chunk_newstr(out);
4342 chunk_strcat(out, "* "); // for check in progress
4343 chunk_strcat(out, get_check_status_info(sv->agent.status));
4344 if (!(sv->agent.state & CHK_ST_INPROGRESS))
4345 fld_chksts += 2; // skip "* "
4346 stats[ST_F_AGENT_STATUS] = mkf_str(FN_OUTPUT, fld_chksts);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004347
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004348 if (sv->agent.status >= HCHK_STATUS_L57DATA)
4349 stats[ST_F_AGENT_CODE] = mkf_u32(FN_OUTPUT, sv->agent.code);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004350
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004351 if (sv->agent.status >= HCHK_STATUS_CHECKED)
4352 stats[ST_F_AGENT_DURATION] = mkf_u64(FN_DURATION, sv->agent.duration);
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01004353
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004354 stats[ST_F_AGENT_DESC] = mkf_str(FN_OUTPUT, get_check_status_description(sv->agent.status));
4355 stats[ST_F_LAST_AGT] = mkf_str(FN_OUTPUT, sv->agent.desc);
4356 stats[ST_F_AGENT_RISE] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.rise);
4357 stats[ST_F_AGENT_FALL] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.fall);
4358 stats[ST_F_AGENT_HEALTH] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.health);
4359 }
Willy Tarreaudd7354b2016-01-08 13:47:26 +01004360
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004361 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
4362 if (px->mode == PR_MODE_HTTP) {
4363 stats[ST_F_HRSP_1XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[1]);
4364 stats[ST_F_HRSP_2XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[2]);
4365 stats[ST_F_HRSP_3XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[3]);
4366 stats[ST_F_HRSP_4XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[4]);
4367 stats[ST_F_HRSP_5XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[5]);
4368 stats[ST_F_HRSP_OTHER] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[0]);
4369 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004370
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004371 if (ref->observe)
4372 stats[ST_F_HANAFAIL] = mkf_u64(FN_COUNTER, sv->counters.failed_hana);
Willy Tarreau164d4a92016-01-06 19:48:21 +01004373
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004374 stats[ST_F_CLI_ABRT] = mkf_u64(FN_COUNTER, sv->counters.cli_aborts);
4375 stats[ST_F_SRV_ABRT] = mkf_u64(FN_COUNTER, sv->counters.srv_aborts);
4376 stats[ST_F_LASTSESS] = mkf_s32(FN_AGE, srv_lastsession(sv));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004377
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004378 stats[ST_F_QTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.q_time, TIME_STATS_SAMPLES));
4379 stats[ST_F_CTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.c_time, TIME_STATS_SAMPLES));
4380 stats[ST_F_RTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.d_time, TIME_STATS_SAMPLES));
4381 stats[ST_F_TTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.t_time, TIME_STATS_SAMPLES));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004382
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004383 if (flags & ST_SHLGNDS) {
4384 switch (addr_to_str(&sv->addr, str, sizeof(str))) {
4385 case AF_INET:
4386 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
4387 chunk_appendf(out, "%s:%d", str, get_host_port(&sv->addr));
4388 break;
4389 case AF_INET6:
4390 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
4391 chunk_appendf(out, "[%s]:%d", str, get_host_port(&sv->addr));
4392 break;
4393 case AF_UNIX:
4394 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, "unix");
4395 break;
4396 case -1:
4397 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
4398 chunk_strcat(out, strerror(errno));
4399 break;
4400 default: /* address family not supported */
4401 break;
Willy Tarreauf4659942013-11-28 10:50:06 +01004402 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004403
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004404 if (sv->cookie)
4405 stats[ST_F_COOKIE] = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, sv->cookie);
4406 }
4407
Thierry Fournier61fe6c02016-03-25 08:21:21 +01004408 return 1;
4409}
4410
4411/* Dumps a line for server <sv> and proxy <px> to the trash and uses the state
4412 * from stream interface <si>, stats flags <flags>, and server state <state>.
4413 * The caller is responsible for clearing the trash if needed. Returns non-zero
4414 * if it emits anything, zero otherwise.
4415 */
4416static int stats_dump_sv_stats(struct stream_interface *si, struct proxy *px, int flags, struct server *sv)
4417{
4418 struct appctx *appctx = __objt_appctx(si->end);
4419
4420 if (!stats_fill_sv_stats(px, sv, flags, stats, ST_F_TOTAL_FIELDS))
4421 return 0;
4422
Willy Tarreau501f6022016-01-08 17:37:22 +01004423 return stats_dump_one_line(stats, flags, px, appctx);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004424}
4425
Thierry Fournierd0a56c22016-03-25 08:21:51 +01004426/* Fill <stats> with the backend statistics. <stats> is
4427 * preallocated array of length <len>. The length of the array
4428 * must be at least ST_F_TOTAL_FIELDS. If this length is less
4429 * then this value, the function returns 0, otherwise, it
4430 * returns 1. <flags> can take the value ST_SHLGNDS.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004431 */
Thierry Fournierd0a56c22016-03-25 08:21:51 +01004432int stats_fill_be_stats(struct proxy *px, int flags, struct field *stats, int len)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004433{
Thierry Fournierd0a56c22016-03-25 08:21:51 +01004434 if (len < ST_F_TOTAL_FIELDS)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004435 return 0;
4436
Thierry Fournierd0a56c22016-03-25 08:21:51 +01004437 memset(stats, 0, sizeof(*stats) * len);
Willy Tarreauf6eecbe2016-01-06 16:14:50 +01004438
4439 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
4440 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, "BACKEND");
Willy Tarreauf8211df2016-01-11 14:09:38 +01004441 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
Willy Tarreauf6eecbe2016-01-06 16:14:50 +01004442 stats[ST_F_QCUR] = mkf_u32(0, px->nbpend);
4443 stats[ST_F_QMAX] = mkf_u32(FN_MAX, px->be_counters.nbpend_max);
4444 stats[ST_F_SCUR] = mkf_u32(FO_CONFIG|FN_LIMIT, px->beconn);
4445 stats[ST_F_SMAX] = mkf_u32(FN_MAX, px->be_counters.conn_max);
4446 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, px->fullconn);
4447 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, px->be_counters.cum_conn);
4448 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, px->be_counters.bytes_in);
4449 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, px->be_counters.bytes_out);
4450 stats[ST_F_DREQ] = mkf_u64(FN_COUNTER, px->be_counters.denied_req);
4451 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, px->be_counters.denied_resp);
4452 stats[ST_F_ECON] = mkf_u64(FN_COUNTER, px->be_counters.failed_conns);
4453 stats[ST_F_ERESP] = mkf_u64(FN_COUNTER, px->be_counters.failed_resp);
4454 stats[ST_F_WRETR] = mkf_u64(FN_COUNTER, px->be_counters.retries);
4455 stats[ST_F_WREDIS] = mkf_u64(FN_COUNTER, px->be_counters.redispatches);
4456 stats[ST_F_STATUS] = mkf_str(FO_STATUS, (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" : "DOWN");
4457 stats[ST_F_WEIGHT] = mkf_u32(FN_AVG, (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv);
4458 stats[ST_F_ACT] = mkf_u32(0, px->srv_act);
4459 stats[ST_F_BCK] = mkf_u32(0, px->srv_bck);
4460 stats[ST_F_CHKDOWN] = mkf_u64(FN_COUNTER, px->down_trans);
4461 stats[ST_F_LASTCHG] = mkf_u32(FN_AGE, now.tv_sec - px->last_change);
Willy Tarreau7344f472016-01-11 12:04:02 +01004462 if (px->srv)
4463 stats[ST_F_DOWNTIME] = mkf_u32(FN_COUNTER, be_downtime(px));
4464
Willy Tarreauf6eecbe2016-01-06 16:14:50 +01004465 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
4466 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
4467 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, 0);
4468 stats[ST_F_LBTOT] = mkf_u64(FN_COUNTER, px->be_counters.cum_lbconn);
4469 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_BE);
4470 stats[ST_F_RATE] = mkf_u32(0, read_freq_ctr(&px->be_sess_per_sec));
4471 stats[ST_F_RATE_MAX] = mkf_u32(0, px->be_counters.sps_max);
Willy Tarreauf1516d92016-01-11 14:48:36 +01004472
Willy Tarreaue4847c62016-01-08 15:43:54 +01004473 if (flags & ST_SHLGNDS) {
4474 if (px->cookie_name)
4475 stats[ST_F_COOKIE] = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, px->cookie_name);
Willy Tarreauf1516d92016-01-11 14:48:36 +01004476 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 +01004477 }
Willy Tarreauf6eecbe2016-01-06 16:14:50 +01004478
4479 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
4480 if (px->mode == PR_MODE_HTTP) {
4481 stats[ST_F_REQ_TOT] = mkf_u64(FN_COUNTER, px->be_counters.p.http.cum_req);
4482 stats[ST_F_HRSP_1XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[1]);
4483 stats[ST_F_HRSP_2XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[2]);
4484 stats[ST_F_HRSP_3XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[3]);
4485 stats[ST_F_HRSP_4XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[4]);
4486 stats[ST_F_HRSP_5XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[5]);
4487 stats[ST_F_HRSP_OTHER] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[0]);
Willy Tarreau5b9bdff2016-01-11 14:40:47 +01004488 stats[ST_F_INTERCEPTED] = mkf_u64(FN_COUNTER, px->be_counters.intercepted_req);
Willy Tarreauf6eecbe2016-01-06 16:14:50 +01004489 }
4490
4491 stats[ST_F_CLI_ABRT] = mkf_u64(FN_COUNTER, px->be_counters.cli_aborts);
4492 stats[ST_F_SRV_ABRT] = mkf_u64(FN_COUNTER, px->be_counters.srv_aborts);
4493
4494 /* compression: in, out, bypassed, responses */
4495 stats[ST_F_COMP_IN] = mkf_u64(FN_COUNTER, px->be_counters.comp_in);
4496 stats[ST_F_COMP_OUT] = mkf_u64(FN_COUNTER, px->be_counters.comp_out);
4497 stats[ST_F_COMP_BYP] = mkf_u64(FN_COUNTER, px->be_counters.comp_byp);
4498 stats[ST_F_COMP_RSP] = mkf_u64(FN_COUNTER, px->be_counters.p.http.comp_rsp);
4499 stats[ST_F_LASTSESS] = mkf_s32(FN_AGE, be_lastsession(px));
4500
4501 stats[ST_F_QTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.q_time, TIME_STATS_SAMPLES));
4502 stats[ST_F_CTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.c_time, TIME_STATS_SAMPLES));
4503 stats[ST_F_RTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.d_time, TIME_STATS_SAMPLES));
4504 stats[ST_F_TTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.t_time, TIME_STATS_SAMPLES));
4505
Thierry Fournierd0a56c22016-03-25 08:21:51 +01004506 return 1;
4507}
4508
4509/* Dumps a line for backend <px> to the trash for and uses the state from stream
4510 * interface <si> and stats flags <flags>. The caller is responsible for clearing
4511 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
4512 */
4513static int stats_dump_be_stats(struct stream_interface *si, struct proxy *px, int flags)
4514{
4515 struct appctx *appctx = __objt_appctx(si->end);
4516
4517 if (!(px->cap & PR_CAP_BE))
4518 return 0;
4519
4520 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_BE)))
4521 return 0;
4522
4523 if (!stats_fill_be_stats(px, flags, stats, ST_F_TOTAL_FIELDS))
4524 return 0;
4525
Willy Tarreau501f6022016-01-08 17:37:22 +01004526 return stats_dump_one_line(stats, flags, px, appctx);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004527}
4528
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004529/* Dumps the HTML table header for proxy <px> to the trash for and uses the state from
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004530 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004531 * for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004532 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004533static void stats_dump_html_px_hdr(struct stream_interface *si, struct proxy *px, struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004534{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004535 struct appctx *appctx = __objt_appctx(si->end);
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004536 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
4537
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004538 if (px->cap & PR_CAP_BE && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004539 /* A form to enable/disable this proxy servers */
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004540
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004541 /* 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 +02004542 scope_txt[0] = 0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004543 if (appctx->ctx.stats.scope_len) {
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004544 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau4e4292b2014-11-28 12:18:45 +01004545 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 +01004546 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004547 }
4548
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004549 chunk_appendf(&trash,
Jeff Buchbinder2dbbf4d2014-08-29 15:10:08 -05004550 "<form method=\"post\">");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004551 }
4552
4553 /* print a new table */
4554 chunk_appendf(&trash,
4555 "<table class=\"tbl\" width=\"100%%\">\n"
4556 "<tr class=\"titre\">"
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01004557 "<th class=\"pxname\" width=\"10%%\">");
4558
4559 chunk_appendf(&trash,
4560 "<a name=\"%s\"></a>%s"
4561 "<a class=px href=\"#%s\">%s</a>",
4562 px->id,
4563 (uri->flags & ST_SHLGNDS) ? "<u>":"",
4564 px->id, px->id);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004565
4566 if (uri->flags & ST_SHLGNDS) {
4567 /* cap, mode, id */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02004568 chunk_appendf(&trash, "<div class=tips>cap: %s, mode: %s, id: %d",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004569 proxy_cap_str(px->cap), proxy_mode_str(px->mode),
4570 px->uuid);
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01004571 chunk_appendf(&trash, "</div>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004572 }
4573
4574 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01004575 "%s</th>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004576 "<th class=\"%s\" width=\"90%%\">%s</th>"
4577 "</tr>\n"
4578 "</table>\n"
4579 "<table class=\"tbl\" width=\"100%%\">\n"
4580 "<tr class=\"titre\">",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004581 (uri->flags & ST_SHLGNDS) ? "</u>":"",
4582 px->desc ? "desc" : "empty", px->desc ? px->desc : "");
4583
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004584 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004585 /* Column heading for Enable or Disable server */
4586 chunk_appendf(&trash, "<th rowspan=2 width=1></th>");
Willy Tarreau91861262007-10-17 17:06:05 +02004587 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004588
4589 chunk_appendf(&trash,
4590 "<th rowspan=2></th>"
4591 "<th colspan=3>Queue</th>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05004592 "<th colspan=3>Session rate</th><th colspan=6>Sessions</th>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004593 "<th colspan=2>Bytes</th><th colspan=2>Denied</th>"
4594 "<th colspan=3>Errors</th><th colspan=2>Warnings</th>"
4595 "<th colspan=9>Server</th>"
4596 "</tr>\n"
4597 "<tr class=\"titre\">"
4598 "<th>Cur</th><th>Max</th><th>Limit</th>"
4599 "<th>Cur</th><th>Max</th><th>Limit</th><th>Cur</th><th>Max</th>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05004600 "<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 +01004601 "<th>Req</th><th>Resp</th><th>Req</th><th>Conn</th>"
4602 "<th>Resp</th><th>Retr</th><th>Redis</th>"
4603 "<th>Status</th><th>LastChk</th><th>Wght</th><th>Act</th>"
4604 "<th>Bck</th><th>Chk</th><th>Dwn</th><th>Dwntme</th>"
4605 "<th>Thrtle</th>\n"
4606 "</tr>");
Willy Tarreau91861262007-10-17 17:06:05 +02004607}
4608
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004609/* Dumps the HTML table trailer for proxy <px> to the trash for and uses the state from
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004610 * stream interface <si>. The caller is responsible for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004611 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004612static void stats_dump_html_px_end(struct stream_interface *si, struct proxy *px)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004613{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004614 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004615 chunk_appendf(&trash, "</table>");
4616
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004617 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004618 /* close the form used to enable/disable this proxy servers */
4619 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004620 "Choose the action to perform on the checked servers : "
4621 "<select name=action>"
4622 "<option value=\"\"></option>"
Willy Tarreaued7df902014-05-22 18:04:49 +02004623 "<option value=\"ready\">Set state to READY</option>"
4624 "<option value=\"drain\">Set state to DRAIN</option>"
Marco Corte8c27bca2014-07-02 17:49:34 +02004625 "<option value=\"maint\">Set state to MAINT</option>"
Willy Tarreau248a60e2014-05-23 14:59:48 +02004626 "<option value=\"dhlth\">Health: disable checks</option>"
4627 "<option value=\"ehlth\">Health: enable checks</option>"
4628 "<option value=\"hrunn\">Health: force UP</option>"
4629 "<option value=\"hnolb\">Health: force NOLB</option>"
4630 "<option value=\"hdown\">Health: force DOWN</option>"
4631 "<option value=\"dagent\">Agent: disable checks</option>"
4632 "<option value=\"eagent\">Agent: enable checks</option>"
4633 "<option value=\"arunn\">Agent: force UP</option>"
4634 "<option value=\"adown\">Agent: force DOWN</option>"
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004635 "<option value=\"shutdown\">Kill Sessions</option>"
4636 "</select>"
4637 "<input type=\"hidden\" name=\"b\" value=\"#%d\">"
4638 "&nbsp;<input type=\"submit\" value=\"Apply\">"
4639 "</form>",
4640 px->uuid);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004641 }
4642
4643 chunk_appendf(&trash, "<p>\n");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004644}
Willy Tarreau91861262007-10-17 17:06:05 +02004645
4646/*
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004647 * Dumps statistics for a proxy. The output is sent to the stream interface's
4648 * input buffer. Returns 0 if it had to stop dumping data because of lack of
4649 * buffer space, or non-zero if everything completed. This function is used
4650 * both by the CLI and the HTTP entry points, and is able to dump the output
4651 * in HTML or CSV formats. If the later, <uri> must be NULL.
Willy Tarreau91861262007-10-17 17:06:05 +02004652 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004653static int stats_dump_proxy_to_buffer(struct stream_interface *si, struct proxy *px, struct uri_auth *uri)
Willy Tarreau91861262007-10-17 17:06:05 +02004654{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004655 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau87b09662015-04-03 00:22:06 +02004656 struct stream *s = si_strm(si);
Willy Tarreau2bb4a962014-11-28 11:11:05 +01004657 struct channel *rep = si_ic(si);
Willy Tarreau44267702011-10-28 15:35:33 +02004658 struct server *sv, *svs; /* server and server-state, server-state=server or server->track */
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004659 struct listener *l;
Willy Tarreau0deb85a2016-01-08 15:33:18 +01004660 unsigned int flags;
4661
4662 if (uri)
4663 flags = uri->flags;
4664 else if (strm_li(s)->bind_conf->level >= ACCESS_LVL_OPER)
4665 flags = ST_SHLGNDS | ST_SHNODE | ST_SHDESC;
4666 else
4667 flags = ST_SHNODE | ST_SHDESC;
Willy Tarreau91861262007-10-17 17:06:05 +02004668
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004669 chunk_reset(&trash);
Willy Tarreau91861262007-10-17 17:06:05 +02004670
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004671 switch (appctx->ctx.stats.px_st) {
Willy Tarreau295a8372011-03-10 11:25:07 +01004672 case STAT_PX_ST_INIT:
Willy Tarreau91861262007-10-17 17:06:05 +02004673 /* we are on a new proxy */
Willy Tarreau91861262007-10-17 17:06:05 +02004674 if (uri && uri->scope) {
4675 /* we have a limited scope, we have to check the proxy name */
4676 struct stat_scope *scope;
4677 int len;
4678
4679 len = strlen(px->id);
4680 scope = uri->scope;
4681
4682 while (scope) {
4683 /* match exact proxy name */
4684 if (scope->px_len == len && !memcmp(px->id, scope->px_id, len))
4685 break;
4686
4687 /* match '.' which means 'self' proxy */
Willy Tarreau1388a3a2007-10-18 16:38:37 +02004688 if (!strcmp(scope->px_id, ".") && px == s->be)
Willy Tarreau91861262007-10-17 17:06:05 +02004689 break;
4690 scope = scope->next;
4691 }
4692
4693 /* proxy name not found : don't dump anything */
4694 if (scope == NULL)
4695 return 1;
4696 }
4697
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004698 /* if the user has requested a limited output and the proxy
4699 * name does not match, skip it.
4700 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004701 if (appctx->ctx.stats.scope_len &&
Willy Tarreau4e4292b2014-11-28 12:18:45 +01004702 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 +02004703 return 1;
4704
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004705 if ((appctx->ctx.stats.flags & STAT_BOUND) &&
4706 (appctx->ctx.stats.iid != -1) &&
4707 (px->uuid != appctx->ctx.stats.iid))
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01004708 return 1;
4709
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004710 appctx->ctx.stats.px_st = STAT_PX_ST_TH;
Willy Tarreau91861262007-10-17 17:06:05 +02004711 /* fall through */
4712
Willy Tarreau295a8372011-03-10 11:25:07 +01004713 case STAT_PX_ST_TH:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004714 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004715 stats_dump_html_px_hdr(si, px, uri);
Willy Tarreaubc18da12015-03-13 14:00:47 +01004716 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004717 si_applet_cant_put(si);
Willy Tarreau55bb8452007-10-17 18:44:57 +02004718 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01004719 }
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004720 }
Willy Tarreau91861262007-10-17 17:06:05 +02004721
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004722 appctx->ctx.stats.px_st = STAT_PX_ST_FE;
Willy Tarreau91861262007-10-17 17:06:05 +02004723 /* fall through */
4724
Willy Tarreau295a8372011-03-10 11:25:07 +01004725 case STAT_PX_ST_FE:
Willy Tarreau91861262007-10-17 17:06:05 +02004726 /* print the frontend */
Willy Tarreaubc18da12015-03-13 14:00:47 +01004727 if (stats_dump_fe_stats(si, px)) {
4728 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004729 si_applet_cant_put(si);
Willy Tarreau91861262007-10-17 17:06:05 +02004730 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01004731 }
4732 }
Willy Tarreau91861262007-10-17 17:06:05 +02004733
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004734 appctx->ctx.stats.l = px->conf.listeners.n;
4735 appctx->ctx.stats.px_st = STAT_PX_ST_LI;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004736 /* fall through */
4737
Willy Tarreau295a8372011-03-10 11:25:07 +01004738 case STAT_PX_ST_LI:
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004739 /* stats.l has been initialized above */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004740 for (; appctx->ctx.stats.l != &px->conf.listeners; appctx->ctx.stats.l = l->by_fe.n) {
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01004741 if (buffer_almost_full(rep->buf)) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004742 si_applet_cant_put(si);
Willy Tarreau4e33d862009-10-11 23:35:10 +02004743 return 0;
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01004744 }
Willy Tarreau4e33d862009-10-11 23:35:10 +02004745
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004746 l = LIST_ELEM(appctx->ctx.stats.l, struct listener *, by_fe);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004747 if (!l->counters)
4748 continue;
4749
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004750 if (appctx->ctx.stats.flags & STAT_BOUND) {
4751 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SO)))
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004752 break;
4753
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004754 if (appctx->ctx.stats.sid != -1 && l->luid != appctx->ctx.stats.sid)
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004755 continue;
4756 }
4757
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004758 /* print the frontend */
Willy Tarreau0deb85a2016-01-08 15:33:18 +01004759 if (stats_dump_li_stats(si, px, l, flags)) {
Willy Tarreaubc18da12015-03-13 14:00:47 +01004760 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004761 si_applet_cant_put(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004762 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01004763 }
4764 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004765 }
4766
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004767 appctx->ctx.stats.sv = px->srv; /* may be NULL */
4768 appctx->ctx.stats.px_st = STAT_PX_ST_SV;
Willy Tarreau91861262007-10-17 17:06:05 +02004769 /* fall through */
4770
Willy Tarreau295a8372011-03-10 11:25:07 +01004771 case STAT_PX_ST_SV:
Willy Tarreau91861262007-10-17 17:06:05 +02004772 /* stats.sv has been initialized above */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004773 for (; appctx->ctx.stats.sv != NULL; appctx->ctx.stats.sv = sv->next) {
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01004774 if (buffer_almost_full(rep->buf)) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004775 si_applet_cant_put(si);
Willy Tarreau4e33d862009-10-11 23:35:10 +02004776 return 0;
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01004777 }
Willy Tarreau4e33d862009-10-11 23:35:10 +02004778
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004779 sv = appctx->ctx.stats.sv;
Willy Tarreau91861262007-10-17 17:06:05 +02004780
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004781 if (appctx->ctx.stats.flags & STAT_BOUND) {
4782 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SV)))
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01004783 break;
4784
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004785 if (appctx->ctx.stats.sid != -1 && sv->puid != appctx->ctx.stats.sid)
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01004786 continue;
4787 }
4788
Willy Tarreau32091232014-05-16 13:52:00 +02004789 svs = sv;
4790 while (svs->track)
4791 svs = svs->track;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01004792
Willy Tarreauba2f2642016-01-07 09:54:40 +01004793 /* do not report servers which are DOWN and not changing state */
4794 if ((appctx->ctx.stats.flags & STAT_HIDE_DOWN) &&
4795 ((sv->admin & SRV_ADMF_MAINT) || /* server is in maintenance */
4796 (sv->state == SRV_ST_STOPPED && /* server is down */
4797 (!((svs->agent.state | svs->check.state) & CHK_ST_ENABLED) ||
4798 ((svs->agent.state & CHK_ST_ENABLED) && !svs->agent.health) ||
4799 ((svs->check.state & CHK_ST_ENABLED) && !svs->check.health))))) {
Willy Tarreau91861262007-10-17 17:06:05 +02004800 continue;
4801 }
4802
Willy Tarreau0deb85a2016-01-08 15:33:18 +01004803 if (stats_dump_sv_stats(si, px, flags, sv)) {
Willy Tarreaubc18da12015-03-13 14:00:47 +01004804 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004805 si_applet_cant_put(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004806 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01004807 }
4808 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004809 } /* for sv */
Cyril Bonté474be412010-10-12 00:14:36 +02004810
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004811 appctx->ctx.stats.px_st = STAT_PX_ST_BE;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004812 /* fall through */
Cyril Bonté70be45d2010-10-12 00:14:35 +02004813
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004814 case STAT_PX_ST_BE:
4815 /* print the backend */
Willy Tarreau0deb85a2016-01-08 15:33:18 +01004816 if (stats_dump_be_stats(si, px, flags)) {
Willy Tarreaubc18da12015-03-13 14:00:47 +01004817 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004818 si_applet_cant_put(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004819 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01004820 }
4821 }
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01004822
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004823 appctx->ctx.stats.px_st = STAT_PX_ST_END;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004824 /* fall through */
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01004825
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004826 case STAT_PX_ST_END:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004827 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004828 stats_dump_html_px_end(si, px);
Willy Tarreaubc18da12015-03-13 14:00:47 +01004829 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004830 si_applet_cant_put(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004831 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01004832 }
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004833 }
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01004834
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004835 appctx->ctx.stats.px_st = STAT_PX_ST_FIN;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004836 /* fall through */
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01004837
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004838 case STAT_PX_ST_FIN:
4839 return 1;
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01004840
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004841 default:
4842 /* unknown state, we should put an abort() here ! */
4843 return 1;
4844 }
4845}
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01004846
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004847/* Dumps the HTTP stats head block to the trash for and uses the per-uri
4848 * parameters <uri>. The caller is responsible for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004849 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004850static void stats_dump_html_head(struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004851{
4852 /* WARNING! This must fit in the first buffer !!! */
4853 chunk_appendf(&trash,
4854 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n"
4855 "\"http://www.w3.org/TR/html4/loose.dtd\">\n"
4856 "<html><head><title>Statistics Report for " PRODUCT_NAME "%s%s</title>\n"
4857 "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n"
4858 "<style type=\"text/css\"><!--\n"
4859 "body {"
4860 " font-family: arial, helvetica, sans-serif;"
4861 " font-size: 12px;"
4862 " font-weight: normal;"
4863 " color: black;"
4864 " background: white;"
4865 "}\n"
4866 "th,td {"
4867 " font-size: 10px;"
4868 "}\n"
4869 "h1 {"
4870 " font-size: x-large;"
4871 " margin-bottom: 0.5em;"
4872 "}\n"
4873 "h2 {"
4874 " font-family: helvetica, arial;"
4875 " font-size: x-large;"
4876 " font-weight: bold;"
4877 " font-style: italic;"
4878 " color: #6020a0;"
4879 " margin-top: 0em;"
4880 " margin-bottom: 0em;"
4881 "}\n"
4882 "h3 {"
4883 " font-family: helvetica, arial;"
4884 " font-size: 16px;"
4885 " font-weight: bold;"
4886 " color: #b00040;"
4887 " background: #e8e8d0;"
4888 " margin-top: 0em;"
4889 " margin-bottom: 0em;"
4890 "}\n"
4891 "li {"
4892 " margin-top: 0.25em;"
4893 " margin-right: 2em;"
4894 "}\n"
4895 ".hr {margin-top: 0.25em;"
4896 " border-color: black;"
4897 " border-bottom-style: solid;"
4898 "}\n"
4899 ".titre {background: #20D0D0;color: #000000; font-weight: bold; text-align: center;}\n"
4900 ".total {background: #20D0D0;color: #ffff80;}\n"
4901 ".frontend {background: #e8e8d0;}\n"
4902 ".socket {background: #d0d0d0;}\n"
4903 ".backend {background: #e8e8d0;}\n"
Simon Horman837bfa72015-04-23 14:51:27 +09004904 ".active_down {background: #ff9090;}\n"
4905 ".active_going_up {background: #ffd020;}\n"
4906 ".active_going_down {background: #ffffa0;}\n"
4907 ".active_up {background: #c0ffc0;}\n"
4908 ".active_nolb {background: #20a0ff;}\n"
4909 ".active_draining {background: #20a0FF;}\n"
4910 ".active_no_check {background: #e0e0e0;}\n"
4911 ".backup_down {background: #ff9090;}\n"
4912 ".backup_going_up {background: #ff80ff;}\n"
4913 ".backup_going_down {background: #c060ff;}\n"
4914 ".backup_up {background: #b0d0ff;}\n"
4915 ".backup_nolb {background: #90b0e0;}\n"
4916 ".backup_draining {background: #cc9900;}\n"
4917 ".backup_no_check {background: #e0e0e0;}\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004918 ".maintain {background: #c07820;}\n"
4919 ".rls {letter-spacing: 0.2em; margin-right: 1px;}\n" /* right letter spacing (used for grouping digits) */
4920 "\n"
4921 "a.px:link {color: #ffff40; text-decoration: none;}"
4922 "a.px:visited {color: #ffff40; text-decoration: none;}"
4923 "a.px:hover {color: #ffffff; text-decoration: none;}"
4924 "a.lfsb:link {color: #000000; text-decoration: none;}"
4925 "a.lfsb:visited {color: #000000; text-decoration: none;}"
4926 "a.lfsb:hover {color: #505050; text-decoration: none;}"
4927 "\n"
4928 "table.tbl { border-collapse: collapse; border-style: none;}\n"
4929 "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"
4930 "table.tbl td.ac { text-align: center;}\n"
4931 "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}\n"
4932 "table.tbl th.pxname { background: #b00040; color: #ffff40; font-weight: bold; border-style: solid solid none solid; padding: 2px 3px; white-space: nowrap;}\n"
4933 "table.tbl th.empty { border-style: none; empty-cells: hide; background: white;}\n"
4934 "table.tbl th.desc { background: white; border-style: solid solid none solid; text-align: left; padding: 2px 3px;}\n"
4935 "\n"
4936 "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n"
4937 "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n"
4938 "table.lgd td.noborder { border-style: none; padding: 2px; white-space: nowrap;}\n"
Willy Tarreau466c9b52012-12-23 02:25:03 +01004939 "table.det { border-collapse: collapse; border-style: none; }\n"
4940 "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 +01004941 "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 +01004942 "u {text-decoration:none; border-bottom: 1px dotted black;}\n"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02004943 "div.tips {\n"
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01004944 " display:block;\n"
4945 " visibility:hidden;\n"
4946 " z-index:2147483647;\n"
4947 " position:absolute;\n"
4948 " padding:2px 4px 3px;\n"
4949 " background:#f0f060; color:#000000;\n"
4950 " border:1px solid #7040c0;\n"
4951 " white-space:nowrap;\n"
4952 " font-style:normal;font-size:11px;font-weight:normal;\n"
4953 " -moz-border-radius:3px;-webkit-border-radius:3px;border-radius:3px;\n"
4954 " -moz-box-shadow:gray 2px 2px 3px;-webkit-box-shadow:gray 2px 2px 3px;box-shadow:gray 2px 2px 3px;\n"
4955 "}\n"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02004956 "u:hover div.tips {visibility:visible;}\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004957 "-->\n"
4958 "</style></head>\n",
4959 (uri->flags & ST_SHNODE) ? " on " : "",
4960 (uri->flags & ST_SHNODE) ? (uri->node ? uri->node : global.node) : ""
4961 );
4962}
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01004963
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004964/* Dumps the HTML stats information block to the trash for and uses the state from
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004965 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004966 * for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004967 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004968static void stats_dump_html_info(struct stream_interface *si, struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004969{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004970 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004971 unsigned int up = (now.tv_sec - start_date.tv_sec);
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004972 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01004973
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004974 /* WARNING! this has to fit the first packet too.
4975 * We are around 3.5 kB, add adding entries will
4976 * become tricky if we want to support 4kB buffers !
4977 */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004978 chunk_appendf(&trash,
4979 "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">"
4980 PRODUCT_NAME "%s</a></h1>\n"
4981 "<h2>Statistics Report for pid %d%s%s%s%s</h2>\n"
4982 "<hr width=\"100%%\" class=\"hr\">\n"
4983 "<h3>&gt; General process information</h3>\n"
4984 "<table border=0><tr><td align=\"left\" nowrap width=\"1%%\">\n"
4985 "<p><b>pid = </b> %d (process #%d, nbproc = %d)<br>\n"
4986 "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n"
4987 "<b>system limits:</b> memmax = %s%s; ulimit-n = %d<br>\n"
4988 "<b>maxsock = </b> %d; <b>maxconn = </b> %d; <b>maxpipes = </b> %d<br>\n"
4989 "current conns = %d; current pipes = %d/%d; conn rate = %d/sec<br>\n"
4990 "Running tasks: %d/%d; idle = %d %%<br>\n"
4991 "</td><td align=\"center\" nowrap>\n"
4992 "<table class=\"lgd\"><tr>\n"
Simon Horman837bfa72015-04-23 14:51:27 +09004993 "<td class=\"active_up\">&nbsp;</td><td class=\"noborder\">active UP </td>"
4994 "<td class=\"backup_up\">&nbsp;</td><td class=\"noborder\">backup UP </td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004995 "</tr><tr>\n"
Simon Horman837bfa72015-04-23 14:51:27 +09004996 "<td class=\"active_going_down\"></td><td class=\"noborder\">active UP, going down </td>"
4997 "<td class=\"backup_going_down\"></td><td class=\"noborder\">backup UP, going down </td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004998 "</tr><tr>\n"
Simon Horman837bfa72015-04-23 14:51:27 +09004999 "<td class=\"active_going_up\"></td><td class=\"noborder\">active DOWN, going up </td>"
5000 "<td class=\"backup_going_up\"></td><td class=\"noborder\">backup DOWN, going up </td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005001 "</tr><tr>\n"
Simon Horman837bfa72015-04-23 14:51:27 +09005002 "<td class=\"active_down\"></td><td class=\"noborder\">active or backup DOWN &nbsp;</td>"
5003 "<td class=\"active_no_check\"></td><td class=\"noborder\">not checked </td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005004 "</tr><tr>\n"
5005 "<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 -07005006 "</tr><tr>\n"
Simon Horman837bfa72015-04-23 14:51:27 +09005007 "<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 +01005008 "</tr></table>\n"
Willy Tarreau6b7764a2013-12-04 00:43:21 +01005009 "Note: \"NOLB\"/\"DRAIN\" = UP with load-balancing disabled."
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005010 "</td>"
5011 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
5012 "<b>Display option:</b><ul style=\"margin-top: 0.25em;\">"
5013 "",
5014 (uri->flags & ST_HIDEVER) ? "" : (STATS_VERSION_STRING),
5015 pid, (uri->flags & ST_SHNODE) ? " on " : "",
5016 (uri->flags & ST_SHNODE) ? (uri->node ? uri->node : global.node) : "",
5017 (uri->flags & ST_SHDESC) ? ": " : "",
5018 (uri->flags & ST_SHDESC) ? (uri->desc ? uri->desc : global.desc) : "",
5019 pid, relative_pid, global.nbproc,
5020 up / 86400, (up % 86400) / 3600,
5021 (up % 3600) / 60, (up % 60),
5022 global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited",
5023 global.rlimit_memmax ? " MB" : "",
5024 global.rlimit_nofile,
5025 global.maxsock, global.maxconn, global.maxpipes,
5026 actconn, pipes_used, pipes_used+pipes_free, read_freq_ctr(&global.conn_per_sec),
5027 run_queue_cur, nb_tasks_cur, idle_pct
5028 );
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005029
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005030 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Willy Tarreau4e4292b2014-11-28 12:18:45 +01005031 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 +01005032 scope_txt[appctx->ctx.stats.scope_len] = '\0';
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02005033
5034 chunk_appendf(&trash,
Jeff Buchbinder2dbbf4d2014-08-29 15:10:08 -05005035 "<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 +01005036 (appctx->ctx.stats.scope_len > 0) ? scope_txt : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02005037 STAT_SCOPE_TXT_MAXLEN);
5038
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005039 /* 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 +02005040 scope_txt[0] = 0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005041 if (appctx->ctx.stats.scope_len) {
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02005042 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau4e4292b2014-11-28 12:18:45 +01005043 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 +01005044 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02005045 }
5046
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005047 if (appctx->ctx.stats.flags & STAT_HIDE_DOWN)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005048 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02005049 "<li><a href=\"%s%s%s%s\">Show all servers</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005050 uri->uri_prefix,
5051 "",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005052 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02005053 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005054 else
5055 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02005056 "<li><a href=\"%s%s%s%s\">Hide 'DOWN' servers</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005057 uri->uri_prefix,
5058 ";up",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005059 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02005060 scope_txt);
Willy Tarreau91861262007-10-17 17:06:05 +02005061
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005062 if (uri->refresh > 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005063 if (appctx->ctx.stats.flags & STAT_NO_REFRESH)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005064 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02005065 "<li><a href=\"%s%s%s%s\">Enable refresh</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005066 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005067 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02005068 "",
5069 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005070 else
5071 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02005072 "<li><a href=\"%s%s%s%s\">Disable refresh</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005073 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005074 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02005075 ";norefresh",
5076 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005077 }
Willy Tarreau55bb8452007-10-17 18:44:57 +02005078
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005079 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02005080 "<li><a href=\"%s%s%s%s\">Refresh now</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005081 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005082 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
5083 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02005084 scope_txt);
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02005085
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005086 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02005087 "<li><a href=\"%s;csv%s%s\">CSV export</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005088 uri->uri_prefix,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02005089 (uri->refresh > 0) ? ";norefresh" : "",
5090 scope_txt);
Willy Tarreau4bab24d2007-11-30 18:16:29 +01005091
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005092 chunk_appendf(&trash,
5093 "</ul></td>"
5094 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
5095 "<b>External resources:</b><ul style=\"margin-top: 0.25em;\">\n"
5096 "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>\n"
5097 "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>\n"
5098 "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>\n"
5099 "</ul>"
5100 "</td>"
5101 "</tr></table>\n"
5102 ""
5103 );
Willy Tarreau4bab24d2007-11-30 18:16:29 +01005104
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005105 if (appctx->ctx.stats.st_code) {
5106 switch (appctx->ctx.stats.st_code) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005107 case STAT_STATUS_DONE:
5108 chunk_appendf(&trash,
Simon Horman837bfa72015-04-23 14:51:27 +09005109 "<p><div class=active_up>"
Willy Tarreauba6be982013-04-19 12:16:55 +02005110 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005111 "Action processed successfully."
Willy Tarreauba6be982013-04-19 12:16:55 +02005112 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005113 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
5114 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02005115 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005116 break;
5117 case STAT_STATUS_NONE:
5118 chunk_appendf(&trash,
Simon Horman837bfa72015-04-23 14:51:27 +09005119 "<p><div class=active_going_down>"
Willy Tarreauba6be982013-04-19 12:16:55 +02005120 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005121 "Nothing has changed."
Willy Tarreauba6be982013-04-19 12:16:55 +02005122 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005123 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
5124 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02005125 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005126 break;
5127 case STAT_STATUS_PART:
5128 chunk_appendf(&trash,
Simon Horman837bfa72015-04-23 14:51:27 +09005129 "<p><div class=active_going_down>"
Willy Tarreauba6be982013-04-19 12:16:55 +02005130 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005131 "Action partially processed.<br>"
5132 "Some server names are probably unknown or ambiguous (duplicated names in the backend)."
Willy Tarreauba6be982013-04-19 12:16:55 +02005133 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005134 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
5135 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02005136 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005137 break;
5138 case STAT_STATUS_ERRP:
5139 chunk_appendf(&trash,
Simon Horman837bfa72015-04-23 14:51:27 +09005140 "<p><div class=active_down>"
Willy Tarreauba6be982013-04-19 12:16:55 +02005141 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005142 "Action not processed because of invalid parameters."
5143 "<ul>"
5144 "<li>The action is maybe unknown.</li>"
5145 "<li>The backend name is probably unknown or ambiguous (duplicated names).</li>"
5146 "<li>Some server names are probably unknown or ambiguous (duplicated names in the backend).</li>"
5147 "</ul>"
Willy Tarreauba6be982013-04-19 12:16:55 +02005148 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005149 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
5150 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02005151 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005152 break;
5153 case STAT_STATUS_EXCD:
5154 chunk_appendf(&trash,
Simon Horman837bfa72015-04-23 14:51:27 +09005155 "<p><div class=active_down>"
Willy Tarreauba6be982013-04-19 12:16:55 +02005156 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005157 "<b>Action not processed : the buffer couldn't store all the data.<br>"
5158 "You should retry with less servers at a time.</b>"
Willy Tarreauba6be982013-04-19 12:16:55 +02005159 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005160 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
5161 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02005162 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005163 break;
5164 case STAT_STATUS_DENY:
5165 chunk_appendf(&trash,
Simon Horman837bfa72015-04-23 14:51:27 +09005166 "<p><div class=active_down>"
Willy Tarreauba6be982013-04-19 12:16:55 +02005167 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005168 "<b>Action denied.</b>"
Willy Tarreauba6be982013-04-19 12:16:55 +02005169 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005170 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
5171 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02005172 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005173 break;
5174 default:
5175 chunk_appendf(&trash,
Simon Horman837bfa72015-04-23 14:51:27 +09005176 "<p><div class=active_no_check>"
Willy Tarreauba6be982013-04-19 12:16:55 +02005177 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005178 "Unexpected result."
Willy Tarreauba6be982013-04-19 12:16:55 +02005179 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005180 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
5181 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02005182 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005183 }
5184 chunk_appendf(&trash, "<p>\n");
5185 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005186}
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01005187
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01005188/* Dumps the HTML stats trailer block to the trash. The caller is responsible
5189 * for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005190 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01005191static void stats_dump_html_end()
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005192{
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005193 chunk_appendf(&trash, "</body></html>\n");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005194}
Willy Tarreau7f062c42009-03-05 18:43:00 +01005195
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01005196/* This function dumps statistics onto the stream interface's read buffer in
5197 * either CSV or HTML format. <uri> contains some HTML-specific parameters that
Willy Tarreau306f8302013-07-08 15:53:06 +02005198 * are ignored for CSV format (hence <uri> may be NULL there). It returns 0 if
5199 * 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 +02005200 * and the stream must be closed, or -1 in case of any error. This function is
Willy Tarreau306f8302013-07-08 15:53:06 +02005201 * used by both the CLI and the HTTP handlers.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005202 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01005203static int stats_dump_stat_to_buffer(struct stream_interface *si, struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005204{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005205 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005206 struct channel *rep = si_ic(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005207 struct proxy *px;
Willy Tarreau7f062c42009-03-05 18:43:00 +01005208
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005209 chunk_reset(&trash);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02005210
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005211 switch (appctx->st2) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005212 case STAT_ST_INIT:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005213 appctx->st2 = STAT_ST_HEAD; /* let's start producing data */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005214 /* fall through */
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005215
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005216 case STAT_ST_HEAD:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005217 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01005218 stats_dump_html_head(uri);
Willy Tarreau1e62df92016-01-11 18:57:53 +01005219 else if (!(appctx->ctx.stats.flags & STAT_FMT_TYPED))
Willy Tarreau354898b2012-12-23 18:15:23 +01005220 stats_dump_csv_header();
Willy Tarreaud9b587f2010-02-26 10:05:55 +01005221
Willy Tarreaubc18da12015-03-13 14:00:47 +01005222 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02005223 si_applet_cant_put(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005224 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01005225 }
Willy Tarreauae526782010-03-04 20:34:23 +01005226
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005227 appctx->st2 = STAT_ST_INFO;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005228 /* fall through */
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01005229
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005230 case STAT_ST_INFO:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005231 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01005232 stats_dump_html_info(si, uri);
Willy Tarreaubc18da12015-03-13 14:00:47 +01005233 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02005234 si_applet_cant_put(si);
Willy Tarreau91861262007-10-17 17:06:05 +02005235 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01005236 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005237 }
Willy Tarreau91861262007-10-17 17:06:05 +02005238
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005239 appctx->ctx.stats.px = proxy;
5240 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
5241 appctx->st2 = STAT_ST_LIST;
Willy Tarreau91861262007-10-17 17:06:05 +02005242 /* fall through */
5243
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005244 case STAT_ST_LIST:
5245 /* dump proxies */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005246 while (appctx->ctx.stats.px) {
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01005247 if (buffer_almost_full(rep->buf)) {
Willy Tarreaufe127932015-04-21 19:23:39 +02005248 si_applet_cant_put(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005249 return 0;
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01005250 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005251
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005252 px = appctx->ctx.stats.px;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005253 /* skip the disabled proxies, global frontend and non-networked ones */
5254 if (px->state != PR_STSTOPPED && px->uuid > 0 && (px->cap & (PR_CAP_FE | PR_CAP_BE)))
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01005255 if (stats_dump_proxy_to_buffer(si, px, uri) == 0)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005256 return 0;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005257
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005258 appctx->ctx.stats.px = px->next;
5259 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005260 }
5261 /* here, we just have reached the last proxy */
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005262
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005263 appctx->st2 = STAT_ST_END;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005264 /* fall through */
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005265
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005266 case STAT_ST_END:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005267 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01005268 stats_dump_html_end();
Willy Tarreaubc18da12015-03-13 14:00:47 +01005269 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02005270 si_applet_cant_put(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005271 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01005272 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005273 }
Willy Tarreau55058a72012-11-21 08:27:21 +01005274
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005275 appctx->st2 = STAT_ST_FIN;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005276 /* fall through */
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02005277
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005278 case STAT_ST_FIN:
5279 return 1;
Willy Tarreau55058a72012-11-21 08:27:21 +01005280
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005281 default:
5282 /* unknown state ! */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005283 appctx->st2 = STAT_ST_FIN;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005284 return -1;
5285 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005286}
Willy Tarreauae526782010-03-04 20:34:23 +01005287
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005288/* We reached the stats page through a POST request. The appctx is
5289 * expected to have already been allocated by the caller.
Willy Tarreau347a35d2013-11-22 17:51:09 +01005290 * Parse the posted data and enable/disable servers if necessary.
5291 * Returns 1 if request was parsed or zero if it needs more data.
5292 */
5293static int stats_process_http_post(struct stream_interface *si)
5294{
Willy Tarreau87b09662015-04-03 00:22:06 +02005295 struct stream *s = si_strm(si);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005296 struct appctx *appctx = objt_appctx(si->end);
Willy Tarreau347a35d2013-11-22 17:51:09 +01005297
5298 struct proxy *px = NULL;
5299 struct server *sv = NULL;
5300
5301 char key[LINESIZE];
5302 int action = ST_ADM_ACTION_NONE;
5303 int reprocess = 0;
5304
5305 int total_servers = 0;
5306 int altered_servers = 0;
5307
5308 char *first_param, *cur_param, *next_param, *end_params;
5309 char *st_cur_param = NULL;
5310 char *st_next_param = NULL;
5311
5312 struct chunk *temp;
5313 int reql;
5314
5315 temp = get_trash_chunk();
Willy Tarreaueee5b512015-04-03 23:46:31 +02005316 if (temp->size < s->txn->req.body_len) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01005317 /* too large request */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005318 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005319 goto out;
5320 }
5321
Willy Tarreaueee5b512015-04-03 23:46:31 +02005322 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 +01005323 if (reql <= 0) {
5324 /* we need more data */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005325 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005326 return 0;
5327 }
5328
5329 first_param = temp->str;
5330 end_params = temp->str + reql;
5331 cur_param = next_param = end_params;
5332 *end_params = '\0';
5333
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005334 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005335
5336 /*
5337 * Parse the parameters in reverse order to only store the last value.
5338 * From the html form, the backend and the action are at the end.
5339 */
5340 while (cur_param > first_param) {
5341 char *value;
5342 int poffset, plen;
5343
5344 cur_param--;
5345
5346 if ((*cur_param == '&') || (cur_param == first_param)) {
5347 reprocess_servers:
5348 /* Parse the key */
5349 poffset = (cur_param != first_param ? 1 : 0);
5350 plen = next_param - cur_param + (cur_param == first_param ? 1 : 0);
5351 if ((plen > 0) && (plen <= sizeof(key))) {
5352 strncpy(key, cur_param + poffset, plen);
5353 key[plen - 1] = '\0';
5354 } else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005355 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005356 goto out;
5357 }
5358
5359 /* Parse the value */
5360 value = key;
5361 while (*value != '\0' && *value != '=') {
5362 value++;
5363 }
5364 if (*value == '=') {
5365 /* Ok, a value is found, we can mark the end of the key */
5366 *value++ = '\0';
5367 }
5368 if (url_decode(key) < 0 || url_decode(value) < 0)
5369 break;
5370
5371 /* Now we can check the key to see what to do */
5372 if (!px && (strcmp(key, "b") == 0)) {
Willy Tarreau9e0bb102015-05-26 11:24:42 +02005373 if ((px = proxy_be_by_name(value)) == NULL) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01005374 /* the backend name is unknown or ambiguous (duplicate names) */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005375 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005376 goto out;
5377 }
5378 }
5379 else if (!action && (strcmp(key, "action") == 0)) {
Willy Tarreaued7df902014-05-22 18:04:49 +02005380 if (strcmp(value, "ready") == 0) {
5381 action = ST_ADM_ACTION_READY;
5382 }
5383 else if (strcmp(value, "drain") == 0) {
5384 action = ST_ADM_ACTION_DRAIN;
5385 }
5386 else if (strcmp(value, "maint") == 0) {
5387 action = ST_ADM_ACTION_MAINT;
5388 }
5389 else if (strcmp(value, "shutdown") == 0) {
5390 action = ST_ADM_ACTION_SHUTDOWN;
5391 }
Willy Tarreau248a60e2014-05-23 14:59:48 +02005392 else if (strcmp(value, "dhlth") == 0) {
5393 action = ST_ADM_ACTION_DHLTH;
5394 }
5395 else if (strcmp(value, "ehlth") == 0) {
5396 action = ST_ADM_ACTION_EHLTH;
5397 }
5398 else if (strcmp(value, "hrunn") == 0) {
5399 action = ST_ADM_ACTION_HRUNN;
5400 }
5401 else if (strcmp(value, "hnolb") == 0) {
5402 action = ST_ADM_ACTION_HNOLB;
5403 }
5404 else if (strcmp(value, "hdown") == 0) {
5405 action = ST_ADM_ACTION_HDOWN;
5406 }
5407 else if (strcmp(value, "dagent") == 0) {
5408 action = ST_ADM_ACTION_DAGENT;
5409 }
5410 else if (strcmp(value, "eagent") == 0) {
5411 action = ST_ADM_ACTION_EAGENT;
5412 }
5413 else if (strcmp(value, "arunn") == 0) {
5414 action = ST_ADM_ACTION_ARUNN;
5415 }
5416 else if (strcmp(value, "adown") == 0) {
5417 action = ST_ADM_ACTION_ADOWN;
5418 }
Willy Tarreaued7df902014-05-22 18:04:49 +02005419 /* else these are the old supported methods */
5420 else if (strcmp(value, "disable") == 0) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01005421 action = ST_ADM_ACTION_DISABLE;
5422 }
5423 else if (strcmp(value, "enable") == 0) {
5424 action = ST_ADM_ACTION_ENABLE;
5425 }
5426 else if (strcmp(value, "stop") == 0) {
5427 action = ST_ADM_ACTION_STOP;
5428 }
5429 else if (strcmp(value, "start") == 0) {
5430 action = ST_ADM_ACTION_START;
5431 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01005432 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005433 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005434 goto out;
5435 }
5436 }
5437 else if (strcmp(key, "s") == 0) {
5438 if (!(px && action)) {
5439 /*
5440 * Indicates that we'll need to reprocess the parameters
5441 * as soon as backend and action are known
5442 */
5443 if (!reprocess) {
5444 st_cur_param = cur_param;
5445 st_next_param = next_param;
5446 }
5447 reprocess = 1;
5448 }
5449 else if ((sv = findserver(px, value)) != NULL) {
5450 switch (action) {
5451 case ST_ADM_ACTION_DISABLE:
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02005452 if (!(sv->admin & SRV_ADMF_FMAINT)) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01005453 altered_servers++;
5454 total_servers++;
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02005455 srv_set_admin_flag(sv, SRV_ADMF_FMAINT);
Willy Tarreau347a35d2013-11-22 17:51:09 +01005456 }
5457 break;
5458 case ST_ADM_ACTION_ENABLE:
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02005459 if (sv->admin & SRV_ADMF_FMAINT) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01005460 altered_servers++;
5461 total_servers++;
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02005462 srv_clr_admin_flag(sv, SRV_ADMF_FMAINT);
Willy Tarreau347a35d2013-11-22 17:51:09 +01005463 }
5464 break;
5465 case ST_ADM_ACTION_STOP:
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02005466 if (!(sv->admin & SRV_ADMF_FDRAIN)) {
5467 srv_set_admin_flag(sv, SRV_ADMF_FDRAIN);
5468 altered_servers++;
5469 total_servers++;
5470 }
5471 break;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005472 case ST_ADM_ACTION_START:
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02005473 if (sv->admin & SRV_ADMF_FDRAIN) {
5474 srv_clr_admin_flag(sv, SRV_ADMF_FDRAIN);
5475 altered_servers++;
5476 total_servers++;
5477 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01005478 break;
Willy Tarreau248a60e2014-05-23 14:59:48 +02005479 case ST_ADM_ACTION_DHLTH:
5480 if (sv->check.state & CHK_ST_CONFIGURED) {
5481 sv->check.state &= ~CHK_ST_ENABLED;
5482 altered_servers++;
5483 total_servers++;
5484 }
5485 break;
5486 case ST_ADM_ACTION_EHLTH:
5487 if (sv->check.state & CHK_ST_CONFIGURED) {
5488 sv->check.state |= CHK_ST_ENABLED;
5489 altered_servers++;
5490 total_servers++;
5491 }
5492 break;
5493 case ST_ADM_ACTION_HRUNN:
5494 if (!(sv->track)) {
5495 sv->check.health = sv->check.rise + sv->check.fall - 1;
5496 srv_set_running(sv, "changed from Web interface");
5497 altered_servers++;
5498 total_servers++;
5499 }
5500 break;
5501 case ST_ADM_ACTION_HNOLB:
5502 if (!(sv->track)) {
5503 sv->check.health = sv->check.rise + sv->check.fall - 1;
5504 srv_set_stopping(sv, "changed from Web interface");
5505 altered_servers++;
5506 total_servers++;
5507 }
5508 break;
5509 case ST_ADM_ACTION_HDOWN:
5510 if (!(sv->track)) {
5511 sv->check.health = 0;
5512 srv_set_stopped(sv, "changed from Web interface");
5513 altered_servers++;
5514 total_servers++;
5515 }
5516 break;
5517 case ST_ADM_ACTION_DAGENT:
5518 if (sv->agent.state & CHK_ST_CONFIGURED) {
5519 sv->agent.state &= ~CHK_ST_ENABLED;
5520 altered_servers++;
5521 total_servers++;
5522 }
5523 break;
5524 case ST_ADM_ACTION_EAGENT:
5525 if (sv->agent.state & CHK_ST_CONFIGURED) {
5526 sv->agent.state |= CHK_ST_ENABLED;
5527 altered_servers++;
5528 total_servers++;
5529 }
5530 break;
5531 case ST_ADM_ACTION_ARUNN:
5532 if (sv->agent.state & CHK_ST_ENABLED) {
5533 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
5534 srv_set_running(sv, "changed from Web interface");
5535 altered_servers++;
5536 total_servers++;
5537 }
5538 break;
5539 case ST_ADM_ACTION_ADOWN:
5540 if (sv->agent.state & CHK_ST_ENABLED) {
5541 sv->agent.health = 0;
5542 srv_set_stopped(sv, "changed from Web interface");
5543 altered_servers++;
5544 total_servers++;
5545 }
5546 break;
Willy Tarreaued7df902014-05-22 18:04:49 +02005547 case ST_ADM_ACTION_READY:
5548 srv_adm_set_ready(sv);
5549 altered_servers++;
5550 total_servers++;
5551 break;
5552 case ST_ADM_ACTION_DRAIN:
5553 srv_adm_set_drain(sv);
5554 altered_servers++;
5555 total_servers++;
5556 break;
5557 case ST_ADM_ACTION_MAINT:
5558 srv_adm_set_maint(sv);
5559 altered_servers++;
5560 total_servers++;
5561 break;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005562 case ST_ADM_ACTION_SHUTDOWN:
5563 if (px->state != PR_STSTOPPED) {
Willy Tarreau87b09662015-04-03 00:22:06 +02005564 struct stream *sess, *sess_bck;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005565
5566 list_for_each_entry_safe(sess, sess_bck, &sv->actconns, by_srv)
5567 if (sess->srv_conn == sv)
Willy Tarreaue7dff022015-04-03 01:14:29 +02005568 stream_shutdown(sess, SF_ERR_KILLED);
Willy Tarreau347a35d2013-11-22 17:51:09 +01005569
5570 altered_servers++;
5571 total_servers++;
5572 }
5573 break;
5574 }
5575 } else {
5576 /* the server name is unknown or ambiguous (duplicate names) */
5577 total_servers++;
5578 }
5579 }
5580 if (reprocess && px && action) {
5581 /* Now, we know the backend and the action chosen by the user.
5582 * We can safely restart from the first server parameter
5583 * to reprocess them
5584 */
5585 cur_param = st_cur_param;
5586 next_param = st_next_param;
5587 reprocess = 0;
5588 goto reprocess_servers;
5589 }
5590
5591 next_param = cur_param;
5592 }
5593 }
5594
5595 if (total_servers == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005596 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005597 }
5598 else if (altered_servers == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005599 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005600 }
5601 else if (altered_servers == total_servers) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005602 appctx->ctx.stats.st_code = STAT_STATUS_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005603 }
5604 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005605 appctx->ctx.stats.st_code = STAT_STATUS_PART;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005606 }
5607 out:
5608 return 1;
5609}
5610
5611
5612static int stats_send_http_headers(struct stream_interface *si)
5613{
Willy Tarreau87b09662015-04-03 00:22:06 +02005614 struct stream *s = si_strm(si);
Willy Tarreau347a35d2013-11-22 17:51:09 +01005615 struct uri_auth *uri = s->be->uri_auth;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005616 struct appctx *appctx = objt_appctx(si->end);
Willy Tarreau347a35d2013-11-22 17:51:09 +01005617
5618 chunk_printf(&trash,
Willy Tarreau8b8995f2014-04-24 22:51:54 +02005619 "HTTP/1.1 200 OK\r\n"
Willy Tarreau347a35d2013-11-22 17:51:09 +01005620 "Cache-Control: no-cache\r\n"
5621 "Connection: close\r\n"
5622 "Content-Type: %s\r\n",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005623 (appctx->ctx.stats.flags & STAT_FMT_HTML) ? "text/html" : "text/plain");
Willy Tarreau347a35d2013-11-22 17:51:09 +01005624
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005625 if (uri->refresh > 0 && !(appctx->ctx.stats.flags & STAT_NO_REFRESH))
Willy Tarreau347a35d2013-11-22 17:51:09 +01005626 chunk_appendf(&trash, "Refresh: %d\r\n",
5627 uri->refresh);
5628
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005629 /* we don't send the CRLF in chunked mode, it will be sent with the first chunk's size */
5630
5631 if (appctx->ctx.stats.flags & STAT_CHUNKED)
5632 chunk_appendf(&trash, "Transfer-Encoding: chunked\r\n");
5633 else
5634 chunk_appendf(&trash, "\r\n");
Willy Tarreau347a35d2013-11-22 17:51:09 +01005635
Willy Tarreaueee5b512015-04-03 23:46:31 +02005636 s->txn->status = 200;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005637 s->logs.tv_request = now;
5638
Willy Tarreaubc18da12015-03-13 14:00:47 +01005639 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02005640 si_applet_cant_put(si);
Willy Tarreau347a35d2013-11-22 17:51:09 +01005641 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01005642 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01005643
5644 return 1;
5645}
5646
5647static int stats_send_http_redirect(struct stream_interface *si)
5648{
5649 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
Willy Tarreau87b09662015-04-03 00:22:06 +02005650 struct stream *s = si_strm(si);
Willy Tarreau347a35d2013-11-22 17:51:09 +01005651 struct uri_auth *uri = s->be->uri_auth;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005652 struct appctx *appctx = objt_appctx(si->end);
Willy Tarreau347a35d2013-11-22 17:51:09 +01005653
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005654 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Willy Tarreau347a35d2013-11-22 17:51:09 +01005655 scope_txt[0] = 0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005656 if (appctx->ctx.stats.scope_len) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01005657 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau4e4292b2014-11-28 12:18:45 +01005658 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 +01005659 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005660 }
5661
5662 /* We don't want to land on the posted stats page because a refresh will
5663 * repost the data. We don't want this to happen on accident so we redirect
5664 * the browse to the stats page with a GET.
5665 */
5666 chunk_printf(&trash,
5667 "HTTP/1.1 303 See Other\r\n"
5668 "Cache-Control: no-cache\r\n"
5669 "Content-Type: text/plain\r\n"
5670 "Connection: close\r\n"
5671 "Location: %s;st=%s%s%s%s\r\n"
Willy Tarreaufdfcc9d2016-01-26 13:57:29 +01005672 "Content-length: 0\r\n"
Willy Tarreau347a35d2013-11-22 17:51:09 +01005673 "\r\n",
5674 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005675 ((appctx->ctx.stats.st_code > STAT_STATUS_INIT) &&
5676 (appctx->ctx.stats.st_code < STAT_STATUS_SIZE) &&
5677 stat_status_codes[appctx->ctx.stats.st_code]) ?
5678 stat_status_codes[appctx->ctx.stats.st_code] :
Willy Tarreau347a35d2013-11-22 17:51:09 +01005679 stat_status_codes[STAT_STATUS_UNKN],
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005680 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
5681 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreau347a35d2013-11-22 17:51:09 +01005682 scope_txt);
5683
Willy Tarreaueee5b512015-04-03 23:46:31 +02005684 s->txn->status = 303;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005685 s->logs.tv_request = now;
5686
Willy Tarreaubc18da12015-03-13 14:00:47 +01005687 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02005688 si_applet_cant_put(si);
Willy Tarreau347a35d2013-11-22 17:51:09 +01005689 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01005690 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01005691
5692 return 1;
5693}
5694
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005695/* This I/O handler runs as an applet embedded in a stream interface. It is
5696 * used to send HTTP stats over a TCP socket. The mechanism is very simple.
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005697 * appctx->st0 contains the operation in progress (dump, done). The handler
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005698 * automatically unregisters itself once transfer is complete.
5699 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02005700static void http_stats_io_handler(struct appctx *appctx)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005701{
Willy Tarreau00a37f02015-04-13 12:05:19 +02005702 struct stream_interface *si = appctx->owner;
Willy Tarreau87b09662015-04-03 00:22:06 +02005703 struct stream *s = si_strm(si);
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005704 struct channel *req = si_oc(si);
5705 struct channel *res = si_ic(si);
Willy Tarreau55058a72012-11-21 08:27:21 +01005706
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005707 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
5708 goto out;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01005709
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005710 /* check that the output is not closed */
5711 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005712 appctx->st0 = STAT_HTTP_DONE;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005713
Willy Tarreau347a35d2013-11-22 17:51:09 +01005714 /* all states are processed in sequence */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005715 if (appctx->st0 == STAT_HTTP_HEAD) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01005716 if (stats_send_http_headers(si)) {
Willy Tarreaueee5b512015-04-03 23:46:31 +02005717 if (s->txn->meth == HTTP_METH_HEAD)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005718 appctx->st0 = STAT_HTTP_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005719 else
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005720 appctx->st0 = STAT_HTTP_DUMP;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005721 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01005722 }
5723
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005724 if (appctx->st0 == STAT_HTTP_DUMP) {
Willy Tarreau4e4292b2014-11-28 12:18:45 +01005725 unsigned int prev_len = si_ib(si)->i;
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005726 unsigned int data_len;
5727 unsigned int last_len;
Willy Tarreaucce36482014-04-24 20:26:41 +02005728 unsigned int last_fwd = 0;
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005729
5730 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
5731 /* One difficulty we're facing is that we must prevent
5732 * the input data from being automatically forwarded to
5733 * the output area. For this, we temporarily disable
5734 * forwarding on the channel.
5735 */
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005736 last_fwd = si_ic(si)->to_forward;
5737 si_ic(si)->to_forward = 0;
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005738 chunk_printf(&trash, "\r\n000000\r\n");
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005739 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02005740 si_applet_cant_put(si);
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005741 si_ic(si)->to_forward = last_fwd;
Willy Tarreau828824a2015-04-19 17:20:03 +02005742 goto out;
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005743 }
5744 }
5745
Willy Tarreau4e4292b2014-11-28 12:18:45 +01005746 data_len = si_ib(si)->i;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005747 if (stats_dump_stat_to_buffer(si, s->be->uri_auth))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005748 appctx->st0 = STAT_HTTP_DONE;
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005749
Willy Tarreau4e4292b2014-11-28 12:18:45 +01005750 last_len = si_ib(si)->i;
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005751
5752 /* Now we must either adjust or remove the chunk size. This is
5753 * not easy because the chunk size might wrap at the end of the
5754 * buffer, so we pretend we have nothing in the buffer, we write
5755 * the size, then restore the buffer's contents. Note that we can
5756 * only do that because no forwarding is scheduled on the stats
5757 * applet.
5758 */
5759 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
Willy Tarreau4e4292b2014-11-28 12:18:45 +01005760 si_ic(si)->total -= (last_len - prev_len);
5761 si_ib(si)->i -= (last_len - prev_len);
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005762
5763 if (last_len != data_len) {
5764 chunk_printf(&trash, "\r\n%06x\r\n", (last_len - data_len));
Willy Tarreaubc18da12015-03-13 14:00:47 +01005765 if (bi_putchk(si_ic(si), &trash) == -1)
Willy Tarreaufe127932015-04-21 19:23:39 +02005766 si_applet_cant_put(si);
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005767
Willy Tarreau4e4292b2014-11-28 12:18:45 +01005768 si_ic(si)->total += (last_len - data_len);
5769 si_ib(si)->i += (last_len - data_len);
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005770 }
5771 /* now re-enable forwarding */
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005772 channel_forward(si_ic(si), last_fwd);
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005773 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005774 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02005775
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005776 if (appctx->st0 == STAT_HTTP_POST) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01005777 if (stats_process_http_post(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005778 appctx->st0 = STAT_HTTP_LAST;
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005779 else if (si_oc(si)->flags & CF_SHUTR)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005780 appctx->st0 = STAT_HTTP_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005781 }
5782
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005783 if (appctx->st0 == STAT_HTTP_LAST) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01005784 if (stats_send_http_redirect(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005785 appctx->st0 = STAT_HTTP_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005786 }
5787
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005788 if (appctx->st0 == STAT_HTTP_DONE) {
5789 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
5790 chunk_printf(&trash, "\r\n0\r\n\r\n");
Willy Tarreaubc18da12015-03-13 14:00:47 +01005791 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02005792 si_applet_cant_put(si);
Willy Tarreau828824a2015-04-19 17:20:03 +02005793 goto out;
Willy Tarreaubc18da12015-03-13 14:00:47 +01005794 }
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005795 }
5796 /* eat the whole request */
Willy Tarreau4e4292b2014-11-28 12:18:45 +01005797 bo_skip(si_oc(si), si_ob(si)->o);
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005798 res->flags |= CF_READ_NULL;
5799 si_shutr(si);
5800 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01005801
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005802 if ((res->flags & CF_SHUTR) && (si->state == SI_ST_EST))
5803 si_shutw(si);
Willy Tarreau91861262007-10-17 17:06:05 +02005804
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005805 if (appctx->st0 == STAT_HTTP_DONE) {
Willy Tarreau96d44912013-11-22 12:25:24 +01005806 if ((req->flags & CF_SHUTW) && (si->state == SI_ST_EST)) {
5807 si_shutr(si);
5808 res->flags |= CF_READ_NULL;
5809 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005810 }
Willy Tarreau828824a2015-04-19 17:20:03 +02005811 out:
Willy Tarreaud4da1962015-04-20 01:31:23 +02005812 /* just to make gcc happy */ ;
Willy Tarreau91861262007-10-17 17:06:05 +02005813}
5814
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005815
Willy Tarreau909d5172012-11-26 03:04:41 +01005816static inline const char *get_conn_ctrl_name(const struct connection *conn)
5817{
Willy Tarreau3c728722014-01-23 13:50:42 +01005818 if (!conn_ctrl_ready(conn))
Willy Tarreau909d5172012-11-26 03:04:41 +01005819 return "NONE";
5820 return conn->ctrl->name;
5821}
5822
5823static inline const char *get_conn_xprt_name(const struct connection *conn)
5824{
5825 static char ptr[17];
5826
Willy Tarreauaad69382014-01-23 14:21:42 +01005827 if (!conn_xprt_ready(conn))
Willy Tarreau909d5172012-11-26 03:04:41 +01005828 return "NONE";
5829
5830 if (conn->xprt == &raw_sock)
5831 return "RAW";
5832
5833#ifdef USE_OPENSSL
5834 if (conn->xprt == &ssl_sock)
5835 return "SSL";
5836#endif
5837 snprintf(ptr, sizeof(ptr), "%p", conn->xprt);
5838 return ptr;
5839}
5840
5841static inline const char *get_conn_data_name(const struct connection *conn)
5842{
5843 static char ptr[17];
5844
5845 if (!conn->data)
5846 return "NONE";
5847
5848 if (conn->data == &sess_conn_cb)
5849 return "SESS";
5850
5851 if (conn->data == &si_conn_cb)
5852 return "STRM";
5853
5854 if (conn->data == &check_conn_cb)
5855 return "CHCK";
5856
5857 snprintf(ptr, sizeof(ptr), "%p", conn->data);
5858 return ptr;
5859}
5860
Willy Tarreau87b09662015-04-03 00:22:06 +02005861/* This function dumps a complete stream state onto the stream interface's
5862 * read buffer. The stream has to be set in sess->target. It returns
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01005863 * 0 if the output buffer is full and it needs to be called again, otherwise
5864 * non-zero. It is designed to be called from stats_dump_sess_to_buffer() below.
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005865 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005866static int stats_dump_full_sess_to_buffer(struct stream_interface *si, struct stream *sess)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005867{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005868 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005869 struct tm tm;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005870 extern const char *monthname[12];
5871 char pn[INET6_ADDRSTRLEN];
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005872 struct connection *conn;
Willy Tarreau284ddbf2013-12-01 20:45:00 +01005873 struct appctx *tmpctx;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005874
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005875 chunk_reset(&trash);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005876
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005877 if (appctx->ctx.sess.section > 0 && appctx->ctx.sess.uid != sess->uniq_id) {
Willy Tarreau87b09662015-04-03 00:22:06 +02005878 /* stream changed, no need to go any further */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005879 chunk_appendf(&trash, " *** session terminated while we were watching it ***\n");
Willy Tarreaubc18da12015-03-13 14:00:47 +01005880 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02005881 si_applet_cant_put(si);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005882 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01005883 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005884 appctx->ctx.sess.uid = 0;
5885 appctx->ctx.sess.section = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005886 return 1;
5887 }
5888
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005889 switch (appctx->ctx.sess.section) {
Willy Tarreau87b09662015-04-03 00:22:06 +02005890 case 0: /* main status of the stream */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005891 appctx->ctx.sess.uid = sess->uniq_id;
5892 appctx->ctx.sess.section = 1;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005893 /* fall through */
5894
5895 case 1:
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005896 get_localtime(sess->logs.accept_date.tv_sec, &tm);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005897 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005898 "%p: [%02d/%s/%04d:%02d:%02d:%02d.%06d] id=%u proto=%s",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005899 sess,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005900 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
5901 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(sess->logs.accept_date.tv_usec),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005902 sess->uniq_id,
Willy Tarreau666f5042015-06-17 19:49:52 +02005903 strm_li(sess) ? strm_li(sess)->proto->name : "?");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005904
Willy Tarreaud0d8da92015-04-04 02:10:38 +02005905 conn = objt_conn(strm_orig(sess));
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005906 switch (conn ? addr_to_str(&conn->addr.from, pn, sizeof(pn)) : AF_UNSPEC) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005907 case AF_INET:
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005908 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005909 chunk_appendf(&trash, " source=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005910 pn, get_host_port(&conn->addr.from));
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005911 break;
5912 case AF_UNIX:
Willy Tarreaud0d8da92015-04-04 02:10:38 +02005913 chunk_appendf(&trash, " source=unix:%d\n", strm_li(sess)->luid);
Emeric Brun837ca522010-10-22 16:19:01 +02005914 break;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005915 default:
5916 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005917 chunk_appendf(&trash, "\n");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005918 break;
5919 }
5920
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005921 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005922 " flags=0x%x, conn_retries=%d, srv_conn=%p, pend_pos=%p\n",
Willy Tarreauee28de02010-06-01 09:51:00 +02005923 sess->flags, sess->si[1].conn_retries, sess->srv_conn, sess->pend_pos);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005924
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005925 chunk_appendf(&trash,
Daniel Schultze90690c72012-03-23 10:53:36 -07005926 " frontend=%s (id=%u mode=%s), listener=%s (id=%u)",
Willy Tarreaud0d8da92015-04-04 02:10:38 +02005927 strm_fe(sess)->id, strm_fe(sess)->uuid, strm_fe(sess)->mode ? "http" : "tcp",
5928 strm_li(sess) ? strm_li(sess)->name ? strm_li(sess)->name : "?" : "?",
5929 strm_li(sess) ? strm_li(sess)->luid : 0);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005930
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005931 if (conn)
5932 conn_get_to_addr(conn);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005933
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005934 switch (conn ? addr_to_str(&conn->addr.to, pn, sizeof(pn)) : AF_UNSPEC) {
Daniel Schultze90690c72012-03-23 10:53:36 -07005935 case AF_INET:
5936 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005937 chunk_appendf(&trash, " addr=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005938 pn, get_host_port(&conn->addr.to));
Daniel Schultze90690c72012-03-23 10:53:36 -07005939 break;
5940 case AF_UNIX:
Willy Tarreaud0d8da92015-04-04 02:10:38 +02005941 chunk_appendf(&trash, " addr=unix:%d\n", strm_li(sess)->luid);
Daniel Schultze90690c72012-03-23 10:53:36 -07005942 break;
5943 default:
5944 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005945 chunk_appendf(&trash, "\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07005946 break;
5947 }
5948
Willy Tarreau50943332011-09-02 17:33:05 +02005949 if (sess->be->cap & PR_CAP_BE)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005950 chunk_appendf(&trash,
Daniel Schultze90690c72012-03-23 10:53:36 -07005951 " backend=%s (id=%u mode=%s)",
Willy Tarreau50943332011-09-02 17:33:05 +02005952 sess->be->id,
Daniel Schultze90690c72012-03-23 10:53:36 -07005953 sess->be->uuid, sess->be->mode ? "http" : "tcp");
5954 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005955 chunk_appendf(&trash, " backend=<NONE> (id=-1 mode=-)");
Daniel Schultze90690c72012-03-23 10:53:36 -07005956
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005957 conn = objt_conn(sess->si[1].end);
5958 if (conn)
5959 conn_get_from_addr(conn);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005960
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005961 switch (conn ? addr_to_str(&conn->addr.from, pn, sizeof(pn)) : AF_UNSPEC) {
Daniel Schultze90690c72012-03-23 10:53:36 -07005962 case AF_INET:
5963 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005964 chunk_appendf(&trash, " addr=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005965 pn, get_host_port(&conn->addr.from));
Daniel Schultze90690c72012-03-23 10:53:36 -07005966 break;
5967 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005968 chunk_appendf(&trash, " addr=unix\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07005969 break;
5970 default:
5971 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005972 chunk_appendf(&trash, "\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07005973 break;
5974 }
5975
5976 if (sess->be->cap & PR_CAP_BE)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005977 chunk_appendf(&trash,
Daniel Schultze90690c72012-03-23 10:53:36 -07005978 " server=%s (id=%u)",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005979 objt_server(sess->target) ? objt_server(sess->target)->id : "<none>",
5980 objt_server(sess->target) ? objt_server(sess->target)->puid : 0);
Willy Tarreau50943332011-09-02 17:33:05 +02005981 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005982 chunk_appendf(&trash, " server=<NONE> (id=-1)");
Daniel Schultze90690c72012-03-23 10:53:36 -07005983
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005984 if (conn)
5985 conn_get_to_addr(conn);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005986
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005987 switch (conn ? addr_to_str(&conn->addr.to, pn, sizeof(pn)) : AF_UNSPEC) {
Daniel Schultze90690c72012-03-23 10:53:36 -07005988 case AF_INET:
5989 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005990 chunk_appendf(&trash, " addr=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005991 pn, get_host_port(&conn->addr.to));
Daniel Schultze90690c72012-03-23 10:53:36 -07005992 break;
5993 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005994 chunk_appendf(&trash, " addr=unix\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07005995 break;
5996 default:
5997 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005998 chunk_appendf(&trash, "\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07005999 break;
6000 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006001
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006002 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01006003 " task=%p (state=0x%02x nice=%d calls=%d exp=%s%s",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006004 sess->task,
6005 sess->task->state,
6006 sess->task->nice, sess->task->calls,
6007 sess->task->expire ?
6008 tick_is_expired(sess->task->expire, now_ms) ? "<PAST>" :
6009 human_time(TICKS_TO_MS(sess->task->expire - now_ms),
6010 TICKS_TO_MS(1000)) : "<NEVER>",
6011 task_in_rq(sess->task) ? ", running" : "");
6012
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006013 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01006014 " age=%s)\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006015 human_time(now.tv_sec - sess->logs.accept_date.tv_sec, 1));
6016
Willy Tarreaueee5b512015-04-03 23:46:31 +02006017 if (sess->txn)
6018 chunk_appendf(&trash,
Willy Tarreau98410192014-11-26 18:05:38 +01006019 " 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 +02006020 sess->txn, sess->txn->flags, sess->txn->meth, sess->txn->status,
6021 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 +01006022
6023 chunk_appendf(&trash,
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02006024 " si[0]=%p (state=%s flags=0x%02x endp0=%s:%p exp=%s, et=0x%03x)\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006025 &sess->si[0],
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01006026 si_state_str(sess->si[0].state),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006027 sess->si[0].flags,
Willy Tarreaucf644ed2013-09-29 17:19:56 +02006028 obj_type_name(sess->si[0].end),
6029 obj_base_ptr(sess->si[0].end),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006030 sess->si[0].exp ?
6031 tick_is_expired(sess->si[0].exp, now_ms) ? "<PAST>" :
6032 human_time(TICKS_TO_MS(sess->si[0].exp - now_ms),
6033 TICKS_TO_MS(1000)) : "<NEVER>",
6034 sess->si[0].err_type);
6035
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006036 chunk_appendf(&trash,
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02006037 " si[1]=%p (state=%s flags=0x%02x endp1=%s:%p exp=%s, et=0x%03x)\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006038 &sess->si[1],
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01006039 si_state_str(sess->si[1].state),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006040 sess->si[1].flags,
Willy Tarreaucf644ed2013-09-29 17:19:56 +02006041 obj_type_name(sess->si[1].end),
6042 obj_base_ptr(sess->si[1].end),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006043 sess->si[1].exp ?
6044 tick_is_expired(sess->si[1].exp, now_ms) ? "<PAST>" :
6045 human_time(TICKS_TO_MS(sess->si[1].exp - now_ms),
6046 TICKS_TO_MS(1000)) : "<NEVER>",
6047 sess->si[1].err_type);
6048
Willy Tarreau284ddbf2013-12-01 20:45:00 +01006049 if ((conn = objt_conn(sess->si[0].end)) != NULL) {
Willy Tarreaucf644ed2013-09-29 17:19:56 +02006050 chunk_appendf(&trash,
6051 " co0=%p ctrl=%s xprt=%s data=%s target=%s:%p\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006052 conn,
6053 get_conn_ctrl_name(conn),
6054 get_conn_xprt_name(conn),
6055 get_conn_data_name(conn),
6056 obj_type_name(conn->target),
6057 obj_base_ptr(conn->target));
Willy Tarreau909d5172012-11-26 03:04:41 +01006058
Willy Tarreaucf644ed2013-09-29 17:19:56 +02006059 chunk_appendf(&trash,
Willy Tarreau16f649c2014-01-25 19:10:48 +01006060 " flags=0x%08x fd=%d fd.state=%02x fd.cache=%d updt=%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006061 conn->flags,
6062 conn->t.sock.fd,
Willy Tarreau69a41fa2014-01-20 11:02:59 +01006063 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].state : 0,
Willy Tarreau15a4dec2014-01-20 11:09:39 +01006064 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].cache : 0,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006065 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].updated : 0);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02006066 }
Willy Tarreau284ddbf2013-12-01 20:45:00 +01006067 else if ((tmpctx = objt_appctx(sess->si[0].end)) != NULL) {
6068 chunk_appendf(&trash,
6069 " app0=%p st0=%d st1=%d st2=%d applet=%s\n",
6070 tmpctx,
6071 tmpctx->st0,
6072 tmpctx->st1,
6073 tmpctx->st2,
6074 tmpctx->applet->name);
6075 }
Willy Tarreaubc174aa2012-11-19 16:10:32 +01006076
Willy Tarreau284ddbf2013-12-01 20:45:00 +01006077 if ((conn = objt_conn(sess->si[1].end)) != NULL) {
Willy Tarreaucf644ed2013-09-29 17:19:56 +02006078 chunk_appendf(&trash,
6079 " co1=%p ctrl=%s xprt=%s data=%s target=%s:%p\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006080 conn,
6081 get_conn_ctrl_name(conn),
6082 get_conn_xprt_name(conn),
6083 get_conn_data_name(conn),
6084 obj_type_name(conn->target),
6085 obj_base_ptr(conn->target));
Willy Tarreau909d5172012-11-26 03:04:41 +01006086
Willy Tarreaucf644ed2013-09-29 17:19:56 +02006087 chunk_appendf(&trash,
Willy Tarreauceeafb52016-01-25 15:27:17 +01006088 " flags=0x%08x fd=%d fd.state=%02x fd.cache=%d updt=%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006089 conn->flags,
6090 conn->t.sock.fd,
Willy Tarreau69a41fa2014-01-20 11:02:59 +01006091 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].state : 0,
Willy Tarreau15a4dec2014-01-20 11:09:39 +01006092 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].cache : 0,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006093 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].updated : 0);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02006094 }
Willy Tarreau284ddbf2013-12-01 20:45:00 +01006095 else if ((tmpctx = objt_appctx(sess->si[1].end)) != NULL) {
6096 chunk_appendf(&trash,
6097 " app1=%p st0=%d st1=%d st2=%d applet=%s\n",
6098 tmpctx,
6099 tmpctx->st0,
6100 tmpctx->st1,
6101 tmpctx->st2,
6102 tmpctx->applet->name);
6103 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006104
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006105 chunk_appendf(&trash,
Willy Tarreau909d5172012-11-26 03:04:41 +01006106 " req=%p (f=0x%06x an=0x%x pipe=%d tofwd=%d total=%lld)\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006107 " an_exp=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006108 &sess->req,
6109 sess->req.flags, sess->req.analysers,
6110 sess->req.pipe ? sess->req.pipe->data : 0,
6111 sess->req.to_forward, sess->req.total,
6112 sess->req.analyse_exp ?
6113 human_time(TICKS_TO_MS(sess->req.analyse_exp - 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 " rex=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006118 sess->req.rex ?
6119 human_time(TICKS_TO_MS(sess->req.rex - now_ms),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006120 TICKS_TO_MS(1000)) : "<NEVER>");
6121
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006122 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006123 " wex=%s\n"
Willy Tarreau909d5172012-11-26 03:04:41 +01006124 " buf=%p data=%p o=%d p=%d req.next=%d i=%d size=%d\n",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006125 sess->req.wex ?
6126 human_time(TICKS_TO_MS(sess->req.wex - now_ms),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006127 TICKS_TO_MS(1000)) : "<NEVER>",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006128 sess->req.buf,
6129 sess->req.buf->data, sess->req.buf->o,
6130 (int)(sess->req.buf->p - sess->req.buf->data),
Willy Tarreaueee5b512015-04-03 23:46:31 +02006131 sess->txn ? sess->txn->req.next : 0, sess->req.buf->i,
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006132 sess->req.buf->size);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006133
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006134 chunk_appendf(&trash,
Willy Tarreau909d5172012-11-26 03:04:41 +01006135 " res=%p (f=0x%06x an=0x%x pipe=%d tofwd=%d total=%lld)\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006136 " an_exp=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006137 &sess->res,
6138 sess->res.flags, sess->res.analysers,
6139 sess->res.pipe ? sess->res.pipe->data : 0,
6140 sess->res.to_forward, sess->res.total,
6141 sess->res.analyse_exp ?
6142 human_time(TICKS_TO_MS(sess->res.analyse_exp - now_ms),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006143 TICKS_TO_MS(1000)) : "<NEVER>");
6144
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006145 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006146 " rex=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006147 sess->res.rex ?
6148 human_time(TICKS_TO_MS(sess->res.rex - now_ms),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006149 TICKS_TO_MS(1000)) : "<NEVER>");
6150
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006151 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006152 " wex=%s\n"
Willy Tarreau909d5172012-11-26 03:04:41 +01006153 " buf=%p data=%p o=%d p=%d rsp.next=%d i=%d size=%d\n",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006154 sess->res.wex ?
6155 human_time(TICKS_TO_MS(sess->res.wex - now_ms),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006156 TICKS_TO_MS(1000)) : "<NEVER>",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006157 sess->res.buf,
6158 sess->res.buf->data, sess->res.buf->o,
6159 (int)(sess->res.buf->p - sess->res.buf->data),
Willy Tarreaueee5b512015-04-03 23:46:31 +02006160 sess->txn ? sess->txn->rsp.next : 0, sess->res.buf->i,
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006161 sess->res.buf->size);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006162
Willy Tarreaubc18da12015-03-13 14:00:47 +01006163 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02006164 si_applet_cant_put(si);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006165 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01006166 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006167
6168 /* use other states to dump the contents */
6169 }
6170 /* end of dump */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006171 appctx->ctx.sess.uid = 0;
6172 appctx->ctx.sess.section = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006173 return 1;
6174}
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006175
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02006176#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
6177static int stats_tlskeys_list(struct stream_interface *si) {
6178 struct appctx *appctx = __objt_appctx(si->end);
6179
6180 switch (appctx->st2) {
6181 case STAT_ST_INIT:
6182 /* Display the column headers. If the message cannot be sent,
6183 * quit the fucntion with returning 0. The function is called
6184 * later and restart at the state "STAT_ST_INIT".
6185 */
6186 chunk_reset(&trash);
William Lallemand1d0b36a2016-05-20 17:40:26 +02006187
6188 if (appctx->st0 == STAT_CLI_O_TLSK_ENT)
6189 chunk_appendf(&trash, "# id secret\n");
6190 else
6191 chunk_appendf(&trash, "# id (file)\n");
6192
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02006193 if (bi_putchk(si_ic(si), &trash) == -1) {
6194 si_applet_cant_put(si);
6195 return 0;
6196 }
6197
William Lallemand72a8a182016-06-14 18:58:55 +02006198 appctx->ctx.tlskeys.dump_keys_index = 0;
William Lallemand1d0b36a2016-05-20 17:40:26 +02006199
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02006200 /* Now, we start the browsing of the references lists.
6201 * Note that the following call to LIST_ELEM return bad pointer. The only
Cyril Bontédb98eb32016-05-06 12:18:50 +02006202 * available field of this pointer is <list>. It is used with the function
6203 * tlskeys_list_get_next() for retruning the first available entry
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02006204 */
William Lallemandcf9e7882016-06-14 17:45:18 +02006205 if (appctx->ctx.tlskeys.ref == NULL) {
6206 appctx->ctx.tlskeys.ref = LIST_ELEM(&tlskeys_reference, struct tls_keys_ref *, list);
6207 appctx->ctx.tlskeys.ref = tlskeys_list_get_next(appctx->ctx.tlskeys.ref, &tlskeys_reference);
William Lallemand1d0b36a2016-05-20 17:40:26 +02006208 }
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02006209
6210 appctx->st2 = STAT_ST_LIST;
6211 /* fall through */
6212
6213 case STAT_ST_LIST:
William Lallemandcf9e7882016-06-14 17:45:18 +02006214 while (appctx->ctx.tlskeys.ref) {
William Lallemandcf9e7882016-06-14 17:45:18 +02006215 int head = appctx->ctx.tlskeys.ref->tls_ticket_enc_index;
William Lallemand1d0b36a2016-05-20 17:40:26 +02006216
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02006217 chunk_reset(&trash);
William Lallemand72a8a182016-06-14 18:58:55 +02006218 if (appctx->st0 == STAT_CLI_O_TLSK_ENT && appctx->ctx.tlskeys.dump_keys_index == 0)
William Lallemand1d0b36a2016-05-20 17:40:26 +02006219 chunk_appendf(&trash, "# ");
William Lallemand72a8a182016-06-14 18:58:55 +02006220 if (appctx->ctx.tlskeys.dump_keys_index == 0)
6221 chunk_appendf(&trash, "%d (%s)\n", appctx->ctx.tlskeys.ref->unique_id,
6222 appctx->ctx.tlskeys.ref->filename);
William Lallemand1d0b36a2016-05-20 17:40:26 +02006223 if (appctx->st0 == STAT_CLI_O_TLSK_ENT) {
William Lallemand72a8a182016-06-14 18:58:55 +02006224 while (appctx->ctx.tlskeys.dump_keys_index < TLS_TICKETS_NO) {
William Lallemand1d0b36a2016-05-20 17:40:26 +02006225 struct chunk *t2 = get_trash_chunk();
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02006226
William Lallemand1d0b36a2016-05-20 17:40:26 +02006227 chunk_reset(t2);
William Lallemand72a8a182016-06-14 18:58:55 +02006228 /* should never fail here because we dump only a key in the t2 buffer */
6229 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 +02006230 sizeof(struct tls_sess_key), t2->str, t2->size);
William Lallemand72a8a182016-06-14 18:58:55 +02006231 chunk_appendf(&trash, "%d.%d %s\n", appctx->ctx.tlskeys.ref->unique_id, appctx->ctx.tlskeys.dump_keys_index, t2->str);
6232
6233 if (bi_putchk(si_ic(si), &trash) == -1) {
6234 /* let's try again later from this stream. We add ourselves into
6235 * this stream's users so that it can remove us upon termination.
6236 */
6237 si_applet_cant_put(si);
William Lallemand1d0b36a2016-05-20 17:40:26 +02006238 return 0;
William Lallemand72a8a182016-06-14 18:58:55 +02006239 }
6240 appctx->ctx.tlskeys.dump_keys_index++;
William Lallemand1d0b36a2016-05-20 17:40:26 +02006241 }
William Lallemand72a8a182016-06-14 18:58:55 +02006242 appctx->ctx.tlskeys.dump_keys_index = 0;
William Lallemand1d0b36a2016-05-20 17:40:26 +02006243 }
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02006244 if (bi_putchk(si_ic(si), &trash) == -1) {
6245 /* let's try again later from this stream. We add ourselves into
6246 * this stream's users so that it can remove us upon termination.
6247 */
6248 si_applet_cant_put(si);
6249 return 0;
6250 }
6251
William Lallemandcf9e7882016-06-14 17:45:18 +02006252 if (appctx->ctx.tlskeys.dump_all == 0) /* don't display everything if not necessary */
William Lallemand1d0b36a2016-05-20 17:40:26 +02006253 break;
6254
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02006255 /* get next list entry and check the end of the list */
William Lallemandcf9e7882016-06-14 17:45:18 +02006256 appctx->ctx.tlskeys.ref = tlskeys_list_get_next(appctx->ctx.tlskeys.ref, &tlskeys_reference);
William Lallemand1d0b36a2016-05-20 17:40:26 +02006257
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02006258 }
6259
6260 appctx->st2 = STAT_ST_FIN;
6261 /* fall through */
6262
6263 default:
6264 appctx->st2 = STAT_ST_FIN;
6265 return 1;
6266 }
6267 return 0;
6268}
6269#endif
6270
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006271static int stats_pats_list(struct stream_interface *si)
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006272{
6273 struct appctx *appctx = __objt_appctx(si->end);
6274
6275 switch (appctx->st2) {
6276 case STAT_ST_INIT:
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01006277 /* Display the column headers. If the message cannot be sent,
6278 * quit the fucntion with returning 0. The function is called
6279 * later and restart at the state "STAT_ST_INIT".
6280 */
6281 chunk_reset(&trash);
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01006282 chunk_appendf(&trash, "# id (file) description\n");
Willy Tarreaubc18da12015-03-13 14:00:47 +01006283 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02006284 si_applet_cant_put(si);
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01006285 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01006286 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006287
6288 /* Now, we start the browsing of the references lists.
6289 * Note that the following call to LIST_ELEM return bad pointer. The only
Cyril Bontédb98eb32016-05-06 12:18:50 +02006290 * available field of this pointer is <list>. It is used with the function
6291 * pat_list_get_next() for retruning the first available entry
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006292 */
6293 appctx->ctx.map.ref = LIST_ELEM(&pattern_reference, struct pat_ref *, list);
6294 appctx->ctx.map.ref = pat_list_get_next(appctx->ctx.map.ref, &pattern_reference,
6295 appctx->ctx.map.display_flags);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006296 appctx->st2 = STAT_ST_LIST;
6297 /* fall through */
6298
6299 case STAT_ST_LIST:
6300 while (appctx->ctx.map.ref) {
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006301 chunk_reset(&trash);
6302
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006303 /* Build messages. If the reference is used by another category than
6304 * the listed categorie, display the information in the massage.
6305 */
Thierry FOURNIERf7e04e92014-03-20 11:45:47 +01006306 chunk_appendf(&trash, "%d (%s) %s\n", appctx->ctx.map.ref->unique_id,
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01006307 appctx->ctx.map.ref->reference ? appctx->ctx.map.ref->reference : "",
6308 appctx->ctx.map.ref->display);
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01006309
Willy Tarreau2bb4a962014-11-28 11:11:05 +01006310 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau87b09662015-04-03 00:22:06 +02006311 /* let's try again later from this stream. We add ourselves into
6312 * this stream's users so that it can remove us upon termination.
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006313 */
Willy Tarreaufe127932015-04-21 19:23:39 +02006314 si_applet_cant_put(si);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006315 return 0;
6316 }
6317
6318 /* get next list entry and check the end of the list */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006319 appctx->ctx.map.ref = pat_list_get_next(appctx->ctx.map.ref, &pattern_reference,
6320 appctx->ctx.map.display_flags);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006321 }
6322
6323 appctx->st2 = STAT_ST_FIN;
6324 /* fall through */
6325
6326 default:
6327 appctx->st2 = STAT_ST_FIN;
6328 return 1;
6329 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006330 return 0;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006331}
6332
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006333static int stats_map_lookup(struct stream_interface *si)
6334{
6335 struct appctx *appctx = __objt_appctx(si->end);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006336 struct sample sample;
6337 struct pattern *pat;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006338 int match_method;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006339
6340 switch (appctx->st2) {
6341 case STAT_ST_INIT:
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006342 /* Init to the first entry. The list cannot be change */
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01006343 appctx->ctx.map.expr = LIST_ELEM(&appctx->ctx.map.ref->pat, struct pattern_expr *, list);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006344 appctx->ctx.map.expr = pat_expr_get_next(appctx->ctx.map.expr, &appctx->ctx.map.ref->pat);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006345 appctx->st2 = STAT_ST_LIST;
6346 /* fall through */
6347
6348 case STAT_ST_LIST:
6349 /* for each lookup type */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006350 while (appctx->ctx.map.expr) {
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006351 /* initialise chunk to build new message */
6352 chunk_reset(&trash);
6353
6354 /* execute pattern matching */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02006355 sample.data.type = SMP_T_STR;
Andreas Seltenreich9727cf42016-03-03 19:32:25 +01006356 sample.flags = SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02006357 sample.data.u.str.len = appctx->ctx.map.chunk.len;
6358 sample.data.u.str.str = appctx->ctx.map.chunk.str;
Thierry FOURNIER5d344082014-01-27 14:19:53 +01006359 if (appctx->ctx.map.expr->pat_head->match &&
6360 sample_convert(&sample, appctx->ctx.map.expr->pat_head->expect_type))
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006361 pat = appctx->ctx.map.expr->pat_head->match(&sample, appctx->ctx.map.expr, 1);
6362 else
6363 pat = NULL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006364
6365 /* build return message: set type of match */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006366 for (match_method=0; match_method<PAT_MATCH_NUM; match_method++)
6367 if (appctx->ctx.map.expr->pat_head->match == pat_match_fcts[match_method])
6368 break;
6369 if (match_method >= PAT_MATCH_NUM)
6370 chunk_appendf(&trash, "type=unknown(%p)", appctx->ctx.map.expr->pat_head->match);
6371 else
6372 chunk_appendf(&trash, "type=%s", pat_match_names[match_method]);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006373
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02006374 /* case sensitive */
6375 if (appctx->ctx.map.expr->mflags & PAT_MF_IGNORE_CASE)
6376 chunk_appendf(&trash, ", case=insensitive");
6377 else
6378 chunk_appendf(&trash, ", case=sensitive");
6379
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006380 /* Display no match, and set default value */
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01006381 if (!pat) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006382 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
6383 chunk_appendf(&trash, ", found=no");
6384 else
6385 chunk_appendf(&trash, ", match=no");
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006386 }
6387
6388 /* Display match and match info */
6389 else {
6390 /* display match */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006391 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
6392 chunk_appendf(&trash, ", found=yes");
6393 else
6394 chunk_appendf(&trash, ", match=yes");
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006395
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01006396 /* display index mode */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02006397 if (pat->sflags & PAT_SF_TREE)
Thierry FOURNIERb9903842014-03-11 18:48:17 +01006398 chunk_appendf(&trash, ", idx=tree");
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006399 else
Thierry FOURNIERb9903842014-03-11 18:48:17 +01006400 chunk_appendf(&trash, ", idx=list");
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006401
Thierry FOURNIER86db66a2014-03-11 18:37:58 +01006402 /* display pattern */
6403 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
6404 if (pat->ref && pat->ref->pattern)
6405 chunk_appendf(&trash, ", key=\"%s\"", pat->ref->pattern);
6406 else
6407 chunk_appendf(&trash, ", key=unknown");
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006408 }
6409 else {
Thierry FOURNIER86db66a2014-03-11 18:37:58 +01006410 if (pat->ref && pat->ref->pattern)
6411 chunk_appendf(&trash, ", pattern=\"%s\"", pat->ref->pattern);
6412 else
6413 chunk_appendf(&trash, ", pattern=unknown");
6414 }
6415
6416 /* display return value */
6417 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
Thierry FOURNIER503bb092015-08-19 08:35:43 +02006418 if (pat->data && pat->ref && pat->ref->sample)
Thierry FOURNIER03d0e452015-07-23 18:33:41 +02006419 chunk_appendf(&trash, ", value=\"%s\", type=\"%s\"", pat->ref->sample,
Thierry FOURNIER7d4335c2015-08-19 09:05:25 +02006420 smp_to_type[pat->data->type]);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006421 else
Thierry FOURNIER86db66a2014-03-11 18:37:58 +01006422 chunk_appendf(&trash, ", value=none");
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006423 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006424 }
6425
Thierry FOURNIERb9903842014-03-11 18:48:17 +01006426 chunk_appendf(&trash, "\n");
6427
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006428 /* display response */
Willy Tarreau2bb4a962014-11-28 11:11:05 +01006429 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau87b09662015-04-03 00:22:06 +02006430 /* let's try again later from this stream. We add ourselves into
6431 * this stream's users so that it can remove us upon termination.
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006432 */
Willy Tarreaufe127932015-04-21 19:23:39 +02006433 si_applet_cant_put(si);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006434 return 0;
6435 }
6436
6437 /* get next entry */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006438 appctx->ctx.map.expr = pat_expr_get_next(appctx->ctx.map.expr,
6439 &appctx->ctx.map.ref->pat);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006440 }
6441
6442 appctx->st2 = STAT_ST_FIN;
6443 /* fall through */
6444
6445 default:
6446 appctx->st2 = STAT_ST_FIN;
6447 free(appctx->ctx.map.chunk.str);
6448 return 1;
6449 }
6450}
6451
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006452static int stats_pat_list(struct stream_interface *si)
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006453{
6454 struct appctx *appctx = __objt_appctx(si->end);
6455
6456 switch (appctx->st2) {
6457
6458 case STAT_ST_INIT:
6459 /* Init to the first entry. The list cannot be change */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006460 appctx->ctx.map.elt = LIST_NEXT(&appctx->ctx.map.ref->head,
6461 struct pat_ref_elt *, list);
6462 if (&appctx->ctx.map.elt->list == &appctx->ctx.map.ref->head)
6463 appctx->ctx.map.elt = NULL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006464 appctx->st2 = STAT_ST_LIST;
6465 /* fall through */
6466
6467 case STAT_ST_LIST:
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006468 while (appctx->ctx.map.elt) {
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006469 chunk_reset(&trash);
6470
6471 /* build messages */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006472 if (appctx->ctx.map.elt->sample)
Thierry FOURNIER9356c682014-01-28 15:55:37 +01006473 chunk_appendf(&trash, "%p %s %s\n",
6474 appctx->ctx.map.elt, appctx->ctx.map.elt->pattern,
6475 appctx->ctx.map.elt->sample);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006476 else
Thierry FOURNIER9356c682014-01-28 15:55:37 +01006477 chunk_appendf(&trash, "%p %s\n",
6478 appctx->ctx.map.elt, appctx->ctx.map.elt->pattern);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006479
Willy Tarreau2bb4a962014-11-28 11:11:05 +01006480 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau87b09662015-04-03 00:22:06 +02006481 /* let's try again later from this stream. We add ourselves into
6482 * this stream's users so that it can remove us upon termination.
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006483 */
Willy Tarreaufe127932015-04-21 19:23:39 +02006484 si_applet_cant_put(si);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006485 return 0;
6486 }
6487
6488 /* get next list entry and check the end of the list */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006489 appctx->ctx.map.elt = LIST_NEXT(&appctx->ctx.map.elt->list,
6490 struct pat_ref_elt *, list);
6491 if (&appctx->ctx.map.elt->list == &appctx->ctx.map.ref->head)
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006492 break;
6493 }
6494
6495 appctx->st2 = STAT_ST_FIN;
6496 /* fall through */
6497
6498 default:
6499 appctx->st2 = STAT_ST_FIN;
6500 return 1;
6501 }
6502}
6503
Willy Tarreau87b09662015-04-03 00:22:06 +02006504/* This function dumps all streams' states onto the stream interface's
Willy Tarreau306f8302013-07-08 15:53:06 +02006505 * read buffer. It returns 0 if the output buffer is full and it needs
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01006506 * to be called again, otherwise non-zero. It is designed to be called
6507 * from stats_dump_sess_to_buffer() below.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006508 */
Simon Horman9bd2c732011-06-15 15:18:44 +09006509static int stats_dump_sess_to_buffer(struct stream_interface *si)
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006510{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006511 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006512 struct connection *conn;
6513
Willy Tarreau2bb4a962014-11-28 11:11:05 +01006514 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW))) {
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006515 /* If we're forced to shut down, we might have to remove our
Willy Tarreau87b09662015-04-03 00:22:06 +02006516 * reference to the last stream being dumped.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006517 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006518 if (appctx->st2 == STAT_ST_LIST) {
6519 if (!LIST_ISEMPTY(&appctx->ctx.sess.bref.users)) {
6520 LIST_DEL(&appctx->ctx.sess.bref.users);
6521 LIST_INIT(&appctx->ctx.sess.bref.users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +01006522 }
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006523 }
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02006524 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006525 }
6526
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006527 chunk_reset(&trash);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006528
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006529 switch (appctx->st2) {
Willy Tarreau295a8372011-03-10 11:25:07 +01006530 case STAT_ST_INIT:
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006531 /* the function had not been called yet, let's prepare the
Willy Tarreau87b09662015-04-03 00:22:06 +02006532 * buffer for a response. We initialize the current stream
Willy Tarreaufd3828e2009-02-22 15:17:24 +01006533 * pointer to the first in the global list. When a target
Willy Tarreau87b09662015-04-03 00:22:06 +02006534 * stream is being destroyed, it is responsible for updating
Willy Tarreaufd3828e2009-02-22 15:17:24 +01006535 * this pointer. We know we have reached the end when this
Willy Tarreau87b09662015-04-03 00:22:06 +02006536 * pointer points back to the head of the streams list.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006537 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006538 LIST_INIT(&appctx->ctx.sess.bref.users);
Willy Tarreau87b09662015-04-03 00:22:06 +02006539 appctx->ctx.sess.bref.ref = streams.n;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006540 appctx->st2 = STAT_ST_LIST;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006541 /* fall through */
6542
Willy Tarreau295a8372011-03-10 11:25:07 +01006543 case STAT_ST_LIST:
Willy Tarreau87b09662015-04-03 00:22:06 +02006544 /* first, let's detach the back-ref from a possible previous stream */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006545 if (!LIST_ISEMPTY(&appctx->ctx.sess.bref.users)) {
6546 LIST_DEL(&appctx->ctx.sess.bref.users);
6547 LIST_INIT(&appctx->ctx.sess.bref.users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +01006548 }
6549
6550 /* and start from where we stopped */
Willy Tarreau87b09662015-04-03 00:22:06 +02006551 while (appctx->ctx.sess.bref.ref != &streams) {
Cyril Bontéacd7d632010-11-01 19:26:02 +01006552 char pn[INET6_ADDRSTRLEN];
Willy Tarreau87b09662015-04-03 00:22:06 +02006553 struct stream *curr_sess;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006554
Willy Tarreau87b09662015-04-03 00:22:06 +02006555 curr_sess = LIST_ELEM(appctx->ctx.sess.bref.ref, struct stream *, list);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006556
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006557 if (appctx->ctx.sess.target) {
6558 if (appctx->ctx.sess.target != (void *)-1 && appctx->ctx.sess.target != curr_sess)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006559 goto next_sess;
6560
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006561 LIST_ADDQ(&curr_sess->back_refs, &appctx->ctx.sess.bref.users);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006562 /* call the proper dump() function and return if we're missing space */
Willy Tarreau76153662012-11-26 01:16:39 +01006563 if (!stats_dump_full_sess_to_buffer(si, curr_sess))
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006564 return 0;
6565
Willy Tarreau87b09662015-04-03 00:22:06 +02006566 /* stream dump complete */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006567 LIST_DEL(&appctx->ctx.sess.bref.users);
6568 LIST_INIT(&appctx->ctx.sess.bref.users);
6569 if (appctx->ctx.sess.target != (void *)-1) {
6570 appctx->ctx.sess.target = NULL;
Willy Tarreau76153662012-11-26 01:16:39 +01006571 break;
6572 }
6573 else
6574 goto next_sess;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006575 }
6576
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006577 chunk_appendf(&trash,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006578 "%p: proto=%s",
6579 curr_sess,
Willy Tarreauf1e02122015-09-23 12:16:43 +02006580 strm_li(curr_sess) ? strm_li(curr_sess)->proto->name : "?");
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006581
Willy Tarreaud0d8da92015-04-04 02:10:38 +02006582 conn = objt_conn(strm_orig(curr_sess));
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006583 switch (conn ? addr_to_str(&conn->addr.from, pn, sizeof(pn)) : AF_UNSPEC) {
Willy Tarreau631f01c2011-09-05 00:36:48 +02006584 case AF_INET:
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006585 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006586 chunk_appendf(&trash,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006587 " src=%s:%d fe=%s be=%s srv=%s",
6588 pn,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006589 get_host_port(&conn->addr.from),
Willy Tarreaud0d8da92015-04-04 02:10:38 +02006590 strm_fe(curr_sess)->id,
Willy Tarreau50943332011-09-02 17:33:05 +02006591 (curr_sess->be->cap & PR_CAP_BE) ? curr_sess->be->id : "<NONE>",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006592 objt_server(curr_sess->target) ? objt_server(curr_sess->target)->id : "<none>"
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006593 );
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006594 break;
6595 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006596 chunk_appendf(&trash,
Emeric Brun837ca522010-10-22 16:19:01 +02006597 " src=unix:%d fe=%s be=%s srv=%s",
Willy Tarreaud0d8da92015-04-04 02:10:38 +02006598 strm_li(curr_sess)->luid,
6599 strm_fe(curr_sess)->id,
Willy Tarreau50943332011-09-02 17:33:05 +02006600 (curr_sess->be->cap & PR_CAP_BE) ? curr_sess->be->id : "<NONE>",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006601 objt_server(curr_sess->target) ? objt_server(curr_sess->target)->id : "<none>"
Emeric Brun837ca522010-10-22 16:19:01 +02006602 );
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006603 break;
6604 }
6605
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006606 chunk_appendf(&trash,
Willy Tarreau65671ab2009-10-04 14:24:59 +02006607 " ts=%02x age=%s calls=%d",
6608 curr_sess->task->state,
Willy Tarreau3884cba2009-03-28 17:54:35 +01006609 human_time(now.tv_sec - curr_sess->logs.tv_accept.tv_sec, 1),
6610 curr_sess->task->calls);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006611
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006612 chunk_appendf(&trash,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01006613 " rq[f=%06xh,i=%d,an=%02xh,rx=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006614 curr_sess->req.flags,
6615 curr_sess->req.buf->i,
6616 curr_sess->req.analysers,
6617 curr_sess->req.rex ?
6618 human_time(TICKS_TO_MS(curr_sess->req.rex - now_ms),
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006619 TICKS_TO_MS(1000)) : "");
6620
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006621 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006622 ",wx=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006623 curr_sess->req.wex ?
6624 human_time(TICKS_TO_MS(curr_sess->req.wex - now_ms),
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006625 TICKS_TO_MS(1000)) : "");
6626
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006627 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006628 ",ax=%s]",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006629 curr_sess->req.analyse_exp ?
6630 human_time(TICKS_TO_MS(curr_sess->req.analyse_exp - now_ms),
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006631 TICKS_TO_MS(1000)) : "");
6632
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006633 chunk_appendf(&trash,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01006634 " rp[f=%06xh,i=%d,an=%02xh,rx=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006635 curr_sess->res.flags,
6636 curr_sess->res.buf->i,
6637 curr_sess->res.analysers,
6638 curr_sess->res.rex ?
6639 human_time(TICKS_TO_MS(curr_sess->res.rex - now_ms),
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006640 TICKS_TO_MS(1000)) : "");
6641
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006642 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006643 ",wx=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006644 curr_sess->res.wex ?
6645 human_time(TICKS_TO_MS(curr_sess->res.wex - now_ms),
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006646 TICKS_TO_MS(1000)) : "");
6647
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006648 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006649 ",ax=%s]",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006650 curr_sess->res.analyse_exp ?
6651 human_time(TICKS_TO_MS(curr_sess->res.analyse_exp - now_ms),
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006652 TICKS_TO_MS(1000)) : "");
6653
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006654 conn = objt_conn(curr_sess->si[0].end);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006655 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006656 " s0=[%d,%1xh,fd=%d,ex=%s]",
6657 curr_sess->si[0].state,
6658 curr_sess->si[0].flags,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006659 conn ? conn->t.sock.fd : -1,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006660 curr_sess->si[0].exp ?
6661 human_time(TICKS_TO_MS(curr_sess->si[0].exp - now_ms),
6662 TICKS_TO_MS(1000)) : "");
6663
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006664 conn = objt_conn(curr_sess->si[1].end);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006665 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006666 " s1=[%d,%1xh,fd=%d,ex=%s]",
6667 curr_sess->si[1].state,
6668 curr_sess->si[1].flags,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006669 conn ? conn->t.sock.fd : -1,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006670 curr_sess->si[1].exp ?
6671 human_time(TICKS_TO_MS(curr_sess->si[1].exp - now_ms),
6672 TICKS_TO_MS(1000)) : "");
6673
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006674 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006675 " exp=%s",
6676 curr_sess->task->expire ?
6677 human_time(TICKS_TO_MS(curr_sess->task->expire - now_ms),
6678 TICKS_TO_MS(1000)) : "");
Willy Tarreau4726f532009-03-07 17:25:21 +01006679 if (task_in_rq(curr_sess->task))
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006680 chunk_appendf(&trash, " run(nice=%d)", curr_sess->task->nice);
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006681
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006682 chunk_appendf(&trash, "\n");
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006683
Willy Tarreau2bb4a962014-11-28 11:11:05 +01006684 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau87b09662015-04-03 00:22:06 +02006685 /* let's try again later from this stream. We add ourselves into
6686 * this stream's users so that it can remove us upon termination.
Willy Tarreaufd3828e2009-02-22 15:17:24 +01006687 */
Willy Tarreaufe127932015-04-21 19:23:39 +02006688 si_applet_cant_put(si);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006689 LIST_ADDQ(&curr_sess->back_refs, &appctx->ctx.sess.bref.users);
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02006690 return 0;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006691 }
6692
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006693 next_sess:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006694 appctx->ctx.sess.bref.ref = curr_sess->list.n;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006695 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006696
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006697 if (appctx->ctx.sess.target && appctx->ctx.sess.target != (void *)-1) {
Willy Tarreau87b09662015-04-03 00:22:06 +02006698 /* specified stream not found */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006699 if (appctx->ctx.sess.section > 0)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006700 chunk_appendf(&trash, " *** session terminated while we were watching it ***\n");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006701 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006702 chunk_appendf(&trash, "Session not found.\n");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006703
Willy Tarreaubc18da12015-03-13 14:00:47 +01006704 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02006705 si_applet_cant_put(si);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006706 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01006707 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006708
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006709 appctx->ctx.sess.target = NULL;
6710 appctx->ctx.sess.uid = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006711 return 1;
6712 }
6713
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006714 appctx->st2 = STAT_ST_FIN;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006715 /* fall through */
6716
6717 default:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006718 appctx->st2 = STAT_ST_FIN;
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02006719 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006720 }
Emeric Brun1e029aa2010-09-23 18:12:53 +02006721}
6722
Willy Tarreau5f9a8772012-11-26 02:22:40 +01006723/* This is called when the stream interface is closed. For instance, upon an
6724 * external abort, we won't call the i/o handler anymore so we may need to
Willy Tarreau87b09662015-04-03 00:22:06 +02006725 * remove back references to the stream currently being dumped.
Willy Tarreau5f9a8772012-11-26 02:22:40 +01006726 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02006727static void cli_release_handler(struct appctx *appctx)
Willy Tarreau5f9a8772012-11-26 02:22:40 +01006728{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006729 if (appctx->st0 == STAT_CLI_O_SESS && appctx->st2 == STAT_ST_LIST) {
6730 if (!LIST_ISEMPTY(&appctx->ctx.sess.bref.users))
6731 LIST_DEL(&appctx->ctx.sess.bref.users);
Willy Tarreau5f9a8772012-11-26 02:22:40 +01006732 }
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01006733 else if (appctx->st0 == STAT_CLI_PRINT_FREE) {
6734 free(appctx->ctx.cli.err);
Willy Tarreau6457d0f2015-10-07 20:00:24 +02006735 appctx->ctx.cli.err = NULL;
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01006736 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006737 else if (appctx->st0 == STAT_CLI_O_MLOOK) {
6738 free(appctx->ctx.map.chunk.str);
Willy Tarreau6457d0f2015-10-07 20:00:24 +02006739 appctx->ctx.map.chunk.str = NULL;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006740 }
Willy Tarreau5f9a8772012-11-26 02:22:40 +01006741}
6742
Willy Tarreau20e99322013-04-13 09:22:25 +02006743/* This function is used to either dump tables states (when action is set
6744 * to STAT_CLI_O_TAB) or clear tables (when action is STAT_CLI_O_CLR).
Willy Tarreau20e99322013-04-13 09:22:25 +02006745 * It returns 0 if the output buffer is full and it needs to be called
6746 * again, otherwise non-zero.
Willy Tarreau69f58c82010-07-12 17:55:33 +02006747 */
Willy Tarreau20e99322013-04-13 09:22:25 +02006748static int stats_table_request(struct stream_interface *si, int action)
Willy Tarreau69f58c82010-07-12 17:55:33 +02006749{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006750 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau87b09662015-04-03 00:22:06 +02006751 struct stream *s = si_strm(si);
Willy Tarreau69f58c82010-07-12 17:55:33 +02006752 struct ebmb_node *eb;
6753 int dt;
Willy Tarreau44455022012-12-05 23:01:12 +01006754 int skip_entry;
Willy Tarreau20e99322013-04-13 09:22:25 +02006755 int show = action == STAT_CLI_O_TAB;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006756
6757 /*
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006758 * We have 3 possible states in appctx->st2 :
Willy Tarreau295a8372011-03-10 11:25:07 +01006759 * - STAT_ST_INIT : the first call
6760 * - STAT_ST_INFO : the proxy pointer points to the next table to
Willy Tarreau69f58c82010-07-12 17:55:33 +02006761 * dump, the entry pointer is NULL ;
Willy Tarreau295a8372011-03-10 11:25:07 +01006762 * - STAT_ST_LIST : the proxy pointer points to the current table
Willy Tarreau69f58c82010-07-12 17:55:33 +02006763 * and the entry pointer points to the next entry to be dumped,
6764 * and the refcount on the next entry is held ;
Willy Tarreau295a8372011-03-10 11:25:07 +01006765 * - STAT_ST_END : nothing left to dump, the buffer may contain some
Willy Tarreau69f58c82010-07-12 17:55:33 +02006766 * data though.
6767 */
6768
Willy Tarreau2bb4a962014-11-28 11:11:05 +01006769 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW))) {
Willy Tarreau69f58c82010-07-12 17:55:33 +02006770 /* in case of abort, remove any refcount we might have set on an entry */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006771 if (appctx->st2 == STAT_ST_LIST) {
6772 appctx->ctx.table.entry->ref_cnt--;
6773 stksess_kill_if_expired(&appctx->ctx.table.proxy->table, appctx->ctx.table.entry);
Willy Tarreauf6efda12010-08-03 20:34:06 +02006774 }
Willy Tarreau69f58c82010-07-12 17:55:33 +02006775 return 1;
6776 }
6777
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006778 chunk_reset(&trash);
Willy Tarreau69f58c82010-07-12 17:55:33 +02006779
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006780 while (appctx->st2 != STAT_ST_FIN) {
6781 switch (appctx->st2) {
Willy Tarreau295a8372011-03-10 11:25:07 +01006782 case STAT_ST_INIT:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006783 appctx->ctx.table.proxy = appctx->ctx.table.target;
6784 if (!appctx->ctx.table.proxy)
6785 appctx->ctx.table.proxy = proxy;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006786
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006787 appctx->ctx.table.entry = NULL;
6788 appctx->st2 = STAT_ST_INFO;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006789 break;
6790
Willy Tarreau295a8372011-03-10 11:25:07 +01006791 case STAT_ST_INFO:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006792 if (!appctx->ctx.table.proxy ||
6793 (appctx->ctx.table.target &&
6794 appctx->ctx.table.proxy != appctx->ctx.table.target)) {
6795 appctx->st2 = STAT_ST_END;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006796 break;
6797 }
6798
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006799 if (appctx->ctx.table.proxy->table.size) {
6800 if (show && !stats_dump_table_head_to_buffer(&trash, si, appctx->ctx.table.proxy,
6801 appctx->ctx.table.target))
Willy Tarreau69f58c82010-07-12 17:55:33 +02006802 return 0;
6803
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006804 if (appctx->ctx.table.target &&
Willy Tarreaud0d8da92015-04-04 02:10:38 +02006805 strm_li(s)->bind_conf->level >= ACCESS_LVL_OPER) {
Willy Tarreau69f58c82010-07-12 17:55:33 +02006806 /* dump entries only if table explicitly requested */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006807 eb = ebmb_first(&appctx->ctx.table.proxy->table.keys);
Willy Tarreau69f58c82010-07-12 17:55:33 +02006808 if (eb) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006809 appctx->ctx.table.entry = ebmb_entry(eb, struct stksess, key);
6810 appctx->ctx.table.entry->ref_cnt++;
6811 appctx->st2 = STAT_ST_LIST;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006812 break;
6813 }
6814 }
6815 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006816 appctx->ctx.table.proxy = appctx->ctx.table.proxy->next;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006817 break;
6818
Willy Tarreau295a8372011-03-10 11:25:07 +01006819 case STAT_ST_LIST:
Willy Tarreau44455022012-12-05 23:01:12 +01006820 skip_entry = 0;
Simon Hormanc88b8872011-06-15 15:18:49 +09006821
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006822 if (appctx->ctx.table.data_type >= 0) {
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02006823 /* we're filtering on some data contents */
6824 void *ptr;
6825 long long data;
6826
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006827 dt = appctx->ctx.table.data_type;
6828 ptr = stktable_data_ptr(&appctx->ctx.table.proxy->table,
6829 appctx->ctx.table.entry,
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02006830 dt);
6831
6832 data = 0;
6833 switch (stktable_data_types[dt].std_type) {
6834 case STD_T_SINT:
6835 data = stktable_data_cast(ptr, std_t_sint);
6836 break;
6837 case STD_T_UINT:
6838 data = stktable_data_cast(ptr, std_t_uint);
6839 break;
6840 case STD_T_ULL:
6841 data = stktable_data_cast(ptr, std_t_ull);
6842 break;
6843 case STD_T_FRQP:
6844 data = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006845 appctx->ctx.table.proxy->table.data_arg[dt].u);
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02006846 break;
6847 }
6848
6849 /* skip the entry if the data does not match the test and the value */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006850 if ((data < appctx->ctx.table.value &&
6851 (appctx->ctx.table.data_op == STD_OP_EQ ||
6852 appctx->ctx.table.data_op == STD_OP_GT ||
6853 appctx->ctx.table.data_op == STD_OP_GE)) ||
6854 (data == appctx->ctx.table.value &&
6855 (appctx->ctx.table.data_op == STD_OP_NE ||
6856 appctx->ctx.table.data_op == STD_OP_GT ||
6857 appctx->ctx.table.data_op == STD_OP_LT)) ||
6858 (data > appctx->ctx.table.value &&
6859 (appctx->ctx.table.data_op == STD_OP_EQ ||
6860 appctx->ctx.table.data_op == STD_OP_LT ||
6861 appctx->ctx.table.data_op == STD_OP_LE)))
Willy Tarreau44455022012-12-05 23:01:12 +01006862 skip_entry = 1;
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02006863 }
6864
Simon Hormanc88b8872011-06-15 15:18:49 +09006865 if (show && !skip_entry &&
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006866 !stats_dump_table_entry_to_buffer(&trash, si, appctx->ctx.table.proxy,
6867 appctx->ctx.table.entry))
Simon Hormand9366582011-06-15 15:18:45 +09006868 return 0;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006869
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006870 appctx->ctx.table.entry->ref_cnt--;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006871
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006872 eb = ebmb_next(&appctx->ctx.table.entry->key);
Willy Tarreau69f58c82010-07-12 17:55:33 +02006873 if (eb) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006874 struct stksess *old = appctx->ctx.table.entry;
6875 appctx->ctx.table.entry = ebmb_entry(eb, struct stksess, key);
Willy Tarreau8fa52f42012-01-09 11:50:03 +01006876 if (show)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006877 stksess_kill_if_expired(&appctx->ctx.table.proxy->table, old);
6878 else if (!skip_entry && !appctx->ctx.table.entry->ref_cnt)
6879 stksess_kill(&appctx->ctx.table.proxy->table, old);
6880 appctx->ctx.table.entry->ref_cnt++;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006881 break;
6882 }
6883
Simon Hormanc88b8872011-06-15 15:18:49 +09006884
6885 if (show)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006886 stksess_kill_if_expired(&appctx->ctx.table.proxy->table, appctx->ctx.table.entry);
6887 else if (!skip_entry && !appctx->ctx.table.entry->ref_cnt)
6888 stksess_kill(&appctx->ctx.table.proxy->table, appctx->ctx.table.entry);
Simon Hormanc88b8872011-06-15 15:18:49 +09006889
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006890 appctx->ctx.table.proxy = appctx->ctx.table.proxy->next;
6891 appctx->st2 = STAT_ST_INFO;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006892 break;
6893
Willy Tarreau295a8372011-03-10 11:25:07 +01006894 case STAT_ST_END:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006895 appctx->st2 = STAT_ST_FIN;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006896 break;
6897 }
6898 }
6899 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006900}
6901
Willy Tarreaud426a182010-03-05 14:58:26 +01006902/* print a line of text buffer (limited to 70 bytes) to <out>. The format is :
Willy Tarreau74808cb2009-03-04 15:53:18 +01006903 * <2 spaces> <offset=5 digits> <space or plus> <space> <70 chars max> <\n>
6904 * which is 60 chars per line. Non-printable chars \t, \n, \r and \e are
6905 * encoded in C format. Other non-printable chars are encoded "\xHH". Original
6906 * lines are respected within the limit of 70 output chars. Lines that are
6907 * continuation of a previous truncated line begin with "+" instead of " "
6908 * after the offset. The new pointer is returned.
6909 */
Willy Tarreaud426a182010-03-05 14:58:26 +01006910static int dump_text_line(struct chunk *out, const char *buf, int bsize, int len,
6911 int *line, int ptr)
Willy Tarreau74808cb2009-03-04 15:53:18 +01006912{
6913 int end;
6914 unsigned char c;
6915
6916 end = out->len + 80;
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02006917 if (end > out->size)
Willy Tarreau74808cb2009-03-04 15:53:18 +01006918 return ptr;
6919
Willy Tarreau77804732012-10-29 16:14:26 +01006920 chunk_appendf(out, " %05d%c ", ptr, (ptr == *line) ? ' ' : '+');
Willy Tarreau74808cb2009-03-04 15:53:18 +01006921
Willy Tarreaud426a182010-03-05 14:58:26 +01006922 while (ptr < len && ptr < bsize) {
6923 c = buf[ptr];
Willy Tarreau787bbd92009-03-12 08:18:33 +01006924 if (isprint(c) && isascii(c) && c != '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006925 if (out->len > end - 2)
6926 break;
6927 out->str[out->len++] = c;
Willy Tarreau787bbd92009-03-12 08:18:33 +01006928 } else if (c == '\t' || c == '\n' || c == '\r' || c == '\e' || c == '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006929 if (out->len > end - 3)
6930 break;
6931 out->str[out->len++] = '\\';
6932 switch (c) {
6933 case '\t': c = 't'; break;
6934 case '\n': c = 'n'; break;
6935 case '\r': c = 'r'; break;
6936 case '\e': c = 'e'; break;
Willy Tarreau787bbd92009-03-12 08:18:33 +01006937 case '\\': c = '\\'; break;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006938 }
6939 out->str[out->len++] = c;
6940 } else {
6941 if (out->len > end - 5)
6942 break;
6943 out->str[out->len++] = '\\';
6944 out->str[out->len++] = 'x';
6945 out->str[out->len++] = hextab[(c >> 4) & 0xF];
6946 out->str[out->len++] = hextab[c & 0xF];
6947 }
Willy Tarreaud426a182010-03-05 14:58:26 +01006948 if (buf[ptr++] == '\n') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006949 /* we had a line break, let's return now */
6950 out->str[out->len++] = '\n';
6951 *line = ptr;
6952 return ptr;
6953 }
6954 }
6955 /* we have an incomplete line, we return it as-is */
6956 out->str[out->len++] = '\n';
6957 return ptr;
6958}
6959
Baptiste Assmann3863f972015-05-17 00:33:24 +02006960/* This function dumps counters from all resolvers section and associated name servers.
6961 * It returns 0 if the output buffer is full and it needs
6962 * to be called again, otherwise non-zero.
6963 */
6964static int stats_dump_resolvers_to_buffer(struct stream_interface *si)
6965{
6966 struct appctx *appctx = __objt_appctx(si->end);
6967 struct dns_resolvers *presolvers;
6968 struct dns_nameserver *pnameserver;
6969
6970 chunk_reset(&trash);
6971
6972 switch (appctx->st2) {
6973 case STAT_ST_INIT:
6974 appctx->st2 = STAT_ST_LIST; /* let's start producing data */
6975 /* fall through */
6976
6977 case STAT_ST_LIST:
Andrew Hayworth68d05342015-10-02 20:33:01 +00006978 if (LIST_ISEMPTY(&dns_resolvers)) {
6979 chunk_appendf(&trash, "No resolvers found\n");
6980 }
6981 else {
6982 list_for_each_entry(presolvers, &dns_resolvers, list) {
6983 if (appctx->ctx.resolvers.ptr != NULL && appctx->ctx.resolvers.ptr != presolvers)
6984 continue;
6985
6986 chunk_appendf(&trash, "Resolvers section %s\n", presolvers->id);
6987 list_for_each_entry(pnameserver, &presolvers->nameserver_list, list) {
6988 chunk_appendf(&trash, " nameserver %s:\n", pnameserver->id);
6989 chunk_appendf(&trash, " sent: %ld\n", pnameserver->counters.sent);
6990 chunk_appendf(&trash, " valid: %ld\n", pnameserver->counters.valid);
6991 chunk_appendf(&trash, " update: %ld\n", pnameserver->counters.update);
6992 chunk_appendf(&trash, " cname: %ld\n", pnameserver->counters.cname);
6993 chunk_appendf(&trash, " cname_error: %ld\n", pnameserver->counters.cname_error);
6994 chunk_appendf(&trash, " any_err: %ld\n", pnameserver->counters.any_err);
6995 chunk_appendf(&trash, " nx: %ld\n", pnameserver->counters.nx);
6996 chunk_appendf(&trash, " timeout: %ld\n", pnameserver->counters.timeout);
6997 chunk_appendf(&trash, " refused: %ld\n", pnameserver->counters.refused);
6998 chunk_appendf(&trash, " other: %ld\n", pnameserver->counters.other);
6999 chunk_appendf(&trash, " invalid: %ld\n", pnameserver->counters.invalid);
7000 chunk_appendf(&trash, " too_big: %ld\n", pnameserver->counters.too_big);
7001 chunk_appendf(&trash, " truncated: %ld\n", pnameserver->counters.truncated);
7002 chunk_appendf(&trash, " outdated: %ld\n", pnameserver->counters.outdated);
7003 }
7004 }
Baptiste Assmann3863f972015-05-17 00:33:24 +02007005 }
7006
7007 /* display response */
7008 if (bi_putchk(si_ic(si), &trash) == -1) {
7009 /* let's try again later from this session. We add ourselves into
7010 * this session's users so that it can remove us upon termination.
7011 */
7012 si->flags |= SI_FL_WAIT_ROOM;
7013 return 0;
7014 }
7015
7016 appctx->st2 = STAT_ST_FIN;
7017 /* fall through */
7018
7019 default:
7020 appctx->st2 = STAT_ST_FIN;
7021 return 1;
7022 }
7023}
7024
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02007025/* This function dumps all captured errors onto the stream interface's
Willy Tarreau306f8302013-07-08 15:53:06 +02007026 * read buffer. It returns 0 if the output buffer is full and it needs
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01007027 * to be called again, otherwise non-zero.
Willy Tarreau74808cb2009-03-04 15:53:18 +01007028 */
Simon Horman9bd2c732011-06-15 15:18:44 +09007029static int stats_dump_errors_to_buffer(struct stream_interface *si)
Willy Tarreau74808cb2009-03-04 15:53:18 +01007030{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01007031 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau74808cb2009-03-04 15:53:18 +01007032 extern const char *monthname[12];
Willy Tarreau74808cb2009-03-04 15:53:18 +01007033
Willy Tarreau2bb4a962014-11-28 11:11:05 +01007034 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
Willy Tarreau61b34732009-10-03 23:49:35 +02007035 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01007036
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007037 chunk_reset(&trash);
Willy Tarreau74808cb2009-03-04 15:53:18 +01007038
Willy Tarreau7b4b4992013-12-01 09:15:12 +01007039 if (!appctx->ctx.errors.px) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01007040 /* the function had not been called yet, let's prepare the
7041 * buffer for a response.
7042 */
Willy Tarreau10479e42010-12-12 14:00:34 +01007043 struct tm tm;
7044
7045 get_localtime(date.tv_sec, &tm);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007046 chunk_appendf(&trash, "Total events captured on [%02d/%s/%04d:%02d:%02d:%02d.%03d] : %u\n",
Willy Tarreau10479e42010-12-12 14:00:34 +01007047 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
7048 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(date.tv_usec/1000),
7049 error_snapshot_id);
7050
Willy Tarreau2bb4a962014-11-28 11:11:05 +01007051 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau10479e42010-12-12 14:00:34 +01007052 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreaufe127932015-04-21 19:23:39 +02007053 si_applet_cant_put(si);
Willy Tarreau10479e42010-12-12 14:00:34 +01007054 return 0;
7055 }
7056
Willy Tarreau7b4b4992013-12-01 09:15:12 +01007057 appctx->ctx.errors.px = proxy;
7058 appctx->ctx.errors.buf = 0;
7059 appctx->ctx.errors.bol = 0;
7060 appctx->ctx.errors.ptr = -1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01007061 }
7062
7063 /* we have two inner loops here, one for the proxy, the other one for
7064 * the buffer.
7065 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01007066 while (appctx->ctx.errors.px) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01007067 struct error_snapshot *es;
7068
Willy Tarreau7b4b4992013-12-01 09:15:12 +01007069 if (appctx->ctx.errors.buf == 0)
7070 es = &appctx->ctx.errors.px->invalid_req;
Willy Tarreau74808cb2009-03-04 15:53:18 +01007071 else
Willy Tarreau7b4b4992013-12-01 09:15:12 +01007072 es = &appctx->ctx.errors.px->invalid_rep;
Willy Tarreau74808cb2009-03-04 15:53:18 +01007073
7074 if (!es->when.tv_sec)
7075 goto next;
7076
Willy Tarreau7b4b4992013-12-01 09:15:12 +01007077 if (appctx->ctx.errors.iid >= 0 &&
7078 appctx->ctx.errors.px->uuid != appctx->ctx.errors.iid &&
7079 es->oe->uuid != appctx->ctx.errors.iid)
Willy Tarreau74808cb2009-03-04 15:53:18 +01007080 goto next;
7081
Willy Tarreau7b4b4992013-12-01 09:15:12 +01007082 if (appctx->ctx.errors.ptr < 0) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01007083 /* just print headers now */
7084
7085 char pn[INET6_ADDRSTRLEN];
7086 struct tm tm;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02007087 int port;
Willy Tarreau74808cb2009-03-04 15:53:18 +01007088
7089 get_localtime(es->when.tv_sec, &tm);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007090 chunk_appendf(&trash, " \n[%02d/%s/%04d:%02d:%02d:%02d.%03d]",
Willy Tarreau74808cb2009-03-04 15:53:18 +01007091 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
Willy Tarreau1772ece2009-04-03 14:49:12 +02007092 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(es->when.tv_usec/1000));
Willy Tarreau74808cb2009-03-04 15:53:18 +01007093
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02007094 switch (addr_to_str(&es->src, pn, sizeof(pn))) {
7095 case AF_INET:
7096 case AF_INET6:
7097 port = get_host_port(&es->src);
7098 break;
7099 default:
7100 port = 0;
7101 }
7102
Willy Tarreau7b4b4992013-12-01 09:15:12 +01007103 switch (appctx->ctx.errors.buf) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01007104 case 0:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007105 chunk_appendf(&trash,
Willy Tarreau74808cb2009-03-04 15:53:18 +01007106 " frontend %s (#%d): invalid request\n"
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02007107 " backend %s (#%d)",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01007108 appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02007109 (es->oe->cap & PR_CAP_BE) ? es->oe->id : "<NONE>",
7110 (es->oe->cap & PR_CAP_BE) ? es->oe->uuid : -1);
Willy Tarreau74808cb2009-03-04 15:53:18 +01007111 break;
7112 case 1:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007113 chunk_appendf(&trash,
Olivier Doucet08afdcb2014-09-08 11:23:00 +02007114 " backend %s (#%d): invalid response\n"
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02007115 " frontend %s (#%d)",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01007116 appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02007117 es->oe->id, es->oe->uuid);
Willy Tarreau74808cb2009-03-04 15:53:18 +01007118 break;
7119 }
7120
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007121 chunk_appendf(&trash,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02007122 ", server %s (#%d), event #%u\n"
7123 " src %s:%d, session #%d, session flags 0x%08x\n"
7124 " HTTP msg state %d, msg flags 0x%08x, tx flags 0x%08x\n"
7125 " HTTP chunk len %lld bytes, HTTP body len %lld bytes\n"
7126 " buffer flags 0x%08x, out %d bytes, total %lld bytes\n"
7127 " pending %d bytes, wrapping at %d, error at position %d:\n \n",
7128 es->srv ? es->srv->id : "<NONE>", es->srv ? es->srv->puid : -1,
7129 es->ev_id,
7130 pn, port, es->sid, es->s_flags,
7131 es->state, es->m_flags, es->t_flags,
7132 es->m_clen, es->m_blen,
7133 es->b_flags, es->b_out, es->b_tot,
7134 es->len, es->b_wrap, es->pos);
7135
Willy Tarreau2bb4a962014-11-28 11:11:05 +01007136 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01007137 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreaufe127932015-04-21 19:23:39 +02007138 si_applet_cant_put(si);
Willy Tarreau61b34732009-10-03 23:49:35 +02007139 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01007140 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01007141 appctx->ctx.errors.ptr = 0;
7142 appctx->ctx.errors.sid = es->sid;
Willy Tarreau74808cb2009-03-04 15:53:18 +01007143 }
7144
Willy Tarreau7b4b4992013-12-01 09:15:12 +01007145 if (appctx->ctx.errors.sid != es->sid) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01007146 /* the snapshot changed while we were dumping it */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007147 chunk_appendf(&trash,
Willy Tarreau74808cb2009-03-04 15:53:18 +01007148 " WARNING! update detected on this snapshot, dump interrupted. Please re-check!\n");
Willy Tarreaubc18da12015-03-13 14:00:47 +01007149 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02007150 si_applet_cant_put(si);
Willy Tarreau61b34732009-10-03 23:49:35 +02007151 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01007152 }
Willy Tarreau74808cb2009-03-04 15:53:18 +01007153 goto next;
7154 }
7155
7156 /* OK, ptr >= 0, so we have to dump the current line */
Willy Tarreauf3764b72016-03-31 13:45:10 +02007157 while (es->buf && appctx->ctx.errors.ptr < es->len && appctx->ctx.errors.ptr < global.tune.bufsize) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01007158 int newptr;
7159 int newline;
7160
Willy Tarreau7b4b4992013-12-01 09:15:12 +01007161 newline = appctx->ctx.errors.bol;
Willy Tarreauf3764b72016-03-31 13:45:10 +02007162 newptr = dump_text_line(&trash, es->buf, global.tune.bufsize, es->len, &newline, appctx->ctx.errors.ptr);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01007163 if (newptr == appctx->ctx.errors.ptr)
Willy Tarreau61b34732009-10-03 23:49:35 +02007164 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01007165
Willy Tarreau2bb4a962014-11-28 11:11:05 +01007166 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01007167 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreaufe127932015-04-21 19:23:39 +02007168 si_applet_cant_put(si);
Willy Tarreau61b34732009-10-03 23:49:35 +02007169 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01007170 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01007171 appctx->ctx.errors.ptr = newptr;
7172 appctx->ctx.errors.bol = newline;
Willy Tarreau74808cb2009-03-04 15:53:18 +01007173 };
7174 next:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01007175 appctx->ctx.errors.bol = 0;
7176 appctx->ctx.errors.ptr = -1;
7177 appctx->ctx.errors.buf++;
7178 if (appctx->ctx.errors.buf > 1) {
7179 appctx->ctx.errors.buf = 0;
7180 appctx->ctx.errors.px = appctx->ctx.errors.px->next;
Willy Tarreau74808cb2009-03-04 15:53:18 +01007181 }
Willy Tarreauae795722016-02-16 11:27:28 +01007182 }
7183
7184 /* dump complete */
7185 return 1;
7186}
7187
7188/* This function dumps all environmnent variables to the buffer. It returns 0
7189 * if the output buffer is full and it needs to be called again, otherwise
7190 * non-zero. Dumps only one entry if st2 == STAT_ST_END.
7191 */
7192static int stats_dump_env_to_buffer(struct stream_interface *si)
7193{
7194 struct appctx *appctx = __objt_appctx(si->end);
7195
7196 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
7197 return 1;
7198
7199 chunk_reset(&trash);
7200
7201 /* we have two inner loops here, one for the proxy, the other one for
7202 * the buffer.
7203 */
7204 while (*appctx->ctx.env.var) {
7205 chunk_printf(&trash, "%s\n", *appctx->ctx.env.var);
7206
7207 if (bi_putchk(si_ic(si), &trash) == -1) {
7208 si_applet_cant_put(si);
7209 return 0;
7210 }
7211 if (appctx->st2 == STAT_ST_END)
7212 break;
7213 appctx->ctx.env.var++;
Willy Tarreau74808cb2009-03-04 15:53:18 +01007214 }
7215
7216 /* dump complete */
Willy Tarreau61b34732009-10-03 23:49:35 +02007217 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01007218}
7219
Willy Tarreaud5781202012-09-22 19:32:35 +02007220/* parse the "level" argument on the bind lines */
7221static int bind_parse_level(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
7222{
7223 if (!*args[cur_arg + 1]) {
7224 memprintf(err, "'%s' : missing level", args[cur_arg]);
7225 return ERR_ALERT | ERR_FATAL;
7226 }
7227
7228 if (!strcmp(args[cur_arg+1], "user"))
7229 conf->level = ACCESS_LVL_USER;
7230 else if (!strcmp(args[cur_arg+1], "operator"))
7231 conf->level = ACCESS_LVL_OPER;
7232 else if (!strcmp(args[cur_arg+1], "admin"))
7233 conf->level = ACCESS_LVL_ADMIN;
7234 else {
7235 memprintf(err, "'%s' only supports 'user', 'operator', and 'admin' (got '%s')",
7236 args[cur_arg], args[cur_arg+1]);
7237 return ERR_ALERT | ERR_FATAL;
7238 }
7239
7240 return 0;
7241}
7242
Willy Tarreau30576452015-04-13 13:50:30 +02007243struct applet http_stats_applet = {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007244 .obj_type = OBJ_TYPE_APPLET,
Willy Tarreaub24281b2011-02-13 13:16:36 +01007245 .name = "<STATS>", /* used for logging */
7246 .fct = http_stats_io_handler,
Aman Gupta9a13e842012-04-02 18:57:53 -07007247 .release = NULL,
Willy Tarreaub24281b2011-02-13 13:16:36 +01007248};
7249
Willy Tarreau30576452015-04-13 13:50:30 +02007250static struct applet cli_applet = {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007251 .obj_type = OBJ_TYPE_APPLET,
Willy Tarreaub24281b2011-02-13 13:16:36 +01007252 .name = "<CLI>", /* used for logging */
7253 .fct = cli_io_handler,
Willy Tarreau5f9a8772012-11-26 02:22:40 +01007254 .release = cli_release_handler,
Willy Tarreaub24281b2011-02-13 13:16:36 +01007255};
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01007256
Willy Tarreaudc13c112013-06-21 23:16:39 +02007257static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreau10522fd2008-07-09 20:12:41 +02007258 { CFG_GLOBAL, "stats", stats_parse_global },
7259 { 0, NULL, NULL },
7260}};
7261
Willy Tarreaud5781202012-09-22 19:32:35 +02007262static struct bind_kw_list bind_kws = { "STAT", { }, {
7263 { "level", bind_parse_level, 1 }, /* set the unix socket admin level */
7264 { NULL, NULL, 0 },
7265}};
7266
Willy Tarreau10522fd2008-07-09 20:12:41 +02007267__attribute__((constructor))
7268static void __dumpstats_module_init(void)
7269{
7270 cfg_register_keywords(&cfg_kws);
Willy Tarreaud5781202012-09-22 19:32:35 +02007271 bind_register_keywords(&bind_kws);
Willy Tarreau10522fd2008-07-09 20:12:41 +02007272}
7273
Willy Tarreau91861262007-10-17 17:06:05 +02007274/*
7275 * Local variables:
7276 * c-indent-level: 8
7277 * c-basic-offset: 8
7278 * End:
7279 */