blob: 05facda7df456c52e1287b8e09cdc1e5696f81df [file] [log] [blame]
Willy Tarreau91861262007-10-17 17:06:05 +02001/*
Willy Tarreaueb472682010-05-28 18:46:57 +02002 * Functions dedicated to statistics output and the stats socket
Willy Tarreau91861262007-10-17 17:06:05 +02003 *
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004 * Copyright 2000-2012 Willy Tarreau <w@1wt.eu>
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02005 * Copyright 2007-2009 Krzysztof Piotr Oledzki <ole@ans.pl>
Willy Tarreau91861262007-10-17 17:06:05 +02006 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
11 *
12 */
13
14#include <ctype.h>
15#include <errno.h>
16#include <fcntl.h>
17#include <stdio.h>
18#include <stdlib.h>
19#include <string.h>
Willy Tarreaufbee7132007-10-18 13:53:22 +020020#include <pwd.h>
21#include <grp.h>
Willy Tarreau91861262007-10-17 17:06:05 +020022
23#include <sys/socket.h>
24#include <sys/stat.h>
25#include <sys/types.h>
26
Willy Tarreau10522fd2008-07-09 20:12:41 +020027#include <common/cfgparse.h>
Willy Tarreau91861262007-10-17 17:06:05 +020028#include <common/compat.h>
29#include <common/config.h>
30#include <common/debug.h>
31#include <common/memory.h>
32#include <common/mini-clist.h>
33#include <common/standard.h>
Willy Tarreau0c303ee2008-07-07 00:09:58 +020034#include <common/ticks.h>
Willy Tarreau91861262007-10-17 17:06:05 +020035#include <common/time.h>
36#include <common/uri_auth.h>
37#include <common/version.h>
Emeric Brun4147b2e2014-06-16 18:36:30 +020038#include <common/base64.h>
Willy Tarreau91861262007-10-17 17:06:05 +020039
Willy Tarreau8a8d83b2015-04-13 13:24:54 +020040#include <types/applet.h>
Willy Tarreau91861262007-10-17 17:06:05 +020041#include <types/global.h>
Baptiste Assmann3863f972015-05-17 00:33:24 +020042#include <types/dns.h>
Willy Tarreau91861262007-10-17 17:06:05 +020043
44#include <proto/backend.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020045#include <proto/channel.h>
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020046#include <proto/checks.h>
William Lallemande3a7d992012-11-20 11:25:20 +010047#include <proto/compression.h>
Willy Tarreau91861262007-10-17 17:06:05 +020048#include <proto/dumpstats.h>
49#include <proto/fd.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010050#include <proto/freq_ctr.h>
Willy Tarreau32b60d42015-03-13 16:14:57 +010051#include <proto/frontend.h>
Willy Tarreaueb472682010-05-28 18:46:57 +020052#include <proto/log.h>
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +010053#include <proto/pattern.h>
Willy Tarreaua206fa92009-01-25 14:02:00 +010054#include <proto/pipe.h>
Willy Tarreaud1d54542012-09-12 22:58:11 +020055#include <proto/listener.h>
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +010056#include <proto/map.h>
Willy Tarreaue6d97022012-11-23 11:19:33 +010057#include <proto/proto_http.h>
Willy Tarreaufbee7132007-10-18 13:53:22 +020058#include <proto/proto_uxst.h>
Willy Tarreau89a63132009-08-16 17:41:45 +020059#include <proto/proxy.h>
Willy Tarreau1cf8f082014-02-07 12:14:54 +010060#include <proto/sample.h>
Willy Tarreau9903f0e2015-04-04 18:50:31 +020061#include <proto/session.h>
Willy Tarreau87b09662015-04-03 00:22:06 +020062#include <proto/stream.h>
Krzysztof Oledzki85130942007-10-22 16:21:10 +020063#include <proto/server.h>
Willy Tarreau75bf2c92012-08-20 17:01:35 +020064#include <proto/raw_sock.h>
Willy Tarreaudded32d2008-11-30 19:48:07 +010065#include <proto/stream_interface.h>
Willy Tarreau4726f532009-03-07 17:25:21 +010066#include <proto/task.h>
Willy Tarreau91861262007-10-17 17:06:05 +020067
Willy Tarreau7a0169a2012-11-19 17:13:16 +010068#ifdef USE_OPENSSL
69#include <proto/ssl_sock.h>
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +020070#include <types/ssl_sock.h>
Willy Tarreau7a0169a2012-11-19 17:13:16 +010071#endif
72
Willy Tarreau91b843d2014-01-28 16:27:17 +010073/* stats socket states */
74enum {
75 STAT_CLI_INIT = 0, /* initial state, must leave to zero ! */
76 STAT_CLI_END, /* final state, let's close */
77 STAT_CLI_GETREQ, /* wait for a request */
78 STAT_CLI_OUTPUT, /* all states after this one are responses */
79 STAT_CLI_PROMPT, /* display the prompt (first output, same code) */
80 STAT_CLI_PRINT, /* display message in cli->msg */
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +010081 STAT_CLI_PRINT_FREE, /* display message in cli->msg. After the display, free the pointer */
Willy Tarreau91b843d2014-01-28 16:27:17 +010082 STAT_CLI_O_INFO, /* dump info */
Willy Tarreau87b09662015-04-03 00:22:06 +020083 STAT_CLI_O_SESS, /* dump streams */
Willy Tarreau91b843d2014-01-28 16:27:17 +010084 STAT_CLI_O_ERR, /* dump errors */
85 STAT_CLI_O_TAB, /* dump tables */
86 STAT_CLI_O_CLR, /* clear tables */
87 STAT_CLI_O_SET, /* set entries in tables */
88 STAT_CLI_O_STAT, /* dump stats */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +010089 STAT_CLI_O_PATS, /* list all pattern reference avalaible */
90 STAT_CLI_O_PAT, /* list all entries of a pattern */
Willy Tarreau91b843d2014-01-28 16:27:17 +010091 STAT_CLI_O_MLOOK, /* lookup a map entry */
Willy Tarreau12833bb2014-01-28 16:49:56 +010092 STAT_CLI_O_POOLS, /* dump memory pools */
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +020093 STAT_CLI_O_TLSK, /* list all TLS ticket keys references */
Baptiste Assmann3863f972015-05-17 00:33:24 +020094 STAT_CLI_O_RESOLVERS,/* dump a resolver's section nameservers counters */
Baptiste Assmann28289462015-07-03 08:01:20 +020095 STAT_CLI_O_SERVERS_STATE, /* dump server state and changing information */
Baptiste Assmann9b6857e2015-09-18 14:49:12 +020096 STAT_CLI_O_BACKEND, /* dump backend list */
Willy Tarreauae795722016-02-16 11:27:28 +010097 STAT_CLI_O_ENV, /* dump environment */
Willy Tarreau91b843d2014-01-28 16:27:17 +010098};
99
Willy Tarreaued7df902014-05-22 18:04:49 +0200100/* Actions available for the stats admin forms */
101enum {
102 ST_ADM_ACTION_NONE = 0,
Willy Tarreau248a60e2014-05-23 14:59:48 +0200103
104 /* enable/disable health checks */
105 ST_ADM_ACTION_DHLTH,
106 ST_ADM_ACTION_EHLTH,
107
108 /* force health check status */
109 ST_ADM_ACTION_HRUNN,
110 ST_ADM_ACTION_HNOLB,
111 ST_ADM_ACTION_HDOWN,
112
113 /* enable/disable agent checks */
114 ST_ADM_ACTION_DAGENT,
115 ST_ADM_ACTION_EAGENT,
116
117 /* force agent check status */
118 ST_ADM_ACTION_ARUNN,
119 ST_ADM_ACTION_ADOWN,
120
121 /* set admin state */
Willy Tarreaued7df902014-05-22 18:04:49 +0200122 ST_ADM_ACTION_READY,
123 ST_ADM_ACTION_DRAIN,
124 ST_ADM_ACTION_MAINT,
125 ST_ADM_ACTION_SHUTDOWN,
126 /* these are the ancient actions, still available for compatibility */
127 ST_ADM_ACTION_DISABLE,
128 ST_ADM_ACTION_ENABLE,
129 ST_ADM_ACTION_STOP,
130 ST_ADM_ACTION_START,
131};
132
Willy Tarreauc4832de2016-01-11 18:12:26 +0100133
134/* Show Info fields for CLI output. For any field added here, please add the text
135 * representation in the info_field_names array below. Please only append at the end,
136 * before the INF_TOTAL_FIELDS entry, and never insert anything in the middle
137 * nor at the beginning.
138 */
139enum info_field {
140 INF_NAME,
141 INF_VERSION,
142 INF_RELEASE_DATE,
143 INF_NBPROC,
144 INF_PROCESS_NUM,
145 INF_PID,
146 INF_UPTIME,
147 INF_UPTIME_SEC,
148 INF_MEMMAX_MB,
149 INF_POOL_ALLOC_MB,
150 INF_POOL_USED_MB,
151 INF_POOL_FAILED,
152 INF_ULIMIT_N,
153 INF_MAXSOCK,
154 INF_MAXCONN,
155 INF_HARD_MAXCONN,
156 INF_CURR_CONN,
157 INF_CUM_CONN,
158 INF_CUM_REQ,
159 INF_MAX_SSL_CONNS,
160 INF_CURR_SSL_CONNS,
161 INF_CUM_SSL_CONNS,
162 INF_MAXPIPES,
163 INF_PIPES_USED,
164 INF_PIPES_FREE,
165 INF_CONN_RATE,
166 INF_CONN_RATE_LIMIT,
167 INF_MAX_CONN_RATE,
168 INF_SESS_RATE,
169 INF_SESS_RATE_LIMIT,
170 INF_MAX_SESS_RATE,
171 INF_SSL_RATE,
172 INF_SSL_RATE_LIMIT,
173 INF_MAX_SSL_RATE,
174 INF_SSL_FRONTEND_KEY_RATE,
175 INF_SSL_FRONTEND_MAX_KEY_RATE,
176 INF_SSL_FRONTEND_SESSION_REUSE_PCT,
177 INF_SSL_BACKEND_KEY_RATE,
178 INF_SSL_BACKEND_MAX_KEY_RATE,
179 INF_SSL_CACHE_LOOKUPS,
180 INF_SSL_CACHE_MISSES,
181 INF_COMPRESS_BPS_IN,
182 INF_COMPRESS_BPS_OUT,
183 INF_COMPRESS_BPS_RATE_LIM,
184 INF_ZLIB_MEM_USAGE,
185 INF_MAX_ZLIB_MEM_USAGE,
186 INF_TASKS,
187 INF_RUN_QUEUE,
188 INF_IDLE_PCT,
189 INF_NODE,
190 INF_DESCRIPTION,
191
192 /* must always be the last one */
193 INF_TOTAL_FIELDS
194};
195
196/* These are the field names for each INF_* field position. Please pay attention
197 * to always use the exact same name except that the strings for new names must
198 * be lower case or CamelCase while the enum entries must be upper case.
199 */
200const char *info_field_names[INF_TOTAL_FIELDS] = {
201 [INF_NAME] = "Name",
202 [INF_VERSION] = "Version",
203 [INF_RELEASE_DATE] = "Release_date",
204 [INF_NBPROC] = "Nbproc",
205 [INF_PROCESS_NUM] = "Process_num",
206 [INF_PID] = "Pid",
207 [INF_UPTIME] = "Uptime",
208 [INF_UPTIME_SEC] = "Uptime_sec",
209 [INF_MEMMAX_MB] = "Memmax_MB",
210 [INF_POOL_ALLOC_MB] = "PoolAlloc_MB",
211 [INF_POOL_USED_MB] = "PoolUsed_MB",
212 [INF_POOL_FAILED] = "PoolFailed",
213 [INF_ULIMIT_N] = "Ulimit-n",
214 [INF_MAXSOCK] = "Maxsock",
215 [INF_MAXCONN] = "Maxconn",
216 [INF_HARD_MAXCONN] = "Hard_maxconn",
217 [INF_CURR_CONN] = "CurrConns",
218 [INF_CUM_CONN] = "CumConns",
219 [INF_CUM_REQ] = "CumReq",
220 [INF_MAX_SSL_CONNS] = "MaxSslConns",
221 [INF_CURR_SSL_CONNS] = "CurrSslConns",
222 [INF_CUM_SSL_CONNS] = "CumSslConns",
223 [INF_MAXPIPES] = "Maxpipes",
224 [INF_PIPES_USED] = "PipesUsed",
225 [INF_PIPES_FREE] = "PipesFree",
226 [INF_CONN_RATE] = "ConnRate",
227 [INF_CONN_RATE_LIMIT] = "ConnRateLimit",
228 [INF_MAX_CONN_RATE] = "MaxConnRate",
229 [INF_SESS_RATE] = "SessRate",
230 [INF_SESS_RATE_LIMIT] = "SessRateLimit",
231 [INF_MAX_SESS_RATE] = "MaxSessRate",
232 [INF_SSL_RATE] = "SslRate",
233 [INF_SSL_RATE_LIMIT] = "SslRateLimit",
234 [INF_MAX_SSL_RATE] = "MaxSslRate",
235 [INF_SSL_FRONTEND_KEY_RATE] = "SslFrontendKeyRate",
236 [INF_SSL_FRONTEND_MAX_KEY_RATE] = "SslFrontendMaxKeyRate",
237 [INF_SSL_FRONTEND_SESSION_REUSE_PCT] = "SslFrontendSessionReuse_pct",
238 [INF_SSL_BACKEND_KEY_RATE] = "SslBackendKeyRate",
239 [INF_SSL_BACKEND_MAX_KEY_RATE] = "SslBackendMaxKeyRate",
240 [INF_SSL_CACHE_LOOKUPS] = "SslCacheLookups",
241 [INF_SSL_CACHE_MISSES] = "SslCacheMisses",
242 [INF_COMPRESS_BPS_IN] = "CompressBpsIn",
243 [INF_COMPRESS_BPS_OUT] = "CompressBpsOut",
244 [INF_COMPRESS_BPS_RATE_LIM] = "CompressBpsRateLim",
245 [INF_ZLIB_MEM_USAGE] = "ZlibMemUsage",
246 [INF_MAX_ZLIB_MEM_USAGE] = "MaxZlibMemUsage",
247 [INF_TASKS] = "Tasks",
248 [INF_RUN_QUEUE] = "Run_queue",
249 [INF_IDLE_PCT] = "Idle_pct",
250 [INF_NODE] = "node",
251 [INF_DESCRIPTION] = "description",
252};
253
254/* one line of stats */
255static struct field info[INF_TOTAL_FIELDS];
256
Baptiste Assmann9b6857e2015-09-18 14:49:12 +0200257static int stats_dump_backend_to_buffer(struct stream_interface *si);
Willy Tarreauae795722016-02-16 11:27:28 +0100258static int stats_dump_env_to_buffer(struct stream_interface *si);
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +0100259static int stats_dump_info_to_buffer(struct stream_interface *si);
Baptiste Assmann28289462015-07-03 08:01:20 +0200260static int stats_dump_servers_state_to_buffer(struct stream_interface *si);
Willy Tarreau12833bb2014-01-28 16:49:56 +0100261static int stats_dump_pools_to_buffer(struct stream_interface *si);
Willy Tarreau87b09662015-04-03 00:22:06 +0200262static int stats_dump_full_sess_to_buffer(struct stream_interface *si, struct stream *sess);
Simon Horman9bd2c732011-06-15 15:18:44 +0900263static int stats_dump_sess_to_buffer(struct stream_interface *si);
264static int stats_dump_errors_to_buffer(struct stream_interface *si);
Willy Tarreau44455022012-12-05 23:01:12 +0100265static int stats_table_request(struct stream_interface *si, int show);
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +0100266static int stats_dump_proxy_to_buffer(struct stream_interface *si, struct proxy *px, struct uri_auth *uri);
267static int stats_dump_stat_to_buffer(struct stream_interface *si, struct uri_auth *uri);
Baptiste Assmann3863f972015-05-17 00:33:24 +0200268static int stats_dump_resolvers_to_buffer(struct stream_interface *si);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100269static int stats_pats_list(struct stream_interface *si);
270static int stats_pat_list(struct stream_interface *si);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +0100271static int stats_map_lookup(struct stream_interface *si);
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +0200272#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
273static int stats_tlskeys_list(struct stream_interface *si);
274#endif
Willy Tarreau00a37f02015-04-13 12:05:19 +0200275static void cli_release_handler(struct appctx *appctx);
Simon Horman9bd2c732011-06-15 15:18:44 +0900276
Baptiste Assmann28289462015-07-03 08:01:20 +0200277static void dump_servers_state(struct proxy *backend, struct chunk *buf);
278
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100279/*
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +0100280 * cli_io_handler()
281 * -> stats_dump_sess_to_buffer() // "show sess"
282 * -> stats_dump_errors_to_buffer() // "show errors"
283 * -> stats_dump_info_to_buffer() // "show info"
Baptiste Assmann9b6857e2015-09-18 14:49:12 +0200284 * -> stats_dump_backend_to_buffer() // "show backend"
Baptiste Assmann28289462015-07-03 08:01:20 +0200285 * -> stats_dump_servers_state_to_buffer() // "show servers state [<backend name>]"
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +0100286 * -> stats_dump_stat_to_buffer() // "show stat"
Baptiste Assmann3863f972015-05-17 00:33:24 +0200287 * -> stats_dump_resolvers_to_buffer() // "show stat resolver <id>"
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +0100288 * -> stats_dump_csv_header()
289 * -> stats_dump_proxy_to_buffer()
290 * -> stats_dump_fe_stats()
291 * -> stats_dump_li_stats()
292 * -> stats_dump_sv_stats()
293 * -> stats_dump_be_stats()
294 *
295 * http_stats_io_handler()
296 * -> stats_dump_stat_to_buffer() // same as above, but used for CSV or HTML
297 * -> stats_dump_csv_header() // emits the CSV headers (same as above)
298 * -> stats_dump_html_head() // emits the HTML headers
299 * -> stats_dump_html_info() // emits the equivalent of "show info" at the top
300 * -> stats_dump_proxy_to_buffer() // same as above, valid for CSV and HTML
301 * -> stats_dump_html_px_hdr()
302 * -> stats_dump_fe_stats()
303 * -> stats_dump_li_stats()
304 * -> stats_dump_sv_stats()
305 * -> stats_dump_be_stats()
306 * -> stats_dump_html_px_end()
307 * -> stats_dump_html_end() // emits HTML trailer
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100308 */
309
Willy Tarreau30576452015-04-13 13:50:30 +0200310static struct applet cli_applet;
Simon Horman9bd2c732011-06-15 15:18:44 +0900311
312static const char stats_sock_usage_msg[] =
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200313 "Unknown command. Please enter one of the following commands only :\n"
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200314 " clear counters : clear max statistics counters (add 'all' for all counters)\n"
Willy Tarreau88ee3972010-07-13 13:48:00 +0200315 " clear table : remove an entry from a table\n"
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200316 " help : this message\n"
317 " prompt : toggle interactive mode with prompt\n"
318 " quit : disconnect\n"
Baptiste Assmann9b6857e2015-09-18 14:49:12 +0200319 " show backend : list backends in the current running config\n"
Willy Tarreauae795722016-02-16 11:27:28 +0100320 " show env [var] : dump environment variables known to the process\n"
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200321 " show info : report information about the running process\n"
Willy Tarreau12833bb2014-01-28 16:49:56 +0100322 " show pools : report information about the memory pools usage\n"
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200323 " show stat : report counters for each proxy and server\n"
324 " show errors : report last request and response errors for each proxy\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +0100325 " show sess [id] : report the list of current sessions or dump this session\n"
Willy Tarreau69f58c82010-07-12 17:55:33 +0200326 " show table [id]: report table usage stats or dump this table's contents\n"
Baptiste Assmann28289462015-07-03 08:01:20 +0200327 " show servers state [id]: dump volatile server information (for backend <id>)\n"
Willy Tarreau38338fa2009-10-10 18:37:29 +0200328 " get weight : report a server's current weight\n"
Willy Tarreau4483d432009-10-10 19:30:08 +0200329 " set weight : change a server's weight\n"
Baptiste Assmann3d8f8312015-04-13 22:54:33 +0200330 " set server : change a server's state, weight or address\n"
Willy Tarreau654694e2012-06-07 01:03:16 +0200331 " set table [id] : update or create a table entry's data\n"
Willy Tarreau7aabd112010-01-26 10:59:06 +0100332 " set timeout : change a timeout setting\n"
Willy Tarreau2a0f4d22011-08-02 11:49:05 +0200333 " set maxconn : change a maxconn setting\n"
Willy Tarreauf5b22872011-09-07 16:13:44 +0200334 " set rate-limit : change a rate limiting value\n"
Willy Tarreaua295edc2011-09-07 23:21:03 +0200335 " disable : put a server or frontend in maintenance mode\n"
336 " enable : re-enable a server or frontend which is in maintenance mode\n"
337 " shutdown : kill a session or a frontend (eg:to release listening ports)\n"
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100338 " show acl [id] : report avalaible acls or dump an acl's contents\n"
339 " get acl : reports the patterns matching a sample for an ACL\n"
340 " add acl : add acl entry\n"
341 " del acl : delete acl entry\n"
342 " clear acl <id> : clear the content of this acl\n"
Thierry FOURNIER1432a0c2014-03-11 13:42:38 +0100343 " show map [id] : report avalaible maps or dump a map's contents\n"
344 " get map : reports the keys and values matching a sample for a map\n"
345 " set map : modify map entry\n"
346 " add map : add map entry\n"
347 " del map : delete map entry\n"
348 " clear map <id> : clear the content of this map\n"
Emeric Brun4147b2e2014-06-16 18:36:30 +0200349 " set ssl <stmt> : set statement for ssl\n"
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200350 "";
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200351
Simon Horman9bd2c732011-06-15 15:18:44 +0900352static const char stats_permission_denied_msg[] =
Willy Tarreau6162db22009-10-10 17:13:00 +0200353 "Permission denied\n"
354 "";
355
Willy Tarreau295a8372011-03-10 11:25:07 +0100356/* data transmission states for the stats responses */
357enum {
358 STAT_ST_INIT = 0,
359 STAT_ST_HEAD,
360 STAT_ST_INFO,
361 STAT_ST_LIST,
362 STAT_ST_END,
363 STAT_ST_FIN,
364};
365
366/* data transmission states for the stats responses inside a proxy */
367enum {
368 STAT_PX_ST_INIT = 0,
369 STAT_PX_ST_TH,
370 STAT_PX_ST_FE,
371 STAT_PX_ST_LI,
372 STAT_PX_ST_SV,
373 STAT_PX_ST_BE,
374 STAT_PX_ST_END,
375 STAT_PX_ST_FIN,
376};
377
Cyril Bonté19979e12012-04-04 12:57:21 +0200378extern const char *stat_status_codes[];
379
Willy Tarreau07e9e642010-08-17 21:48:17 +0200380/* allocate a new stats frontend named <name>, and return it
381 * (or NULL in case of lack of memory).
382 */
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200383static struct proxy *alloc_stats_fe(const char *name, const char *file, int line)
Willy Tarreau07e9e642010-08-17 21:48:17 +0200384{
385 struct proxy *fe;
386
387 fe = (struct proxy *)calloc(1, sizeof(struct proxy));
388 if (!fe)
389 return NULL;
390
Willy Tarreau237250c2011-07-29 01:49:03 +0200391 init_new_proxy(fe);
Willy Tarreau050536d2012-10-04 08:47:34 +0200392 fe->next = proxy;
393 proxy = fe;
Willy Tarreau07e9e642010-08-17 21:48:17 +0200394 fe->last_change = now.tv_sec;
395 fe->id = strdup("GLOBAL");
396 fe->cap = PR_CAP_FE;
Willy Tarreauc2adf8b2011-09-07 12:13:34 +0200397 fe->maxconn = 10; /* default to 10 concurrent connections */
398 fe->timeout.client = MS_TO_TICKS(10000); /* default timeout of 10 seconds */
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200399 fe->conf.file = strdup(file);
400 fe->conf.line = line;
Willy Tarreau32b60d42015-03-13 16:14:57 +0100401 fe->accept = frontend_accept;
Willy Tarreauf87ab942015-03-13 15:55:16 +0100402 fe->default_target = &cli_applet.obj_type;
Willy Tarreau050536d2012-10-04 08:47:34 +0200403
404 /* the stats frontend is the only one able to assign ID #0 */
405 fe->conf.id.key = fe->uuid = 0;
406 eb32_insert(&used_proxy_id, &fe->conf.id);
Willy Tarreau07e9e642010-08-17 21:48:17 +0200407 return fe;
408}
409
Willy Tarreaufbee7132007-10-18 13:53:22 +0200410/* This function parses a "stats" statement in the "global" section. It returns
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200411 * -1 if there is any error, otherwise zero. If it returns -1, it will write an
412 * error message into the <err> buffer which will be preallocated. The trailing
413 * '\n' must not be written. The function must be called with <args> pointing to
414 * the first word after "stats".
Willy Tarreaufbee7132007-10-18 13:53:22 +0200415 */
Willy Tarreau10522fd2008-07-09 20:12:41 +0200416static int stats_parse_global(char **args, int section_type, struct proxy *curpx,
Willy Tarreau28a47d62012-09-18 20:02:48 +0200417 struct proxy *defpx, const char *file, int line,
418 char **err)
Willy Tarreaufbee7132007-10-18 13:53:22 +0200419{
Willy Tarreau4348fad2012-09-20 16:48:07 +0200420 struct bind_conf *bind_conf;
Willy Tarreauc53d4222012-09-20 20:19:28 +0200421 struct listener *l;
Willy Tarreau4348fad2012-09-20 16:48:07 +0200422
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200423 if (!strcmp(args[1], "socket")) {
Willy Tarreaufbee7132007-10-18 13:53:22 +0200424 int cur_arg;
425
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200426 if (*args[2] == 0) {
Willy Tarreauc53d4222012-09-20 20:19:28 +0200427 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 +0200428 return -1;
429 }
430
Willy Tarreau89a63132009-08-16 17:41:45 +0200431 if (!global.stats_fe) {
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200432 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200433 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
Willy Tarreau89a63132009-08-16 17:41:45 +0200434 return -1;
435 }
Willy Tarreau89a63132009-08-16 17:41:45 +0200436 }
437
Willy Tarreau4348fad2012-09-20 16:48:07 +0200438 bind_conf = bind_conf_alloc(&global.stats_fe->conf.bind, file, line, args[2]);
Willy Tarreau290e63a2012-09-20 18:07:14 +0200439 bind_conf->level = ACCESS_LVL_OPER; /* default access level */
Willy Tarreau4348fad2012-09-20 16:48:07 +0200440
Willy Tarreauc53d4222012-09-20 20:19:28 +0200441 if (!str2listener(args[2], global.stats_fe, bind_conf, file, line, err)) {
442 memprintf(err, "parsing [%s:%d] : '%s %s' : %s\n",
443 file, line, args[0], args[1], err && *err ? *err : "error");
444 return -1;
445 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200446
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200447 cur_arg = 3;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200448 while (*args[cur_arg]) {
Willy Tarreaud5781202012-09-22 19:32:35 +0200449 static int bind_dumped;
450 struct bind_kw *kw;
451
452 kw = bind_find_kw(args[cur_arg]);
453 if (kw) {
454 if (!kw->parse) {
455 memprintf(err, "'%s %s' : '%s' option is not implemented in this version (check build options).",
456 args[0], args[1], args[cur_arg]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200457 return -1;
458 }
Willy Tarreaud5781202012-09-22 19:32:35 +0200459
Willy Tarreau061b5de2015-10-13 15:06:57 +0200460 if (kw->parse(args, cur_arg, global.stats_fe, bind_conf, err) != 0) {
Willy Tarreaud5781202012-09-22 19:32:35 +0200461 if (err && *err)
462 memprintf(err, "'%s %s' : '%s'", args[0], args[1], *err);
463 else
464 memprintf(err, "'%s %s' : error encountered while processing '%s'",
465 args[0], args[1], args[cur_arg]);
Willy Tarreau6162db22009-10-10 17:13:00 +0200466 return -1;
467 }
Willy Tarreaud5781202012-09-22 19:32:35 +0200468
469 cur_arg += 1 + kw->skip;
470 continue;
Willy Tarreau6162db22009-10-10 17:13:00 +0200471 }
Willy Tarreaud5781202012-09-22 19:32:35 +0200472
473 if (!bind_dumped) {
474 bind_dump_kws(err);
475 indent_msg(err, 4);
476 bind_dumped = 1;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200477 }
Willy Tarreaud5781202012-09-22 19:32:35 +0200478
479 memprintf(err, "'%s %s' : unknown keyword '%s'.%s%s",
480 args[0], args[1], args[cur_arg],
481 err && *err ? " Registered keywords :" : "", err && *err ? *err : "");
482 return -1;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200483 }
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100484
Willy Tarreauc53d4222012-09-20 20:19:28 +0200485 list_for_each_entry(l, &bind_conf->listeners, by_bind) {
486 l->maxconn = global.stats_fe->maxconn;
487 l->backlog = global.stats_fe->backlog;
Willy Tarreau9903f0e2015-04-04 18:50:31 +0200488 l->accept = session_accept_fd;
Willy Tarreau87b09662015-04-03 00:22:06 +0200489 l->handler = process_stream;
Willy Tarreau10b688f2015-03-13 16:43:12 +0100490 l->default_target = global.stats_fe->default_target;
Willy Tarreauc53d4222012-09-20 20:19:28 +0200491 l->options |= LI_O_UNLIMITED; /* don't make the peers subject to global limits */
492 l->nice = -64; /* we want to boost priority for local stats */
493 global.maxsock += l->maxconn;
494 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200495 }
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200496 else if (!strcmp(args[1], "timeout")) {
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100497 unsigned timeout;
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200498 const char *res = parse_time_err(args[2], &timeout, TIME_UNIT_MS);
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100499
500 if (res) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200501 memprintf(err, "'%s %s' : unexpected character '%c'", args[0], args[1], *res);
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100502 return -1;
503 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200504
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100505 if (!timeout) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200506 memprintf(err, "'%s %s' expects a positive value", args[0], args[1]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200507 return -1;
508 }
Willy Tarreau07e9e642010-08-17 21:48:17 +0200509 if (!global.stats_fe) {
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200510 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200511 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
Willy Tarreau07e9e642010-08-17 21:48:17 +0200512 return -1;
513 }
514 }
Willy Tarreau89a63132009-08-16 17:41:45 +0200515 global.stats_fe->timeout.client = MS_TO_TICKS(timeout);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200516 }
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200517 else if (!strcmp(args[1], "maxconn")) {
518 int maxconn = atol(args[2]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200519
520 if (maxconn <= 0) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200521 memprintf(err, "'%s %s' expects a positive value", args[0], args[1]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200522 return -1;
523 }
Willy Tarreauc2adf8b2011-09-07 12:13:34 +0200524
525 if (!global.stats_fe) {
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200526 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200527 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
Willy Tarreauc2adf8b2011-09-07 12:13:34 +0200528 return -1;
529 }
530 }
531 global.stats_fe->maxconn = maxconn;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200532 }
Willy Tarreau35b7b162012-10-22 23:17:18 +0200533 else if (!strcmp(args[1], "bind-process")) { /* enable the socket only on some processes */
534 int cur_arg = 2;
Willy Tarreaua9db57e2013-01-18 11:29:29 +0100535 unsigned long set = 0;
Willy Tarreau35b7b162012-10-22 23:17:18 +0200536
Willy Tarreau91319572013-04-20 09:48:50 +0200537 if (!global.stats_fe) {
538 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
539 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
540 return -1;
541 }
542 }
543
Willy Tarreau35b7b162012-10-22 23:17:18 +0200544 while (*args[cur_arg]) {
Willy Tarreau110ecc12012-11-15 17:50:01 +0100545 unsigned int low, high;
546
Willy Tarreau35b7b162012-10-22 23:17:18 +0200547 if (strcmp(args[cur_arg], "all") == 0) {
548 set = 0;
549 break;
550 }
551 else if (strcmp(args[cur_arg], "odd") == 0) {
Willy Tarreaua9db57e2013-01-18 11:29:29 +0100552 set |= ~0UL/3UL; /* 0x555....555 */
Willy Tarreau35b7b162012-10-22 23:17:18 +0200553 }
554 else if (strcmp(args[cur_arg], "even") == 0) {
Willy Tarreaua9db57e2013-01-18 11:29:29 +0100555 set |= (~0UL/3UL) << 1; /* 0xAAA...AAA */
Willy Tarreau35b7b162012-10-22 23:17:18 +0200556 }
Willy Tarreau83d84cf2012-11-22 01:04:31 +0100557 else if (isdigit((int)*args[cur_arg])) {
Willy Tarreau110ecc12012-11-15 17:50:01 +0100558 char *dash = strchr(args[cur_arg], '-');
559
560 low = high = str2uic(args[cur_arg]);
561 if (dash)
562 high = str2uic(dash + 1);
563
564 if (high < low) {
565 unsigned int swap = low;
566 low = high;
567 high = swap;
568 }
569
Willy Tarreaua9db57e2013-01-18 11:29:29 +0100570 if (low < 1 || high > LONGBITS) {
571 memprintf(err, "'%s %s' supports process numbers from 1 to %d.\n",
572 args[0], args[1], LONGBITS);
Willy Tarreau35b7b162012-10-22 23:17:18 +0200573 return -1;
574 }
Willy Tarreau110ecc12012-11-15 17:50:01 +0100575 while (low <= high)
Willy Tarreaua9db57e2013-01-18 11:29:29 +0100576 set |= 1UL << (low++ - 1);
Willy Tarreau110ecc12012-11-15 17:50:01 +0100577 }
578 else {
579 memprintf(err,
Willy Tarreaua9db57e2013-01-18 11:29:29 +0100580 "'%s %s' expects 'all', 'odd', 'even', or a list of process ranges with numbers from 1 to %d.\n",
581 args[0], args[1], LONGBITS);
Willy Tarreau110ecc12012-11-15 17:50:01 +0100582 return -1;
Willy Tarreau35b7b162012-10-22 23:17:18 +0200583 }
584 cur_arg++;
585 }
586 global.stats_fe->bind_proc = set;
587 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200588 else {
Willy Tarreau35b7b162012-10-22 23:17:18 +0200589 memprintf(err, "'%s' only supports 'socket', 'maxconn', 'bind-process' and 'timeout' (got '%s')", args[0], args[1]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200590 return -1;
591 }
592 return 0;
593}
594
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100595/* Dumps the stats CSV header to the trash buffer which. The caller is responsible
596 * for clearing it if needed.
Willy Tarreauf522f3d2014-02-10 22:22:49 +0100597 * NOTE: Some tools happen to rely on the field position instead of its name,
598 * so please only append new fields at the end, never in the middle.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100599 */
600static void stats_dump_csv_header()
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100601{
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100602 chunk_appendf(&trash,
603 "# pxname,svname,"
604 "qcur,qmax,"
Willy Tarreauf522f3d2014-02-10 22:22:49 +0100605 "scur,smax,slim,stot,"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100606 "bin,bout,"
607 "dreq,dresp,"
608 "ereq,econ,eresp,"
609 "wretr,wredis,"
610 "status,weight,act,bck,"
611 "chkfail,chkdown,lastchg,downtime,qlimit,"
612 "pid,iid,sid,throttle,lbtot,tracked,type,"
613 "rate,rate_lim,rate_max,"
614 "check_status,check_code,check_duration,"
615 "hrsp_1xx,hrsp_2xx,hrsp_3xx,hrsp_4xx,hrsp_5xx,hrsp_other,hanafail,"
616 "req_rate,req_rate_max,req_tot,"
617 "cli_abrt,srv_abrt,"
Willy Tarreauf5b1cc32014-06-17 12:20:59 +0200618 "comp_in,comp_out,comp_byp,comp_rsp,lastsess,last_chk,last_agt,qtime,ctime,rtime,ttime,"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100619 "\n");
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100620}
621
Simon Hormand9366582011-06-15 15:18:45 +0900622/* print a string of text buffer to <out>. The format is :
623 * Non-printable chars \t, \n, \r and \e are * encoded in C format.
624 * Other non-printable chars are encoded "\xHH". Space and '\' are also escaped.
625 * Print stopped if null char or <bsize> is reached, or if no more place in the chunk.
626 */
627static int dump_text(struct chunk *out, const char *buf, int bsize)
628{
629 unsigned char c;
630 int ptr = 0;
631
632 while (buf[ptr] && ptr < bsize) {
633 c = buf[ptr];
634 if (isprint(c) && isascii(c) && c != '\\' && c != ' ') {
635 if (out->len > out->size - 1)
636 break;
637 out->str[out->len++] = c;
638 }
639 else if (c == '\t' || c == '\n' || c == '\r' || c == '\e' || c == '\\' || c == ' ') {
640 if (out->len > out->size - 2)
641 break;
642 out->str[out->len++] = '\\';
643 switch (c) {
644 case ' ': c = ' '; break;
645 case '\t': c = 't'; break;
646 case '\n': c = 'n'; break;
647 case '\r': c = 'r'; break;
648 case '\e': c = 'e'; break;
649 case '\\': c = '\\'; break;
650 }
651 out->str[out->len++] = c;
652 }
653 else {
654 if (out->len > out->size - 4)
655 break;
656 out->str[out->len++] = '\\';
657 out->str[out->len++] = 'x';
658 out->str[out->len++] = hextab[(c >> 4) & 0xF];
659 out->str[out->len++] = hextab[c & 0xF];
660 }
661 ptr++;
662 }
663
664 return ptr;
665}
666
667/* print a buffer in hexa.
668 * Print stopped if <bsize> is reached, or if no more place in the chunk.
669 */
670static int dump_binary(struct chunk *out, const char *buf, int bsize)
671{
672 unsigned char c;
673 int ptr = 0;
674
675 while (ptr < bsize) {
676 c = buf[ptr];
677
678 if (out->len > out->size - 2)
679 break;
680 out->str[out->len++] = hextab[(c >> 4) & 0xF];
681 out->str[out->len++] = hextab[c & 0xF];
682
683 ptr++;
684 }
685 return ptr;
686}
687
688/* Dump the status of a table to a stream interface's
689 * read buffer. It returns 0 if the output buffer is full
690 * and needs to be called again, otherwise non-zero.
691 */
692static int stats_dump_table_head_to_buffer(struct chunk *msg, struct stream_interface *si,
693 struct proxy *proxy, struct proxy *target)
694{
Willy Tarreau87b09662015-04-03 00:22:06 +0200695 struct stream *s = si_strm(si);
Simon Hormand9366582011-06-15 15:18:45 +0900696
Willy Tarreau77804732012-10-29 16:14:26 +0100697 chunk_appendf(msg, "# table: %s, type: %s, size:%d, used:%d\n",
Simon Hormand9366582011-06-15 15:18:45 +0900698 proxy->id, stktable_types[proxy->table.type].kw, proxy->table.size, proxy->table.current);
699
700 /* any other information should be dumped here */
701
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200702 if (target && strm_li(s)->bind_conf->level < ACCESS_LVL_OPER)
Willy Tarreau77804732012-10-29 16:14:26 +0100703 chunk_appendf(msg, "# contents not dumped due to insufficient privileges\n");
Simon Hormand9366582011-06-15 15:18:45 +0900704
Willy Tarreaubc18da12015-03-13 14:00:47 +0100705 if (bi_putchk(si_ic(si), msg) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +0200706 si_applet_cant_put(si);
Simon Hormand9366582011-06-15 15:18:45 +0900707 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +0100708 }
Simon Hormand9366582011-06-15 15:18:45 +0900709
710 return 1;
711}
712
713/* Dump the a table entry to a stream interface's
714 * read buffer. It returns 0 if the output buffer is full
715 * and needs to be called again, otherwise non-zero.
716 */
717static int stats_dump_table_entry_to_buffer(struct chunk *msg, struct stream_interface *si,
718 struct proxy *proxy, struct stksess *entry)
719{
720 int dt;
721
Willy Tarreau77804732012-10-29 16:14:26 +0100722 chunk_appendf(msg, "%p:", entry);
Simon Hormand9366582011-06-15 15:18:45 +0900723
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200724 if (proxy->table.type == SMP_T_IPV4) {
Simon Hormand9366582011-06-15 15:18:45 +0900725 char addr[INET_ADDRSTRLEN];
726 inet_ntop(AF_INET, (const void *)&entry->key.key, addr, sizeof(addr));
Willy Tarreau77804732012-10-29 16:14:26 +0100727 chunk_appendf(msg, " key=%s", addr);
Simon Hormand9366582011-06-15 15:18:45 +0900728 }
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200729 else if (proxy->table.type == SMP_T_IPV6) {
Simon Hormand9366582011-06-15 15:18:45 +0900730 char addr[INET6_ADDRSTRLEN];
731 inet_ntop(AF_INET6, (const void *)&entry->key.key, addr, sizeof(addr));
Willy Tarreau77804732012-10-29 16:14:26 +0100732 chunk_appendf(msg, " key=%s", addr);
Simon Hormand9366582011-06-15 15:18:45 +0900733 }
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200734 else if (proxy->table.type == SMP_T_SINT) {
Willy Tarreau77804732012-10-29 16:14:26 +0100735 chunk_appendf(msg, " key=%u", *(unsigned int *)entry->key.key);
Simon Hormand9366582011-06-15 15:18:45 +0900736 }
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200737 else if (proxy->table.type == SMP_T_STR) {
Willy Tarreau77804732012-10-29 16:14:26 +0100738 chunk_appendf(msg, " key=");
Simon Hormand9366582011-06-15 15:18:45 +0900739 dump_text(msg, (const char *)entry->key.key, proxy->table.key_size);
740 }
741 else {
Willy Tarreau77804732012-10-29 16:14:26 +0100742 chunk_appendf(msg, " key=");
Simon Hormand9366582011-06-15 15:18:45 +0900743 dump_binary(msg, (const char *)entry->key.key, proxy->table.key_size);
744 }
745
Willy Tarreau77804732012-10-29 16:14:26 +0100746 chunk_appendf(msg, " use=%d exp=%d", entry->ref_cnt - 1, tick_remain(now_ms, entry->expire));
Simon Hormand9366582011-06-15 15:18:45 +0900747
748 for (dt = 0; dt < STKTABLE_DATA_TYPES; dt++) {
749 void *ptr;
750
751 if (proxy->table.data_ofs[dt] == 0)
752 continue;
753 if (stktable_data_types[dt].arg_type == ARG_T_DELAY)
Willy Tarreau77804732012-10-29 16:14:26 +0100754 chunk_appendf(msg, " %s(%d)=", stktable_data_types[dt].name, proxy->table.data_arg[dt].u);
Simon Hormand9366582011-06-15 15:18:45 +0900755 else
Willy Tarreau77804732012-10-29 16:14:26 +0100756 chunk_appendf(msg, " %s=", stktable_data_types[dt].name);
Simon Hormand9366582011-06-15 15:18:45 +0900757
758 ptr = stktable_data_ptr(&proxy->table, entry, dt);
759 switch (stktable_data_types[dt].std_type) {
760 case STD_T_SINT:
Willy Tarreau77804732012-10-29 16:14:26 +0100761 chunk_appendf(msg, "%d", stktable_data_cast(ptr, std_t_sint));
Simon Hormand9366582011-06-15 15:18:45 +0900762 break;
763 case STD_T_UINT:
Willy Tarreau77804732012-10-29 16:14:26 +0100764 chunk_appendf(msg, "%u", stktable_data_cast(ptr, std_t_uint));
Simon Hormand9366582011-06-15 15:18:45 +0900765 break;
766 case STD_T_ULL:
Willy Tarreau77804732012-10-29 16:14:26 +0100767 chunk_appendf(msg, "%lld", stktable_data_cast(ptr, std_t_ull));
Simon Hormand9366582011-06-15 15:18:45 +0900768 break;
769 case STD_T_FRQP:
Willy Tarreau77804732012-10-29 16:14:26 +0100770 chunk_appendf(msg, "%d",
Simon Hormand9366582011-06-15 15:18:45 +0900771 read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
772 proxy->table.data_arg[dt].u));
773 break;
774 }
775 }
Willy Tarreau77804732012-10-29 16:14:26 +0100776 chunk_appendf(msg, "\n");
Simon Hormand9366582011-06-15 15:18:45 +0900777
Willy Tarreaubc18da12015-03-13 14:00:47 +0100778 if (bi_putchk(si_ic(si), msg) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +0200779 si_applet_cant_put(si);
Simon Hormand9366582011-06-15 15:18:45 +0900780 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +0100781 }
Simon Hormand9366582011-06-15 15:18:45 +0900782
783 return 1;
784}
785
Willy Tarreaudec98142012-06-06 23:37:08 +0200786static void stats_sock_table_key_request(struct stream_interface *si, char **args, int action)
Simon Horman121f3052011-06-15 15:18:46 +0900787{
Willy Tarreau87b09662015-04-03 00:22:06 +0200788 struct stream *s = si_strm(si);
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100789 struct appctx *appctx = __objt_appctx(si->end);
790 struct proxy *px = appctx->ctx.table.target;
Simon Horman121f3052011-06-15 15:18:46 +0900791 struct stksess *ts;
Simon Hormancec9a222011-06-15 15:18:51 +0900792 uint32_t uint32_key;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900793 unsigned char ip6_key[sizeof(struct in6_addr)];
Willy Tarreau654694e2012-06-07 01:03:16 +0200794 long long value;
795 int data_type;
Willy Tarreau47060b62013-08-01 21:11:42 +0200796 int cur_arg;
Willy Tarreau654694e2012-06-07 01:03:16 +0200797 void *ptr;
798 struct freq_ctr_period *frqp;
Simon Horman121f3052011-06-15 15:18:46 +0900799
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100800 appctx->st0 = STAT_CLI_OUTPUT;
Simon Horman121f3052011-06-15 15:18:46 +0900801
802 if (!*args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100803 appctx->ctx.cli.msg = "Key value expected\n";
804 appctx->st0 = STAT_CLI_PRINT;
Simon Horman121f3052011-06-15 15:18:46 +0900805 return;
806 }
807
Simon Hormanc5b89f62011-06-15 15:18:50 +0900808 switch (px->table.type) {
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200809 case SMP_T_IPV4:
Simon Hormancec9a222011-06-15 15:18:51 +0900810 uint32_key = htonl(inetaddr_host(args[4]));
Willy Tarreau07115412012-10-29 21:56:59 +0100811 static_table_key->key = &uint32_key;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900812 break;
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200813 case SMP_T_IPV6:
Simon Hormanc5b89f62011-06-15 15:18:50 +0900814 inet_pton(AF_INET6, args[4], ip6_key);
Willy Tarreau07115412012-10-29 21:56:59 +0100815 static_table_key->key = &ip6_key;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900816 break;
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200817 case SMP_T_SINT:
Simon Hormancec9a222011-06-15 15:18:51 +0900818 {
819 char *endptr;
820 unsigned long val;
821 errno = 0;
822 val = strtoul(args[4], &endptr, 10);
823 if ((errno == ERANGE && val == ULONG_MAX) ||
824 (errno != 0 && val == 0) || endptr == args[4] ||
825 val > 0xffffffff) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100826 appctx->ctx.cli.msg = "Invalid key\n";
827 appctx->st0 = STAT_CLI_PRINT;
Simon Hormancec9a222011-06-15 15:18:51 +0900828 return;
829 }
830 uint32_key = (uint32_t) val;
Willy Tarreau07115412012-10-29 21:56:59 +0100831 static_table_key->key = &uint32_key;
Simon Hormancec9a222011-06-15 15:18:51 +0900832 break;
833 }
834 break;
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200835 case SMP_T_STR:
Willy Tarreau07115412012-10-29 21:56:59 +0100836 static_table_key->key = args[4];
837 static_table_key->key_len = strlen(args[4]);
Simon Horman619e3cc2011-06-15 15:18:52 +0900838 break;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900839 default:
Willy Tarreaudec98142012-06-06 23:37:08 +0200840 switch (action) {
841 case STAT_CLI_O_TAB:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100842 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 +0200843 break;
844 case STAT_CLI_O_CLR:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100845 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 +0200846 break;
847 default:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100848 appctx->ctx.cli.msg = "Unknown action\n";
Willy Tarreaudec98142012-06-06 23:37:08 +0200849 break;
850 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100851 appctx->st0 = STAT_CLI_PRINT;
Simon Horman121f3052011-06-15 15:18:46 +0900852 return;
853 }
854
855 /* check permissions */
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200856 if (strm_li(s)->bind_conf->level < ACCESS_LVL_OPER) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100857 appctx->ctx.cli.msg = stats_permission_denied_msg;
858 appctx->st0 = STAT_CLI_PRINT;
Simon Horman121f3052011-06-15 15:18:46 +0900859 return;
860 }
861
Willy Tarreau07115412012-10-29 21:56:59 +0100862 ts = stktable_lookup_key(&px->table, static_table_key);
Simon Horman17bce342011-06-15 15:18:47 +0900863
Willy Tarreaudec98142012-06-06 23:37:08 +0200864 switch (action) {
865 case STAT_CLI_O_TAB:
866 if (!ts)
867 return;
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100868 chunk_reset(&trash);
869 if (!stats_dump_table_head_to_buffer(&trash, si, px, px))
Simon Horman17bce342011-06-15 15:18:47 +0900870 return;
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100871 stats_dump_table_entry_to_buffer(&trash, si, px, ts);
Simon Horman121f3052011-06-15 15:18:46 +0900872 return;
Willy Tarreaudec98142012-06-06 23:37:08 +0200873
874 case STAT_CLI_O_CLR:
875 if (!ts)
876 return;
877 if (ts->ref_cnt) {
878 /* don't delete an entry which is currently referenced */
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100879 appctx->ctx.cli.msg = "Entry currently in use, cannot remove\n";
880 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaudec98142012-06-06 23:37:08 +0200881 return;
882 }
883 stksess_kill(&px->table, ts);
884 break;
Simon Horman17bce342011-06-15 15:18:47 +0900885
Willy Tarreau654694e2012-06-07 01:03:16 +0200886 case STAT_CLI_O_SET:
Willy Tarreau654694e2012-06-07 01:03:16 +0200887 if (ts)
888 stktable_touch(&px->table, ts, 1);
889 else {
Willy Tarreau07115412012-10-29 21:56:59 +0100890 ts = stksess_new(&px->table, static_table_key);
Willy Tarreau654694e2012-06-07 01:03:16 +0200891 if (!ts) {
892 /* don't delete an entry which is currently referenced */
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100893 appctx->ctx.cli.msg = "Unable to allocate a new entry\n";
894 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau654694e2012-06-07 01:03:16 +0200895 return;
896 }
897 stktable_store(&px->table, ts, 1);
898 }
899
Willy Tarreau47060b62013-08-01 21:11:42 +0200900 for (cur_arg = 5; *args[cur_arg]; cur_arg += 2) {
901 if (strncmp(args[cur_arg], "data.", 5) != 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100902 appctx->ctx.cli.msg = "\"data.<type>\" followed by a value expected\n";
903 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau47060b62013-08-01 21:11:42 +0200904 return;
905 }
906
907 data_type = stktable_get_data_type(args[cur_arg] + 5);
908 if (data_type < 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100909 appctx->ctx.cli.msg = "Unknown data type\n";
910 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau47060b62013-08-01 21:11:42 +0200911 return;
912 }
913
914 if (!px->table.data_ofs[data_type]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100915 appctx->ctx.cli.msg = "Data type not stored in this table\n";
916 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau47060b62013-08-01 21:11:42 +0200917 return;
918 }
919
920 if (!*args[cur_arg+1] || strl2llrc(args[cur_arg+1], strlen(args[cur_arg+1]), &value) != 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100921 appctx->ctx.cli.msg = "Require a valid integer value to store\n";
922 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau47060b62013-08-01 21:11:42 +0200923 return;
924 }
925
926 ptr = stktable_data_ptr(&px->table, ts, data_type);
927
928 switch (stktable_data_types[data_type].std_type) {
929 case STD_T_SINT:
930 stktable_data_cast(ptr, std_t_sint) = value;
931 break;
932 case STD_T_UINT:
933 stktable_data_cast(ptr, std_t_uint) = value;
934 break;
935 case STD_T_ULL:
936 stktable_data_cast(ptr, std_t_ull) = value;
937 break;
938 case STD_T_FRQP:
939 /* We set both the current and previous values. That way
940 * the reported frequency is stable during all the period
941 * then slowly fades out. This allows external tools to
942 * push measures without having to update them too often.
943 */
944 frqp = &stktable_data_cast(ptr, std_t_frqp);
945 frqp->curr_tick = now_ms;
946 frqp->prev_ctr = 0;
947 frqp->curr_ctr = value;
948 break;
949 }
Willy Tarreau654694e2012-06-07 01:03:16 +0200950 }
951 break;
952
Willy Tarreaudec98142012-06-06 23:37:08 +0200953 default:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100954 appctx->ctx.cli.msg = "Unknown action\n";
955 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaudec98142012-06-06 23:37:08 +0200956 break;
Simon Horman121f3052011-06-15 15:18:46 +0900957 }
Simon Horman121f3052011-06-15 15:18:46 +0900958}
959
Willy Tarreau654694e2012-06-07 01:03:16 +0200960static void stats_sock_table_data_request(struct stream_interface *si, char **args, int action)
Simon Hormand5b9fd92011-06-15 15:18:48 +0900961{
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100962 struct appctx *appctx = __objt_appctx(si->end);
963
Willy Tarreau04b3a192013-04-13 09:41:37 +0200964 if (action != STAT_CLI_O_TAB && action != STAT_CLI_O_CLR) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100965 appctx->ctx.cli.msg = "content-based lookup is only supported with the \"show\" and \"clear\" actions";
966 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau654694e2012-06-07 01:03:16 +0200967 return;
968 }
969
Simon Hormand5b9fd92011-06-15 15:18:48 +0900970 /* condition on stored data value */
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100971 appctx->ctx.table.data_type = stktable_get_data_type(args[3] + 5);
972 if (appctx->ctx.table.data_type < 0) {
973 appctx->ctx.cli.msg = "Unknown data type\n";
974 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900975 return;
976 }
977
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100978 if (!((struct proxy *)appctx->ctx.table.target)->table.data_ofs[appctx->ctx.table.data_type]) {
979 appctx->ctx.cli.msg = "Data type not stored in this table\n";
980 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900981 return;
982 }
983
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100984 appctx->ctx.table.data_op = get_std_op(args[4]);
985 if (appctx->ctx.table.data_op < 0) {
986 appctx->ctx.cli.msg = "Require and operator among \"eq\", \"ne\", \"le\", \"ge\", \"lt\", \"gt\"\n";
987 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900988 return;
989 }
990
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100991 if (!*args[5] || strl2llrc(args[5], strlen(args[5]), &appctx->ctx.table.value) != 0) {
992 appctx->ctx.cli.msg = "Require a valid integer value to compare against\n";
993 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900994 return;
995 }
996}
997
Willy Tarreaudec98142012-06-06 23:37:08 +0200998static void stats_sock_table_request(struct stream_interface *si, char **args, int action)
Simon Hormand5b9fd92011-06-15 15:18:48 +0900999{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001000 struct appctx *appctx = __objt_appctx(si->end);
1001
1002 appctx->ctx.table.data_type = -1;
1003 appctx->st2 = STAT_ST_INIT;
1004 appctx->ctx.table.target = NULL;
1005 appctx->ctx.table.proxy = NULL;
1006 appctx->ctx.table.entry = NULL;
1007 appctx->st0 = action;
Simon Hormand5b9fd92011-06-15 15:18:48 +09001008
1009 if (*args[2]) {
Willy Tarreaue2dc1fa2015-05-26 12:08:07 +02001010 appctx->ctx.table.target = proxy_tbl_by_name(args[2]);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001011 if (!appctx->ctx.table.target) {
1012 appctx->ctx.cli.msg = "No such table\n";
1013 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +09001014 return;
1015 }
1016 }
1017 else {
Willy Tarreaudec98142012-06-06 23:37:08 +02001018 if (action != STAT_CLI_O_TAB)
Simon Hormand5b9fd92011-06-15 15:18:48 +09001019 goto err_args;
1020 return;
1021 }
1022
1023 if (strcmp(args[3], "key") == 0)
Willy Tarreaudec98142012-06-06 23:37:08 +02001024 stats_sock_table_key_request(si, args, action);
Simon Hormanc88b8872011-06-15 15:18:49 +09001025 else if (strncmp(args[3], "data.", 5) == 0)
Willy Tarreau654694e2012-06-07 01:03:16 +02001026 stats_sock_table_data_request(si, args, action);
Simon Hormanc88b8872011-06-15 15:18:49 +09001027 else if (*args[3])
Simon Hormand5b9fd92011-06-15 15:18:48 +09001028 goto err_args;
1029
1030 return;
1031
1032err_args:
Willy Tarreaudec98142012-06-06 23:37:08 +02001033 switch (action) {
1034 case STAT_CLI_O_TAB:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001035 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 +02001036 break;
1037 case STAT_CLI_O_CLR:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001038 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 +02001039 break;
1040 default:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001041 appctx->ctx.cli.msg = "Unknown action\n";
Willy Tarreaudec98142012-06-06 23:37:08 +02001042 break;
1043 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001044 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +09001045}
1046
Willy Tarreau532a4502011-09-07 22:37:44 +02001047/* Expects to find a frontend named <arg> and returns it, otherwise displays various
Willy Tarreau87b09662015-04-03 00:22:06 +02001048 * adequate error messages and returns NULL. This function also expects the stream
Willy Tarreau532a4502011-09-07 22:37:44 +02001049 * level to be admin.
1050 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001051static struct proxy *expect_frontend_admin(struct stream *s, struct stream_interface *si, const char *arg)
Willy Tarreau532a4502011-09-07 22:37:44 +02001052{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001053 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau532a4502011-09-07 22:37:44 +02001054 struct proxy *px;
1055
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001056 if (strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001057 appctx->ctx.cli.msg = stats_permission_denied_msg;
1058 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001059 return NULL;
1060 }
1061
1062 if (!*arg) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001063 appctx->ctx.cli.msg = "A frontend name is expected.\n";
1064 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001065 return NULL;
1066 }
1067
Willy Tarreau9e0bb102015-05-26 11:24:42 +02001068 px = proxy_fe_by_name(arg);
Willy Tarreau532a4502011-09-07 22:37:44 +02001069 if (!px) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001070 appctx->ctx.cli.msg = "No such frontend.\n";
1071 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001072 return NULL;
1073 }
1074 return px;
1075}
1076
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001077/* Expects to find a backend and a server in <arg> under the form <backend>/<server>,
1078 * and returns the pointer to the server. Otherwise, display adequate error messages
Willy Tarreau87b09662015-04-03 00:22:06 +02001079 * and returns NULL. This function also expects the stream level to be admin. Note:
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001080 * the <arg> is modified to remove the '/'.
1081 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001082static struct server *expect_server_admin(struct stream *s, struct stream_interface *si, char *arg)
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001083{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001084 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001085 struct proxy *px;
1086 struct server *sv;
1087 char *line;
1088
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001089 if (strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001090 appctx->ctx.cli.msg = stats_permission_denied_msg;
1091 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001092 return NULL;
1093 }
1094
1095 /* split "backend/server" and make <line> point to server */
1096 for (line = arg; *line; line++)
1097 if (*line == '/') {
1098 *line++ = '\0';
1099 break;
1100 }
1101
1102 if (!*line || !*arg) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001103 appctx->ctx.cli.msg = "Require 'backend/server'.\n";
1104 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001105 return NULL;
1106 }
1107
1108 if (!get_backend_server(arg, line, &px, &sv)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001109 appctx->ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
1110 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001111 return NULL;
1112 }
1113
1114 if (px->state == PR_STSTOPPED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001115 appctx->ctx.cli.msg = "Proxy is disabled.\n";
1116 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001117 return NULL;
1118 }
1119
1120 return sv;
1121}
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02001122
1123/* This function is used with TLS ticket keys management. It permits to browse
1124 * each reference. The variable <getnext> must contain the current node,
1125 * <end> point to the root node.
1126 */
1127#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
1128static inline
1129struct tls_keys_ref *tlskeys_list_get_next(struct tls_keys_ref *getnext, struct list *end)
1130{
1131 struct tls_keys_ref *ref = getnext;
1132
1133 while (1) {
1134
1135 /* Get next list entry. */
1136 ref = LIST_NEXT(&ref->list, struct tls_keys_ref *, list);
1137
1138 /* If the entry is the last of the list, return NULL. */
1139 if (&ref->list == end)
1140 return NULL;
1141
1142 return ref;
1143 }
1144}
1145
1146static inline
1147struct tls_keys_ref *tlskeys_ref_lookup_ref(const char *reference)
1148{
1149 int id;
1150 char *error;
1151
1152 /* If the reference starts by a '#', this is numeric id. */
1153 if (reference[0] == '#') {
1154 /* Try to convert the numeric id. If the conversion fails, the lookup fails. */
1155 id = strtol(reference + 1, &error, 10);
1156 if (*error != '\0')
1157 return NULL;
1158
1159 /* Perform the unique id lookup. */
1160 return tlskeys_ref_lookupid(id);
1161 }
1162
1163 /* Perform the string lookup. */
1164 return tlskeys_ref_lookup(reference);
1165}
1166#endif
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001167
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001168/* This function is used with map and acl management. It permits to browse
1169 * each reference. The variable <getnext> must contain the current node,
1170 * <end> point to the root node and the <flags> permit to filter required
1171 * nodes.
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001172 */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001173static inline
1174struct pat_ref *pat_list_get_next(struct pat_ref *getnext, struct list *end,
1175 unsigned int flags)
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001176{
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001177 struct pat_ref *ref = getnext;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001178
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001179 while (1) {
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001180
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001181 /* Get next list entry. */
1182 ref = LIST_NEXT(&ref->list, struct pat_ref *, list);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001183
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001184 /* If the entry is the last of the list, return NULL. */
1185 if (&ref->list == end)
1186 return NULL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001187
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001188 /* If the entry match the flag, return it. */
1189 if (ref->flags & flags)
1190 return ref;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001191 }
1192}
1193
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001194static inline
1195struct pat_ref *pat_ref_lookup_ref(const char *reference)
1196{
1197 int id;
1198 char *error;
1199
1200 /* If the reference starts by a '#', this is numeric id. */
1201 if (reference[0] == '#') {
1202 /* Try to convert the numeric id. If the conversion fails, the lookup fails. */
1203 id = strtol(reference + 1, &error, 10);
1204 if (*error != '\0')
1205 return NULL;
1206
1207 /* Perform the unique id lookup. */
1208 return pat_ref_lookupid(id);
1209 }
1210
1211 /* Perform the string lookup. */
1212 return pat_ref_lookup(reference);
1213}
1214
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001215/* This function is used with map and acl management. It permits to browse
1216 * each reference.
1217 */
1218static inline
1219struct pattern_expr *pat_expr_get_next(struct pattern_expr *getnext, struct list *end)
1220{
1221 struct pattern_expr *expr;
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01001222 expr = LIST_NEXT(&getnext->list, struct pattern_expr *, list);
1223 if (&expr->list == end)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001224 return NULL;
1225 return expr;
1226}
1227
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001228/* Processes the stats interpreter on the statistics socket. This function is
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001229 * called from an applet running in a stream interface. The function returns 1
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001230 * if the request was understood, otherwise zero. It sets appctx->st0 to a value
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02001231 * designating the function which will have to process the request, which can
1232 * also be the print function to display the return message set into cli.msg.
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001233 */
Simon Horman9bd2c732011-06-15 15:18:44 +09001234static int stats_sock_parse_request(struct stream_interface *si, char *line)
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001235{
Willy Tarreau87b09662015-04-03 00:22:06 +02001236 struct stream *s = si_strm(si);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001237 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001238 char *args[MAX_STATS_ARGS + 1];
1239 int arg;
Thierry FOURNIER48bcfda2013-12-10 18:54:58 +01001240 int i, j;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001241
1242 while (isspace((unsigned char)*line))
1243 line++;
1244
1245 arg = 0;
1246 args[arg] = line;
1247
1248 while (*line && arg < MAX_STATS_ARGS) {
Thierry FOURNIER48bcfda2013-12-10 18:54:58 +01001249 if (*line == '\\') {
1250 line++;
1251 if (*line == '\0')
1252 break;
1253 }
1254 else if (isspace((unsigned char)*line)) {
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001255 *line++ = '\0';
1256
1257 while (isspace((unsigned char)*line))
1258 line++;
1259
1260 args[++arg] = line;
1261 continue;
1262 }
1263
1264 line++;
1265 }
1266
1267 while (++arg <= MAX_STATS_ARGS)
1268 args[arg] = line;
1269
Thierry FOURNIER48bcfda2013-12-10 18:54:58 +01001270 /* remove \ */
1271 arg = 0;
1272 while (*args[arg] != '\0') {
1273 j = 0;
1274 for (i=0; args[arg][i] != '\0'; i++) {
1275 if (args[arg][i] == '\\')
1276 continue;
1277 args[arg][j] = args[arg][i];
1278 j++;
1279 }
1280 args[arg][j] = '\0';
1281 arg++;
1282 }
1283
Willy Tarreau6bcb95d2015-05-04 18:07:56 +02001284 appctx->ctx.stats.scope_str = 0;
1285 appctx->ctx.stats.scope_len = 0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001286 appctx->ctx.stats.flags = 0;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001287 if (strcmp(args[0], "show") == 0) {
Baptiste Assmann9b6857e2015-09-18 14:49:12 +02001288 if (strcmp(args[1], "backend") == 0) {
1289 appctx->st2 = STAT_ST_INIT;
1290 appctx->st0 = STAT_CLI_O_BACKEND;
1291 }
Willy Tarreauae795722016-02-16 11:27:28 +01001292 else if (strcmp(args[1], "env") == 0) {
1293 extern char **environ;
1294
1295 if (strm_li(s)->bind_conf->level < ACCESS_LVL_OPER) {
1296 appctx->ctx.cli.msg = stats_permission_denied_msg;
1297 appctx->st0 = STAT_CLI_PRINT;
1298 return 1;
1299 }
1300 appctx->ctx.env.var = environ;
1301 appctx->st2 = STAT_ST_INIT;
1302 appctx->st0 = STAT_CLI_O_ENV; // stats_dump_env_to_buffer
1303
1304 if (*args[2]) {
1305 int len = strlen(args[2]);
1306
1307 for (; *appctx->ctx.env.var; appctx->ctx.env.var++) {
1308 if (strncmp(*appctx->ctx.env.var, args[2], len) == 0 &&
1309 (*appctx->ctx.env.var)[len] == '=')
1310 break;
1311 }
1312 if (!*appctx->ctx.env.var) {
1313 appctx->ctx.cli.msg = "Variable not found\n";
1314 appctx->st0 = STAT_CLI_PRINT;
1315 return 1;
1316 }
1317 appctx->st2 = STAT_ST_END;
1318 }
1319 }
1320 else if (strcmp(args[1], "stat") == 0) {
Baptiste Assmann3863f972015-05-17 00:33:24 +02001321 if (strcmp(args[2], "resolvers") == 0) {
1322 struct dns_resolvers *presolvers;
1323
Andrew Hayworth68d05342015-10-02 20:33:01 +00001324 if (*args[3]) {
1325 appctx->ctx.resolvers.ptr = NULL;
1326 list_for_each_entry(presolvers, &dns_resolvers, list) {
1327 if (strcmp(presolvers->id, args[3]) == 0) {
1328 appctx->ctx.resolvers.ptr = presolvers;
1329 break;
1330 }
Baptiste Assmann3863f972015-05-17 00:33:24 +02001331 }
Andrew Hayworth68d05342015-10-02 20:33:01 +00001332 if (appctx->ctx.resolvers.ptr == NULL) {
1333 appctx->ctx.cli.msg = "Can't find that resolvers section\n";
1334 appctx->st0 = STAT_CLI_PRINT;
1335 return 1;
1336 }
Baptiste Assmann3863f972015-05-17 00:33:24 +02001337 }
1338
1339 appctx->st2 = STAT_ST_INIT;
1340 appctx->st0 = STAT_CLI_O_RESOLVERS;
1341 return 1;
1342 }
1343 else if (*args[2] && *args[3] && *args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001344 appctx->ctx.stats.flags |= STAT_BOUND;
1345 appctx->ctx.stats.iid = atoi(args[2]);
1346 appctx->ctx.stats.type = atoi(args[3]);
1347 appctx->ctx.stats.sid = atoi(args[4]);
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001348 }
1349
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001350 appctx->st2 = STAT_ST_INIT;
1351 appctx->st0 = STAT_CLI_O_STAT; // stats_dump_stat_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001352 }
1353 else if (strcmp(args[1], "info") == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001354 appctx->st2 = STAT_ST_INIT;
1355 appctx->st0 = STAT_CLI_O_INFO; // stats_dump_info_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001356 }
Baptiste Assmann28289462015-07-03 08:01:20 +02001357 else if (strcmp(args[1], "servers") == 0 && strcmp(args[2], "state") == 0) {
1358 appctx->ctx.server_state.backend = NULL;
1359
1360 /* check if a backend name has been provided */
1361 if (*args[3]) {
1362 /* read server state from local file */
1363 appctx->ctx.server_state.backend = proxy_be_by_name(args[3]);
1364
1365 if (appctx->ctx.server_state.backend == NULL) {
1366 appctx->ctx.cli.msg = "Can't find backend.\n";
1367 appctx->st0 = STAT_CLI_PRINT;
1368 return 1;
1369 }
1370 }
1371 appctx->st2 = STAT_ST_INIT;
1372 appctx->st0 = STAT_CLI_O_SERVERS_STATE; // stats_dump_servers_state_to_buffer
1373 return 1;
1374 }
Willy Tarreau12833bb2014-01-28 16:49:56 +01001375 else if (strcmp(args[1], "pools") == 0) {
1376 appctx->st2 = STAT_ST_INIT;
1377 appctx->st0 = STAT_CLI_O_POOLS; // stats_dump_pools_to_buffer
1378 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001379 else if (strcmp(args[1], "sess") == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001380 appctx->st2 = STAT_ST_INIT;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001381 if (strm_li(s)->bind_conf->level < ACCESS_LVL_OPER) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001382 appctx->ctx.cli.msg = stats_permission_denied_msg;
1383 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +02001384 return 1;
1385 }
Willy Tarreau76153662012-11-26 01:16:39 +01001386 if (*args[2] && strcmp(args[2], "all") == 0)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001387 appctx->ctx.sess.target = (void *)-1;
Willy Tarreau76153662012-11-26 01:16:39 +01001388 else if (*args[2])
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001389 appctx->ctx.sess.target = (void *)strtoul(args[2], NULL, 0);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01001390 else
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001391 appctx->ctx.sess.target = NULL;
Willy Tarreau87b09662015-04-03 00:22:06 +02001392 appctx->ctx.sess.section = 0; /* start with stream status */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001393 appctx->ctx.sess.pos = 0;
1394 appctx->st0 = STAT_CLI_O_SESS; // stats_dump_sess_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001395 }
1396 else if (strcmp(args[1], "errors") == 0) {
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001397 if (strm_li(s)->bind_conf->level < ACCESS_LVL_OPER) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001398 appctx->ctx.cli.msg = stats_permission_denied_msg;
1399 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +02001400 return 1;
1401 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001402 if (*args[2])
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001403 appctx->ctx.errors.iid = atoi(args[2]);
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001404 else
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001405 appctx->ctx.errors.iid = -1;
1406 appctx->ctx.errors.px = NULL;
1407 appctx->st2 = STAT_ST_INIT;
1408 appctx->st0 = STAT_CLI_O_ERR; // stats_dump_errors_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001409 }
Willy Tarreau69f58c82010-07-12 17:55:33 +02001410 else if (strcmp(args[1], "table") == 0) {
Willy Tarreaudec98142012-06-06 23:37:08 +02001411 stats_sock_table_request(si, args, STAT_CLI_O_TAB);
Willy Tarreau69f58c82010-07-12 17:55:33 +02001412 }
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02001413 else if (strcmp(args[1], "tls-keys") == 0) {
1414#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
1415 appctx->st2 = STAT_ST_INIT;
1416 appctx->st0 = STAT_CLI_O_TLSK;
1417#else
1418 appctx->ctx.cli.msg = "HAProxy was compiled against a version of OpenSSL "
1419 "that doesn't support specifying TLS ticket keys\n";
1420 appctx->st0 = STAT_CLI_PRINT;
1421#endif
1422 return 1;
1423 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001424 else if (strcmp(args[1], "map") == 0 ||
1425 strcmp(args[1], "acl") == 0) {
1426
1427 /* Set ACL or MAP flags. */
1428 if (args[1][0] == 'm')
1429 appctx->ctx.map.display_flags = PAT_REF_MAP;
1430 else
1431 appctx->ctx.map.display_flags = PAT_REF_ACL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001432
1433 /* no parameter: display all map avalaible */
1434 if (!*args[2]) {
1435 appctx->st2 = STAT_ST_INIT;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001436 appctx->st0 = STAT_CLI_O_PATS;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001437 return 1;
1438 }
1439
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001440 /* lookup into the refs and check the map flag */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001441 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001442 if (!appctx->ctx.map.ref ||
1443 !(appctx->ctx.map.ref->flags & appctx->ctx.map.display_flags)) {
1444 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001445 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001446 else
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001447 appctx->ctx.cli.msg = "Unknown ACL identifier. Please use #<id> or <file>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001448 appctx->st0 = STAT_CLI_PRINT;
1449 return 1;
1450 }
1451 appctx->st2 = STAT_ST_INIT;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001452 appctx->st0 = STAT_CLI_O_PAT;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001453 }
Aman Guptaceafb4a2012-04-02 18:57:54 -07001454 else { /* neither "stat" nor "info" nor "sess" nor "errors" nor "table" */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001455 return 0;
1456 }
1457 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001458 else if (strcmp(args[0], "clear") == 0) {
1459 if (strcmp(args[1], "counters") == 0) {
1460 struct proxy *px;
1461 struct server *sv;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001462 struct listener *li;
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001463 int clrall = 0;
1464
1465 if (strcmp(args[2], "all") == 0)
1466 clrall = 1;
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001467
Willy Tarreau6162db22009-10-10 17:13:00 +02001468 /* check permissions */
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001469 if (strm_li(s)->bind_conf->level < ACCESS_LVL_OPER ||
1470 (clrall && strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001471 appctx->ctx.cli.msg = stats_permission_denied_msg;
1472 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +02001473 return 1;
1474 }
1475
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001476 for (px = proxy; px; px = px->next) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001477 if (clrall) {
1478 memset(&px->be_counters, 0, sizeof(px->be_counters));
1479 memset(&px->fe_counters, 0, sizeof(px->fe_counters));
1480 }
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001481 else {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001482 px->be_counters.conn_max = 0;
1483 px->be_counters.p.http.rps_max = 0;
1484 px->be_counters.sps_max = 0;
1485 px->be_counters.cps_max = 0;
1486 px->be_counters.nbpend_max = 0;
1487
1488 px->fe_counters.conn_max = 0;
1489 px->fe_counters.p.http.rps_max = 0;
1490 px->fe_counters.sps_max = 0;
1491 px->fe_counters.cps_max = 0;
1492 px->fe_counters.nbpend_max = 0;
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001493 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001494
1495 for (sv = px->srv; sv; sv = sv->next)
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001496 if (clrall)
1497 memset(&sv->counters, 0, sizeof(sv->counters));
1498 else {
1499 sv->counters.cur_sess_max = 0;
1500 sv->counters.nbpend_max = 0;
1501 sv->counters.sps_max = 0;
1502 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001503
Willy Tarreaua7944ad2012-09-26 21:03:11 +02001504 list_for_each_entry(li, &px->conf.listeners, by_fe)
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001505 if (li->counters) {
1506 if (clrall)
1507 memset(li->counters, 0, sizeof(*li->counters));
1508 else
1509 li->counters->conn_max = 0;
1510 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001511 }
1512
Willy Tarreau81c25d02011-09-07 15:17:21 +02001513 global.cps_max = 0;
Willy Tarreau93e7c002013-10-07 18:51:07 +02001514 global.sps_max = 0;
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001515 return 1;
1516 }
Willy Tarreau88ee3972010-07-13 13:48:00 +02001517 else if (strcmp(args[1], "table") == 0) {
Willy Tarreaudec98142012-06-06 23:37:08 +02001518 stats_sock_table_request(si, args, STAT_CLI_O_CLR);
Willy Tarreau88ee3972010-07-13 13:48:00 +02001519 /* end of processing */
1520 return 1;
1521 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001522 else if (strcmp(args[1], "map") == 0 || strcmp(args[1], "acl") == 0) {
1523 /* Set ACL or MAP flags. */
1524 if (args[1][0] == 'm')
1525 appctx->ctx.map.display_flags = PAT_REF_MAP;
1526 else
1527 appctx->ctx.map.display_flags = PAT_REF_ACL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001528
1529 /* no parameter */
1530 if (!*args[2]) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001531 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
1532 appctx->ctx.cli.msg = "Missing map identifier.\n";
1533 else
1534 appctx->ctx.cli.msg = "Missing ACL identifier.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001535 appctx->st0 = STAT_CLI_PRINT;
1536 return 1;
1537 }
1538
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001539 /* lookup into the refs and check the map flag */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001540 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001541 if (!appctx->ctx.map.ref ||
1542 !(appctx->ctx.map.ref->flags & appctx->ctx.map.display_flags)) {
1543 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001544 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001545 else
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001546 appctx->ctx.cli.msg = "Unknown ACL identifier. Please use #<id> or <file>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001547 appctx->st0 = STAT_CLI_PRINT;
1548 return 1;
1549 }
1550
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001551 /* Clear all. */
1552 pat_ref_prune(appctx->ctx.map.ref);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001553
1554 /* return response */
Thierry FOURNIER07e78c52014-12-18 15:28:01 +01001555 appctx->st0 = STAT_CLI_PROMPT;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001556 return 1;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001557 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001558 else {
Willy Tarreau88ee3972010-07-13 13:48:00 +02001559 /* unknown "clear" argument */
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001560 return 0;
1561 }
1562 }
Willy Tarreau38338fa2009-10-10 18:37:29 +02001563 else if (strcmp(args[0], "get") == 0) {
1564 if (strcmp(args[1], "weight") == 0) {
1565 struct proxy *px;
1566 struct server *sv;
1567
1568 /* split "backend/server" and make <line> point to server */
1569 for (line = args[2]; *line; line++)
1570 if (*line == '/') {
1571 *line++ = '\0';
1572 break;
1573 }
1574
1575 if (!*line) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001576 appctx->ctx.cli.msg = "Require 'backend/server'.\n";
1577 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau38338fa2009-10-10 18:37:29 +02001578 return 1;
1579 }
1580
1581 if (!get_backend_server(args[2], line, &px, &sv)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001582 appctx->ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
1583 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau38338fa2009-10-10 18:37:29 +02001584 return 1;
1585 }
1586
1587 /* return server's effective weight at the moment */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001588 snprintf(trash.str, trash.size, "%d (initial %d)\n", sv->uweight, sv->iweight);
Willy Tarreaubc18da12015-03-13 14:00:47 +01001589 if (bi_putstr(si_ic(si), trash.str) == -1)
Willy Tarreaufe127932015-04-21 19:23:39 +02001590 si_applet_cant_put(si);
Willy Tarreaubc18da12015-03-13 14:00:47 +01001591
Willy Tarreau38338fa2009-10-10 18:37:29 +02001592 return 1;
1593 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001594 else if (strcmp(args[1], "map") == 0 || strcmp(args[1], "acl") == 0) {
1595 /* Set flags. */
1596 if (args[1][0] == 'm')
1597 appctx->ctx.map.display_flags = PAT_REF_MAP;
1598 else
1599 appctx->ctx.map.display_flags = PAT_REF_ACL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001600
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001601 /* No parameter. */
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001602 if (!*args[2] || !*args[3]) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001603 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
1604 appctx->ctx.cli.msg = "Missing map identifier and/or key.\n";
1605 else
1606 appctx->ctx.cli.msg = "Missing ACL identifier and/or key.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001607 appctx->st0 = STAT_CLI_PRINT;
1608 return 1;
1609 }
1610
1611 /* lookup into the maps */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001612 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001613 if (!appctx->ctx.map.ref) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001614 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001615 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001616 else
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001617 appctx->ctx.cli.msg = "Unknown ACL identifier. Please use #<id> or <file>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001618 appctx->st0 = STAT_CLI_PRINT;
1619 return 1;
1620 }
1621
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001622 /* copy input string. The string must be allocated because
1623 * it may be used over multiple iterations. It's released
1624 * at the end and upon abort anyway.
1625 */
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001626 appctx->ctx.map.chunk.len = strlen(args[3]);
1627 appctx->ctx.map.chunk.size = appctx->ctx.map.chunk.len + 1;
1628 appctx->ctx.map.chunk.str = strdup(args[3]);
1629 if (!appctx->ctx.map.chunk.str) {
1630 appctx->ctx.cli.msg = "Out of memory error.\n";
1631 appctx->st0 = STAT_CLI_PRINT;
1632 return 1;
1633 }
1634
1635 /* prepare response */
1636 appctx->st2 = STAT_ST_INIT;
1637 appctx->st0 = STAT_CLI_O_MLOOK;
1638 }
Willy Tarreau38338fa2009-10-10 18:37:29 +02001639 else { /* not "get weight" */
1640 return 0;
1641 }
1642 }
Willy Tarreau4483d432009-10-10 19:30:08 +02001643 else if (strcmp(args[0], "set") == 0) {
1644 if (strcmp(args[1], "weight") == 0) {
Willy Tarreau4483d432009-10-10 19:30:08 +02001645 struct server *sv;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001646 const char *warning;
Willy Tarreau4483d432009-10-10 19:30:08 +02001647
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001648 sv = expect_server_admin(s, si, args[2]);
1649 if (!sv)
Willy Tarreau4483d432009-10-10 19:30:08 +02001650 return 1;
Willy Tarreau4483d432009-10-10 19:30:08 +02001651
Simon Horman7d09b9a2013-02-12 10:45:51 +09001652 warning = server_parse_weight_change_request(sv, args[3]);
1653 if (warning) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001654 appctx->ctx.cli.msg = warning;
1655 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +02001656 }
Willy Tarreau4483d432009-10-10 19:30:08 +02001657 return 1;
1658 }
Willy Tarreau2a4b70f2014-05-22 18:42:35 +02001659 else if (strcmp(args[1], "server") == 0) {
1660 struct server *sv;
1661 const char *warning;
1662
1663 sv = expect_server_admin(s, si, args[2]);
1664 if (!sv)
1665 return 1;
1666
1667 if (strcmp(args[3], "weight") == 0) {
1668 warning = server_parse_weight_change_request(sv, args[4]);
1669 if (warning) {
1670 appctx->ctx.cli.msg = warning;
1671 appctx->st0 = STAT_CLI_PRINT;
1672 }
1673 }
1674 else if (strcmp(args[3], "state") == 0) {
1675 if (strcmp(args[4], "ready") == 0)
1676 srv_adm_set_ready(sv);
1677 else if (strcmp(args[4], "drain") == 0)
1678 srv_adm_set_drain(sv);
1679 else if (strcmp(args[4], "maint") == 0)
1680 srv_adm_set_maint(sv);
1681 else {
1682 appctx->ctx.cli.msg = "'set server <srv> state' expects 'ready', 'drain' and 'maint'.\n";
1683 appctx->st0 = STAT_CLI_PRINT;
1684 }
1685 }
1686 else if (strcmp(args[3], "health") == 0) {
1687 if (sv->track) {
1688 appctx->ctx.cli.msg = "cannot change health on a tracking server.\n";
1689 appctx->st0 = STAT_CLI_PRINT;
1690 }
1691 else if (strcmp(args[4], "up") == 0) {
1692 sv->check.health = sv->check.rise + sv->check.fall - 1;
1693 srv_set_running(sv, "changed from CLI");
1694 }
1695 else if (strcmp(args[4], "stopping") == 0) {
1696 sv->check.health = sv->check.rise + sv->check.fall - 1;
1697 srv_set_stopping(sv, "changed from CLI");
1698 }
1699 else if (strcmp(args[4], "down") == 0) {
1700 sv->check.health = 0;
1701 srv_set_stopped(sv, "changed from CLI");
1702 }
1703 else {
1704 appctx->ctx.cli.msg = "'set server <srv> health' expects 'up', 'stopping', or 'down'.\n";
1705 appctx->st0 = STAT_CLI_PRINT;
1706 }
1707 }
1708 else if (strcmp(args[3], "agent") == 0) {
1709 if (!(sv->agent.state & CHK_ST_ENABLED)) {
1710 appctx->ctx.cli.msg = "agent checks are not enabled on this server.\n";
1711 appctx->st0 = STAT_CLI_PRINT;
1712 }
1713 else if (strcmp(args[4], "up") == 0) {
1714 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
1715 srv_set_running(sv, "changed from CLI");
1716 }
1717 else if (strcmp(args[4], "down") == 0) {
1718 sv->agent.health = 0;
1719 srv_set_stopped(sv, "changed from CLI");
1720 }
1721 else {
1722 appctx->ctx.cli.msg = "'set server <srv> agent' expects 'up' or 'down'.\n";
1723 appctx->st0 = STAT_CLI_PRINT;
1724 }
1725 }
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001726 else if (strcmp(args[3], "addr") == 0) {
Thierry Fournier09a91782016-02-24 08:25:39 +01001727 warning = server_parse_addr_change_request(sv, args[4], "stats command");
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001728 if (warning) {
1729 appctx->ctx.cli.msg = warning;
1730 appctx->st0 = STAT_CLI_PRINT;
1731 }
1732 }
Willy Tarreau2a4b70f2014-05-22 18:42:35 +02001733 else {
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001734 appctx->ctx.cli.msg = "'set server <srv>' only supports 'agent', 'health', 'state', 'weight' and 'addr'.\n";
Willy Tarreau2a4b70f2014-05-22 18:42:35 +02001735 appctx->st0 = STAT_CLI_PRINT;
1736 }
1737 return 1;
1738 }
Willy Tarreau7aabd112010-01-26 10:59:06 +01001739 else if (strcmp(args[1], "timeout") == 0) {
1740 if (strcmp(args[2], "cli") == 0) {
1741 unsigned timeout;
1742 const char *res;
1743
1744 if (!*args[3]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001745 appctx->ctx.cli.msg = "Expects an integer value.\n";
1746 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001747 return 1;
1748 }
1749
1750 res = parse_time_err(args[3], &timeout, TIME_UNIT_S);
1751 if (res || timeout < 1) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001752 appctx->ctx.cli.msg = "Invalid timeout value.\n";
1753 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001754 return 1;
1755 }
1756
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001757 s->req.rto = s->res.wto = 1 + MS_TO_TICKS(timeout*1000);
Willy Tarreau7aabd112010-01-26 10:59:06 +01001758 return 1;
1759 }
1760 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001761 appctx->ctx.cli.msg = "'set timeout' only supports 'cli'.\n";
1762 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001763 return 1;
1764 }
1765 }
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001766 else if (strcmp(args[1], "maxconn") == 0) {
1767 if (strcmp(args[2], "frontend") == 0) {
1768 struct proxy *px;
1769 struct listener *l;
1770 int v;
1771
Willy Tarreau532a4502011-09-07 22:37:44 +02001772 px = expect_frontend_admin(s, si, args[3]);
1773 if (!px)
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001774 return 1;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001775
1776 if (!*args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001777 appctx->ctx.cli.msg = "Integer value expected.\n";
1778 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001779 return 1;
1780 }
1781
1782 v = atoi(args[4]);
Willy Tarreau3c7a79d2012-09-26 21:07:15 +02001783 if (v < 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001784 appctx->ctx.cli.msg = "Value out of range.\n";
1785 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001786 return 1;
1787 }
1788
1789 /* OK, the value is fine, so we assign it to the proxy and to all of
1790 * its listeners. The blocked ones will be dequeued.
1791 */
1792 px->maxconn = v;
Willy Tarreaua7944ad2012-09-26 21:03:11 +02001793 list_for_each_entry(l, &px->conf.listeners, by_fe) {
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001794 l->maxconn = v;
1795 if (l->state == LI_FULL)
1796 resume_listener(l);
1797 }
1798
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001799 if (px->maxconn > px->feconn && !LIST_ISEMPTY(&strm_fe(s)->listener_queue))
1800 dequeue_all_listeners(&strm_fe(s)->listener_queue);
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001801
1802 return 1;
1803 }
Andrew Hayworthedb93a72015-10-27 21:46:25 +00001804 else if (strcmp(args[2], "server") == 0) {
1805 struct server *sv;
1806 int v;
1807
1808 sv = expect_server_admin(s, si, args[3]);
1809 if (!sv)
1810 return 1;
1811
1812 if (!*args[4]) {
1813 appctx->ctx.cli.msg = "Integer value expected.\n";
1814 appctx->st0 = STAT_CLI_PRINT;
1815 return 1;
1816 }
1817
1818 v = atoi(args[4]);
1819 if (v < 0) {
1820 appctx->ctx.cli.msg = "Value out of range.\n";
1821 appctx->st0 = STAT_CLI_PRINT;
1822 return 1;
1823 }
1824
1825 if (sv->maxconn == sv->minconn) { // static maxconn
1826 sv->maxconn = sv->minconn = v;
1827 } else { // dynamic maxconn
1828 sv->maxconn = v;
1829 }
1830
1831 if (may_dequeue_tasks(sv, sv->proxy))
1832 process_srv_queue(sv);
1833
1834 return 1;
1835 }
Willy Tarreau91886b62011-09-07 14:38:31 +02001836 else if (strcmp(args[2], "global") == 0) {
1837 int v;
1838
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001839 if (strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001840 appctx->ctx.cli.msg = stats_permission_denied_msg;
1841 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau91886b62011-09-07 14:38:31 +02001842 return 1;
1843 }
1844
1845 if (!*args[3]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001846 appctx->ctx.cli.msg = "Expects an integer value.\n";
1847 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau91886b62011-09-07 14:38:31 +02001848 return 1;
1849 }
1850
1851 v = atoi(args[3]);
1852 if (v > global.hardmaxconn) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001853 appctx->ctx.cli.msg = "Value out of range.\n";
1854 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau91886b62011-09-07 14:38:31 +02001855 return 1;
1856 }
1857
1858 /* check for unlimited values */
1859 if (v <= 0)
1860 v = global.hardmaxconn;
1861
1862 global.maxconn = v;
1863
1864 /* Dequeues all of the listeners waiting for a resource */
1865 if (!LIST_ISEMPTY(&global_listener_queue))
1866 dequeue_all_listeners(&global_listener_queue);
1867
1868 return 1;
1869 }
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001870 else {
Andrew Hayworthedb93a72015-10-27 21:46:25 +00001871 appctx->ctx.cli.msg = "'set maxconn' only supports 'frontend', 'server', and 'global'.\n";
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001872 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001873 return 1;
1874 }
1875 }
Willy Tarreauf5b22872011-09-07 16:13:44 +02001876 else if (strcmp(args[1], "rate-limit") == 0) {
1877 if (strcmp(args[2], "connections") == 0) {
1878 if (strcmp(args[3], "global") == 0) {
1879 int v;
1880
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001881 if (strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001882 appctx->ctx.cli.msg = stats_permission_denied_msg;
1883 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001884 return 1;
1885 }
1886
1887 if (!*args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001888 appctx->ctx.cli.msg = "Expects an integer value.\n";
1889 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001890 return 1;
1891 }
1892
1893 v = atoi(args[4]);
1894 if (v < 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001895 appctx->ctx.cli.msg = "Value out of range.\n";
1896 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001897 return 1;
1898 }
1899
1900 global.cps_lim = v;
1901
1902 /* Dequeues all of the listeners waiting for a resource */
1903 if (!LIST_ISEMPTY(&global_listener_queue))
1904 dequeue_all_listeners(&global_listener_queue);
1905
1906 return 1;
1907 }
1908 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001909 appctx->ctx.cli.msg = "'set rate-limit connections' only supports 'global'.\n";
1910 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001911 return 1;
1912 }
1913 }
Willy Tarreau93e7c002013-10-07 18:51:07 +02001914 else if (strcmp(args[2], "sessions") == 0) {
1915 if (strcmp(args[3], "global") == 0) {
1916 int v;
1917
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001918 if (strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau93e7c002013-10-07 18:51:07 +02001919 appctx->ctx.cli.msg = stats_permission_denied_msg;
1920 appctx->st0 = STAT_CLI_PRINT;
1921 return 1;
1922 }
1923
1924 if (!*args[4]) {
1925 appctx->ctx.cli.msg = "Expects an integer value.\n";
1926 appctx->st0 = STAT_CLI_PRINT;
1927 return 1;
1928 }
1929
1930 v = atoi(args[4]);
1931 if (v < 0) {
1932 appctx->ctx.cli.msg = "Value out of range.\n";
1933 appctx->st0 = STAT_CLI_PRINT;
1934 return 1;
1935 }
1936
1937 global.sps_lim = v;
1938
1939 /* Dequeues all of the listeners waiting for a resource */
1940 if (!LIST_ISEMPTY(&global_listener_queue))
1941 dequeue_all_listeners(&global_listener_queue);
1942
1943 return 1;
1944 }
1945 else {
1946 appctx->ctx.cli.msg = "'set rate-limit sessions' only supports 'global'.\n";
1947 appctx->st0 = STAT_CLI_PRINT;
1948 return 1;
1949 }
1950 }
Willy Tarreaue43d5322013-10-07 20:01:52 +02001951#ifdef USE_OPENSSL
1952 else if (strcmp(args[2], "ssl-sessions") == 0) {
1953 if (strcmp(args[3], "global") == 0) {
1954 int v;
1955
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001956 if (strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreaue43d5322013-10-07 20:01:52 +02001957 appctx->ctx.cli.msg = stats_permission_denied_msg;
1958 appctx->st0 = STAT_CLI_PRINT;
1959 return 1;
1960 }
1961
1962 if (!*args[4]) {
1963 appctx->ctx.cli.msg = "Expects an integer value.\n";
1964 appctx->st0 = STAT_CLI_PRINT;
1965 return 1;
1966 }
1967
1968 v = atoi(args[4]);
1969 if (v < 0) {
1970 appctx->ctx.cli.msg = "Value out of range.\n";
1971 appctx->st0 = STAT_CLI_PRINT;
1972 return 1;
1973 }
1974
1975 global.ssl_lim = v;
1976
1977 /* Dequeues all of the listeners waiting for a resource */
1978 if (!LIST_ISEMPTY(&global_listener_queue))
1979 dequeue_all_listeners(&global_listener_queue);
1980
1981 return 1;
1982 }
1983 else {
1984 appctx->ctx.cli.msg = "'set rate-limit ssl-sessions' only supports 'global'.\n";
1985 appctx->st0 = STAT_CLI_PRINT;
1986 return 1;
1987 }
1988 }
1989#endif
William Lallemandd85f9172012-11-09 17:05:39 +01001990 else if (strcmp(args[2], "http-compression") == 0) {
1991 if (strcmp(args[3], "global") == 0) {
1992 int v;
1993
Willy Tarreaua1c2b2c2015-11-26 18:32:39 +01001994 if (strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN) {
1995 appctx->ctx.cli.msg = stats_permission_denied_msg;
1996 appctx->st0 = STAT_CLI_PRINT;
1997 return 1;
1998 }
1999
Willy Tarreau85d47f92012-11-21 00:29:50 +01002000 if (!*args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002001 appctx->ctx.cli.msg = "Expects a maximum input byte rate in kB/s.\n";
2002 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau85d47f92012-11-21 00:29:50 +01002003 return 1;
2004 }
2005
William Lallemandd85f9172012-11-09 17:05:39 +01002006 v = atoi(args[4]);
2007 global.comp_rate_lim = v * 1024; /* Kilo to bytes. */
2008 }
2009 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002010 appctx->ctx.cli.msg = "'set rate-limit http-compression' only supports 'global'.\n";
2011 appctx->st0 = STAT_CLI_PRINT;
William Lallemandd85f9172012-11-09 17:05:39 +01002012 return 1;
2013 }
2014 }
Willy Tarreauf5b22872011-09-07 16:13:44 +02002015 else {
Willy Tarreaue43d5322013-10-07 20:01:52 +02002016 appctx->ctx.cli.msg = "'set rate-limit' supports 'connections', 'sessions', 'ssl-sessions', and 'http-compression'.\n";
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002017 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02002018 return 1;
2019 }
2020 }
Willy Tarreau654694e2012-06-07 01:03:16 +02002021 else if (strcmp(args[1], "table") == 0) {
2022 stats_sock_table_request(si, args, STAT_CLI_O_SET);
2023 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002024 else if (strcmp(args[1], "map") == 0) {
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002025 char *err;
2026
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002027 /* Set flags. */
2028 appctx->ctx.map.display_flags = PAT_REF_MAP;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002029
2030 /* Expect three parameters: map name, key and new value. */
2031 if (!*args[2] || !*args[3] || !*args[4]) {
Thierry FOURNIERd5723432014-03-11 13:52:44 +01002032 appctx->ctx.cli.msg = "'set map' expects three parameters: map identifier, key and value.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002033 appctx->st0 = STAT_CLI_PRINT;
2034 return 1;
2035 }
2036
2037 /* Lookup the reference in the maps. */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002038 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002039 if (!appctx->ctx.map.ref) {
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01002040 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002041 appctx->st0 = STAT_CLI_PRINT;
2042 return 1;
2043 }
2044
Thierry FOURNIER9356c682014-01-28 15:55:37 +01002045 /* If the entry identifier start with a '#', it is considered as
2046 * pointer id
2047 */
2048 if (args[3][0] == '#' && args[3][1] == '0' && args[3][2] == 'x') {
2049 struct pat_ref_elt *ref;
2050 long long int conv;
2051 char *error;
2052
2053 /* Convert argument to integer value. */
2054 conv = strtoll(&args[3][1], &error, 16);
2055 if (*error != '\0') {
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01002056 appctx->ctx.cli.msg = "Malformed identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER9356c682014-01-28 15:55:37 +01002057 appctx->st0 = STAT_CLI_PRINT;
2058 return 1;
2059 }
2060
2061 /* Convert and check integer to pointer. */
2062 ref = (struct pat_ref_elt *)(long)conv;
2063 if ((long long int)(long)ref != conv) {
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01002064 appctx->ctx.cli.msg = "Malformed identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER9356c682014-01-28 15:55:37 +01002065 appctx->st0 = STAT_CLI_PRINT;
2066 return 1;
2067 }
2068
2069 /* Try to delete the entry. */
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002070 err = NULL;
2071 if (!pat_ref_set_by_id(appctx->ctx.map.ref, ref, args[4], &err)) {
2072 if (err)
2073 memprintf(&err, "%s.\n", err);
2074 appctx->ctx.cli.err = err;
2075 appctx->st0 = STAT_CLI_PRINT_FREE;
Thierry FOURNIER9356c682014-01-28 15:55:37 +01002076 return 1;
2077 }
2078 }
2079 else {
2080 /* Else, use the entry identifier as pattern
2081 * string, and update the value.
2082 */
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002083 err = NULL;
2084 if (!pat_ref_set(appctx->ctx.map.ref, args[3], args[4], &err)) {
2085 if (err)
2086 memprintf(&err, "%s.\n", err);
2087 appctx->ctx.cli.err = err;
2088 appctx->st0 = STAT_CLI_PRINT_FREE;
Thierry FOURNIER9356c682014-01-28 15:55:37 +01002089 return 1;
2090 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002091 }
2092
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002093 /* The set is done, send message. */
Thierry FOURNIER07e78c52014-12-18 15:28:01 +01002094 appctx->st0 = STAT_CLI_PROMPT;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002095 return 1;
2096 }
Emeric Brun4147b2e2014-06-16 18:36:30 +02002097#ifdef USE_OPENSSL
2098 else if (strcmp(args[1], "ssl") == 0) {
2099 if (strcmp(args[2], "ocsp-response") == 0) {
Lukas Tribuse4e30f72014-12-09 16:32:51 +01002100#if (defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP)
Emeric Brun4147b2e2014-06-16 18:36:30 +02002101 char *err = NULL;
2102
Emeric Brunaf4ef742014-06-19 14:10:45 +02002103 /* Expect one parameter: the new response in base64 encoding */
Emeric Brun4147b2e2014-06-16 18:36:30 +02002104 if (!*args[3]) {
2105 appctx->ctx.cli.msg = "'set ssl ocsp-response' expects response in base64 encoding.\n";
2106 appctx->st0 = STAT_CLI_PRINT;
2107 return 1;
2108 }
2109
2110 trash.len = base64dec(args[3], strlen(args[3]), trash.str, trash.size);
2111 if (trash.len < 0) {
2112 appctx->ctx.cli.msg = "'set ssl ocsp-response' received invalid base64 encoded response.\n";
2113 appctx->st0 = STAT_CLI_PRINT;
2114 return 1;
2115 }
2116
2117 if (ssl_sock_update_ocsp_response(&trash, &err)) {
2118 if (err) {
2119 memprintf(&err, "%s.\n", err);
2120 appctx->ctx.cli.err = err;
2121 appctx->st0 = STAT_CLI_PRINT_FREE;
2122 }
2123 return 1;
2124 }
2125 appctx->ctx.cli.msg = "OCSP Response updated!";
2126 appctx->st0 = STAT_CLI_PRINT;
2127 return 1;
2128#else
2129 appctx->ctx.cli.msg = "HAProxy was compiled against a version of OpenSSL that doesn't support OCSP stapling.\n";
2130 appctx->st0 = STAT_CLI_PRINT;
2131 return 1;
2132#endif
2133 }
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02002134 else if (strcmp(args[2], "tls-key") == 0) {
2135#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
2136 /* Expect two parameters: the filename and the new new TLS key in encoding */
2137 if (!*args[3] || !*args[4]) {
2138 appctx->ctx.cli.msg = "'set ssl tls-key' expects a filename and the new TLS key in base64 encoding.\n";
2139 appctx->st0 = STAT_CLI_PRINT;
2140 return 1;
2141 }
2142
2143 appctx->ctx.tlskeys.ref = tlskeys_ref_lookup_ref(args[3]);
2144 if(!appctx->ctx.tlskeys.ref) {
2145 appctx->ctx.cli.msg = "'set ssl tls-key' unable to locate referenced filename\n";
2146 appctx->st0 = STAT_CLI_PRINT;
2147 return 1;
2148 }
2149
2150 trash.len = base64dec(args[4], strlen(args[4]), trash.str, trash.size);
2151 if (trash.len != sizeof(struct tls_sess_key)) {
2152 appctx->ctx.cli.msg = "'set ssl tls-key' received invalid base64 encoded TLS key.\n";
2153 appctx->st0 = STAT_CLI_PRINT;
2154 return 1;
2155 }
2156
Pradeep Jindalcc79b002015-08-20 18:25:17 +05302157 memcpy(appctx->ctx.tlskeys.ref->tlskeys + ((appctx->ctx.tlskeys.ref->tls_ticket_enc_index + 2) % TLS_TICKETS_NO), trash.str, trash.len);
2158 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 +02002159
2160 appctx->ctx.cli.msg = "TLS ticket key updated!";
2161 appctx->st0 = STAT_CLI_PRINT;
2162 return 1;
2163#else
2164 appctx->ctx.cli.msg = "HAProxy was compiled against a version of OpenSSL "
2165 "that doesn't support specifying TLS ticket keys\n";
2166 appctx->st0 = STAT_CLI_PRINT;
2167 return 1;
2168#endif
2169 }
Emeric Brun4147b2e2014-06-16 18:36:30 +02002170 else {
2171 appctx->ctx.cli.msg = "'set ssl' only supports 'ocsp-response'.\n";
2172 appctx->st0 = STAT_CLI_PRINT;
2173 return 1;
2174 }
2175 }
2176#endif
Willy Tarreau7aabd112010-01-26 10:59:06 +01002177 else { /* unknown "set" parameter */
Willy Tarreau4483d432009-10-10 19:30:08 +02002178 return 0;
2179 }
2180 }
Cyril Bontécd19e512010-01-31 22:34:03 +01002181 else if (strcmp(args[0], "enable") == 0) {
Simon Horman671b6f02013-11-25 10:46:39 +09002182 if (strcmp(args[1], "agent") == 0) {
2183 struct server *sv;
2184
2185 sv = expect_server_admin(s, si, args[2]);
2186 if (!sv)
2187 return 1;
2188
Willy Tarreau2e10f5a2013-12-11 20:11:55 +01002189 if (!(sv->agent.state & CHK_ST_CONFIGURED)) {
2190 appctx->ctx.cli.msg = "Agent was not configured on this server, cannot enable.\n";
2191 appctx->st0 = STAT_CLI_PRINT;
2192 return 1;
2193 }
2194
2195 sv->agent.state |= CHK_ST_ENABLED;
Simon Horman671b6f02013-11-25 10:46:39 +09002196 return 1;
2197 }
Willy Tarreau9b5aecd2014-05-23 11:53:10 +02002198 else if (strcmp(args[1], "health") == 0) {
Cyril Bontécd19e512010-01-31 22:34:03 +01002199 struct server *sv;
2200
Willy Tarreaud52c41e2011-09-07 23:41:01 +02002201 sv = expect_server_admin(s, si, args[2]);
2202 if (!sv)
Cyril Bonté613f0df2011-03-03 20:49:04 +01002203 return 1;
Cyril Bonté613f0df2011-03-03 20:49:04 +01002204
Willy Tarreau9b5aecd2014-05-23 11:53:10 +02002205 if (!(sv->check.state & CHK_ST_CONFIGURED)) {
2206 appctx->ctx.cli.msg = "Health checks are not configured on this server, cannot enable.\n";
2207 appctx->st0 = STAT_CLI_PRINT;
2208 return 1;
2209 }
2210
2211 sv->check.state |= CHK_ST_ENABLED;
2212 return 1;
2213 }
2214 else if (strcmp(args[1], "server") == 0) {
2215 struct server *sv;
2216
2217 sv = expect_server_admin(s, si, args[2]);
2218 if (!sv)
2219 return 1;
2220
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02002221 srv_adm_set_ready(sv);
Willy Tarreau532a4502011-09-07 22:37:44 +02002222 return 1;
2223 }
2224 else if (strcmp(args[1], "frontend") == 0) {
2225 struct proxy *px;
2226
2227 px = expect_frontend_admin(s, si, args[2]);
2228 if (!px)
2229 return 1;
2230
2231 if (px->state == PR_STSTOPPED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002232 appctx->ctx.cli.msg = "Frontend was previously shut down, cannot enable.\n";
2233 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002234 return 1;
2235 }
2236
2237 if (px->state != PR_STPAUSED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002238 appctx->ctx.cli.msg = "Frontend is already enabled.\n";
2239 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002240 return 1;
2241 }
2242
2243 if (!resume_proxy(px)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002244 appctx->ctx.cli.msg = "Failed to resume frontend, check logs for precise cause (port conflict?).\n";
2245 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002246 return 1;
2247 }
Cyril Bontécd19e512010-01-31 22:34:03 +01002248 return 1;
2249 }
2250 else { /* unknown "enable" parameter */
Willy Tarreau9b5aecd2014-05-23 11:53:10 +02002251 appctx->ctx.cli.msg = "'enable' only supports 'agent', 'frontend', 'health', and 'server'.\n";
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002252 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002253 return 1;
Cyril Bontécd19e512010-01-31 22:34:03 +01002254 }
2255 }
2256 else if (strcmp(args[0], "disable") == 0) {
Simon Horman671b6f02013-11-25 10:46:39 +09002257 if (strcmp(args[1], "agent") == 0) {
2258 struct server *sv;
2259
2260 sv = expect_server_admin(s, si, args[2]);
2261 if (!sv)
2262 return 1;
2263
Willy Tarreau2e10f5a2013-12-11 20:11:55 +01002264 sv->agent.state &= ~CHK_ST_ENABLED;
Simon Horman671b6f02013-11-25 10:46:39 +09002265 return 1;
2266 }
Willy Tarreau9b5aecd2014-05-23 11:53:10 +02002267 else if (strcmp(args[1], "health") == 0) {
2268 struct server *sv;
2269
2270 sv = expect_server_admin(s, si, args[2]);
2271 if (!sv)
2272 return 1;
2273
2274 sv->check.state &= ~CHK_ST_ENABLED;
2275 return 1;
2276 }
Simon Horman671b6f02013-11-25 10:46:39 +09002277 else if (strcmp(args[1], "server") == 0) {
Cyril Bontécd19e512010-01-31 22:34:03 +01002278 struct server *sv;
2279
Willy Tarreaud52c41e2011-09-07 23:41:01 +02002280 sv = expect_server_admin(s, si, args[2]);
2281 if (!sv)
Cyril Bonté613f0df2011-03-03 20:49:04 +01002282 return 1;
Cyril Bonté613f0df2011-03-03 20:49:04 +01002283
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02002284 srv_adm_set_maint(sv);
Willy Tarreau532a4502011-09-07 22:37:44 +02002285 return 1;
2286 }
2287 else if (strcmp(args[1], "frontend") == 0) {
2288 struct proxy *px;
2289
2290 px = expect_frontend_admin(s, si, args[2]);
2291 if (!px)
2292 return 1;
2293
2294 if (px->state == PR_STSTOPPED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002295 appctx->ctx.cli.msg = "Frontend was previously shut down, cannot disable.\n";
2296 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002297 return 1;
2298 }
2299
2300 if (px->state == PR_STPAUSED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002301 appctx->ctx.cli.msg = "Frontend is already disabled.\n";
2302 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002303 return 1;
2304 }
2305
2306 if (!pause_proxy(px)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002307 appctx->ctx.cli.msg = "Failed to pause frontend, check logs for precise cause.\n";
2308 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002309 return 1;
2310 }
Cyril Bontécd19e512010-01-31 22:34:03 +01002311 return 1;
2312 }
2313 else { /* unknown "disable" parameter */
Willy Tarreau9b5aecd2014-05-23 11:53:10 +02002314 appctx->ctx.cli.msg = "'disable' only supports 'agent', 'frontend', 'health', and 'server'.\n";
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002315 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002316 return 1;
2317 }
2318 }
2319 else if (strcmp(args[0], "shutdown") == 0) {
2320 if (strcmp(args[1], "frontend") == 0) {
2321 struct proxy *px;
2322
2323 px = expect_frontend_admin(s, si, args[2]);
2324 if (!px)
2325 return 1;
2326
2327 if (px->state == PR_STSTOPPED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002328 appctx->ctx.cli.msg = "Frontend was already shut down.\n";
2329 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002330 return 1;
2331 }
2332
2333 Warning("Proxy %s stopped (FE: %lld conns, BE: %lld conns).\n",
2334 px->id, px->fe_counters.cum_conn, px->be_counters.cum_conn);
2335 send_log(px, LOG_WARNING, "Proxy %s stopped (FE: %lld conns, BE: %lld conns).\n",
2336 px->id, px->fe_counters.cum_conn, px->be_counters.cum_conn);
2337 stop_proxy(px);
2338 return 1;
2339 }
Willy Tarreaua295edc2011-09-07 23:21:03 +02002340 else if (strcmp(args[1], "session") == 0) {
Willy Tarreau87b09662015-04-03 00:22:06 +02002341 struct stream *sess, *ptr;
Willy Tarreaua295edc2011-09-07 23:21:03 +02002342
Willy Tarreaud0d8da92015-04-04 02:10:38 +02002343 if (strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002344 appctx->ctx.cli.msg = stats_permission_denied_msg;
2345 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaua295edc2011-09-07 23:21:03 +02002346 return 1;
2347 }
2348
2349 if (!*args[2]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002350 appctx->ctx.cli.msg = "Session pointer expected (use 'show sess').\n";
2351 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaua295edc2011-09-07 23:21:03 +02002352 return 1;
2353 }
2354
2355 ptr = (void *)strtoul(args[2], NULL, 0);
2356
Willy Tarreau87b09662015-04-03 00:22:06 +02002357 /* first, look for the requested stream in the stream table */
2358 list_for_each_entry(sess, &streams, list) {
Willy Tarreaua295edc2011-09-07 23:21:03 +02002359 if (sess == ptr)
2360 break;
2361 }
2362
Willy Tarreau87b09662015-04-03 00:22:06 +02002363 /* do we have the stream ? */
Willy Tarreaua295edc2011-09-07 23:21:03 +02002364 if (sess != ptr) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002365 appctx->ctx.cli.msg = "No such session (use 'show sess').\n";
2366 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaua295edc2011-09-07 23:21:03 +02002367 return 1;
2368 }
2369
Willy Tarreaue7dff022015-04-03 01:14:29 +02002370 stream_shutdown(sess, SF_ERR_KILLED);
Willy Tarreaua295edc2011-09-07 23:21:03 +02002371 return 1;
2372 }
Willy Tarreau52b2d222011-09-07 23:48:48 +02002373 else if (strcmp(args[1], "sessions") == 0) {
2374 if (strcmp(args[2], "server") == 0) {
2375 struct server *sv;
Willy Tarreau87b09662015-04-03 00:22:06 +02002376 struct stream *sess, *sess_bck;
Willy Tarreau52b2d222011-09-07 23:48:48 +02002377
2378 sv = expect_server_admin(s, si, args[3]);
2379 if (!sv)
2380 return 1;
2381
Willy Tarreau87b09662015-04-03 00:22:06 +02002382 /* kill all the stream that are on this server */
Willy Tarreau52b2d222011-09-07 23:48:48 +02002383 list_for_each_entry_safe(sess, sess_bck, &sv->actconns, by_srv)
2384 if (sess->srv_conn == sv)
Willy Tarreaue7dff022015-04-03 01:14:29 +02002385 stream_shutdown(sess, SF_ERR_KILLED);
Willy Tarreau52b2d222011-09-07 23:48:48 +02002386
2387 return 1;
2388 }
2389 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002390 appctx->ctx.cli.msg = "'shutdown sessions' only supports 'server'.\n";
2391 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau52b2d222011-09-07 23:48:48 +02002392 return 1;
2393 }
2394 }
Willy Tarreau532a4502011-09-07 22:37:44 +02002395 else { /* unknown "disable" parameter */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002396 appctx->ctx.cli.msg = "'shutdown' only supports 'frontend', 'session' and 'sessions'.\n";
2397 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002398 return 1;
Cyril Bontécd19e512010-01-31 22:34:03 +01002399 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002400 }
2401 else if (strcmp(args[0], "del") == 0) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002402 if (strcmp(args[1], "map") == 0 || strcmp(args[1], "acl") == 0) {
2403 if (args[1][0] == 'm')
2404 appctx->ctx.map.display_flags = PAT_REF_MAP;
2405 else
2406 appctx->ctx.map.display_flags = PAT_REF_ACL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002407
2408 /* Expect two parameters: map name and key. */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002409 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
2410 if (!*args[2] || !*args[3]) {
2411 appctx->ctx.cli.msg = "This command expects two parameters: map identifier and key.\n";
2412 appctx->st0 = STAT_CLI_PRINT;
2413 return 1;
2414 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002415 }
2416
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002417 else {
2418 if (!*args[2] || !*args[3]) {
2419 appctx->ctx.cli.msg = "This command expects two parameters: ACL identifier and key.\n";
2420 appctx->st0 = STAT_CLI_PRINT;
2421 return 1;
2422 }
2423 }
2424
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002425 /* Lookup the reference in the maps. */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002426 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002427 if (!appctx->ctx.map.ref ||
2428 !(appctx->ctx.map.ref->flags & appctx->ctx.map.display_flags)) {
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01002429 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002430 appctx->st0 = STAT_CLI_PRINT;
2431 return 1;
2432 }
2433
Thierry FOURNIER9356c682014-01-28 15:55:37 +01002434 /* If the entry identifier start with a '#', it is considered as
2435 * pointer id
2436 */
2437 if (args[3][0] == '#' && args[3][1] == '0' && args[3][2] == 'x') {
2438 struct pat_ref_elt *ref;
2439 long long int conv;
2440 char *error;
2441
2442 /* Convert argument to integer value. */
2443 conv = strtoll(&args[3][1], &error, 16);
2444 if (*error != '\0') {
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01002445 appctx->ctx.cli.msg = "Malformed identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER9356c682014-01-28 15:55:37 +01002446 appctx->st0 = STAT_CLI_PRINT;
2447 return 1;
2448 }
2449
2450 /* Convert and check integer to pointer. */
2451 ref = (struct pat_ref_elt *)(long)conv;
2452 if ((long long int)(long)ref != conv) {
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01002453 appctx->ctx.cli.msg = "Malformed identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER9356c682014-01-28 15:55:37 +01002454 appctx->st0 = STAT_CLI_PRINT;
2455 return 1;
2456 }
2457
2458 /* Try to delete the entry. */
2459 if (!pat_ref_delete_by_id(appctx->ctx.map.ref, ref)) {
2460 /* The entry is not found, send message. */
2461 appctx->ctx.cli.msg = "Key not found.\n";
2462 appctx->st0 = STAT_CLI_PRINT;
2463 return 1;
2464 }
2465 }
2466 else {
2467 /* Else, use the entry identifier as pattern
2468 * string and try to delete the entry.
2469 */
2470 if (!pat_ref_delete(appctx->ctx.map.ref, args[3])) {
2471 /* The entry is not found, send message. */
2472 appctx->ctx.cli.msg = "Key not found.\n";
2473 appctx->st0 = STAT_CLI_PRINT;
2474 return 1;
2475 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002476 }
2477
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002478 /* The deletion is done, send message. */
Thierry FOURNIER07e78c52014-12-18 15:28:01 +01002479 appctx->st0 = STAT_CLI_PROMPT;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002480 return 1;
2481 }
2482 else { /* unknown "del" parameter */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002483 appctx->ctx.cli.msg = "'del' only supports 'map' or 'acl'.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002484 appctx->st0 = STAT_CLI_PRINT;
2485 return 1;
2486 }
2487 }
2488 else if (strcmp(args[0], "add") == 0) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002489 if (strcmp(args[1], "map") == 0 ||
2490 strcmp(args[1], "acl") == 0) {
2491 int ret;
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002492 char *err;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002493
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002494 /* Set flags. */
2495 if (args[1][0] == 'm')
2496 appctx->ctx.map.display_flags = PAT_REF_MAP;
2497 else
2498 appctx->ctx.map.display_flags = PAT_REF_ACL;
2499
2500 /* If the keywork is "map", we expect three parameters, if it
2501 * is "acl", we expect only two parameters
2502 */
2503 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
2504 if (!*args[2] || !*args[3] || !*args[4]) {
2505 appctx->ctx.cli.msg = "'add map' expects three parameters: map identifier, key and value.\n";
2506 appctx->st0 = STAT_CLI_PRINT;
2507 return 1;
2508 }
2509 }
2510 else {
2511 if (!*args[2] || !*args[3]) {
2512 appctx->ctx.cli.msg = "'add acl' expects two parameters: ACL identifier and pattern.\n";
2513 appctx->st0 = STAT_CLI_PRINT;
2514 return 1;
2515 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002516 }
2517
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002518 /* Lookup for the reference. */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002519 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002520 if (!appctx->ctx.map.ref) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002521 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01002522 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002523 else
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01002524 appctx->ctx.cli.msg = "Unknown ACL identifier. Please use #<id> or <file>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002525 appctx->st0 = STAT_CLI_PRINT;
2526 return 1;
2527 }
2528
Thierry FOURNIER64c585f2014-01-29 20:02:36 +01002529 /* The command "add acl" is prohibited if the reference
2530 * use samples.
2531 */
2532 if ((appctx->ctx.map.display_flags & PAT_REF_ACL) &&
2533 (appctx->ctx.map.ref->flags & PAT_REF_SMP)) {
2534 appctx->ctx.cli.msg = "This ACL is shared with a map containing samples. "
2535 "You must use the command 'add map' to add values.\n";
2536 appctx->st0 = STAT_CLI_PRINT;
2537 return 1;
2538 }
2539
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002540 /* Add value. */
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002541 err = NULL;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002542 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02002543 ret = pat_ref_add(appctx->ctx.map.ref, args[3], args[4], &err);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002544 else
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02002545 ret = pat_ref_add(appctx->ctx.map.ref, args[3], NULL, &err);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002546 if (!ret) {
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002547 if (err)
2548 memprintf(&err, "%s.\n", err);
2549 appctx->ctx.cli.err = err;
2550 appctx->st0 = STAT_CLI_PRINT_FREE;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002551 return 1;
2552 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002553
2554 /* The add is done, send message. */
Thierry FOURNIER07e78c52014-12-18 15:28:01 +01002555 appctx->st0 = STAT_CLI_PROMPT;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002556 return 1;
2557 }
2558 else { /* unknown "del" parameter */
2559 appctx->ctx.cli.msg = "'add' only supports 'map'.\n";
2560 appctx->st0 = STAT_CLI_PRINT;
2561 return 1;
2562 }
Cyril Bontécd19e512010-01-31 22:34:03 +01002563 }
2564 else { /* not "show" nor "clear" nor "get" nor "set" nor "enable" nor "disable" */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002565 return 0;
2566 }
2567 return 1;
2568}
2569
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002570/* This I/O handler runs as an applet embedded in a stream interface. It is
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002571 * used to processes I/O from/to the stats unix socket. The system relies on a
2572 * state machine handling requests and various responses. We read a request,
2573 * then we process it and send the response, and we possibly display a prompt.
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002574 * Then we can read again. The state is stored in appctx->st0 and is one of the
2575 * STAT_CLI_* constants. appctx->st1 is used to indicate whether prompt is enabled
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002576 * or not.
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002577 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02002578static void cli_io_handler(struct appctx *appctx)
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002579{
Willy Tarreau00a37f02015-04-13 12:05:19 +02002580 struct stream_interface *si = appctx->owner;
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002581 struct channel *req = si_oc(si);
2582 struct channel *res = si_ic(si);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002583 int reql;
2584 int len;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002585
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002586 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
2587 goto out;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002588
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002589 while (1) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002590 if (appctx->st0 == STAT_CLI_INIT) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002591 /* Stats output not initialized yet */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002592 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
2593 appctx->st0 = STAT_CLI_GETREQ;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002594 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002595 else if (appctx->st0 == STAT_CLI_END) {
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002596 /* Let's close for real now. We just close the request
2597 * side, the conditions below will complete if needed.
2598 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02002599 si_shutw(si);
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002600 break;
2601 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002602 else if (appctx->st0 == STAT_CLI_GETREQ) {
Willy Tarreau4e33d862009-10-11 23:35:10 +02002603 /* ensure we have some output room left in the event we
2604 * would want to return some info right after parsing.
2605 */
Willy Tarreau4e4292b2014-11-28 12:18:45 +01002606 if (buffer_almost_full(si_ib(si))) {
Willy Tarreaufe127932015-04-21 19:23:39 +02002607 si_applet_cant_put(si);
Willy Tarreau4e33d862009-10-11 23:35:10 +02002608 break;
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01002609 }
Willy Tarreau4e33d862009-10-11 23:35:10 +02002610
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002611 reql = bo_getline(si_oc(si), trash.str, trash.size);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002612 if (reql <= 0) { /* closed or EOL not found */
2613 if (reql == 0)
2614 break;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002615 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002616 continue;
2617 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002618
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002619 /* seek for a possible semi-colon. If we find one, we
2620 * replace it with an LF and skip only this part.
2621 */
2622 for (len = 0; len < reql; len++)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002623 if (trash.str[len] == ';') {
2624 trash.str[len] = '\n';
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002625 reql = len + 1;
2626 break;
2627 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002628
Willy Tarreau816fc222009-10-04 07:36:58 +02002629 /* now it is time to check that we have a full line,
2630 * remove the trailing \n and possibly \r, then cut the
2631 * line.
2632 */
2633 len = reql - 1;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002634 if (trash.str[len] != '\n') {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002635 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002636 continue;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002637 }
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002638
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002639 if (len && trash.str[len-1] == '\r')
Willy Tarreau816fc222009-10-04 07:36:58 +02002640 len--;
2641
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002642 trash.str[len] = '\0';
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002643
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002644 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002645 if (len) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002646 if (strcmp(trash.str, "quit") == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002647 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002648 continue;
2649 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002650 else if (strcmp(trash.str, "prompt") == 0)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002651 appctx->st1 = !appctx->st1;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002652 else if (strcmp(trash.str, "help") == 0 ||
2653 !stats_sock_parse_request(si, trash.str)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002654 appctx->ctx.cli.msg = stats_sock_usage_msg;
2655 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02002656 }
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002657 /* NB: stats_sock_parse_request() may have put
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002658 * another STAT_CLI_O_* into appctx->st0.
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002659 */
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002660 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002661 else if (!appctx->st1) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002662 /* if prompt is disabled, print help on empty lines,
2663 * so that the user at least knows how to enable
2664 * prompt and find help.
2665 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002666 appctx->ctx.cli.msg = stats_sock_usage_msg;
2667 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002668 }
2669
2670 /* re-adjust req buffer */
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002671 bo_skip(si_oc(si), reql);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002672 req->flags |= CF_READ_DONTWAIT; /* we plan to read small requests */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002673 }
Willy Tarreau68c00c72015-09-25 19:21:19 +02002674 else { /* output functions */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002675 switch (appctx->st0) {
Andrew Hayworthe32d1862015-10-02 15:08:10 +00002676 case STAT_CLI_PROMPT:
2677 break;
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02002678 case STAT_CLI_PRINT:
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002679 if (bi_putstr(si_ic(si), appctx->ctx.cli.msg) != -1)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002680 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreaubc18da12015-03-13 14:00:47 +01002681 else
Willy Tarreaufe127932015-04-21 19:23:39 +02002682 si_applet_cant_put(si);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002683 break;
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002684 case STAT_CLI_PRINT_FREE:
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002685 if (bi_putstr(si_ic(si), appctx->ctx.cli.err) != -1) {
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002686 free(appctx->ctx.cli.err);
2687 appctx->st0 = STAT_CLI_PROMPT;
2688 }
Willy Tarreaubc18da12015-03-13 14:00:47 +01002689 else
Willy Tarreaufe127932015-04-21 19:23:39 +02002690 si_applet_cant_put(si);
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002691 break;
Baptiste Assmann9b6857e2015-09-18 14:49:12 +02002692 case STAT_CLI_O_BACKEND:
2693 if (stats_dump_backend_to_buffer(si))
2694 appctx->st0 = STAT_CLI_PROMPT;
2695 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002696 case STAT_CLI_O_INFO:
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002697 if (stats_dump_info_to_buffer(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002698 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002699 break;
Baptiste Assmann28289462015-07-03 08:01:20 +02002700 case STAT_CLI_O_SERVERS_STATE:
2701 if (stats_dump_servers_state_to_buffer(si))
2702 appctx->st0 = STAT_CLI_PROMPT;
2703 break;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002704 case STAT_CLI_O_STAT:
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002705 if (stats_dump_stat_to_buffer(si, NULL))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002706 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002707 break;
Baptiste Assmann3863f972015-05-17 00:33:24 +02002708 case STAT_CLI_O_RESOLVERS:
2709 if (stats_dump_resolvers_to_buffer(si))
2710 appctx->st0 = STAT_CLI_PROMPT;
2711 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002712 case STAT_CLI_O_SESS:
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01002713 if (stats_dump_sess_to_buffer(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002714 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002715 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002716 case STAT_CLI_O_ERR: /* errors dump */
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01002717 if (stats_dump_errors_to_buffer(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002718 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002719 break;
Willy Tarreau69f58c82010-07-12 17:55:33 +02002720 case STAT_CLI_O_TAB:
Simon Hormanc88b8872011-06-15 15:18:49 +09002721 case STAT_CLI_O_CLR:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002722 if (stats_table_request(si, appctx->st0))
2723 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau69f58c82010-07-12 17:55:33 +02002724 break;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002725 case STAT_CLI_O_PATS:
2726 if (stats_pats_list(si))
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002727 appctx->st0 = STAT_CLI_PROMPT;
2728 break;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002729 case STAT_CLI_O_PAT:
2730 if (stats_pat_list(si))
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002731 appctx->st0 = STAT_CLI_PROMPT;
2732 break;
2733 case STAT_CLI_O_MLOOK:
2734 if (stats_map_lookup(si))
2735 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau4efb3532014-01-29 12:13:39 +01002736 break;
Willy Tarreau12833bb2014-01-28 16:49:56 +01002737 case STAT_CLI_O_POOLS:
2738 if (stats_dump_pools_to_buffer(si))
2739 appctx->st0 = STAT_CLI_PROMPT;
2740 break;
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02002741#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
2742 case STAT_CLI_O_TLSK:
2743 if (stats_tlskeys_list(si))
2744 appctx->st0 = STAT_CLI_PROMPT;
2745 break;
2746#endif
Willy Tarreauae795722016-02-16 11:27:28 +01002747 case STAT_CLI_O_ENV: /* environment dump */
2748 if (stats_dump_env_to_buffer(si))
2749 appctx->st0 = STAT_CLI_PROMPT;
2750 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002751 default: /* abnormal state */
Willy Tarreau5cfa3bc2015-09-25 20:08:51 +02002752 si->flags |= SI_FL_ERR;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002753 break;
2754 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002755
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002756 /* The post-command prompt is either LF alone or LF + '> ' in interactive mode */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002757 if (appctx->st0 == STAT_CLI_PROMPT) {
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002758 if (bi_putstr(si_ic(si), appctx->st1 ? "\n> " : "\n") != -1)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002759 appctx->st0 = STAT_CLI_GETREQ;
Willy Tarreaubc18da12015-03-13 14:00:47 +01002760 else
Willy Tarreaufe127932015-04-21 19:23:39 +02002761 si_applet_cant_put(si);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002762 }
2763
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002764 /* If the output functions are still there, it means they require more room. */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002765 if (appctx->st0 >= STAT_CLI_OUTPUT)
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002766 break;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002767
2768 /* Now we close the output if one of the writers did so,
2769 * or if we're not in interactive mode and the request
2770 * buffer is empty. This still allows pipelined requests
2771 * to be sent in non-interactive mode.
2772 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002773 if ((res->flags & (CF_SHUTW|CF_SHUTW_NOW)) || (!appctx->st1 && !req->buf->o)) {
2774 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002775 continue;
2776 }
2777
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002778 /* switch state back to GETREQ to read next requests */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002779 appctx->st0 = STAT_CLI_GETREQ;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002780 }
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002781 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002782
Willy Tarreauc9e930a2015-09-25 20:06:08 +02002783 if ((res->flags & CF_SHUTR) && (si->state == SI_ST_EST)) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002784 DPRINTF(stderr, "%s@%d: si to buf closed. req=%08x, res=%08x, st=%d\n",
2785 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
Aman Guptaceafb4a2012-04-02 18:57:54 -07002786 /* Other side has closed, let's abort if we have no more processing to do
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002787 * and nothing more to consume. This is comparable to a broken pipe, so
2788 * we forward the close to the request side so that it flows upstream to
2789 * the client.
2790 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02002791 si_shutw(si);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002792 }
2793
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002794 if ((req->flags & CF_SHUTW) && (si->state == SI_ST_EST) && (appctx->st0 < STAT_CLI_OUTPUT)) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002795 DPRINTF(stderr, "%s@%d: buf to si closed. req=%08x, res=%08x, st=%d\n",
2796 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
2797 /* We have no more processing to do, and nothing more to send, and
2798 * the client side has closed. So we'll forward this state downstream
2799 * on the response buffer.
2800 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02002801 si_shutr(si);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002802 res->flags |= CF_READ_NULL;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002803 }
2804
Willy Tarreau828824a2015-04-19 17:20:03 +02002805 out:
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01002806 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 +02002807 __FUNCTION__, __LINE__,
Willy Tarreau9b28e032012-10-12 23:49:43 +02002808 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 +02002809}
2810
Willy Tarreau638d40a2016-02-24 23:11:01 +01002811/* Emits a stats field without any surrounding element and properly encoded to
2812 * resist CSV output. Returns non-zero on success, 0 if the buffer is full.
2813 */
2814static int stats_emit_raw_data_field(struct chunk *out, const struct field *f)
2815{
2816 switch (field_format(f, 0)) {
2817 case FF_EMPTY: return 1;
2818 case FF_S32: return chunk_appendf(out, "%d", f->u.s32);
2819 case FF_U32: return chunk_appendf(out, "%u", f->u.u32);
2820 case FF_S64: return chunk_appendf(out, "%lld", (long long)f->u.s64);
2821 case FF_U64: return chunk_appendf(out, "%llu", (unsigned long long)f->u.u64);
2822 case FF_STR: return csv_enc_append(field_str(f, 0), 1, out) != NULL;
2823 default: return chunk_appendf(out, "[INCORRECT_FIELD_TYPE_%08x]", f->type);
2824 }
2825}
2826
Willy Tarreaubf95cba2016-01-11 18:27:29 +01002827/* Dump all fields from <info> into <out> using the "show info" format (name: value) */
2828static int stats_dump_info_fields(struct chunk *out, const struct field *info)
2829{
2830 int field;
2831
2832 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
2833 if (!field_format(info, field))
2834 continue;
2835
2836 if (!chunk_appendf(out, "%s: ", info_field_names[field]))
2837 return 0;
2838 if (!stats_emit_raw_data_field(out, &info[field]))
2839 return 0;
2840 if (!chunk_strcat(out, "\n"))
2841 return 0;
2842 }
2843 return 1;
2844}
2845
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002846/* This function dumps information onto the stream interface's read buffer.
2847 * It returns 0 as long as it does not complete, non-zero upon completion.
2848 * No state is used.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002849 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002850static int stats_dump_info_to_buffer(struct stream_interface *si)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002851{
2852 unsigned int up = (now.tv_sec - start_date.tv_sec);
Willy Tarreau4529c072016-01-11 18:17:07 +01002853 struct chunk *out = get_trash_chunk();
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002854
Willy Tarreau0c9c2722014-05-28 12:28:58 +02002855#ifdef USE_OPENSSL
2856 int ssl_sess_rate = read_freq_ctr(&global.ssl_per_sec);
2857 int ssl_key_rate = read_freq_ctr(&global.ssl_fe_keys_per_sec);
2858 int ssl_reuse = 0;
2859
2860 if (ssl_key_rate < ssl_sess_rate) {
2861 /* count the ssl reuse ratio and avoid overflows in both directions */
2862 ssl_reuse = 100 - (100 * ssl_key_rate + (ssl_sess_rate - 1) / 2) / ssl_sess_rate;
2863 }
2864#endif
2865
Willy Tarreau4529c072016-01-11 18:17:07 +01002866 chunk_reset(out);
2867 memset(&info, 0, sizeof(info));
2868
2869 info[INF_NAME] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, PRODUCT_NAME);
2870 info[INF_VERSION] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, HAPROXY_VERSION);
2871 info[INF_RELEASE_DATE] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, HAPROXY_DATE);
2872
2873 info[INF_NBPROC] = mkf_u32(FO_CONFIG|FS_SERVICE, global.nbproc);
2874 info[INF_PROCESS_NUM] = mkf_u32(FO_KEY, relative_pid);
2875 info[INF_PID] = mkf_u32(FO_STATUS, pid);
2876
2877 info[INF_UPTIME] = mkf_str(FN_DURATION, chunk_newstr(out));
2878 chunk_appendf(out, "%ud %uh%02um%02us", up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60));
2879
2880 info[INF_UPTIME_SEC] = mkf_u32(FN_DURATION, up);
2881 info[INF_MEMMAX_MB] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_memmax);
2882 info[INF_POOL_ALLOC_MB] = mkf_u32(0, (unsigned)(pool_total_allocated() / 1048576L));
2883 info[INF_POOL_USED_MB] = mkf_u32(0, (unsigned)(pool_total_used() / 1048576L));
2884 info[INF_POOL_FAILED] = mkf_u32(FN_COUNTER, pool_total_failures());
2885 info[INF_ULIMIT_N] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_nofile);
2886 info[INF_MAXSOCK] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxsock);
2887 info[INF_MAXCONN] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxconn);
2888 info[INF_HARD_MAXCONN] = mkf_u32(FO_CONFIG|FN_LIMIT, global.hardmaxconn);
2889 info[INF_CURR_CONN] = mkf_u32(0, actconn);
2890 info[INF_CUM_CONN] = mkf_u32(FN_COUNTER, totalconn);
2891 info[INF_CUM_REQ] = mkf_u32(FN_COUNTER, global.req_count);
2892#ifdef USE_OPENSSL
2893 info[INF_MAX_SSL_CONNS] = mkf_u32(FN_MAX, global.maxsslconn);
2894 info[INF_CURR_SSL_CONNS] = mkf_u32(0, sslconns);
2895 info[INF_CUM_SSL_CONNS] = mkf_u32(FN_COUNTER, totalsslconns);
2896#endif
2897 info[INF_MAXPIPES] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxpipes);
2898 info[INF_PIPES_USED] = mkf_u32(0, pipes_used);
2899 info[INF_PIPES_FREE] = mkf_u32(0, pipes_free);
2900 info[INF_CONN_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.conn_per_sec));
2901 info[INF_CONN_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.cps_lim);
2902 info[INF_MAX_CONN_RATE] = mkf_u32(FN_MAX, global.cps_max);
2903 info[INF_SESS_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.sess_per_sec));
2904 info[INF_SESS_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.sps_lim);
2905 info[INF_MAX_SESS_RATE] = mkf_u32(FN_RATE, global.sps_max);
2906
2907#ifdef USE_OPENSSL
2908 info[INF_SSL_RATE] = mkf_u32(FN_RATE, ssl_sess_rate);
2909 info[INF_SSL_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.ssl_lim);
2910 info[INF_MAX_SSL_RATE] = mkf_u32(FN_MAX, global.ssl_max);
2911 info[INF_SSL_FRONTEND_KEY_RATE] = mkf_u32(0, ssl_key_rate);
2912 info[INF_SSL_FRONTEND_MAX_KEY_RATE] = mkf_u32(FN_MAX, global.ssl_fe_keys_max);
2913 info[INF_SSL_FRONTEND_SESSION_REUSE_PCT] = mkf_u32(0, ssl_reuse);
2914 info[INF_SSL_BACKEND_KEY_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.ssl_be_keys_per_sec));
2915 info[INF_SSL_BACKEND_MAX_KEY_RATE] = mkf_u32(FN_MAX, global.ssl_be_keys_max);
2916 info[INF_SSL_CACHE_LOOKUPS] = mkf_u32(FN_COUNTER, global.shctx_lookups);
2917 info[INF_SSL_CACHE_MISSES] = mkf_u32(FN_COUNTER, global.shctx_misses);
2918#endif
2919 info[INF_COMPRESS_BPS_IN] = mkf_u32(FN_RATE, read_freq_ctr(&global.comp_bps_in));
2920 info[INF_COMPRESS_BPS_OUT] = mkf_u32(FN_RATE, read_freq_ctr(&global.comp_bps_out));
2921 info[INF_COMPRESS_BPS_RATE_LIM] = mkf_u32(FO_CONFIG|FN_LIMIT, global.comp_rate_lim);
2922#ifdef USE_ZLIB
2923 info[INF_ZLIB_MEM_USAGE] = mkf_u32(0, zlib_used_memory);
2924 info[INF_MAX_ZLIB_MEM_USAGE] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxzlibmem);
2925#endif
2926 info[INF_TASKS] = mkf_u32(0, nb_tasks_cur);
2927 info[INF_RUN_QUEUE] = mkf_u32(0, run_queue_cur);
2928 info[INF_IDLE_PCT] = mkf_u32(FN_AVG, idle_pct);
2929 info[INF_NODE] = mkf_str(FO_CONFIG|FN_OUTPUT|FS_SERVICE, global.node);
2930 if (global.desc)
2931 info[INF_DESCRIPTION] = mkf_str(FO_CONFIG|FN_OUTPUT|FS_SERVICE, global.desc);
2932
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002933 chunk_printf(&trash,
2934 "Name: " PRODUCT_NAME "\n"
2935 "Version: " HAPROXY_VERSION "\n"
2936 "Release_date: " HAPROXY_DATE "\n"
2937 "Nbproc: %d\n"
2938 "Process_num: %d\n"
2939 "Pid: %d\n"
2940 "Uptime: %dd %dh%02dm%02ds\n"
2941 "Uptime_sec: %d\n"
2942 "Memmax_MB: %d\n"
Willy Tarreau58102cf2015-10-28 16:24:21 +01002943 "PoolAlloc_MB: %d\n"
2944 "PoolUsed_MB: %d\n"
2945 "PoolFailed: %d\n"
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002946 "Ulimit-n: %d\n"
2947 "Maxsock: %d\n"
2948 "Maxconn: %d\n"
2949 "Hard_maxconn: %d\n"
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002950 "CurrConns: %d\n"
Willy Tarreau71b734c2014-01-28 15:19:44 +01002951 "CumConns: %d\n"
Warren Turkalb197d7f2015-01-27 15:04:16 -08002952 "CumReq: %u\n"
Willy Tarreau71b734c2014-01-28 15:19:44 +01002953#ifdef USE_OPENSSL
2954 "MaxSslConns: %d\n"
2955 "CurrSslConns: %d\n"
2956 "CumSslConns: %d\n"
2957#endif
2958 "Maxpipes: %d\n"
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002959 "PipesUsed: %d\n"
2960 "PipesFree: %d\n"
2961 "ConnRate: %d\n"
2962 "ConnRateLimit: %d\n"
2963 "MaxConnRate: %d\n"
Willy Tarreau93e7c002013-10-07 18:51:07 +02002964 "SessRate: %d\n"
2965 "SessRateLimit: %d\n"
2966 "MaxSessRate: %d\n"
Willy Tarreaue43d5322013-10-07 20:01:52 +02002967#ifdef USE_OPENSSL
2968 "SslRate: %d\n"
2969 "SslRateLimit: %d\n"
2970 "MaxSslRate: %d\n"
Willy Tarreau0c9c2722014-05-28 12:28:58 +02002971 "SslFrontendKeyRate: %d\n"
2972 "SslFrontendMaxKeyRate: %d\n"
2973 "SslFrontendSessionReuse_pct: %d\n"
2974 "SslBackendKeyRate: %d\n"
2975 "SslBackendMaxKeyRate: %d\n"
Willy Tarreauce3f9132014-05-28 16:47:01 +02002976 "SslCacheLookups: %u\n"
2977 "SslCacheMisses: %u\n"
Willy Tarreaue43d5322013-10-07 20:01:52 +02002978#endif
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002979 "CompressBpsIn: %u\n"
2980 "CompressBpsOut: %u\n"
2981 "CompressBpsRateLim: %u\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002982#ifdef USE_ZLIB
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002983 "ZlibMemUsage: %ld\n"
2984 "MaxZlibMemUsage: %ld\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002985#endif
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002986 "Tasks: %d\n"
2987 "Run_queue: %d\n"
2988 "Idle_pct: %d\n"
2989 "node: %s\n"
2990 "description: %s\n"
2991 "",
2992 global.nbproc,
2993 relative_pid,
2994 pid,
2995 up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60),
2996 up,
2997 global.rlimit_memmax,
Willy Tarreau58102cf2015-10-28 16:24:21 +01002998 (int)(pool_total_allocated() / 1048576L),
2999 (int)(pool_total_used() / 1048576L),
3000 pool_total_failures(),
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003001 global.rlimit_nofile,
Willy Tarreau71b734c2014-01-28 15:19:44 +01003002 global.maxsock, global.maxconn, global.hardmaxconn,
3003 actconn, totalconn, global.req_count,
3004#ifdef USE_OPENSSL
3005 global.maxsslconn, sslconns, totalsslconns,
3006#endif
3007 global.maxpipes, pipes_used, pipes_free,
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003008 read_freq_ctr(&global.conn_per_sec), global.cps_lim, global.cps_max,
Willy Tarreau93e7c002013-10-07 18:51:07 +02003009 read_freq_ctr(&global.sess_per_sec), global.sps_lim, global.sps_max,
Willy Tarreaue43d5322013-10-07 20:01:52 +02003010#ifdef USE_OPENSSL
Willy Tarreau0c9c2722014-05-28 12:28:58 +02003011 ssl_sess_rate, global.ssl_lim, global.ssl_max,
3012 ssl_key_rate, global.ssl_fe_keys_max,
3013 ssl_reuse,
3014 read_freq_ctr(&global.ssl_be_keys_per_sec), global.ssl_be_keys_max,
Willy Tarreauce3f9132014-05-28 16:47:01 +02003015 global.shctx_lookups, global.shctx_misses,
Willy Tarreaue43d5322013-10-07 20:01:52 +02003016#endif
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003017 read_freq_ctr(&global.comp_bps_in), read_freq_ctr(&global.comp_bps_out),
3018 global.comp_rate_lim,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003019#ifdef USE_ZLIB
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003020 zlib_used_memory, global.maxzlibmem,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003021#endif
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01003022 nb_tasks_cur, run_queue_cur, idle_pct,
3023 global.node, global.desc ? global.desc : ""
3024 );
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003025
Willy Tarreaubc18da12015-03-13 14:00:47 +01003026 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02003027 si_applet_cant_put(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003028 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01003029 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003030
3031 return 1;
3032}
3033
Baptiste Assmann28289462015-07-03 08:01:20 +02003034/* dumps server state information into <buf> for all the servers found in <backend>
3035 * These information are all the parameters which may change during HAProxy runtime.
3036 * By default, we only export to the last known server state file format.
3037 * These information can be used at next startup to recover same level of server state.
3038 */
3039static void dump_servers_state(struct proxy *backend, struct chunk *buf)
3040{
3041 struct server *srv;
3042 char srv_addr[INET6_ADDRSTRLEN + 1];
3043 time_t srv_time_since_last_change;
3044 int bk_f_forced_id, srv_f_forced_id;
3045
3046 /* we don't want to report any state if the backend is not enabled on this process */
3047 if (backend->bind_proc && !(backend->bind_proc & (1UL << (relative_pid - 1))))
3048 return;
3049
3050 srv = backend->srv;
3051
3052 while (srv) {
3053 srv_addr[0] = '\0';
3054 srv_time_since_last_change = 0;
3055 bk_f_forced_id = 0;
3056 srv_f_forced_id = 0;
3057
3058 switch (srv->addr.ss_family) {
3059 case AF_INET:
3060 inet_ntop(srv->addr.ss_family, &((struct sockaddr_in *)&srv->addr)->sin_addr,
3061 srv_addr, INET_ADDRSTRLEN + 1);
3062 break;
3063 case AF_INET6:
3064 inet_ntop(srv->addr.ss_family, &((struct sockaddr_in6 *)&srv->addr)->sin6_addr,
3065 srv_addr, INET6_ADDRSTRLEN + 1);
3066 break;
3067 }
3068 srv_time_since_last_change = now.tv_sec - srv->last_change;
3069 bk_f_forced_id = backend->options & PR_O_FORCED_ID ? 1 : 0;
3070 srv_f_forced_id = srv->flags & SRV_F_FORCED_ID ? 1 : 0;
3071
3072 chunk_appendf(buf,
3073 "%d %s "
3074 "%d %s %s "
3075 "%d %d %d %d %ld "
3076 "%d %d %d %d %d "
3077 "%d %d"
3078 "\n",
3079 backend->uuid, backend->id,
3080 srv->puid, srv->id, srv_addr,
David Carlier081b3362015-11-18 06:10:22 +00003081 srv->state, srv->admin, srv->uweight, srv->iweight, (long int)srv_time_since_last_change,
Baptiste Assmann28289462015-07-03 08:01:20 +02003082 srv->check.status, srv->check.result, srv->check.health, srv->check.state, srv->agent.state,
3083 bk_f_forced_id, srv_f_forced_id);
3084
3085 srv = srv->next;
3086 }
3087}
3088
Baptiste Assmann9b6857e2015-09-18 14:49:12 +02003089/* Parses backend list and simply report backend names */
3090static int stats_dump_backend_to_buffer(struct stream_interface *si)
3091{
3092 extern struct proxy *proxy;
3093 struct proxy *curproxy;
3094
3095 chunk_reset(&trash);
3096 chunk_printf(&trash, "# name\n");
3097
3098 for (curproxy = proxy; curproxy != NULL; curproxy = curproxy->next) {
3099 /* looking for backends only */
3100 if (!(curproxy->cap & PR_CAP_BE))
3101 continue;
3102
3103 /* we don't want to list a backend which is bound to this process */
3104 if (curproxy->bind_proc && !(curproxy->bind_proc & (1UL << (relative_pid - 1))))
3105 continue;
3106
3107 chunk_appendf(&trash, "%s\n", curproxy->id);
3108 }
3109
3110 if (bi_putchk(si_ic(si), &trash) == -1) {
3111 si_applet_cant_put(si);
3112 return 0;
3113 }
3114
3115 return 1;
3116}
3117
Baptiste Assmann28289462015-07-03 08:01:20 +02003118/* Parses backend list or simply use backend name provided by the user to return
3119 * states of servers to stdout.
3120 */
3121static int stats_dump_servers_state_to_buffer(struct stream_interface *si)
3122{
3123 struct appctx *appctx = __objt_appctx(si->end);
3124 extern struct proxy *proxy;
3125 struct proxy *curproxy;
3126
3127 chunk_reset(&trash);
3128
3129 chunk_printf(&trash, "%d\n# %s\n", SRV_STATE_FILE_VERSION, SRV_STATE_FILE_FIELD_NAMES);
3130
3131 if (appctx->ctx.server_state.backend) {
3132 dump_servers_state(appctx->ctx.server_state.backend, &trash);
3133 }
3134 else {
3135 for (curproxy = proxy; curproxy != NULL; curproxy = curproxy->next) {
3136 /* servers are only in backends */
3137 if (!(curproxy->cap & PR_CAP_BE))
3138 continue;
3139
3140 dump_servers_state(curproxy, &trash);
3141 }
3142 }
3143
3144 if (bi_putchk(si_ic(si), &trash) == -1) {
3145 si_applet_cant_put(si);
3146 return 0;
3147 }
3148
3149 return 1;
3150}
3151
Willy Tarreau12833bb2014-01-28 16:49:56 +01003152/* This function dumps memory usage information onto the stream interface's
3153 * read buffer. It returns 0 as long as it does not complete, non-zero upon
3154 * completion. No state is used.
3155 */
3156static int stats_dump_pools_to_buffer(struct stream_interface *si)
3157{
3158 dump_pools_to_trash();
Willy Tarreaubc18da12015-03-13 14:00:47 +01003159 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02003160 si_applet_cant_put(si);
Willy Tarreau12833bb2014-01-28 16:49:56 +01003161 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01003162 }
Willy Tarreau12833bb2014-01-28 16:49:56 +01003163 return 1;
3164}
3165
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003166/* Dumps a frontend's line to the trash for the current proxy <px> and uses
3167 * the state from stream interface <si>. The caller is responsible for clearing
3168 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
Cyril Bonté70be45d2010-10-12 00:14:35 +02003169 */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003170static int stats_dump_fe_stats(struct stream_interface *si, struct proxy *px)
Cyril Bonté70be45d2010-10-12 00:14:35 +02003171{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003172 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003173 int i;
Cyril Bonté70be45d2010-10-12 00:14:35 +02003174
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003175 if (!(px->cap & PR_CAP_FE))
3176 return 0;
Cyril Bonté70be45d2010-10-12 00:14:35 +02003177
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003178 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_FE)))
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003179 return 0;
3180
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003181 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003182 chunk_appendf(&trash,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003183 /* name, queue */
3184 "<tr class=\"frontend\">");
Cyril Bonté70be45d2010-10-12 00:14:35 +02003185
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003186 if (px->cap & PR_CAP_BE && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003187 /* Column sub-heading for Enable or Disable server */
3188 chunk_appendf(&trash, "<td></td>");
3189 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02003190
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003191 chunk_appendf(&trash,
3192 "<td class=ac>"
3193 "<a name=\"%s/Frontend\"></a>"
3194 "<a class=lfsb href=\"#%s/Frontend\">Frontend</a></td>"
3195 "<td colspan=3></td>"
3196 "",
3197 px->id, px->id);
Cyril Bonté70be45d2010-10-12 00:14:35 +02003198
Willy Tarreau466c9b52012-12-23 02:25:03 +01003199 chunk_appendf(&trash,
3200 /* sessions rate : current */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003201 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01003202 "<tr><th>Current connection rate:</th><td>%s/s</td></tr>"
3203 "<tr><th>Current session rate:</th><td>%s/s</td></tr>"
3204 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003205 U2H(read_freq_ctr(&px->fe_sess_per_sec)),
3206 U2H(read_freq_ctr(&px->fe_conn_per_sec)),
3207 U2H(read_freq_ctr(&px->fe_sess_per_sec)));
Willy Tarreau466c9b52012-12-23 02:25:03 +01003208
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003209 if (px->mode == PR_MODE_HTTP)
3210 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01003211 "<tr><th>Current request rate:</th><td>%s/s</td></tr>",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003212 U2H(read_freq_ctr(&px->fe_req_per_sec)));
Willy Tarreau466c9b52012-12-23 02:25:03 +01003213
3214 chunk_appendf(&trash,
3215 "</table></div></u></td>"
3216 /* sessions rate : max */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003217 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01003218 "<tr><th>Max connection rate:</th><td>%s/s</td></tr>"
3219 "<tr><th>Max session rate:</th><td>%s/s</td></tr>"
3220 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003221 U2H(px->fe_counters.sps_max),
3222 U2H(px->fe_counters.cps_max),
3223 U2H(px->fe_counters.sps_max));
Willy Tarreau466c9b52012-12-23 02:25:03 +01003224
3225 if (px->mode == PR_MODE_HTTP)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003226 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01003227 "<tr><th>Max request rate:</th><td>%s/s</td></tr>",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003228 U2H(px->fe_counters.p.http.rps_max));
Willy Tarreau466c9b52012-12-23 02:25:03 +01003229
3230 chunk_appendf(&trash,
3231 "</table></div></u></td>"
3232 /* sessions rate : limit */
3233 "<td>%s</td>",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003234 LIM2A(px->fe_sps_lim, "-"));
Cyril Bonté70be45d2010-10-12 00:14:35 +02003235
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003236 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003237 /* sessions: current, max, limit, total */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003238 "<td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003239 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01003240 "<tr><th>Cum. connections:</th><td>%s</td></tr>"
3241 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003242 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003243 U2H(px->feconn), U2H(px->fe_counters.conn_max), U2H(px->maxconn),
3244 U2H(px->fe_counters.cum_sess),
3245 U2H(px->fe_counters.cum_conn),
3246 U2H(px->fe_counters.cum_sess));
Cyril Bonté70be45d2010-10-12 00:14:35 +02003247
Willy Tarreau466c9b52012-12-23 02:25:03 +01003248 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003249 if (px->mode == PR_MODE_HTTP) {
Willy Tarreau466c9b52012-12-23 02:25:03 +01003250 chunk_appendf(&trash,
3251 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
3252 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
3253 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
3254 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
3255 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
3256 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
3257 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
3258 "<tr><th>- other responses:</th><td>%s</td></tr>"
3259 "<tr><th>Intercepted requests:</th><td>%s</td></tr>"
3260 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003261 U2H(px->fe_counters.p.http.cum_req),
3262 U2H(px->fe_counters.p.http.rsp[1]),
3263 U2H(px->fe_counters.p.http.rsp[2]),
3264 U2H(px->fe_counters.p.http.comp_rsp),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003265 px->fe_counters.p.http.rsp[2] ?
Willy Tarreau466c9b52012-12-23 02:25:03 +01003266 (int)(100*px->fe_counters.p.http.comp_rsp/px->fe_counters.p.http.rsp[2]) : 0,
Willy Tarreau56adcf22012-12-23 18:00:29 +01003267 U2H(px->fe_counters.p.http.rsp[3]),
3268 U2H(px->fe_counters.p.http.rsp[4]),
3269 U2H(px->fe_counters.p.http.rsp[5]),
3270 U2H(px->fe_counters.p.http.rsp[0]),
3271 U2H(px->fe_counters.intercepted_req));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003272 }
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02003273
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003274 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01003275 "</table></div></u></td>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05003276 /* sessions: lbtot, lastsess */
3277 "<td></td><td></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003278 /* bytes : in */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003279 "<td>%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003280 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003281 U2H(px->fe_counters.bytes_in));
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02003282
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003283 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003284 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
Willy Tarreau4e5d58e2015-07-04 14:35:15 +02003285 "<td>%s%s<div class=tips><table class=det>"
3286 "<tr><th>Response bytes in:</th><td>%s</td></tr>"
3287 "<tr><th>Compression in:</th><td>%s</td></tr>"
3288 "<tr><th>Compression out:</th><td>%s</td><td>(%d%%)</td></tr>"
3289 "<tr><th>Compression bypass:</th><td>%s</td></tr>"
3290 "<tr><th>Total bytes saved:</th><td>%s</td><td>(%d%%)</td></tr>"
3291 "</table></div>%s</td>",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003292 (px->fe_counters.comp_in || px->fe_counters.comp_byp) ? "<u>":"",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003293 U2H(px->fe_counters.bytes_out),
Willy Tarreau4e5d58e2015-07-04 14:35:15 +02003294 U2H(px->fe_counters.bytes_out),
3295 U2H(px->fe_counters.comp_in),
3296 U2H(px->fe_counters.comp_out),
3297 px->fe_counters.comp_in ? (int)(px->fe_counters.comp_out * 100 / px->fe_counters.comp_in) : 0,
3298 U2H(px->fe_counters.comp_byp),
3299 U2H(px->fe_counters.comp_in - px->fe_counters.comp_out),
3300 px->fe_counters.bytes_out ? (int)((px->fe_counters.comp_in - px->fe_counters.comp_out) * 100 / px->fe_counters.bytes_out) : 0,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003301 (px->fe_counters.comp_in || px->fe_counters.comp_byp) ? "</u>":"");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02003302
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003303 chunk_appendf(&trash,
3304 /* denied: req, resp */
3305 "<td>%s</td><td>%s</td>"
3306 /* errors : request, connect, response */
3307 "<td>%s</td><td></td><td></td>"
3308 /* warnings: retries, redispatches */
3309 "<td></td><td></td>"
3310 /* server status : reflect frontend status */
3311 "<td class=ac>%s</td>"
3312 /* rest of server: nothing */
3313 "<td class=ac colspan=8></td></tr>"
3314 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003315 U2H(px->fe_counters.denied_req), U2H(px->fe_counters.denied_resp),
3316 U2H(px->fe_counters.failed_req),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003317 px->state == PR_STREADY ? "OPEN" :
3318 px->state == PR_STFULL ? "FULL" : "STOP");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02003319 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003320 else { /* CSV mode */
3321 chunk_appendf(&trash,
3322 /* pxid, name, queue cur, queue max, */
3323 "%s,FRONTEND,,,"
Willy Tarreauf522f3d2014-02-10 22:22:49 +01003324 /* sessions : current, max, limit, total */
3325 "%d,%d,%d,%lld,"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003326 /* bytes : in, out */
3327 "%lld,%lld,"
3328 /* denied: req, resp */
3329 "%lld,%lld,"
3330 /* errors : request, connect, response */
3331 "%lld,,,"
3332 /* warnings: retries, redispatches */
3333 ",,"
3334 /* server status : reflect frontend status */
3335 "%s,"
3336 /* rest of server: nothing */
3337 ",,,,,,,,"
3338 /* pid, iid, sid, throttle, lbtot, tracked, type */
3339 "%d,%d,0,,,,%d,"
3340 /* rate, rate_lim, rate_max */
3341 "%u,%u,%u,"
3342 /* check_status, check_code, check_duration */
3343 ",,,",
3344 px->id,
3345 px->feconn, px->fe_counters.conn_max, px->maxconn, px->fe_counters.cum_sess,
3346 px->fe_counters.bytes_in, px->fe_counters.bytes_out,
3347 px->fe_counters.denied_req, px->fe_counters.denied_resp,
3348 px->fe_counters.failed_req,
3349 px->state == PR_STREADY ? "OPEN" :
3350 px->state == PR_STFULL ? "FULL" : "STOP",
3351 relative_pid, px->uuid, STATS_TYPE_FE,
3352 read_freq_ctr(&px->fe_sess_per_sec),
3353 px->fe_sps_lim, px->fe_counters.sps_max);
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02003354
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003355 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
3356 if (px->mode == PR_MODE_HTTP) {
3357 for (i=1; i<6; i++)
3358 chunk_appendf(&trash, "%lld,", px->fe_counters.p.http.rsp[i]);
3359 chunk_appendf(&trash, "%lld,", px->fe_counters.p.http.rsp[0]);
3360 }
3361 else
3362 chunk_appendf(&trash, ",,,,,,");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02003363
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003364 /* failed health analyses */
3365 chunk_appendf(&trash, ",");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02003366
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003367 /* requests : req_rate, req_rate_max, req_tot, */
3368 chunk_appendf(&trash, "%u,%u,%lld,",
3369 read_freq_ctr(&px->fe_req_per_sec),
3370 px->fe_counters.p.http.rps_max, px->fe_counters.p.http.cum_req);
3371
3372 /* errors: cli_aborts, srv_aborts */
3373 chunk_appendf(&trash, ",,");
3374
3375 /* compression: in, out, bypassed */
3376 chunk_appendf(&trash, "%lld,%lld,%lld,",
3377 px->fe_counters.comp_in, px->fe_counters.comp_out, px->fe_counters.comp_byp);
3378
3379 /* compression: comp_rsp */
3380 chunk_appendf(&trash, "%lld,",
3381 px->fe_counters.p.http.comp_rsp);
3382
Willy Tarreauf5b1cc32014-06-17 12:20:59 +02003383 /* lastsess, last_chk, last_agt, qtime, ctime, rtime, ttime, */
3384 chunk_appendf(&trash, ",,,,,,,");
Willy Tarreauf522f3d2014-02-10 22:22:49 +01003385
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003386 /* finish with EOL */
3387 chunk_appendf(&trash, "\n");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02003388 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003389 return 1;
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02003390}
3391
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003392/* Dumps a line for listener <l> and proxy <px> to the trash and uses the state
3393 * from stream interface <si>, and stats flags <flags>. The caller is responsible
3394 * for clearing the trash if needed. Returns non-zero if it emits anything, zero
3395 * otherwise.
Willy Tarreau91861262007-10-17 17:06:05 +02003396 */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003397static int stats_dump_li_stats(struct stream_interface *si, struct proxy *px, struct listener *l, int flags)
Willy Tarreau91861262007-10-17 17:06:05 +02003398{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003399 struct appctx *appctx = __objt_appctx(si->end);
3400
3401 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003402 chunk_appendf(&trash, "<tr class=socket>");
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003403 if (px->cap & PR_CAP_BE && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003404 /* Column sub-heading for Enable or Disable server */
3405 chunk_appendf(&trash, "<td></td>");
3406 }
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003407 chunk_appendf(&trash,
3408 /* frontend name, listener name */
3409 "<td class=ac><a name=\"%s/+%s\"></a>%s"
3410 "<a class=lfsb href=\"#%s/+%s\">%s</a>"
3411 "",
3412 px->id, l->name,
3413 (flags & ST_SHLGNDS)?"<u>":"",
3414 px->id, l->name, l->name);
Willy Tarreau91861262007-10-17 17:06:05 +02003415
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003416 if (flags & ST_SHLGNDS) {
3417 char str[INET6_ADDRSTRLEN];
3418 int port;
Willy Tarreau91861262007-10-17 17:06:05 +02003419
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003420 chunk_appendf(&trash, "<div class=tips>");
Willy Tarreau91861262007-10-17 17:06:05 +02003421
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003422 port = get_host_port(&l->addr);
3423 switch (addr_to_str(&l->addr, str, sizeof(str))) {
3424 case AF_INET:
3425 chunk_appendf(&trash, "IPv4: %s:%d, ", str, port);
3426 break;
3427 case AF_INET6:
3428 chunk_appendf(&trash, "IPv6: [%s]:%d, ", str, port);
3429 break;
3430 case AF_UNIX:
3431 chunk_appendf(&trash, "unix, ");
3432 break;
3433 case -1:
3434 chunk_appendf(&trash, "(%s), ", strerror(errno));
3435 break;
3436 }
Willy Tarreau91861262007-10-17 17:06:05 +02003437
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003438 /* id */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003439 chunk_appendf(&trash, "id: %d</div>", l->luid);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003440 }
Willy Tarreau91861262007-10-17 17:06:05 +02003441
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003442 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003443 /* queue */
3444 "%s</td><td colspan=3></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003445 /* sessions rate: current, max, limit */
3446 "<td colspan=3>&nbsp;</td>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05003447 /* sessions: current, max, limit, total, lbtot, lastsess */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003448 "<td>%s</td><td>%s</td><td>%s</td>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05003449 "<td>%s</td><td>&nbsp;</td><td>&nbsp;</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003450 /* bytes: in, out */
3451 "<td>%s</td><td>%s</td>"
3452 "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003453 (flags & ST_SHLGNDS)?"</u>":"",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003454 U2H(l->nbconn), U2H(l->counters->conn_max), U2H(l->maxconn),
3455 U2H(l->counters->cum_conn), U2H(l->counters->bytes_in), U2H(l->counters->bytes_out));
Willy Tarreau56a560a2009-09-22 19:27:35 +02003456
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003457 chunk_appendf(&trash,
3458 /* denied: req, resp */
3459 "<td>%s</td><td>%s</td>"
3460 /* errors: request, connect, response */
3461 "<td>%s</td><td></td><td></td>"
3462 /* warnings: retries, redispatches */
3463 "<td></td><td></td>"
3464 /* server status: reflect listener status */
3465 "<td class=ac>%s</td>"
3466 /* rest of server: nothing */
3467 "<td class=ac colspan=8></td></tr>"
3468 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003469 U2H(l->counters->denied_req), U2H(l->counters->denied_resp),
3470 U2H(l->counters->failed_req),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003471 (l->nbconn < l->maxconn) ? (l->state == LI_LIMITED) ? "WAITING" : "OPEN" : "FULL");
3472 }
3473 else { /* CSV mode */
3474 chunk_appendf(&trash,
3475 /* pxid, name, queue cur, queue max, */
3476 "%s,%s,,,"
Willy Tarreauf522f3d2014-02-10 22:22:49 +01003477 /* sessions: current, max, limit, total */
3478 "%d,%d,%d,%lld,"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003479 /* bytes: in, out */
3480 "%lld,%lld,"
3481 /* denied: req, resp */
3482 "%lld,%lld,"
3483 /* errors: request, connect, response */
3484 "%lld,,,"
3485 /* warnings: retries, redispatches */
3486 ",,"
3487 /* server status: reflect listener status */
3488 "%s,"
3489 /* rest of server: nothing */
3490 ",,,,,,,,"
3491 /* pid, iid, sid, throttle, lbtot, tracked, type */
3492 "%d,%d,%d,,,,%d,"
3493 /* rate, rate_lim, rate_max */
3494 ",,,"
3495 /* check_status, check_code, check_duration */
3496 ",,,"
3497 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
3498 ",,,,,,"
3499 /* failed health analyses */
3500 ","
3501 /* requests : req_rate, req_rate_max, req_tot, */
3502 ",,,"
3503 /* errors: cli_aborts, srv_aborts */
3504 ",,"
3505 /* compression: in, out, bypassed, comp_rsp */
3506 ",,,,"
Willy Tarreauf5b1cc32014-06-17 12:20:59 +02003507 /* lastsess, last_chk, last_agt, qtime, ctime, rtime, ttime, */
3508 ",,,,,,,"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003509 "\n",
3510 px->id, l->name,
3511 l->nbconn, l->counters->conn_max,
3512 l->maxconn, l->counters->cum_conn,
3513 l->counters->bytes_in, l->counters->bytes_out,
3514 l->counters->denied_req, l->counters->denied_resp,
3515 l->counters->failed_req,
3516 (l->nbconn < l->maxconn) ? "OPEN" : "FULL",
3517 relative_pid, px->uuid, l->luid, STATS_TYPE_SO);
3518 }
3519 return 1;
3520}
Willy Tarreau91861262007-10-17 17:06:05 +02003521
Simon Horman4d2eab62015-04-23 14:51:26 +09003522enum srv_stats_state {
3523 SRV_STATS_STATE_DOWN = 0,
3524 SRV_STATS_STATE_DOWN_AGENT,
3525 SRV_STATS_STATE_GOING_UP,
3526 SRV_STATS_STATE_UP_GOING_DOWN,
3527 SRV_STATS_STATE_UP,
3528 SRV_STATS_STATE_NOLB_GOING_DOWN,
3529 SRV_STATS_STATE_NOLB,
3530 SRV_STATS_STATE_DRAIN_GOING_DOWN,
3531 SRV_STATS_STATE_DRAIN,
Simon Hormanb167b6b2015-04-23 14:51:29 +09003532 SRV_STATS_STATE_DRAIN_AGENT,
Simon Horman4d2eab62015-04-23 14:51:26 +09003533 SRV_STATS_STATE_NO_CHECK,
3534
3535 SRV_STATS_STATE_COUNT, /* Must be last */
3536};
3537
Simon Horman837bfa72015-04-23 14:51:27 +09003538enum srv_stats_colour {
3539 SRV_STATS_COLOUR_DOWN = 0,
3540 SRV_STATS_COLOUR_GOING_UP,
3541 SRV_STATS_COLOUR_GOING_DOWN,
3542 SRV_STATS_COLOUR_UP,
3543 SRV_STATS_COLOUR_NOLB,
3544 SRV_STATS_COLOUR_DRAINING,
3545 SRV_STATS_COLOUR_NO_CHECK,
3546
3547 SRV_STATS_COLOUR_COUNT, /* Must be last */
3548};
3549
3550static const char *srv_stats_colour_st[SRV_STATS_COLOUR_COUNT] = {
3551 [SRV_STATS_COLOUR_DOWN] = "down",
3552 [SRV_STATS_COLOUR_GOING_UP] = "going_up",
3553 [SRV_STATS_COLOUR_GOING_DOWN] = "going_down",
3554 [SRV_STATS_COLOUR_UP] = "up",
3555 [SRV_STATS_COLOUR_NOLB] = "nolb",
3556 [SRV_STATS_COLOUR_DRAINING] = "draining",
3557 [SRV_STATS_COLOUR_NO_CHECK] = "no_check",
3558};
3559
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003560/* Dumps a line for server <sv> and proxy <px> to the trash and uses the state
3561 * from stream interface <si>, stats flags <flags>, and server state <state>.
3562 * The caller is responsible for clearing the trash if needed. Returns non-zero
Simon Horman4d2eab62015-04-23 14:51:26 +09003563 * if it emits anything, zero otherwise.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003564 */
Simon Horman4d2eab62015-04-23 14:51:26 +09003565static int stats_dump_sv_stats(struct stream_interface *si, struct proxy *px, int flags, struct server *sv,
Simon Horman837bfa72015-04-23 14:51:27 +09003566 enum srv_stats_state state, enum srv_stats_colour colour)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003567{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003568 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau32091232014-05-16 13:52:00 +02003569 struct server *via, *ref;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003570 char str[INET6_ADDRSTRLEN];
3571 struct chunk src;
3572 int i;
Willy Tarreau91861262007-10-17 17:06:05 +02003573
Willy Tarreau32091232014-05-16 13:52:00 +02003574 /* we have "via" which is the tracked server as described in the configuration,
3575 * and "ref" which is the checked server and the end of the chain.
3576 */
3577 via = sv->track ? sv->track : sv;
3578 ref = via;
3579 while (ref->track)
3580 ref = ref->track;
3581
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003582 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Simon Horman4d2eab62015-04-23 14:51:26 +09003583 static char *srv_hlt_st[SRV_STATS_STATE_COUNT] = {
3584 [SRV_STATS_STATE_DOWN] = "DOWN",
3585 [SRV_STATS_STATE_DOWN_AGENT] = "DOWN (agent)",
3586 [SRV_STATS_STATE_GOING_UP] = "DN %d/%d &uarr;",
3587 [SRV_STATS_STATE_UP_GOING_DOWN] = "UP %d/%d &darr;",
3588 [SRV_STATS_STATE_UP] = "UP",
3589 [SRV_STATS_STATE_NOLB_GOING_DOWN] = "NOLB %d/%d &darr;",
3590 [SRV_STATS_STATE_NOLB] = "NOLB",
3591 [SRV_STATS_STATE_DRAIN_GOING_DOWN] = "DRAIN %d/%d &darr;",
3592 [SRV_STATS_STATE_DRAIN] = "DRAIN",
Simon Hormanb167b6b2015-04-23 14:51:29 +09003593 [SRV_STATS_STATE_DRAIN_AGENT] = "DRAIN (agent)",
Simon Horman4d2eab62015-04-23 14:51:26 +09003594 [SRV_STATS_STATE_NO_CHECK] = "<i>no check</i>",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003595 };
Willy Tarreau91861262007-10-17 17:06:05 +02003596
Willy Tarreaua0066dd2014-05-16 11:25:16 +02003597 if (sv->admin & SRV_ADMF_MAINT)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003598 chunk_appendf(&trash, "<tr class=\"maintain\">");
3599 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003600 chunk_appendf(&trash,
Simon Horman837bfa72015-04-23 14:51:27 +09003601 "<tr class=\"%s_%s\">",
3602 (sv->flags & SRV_F_BACKUP) ? "backup" : "active", srv_stats_colour_st[colour]);
Willy Tarreau91861262007-10-17 17:06:05 +02003603
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003604 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN))
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003605 chunk_appendf(&trash,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003606 "<td><input type=\"checkbox\" name=\"s\" value=\"%s\"></td>",
3607 sv->id);
Willy Tarreaub1356cf2008-12-07 16:06:43 +01003608
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003609 chunk_appendf(&trash,
3610 "<td class=ac><a name=\"%s/%s\"></a>%s"
3611 "<a class=lfsb href=\"#%s/%s\">%s</a>"
3612 "",
3613 px->id, sv->id,
3614 (flags & ST_SHLGNDS) ? "<u>" : "",
3615 px->id, sv->id, sv->id);
Willy Tarreau91861262007-10-17 17:06:05 +02003616
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003617 if (flags & ST_SHLGNDS) {
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003618 chunk_appendf(&trash, "<div class=tips>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003619
3620 switch (addr_to_str(&sv->addr, str, sizeof(str))) {
3621 case AF_INET:
3622 chunk_appendf(&trash, "IPv4: %s:%d, ", str, get_host_port(&sv->addr));
3623 break;
3624 case AF_INET6:
3625 chunk_appendf(&trash, "IPv6: [%s]:%d, ", str, get_host_port(&sv->addr));
3626 break;
3627 case AF_UNIX:
3628 chunk_appendf(&trash, "unix, ");
3629 break;
3630 case -1:
3631 chunk_appendf(&trash, "(%s), ", strerror(errno));
3632 break;
3633 default: /* address family not supported */
3634 break;
Willy Tarreau55bb8452007-10-17 18:44:57 +02003635 }
Willy Tarreau91861262007-10-17 17:06:05 +02003636
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003637 /* id */
3638 chunk_appendf(&trash, "id: %d", sv->puid);
Willy Tarreau91861262007-10-17 17:06:05 +02003639
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003640 /* cookie */
3641 if (sv->cookie) {
3642 chunk_appendf(&trash, ", cookie: '");
Willy Tarreau5031e6a2007-10-18 11:05:48 +02003643
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003644 chunk_initlen(&src, sv->cookie, 0, strlen(sv->cookie));
3645 chunk_htmlencode(&trash, &src);
Willy Tarreaub1356cf2008-12-07 16:06:43 +01003646
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003647 chunk_appendf(&trash, "'");
Cyril Bonté70be45d2010-10-12 00:14:35 +02003648 }
3649
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003650 chunk_appendf(&trash, "</div>");
Willy Tarreau55bb8452007-10-17 18:44:57 +02003651 }
Willy Tarreau91861262007-10-17 17:06:05 +02003652
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003653 chunk_appendf(&trash,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003654 /* queue : current, max, limit */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003655 "%s</td><td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003656 /* sessions rate : current, max, limit */
3657 "<td>%s</td><td>%s</td><td></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003658 "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003659 (flags & ST_SHLGNDS) ? "</u>" : "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003660 U2H(sv->nbpend), U2H(sv->counters.nbpend_max), LIM2A(sv->maxqueue, "-"),
3661 U2H(read_freq_ctr(&sv->sess_per_sec)), U2H(sv->counters.sps_max));
Willy Tarreau91861262007-10-17 17:06:05 +02003662
Willy Tarreau466c9b52012-12-23 02:25:03 +01003663
3664 chunk_appendf(&trash,
3665 /* sessions: current, max, limit, total */
3666 "<td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003667 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01003668 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
3669 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003670 U2H(sv->cur_sess), U2H(sv->counters.cur_sess_max), LIM2A(sv->maxconn, "-"),
3671 U2H(sv->counters.cum_sess),
3672 U2H(sv->counters.cum_sess));
Willy Tarreau91861262007-10-17 17:06:05 +02003673
Willy Tarreau466c9b52012-12-23 02:25:03 +01003674 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
3675 if (px->mode == PR_MODE_HTTP) {
3676 unsigned long long tot;
3677 for (tot = i = 0; i < 6; i++)
3678 tot += sv->counters.p.http.rsp[i];
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003679
Willy Tarreau466c9b52012-12-23 02:25:03 +01003680 chunk_appendf(&trash,
3681 "<tr><th>Cum. HTTP responses:</th><td>%s</td></tr>"
3682 "<tr><th>- HTTP 1xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
3683 "<tr><th>- HTTP 2xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
3684 "<tr><th>- HTTP 3xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
3685 "<tr><th>- HTTP 4xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
3686 "<tr><th>- HTTP 5xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
3687 "<tr><th>- other responses:</th><td>%s</td><td>(%d%%)</td></tr>"
3688 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003689 U2H(tot),
3690 U2H(sv->counters.p.http.rsp[1]), tot ? (int)(100*sv->counters.p.http.rsp[1] / tot) : 0,
3691 U2H(sv->counters.p.http.rsp[2]), tot ? (int)(100*sv->counters.p.http.rsp[2] / tot) : 0,
3692 U2H(sv->counters.p.http.rsp[3]), tot ? (int)(100*sv->counters.p.http.rsp[3] / tot) : 0,
3693 U2H(sv->counters.p.http.rsp[4]), tot ? (int)(100*sv->counters.p.http.rsp[4] / tot) : 0,
3694 U2H(sv->counters.p.http.rsp[5]), tot ? (int)(100*sv->counters.p.http.rsp[5] / tot) : 0,
3695 U2H(sv->counters.p.http.rsp[0]), tot ? (int)(100*sv->counters.p.http.rsp[0] / tot) : 0);
Willy Tarreau91861262007-10-17 17:06:05 +02003696 }
Willy Tarreau91861262007-10-17 17:06:05 +02003697
Willy Tarreauf5b1cc32014-06-17 12:20:59 +02003698 chunk_appendf(&trash, "<tr><th colspan=3>Avg over last 1024 success. conn.</th></tr>");
3699 chunk_appendf(&trash, "<tr><th>- Queue time:</th><td>%s</td><td>ms</td></tr>", U2H(swrate_avg(sv->counters.q_time, TIME_STATS_SAMPLES)));
3700 chunk_appendf(&trash, "<tr><th>- Connect time:</th><td>%s</td><td>ms</td></tr>", U2H(swrate_avg(sv->counters.c_time, TIME_STATS_SAMPLES)));
3701 if (px->mode == PR_MODE_HTTP)
3702 chunk_appendf(&trash, "<tr><th>- Response time:</th><td>%s</td><td>ms</td></tr>", U2H(swrate_avg(sv->counters.d_time, TIME_STATS_SAMPLES)));
3703 chunk_appendf(&trash, "<tr><th>- Total time:</th><td>%s</td><td>ms</td></tr>", U2H(swrate_avg(sv->counters.t_time, TIME_STATS_SAMPLES)));
3704
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003705 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01003706 "</table></div></u></td>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05003707 /* sessions: lbtot, last */
3708 "<td>%s</td><td>%s</td>",
3709 U2H(sv->counters.cum_lbconn),
3710 human_time(srv_lastsession(sv), 1));
Willy Tarreau91861262007-10-17 17:06:05 +02003711
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003712 chunk_appendf(&trash,
3713 /* bytes : in, out */
3714 "<td>%s</td><td>%s</td>"
3715 /* denied: req, resp */
3716 "<td></td><td>%s</td>"
3717 /* errors : request, connect */
3718 "<td></td><td>%s</td>"
3719 /* errors : response */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003720 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003721 /* warnings: retries, redispatches */
3722 "<td>%lld</td><td>%lld</td>"
3723 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003724 U2H(sv->counters.bytes_in), U2H(sv->counters.bytes_out),
3725 U2H(sv->counters.failed_secu),
3726 U2H(sv->counters.failed_conns),
3727 U2H(sv->counters.failed_resp),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003728 sv->counters.cli_aborts,
3729 sv->counters.srv_aborts,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003730 sv->counters.retries, sv->counters.redispatches);
3731
3732 /* status, lest check */
3733 chunk_appendf(&trash, "<td class=ac>");
3734
Willy Tarreau20125212014-05-13 19:44:56 +02003735 if (sv->admin & SRV_ADMF_MAINT) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003736 chunk_appendf(&trash, "%s ", human_time(now.tv_sec - sv->last_change, 1));
3737 chunk_appendf(&trash, "MAINT");
3738 }
Simon Horman0766e442014-11-12 15:55:54 +09003739 else if ((ref->agent.state & CHK_ST_ENABLED) && !(sv->agent.health) && (ref->state == SRV_ST_STOPPED)) {
Willy Tarreaucf2924b2014-05-23 12:15:15 +02003740 chunk_appendf(&trash, "%s ", human_time(now.tv_sec - ref->last_change, 1));
Willy Tarreauac497072014-05-29 01:04:35 +02003741 /* DOWN (agent) */
3742 chunk_appendf(&trash, srv_hlt_st[1], "GCC: your -Werror=format-security is bogus, annoying, and hides real bugs, I don't thank you, really!");
Willy Tarreaucf2924b2014-05-23 12:15:15 +02003743 }
Willy Tarreauff5ae352013-12-11 20:36:34 +01003744 else if (ref->check.state & CHK_ST_ENABLED) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003745 chunk_appendf(&trash, "%s ", human_time(now.tv_sec - ref->last_change, 1));
3746 chunk_appendf(&trash,
3747 srv_hlt_st[state],
Willy Tarreau892337c2014-05-13 23:41:20 +02003748 (ref->state != SRV_ST_STOPPED) ? (ref->check.health - ref->check.rise + 1) : (ref->check.health),
3749 (ref->state != SRV_ST_STOPPED) ? (ref->check.fall) : (ref->check.rise));
Willy Tarreau55bb8452007-10-17 18:44:57 +02003750 }
Willy Tarreau91861262007-10-17 17:06:05 +02003751
Willy Tarreaucf2924b2014-05-23 12:15:15 +02003752 if ((sv->state == SRV_ST_STOPPED) &&
3753 ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) && !(sv->agent.health)) {
3754 chunk_appendf(&trash,
3755 "</td><td class=ac><u> %s%s",
3756 (sv->agent.state & CHK_ST_INPROGRESS) ? "* " : "",
3757 get_check_status_info(sv->agent.status));
3758
3759 if (sv->agent.status >= HCHK_STATUS_L57DATA)
3760 chunk_appendf(&trash, "/%d", sv->agent.code);
3761
3762 if (sv->agent.status >= HCHK_STATUS_CHECKED && sv->agent.duration >= 0)
3763 chunk_appendf(&trash, " in %lums", sv->agent.duration);
3764
3765 chunk_appendf(&trash, "<div class=tips>%s",
3766 get_check_status_description(sv->agent.status));
3767 if (*sv->agent.desc) {
3768 chunk_appendf(&trash, ": ");
3769 chunk_initlen(&src, sv->agent.desc, 0, strlen(sv->agent.desc));
3770 chunk_htmlencode(&trash, &src);
3771 }
3772 chunk_appendf(&trash, "</div></u>");
3773 }
3774 else if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) {
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003775 chunk_appendf(&trash,
3776 "</td><td class=ac><u> %s%s",
Willy Tarreau2c115e52013-12-11 19:41:16 +01003777 (sv->check.state & CHK_ST_INPROGRESS) ? "* " : "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003778 get_check_status_info(sv->check.status));
3779
3780 if (sv->check.status >= HCHK_STATUS_L57DATA)
3781 chunk_appendf(&trash, "/%d", sv->check.code);
3782
3783 if (sv->check.status >= HCHK_STATUS_CHECKED && sv->check.duration >= 0)
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003784 chunk_appendf(&trash, " in %lums", sv->check.duration);
3785
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003786 chunk_appendf(&trash, "<div class=tips>%s",
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003787 get_check_status_description(sv->check.status));
3788 if (*sv->check.desc) {
3789 chunk_appendf(&trash, ": ");
3790 chunk_initlen(&src, sv->check.desc, 0, strlen(sv->check.desc));
3791 chunk_htmlencode(&trash, &src);
3792 }
3793 chunk_appendf(&trash, "</div></u>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003794 }
3795 else
3796 chunk_appendf(&trash, "</td><td>");
3797
3798 chunk_appendf(&trash,
3799 /* weight */
3800 "</td><td class=ac>%d</td>"
3801 /* act, bck */
3802 "<td class=ac>%s</td><td class=ac>%s</td>"
3803 "",
3804 (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreauc93cd162014-05-13 15:54:22 +02003805 (sv->flags & SRV_F_BACKUP) ? "-" : "Y",
3806 (sv->flags & SRV_F_BACKUP) ? "Y" : "-");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003807
3808 /* check failures: unique, fatal, down time */
Willy Tarreauff5ae352013-12-11 20:36:34 +01003809 if (sv->check.state & CHK_ST_ENABLED) {
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003810 chunk_appendf(&trash, "<td><u>%lld", ref->counters.failed_checks);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003811
3812 if (ref->observe)
3813 chunk_appendf(&trash, "/%lld", ref->counters.failed_hana);
3814
3815 chunk_appendf(&trash,
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003816 "<div class=tips>Failed Health Checks%s</div></u></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003817 "<td>%lld</td><td>%s</td>"
3818 "",
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003819 ref->observe ? "/Health Analyses" : "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003820 ref->counters.down_trans, human_time(srv_downtime(sv), 1));
3821 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +02003822 else if (!(sv->admin & SRV_ADMF_FMAINT) && sv != ref) {
3823 /* tracking a server */
3824 chunk_appendf(&trash,
3825 "<td class=ac colspan=3><a class=lfsb href=\"#%s/%s\">via %s/%s</a></td>",
Willy Tarreau32091232014-05-16 13:52:00 +02003826 via->proxy->id, via->id, via->proxy->id, via->id);
Willy Tarreauf4659942013-11-28 10:50:06 +01003827 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003828 else
3829 chunk_appendf(&trash, "<td colspan=3></td>");
3830
3831 /* throttle */
Willy Tarreau892337c2014-05-13 23:41:20 +02003832 if (sv->state == SRV_ST_STARTING && !server_is_draining(sv))
Willy Tarreaud32c3992013-11-21 15:30:45 +01003833 chunk_appendf(&trash, "<td class=ac>%d %%</td></tr>\n", server_throttle_rate(sv));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003834 else
3835 chunk_appendf(&trash, "<td class=ac>-</td></tr>\n");
3836 }
3837 else { /* CSV mode */
Thierry FOURNIERbe1ccae2015-05-28 16:02:01 +02003838 struct chunk *out = get_trash_chunk();
Simon Horman4d2eab62015-04-23 14:51:26 +09003839 static char *srv_hlt_st[SRV_STATS_STATE_COUNT] = {
3840 [SRV_STATS_STATE_DOWN] = "DOWN,",
3841 [SRV_STATS_STATE_DOWN_AGENT] = "DOWN (agent),",
3842 [SRV_STATS_STATE_GOING_UP] = "DOWN %d/%d,",
3843 [SRV_STATS_STATE_UP_GOING_DOWN] = "UP %d/%d,",
3844 [SRV_STATS_STATE_UP] = "UP,",
3845 [SRV_STATS_STATE_NOLB_GOING_DOWN] = "NOLB %d/%d,",
3846 [SRV_STATS_STATE_NOLB] = "NOLB,",
3847 [SRV_STATS_STATE_DRAIN_GOING_DOWN] = "DRAIN %d/%d,",
3848 [SRV_STATS_STATE_DRAIN] = "DRAIN,",
Raghu Udiyar0d6b7a42016-02-05 22:30:11 +05303849 [SRV_STATS_STATE_DRAIN_AGENT] = "DRAIN (agent),",
Simon Horman4d2eab62015-04-23 14:51:26 +09003850 [SRV_STATS_STATE_NO_CHECK] = "no check,"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003851 };
3852
3853 chunk_appendf(&trash,
3854 /* pxid, name */
3855 "%s,%s,"
3856 /* queue : current, max */
3857 "%d,%d,"
Willy Tarreauf522f3d2014-02-10 22:22:49 +01003858 /* sessions : current, max, limit, total */
3859 "%d,%d,%s,%lld,"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003860 /* bytes : in, out */
3861 "%lld,%lld,"
3862 /* denied: req, resp */
3863 ",%lld,"
3864 /* errors : request, connect, response */
3865 ",%lld,%lld,"
3866 /* warnings: retries, redispatches */
3867 "%lld,%lld,"
3868 "",
3869 px->id, sv->id,
3870 sv->nbpend, sv->counters.nbpend_max,
Willy Tarreau56adcf22012-12-23 18:00:29 +01003871 sv->cur_sess, sv->counters.cur_sess_max, LIM2A(sv->maxconn, ""), sv->counters.cum_sess,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003872 sv->counters.bytes_in, sv->counters.bytes_out,
3873 sv->counters.failed_secu,
3874 sv->counters.failed_conns, sv->counters.failed_resp,
3875 sv->counters.retries, sv->counters.redispatches);
3876
3877 /* status */
Willy Tarreaua0066dd2014-05-16 11:25:16 +02003878 if (sv->admin & SRV_ADMF_IMAINT)
Willy Tarreau32091232014-05-16 13:52:00 +02003879 chunk_appendf(&trash, "MAINT (via %s/%s),", via->proxy->id, via->id);
Willy Tarreaua0066dd2014-05-16 11:25:16 +02003880 else if (sv->admin & SRV_ADMF_MAINT)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003881 chunk_appendf(&trash, "MAINT,");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003882 else
3883 chunk_appendf(&trash,
3884 srv_hlt_st[state],
Willy Tarreau892337c2014-05-13 23:41:20 +02003885 (ref->state != SRV_ST_STOPPED) ? (ref->check.health - ref->check.rise + 1) : (ref->check.health),
3886 (ref->state != SRV_ST_STOPPED) ? (ref->check.fall) : (ref->check.rise));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003887
3888 chunk_appendf(&trash,
3889 /* weight, active, backup */
3890 "%d,%d,%d,"
3891 "",
3892 (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
Willy Tarreauc93cd162014-05-13 15:54:22 +02003893 (sv->flags & SRV_F_BACKUP) ? 0 : 1,
3894 (sv->flags & SRV_F_BACKUP) ? 1 : 0);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003895
3896 /* check failures: unique, fatal; last change, total downtime */
Willy Tarreauff5ae352013-12-11 20:36:34 +01003897 if (sv->check.state & CHK_ST_ENABLED)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003898 chunk_appendf(&trash,
3899 "%lld,%lld,%d,%d,",
3900 sv->counters.failed_checks, sv->counters.down_trans,
3901 (int)(now.tv_sec - sv->last_change), srv_downtime(sv));
3902 else
3903 chunk_appendf(&trash, ",,,,");
3904
3905 /* queue limit, pid, iid, sid, */
3906 chunk_appendf(&trash,
3907 "%s,"
3908 "%d,%d,%d,",
Willy Tarreau56adcf22012-12-23 18:00:29 +01003909 LIM2A(sv->maxqueue, ""),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003910 relative_pid, px->uuid, sv->puid);
3911
3912 /* throttle */
Willy Tarreau892337c2014-05-13 23:41:20 +02003913 if (sv->state == SRV_ST_STARTING && !server_is_draining(sv))
Willy Tarreaud32c3992013-11-21 15:30:45 +01003914 chunk_appendf(&trash, "%d", server_throttle_rate(sv));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003915
3916 /* sessions: lbtot */
3917 chunk_appendf(&trash, ",%lld,", sv->counters.cum_lbconn);
3918
3919 /* tracked */
3920 if (sv->track)
3921 chunk_appendf(&trash, "%s/%s,",
3922 sv->track->proxy->id, sv->track->id);
3923 else
3924 chunk_appendf(&trash, ",");
3925
3926 /* type */
3927 chunk_appendf(&trash, "%d,", STATS_TYPE_SV);
3928
3929 /* rate */
3930 chunk_appendf(&trash, "%u,,%u,",
3931 read_freq_ctr(&sv->sess_per_sec),
3932 sv->counters.sps_max);
3933
Willy Tarreauff5ae352013-12-11 20:36:34 +01003934 if (sv->check.state & CHK_ST_ENABLED) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003935 /* check_status */
Thierry FOURNIERbe1ccae2015-05-28 16:02:01 +02003936 chunk_appendf(&trash, "%s,", csv_enc(get_check_status_info(sv->check.status), 1, out));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003937
3938 /* check_code */
3939 if (sv->check.status >= HCHK_STATUS_L57DATA)
3940 chunk_appendf(&trash, "%u,", sv->check.code);
3941 else
3942 chunk_appendf(&trash, ",");
3943
3944 /* check_duration */
3945 if (sv->check.status >= HCHK_STATUS_CHECKED)
3946 chunk_appendf(&trash, "%lu,", sv->check.duration);
3947 else
3948 chunk_appendf(&trash, ",");
3949
3950 }
3951 else
3952 chunk_appendf(&trash, ",,,");
3953
3954 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
3955 if (px->mode == PR_MODE_HTTP) {
3956 for (i=1; i<6; i++)
3957 chunk_appendf(&trash, "%lld,", sv->counters.p.http.rsp[i]);
3958
3959 chunk_appendf(&trash, "%lld,", sv->counters.p.http.rsp[0]);
3960 }
3961 else
3962 chunk_appendf(&trash, ",,,,,,");
3963
3964 /* failed health analyses */
3965 chunk_appendf(&trash, "%lld,", sv->counters.failed_hana);
3966
3967 /* requests : req_rate, req_rate_max, req_tot, */
3968 chunk_appendf(&trash, ",,,");
3969
3970 /* errors: cli_aborts, srv_aborts */
3971 chunk_appendf(&trash, "%lld,%lld,",
3972 sv->counters.cli_aborts, sv->counters.srv_aborts);
3973
3974 /* compression: in, out, bypassed, comp_rsp */
3975 chunk_appendf(&trash, ",,,,");
3976
Willy Tarreauf522f3d2014-02-10 22:22:49 +01003977 /* lastsess */
3978 chunk_appendf(&trash, "%d,", srv_lastsession(sv));
3979
Willy Tarreaua28df3e2014-06-16 16:40:14 +02003980 /* capture of last check and agent statuses */
Thierry FOURNIERbe1ccae2015-05-28 16:02:01 +02003981 chunk_appendf(&trash, "%s,", ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) ? csv_enc(cstr(sv->check.desc), 1, out) : "");
3982 chunk_appendf(&trash, "%s,", ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) ? csv_enc(cstr(sv->agent.desc), 1, out) : "");
Willy Tarreaua28df3e2014-06-16 16:40:14 +02003983
Willy Tarreauf5b1cc32014-06-17 12:20:59 +02003984 /* qtime, ctime, rtime, ttime, */
3985 chunk_appendf(&trash, "%u,%u,%u,%u,",
3986 swrate_avg(sv->counters.q_time, TIME_STATS_SAMPLES),
3987 swrate_avg(sv->counters.c_time, TIME_STATS_SAMPLES),
3988 swrate_avg(sv->counters.d_time, TIME_STATS_SAMPLES),
3989 swrate_avg(sv->counters.t_time, TIME_STATS_SAMPLES));
3990
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003991 /* finish with EOL */
3992 chunk_appendf(&trash, "\n");
3993 }
3994 return 1;
3995}
3996
3997/* Dumps a line for backend <px> to the trash for and uses the state from stream
3998 * interface <si> and stats flags <flags>. The caller is responsible for clearing
3999 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
4000 */
4001static int stats_dump_be_stats(struct stream_interface *si, struct proxy *px, int flags)
4002{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004003 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004004 struct chunk src;
4005 int i;
4006
4007 if (!(px->cap & PR_CAP_BE))
4008 return 0;
4009
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004010 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_BE)))
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004011 return 0;
4012
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004013 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004014 chunk_appendf(&trash, "<tr class=\"backend\">");
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004015 if (px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004016 /* Column sub-heading for Enable or Disable server */
4017 chunk_appendf(&trash, "<td></td>");
4018 }
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01004019 chunk_appendf(&trash,
4020 "<td class=ac>"
4021 /* name */
4022 "%s<a name=\"%s/Backend\"></a>"
4023 "<a class=lfsb href=\"#%s/Backend\">Backend</a>"
4024 "",
4025 (flags & ST_SHLGNDS)?"<u>":"",
4026 px->id, px->id);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004027
4028 if (flags & ST_SHLGNDS) {
4029 /* balancing */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02004030 chunk_appendf(&trash, "<div class=tips>balancing: %s",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004031 backend_lb_algo_str(px->lbprm.algo & BE_LB_ALGO));
4032
4033 /* cookie */
4034 if (px->cookie_name) {
4035 chunk_appendf(&trash, ", cookie: '");
4036 chunk_initlen(&src, px->cookie_name, 0, strlen(px->cookie_name));
4037 chunk_htmlencode(&trash, &src);
4038 chunk_appendf(&trash, "'");
4039 }
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01004040 chunk_appendf(&trash, "</div>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004041 }
4042
4043 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01004044 "%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004045 /* queue : current, max */
4046 "<td>%s</td><td>%s</td><td></td>"
4047 /* sessions rate : current, max, limit */
4048 "<td>%s</td><td>%s</td><td></td>"
4049 "",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004050 (flags & ST_SHLGNDS)?"</u>":"",
Willy Tarreau56adcf22012-12-23 18:00:29 +01004051 U2H(px->nbpend) /* or px->totpend ? */, U2H(px->be_counters.nbpend_max),
4052 U2H(read_freq_ctr(&px->be_sess_per_sec)), U2H(px->be_counters.sps_max));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004053
4054 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01004055 /* sessions: current, max, limit, total */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004056 "<td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02004057 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01004058 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004059 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01004060 U2H(px->beconn), U2H(px->be_counters.conn_max), U2H(px->fullconn),
4061 U2H(px->be_counters.cum_conn),
4062 U2H(px->be_counters.cum_conn));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004063
Willy Tarreau466c9b52012-12-23 02:25:03 +01004064 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004065 if (px->mode == PR_MODE_HTTP) {
Willy Tarreau466c9b52012-12-23 02:25:03 +01004066 chunk_appendf(&trash,
4067 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
4068 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
4069 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
4070 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
4071 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
4072 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
4073 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
4074 "<tr><th>- other responses:</th><td>%s</td></tr>"
4075 "<tr><th>Intercepted requests:</th><td>%s</td></tr>"
Willy Tarreauf5b1cc32014-06-17 12:20:59 +02004076 "<tr><th colspan=3>Avg over last 1024 success. conn.</th></tr>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01004077 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01004078 U2H(px->be_counters.p.http.cum_req),
4079 U2H(px->be_counters.p.http.rsp[1]),
4080 U2H(px->be_counters.p.http.rsp[2]),
4081 U2H(px->be_counters.p.http.comp_rsp),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004082 px->be_counters.p.http.rsp[2] ?
Willy Tarreau466c9b52012-12-23 02:25:03 +01004083 (int)(100*px->be_counters.p.http.comp_rsp/px->be_counters.p.http.rsp[2]) : 0,
Willy Tarreau56adcf22012-12-23 18:00:29 +01004084 U2H(px->be_counters.p.http.rsp[3]),
4085 U2H(px->be_counters.p.http.rsp[4]),
4086 U2H(px->be_counters.p.http.rsp[5]),
4087 U2H(px->be_counters.p.http.rsp[0]),
4088 U2H(px->be_counters.intercepted_req));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004089 }
4090
Willy Tarreauf5b1cc32014-06-17 12:20:59 +02004091 chunk_appendf(&trash, "<tr><th>- Queue time:</th><td>%s</td><td>ms</td></tr>", U2H(swrate_avg(px->be_counters.q_time, TIME_STATS_SAMPLES)));
4092 chunk_appendf(&trash, "<tr><th>- Connect time:</th><td>%s</td><td>ms</td></tr>", U2H(swrate_avg(px->be_counters.c_time, TIME_STATS_SAMPLES)));
4093 if (px->mode == PR_MODE_HTTP)
4094 chunk_appendf(&trash, "<tr><th>- Response time:</th><td>%s</td><td>ms</td></tr>", U2H(swrate_avg(px->be_counters.d_time, TIME_STATS_SAMPLES)));
4095 chunk_appendf(&trash, "<tr><th>- Total time:</th><td>%s</td><td>ms</td></tr>", U2H(swrate_avg(px->be_counters.t_time, TIME_STATS_SAMPLES)));
4096
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004097 chunk_appendf(&trash,
Willy Tarreau466c9b52012-12-23 02:25:03 +01004098 "</table></div></u></td>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05004099 /* sessions: lbtot, last */
4100 "<td>%s</td><td>%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004101 /* bytes: in */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01004102 "<td>%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004103 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01004104 U2H(px->be_counters.cum_lbconn),
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05004105 human_time(be_lastsession(px), 1),
Willy Tarreau56adcf22012-12-23 18:00:29 +01004106 U2H(px->be_counters.bytes_in));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004107
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004108 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01004109 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
Willy Tarreau4e5d58e2015-07-04 14:35:15 +02004110 "<td>%s%s<div class=tips><table class=det>"
4111 "<tr><th>Response bytes in:</th><td>%s</td></tr>"
4112 "<tr><th>Compression in:</th><td>%s</td></tr>"
4113 "<tr><th>Compression out:</th><td>%s</td><td>(%d%%)</td></tr>"
4114 "<tr><th>Compression bypass:</th><td>%s</td></tr>"
4115 "<tr><th>Total bytes saved:</th><td>%s</td><td>(%d%%)</td></tr>"
4116 "</table></div>%s</td>",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004117 (px->be_counters.comp_in || px->be_counters.comp_byp) ? "<u>":"",
Willy Tarreau56adcf22012-12-23 18:00:29 +01004118 U2H(px->be_counters.bytes_out),
Willy Tarreau4e5d58e2015-07-04 14:35:15 +02004119 U2H(px->be_counters.bytes_out),
4120 U2H(px->be_counters.comp_in),
4121 U2H(px->be_counters.comp_out),
4122 px->be_counters.comp_in ? (int)(px->be_counters.comp_out * 100 / px->be_counters.comp_in) : 0,
4123 U2H(px->be_counters.comp_byp),
4124 U2H(px->be_counters.comp_in - px->be_counters.comp_out),
4125 px->be_counters.bytes_out ? (int)((px->be_counters.comp_in - px->be_counters.comp_out) * 100 / px->be_counters.bytes_out) : 0,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004126 (px->be_counters.comp_in || px->be_counters.comp_byp) ? "</u>":"");
4127
4128 chunk_appendf(&trash,
4129 /* denied: req, resp */
4130 "<td>%s</td><td>%s</td>"
4131 /* errors : request, connect */
4132 "<td></td><td>%s</td>"
4133 /* errors : response */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02004134 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004135 /* warnings: retries, redispatches */
4136 "<td>%lld</td><td>%lld</td>"
4137 /* backend status: reflect backend status (up/down): we display UP
4138 * if the backend has known working servers or if it has no server at
4139 * all (eg: for stats). Then we display the total weight, number of
4140 * active and backups. */
4141 "<td class=ac>%s %s</td><td class=ac>&nbsp;</td><td class=ac>%d</td>"
4142 "<td class=ac>%d</td><td class=ac>%d</td>"
4143 "",
Willy Tarreau56adcf22012-12-23 18:00:29 +01004144 U2H(px->be_counters.denied_req), U2H(px->be_counters.denied_resp),
4145 U2H(px->be_counters.failed_conns),
4146 U2H(px->be_counters.failed_resp),
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004147 px->be_counters.cli_aborts,
4148 px->be_counters.srv_aborts,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004149 px->be_counters.retries, px->be_counters.redispatches,
4150 human_time(now.tv_sec - px->last_change, 1),
4151 (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" :
4152 "<font color=\"red\"><b>DOWN</b></font>",
4153 (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
4154 px->srv_act, px->srv_bck);
4155
4156 chunk_appendf(&trash,
4157 /* rest of backend: nothing, down transitions, total downtime, throttle */
4158 "<td class=ac>&nbsp;</td><td>%d</td>"
4159 "<td>%s</td>"
4160 "<td></td>"
4161 "</tr>",
4162 px->down_trans,
4163 px->srv?human_time(be_downtime(px), 1):"&nbsp;");
4164 }
4165 else { /* CSV mode */
4166 chunk_appendf(&trash,
4167 /* pxid, name */
4168 "%s,BACKEND,"
4169 /* queue : current, max */
4170 "%d,%d,"
Willy Tarreauf522f3d2014-02-10 22:22:49 +01004171 /* sessions : current, max, limit, total */
4172 "%d,%d,%d,%lld,"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004173 /* bytes : in, out */
4174 "%lld,%lld,"
4175 /* denied: req, resp */
4176 "%lld,%lld,"
4177 /* errors : request, connect, response */
4178 ",%lld,%lld,"
4179 /* warnings: retries, redispatches */
4180 "%lld,%lld,"
4181 /* backend status: reflect backend status (up/down): we display UP
4182 * if the backend has known working servers or if it has no server at
4183 * all (eg: for stats). Then we display the total weight, number of
4184 * active and backups. */
4185 "%s,"
4186 "%d,%d,%d,"
4187 /* rest of backend: nothing, down transitions, last change, total downtime */
4188 ",%d,%d,%d,,"
4189 /* pid, iid, sid, throttle, lbtot, tracked, type */
4190 "%d,%d,0,,%lld,,%d,"
4191 /* rate, rate_lim, rate_max, */
4192 "%u,,%u,"
4193 /* check_status, check_code, check_duration */
4194 ",,,",
4195 px->id,
4196 px->nbpend /* or px->totpend ? */, px->be_counters.nbpend_max,
4197 px->beconn, px->be_counters.conn_max, px->fullconn, px->be_counters.cum_conn,
4198 px->be_counters.bytes_in, px->be_counters.bytes_out,
4199 px->be_counters.denied_req, px->be_counters.denied_resp,
4200 px->be_counters.failed_conns, px->be_counters.failed_resp,
4201 px->be_counters.retries, px->be_counters.redispatches,
4202 (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" : "DOWN",
4203 (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv,
4204 px->srv_act, px->srv_bck,
4205 px->down_trans, (int)(now.tv_sec - px->last_change),
4206 px->srv?be_downtime(px):0,
4207 relative_pid, px->uuid,
4208 px->be_counters.cum_lbconn, STATS_TYPE_BE,
4209 read_freq_ctr(&px->be_sess_per_sec),
4210 px->be_counters.sps_max);
4211
4212 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
4213 if (px->mode == PR_MODE_HTTP) {
4214 for (i=1; i<6; i++)
4215 chunk_appendf(&trash, "%lld,", px->be_counters.p.http.rsp[i]);
4216 chunk_appendf(&trash, "%lld,", px->be_counters.p.http.rsp[0]);
4217 }
4218 else
4219 chunk_appendf(&trash, ",,,,,,");
4220
4221 /* failed health analyses */
4222 chunk_appendf(&trash, ",");
4223
4224 /* requests : req_rate, req_rate_max, req_tot, */
4225 chunk_appendf(&trash, ",,,");
4226
4227 /* errors: cli_aborts, srv_aborts */
4228 chunk_appendf(&trash, "%lld,%lld,",
4229 px->be_counters.cli_aborts, px->be_counters.srv_aborts);
4230
4231 /* compression: in, out, bypassed */
4232 chunk_appendf(&trash, "%lld,%lld,%lld,",
4233 px->be_counters.comp_in, px->be_counters.comp_out, px->be_counters.comp_byp);
4234
4235 /* compression: comp_rsp */
4236 chunk_appendf(&trash, "%lld,", px->be_counters.p.http.comp_rsp);
4237
Willy Tarreaua28df3e2014-06-16 16:40:14 +02004238 /* lastsess, last_chk, last_agt, */
4239 chunk_appendf(&trash, "%d,,,", be_lastsession(px));
Willy Tarreauf522f3d2014-02-10 22:22:49 +01004240
Willy Tarreauf5b1cc32014-06-17 12:20:59 +02004241 /* qtime, ctime, rtime, ttime, */
4242 chunk_appendf(&trash, "%u,%u,%u,%u,",
4243 swrate_avg(px->be_counters.q_time, TIME_STATS_SAMPLES),
4244 swrate_avg(px->be_counters.c_time, TIME_STATS_SAMPLES),
4245 swrate_avg(px->be_counters.d_time, TIME_STATS_SAMPLES),
4246 swrate_avg(px->be_counters.t_time, TIME_STATS_SAMPLES));
4247
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004248 /* finish with EOL */
4249 chunk_appendf(&trash, "\n");
4250 }
4251 return 1;
4252}
4253
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004254/* Dumps the HTML table header for proxy <px> to the trash for and uses the state from
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004255 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004256 * for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004257 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004258static void stats_dump_html_px_hdr(struct stream_interface *si, struct proxy *px, struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004259{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004260 struct appctx *appctx = __objt_appctx(si->end);
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004261 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
4262
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004263 if (px->cap & PR_CAP_BE && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004264 /* A form to enable/disable this proxy servers */
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004265
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004266 /* 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 +02004267 scope_txt[0] = 0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004268 if (appctx->ctx.stats.scope_len) {
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004269 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau4e4292b2014-11-28 12:18:45 +01004270 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 +01004271 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004272 }
4273
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004274 chunk_appendf(&trash,
Jeff Buchbinder2dbbf4d2014-08-29 15:10:08 -05004275 "<form method=\"post\">");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004276 }
4277
4278 /* print a new table */
4279 chunk_appendf(&trash,
4280 "<table class=\"tbl\" width=\"100%%\">\n"
4281 "<tr class=\"titre\">"
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01004282 "<th class=\"pxname\" width=\"10%%\">");
4283
4284 chunk_appendf(&trash,
4285 "<a name=\"%s\"></a>%s"
4286 "<a class=px href=\"#%s\">%s</a>",
4287 px->id,
4288 (uri->flags & ST_SHLGNDS) ? "<u>":"",
4289 px->id, px->id);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004290
4291 if (uri->flags & ST_SHLGNDS) {
4292 /* cap, mode, id */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02004293 chunk_appendf(&trash, "<div class=tips>cap: %s, mode: %s, id: %d",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004294 proxy_cap_str(px->cap), proxy_mode_str(px->mode),
4295 px->uuid);
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01004296 chunk_appendf(&trash, "</div>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004297 }
4298
4299 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01004300 "%s</th>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004301 "<th class=\"%s\" width=\"90%%\">%s</th>"
4302 "</tr>\n"
4303 "</table>\n"
4304 "<table class=\"tbl\" width=\"100%%\">\n"
4305 "<tr class=\"titre\">",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004306 (uri->flags & ST_SHLGNDS) ? "</u>":"",
4307 px->desc ? "desc" : "empty", px->desc ? px->desc : "");
4308
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004309 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004310 /* Column heading for Enable or Disable server */
4311 chunk_appendf(&trash, "<th rowspan=2 width=1></th>");
Willy Tarreau91861262007-10-17 17:06:05 +02004312 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004313
4314 chunk_appendf(&trash,
4315 "<th rowspan=2></th>"
4316 "<th colspan=3>Queue</th>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05004317 "<th colspan=3>Session rate</th><th colspan=6>Sessions</th>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004318 "<th colspan=2>Bytes</th><th colspan=2>Denied</th>"
4319 "<th colspan=3>Errors</th><th colspan=2>Warnings</th>"
4320 "<th colspan=9>Server</th>"
4321 "</tr>\n"
4322 "<tr class=\"titre\">"
4323 "<th>Cur</th><th>Max</th><th>Limit</th>"
4324 "<th>Cur</th><th>Max</th><th>Limit</th><th>Cur</th><th>Max</th>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05004325 "<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 +01004326 "<th>Req</th><th>Resp</th><th>Req</th><th>Conn</th>"
4327 "<th>Resp</th><th>Retr</th><th>Redis</th>"
4328 "<th>Status</th><th>LastChk</th><th>Wght</th><th>Act</th>"
4329 "<th>Bck</th><th>Chk</th><th>Dwn</th><th>Dwntme</th>"
4330 "<th>Thrtle</th>\n"
4331 "</tr>");
Willy Tarreau91861262007-10-17 17:06:05 +02004332}
4333
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004334/* Dumps the HTML table trailer for proxy <px> to the trash for and uses the state from
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004335 * stream interface <si>. The caller is responsible for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004336 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004337static void stats_dump_html_px_end(struct stream_interface *si, struct proxy *px)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004338{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004339 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004340 chunk_appendf(&trash, "</table>");
4341
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004342 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004343 /* close the form used to enable/disable this proxy servers */
4344 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004345 "Choose the action to perform on the checked servers : "
4346 "<select name=action>"
4347 "<option value=\"\"></option>"
Willy Tarreaued7df902014-05-22 18:04:49 +02004348 "<option value=\"ready\">Set state to READY</option>"
4349 "<option value=\"drain\">Set state to DRAIN</option>"
Marco Corte8c27bca2014-07-02 17:49:34 +02004350 "<option value=\"maint\">Set state to MAINT</option>"
Willy Tarreau248a60e2014-05-23 14:59:48 +02004351 "<option value=\"dhlth\">Health: disable checks</option>"
4352 "<option value=\"ehlth\">Health: enable checks</option>"
4353 "<option value=\"hrunn\">Health: force UP</option>"
4354 "<option value=\"hnolb\">Health: force NOLB</option>"
4355 "<option value=\"hdown\">Health: force DOWN</option>"
4356 "<option value=\"dagent\">Agent: disable checks</option>"
4357 "<option value=\"eagent\">Agent: enable checks</option>"
4358 "<option value=\"arunn\">Agent: force UP</option>"
4359 "<option value=\"adown\">Agent: force DOWN</option>"
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004360 "<option value=\"shutdown\">Kill Sessions</option>"
4361 "</select>"
4362 "<input type=\"hidden\" name=\"b\" value=\"#%d\">"
4363 "&nbsp;<input type=\"submit\" value=\"Apply\">"
4364 "</form>",
4365 px->uuid);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004366 }
4367
4368 chunk_appendf(&trash, "<p>\n");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004369}
Willy Tarreau91861262007-10-17 17:06:05 +02004370
4371/*
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004372 * Dumps statistics for a proxy. The output is sent to the stream interface's
4373 * input buffer. Returns 0 if it had to stop dumping data because of lack of
4374 * buffer space, or non-zero if everything completed. This function is used
4375 * both by the CLI and the HTTP entry points, and is able to dump the output
4376 * in HTML or CSV formats. If the later, <uri> must be NULL.
Willy Tarreau91861262007-10-17 17:06:05 +02004377 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004378static int stats_dump_proxy_to_buffer(struct stream_interface *si, struct proxy *px, struct uri_auth *uri)
Willy Tarreau91861262007-10-17 17:06:05 +02004379{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004380 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau87b09662015-04-03 00:22:06 +02004381 struct stream *s = si_strm(si);
Willy Tarreau2bb4a962014-11-28 11:11:05 +01004382 struct channel *rep = si_ic(si);
Willy Tarreau44267702011-10-28 15:35:33 +02004383 struct server *sv, *svs; /* server and server-state, server-state=server or server->track */
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004384 struct listener *l;
Willy Tarreau91861262007-10-17 17:06:05 +02004385
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004386 chunk_reset(&trash);
Willy Tarreau91861262007-10-17 17:06:05 +02004387
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004388 switch (appctx->ctx.stats.px_st) {
Willy Tarreau295a8372011-03-10 11:25:07 +01004389 case STAT_PX_ST_INIT:
Willy Tarreau91861262007-10-17 17:06:05 +02004390 /* we are on a new proxy */
Willy Tarreau91861262007-10-17 17:06:05 +02004391 if (uri && uri->scope) {
4392 /* we have a limited scope, we have to check the proxy name */
4393 struct stat_scope *scope;
4394 int len;
4395
4396 len = strlen(px->id);
4397 scope = uri->scope;
4398
4399 while (scope) {
4400 /* match exact proxy name */
4401 if (scope->px_len == len && !memcmp(px->id, scope->px_id, len))
4402 break;
4403
4404 /* match '.' which means 'self' proxy */
Willy Tarreau1388a3a2007-10-18 16:38:37 +02004405 if (!strcmp(scope->px_id, ".") && px == s->be)
Willy Tarreau91861262007-10-17 17:06:05 +02004406 break;
4407 scope = scope->next;
4408 }
4409
4410 /* proxy name not found : don't dump anything */
4411 if (scope == NULL)
4412 return 1;
4413 }
4414
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004415 /* if the user has requested a limited output and the proxy
4416 * name does not match, skip it.
4417 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004418 if (appctx->ctx.stats.scope_len &&
Willy Tarreau4e4292b2014-11-28 12:18:45 +01004419 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 +02004420 return 1;
4421
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004422 if ((appctx->ctx.stats.flags & STAT_BOUND) &&
4423 (appctx->ctx.stats.iid != -1) &&
4424 (px->uuid != appctx->ctx.stats.iid))
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01004425 return 1;
4426
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004427 appctx->ctx.stats.px_st = STAT_PX_ST_TH;
Willy Tarreau91861262007-10-17 17:06:05 +02004428 /* fall through */
4429
Willy Tarreau295a8372011-03-10 11:25:07 +01004430 case STAT_PX_ST_TH:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004431 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004432 stats_dump_html_px_hdr(si, px, uri);
Willy Tarreaubc18da12015-03-13 14:00:47 +01004433 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004434 si_applet_cant_put(si);
Willy Tarreau55bb8452007-10-17 18:44:57 +02004435 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01004436 }
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004437 }
Willy Tarreau91861262007-10-17 17:06:05 +02004438
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004439 appctx->ctx.stats.px_st = STAT_PX_ST_FE;
Willy Tarreau91861262007-10-17 17:06:05 +02004440 /* fall through */
4441
Willy Tarreau295a8372011-03-10 11:25:07 +01004442 case STAT_PX_ST_FE:
Willy Tarreau91861262007-10-17 17:06:05 +02004443 /* print the frontend */
Willy Tarreaubc18da12015-03-13 14:00:47 +01004444 if (stats_dump_fe_stats(si, px)) {
4445 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004446 si_applet_cant_put(si);
Willy Tarreau91861262007-10-17 17:06:05 +02004447 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01004448 }
4449 }
Willy Tarreau91861262007-10-17 17:06:05 +02004450
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004451 appctx->ctx.stats.l = px->conf.listeners.n;
4452 appctx->ctx.stats.px_st = STAT_PX_ST_LI;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004453 /* fall through */
4454
Willy Tarreau295a8372011-03-10 11:25:07 +01004455 case STAT_PX_ST_LI:
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004456 /* stats.l has been initialized above */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004457 for (; appctx->ctx.stats.l != &px->conf.listeners; appctx->ctx.stats.l = l->by_fe.n) {
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01004458 if (buffer_almost_full(rep->buf)) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004459 si_applet_cant_put(si);
Willy Tarreau4e33d862009-10-11 23:35:10 +02004460 return 0;
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01004461 }
Willy Tarreau4e33d862009-10-11 23:35:10 +02004462
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004463 l = LIST_ELEM(appctx->ctx.stats.l, struct listener *, by_fe);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004464 if (!l->counters)
4465 continue;
4466
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004467 if (appctx->ctx.stats.flags & STAT_BOUND) {
4468 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SO)))
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004469 break;
4470
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004471 if (appctx->ctx.stats.sid != -1 && l->luid != appctx->ctx.stats.sid)
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004472 continue;
4473 }
4474
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004475 /* print the frontend */
Willy Tarreaubc18da12015-03-13 14:00:47 +01004476 if (stats_dump_li_stats(si, px, l, uri ? uri->flags : 0)) {
4477 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004478 si_applet_cant_put(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004479 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01004480 }
4481 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004482 }
4483
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004484 appctx->ctx.stats.sv = px->srv; /* may be NULL */
4485 appctx->ctx.stats.px_st = STAT_PX_ST_SV;
Willy Tarreau91861262007-10-17 17:06:05 +02004486 /* fall through */
4487
Willy Tarreau295a8372011-03-10 11:25:07 +01004488 case STAT_PX_ST_SV:
Willy Tarreau91861262007-10-17 17:06:05 +02004489 /* stats.sv has been initialized above */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004490 for (; appctx->ctx.stats.sv != NULL; appctx->ctx.stats.sv = sv->next) {
Simon Horman4d2eab62015-04-23 14:51:26 +09004491 enum srv_stats_state sv_state;
Simon Horman837bfa72015-04-23 14:51:27 +09004492 enum srv_stats_colour sv_colour;
Willy Tarreau91861262007-10-17 17:06:05 +02004493
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01004494 if (buffer_almost_full(rep->buf)) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004495 si_applet_cant_put(si);
Willy Tarreau4e33d862009-10-11 23:35:10 +02004496 return 0;
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01004497 }
Willy Tarreau4e33d862009-10-11 23:35:10 +02004498
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004499 sv = appctx->ctx.stats.sv;
Willy Tarreau91861262007-10-17 17:06:05 +02004500
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004501 if (appctx->ctx.stats.flags & STAT_BOUND) {
4502 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SV)))
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01004503 break;
4504
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004505 if (appctx->ctx.stats.sid != -1 && sv->puid != appctx->ctx.stats.sid)
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01004506 continue;
4507 }
4508
Willy Tarreau32091232014-05-16 13:52:00 +02004509 svs = sv;
4510 while (svs->track)
4511 svs = svs->track;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01004512
Willy Tarreauf4e38b32014-05-21 15:04:05 +02004513 if (sv->state == SRV_ST_RUNNING || sv->state == SRV_ST_STARTING) {
Willy Tarreauf4e38b32014-05-21 15:04:05 +02004514 if ((svs->check.state & CHK_ST_ENABLED) &&
Simon Horman837bfa72015-04-23 14:51:27 +09004515 (svs->check.health < svs->check.rise + svs->check.fall - 1)) {
Simon Horman4d2eab62015-04-23 14:51:26 +09004516 sv_state = SRV_STATS_STATE_UP_GOING_DOWN;
Simon Horman837bfa72015-04-23 14:51:27 +09004517 sv_colour = SRV_STATS_COLOUR_GOING_DOWN;
4518 } else {
Simon Horman4d2eab62015-04-23 14:51:26 +09004519 sv_state = SRV_STATS_STATE_UP;
Simon Horman837bfa72015-04-23 14:51:27 +09004520 sv_colour = SRV_STATS_COLOUR_UP;
4521 }
Willy Tarreau2ea81932007-11-30 12:04:38 +01004522
Simon Horman4463d192015-04-23 14:51:28 +09004523 if (sv_state == SRV_STATS_STATE_UP && !svs->uweight)
4524 sv_colour = SRV_STATS_COLOUR_DRAINING;
4525
4526 if (sv->admin & SRV_ADMF_DRAIN) {
Simon Hormanb167b6b2015-04-23 14:51:29 +09004527 if (svs->agent.state & CHK_ST_ENABLED)
4528 sv_state = SRV_STATS_STATE_DRAIN_AGENT;
4529 else if (sv_state == SRV_STATS_STATE_UP_GOING_DOWN)
Simon Horman4d2eab62015-04-23 14:51:26 +09004530 sv_state = SRV_STATS_STATE_DRAIN_GOING_DOWN;
Simon Horman4463d192015-04-23 14:51:28 +09004531 else
Simon Horman4d2eab62015-04-23 14:51:26 +09004532 sv_state = SRV_STATS_STATE_DRAIN;
4533 }
Willy Tarreauf4e38b32014-05-21 15:04:05 +02004534
Simon Horman837bfa72015-04-23 14:51:27 +09004535 if (sv_state == SRV_STATS_STATE_UP && !(svs->check.state & CHK_ST_ENABLED)) {
Simon Horman4d2eab62015-04-23 14:51:26 +09004536 sv_state = SRV_STATS_STATE_NO_CHECK;
Simon Horman837bfa72015-04-23 14:51:27 +09004537 sv_colour = SRV_STATS_COLOUR_NO_CHECK;
4538 }
Willy Tarreau2ea81932007-11-30 12:04:38 +01004539 }
Willy Tarreauf4e38b32014-05-21 15:04:05 +02004540 else if (sv->state == SRV_ST_STOPPING) {
4541 if ((!(sv->check.state & CHK_ST_ENABLED) && !sv->track) ||
Simon Horman837bfa72015-04-23 14:51:27 +09004542 (svs->check.health == svs->check.rise + svs->check.fall - 1)) {
Simon Horman4d2eab62015-04-23 14:51:26 +09004543 sv_state = SRV_STATS_STATE_NOLB;
Simon Horman837bfa72015-04-23 14:51:27 +09004544 sv_colour = SRV_STATS_COLOUR_NOLB;
4545 } else {
Simon Horman4d2eab62015-04-23 14:51:26 +09004546 sv_state = SRV_STATS_STATE_NOLB_GOING_DOWN;
Simon Horman837bfa72015-04-23 14:51:27 +09004547 sv_colour = SRV_STATS_COLOUR_GOING_DOWN;
4548 }
Willy Tarreauf4e38b32014-05-21 15:04:05 +02004549 }
4550 else { /* stopped */
Simon Horman837bfa72015-04-23 14:51:27 +09004551 if ((svs->agent.state & CHK_ST_ENABLED) && !svs->agent.health) {
Simon Horman4d2eab62015-04-23 14:51:26 +09004552 sv_state = SRV_STATS_STATE_DOWN_AGENT;
Simon Horman837bfa72015-04-23 14:51:27 +09004553 sv_colour = SRV_STATS_COLOUR_DOWN;
4554 } else if ((svs->check.state & CHK_ST_ENABLED) && !svs->check.health) {
Simon Horman4d2eab62015-04-23 14:51:26 +09004555 sv_state = SRV_STATS_STATE_DOWN; /* DOWN */
Simon Horman837bfa72015-04-23 14:51:27 +09004556 sv_colour = SRV_STATS_COLOUR_DOWN;
4557 } else if ((svs->agent.state & CHK_ST_ENABLED) || (svs->check.state & CHK_ST_ENABLED)) {
Simon Horman4d2eab62015-04-23 14:51:26 +09004558 sv_state = SRV_STATS_STATE_GOING_UP;
Simon Horman837bfa72015-04-23 14:51:27 +09004559 sv_colour = SRV_STATS_COLOUR_GOING_UP;
4560 } else {
Simon Horman4d2eab62015-04-23 14:51:26 +09004561 sv_state = SRV_STATS_STATE_DOWN; /* DOWN, unchecked */
Simon Horman837bfa72015-04-23 14:51:27 +09004562 sv_colour = SRV_STATS_COLOUR_DOWN;
4563 }
Willy Tarreauf4e38b32014-05-21 15:04:05 +02004564 }
Willy Tarreau91861262007-10-17 17:06:05 +02004565
Willy Tarreau9638efa2014-05-23 11:19:57 +02004566 if (((sv_state <= 1) || (sv->admin & SRV_ADMF_MAINT)) && (appctx->ctx.stats.flags & STAT_HIDE_DOWN)) {
Willy Tarreau91861262007-10-17 17:06:05 +02004567 /* do not report servers which are DOWN */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004568 appctx->ctx.stats.sv = sv->next;
Willy Tarreau91861262007-10-17 17:06:05 +02004569 continue;
4570 }
4571
Simon Horman837bfa72015-04-23 14:51:27 +09004572 if (stats_dump_sv_stats(si, px, uri ? uri->flags : 0, sv, sv_state, sv_colour)) {
Willy Tarreaubc18da12015-03-13 14:00:47 +01004573 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004574 si_applet_cant_put(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004575 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01004576 }
4577 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004578 } /* for sv */
Cyril Bonté474be412010-10-12 00:14:36 +02004579
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004580 appctx->ctx.stats.px_st = STAT_PX_ST_BE;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004581 /* fall through */
Cyril Bonté70be45d2010-10-12 00:14:35 +02004582
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004583 case STAT_PX_ST_BE:
4584 /* print the backend */
Willy Tarreaubc18da12015-03-13 14:00:47 +01004585 if (stats_dump_be_stats(si, px, uri ? uri->flags : 0)) {
4586 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004587 si_applet_cant_put(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004588 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01004589 }
4590 }
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01004591
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004592 appctx->ctx.stats.px_st = STAT_PX_ST_END;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004593 /* fall through */
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01004594
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004595 case STAT_PX_ST_END:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004596 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004597 stats_dump_html_px_end(si, px);
Willy Tarreaubc18da12015-03-13 14:00:47 +01004598 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004599 si_applet_cant_put(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004600 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01004601 }
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004602 }
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01004603
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004604 appctx->ctx.stats.px_st = STAT_PX_ST_FIN;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004605 /* fall through */
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01004606
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004607 case STAT_PX_ST_FIN:
4608 return 1;
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01004609
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004610 default:
4611 /* unknown state, we should put an abort() here ! */
4612 return 1;
4613 }
4614}
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01004615
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004616/* Dumps the HTTP stats head block to the trash for and uses the per-uri
4617 * parameters <uri>. The caller is responsible for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004618 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004619static void stats_dump_html_head(struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004620{
4621 /* WARNING! This must fit in the first buffer !!! */
4622 chunk_appendf(&trash,
4623 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n"
4624 "\"http://www.w3.org/TR/html4/loose.dtd\">\n"
4625 "<html><head><title>Statistics Report for " PRODUCT_NAME "%s%s</title>\n"
4626 "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n"
4627 "<style type=\"text/css\"><!--\n"
4628 "body {"
4629 " font-family: arial, helvetica, sans-serif;"
4630 " font-size: 12px;"
4631 " font-weight: normal;"
4632 " color: black;"
4633 " background: white;"
4634 "}\n"
4635 "th,td {"
4636 " font-size: 10px;"
4637 "}\n"
4638 "h1 {"
4639 " font-size: x-large;"
4640 " margin-bottom: 0.5em;"
4641 "}\n"
4642 "h2 {"
4643 " font-family: helvetica, arial;"
4644 " font-size: x-large;"
4645 " font-weight: bold;"
4646 " font-style: italic;"
4647 " color: #6020a0;"
4648 " margin-top: 0em;"
4649 " margin-bottom: 0em;"
4650 "}\n"
4651 "h3 {"
4652 " font-family: helvetica, arial;"
4653 " font-size: 16px;"
4654 " font-weight: bold;"
4655 " color: #b00040;"
4656 " background: #e8e8d0;"
4657 " margin-top: 0em;"
4658 " margin-bottom: 0em;"
4659 "}\n"
4660 "li {"
4661 " margin-top: 0.25em;"
4662 " margin-right: 2em;"
4663 "}\n"
4664 ".hr {margin-top: 0.25em;"
4665 " border-color: black;"
4666 " border-bottom-style: solid;"
4667 "}\n"
4668 ".titre {background: #20D0D0;color: #000000; font-weight: bold; text-align: center;}\n"
4669 ".total {background: #20D0D0;color: #ffff80;}\n"
4670 ".frontend {background: #e8e8d0;}\n"
4671 ".socket {background: #d0d0d0;}\n"
4672 ".backend {background: #e8e8d0;}\n"
Simon Horman837bfa72015-04-23 14:51:27 +09004673 ".active_down {background: #ff9090;}\n"
4674 ".active_going_up {background: #ffd020;}\n"
4675 ".active_going_down {background: #ffffa0;}\n"
4676 ".active_up {background: #c0ffc0;}\n"
4677 ".active_nolb {background: #20a0ff;}\n"
4678 ".active_draining {background: #20a0FF;}\n"
4679 ".active_no_check {background: #e0e0e0;}\n"
4680 ".backup_down {background: #ff9090;}\n"
4681 ".backup_going_up {background: #ff80ff;}\n"
4682 ".backup_going_down {background: #c060ff;}\n"
4683 ".backup_up {background: #b0d0ff;}\n"
4684 ".backup_nolb {background: #90b0e0;}\n"
4685 ".backup_draining {background: #cc9900;}\n"
4686 ".backup_no_check {background: #e0e0e0;}\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004687 ".maintain {background: #c07820;}\n"
4688 ".rls {letter-spacing: 0.2em; margin-right: 1px;}\n" /* right letter spacing (used for grouping digits) */
4689 "\n"
4690 "a.px:link {color: #ffff40; text-decoration: none;}"
4691 "a.px:visited {color: #ffff40; text-decoration: none;}"
4692 "a.px:hover {color: #ffffff; text-decoration: none;}"
4693 "a.lfsb:link {color: #000000; text-decoration: none;}"
4694 "a.lfsb:visited {color: #000000; text-decoration: none;}"
4695 "a.lfsb:hover {color: #505050; text-decoration: none;}"
4696 "\n"
4697 "table.tbl { border-collapse: collapse; border-style: none;}\n"
4698 "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"
4699 "table.tbl td.ac { text-align: center;}\n"
4700 "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}\n"
4701 "table.tbl th.pxname { background: #b00040; color: #ffff40; font-weight: bold; border-style: solid solid none solid; padding: 2px 3px; white-space: nowrap;}\n"
4702 "table.tbl th.empty { border-style: none; empty-cells: hide; background: white;}\n"
4703 "table.tbl th.desc { background: white; border-style: solid solid none solid; text-align: left; padding: 2px 3px;}\n"
4704 "\n"
4705 "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n"
4706 "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n"
4707 "table.lgd td.noborder { border-style: none; padding: 2px; white-space: nowrap;}\n"
Willy Tarreau466c9b52012-12-23 02:25:03 +01004708 "table.det { border-collapse: collapse; border-style: none; }\n"
4709 "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 +01004710 "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 +01004711 "u {text-decoration:none; border-bottom: 1px dotted black;}\n"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02004712 "div.tips {\n"
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01004713 " display:block;\n"
4714 " visibility:hidden;\n"
4715 " z-index:2147483647;\n"
4716 " position:absolute;\n"
4717 " padding:2px 4px 3px;\n"
4718 " background:#f0f060; color:#000000;\n"
4719 " border:1px solid #7040c0;\n"
4720 " white-space:nowrap;\n"
4721 " font-style:normal;font-size:11px;font-weight:normal;\n"
4722 " -moz-border-radius:3px;-webkit-border-radius:3px;border-radius:3px;\n"
4723 " -moz-box-shadow:gray 2px 2px 3px;-webkit-box-shadow:gray 2px 2px 3px;box-shadow:gray 2px 2px 3px;\n"
4724 "}\n"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02004725 "u:hover div.tips {visibility:visible;}\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004726 "-->\n"
4727 "</style></head>\n",
4728 (uri->flags & ST_SHNODE) ? " on " : "",
4729 (uri->flags & ST_SHNODE) ? (uri->node ? uri->node : global.node) : ""
4730 );
4731}
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01004732
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004733/* Dumps the HTML stats information block to the trash for and uses the state from
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004734 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004735 * for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004736 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004737static void stats_dump_html_info(struct stream_interface *si, struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004738{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004739 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004740 unsigned int up = (now.tv_sec - start_date.tv_sec);
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004741 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01004742
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004743 /* WARNING! this has to fit the first packet too.
4744 * We are around 3.5 kB, add adding entries will
4745 * become tricky if we want to support 4kB buffers !
4746 */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004747 chunk_appendf(&trash,
4748 "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">"
4749 PRODUCT_NAME "%s</a></h1>\n"
4750 "<h2>Statistics Report for pid %d%s%s%s%s</h2>\n"
4751 "<hr width=\"100%%\" class=\"hr\">\n"
4752 "<h3>&gt; General process information</h3>\n"
4753 "<table border=0><tr><td align=\"left\" nowrap width=\"1%%\">\n"
4754 "<p><b>pid = </b> %d (process #%d, nbproc = %d)<br>\n"
4755 "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n"
4756 "<b>system limits:</b> memmax = %s%s; ulimit-n = %d<br>\n"
4757 "<b>maxsock = </b> %d; <b>maxconn = </b> %d; <b>maxpipes = </b> %d<br>\n"
4758 "current conns = %d; current pipes = %d/%d; conn rate = %d/sec<br>\n"
4759 "Running tasks: %d/%d; idle = %d %%<br>\n"
4760 "</td><td align=\"center\" nowrap>\n"
4761 "<table class=\"lgd\"><tr>\n"
Simon Horman837bfa72015-04-23 14:51:27 +09004762 "<td class=\"active_up\">&nbsp;</td><td class=\"noborder\">active UP </td>"
4763 "<td class=\"backup_up\">&nbsp;</td><td class=\"noborder\">backup UP </td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004764 "</tr><tr>\n"
Simon Horman837bfa72015-04-23 14:51:27 +09004765 "<td class=\"active_going_down\"></td><td class=\"noborder\">active UP, going down </td>"
4766 "<td class=\"backup_going_down\"></td><td class=\"noborder\">backup UP, going down </td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004767 "</tr><tr>\n"
Simon Horman837bfa72015-04-23 14:51:27 +09004768 "<td class=\"active_going_up\"></td><td class=\"noborder\">active DOWN, going up </td>"
4769 "<td class=\"backup_going_up\"></td><td class=\"noborder\">backup DOWN, going up </td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004770 "</tr><tr>\n"
Simon Horman837bfa72015-04-23 14:51:27 +09004771 "<td class=\"active_down\"></td><td class=\"noborder\">active or backup DOWN &nbsp;</td>"
4772 "<td class=\"active_no_check\"></td><td class=\"noborder\">not checked </td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004773 "</tr><tr>\n"
4774 "<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 -07004775 "</tr><tr>\n"
Simon Horman837bfa72015-04-23 14:51:27 +09004776 "<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 +01004777 "</tr></table>\n"
Willy Tarreau6b7764a2013-12-04 00:43:21 +01004778 "Note: \"NOLB\"/\"DRAIN\" = UP with load-balancing disabled."
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004779 "</td>"
4780 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
4781 "<b>Display option:</b><ul style=\"margin-top: 0.25em;\">"
4782 "",
4783 (uri->flags & ST_HIDEVER) ? "" : (STATS_VERSION_STRING),
4784 pid, (uri->flags & ST_SHNODE) ? " on " : "",
4785 (uri->flags & ST_SHNODE) ? (uri->node ? uri->node : global.node) : "",
4786 (uri->flags & ST_SHDESC) ? ": " : "",
4787 (uri->flags & ST_SHDESC) ? (uri->desc ? uri->desc : global.desc) : "",
4788 pid, relative_pid, global.nbproc,
4789 up / 86400, (up % 86400) / 3600,
4790 (up % 3600) / 60, (up % 60),
4791 global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited",
4792 global.rlimit_memmax ? " MB" : "",
4793 global.rlimit_nofile,
4794 global.maxsock, global.maxconn, global.maxpipes,
4795 actconn, pipes_used, pipes_used+pipes_free, read_freq_ctr(&global.conn_per_sec),
4796 run_queue_cur, nb_tasks_cur, idle_pct
4797 );
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004798
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004799 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Willy Tarreau4e4292b2014-11-28 12:18:45 +01004800 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 +01004801 scope_txt[appctx->ctx.stats.scope_len] = '\0';
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004802
4803 chunk_appendf(&trash,
Jeff Buchbinder2dbbf4d2014-08-29 15:10:08 -05004804 "<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 +01004805 (appctx->ctx.stats.scope_len > 0) ? scope_txt : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004806 STAT_SCOPE_TXT_MAXLEN);
4807
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004808 /* 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 +02004809 scope_txt[0] = 0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004810 if (appctx->ctx.stats.scope_len) {
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004811 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau4e4292b2014-11-28 12:18:45 +01004812 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 +01004813 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004814 }
4815
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004816 if (appctx->ctx.stats.flags & STAT_HIDE_DOWN)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004817 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004818 "<li><a href=\"%s%s%s%s\">Show all servers</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004819 uri->uri_prefix,
4820 "",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004821 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004822 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004823 else
4824 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004825 "<li><a href=\"%s%s%s%s\">Hide 'DOWN' servers</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004826 uri->uri_prefix,
4827 ";up",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004828 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004829 scope_txt);
Willy Tarreau91861262007-10-17 17:06:05 +02004830
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004831 if (uri->refresh > 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004832 if (appctx->ctx.stats.flags & STAT_NO_REFRESH)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004833 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004834 "<li><a href=\"%s%s%s%s\">Enable refresh</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004835 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004836 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004837 "",
4838 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004839 else
4840 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004841 "<li><a href=\"%s%s%s%s\">Disable refresh</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004842 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004843 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004844 ";norefresh",
4845 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004846 }
Willy Tarreau55bb8452007-10-17 18:44:57 +02004847
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004848 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004849 "<li><a href=\"%s%s%s%s\">Refresh now</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004850 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004851 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4852 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004853 scope_txt);
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02004854
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004855 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004856 "<li><a href=\"%s;csv%s%s\">CSV export</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004857 uri->uri_prefix,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004858 (uri->refresh > 0) ? ";norefresh" : "",
4859 scope_txt);
Willy Tarreau4bab24d2007-11-30 18:16:29 +01004860
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004861 chunk_appendf(&trash,
4862 "</ul></td>"
4863 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
4864 "<b>External resources:</b><ul style=\"margin-top: 0.25em;\">\n"
4865 "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>\n"
4866 "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>\n"
4867 "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>\n"
4868 "</ul>"
4869 "</td>"
4870 "</tr></table>\n"
4871 ""
4872 );
Willy Tarreau4bab24d2007-11-30 18:16:29 +01004873
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004874 if (appctx->ctx.stats.st_code) {
4875 switch (appctx->ctx.stats.st_code) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004876 case STAT_STATUS_DONE:
4877 chunk_appendf(&trash,
Simon Horman837bfa72015-04-23 14:51:27 +09004878 "<p><div class=active_up>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004879 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004880 "Action processed successfully."
Willy Tarreauba6be982013-04-19 12:16:55 +02004881 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004882 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4883 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02004884 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004885 break;
4886 case STAT_STATUS_NONE:
4887 chunk_appendf(&trash,
Simon Horman837bfa72015-04-23 14:51:27 +09004888 "<p><div class=active_going_down>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004889 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004890 "Nothing has changed."
Willy Tarreauba6be982013-04-19 12:16:55 +02004891 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004892 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4893 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02004894 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004895 break;
4896 case STAT_STATUS_PART:
4897 chunk_appendf(&trash,
Simon Horman837bfa72015-04-23 14:51:27 +09004898 "<p><div class=active_going_down>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004899 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004900 "Action partially processed.<br>"
4901 "Some server names are probably unknown or ambiguous (duplicated names in the backend)."
Willy Tarreauba6be982013-04-19 12:16:55 +02004902 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004903 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4904 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02004905 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004906 break;
4907 case STAT_STATUS_ERRP:
4908 chunk_appendf(&trash,
Simon Horman837bfa72015-04-23 14:51:27 +09004909 "<p><div class=active_down>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004910 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004911 "Action not processed because of invalid parameters."
4912 "<ul>"
4913 "<li>The action is maybe unknown.</li>"
4914 "<li>The backend name is probably unknown or ambiguous (duplicated names).</li>"
4915 "<li>Some server names are probably unknown or ambiguous (duplicated names in the backend).</li>"
4916 "</ul>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004917 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004918 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4919 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02004920 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004921 break;
4922 case STAT_STATUS_EXCD:
4923 chunk_appendf(&trash,
Simon Horman837bfa72015-04-23 14:51:27 +09004924 "<p><div class=active_down>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004925 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004926 "<b>Action not processed : the buffer couldn't store all the data.<br>"
4927 "You should retry with less servers at a time.</b>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004928 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004929 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4930 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02004931 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004932 break;
4933 case STAT_STATUS_DENY:
4934 chunk_appendf(&trash,
Simon Horman837bfa72015-04-23 14:51:27 +09004935 "<p><div class=active_down>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004936 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004937 "<b>Action denied.</b>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004938 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004939 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4940 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02004941 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004942 break;
4943 default:
4944 chunk_appendf(&trash,
Simon Horman837bfa72015-04-23 14:51:27 +09004945 "<p><div class=active_no_check>"
Willy Tarreauba6be982013-04-19 12:16:55 +02004946 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004947 "Unexpected result."
Willy Tarreauba6be982013-04-19 12:16:55 +02004948 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004949 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4950 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02004951 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004952 }
4953 chunk_appendf(&trash, "<p>\n");
4954 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004955}
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01004956
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004957/* Dumps the HTML stats trailer block to the trash. The caller is responsible
4958 * for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004959 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004960static void stats_dump_html_end()
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004961{
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004962 chunk_appendf(&trash, "</body></html>\n");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004963}
Willy Tarreau7f062c42009-03-05 18:43:00 +01004964
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004965/* This function dumps statistics onto the stream interface's read buffer in
4966 * either CSV or HTML format. <uri> contains some HTML-specific parameters that
Willy Tarreau306f8302013-07-08 15:53:06 +02004967 * are ignored for CSV format (hence <uri> may be NULL there). It returns 0 if
4968 * 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 +02004969 * and the stream must be closed, or -1 in case of any error. This function is
Willy Tarreau306f8302013-07-08 15:53:06 +02004970 * used by both the CLI and the HTTP handlers.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004971 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004972static int stats_dump_stat_to_buffer(struct stream_interface *si, struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004973{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004974 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau2bb4a962014-11-28 11:11:05 +01004975 struct channel *rep = si_ic(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004976 struct proxy *px;
Willy Tarreau7f062c42009-03-05 18:43:00 +01004977
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004978 chunk_reset(&trash);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02004979
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004980 switch (appctx->st2) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004981 case STAT_ST_INIT:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004982 appctx->st2 = STAT_ST_HEAD; /* let's start producing data */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004983 /* fall through */
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004984
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004985 case STAT_ST_HEAD:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004986 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004987 stats_dump_html_head(uri);
Willy Tarreau354898b2012-12-23 18:15:23 +01004988 else
4989 stats_dump_csv_header();
Willy Tarreaud9b587f2010-02-26 10:05:55 +01004990
Willy Tarreaubc18da12015-03-13 14:00:47 +01004991 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004992 si_applet_cant_put(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004993 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01004994 }
Willy Tarreauae526782010-03-04 20:34:23 +01004995
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004996 appctx->st2 = STAT_ST_INFO;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004997 /* fall through */
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004998
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004999 case STAT_ST_INFO:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005000 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01005001 stats_dump_html_info(si, uri);
Willy Tarreaubc18da12015-03-13 14:00:47 +01005002 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02005003 si_applet_cant_put(si);
Willy Tarreau91861262007-10-17 17:06:05 +02005004 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01005005 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005006 }
Willy Tarreau91861262007-10-17 17:06:05 +02005007
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005008 appctx->ctx.stats.px = proxy;
5009 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
5010 appctx->st2 = STAT_ST_LIST;
Willy Tarreau91861262007-10-17 17:06:05 +02005011 /* fall through */
5012
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005013 case STAT_ST_LIST:
5014 /* dump proxies */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005015 while (appctx->ctx.stats.px) {
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01005016 if (buffer_almost_full(rep->buf)) {
Willy Tarreaufe127932015-04-21 19:23:39 +02005017 si_applet_cant_put(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005018 return 0;
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01005019 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005020
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005021 px = appctx->ctx.stats.px;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005022 /* skip the disabled proxies, global frontend and non-networked ones */
5023 if (px->state != PR_STSTOPPED && px->uuid > 0 && (px->cap & (PR_CAP_FE | PR_CAP_BE)))
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01005024 if (stats_dump_proxy_to_buffer(si, px, uri) == 0)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005025 return 0;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005026
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005027 appctx->ctx.stats.px = px->next;
5028 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005029 }
5030 /* here, we just have reached the last proxy */
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005031
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005032 appctx->st2 = STAT_ST_END;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005033 /* fall through */
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005034
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005035 case STAT_ST_END:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005036 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01005037 stats_dump_html_end();
Willy Tarreaubc18da12015-03-13 14:00:47 +01005038 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02005039 si_applet_cant_put(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005040 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01005041 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005042 }
Willy Tarreau55058a72012-11-21 08:27:21 +01005043
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005044 appctx->st2 = STAT_ST_FIN;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005045 /* fall through */
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02005046
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005047 case STAT_ST_FIN:
5048 return 1;
Willy Tarreau55058a72012-11-21 08:27:21 +01005049
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005050 default:
5051 /* unknown state ! */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005052 appctx->st2 = STAT_ST_FIN;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005053 return -1;
5054 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005055}
Willy Tarreauae526782010-03-04 20:34:23 +01005056
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005057/* We reached the stats page through a POST request. The appctx is
5058 * expected to have already been allocated by the caller.
Willy Tarreau347a35d2013-11-22 17:51:09 +01005059 * Parse the posted data and enable/disable servers if necessary.
5060 * Returns 1 if request was parsed or zero if it needs more data.
5061 */
5062static int stats_process_http_post(struct stream_interface *si)
5063{
Willy Tarreau87b09662015-04-03 00:22:06 +02005064 struct stream *s = si_strm(si);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005065 struct appctx *appctx = objt_appctx(si->end);
Willy Tarreau347a35d2013-11-22 17:51:09 +01005066
5067 struct proxy *px = NULL;
5068 struct server *sv = NULL;
5069
5070 char key[LINESIZE];
5071 int action = ST_ADM_ACTION_NONE;
5072 int reprocess = 0;
5073
5074 int total_servers = 0;
5075 int altered_servers = 0;
5076
5077 char *first_param, *cur_param, *next_param, *end_params;
5078 char *st_cur_param = NULL;
5079 char *st_next_param = NULL;
5080
5081 struct chunk *temp;
5082 int reql;
5083
5084 temp = get_trash_chunk();
Willy Tarreaueee5b512015-04-03 23:46:31 +02005085 if (temp->size < s->txn->req.body_len) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01005086 /* too large request */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005087 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005088 goto out;
5089 }
5090
Willy Tarreaueee5b512015-04-03 23:46:31 +02005091 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 +01005092 if (reql <= 0) {
5093 /* we need more data */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005094 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005095 return 0;
5096 }
5097
5098 first_param = temp->str;
5099 end_params = temp->str + reql;
5100 cur_param = next_param = end_params;
5101 *end_params = '\0';
5102
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005103 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005104
5105 /*
5106 * Parse the parameters in reverse order to only store the last value.
5107 * From the html form, the backend and the action are at the end.
5108 */
5109 while (cur_param > first_param) {
5110 char *value;
5111 int poffset, plen;
5112
5113 cur_param--;
5114
5115 if ((*cur_param == '&') || (cur_param == first_param)) {
5116 reprocess_servers:
5117 /* Parse the key */
5118 poffset = (cur_param != first_param ? 1 : 0);
5119 plen = next_param - cur_param + (cur_param == first_param ? 1 : 0);
5120 if ((plen > 0) && (plen <= sizeof(key))) {
5121 strncpy(key, cur_param + poffset, plen);
5122 key[plen - 1] = '\0';
5123 } else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005124 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005125 goto out;
5126 }
5127
5128 /* Parse the value */
5129 value = key;
5130 while (*value != '\0' && *value != '=') {
5131 value++;
5132 }
5133 if (*value == '=') {
5134 /* Ok, a value is found, we can mark the end of the key */
5135 *value++ = '\0';
5136 }
5137 if (url_decode(key) < 0 || url_decode(value) < 0)
5138 break;
5139
5140 /* Now we can check the key to see what to do */
5141 if (!px && (strcmp(key, "b") == 0)) {
Willy Tarreau9e0bb102015-05-26 11:24:42 +02005142 if ((px = proxy_be_by_name(value)) == NULL) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01005143 /* the backend name is unknown or ambiguous (duplicate names) */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005144 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005145 goto out;
5146 }
5147 }
5148 else if (!action && (strcmp(key, "action") == 0)) {
Willy Tarreaued7df902014-05-22 18:04:49 +02005149 if (strcmp(value, "ready") == 0) {
5150 action = ST_ADM_ACTION_READY;
5151 }
5152 else if (strcmp(value, "drain") == 0) {
5153 action = ST_ADM_ACTION_DRAIN;
5154 }
5155 else if (strcmp(value, "maint") == 0) {
5156 action = ST_ADM_ACTION_MAINT;
5157 }
5158 else if (strcmp(value, "shutdown") == 0) {
5159 action = ST_ADM_ACTION_SHUTDOWN;
5160 }
Willy Tarreau248a60e2014-05-23 14:59:48 +02005161 else if (strcmp(value, "dhlth") == 0) {
5162 action = ST_ADM_ACTION_DHLTH;
5163 }
5164 else if (strcmp(value, "ehlth") == 0) {
5165 action = ST_ADM_ACTION_EHLTH;
5166 }
5167 else if (strcmp(value, "hrunn") == 0) {
5168 action = ST_ADM_ACTION_HRUNN;
5169 }
5170 else if (strcmp(value, "hnolb") == 0) {
5171 action = ST_ADM_ACTION_HNOLB;
5172 }
5173 else if (strcmp(value, "hdown") == 0) {
5174 action = ST_ADM_ACTION_HDOWN;
5175 }
5176 else if (strcmp(value, "dagent") == 0) {
5177 action = ST_ADM_ACTION_DAGENT;
5178 }
5179 else if (strcmp(value, "eagent") == 0) {
5180 action = ST_ADM_ACTION_EAGENT;
5181 }
5182 else if (strcmp(value, "arunn") == 0) {
5183 action = ST_ADM_ACTION_ARUNN;
5184 }
5185 else if (strcmp(value, "adown") == 0) {
5186 action = ST_ADM_ACTION_ADOWN;
5187 }
Willy Tarreaued7df902014-05-22 18:04:49 +02005188 /* else these are the old supported methods */
5189 else if (strcmp(value, "disable") == 0) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01005190 action = ST_ADM_ACTION_DISABLE;
5191 }
5192 else if (strcmp(value, "enable") == 0) {
5193 action = ST_ADM_ACTION_ENABLE;
5194 }
5195 else if (strcmp(value, "stop") == 0) {
5196 action = ST_ADM_ACTION_STOP;
5197 }
5198 else if (strcmp(value, "start") == 0) {
5199 action = ST_ADM_ACTION_START;
5200 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01005201 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005202 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005203 goto out;
5204 }
5205 }
5206 else if (strcmp(key, "s") == 0) {
5207 if (!(px && action)) {
5208 /*
5209 * Indicates that we'll need to reprocess the parameters
5210 * as soon as backend and action are known
5211 */
5212 if (!reprocess) {
5213 st_cur_param = cur_param;
5214 st_next_param = next_param;
5215 }
5216 reprocess = 1;
5217 }
5218 else if ((sv = findserver(px, value)) != NULL) {
5219 switch (action) {
5220 case ST_ADM_ACTION_DISABLE:
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02005221 if (!(sv->admin & SRV_ADMF_FMAINT)) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01005222 altered_servers++;
5223 total_servers++;
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02005224 srv_set_admin_flag(sv, SRV_ADMF_FMAINT);
Willy Tarreau347a35d2013-11-22 17:51:09 +01005225 }
5226 break;
5227 case ST_ADM_ACTION_ENABLE:
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02005228 if (sv->admin & SRV_ADMF_FMAINT) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01005229 altered_servers++;
5230 total_servers++;
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02005231 srv_clr_admin_flag(sv, SRV_ADMF_FMAINT);
Willy Tarreau347a35d2013-11-22 17:51:09 +01005232 }
5233 break;
5234 case ST_ADM_ACTION_STOP:
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02005235 if (!(sv->admin & SRV_ADMF_FDRAIN)) {
5236 srv_set_admin_flag(sv, SRV_ADMF_FDRAIN);
5237 altered_servers++;
5238 total_servers++;
5239 }
5240 break;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005241 case ST_ADM_ACTION_START:
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02005242 if (sv->admin & SRV_ADMF_FDRAIN) {
5243 srv_clr_admin_flag(sv, SRV_ADMF_FDRAIN);
5244 altered_servers++;
5245 total_servers++;
5246 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01005247 break;
Willy Tarreau248a60e2014-05-23 14:59:48 +02005248 case ST_ADM_ACTION_DHLTH:
5249 if (sv->check.state & CHK_ST_CONFIGURED) {
5250 sv->check.state &= ~CHK_ST_ENABLED;
5251 altered_servers++;
5252 total_servers++;
5253 }
5254 break;
5255 case ST_ADM_ACTION_EHLTH:
5256 if (sv->check.state & CHK_ST_CONFIGURED) {
5257 sv->check.state |= CHK_ST_ENABLED;
5258 altered_servers++;
5259 total_servers++;
5260 }
5261 break;
5262 case ST_ADM_ACTION_HRUNN:
5263 if (!(sv->track)) {
5264 sv->check.health = sv->check.rise + sv->check.fall - 1;
5265 srv_set_running(sv, "changed from Web interface");
5266 altered_servers++;
5267 total_servers++;
5268 }
5269 break;
5270 case ST_ADM_ACTION_HNOLB:
5271 if (!(sv->track)) {
5272 sv->check.health = sv->check.rise + sv->check.fall - 1;
5273 srv_set_stopping(sv, "changed from Web interface");
5274 altered_servers++;
5275 total_servers++;
5276 }
5277 break;
5278 case ST_ADM_ACTION_HDOWN:
5279 if (!(sv->track)) {
5280 sv->check.health = 0;
5281 srv_set_stopped(sv, "changed from Web interface");
5282 altered_servers++;
5283 total_servers++;
5284 }
5285 break;
5286 case ST_ADM_ACTION_DAGENT:
5287 if (sv->agent.state & CHK_ST_CONFIGURED) {
5288 sv->agent.state &= ~CHK_ST_ENABLED;
5289 altered_servers++;
5290 total_servers++;
5291 }
5292 break;
5293 case ST_ADM_ACTION_EAGENT:
5294 if (sv->agent.state & CHK_ST_CONFIGURED) {
5295 sv->agent.state |= CHK_ST_ENABLED;
5296 altered_servers++;
5297 total_servers++;
5298 }
5299 break;
5300 case ST_ADM_ACTION_ARUNN:
5301 if (sv->agent.state & CHK_ST_ENABLED) {
5302 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
5303 srv_set_running(sv, "changed from Web interface");
5304 altered_servers++;
5305 total_servers++;
5306 }
5307 break;
5308 case ST_ADM_ACTION_ADOWN:
5309 if (sv->agent.state & CHK_ST_ENABLED) {
5310 sv->agent.health = 0;
5311 srv_set_stopped(sv, "changed from Web interface");
5312 altered_servers++;
5313 total_servers++;
5314 }
5315 break;
Willy Tarreaued7df902014-05-22 18:04:49 +02005316 case ST_ADM_ACTION_READY:
5317 srv_adm_set_ready(sv);
5318 altered_servers++;
5319 total_servers++;
5320 break;
5321 case ST_ADM_ACTION_DRAIN:
5322 srv_adm_set_drain(sv);
5323 altered_servers++;
5324 total_servers++;
5325 break;
5326 case ST_ADM_ACTION_MAINT:
5327 srv_adm_set_maint(sv);
5328 altered_servers++;
5329 total_servers++;
5330 break;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005331 case ST_ADM_ACTION_SHUTDOWN:
5332 if (px->state != PR_STSTOPPED) {
Willy Tarreau87b09662015-04-03 00:22:06 +02005333 struct stream *sess, *sess_bck;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005334
5335 list_for_each_entry_safe(sess, sess_bck, &sv->actconns, by_srv)
5336 if (sess->srv_conn == sv)
Willy Tarreaue7dff022015-04-03 01:14:29 +02005337 stream_shutdown(sess, SF_ERR_KILLED);
Willy Tarreau347a35d2013-11-22 17:51:09 +01005338
5339 altered_servers++;
5340 total_servers++;
5341 }
5342 break;
5343 }
5344 } else {
5345 /* the server name is unknown or ambiguous (duplicate names) */
5346 total_servers++;
5347 }
5348 }
5349 if (reprocess && px && action) {
5350 /* Now, we know the backend and the action chosen by the user.
5351 * We can safely restart from the first server parameter
5352 * to reprocess them
5353 */
5354 cur_param = st_cur_param;
5355 next_param = st_next_param;
5356 reprocess = 0;
5357 goto reprocess_servers;
5358 }
5359
5360 next_param = cur_param;
5361 }
5362 }
5363
5364 if (total_servers == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005365 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005366 }
5367 else if (altered_servers == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005368 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005369 }
5370 else if (altered_servers == total_servers) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005371 appctx->ctx.stats.st_code = STAT_STATUS_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005372 }
5373 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005374 appctx->ctx.stats.st_code = STAT_STATUS_PART;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005375 }
5376 out:
5377 return 1;
5378}
5379
5380
5381static int stats_send_http_headers(struct stream_interface *si)
5382{
Willy Tarreau87b09662015-04-03 00:22:06 +02005383 struct stream *s = si_strm(si);
Willy Tarreau347a35d2013-11-22 17:51:09 +01005384 struct uri_auth *uri = s->be->uri_auth;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005385 struct appctx *appctx = objt_appctx(si->end);
Willy Tarreau347a35d2013-11-22 17:51:09 +01005386
5387 chunk_printf(&trash,
Willy Tarreau8b8995f2014-04-24 22:51:54 +02005388 "HTTP/1.1 200 OK\r\n"
Willy Tarreau347a35d2013-11-22 17:51:09 +01005389 "Cache-Control: no-cache\r\n"
5390 "Connection: close\r\n"
5391 "Content-Type: %s\r\n",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005392 (appctx->ctx.stats.flags & STAT_FMT_HTML) ? "text/html" : "text/plain");
Willy Tarreau347a35d2013-11-22 17:51:09 +01005393
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005394 if (uri->refresh > 0 && !(appctx->ctx.stats.flags & STAT_NO_REFRESH))
Willy Tarreau347a35d2013-11-22 17:51:09 +01005395 chunk_appendf(&trash, "Refresh: %d\r\n",
5396 uri->refresh);
5397
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005398 /* we don't send the CRLF in chunked mode, it will be sent with the first chunk's size */
5399
5400 if (appctx->ctx.stats.flags & STAT_CHUNKED)
5401 chunk_appendf(&trash, "Transfer-Encoding: chunked\r\n");
5402 else
5403 chunk_appendf(&trash, "\r\n");
Willy Tarreau347a35d2013-11-22 17:51:09 +01005404
Willy Tarreaueee5b512015-04-03 23:46:31 +02005405 s->txn->status = 200;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005406 s->logs.tv_request = now;
5407
Willy Tarreaubc18da12015-03-13 14:00:47 +01005408 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02005409 si_applet_cant_put(si);
Willy Tarreau347a35d2013-11-22 17:51:09 +01005410 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01005411 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01005412
5413 return 1;
5414}
5415
5416static int stats_send_http_redirect(struct stream_interface *si)
5417{
5418 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
Willy Tarreau87b09662015-04-03 00:22:06 +02005419 struct stream *s = si_strm(si);
Willy Tarreau347a35d2013-11-22 17:51:09 +01005420 struct uri_auth *uri = s->be->uri_auth;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005421 struct appctx *appctx = objt_appctx(si->end);
Willy Tarreau347a35d2013-11-22 17:51:09 +01005422
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005423 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Willy Tarreau347a35d2013-11-22 17:51:09 +01005424 scope_txt[0] = 0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005425 if (appctx->ctx.stats.scope_len) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01005426 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau4e4292b2014-11-28 12:18:45 +01005427 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 +01005428 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005429 }
5430
5431 /* We don't want to land on the posted stats page because a refresh will
5432 * repost the data. We don't want this to happen on accident so we redirect
5433 * the browse to the stats page with a GET.
5434 */
5435 chunk_printf(&trash,
5436 "HTTP/1.1 303 See Other\r\n"
5437 "Cache-Control: no-cache\r\n"
5438 "Content-Type: text/plain\r\n"
5439 "Connection: close\r\n"
5440 "Location: %s;st=%s%s%s%s\r\n"
Willy Tarreaufdfcc9d2016-01-26 13:57:29 +01005441 "Content-length: 0\r\n"
Willy Tarreau347a35d2013-11-22 17:51:09 +01005442 "\r\n",
5443 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005444 ((appctx->ctx.stats.st_code > STAT_STATUS_INIT) &&
5445 (appctx->ctx.stats.st_code < STAT_STATUS_SIZE) &&
5446 stat_status_codes[appctx->ctx.stats.st_code]) ?
5447 stat_status_codes[appctx->ctx.stats.st_code] :
Willy Tarreau347a35d2013-11-22 17:51:09 +01005448 stat_status_codes[STAT_STATUS_UNKN],
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005449 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
5450 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreau347a35d2013-11-22 17:51:09 +01005451 scope_txt);
5452
Willy Tarreaueee5b512015-04-03 23:46:31 +02005453 s->txn->status = 303;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005454 s->logs.tv_request = now;
5455
Willy Tarreaubc18da12015-03-13 14:00:47 +01005456 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02005457 si_applet_cant_put(si);
Willy Tarreau347a35d2013-11-22 17:51:09 +01005458 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01005459 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01005460
5461 return 1;
5462}
5463
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005464/* This I/O handler runs as an applet embedded in a stream interface. It is
5465 * used to send HTTP stats over a TCP socket. The mechanism is very simple.
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005466 * appctx->st0 contains the operation in progress (dump, done). The handler
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005467 * automatically unregisters itself once transfer is complete.
5468 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02005469static void http_stats_io_handler(struct appctx *appctx)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005470{
Willy Tarreau00a37f02015-04-13 12:05:19 +02005471 struct stream_interface *si = appctx->owner;
Willy Tarreau87b09662015-04-03 00:22:06 +02005472 struct stream *s = si_strm(si);
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005473 struct channel *req = si_oc(si);
5474 struct channel *res = si_ic(si);
Willy Tarreau55058a72012-11-21 08:27:21 +01005475
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005476 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
5477 goto out;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01005478
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005479 /* check that the output is not closed */
5480 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005481 appctx->st0 = STAT_HTTP_DONE;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005482
Willy Tarreau347a35d2013-11-22 17:51:09 +01005483 /* all states are processed in sequence */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005484 if (appctx->st0 == STAT_HTTP_HEAD) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01005485 if (stats_send_http_headers(si)) {
Willy Tarreaueee5b512015-04-03 23:46:31 +02005486 if (s->txn->meth == HTTP_METH_HEAD)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005487 appctx->st0 = STAT_HTTP_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005488 else
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005489 appctx->st0 = STAT_HTTP_DUMP;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005490 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01005491 }
5492
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005493 if (appctx->st0 == STAT_HTTP_DUMP) {
Willy Tarreau4e4292b2014-11-28 12:18:45 +01005494 unsigned int prev_len = si_ib(si)->i;
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005495 unsigned int data_len;
5496 unsigned int last_len;
Willy Tarreaucce36482014-04-24 20:26:41 +02005497 unsigned int last_fwd = 0;
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005498
5499 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
5500 /* One difficulty we're facing is that we must prevent
5501 * the input data from being automatically forwarded to
5502 * the output area. For this, we temporarily disable
5503 * forwarding on the channel.
5504 */
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005505 last_fwd = si_ic(si)->to_forward;
5506 si_ic(si)->to_forward = 0;
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005507 chunk_printf(&trash, "\r\n000000\r\n");
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005508 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02005509 si_applet_cant_put(si);
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005510 si_ic(si)->to_forward = last_fwd;
Willy Tarreau828824a2015-04-19 17:20:03 +02005511 goto out;
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005512 }
5513 }
5514
Willy Tarreau4e4292b2014-11-28 12:18:45 +01005515 data_len = si_ib(si)->i;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005516 if (stats_dump_stat_to_buffer(si, s->be->uri_auth))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005517 appctx->st0 = STAT_HTTP_DONE;
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005518
Willy Tarreau4e4292b2014-11-28 12:18:45 +01005519 last_len = si_ib(si)->i;
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005520
5521 /* Now we must either adjust or remove the chunk size. This is
5522 * not easy because the chunk size might wrap at the end of the
5523 * buffer, so we pretend we have nothing in the buffer, we write
5524 * the size, then restore the buffer's contents. Note that we can
5525 * only do that because no forwarding is scheduled on the stats
5526 * applet.
5527 */
5528 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
Willy Tarreau4e4292b2014-11-28 12:18:45 +01005529 si_ic(si)->total -= (last_len - prev_len);
5530 si_ib(si)->i -= (last_len - prev_len);
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005531
5532 if (last_len != data_len) {
5533 chunk_printf(&trash, "\r\n%06x\r\n", (last_len - data_len));
Willy Tarreaubc18da12015-03-13 14:00:47 +01005534 if (bi_putchk(si_ic(si), &trash) == -1)
Willy Tarreaufe127932015-04-21 19:23:39 +02005535 si_applet_cant_put(si);
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005536
Willy Tarreau4e4292b2014-11-28 12:18:45 +01005537 si_ic(si)->total += (last_len - data_len);
5538 si_ib(si)->i += (last_len - data_len);
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005539 }
5540 /* now re-enable forwarding */
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005541 channel_forward(si_ic(si), last_fwd);
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005542 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005543 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02005544
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005545 if (appctx->st0 == STAT_HTTP_POST) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01005546 if (stats_process_http_post(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005547 appctx->st0 = STAT_HTTP_LAST;
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005548 else if (si_oc(si)->flags & CF_SHUTR)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005549 appctx->st0 = STAT_HTTP_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005550 }
5551
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005552 if (appctx->st0 == STAT_HTTP_LAST) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01005553 if (stats_send_http_redirect(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005554 appctx->st0 = STAT_HTTP_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005555 }
5556
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005557 if (appctx->st0 == STAT_HTTP_DONE) {
5558 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
5559 chunk_printf(&trash, "\r\n0\r\n\r\n");
Willy Tarreaubc18da12015-03-13 14:00:47 +01005560 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02005561 si_applet_cant_put(si);
Willy Tarreau828824a2015-04-19 17:20:03 +02005562 goto out;
Willy Tarreaubc18da12015-03-13 14:00:47 +01005563 }
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005564 }
5565 /* eat the whole request */
Willy Tarreau4e4292b2014-11-28 12:18:45 +01005566 bo_skip(si_oc(si), si_ob(si)->o);
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005567 res->flags |= CF_READ_NULL;
5568 si_shutr(si);
5569 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01005570
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005571 if ((res->flags & CF_SHUTR) && (si->state == SI_ST_EST))
5572 si_shutw(si);
Willy Tarreau91861262007-10-17 17:06:05 +02005573
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005574 if (appctx->st0 == STAT_HTTP_DONE) {
Willy Tarreau96d44912013-11-22 12:25:24 +01005575 if ((req->flags & CF_SHUTW) && (si->state == SI_ST_EST)) {
5576 si_shutr(si);
5577 res->flags |= CF_READ_NULL;
5578 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005579 }
Willy Tarreau828824a2015-04-19 17:20:03 +02005580 out:
Willy Tarreaud4da1962015-04-20 01:31:23 +02005581 /* just to make gcc happy */ ;
Willy Tarreau91861262007-10-17 17:06:05 +02005582}
5583
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005584
Willy Tarreau909d5172012-11-26 03:04:41 +01005585static inline const char *get_conn_ctrl_name(const struct connection *conn)
5586{
Willy Tarreau3c728722014-01-23 13:50:42 +01005587 if (!conn_ctrl_ready(conn))
Willy Tarreau909d5172012-11-26 03:04:41 +01005588 return "NONE";
5589 return conn->ctrl->name;
5590}
5591
5592static inline const char *get_conn_xprt_name(const struct connection *conn)
5593{
5594 static char ptr[17];
5595
Willy Tarreauaad69382014-01-23 14:21:42 +01005596 if (!conn_xprt_ready(conn))
Willy Tarreau909d5172012-11-26 03:04:41 +01005597 return "NONE";
5598
5599 if (conn->xprt == &raw_sock)
5600 return "RAW";
5601
5602#ifdef USE_OPENSSL
5603 if (conn->xprt == &ssl_sock)
5604 return "SSL";
5605#endif
5606 snprintf(ptr, sizeof(ptr), "%p", conn->xprt);
5607 return ptr;
5608}
5609
5610static inline const char *get_conn_data_name(const struct connection *conn)
5611{
5612 static char ptr[17];
5613
5614 if (!conn->data)
5615 return "NONE";
5616
5617 if (conn->data == &sess_conn_cb)
5618 return "SESS";
5619
5620 if (conn->data == &si_conn_cb)
5621 return "STRM";
5622
5623 if (conn->data == &check_conn_cb)
5624 return "CHCK";
5625
5626 snprintf(ptr, sizeof(ptr), "%p", conn->data);
5627 return ptr;
5628}
5629
Willy Tarreau87b09662015-04-03 00:22:06 +02005630/* This function dumps a complete stream state onto the stream interface's
5631 * read buffer. The stream has to be set in sess->target. It returns
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01005632 * 0 if the output buffer is full and it needs to be called again, otherwise
5633 * non-zero. It is designed to be called from stats_dump_sess_to_buffer() below.
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005634 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005635static int stats_dump_full_sess_to_buffer(struct stream_interface *si, struct stream *sess)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005636{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005637 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005638 struct tm tm;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005639 extern const char *monthname[12];
5640 char pn[INET6_ADDRSTRLEN];
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005641 struct connection *conn;
Willy Tarreau284ddbf2013-12-01 20:45:00 +01005642 struct appctx *tmpctx;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005643
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005644 chunk_reset(&trash);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005645
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005646 if (appctx->ctx.sess.section > 0 && appctx->ctx.sess.uid != sess->uniq_id) {
Willy Tarreau87b09662015-04-03 00:22:06 +02005647 /* stream changed, no need to go any further */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005648 chunk_appendf(&trash, " *** session terminated while we were watching it ***\n");
Willy Tarreaubc18da12015-03-13 14:00:47 +01005649 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02005650 si_applet_cant_put(si);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005651 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01005652 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005653 appctx->ctx.sess.uid = 0;
5654 appctx->ctx.sess.section = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005655 return 1;
5656 }
5657
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005658 switch (appctx->ctx.sess.section) {
Willy Tarreau87b09662015-04-03 00:22:06 +02005659 case 0: /* main status of the stream */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005660 appctx->ctx.sess.uid = sess->uniq_id;
5661 appctx->ctx.sess.section = 1;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005662 /* fall through */
5663
5664 case 1:
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005665 get_localtime(sess->logs.accept_date.tv_sec, &tm);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005666 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005667 "%p: [%02d/%s/%04d:%02d:%02d:%02d.%06d] id=%u proto=%s",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005668 sess,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005669 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
5670 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(sess->logs.accept_date.tv_usec),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005671 sess->uniq_id,
Willy Tarreau666f5042015-06-17 19:49:52 +02005672 strm_li(sess) ? strm_li(sess)->proto->name : "?");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005673
Willy Tarreaud0d8da92015-04-04 02:10:38 +02005674 conn = objt_conn(strm_orig(sess));
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005675 switch (conn ? addr_to_str(&conn->addr.from, pn, sizeof(pn)) : AF_UNSPEC) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005676 case AF_INET:
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005677 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005678 chunk_appendf(&trash, " source=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005679 pn, get_host_port(&conn->addr.from));
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005680 break;
5681 case AF_UNIX:
Willy Tarreaud0d8da92015-04-04 02:10:38 +02005682 chunk_appendf(&trash, " source=unix:%d\n", strm_li(sess)->luid);
Emeric Brun837ca522010-10-22 16:19:01 +02005683 break;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005684 default:
5685 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005686 chunk_appendf(&trash, "\n");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005687 break;
5688 }
5689
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005690 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005691 " flags=0x%x, conn_retries=%d, srv_conn=%p, pend_pos=%p\n",
Willy Tarreauee28de02010-06-01 09:51:00 +02005692 sess->flags, sess->si[1].conn_retries, sess->srv_conn, sess->pend_pos);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005693
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005694 chunk_appendf(&trash,
Daniel Schultze90690c72012-03-23 10:53:36 -07005695 " frontend=%s (id=%u mode=%s), listener=%s (id=%u)",
Willy Tarreaud0d8da92015-04-04 02:10:38 +02005696 strm_fe(sess)->id, strm_fe(sess)->uuid, strm_fe(sess)->mode ? "http" : "tcp",
5697 strm_li(sess) ? strm_li(sess)->name ? strm_li(sess)->name : "?" : "?",
5698 strm_li(sess) ? strm_li(sess)->luid : 0);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005699
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005700 if (conn)
5701 conn_get_to_addr(conn);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005702
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005703 switch (conn ? addr_to_str(&conn->addr.to, pn, sizeof(pn)) : AF_UNSPEC) {
Daniel Schultze90690c72012-03-23 10:53:36 -07005704 case AF_INET:
5705 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005706 chunk_appendf(&trash, " addr=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005707 pn, get_host_port(&conn->addr.to));
Daniel Schultze90690c72012-03-23 10:53:36 -07005708 break;
5709 case AF_UNIX:
Willy Tarreaud0d8da92015-04-04 02:10:38 +02005710 chunk_appendf(&trash, " addr=unix:%d\n", strm_li(sess)->luid);
Daniel Schultze90690c72012-03-23 10:53:36 -07005711 break;
5712 default:
5713 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005714 chunk_appendf(&trash, "\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07005715 break;
5716 }
5717
Willy Tarreau50943332011-09-02 17:33:05 +02005718 if (sess->be->cap & PR_CAP_BE)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005719 chunk_appendf(&trash,
Daniel Schultze90690c72012-03-23 10:53:36 -07005720 " backend=%s (id=%u mode=%s)",
Willy Tarreau50943332011-09-02 17:33:05 +02005721 sess->be->id,
Daniel Schultze90690c72012-03-23 10:53:36 -07005722 sess->be->uuid, sess->be->mode ? "http" : "tcp");
5723 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005724 chunk_appendf(&trash, " backend=<NONE> (id=-1 mode=-)");
Daniel Schultze90690c72012-03-23 10:53:36 -07005725
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005726 conn = objt_conn(sess->si[1].end);
5727 if (conn)
5728 conn_get_from_addr(conn);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005729
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005730 switch (conn ? addr_to_str(&conn->addr.from, pn, sizeof(pn)) : AF_UNSPEC) {
Daniel Schultze90690c72012-03-23 10:53:36 -07005731 case AF_INET:
5732 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005733 chunk_appendf(&trash, " addr=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005734 pn, get_host_port(&conn->addr.from));
Daniel Schultze90690c72012-03-23 10:53:36 -07005735 break;
5736 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005737 chunk_appendf(&trash, " addr=unix\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07005738 break;
5739 default:
5740 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005741 chunk_appendf(&trash, "\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07005742 break;
5743 }
5744
5745 if (sess->be->cap & PR_CAP_BE)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005746 chunk_appendf(&trash,
Daniel Schultze90690c72012-03-23 10:53:36 -07005747 " server=%s (id=%u)",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005748 objt_server(sess->target) ? objt_server(sess->target)->id : "<none>",
5749 objt_server(sess->target) ? objt_server(sess->target)->puid : 0);
Willy Tarreau50943332011-09-02 17:33:05 +02005750 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005751 chunk_appendf(&trash, " server=<NONE> (id=-1)");
Daniel Schultze90690c72012-03-23 10:53:36 -07005752
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005753 if (conn)
5754 conn_get_to_addr(conn);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005755
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005756 switch (conn ? addr_to_str(&conn->addr.to, pn, sizeof(pn)) : AF_UNSPEC) {
Daniel Schultze90690c72012-03-23 10:53:36 -07005757 case AF_INET:
5758 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005759 chunk_appendf(&trash, " addr=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005760 pn, get_host_port(&conn->addr.to));
Daniel Schultze90690c72012-03-23 10:53:36 -07005761 break;
5762 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005763 chunk_appendf(&trash, " addr=unix\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07005764 break;
5765 default:
5766 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005767 chunk_appendf(&trash, "\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07005768 break;
5769 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005770
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005771 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005772 " task=%p (state=0x%02x nice=%d calls=%d exp=%s%s",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005773 sess->task,
5774 sess->task->state,
5775 sess->task->nice, sess->task->calls,
5776 sess->task->expire ?
5777 tick_is_expired(sess->task->expire, now_ms) ? "<PAST>" :
5778 human_time(TICKS_TO_MS(sess->task->expire - now_ms),
5779 TICKS_TO_MS(1000)) : "<NEVER>",
5780 task_in_rq(sess->task) ? ", running" : "");
5781
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005782 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005783 " age=%s)\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005784 human_time(now.tv_sec - sess->logs.accept_date.tv_sec, 1));
5785
Willy Tarreaueee5b512015-04-03 23:46:31 +02005786 if (sess->txn)
5787 chunk_appendf(&trash,
Willy Tarreau98410192014-11-26 18:05:38 +01005788 " 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 +02005789 sess->txn, sess->txn->flags, sess->txn->meth, sess->txn->status,
5790 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 +01005791
5792 chunk_appendf(&trash,
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02005793 " si[0]=%p (state=%s flags=0x%02x endp0=%s:%p exp=%s, et=0x%03x)\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005794 &sess->si[0],
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005795 si_state_str(sess->si[0].state),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005796 sess->si[0].flags,
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005797 obj_type_name(sess->si[0].end),
5798 obj_base_ptr(sess->si[0].end),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005799 sess->si[0].exp ?
5800 tick_is_expired(sess->si[0].exp, now_ms) ? "<PAST>" :
5801 human_time(TICKS_TO_MS(sess->si[0].exp - now_ms),
5802 TICKS_TO_MS(1000)) : "<NEVER>",
5803 sess->si[0].err_type);
5804
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005805 chunk_appendf(&trash,
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02005806 " si[1]=%p (state=%s flags=0x%02x endp1=%s:%p exp=%s, et=0x%03x)\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005807 &sess->si[1],
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005808 si_state_str(sess->si[1].state),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005809 sess->si[1].flags,
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005810 obj_type_name(sess->si[1].end),
5811 obj_base_ptr(sess->si[1].end),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005812 sess->si[1].exp ?
5813 tick_is_expired(sess->si[1].exp, now_ms) ? "<PAST>" :
5814 human_time(TICKS_TO_MS(sess->si[1].exp - now_ms),
5815 TICKS_TO_MS(1000)) : "<NEVER>",
5816 sess->si[1].err_type);
5817
Willy Tarreau284ddbf2013-12-01 20:45:00 +01005818 if ((conn = objt_conn(sess->si[0].end)) != NULL) {
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005819 chunk_appendf(&trash,
5820 " co0=%p ctrl=%s xprt=%s data=%s target=%s:%p\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005821 conn,
5822 get_conn_ctrl_name(conn),
5823 get_conn_xprt_name(conn),
5824 get_conn_data_name(conn),
5825 obj_type_name(conn->target),
5826 obj_base_ptr(conn->target));
Willy Tarreau909d5172012-11-26 03:04:41 +01005827
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005828 chunk_appendf(&trash,
Willy Tarreau16f649c2014-01-25 19:10:48 +01005829 " flags=0x%08x fd=%d fd.state=%02x fd.cache=%d updt=%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005830 conn->flags,
5831 conn->t.sock.fd,
Willy Tarreau69a41fa2014-01-20 11:02:59 +01005832 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].state : 0,
Willy Tarreau15a4dec2014-01-20 11:09:39 +01005833 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].cache : 0,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005834 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].updated : 0);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005835 }
Willy Tarreau284ddbf2013-12-01 20:45:00 +01005836 else if ((tmpctx = objt_appctx(sess->si[0].end)) != NULL) {
5837 chunk_appendf(&trash,
5838 " app0=%p st0=%d st1=%d st2=%d applet=%s\n",
5839 tmpctx,
5840 tmpctx->st0,
5841 tmpctx->st1,
5842 tmpctx->st2,
5843 tmpctx->applet->name);
5844 }
Willy Tarreaubc174aa2012-11-19 16:10:32 +01005845
Willy Tarreau284ddbf2013-12-01 20:45:00 +01005846 if ((conn = objt_conn(sess->si[1].end)) != NULL) {
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005847 chunk_appendf(&trash,
5848 " co1=%p ctrl=%s xprt=%s data=%s target=%s:%p\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005849 conn,
5850 get_conn_ctrl_name(conn),
5851 get_conn_xprt_name(conn),
5852 get_conn_data_name(conn),
5853 obj_type_name(conn->target),
5854 obj_base_ptr(conn->target));
Willy Tarreau909d5172012-11-26 03:04:41 +01005855
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005856 chunk_appendf(&trash,
Willy Tarreauceeafb52016-01-25 15:27:17 +01005857 " flags=0x%08x fd=%d fd.state=%02x fd.cache=%d updt=%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005858 conn->flags,
5859 conn->t.sock.fd,
Willy Tarreau69a41fa2014-01-20 11:02:59 +01005860 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].state : 0,
Willy Tarreau15a4dec2014-01-20 11:09:39 +01005861 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].cache : 0,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005862 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].updated : 0);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005863 }
Willy Tarreau284ddbf2013-12-01 20:45:00 +01005864 else if ((tmpctx = objt_appctx(sess->si[1].end)) != NULL) {
5865 chunk_appendf(&trash,
5866 " app1=%p st0=%d st1=%d st2=%d applet=%s\n",
5867 tmpctx,
5868 tmpctx->st0,
5869 tmpctx->st1,
5870 tmpctx->st2,
5871 tmpctx->applet->name);
5872 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005873
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005874 chunk_appendf(&trash,
Willy Tarreau909d5172012-11-26 03:04:41 +01005875 " req=%p (f=0x%06x an=0x%x pipe=%d tofwd=%d total=%lld)\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005876 " an_exp=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005877 &sess->req,
5878 sess->req.flags, sess->req.analysers,
5879 sess->req.pipe ? sess->req.pipe->data : 0,
5880 sess->req.to_forward, sess->req.total,
5881 sess->req.analyse_exp ?
5882 human_time(TICKS_TO_MS(sess->req.analyse_exp - now_ms),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005883 TICKS_TO_MS(1000)) : "<NEVER>");
5884
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005885 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005886 " rex=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005887 sess->req.rex ?
5888 human_time(TICKS_TO_MS(sess->req.rex - now_ms),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005889 TICKS_TO_MS(1000)) : "<NEVER>");
5890
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005891 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005892 " wex=%s\n"
Willy Tarreau909d5172012-11-26 03:04:41 +01005893 " buf=%p data=%p o=%d p=%d req.next=%d i=%d size=%d\n",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005894 sess->req.wex ?
5895 human_time(TICKS_TO_MS(sess->req.wex - now_ms),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005896 TICKS_TO_MS(1000)) : "<NEVER>",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005897 sess->req.buf,
5898 sess->req.buf->data, sess->req.buf->o,
5899 (int)(sess->req.buf->p - sess->req.buf->data),
Willy Tarreaueee5b512015-04-03 23:46:31 +02005900 sess->txn ? sess->txn->req.next : 0, sess->req.buf->i,
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005901 sess->req.buf->size);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005902
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005903 chunk_appendf(&trash,
Willy Tarreau909d5172012-11-26 03:04:41 +01005904 " res=%p (f=0x%06x an=0x%x pipe=%d tofwd=%d total=%lld)\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005905 " an_exp=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005906 &sess->res,
5907 sess->res.flags, sess->res.analysers,
5908 sess->res.pipe ? sess->res.pipe->data : 0,
5909 sess->res.to_forward, sess->res.total,
5910 sess->res.analyse_exp ?
5911 human_time(TICKS_TO_MS(sess->res.analyse_exp - now_ms),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005912 TICKS_TO_MS(1000)) : "<NEVER>");
5913
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005914 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005915 " rex=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005916 sess->res.rex ?
5917 human_time(TICKS_TO_MS(sess->res.rex - now_ms),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005918 TICKS_TO_MS(1000)) : "<NEVER>");
5919
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005920 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005921 " wex=%s\n"
Willy Tarreau909d5172012-11-26 03:04:41 +01005922 " buf=%p data=%p o=%d p=%d rsp.next=%d i=%d size=%d\n",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005923 sess->res.wex ?
5924 human_time(TICKS_TO_MS(sess->res.wex - now_ms),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005925 TICKS_TO_MS(1000)) : "<NEVER>",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005926 sess->res.buf,
5927 sess->res.buf->data, sess->res.buf->o,
5928 (int)(sess->res.buf->p - sess->res.buf->data),
Willy Tarreaueee5b512015-04-03 23:46:31 +02005929 sess->txn ? sess->txn->rsp.next : 0, sess->res.buf->i,
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01005930 sess->res.buf->size);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005931
Willy Tarreaubc18da12015-03-13 14:00:47 +01005932 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02005933 si_applet_cant_put(si);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005934 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01005935 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005936
5937 /* use other states to dump the contents */
5938 }
5939 /* end of dump */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005940 appctx->ctx.sess.uid = 0;
5941 appctx->ctx.sess.section = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005942 return 1;
5943}
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01005944
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02005945#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
5946static int stats_tlskeys_list(struct stream_interface *si) {
5947 struct appctx *appctx = __objt_appctx(si->end);
5948
5949 switch (appctx->st2) {
5950 case STAT_ST_INIT:
5951 /* Display the column headers. If the message cannot be sent,
5952 * quit the fucntion with returning 0. The function is called
5953 * later and restart at the state "STAT_ST_INIT".
5954 */
5955 chunk_reset(&trash);
5956 chunk_appendf(&trash, "# id (file)\n");
5957 if (bi_putchk(si_ic(si), &trash) == -1) {
5958 si_applet_cant_put(si);
5959 return 0;
5960 }
5961
5962 /* Now, we start the browsing of the references lists.
5963 * Note that the following call to LIST_ELEM return bad pointer. The only
5964 * avalaible field of this pointer is <list>. It is used with the function
5965 * tlskeys_list_get_next() for retruning the first avalaible entry
5966 */
5967 appctx->ctx.tlskeys.ref = LIST_ELEM(&tlskeys_reference, struct tls_keys_ref *, list);
5968 appctx->ctx.tlskeys.ref = tlskeys_list_get_next(appctx->ctx.tlskeys.ref, &tlskeys_reference);
5969
5970 appctx->st2 = STAT_ST_LIST;
5971 /* fall through */
5972
5973 case STAT_ST_LIST:
5974 while (appctx->ctx.tlskeys.ref) {
5975 chunk_reset(&trash);
5976
5977 chunk_appendf(&trash, "%d (%s)\n", appctx->ctx.tlskeys.ref->unique_id,
5978 appctx->ctx.tlskeys.ref->filename);
5979
5980 if (bi_putchk(si_ic(si), &trash) == -1) {
5981 /* let's try again later from this stream. We add ourselves into
5982 * this stream's users so that it can remove us upon termination.
5983 */
5984 si_applet_cant_put(si);
5985 return 0;
5986 }
5987
5988 /* get next list entry and check the end of the list */
5989 appctx->ctx.tlskeys.ref = tlskeys_list_get_next(appctx->ctx.tlskeys.ref, &tlskeys_reference);
5990 }
5991
5992 appctx->st2 = STAT_ST_FIN;
5993 /* fall through */
5994
5995 default:
5996 appctx->st2 = STAT_ST_FIN;
5997 return 1;
5998 }
5999 return 0;
6000}
6001#endif
6002
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006003static int stats_pats_list(struct stream_interface *si)
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006004{
6005 struct appctx *appctx = __objt_appctx(si->end);
6006
6007 switch (appctx->st2) {
6008 case STAT_ST_INIT:
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01006009 /* Display the column headers. If the message cannot be sent,
6010 * quit the fucntion with returning 0. The function is called
6011 * later and restart at the state "STAT_ST_INIT".
6012 */
6013 chunk_reset(&trash);
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01006014 chunk_appendf(&trash, "# id (file) description\n");
Willy Tarreaubc18da12015-03-13 14:00:47 +01006015 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02006016 si_applet_cant_put(si);
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01006017 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01006018 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006019
6020 /* Now, we start the browsing of the references lists.
6021 * Note that the following call to LIST_ELEM return bad pointer. The only
6022 * avalaible field of this pointer is <list>. It is used with the function
6023 * pat_list_get_next() for retruning the first avalaible entry
6024 */
6025 appctx->ctx.map.ref = LIST_ELEM(&pattern_reference, struct pat_ref *, list);
6026 appctx->ctx.map.ref = pat_list_get_next(appctx->ctx.map.ref, &pattern_reference,
6027 appctx->ctx.map.display_flags);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006028 appctx->st2 = STAT_ST_LIST;
6029 /* fall through */
6030
6031 case STAT_ST_LIST:
6032 while (appctx->ctx.map.ref) {
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006033 chunk_reset(&trash);
6034
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006035 /* Build messages. If the reference is used by another category than
6036 * the listed categorie, display the information in the massage.
6037 */
Thierry FOURNIERf7e04e92014-03-20 11:45:47 +01006038 chunk_appendf(&trash, "%d (%s) %s\n", appctx->ctx.map.ref->unique_id,
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01006039 appctx->ctx.map.ref->reference ? appctx->ctx.map.ref->reference : "",
6040 appctx->ctx.map.ref->display);
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01006041
Willy Tarreau2bb4a962014-11-28 11:11:05 +01006042 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau87b09662015-04-03 00:22:06 +02006043 /* let's try again later from this stream. We add ourselves into
6044 * this stream's users so that it can remove us upon termination.
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006045 */
Willy Tarreaufe127932015-04-21 19:23:39 +02006046 si_applet_cant_put(si);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006047 return 0;
6048 }
6049
6050 /* get next list entry and check the end of the list */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006051 appctx->ctx.map.ref = pat_list_get_next(appctx->ctx.map.ref, &pattern_reference,
6052 appctx->ctx.map.display_flags);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006053 }
6054
6055 appctx->st2 = STAT_ST_FIN;
6056 /* fall through */
6057
6058 default:
6059 appctx->st2 = STAT_ST_FIN;
6060 return 1;
6061 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006062 return 0;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006063}
6064
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006065static int stats_map_lookup(struct stream_interface *si)
6066{
6067 struct appctx *appctx = __objt_appctx(si->end);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006068 struct sample sample;
6069 struct pattern *pat;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006070 int match_method;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006071
6072 switch (appctx->st2) {
6073 case STAT_ST_INIT:
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006074 /* Init to the first entry. The list cannot be change */
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01006075 appctx->ctx.map.expr = LIST_ELEM(&appctx->ctx.map.ref->pat, struct pattern_expr *, list);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006076 appctx->ctx.map.expr = pat_expr_get_next(appctx->ctx.map.expr, &appctx->ctx.map.ref->pat);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006077 appctx->st2 = STAT_ST_LIST;
6078 /* fall through */
6079
6080 case STAT_ST_LIST:
6081 /* for each lookup type */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006082 while (appctx->ctx.map.expr) {
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006083 /* initialise chunk to build new message */
6084 chunk_reset(&trash);
6085
6086 /* execute pattern matching */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02006087 sample.data.type = SMP_T_STR;
Andreas Seltenreich9727cf42016-03-03 19:32:25 +01006088 sample.flags = SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02006089 sample.data.u.str.len = appctx->ctx.map.chunk.len;
6090 sample.data.u.str.str = appctx->ctx.map.chunk.str;
Thierry FOURNIER5d344082014-01-27 14:19:53 +01006091 if (appctx->ctx.map.expr->pat_head->match &&
6092 sample_convert(&sample, appctx->ctx.map.expr->pat_head->expect_type))
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006093 pat = appctx->ctx.map.expr->pat_head->match(&sample, appctx->ctx.map.expr, 1);
6094 else
6095 pat = NULL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006096
6097 /* build return message: set type of match */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006098 for (match_method=0; match_method<PAT_MATCH_NUM; match_method++)
6099 if (appctx->ctx.map.expr->pat_head->match == pat_match_fcts[match_method])
6100 break;
6101 if (match_method >= PAT_MATCH_NUM)
6102 chunk_appendf(&trash, "type=unknown(%p)", appctx->ctx.map.expr->pat_head->match);
6103 else
6104 chunk_appendf(&trash, "type=%s", pat_match_names[match_method]);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006105
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02006106 /* case sensitive */
6107 if (appctx->ctx.map.expr->mflags & PAT_MF_IGNORE_CASE)
6108 chunk_appendf(&trash, ", case=insensitive");
6109 else
6110 chunk_appendf(&trash, ", case=sensitive");
6111
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006112 /* Display no match, and set default value */
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01006113 if (!pat) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006114 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
6115 chunk_appendf(&trash, ", found=no");
6116 else
6117 chunk_appendf(&trash, ", match=no");
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006118 }
6119
6120 /* Display match and match info */
6121 else {
6122 /* display match */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006123 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
6124 chunk_appendf(&trash, ", found=yes");
6125 else
6126 chunk_appendf(&trash, ", match=yes");
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006127
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01006128 /* display index mode */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02006129 if (pat->sflags & PAT_SF_TREE)
Thierry FOURNIERb9903842014-03-11 18:48:17 +01006130 chunk_appendf(&trash, ", idx=tree");
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006131 else
Thierry FOURNIERb9903842014-03-11 18:48:17 +01006132 chunk_appendf(&trash, ", idx=list");
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006133
Thierry FOURNIER86db66a2014-03-11 18:37:58 +01006134 /* display pattern */
6135 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
6136 if (pat->ref && pat->ref->pattern)
6137 chunk_appendf(&trash, ", key=\"%s\"", pat->ref->pattern);
6138 else
6139 chunk_appendf(&trash, ", key=unknown");
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006140 }
6141 else {
Thierry FOURNIER86db66a2014-03-11 18:37:58 +01006142 if (pat->ref && pat->ref->pattern)
6143 chunk_appendf(&trash, ", pattern=\"%s\"", pat->ref->pattern);
6144 else
6145 chunk_appendf(&trash, ", pattern=unknown");
6146 }
6147
6148 /* display return value */
6149 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
Thierry FOURNIER503bb092015-08-19 08:35:43 +02006150 if (pat->data && pat->ref && pat->ref->sample)
Thierry FOURNIER03d0e452015-07-23 18:33:41 +02006151 chunk_appendf(&trash, ", value=\"%s\", type=\"%s\"", pat->ref->sample,
Thierry FOURNIER7d4335c2015-08-19 09:05:25 +02006152 smp_to_type[pat->data->type]);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006153 else
Thierry FOURNIER86db66a2014-03-11 18:37:58 +01006154 chunk_appendf(&trash, ", value=none");
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006155 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006156 }
6157
Thierry FOURNIERb9903842014-03-11 18:48:17 +01006158 chunk_appendf(&trash, "\n");
6159
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006160 /* display response */
Willy Tarreau2bb4a962014-11-28 11:11:05 +01006161 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau87b09662015-04-03 00:22:06 +02006162 /* let's try again later from this stream. We add ourselves into
6163 * this stream's users so that it can remove us upon termination.
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006164 */
Willy Tarreaufe127932015-04-21 19:23:39 +02006165 si_applet_cant_put(si);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006166 return 0;
6167 }
6168
6169 /* get next entry */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006170 appctx->ctx.map.expr = pat_expr_get_next(appctx->ctx.map.expr,
6171 &appctx->ctx.map.ref->pat);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006172 }
6173
6174 appctx->st2 = STAT_ST_FIN;
6175 /* fall through */
6176
6177 default:
6178 appctx->st2 = STAT_ST_FIN;
6179 free(appctx->ctx.map.chunk.str);
6180 return 1;
6181 }
6182}
6183
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006184static int stats_pat_list(struct stream_interface *si)
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006185{
6186 struct appctx *appctx = __objt_appctx(si->end);
6187
6188 switch (appctx->st2) {
6189
6190 case STAT_ST_INIT:
6191 /* Init to the first entry. The list cannot be change */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006192 appctx->ctx.map.elt = LIST_NEXT(&appctx->ctx.map.ref->head,
6193 struct pat_ref_elt *, list);
6194 if (&appctx->ctx.map.elt->list == &appctx->ctx.map.ref->head)
6195 appctx->ctx.map.elt = NULL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006196 appctx->st2 = STAT_ST_LIST;
6197 /* fall through */
6198
6199 case STAT_ST_LIST:
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006200 while (appctx->ctx.map.elt) {
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006201 chunk_reset(&trash);
6202
6203 /* build messages */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006204 if (appctx->ctx.map.elt->sample)
Thierry FOURNIER9356c682014-01-28 15:55:37 +01006205 chunk_appendf(&trash, "%p %s %s\n",
6206 appctx->ctx.map.elt, appctx->ctx.map.elt->pattern,
6207 appctx->ctx.map.elt->sample);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006208 else
Thierry FOURNIER9356c682014-01-28 15:55:37 +01006209 chunk_appendf(&trash, "%p %s\n",
6210 appctx->ctx.map.elt, appctx->ctx.map.elt->pattern);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006211
Willy Tarreau2bb4a962014-11-28 11:11:05 +01006212 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau87b09662015-04-03 00:22:06 +02006213 /* let's try again later from this stream. We add ourselves into
6214 * this stream's users so that it can remove us upon termination.
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006215 */
Willy Tarreaufe127932015-04-21 19:23:39 +02006216 si_applet_cant_put(si);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006217 return 0;
6218 }
6219
6220 /* get next list entry and check the end of the list */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006221 appctx->ctx.map.elt = LIST_NEXT(&appctx->ctx.map.elt->list,
6222 struct pat_ref_elt *, list);
6223 if (&appctx->ctx.map.elt->list == &appctx->ctx.map.ref->head)
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006224 break;
6225 }
6226
6227 appctx->st2 = STAT_ST_FIN;
6228 /* fall through */
6229
6230 default:
6231 appctx->st2 = STAT_ST_FIN;
6232 return 1;
6233 }
6234}
6235
Willy Tarreau87b09662015-04-03 00:22:06 +02006236/* This function dumps all streams' states onto the stream interface's
Willy Tarreau306f8302013-07-08 15:53:06 +02006237 * read buffer. It returns 0 if the output buffer is full and it needs
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01006238 * to be called again, otherwise non-zero. It is designed to be called
6239 * from stats_dump_sess_to_buffer() below.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006240 */
Simon Horman9bd2c732011-06-15 15:18:44 +09006241static int stats_dump_sess_to_buffer(struct stream_interface *si)
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006242{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006243 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006244 struct connection *conn;
6245
Willy Tarreau2bb4a962014-11-28 11:11:05 +01006246 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW))) {
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006247 /* If we're forced to shut down, we might have to remove our
Willy Tarreau87b09662015-04-03 00:22:06 +02006248 * reference to the last stream being dumped.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006249 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006250 if (appctx->st2 == STAT_ST_LIST) {
6251 if (!LIST_ISEMPTY(&appctx->ctx.sess.bref.users)) {
6252 LIST_DEL(&appctx->ctx.sess.bref.users);
6253 LIST_INIT(&appctx->ctx.sess.bref.users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +01006254 }
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006255 }
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02006256 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006257 }
6258
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006259 chunk_reset(&trash);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006260
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006261 switch (appctx->st2) {
Willy Tarreau295a8372011-03-10 11:25:07 +01006262 case STAT_ST_INIT:
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006263 /* the function had not been called yet, let's prepare the
Willy Tarreau87b09662015-04-03 00:22:06 +02006264 * buffer for a response. We initialize the current stream
Willy Tarreaufd3828e2009-02-22 15:17:24 +01006265 * pointer to the first in the global list. When a target
Willy Tarreau87b09662015-04-03 00:22:06 +02006266 * stream is being destroyed, it is responsible for updating
Willy Tarreaufd3828e2009-02-22 15:17:24 +01006267 * this pointer. We know we have reached the end when this
Willy Tarreau87b09662015-04-03 00:22:06 +02006268 * pointer points back to the head of the streams list.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006269 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006270 LIST_INIT(&appctx->ctx.sess.bref.users);
Willy Tarreau87b09662015-04-03 00:22:06 +02006271 appctx->ctx.sess.bref.ref = streams.n;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006272 appctx->st2 = STAT_ST_LIST;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006273 /* fall through */
6274
Willy Tarreau295a8372011-03-10 11:25:07 +01006275 case STAT_ST_LIST:
Willy Tarreau87b09662015-04-03 00:22:06 +02006276 /* first, let's detach the back-ref from a possible previous stream */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006277 if (!LIST_ISEMPTY(&appctx->ctx.sess.bref.users)) {
6278 LIST_DEL(&appctx->ctx.sess.bref.users);
6279 LIST_INIT(&appctx->ctx.sess.bref.users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +01006280 }
6281
6282 /* and start from where we stopped */
Willy Tarreau87b09662015-04-03 00:22:06 +02006283 while (appctx->ctx.sess.bref.ref != &streams) {
Cyril Bontéacd7d632010-11-01 19:26:02 +01006284 char pn[INET6_ADDRSTRLEN];
Willy Tarreau87b09662015-04-03 00:22:06 +02006285 struct stream *curr_sess;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006286
Willy Tarreau87b09662015-04-03 00:22:06 +02006287 curr_sess = LIST_ELEM(appctx->ctx.sess.bref.ref, struct stream *, list);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006288
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006289 if (appctx->ctx.sess.target) {
6290 if (appctx->ctx.sess.target != (void *)-1 && appctx->ctx.sess.target != curr_sess)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006291 goto next_sess;
6292
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006293 LIST_ADDQ(&curr_sess->back_refs, &appctx->ctx.sess.bref.users);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006294 /* call the proper dump() function and return if we're missing space */
Willy Tarreau76153662012-11-26 01:16:39 +01006295 if (!stats_dump_full_sess_to_buffer(si, curr_sess))
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006296 return 0;
6297
Willy Tarreau87b09662015-04-03 00:22:06 +02006298 /* stream dump complete */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006299 LIST_DEL(&appctx->ctx.sess.bref.users);
6300 LIST_INIT(&appctx->ctx.sess.bref.users);
6301 if (appctx->ctx.sess.target != (void *)-1) {
6302 appctx->ctx.sess.target = NULL;
Willy Tarreau76153662012-11-26 01:16:39 +01006303 break;
6304 }
6305 else
6306 goto next_sess;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006307 }
6308
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006309 chunk_appendf(&trash,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006310 "%p: proto=%s",
6311 curr_sess,
Willy Tarreauf1e02122015-09-23 12:16:43 +02006312 strm_li(curr_sess) ? strm_li(curr_sess)->proto->name : "?");
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006313
Willy Tarreaud0d8da92015-04-04 02:10:38 +02006314 conn = objt_conn(strm_orig(curr_sess));
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006315 switch (conn ? addr_to_str(&conn->addr.from, pn, sizeof(pn)) : AF_UNSPEC) {
Willy Tarreau631f01c2011-09-05 00:36:48 +02006316 case AF_INET:
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006317 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006318 chunk_appendf(&trash,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006319 " src=%s:%d fe=%s be=%s srv=%s",
6320 pn,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006321 get_host_port(&conn->addr.from),
Willy Tarreaud0d8da92015-04-04 02:10:38 +02006322 strm_fe(curr_sess)->id,
Willy Tarreau50943332011-09-02 17:33:05 +02006323 (curr_sess->be->cap & PR_CAP_BE) ? curr_sess->be->id : "<NONE>",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006324 objt_server(curr_sess->target) ? objt_server(curr_sess->target)->id : "<none>"
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006325 );
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006326 break;
6327 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006328 chunk_appendf(&trash,
Emeric Brun837ca522010-10-22 16:19:01 +02006329 " src=unix:%d fe=%s be=%s srv=%s",
Willy Tarreaud0d8da92015-04-04 02:10:38 +02006330 strm_li(curr_sess)->luid,
6331 strm_fe(curr_sess)->id,
Willy Tarreau50943332011-09-02 17:33:05 +02006332 (curr_sess->be->cap & PR_CAP_BE) ? curr_sess->be->id : "<NONE>",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006333 objt_server(curr_sess->target) ? objt_server(curr_sess->target)->id : "<none>"
Emeric Brun837ca522010-10-22 16:19:01 +02006334 );
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006335 break;
6336 }
6337
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006338 chunk_appendf(&trash,
Willy Tarreau65671ab2009-10-04 14:24:59 +02006339 " ts=%02x age=%s calls=%d",
6340 curr_sess->task->state,
Willy Tarreau3884cba2009-03-28 17:54:35 +01006341 human_time(now.tv_sec - curr_sess->logs.tv_accept.tv_sec, 1),
6342 curr_sess->task->calls);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006343
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006344 chunk_appendf(&trash,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01006345 " rq[f=%06xh,i=%d,an=%02xh,rx=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006346 curr_sess->req.flags,
6347 curr_sess->req.buf->i,
6348 curr_sess->req.analysers,
6349 curr_sess->req.rex ?
6350 human_time(TICKS_TO_MS(curr_sess->req.rex - now_ms),
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006351 TICKS_TO_MS(1000)) : "");
6352
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006353 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006354 ",wx=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006355 curr_sess->req.wex ?
6356 human_time(TICKS_TO_MS(curr_sess->req.wex - now_ms),
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006357 TICKS_TO_MS(1000)) : "");
6358
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006359 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006360 ",ax=%s]",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006361 curr_sess->req.analyse_exp ?
6362 human_time(TICKS_TO_MS(curr_sess->req.analyse_exp - now_ms),
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006363 TICKS_TO_MS(1000)) : "");
6364
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006365 chunk_appendf(&trash,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01006366 " rp[f=%06xh,i=%d,an=%02xh,rx=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006367 curr_sess->res.flags,
6368 curr_sess->res.buf->i,
6369 curr_sess->res.analysers,
6370 curr_sess->res.rex ?
6371 human_time(TICKS_TO_MS(curr_sess->res.rex - now_ms),
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006372 TICKS_TO_MS(1000)) : "");
6373
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006374 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006375 ",wx=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006376 curr_sess->res.wex ?
6377 human_time(TICKS_TO_MS(curr_sess->res.wex - now_ms),
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006378 TICKS_TO_MS(1000)) : "");
6379
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006380 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006381 ",ax=%s]",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006382 curr_sess->res.analyse_exp ?
6383 human_time(TICKS_TO_MS(curr_sess->res.analyse_exp - now_ms),
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006384 TICKS_TO_MS(1000)) : "");
6385
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006386 conn = objt_conn(curr_sess->si[0].end);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006387 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006388 " s0=[%d,%1xh,fd=%d,ex=%s]",
6389 curr_sess->si[0].state,
6390 curr_sess->si[0].flags,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006391 conn ? conn->t.sock.fd : -1,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006392 curr_sess->si[0].exp ?
6393 human_time(TICKS_TO_MS(curr_sess->si[0].exp - now_ms),
6394 TICKS_TO_MS(1000)) : "");
6395
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006396 conn = objt_conn(curr_sess->si[1].end);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006397 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006398 " s1=[%d,%1xh,fd=%d,ex=%s]",
6399 curr_sess->si[1].state,
6400 curr_sess->si[1].flags,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006401 conn ? conn->t.sock.fd : -1,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006402 curr_sess->si[1].exp ?
6403 human_time(TICKS_TO_MS(curr_sess->si[1].exp - now_ms),
6404 TICKS_TO_MS(1000)) : "");
6405
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006406 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006407 " exp=%s",
6408 curr_sess->task->expire ?
6409 human_time(TICKS_TO_MS(curr_sess->task->expire - now_ms),
6410 TICKS_TO_MS(1000)) : "");
Willy Tarreau4726f532009-03-07 17:25:21 +01006411 if (task_in_rq(curr_sess->task))
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006412 chunk_appendf(&trash, " run(nice=%d)", curr_sess->task->nice);
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006413
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006414 chunk_appendf(&trash, "\n");
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006415
Willy Tarreau2bb4a962014-11-28 11:11:05 +01006416 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau87b09662015-04-03 00:22:06 +02006417 /* let's try again later from this stream. We add ourselves into
6418 * this stream's users so that it can remove us upon termination.
Willy Tarreaufd3828e2009-02-22 15:17:24 +01006419 */
Willy Tarreaufe127932015-04-21 19:23:39 +02006420 si_applet_cant_put(si);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006421 LIST_ADDQ(&curr_sess->back_refs, &appctx->ctx.sess.bref.users);
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02006422 return 0;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006423 }
6424
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006425 next_sess:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006426 appctx->ctx.sess.bref.ref = curr_sess->list.n;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006427 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006428
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006429 if (appctx->ctx.sess.target && appctx->ctx.sess.target != (void *)-1) {
Willy Tarreau87b09662015-04-03 00:22:06 +02006430 /* specified stream not found */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006431 if (appctx->ctx.sess.section > 0)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006432 chunk_appendf(&trash, " *** session terminated while we were watching it ***\n");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006433 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006434 chunk_appendf(&trash, "Session not found.\n");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006435
Willy Tarreaubc18da12015-03-13 14:00:47 +01006436 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02006437 si_applet_cant_put(si);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006438 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01006439 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006440
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006441 appctx->ctx.sess.target = NULL;
6442 appctx->ctx.sess.uid = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006443 return 1;
6444 }
6445
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006446 appctx->st2 = STAT_ST_FIN;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006447 /* fall through */
6448
6449 default:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006450 appctx->st2 = STAT_ST_FIN;
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02006451 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006452 }
Emeric Brun1e029aa2010-09-23 18:12:53 +02006453}
6454
Willy Tarreau5f9a8772012-11-26 02:22:40 +01006455/* This is called when the stream interface is closed. For instance, upon an
6456 * external abort, we won't call the i/o handler anymore so we may need to
Willy Tarreau87b09662015-04-03 00:22:06 +02006457 * remove back references to the stream currently being dumped.
Willy Tarreau5f9a8772012-11-26 02:22:40 +01006458 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02006459static void cli_release_handler(struct appctx *appctx)
Willy Tarreau5f9a8772012-11-26 02:22:40 +01006460{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006461 if (appctx->st0 == STAT_CLI_O_SESS && appctx->st2 == STAT_ST_LIST) {
6462 if (!LIST_ISEMPTY(&appctx->ctx.sess.bref.users))
6463 LIST_DEL(&appctx->ctx.sess.bref.users);
Willy Tarreau5f9a8772012-11-26 02:22:40 +01006464 }
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01006465 else if (appctx->st0 == STAT_CLI_PRINT_FREE) {
6466 free(appctx->ctx.cli.err);
Willy Tarreau6457d0f2015-10-07 20:00:24 +02006467 appctx->ctx.cli.err = NULL;
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01006468 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006469 else if (appctx->st0 == STAT_CLI_O_MLOOK) {
6470 free(appctx->ctx.map.chunk.str);
Willy Tarreau6457d0f2015-10-07 20:00:24 +02006471 appctx->ctx.map.chunk.str = NULL;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006472 }
Willy Tarreau5f9a8772012-11-26 02:22:40 +01006473}
6474
Willy Tarreau20e99322013-04-13 09:22:25 +02006475/* This function is used to either dump tables states (when action is set
6476 * to STAT_CLI_O_TAB) or clear tables (when action is STAT_CLI_O_CLR).
Willy Tarreau20e99322013-04-13 09:22:25 +02006477 * It returns 0 if the output buffer is full and it needs to be called
6478 * again, otherwise non-zero.
Willy Tarreau69f58c82010-07-12 17:55:33 +02006479 */
Willy Tarreau20e99322013-04-13 09:22:25 +02006480static int stats_table_request(struct stream_interface *si, int action)
Willy Tarreau69f58c82010-07-12 17:55:33 +02006481{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006482 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau87b09662015-04-03 00:22:06 +02006483 struct stream *s = si_strm(si);
Willy Tarreau69f58c82010-07-12 17:55:33 +02006484 struct ebmb_node *eb;
6485 int dt;
Willy Tarreau44455022012-12-05 23:01:12 +01006486 int skip_entry;
Willy Tarreau20e99322013-04-13 09:22:25 +02006487 int show = action == STAT_CLI_O_TAB;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006488
6489 /*
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006490 * We have 3 possible states in appctx->st2 :
Willy Tarreau295a8372011-03-10 11:25:07 +01006491 * - STAT_ST_INIT : the first call
6492 * - STAT_ST_INFO : the proxy pointer points to the next table to
Willy Tarreau69f58c82010-07-12 17:55:33 +02006493 * dump, the entry pointer is NULL ;
Willy Tarreau295a8372011-03-10 11:25:07 +01006494 * - STAT_ST_LIST : the proxy pointer points to the current table
Willy Tarreau69f58c82010-07-12 17:55:33 +02006495 * and the entry pointer points to the next entry to be dumped,
6496 * and the refcount on the next entry is held ;
Willy Tarreau295a8372011-03-10 11:25:07 +01006497 * - STAT_ST_END : nothing left to dump, the buffer may contain some
Willy Tarreau69f58c82010-07-12 17:55:33 +02006498 * data though.
6499 */
6500
Willy Tarreau2bb4a962014-11-28 11:11:05 +01006501 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW))) {
Willy Tarreau69f58c82010-07-12 17:55:33 +02006502 /* in case of abort, remove any refcount we might have set on an entry */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006503 if (appctx->st2 == STAT_ST_LIST) {
6504 appctx->ctx.table.entry->ref_cnt--;
6505 stksess_kill_if_expired(&appctx->ctx.table.proxy->table, appctx->ctx.table.entry);
Willy Tarreauf6efda12010-08-03 20:34:06 +02006506 }
Willy Tarreau69f58c82010-07-12 17:55:33 +02006507 return 1;
6508 }
6509
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006510 chunk_reset(&trash);
Willy Tarreau69f58c82010-07-12 17:55:33 +02006511
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006512 while (appctx->st2 != STAT_ST_FIN) {
6513 switch (appctx->st2) {
Willy Tarreau295a8372011-03-10 11:25:07 +01006514 case STAT_ST_INIT:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006515 appctx->ctx.table.proxy = appctx->ctx.table.target;
6516 if (!appctx->ctx.table.proxy)
6517 appctx->ctx.table.proxy = proxy;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006518
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006519 appctx->ctx.table.entry = NULL;
6520 appctx->st2 = STAT_ST_INFO;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006521 break;
6522
Willy Tarreau295a8372011-03-10 11:25:07 +01006523 case STAT_ST_INFO:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006524 if (!appctx->ctx.table.proxy ||
6525 (appctx->ctx.table.target &&
6526 appctx->ctx.table.proxy != appctx->ctx.table.target)) {
6527 appctx->st2 = STAT_ST_END;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006528 break;
6529 }
6530
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006531 if (appctx->ctx.table.proxy->table.size) {
6532 if (show && !stats_dump_table_head_to_buffer(&trash, si, appctx->ctx.table.proxy,
6533 appctx->ctx.table.target))
Willy Tarreau69f58c82010-07-12 17:55:33 +02006534 return 0;
6535
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006536 if (appctx->ctx.table.target &&
Willy Tarreaud0d8da92015-04-04 02:10:38 +02006537 strm_li(s)->bind_conf->level >= ACCESS_LVL_OPER) {
Willy Tarreau69f58c82010-07-12 17:55:33 +02006538 /* dump entries only if table explicitly requested */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006539 eb = ebmb_first(&appctx->ctx.table.proxy->table.keys);
Willy Tarreau69f58c82010-07-12 17:55:33 +02006540 if (eb) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006541 appctx->ctx.table.entry = ebmb_entry(eb, struct stksess, key);
6542 appctx->ctx.table.entry->ref_cnt++;
6543 appctx->st2 = STAT_ST_LIST;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006544 break;
6545 }
6546 }
6547 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006548 appctx->ctx.table.proxy = appctx->ctx.table.proxy->next;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006549 break;
6550
Willy Tarreau295a8372011-03-10 11:25:07 +01006551 case STAT_ST_LIST:
Willy Tarreau44455022012-12-05 23:01:12 +01006552 skip_entry = 0;
Simon Hormanc88b8872011-06-15 15:18:49 +09006553
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006554 if (appctx->ctx.table.data_type >= 0) {
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02006555 /* we're filtering on some data contents */
6556 void *ptr;
6557 long long data;
6558
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006559 dt = appctx->ctx.table.data_type;
6560 ptr = stktable_data_ptr(&appctx->ctx.table.proxy->table,
6561 appctx->ctx.table.entry,
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02006562 dt);
6563
6564 data = 0;
6565 switch (stktable_data_types[dt].std_type) {
6566 case STD_T_SINT:
6567 data = stktable_data_cast(ptr, std_t_sint);
6568 break;
6569 case STD_T_UINT:
6570 data = stktable_data_cast(ptr, std_t_uint);
6571 break;
6572 case STD_T_ULL:
6573 data = stktable_data_cast(ptr, std_t_ull);
6574 break;
6575 case STD_T_FRQP:
6576 data = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006577 appctx->ctx.table.proxy->table.data_arg[dt].u);
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02006578 break;
6579 }
6580
6581 /* skip the entry if the data does not match the test and the value */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006582 if ((data < appctx->ctx.table.value &&
6583 (appctx->ctx.table.data_op == STD_OP_EQ ||
6584 appctx->ctx.table.data_op == STD_OP_GT ||
6585 appctx->ctx.table.data_op == STD_OP_GE)) ||
6586 (data == appctx->ctx.table.value &&
6587 (appctx->ctx.table.data_op == STD_OP_NE ||
6588 appctx->ctx.table.data_op == STD_OP_GT ||
6589 appctx->ctx.table.data_op == STD_OP_LT)) ||
6590 (data > appctx->ctx.table.value &&
6591 (appctx->ctx.table.data_op == STD_OP_EQ ||
6592 appctx->ctx.table.data_op == STD_OP_LT ||
6593 appctx->ctx.table.data_op == STD_OP_LE)))
Willy Tarreau44455022012-12-05 23:01:12 +01006594 skip_entry = 1;
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02006595 }
6596
Simon Hormanc88b8872011-06-15 15:18:49 +09006597 if (show && !skip_entry &&
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006598 !stats_dump_table_entry_to_buffer(&trash, si, appctx->ctx.table.proxy,
6599 appctx->ctx.table.entry))
Simon Hormand9366582011-06-15 15:18:45 +09006600 return 0;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006601
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006602 appctx->ctx.table.entry->ref_cnt--;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006603
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006604 eb = ebmb_next(&appctx->ctx.table.entry->key);
Willy Tarreau69f58c82010-07-12 17:55:33 +02006605 if (eb) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006606 struct stksess *old = appctx->ctx.table.entry;
6607 appctx->ctx.table.entry = ebmb_entry(eb, struct stksess, key);
Willy Tarreau8fa52f42012-01-09 11:50:03 +01006608 if (show)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006609 stksess_kill_if_expired(&appctx->ctx.table.proxy->table, old);
6610 else if (!skip_entry && !appctx->ctx.table.entry->ref_cnt)
6611 stksess_kill(&appctx->ctx.table.proxy->table, old);
6612 appctx->ctx.table.entry->ref_cnt++;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006613 break;
6614 }
6615
Simon Hormanc88b8872011-06-15 15:18:49 +09006616
6617 if (show)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006618 stksess_kill_if_expired(&appctx->ctx.table.proxy->table, appctx->ctx.table.entry);
6619 else if (!skip_entry && !appctx->ctx.table.entry->ref_cnt)
6620 stksess_kill(&appctx->ctx.table.proxy->table, appctx->ctx.table.entry);
Simon Hormanc88b8872011-06-15 15:18:49 +09006621
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006622 appctx->ctx.table.proxy = appctx->ctx.table.proxy->next;
6623 appctx->st2 = STAT_ST_INFO;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006624 break;
6625
Willy Tarreau295a8372011-03-10 11:25:07 +01006626 case STAT_ST_END:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006627 appctx->st2 = STAT_ST_FIN;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006628 break;
6629 }
6630 }
6631 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006632}
6633
Willy Tarreaud426a182010-03-05 14:58:26 +01006634/* print a line of text buffer (limited to 70 bytes) to <out>. The format is :
Willy Tarreau74808cb2009-03-04 15:53:18 +01006635 * <2 spaces> <offset=5 digits> <space or plus> <space> <70 chars max> <\n>
6636 * which is 60 chars per line. Non-printable chars \t, \n, \r and \e are
6637 * encoded in C format. Other non-printable chars are encoded "\xHH". Original
6638 * lines are respected within the limit of 70 output chars. Lines that are
6639 * continuation of a previous truncated line begin with "+" instead of " "
6640 * after the offset. The new pointer is returned.
6641 */
Willy Tarreaud426a182010-03-05 14:58:26 +01006642static int dump_text_line(struct chunk *out, const char *buf, int bsize, int len,
6643 int *line, int ptr)
Willy Tarreau74808cb2009-03-04 15:53:18 +01006644{
6645 int end;
6646 unsigned char c;
6647
6648 end = out->len + 80;
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02006649 if (end > out->size)
Willy Tarreau74808cb2009-03-04 15:53:18 +01006650 return ptr;
6651
Willy Tarreau77804732012-10-29 16:14:26 +01006652 chunk_appendf(out, " %05d%c ", ptr, (ptr == *line) ? ' ' : '+');
Willy Tarreau74808cb2009-03-04 15:53:18 +01006653
Willy Tarreaud426a182010-03-05 14:58:26 +01006654 while (ptr < len && ptr < bsize) {
6655 c = buf[ptr];
Willy Tarreau787bbd92009-03-12 08:18:33 +01006656 if (isprint(c) && isascii(c) && c != '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006657 if (out->len > end - 2)
6658 break;
6659 out->str[out->len++] = c;
Willy Tarreau787bbd92009-03-12 08:18:33 +01006660 } else if (c == '\t' || c == '\n' || c == '\r' || c == '\e' || c == '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006661 if (out->len > end - 3)
6662 break;
6663 out->str[out->len++] = '\\';
6664 switch (c) {
6665 case '\t': c = 't'; break;
6666 case '\n': c = 'n'; break;
6667 case '\r': c = 'r'; break;
6668 case '\e': c = 'e'; break;
Willy Tarreau787bbd92009-03-12 08:18:33 +01006669 case '\\': c = '\\'; break;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006670 }
6671 out->str[out->len++] = c;
6672 } else {
6673 if (out->len > end - 5)
6674 break;
6675 out->str[out->len++] = '\\';
6676 out->str[out->len++] = 'x';
6677 out->str[out->len++] = hextab[(c >> 4) & 0xF];
6678 out->str[out->len++] = hextab[c & 0xF];
6679 }
Willy Tarreaud426a182010-03-05 14:58:26 +01006680 if (buf[ptr++] == '\n') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006681 /* we had a line break, let's return now */
6682 out->str[out->len++] = '\n';
6683 *line = ptr;
6684 return ptr;
6685 }
6686 }
6687 /* we have an incomplete line, we return it as-is */
6688 out->str[out->len++] = '\n';
6689 return ptr;
6690}
6691
Baptiste Assmann3863f972015-05-17 00:33:24 +02006692/* This function dumps counters from all resolvers section and associated name servers.
6693 * It returns 0 if the output buffer is full and it needs
6694 * to be called again, otherwise non-zero.
6695 */
6696static int stats_dump_resolvers_to_buffer(struct stream_interface *si)
6697{
6698 struct appctx *appctx = __objt_appctx(si->end);
6699 struct dns_resolvers *presolvers;
6700 struct dns_nameserver *pnameserver;
6701
6702 chunk_reset(&trash);
6703
6704 switch (appctx->st2) {
6705 case STAT_ST_INIT:
6706 appctx->st2 = STAT_ST_LIST; /* let's start producing data */
6707 /* fall through */
6708
6709 case STAT_ST_LIST:
Andrew Hayworth68d05342015-10-02 20:33:01 +00006710 if (LIST_ISEMPTY(&dns_resolvers)) {
6711 chunk_appendf(&trash, "No resolvers found\n");
6712 }
6713 else {
6714 list_for_each_entry(presolvers, &dns_resolvers, list) {
6715 if (appctx->ctx.resolvers.ptr != NULL && appctx->ctx.resolvers.ptr != presolvers)
6716 continue;
6717
6718 chunk_appendf(&trash, "Resolvers section %s\n", presolvers->id);
6719 list_for_each_entry(pnameserver, &presolvers->nameserver_list, list) {
6720 chunk_appendf(&trash, " nameserver %s:\n", pnameserver->id);
6721 chunk_appendf(&trash, " sent: %ld\n", pnameserver->counters.sent);
6722 chunk_appendf(&trash, " valid: %ld\n", pnameserver->counters.valid);
6723 chunk_appendf(&trash, " update: %ld\n", pnameserver->counters.update);
6724 chunk_appendf(&trash, " cname: %ld\n", pnameserver->counters.cname);
6725 chunk_appendf(&trash, " cname_error: %ld\n", pnameserver->counters.cname_error);
6726 chunk_appendf(&trash, " any_err: %ld\n", pnameserver->counters.any_err);
6727 chunk_appendf(&trash, " nx: %ld\n", pnameserver->counters.nx);
6728 chunk_appendf(&trash, " timeout: %ld\n", pnameserver->counters.timeout);
6729 chunk_appendf(&trash, " refused: %ld\n", pnameserver->counters.refused);
6730 chunk_appendf(&trash, " other: %ld\n", pnameserver->counters.other);
6731 chunk_appendf(&trash, " invalid: %ld\n", pnameserver->counters.invalid);
6732 chunk_appendf(&trash, " too_big: %ld\n", pnameserver->counters.too_big);
6733 chunk_appendf(&trash, " truncated: %ld\n", pnameserver->counters.truncated);
6734 chunk_appendf(&trash, " outdated: %ld\n", pnameserver->counters.outdated);
6735 }
6736 }
Baptiste Assmann3863f972015-05-17 00:33:24 +02006737 }
6738
6739 /* display response */
6740 if (bi_putchk(si_ic(si), &trash) == -1) {
6741 /* let's try again later from this session. We add ourselves into
6742 * this session's users so that it can remove us upon termination.
6743 */
6744 si->flags |= SI_FL_WAIT_ROOM;
6745 return 0;
6746 }
6747
6748 appctx->st2 = STAT_ST_FIN;
6749 /* fall through */
6750
6751 default:
6752 appctx->st2 = STAT_ST_FIN;
6753 return 1;
6754 }
6755}
6756
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02006757/* This function dumps all captured errors onto the stream interface's
Willy Tarreau306f8302013-07-08 15:53:06 +02006758 * read buffer. It returns 0 if the output buffer is full and it needs
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01006759 * to be called again, otherwise non-zero.
Willy Tarreau74808cb2009-03-04 15:53:18 +01006760 */
Simon Horman9bd2c732011-06-15 15:18:44 +09006761static int stats_dump_errors_to_buffer(struct stream_interface *si)
Willy Tarreau74808cb2009-03-04 15:53:18 +01006762{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006763 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau74808cb2009-03-04 15:53:18 +01006764 extern const char *monthname[12];
Willy Tarreau74808cb2009-03-04 15:53:18 +01006765
Willy Tarreau2bb4a962014-11-28 11:11:05 +01006766 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
Willy Tarreau61b34732009-10-03 23:49:35 +02006767 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006768
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006769 chunk_reset(&trash);
Willy Tarreau74808cb2009-03-04 15:53:18 +01006770
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006771 if (!appctx->ctx.errors.px) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006772 /* the function had not been called yet, let's prepare the
6773 * buffer for a response.
6774 */
Willy Tarreau10479e42010-12-12 14:00:34 +01006775 struct tm tm;
6776
6777 get_localtime(date.tv_sec, &tm);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006778 chunk_appendf(&trash, "Total events captured on [%02d/%s/%04d:%02d:%02d:%02d.%03d] : %u\n",
Willy Tarreau10479e42010-12-12 14:00:34 +01006779 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
6780 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(date.tv_usec/1000),
6781 error_snapshot_id);
6782
Willy Tarreau2bb4a962014-11-28 11:11:05 +01006783 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau10479e42010-12-12 14:00:34 +01006784 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreaufe127932015-04-21 19:23:39 +02006785 si_applet_cant_put(si);
Willy Tarreau10479e42010-12-12 14:00:34 +01006786 return 0;
6787 }
6788
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006789 appctx->ctx.errors.px = proxy;
6790 appctx->ctx.errors.buf = 0;
6791 appctx->ctx.errors.bol = 0;
6792 appctx->ctx.errors.ptr = -1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006793 }
6794
6795 /* we have two inner loops here, one for the proxy, the other one for
6796 * the buffer.
6797 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006798 while (appctx->ctx.errors.px) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006799 struct error_snapshot *es;
6800
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006801 if (appctx->ctx.errors.buf == 0)
6802 es = &appctx->ctx.errors.px->invalid_req;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006803 else
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006804 es = &appctx->ctx.errors.px->invalid_rep;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006805
6806 if (!es->when.tv_sec)
6807 goto next;
6808
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006809 if (appctx->ctx.errors.iid >= 0 &&
6810 appctx->ctx.errors.px->uuid != appctx->ctx.errors.iid &&
6811 es->oe->uuid != appctx->ctx.errors.iid)
Willy Tarreau74808cb2009-03-04 15:53:18 +01006812 goto next;
6813
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006814 if (appctx->ctx.errors.ptr < 0) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006815 /* just print headers now */
6816
6817 char pn[INET6_ADDRSTRLEN];
6818 struct tm tm;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02006819 int port;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006820
6821 get_localtime(es->when.tv_sec, &tm);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006822 chunk_appendf(&trash, " \n[%02d/%s/%04d:%02d:%02d:%02d.%03d]",
Willy Tarreau74808cb2009-03-04 15:53:18 +01006823 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
Willy Tarreau1772ece2009-04-03 14:49:12 +02006824 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(es->when.tv_usec/1000));
Willy Tarreau74808cb2009-03-04 15:53:18 +01006825
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02006826 switch (addr_to_str(&es->src, pn, sizeof(pn))) {
6827 case AF_INET:
6828 case AF_INET6:
6829 port = get_host_port(&es->src);
6830 break;
6831 default:
6832 port = 0;
6833 }
6834
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006835 switch (appctx->ctx.errors.buf) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006836 case 0:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006837 chunk_appendf(&trash,
Willy Tarreau74808cb2009-03-04 15:53:18 +01006838 " frontend %s (#%d): invalid request\n"
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02006839 " backend %s (#%d)",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006840 appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02006841 (es->oe->cap & PR_CAP_BE) ? es->oe->id : "<NONE>",
6842 (es->oe->cap & PR_CAP_BE) ? es->oe->uuid : -1);
Willy Tarreau74808cb2009-03-04 15:53:18 +01006843 break;
6844 case 1:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006845 chunk_appendf(&trash,
Olivier Doucet08afdcb2014-09-08 11:23:00 +02006846 " backend %s (#%d): invalid response\n"
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02006847 " frontend %s (#%d)",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006848 appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02006849 es->oe->id, es->oe->uuid);
Willy Tarreau74808cb2009-03-04 15:53:18 +01006850 break;
6851 }
6852
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006853 chunk_appendf(&trash,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02006854 ", server %s (#%d), event #%u\n"
6855 " src %s:%d, session #%d, session flags 0x%08x\n"
6856 " HTTP msg state %d, msg flags 0x%08x, tx flags 0x%08x\n"
6857 " HTTP chunk len %lld bytes, HTTP body len %lld bytes\n"
6858 " buffer flags 0x%08x, out %d bytes, total %lld bytes\n"
6859 " pending %d bytes, wrapping at %d, error at position %d:\n \n",
6860 es->srv ? es->srv->id : "<NONE>", es->srv ? es->srv->puid : -1,
6861 es->ev_id,
6862 pn, port, es->sid, es->s_flags,
6863 es->state, es->m_flags, es->t_flags,
6864 es->m_clen, es->m_blen,
6865 es->b_flags, es->b_out, es->b_tot,
6866 es->len, es->b_wrap, es->pos);
6867
Willy Tarreau2bb4a962014-11-28 11:11:05 +01006868 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006869 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreaufe127932015-04-21 19:23:39 +02006870 si_applet_cant_put(si);
Willy Tarreau61b34732009-10-03 23:49:35 +02006871 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006872 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006873 appctx->ctx.errors.ptr = 0;
6874 appctx->ctx.errors.sid = es->sid;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006875 }
6876
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006877 if (appctx->ctx.errors.sid != es->sid) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006878 /* the snapshot changed while we were dumping it */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006879 chunk_appendf(&trash,
Willy Tarreau74808cb2009-03-04 15:53:18 +01006880 " WARNING! update detected on this snapshot, dump interrupted. Please re-check!\n");
Willy Tarreaubc18da12015-03-13 14:00:47 +01006881 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02006882 si_applet_cant_put(si);
Willy Tarreau61b34732009-10-03 23:49:35 +02006883 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01006884 }
Willy Tarreau74808cb2009-03-04 15:53:18 +01006885 goto next;
6886 }
6887
6888 /* OK, ptr >= 0, so we have to dump the current line */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006889 while (appctx->ctx.errors.ptr < es->len && appctx->ctx.errors.ptr < sizeof(es->buf)) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006890 int newptr;
6891 int newline;
6892
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006893 newline = appctx->ctx.errors.bol;
6894 newptr = dump_text_line(&trash, es->buf, sizeof(es->buf), es->len, &newline, appctx->ctx.errors.ptr);
6895 if (newptr == appctx->ctx.errors.ptr)
Willy Tarreau61b34732009-10-03 23:49:35 +02006896 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006897
Willy Tarreau2bb4a962014-11-28 11:11:05 +01006898 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006899 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreaufe127932015-04-21 19:23:39 +02006900 si_applet_cant_put(si);
Willy Tarreau61b34732009-10-03 23:49:35 +02006901 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006902 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006903 appctx->ctx.errors.ptr = newptr;
6904 appctx->ctx.errors.bol = newline;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006905 };
6906 next:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006907 appctx->ctx.errors.bol = 0;
6908 appctx->ctx.errors.ptr = -1;
6909 appctx->ctx.errors.buf++;
6910 if (appctx->ctx.errors.buf > 1) {
6911 appctx->ctx.errors.buf = 0;
6912 appctx->ctx.errors.px = appctx->ctx.errors.px->next;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006913 }
Willy Tarreauae795722016-02-16 11:27:28 +01006914 }
6915
6916 /* dump complete */
6917 return 1;
6918}
6919
6920/* This function dumps all environmnent variables to the buffer. It returns 0
6921 * if the output buffer is full and it needs to be called again, otherwise
6922 * non-zero. Dumps only one entry if st2 == STAT_ST_END.
6923 */
6924static int stats_dump_env_to_buffer(struct stream_interface *si)
6925{
6926 struct appctx *appctx = __objt_appctx(si->end);
6927
6928 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
6929 return 1;
6930
6931 chunk_reset(&trash);
6932
6933 /* we have two inner loops here, one for the proxy, the other one for
6934 * the buffer.
6935 */
6936 while (*appctx->ctx.env.var) {
6937 chunk_printf(&trash, "%s\n", *appctx->ctx.env.var);
6938
6939 if (bi_putchk(si_ic(si), &trash) == -1) {
6940 si_applet_cant_put(si);
6941 return 0;
6942 }
6943 if (appctx->st2 == STAT_ST_END)
6944 break;
6945 appctx->ctx.env.var++;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006946 }
6947
6948 /* dump complete */
Willy Tarreau61b34732009-10-03 23:49:35 +02006949 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006950}
6951
Willy Tarreaud5781202012-09-22 19:32:35 +02006952/* parse the "level" argument on the bind lines */
6953static int bind_parse_level(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
6954{
6955 if (!*args[cur_arg + 1]) {
6956 memprintf(err, "'%s' : missing level", args[cur_arg]);
6957 return ERR_ALERT | ERR_FATAL;
6958 }
6959
6960 if (!strcmp(args[cur_arg+1], "user"))
6961 conf->level = ACCESS_LVL_USER;
6962 else if (!strcmp(args[cur_arg+1], "operator"))
6963 conf->level = ACCESS_LVL_OPER;
6964 else if (!strcmp(args[cur_arg+1], "admin"))
6965 conf->level = ACCESS_LVL_ADMIN;
6966 else {
6967 memprintf(err, "'%s' only supports 'user', 'operator', and 'admin' (got '%s')",
6968 args[cur_arg], args[cur_arg+1]);
6969 return ERR_ALERT | ERR_FATAL;
6970 }
6971
6972 return 0;
6973}
6974
Willy Tarreau30576452015-04-13 13:50:30 +02006975struct applet http_stats_applet = {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006976 .obj_type = OBJ_TYPE_APPLET,
Willy Tarreaub24281b2011-02-13 13:16:36 +01006977 .name = "<STATS>", /* used for logging */
6978 .fct = http_stats_io_handler,
Aman Gupta9a13e842012-04-02 18:57:53 -07006979 .release = NULL,
Willy Tarreaub24281b2011-02-13 13:16:36 +01006980};
6981
Willy Tarreau30576452015-04-13 13:50:30 +02006982static struct applet cli_applet = {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006983 .obj_type = OBJ_TYPE_APPLET,
Willy Tarreaub24281b2011-02-13 13:16:36 +01006984 .name = "<CLI>", /* used for logging */
6985 .fct = cli_io_handler,
Willy Tarreau5f9a8772012-11-26 02:22:40 +01006986 .release = cli_release_handler,
Willy Tarreaub24281b2011-02-13 13:16:36 +01006987};
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006988
Willy Tarreaudc13c112013-06-21 23:16:39 +02006989static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreau10522fd2008-07-09 20:12:41 +02006990 { CFG_GLOBAL, "stats", stats_parse_global },
6991 { 0, NULL, NULL },
6992}};
6993
Willy Tarreaud5781202012-09-22 19:32:35 +02006994static struct bind_kw_list bind_kws = { "STAT", { }, {
6995 { "level", bind_parse_level, 1 }, /* set the unix socket admin level */
6996 { NULL, NULL, 0 },
6997}};
6998
Willy Tarreau10522fd2008-07-09 20:12:41 +02006999__attribute__((constructor))
7000static void __dumpstats_module_init(void)
7001{
7002 cfg_register_keywords(&cfg_kws);
Willy Tarreaud5781202012-09-22 19:32:35 +02007003 bind_register_keywords(&bind_kws);
Willy Tarreau10522fd2008-07-09 20:12:41 +02007004}
7005
Willy Tarreau91861262007-10-17 17:06:05 +02007006/*
7007 * Local variables:
7008 * c-indent-level: 8
7009 * c-basic-offset: 8
7010 * End:
7011 */