blob: 05d30aa6319eb1ee6c8f69342b5632dc4c0270e4 [file] [log] [blame]
Willy Tarreau91861262007-10-17 17:06:05 +02001/*
Willy Tarreaueb472682010-05-28 18:46:57 +02002 * Functions dedicated to statistics output and the stats socket
Willy Tarreau91861262007-10-17 17:06:05 +02003 *
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004 * Copyright 2000-2012 Willy Tarreau <w@1wt.eu>
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02005 * Copyright 2007-2009 Krzysztof Piotr Oledzki <ole@ans.pl>
Willy Tarreau91861262007-10-17 17:06:05 +02006 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
11 *
12 */
13
14#include <ctype.h>
15#include <errno.h>
16#include <fcntl.h>
17#include <stdio.h>
18#include <stdlib.h>
19#include <string.h>
Willy Tarreaufbee7132007-10-18 13:53:22 +020020#include <pwd.h>
21#include <grp.h>
Willy Tarreau91861262007-10-17 17:06:05 +020022
23#include <sys/socket.h>
24#include <sys/stat.h>
25#include <sys/types.h>
26
Willy Tarreau10522fd2008-07-09 20:12:41 +020027#include <common/cfgparse.h>
Willy Tarreau91861262007-10-17 17:06:05 +020028#include <common/compat.h>
29#include <common/config.h>
30#include <common/debug.h>
31#include <common/memory.h>
32#include <common/mini-clist.h>
33#include <common/standard.h>
Willy Tarreau0c303ee2008-07-07 00:09:58 +020034#include <common/ticks.h>
Willy Tarreau91861262007-10-17 17:06:05 +020035#include <common/time.h>
36#include <common/uri_auth.h>
37#include <common/version.h>
Emeric Brun4147b2e2014-06-16 18:36:30 +020038#include <common/base64.h>
Willy Tarreau91861262007-10-17 17:06:05 +020039
Willy Tarreau8a8d83b2015-04-13 13:24:54 +020040#include <types/applet.h>
Willy Tarreau91861262007-10-17 17:06:05 +020041#include <types/global.h>
Baptiste Assmann3863f972015-05-17 00:33:24 +020042#include <types/dns.h>
Willy Tarreau91861262007-10-17 17:06:05 +020043
44#include <proto/backend.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020045#include <proto/channel.h>
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020046#include <proto/checks.h>
William Lallemande3a7d992012-11-20 11:25:20 +010047#include <proto/compression.h>
Willy Tarreau91861262007-10-17 17:06:05 +020048#include <proto/dumpstats.h>
49#include <proto/fd.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010050#include <proto/freq_ctr.h>
Willy Tarreau32b60d42015-03-13 16:14:57 +010051#include <proto/frontend.h>
Willy Tarreaueb472682010-05-28 18:46:57 +020052#include <proto/log.h>
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +010053#include <proto/pattern.h>
Willy Tarreaua206fa92009-01-25 14:02:00 +010054#include <proto/pipe.h>
Willy Tarreaud1d54542012-09-12 22:58:11 +020055#include <proto/listener.h>
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +010056#include <proto/map.h>
Willy Tarreaue6d97022012-11-23 11:19:33 +010057#include <proto/proto_http.h>
Willy Tarreaufbee7132007-10-18 13:53:22 +020058#include <proto/proto_uxst.h>
Willy Tarreau89a63132009-08-16 17:41:45 +020059#include <proto/proxy.h>
Willy Tarreau1cf8f082014-02-07 12:14:54 +010060#include <proto/sample.h>
Willy Tarreau9903f0e2015-04-04 18:50:31 +020061#include <proto/session.h>
Willy Tarreau87b09662015-04-03 00:22:06 +020062#include <proto/stream.h>
Krzysztof Oledzki85130942007-10-22 16:21:10 +020063#include <proto/server.h>
Willy Tarreau75bf2c92012-08-20 17:01:35 +020064#include <proto/raw_sock.h>
Willy Tarreaudded32d2008-11-30 19:48:07 +010065#include <proto/stream_interface.h>
Willy Tarreau4726f532009-03-07 17:25:21 +010066#include <proto/task.h>
Willy Tarreau91861262007-10-17 17:06:05 +020067
Willy Tarreau7a0169a2012-11-19 17:13:16 +010068#ifdef USE_OPENSSL
69#include <proto/ssl_sock.h>
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +020070#include <types/ssl_sock.h>
Willy Tarreau7a0169a2012-11-19 17:13:16 +010071#endif
72
Willy Tarreau91b843d2014-01-28 16:27:17 +010073/* stats socket states */
74enum {
75 STAT_CLI_INIT = 0, /* initial state, must leave to zero ! */
76 STAT_CLI_END, /* final state, let's close */
77 STAT_CLI_GETREQ, /* wait for a request */
78 STAT_CLI_OUTPUT, /* all states after this one are responses */
79 STAT_CLI_PROMPT, /* display the prompt (first output, same code) */
80 STAT_CLI_PRINT, /* display message in cli->msg */
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +010081 STAT_CLI_PRINT_FREE, /* display message in cli->msg. After the display, free the pointer */
Willy Tarreau91b843d2014-01-28 16:27:17 +010082 STAT_CLI_O_INFO, /* dump info */
Willy Tarreau87b09662015-04-03 00:22:06 +020083 STAT_CLI_O_SESS, /* dump streams */
Willy Tarreau91b843d2014-01-28 16:27:17 +010084 STAT_CLI_O_ERR, /* dump errors */
85 STAT_CLI_O_TAB, /* dump tables */
86 STAT_CLI_O_CLR, /* clear tables */
87 STAT_CLI_O_SET, /* set entries in tables */
88 STAT_CLI_O_STAT, /* dump stats */
Cyril Bontédb98eb32016-05-06 12:18:50 +020089 STAT_CLI_O_PATS, /* list all pattern reference available */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +010090 STAT_CLI_O_PAT, /* list all entries of a pattern */
Willy Tarreau91b843d2014-01-28 16:27:17 +010091 STAT_CLI_O_MLOOK, /* lookup a map entry */
Willy Tarreau12833bb2014-01-28 16:49:56 +010092 STAT_CLI_O_POOLS, /* dump memory pools */
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +020093 STAT_CLI_O_TLSK, /* list all TLS ticket keys references */
William Lallemand1d0b36a2016-05-20 17:40:26 +020094 STAT_CLI_O_TLSK_ENT, /* list all TLS ticket keys entries for a reference */
Baptiste Assmann3863f972015-05-17 00:33:24 +020095 STAT_CLI_O_RESOLVERS,/* dump a resolver's section nameservers counters */
Baptiste Assmann28289462015-07-03 08:01:20 +020096 STAT_CLI_O_SERVERS_STATE, /* dump server state and changing information */
Baptiste Assmann9b6857e2015-09-18 14:49:12 +020097 STAT_CLI_O_BACKEND, /* dump backend list */
Willy Tarreauae795722016-02-16 11:27:28 +010098 STAT_CLI_O_ENV, /* dump environment */
Willy Tarreau91b843d2014-01-28 16:27:17 +010099};
100
Willy Tarreaued7df902014-05-22 18:04:49 +0200101/* Actions available for the stats admin forms */
102enum {
103 ST_ADM_ACTION_NONE = 0,
Willy Tarreau248a60e2014-05-23 14:59:48 +0200104
105 /* enable/disable health checks */
106 ST_ADM_ACTION_DHLTH,
107 ST_ADM_ACTION_EHLTH,
108
109 /* force health check status */
110 ST_ADM_ACTION_HRUNN,
111 ST_ADM_ACTION_HNOLB,
112 ST_ADM_ACTION_HDOWN,
113
114 /* enable/disable agent checks */
115 ST_ADM_ACTION_DAGENT,
116 ST_ADM_ACTION_EAGENT,
117
118 /* force agent check status */
119 ST_ADM_ACTION_ARUNN,
120 ST_ADM_ACTION_ADOWN,
121
122 /* set admin state */
Willy Tarreaued7df902014-05-22 18:04:49 +0200123 ST_ADM_ACTION_READY,
124 ST_ADM_ACTION_DRAIN,
125 ST_ADM_ACTION_MAINT,
126 ST_ADM_ACTION_SHUTDOWN,
127 /* these are the ancient actions, still available for compatibility */
128 ST_ADM_ACTION_DISABLE,
129 ST_ADM_ACTION_ENABLE,
130 ST_ADM_ACTION_STOP,
131 ST_ADM_ACTION_START,
132};
133
Willy Tarreauc4832de2016-01-11 18:12:26 +0100134
Willy Tarreauc4832de2016-01-11 18:12:26 +0100135/* These are the field names for each INF_* field position. Please pay attention
136 * to always use the exact same name except that the strings for new names must
137 * be lower case or CamelCase while the enum entries must be upper case.
138 */
139const char *info_field_names[INF_TOTAL_FIELDS] = {
140 [INF_NAME] = "Name",
141 [INF_VERSION] = "Version",
142 [INF_RELEASE_DATE] = "Release_date",
143 [INF_NBPROC] = "Nbproc",
144 [INF_PROCESS_NUM] = "Process_num",
145 [INF_PID] = "Pid",
146 [INF_UPTIME] = "Uptime",
147 [INF_UPTIME_SEC] = "Uptime_sec",
148 [INF_MEMMAX_MB] = "Memmax_MB",
149 [INF_POOL_ALLOC_MB] = "PoolAlloc_MB",
150 [INF_POOL_USED_MB] = "PoolUsed_MB",
151 [INF_POOL_FAILED] = "PoolFailed",
152 [INF_ULIMIT_N] = "Ulimit-n",
153 [INF_MAXSOCK] = "Maxsock",
154 [INF_MAXCONN] = "Maxconn",
155 [INF_HARD_MAXCONN] = "Hard_maxconn",
156 [INF_CURR_CONN] = "CurrConns",
157 [INF_CUM_CONN] = "CumConns",
158 [INF_CUM_REQ] = "CumReq",
159 [INF_MAX_SSL_CONNS] = "MaxSslConns",
160 [INF_CURR_SSL_CONNS] = "CurrSslConns",
161 [INF_CUM_SSL_CONNS] = "CumSslConns",
162 [INF_MAXPIPES] = "Maxpipes",
163 [INF_PIPES_USED] = "PipesUsed",
164 [INF_PIPES_FREE] = "PipesFree",
165 [INF_CONN_RATE] = "ConnRate",
166 [INF_CONN_RATE_LIMIT] = "ConnRateLimit",
167 [INF_MAX_CONN_RATE] = "MaxConnRate",
168 [INF_SESS_RATE] = "SessRate",
169 [INF_SESS_RATE_LIMIT] = "SessRateLimit",
170 [INF_MAX_SESS_RATE] = "MaxSessRate",
171 [INF_SSL_RATE] = "SslRate",
172 [INF_SSL_RATE_LIMIT] = "SslRateLimit",
173 [INF_MAX_SSL_RATE] = "MaxSslRate",
174 [INF_SSL_FRONTEND_KEY_RATE] = "SslFrontendKeyRate",
175 [INF_SSL_FRONTEND_MAX_KEY_RATE] = "SslFrontendMaxKeyRate",
176 [INF_SSL_FRONTEND_SESSION_REUSE_PCT] = "SslFrontendSessionReuse_pct",
177 [INF_SSL_BACKEND_KEY_RATE] = "SslBackendKeyRate",
178 [INF_SSL_BACKEND_MAX_KEY_RATE] = "SslBackendMaxKeyRate",
179 [INF_SSL_CACHE_LOOKUPS] = "SslCacheLookups",
180 [INF_SSL_CACHE_MISSES] = "SslCacheMisses",
181 [INF_COMPRESS_BPS_IN] = "CompressBpsIn",
182 [INF_COMPRESS_BPS_OUT] = "CompressBpsOut",
183 [INF_COMPRESS_BPS_RATE_LIM] = "CompressBpsRateLim",
184 [INF_ZLIB_MEM_USAGE] = "ZlibMemUsage",
185 [INF_MAX_ZLIB_MEM_USAGE] = "MaxZlibMemUsage",
186 [INF_TASKS] = "Tasks",
187 [INF_RUN_QUEUE] = "Run_queue",
188 [INF_IDLE_PCT] = "Idle_pct",
189 [INF_NODE] = "node",
190 [INF_DESCRIPTION] = "description",
191};
192
193/* one line of stats */
194static struct field info[INF_TOTAL_FIELDS];
195
Willy Tarreau8e205142016-01-04 17:23:25 +0100196/* These are the field names for each ST_F_* field position. Please pay attention
197 * to always use the exact same name except that the strings must be lower case
198 * while the enum entries must be upper case.
199 */
200const char *stat_field_names[ST_F_TOTAL_FIELDS] = {
201 [ST_F_PXNAME] = "pxname",
202 [ST_F_SVNAME] = "svname",
203 [ST_F_QCUR] = "qcur",
204 [ST_F_QMAX] = "qmax",
205 [ST_F_SCUR] = "scur",
206 [ST_F_SMAX] = "smax",
207 [ST_F_SLIM] = "slim",
208 [ST_F_STOT] = "stot",
209 [ST_F_BIN] = "bin",
210 [ST_F_BOUT] = "bout",
211 [ST_F_DREQ] = "dreq",
212 [ST_F_DRESP] = "dresp",
213 [ST_F_EREQ] = "ereq",
214 [ST_F_ECON] = "econ",
215 [ST_F_ERESP] = "eresp",
216 [ST_F_WRETR] = "wretr",
217 [ST_F_WREDIS] = "wredis",
218 [ST_F_STATUS] = "status",
219 [ST_F_WEIGHT] = "weight",
220 [ST_F_ACT] = "act",
221 [ST_F_BCK] = "bck",
222 [ST_F_CHKFAIL] = "chkfail",
223 [ST_F_CHKDOWN] = "chkdown",
224 [ST_F_LASTCHG] = "lastchg",
225 [ST_F_DOWNTIME] = "downtime",
226 [ST_F_QLIMIT] = "qlimit",
227 [ST_F_PID] = "pid",
228 [ST_F_IID] = "iid",
229 [ST_F_SID] = "sid",
230 [ST_F_THROTTLE] = "throttle",
231 [ST_F_LBTOT] = "lbtot",
232 [ST_F_TRACKED] = "tracked",
233 [ST_F_TYPE] = "type",
234 [ST_F_RATE] = "rate",
235 [ST_F_RATE_LIM] = "rate_lim",
236 [ST_F_RATE_MAX] = "rate_max",
237 [ST_F_CHECK_STATUS] = "check_status",
238 [ST_F_CHECK_CODE] = "check_code",
239 [ST_F_CHECK_DURATION] = "check_duration",
240 [ST_F_HRSP_1XX] = "hrsp_1xx",
241 [ST_F_HRSP_2XX] = "hrsp_2xx",
242 [ST_F_HRSP_3XX] = "hrsp_3xx",
243 [ST_F_HRSP_4XX] = "hrsp_4xx",
244 [ST_F_HRSP_5XX] = "hrsp_5xx",
245 [ST_F_HRSP_OTHER] = "hrsp_other",
246 [ST_F_HANAFAIL] = "hanafail",
247 [ST_F_REQ_RATE] = "req_rate",
248 [ST_F_REQ_RATE_MAX] = "req_rate_max",
249 [ST_F_REQ_TOT] = "req_tot",
250 [ST_F_CLI_ABRT] = "cli_abrt",
251 [ST_F_SRV_ABRT] = "srv_abrt",
252 [ST_F_COMP_IN] = "comp_in",
253 [ST_F_COMP_OUT] = "comp_out",
254 [ST_F_COMP_BYP] = "comp_byp",
255 [ST_F_COMP_RSP] = "comp_rsp",
256 [ST_F_LASTSESS] = "lastsess",
257 [ST_F_LAST_CHK] = "last_chk",
258 [ST_F_LAST_AGT] = "last_agt",
259 [ST_F_QTIME] = "qtime",
260 [ST_F_CTIME] = "ctime",
261 [ST_F_RTIME] = "rtime",
262 [ST_F_TTIME] = "ttime",
Willy Tarreau7f618842016-01-08 11:40:03 +0100263 [ST_F_AGENT_STATUS] = "agent_status",
264 [ST_F_AGENT_CODE] = "agent_code",
265 [ST_F_AGENT_DURATION] = "agent_duration",
Willy Tarreaudd7354b2016-01-08 13:47:26 +0100266 [ST_F_CHECK_DESC] = "check_desc",
267 [ST_F_AGENT_DESC] = "agent_desc",
Willy Tarreau3141f592016-01-08 14:25:28 +0100268 [ST_F_CHECK_RISE] = "check_rise",
269 [ST_F_CHECK_FALL] = "check_fall",
270 [ST_F_CHECK_HEALTH] = "check_health",
271 [ST_F_AGENT_RISE] = "agent_rise",
272 [ST_F_AGENT_FALL] = "agent_fall",
273 [ST_F_AGENT_HEALTH] = "agent_health",
Willy Tarreau3a4ec3a2016-01-08 15:35:43 +0100274 [ST_F_ADDR] = "addr",
Willy Tarreaue4847c62016-01-08 15:43:54 +0100275 [ST_F_COOKIE] = "cookie",
Willy Tarreauf8211df2016-01-11 14:09:38 +0100276 [ST_F_MODE] = "mode",
Willy Tarreauf1516d92016-01-11 14:48:36 +0100277 [ST_F_ALGO] = "algo",
Willy Tarreauc73810f2016-01-11 13:52:04 +0100278 [ST_F_CONN_RATE] = "conn_rate",
279 [ST_F_CONN_RATE_MAX] = "conn_rate_max",
280 [ST_F_CONN_TOT] = "conn_tot",
Willy Tarreau5b9bdff2016-01-11 14:40:47 +0100281 [ST_F_INTERCEPTED] = "intercepted",
Willy Tarreau8e205142016-01-04 17:23:25 +0100282};
283
Willy Tarreau82a86022016-01-04 19:04:18 +0100284/* one line of stats */
285static struct field stats[ST_F_TOTAL_FIELDS];
286
Baptiste Assmann9b6857e2015-09-18 14:49:12 +0200287static int stats_dump_backend_to_buffer(struct stream_interface *si);
Willy Tarreauae795722016-02-16 11:27:28 +0100288static int stats_dump_env_to_buffer(struct stream_interface *si);
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +0100289static int stats_dump_info_to_buffer(struct stream_interface *si);
Baptiste Assmann28289462015-07-03 08:01:20 +0200290static int stats_dump_servers_state_to_buffer(struct stream_interface *si);
Willy Tarreau12833bb2014-01-28 16:49:56 +0100291static int stats_dump_pools_to_buffer(struct stream_interface *si);
Willy Tarreau87b09662015-04-03 00:22:06 +0200292static int stats_dump_full_sess_to_buffer(struct stream_interface *si, struct stream *sess);
Simon Horman9bd2c732011-06-15 15:18:44 +0900293static int stats_dump_sess_to_buffer(struct stream_interface *si);
294static int stats_dump_errors_to_buffer(struct stream_interface *si);
Willy Tarreau44455022012-12-05 23:01:12 +0100295static int stats_table_request(struct stream_interface *si, int show);
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +0100296static int stats_dump_proxy_to_buffer(struct stream_interface *si, struct proxy *px, struct uri_auth *uri);
297static int stats_dump_stat_to_buffer(struct stream_interface *si, struct uri_auth *uri);
Baptiste Assmann3863f972015-05-17 00:33:24 +0200298static int stats_dump_resolvers_to_buffer(struct stream_interface *si);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100299static int stats_pats_list(struct stream_interface *si);
300static int stats_pat_list(struct stream_interface *si);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +0100301static int stats_map_lookup(struct stream_interface *si);
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +0200302#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
303static int stats_tlskeys_list(struct stream_interface *si);
304#endif
Willy Tarreau00a37f02015-04-13 12:05:19 +0200305static void cli_release_handler(struct appctx *appctx);
Simon Horman9bd2c732011-06-15 15:18:44 +0900306
Cyril Bonté76a99782016-05-06 12:18:48 +0200307static int dump_servers_state(struct stream_interface *si, struct chunk *buf);
Baptiste Assmann28289462015-07-03 08:01:20 +0200308
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100309/*
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +0100310 * cli_io_handler()
311 * -> stats_dump_sess_to_buffer() // "show sess"
312 * -> stats_dump_errors_to_buffer() // "show errors"
313 * -> stats_dump_info_to_buffer() // "show info"
Baptiste Assmann9b6857e2015-09-18 14:49:12 +0200314 * -> stats_dump_backend_to_buffer() // "show backend"
Baptiste Assmann28289462015-07-03 08:01:20 +0200315 * -> stats_dump_servers_state_to_buffer() // "show servers state [<backend name>]"
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +0100316 * -> stats_dump_stat_to_buffer() // "show stat"
Cyril Bontéa8322f92016-05-06 12:18:51 +0200317 * -> stats_dump_resolvers_to_buffer() // "show stat resolvers <id>"
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +0100318 * -> stats_dump_csv_header()
319 * -> stats_dump_proxy_to_buffer()
320 * -> stats_dump_fe_stats()
321 * -> stats_dump_li_stats()
322 * -> stats_dump_sv_stats()
323 * -> stats_dump_be_stats()
324 *
325 * http_stats_io_handler()
326 * -> stats_dump_stat_to_buffer() // same as above, but used for CSV or HTML
327 * -> stats_dump_csv_header() // emits the CSV headers (same as above)
328 * -> stats_dump_html_head() // emits the HTML headers
329 * -> stats_dump_html_info() // emits the equivalent of "show info" at the top
330 * -> stats_dump_proxy_to_buffer() // same as above, valid for CSV and HTML
331 * -> stats_dump_html_px_hdr()
332 * -> stats_dump_fe_stats()
333 * -> stats_dump_li_stats()
334 * -> stats_dump_sv_stats()
335 * -> stats_dump_be_stats()
336 * -> stats_dump_html_px_end()
337 * -> stats_dump_html_end() // emits HTML trailer
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100338 */
339
Willy Tarreau30576452015-04-13 13:50:30 +0200340static struct applet cli_applet;
Simon Horman9bd2c732011-06-15 15:18:44 +0900341
342static const char stats_sock_usage_msg[] =
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200343 "Unknown command. Please enter one of the following commands only :\n"
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200344 " clear counters : clear max statistics counters (add 'all' for all counters)\n"
Willy Tarreau88ee3972010-07-13 13:48:00 +0200345 " clear table : remove an entry from a table\n"
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200346 " help : this message\n"
347 " prompt : toggle interactive mode with prompt\n"
348 " quit : disconnect\n"
Baptiste Assmann9b6857e2015-09-18 14:49:12 +0200349 " show backend : list backends in the current running config\n"
Willy Tarreauae795722016-02-16 11:27:28 +0100350 " show env [var] : dump environment variables known to the process\n"
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200351 " show info : report information about the running process\n"
Willy Tarreau12833bb2014-01-28 16:49:56 +0100352 " show pools : report information about the memory pools usage\n"
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200353 " show stat : report counters for each proxy and server\n"
Cyril Bontéa8322f92016-05-06 12:18:51 +0200354 " show stat resolvers [id]: dumps counters from all resolvers section and\n"
355 " associated name servers\n"
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200356 " show errors : report last request and response errors for each proxy\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +0100357 " show sess [id] : report the list of current sessions or dump this session\n"
Willy Tarreau69f58c82010-07-12 17:55:33 +0200358 " show table [id]: report table usage stats or dump this table's contents\n"
Baptiste Assmann28289462015-07-03 08:01:20 +0200359 " show servers state [id]: dump volatile server information (for backend <id>)\n"
Willy Tarreau38338fa2009-10-10 18:37:29 +0200360 " get weight : report a server's current weight\n"
Willy Tarreau4483d432009-10-10 19:30:08 +0200361 " set weight : change a server's weight\n"
Baptiste Assmann3d8f8312015-04-13 22:54:33 +0200362 " set server : change a server's state, weight or address\n"
Willy Tarreau654694e2012-06-07 01:03:16 +0200363 " set table [id] : update or create a table entry's data\n"
Willy Tarreau7aabd112010-01-26 10:59:06 +0100364 " set timeout : change a timeout setting\n"
Willy Tarreau2a0f4d22011-08-02 11:49:05 +0200365 " set maxconn : change a maxconn setting\n"
Willy Tarreauf5b22872011-09-07 16:13:44 +0200366 " set rate-limit : change a rate limiting value\n"
Willy Tarreaua295edc2011-09-07 23:21:03 +0200367 " disable : put a server or frontend in maintenance mode\n"
368 " enable : re-enable a server or frontend which is in maintenance mode\n"
369 " shutdown : kill a session or a frontend (eg:to release listening ports)\n"
Cyril Bontédb98eb32016-05-06 12:18:50 +0200370 " show acl [id] : report available acls or dump an acl's contents\n"
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100371 " get acl : reports the patterns matching a sample for an ACL\n"
372 " add acl : add acl entry\n"
373 " del acl : delete acl entry\n"
374 " clear acl <id> : clear the content of this acl\n"
Cyril Bontédb98eb32016-05-06 12:18:50 +0200375 " show map [id] : report available maps or dump a map's contents\n"
Thierry FOURNIER1432a0c2014-03-11 13:42:38 +0100376 " get map : reports the keys and values matching a sample for a map\n"
377 " set map : modify map entry\n"
378 " add map : add map entry\n"
379 " del map : delete map entry\n"
380 " clear map <id> : clear the content of this map\n"
Emeric Brun4147b2e2014-06-16 18:36:30 +0200381 " set ssl <stmt> : set statement for ssl\n"
William Lallemand1d0b36a2016-05-20 17:40:26 +0200382#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
383 " show tls-keys [id|*]: show tls keys references or dump tls ticket keys when id specified\n"
384#endif
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200385 "";
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200386
Simon Horman9bd2c732011-06-15 15:18:44 +0900387static const char stats_permission_denied_msg[] =
Willy Tarreau6162db22009-10-10 17:13:00 +0200388 "Permission denied\n"
389 "";
390
Willy Tarreau295a8372011-03-10 11:25:07 +0100391/* data transmission states for the stats responses */
392enum {
393 STAT_ST_INIT = 0,
394 STAT_ST_HEAD,
395 STAT_ST_INFO,
396 STAT_ST_LIST,
397 STAT_ST_END,
398 STAT_ST_FIN,
399};
400
401/* data transmission states for the stats responses inside a proxy */
402enum {
403 STAT_PX_ST_INIT = 0,
404 STAT_PX_ST_TH,
405 STAT_PX_ST_FE,
406 STAT_PX_ST_LI,
407 STAT_PX_ST_SV,
408 STAT_PX_ST_BE,
409 STAT_PX_ST_END,
410 STAT_PX_ST_FIN,
411};
412
Cyril Bonté19979e12012-04-04 12:57:21 +0200413extern const char *stat_status_codes[];
414
Willy Tarreau07e9e642010-08-17 21:48:17 +0200415/* allocate a new stats frontend named <name>, and return it
416 * (or NULL in case of lack of memory).
417 */
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200418static struct proxy *alloc_stats_fe(const char *name, const char *file, int line)
Willy Tarreau07e9e642010-08-17 21:48:17 +0200419{
420 struct proxy *fe;
421
Vincent Bernat02779b62016-04-03 13:48:43 +0200422 fe = calloc(1, sizeof(*fe));
Willy Tarreau07e9e642010-08-17 21:48:17 +0200423 if (!fe)
424 return NULL;
425
Willy Tarreau237250c2011-07-29 01:49:03 +0200426 init_new_proxy(fe);
Willy Tarreau050536d2012-10-04 08:47:34 +0200427 fe->next = proxy;
428 proxy = fe;
Willy Tarreau07e9e642010-08-17 21:48:17 +0200429 fe->last_change = now.tv_sec;
430 fe->id = strdup("GLOBAL");
431 fe->cap = PR_CAP_FE;
Willy Tarreauc2adf8b2011-09-07 12:13:34 +0200432 fe->maxconn = 10; /* default to 10 concurrent connections */
433 fe->timeout.client = MS_TO_TICKS(10000); /* default timeout of 10 seconds */
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200434 fe->conf.file = strdup(file);
435 fe->conf.line = line;
Willy Tarreau32b60d42015-03-13 16:14:57 +0100436 fe->accept = frontend_accept;
Willy Tarreauf87ab942015-03-13 15:55:16 +0100437 fe->default_target = &cli_applet.obj_type;
Willy Tarreau050536d2012-10-04 08:47:34 +0200438
439 /* the stats frontend is the only one able to assign ID #0 */
440 fe->conf.id.key = fe->uuid = 0;
441 eb32_insert(&used_proxy_id, &fe->conf.id);
Willy Tarreau07e9e642010-08-17 21:48:17 +0200442 return fe;
443}
444
Willy Tarreaufbee7132007-10-18 13:53:22 +0200445/* This function parses a "stats" statement in the "global" section. It returns
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200446 * -1 if there is any error, otherwise zero. If it returns -1, it will write an
447 * error message into the <err> buffer which will be preallocated. The trailing
448 * '\n' must not be written. The function must be called with <args> pointing to
449 * the first word after "stats".
Willy Tarreaufbee7132007-10-18 13:53:22 +0200450 */
Willy Tarreau10522fd2008-07-09 20:12:41 +0200451static int stats_parse_global(char **args, int section_type, struct proxy *curpx,
Willy Tarreau28a47d62012-09-18 20:02:48 +0200452 struct proxy *defpx, const char *file, int line,
453 char **err)
Willy Tarreaufbee7132007-10-18 13:53:22 +0200454{
Willy Tarreau4348fad2012-09-20 16:48:07 +0200455 struct bind_conf *bind_conf;
Willy Tarreauc53d4222012-09-20 20:19:28 +0200456 struct listener *l;
Willy Tarreau4348fad2012-09-20 16:48:07 +0200457
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200458 if (!strcmp(args[1], "socket")) {
Willy Tarreaufbee7132007-10-18 13:53:22 +0200459 int cur_arg;
460
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200461 if (*args[2] == 0) {
Willy Tarreauc53d4222012-09-20 20:19:28 +0200462 memprintf(err, "'%s %s' in global section expects an address or a path to a UNIX socket", args[0], args[1]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200463 return -1;
464 }
465
Willy Tarreau89a63132009-08-16 17:41:45 +0200466 if (!global.stats_fe) {
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200467 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200468 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
Willy Tarreau89a63132009-08-16 17:41:45 +0200469 return -1;
470 }
Willy Tarreau89a63132009-08-16 17:41:45 +0200471 }
472
Willy Tarreau4348fad2012-09-20 16:48:07 +0200473 bind_conf = bind_conf_alloc(&global.stats_fe->conf.bind, file, line, args[2]);
Willy Tarreau290e63a2012-09-20 18:07:14 +0200474 bind_conf->level = ACCESS_LVL_OPER; /* default access level */
Willy Tarreau4348fad2012-09-20 16:48:07 +0200475
Willy Tarreauc53d4222012-09-20 20:19:28 +0200476 if (!str2listener(args[2], global.stats_fe, bind_conf, file, line, err)) {
477 memprintf(err, "parsing [%s:%d] : '%s %s' : %s\n",
478 file, line, args[0], args[1], err && *err ? *err : "error");
479 return -1;
480 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200481
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200482 cur_arg = 3;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200483 while (*args[cur_arg]) {
Willy Tarreaud5781202012-09-22 19:32:35 +0200484 static int bind_dumped;
485 struct bind_kw *kw;
486
487 kw = bind_find_kw(args[cur_arg]);
488 if (kw) {
489 if (!kw->parse) {
490 memprintf(err, "'%s %s' : '%s' option is not implemented in this version (check build options).",
491 args[0], args[1], args[cur_arg]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200492 return -1;
493 }
Willy Tarreaud5781202012-09-22 19:32:35 +0200494
Willy Tarreau061b5de2015-10-13 15:06:57 +0200495 if (kw->parse(args, cur_arg, global.stats_fe, bind_conf, err) != 0) {
Willy Tarreaud5781202012-09-22 19:32:35 +0200496 if (err && *err)
497 memprintf(err, "'%s %s' : '%s'", args[0], args[1], *err);
498 else
499 memprintf(err, "'%s %s' : error encountered while processing '%s'",
500 args[0], args[1], args[cur_arg]);
Willy Tarreau6162db22009-10-10 17:13:00 +0200501 return -1;
502 }
Willy Tarreaud5781202012-09-22 19:32:35 +0200503
504 cur_arg += 1 + kw->skip;
505 continue;
Willy Tarreau6162db22009-10-10 17:13:00 +0200506 }
Willy Tarreaud5781202012-09-22 19:32:35 +0200507
508 if (!bind_dumped) {
509 bind_dump_kws(err);
510 indent_msg(err, 4);
511 bind_dumped = 1;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200512 }
Willy Tarreaud5781202012-09-22 19:32:35 +0200513
514 memprintf(err, "'%s %s' : unknown keyword '%s'.%s%s",
515 args[0], args[1], args[cur_arg],
516 err && *err ? " Registered keywords :" : "", err && *err ? *err : "");
517 return -1;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200518 }
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100519
Willy Tarreauc53d4222012-09-20 20:19:28 +0200520 list_for_each_entry(l, &bind_conf->listeners, by_bind) {
521 l->maxconn = global.stats_fe->maxconn;
522 l->backlog = global.stats_fe->backlog;
Willy Tarreau9903f0e2015-04-04 18:50:31 +0200523 l->accept = session_accept_fd;
Willy Tarreau87b09662015-04-03 00:22:06 +0200524 l->handler = process_stream;
Willy Tarreau10b688f2015-03-13 16:43:12 +0100525 l->default_target = global.stats_fe->default_target;
Willy Tarreauc53d4222012-09-20 20:19:28 +0200526 l->options |= LI_O_UNLIMITED; /* don't make the peers subject to global limits */
527 l->nice = -64; /* we want to boost priority for local stats */
528 global.maxsock += l->maxconn;
529 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200530 }
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200531 else if (!strcmp(args[1], "timeout")) {
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100532 unsigned timeout;
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200533 const char *res = parse_time_err(args[2], &timeout, TIME_UNIT_MS);
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100534
535 if (res) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200536 memprintf(err, "'%s %s' : unexpected character '%c'", args[0], args[1], *res);
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100537 return -1;
538 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200539
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100540 if (!timeout) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200541 memprintf(err, "'%s %s' expects a positive value", args[0], args[1]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200542 return -1;
543 }
Willy Tarreau07e9e642010-08-17 21:48:17 +0200544 if (!global.stats_fe) {
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200545 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200546 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
Willy Tarreau07e9e642010-08-17 21:48:17 +0200547 return -1;
548 }
549 }
Willy Tarreau89a63132009-08-16 17:41:45 +0200550 global.stats_fe->timeout.client = MS_TO_TICKS(timeout);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200551 }
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200552 else if (!strcmp(args[1], "maxconn")) {
553 int maxconn = atol(args[2]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200554
555 if (maxconn <= 0) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200556 memprintf(err, "'%s %s' expects a positive value", args[0], args[1]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200557 return -1;
558 }
Willy Tarreauc2adf8b2011-09-07 12:13:34 +0200559
560 if (!global.stats_fe) {
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200561 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200562 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
Willy Tarreauc2adf8b2011-09-07 12:13:34 +0200563 return -1;
564 }
565 }
566 global.stats_fe->maxconn = maxconn;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200567 }
Willy Tarreau35b7b162012-10-22 23:17:18 +0200568 else if (!strcmp(args[1], "bind-process")) { /* enable the socket only on some processes */
569 int cur_arg = 2;
Willy Tarreaua9db57e2013-01-18 11:29:29 +0100570 unsigned long set = 0;
Willy Tarreau35b7b162012-10-22 23:17:18 +0200571
Willy Tarreau91319572013-04-20 09:48:50 +0200572 if (!global.stats_fe) {
573 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
574 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
575 return -1;
576 }
577 }
578
Willy Tarreau35b7b162012-10-22 23:17:18 +0200579 while (*args[cur_arg]) {
Willy Tarreau110ecc12012-11-15 17:50:01 +0100580 unsigned int low, high;
581
Willy Tarreau35b7b162012-10-22 23:17:18 +0200582 if (strcmp(args[cur_arg], "all") == 0) {
583 set = 0;
584 break;
585 }
586 else if (strcmp(args[cur_arg], "odd") == 0) {
Willy Tarreaua9db57e2013-01-18 11:29:29 +0100587 set |= ~0UL/3UL; /* 0x555....555 */
Willy Tarreau35b7b162012-10-22 23:17:18 +0200588 }
589 else if (strcmp(args[cur_arg], "even") == 0) {
Willy Tarreaua9db57e2013-01-18 11:29:29 +0100590 set |= (~0UL/3UL) << 1; /* 0xAAA...AAA */
Willy Tarreau35b7b162012-10-22 23:17:18 +0200591 }
Willy Tarreau83d84cf2012-11-22 01:04:31 +0100592 else if (isdigit((int)*args[cur_arg])) {
Willy Tarreau110ecc12012-11-15 17:50:01 +0100593 char *dash = strchr(args[cur_arg], '-');
594
595 low = high = str2uic(args[cur_arg]);
596 if (dash)
597 high = str2uic(dash + 1);
598
599 if (high < low) {
600 unsigned int swap = low;
601 low = high;
602 high = swap;
603 }
604
Willy Tarreaua9db57e2013-01-18 11:29:29 +0100605 if (low < 1 || high > LONGBITS) {
606 memprintf(err, "'%s %s' supports process numbers from 1 to %d.\n",
607 args[0], args[1], LONGBITS);
Willy Tarreau35b7b162012-10-22 23:17:18 +0200608 return -1;
609 }
Willy Tarreau110ecc12012-11-15 17:50:01 +0100610 while (low <= high)
Willy Tarreaua9db57e2013-01-18 11:29:29 +0100611 set |= 1UL << (low++ - 1);
Willy Tarreau110ecc12012-11-15 17:50:01 +0100612 }
613 else {
614 memprintf(err,
Willy Tarreaua9db57e2013-01-18 11:29:29 +0100615 "'%s %s' expects 'all', 'odd', 'even', or a list of process ranges with numbers from 1 to %d.\n",
616 args[0], args[1], LONGBITS);
Willy Tarreau110ecc12012-11-15 17:50:01 +0100617 return -1;
Willy Tarreau35b7b162012-10-22 23:17:18 +0200618 }
619 cur_arg++;
620 }
621 global.stats_fe->bind_proc = set;
622 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200623 else {
Willy Tarreau35b7b162012-10-22 23:17:18 +0200624 memprintf(err, "'%s' only supports 'socket', 'maxconn', 'bind-process' and 'timeout' (got '%s')", args[0], args[1]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200625 return -1;
626 }
627 return 0;
628}
629
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100630/* Dumps the stats CSV header to the trash buffer which. The caller is responsible
631 * for clearing it if needed.
Willy Tarreauf522f3d2014-02-10 22:22:49 +0100632 * NOTE: Some tools happen to rely on the field position instead of its name,
633 * so please only append new fields at the end, never in the middle.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100634 */
635static void stats_dump_csv_header()
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100636{
Willy Tarreauf6142292016-01-04 17:24:29 +0100637 int field;
638
639 chunk_appendf(&trash, "# ");
640 for (field = 0; field < ST_F_TOTAL_FIELDS; field++)
641 chunk_appendf(&trash, "%s,", stat_field_names[field]);
642
643 chunk_appendf(&trash, "\n");
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100644}
645
Simon Hormand9366582011-06-15 15:18:45 +0900646/* print a string of text buffer to <out>. The format is :
647 * Non-printable chars \t, \n, \r and \e are * encoded in C format.
648 * Other non-printable chars are encoded "\xHH". Space and '\' are also escaped.
649 * Print stopped if null char or <bsize> is reached, or if no more place in the chunk.
650 */
651static int dump_text(struct chunk *out, const char *buf, int bsize)
652{
653 unsigned char c;
654 int ptr = 0;
655
656 while (buf[ptr] && ptr < bsize) {
657 c = buf[ptr];
658 if (isprint(c) && isascii(c) && c != '\\' && c != ' ') {
659 if (out->len > out->size - 1)
660 break;
661 out->str[out->len++] = c;
662 }
663 else if (c == '\t' || c == '\n' || c == '\r' || c == '\e' || c == '\\' || c == ' ') {
664 if (out->len > out->size - 2)
665 break;
666 out->str[out->len++] = '\\';
667 switch (c) {
668 case ' ': c = ' '; break;
669 case '\t': c = 't'; break;
670 case '\n': c = 'n'; break;
671 case '\r': c = 'r'; break;
672 case '\e': c = 'e'; break;
673 case '\\': c = '\\'; break;
674 }
675 out->str[out->len++] = c;
676 }
677 else {
678 if (out->len > out->size - 4)
679 break;
680 out->str[out->len++] = '\\';
681 out->str[out->len++] = 'x';
682 out->str[out->len++] = hextab[(c >> 4) & 0xF];
683 out->str[out->len++] = hextab[c & 0xF];
684 }
685 ptr++;
686 }
687
688 return ptr;
689}
690
691/* print a buffer in hexa.
692 * Print stopped if <bsize> is reached, or if no more place in the chunk.
693 */
694static int dump_binary(struct chunk *out, const char *buf, int bsize)
695{
696 unsigned char c;
697 int ptr = 0;
698
699 while (ptr < bsize) {
700 c = buf[ptr];
701
702 if (out->len > out->size - 2)
703 break;
704 out->str[out->len++] = hextab[(c >> 4) & 0xF];
705 out->str[out->len++] = hextab[c & 0xF];
706
707 ptr++;
708 }
709 return ptr;
710}
711
712/* Dump the status of a table to a stream interface's
713 * read buffer. It returns 0 if the output buffer is full
714 * and needs to be called again, otherwise non-zero.
715 */
716static int stats_dump_table_head_to_buffer(struct chunk *msg, struct stream_interface *si,
717 struct proxy *proxy, struct proxy *target)
718{
Willy Tarreau87b09662015-04-03 00:22:06 +0200719 struct stream *s = si_strm(si);
Simon Hormand9366582011-06-15 15:18:45 +0900720
Willy Tarreau77804732012-10-29 16:14:26 +0100721 chunk_appendf(msg, "# table: %s, type: %s, size:%d, used:%d\n",
Simon Hormand9366582011-06-15 15:18:45 +0900722 proxy->id, stktable_types[proxy->table.type].kw, proxy->table.size, proxy->table.current);
723
724 /* any other information should be dumped here */
725
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200726 if (target && strm_li(s)->bind_conf->level < ACCESS_LVL_OPER)
Willy Tarreau77804732012-10-29 16:14:26 +0100727 chunk_appendf(msg, "# contents not dumped due to insufficient privileges\n");
Simon Hormand9366582011-06-15 15:18:45 +0900728
Willy Tarreaubc18da12015-03-13 14:00:47 +0100729 if (bi_putchk(si_ic(si), msg) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +0200730 si_applet_cant_put(si);
Simon Hormand9366582011-06-15 15:18:45 +0900731 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +0100732 }
Simon Hormand9366582011-06-15 15:18:45 +0900733
734 return 1;
735}
736
737/* Dump the a table entry to a stream interface's
738 * read buffer. It returns 0 if the output buffer is full
739 * and needs to be called again, otherwise non-zero.
740 */
741static int stats_dump_table_entry_to_buffer(struct chunk *msg, struct stream_interface *si,
742 struct proxy *proxy, struct stksess *entry)
743{
744 int dt;
745
Willy Tarreau77804732012-10-29 16:14:26 +0100746 chunk_appendf(msg, "%p:", entry);
Simon Hormand9366582011-06-15 15:18:45 +0900747
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200748 if (proxy->table.type == SMP_T_IPV4) {
Simon Hormand9366582011-06-15 15:18:45 +0900749 char addr[INET_ADDRSTRLEN];
750 inet_ntop(AF_INET, (const void *)&entry->key.key, addr, sizeof(addr));
Willy Tarreau77804732012-10-29 16:14:26 +0100751 chunk_appendf(msg, " key=%s", addr);
Simon Hormand9366582011-06-15 15:18:45 +0900752 }
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200753 else if (proxy->table.type == SMP_T_IPV6) {
Simon Hormand9366582011-06-15 15:18:45 +0900754 char addr[INET6_ADDRSTRLEN];
755 inet_ntop(AF_INET6, (const void *)&entry->key.key, addr, sizeof(addr));
Willy Tarreau77804732012-10-29 16:14:26 +0100756 chunk_appendf(msg, " key=%s", addr);
Simon Hormand9366582011-06-15 15:18:45 +0900757 }
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200758 else if (proxy->table.type == SMP_T_SINT) {
Willy Tarreau77804732012-10-29 16:14:26 +0100759 chunk_appendf(msg, " key=%u", *(unsigned int *)entry->key.key);
Simon Hormand9366582011-06-15 15:18:45 +0900760 }
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200761 else if (proxy->table.type == SMP_T_STR) {
Willy Tarreau77804732012-10-29 16:14:26 +0100762 chunk_appendf(msg, " key=");
Simon Hormand9366582011-06-15 15:18:45 +0900763 dump_text(msg, (const char *)entry->key.key, proxy->table.key_size);
764 }
765 else {
Willy Tarreau77804732012-10-29 16:14:26 +0100766 chunk_appendf(msg, " key=");
Simon Hormand9366582011-06-15 15:18:45 +0900767 dump_binary(msg, (const char *)entry->key.key, proxy->table.key_size);
768 }
769
Willy Tarreau77804732012-10-29 16:14:26 +0100770 chunk_appendf(msg, " use=%d exp=%d", entry->ref_cnt - 1, tick_remain(now_ms, entry->expire));
Simon Hormand9366582011-06-15 15:18:45 +0900771
772 for (dt = 0; dt < STKTABLE_DATA_TYPES; dt++) {
773 void *ptr;
774
775 if (proxy->table.data_ofs[dt] == 0)
776 continue;
777 if (stktable_data_types[dt].arg_type == ARG_T_DELAY)
Willy Tarreau77804732012-10-29 16:14:26 +0100778 chunk_appendf(msg, " %s(%d)=", stktable_data_types[dt].name, proxy->table.data_arg[dt].u);
Simon Hormand9366582011-06-15 15:18:45 +0900779 else
Willy Tarreau77804732012-10-29 16:14:26 +0100780 chunk_appendf(msg, " %s=", stktable_data_types[dt].name);
Simon Hormand9366582011-06-15 15:18:45 +0900781
782 ptr = stktable_data_ptr(&proxy->table, entry, dt);
783 switch (stktable_data_types[dt].std_type) {
784 case STD_T_SINT:
Willy Tarreau77804732012-10-29 16:14:26 +0100785 chunk_appendf(msg, "%d", stktable_data_cast(ptr, std_t_sint));
Simon Hormand9366582011-06-15 15:18:45 +0900786 break;
787 case STD_T_UINT:
Willy Tarreau77804732012-10-29 16:14:26 +0100788 chunk_appendf(msg, "%u", stktable_data_cast(ptr, std_t_uint));
Simon Hormand9366582011-06-15 15:18:45 +0900789 break;
790 case STD_T_ULL:
Willy Tarreau77804732012-10-29 16:14:26 +0100791 chunk_appendf(msg, "%lld", stktable_data_cast(ptr, std_t_ull));
Simon Hormand9366582011-06-15 15:18:45 +0900792 break;
793 case STD_T_FRQP:
Willy Tarreau77804732012-10-29 16:14:26 +0100794 chunk_appendf(msg, "%d",
Simon Hormand9366582011-06-15 15:18:45 +0900795 read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
796 proxy->table.data_arg[dt].u));
797 break;
798 }
799 }
Willy Tarreau77804732012-10-29 16:14:26 +0100800 chunk_appendf(msg, "\n");
Simon Hormand9366582011-06-15 15:18:45 +0900801
Willy Tarreaubc18da12015-03-13 14:00:47 +0100802 if (bi_putchk(si_ic(si), msg) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +0200803 si_applet_cant_put(si);
Simon Hormand9366582011-06-15 15:18:45 +0900804 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +0100805 }
Simon Hormand9366582011-06-15 15:18:45 +0900806
807 return 1;
808}
809
Willy Tarreaudec98142012-06-06 23:37:08 +0200810static void stats_sock_table_key_request(struct stream_interface *si, char **args, int action)
Simon Horman121f3052011-06-15 15:18:46 +0900811{
Willy Tarreau87b09662015-04-03 00:22:06 +0200812 struct stream *s = si_strm(si);
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100813 struct appctx *appctx = __objt_appctx(si->end);
814 struct proxy *px = appctx->ctx.table.target;
Simon Horman121f3052011-06-15 15:18:46 +0900815 struct stksess *ts;
Simon Hormancec9a222011-06-15 15:18:51 +0900816 uint32_t uint32_key;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900817 unsigned char ip6_key[sizeof(struct in6_addr)];
Willy Tarreau654694e2012-06-07 01:03:16 +0200818 long long value;
819 int data_type;
Willy Tarreau47060b62013-08-01 21:11:42 +0200820 int cur_arg;
Willy Tarreau654694e2012-06-07 01:03:16 +0200821 void *ptr;
822 struct freq_ctr_period *frqp;
Simon Horman121f3052011-06-15 15:18:46 +0900823
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100824 appctx->st0 = STAT_CLI_OUTPUT;
Simon Horman121f3052011-06-15 15:18:46 +0900825
826 if (!*args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100827 appctx->ctx.cli.msg = "Key value expected\n";
828 appctx->st0 = STAT_CLI_PRINT;
Simon Horman121f3052011-06-15 15:18:46 +0900829 return;
830 }
831
Simon Hormanc5b89f62011-06-15 15:18:50 +0900832 switch (px->table.type) {
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200833 case SMP_T_IPV4:
Simon Hormancec9a222011-06-15 15:18:51 +0900834 uint32_key = htonl(inetaddr_host(args[4]));
Willy Tarreau07115412012-10-29 21:56:59 +0100835 static_table_key->key = &uint32_key;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900836 break;
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200837 case SMP_T_IPV6:
Simon Hormanc5b89f62011-06-15 15:18:50 +0900838 inet_pton(AF_INET6, args[4], ip6_key);
Willy Tarreau07115412012-10-29 21:56:59 +0100839 static_table_key->key = &ip6_key;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900840 break;
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200841 case SMP_T_SINT:
Simon Hormancec9a222011-06-15 15:18:51 +0900842 {
843 char *endptr;
844 unsigned long val;
845 errno = 0;
846 val = strtoul(args[4], &endptr, 10);
847 if ((errno == ERANGE && val == ULONG_MAX) ||
848 (errno != 0 && val == 0) || endptr == args[4] ||
849 val > 0xffffffff) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100850 appctx->ctx.cli.msg = "Invalid key\n";
851 appctx->st0 = STAT_CLI_PRINT;
Simon Hormancec9a222011-06-15 15:18:51 +0900852 return;
853 }
854 uint32_key = (uint32_t) val;
Willy Tarreau07115412012-10-29 21:56:59 +0100855 static_table_key->key = &uint32_key;
Simon Hormancec9a222011-06-15 15:18:51 +0900856 break;
857 }
858 break;
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200859 case SMP_T_STR:
Willy Tarreau07115412012-10-29 21:56:59 +0100860 static_table_key->key = args[4];
861 static_table_key->key_len = strlen(args[4]);
Simon Horman619e3cc2011-06-15 15:18:52 +0900862 break;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900863 default:
Willy Tarreaudec98142012-06-06 23:37:08 +0200864 switch (action) {
865 case STAT_CLI_O_TAB:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100866 appctx->ctx.cli.msg = "Showing keys from tables of type other than ip, ipv6, string and integer is not supported\n";
Willy Tarreaudec98142012-06-06 23:37:08 +0200867 break;
868 case STAT_CLI_O_CLR:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100869 appctx->ctx.cli.msg = "Removing keys from ip tables of type other than ip, ipv6, string and integer is not supported\n";
Willy Tarreaudec98142012-06-06 23:37:08 +0200870 break;
871 default:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100872 appctx->ctx.cli.msg = "Unknown action\n";
Willy Tarreaudec98142012-06-06 23:37:08 +0200873 break;
874 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100875 appctx->st0 = STAT_CLI_PRINT;
Simon Horman121f3052011-06-15 15:18:46 +0900876 return;
877 }
878
879 /* check permissions */
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200880 if (strm_li(s)->bind_conf->level < ACCESS_LVL_OPER) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100881 appctx->ctx.cli.msg = stats_permission_denied_msg;
882 appctx->st0 = STAT_CLI_PRINT;
Simon Horman121f3052011-06-15 15:18:46 +0900883 return;
884 }
885
Willy Tarreau07115412012-10-29 21:56:59 +0100886 ts = stktable_lookup_key(&px->table, static_table_key);
Simon Horman17bce342011-06-15 15:18:47 +0900887
Willy Tarreaudec98142012-06-06 23:37:08 +0200888 switch (action) {
889 case STAT_CLI_O_TAB:
890 if (!ts)
891 return;
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100892 chunk_reset(&trash);
893 if (!stats_dump_table_head_to_buffer(&trash, si, px, px))
Simon Horman17bce342011-06-15 15:18:47 +0900894 return;
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100895 stats_dump_table_entry_to_buffer(&trash, si, px, ts);
Simon Horman121f3052011-06-15 15:18:46 +0900896 return;
Willy Tarreaudec98142012-06-06 23:37:08 +0200897
898 case STAT_CLI_O_CLR:
899 if (!ts)
900 return;
901 if (ts->ref_cnt) {
902 /* don't delete an entry which is currently referenced */
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100903 appctx->ctx.cli.msg = "Entry currently in use, cannot remove\n";
904 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaudec98142012-06-06 23:37:08 +0200905 return;
906 }
907 stksess_kill(&px->table, ts);
908 break;
Simon Horman17bce342011-06-15 15:18:47 +0900909
Willy Tarreau654694e2012-06-07 01:03:16 +0200910 case STAT_CLI_O_SET:
Willy Tarreau654694e2012-06-07 01:03:16 +0200911 if (ts)
912 stktable_touch(&px->table, ts, 1);
913 else {
Willy Tarreau07115412012-10-29 21:56:59 +0100914 ts = stksess_new(&px->table, static_table_key);
Willy Tarreau654694e2012-06-07 01:03:16 +0200915 if (!ts) {
916 /* don't delete an entry which is currently referenced */
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100917 appctx->ctx.cli.msg = "Unable to allocate a new entry\n";
918 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau654694e2012-06-07 01:03:16 +0200919 return;
920 }
921 stktable_store(&px->table, ts, 1);
922 }
923
Willy Tarreau47060b62013-08-01 21:11:42 +0200924 for (cur_arg = 5; *args[cur_arg]; cur_arg += 2) {
925 if (strncmp(args[cur_arg], "data.", 5) != 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100926 appctx->ctx.cli.msg = "\"data.<type>\" followed by a value expected\n";
927 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau47060b62013-08-01 21:11:42 +0200928 return;
929 }
930
931 data_type = stktable_get_data_type(args[cur_arg] + 5);
932 if (data_type < 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100933 appctx->ctx.cli.msg = "Unknown data type\n";
934 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau47060b62013-08-01 21:11:42 +0200935 return;
936 }
937
938 if (!px->table.data_ofs[data_type]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100939 appctx->ctx.cli.msg = "Data type not stored in this table\n";
940 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau47060b62013-08-01 21:11:42 +0200941 return;
942 }
943
944 if (!*args[cur_arg+1] || strl2llrc(args[cur_arg+1], strlen(args[cur_arg+1]), &value) != 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100945 appctx->ctx.cli.msg = "Require a valid integer value to store\n";
946 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau47060b62013-08-01 21:11:42 +0200947 return;
948 }
949
950 ptr = stktable_data_ptr(&px->table, ts, data_type);
951
952 switch (stktable_data_types[data_type].std_type) {
953 case STD_T_SINT:
954 stktable_data_cast(ptr, std_t_sint) = value;
955 break;
956 case STD_T_UINT:
957 stktable_data_cast(ptr, std_t_uint) = value;
958 break;
959 case STD_T_ULL:
960 stktable_data_cast(ptr, std_t_ull) = value;
961 break;
962 case STD_T_FRQP:
963 /* We set both the current and previous values. That way
964 * the reported frequency is stable during all the period
965 * then slowly fades out. This allows external tools to
966 * push measures without having to update them too often.
967 */
968 frqp = &stktable_data_cast(ptr, std_t_frqp);
969 frqp->curr_tick = now_ms;
970 frqp->prev_ctr = 0;
971 frqp->curr_ctr = value;
972 break;
973 }
Willy Tarreau654694e2012-06-07 01:03:16 +0200974 }
975 break;
976
Willy Tarreaudec98142012-06-06 23:37:08 +0200977 default:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100978 appctx->ctx.cli.msg = "Unknown action\n";
979 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaudec98142012-06-06 23:37:08 +0200980 break;
Simon Horman121f3052011-06-15 15:18:46 +0900981 }
Simon Horman121f3052011-06-15 15:18:46 +0900982}
983
Willy Tarreau654694e2012-06-07 01:03:16 +0200984static void stats_sock_table_data_request(struct stream_interface *si, char **args, int action)
Simon Hormand5b9fd92011-06-15 15:18:48 +0900985{
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100986 struct appctx *appctx = __objt_appctx(si->end);
987
Willy Tarreau04b3a192013-04-13 09:41:37 +0200988 if (action != STAT_CLI_O_TAB && action != STAT_CLI_O_CLR) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100989 appctx->ctx.cli.msg = "content-based lookup is only supported with the \"show\" and \"clear\" actions";
990 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau654694e2012-06-07 01:03:16 +0200991 return;
992 }
993
Simon Hormand5b9fd92011-06-15 15:18:48 +0900994 /* condition on stored data value */
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100995 appctx->ctx.table.data_type = stktable_get_data_type(args[3] + 5);
996 if (appctx->ctx.table.data_type < 0) {
997 appctx->ctx.cli.msg = "Unknown data type\n";
998 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900999 return;
1000 }
1001
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001002 if (!((struct proxy *)appctx->ctx.table.target)->table.data_ofs[appctx->ctx.table.data_type]) {
1003 appctx->ctx.cli.msg = "Data type not stored in this table\n";
1004 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +09001005 return;
1006 }
1007
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001008 appctx->ctx.table.data_op = get_std_op(args[4]);
1009 if (appctx->ctx.table.data_op < 0) {
1010 appctx->ctx.cli.msg = "Require and operator among \"eq\", \"ne\", \"le\", \"ge\", \"lt\", \"gt\"\n";
1011 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +09001012 return;
1013 }
1014
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001015 if (!*args[5] || strl2llrc(args[5], strlen(args[5]), &appctx->ctx.table.value) != 0) {
1016 appctx->ctx.cli.msg = "Require a valid integer value to compare against\n";
1017 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +09001018 return;
1019 }
1020}
1021
Willy Tarreaudec98142012-06-06 23:37:08 +02001022static void stats_sock_table_request(struct stream_interface *si, char **args, int action)
Simon Hormand5b9fd92011-06-15 15:18:48 +09001023{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001024 struct appctx *appctx = __objt_appctx(si->end);
1025
1026 appctx->ctx.table.data_type = -1;
1027 appctx->st2 = STAT_ST_INIT;
1028 appctx->ctx.table.target = NULL;
1029 appctx->ctx.table.proxy = NULL;
1030 appctx->ctx.table.entry = NULL;
1031 appctx->st0 = action;
Simon Hormand5b9fd92011-06-15 15:18:48 +09001032
1033 if (*args[2]) {
Willy Tarreaue2dc1fa2015-05-26 12:08:07 +02001034 appctx->ctx.table.target = proxy_tbl_by_name(args[2]);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001035 if (!appctx->ctx.table.target) {
1036 appctx->ctx.cli.msg = "No such table\n";
1037 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +09001038 return;
1039 }
1040 }
1041 else {
Willy Tarreaudec98142012-06-06 23:37:08 +02001042 if (action != STAT_CLI_O_TAB)
Simon Hormand5b9fd92011-06-15 15:18:48 +09001043 goto err_args;
1044 return;
1045 }
1046
1047 if (strcmp(args[3], "key") == 0)
Willy Tarreaudec98142012-06-06 23:37:08 +02001048 stats_sock_table_key_request(si, args, action);
Simon Hormanc88b8872011-06-15 15:18:49 +09001049 else if (strncmp(args[3], "data.", 5) == 0)
Willy Tarreau654694e2012-06-07 01:03:16 +02001050 stats_sock_table_data_request(si, args, action);
Simon Hormanc88b8872011-06-15 15:18:49 +09001051 else if (*args[3])
Simon Hormand5b9fd92011-06-15 15:18:48 +09001052 goto err_args;
1053
1054 return;
1055
1056err_args:
Willy Tarreaudec98142012-06-06 23:37:08 +02001057 switch (action) {
1058 case STAT_CLI_O_TAB:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001059 appctx->ctx.cli.msg = "Optional argument only supports \"data.<store_data_type>\" <operator> <value> and key <key>\n";
Willy Tarreaudec98142012-06-06 23:37:08 +02001060 break;
1061 case STAT_CLI_O_CLR:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001062 appctx->ctx.cli.msg = "Required arguments: <table> \"data.<store_data_type>\" <operator> <value> or <table> key <key>\n";
Willy Tarreaudec98142012-06-06 23:37:08 +02001063 break;
1064 default:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001065 appctx->ctx.cli.msg = "Unknown action\n";
Willy Tarreaudec98142012-06-06 23:37:08 +02001066 break;
1067 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001068 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +09001069}
1070
Willy Tarreau532a4502011-09-07 22:37:44 +02001071/* Expects to find a frontend named <arg> and returns it, otherwise displays various
Willy Tarreau87b09662015-04-03 00:22:06 +02001072 * adequate error messages and returns NULL. This function also expects the stream
Willy Tarreau532a4502011-09-07 22:37:44 +02001073 * level to be admin.
1074 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001075static struct proxy *expect_frontend_admin(struct stream *s, struct stream_interface *si, const char *arg)
Willy Tarreau532a4502011-09-07 22:37:44 +02001076{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001077 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau532a4502011-09-07 22:37:44 +02001078 struct proxy *px;
1079
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001080 if (strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001081 appctx->ctx.cli.msg = stats_permission_denied_msg;
1082 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001083 return NULL;
1084 }
1085
1086 if (!*arg) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001087 appctx->ctx.cli.msg = "A frontend name is expected.\n";
1088 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001089 return NULL;
1090 }
1091
Willy Tarreau9e0bb102015-05-26 11:24:42 +02001092 px = proxy_fe_by_name(arg);
Willy Tarreau532a4502011-09-07 22:37:44 +02001093 if (!px) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001094 appctx->ctx.cli.msg = "No such frontend.\n";
1095 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001096 return NULL;
1097 }
1098 return px;
1099}
1100
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001101/* Expects to find a backend and a server in <arg> under the form <backend>/<server>,
1102 * and returns the pointer to the server. Otherwise, display adequate error messages
Willy Tarreau87b09662015-04-03 00:22:06 +02001103 * and returns NULL. This function also expects the stream level to be admin. Note:
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001104 * the <arg> is modified to remove the '/'.
1105 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001106static struct server *expect_server_admin(struct stream *s, struct stream_interface *si, char *arg)
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001107{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001108 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001109 struct proxy *px;
1110 struct server *sv;
1111 char *line;
1112
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001113 if (strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001114 appctx->ctx.cli.msg = stats_permission_denied_msg;
1115 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001116 return NULL;
1117 }
1118
1119 /* split "backend/server" and make <line> point to server */
1120 for (line = arg; *line; line++)
1121 if (*line == '/') {
1122 *line++ = '\0';
1123 break;
1124 }
1125
1126 if (!*line || !*arg) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001127 appctx->ctx.cli.msg = "Require 'backend/server'.\n";
1128 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001129 return NULL;
1130 }
1131
1132 if (!get_backend_server(arg, line, &px, &sv)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001133 appctx->ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
1134 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001135 return NULL;
1136 }
1137
1138 if (px->state == PR_STSTOPPED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001139 appctx->ctx.cli.msg = "Proxy is disabled.\n";
1140 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001141 return NULL;
1142 }
1143
1144 return sv;
1145}
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02001146
1147/* This function is used with TLS ticket keys management. It permits to browse
1148 * each reference. The variable <getnext> must contain the current node,
1149 * <end> point to the root node.
1150 */
1151#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
1152static inline
1153struct tls_keys_ref *tlskeys_list_get_next(struct tls_keys_ref *getnext, struct list *end)
1154{
1155 struct tls_keys_ref *ref = getnext;
1156
1157 while (1) {
1158
1159 /* Get next list entry. */
1160 ref = LIST_NEXT(&ref->list, struct tls_keys_ref *, list);
1161
1162 /* If the entry is the last of the list, return NULL. */
1163 if (&ref->list == end)
1164 return NULL;
1165
1166 return ref;
1167 }
1168}
1169
1170static inline
1171struct tls_keys_ref *tlskeys_ref_lookup_ref(const char *reference)
1172{
1173 int id;
1174 char *error;
1175
1176 /* If the reference starts by a '#', this is numeric id. */
1177 if (reference[0] == '#') {
1178 /* Try to convert the numeric id. If the conversion fails, the lookup fails. */
1179 id = strtol(reference + 1, &error, 10);
1180 if (*error != '\0')
1181 return NULL;
1182
1183 /* Perform the unique id lookup. */
1184 return tlskeys_ref_lookupid(id);
1185 }
1186
1187 /* Perform the string lookup. */
1188 return tlskeys_ref_lookup(reference);
1189}
1190#endif
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001191
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001192/* This function is used with map and acl management. It permits to browse
1193 * each reference. The variable <getnext> must contain the current node,
1194 * <end> point to the root node and the <flags> permit to filter required
1195 * nodes.
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001196 */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001197static inline
1198struct pat_ref *pat_list_get_next(struct pat_ref *getnext, struct list *end,
1199 unsigned int flags)
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001200{
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001201 struct pat_ref *ref = getnext;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001202
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001203 while (1) {
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001204
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001205 /* Get next list entry. */
1206 ref = LIST_NEXT(&ref->list, struct pat_ref *, list);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001207
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001208 /* If the entry is the last of the list, return NULL. */
1209 if (&ref->list == end)
1210 return NULL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001211
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001212 /* If the entry match the flag, return it. */
1213 if (ref->flags & flags)
1214 return ref;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001215 }
1216}
1217
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001218static inline
1219struct pat_ref *pat_ref_lookup_ref(const char *reference)
1220{
1221 int id;
1222 char *error;
1223
1224 /* If the reference starts by a '#', this is numeric id. */
1225 if (reference[0] == '#') {
1226 /* Try to convert the numeric id. If the conversion fails, the lookup fails. */
1227 id = strtol(reference + 1, &error, 10);
1228 if (*error != '\0')
1229 return NULL;
1230
1231 /* Perform the unique id lookup. */
1232 return pat_ref_lookupid(id);
1233 }
1234
1235 /* Perform the string lookup. */
1236 return pat_ref_lookup(reference);
1237}
1238
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001239/* This function is used with map and acl management. It permits to browse
1240 * each reference.
1241 */
1242static inline
1243struct pattern_expr *pat_expr_get_next(struct pattern_expr *getnext, struct list *end)
1244{
1245 struct pattern_expr *expr;
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01001246 expr = LIST_NEXT(&getnext->list, struct pattern_expr *, list);
1247 if (&expr->list == end)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001248 return NULL;
1249 return expr;
1250}
1251
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001252/* Processes the stats interpreter on the statistics socket. This function is
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001253 * called from an applet running in a stream interface. The function returns 1
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001254 * if the request was understood, otherwise zero. It sets appctx->st0 to a value
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02001255 * designating the function which will have to process the request, which can
1256 * also be the print function to display the return message set into cli.msg.
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001257 */
Simon Horman9bd2c732011-06-15 15:18:44 +09001258static int stats_sock_parse_request(struct stream_interface *si, char *line)
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001259{
Willy Tarreau87b09662015-04-03 00:22:06 +02001260 struct stream *s = si_strm(si);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001261 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001262 char *args[MAX_STATS_ARGS + 1];
1263 int arg;
Thierry FOURNIER48bcfda2013-12-10 18:54:58 +01001264 int i, j;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001265
1266 while (isspace((unsigned char)*line))
1267 line++;
1268
1269 arg = 0;
1270 args[arg] = line;
1271
1272 while (*line && arg < MAX_STATS_ARGS) {
Thierry FOURNIER48bcfda2013-12-10 18:54:58 +01001273 if (*line == '\\') {
1274 line++;
1275 if (*line == '\0')
1276 break;
1277 }
1278 else if (isspace((unsigned char)*line)) {
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001279 *line++ = '\0';
1280
1281 while (isspace((unsigned char)*line))
1282 line++;
1283
1284 args[++arg] = line;
1285 continue;
1286 }
1287
1288 line++;
1289 }
1290
1291 while (++arg <= MAX_STATS_ARGS)
1292 args[arg] = line;
1293
Thierry FOURNIER48bcfda2013-12-10 18:54:58 +01001294 /* remove \ */
1295 arg = 0;
1296 while (*args[arg] != '\0') {
1297 j = 0;
1298 for (i=0; args[arg][i] != '\0'; i++) {
1299 if (args[arg][i] == '\\')
1300 continue;
1301 args[arg][j] = args[arg][i];
1302 j++;
1303 }
1304 args[arg][j] = '\0';
1305 arg++;
1306 }
1307
Willy Tarreau6bcb95d2015-05-04 18:07:56 +02001308 appctx->ctx.stats.scope_str = 0;
1309 appctx->ctx.stats.scope_len = 0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001310 appctx->ctx.stats.flags = 0;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001311 if (strcmp(args[0], "show") == 0) {
Baptiste Assmann9b6857e2015-09-18 14:49:12 +02001312 if (strcmp(args[1], "backend") == 0) {
Cyril Bonté6ca9e012016-05-06 12:18:49 +02001313 appctx->ctx.be.px = NULL;
Baptiste Assmann9b6857e2015-09-18 14:49:12 +02001314 appctx->st2 = STAT_ST_INIT;
1315 appctx->st0 = STAT_CLI_O_BACKEND;
1316 }
Willy Tarreauae795722016-02-16 11:27:28 +01001317 else if (strcmp(args[1], "env") == 0) {
1318 extern char **environ;
1319
1320 if (strm_li(s)->bind_conf->level < ACCESS_LVL_OPER) {
1321 appctx->ctx.cli.msg = stats_permission_denied_msg;
1322 appctx->st0 = STAT_CLI_PRINT;
1323 return 1;
1324 }
1325 appctx->ctx.env.var = environ;
1326 appctx->st2 = STAT_ST_INIT;
1327 appctx->st0 = STAT_CLI_O_ENV; // stats_dump_env_to_buffer
1328
1329 if (*args[2]) {
1330 int len = strlen(args[2]);
1331
1332 for (; *appctx->ctx.env.var; appctx->ctx.env.var++) {
1333 if (strncmp(*appctx->ctx.env.var, args[2], len) == 0 &&
1334 (*appctx->ctx.env.var)[len] == '=')
1335 break;
1336 }
1337 if (!*appctx->ctx.env.var) {
1338 appctx->ctx.cli.msg = "Variable not found\n";
1339 appctx->st0 = STAT_CLI_PRINT;
1340 return 1;
1341 }
1342 appctx->st2 = STAT_ST_END;
1343 }
1344 }
1345 else if (strcmp(args[1], "stat") == 0) {
Baptiste Assmann3863f972015-05-17 00:33:24 +02001346 if (strcmp(args[2], "resolvers") == 0) {
1347 struct dns_resolvers *presolvers;
1348
Andrew Hayworth68d05342015-10-02 20:33:01 +00001349 if (*args[3]) {
1350 appctx->ctx.resolvers.ptr = NULL;
1351 list_for_each_entry(presolvers, &dns_resolvers, list) {
1352 if (strcmp(presolvers->id, args[3]) == 0) {
1353 appctx->ctx.resolvers.ptr = presolvers;
1354 break;
1355 }
Baptiste Assmann3863f972015-05-17 00:33:24 +02001356 }
Andrew Hayworth68d05342015-10-02 20:33:01 +00001357 if (appctx->ctx.resolvers.ptr == NULL) {
1358 appctx->ctx.cli.msg = "Can't find that resolvers section\n";
1359 appctx->st0 = STAT_CLI_PRINT;
1360 return 1;
1361 }
Baptiste Assmann3863f972015-05-17 00:33:24 +02001362 }
1363
1364 appctx->st2 = STAT_ST_INIT;
1365 appctx->st0 = STAT_CLI_O_RESOLVERS;
1366 return 1;
1367 }
1368 else if (*args[2] && *args[3] && *args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001369 appctx->ctx.stats.flags |= STAT_BOUND;
1370 appctx->ctx.stats.iid = atoi(args[2]);
1371 appctx->ctx.stats.type = atoi(args[3]);
1372 appctx->ctx.stats.sid = atoi(args[4]);
Willy Tarreau1e62df92016-01-11 18:57:53 +01001373 if (strcmp(args[5], "typed") == 0)
1374 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001375 }
Willy Tarreau1e62df92016-01-11 18:57:53 +01001376 else if (strcmp(args[2], "typed") == 0)
1377 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001378
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001379 appctx->st2 = STAT_ST_INIT;
1380 appctx->st0 = STAT_CLI_O_STAT; // stats_dump_stat_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001381 }
1382 else if (strcmp(args[1], "info") == 0) {
Willy Tarreaucb809122016-01-11 20:08:42 +01001383 if (strcmp(args[2], "typed") == 0)
1384 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001385 appctx->st2 = STAT_ST_INIT;
1386 appctx->st0 = STAT_CLI_O_INFO; // stats_dump_info_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001387 }
Baptiste Assmann28289462015-07-03 08:01:20 +02001388 else if (strcmp(args[1], "servers") == 0 && strcmp(args[2], "state") == 0) {
Cyril Bonté76a99782016-05-06 12:18:48 +02001389 appctx->ctx.server_state.iid = 0;
1390 appctx->ctx.server_state.px = NULL;
1391 appctx->ctx.server_state.sv = NULL;
Baptiste Assmann28289462015-07-03 08:01:20 +02001392
1393 /* check if a backend name has been provided */
1394 if (*args[3]) {
1395 /* read server state from local file */
Cyril Bonté76a99782016-05-06 12:18:48 +02001396 appctx->ctx.server_state.px = proxy_be_by_name(args[3]);
Baptiste Assmann28289462015-07-03 08:01:20 +02001397
Cyril Bonté76a99782016-05-06 12:18:48 +02001398 if (!appctx->ctx.server_state.px) {
Baptiste Assmann28289462015-07-03 08:01:20 +02001399 appctx->ctx.cli.msg = "Can't find backend.\n";
1400 appctx->st0 = STAT_CLI_PRINT;
1401 return 1;
1402 }
Cyril Bonté76a99782016-05-06 12:18:48 +02001403 appctx->ctx.server_state.iid = appctx->ctx.server_state.px->uuid;
Baptiste Assmann28289462015-07-03 08:01:20 +02001404 }
1405 appctx->st2 = STAT_ST_INIT;
1406 appctx->st0 = STAT_CLI_O_SERVERS_STATE; // stats_dump_servers_state_to_buffer
1407 return 1;
1408 }
Willy Tarreau12833bb2014-01-28 16:49:56 +01001409 else if (strcmp(args[1], "pools") == 0) {
1410 appctx->st2 = STAT_ST_INIT;
1411 appctx->st0 = STAT_CLI_O_POOLS; // stats_dump_pools_to_buffer
1412 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001413 else if (strcmp(args[1], "sess") == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001414 appctx->st2 = STAT_ST_INIT;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001415 if (strm_li(s)->bind_conf->level < ACCESS_LVL_OPER) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001416 appctx->ctx.cli.msg = stats_permission_denied_msg;
1417 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +02001418 return 1;
1419 }
Willy Tarreau76153662012-11-26 01:16:39 +01001420 if (*args[2] && strcmp(args[2], "all") == 0)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001421 appctx->ctx.sess.target = (void *)-1;
Willy Tarreau76153662012-11-26 01:16:39 +01001422 else if (*args[2])
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001423 appctx->ctx.sess.target = (void *)strtoul(args[2], NULL, 0);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01001424 else
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001425 appctx->ctx.sess.target = NULL;
Willy Tarreau87b09662015-04-03 00:22:06 +02001426 appctx->ctx.sess.section = 0; /* start with stream status */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001427 appctx->ctx.sess.pos = 0;
1428 appctx->st0 = STAT_CLI_O_SESS; // stats_dump_sess_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001429 }
1430 else if (strcmp(args[1], "errors") == 0) {
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001431 if (strm_li(s)->bind_conf->level < ACCESS_LVL_OPER) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001432 appctx->ctx.cli.msg = stats_permission_denied_msg;
1433 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +02001434 return 1;
1435 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001436 if (*args[2])
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001437 appctx->ctx.errors.iid = atoi(args[2]);
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001438 else
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001439 appctx->ctx.errors.iid = -1;
1440 appctx->ctx.errors.px = NULL;
1441 appctx->st2 = STAT_ST_INIT;
1442 appctx->st0 = STAT_CLI_O_ERR; // stats_dump_errors_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001443 }
Willy Tarreau69f58c82010-07-12 17:55:33 +02001444 else if (strcmp(args[1], "table") == 0) {
Willy Tarreaudec98142012-06-06 23:37:08 +02001445 stats_sock_table_request(si, args, STAT_CLI_O_TAB);
Willy Tarreau69f58c82010-07-12 17:55:33 +02001446 }
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02001447 else if (strcmp(args[1], "tls-keys") == 0) {
1448#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
William Lallemand1d0b36a2016-05-20 17:40:26 +02001449 /* no parameter, shows only file list */
1450 if (!*args[2]) {
1451 appctx->st2 = STAT_ST_INIT;
1452 appctx->st0 = STAT_CLI_O_TLSK;
1453 return 1;
1454 }
1455
1456 if (args[2][0] == '*') {
1457 /* list every TLS ticket keys */
1458 appctx->ctx.tlskeys.ref = NULL;
1459 } else {
1460 appctx->ctx.tlskeys.ref = tlskeys_ref_lookup_ref(args[2]);
1461 if(!appctx->ctx.tlskeys.ref) {
1462 appctx->ctx.cli.msg = "'show tls-keys' unable to locate referenced filename\n";
1463 appctx->st0 = STAT_CLI_PRINT;
1464 return 1;
1465 }
1466 }
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02001467 appctx->st2 = STAT_ST_INIT;
William Lallemand1d0b36a2016-05-20 17:40:26 +02001468 appctx->st0 = STAT_CLI_O_TLSK_ENT;
1469
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02001470#else
1471 appctx->ctx.cli.msg = "HAProxy was compiled against a version of OpenSSL "
1472 "that doesn't support specifying TLS ticket keys\n";
1473 appctx->st0 = STAT_CLI_PRINT;
1474#endif
1475 return 1;
1476 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001477 else if (strcmp(args[1], "map") == 0 ||
1478 strcmp(args[1], "acl") == 0) {
1479
1480 /* Set ACL or MAP flags. */
1481 if (args[1][0] == 'm')
1482 appctx->ctx.map.display_flags = PAT_REF_MAP;
1483 else
1484 appctx->ctx.map.display_flags = PAT_REF_ACL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001485
Cyril Bontédb98eb32016-05-06 12:18:50 +02001486 /* no parameter: display all map available */
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001487 if (!*args[2]) {
1488 appctx->st2 = STAT_ST_INIT;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001489 appctx->st0 = STAT_CLI_O_PATS;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001490 return 1;
1491 }
1492
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001493 /* lookup into the refs and check the map flag */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001494 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001495 if (!appctx->ctx.map.ref ||
1496 !(appctx->ctx.map.ref->flags & appctx->ctx.map.display_flags)) {
1497 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001498 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001499 else
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001500 appctx->ctx.cli.msg = "Unknown ACL identifier. Please use #<id> or <file>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001501 appctx->st0 = STAT_CLI_PRINT;
1502 return 1;
1503 }
1504 appctx->st2 = STAT_ST_INIT;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001505 appctx->st0 = STAT_CLI_O_PAT;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001506 }
Aman Guptaceafb4a2012-04-02 18:57:54 -07001507 else { /* neither "stat" nor "info" nor "sess" nor "errors" nor "table" */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001508 return 0;
1509 }
1510 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001511 else if (strcmp(args[0], "clear") == 0) {
1512 if (strcmp(args[1], "counters") == 0) {
1513 struct proxy *px;
1514 struct server *sv;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001515 struct listener *li;
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001516 int clrall = 0;
1517
1518 if (strcmp(args[2], "all") == 0)
1519 clrall = 1;
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001520
Willy Tarreau6162db22009-10-10 17:13:00 +02001521 /* check permissions */
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001522 if (strm_li(s)->bind_conf->level < ACCESS_LVL_OPER ||
1523 (clrall && strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001524 appctx->ctx.cli.msg = stats_permission_denied_msg;
1525 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +02001526 return 1;
1527 }
1528
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001529 for (px = proxy; px; px = px->next) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001530 if (clrall) {
1531 memset(&px->be_counters, 0, sizeof(px->be_counters));
1532 memset(&px->fe_counters, 0, sizeof(px->fe_counters));
1533 }
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001534 else {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001535 px->be_counters.conn_max = 0;
1536 px->be_counters.p.http.rps_max = 0;
1537 px->be_counters.sps_max = 0;
1538 px->be_counters.cps_max = 0;
1539 px->be_counters.nbpend_max = 0;
1540
1541 px->fe_counters.conn_max = 0;
1542 px->fe_counters.p.http.rps_max = 0;
1543 px->fe_counters.sps_max = 0;
1544 px->fe_counters.cps_max = 0;
1545 px->fe_counters.nbpend_max = 0;
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001546 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001547
1548 for (sv = px->srv; sv; sv = sv->next)
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001549 if (clrall)
1550 memset(&sv->counters, 0, sizeof(sv->counters));
1551 else {
1552 sv->counters.cur_sess_max = 0;
1553 sv->counters.nbpend_max = 0;
1554 sv->counters.sps_max = 0;
1555 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001556
Willy Tarreaua7944ad2012-09-26 21:03:11 +02001557 list_for_each_entry(li, &px->conf.listeners, by_fe)
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001558 if (li->counters) {
1559 if (clrall)
1560 memset(li->counters, 0, sizeof(*li->counters));
1561 else
1562 li->counters->conn_max = 0;
1563 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001564 }
1565
Willy Tarreau81c25d02011-09-07 15:17:21 +02001566 global.cps_max = 0;
Willy Tarreau93e7c002013-10-07 18:51:07 +02001567 global.sps_max = 0;
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001568 return 1;
1569 }
Willy Tarreau88ee3972010-07-13 13:48:00 +02001570 else if (strcmp(args[1], "table") == 0) {
Willy Tarreaudec98142012-06-06 23:37:08 +02001571 stats_sock_table_request(si, args, STAT_CLI_O_CLR);
Willy Tarreau88ee3972010-07-13 13:48:00 +02001572 /* end of processing */
1573 return 1;
1574 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001575 else if (strcmp(args[1], "map") == 0 || strcmp(args[1], "acl") == 0) {
1576 /* Set ACL or MAP flags. */
1577 if (args[1][0] == 'm')
1578 appctx->ctx.map.display_flags = PAT_REF_MAP;
1579 else
1580 appctx->ctx.map.display_flags = PAT_REF_ACL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001581
1582 /* no parameter */
1583 if (!*args[2]) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001584 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
1585 appctx->ctx.cli.msg = "Missing map identifier.\n";
1586 else
1587 appctx->ctx.cli.msg = "Missing ACL identifier.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001588 appctx->st0 = STAT_CLI_PRINT;
1589 return 1;
1590 }
1591
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001592 /* lookup into the refs and check the map flag */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001593 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001594 if (!appctx->ctx.map.ref ||
1595 !(appctx->ctx.map.ref->flags & appctx->ctx.map.display_flags)) {
1596 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001597 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001598 else
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001599 appctx->ctx.cli.msg = "Unknown ACL identifier. Please use #<id> or <file>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001600 appctx->st0 = STAT_CLI_PRINT;
1601 return 1;
1602 }
1603
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001604 /* Clear all. */
1605 pat_ref_prune(appctx->ctx.map.ref);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001606
1607 /* return response */
Thierry FOURNIER07e78c52014-12-18 15:28:01 +01001608 appctx->st0 = STAT_CLI_PROMPT;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001609 return 1;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001610 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001611 else {
Willy Tarreau88ee3972010-07-13 13:48:00 +02001612 /* unknown "clear" argument */
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001613 return 0;
1614 }
1615 }
Willy Tarreau38338fa2009-10-10 18:37:29 +02001616 else if (strcmp(args[0], "get") == 0) {
1617 if (strcmp(args[1], "weight") == 0) {
1618 struct proxy *px;
1619 struct server *sv;
1620
1621 /* split "backend/server" and make <line> point to server */
1622 for (line = args[2]; *line; line++)
1623 if (*line == '/') {
1624 *line++ = '\0';
1625 break;
1626 }
1627
1628 if (!*line) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001629 appctx->ctx.cli.msg = "Require 'backend/server'.\n";
1630 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau38338fa2009-10-10 18:37:29 +02001631 return 1;
1632 }
1633
1634 if (!get_backend_server(args[2], line, &px, &sv)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001635 appctx->ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
1636 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau38338fa2009-10-10 18:37:29 +02001637 return 1;
1638 }
1639
1640 /* return server's effective weight at the moment */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001641 snprintf(trash.str, trash.size, "%d (initial %d)\n", sv->uweight, sv->iweight);
Willy Tarreaubc18da12015-03-13 14:00:47 +01001642 if (bi_putstr(si_ic(si), trash.str) == -1)
Willy Tarreaufe127932015-04-21 19:23:39 +02001643 si_applet_cant_put(si);
Willy Tarreaubc18da12015-03-13 14:00:47 +01001644
Willy Tarreau38338fa2009-10-10 18:37:29 +02001645 return 1;
1646 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001647 else if (strcmp(args[1], "map") == 0 || strcmp(args[1], "acl") == 0) {
1648 /* Set flags. */
1649 if (args[1][0] == 'm')
1650 appctx->ctx.map.display_flags = PAT_REF_MAP;
1651 else
1652 appctx->ctx.map.display_flags = PAT_REF_ACL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001653
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001654 /* No parameter. */
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001655 if (!*args[2] || !*args[3]) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001656 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
1657 appctx->ctx.cli.msg = "Missing map identifier and/or key.\n";
1658 else
1659 appctx->ctx.cli.msg = "Missing ACL identifier and/or key.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001660 appctx->st0 = STAT_CLI_PRINT;
1661 return 1;
1662 }
1663
1664 /* lookup into the maps */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001665 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001666 if (!appctx->ctx.map.ref) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001667 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001668 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001669 else
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001670 appctx->ctx.cli.msg = "Unknown ACL identifier. Please use #<id> or <file>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001671 appctx->st0 = STAT_CLI_PRINT;
1672 return 1;
1673 }
1674
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001675 /* copy input string. The string must be allocated because
1676 * it may be used over multiple iterations. It's released
1677 * at the end and upon abort anyway.
1678 */
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001679 appctx->ctx.map.chunk.len = strlen(args[3]);
1680 appctx->ctx.map.chunk.size = appctx->ctx.map.chunk.len + 1;
1681 appctx->ctx.map.chunk.str = strdup(args[3]);
1682 if (!appctx->ctx.map.chunk.str) {
1683 appctx->ctx.cli.msg = "Out of memory error.\n";
1684 appctx->st0 = STAT_CLI_PRINT;
1685 return 1;
1686 }
1687
1688 /* prepare response */
1689 appctx->st2 = STAT_ST_INIT;
1690 appctx->st0 = STAT_CLI_O_MLOOK;
1691 }
Willy Tarreau38338fa2009-10-10 18:37:29 +02001692 else { /* not "get weight" */
1693 return 0;
1694 }
1695 }
Willy Tarreau4483d432009-10-10 19:30:08 +02001696 else if (strcmp(args[0], "set") == 0) {
1697 if (strcmp(args[1], "weight") == 0) {
Willy Tarreau4483d432009-10-10 19:30:08 +02001698 struct server *sv;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001699 const char *warning;
Willy Tarreau4483d432009-10-10 19:30:08 +02001700
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001701 sv = expect_server_admin(s, si, args[2]);
1702 if (!sv)
Willy Tarreau4483d432009-10-10 19:30:08 +02001703 return 1;
Willy Tarreau4483d432009-10-10 19:30:08 +02001704
Simon Horman7d09b9a2013-02-12 10:45:51 +09001705 warning = server_parse_weight_change_request(sv, args[3]);
1706 if (warning) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001707 appctx->ctx.cli.msg = warning;
1708 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +02001709 }
Willy Tarreau4483d432009-10-10 19:30:08 +02001710 return 1;
1711 }
Willy Tarreau2a4b70f2014-05-22 18:42:35 +02001712 else if (strcmp(args[1], "server") == 0) {
1713 struct server *sv;
1714 const char *warning;
1715
1716 sv = expect_server_admin(s, si, args[2]);
1717 if (!sv)
1718 return 1;
1719
1720 if (strcmp(args[3], "weight") == 0) {
1721 warning = server_parse_weight_change_request(sv, args[4]);
1722 if (warning) {
1723 appctx->ctx.cli.msg = warning;
1724 appctx->st0 = STAT_CLI_PRINT;
1725 }
1726 }
1727 else if (strcmp(args[3], "state") == 0) {
1728 if (strcmp(args[4], "ready") == 0)
1729 srv_adm_set_ready(sv);
1730 else if (strcmp(args[4], "drain") == 0)
1731 srv_adm_set_drain(sv);
1732 else if (strcmp(args[4], "maint") == 0)
1733 srv_adm_set_maint(sv);
1734 else {
1735 appctx->ctx.cli.msg = "'set server <srv> state' expects 'ready', 'drain' and 'maint'.\n";
1736 appctx->st0 = STAT_CLI_PRINT;
1737 }
1738 }
1739 else if (strcmp(args[3], "health") == 0) {
1740 if (sv->track) {
1741 appctx->ctx.cli.msg = "cannot change health on a tracking server.\n";
1742 appctx->st0 = STAT_CLI_PRINT;
1743 }
1744 else if (strcmp(args[4], "up") == 0) {
1745 sv->check.health = sv->check.rise + sv->check.fall - 1;
1746 srv_set_running(sv, "changed from CLI");
1747 }
1748 else if (strcmp(args[4], "stopping") == 0) {
1749 sv->check.health = sv->check.rise + sv->check.fall - 1;
1750 srv_set_stopping(sv, "changed from CLI");
1751 }
1752 else if (strcmp(args[4], "down") == 0) {
1753 sv->check.health = 0;
1754 srv_set_stopped(sv, "changed from CLI");
1755 }
1756 else {
1757 appctx->ctx.cli.msg = "'set server <srv> health' expects 'up', 'stopping', or 'down'.\n";
1758 appctx->st0 = STAT_CLI_PRINT;
1759 }
1760 }
1761 else if (strcmp(args[3], "agent") == 0) {
1762 if (!(sv->agent.state & CHK_ST_ENABLED)) {
1763 appctx->ctx.cli.msg = "agent checks are not enabled on this server.\n";
1764 appctx->st0 = STAT_CLI_PRINT;
1765 }
1766 else if (strcmp(args[4], "up") == 0) {
1767 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
1768 srv_set_running(sv, "changed from CLI");
1769 }
1770 else if (strcmp(args[4], "down") == 0) {
1771 sv->agent.health = 0;
1772 srv_set_stopped(sv, "changed from CLI");
1773 }
1774 else {
1775 appctx->ctx.cli.msg = "'set server <srv> agent' expects 'up' or 'down'.\n";
1776 appctx->st0 = STAT_CLI_PRINT;
1777 }
1778 }
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001779 else if (strcmp(args[3], "addr") == 0) {
Thierry Fournier09a91782016-02-24 08:25:39 +01001780 warning = server_parse_addr_change_request(sv, args[4], "stats command");
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001781 if (warning) {
1782 appctx->ctx.cli.msg = warning;
1783 appctx->st0 = STAT_CLI_PRINT;
1784 }
1785 }
Willy Tarreau2a4b70f2014-05-22 18:42:35 +02001786 else {
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001787 appctx->ctx.cli.msg = "'set server <srv>' only supports 'agent', 'health', 'state', 'weight' and 'addr'.\n";
Willy Tarreau2a4b70f2014-05-22 18:42:35 +02001788 appctx->st0 = STAT_CLI_PRINT;
1789 }
1790 return 1;
1791 }
Willy Tarreau7aabd112010-01-26 10:59:06 +01001792 else if (strcmp(args[1], "timeout") == 0) {
1793 if (strcmp(args[2], "cli") == 0) {
1794 unsigned timeout;
1795 const char *res;
1796
1797 if (!*args[3]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001798 appctx->ctx.cli.msg = "Expects an integer value.\n";
1799 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001800 return 1;
1801 }
1802
1803 res = parse_time_err(args[3], &timeout, TIME_UNIT_S);
1804 if (res || timeout < 1) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001805 appctx->ctx.cli.msg = "Invalid timeout value.\n";
1806 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001807 return 1;
1808 }
1809
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001810 s->req.rto = s->res.wto = 1 + MS_TO_TICKS(timeout*1000);
Willy Tarreau7aabd112010-01-26 10:59:06 +01001811 return 1;
1812 }
1813 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001814 appctx->ctx.cli.msg = "'set timeout' only supports 'cli'.\n";
1815 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001816 return 1;
1817 }
1818 }
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001819 else if (strcmp(args[1], "maxconn") == 0) {
1820 if (strcmp(args[2], "frontend") == 0) {
1821 struct proxy *px;
1822 struct listener *l;
1823 int v;
1824
Willy Tarreau532a4502011-09-07 22:37:44 +02001825 px = expect_frontend_admin(s, si, args[3]);
1826 if (!px)
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001827 return 1;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001828
1829 if (!*args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001830 appctx->ctx.cli.msg = "Integer value expected.\n";
1831 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001832 return 1;
1833 }
1834
1835 v = atoi(args[4]);
Willy Tarreau3c7a79d2012-09-26 21:07:15 +02001836 if (v < 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001837 appctx->ctx.cli.msg = "Value out of range.\n";
1838 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001839 return 1;
1840 }
1841
1842 /* OK, the value is fine, so we assign it to the proxy and to all of
1843 * its listeners. The blocked ones will be dequeued.
1844 */
1845 px->maxconn = v;
Willy Tarreaua7944ad2012-09-26 21:03:11 +02001846 list_for_each_entry(l, &px->conf.listeners, by_fe) {
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001847 l->maxconn = v;
1848 if (l->state == LI_FULL)
1849 resume_listener(l);
1850 }
1851
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001852 if (px->maxconn > px->feconn && !LIST_ISEMPTY(&strm_fe(s)->listener_queue))
1853 dequeue_all_listeners(&strm_fe(s)->listener_queue);
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001854
1855 return 1;
1856 }
Andrew Hayworthedb93a72015-10-27 21:46:25 +00001857 else if (strcmp(args[2], "server") == 0) {
1858 struct server *sv;
Nenad Merdanovic5c3ed342016-04-24 23:10:07 +02001859 const char *warning;
Andrew Hayworthedb93a72015-10-27 21:46:25 +00001860
1861 sv = expect_server_admin(s, si, args[3]);
1862 if (!sv)
1863 return 1;
1864
Nenad Merdanovic5c3ed342016-04-24 23:10:07 +02001865 warning = server_parse_maxconn_change_request(sv, args[4]);
1866 if (warning) {
1867 appctx->ctx.cli.msg = warning;
Andrew Hayworthedb93a72015-10-27 21:46:25 +00001868 appctx->st0 = STAT_CLI_PRINT;
Andrew Hayworthedb93a72015-10-27 21:46:25 +00001869 }
1870
Andrew Hayworthedb93a72015-10-27 21:46:25 +00001871 return 1;
1872 }
Willy Tarreau91886b62011-09-07 14:38:31 +02001873 else if (strcmp(args[2], "global") == 0) {
1874 int v;
1875
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001876 if (strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001877 appctx->ctx.cli.msg = stats_permission_denied_msg;
1878 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau91886b62011-09-07 14:38:31 +02001879 return 1;
1880 }
1881
1882 if (!*args[3]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001883 appctx->ctx.cli.msg = "Expects an integer value.\n";
1884 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau91886b62011-09-07 14:38:31 +02001885 return 1;
1886 }
1887
1888 v = atoi(args[3]);
1889 if (v > global.hardmaxconn) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001890 appctx->ctx.cli.msg = "Value out of range.\n";
1891 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau91886b62011-09-07 14:38:31 +02001892 return 1;
1893 }
1894
1895 /* check for unlimited values */
1896 if (v <= 0)
1897 v = global.hardmaxconn;
1898
1899 global.maxconn = v;
1900
1901 /* Dequeues all of the listeners waiting for a resource */
1902 if (!LIST_ISEMPTY(&global_listener_queue))
1903 dequeue_all_listeners(&global_listener_queue);
1904
1905 return 1;
1906 }
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001907 else {
Andrew Hayworthedb93a72015-10-27 21:46:25 +00001908 appctx->ctx.cli.msg = "'set maxconn' only supports 'frontend', 'server', and 'global'.\n";
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001909 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001910 return 1;
1911 }
1912 }
Willy Tarreauf5b22872011-09-07 16:13:44 +02001913 else if (strcmp(args[1], "rate-limit") == 0) {
1914 if (strcmp(args[2], "connections") == 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 Tarreau7b4b4992013-12-01 09:15:12 +01001919 appctx->ctx.cli.msg = stats_permission_denied_msg;
1920 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001921 return 1;
1922 }
1923
1924 if (!*args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001925 appctx->ctx.cli.msg = "Expects an integer value.\n";
1926 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001927 return 1;
1928 }
1929
1930 v = atoi(args[4]);
1931 if (v < 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001932 appctx->ctx.cli.msg = "Value out of range.\n";
1933 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001934 return 1;
1935 }
1936
1937 global.cps_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 {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001946 appctx->ctx.cli.msg = "'set rate-limit connections' only supports 'global'.\n";
1947 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001948 return 1;
1949 }
1950 }
Willy Tarreau93e7c002013-10-07 18:51:07 +02001951 else if (strcmp(args[2], "sessions") == 0) {
1952 if (strcmp(args[3], "global") == 0) {
1953 int v;
1954
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001955 if (strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau93e7c002013-10-07 18:51:07 +02001956 appctx->ctx.cli.msg = stats_permission_denied_msg;
1957 appctx->st0 = STAT_CLI_PRINT;
1958 return 1;
1959 }
1960
1961 if (!*args[4]) {
1962 appctx->ctx.cli.msg = "Expects an integer value.\n";
1963 appctx->st0 = STAT_CLI_PRINT;
1964 return 1;
1965 }
1966
1967 v = atoi(args[4]);
1968 if (v < 0) {
1969 appctx->ctx.cli.msg = "Value out of range.\n";
1970 appctx->st0 = STAT_CLI_PRINT;
1971 return 1;
1972 }
1973
1974 global.sps_lim = v;
1975
1976 /* Dequeues all of the listeners waiting for a resource */
1977 if (!LIST_ISEMPTY(&global_listener_queue))
1978 dequeue_all_listeners(&global_listener_queue);
1979
1980 return 1;
1981 }
1982 else {
1983 appctx->ctx.cli.msg = "'set rate-limit sessions' only supports 'global'.\n";
1984 appctx->st0 = STAT_CLI_PRINT;
1985 return 1;
1986 }
1987 }
Willy Tarreaue43d5322013-10-07 20:01:52 +02001988#ifdef USE_OPENSSL
1989 else if (strcmp(args[2], "ssl-sessions") == 0) {
1990 if (strcmp(args[3], "global") == 0) {
1991 int v;
1992
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001993 if (strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreaue43d5322013-10-07 20:01:52 +02001994 appctx->ctx.cli.msg = stats_permission_denied_msg;
1995 appctx->st0 = STAT_CLI_PRINT;
1996 return 1;
1997 }
1998
1999 if (!*args[4]) {
2000 appctx->ctx.cli.msg = "Expects an integer value.\n";
2001 appctx->st0 = STAT_CLI_PRINT;
2002 return 1;
2003 }
2004
2005 v = atoi(args[4]);
2006 if (v < 0) {
2007 appctx->ctx.cli.msg = "Value out of range.\n";
2008 appctx->st0 = STAT_CLI_PRINT;
2009 return 1;
2010 }
2011
2012 global.ssl_lim = v;
2013
2014 /* Dequeues all of the listeners waiting for a resource */
2015 if (!LIST_ISEMPTY(&global_listener_queue))
2016 dequeue_all_listeners(&global_listener_queue);
2017
2018 return 1;
2019 }
2020 else {
2021 appctx->ctx.cli.msg = "'set rate-limit ssl-sessions' only supports 'global'.\n";
2022 appctx->st0 = STAT_CLI_PRINT;
2023 return 1;
2024 }
2025 }
2026#endif
William Lallemandd85f9172012-11-09 17:05:39 +01002027 else if (strcmp(args[2], "http-compression") == 0) {
2028 if (strcmp(args[3], "global") == 0) {
2029 int v;
2030
Willy Tarreaua1c2b2c2015-11-26 18:32:39 +01002031 if (strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN) {
2032 appctx->ctx.cli.msg = stats_permission_denied_msg;
2033 appctx->st0 = STAT_CLI_PRINT;
2034 return 1;
2035 }
2036
Willy Tarreau85d47f92012-11-21 00:29:50 +01002037 if (!*args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002038 appctx->ctx.cli.msg = "Expects a maximum input byte rate in kB/s.\n";
2039 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau85d47f92012-11-21 00:29:50 +01002040 return 1;
2041 }
2042
William Lallemandd85f9172012-11-09 17:05:39 +01002043 v = atoi(args[4]);
2044 global.comp_rate_lim = v * 1024; /* Kilo to bytes. */
2045 }
2046 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002047 appctx->ctx.cli.msg = "'set rate-limit http-compression' only supports 'global'.\n";
2048 appctx->st0 = STAT_CLI_PRINT;
William Lallemandd85f9172012-11-09 17:05:39 +01002049 return 1;
2050 }
2051 }
Willy Tarreauf5b22872011-09-07 16:13:44 +02002052 else {
Willy Tarreaue43d5322013-10-07 20:01:52 +02002053 appctx->ctx.cli.msg = "'set rate-limit' supports 'connections', 'sessions', 'ssl-sessions', and 'http-compression'.\n";
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002054 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02002055 return 1;
2056 }
2057 }
Willy Tarreau654694e2012-06-07 01:03:16 +02002058 else if (strcmp(args[1], "table") == 0) {
2059 stats_sock_table_request(si, args, STAT_CLI_O_SET);
2060 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002061 else if (strcmp(args[1], "map") == 0) {
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002062 char *err;
2063
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002064 /* Set flags. */
2065 appctx->ctx.map.display_flags = PAT_REF_MAP;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002066
2067 /* Expect three parameters: map name, key and new value. */
2068 if (!*args[2] || !*args[3] || !*args[4]) {
Thierry FOURNIERd5723432014-03-11 13:52:44 +01002069 appctx->ctx.cli.msg = "'set map' expects three parameters: map identifier, key and value.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002070 appctx->st0 = STAT_CLI_PRINT;
2071 return 1;
2072 }
2073
2074 /* Lookup the reference in the maps. */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002075 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002076 if (!appctx->ctx.map.ref) {
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01002077 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002078 appctx->st0 = STAT_CLI_PRINT;
2079 return 1;
2080 }
2081
Thierry FOURNIER9356c682014-01-28 15:55:37 +01002082 /* If the entry identifier start with a '#', it is considered as
2083 * pointer id
2084 */
2085 if (args[3][0] == '#' && args[3][1] == '0' && args[3][2] == 'x') {
2086 struct pat_ref_elt *ref;
2087 long long int conv;
2088 char *error;
2089
2090 /* Convert argument to integer value. */
2091 conv = strtoll(&args[3][1], &error, 16);
2092 if (*error != '\0') {
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01002093 appctx->ctx.cli.msg = "Malformed identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER9356c682014-01-28 15:55:37 +01002094 appctx->st0 = STAT_CLI_PRINT;
2095 return 1;
2096 }
2097
2098 /* Convert and check integer to pointer. */
2099 ref = (struct pat_ref_elt *)(long)conv;
2100 if ((long long int)(long)ref != conv) {
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01002101 appctx->ctx.cli.msg = "Malformed identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER9356c682014-01-28 15:55:37 +01002102 appctx->st0 = STAT_CLI_PRINT;
2103 return 1;
2104 }
2105
2106 /* Try to delete the entry. */
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002107 err = NULL;
2108 if (!pat_ref_set_by_id(appctx->ctx.map.ref, ref, args[4], &err)) {
2109 if (err)
2110 memprintf(&err, "%s.\n", err);
2111 appctx->ctx.cli.err = err;
2112 appctx->st0 = STAT_CLI_PRINT_FREE;
Thierry FOURNIER9356c682014-01-28 15:55:37 +01002113 return 1;
2114 }
2115 }
2116 else {
2117 /* Else, use the entry identifier as pattern
2118 * string, and update the value.
2119 */
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002120 err = NULL;
2121 if (!pat_ref_set(appctx->ctx.map.ref, args[3], args[4], &err)) {
2122 if (err)
2123 memprintf(&err, "%s.\n", err);
2124 appctx->ctx.cli.err = err;
2125 appctx->st0 = STAT_CLI_PRINT_FREE;
Thierry FOURNIER9356c682014-01-28 15:55:37 +01002126 return 1;
2127 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002128 }
2129
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002130 /* The set is done, send message. */
Thierry FOURNIER07e78c52014-12-18 15:28:01 +01002131 appctx->st0 = STAT_CLI_PROMPT;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002132 return 1;
2133 }
Emeric Brun4147b2e2014-06-16 18:36:30 +02002134#ifdef USE_OPENSSL
2135 else if (strcmp(args[1], "ssl") == 0) {
2136 if (strcmp(args[2], "ocsp-response") == 0) {
Lukas Tribuse4e30f72014-12-09 16:32:51 +01002137#if (defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP)
Emeric Brun4147b2e2014-06-16 18:36:30 +02002138 char *err = NULL;
2139
Emeric Brunaf4ef742014-06-19 14:10:45 +02002140 /* Expect one parameter: the new response in base64 encoding */
Emeric Brun4147b2e2014-06-16 18:36:30 +02002141 if (!*args[3]) {
2142 appctx->ctx.cli.msg = "'set ssl ocsp-response' expects response in base64 encoding.\n";
2143 appctx->st0 = STAT_CLI_PRINT;
2144 return 1;
2145 }
2146
2147 trash.len = base64dec(args[3], strlen(args[3]), trash.str, trash.size);
2148 if (trash.len < 0) {
2149 appctx->ctx.cli.msg = "'set ssl ocsp-response' received invalid base64 encoded response.\n";
2150 appctx->st0 = STAT_CLI_PRINT;
2151 return 1;
2152 }
2153
2154 if (ssl_sock_update_ocsp_response(&trash, &err)) {
2155 if (err) {
2156 memprintf(&err, "%s.\n", err);
2157 appctx->ctx.cli.err = err;
2158 appctx->st0 = STAT_CLI_PRINT_FREE;
2159 }
2160 return 1;
2161 }
2162 appctx->ctx.cli.msg = "OCSP Response updated!";
2163 appctx->st0 = STAT_CLI_PRINT;
2164 return 1;
2165#else
2166 appctx->ctx.cli.msg = "HAProxy was compiled against a version of OpenSSL that doesn't support OCSP stapling.\n";
2167 appctx->st0 = STAT_CLI_PRINT;
2168 return 1;
2169#endif
2170 }
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02002171 else if (strcmp(args[2], "tls-key") == 0) {
2172#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
2173 /* Expect two parameters: the filename and the new new TLS key in encoding */
2174 if (!*args[3] || !*args[4]) {
2175 appctx->ctx.cli.msg = "'set ssl tls-key' expects a filename and the new TLS key in base64 encoding.\n";
2176 appctx->st0 = STAT_CLI_PRINT;
2177 return 1;
2178 }
2179
2180 appctx->ctx.tlskeys.ref = tlskeys_ref_lookup_ref(args[3]);
2181 if(!appctx->ctx.tlskeys.ref) {
2182 appctx->ctx.cli.msg = "'set ssl tls-key' unable to locate referenced filename\n";
2183 appctx->st0 = STAT_CLI_PRINT;
2184 return 1;
2185 }
2186
2187 trash.len = base64dec(args[4], strlen(args[4]), trash.str, trash.size);
2188 if (trash.len != sizeof(struct tls_sess_key)) {
2189 appctx->ctx.cli.msg = "'set ssl tls-key' received invalid base64 encoded TLS key.\n";
2190 appctx->st0 = STAT_CLI_PRINT;
2191 return 1;
2192 }
2193
Pradeep Jindalcc79b002015-08-20 18:25:17 +05302194 memcpy(appctx->ctx.tlskeys.ref->tlskeys + ((appctx->ctx.tlskeys.ref->tls_ticket_enc_index + 2) % TLS_TICKETS_NO), trash.str, trash.len);
2195 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 +02002196
2197 appctx->ctx.cli.msg = "TLS ticket key updated!";
2198 appctx->st0 = STAT_CLI_PRINT;
2199 return 1;
2200#else
2201 appctx->ctx.cli.msg = "HAProxy was compiled against a version of OpenSSL "
2202 "that doesn't support specifying TLS ticket keys\n";
2203 appctx->st0 = STAT_CLI_PRINT;
2204 return 1;
2205#endif
2206 }
Emeric Brun4147b2e2014-06-16 18:36:30 +02002207 else {
2208 appctx->ctx.cli.msg = "'set ssl' only supports 'ocsp-response'.\n";
2209 appctx->st0 = STAT_CLI_PRINT;
2210 return 1;
2211 }
2212 }
2213#endif
Willy Tarreau7aabd112010-01-26 10:59:06 +01002214 else { /* unknown "set" parameter */
Willy Tarreau4483d432009-10-10 19:30:08 +02002215 return 0;
2216 }
2217 }
Cyril Bontécd19e512010-01-31 22:34:03 +01002218 else if (strcmp(args[0], "enable") == 0) {
Simon Horman671b6f02013-11-25 10:46:39 +09002219 if (strcmp(args[1], "agent") == 0) {
2220 struct server *sv;
2221
2222 sv = expect_server_admin(s, si, args[2]);
2223 if (!sv)
2224 return 1;
2225
Willy Tarreau2e10f5a2013-12-11 20:11:55 +01002226 if (!(sv->agent.state & CHK_ST_CONFIGURED)) {
2227 appctx->ctx.cli.msg = "Agent was not configured on this server, cannot enable.\n";
2228 appctx->st0 = STAT_CLI_PRINT;
2229 return 1;
2230 }
2231
2232 sv->agent.state |= CHK_ST_ENABLED;
Simon Horman671b6f02013-11-25 10:46:39 +09002233 return 1;
2234 }
Willy Tarreau9b5aecd2014-05-23 11:53:10 +02002235 else if (strcmp(args[1], "health") == 0) {
Cyril Bontécd19e512010-01-31 22:34:03 +01002236 struct server *sv;
2237
Willy Tarreaud52c41e2011-09-07 23:41:01 +02002238 sv = expect_server_admin(s, si, args[2]);
2239 if (!sv)
Cyril Bonté613f0df2011-03-03 20:49:04 +01002240 return 1;
Cyril Bonté613f0df2011-03-03 20:49:04 +01002241
Willy Tarreau9b5aecd2014-05-23 11:53:10 +02002242 if (!(sv->check.state & CHK_ST_CONFIGURED)) {
2243 appctx->ctx.cli.msg = "Health checks are not configured on this server, cannot enable.\n";
2244 appctx->st0 = STAT_CLI_PRINT;
2245 return 1;
2246 }
2247
2248 sv->check.state |= CHK_ST_ENABLED;
2249 return 1;
2250 }
2251 else if (strcmp(args[1], "server") == 0) {
2252 struct server *sv;
2253
2254 sv = expect_server_admin(s, si, args[2]);
2255 if (!sv)
2256 return 1;
2257
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02002258 srv_adm_set_ready(sv);
Willy Tarreau532a4502011-09-07 22:37:44 +02002259 return 1;
2260 }
2261 else if (strcmp(args[1], "frontend") == 0) {
2262 struct proxy *px;
2263
2264 px = expect_frontend_admin(s, si, args[2]);
2265 if (!px)
2266 return 1;
2267
2268 if (px->state == PR_STSTOPPED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002269 appctx->ctx.cli.msg = "Frontend was previously shut down, cannot enable.\n";
2270 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002271 return 1;
2272 }
2273
2274 if (px->state != PR_STPAUSED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002275 appctx->ctx.cli.msg = "Frontend is already enabled.\n";
2276 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002277 return 1;
2278 }
2279
2280 if (!resume_proxy(px)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002281 appctx->ctx.cli.msg = "Failed to resume frontend, check logs for precise cause (port conflict?).\n";
2282 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002283 return 1;
2284 }
Cyril Bontécd19e512010-01-31 22:34:03 +01002285 return 1;
2286 }
2287 else { /* unknown "enable" parameter */
Willy Tarreau9b5aecd2014-05-23 11:53:10 +02002288 appctx->ctx.cli.msg = "'enable' only supports 'agent', 'frontend', 'health', and 'server'.\n";
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002289 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002290 return 1;
Cyril Bontécd19e512010-01-31 22:34:03 +01002291 }
2292 }
2293 else if (strcmp(args[0], "disable") == 0) {
Simon Horman671b6f02013-11-25 10:46:39 +09002294 if (strcmp(args[1], "agent") == 0) {
2295 struct server *sv;
2296
2297 sv = expect_server_admin(s, si, args[2]);
2298 if (!sv)
2299 return 1;
2300
Willy Tarreau2e10f5a2013-12-11 20:11:55 +01002301 sv->agent.state &= ~CHK_ST_ENABLED;
Simon Horman671b6f02013-11-25 10:46:39 +09002302 return 1;
2303 }
Willy Tarreau9b5aecd2014-05-23 11:53:10 +02002304 else if (strcmp(args[1], "health") == 0) {
2305 struct server *sv;
2306
2307 sv = expect_server_admin(s, si, args[2]);
2308 if (!sv)
2309 return 1;
2310
2311 sv->check.state &= ~CHK_ST_ENABLED;
2312 return 1;
2313 }
Simon Horman671b6f02013-11-25 10:46:39 +09002314 else if (strcmp(args[1], "server") == 0) {
Cyril Bontécd19e512010-01-31 22:34:03 +01002315 struct server *sv;
2316
Willy Tarreaud52c41e2011-09-07 23:41:01 +02002317 sv = expect_server_admin(s, si, args[2]);
2318 if (!sv)
Cyril Bonté613f0df2011-03-03 20:49:04 +01002319 return 1;
Cyril Bonté613f0df2011-03-03 20:49:04 +01002320
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02002321 srv_adm_set_maint(sv);
Willy Tarreau532a4502011-09-07 22:37:44 +02002322 return 1;
2323 }
2324 else if (strcmp(args[1], "frontend") == 0) {
2325 struct proxy *px;
2326
2327 px = expect_frontend_admin(s, si, args[2]);
2328 if (!px)
2329 return 1;
2330
2331 if (px->state == PR_STSTOPPED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002332 appctx->ctx.cli.msg = "Frontend was previously shut down, cannot disable.\n";
2333 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002334 return 1;
2335 }
2336
2337 if (px->state == PR_STPAUSED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002338 appctx->ctx.cli.msg = "Frontend is already disabled.\n";
2339 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002340 return 1;
2341 }
2342
2343 if (!pause_proxy(px)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002344 appctx->ctx.cli.msg = "Failed to pause frontend, check logs for precise cause.\n";
2345 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002346 return 1;
2347 }
Cyril Bontécd19e512010-01-31 22:34:03 +01002348 return 1;
2349 }
2350 else { /* unknown "disable" parameter */
Willy Tarreau9b5aecd2014-05-23 11:53:10 +02002351 appctx->ctx.cli.msg = "'disable' only supports 'agent', 'frontend', 'health', and 'server'.\n";
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002352 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002353 return 1;
2354 }
2355 }
2356 else if (strcmp(args[0], "shutdown") == 0) {
2357 if (strcmp(args[1], "frontend") == 0) {
2358 struct proxy *px;
2359
2360 px = expect_frontend_admin(s, si, args[2]);
2361 if (!px)
2362 return 1;
2363
2364 if (px->state == PR_STSTOPPED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002365 appctx->ctx.cli.msg = "Frontend was already shut down.\n";
2366 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002367 return 1;
2368 }
2369
2370 Warning("Proxy %s stopped (FE: %lld conns, BE: %lld conns).\n",
2371 px->id, px->fe_counters.cum_conn, px->be_counters.cum_conn);
2372 send_log(px, LOG_WARNING, "Proxy %s stopped (FE: %lld conns, BE: %lld conns).\n",
2373 px->id, px->fe_counters.cum_conn, px->be_counters.cum_conn);
2374 stop_proxy(px);
2375 return 1;
2376 }
Willy Tarreaua295edc2011-09-07 23:21:03 +02002377 else if (strcmp(args[1], "session") == 0) {
Willy Tarreau87b09662015-04-03 00:22:06 +02002378 struct stream *sess, *ptr;
Willy Tarreaua295edc2011-09-07 23:21:03 +02002379
Willy Tarreaud0d8da92015-04-04 02:10:38 +02002380 if (strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002381 appctx->ctx.cli.msg = stats_permission_denied_msg;
2382 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaua295edc2011-09-07 23:21:03 +02002383 return 1;
2384 }
2385
2386 if (!*args[2]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002387 appctx->ctx.cli.msg = "Session pointer expected (use 'show sess').\n";
2388 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaua295edc2011-09-07 23:21:03 +02002389 return 1;
2390 }
2391
2392 ptr = (void *)strtoul(args[2], NULL, 0);
2393
Willy Tarreau87b09662015-04-03 00:22:06 +02002394 /* first, look for the requested stream in the stream table */
2395 list_for_each_entry(sess, &streams, list) {
Willy Tarreaua295edc2011-09-07 23:21:03 +02002396 if (sess == ptr)
2397 break;
2398 }
2399
Willy Tarreau87b09662015-04-03 00:22:06 +02002400 /* do we have the stream ? */
Willy Tarreaua295edc2011-09-07 23:21:03 +02002401 if (sess != ptr) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002402 appctx->ctx.cli.msg = "No such session (use 'show sess').\n";
2403 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaua295edc2011-09-07 23:21:03 +02002404 return 1;
2405 }
2406
Willy Tarreaue7dff022015-04-03 01:14:29 +02002407 stream_shutdown(sess, SF_ERR_KILLED);
Willy Tarreaua295edc2011-09-07 23:21:03 +02002408 return 1;
2409 }
Willy Tarreau52b2d222011-09-07 23:48:48 +02002410 else if (strcmp(args[1], "sessions") == 0) {
2411 if (strcmp(args[2], "server") == 0) {
2412 struct server *sv;
Willy Tarreau87b09662015-04-03 00:22:06 +02002413 struct stream *sess, *sess_bck;
Willy Tarreau52b2d222011-09-07 23:48:48 +02002414
2415 sv = expect_server_admin(s, si, args[3]);
2416 if (!sv)
2417 return 1;
2418
Willy Tarreau87b09662015-04-03 00:22:06 +02002419 /* kill all the stream that are on this server */
Willy Tarreau52b2d222011-09-07 23:48:48 +02002420 list_for_each_entry_safe(sess, sess_bck, &sv->actconns, by_srv)
2421 if (sess->srv_conn == sv)
Willy Tarreaue7dff022015-04-03 01:14:29 +02002422 stream_shutdown(sess, SF_ERR_KILLED);
Willy Tarreau52b2d222011-09-07 23:48:48 +02002423
2424 return 1;
2425 }
2426 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002427 appctx->ctx.cli.msg = "'shutdown sessions' only supports 'server'.\n";
2428 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau52b2d222011-09-07 23:48:48 +02002429 return 1;
2430 }
2431 }
Willy Tarreau532a4502011-09-07 22:37:44 +02002432 else { /* unknown "disable" parameter */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002433 appctx->ctx.cli.msg = "'shutdown' only supports 'frontend', 'session' and 'sessions'.\n";
2434 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002435 return 1;
Cyril Bontécd19e512010-01-31 22:34:03 +01002436 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002437 }
2438 else if (strcmp(args[0], "del") == 0) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002439 if (strcmp(args[1], "map") == 0 || strcmp(args[1], "acl") == 0) {
2440 if (args[1][0] == 'm')
2441 appctx->ctx.map.display_flags = PAT_REF_MAP;
2442 else
2443 appctx->ctx.map.display_flags = PAT_REF_ACL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002444
2445 /* Expect two parameters: map name and key. */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002446 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
2447 if (!*args[2] || !*args[3]) {
2448 appctx->ctx.cli.msg = "This command expects two parameters: map identifier and key.\n";
2449 appctx->st0 = STAT_CLI_PRINT;
2450 return 1;
2451 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002452 }
2453
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002454 else {
2455 if (!*args[2] || !*args[3]) {
2456 appctx->ctx.cli.msg = "This command expects two parameters: ACL identifier and key.\n";
2457 appctx->st0 = STAT_CLI_PRINT;
2458 return 1;
2459 }
2460 }
2461
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002462 /* Lookup the reference in the maps. */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002463 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002464 if (!appctx->ctx.map.ref ||
2465 !(appctx->ctx.map.ref->flags & appctx->ctx.map.display_flags)) {
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01002466 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002467 appctx->st0 = STAT_CLI_PRINT;
2468 return 1;
2469 }
2470
Thierry FOURNIER9356c682014-01-28 15:55:37 +01002471 /* If the entry identifier start with a '#', it is considered as
2472 * pointer id
2473 */
2474 if (args[3][0] == '#' && args[3][1] == '0' && args[3][2] == 'x') {
2475 struct pat_ref_elt *ref;
2476 long long int conv;
2477 char *error;
2478
2479 /* Convert argument to integer value. */
2480 conv = strtoll(&args[3][1], &error, 16);
2481 if (*error != '\0') {
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01002482 appctx->ctx.cli.msg = "Malformed identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER9356c682014-01-28 15:55:37 +01002483 appctx->st0 = STAT_CLI_PRINT;
2484 return 1;
2485 }
2486
2487 /* Convert and check integer to pointer. */
2488 ref = (struct pat_ref_elt *)(long)conv;
2489 if ((long long int)(long)ref != conv) {
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01002490 appctx->ctx.cli.msg = "Malformed identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER9356c682014-01-28 15:55:37 +01002491 appctx->st0 = STAT_CLI_PRINT;
2492 return 1;
2493 }
2494
2495 /* Try to delete the entry. */
2496 if (!pat_ref_delete_by_id(appctx->ctx.map.ref, ref)) {
2497 /* The entry is not found, send message. */
2498 appctx->ctx.cli.msg = "Key not found.\n";
2499 appctx->st0 = STAT_CLI_PRINT;
2500 return 1;
2501 }
2502 }
2503 else {
2504 /* Else, use the entry identifier as pattern
2505 * string and try to delete the entry.
2506 */
2507 if (!pat_ref_delete(appctx->ctx.map.ref, args[3])) {
2508 /* The entry is not found, send message. */
2509 appctx->ctx.cli.msg = "Key not found.\n";
2510 appctx->st0 = STAT_CLI_PRINT;
2511 return 1;
2512 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002513 }
2514
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002515 /* The deletion is done, send message. */
Thierry FOURNIER07e78c52014-12-18 15:28:01 +01002516 appctx->st0 = STAT_CLI_PROMPT;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002517 return 1;
2518 }
2519 else { /* unknown "del" parameter */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002520 appctx->ctx.cli.msg = "'del' only supports 'map' or 'acl'.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002521 appctx->st0 = STAT_CLI_PRINT;
2522 return 1;
2523 }
2524 }
2525 else if (strcmp(args[0], "add") == 0) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002526 if (strcmp(args[1], "map") == 0 ||
2527 strcmp(args[1], "acl") == 0) {
2528 int ret;
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002529 char *err;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002530
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002531 /* Set flags. */
2532 if (args[1][0] == 'm')
2533 appctx->ctx.map.display_flags = PAT_REF_MAP;
2534 else
2535 appctx->ctx.map.display_flags = PAT_REF_ACL;
2536
2537 /* If the keywork is "map", we expect three parameters, if it
2538 * is "acl", we expect only two parameters
2539 */
2540 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
2541 if (!*args[2] || !*args[3] || !*args[4]) {
2542 appctx->ctx.cli.msg = "'add map' expects three parameters: map identifier, key and value.\n";
2543 appctx->st0 = STAT_CLI_PRINT;
2544 return 1;
2545 }
2546 }
2547 else {
2548 if (!*args[2] || !*args[3]) {
2549 appctx->ctx.cli.msg = "'add acl' expects two parameters: ACL identifier and pattern.\n";
2550 appctx->st0 = STAT_CLI_PRINT;
2551 return 1;
2552 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002553 }
2554
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002555 /* Lookup for the reference. */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002556 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002557 if (!appctx->ctx.map.ref) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002558 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01002559 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002560 else
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01002561 appctx->ctx.cli.msg = "Unknown ACL identifier. Please use #<id> or <file>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002562 appctx->st0 = STAT_CLI_PRINT;
2563 return 1;
2564 }
2565
Thierry FOURNIER64c585f2014-01-29 20:02:36 +01002566 /* The command "add acl" is prohibited if the reference
2567 * use samples.
2568 */
2569 if ((appctx->ctx.map.display_flags & PAT_REF_ACL) &&
2570 (appctx->ctx.map.ref->flags & PAT_REF_SMP)) {
2571 appctx->ctx.cli.msg = "This ACL is shared with a map containing samples. "
2572 "You must use the command 'add map' to add values.\n";
2573 appctx->st0 = STAT_CLI_PRINT;
2574 return 1;
2575 }
2576
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002577 /* Add value. */
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002578 err = NULL;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002579 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02002580 ret = pat_ref_add(appctx->ctx.map.ref, args[3], args[4], &err);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002581 else
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02002582 ret = pat_ref_add(appctx->ctx.map.ref, args[3], NULL, &err);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002583 if (!ret) {
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002584 if (err)
2585 memprintf(&err, "%s.\n", err);
2586 appctx->ctx.cli.err = err;
2587 appctx->st0 = STAT_CLI_PRINT_FREE;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002588 return 1;
2589 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002590
2591 /* The add is done, send message. */
Thierry FOURNIER07e78c52014-12-18 15:28:01 +01002592 appctx->st0 = STAT_CLI_PROMPT;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002593 return 1;
2594 }
2595 else { /* unknown "del" parameter */
2596 appctx->ctx.cli.msg = "'add' only supports 'map'.\n";
2597 appctx->st0 = STAT_CLI_PRINT;
2598 return 1;
2599 }
Cyril Bontécd19e512010-01-31 22:34:03 +01002600 }
2601 else { /* not "show" nor "clear" nor "get" nor "set" nor "enable" nor "disable" */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002602 return 0;
2603 }
2604 return 1;
2605}
2606
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002607/* This I/O handler runs as an applet embedded in a stream interface. It is
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002608 * used to processes I/O from/to the stats unix socket. The system relies on a
2609 * state machine handling requests and various responses. We read a request,
2610 * then we process it and send the response, and we possibly display a prompt.
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002611 * Then we can read again. The state is stored in appctx->st0 and is one of the
2612 * STAT_CLI_* constants. appctx->st1 is used to indicate whether prompt is enabled
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002613 * or not.
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002614 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02002615static void cli_io_handler(struct appctx *appctx)
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002616{
Willy Tarreau00a37f02015-04-13 12:05:19 +02002617 struct stream_interface *si = appctx->owner;
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002618 struct channel *req = si_oc(si);
2619 struct channel *res = si_ic(si);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002620 int reql;
2621 int len;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002622
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002623 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
2624 goto out;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002625
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002626 while (1) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002627 if (appctx->st0 == STAT_CLI_INIT) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002628 /* Stats output not initialized yet */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002629 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
2630 appctx->st0 = STAT_CLI_GETREQ;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002631 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002632 else if (appctx->st0 == STAT_CLI_END) {
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002633 /* Let's close for real now. We just close the request
2634 * side, the conditions below will complete if needed.
2635 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02002636 si_shutw(si);
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002637 break;
2638 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002639 else if (appctx->st0 == STAT_CLI_GETREQ) {
Willy Tarreau4e33d862009-10-11 23:35:10 +02002640 /* ensure we have some output room left in the event we
2641 * would want to return some info right after parsing.
2642 */
Willy Tarreau4e4292b2014-11-28 12:18:45 +01002643 if (buffer_almost_full(si_ib(si))) {
Willy Tarreaufe127932015-04-21 19:23:39 +02002644 si_applet_cant_put(si);
Willy Tarreau4e33d862009-10-11 23:35:10 +02002645 break;
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01002646 }
Willy Tarreau4e33d862009-10-11 23:35:10 +02002647
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002648 reql = bo_getline(si_oc(si), trash.str, trash.size);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002649 if (reql <= 0) { /* closed or EOL not found */
2650 if (reql == 0)
2651 break;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002652 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002653 continue;
2654 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002655
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002656 /* seek for a possible semi-colon. If we find one, we
2657 * replace it with an LF and skip only this part.
2658 */
2659 for (len = 0; len < reql; len++)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002660 if (trash.str[len] == ';') {
2661 trash.str[len] = '\n';
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002662 reql = len + 1;
2663 break;
2664 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002665
Willy Tarreau816fc222009-10-04 07:36:58 +02002666 /* now it is time to check that we have a full line,
2667 * remove the trailing \n and possibly \r, then cut the
2668 * line.
2669 */
2670 len = reql - 1;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002671 if (trash.str[len] != '\n') {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002672 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002673 continue;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002674 }
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002675
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002676 if (len && trash.str[len-1] == '\r')
Willy Tarreau816fc222009-10-04 07:36:58 +02002677 len--;
2678
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002679 trash.str[len] = '\0';
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002680
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002681 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002682 if (len) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002683 if (strcmp(trash.str, "quit") == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002684 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002685 continue;
2686 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002687 else if (strcmp(trash.str, "prompt") == 0)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002688 appctx->st1 = !appctx->st1;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002689 else if (strcmp(trash.str, "help") == 0 ||
2690 !stats_sock_parse_request(si, trash.str)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002691 appctx->ctx.cli.msg = stats_sock_usage_msg;
2692 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02002693 }
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002694 /* NB: stats_sock_parse_request() may have put
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002695 * another STAT_CLI_O_* into appctx->st0.
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002696 */
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002697 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002698 else if (!appctx->st1) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002699 /* if prompt is disabled, print help on empty lines,
2700 * so that the user at least knows how to enable
2701 * prompt and find help.
2702 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002703 appctx->ctx.cli.msg = stats_sock_usage_msg;
2704 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002705 }
2706
2707 /* re-adjust req buffer */
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002708 bo_skip(si_oc(si), reql);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002709 req->flags |= CF_READ_DONTWAIT; /* we plan to read small requests */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002710 }
Willy Tarreau68c00c72015-09-25 19:21:19 +02002711 else { /* output functions */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002712 switch (appctx->st0) {
Andrew Hayworthe32d1862015-10-02 15:08:10 +00002713 case STAT_CLI_PROMPT:
2714 break;
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02002715 case STAT_CLI_PRINT:
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002716 if (bi_putstr(si_ic(si), appctx->ctx.cli.msg) != -1)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002717 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreaubc18da12015-03-13 14:00:47 +01002718 else
Willy Tarreaufe127932015-04-21 19:23:39 +02002719 si_applet_cant_put(si);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002720 break;
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002721 case STAT_CLI_PRINT_FREE:
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002722 if (bi_putstr(si_ic(si), appctx->ctx.cli.err) != -1) {
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002723 free(appctx->ctx.cli.err);
2724 appctx->st0 = STAT_CLI_PROMPT;
2725 }
Willy Tarreaubc18da12015-03-13 14:00:47 +01002726 else
Willy Tarreaufe127932015-04-21 19:23:39 +02002727 si_applet_cant_put(si);
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002728 break;
Baptiste Assmann9b6857e2015-09-18 14:49:12 +02002729 case STAT_CLI_O_BACKEND:
2730 if (stats_dump_backend_to_buffer(si))
2731 appctx->st0 = STAT_CLI_PROMPT;
2732 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002733 case STAT_CLI_O_INFO:
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002734 if (stats_dump_info_to_buffer(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002735 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002736 break;
Baptiste Assmann28289462015-07-03 08:01:20 +02002737 case STAT_CLI_O_SERVERS_STATE:
2738 if (stats_dump_servers_state_to_buffer(si))
2739 appctx->st0 = STAT_CLI_PROMPT;
2740 break;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002741 case STAT_CLI_O_STAT:
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002742 if (stats_dump_stat_to_buffer(si, NULL))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002743 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002744 break;
Baptiste Assmann3863f972015-05-17 00:33:24 +02002745 case STAT_CLI_O_RESOLVERS:
2746 if (stats_dump_resolvers_to_buffer(si))
2747 appctx->st0 = STAT_CLI_PROMPT;
2748 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002749 case STAT_CLI_O_SESS:
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01002750 if (stats_dump_sess_to_buffer(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002751 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002752 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002753 case STAT_CLI_O_ERR: /* errors dump */
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01002754 if (stats_dump_errors_to_buffer(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002755 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002756 break;
Willy Tarreau69f58c82010-07-12 17:55:33 +02002757 case STAT_CLI_O_TAB:
Simon Hormanc88b8872011-06-15 15:18:49 +09002758 case STAT_CLI_O_CLR:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002759 if (stats_table_request(si, appctx->st0))
2760 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau69f58c82010-07-12 17:55:33 +02002761 break;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002762 case STAT_CLI_O_PATS:
2763 if (stats_pats_list(si))
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002764 appctx->st0 = STAT_CLI_PROMPT;
2765 break;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002766 case STAT_CLI_O_PAT:
2767 if (stats_pat_list(si))
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002768 appctx->st0 = STAT_CLI_PROMPT;
2769 break;
2770 case STAT_CLI_O_MLOOK:
2771 if (stats_map_lookup(si))
2772 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau4efb3532014-01-29 12:13:39 +01002773 break;
Willy Tarreau12833bb2014-01-28 16:49:56 +01002774 case STAT_CLI_O_POOLS:
2775 if (stats_dump_pools_to_buffer(si))
2776 appctx->st0 = STAT_CLI_PROMPT;
2777 break;
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02002778#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
2779 case STAT_CLI_O_TLSK:
2780 if (stats_tlskeys_list(si))
2781 appctx->st0 = STAT_CLI_PROMPT;
2782 break;
William Lallemand1d0b36a2016-05-20 17:40:26 +02002783 case STAT_CLI_O_TLSK_ENT:
2784 if (stats_tlskeys_list(si))
2785 appctx->st0 = STAT_CLI_PROMPT;
2786 break;
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02002787#endif
Willy Tarreauae795722016-02-16 11:27:28 +01002788 case STAT_CLI_O_ENV: /* environment dump */
2789 if (stats_dump_env_to_buffer(si))
2790 appctx->st0 = STAT_CLI_PROMPT;
2791 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002792 default: /* abnormal state */
Willy Tarreau5cfa3bc2015-09-25 20:08:51 +02002793 si->flags |= SI_FL_ERR;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002794 break;
2795 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002796
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002797 /* The post-command prompt is either LF alone or LF + '> ' in interactive mode */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002798 if (appctx->st0 == STAT_CLI_PROMPT) {
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002799 if (bi_putstr(si_ic(si), appctx->st1 ? "\n> " : "\n") != -1)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002800 appctx->st0 = STAT_CLI_GETREQ;
Willy Tarreaubc18da12015-03-13 14:00:47 +01002801 else
Willy Tarreaufe127932015-04-21 19:23:39 +02002802 si_applet_cant_put(si);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002803 }
2804
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002805 /* If the output functions are still there, it means they require more room. */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002806 if (appctx->st0 >= STAT_CLI_OUTPUT)
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002807 break;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002808
2809 /* Now we close the output if one of the writers did so,
2810 * or if we're not in interactive mode and the request
2811 * buffer is empty. This still allows pipelined requests
2812 * to be sent in non-interactive mode.
2813 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002814 if ((res->flags & (CF_SHUTW|CF_SHUTW_NOW)) || (!appctx->st1 && !req->buf->o)) {
2815 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002816 continue;
2817 }
2818
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002819 /* switch state back to GETREQ to read next requests */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002820 appctx->st0 = STAT_CLI_GETREQ;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002821 }
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002822 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002823
Willy Tarreauc9e930a2015-09-25 20:06:08 +02002824 if ((res->flags & CF_SHUTR) && (si->state == SI_ST_EST)) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002825 DPRINTF(stderr, "%s@%d: si to buf closed. req=%08x, res=%08x, st=%d\n",
2826 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
Aman Guptaceafb4a2012-04-02 18:57:54 -07002827 /* Other side has closed, let's abort if we have no more processing to do
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002828 * and nothing more to consume. This is comparable to a broken pipe, so
2829 * we forward the close to the request side so that it flows upstream to
2830 * the client.
2831 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02002832 si_shutw(si);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002833 }
2834
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002835 if ((req->flags & CF_SHUTW) && (si->state == SI_ST_EST) && (appctx->st0 < STAT_CLI_OUTPUT)) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002836 DPRINTF(stderr, "%s@%d: buf to si closed. req=%08x, res=%08x, st=%d\n",
2837 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
2838 /* We have no more processing to do, and nothing more to send, and
2839 * the client side has closed. So we'll forward this state downstream
2840 * on the response buffer.
2841 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02002842 si_shutr(si);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002843 res->flags |= CF_READ_NULL;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002844 }
2845
Willy Tarreau828824a2015-04-19 17:20:03 +02002846 out:
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01002847 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 +02002848 __FUNCTION__, __LINE__,
Willy Tarreau9b28e032012-10-12 23:49:43 +02002849 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 +02002850}
2851
Willy Tarreau638d40a2016-02-24 23:11:01 +01002852/* Emits a stats field without any surrounding element and properly encoded to
2853 * resist CSV output. Returns non-zero on success, 0 if the buffer is full.
2854 */
Willy Tarreaub47785f2016-02-24 23:28:31 +01002855int stats_emit_raw_data_field(struct chunk *out, const struct field *f)
Willy Tarreau638d40a2016-02-24 23:11:01 +01002856{
2857 switch (field_format(f, 0)) {
2858 case FF_EMPTY: return 1;
2859 case FF_S32: return chunk_appendf(out, "%d", f->u.s32);
2860 case FF_U32: return chunk_appendf(out, "%u", f->u.u32);
2861 case FF_S64: return chunk_appendf(out, "%lld", (long long)f->u.s64);
2862 case FF_U64: return chunk_appendf(out, "%llu", (unsigned long long)f->u.u64);
2863 case FF_STR: return csv_enc_append(field_str(f, 0), 1, out) != NULL;
2864 default: return chunk_appendf(out, "[INCORRECT_FIELD_TYPE_%08x]", f->type);
2865 }
2866}
2867
Willy Tarreaub47785f2016-02-24 23:28:31 +01002868/* Emits a stats field prefixed with its type. No CSV encoding is prepared, the
2869 * output is supposed to be used on its own line. Returns non-zero on success, 0
2870 * if the buffer is full.
2871 */
2872int stats_emit_typed_data_field(struct chunk *out, const struct field *f)
2873{
2874 switch (field_format(f, 0)) {
2875 case FF_EMPTY: return 1;
2876 case FF_S32: return chunk_appendf(out, "s32:%d", f->u.s32);
2877 case FF_U32: return chunk_appendf(out, "u32:%u", f->u.u32);
2878 case FF_S64: return chunk_appendf(out, "s64:%lld", (long long)f->u.s64);
2879 case FF_U64: return chunk_appendf(out, "u64:%llu", (unsigned long long)f->u.u64);
2880 case FF_STR: return chunk_appendf(out, "str:%s", field_str(f, 0));
2881 default: return chunk_appendf(out, "%08x:?", f->type);
2882 }
2883}
2884
2885/* Emits an encoding of the field type on 3 characters followed by a delimiter.
2886 * Returns non-zero on success, 0 if the buffer is full.
2887 */
2888int stats_emit_field_tags(struct chunk *out, const struct field *f, char delim)
2889{
2890 char origin, nature, scope;
2891
2892 switch (field_origin(f, 0)) {
2893 case FO_METRIC: origin = 'M'; break;
2894 case FO_STATUS: origin = 'S'; break;
2895 case FO_KEY: origin = 'K'; break;
2896 case FO_CONFIG: origin = 'C'; break;
2897 case FO_PRODUCT: origin = 'P'; break;
2898 default: origin = '?'; break;
2899 }
2900
2901 switch (field_nature(f, 0)) {
2902 case FN_GAUGE: nature = 'G'; break;
2903 case FN_LIMIT: nature = 'L'; break;
2904 case FN_MIN: nature = 'm'; break;
2905 case FN_MAX: nature = 'M'; break;
2906 case FN_RATE: nature = 'R'; break;
2907 case FN_COUNTER: nature = 'C'; break;
2908 case FN_DURATION: nature = 'D'; break;
2909 case FN_AGE: nature = 'A'; break;
2910 case FN_TIME: nature = 'T'; break;
2911 case FN_NAME: nature = 'N'; break;
2912 case FN_OUTPUT: nature = 'O'; break;
2913 case FN_AVG: nature = 'a'; break;
2914 default: nature = '?'; break;
2915 }
2916
2917 switch (field_scope(f, 0)) {
2918 case FS_PROCESS: scope = 'P'; break;
2919 case FS_SERVICE: scope = 'S'; break;
2920 case FS_SYSTEM: scope = 's'; break;
2921 case FS_CLUSTER: scope = 'C'; break;
2922 default: scope = '?'; break;
2923 }
2924
2925 return chunk_appendf(out, "%c%c%c%c", origin, nature, scope, delim);
2926}
2927
Willy Tarreaubf95cba2016-01-11 18:27:29 +01002928/* Dump all fields from <info> into <out> using the "show info" format (name: value) */
2929static int stats_dump_info_fields(struct chunk *out, const struct field *info)
2930{
2931 int field;
2932
2933 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
2934 if (!field_format(info, field))
2935 continue;
2936
2937 if (!chunk_appendf(out, "%s: ", info_field_names[field]))
2938 return 0;
2939 if (!stats_emit_raw_data_field(out, &info[field]))
2940 return 0;
2941 if (!chunk_strcat(out, "\n"))
2942 return 0;
2943 }
2944 return 1;
2945}
2946
Willy Tarreaucb809122016-01-11 20:08:42 +01002947/* Dump all fields from <info> into <out> using the "show info typed" format */
2948static int stats_dump_typed_info_fields(struct chunk *out, const struct field *info)
2949{
2950 int field;
2951
2952 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
2953 if (!field_format(info, field))
2954 continue;
2955
2956 if (!chunk_appendf(out, "%d.%s.%u:", field, info_field_names[field], info[INF_PROCESS_NUM].u.u32))
2957 return 0;
2958 if (!stats_emit_field_tags(out, &info[field], ':'))
2959 return 0;
2960 if (!stats_emit_typed_data_field(out, &info[field]))
2961 return 0;
2962 if (!chunk_strcat(out, "\n"))
2963 return 0;
2964 }
2965 return 1;
2966}
2967
Thierry Fourniercb2c7672016-03-25 08:19:23 +01002968/* Fill <info> with HAProxy global info. <info> is preallocated
2969 * array of length <len>. The length of the aray must be
2970 * INF_TOTAL_FIELDS. If this length is less then this value, the
2971 * function returns 0, otherwise, it returns 1.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002972 */
Thierry Fourniercb2c7672016-03-25 08:19:23 +01002973int stats_fill_info(struct field *info, int len)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002974{
2975 unsigned int up = (now.tv_sec - start_date.tv_sec);
Willy Tarreau4529c072016-01-11 18:17:07 +01002976 struct chunk *out = get_trash_chunk();
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002977
Willy Tarreau0c9c2722014-05-28 12:28:58 +02002978#ifdef USE_OPENSSL
2979 int ssl_sess_rate = read_freq_ctr(&global.ssl_per_sec);
2980 int ssl_key_rate = read_freq_ctr(&global.ssl_fe_keys_per_sec);
2981 int ssl_reuse = 0;
2982
2983 if (ssl_key_rate < ssl_sess_rate) {
2984 /* count the ssl reuse ratio and avoid overflows in both directions */
2985 ssl_reuse = 100 - (100 * ssl_key_rate + (ssl_sess_rate - 1) / 2) / ssl_sess_rate;
2986 }
2987#endif
2988
Thierry Fourniercb2c7672016-03-25 08:19:23 +01002989 if (len < INF_TOTAL_FIELDS)
2990 return 0;
2991
Willy Tarreau4529c072016-01-11 18:17:07 +01002992 chunk_reset(out);
Thierry Fourniercb2c7672016-03-25 08:19:23 +01002993 memset(info, 0, sizeof(*info) * len);
Willy Tarreau4529c072016-01-11 18:17:07 +01002994
2995 info[INF_NAME] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, PRODUCT_NAME);
2996 info[INF_VERSION] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, HAPROXY_VERSION);
2997 info[INF_RELEASE_DATE] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, HAPROXY_DATE);
2998
2999 info[INF_NBPROC] = mkf_u32(FO_CONFIG|FS_SERVICE, global.nbproc);
3000 info[INF_PROCESS_NUM] = mkf_u32(FO_KEY, relative_pid);
3001 info[INF_PID] = mkf_u32(FO_STATUS, pid);
3002
3003 info[INF_UPTIME] = mkf_str(FN_DURATION, chunk_newstr(out));
3004 chunk_appendf(out, "%ud %uh%02um%02us", up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60));
3005
3006 info[INF_UPTIME_SEC] = mkf_u32(FN_DURATION, up);
3007 info[INF_MEMMAX_MB] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_memmax);
3008 info[INF_POOL_ALLOC_MB] = mkf_u32(0, (unsigned)(pool_total_allocated() / 1048576L));
3009 info[INF_POOL_USED_MB] = mkf_u32(0, (unsigned)(pool_total_used() / 1048576L));
3010 info[INF_POOL_FAILED] = mkf_u32(FN_COUNTER, pool_total_failures());
3011 info[INF_ULIMIT_N] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_nofile);
3012 info[INF_MAXSOCK] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxsock);
3013 info[INF_MAXCONN] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxconn);
3014 info[INF_HARD_MAXCONN] = mkf_u32(FO_CONFIG|FN_LIMIT, global.hardmaxconn);
3015 info[INF_CURR_CONN] = mkf_u32(0, actconn);
3016 info[INF_CUM_CONN] = mkf_u32(FN_COUNTER, totalconn);
3017 info[INF_CUM_REQ] = mkf_u32(FN_COUNTER, global.req_count);
3018#ifdef USE_OPENSSL
3019 info[INF_MAX_SSL_CONNS] = mkf_u32(FN_MAX, global.maxsslconn);
3020 info[INF_CURR_SSL_CONNS] = mkf_u32(0, sslconns);
3021 info[INF_CUM_SSL_CONNS] = mkf_u32(FN_COUNTER, totalsslconns);
3022#endif
3023 info[INF_MAXPIPES] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxpipes);
3024 info[INF_PIPES_USED] = mkf_u32(0, pipes_used);
3025 info[INF_PIPES_FREE] = mkf_u32(0, pipes_free);
3026 info[INF_CONN_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.conn_per_sec));
3027 info[INF_CONN_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.cps_lim);
3028 info[INF_MAX_CONN_RATE] = mkf_u32(FN_MAX, global.cps_max);
3029 info[INF_SESS_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.sess_per_sec));
3030 info[INF_SESS_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.sps_lim);
3031 info[INF_MAX_SESS_RATE] = mkf_u32(FN_RATE, global.sps_max);
3032
3033#ifdef USE_OPENSSL
3034 info[INF_SSL_RATE] = mkf_u32(FN_RATE, ssl_sess_rate);
3035 info[INF_SSL_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.ssl_lim);
3036 info[INF_MAX_SSL_RATE] = mkf_u32(FN_MAX, global.ssl_max);
3037 info[INF_SSL_FRONTEND_KEY_RATE] = mkf_u32(0, ssl_key_rate);
3038 info[INF_SSL_FRONTEND_MAX_KEY_RATE] = mkf_u32(FN_MAX, global.ssl_fe_keys_max);
3039 info[INF_SSL_FRONTEND_SESSION_REUSE_PCT] = mkf_u32(0, ssl_reuse);
3040 info[INF_SSL_BACKEND_KEY_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.ssl_be_keys_per_sec));
3041 info[INF_SSL_BACKEND_MAX_KEY_RATE] = mkf_u32(FN_MAX, global.ssl_be_keys_max);
3042 info[INF_SSL_CACHE_LOOKUPS] = mkf_u32(FN_COUNTER, global.shctx_lookups);
3043 info[INF_SSL_CACHE_MISSES] = mkf_u32(FN_COUNTER, global.shctx_misses);
3044#endif
3045 info[INF_COMPRESS_BPS_IN] = mkf_u32(FN_RATE, read_freq_ctr(&global.comp_bps_in));
3046 info[INF_COMPRESS_BPS_OUT] = mkf_u32(FN_RATE, read_freq_ctr(&global.comp_bps_out));
3047 info[INF_COMPRESS_BPS_RATE_LIM] = mkf_u32(FO_CONFIG|FN_LIMIT, global.comp_rate_lim);
3048#ifdef USE_ZLIB
3049 info[INF_ZLIB_MEM_USAGE] = mkf_u32(0, zlib_used_memory);
3050 info[INF_MAX_ZLIB_MEM_USAGE] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxzlibmem);
3051#endif
3052 info[INF_TASKS] = mkf_u32(0, nb_tasks_cur);
3053 info[INF_RUN_QUEUE] = mkf_u32(0, run_queue_cur);
3054 info[INF_IDLE_PCT] = mkf_u32(FN_AVG, idle_pct);
3055 info[INF_NODE] = mkf_str(FO_CONFIG|FN_OUTPUT|FS_SERVICE, global.node);
3056 if (global.desc)
3057 info[INF_DESCRIPTION] = mkf_str(FO_CONFIG|FN_OUTPUT|FS_SERVICE, global.desc);
3058
Thierry Fourniercb2c7672016-03-25 08:19:23 +01003059 return 1;
3060}
3061
3062/* This function dumps information onto the stream interface's read buffer.
3063 * It returns 0 as long as it does not complete, non-zero upon completion.
3064 * No state is used.
3065 */
3066static int stats_dump_info_to_buffer(struct stream_interface *si)
3067{
3068 struct appctx *appctx = __objt_appctx(si->end);
3069
3070 if (!stats_fill_info(info, INF_TOTAL_FIELDS))
3071 return 0;
3072
Willy Tarreau1b4ba1e2016-01-11 18:29:04 +01003073 chunk_reset(&trash);
Willy Tarreaucb809122016-01-11 20:08:42 +01003074
3075 if (appctx->ctx.stats.flags & STAT_FMT_TYPED)
3076 stats_dump_typed_info_fields(&trash, info);
3077 else
3078 stats_dump_info_fields(&trash, info);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003079
Willy Tarreaubc18da12015-03-13 14:00:47 +01003080 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02003081 si_applet_cant_put(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003082 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01003083 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003084
3085 return 1;
3086}
3087
Baptiste Assmann28289462015-07-03 08:01:20 +02003088/* dumps server state information into <buf> for all the servers found in <backend>
3089 * These information are all the parameters which may change during HAProxy runtime.
3090 * By default, we only export to the last known server state file format.
3091 * These information can be used at next startup to recover same level of server state.
3092 */
Cyril Bonté76a99782016-05-06 12:18:48 +02003093static int dump_servers_state(struct stream_interface *si, struct chunk *buf)
Baptiste Assmann28289462015-07-03 08:01:20 +02003094{
Cyril Bonté76a99782016-05-06 12:18:48 +02003095 struct appctx *appctx = __objt_appctx(si->end);
Baptiste Assmann28289462015-07-03 08:01:20 +02003096 struct server *srv;
3097 char srv_addr[INET6_ADDRSTRLEN + 1];
3098 time_t srv_time_since_last_change;
3099 int bk_f_forced_id, srv_f_forced_id;
3100
Baptiste Assmann28289462015-07-03 08:01:20 +02003101
Cyril Bonté76a99782016-05-06 12:18:48 +02003102 /* we don't want to report any state if the backend is not enabled on this process */
3103 if (appctx->ctx.server_state.px->bind_proc && !(appctx->ctx.server_state.px->bind_proc & (1UL << (relative_pid - 1))))
3104 return 1;
Baptiste Assmann28289462015-07-03 08:01:20 +02003105
Cyril Bontéd55bd7a2016-05-27 00:06:45 +02003106 if (!appctx->ctx.server_state.sv)
3107 appctx->ctx.server_state.sv = appctx->ctx.server_state.px->srv;
3108
Cyril Bonté76a99782016-05-06 12:18:48 +02003109 for (; appctx->ctx.server_state.sv != NULL; appctx->ctx.server_state.sv = srv->next) {
3110 srv = appctx->ctx.server_state.sv;
Baptiste Assmann28289462015-07-03 08:01:20 +02003111 srv_addr[0] = '\0';
3112 srv_time_since_last_change = 0;
3113 bk_f_forced_id = 0;
3114 srv_f_forced_id = 0;
3115
3116 switch (srv->addr.ss_family) {
3117 case AF_INET:
3118 inet_ntop(srv->addr.ss_family, &((struct sockaddr_in *)&srv->addr)->sin_addr,
3119 srv_addr, INET_ADDRSTRLEN + 1);
3120 break;
3121 case AF_INET6:
3122 inet_ntop(srv->addr.ss_family, &((struct sockaddr_in6 *)&srv->addr)->sin6_addr,
3123 srv_addr, INET6_ADDRSTRLEN + 1);
3124 break;
3125 }
3126 srv_time_since_last_change = now.tv_sec - srv->last_change;
Cyril Bonté76a99782016-05-06 12:18:48 +02003127 bk_f_forced_id = appctx->ctx.server_state.px->options & PR_O_FORCED_ID ? 1 : 0;
Baptiste Assmann28289462015-07-03 08:01:20 +02003128 srv_f_forced_id = srv->flags & SRV_F_FORCED_ID ? 1 : 0;
3129
3130 chunk_appendf(buf,
3131 "%d %s "
3132 "%d %s %s "
3133 "%d %d %d %d %ld "
3134 "%d %d %d %d %d "
3135 "%d %d"
3136 "\n",
Cyril Bonté76a99782016-05-06 12:18:48 +02003137 appctx->ctx.server_state.px->uuid, appctx->ctx.server_state.px->id,
Baptiste Assmann28289462015-07-03 08:01:20 +02003138 srv->puid, srv->id, srv_addr,
David Carlier081b3362015-11-18 06:10:22 +00003139 srv->state, srv->admin, srv->uweight, srv->iweight, (long int)srv_time_since_last_change,
Baptiste Assmann28289462015-07-03 08:01:20 +02003140 srv->check.status, srv->check.result, srv->check.health, srv->check.state, srv->agent.state,
3141 bk_f_forced_id, srv_f_forced_id);
Cyril Bonté76a99782016-05-06 12:18:48 +02003142 if (bi_putchk(si_ic(si), &trash) == -1) {
3143 si_applet_cant_put(si);
3144 return 0;
3145 }
Baptiste Assmann28289462015-07-03 08:01:20 +02003146 }
Cyril Bonté76a99782016-05-06 12:18:48 +02003147 return 1;
Baptiste Assmann28289462015-07-03 08:01:20 +02003148}
3149
Baptiste Assmann9b6857e2015-09-18 14:49:12 +02003150/* Parses backend list and simply report backend names */
3151static int stats_dump_backend_to_buffer(struct stream_interface *si)
3152{
Cyril Bonté6ca9e012016-05-06 12:18:49 +02003153 struct appctx *appctx = __objt_appctx(si->end);
Baptiste Assmann9b6857e2015-09-18 14:49:12 +02003154 extern struct proxy *proxy;
3155 struct proxy *curproxy;
3156
3157 chunk_reset(&trash);
Baptiste Assmann9b6857e2015-09-18 14:49:12 +02003158
Cyril Bonté6ca9e012016-05-06 12:18:49 +02003159 if (!appctx->ctx.be.px) {
3160 chunk_printf(&trash, "# name\n");
3161 if (bi_putchk(si_ic(si), &trash) == -1) {
3162 si_applet_cant_put(si);
3163 return 0;
3164 }
3165 appctx->ctx.be.px = proxy;
3166 }
3167
3168 for (; appctx->ctx.be.px != NULL; appctx->ctx.be.px = curproxy->next) {
3169 curproxy = appctx->ctx.be.px;
3170
Baptiste Assmann9b6857e2015-09-18 14:49:12 +02003171 /* looking for backends only */
3172 if (!(curproxy->cap & PR_CAP_BE))
3173 continue;
3174
3175 /* we don't want to list a backend which is bound to this process */
3176 if (curproxy->bind_proc && !(curproxy->bind_proc & (1UL << (relative_pid - 1))))
3177 continue;
3178
3179 chunk_appendf(&trash, "%s\n", curproxy->id);
Cyril Bonté6ca9e012016-05-06 12:18:49 +02003180 if (bi_putchk(si_ic(si), &trash) == -1) {
3181 si_applet_cant_put(si);
3182 return 0;
3183 }
Baptiste Assmann9b6857e2015-09-18 14:49:12 +02003184 }
3185
3186 return 1;
3187}
3188
Baptiste Assmann28289462015-07-03 08:01:20 +02003189/* Parses backend list or simply use backend name provided by the user to return
3190 * states of servers to stdout.
3191 */
3192static int stats_dump_servers_state_to_buffer(struct stream_interface *si)
3193{
3194 struct appctx *appctx = __objt_appctx(si->end);
3195 extern struct proxy *proxy;
3196 struct proxy *curproxy;
3197
3198 chunk_reset(&trash);
3199
Cyril Bonté76a99782016-05-06 12:18:48 +02003200 if (!appctx->ctx.server_state.px) {
3201 chunk_printf(&trash, "%d\n# %s\n", SRV_STATE_FILE_VERSION, SRV_STATE_FILE_FIELD_NAMES);
3202 if (bi_putchk(si_ic(si), &trash) == -1) {
3203 si_applet_cant_put(si);
3204 return 0;
Baptiste Assmann28289462015-07-03 08:01:20 +02003205 }
Cyril Bonté76a99782016-05-06 12:18:48 +02003206 appctx->ctx.server_state.px = proxy;
Baptiste Assmann28289462015-07-03 08:01:20 +02003207 }
3208
Cyril Bonté76a99782016-05-06 12:18:48 +02003209 for (; appctx->ctx.server_state.px != NULL; appctx->ctx.server_state.px = curproxy->next) {
3210 curproxy = appctx->ctx.server_state.px;
Cyril Bonté76a99782016-05-06 12:18:48 +02003211 /* servers are only in backends */
3212 if (curproxy->cap & PR_CAP_BE) {
3213 if (!dump_servers_state(si, &trash))
3214 return 0;
3215
3216 if (bi_putchk(si_ic(si), &trash) == -1) {
3217 si_applet_cant_put(si);
3218 return 0;
3219 }
3220 }
3221 /* only the selected proxy is dumped */
3222 if (appctx->ctx.server_state.iid)
3223 break;
Baptiste Assmann28289462015-07-03 08:01:20 +02003224 }
3225
3226 return 1;
3227}
3228
Willy Tarreau12833bb2014-01-28 16:49:56 +01003229/* This function dumps memory usage information onto the stream interface's
3230 * read buffer. It returns 0 as long as it does not complete, non-zero upon
3231 * completion. No state is used.
3232 */
3233static int stats_dump_pools_to_buffer(struct stream_interface *si)
3234{
3235 dump_pools_to_trash();
Willy Tarreaubc18da12015-03-13 14:00:47 +01003236 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02003237 si_applet_cant_put(si);
Willy Tarreau12833bb2014-01-28 16:49:56 +01003238 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01003239 }
Willy Tarreau82a86022016-01-04 19:04:18 +01003240 return 1;
3241}
3242
3243/* Dump all fields from <stats> into <out> using CSV format */
3244static int stats_dump_fields_csv(struct chunk *out, const struct field *stats)
3245{
3246 int field;
3247
3248 for (field = 0; field < ST_F_TOTAL_FIELDS; field++) {
3249 if (!stats_emit_raw_data_field(out, &stats[field]))
3250 return 0;
3251 if (!chunk_strcat(out, ","))
3252 return 0;
3253 }
Conrad Hoffmann692c9382016-04-01 20:40:58 +02003254 chunk_strcat(out, "\n");
Willy Tarreau12833bb2014-01-28 16:49:56 +01003255 return 1;
3256}
3257
Willy Tarreau1e62df92016-01-11 18:57:53 +01003258/* Dump all fields from <stats> into <out> using a typed "field:desc:type:value" format */
3259static int stats_dump_fields_typed(struct chunk *out, const struct field *stats)
3260{
3261 int field;
3262
3263 for (field = 0; field < ST_F_TOTAL_FIELDS; field++) {
3264 if (!stats[field].type)
3265 continue;
3266
3267 chunk_appendf(out, "%c.%u.%u.%d.%s.%u:",
3268 stats[ST_F_TYPE].u.u32 == STATS_TYPE_FE ? 'F' :
3269 stats[ST_F_TYPE].u.u32 == STATS_TYPE_BE ? 'B' :
3270 stats[ST_F_TYPE].u.u32 == STATS_TYPE_SO ? 'L' :
3271 stats[ST_F_TYPE].u.u32 == STATS_TYPE_SV ? 'S' :
3272 '?',
3273 stats[ST_F_IID].u.u32, stats[ST_F_SID].u.u32,
3274 field, stat_field_names[field], stats[ST_F_PID].u.u32);
3275
3276 if (!stats_emit_field_tags(out, &stats[field], ':'))
3277 return 0;
3278 if (!stats_emit_typed_data_field(out, &stats[field]))
3279 return 0;
3280 if (!chunk_strcat(out, "\n"))
3281 return 0;
3282 }
3283 return 1;
3284}
3285
Willy Tarreau60600742016-01-11 15:32:30 +01003286/* Dump all fields from <stats> into <out> using the HTML format. A column is
Willy Tarreau508a63f2016-01-11 15:28:40 +01003287 * reserved for the checkbox is ST_SHOWADMIN is set in <flags>. Some extra info
3288 * are provided if ST_SHLGNDS is present in <flags>.
Cyril Bonté70be45d2010-10-12 00:14:35 +02003289 */
Willy Tarreau60600742016-01-11 15:32:30 +01003290static int stats_dump_fields_html(struct chunk *out, const struct field *stats, unsigned int flags)
Cyril Bonté70be45d2010-10-12 00:14:35 +02003291{
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003292 struct chunk src;
3293
Willy Tarreaub5f66b82016-01-08 17:15:39 +01003294 if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_FE) {
Willy Tarreau60600742016-01-11 15:32:30 +01003295 chunk_appendf(out,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003296 /* name, queue */
3297 "<tr class=\"frontend\">");
Cyril Bonté70be45d2010-10-12 00:14:35 +02003298
Willy Tarreau508a63f2016-01-11 15:28:40 +01003299 if (flags & ST_SHOWADMIN) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003300 /* Column sub-heading for Enable or Disable server */
Willy Tarreau60600742016-01-11 15:32:30 +01003301 chunk_appendf(out, "<td></td>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003302 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02003303
Willy Tarreau60600742016-01-11 15:32:30 +01003304 chunk_appendf(out,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003305 "<td class=ac>"
3306 "<a name=\"%s/Frontend\"></a>"
3307 "<a class=lfsb href=\"#%s/Frontend\">Frontend</a></td>"
3308 "<td colspan=3></td>"
3309 "",
Willy Tarreaud72c9172016-01-06 15:26:40 +01003310 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_PXNAME));
Cyril Bonté70be45d2010-10-12 00:14:35 +02003311
Willy Tarreau60600742016-01-11 15:32:30 +01003312 chunk_appendf(out,
Willy Tarreau466c9b52012-12-23 02:25:03 +01003313 /* sessions rate : current */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003314 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01003315 "<tr><th>Current connection rate:</th><td>%s/s</td></tr>"
3316 "<tr><th>Current session rate:</th><td>%s/s</td></tr>"
3317 "",
Willy Tarreaud72c9172016-01-06 15:26:40 +01003318 U2H(stats[ST_F_RATE].u.u32),
Willy Tarreauc73810f2016-01-11 13:52:04 +01003319 U2H(stats[ST_F_CONN_RATE].u.u32),
Willy Tarreaud72c9172016-01-06 15:26:40 +01003320 U2H(stats[ST_F_RATE].u.u32));
Willy Tarreau466c9b52012-12-23 02:25:03 +01003321
Willy Tarreauf8211df2016-01-11 14:09:38 +01003322 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
Willy Tarreau60600742016-01-11 15:32:30 +01003323 chunk_appendf(out,
Willy Tarreau466c9b52012-12-23 02:25:03 +01003324 "<tr><th>Current request rate:</th><td>%s/s</td></tr>",
Willy Tarreaud72c9172016-01-06 15:26:40 +01003325 U2H(stats[ST_F_REQ_RATE].u.u32));
Willy Tarreau466c9b52012-12-23 02:25:03 +01003326
Willy Tarreau60600742016-01-11 15:32:30 +01003327 chunk_appendf(out,
Willy Tarreau466c9b52012-12-23 02:25:03 +01003328 "</table></div></u></td>"
3329 /* sessions rate : max */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003330 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01003331 "<tr><th>Max connection rate:</th><td>%s/s</td></tr>"
3332 "<tr><th>Max session rate:</th><td>%s/s</td></tr>"
3333 "",
Willy Tarreaud72c9172016-01-06 15:26:40 +01003334 U2H(stats[ST_F_RATE_MAX].u.u32),
Willy Tarreauc73810f2016-01-11 13:52:04 +01003335 U2H(stats[ST_F_CONN_RATE_MAX].u.u32),
Willy Tarreaud72c9172016-01-06 15:26:40 +01003336 U2H(stats[ST_F_RATE_MAX].u.u32));
Willy Tarreau466c9b52012-12-23 02:25:03 +01003337
Willy Tarreauf8211df2016-01-11 14:09:38 +01003338 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
Willy Tarreau60600742016-01-11 15:32:30 +01003339 chunk_appendf(out,
Willy Tarreau466c9b52012-12-23 02:25:03 +01003340 "<tr><th>Max request rate:</th><td>%s/s</td></tr>",
Willy Tarreaud72c9172016-01-06 15:26:40 +01003341 U2H(stats[ST_F_REQ_RATE_MAX].u.u32));
Willy Tarreau466c9b52012-12-23 02:25:03 +01003342
Willy Tarreau60600742016-01-11 15:32:30 +01003343 chunk_appendf(out,
Willy Tarreau466c9b52012-12-23 02:25:03 +01003344 "</table></div></u></td>"
3345 /* sessions rate : limit */
3346 "<td>%s</td>",
Willy Tarreaud72c9172016-01-06 15:26:40 +01003347 LIM2A(stats[ST_F_RATE_LIM].u.u32, "-"));
Cyril Bonté70be45d2010-10-12 00:14:35 +02003348
Willy Tarreau60600742016-01-11 15:32:30 +01003349 chunk_appendf(out,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003350 /* sessions: current, max, limit, total */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003351 "<td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003352 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01003353 "<tr><th>Cum. connections:</th><td>%s</td></tr>"
3354 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003355 "",
Willy Tarreauc73810f2016-01-11 13:52:04 +01003356 U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SMAX].u.u32), U2H(stats[ST_F_SLIM].u.u32),
Willy Tarreaud72c9172016-01-06 15:26:40 +01003357 U2H(stats[ST_F_STOT].u.u64),
Willy Tarreauc73810f2016-01-11 13:52:04 +01003358 U2H(stats[ST_F_CONN_TOT].u.u64),
Willy Tarreaud72c9172016-01-06 15:26:40 +01003359 U2H(stats[ST_F_STOT].u.u64));
Cyril Bonté70be45d2010-10-12 00:14:35 +02003360
Willy Tarreau466c9b52012-12-23 02:25:03 +01003361 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
Willy Tarreauf8211df2016-01-11 14:09:38 +01003362 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
Willy Tarreau60600742016-01-11 15:32:30 +01003363 chunk_appendf(out,
Willy Tarreau466c9b52012-12-23 02:25:03 +01003364 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
3365 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
3366 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
3367 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
3368 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
3369 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
3370 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
3371 "<tr><th>- other responses:</th><td>%s</td></tr>"
3372 "<tr><th>Intercepted requests:</th><td>%s</td></tr>"
3373 "",
Willy Tarreaud72c9172016-01-06 15:26:40 +01003374 U2H(stats[ST_F_REQ_TOT].u.u64),
3375 U2H(stats[ST_F_HRSP_1XX].u.u64),
3376 U2H(stats[ST_F_HRSP_2XX].u.u64),
3377 U2H(stats[ST_F_COMP_RSP].u.u64),
3378 stats[ST_F_HRSP_2XX].u.u64 ?
3379 (int)(100 * stats[ST_F_COMP_RSP].u.u64 / stats[ST_F_HRSP_2XX].u.u64) : 0,
3380 U2H(stats[ST_F_HRSP_3XX].u.u64),
3381 U2H(stats[ST_F_HRSP_4XX].u.u64),
3382 U2H(stats[ST_F_HRSP_5XX].u.u64),
3383 U2H(stats[ST_F_HRSP_OTHER].u.u64),
Willy Tarreau5b9bdff2016-01-11 14:40:47 +01003384 U2H(stats[ST_F_INTERCEPTED].u.u64));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003385 }
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02003386
Willy Tarreau60600742016-01-11 15:32:30 +01003387 chunk_appendf(out,
Willy Tarreau466c9b52012-12-23 02:25:03 +01003388 "</table></div></u></td>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05003389 /* sessions: lbtot, lastsess */
3390 "<td></td><td></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003391 /* bytes : in */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003392 "<td>%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003393 "",
Willy Tarreaud72c9172016-01-06 15:26:40 +01003394 U2H(stats[ST_F_BIN].u.u64));
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02003395
Willy Tarreau60600742016-01-11 15:32:30 +01003396 chunk_appendf(out,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003397 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
Willy Tarreau4e5d58e2015-07-04 14:35:15 +02003398 "<td>%s%s<div class=tips><table class=det>"
3399 "<tr><th>Response bytes in:</th><td>%s</td></tr>"
3400 "<tr><th>Compression in:</th><td>%s</td></tr>"
3401 "<tr><th>Compression out:</th><td>%s</td><td>(%d%%)</td></tr>"
3402 "<tr><th>Compression bypass:</th><td>%s</td></tr>"
3403 "<tr><th>Total bytes saved:</th><td>%s</td><td>(%d%%)</td></tr>"
3404 "</table></div>%s</td>",
Willy Tarreaud72c9172016-01-06 15:26:40 +01003405 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "<u>":"",
3406 U2H(stats[ST_F_BOUT].u.u64),
3407 U2H(stats[ST_F_BOUT].u.u64),
3408 U2H(stats[ST_F_COMP_IN].u.u64),
3409 U2H(stats[ST_F_COMP_OUT].u.u64),
3410 stats[ST_F_COMP_IN].u.u64 ? (int)(stats[ST_F_COMP_OUT].u.u64 * 100 / stats[ST_F_COMP_IN].u.u64) : 0,
3411 U2H(stats[ST_F_COMP_BYP].u.u64),
3412 U2H(stats[ST_F_COMP_IN].u.u64 - stats[ST_F_COMP_OUT].u.u64),
3413 stats[ST_F_BOUT].u.u64 ? (int)((stats[ST_F_COMP_IN].u.u64 - stats[ST_F_COMP_OUT].u.u64) * 100 / stats[ST_F_BOUT].u.u64) : 0,
3414 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "</u>":"");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02003415
Willy Tarreau60600742016-01-11 15:32:30 +01003416 chunk_appendf(out,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003417 /* denied: req, resp */
3418 "<td>%s</td><td>%s</td>"
3419 /* errors : request, connect, response */
3420 "<td>%s</td><td></td><td></td>"
3421 /* warnings: retries, redispatches */
3422 "<td></td><td></td>"
3423 /* server status : reflect frontend status */
3424 "<td class=ac>%s</td>"
3425 /* rest of server: nothing */
3426 "<td class=ac colspan=8></td></tr>"
3427 "",
Willy Tarreaud72c9172016-01-06 15:26:40 +01003428 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
3429 U2H(stats[ST_F_EREQ].u.u64),
3430 field_str(stats, ST_F_STATUS));
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02003431 }
Willy Tarreaufa9512f2016-01-08 17:20:51 +01003432 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_SO) {
Willy Tarreau60600742016-01-11 15:32:30 +01003433 chunk_appendf(out, "<tr class=socket>");
Willy Tarreau508a63f2016-01-11 15:28:40 +01003434 if (flags & ST_SHOWADMIN) {
Willy Tarreaufa9512f2016-01-08 17:20:51 +01003435 /* Column sub-heading for Enable or Disable server */
Willy Tarreau60600742016-01-11 15:32:30 +01003436 chunk_appendf(out, "<td></td>");
Willy Tarreaufa9512f2016-01-08 17:20:51 +01003437 }
3438
Willy Tarreau60600742016-01-11 15:32:30 +01003439 chunk_appendf(out,
Willy Tarreaufa9512f2016-01-08 17:20:51 +01003440 /* frontend name, listener name */
3441 "<td class=ac><a name=\"%s/+%s\"></a>%s"
3442 "<a class=lfsb href=\"#%s/+%s\">%s</a>"
3443 "",
3444 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME),
3445 (flags & ST_SHLGNDS)?"<u>":"",
3446 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME), field_str(stats, ST_F_SVNAME));
3447
3448 if (flags & ST_SHLGNDS) {
Willy Tarreau60600742016-01-11 15:32:30 +01003449 chunk_appendf(out, "<div class=tips>");
Willy Tarreaufa9512f2016-01-08 17:20:51 +01003450
3451 if (isdigit(*field_str(stats, ST_F_ADDR)))
Willy Tarreau60600742016-01-11 15:32:30 +01003452 chunk_appendf(out, "IPv4: %s, ", field_str(stats, ST_F_ADDR));
Willy Tarreaufa9512f2016-01-08 17:20:51 +01003453 else if (*field_str(stats, ST_F_ADDR) == '[')
Willy Tarreau60600742016-01-11 15:32:30 +01003454 chunk_appendf(out, "IPv6: %s, ", field_str(stats, ST_F_ADDR));
Willy Tarreaufa9512f2016-01-08 17:20:51 +01003455 else if (*field_str(stats, ST_F_ADDR))
Willy Tarreau60600742016-01-11 15:32:30 +01003456 chunk_appendf(out, "%s, ", field_str(stats, ST_F_ADDR));
Willy Tarreaufa9512f2016-01-08 17:20:51 +01003457
3458 /* id */
Willy Tarreau60600742016-01-11 15:32:30 +01003459 chunk_appendf(out, "id: %d</div>", stats[ST_F_SID].u.u32);
Willy Tarreaufa9512f2016-01-08 17:20:51 +01003460 }
3461
Willy Tarreau60600742016-01-11 15:32:30 +01003462 chunk_appendf(out,
Willy Tarreaufa9512f2016-01-08 17:20:51 +01003463 /* queue */
3464 "%s</td><td colspan=3></td>"
3465 /* sessions rate: current, max, limit */
3466 "<td colspan=3>&nbsp;</td>"
3467 /* sessions: current, max, limit, total, lbtot, lastsess */
3468 "<td>%s</td><td>%s</td><td>%s</td>"
3469 "<td>%s</td><td>&nbsp;</td><td>&nbsp;</td>"
3470 /* bytes: in, out */
3471 "<td>%s</td><td>%s</td>"
3472 "",
3473 (flags & ST_SHLGNDS)?"</u>":"",
3474 U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SMAX].u.u32), U2H(stats[ST_F_SLIM].u.u32),
3475 U2H(stats[ST_F_STOT].u.u64), U2H(stats[ST_F_BIN].u.u64), U2H(stats[ST_F_BOUT].u.u64));
3476
Willy Tarreau60600742016-01-11 15:32:30 +01003477 chunk_appendf(out,
Willy Tarreaufa9512f2016-01-08 17:20:51 +01003478 /* denied: req, resp */
3479 "<td>%s</td><td>%s</td>"
3480 /* errors: request, connect, response */
3481 "<td>%s</td><td></td><td></td>"
3482 /* warnings: retries, redispatches */
3483 "<td></td><td></td>"
3484 /* server status: reflect listener status */
3485 "<td class=ac>%s</td>"
3486 /* rest of server: nothing */
3487 "<td class=ac colspan=8></td></tr>"
3488 "",
3489 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
3490 U2H(stats[ST_F_EREQ].u.u64),
3491 field_str(stats, ST_F_STATUS));
3492 }
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003493 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_SV) {
3494 const char *style;
Willy Tarreaub5f66b82016-01-08 17:15:39 +01003495
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003496 /* determine the style to use depending on the server's state,
3497 * its health and weight. There isn't a 1-to-1 mapping between
3498 * state and styles for the cases where the server is (still)
3499 * up. The reason is that we don't want to report nolb and
3500 * drain with the same color.
3501 */
Willy Tarreaub5f66b82016-01-08 17:15:39 +01003502
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003503 if (strcmp(field_str(stats, ST_F_STATUS), "DOWN") == 0 ||
3504 strcmp(field_str(stats, ST_F_STATUS), "DOWN (agent)") == 0) {
3505 style = "down";
3506 }
3507 else if (strcmp(field_str(stats, ST_F_STATUS), "DOWN ") == 0) {
3508 style = "going_up";
3509 }
3510 else if (strcmp(field_str(stats, ST_F_STATUS), "NOLB ") == 0) {
3511 style = "going_down";
3512 }
3513 else if (strcmp(field_str(stats, ST_F_STATUS), "NOLB") == 0) {
3514 style = "nolb";
3515 }
3516 else if (strcmp(field_str(stats, ST_F_STATUS), "no check") == 0) {
3517 style = "no_check";
3518 }
3519 else if (!stats[ST_F_CHKFAIL].type ||
3520 stats[ST_F_CHECK_HEALTH].u.u32 == stats[ST_F_CHECK_RISE].u.u32 + stats[ST_F_CHECK_FALL].u.u32 - 1) {
3521 /* no check or max health = UP */
3522 if (stats[ST_F_WEIGHT].u.u32)
3523 style = "up";
3524 else
3525 style = "draining";
3526 }
3527 else {
3528 style = "going_down";
3529 }
Willy Tarreaub5f66b82016-01-08 17:15:39 +01003530
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003531 if (memcmp(field_str(stats, ST_F_STATUS), "MAINT", 5) == 0)
Willy Tarreau60600742016-01-11 15:32:30 +01003532 chunk_appendf(out, "<tr class=\"maintain\">");
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003533 else
Willy Tarreau60600742016-01-11 15:32:30 +01003534 chunk_appendf(out,
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003535 "<tr class=\"%s_%s\">",
3536 (stats[ST_F_BCK].u.u32) ? "backup" : "active", style);
Willy Tarreaub5f66b82016-01-08 17:15:39 +01003537
Willy Tarreaub5f66b82016-01-08 17:15:39 +01003538
Willy Tarreau508a63f2016-01-11 15:28:40 +01003539 if (flags & ST_SHOWADMIN)
Willy Tarreau60600742016-01-11 15:32:30 +01003540 chunk_appendf(out,
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003541 "<td><input type=\"checkbox\" name=\"s\" value=\"%s\"></td>",
3542 field_str(stats, ST_F_SVNAME));
Willy Tarreaub5f66b82016-01-08 17:15:39 +01003543
Willy Tarreau60600742016-01-11 15:32:30 +01003544 chunk_appendf(out,
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003545 "<td class=ac><a name=\"%s/%s\"></a>%s"
3546 "<a class=lfsb href=\"#%s/%s\">%s</a>"
3547 "",
3548 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME),
3549 (flags & ST_SHLGNDS) ? "<u>" : "",
3550 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME), field_str(stats, ST_F_SVNAME));
Willy Tarreaub5f66b82016-01-08 17:15:39 +01003551
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003552 if (flags & ST_SHLGNDS) {
Willy Tarreau60600742016-01-11 15:32:30 +01003553 chunk_appendf(out, "<div class=tips>");
Willy Tarreaub5f66b82016-01-08 17:15:39 +01003554
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003555 if (isdigit(*field_str(stats, ST_F_ADDR)))
Willy Tarreau60600742016-01-11 15:32:30 +01003556 chunk_appendf(out, "IPv4: %s, ", field_str(stats, ST_F_ADDR));
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003557 else if (*field_str(stats, ST_F_ADDR) == '[')
Willy Tarreau60600742016-01-11 15:32:30 +01003558 chunk_appendf(out, "IPv6: %s, ", field_str(stats, ST_F_ADDR));
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003559 else if (*field_str(stats, ST_F_ADDR))
Willy Tarreau60600742016-01-11 15:32:30 +01003560 chunk_appendf(out, "%s, ", field_str(stats, ST_F_ADDR));
Willy Tarreaub5f66b82016-01-08 17:15:39 +01003561
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003562 /* id */
Willy Tarreau60600742016-01-11 15:32:30 +01003563 chunk_appendf(out, "id: %d", stats[ST_F_SID].u.u32);
Willy Tarreaub5f66b82016-01-08 17:15:39 +01003564
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003565 /* cookie */
3566 if (stats[ST_F_COOKIE].type) {
Willy Tarreau60600742016-01-11 15:32:30 +01003567 chunk_appendf(out, ", cookie: '");
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003568 chunk_initstr(&src, field_str(stats, ST_F_COOKIE));
Willy Tarreau60600742016-01-11 15:32:30 +01003569 chunk_htmlencode(out, &src);
3570 chunk_appendf(out, "'");
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003571 }
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02003572
Willy Tarreau60600742016-01-11 15:32:30 +01003573 chunk_appendf(out, "</div>");
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003574 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003575
Willy Tarreau60600742016-01-11 15:32:30 +01003576 chunk_appendf(out,
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003577 /* queue : current, max, limit */
3578 "%s</td><td>%s</td><td>%s</td><td>%s</td>"
3579 /* sessions rate : current, max, limit */
3580 "<td>%s</td><td>%s</td><td></td>"
3581 "",
3582 (flags & ST_SHLGNDS) ? "</u>" : "",
3583 U2H(stats[ST_F_QCUR].u.u32), U2H(stats[ST_F_QMAX].u.u32), LIM2A(stats[ST_F_QLIMIT].u.u32, "-"),
3584 U2H(stats[ST_F_RATE].u.u32), U2H(stats[ST_F_RATE_MAX].u.u32));
Willy Tarreau4607fad2016-01-06 15:41:29 +01003585
Willy Tarreau60600742016-01-11 15:32:30 +01003586 chunk_appendf(out,
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003587 /* sessions: current, max, limit, total */
3588 "<td>%s</td><td>%s</td><td>%s</td>"
3589 "<td><u>%s<div class=tips><table class=det>"
3590 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
3591 "",
3592 U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SMAX].u.u32), LIM2A(stats[ST_F_SLIM].u.u32, "-"),
3593 U2H(stats[ST_F_STOT].u.u64),
3594 U2H(stats[ST_F_STOT].u.u64));
Willy Tarreau4607fad2016-01-06 15:41:29 +01003595
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003596 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
Willy Tarreauf8211df2016-01-11 14:09:38 +01003597 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003598 unsigned long long tot;
Willy Tarreaua6f5a732016-01-08 16:59:56 +01003599
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003600 tot = stats[ST_F_HRSP_OTHER].u.u64;
3601 tot += stats[ST_F_HRSP_1XX].u.u64;
3602 tot += stats[ST_F_HRSP_2XX].u.u64;
3603 tot += stats[ST_F_HRSP_3XX].u.u64;
3604 tot += stats[ST_F_HRSP_4XX].u.u64;
3605 tot += stats[ST_F_HRSP_5XX].u.u64;
3606
Willy Tarreau60600742016-01-11 15:32:30 +01003607 chunk_appendf(out,
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003608 "<tr><th>Cum. HTTP responses:</th><td>%s</td></tr>"
3609 "<tr><th>- HTTP 1xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
3610 "<tr><th>- HTTP 2xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
3611 "<tr><th>- HTTP 3xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
3612 "<tr><th>- HTTP 4xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
3613 "<tr><th>- HTTP 5xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
3614 "<tr><th>- other responses:</th><td>%s</td><td>(%d%%)</td></tr>"
3615 "",
3616 U2H(tot),
3617 U2H(stats[ST_F_HRSP_1XX].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_1XX].u.u64 / tot) : 0,
3618 U2H(stats[ST_F_HRSP_2XX].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_2XX].u.u64 / tot) : 0,
3619 U2H(stats[ST_F_HRSP_3XX].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_3XX].u.u64 / tot) : 0,
3620 U2H(stats[ST_F_HRSP_4XX].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_4XX].u.u64 / tot) : 0,
3621 U2H(stats[ST_F_HRSP_5XX].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_5XX].u.u64 / tot) : 0,
3622 U2H(stats[ST_F_HRSP_OTHER].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_OTHER].u.u64 / tot) : 0);
Willy Tarreaua6f5a732016-01-08 16:59:56 +01003623 }
Willy Tarreaua6f5a732016-01-08 16:59:56 +01003624
Willy Tarreau60600742016-01-11 15:32:30 +01003625 chunk_appendf(out, "<tr><th colspan=3>Avg over last 1024 success. conn.</th></tr>");
3626 chunk_appendf(out, "<tr><th>- Queue time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_QTIME].u.u32));
3627 chunk_appendf(out, "<tr><th>- Connect time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_CTIME].u.u32));
Willy Tarreauf8211df2016-01-11 14:09:38 +01003628 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
Willy Tarreau60600742016-01-11 15:32:30 +01003629 chunk_appendf(out, "<tr><th>- Response time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_RTIME].u.u32));
3630 chunk_appendf(out, "<tr><th>- Total time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_TTIME].u.u32));
Willy Tarreau56a560a2009-09-22 19:27:35 +02003631
Willy Tarreau60600742016-01-11 15:32:30 +01003632 chunk_appendf(out,
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003633 "</table></div></u></td>"
3634 /* sessions: lbtot, last */
3635 "<td>%s</td><td>%s</td>",
3636 U2H(stats[ST_F_LBTOT].u.u64),
3637 human_time(stats[ST_F_LASTSESS].u.s32, 1));
Willy Tarreau91861262007-10-17 17:06:05 +02003638
Willy Tarreau60600742016-01-11 15:32:30 +01003639 chunk_appendf(out,
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003640 /* bytes : in, out */
3641 "<td>%s</td><td>%s</td>"
3642 /* denied: req, resp */
3643 "<td></td><td>%s</td>"
3644 /* errors : request, connect */
3645 "<td></td><td>%s</td>"
3646 /* errors : response */
3647 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
3648 /* warnings: retries, redispatches */
3649 "<td>%lld</td><td>%lld</td>"
3650 "",
3651 U2H(stats[ST_F_BIN].u.u64), U2H(stats[ST_F_BOUT].u.u64),
3652 U2H(stats[ST_F_DRESP].u.u64),
3653 U2H(stats[ST_F_ECON].u.u64),
3654 U2H(stats[ST_F_ERESP].u.u64),
3655 (long long)stats[ST_F_CLI_ABRT].u.u64,
3656 (long long)stats[ST_F_SRV_ABRT].u.u64,
3657 (long long)stats[ST_F_WRETR].u.u64,
3658 (long long)stats[ST_F_WREDIS].u.u64);
3659
3660 /* status, last change */
Willy Tarreau60600742016-01-11 15:32:30 +01003661 chunk_appendf(out, "<td class=ac>");
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003662
3663 /* FIXME!!!!
3664 * LASTCHG should contain the last change for *this* server and must be computed
3665 * properly above, as was done below, ie: this server if maint, otherwise ref server
3666 * if tracking. Note that ref is either local or remote depending on tracking.
3667 */
3668
3669
3670 if (memcmp(field_str(stats, ST_F_STATUS), "MAINT", 5) == 0) {
Willy Tarreau60600742016-01-11 15:32:30 +01003671 chunk_appendf(out, "%s MAINT", human_time(stats[ST_F_LASTCHG].u.u32, 1));
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003672 }
3673 else if (memcmp(field_str(stats, ST_F_STATUS), "no check", 5) == 0) {
Willy Tarreau60600742016-01-11 15:32:30 +01003674 chunk_strcat(out, "<i>no check</i>");
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003675 }
3676 else {
Willy Tarreau60600742016-01-11 15:32:30 +01003677 chunk_appendf(out, "%s %s", human_time(stats[ST_F_LASTCHG].u.u32, 1), field_str(stats, ST_F_STATUS));
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003678 if (memcmp(field_str(stats, ST_F_STATUS), "DOWN", 4) == 0) {
3679 if (stats[ST_F_CHECK_HEALTH].u.u32)
Willy Tarreau60600742016-01-11 15:32:30 +01003680 chunk_strcat(out, " &uarr;");
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003681 }
3682 else if (stats[ST_F_CHECK_HEALTH].u.u32 < stats[ST_F_CHECK_RISE].u.u32 + stats[ST_F_CHECK_FALL].u.u32 - 1)
Willy Tarreau60600742016-01-11 15:32:30 +01003683 chunk_strcat(out, " &darr;");
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003684 }
3685
3686 if (memcmp(field_str(stats, ST_F_STATUS), "DOWN", 4) == 0 &&
3687 stats[ST_F_AGENT_STATUS].type && !stats[ST_F_AGENT_HEALTH].u.u32) {
Willy Tarreau60600742016-01-11 15:32:30 +01003688 chunk_appendf(out,
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003689 "</td><td class=ac><u> %s",
3690 field_str(stats, ST_F_AGENT_STATUS));
3691
3692 if (stats[ST_F_AGENT_CODE].type)
Willy Tarreau60600742016-01-11 15:32:30 +01003693 chunk_appendf(out, "/%d", stats[ST_F_AGENT_CODE].u.u32);
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003694
3695 if (stats[ST_F_AGENT_DURATION].type && stats[ST_F_AGENT_DURATION].u.u64 >= 0)
Willy Tarreau60600742016-01-11 15:32:30 +01003696 chunk_appendf(out, " in %lums", (long)stats[ST_F_AGENT_DURATION].u.u64);
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003697
Willy Tarreau60600742016-01-11 15:32:30 +01003698 chunk_appendf(out, "<div class=tips>%s", field_str(stats, ST_F_AGENT_DESC));
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003699
3700 if (*field_str(stats, ST_F_LAST_AGT)) {
Willy Tarreau60600742016-01-11 15:32:30 +01003701 chunk_appendf(out, ": ");
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003702 chunk_initstr(&src, field_str(stats, ST_F_LAST_AGT));
Willy Tarreau60600742016-01-11 15:32:30 +01003703 chunk_htmlencode(out, &src);
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003704 }
Willy Tarreau60600742016-01-11 15:32:30 +01003705 chunk_appendf(out, "</div></u>");
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003706 }
3707 else if (stats[ST_F_CHECK_STATUS].type) {
Willy Tarreau60600742016-01-11 15:32:30 +01003708 chunk_appendf(out,
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003709 "</td><td class=ac><u> %s",
3710 field_str(stats, ST_F_CHECK_STATUS));
3711
3712 if (stats[ST_F_CHECK_CODE].type)
Willy Tarreau60600742016-01-11 15:32:30 +01003713 chunk_appendf(out, "/%d", stats[ST_F_CHECK_CODE].u.u32);
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003714
3715 if (stats[ST_F_CHECK_DURATION].type && stats[ST_F_CHECK_DURATION].u.u64 >= 0)
Willy Tarreau60600742016-01-11 15:32:30 +01003716 chunk_appendf(out, " in %lums", (long)stats[ST_F_CHECK_DURATION].u.u64);
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003717
Willy Tarreau60600742016-01-11 15:32:30 +01003718 chunk_appendf(out, "<div class=tips>%s", field_str(stats, ST_F_CHECK_DESC));
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003719
3720 if (*field_str(stats, ST_F_LAST_CHK)) {
Willy Tarreau60600742016-01-11 15:32:30 +01003721 chunk_appendf(out, ": ");
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003722 chunk_initstr(&src, field_str(stats, ST_F_LAST_CHK));
Willy Tarreau60600742016-01-11 15:32:30 +01003723 chunk_htmlencode(out, &src);
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003724 }
Willy Tarreau60600742016-01-11 15:32:30 +01003725 chunk_appendf(out, "</div></u>");
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003726 }
3727 else
Willy Tarreau60600742016-01-11 15:32:30 +01003728 chunk_appendf(out, "</td><td>");
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003729
Willy Tarreau60600742016-01-11 15:32:30 +01003730 chunk_appendf(out,
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003731 /* weight */
3732 "</td><td class=ac>%d</td>"
3733 /* act, bck */
3734 "<td class=ac>%s</td><td class=ac>%s</td>"
3735 "",
3736 stats[ST_F_WEIGHT].u.u32,
3737 stats[ST_F_BCK].u.u32 ? "-" : "Y",
3738 stats[ST_F_BCK].u.u32 ? "Y" : "-");
3739
3740 /* check failures: unique, fatal, down time */
3741 if (stats[ST_F_CHKFAIL].type) {
Willy Tarreau60600742016-01-11 15:32:30 +01003742 chunk_appendf(out, "<td><u>%lld", (long long)stats[ST_F_CHKFAIL].u.u64);
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003743
3744 if (stats[ST_F_HANAFAIL].type)
Willy Tarreau60600742016-01-11 15:32:30 +01003745 chunk_appendf(out, "/%lld", (long long)stats[ST_F_HANAFAIL].u.u64);
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003746
Willy Tarreau60600742016-01-11 15:32:30 +01003747 chunk_appendf(out,
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003748 "<div class=tips>Failed Health Checks%s</div></u></td>"
3749 "<td>%lld</td><td>%s</td>"
3750 "",
3751 stats[ST_F_HANAFAIL].type ? "/Health Analyses" : "",
3752 (long long)stats[ST_F_CHKDOWN].u.u64, human_time(stats[ST_F_DOWNTIME].u.u32, 1));
3753 }
3754 else if (strcmp(field_str(stats, ST_F_STATUS), "MAINT") != 0 && field_format(stats, ST_F_TRACKED) == FF_STR) {
3755 /* tracking a server (hence inherited maint would appear as "MAINT (via...)" */
Willy Tarreau60600742016-01-11 15:32:30 +01003756 chunk_appendf(out,
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003757 "<td class=ac colspan=3><a class=lfsb href=\"#%s\">via %s</a></td>",
3758 field_str(stats, ST_F_TRACKED), field_str(stats, ST_F_TRACKED));
3759 }
3760 else
Willy Tarreau60600742016-01-11 15:32:30 +01003761 chunk_appendf(out, "<td colspan=3></td>");
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003762
3763 /* throttle */
3764 if (stats[ST_F_THROTTLE].type)
Willy Tarreau60600742016-01-11 15:32:30 +01003765 chunk_appendf(out, "<td class=ac>%d %%</td></tr>\n", stats[ST_F_THROTTLE].u.u32);
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003766 else
Willy Tarreau60600742016-01-11 15:32:30 +01003767 chunk_appendf(out, "<td class=ac>-</td></tr>\n");
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003768 }
Willy Tarreaubbf84502016-01-08 17:25:50 +01003769 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_BE) {
Willy Tarreau60600742016-01-11 15:32:30 +01003770 chunk_appendf(out, "<tr class=\"backend\">");
Willy Tarreau508a63f2016-01-11 15:28:40 +01003771 if (flags & ST_SHOWADMIN) {
Willy Tarreaubbf84502016-01-08 17:25:50 +01003772 /* Column sub-heading for Enable or Disable server */
Willy Tarreau60600742016-01-11 15:32:30 +01003773 chunk_appendf(out, "<td></td>");
Willy Tarreaubbf84502016-01-08 17:25:50 +01003774 }
Willy Tarreau60600742016-01-11 15:32:30 +01003775 chunk_appendf(out,
Willy Tarreaubbf84502016-01-08 17:25:50 +01003776 "<td class=ac>"
3777 /* name */
3778 "%s<a name=\"%s/Backend\"></a>"
3779 "<a class=lfsb href=\"#%s/Backend\">Backend</a>"
3780 "",
3781 (flags & ST_SHLGNDS)?"<u>":"",
3782 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_PXNAME));
3783
3784 if (flags & ST_SHLGNDS) {
3785 /* balancing */
Willy Tarreau60600742016-01-11 15:32:30 +01003786 chunk_appendf(out, "<div class=tips>balancing: %s",
Willy Tarreauf1516d92016-01-11 14:48:36 +01003787 field_str(stats, ST_F_ALGO));
Willy Tarreaubbf84502016-01-08 17:25:50 +01003788
3789 /* cookie */
3790 if (stats[ST_F_COOKIE].type) {
Willy Tarreau60600742016-01-11 15:32:30 +01003791 chunk_appendf(out, ", cookie: '");
Willy Tarreaubbf84502016-01-08 17:25:50 +01003792 chunk_initstr(&src, field_str(stats, ST_F_COOKIE));
Willy Tarreau60600742016-01-11 15:32:30 +01003793 chunk_htmlencode(out, &src);
3794 chunk_appendf(out, "'");
Willy Tarreaubbf84502016-01-08 17:25:50 +01003795 }
Willy Tarreau60600742016-01-11 15:32:30 +01003796 chunk_appendf(out, "</div>");
Willy Tarreaubbf84502016-01-08 17:25:50 +01003797 }
3798
Willy Tarreau60600742016-01-11 15:32:30 +01003799 chunk_appendf(out,
Willy Tarreaubbf84502016-01-08 17:25:50 +01003800 "%s</td>"
3801 /* queue : current, max */
3802 "<td>%s</td><td>%s</td><td></td>"
3803 /* sessions rate : current, max, limit */
3804 "<td>%s</td><td>%s</td><td></td>"
3805 "",
3806 (flags & ST_SHLGNDS)?"</u>":"",
3807 U2H(stats[ST_F_QCUR].u.u32), U2H(stats[ST_F_QMAX].u.u32),
3808 U2H(stats[ST_F_RATE].u.u32), U2H(stats[ST_F_RATE_MAX].u.u32));
3809
Willy Tarreau60600742016-01-11 15:32:30 +01003810 chunk_appendf(out,
Willy Tarreaubbf84502016-01-08 17:25:50 +01003811 /* sessions: current, max, limit, total */
3812 "<td>%s</td><td>%s</td><td>%s</td>"
3813 "<td><u>%s<div class=tips><table class=det>"
3814 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
3815 "",
3816 U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SLIM].u.u32),
3817 U2H(stats[ST_F_STOT].u.u64),
3818 U2H(stats[ST_F_STOT].u.u64));
3819
3820 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
Willy Tarreauf8211df2016-01-11 14:09:38 +01003821 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
Willy Tarreau60600742016-01-11 15:32:30 +01003822 chunk_appendf(out,
Willy Tarreaubbf84502016-01-08 17:25:50 +01003823 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
3824 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
3825 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
3826 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
3827 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
3828 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
3829 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
3830 "<tr><th>- other responses:</th><td>%s</td></tr>"
3831 "<tr><th>Intercepted requests:</th><td>%s</td></tr>"
3832 "<tr><th colspan=3>Avg over last 1024 success. conn.</th></tr>"
3833 "",
3834 U2H(stats[ST_F_REQ_TOT].u.u64),
3835 U2H(stats[ST_F_HRSP_1XX].u.u64),
3836 U2H(stats[ST_F_HRSP_2XX].u.u64),
3837 U2H(stats[ST_F_COMP_RSP].u.u64),
3838 stats[ST_F_HRSP_2XX].u.u64 ?
3839 (int)(100 * stats[ST_F_COMP_RSP].u.u64 / stats[ST_F_HRSP_2XX].u.u64) : 0,
3840 U2H(stats[ST_F_HRSP_3XX].u.u64),
3841 U2H(stats[ST_F_HRSP_4XX].u.u64),
3842 U2H(stats[ST_F_HRSP_5XX].u.u64),
3843 U2H(stats[ST_F_HRSP_OTHER].u.u64),
Willy Tarreau5b9bdff2016-01-11 14:40:47 +01003844 U2H(stats[ST_F_INTERCEPTED].u.u64));
Willy Tarreaubbf84502016-01-08 17:25:50 +01003845 }
3846
Willy Tarreau60600742016-01-11 15:32:30 +01003847 chunk_appendf(out, "<tr><th>- Queue time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_QTIME].u.u32));
3848 chunk_appendf(out, "<tr><th>- Connect time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_QTIME].u.u32));
Willy Tarreauf8211df2016-01-11 14:09:38 +01003849 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
Willy Tarreau60600742016-01-11 15:32:30 +01003850 chunk_appendf(out, "<tr><th>- Response time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_RTIME].u.u32));
3851 chunk_appendf(out, "<tr><th>- Total time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_TTIME].u.u32));
Willy Tarreaubbf84502016-01-08 17:25:50 +01003852
Willy Tarreau60600742016-01-11 15:32:30 +01003853 chunk_appendf(out,
Willy Tarreaubbf84502016-01-08 17:25:50 +01003854 "</table></div></u></td>"
3855 /* sessions: lbtot, last */
3856 "<td>%s</td><td>%s</td>"
3857 /* bytes: in */
3858 "<td>%s</td>"
3859 "",
3860 U2H(stats[ST_F_LBTOT].u.u64),
3861 human_time(stats[ST_F_LASTSESS].u.s32, 1),
3862 U2H(stats[ST_F_BIN].u.u64));
3863
Willy Tarreau60600742016-01-11 15:32:30 +01003864 chunk_appendf(out,
Willy Tarreaubbf84502016-01-08 17:25:50 +01003865 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
3866 "<td>%s%s<div class=tips><table class=det>"
3867 "<tr><th>Response bytes in:</th><td>%s</td></tr>"
3868 "<tr><th>Compression in:</th><td>%s</td></tr>"
3869 "<tr><th>Compression out:</th><td>%s</td><td>(%d%%)</td></tr>"
3870 "<tr><th>Compression bypass:</th><td>%s</td></tr>"
3871 "<tr><th>Total bytes saved:</th><td>%s</td><td>(%d%%)</td></tr>"
3872 "</table></div>%s</td>",
3873 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "<u>":"",
3874 U2H(stats[ST_F_BOUT].u.u64),
3875 U2H(stats[ST_F_BOUT].u.u64),
3876 U2H(stats[ST_F_COMP_IN].u.u64),
3877 U2H(stats[ST_F_COMP_OUT].u.u64),
3878 stats[ST_F_COMP_IN].u.u64 ? (int)(stats[ST_F_COMP_OUT].u.u64 * 100 / stats[ST_F_COMP_IN].u.u64) : 0,
3879 U2H(stats[ST_F_COMP_BYP].u.u64),
Christopher Faulet0b64f622016-04-28 15:09:31 +02003880 U2H(stats[ST_F_COMP_IN].u.u64 - stats[ST_F_COMP_OUT].u.u64),
Willy Tarreaubbf84502016-01-08 17:25:50 +01003881 stats[ST_F_BOUT].u.u64 ? (int)((stats[ST_F_COMP_IN].u.u64 - stats[ST_F_COMP_OUT].u.u64) * 100 / stats[ST_F_BOUT].u.u64) : 0,
3882 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "</u>":"");
3883
Willy Tarreau60600742016-01-11 15:32:30 +01003884 chunk_appendf(out,
Willy Tarreaubbf84502016-01-08 17:25:50 +01003885 /* denied: req, resp */
3886 "<td>%s</td><td>%s</td>"
3887 /* errors : request, connect */
3888 "<td></td><td>%s</td>"
3889 /* errors : response */
3890 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
3891 /* warnings: retries, redispatches */
3892 "<td>%lld</td><td>%lld</td>"
3893 /* backend status: reflect backend status (up/down): we display UP
3894 * if the backend has known working servers or if it has no server at
3895 * all (eg: for stats). Then we display the total weight, number of
3896 * active and backups. */
3897 "<td class=ac>%s %s</td><td class=ac>&nbsp;</td><td class=ac>%d</td>"
3898 "<td class=ac>%d</td><td class=ac>%d</td>"
3899 "",
3900 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
3901 U2H(stats[ST_F_ECON].u.u64),
3902 U2H(stats[ST_F_ERESP].u.u64),
3903 (long long)stats[ST_F_CLI_ABRT].u.u64,
3904 (long long)stats[ST_F_SRV_ABRT].u.u64,
3905 (long long)stats[ST_F_WRETR].u.u64, (long long)stats[ST_F_WREDIS].u.u64,
3906 human_time(stats[ST_F_LASTCHG].u.u32, 1),
3907 strcmp(field_str(stats, ST_F_STATUS), "DOWN") ? field_str(stats, ST_F_STATUS) : "<font color=\"red\"><b>DOWN</b></font>",
3908 stats[ST_F_WEIGHT].u.u32,
3909 stats[ST_F_ACT].u.u32, stats[ST_F_BCK].u.u32);
3910
Willy Tarreau60600742016-01-11 15:32:30 +01003911 chunk_appendf(out,
Willy Tarreaubbf84502016-01-08 17:25:50 +01003912 /* rest of backend: nothing, down transitions, total downtime, throttle */
3913 "<td class=ac>&nbsp;</td><td>%d</td>"
3914 "<td>%s</td>"
3915 "<td></td>"
3916 "</tr>",
3917 stats[ST_F_CHKDOWN].u.u32,
3918 stats[ST_F_DOWNTIME].type ? human_time(stats[ST_F_DOWNTIME].u.u32, 1) : "&nbsp;");
3919 }
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003920 return 1;
3921}
3922
Willy Tarreau501f6022016-01-08 17:37:22 +01003923static int stats_dump_one_line(const struct field *stats, unsigned int flags, struct proxy *px, struct appctx *appctx)
3924{
Willy Tarreau508a63f2016-01-11 15:28:40 +01003925 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN))
3926 flags |= ST_SHOWADMIN;
Willy Tarreau501f6022016-01-08 17:37:22 +01003927
3928 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
Willy Tarreau60600742016-01-11 15:32:30 +01003929 return stats_dump_fields_html(&trash, stats, flags);
Willy Tarreau1e62df92016-01-11 18:57:53 +01003930 else if (appctx->ctx.stats.flags & STAT_FMT_TYPED)
3931 return stats_dump_fields_typed(&trash, stats);
Willy Tarreau501f6022016-01-08 17:37:22 +01003932 else
3933 return stats_dump_fields_csv(&trash, stats);
3934}
3935
Thierry Fournier23d2d642016-03-25 08:20:11 +01003936/* Fill <stats> with the frontend statistics. <stats> is
3937 * preallocated array of length <len>. The length of the array
3938 * must be at least ST_F_TOTAL_FIELDS. If this length is less then
3939 * this value, the function returns 0, otherwise, it returns 1.
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003940 */
Thierry Fournier23d2d642016-03-25 08:20:11 +01003941int stats_fill_fe_stats(struct proxy *px, struct field *stats, int len)
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003942{
Thierry Fournier23d2d642016-03-25 08:20:11 +01003943 if (len < ST_F_TOTAL_FIELDS)
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003944 return 0;
3945
Thierry Fournier23d2d642016-03-25 08:20:11 +01003946 memset(stats, 0, sizeof(*stats) * len);
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003947
3948 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
3949 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, "FRONTEND");
Willy Tarreauf8211df2016-01-11 14:09:38 +01003950 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003951 stats[ST_F_SCUR] = mkf_u32(0, px->feconn);
3952 stats[ST_F_SMAX] = mkf_u32(FN_MAX, px->fe_counters.conn_max);
3953 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, px->maxconn);
3954 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, px->fe_counters.cum_sess);
3955 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, px->fe_counters.bytes_in);
3956 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, px->fe_counters.bytes_out);
3957 stats[ST_F_DREQ] = mkf_u64(FN_COUNTER, px->fe_counters.denied_req);
3958 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, px->fe_counters.denied_resp);
3959 stats[ST_F_EREQ] = mkf_u64(FN_COUNTER, px->fe_counters.failed_req);
3960 stats[ST_F_STATUS] = mkf_str(FO_STATUS, px->state == PR_STREADY ? "OPEN" : px->state == PR_STFULL ? "FULL" : "STOP");
3961 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
3962 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
3963 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, 0);
3964 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_FE);
3965 stats[ST_F_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_sess_per_sec));
3966 stats[ST_F_RATE_LIM] = mkf_u32(FO_CONFIG|FN_LIMIT, px->fe_sps_lim);
3967 stats[ST_F_RATE_MAX] = mkf_u32(FN_MAX, px->fe_counters.sps_max);
3968
3969 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
3970 if (px->mode == PR_MODE_HTTP) {
3971 stats[ST_F_HRSP_1XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[1]);
3972 stats[ST_F_HRSP_2XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[2]);
3973 stats[ST_F_HRSP_3XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[3]);
3974 stats[ST_F_HRSP_4XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[4]);
3975 stats[ST_F_HRSP_5XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[5]);
3976 stats[ST_F_HRSP_OTHER] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[0]);
Willy Tarreau5b9bdff2016-01-11 14:40:47 +01003977 stats[ST_F_INTERCEPTED] = mkf_u64(FN_COUNTER, px->fe_counters.intercepted_req);
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003978 }
3979
3980 /* requests : req_rate, req_rate_max, req_tot, */
3981 stats[ST_F_REQ_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_req_per_sec));
3982 stats[ST_F_REQ_RATE_MAX] = mkf_u32(FN_MAX, px->fe_counters.p.http.rps_max);
3983 stats[ST_F_REQ_TOT] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cum_req);
3984
3985 /* compression: in, out, bypassed, responses */
3986 stats[ST_F_COMP_IN] = mkf_u64(FN_COUNTER, px->fe_counters.comp_in);
3987 stats[ST_F_COMP_OUT] = mkf_u64(FN_COUNTER, px->fe_counters.comp_out);
3988 stats[ST_F_COMP_BYP] = mkf_u64(FN_COUNTER, px->fe_counters.comp_byp);
3989 stats[ST_F_COMP_RSP] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.comp_rsp);
3990
Willy Tarreauc73810f2016-01-11 13:52:04 +01003991 /* connections : conn_rate, conn_rate_max, conn_tot, conn_max */
3992 stats[ST_F_CONN_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_conn_per_sec));
3993 stats[ST_F_CONN_RATE_MAX] = mkf_u32(FN_MAX, px->fe_counters.cps_max);
3994 stats[ST_F_CONN_TOT] = mkf_u64(FN_COUNTER, px->fe_counters.cum_conn);
3995
Thierry Fournier23d2d642016-03-25 08:20:11 +01003996 return 1;
3997}
3998
3999/* Dumps a frontend's line to the trash for the current proxy <px> and uses
4000 * the state from stream interface <si>. The caller is responsible for clearing
4001 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
4002 */
4003static int stats_dump_fe_stats(struct stream_interface *si, struct proxy *px)
4004{
4005 struct appctx *appctx = __objt_appctx(si->end);
4006
4007 if (!(px->cap & PR_CAP_FE))
4008 return 0;
4009
4010 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_FE)))
4011 return 0;
4012
4013 if (!stats_fill_fe_stats(px, stats, ST_F_TOTAL_FIELDS))
4014 return 0;
4015
Willy Tarreau501f6022016-01-08 17:37:22 +01004016 return stats_dump_one_line(stats, 0, px, appctx);
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004017}
4018
Thierry Fournierc4456852016-03-25 08:20:49 +01004019/* Fill <stats> with the listener statistics. <stats> is
4020 * preallocated array of length <len>. The length of the array
4021 * must be at least ST_F_TOTAL_FIELDS. If this length is less
4022 * then this value, the function returns 0, otherwise, it
4023 * returns 1. <flags> can take the value ST_SHLGNDS.
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004024 */
Thierry Fournierc4456852016-03-25 08:20:49 +01004025int stats_fill_li_stats(struct proxy *px, struct listener *l, int flags,
4026 struct field *stats, int len)
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004027{
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004028 struct chunk *out = get_trash_chunk();
4029
Thierry Fournierc4456852016-03-25 08:20:49 +01004030 if (len < ST_F_TOTAL_FIELDS)
4031 return 0;
4032
Thierry Fournierac9d4672016-03-25 08:43:46 +01004033 if (!l->counters)
4034 return 0;
4035
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004036 chunk_reset(out);
Thierry Fournierc4456852016-03-25 08:20:49 +01004037 memset(stats, 0, sizeof(*stats) * len);
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004038
4039 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
4040 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, l->name);
Willy Tarreauf8211df2016-01-11 14:09:38 +01004041 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004042 stats[ST_F_SCUR] = mkf_u32(0, l->nbconn);
4043 stats[ST_F_SMAX] = mkf_u32(FN_MAX, l->counters->conn_max);
4044 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, l->maxconn);
4045 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, l->counters->cum_conn);
4046 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, l->counters->bytes_in);
4047 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, l->counters->bytes_out);
4048 stats[ST_F_DREQ] = mkf_u64(FN_COUNTER, l->counters->denied_req);
4049 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, l->counters->denied_resp);
4050 stats[ST_F_EREQ] = mkf_u64(FN_COUNTER, l->counters->failed_req);
4051 stats[ST_F_STATUS] = mkf_str(FO_STATUS, (l->nbconn < l->maxconn) ? (l->state == LI_LIMITED) ? "WAITING" : "OPEN" : "FULL");
4052 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
4053 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
4054 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, l->luid);
4055 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_SO);
4056
4057 if (flags & ST_SHLGNDS) {
4058 char str[INET6_ADDRSTRLEN];
4059 int port;
4060
4061 port = get_host_port(&l->addr);
4062 switch (addr_to_str(&l->addr, str, sizeof(str))) {
4063 case AF_INET:
4064 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
4065 chunk_appendf(out, "%s:%d", str, port);
4066 break;
4067 case AF_INET6:
4068 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
4069 chunk_appendf(out, "[%s]:%d", str, port);
4070 break;
4071 case AF_UNIX:
4072 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, "unix");
4073 break;
4074 case -1:
4075 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
4076 chunk_strcat(out, strerror(errno));
4077 break;
4078 default: /* address family not supported */
4079 break;
4080 }
4081 }
4082
Thierry Fournierc4456852016-03-25 08:20:49 +01004083 return 1;
4084}
4085
4086/* Dumps a line for listener <l> and proxy <px> to the trash and uses the state
4087 * from stream interface <si>, and stats flags <flags>. The caller is responsible
4088 * for clearing the trash if needed. Returns non-zero if it emits anything, zero
4089 * otherwise.
4090 */
4091static int stats_dump_li_stats(struct stream_interface *si, struct proxy *px, struct listener *l, int flags)
4092{
4093 struct appctx *appctx = __objt_appctx(si->end);
4094
4095 if (!stats_fill_li_stats(px, l, flags, stats, ST_F_TOTAL_FIELDS))
4096 return 0;
4097
Willy Tarreau501f6022016-01-08 17:37:22 +01004098 return stats_dump_one_line(stats, flags, px, appctx);
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004099}
4100
4101enum srv_stats_state {
4102 SRV_STATS_STATE_DOWN = 0,
4103 SRV_STATS_STATE_DOWN_AGENT,
4104 SRV_STATS_STATE_GOING_UP,
4105 SRV_STATS_STATE_UP_GOING_DOWN,
4106 SRV_STATS_STATE_UP,
4107 SRV_STATS_STATE_NOLB_GOING_DOWN,
Simon Horman4d2eab62015-04-23 14:51:26 +09004108 SRV_STATS_STATE_NOLB,
4109 SRV_STATS_STATE_DRAIN_GOING_DOWN,
4110 SRV_STATS_STATE_DRAIN,
Simon Hormanb167b6b2015-04-23 14:51:29 +09004111 SRV_STATS_STATE_DRAIN_AGENT,
Simon Horman4d2eab62015-04-23 14:51:26 +09004112 SRV_STATS_STATE_NO_CHECK,
4113
4114 SRV_STATS_STATE_COUNT, /* Must be last */
4115};
4116
Willy Tarreau2b96cf12016-01-06 19:25:38 +01004117static const char *srv_hlt_st[SRV_STATS_STATE_COUNT] = {
4118 [SRV_STATS_STATE_DOWN] = "DOWN",
4119 [SRV_STATS_STATE_DOWN_AGENT] = "DOWN (agent)",
4120 [SRV_STATS_STATE_GOING_UP] = "DOWN %d/%d",
4121 [SRV_STATS_STATE_UP_GOING_DOWN] = "UP %d/%d",
4122 [SRV_STATS_STATE_UP] = "UP",
4123 [SRV_STATS_STATE_NOLB_GOING_DOWN] = "NOLB %d/%d",
4124 [SRV_STATS_STATE_NOLB] = "NOLB",
4125 [SRV_STATS_STATE_DRAIN_GOING_DOWN] = "DRAIN %d/%d",
4126 [SRV_STATS_STATE_DRAIN] = "DRAIN",
4127 [SRV_STATS_STATE_DRAIN_AGENT] = "DRAIN (agent)",
4128 [SRV_STATS_STATE_NO_CHECK] = "no check"
4129};
4130
Thierry Fournier61fe6c02016-03-25 08:21:21 +01004131/* Fill <stats> with the server statistics. <stats> is
4132 * preallocated array of length <len>. The length of the array
4133 * must be at least ST_F_TOTAL_FIELDS. If this length is less
4134 * then this value, the function returns 0, otherwise, it
4135 * returns 1. <flags> can take the value ST_SHLGNDS.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004136 */
Thierry Fournier61fe6c02016-03-25 08:21:21 +01004137int stats_fill_sv_stats(struct proxy *px, struct server *sv, int flags,
4138 struct field *stats, int len)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004139{
Willy Tarreau32091232014-05-16 13:52:00 +02004140 struct server *via, *ref;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004141 char str[INET6_ADDRSTRLEN];
Willy Tarreau2b96cf12016-01-06 19:25:38 +01004142 struct chunk *out = get_trash_chunk();
Willy Tarreauba2f2642016-01-07 09:54:40 +01004143 enum srv_stats_state state;
Willy Tarreau2b96cf12016-01-06 19:25:38 +01004144 char *fld_status;
Thierry Fournier61fe6c02016-03-25 08:21:21 +01004145
4146 if (len < ST_F_TOTAL_FIELDS)
4147 return 0;
4148
4149 memset(stats, 0, sizeof(*stats) * len);
4150
Willy Tarreau32091232014-05-16 13:52:00 +02004151 /* we have "via" which is the tracked server as described in the configuration,
4152 * and "ref" which is the checked server and the end of the chain.
4153 */
4154 via = sv->track ? sv->track : sv;
4155 ref = via;
4156 while (ref->track)
4157 ref = ref->track;
4158
Willy Tarreauba2f2642016-01-07 09:54:40 +01004159 if (sv->state == SRV_ST_RUNNING || sv->state == SRV_ST_STARTING) {
4160 if ((ref->check.state & CHK_ST_ENABLED) &&
4161 (ref->check.health < ref->check.rise + ref->check.fall - 1)) {
4162 state = SRV_STATS_STATE_UP_GOING_DOWN;
Willy Tarreauba2f2642016-01-07 09:54:40 +01004163 } else {
4164 state = SRV_STATS_STATE_UP;
Willy Tarreauba2f2642016-01-07 09:54:40 +01004165 }
4166
Willy Tarreauba2f2642016-01-07 09:54:40 +01004167 if (sv->admin & SRV_ADMF_DRAIN) {
4168 if (ref->agent.state & CHK_ST_ENABLED)
4169 state = SRV_STATS_STATE_DRAIN_AGENT;
4170 else if (state == SRV_STATS_STATE_UP_GOING_DOWN)
4171 state = SRV_STATS_STATE_DRAIN_GOING_DOWN;
4172 else
4173 state = SRV_STATS_STATE_DRAIN;
4174 }
4175
4176 if (state == SRV_STATS_STATE_UP && !(ref->check.state & CHK_ST_ENABLED)) {
4177 state = SRV_STATS_STATE_NO_CHECK;
Willy Tarreauba2f2642016-01-07 09:54:40 +01004178 }
4179 }
4180 else if (sv->state == SRV_ST_STOPPING) {
4181 if ((!(sv->check.state & CHK_ST_ENABLED) && !sv->track) ||
4182 (ref->check.health == ref->check.rise + ref->check.fall - 1)) {
4183 state = SRV_STATS_STATE_NOLB;
Willy Tarreauba2f2642016-01-07 09:54:40 +01004184 } else {
4185 state = SRV_STATS_STATE_NOLB_GOING_DOWN;
Willy Tarreauba2f2642016-01-07 09:54:40 +01004186 }
4187 }
4188 else { /* stopped */
4189 if ((ref->agent.state & CHK_ST_ENABLED) && !ref->agent.health) {
4190 state = SRV_STATS_STATE_DOWN_AGENT;
Willy Tarreauba2f2642016-01-07 09:54:40 +01004191 } else if ((ref->check.state & CHK_ST_ENABLED) && !ref->check.health) {
4192 state = SRV_STATS_STATE_DOWN; /* DOWN */
Willy Tarreauba2f2642016-01-07 09:54:40 +01004193 } else if ((ref->agent.state & CHK_ST_ENABLED) || (ref->check.state & CHK_ST_ENABLED)) {
4194 state = SRV_STATS_STATE_GOING_UP;
Willy Tarreauba2f2642016-01-07 09:54:40 +01004195 } else {
4196 state = SRV_STATS_STATE_DOWN; /* DOWN, unchecked */
Willy Tarreauba2f2642016-01-07 09:54:40 +01004197 }
4198 }
4199
Willy Tarreau2b96cf12016-01-06 19:25:38 +01004200 chunk_reset(out);
Willy Tarreau2b96cf12016-01-06 19:25:38 +01004201
4202 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
4203 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, sv->id);
Willy Tarreauf8211df2016-01-11 14:09:38 +01004204 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
Willy Tarreau2b96cf12016-01-06 19:25:38 +01004205 stats[ST_F_QCUR] = mkf_u32(0, sv->nbpend);
4206 stats[ST_F_QMAX] = mkf_u32(FN_MAX, sv->counters.nbpend_max);
4207 stats[ST_F_SCUR] = mkf_u32(0, sv->cur_sess);
4208 stats[ST_F_SMAX] = mkf_u32(FN_MAX, sv->counters.cur_sess_max);
4209
4210 if (sv->maxconn)
4211 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, sv->maxconn);
4212
4213 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, sv->counters.cum_sess);
4214 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, sv->counters.bytes_in);
4215 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, sv->counters.bytes_out);
4216 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, sv->counters.failed_secu);
4217 stats[ST_F_ECON] = mkf_u64(FN_COUNTER, sv->counters.failed_conns);
4218 stats[ST_F_ERESP] = mkf_u64(FN_COUNTER, sv->counters.failed_resp);
4219 stats[ST_F_WRETR] = mkf_u64(FN_COUNTER, sv->counters.retries);
4220 stats[ST_F_WREDIS] = mkf_u64(FN_COUNTER, sv->counters.redispatches);
4221
4222 /* status */
4223 fld_status = chunk_newstr(out);
4224 if (sv->admin & SRV_ADMF_IMAINT)
4225 chunk_appendf(out, "MAINT (via %s/%s)", via->proxy->id, via->id);
4226 else if (sv->admin & SRV_ADMF_MAINT)
4227 chunk_appendf(out, "MAINT");
4228 else
4229 chunk_appendf(out,
4230 srv_hlt_st[state],
4231 (ref->state != SRV_ST_STOPPED) ? (ref->check.health - ref->check.rise + 1) : (ref->check.health),
4232 (ref->state != SRV_ST_STOPPED) ? (ref->check.fall) : (ref->check.rise));
4233
4234 stats[ST_F_STATUS] = mkf_str(FO_STATUS, fld_status);
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004235 stats[ST_F_LASTCHG] = mkf_u32(FN_AGE, now.tv_sec - sv->last_change);
4236 stats[ST_F_WEIGHT] = mkf_u32(FN_AVG, (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv);
4237 stats[ST_F_ACT] = mkf_u32(FO_STATUS, (sv->flags & SRV_F_BACKUP) ? 0 : 1);
4238 stats[ST_F_BCK] = mkf_u32(FO_STATUS, (sv->flags & SRV_F_BACKUP) ? 1 : 0);
Willy Tarreau91861262007-10-17 17:06:05 +02004239
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004240 /* check failures: unique, fatal; last change, total downtime */
4241 if (sv->check.state & CHK_ST_ENABLED) {
4242 stats[ST_F_CHKFAIL] = mkf_u64(FN_COUNTER, sv->counters.failed_checks);
4243 stats[ST_F_CHKDOWN] = mkf_u64(FN_COUNTER, sv->counters.down_trans);
4244 stats[ST_F_DOWNTIME] = mkf_u32(FN_COUNTER, srv_downtime(sv));
4245 }
Willy Tarreau164d4a92016-01-06 19:48:21 +01004246
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004247 if (sv->maxqueue)
4248 stats[ST_F_QLIMIT] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->maxqueue);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004249
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004250 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
4251 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
4252 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, sv->puid);
Willy Tarreau91861262007-10-17 17:06:05 +02004253
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004254 if (sv->state == SRV_ST_STARTING && !server_is_draining(sv))
4255 stats[ST_F_THROTTLE] = mkf_u32(FN_AVG, server_throttle_rate(sv));
Willy Tarreauf5b1cc32014-06-17 12:20:59 +02004256
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004257 stats[ST_F_LBTOT] = mkf_u64(FN_COUNTER, sv->counters.cum_lbconn);
Willy Tarreau91861262007-10-17 17:06:05 +02004258
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004259 if (sv->track) {
4260 char *fld_track = chunk_newstr(out);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004261
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004262 chunk_appendf(out, "%s/%s", sv->track->proxy->id, sv->track->id);
4263 stats[ST_F_TRACKED] = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, fld_track);
4264 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004265
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004266 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_SV);
4267 stats[ST_F_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&sv->sess_per_sec));
4268 stats[ST_F_RATE_MAX] = mkf_u32(FN_MAX, sv->counters.sps_max);
Willy Tarreau164d4a92016-01-06 19:48:21 +01004269
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004270 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) {
4271 const char *fld_chksts;
Willy Tarreau164d4a92016-01-06 19:48:21 +01004272
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004273 fld_chksts = chunk_newstr(out);
4274 chunk_strcat(out, "* "); // for check in progress
4275 chunk_strcat(out, get_check_status_info(sv->check.status));
4276 if (!(sv->check.state & CHK_ST_INPROGRESS))
4277 fld_chksts += 2; // skip "* "
4278 stats[ST_F_CHECK_STATUS] = mkf_str(FN_OUTPUT, fld_chksts);
Willy Tarreau91861262007-10-17 17:06:05 +02004279
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004280 if (sv->check.status >= HCHK_STATUS_L57DATA)
4281 stats[ST_F_CHECK_CODE] = mkf_u32(FN_OUTPUT, sv->check.code);
Willy Tarreaucf2924b2014-05-23 12:15:15 +02004282
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004283 if (sv->check.status >= HCHK_STATUS_CHECKED)
4284 stats[ST_F_CHECK_DURATION] = mkf_u64(FN_DURATION, sv->check.duration);
Willy Tarreaucf2924b2014-05-23 12:15:15 +02004285
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004286 stats[ST_F_CHECK_DESC] = mkf_str(FN_OUTPUT, get_check_status_description(sv->check.status));
4287 stats[ST_F_LAST_CHK] = mkf_str(FN_OUTPUT, sv->check.desc);
4288 stats[ST_F_CHECK_RISE] = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.rise);
4289 stats[ST_F_CHECK_FALL] = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.fall);
4290 stats[ST_F_CHECK_HEALTH] = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.health);
4291 }
Willy Tarreaucf2924b2014-05-23 12:15:15 +02004292
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004293 if ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) {
4294 const char *fld_chksts;
Willy Tarreaudd7354b2016-01-08 13:47:26 +01004295
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004296 fld_chksts = chunk_newstr(out);
4297 chunk_strcat(out, "* "); // for check in progress
4298 chunk_strcat(out, get_check_status_info(sv->agent.status));
4299 if (!(sv->agent.state & CHK_ST_INPROGRESS))
4300 fld_chksts += 2; // skip "* "
4301 stats[ST_F_AGENT_STATUS] = mkf_str(FN_OUTPUT, fld_chksts);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004302
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004303 if (sv->agent.status >= HCHK_STATUS_L57DATA)
4304 stats[ST_F_AGENT_CODE] = mkf_u32(FN_OUTPUT, sv->agent.code);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004305
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004306 if (sv->agent.status >= HCHK_STATUS_CHECKED)
4307 stats[ST_F_AGENT_DURATION] = mkf_u64(FN_DURATION, sv->agent.duration);
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01004308
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004309 stats[ST_F_AGENT_DESC] = mkf_str(FN_OUTPUT, get_check_status_description(sv->agent.status));
4310 stats[ST_F_LAST_AGT] = mkf_str(FN_OUTPUT, sv->agent.desc);
4311 stats[ST_F_AGENT_RISE] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.rise);
4312 stats[ST_F_AGENT_FALL] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.fall);
4313 stats[ST_F_AGENT_HEALTH] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.health);
4314 }
Willy Tarreaudd7354b2016-01-08 13:47:26 +01004315
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004316 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
4317 if (px->mode == PR_MODE_HTTP) {
4318 stats[ST_F_HRSP_1XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[1]);
4319 stats[ST_F_HRSP_2XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[2]);
4320 stats[ST_F_HRSP_3XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[3]);
4321 stats[ST_F_HRSP_4XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[4]);
4322 stats[ST_F_HRSP_5XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[5]);
4323 stats[ST_F_HRSP_OTHER] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[0]);
4324 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004325
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004326 if (ref->observe)
4327 stats[ST_F_HANAFAIL] = mkf_u64(FN_COUNTER, sv->counters.failed_hana);
Willy Tarreau164d4a92016-01-06 19:48:21 +01004328
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004329 stats[ST_F_CLI_ABRT] = mkf_u64(FN_COUNTER, sv->counters.cli_aborts);
4330 stats[ST_F_SRV_ABRT] = mkf_u64(FN_COUNTER, sv->counters.srv_aborts);
4331 stats[ST_F_LASTSESS] = mkf_s32(FN_AGE, srv_lastsession(sv));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004332
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004333 stats[ST_F_QTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.q_time, TIME_STATS_SAMPLES));
4334 stats[ST_F_CTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.c_time, TIME_STATS_SAMPLES));
4335 stats[ST_F_RTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.d_time, TIME_STATS_SAMPLES));
4336 stats[ST_F_TTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.t_time, TIME_STATS_SAMPLES));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004337
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004338 if (flags & ST_SHLGNDS) {
4339 switch (addr_to_str(&sv->addr, str, sizeof(str))) {
4340 case AF_INET:
4341 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
4342 chunk_appendf(out, "%s:%d", str, get_host_port(&sv->addr));
4343 break;
4344 case AF_INET6:
4345 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
4346 chunk_appendf(out, "[%s]:%d", str, get_host_port(&sv->addr));
4347 break;
4348 case AF_UNIX:
4349 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, "unix");
4350 break;
4351 case -1:
4352 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
4353 chunk_strcat(out, strerror(errno));
4354 break;
4355 default: /* address family not supported */
4356 break;
Willy Tarreauf4659942013-11-28 10:50:06 +01004357 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004358
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004359 if (sv->cookie)
4360 stats[ST_F_COOKIE] = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, sv->cookie);
4361 }
4362
Thierry Fournier61fe6c02016-03-25 08:21:21 +01004363 return 1;
4364}
4365
4366/* Dumps a line for server <sv> and proxy <px> to the trash and uses the state
4367 * from stream interface <si>, stats flags <flags>, and server state <state>.
4368 * The caller is responsible for clearing the trash if needed. Returns non-zero
4369 * if it emits anything, zero otherwise.
4370 */
4371static int stats_dump_sv_stats(struct stream_interface *si, struct proxy *px, int flags, struct server *sv)
4372{
4373 struct appctx *appctx = __objt_appctx(si->end);
4374
4375 if (!stats_fill_sv_stats(px, sv, flags, stats, ST_F_TOTAL_FIELDS))
4376 return 0;
4377
Willy Tarreau501f6022016-01-08 17:37:22 +01004378 return stats_dump_one_line(stats, flags, px, appctx);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004379}
4380
Thierry Fournierd0a56c22016-03-25 08:21:51 +01004381/* Fill <stats> with the backend statistics. <stats> is
4382 * preallocated array of length <len>. The length of the array
4383 * must be at least ST_F_TOTAL_FIELDS. If this length is less
4384 * then this value, the function returns 0, otherwise, it
4385 * returns 1. <flags> can take the value ST_SHLGNDS.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004386 */
Thierry Fournierd0a56c22016-03-25 08:21:51 +01004387int stats_fill_be_stats(struct proxy *px, int flags, struct field *stats, int len)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004388{
Thierry Fournierd0a56c22016-03-25 08:21:51 +01004389 if (len < ST_F_TOTAL_FIELDS)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004390 return 0;
4391
Thierry Fournierd0a56c22016-03-25 08:21:51 +01004392 memset(stats, 0, sizeof(*stats) * len);
Willy Tarreauf6eecbe2016-01-06 16:14:50 +01004393
4394 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
4395 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, "BACKEND");
Willy Tarreauf8211df2016-01-11 14:09:38 +01004396 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
Willy Tarreauf6eecbe2016-01-06 16:14:50 +01004397 stats[ST_F_QCUR] = mkf_u32(0, px->nbpend);
4398 stats[ST_F_QMAX] = mkf_u32(FN_MAX, px->be_counters.nbpend_max);
4399 stats[ST_F_SCUR] = mkf_u32(FO_CONFIG|FN_LIMIT, px->beconn);
4400 stats[ST_F_SMAX] = mkf_u32(FN_MAX, px->be_counters.conn_max);
4401 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, px->fullconn);
4402 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, px->be_counters.cum_conn);
4403 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, px->be_counters.bytes_in);
4404 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, px->be_counters.bytes_out);
4405 stats[ST_F_DREQ] = mkf_u64(FN_COUNTER, px->be_counters.denied_req);
4406 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, px->be_counters.denied_resp);
4407 stats[ST_F_ECON] = mkf_u64(FN_COUNTER, px->be_counters.failed_conns);
4408 stats[ST_F_ERESP] = mkf_u64(FN_COUNTER, px->be_counters.failed_resp);
4409 stats[ST_F_WRETR] = mkf_u64(FN_COUNTER, px->be_counters.retries);
4410 stats[ST_F_WREDIS] = mkf_u64(FN_COUNTER, px->be_counters.redispatches);
4411 stats[ST_F_STATUS] = mkf_str(FO_STATUS, (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" : "DOWN");
4412 stats[ST_F_WEIGHT] = mkf_u32(FN_AVG, (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv);
4413 stats[ST_F_ACT] = mkf_u32(0, px->srv_act);
4414 stats[ST_F_BCK] = mkf_u32(0, px->srv_bck);
4415 stats[ST_F_CHKDOWN] = mkf_u64(FN_COUNTER, px->down_trans);
4416 stats[ST_F_LASTCHG] = mkf_u32(FN_AGE, now.tv_sec - px->last_change);
Willy Tarreau7344f472016-01-11 12:04:02 +01004417 if (px->srv)
4418 stats[ST_F_DOWNTIME] = mkf_u32(FN_COUNTER, be_downtime(px));
4419
Willy Tarreauf6eecbe2016-01-06 16:14:50 +01004420 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
4421 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
4422 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, 0);
4423 stats[ST_F_LBTOT] = mkf_u64(FN_COUNTER, px->be_counters.cum_lbconn);
4424 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_BE);
4425 stats[ST_F_RATE] = mkf_u32(0, read_freq_ctr(&px->be_sess_per_sec));
4426 stats[ST_F_RATE_MAX] = mkf_u32(0, px->be_counters.sps_max);
Willy Tarreauf1516d92016-01-11 14:48:36 +01004427
Willy Tarreaue4847c62016-01-08 15:43:54 +01004428 if (flags & ST_SHLGNDS) {
4429 if (px->cookie_name)
4430 stats[ST_F_COOKIE] = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, px->cookie_name);
Willy Tarreauf1516d92016-01-11 14:48:36 +01004431 stats[ST_F_ALGO] = mkf_str(FO_CONFIG|FS_SERVICE, backend_lb_algo_str(px->lbprm.algo & BE_LB_ALGO));
Willy Tarreaue4847c62016-01-08 15:43:54 +01004432 }
Willy Tarreauf6eecbe2016-01-06 16:14:50 +01004433
4434 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
4435 if (px->mode == PR_MODE_HTTP) {
4436 stats[ST_F_REQ_TOT] = mkf_u64(FN_COUNTER, px->be_counters.p.http.cum_req);
4437 stats[ST_F_HRSP_1XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[1]);
4438 stats[ST_F_HRSP_2XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[2]);
4439 stats[ST_F_HRSP_3XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[3]);
4440 stats[ST_F_HRSP_4XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[4]);
4441 stats[ST_F_HRSP_5XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[5]);
4442 stats[ST_F_HRSP_OTHER] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[0]);
Willy Tarreau5b9bdff2016-01-11 14:40:47 +01004443 stats[ST_F_INTERCEPTED] = mkf_u64(FN_COUNTER, px->be_counters.intercepted_req);
Willy Tarreauf6eecbe2016-01-06 16:14:50 +01004444 }
4445
4446 stats[ST_F_CLI_ABRT] = mkf_u64(FN_COUNTER, px->be_counters.cli_aborts);
4447 stats[ST_F_SRV_ABRT] = mkf_u64(FN_COUNTER, px->be_counters.srv_aborts);
4448
4449 /* compression: in, out, bypassed, responses */
4450 stats[ST_F_COMP_IN] = mkf_u64(FN_COUNTER, px->be_counters.comp_in);
4451 stats[ST_F_COMP_OUT] = mkf_u64(FN_COUNTER, px->be_counters.comp_out);
4452 stats[ST_F_COMP_BYP] = mkf_u64(FN_COUNTER, px->be_counters.comp_byp);
4453 stats[ST_F_COMP_RSP] = mkf_u64(FN_COUNTER, px->be_counters.p.http.comp_rsp);
4454 stats[ST_F_LASTSESS] = mkf_s32(FN_AGE, be_lastsession(px));
4455
4456 stats[ST_F_QTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.q_time, TIME_STATS_SAMPLES));
4457 stats[ST_F_CTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.c_time, TIME_STATS_SAMPLES));
4458 stats[ST_F_RTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.d_time, TIME_STATS_SAMPLES));
4459 stats[ST_F_TTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.t_time, TIME_STATS_SAMPLES));
4460
Thierry Fournierd0a56c22016-03-25 08:21:51 +01004461 return 1;
4462}
4463
4464/* Dumps a line for backend <px> to the trash for and uses the state from stream
4465 * interface <si> and stats flags <flags>. The caller is responsible for clearing
4466 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
4467 */
4468static int stats_dump_be_stats(struct stream_interface *si, struct proxy *px, int flags)
4469{
4470 struct appctx *appctx = __objt_appctx(si->end);
4471
4472 if (!(px->cap & PR_CAP_BE))
4473 return 0;
4474
4475 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_BE)))
4476 return 0;
4477
4478 if (!stats_fill_be_stats(px, flags, stats, ST_F_TOTAL_FIELDS))
4479 return 0;
4480
Willy Tarreau501f6022016-01-08 17:37:22 +01004481 return stats_dump_one_line(stats, flags, px, appctx);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004482}
4483
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004484/* Dumps the HTML table header for proxy <px> to the trash for and uses the state from
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004485 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004486 * for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004487 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004488static void stats_dump_html_px_hdr(struct stream_interface *si, struct proxy *px, struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004489{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004490 struct appctx *appctx = __objt_appctx(si->end);
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004491 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
4492
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004493 if (px->cap & PR_CAP_BE && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004494 /* A form to enable/disable this proxy servers */
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004495
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004496 /* 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 +02004497 scope_txt[0] = 0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004498 if (appctx->ctx.stats.scope_len) {
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004499 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau4e4292b2014-11-28 12:18:45 +01004500 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 +01004501 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004502 }
4503
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004504 chunk_appendf(&trash,
Jeff Buchbinder2dbbf4d2014-08-29 15:10:08 -05004505 "<form method=\"post\">");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004506 }
4507
4508 /* print a new table */
4509 chunk_appendf(&trash,
4510 "<table class=\"tbl\" width=\"100%%\">\n"
4511 "<tr class=\"titre\">"
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01004512 "<th class=\"pxname\" width=\"10%%\">");
4513
4514 chunk_appendf(&trash,
4515 "<a name=\"%s\"></a>%s"
4516 "<a class=px href=\"#%s\">%s</a>",
4517 px->id,
4518 (uri->flags & ST_SHLGNDS) ? "<u>":"",
4519 px->id, px->id);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004520
4521 if (uri->flags & ST_SHLGNDS) {
4522 /* cap, mode, id */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02004523 chunk_appendf(&trash, "<div class=tips>cap: %s, mode: %s, id: %d",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004524 proxy_cap_str(px->cap), proxy_mode_str(px->mode),
4525 px->uuid);
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01004526 chunk_appendf(&trash, "</div>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004527 }
4528
4529 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01004530 "%s</th>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004531 "<th class=\"%s\" width=\"90%%\">%s</th>"
4532 "</tr>\n"
4533 "</table>\n"
4534 "<table class=\"tbl\" width=\"100%%\">\n"
4535 "<tr class=\"titre\">",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004536 (uri->flags & ST_SHLGNDS) ? "</u>":"",
4537 px->desc ? "desc" : "empty", px->desc ? px->desc : "");
4538
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004539 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004540 /* Column heading for Enable or Disable server */
4541 chunk_appendf(&trash, "<th rowspan=2 width=1></th>");
Willy Tarreau91861262007-10-17 17:06:05 +02004542 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004543
4544 chunk_appendf(&trash,
4545 "<th rowspan=2></th>"
4546 "<th colspan=3>Queue</th>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05004547 "<th colspan=3>Session rate</th><th colspan=6>Sessions</th>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004548 "<th colspan=2>Bytes</th><th colspan=2>Denied</th>"
4549 "<th colspan=3>Errors</th><th colspan=2>Warnings</th>"
4550 "<th colspan=9>Server</th>"
4551 "</tr>\n"
4552 "<tr class=\"titre\">"
4553 "<th>Cur</th><th>Max</th><th>Limit</th>"
4554 "<th>Cur</th><th>Max</th><th>Limit</th><th>Cur</th><th>Max</th>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05004555 "<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 +01004556 "<th>Req</th><th>Resp</th><th>Req</th><th>Conn</th>"
4557 "<th>Resp</th><th>Retr</th><th>Redis</th>"
4558 "<th>Status</th><th>LastChk</th><th>Wght</th><th>Act</th>"
4559 "<th>Bck</th><th>Chk</th><th>Dwn</th><th>Dwntme</th>"
4560 "<th>Thrtle</th>\n"
4561 "</tr>");
Willy Tarreau91861262007-10-17 17:06:05 +02004562}
4563
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004564/* Dumps the HTML table trailer for proxy <px> to the trash for and uses the state from
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004565 * stream interface <si>. The caller is responsible for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004566 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004567static void stats_dump_html_px_end(struct stream_interface *si, struct proxy *px)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004568{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004569 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004570 chunk_appendf(&trash, "</table>");
4571
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004572 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004573 /* close the form used to enable/disable this proxy servers */
4574 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004575 "Choose the action to perform on the checked servers : "
4576 "<select name=action>"
4577 "<option value=\"\"></option>"
Willy Tarreaued7df902014-05-22 18:04:49 +02004578 "<option value=\"ready\">Set state to READY</option>"
4579 "<option value=\"drain\">Set state to DRAIN</option>"
Marco Corte8c27bca2014-07-02 17:49:34 +02004580 "<option value=\"maint\">Set state to MAINT</option>"
Willy Tarreau248a60e2014-05-23 14:59:48 +02004581 "<option value=\"dhlth\">Health: disable checks</option>"
4582 "<option value=\"ehlth\">Health: enable checks</option>"
4583 "<option value=\"hrunn\">Health: force UP</option>"
4584 "<option value=\"hnolb\">Health: force NOLB</option>"
4585 "<option value=\"hdown\">Health: force DOWN</option>"
4586 "<option value=\"dagent\">Agent: disable checks</option>"
4587 "<option value=\"eagent\">Agent: enable checks</option>"
4588 "<option value=\"arunn\">Agent: force UP</option>"
4589 "<option value=\"adown\">Agent: force DOWN</option>"
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004590 "<option value=\"shutdown\">Kill Sessions</option>"
4591 "</select>"
4592 "<input type=\"hidden\" name=\"b\" value=\"#%d\">"
4593 "&nbsp;<input type=\"submit\" value=\"Apply\">"
4594 "</form>",
4595 px->uuid);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004596 }
4597
4598 chunk_appendf(&trash, "<p>\n");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004599}
Willy Tarreau91861262007-10-17 17:06:05 +02004600
4601/*
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004602 * Dumps statistics for a proxy. The output is sent to the stream interface's
4603 * input buffer. Returns 0 if it had to stop dumping data because of lack of
4604 * buffer space, or non-zero if everything completed. This function is used
4605 * both by the CLI and the HTTP entry points, and is able to dump the output
4606 * in HTML or CSV formats. If the later, <uri> must be NULL.
Willy Tarreau91861262007-10-17 17:06:05 +02004607 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004608static int stats_dump_proxy_to_buffer(struct stream_interface *si, struct proxy *px, struct uri_auth *uri)
Willy Tarreau91861262007-10-17 17:06:05 +02004609{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004610 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau87b09662015-04-03 00:22:06 +02004611 struct stream *s = si_strm(si);
Willy Tarreau2bb4a962014-11-28 11:11:05 +01004612 struct channel *rep = si_ic(si);
Willy Tarreau44267702011-10-28 15:35:33 +02004613 struct server *sv, *svs; /* server and server-state, server-state=server or server->track */
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004614 struct listener *l;
Willy Tarreau0deb85a2016-01-08 15:33:18 +01004615 unsigned int flags;
4616
4617 if (uri)
4618 flags = uri->flags;
4619 else if (strm_li(s)->bind_conf->level >= ACCESS_LVL_OPER)
4620 flags = ST_SHLGNDS | ST_SHNODE | ST_SHDESC;
4621 else
4622 flags = ST_SHNODE | ST_SHDESC;
Willy Tarreau91861262007-10-17 17:06:05 +02004623
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004624 chunk_reset(&trash);
Willy Tarreau91861262007-10-17 17:06:05 +02004625
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004626 switch (appctx->ctx.stats.px_st) {
Willy Tarreau295a8372011-03-10 11:25:07 +01004627 case STAT_PX_ST_INIT:
Willy Tarreau91861262007-10-17 17:06:05 +02004628 /* we are on a new proxy */
Willy Tarreau91861262007-10-17 17:06:05 +02004629 if (uri && uri->scope) {
4630 /* we have a limited scope, we have to check the proxy name */
4631 struct stat_scope *scope;
4632 int len;
4633
4634 len = strlen(px->id);
4635 scope = uri->scope;
4636
4637 while (scope) {
4638 /* match exact proxy name */
4639 if (scope->px_len == len && !memcmp(px->id, scope->px_id, len))
4640 break;
4641
4642 /* match '.' which means 'self' proxy */
Willy Tarreau1388a3a2007-10-18 16:38:37 +02004643 if (!strcmp(scope->px_id, ".") && px == s->be)
Willy Tarreau91861262007-10-17 17:06:05 +02004644 break;
4645 scope = scope->next;
4646 }
4647
4648 /* proxy name not found : don't dump anything */
4649 if (scope == NULL)
4650 return 1;
4651 }
4652
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004653 /* if the user has requested a limited output and the proxy
4654 * name does not match, skip it.
4655 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004656 if (appctx->ctx.stats.scope_len &&
Willy Tarreau4e4292b2014-11-28 12:18:45 +01004657 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 +02004658 return 1;
4659
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004660 if ((appctx->ctx.stats.flags & STAT_BOUND) &&
4661 (appctx->ctx.stats.iid != -1) &&
4662 (px->uuid != appctx->ctx.stats.iid))
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01004663 return 1;
4664
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004665 appctx->ctx.stats.px_st = STAT_PX_ST_TH;
Willy Tarreau91861262007-10-17 17:06:05 +02004666 /* fall through */
4667
Willy Tarreau295a8372011-03-10 11:25:07 +01004668 case STAT_PX_ST_TH:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004669 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004670 stats_dump_html_px_hdr(si, px, uri);
Willy Tarreaubc18da12015-03-13 14:00:47 +01004671 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004672 si_applet_cant_put(si);
Willy Tarreau55bb8452007-10-17 18:44:57 +02004673 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01004674 }
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004675 }
Willy Tarreau91861262007-10-17 17:06:05 +02004676
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004677 appctx->ctx.stats.px_st = STAT_PX_ST_FE;
Willy Tarreau91861262007-10-17 17:06:05 +02004678 /* fall through */
4679
Willy Tarreau295a8372011-03-10 11:25:07 +01004680 case STAT_PX_ST_FE:
Willy Tarreau91861262007-10-17 17:06:05 +02004681 /* print the frontend */
Willy Tarreaubc18da12015-03-13 14:00:47 +01004682 if (stats_dump_fe_stats(si, px)) {
4683 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004684 si_applet_cant_put(si);
Willy Tarreau91861262007-10-17 17:06:05 +02004685 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01004686 }
4687 }
Willy Tarreau91861262007-10-17 17:06:05 +02004688
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004689 appctx->ctx.stats.l = px->conf.listeners.n;
4690 appctx->ctx.stats.px_st = STAT_PX_ST_LI;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004691 /* fall through */
4692
Willy Tarreau295a8372011-03-10 11:25:07 +01004693 case STAT_PX_ST_LI:
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004694 /* stats.l has been initialized above */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004695 for (; appctx->ctx.stats.l != &px->conf.listeners; appctx->ctx.stats.l = l->by_fe.n) {
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01004696 if (buffer_almost_full(rep->buf)) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004697 si_applet_cant_put(si);
Willy Tarreau4e33d862009-10-11 23:35:10 +02004698 return 0;
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01004699 }
Willy Tarreau4e33d862009-10-11 23:35:10 +02004700
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004701 l = LIST_ELEM(appctx->ctx.stats.l, struct listener *, by_fe);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004702 if (!l->counters)
4703 continue;
4704
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004705 if (appctx->ctx.stats.flags & STAT_BOUND) {
4706 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SO)))
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004707 break;
4708
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004709 if (appctx->ctx.stats.sid != -1 && l->luid != appctx->ctx.stats.sid)
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004710 continue;
4711 }
4712
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004713 /* print the frontend */
Willy Tarreau0deb85a2016-01-08 15:33:18 +01004714 if (stats_dump_li_stats(si, px, l, flags)) {
Willy Tarreaubc18da12015-03-13 14:00:47 +01004715 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004716 si_applet_cant_put(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004717 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01004718 }
4719 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004720 }
4721
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004722 appctx->ctx.stats.sv = px->srv; /* may be NULL */
4723 appctx->ctx.stats.px_st = STAT_PX_ST_SV;
Willy Tarreau91861262007-10-17 17:06:05 +02004724 /* fall through */
4725
Willy Tarreau295a8372011-03-10 11:25:07 +01004726 case STAT_PX_ST_SV:
Willy Tarreau91861262007-10-17 17:06:05 +02004727 /* stats.sv has been initialized above */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004728 for (; appctx->ctx.stats.sv != NULL; appctx->ctx.stats.sv = sv->next) {
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01004729 if (buffer_almost_full(rep->buf)) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004730 si_applet_cant_put(si);
Willy Tarreau4e33d862009-10-11 23:35:10 +02004731 return 0;
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01004732 }
Willy Tarreau4e33d862009-10-11 23:35:10 +02004733
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004734 sv = appctx->ctx.stats.sv;
Willy Tarreau91861262007-10-17 17:06:05 +02004735
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004736 if (appctx->ctx.stats.flags & STAT_BOUND) {
4737 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SV)))
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01004738 break;
4739
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004740 if (appctx->ctx.stats.sid != -1 && sv->puid != appctx->ctx.stats.sid)
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01004741 continue;
4742 }
4743
Willy Tarreau32091232014-05-16 13:52:00 +02004744 svs = sv;
4745 while (svs->track)
4746 svs = svs->track;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01004747
Willy Tarreauba2f2642016-01-07 09:54:40 +01004748 /* do not report servers which are DOWN and not changing state */
4749 if ((appctx->ctx.stats.flags & STAT_HIDE_DOWN) &&
4750 ((sv->admin & SRV_ADMF_MAINT) || /* server is in maintenance */
4751 (sv->state == SRV_ST_STOPPED && /* server is down */
4752 (!((svs->agent.state | svs->check.state) & CHK_ST_ENABLED) ||
4753 ((svs->agent.state & CHK_ST_ENABLED) && !svs->agent.health) ||
4754 ((svs->check.state & CHK_ST_ENABLED) && !svs->check.health))))) {
Willy Tarreau91861262007-10-17 17:06:05 +02004755 continue;
4756 }
4757
Willy Tarreau0deb85a2016-01-08 15:33:18 +01004758 if (stats_dump_sv_stats(si, px, flags, sv)) {
Willy Tarreaubc18da12015-03-13 14:00:47 +01004759 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004760 si_applet_cant_put(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004761 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01004762 }
4763 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004764 } /* for sv */
Cyril Bonté474be412010-10-12 00:14:36 +02004765
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004766 appctx->ctx.stats.px_st = STAT_PX_ST_BE;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004767 /* fall through */
Cyril Bonté70be45d2010-10-12 00:14:35 +02004768
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004769 case STAT_PX_ST_BE:
4770 /* print the backend */
Willy Tarreau0deb85a2016-01-08 15:33:18 +01004771 if (stats_dump_be_stats(si, px, flags)) {
Willy Tarreaubc18da12015-03-13 14:00:47 +01004772 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004773 si_applet_cant_put(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004774 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01004775 }
4776 }
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01004777
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004778 appctx->ctx.stats.px_st = STAT_PX_ST_END;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004779 /* fall through */
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01004780
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004781 case STAT_PX_ST_END:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004782 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004783 stats_dump_html_px_end(si, px);
Willy Tarreaubc18da12015-03-13 14:00:47 +01004784 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004785 si_applet_cant_put(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004786 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01004787 }
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004788 }
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01004789
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004790 appctx->ctx.stats.px_st = STAT_PX_ST_FIN;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004791 /* fall through */
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01004792
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004793 case STAT_PX_ST_FIN:
4794 return 1;
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01004795
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004796 default:
4797 /* unknown state, we should put an abort() here ! */
4798 return 1;
4799 }
4800}
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01004801
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004802/* Dumps the HTTP stats head block to the trash for and uses the per-uri
4803 * parameters <uri>. The caller is responsible for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004804 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004805static void stats_dump_html_head(struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004806{
4807 /* WARNING! This must fit in the first buffer !!! */
4808 chunk_appendf(&trash,
4809 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n"
4810 "\"http://www.w3.org/TR/html4/loose.dtd\">\n"
4811 "<html><head><title>Statistics Report for " PRODUCT_NAME "%s%s</title>\n"
4812 "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n"
4813 "<style type=\"text/css\"><!--\n"
4814 "body {"
4815 " font-family: arial, helvetica, sans-serif;"
4816 " font-size: 12px;"
4817 " font-weight: normal;"
4818 " color: black;"
4819 " background: white;"
4820 "}\n"
4821 "th,td {"
4822 " font-size: 10px;"
4823 "}\n"
4824 "h1 {"
4825 " font-size: x-large;"
4826 " margin-bottom: 0.5em;"
4827 "}\n"
4828 "h2 {"
4829 " font-family: helvetica, arial;"
4830 " font-size: x-large;"
4831 " font-weight: bold;"
4832 " font-style: italic;"
4833 " color: #6020a0;"
4834 " margin-top: 0em;"
4835 " margin-bottom: 0em;"
4836 "}\n"
4837 "h3 {"
4838 " font-family: helvetica, arial;"
4839 " font-size: 16px;"
4840 " font-weight: bold;"
4841 " color: #b00040;"
4842 " background: #e8e8d0;"
4843 " margin-top: 0em;"
4844 " margin-bottom: 0em;"
4845 "}\n"
4846 "li {"
4847 " margin-top: 0.25em;"
4848 " margin-right: 2em;"
4849 "}\n"
4850 ".hr {margin-top: 0.25em;"
4851 " border-color: black;"
4852 " border-bottom-style: solid;"
4853 "}\n"
4854 ".titre {background: #20D0D0;color: #000000; font-weight: bold; text-align: center;}\n"
4855 ".total {background: #20D0D0;color: #ffff80;}\n"
4856 ".frontend {background: #e8e8d0;}\n"
4857 ".socket {background: #d0d0d0;}\n"
4858 ".backend {background: #e8e8d0;}\n"
Simon Horman837bfa72015-04-23 14:51:27 +09004859 ".active_down {background: #ff9090;}\n"
4860 ".active_going_up {background: #ffd020;}\n"
4861 ".active_going_down {background: #ffffa0;}\n"
4862 ".active_up {background: #c0ffc0;}\n"
4863 ".active_nolb {background: #20a0ff;}\n"
4864 ".active_draining {background: #20a0FF;}\n"
4865 ".active_no_check {background: #e0e0e0;}\n"
4866 ".backup_down {background: #ff9090;}\n"
4867 ".backup_going_up {background: #ff80ff;}\n"
4868 ".backup_going_down {background: #c060ff;}\n"
4869 ".backup_up {background: #b0d0ff;}\n"
4870 ".backup_nolb {background: #90b0e0;}\n"
4871 ".backup_draining {background: #cc9900;}\n"
4872 ".backup_no_check {background: #e0e0e0;}\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004873 ".maintain {background: #c07820;}\n"
4874 ".rls {letter-spacing: 0.2em; margin-right: 1px;}\n" /* right letter spacing (used for grouping digits) */
4875 "\n"
4876 "a.px:link {color: #ffff40; text-decoration: none;}"
4877 "a.px:visited {color: #ffff40; text-decoration: none;}"
4878 "a.px:hover {color: #ffffff; text-decoration: none;}"
4879 "a.lfsb:link {color: #000000; text-decoration: none;}"
4880 "a.lfsb:visited {color: #000000; text-decoration: none;}"
4881 "a.lfsb:hover {color: #505050; text-decoration: none;}"
4882 "\n"
4883 "table.tbl { border-collapse: collapse; border-style: none;}\n"
4884 "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"
4885 "table.tbl td.ac { text-align: center;}\n"
4886 "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}\n"
4887 "table.tbl th.pxname { background: #b00040; color: #ffff40; font-weight: bold; border-style: solid solid none solid; padding: 2px 3px; white-space: nowrap;}\n"
4888 "table.tbl th.empty { border-style: none; empty-cells: hide; background: white;}\n"
4889 "table.tbl th.desc { background: white; border-style: solid solid none solid; text-align: left; padding: 2px 3px;}\n"
4890 "\n"
4891 "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n"
4892 "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n"
4893 "table.lgd td.noborder { border-style: none; padding: 2px; white-space: nowrap;}\n"
Willy Tarreau466c9b52012-12-23 02:25:03 +01004894 "table.det { border-collapse: collapse; border-style: none; }\n"
4895 "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 +01004896 "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 +01004897 "u {text-decoration:none; border-bottom: 1px dotted black;}\n"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02004898 "div.tips {\n"
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01004899 " display:block;\n"
4900 " visibility:hidden;\n"
4901 " z-index:2147483647;\n"
4902 " position:absolute;\n"
4903 " padding:2px 4px 3px;\n"
4904 " background:#f0f060; color:#000000;\n"
4905 " border:1px solid #7040c0;\n"
4906 " white-space:nowrap;\n"
4907 " font-style:normal;font-size:11px;font-weight:normal;\n"
4908 " -moz-border-radius:3px;-webkit-border-radius:3px;border-radius:3px;\n"
4909 " -moz-box-shadow:gray 2px 2px 3px;-webkit-box-shadow:gray 2px 2px 3px;box-shadow:gray 2px 2px 3px;\n"
4910 "}\n"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02004911 "u:hover div.tips {visibility:visible;}\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004912 "-->\n"
4913 "</style></head>\n",
4914 (uri->flags & ST_SHNODE) ? " on " : "",
4915 (uri->flags & ST_SHNODE) ? (uri->node ? uri->node : global.node) : ""
4916 );
4917}
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01004918
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004919/* Dumps the HTML stats information block to the trash for and uses the state from
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004920 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004921 * for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004922 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004923static void stats_dump_html_info(struct stream_interface *si, struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004924{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004925 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004926 unsigned int up = (now.tv_sec - start_date.tv_sec);
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004927 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01004928
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004929 /* WARNING! this has to fit the first packet too.
4930 * We are around 3.5 kB, add adding entries will
4931 * become tricky if we want to support 4kB buffers !
4932 */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004933 chunk_appendf(&trash,
4934 "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">"
4935 PRODUCT_NAME "%s</a></h1>\n"
4936 "<h2>Statistics Report for pid %d%s%s%s%s</h2>\n"
4937 "<hr width=\"100%%\" class=\"hr\">\n"
4938 "<h3>&gt; General process information</h3>\n"
4939 "<table border=0><tr><td align=\"left\" nowrap width=\"1%%\">\n"
4940 "<p><b>pid = </b> %d (process #%d, nbproc = %d)<br>\n"
4941 "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n"
4942 "<b>system limits:</b> memmax = %s%s; ulimit-n = %d<br>\n"
4943 "<b>maxsock = </b> %d; <b>maxconn = </b> %d; <b>maxpipes = </b> %d<br>\n"
4944 "current conns = %d; current pipes = %d/%d; conn rate = %d/sec<br>\n"
4945 "Running tasks: %d/%d; idle = %d %%<br>\n"
4946 "</td><td align=\"center\" nowrap>\n"
4947 "<table class=\"lgd\"><tr>\n"
Simon Horman837bfa72015-04-23 14:51:27 +09004948 "<td class=\"active_up\">&nbsp;</td><td class=\"noborder\">active UP </td>"
4949 "<td class=\"backup_up\">&nbsp;</td><td class=\"noborder\">backup UP </td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004950 "</tr><tr>\n"
Simon Horman837bfa72015-04-23 14:51:27 +09004951 "<td class=\"active_going_down\"></td><td class=\"noborder\">active UP, going down </td>"
4952 "<td class=\"backup_going_down\"></td><td class=\"noborder\">backup UP, going down </td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004953 "</tr><tr>\n"
Simon Horman837bfa72015-04-23 14:51:27 +09004954 "<td class=\"active_going_up\"></td><td class=\"noborder\">active DOWN, going up </td>"
4955 "<td class=\"backup_going_up\"></td><td class=\"noborder\">backup DOWN, going up </td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004956 "</tr><tr>\n"
Simon Horman837bfa72015-04-23 14:51:27 +09004957 "<td class=\"active_down\"></td><td class=\"noborder\">active or backup DOWN &nbsp;</td>"
4958 "<td class=\"active_no_check\"></td><td class=\"noborder\">not checked </td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004959 "</tr><tr>\n"
4960 "<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 -07004961 "</tr><tr>\n"
Simon Horman837bfa72015-04-23 14:51:27 +09004962 "<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 +01004963 "</tr></table>\n"
Willy Tarreau6b7764a2013-12-04 00:43:21 +01004964 "Note: \"NOLB\"/\"DRAIN\" = UP with load-balancing disabled."
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004965 "</td>"
4966 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
4967 "<b>Display option:</b><ul style=\"margin-top: 0.25em;\">"
4968 "",
4969 (uri->flags & ST_HIDEVER) ? "" : (STATS_VERSION_STRING),
4970 pid, (uri->flags & ST_SHNODE) ? " on " : "",
4971 (uri->flags & ST_SHNODE) ? (uri->node ? uri->node : global.node) : "",
4972 (uri->flags & ST_SHDESC) ? ": " : "",
4973 (uri->flags & ST_SHDESC) ? (uri->desc ? uri->desc : global.desc) : "",
4974 pid, relative_pid, global.nbproc,
4975 up / 86400, (up % 86400) / 3600,
4976 (up % 3600) / 60, (up % 60),
4977 global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited",
4978 global.rlimit_memmax ? " MB" : "",
4979 global.rlimit_nofile,
4980 global.maxsock, global.maxconn, global.maxpipes,
4981 actconn, pipes_used, pipes_used+pipes_free, read_freq_ctr(&global.conn_per_sec),
4982 run_queue_cur, nb_tasks_cur, idle_pct
4983 );
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004984
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004985 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Willy Tarreau4e4292b2014-11-28 12:18:45 +01004986 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 +01004987 scope_txt[appctx->ctx.stats.scope_len] = '\0';
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004988
4989 chunk_appendf(&trash,
Jeff Buchbinder2dbbf4d2014-08-29 15:10:08 -05004990 "<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 +01004991 (appctx->ctx.stats.scope_len > 0) ? scope_txt : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004992 STAT_SCOPE_TXT_MAXLEN);
4993
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004994 /* 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 +02004995 scope_txt[0] = 0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004996 if (appctx->ctx.stats.scope_len) {
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004997 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau4e4292b2014-11-28 12:18:45 +01004998 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 +01004999 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02005000 }
5001
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005002 if (appctx->ctx.stats.flags & STAT_HIDE_DOWN)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005003 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02005004 "<li><a href=\"%s%s%s%s\">Show all servers</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005005 uri->uri_prefix,
5006 "",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005007 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02005008 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005009 else
5010 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02005011 "<li><a href=\"%s%s%s%s\">Hide 'DOWN' servers</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005012 uri->uri_prefix,
5013 ";up",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005014 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02005015 scope_txt);
Willy Tarreau91861262007-10-17 17:06:05 +02005016
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005017 if (uri->refresh > 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005018 if (appctx->ctx.stats.flags & STAT_NO_REFRESH)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005019 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02005020 "<li><a href=\"%s%s%s%s\">Enable refresh</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005021 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005022 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02005023 "",
5024 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005025 else
5026 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02005027 "<li><a href=\"%s%s%s%s\">Disable refresh</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005028 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005029 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02005030 ";norefresh",
5031 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005032 }
Willy Tarreau55bb8452007-10-17 18:44:57 +02005033
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005034 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02005035 "<li><a href=\"%s%s%s%s\">Refresh now</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005036 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005037 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
5038 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02005039 scope_txt);
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02005040
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005041 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02005042 "<li><a href=\"%s;csv%s%s\">CSV export</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005043 uri->uri_prefix,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02005044 (uri->refresh > 0) ? ";norefresh" : "",
5045 scope_txt);
Willy Tarreau4bab24d2007-11-30 18:16:29 +01005046
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005047 chunk_appendf(&trash,
5048 "</ul></td>"
5049 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
5050 "<b>External resources:</b><ul style=\"margin-top: 0.25em;\">\n"
5051 "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>\n"
5052 "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>\n"
5053 "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>\n"
5054 "</ul>"
5055 "</td>"
5056 "</tr></table>\n"
5057 ""
5058 );
Willy Tarreau4bab24d2007-11-30 18:16:29 +01005059
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005060 if (appctx->ctx.stats.st_code) {
5061 switch (appctx->ctx.stats.st_code) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005062 case STAT_STATUS_DONE:
5063 chunk_appendf(&trash,
Simon Horman837bfa72015-04-23 14:51:27 +09005064 "<p><div class=active_up>"
Willy Tarreauba6be982013-04-19 12:16:55 +02005065 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005066 "Action processed successfully."
Willy Tarreauba6be982013-04-19 12:16:55 +02005067 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005068 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
5069 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02005070 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005071 break;
5072 case STAT_STATUS_NONE:
5073 chunk_appendf(&trash,
Simon Horman837bfa72015-04-23 14:51:27 +09005074 "<p><div class=active_going_down>"
Willy Tarreauba6be982013-04-19 12:16:55 +02005075 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005076 "Nothing has changed."
Willy Tarreauba6be982013-04-19 12:16:55 +02005077 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005078 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
5079 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02005080 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005081 break;
5082 case STAT_STATUS_PART:
5083 chunk_appendf(&trash,
Simon Horman837bfa72015-04-23 14:51:27 +09005084 "<p><div class=active_going_down>"
Willy Tarreauba6be982013-04-19 12:16:55 +02005085 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005086 "Action partially processed.<br>"
5087 "Some server names are probably unknown or ambiguous (duplicated names in the backend)."
Willy Tarreauba6be982013-04-19 12:16:55 +02005088 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005089 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
5090 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02005091 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005092 break;
5093 case STAT_STATUS_ERRP:
5094 chunk_appendf(&trash,
Simon Horman837bfa72015-04-23 14:51:27 +09005095 "<p><div class=active_down>"
Willy Tarreauba6be982013-04-19 12:16:55 +02005096 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005097 "Action not processed because of invalid parameters."
5098 "<ul>"
5099 "<li>The action is maybe unknown.</li>"
5100 "<li>The backend name is probably unknown or ambiguous (duplicated names).</li>"
5101 "<li>Some server names are probably unknown or ambiguous (duplicated names in the backend).</li>"
5102 "</ul>"
Willy Tarreauba6be982013-04-19 12:16:55 +02005103 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005104 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
5105 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02005106 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005107 break;
5108 case STAT_STATUS_EXCD:
5109 chunk_appendf(&trash,
Simon Horman837bfa72015-04-23 14:51:27 +09005110 "<p><div class=active_down>"
Willy Tarreauba6be982013-04-19 12:16:55 +02005111 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005112 "<b>Action not processed : the buffer couldn't store all the data.<br>"
5113 "You should retry with less servers at a time.</b>"
Willy Tarreauba6be982013-04-19 12:16:55 +02005114 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005115 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
5116 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02005117 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005118 break;
5119 case STAT_STATUS_DENY:
5120 chunk_appendf(&trash,
Simon Horman837bfa72015-04-23 14:51:27 +09005121 "<p><div class=active_down>"
Willy Tarreauba6be982013-04-19 12:16:55 +02005122 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005123 "<b>Action denied.</b>"
Willy Tarreauba6be982013-04-19 12:16:55 +02005124 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005125 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
5126 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02005127 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005128 break;
5129 default:
5130 chunk_appendf(&trash,
Simon Horman837bfa72015-04-23 14:51:27 +09005131 "<p><div class=active_no_check>"
Willy Tarreauba6be982013-04-19 12:16:55 +02005132 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005133 "Unexpected result."
Willy Tarreauba6be982013-04-19 12:16:55 +02005134 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005135 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
5136 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02005137 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005138 }
5139 chunk_appendf(&trash, "<p>\n");
5140 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005141}
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01005142
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01005143/* Dumps the HTML stats trailer block to the trash. The caller is responsible
5144 * for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005145 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01005146static void stats_dump_html_end()
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005147{
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005148 chunk_appendf(&trash, "</body></html>\n");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005149}
Willy Tarreau7f062c42009-03-05 18:43:00 +01005150
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01005151/* This function dumps statistics onto the stream interface's read buffer in
5152 * either CSV or HTML format. <uri> contains some HTML-specific parameters that
Willy Tarreau306f8302013-07-08 15:53:06 +02005153 * are ignored for CSV format (hence <uri> may be NULL there). It returns 0 if
5154 * 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 +02005155 * and the stream must be closed, or -1 in case of any error. This function is
Willy Tarreau306f8302013-07-08 15:53:06 +02005156 * used by both the CLI and the HTTP handlers.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005157 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01005158static int stats_dump_stat_to_buffer(struct stream_interface *si, struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005159{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005160 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005161 struct channel *rep = si_ic(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005162 struct proxy *px;
Willy Tarreau7f062c42009-03-05 18:43:00 +01005163
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005164 chunk_reset(&trash);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02005165
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005166 switch (appctx->st2) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005167 case STAT_ST_INIT:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005168 appctx->st2 = STAT_ST_HEAD; /* let's start producing data */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005169 /* fall through */
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005170
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005171 case STAT_ST_HEAD:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005172 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01005173 stats_dump_html_head(uri);
Willy Tarreau1e62df92016-01-11 18:57:53 +01005174 else if (!(appctx->ctx.stats.flags & STAT_FMT_TYPED))
Willy Tarreau354898b2012-12-23 18:15:23 +01005175 stats_dump_csv_header();
Willy Tarreaud9b587f2010-02-26 10:05:55 +01005176
Willy Tarreaubc18da12015-03-13 14:00:47 +01005177 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02005178 si_applet_cant_put(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005179 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01005180 }
Willy Tarreauae526782010-03-04 20:34:23 +01005181
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005182 appctx->st2 = STAT_ST_INFO;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005183 /* fall through */
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01005184
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005185 case STAT_ST_INFO:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005186 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01005187 stats_dump_html_info(si, uri);
Willy Tarreaubc18da12015-03-13 14:00:47 +01005188 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02005189 si_applet_cant_put(si);
Willy Tarreau91861262007-10-17 17:06:05 +02005190 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01005191 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005192 }
Willy Tarreau91861262007-10-17 17:06:05 +02005193
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005194 appctx->ctx.stats.px = proxy;
5195 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
5196 appctx->st2 = STAT_ST_LIST;
Willy Tarreau91861262007-10-17 17:06:05 +02005197 /* fall through */
5198
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005199 case STAT_ST_LIST:
5200 /* dump proxies */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005201 while (appctx->ctx.stats.px) {
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01005202 if (buffer_almost_full(rep->buf)) {
Willy Tarreaufe127932015-04-21 19:23:39 +02005203 si_applet_cant_put(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005204 return 0;
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01005205 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005206
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005207 px = appctx->ctx.stats.px;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005208 /* skip the disabled proxies, global frontend and non-networked ones */
5209 if (px->state != PR_STSTOPPED && px->uuid > 0 && (px->cap & (PR_CAP_FE | PR_CAP_BE)))
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01005210 if (stats_dump_proxy_to_buffer(si, px, uri) == 0)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005211 return 0;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005212
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005213 appctx->ctx.stats.px = px->next;
5214 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005215 }
5216 /* here, we just have reached the last proxy */
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005217
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005218 appctx->st2 = STAT_ST_END;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005219 /* fall through */
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005220
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005221 case STAT_ST_END:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005222 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01005223 stats_dump_html_end();
Willy Tarreaubc18da12015-03-13 14:00:47 +01005224 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02005225 si_applet_cant_put(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005226 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01005227 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005228 }
Willy Tarreau55058a72012-11-21 08:27:21 +01005229
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005230 appctx->st2 = STAT_ST_FIN;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005231 /* fall through */
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02005232
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005233 case STAT_ST_FIN:
5234 return 1;
Willy Tarreau55058a72012-11-21 08:27:21 +01005235
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005236 default:
5237 /* unknown state ! */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005238 appctx->st2 = STAT_ST_FIN;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005239 return -1;
5240 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005241}
Willy Tarreauae526782010-03-04 20:34:23 +01005242
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005243/* We reached the stats page through a POST request. The appctx is
5244 * expected to have already been allocated by the caller.
Willy Tarreau347a35d2013-11-22 17:51:09 +01005245 * Parse the posted data and enable/disable servers if necessary.
5246 * Returns 1 if request was parsed or zero if it needs more data.
5247 */
5248static int stats_process_http_post(struct stream_interface *si)
5249{
Willy Tarreau87b09662015-04-03 00:22:06 +02005250 struct stream *s = si_strm(si);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005251 struct appctx *appctx = objt_appctx(si->end);
Willy Tarreau347a35d2013-11-22 17:51:09 +01005252
5253 struct proxy *px = NULL;
5254 struct server *sv = NULL;
5255
5256 char key[LINESIZE];
5257 int action = ST_ADM_ACTION_NONE;
5258 int reprocess = 0;
5259
5260 int total_servers = 0;
5261 int altered_servers = 0;
5262
5263 char *first_param, *cur_param, *next_param, *end_params;
5264 char *st_cur_param = NULL;
5265 char *st_next_param = NULL;
5266
5267 struct chunk *temp;
5268 int reql;
5269
5270 temp = get_trash_chunk();
Willy Tarreaueee5b512015-04-03 23:46:31 +02005271 if (temp->size < s->txn->req.body_len) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01005272 /* too large request */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005273 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005274 goto out;
5275 }
5276
Willy Tarreaueee5b512015-04-03 23:46:31 +02005277 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 +01005278 if (reql <= 0) {
5279 /* we need more data */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005280 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005281 return 0;
5282 }
5283
5284 first_param = temp->str;
5285 end_params = temp->str + reql;
5286 cur_param = next_param = end_params;
5287 *end_params = '\0';
5288
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005289 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005290
5291 /*
5292 * Parse the parameters in reverse order to only store the last value.
5293 * From the html form, the backend and the action are at the end.
5294 */
5295 while (cur_param > first_param) {
5296 char *value;
5297 int poffset, plen;
5298
5299 cur_param--;
5300
5301 if ((*cur_param == '&') || (cur_param == first_param)) {
5302 reprocess_servers:
5303 /* Parse the key */
5304 poffset = (cur_param != first_param ? 1 : 0);
5305 plen = next_param - cur_param + (cur_param == first_param ? 1 : 0);
5306 if ((plen > 0) && (plen <= sizeof(key))) {
5307 strncpy(key, cur_param + poffset, plen);
5308 key[plen - 1] = '\0';
5309 } else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005310 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005311 goto out;
5312 }
5313
5314 /* Parse the value */
5315 value = key;
5316 while (*value != '\0' && *value != '=') {
5317 value++;
5318 }
5319 if (*value == '=') {
5320 /* Ok, a value is found, we can mark the end of the key */
5321 *value++ = '\0';
5322 }
5323 if (url_decode(key) < 0 || url_decode(value) < 0)
5324 break;
5325
5326 /* Now we can check the key to see what to do */
5327 if (!px && (strcmp(key, "b") == 0)) {
Willy Tarreau9e0bb102015-05-26 11:24:42 +02005328 if ((px = proxy_be_by_name(value)) == NULL) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01005329 /* the backend name is unknown or ambiguous (duplicate names) */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005330 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005331 goto out;
5332 }
5333 }
5334 else if (!action && (strcmp(key, "action") == 0)) {
Willy Tarreaued7df902014-05-22 18:04:49 +02005335 if (strcmp(value, "ready") == 0) {
5336 action = ST_ADM_ACTION_READY;
5337 }
5338 else if (strcmp(value, "drain") == 0) {
5339 action = ST_ADM_ACTION_DRAIN;
5340 }
5341 else if (strcmp(value, "maint") == 0) {
5342 action = ST_ADM_ACTION_MAINT;
5343 }
5344 else if (strcmp(value, "shutdown") == 0) {
5345 action = ST_ADM_ACTION_SHUTDOWN;
5346 }
Willy Tarreau248a60e2014-05-23 14:59:48 +02005347 else if (strcmp(value, "dhlth") == 0) {
5348 action = ST_ADM_ACTION_DHLTH;
5349 }
5350 else if (strcmp(value, "ehlth") == 0) {
5351 action = ST_ADM_ACTION_EHLTH;
5352 }
5353 else if (strcmp(value, "hrunn") == 0) {
5354 action = ST_ADM_ACTION_HRUNN;
5355 }
5356 else if (strcmp(value, "hnolb") == 0) {
5357 action = ST_ADM_ACTION_HNOLB;
5358 }
5359 else if (strcmp(value, "hdown") == 0) {
5360 action = ST_ADM_ACTION_HDOWN;
5361 }
5362 else if (strcmp(value, "dagent") == 0) {
5363 action = ST_ADM_ACTION_DAGENT;
5364 }
5365 else if (strcmp(value, "eagent") == 0) {
5366 action = ST_ADM_ACTION_EAGENT;
5367 }
5368 else if (strcmp(value, "arunn") == 0) {
5369 action = ST_ADM_ACTION_ARUNN;
5370 }
5371 else if (strcmp(value, "adown") == 0) {
5372 action = ST_ADM_ACTION_ADOWN;
5373 }
Willy Tarreaued7df902014-05-22 18:04:49 +02005374 /* else these are the old supported methods */
5375 else if (strcmp(value, "disable") == 0) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01005376 action = ST_ADM_ACTION_DISABLE;
5377 }
5378 else if (strcmp(value, "enable") == 0) {
5379 action = ST_ADM_ACTION_ENABLE;
5380 }
5381 else if (strcmp(value, "stop") == 0) {
5382 action = ST_ADM_ACTION_STOP;
5383 }
5384 else if (strcmp(value, "start") == 0) {
5385 action = ST_ADM_ACTION_START;
5386 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01005387 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005388 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005389 goto out;
5390 }
5391 }
5392 else if (strcmp(key, "s") == 0) {
5393 if (!(px && action)) {
5394 /*
5395 * Indicates that we'll need to reprocess the parameters
5396 * as soon as backend and action are known
5397 */
5398 if (!reprocess) {
5399 st_cur_param = cur_param;
5400 st_next_param = next_param;
5401 }
5402 reprocess = 1;
5403 }
5404 else if ((sv = findserver(px, value)) != NULL) {
5405 switch (action) {
5406 case ST_ADM_ACTION_DISABLE:
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02005407 if (!(sv->admin & SRV_ADMF_FMAINT)) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01005408 altered_servers++;
5409 total_servers++;
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02005410 srv_set_admin_flag(sv, SRV_ADMF_FMAINT);
Willy Tarreau347a35d2013-11-22 17:51:09 +01005411 }
5412 break;
5413 case ST_ADM_ACTION_ENABLE:
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02005414 if (sv->admin & SRV_ADMF_FMAINT) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01005415 altered_servers++;
5416 total_servers++;
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02005417 srv_clr_admin_flag(sv, SRV_ADMF_FMAINT);
Willy Tarreau347a35d2013-11-22 17:51:09 +01005418 }
5419 break;
5420 case ST_ADM_ACTION_STOP:
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02005421 if (!(sv->admin & SRV_ADMF_FDRAIN)) {
5422 srv_set_admin_flag(sv, SRV_ADMF_FDRAIN);
5423 altered_servers++;
5424 total_servers++;
5425 }
5426 break;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005427 case ST_ADM_ACTION_START:
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02005428 if (sv->admin & SRV_ADMF_FDRAIN) {
5429 srv_clr_admin_flag(sv, SRV_ADMF_FDRAIN);
5430 altered_servers++;
5431 total_servers++;
5432 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01005433 break;
Willy Tarreau248a60e2014-05-23 14:59:48 +02005434 case ST_ADM_ACTION_DHLTH:
5435 if (sv->check.state & CHK_ST_CONFIGURED) {
5436 sv->check.state &= ~CHK_ST_ENABLED;
5437 altered_servers++;
5438 total_servers++;
5439 }
5440 break;
5441 case ST_ADM_ACTION_EHLTH:
5442 if (sv->check.state & CHK_ST_CONFIGURED) {
5443 sv->check.state |= CHK_ST_ENABLED;
5444 altered_servers++;
5445 total_servers++;
5446 }
5447 break;
5448 case ST_ADM_ACTION_HRUNN:
5449 if (!(sv->track)) {
5450 sv->check.health = sv->check.rise + sv->check.fall - 1;
5451 srv_set_running(sv, "changed from Web interface");
5452 altered_servers++;
5453 total_servers++;
5454 }
5455 break;
5456 case ST_ADM_ACTION_HNOLB:
5457 if (!(sv->track)) {
5458 sv->check.health = sv->check.rise + sv->check.fall - 1;
5459 srv_set_stopping(sv, "changed from Web interface");
5460 altered_servers++;
5461 total_servers++;
5462 }
5463 break;
5464 case ST_ADM_ACTION_HDOWN:
5465 if (!(sv->track)) {
5466 sv->check.health = 0;
5467 srv_set_stopped(sv, "changed from Web interface");
5468 altered_servers++;
5469 total_servers++;
5470 }
5471 break;
5472 case ST_ADM_ACTION_DAGENT:
5473 if (sv->agent.state & CHK_ST_CONFIGURED) {
5474 sv->agent.state &= ~CHK_ST_ENABLED;
5475 altered_servers++;
5476 total_servers++;
5477 }
5478 break;
5479 case ST_ADM_ACTION_EAGENT:
5480 if (sv->agent.state & CHK_ST_CONFIGURED) {
5481 sv->agent.state |= CHK_ST_ENABLED;
5482 altered_servers++;
5483 total_servers++;
5484 }
5485 break;
5486 case ST_ADM_ACTION_ARUNN:
5487 if (sv->agent.state & CHK_ST_ENABLED) {
5488 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
5489 srv_set_running(sv, "changed from Web interface");
5490 altered_servers++;
5491 total_servers++;
5492 }
5493 break;
5494 case ST_ADM_ACTION_ADOWN:
5495 if (sv->agent.state & CHK_ST_ENABLED) {
5496 sv->agent.health = 0;
5497 srv_set_stopped(sv, "changed from Web interface");
5498 altered_servers++;
5499 total_servers++;
5500 }
5501 break;
Willy Tarreaued7df902014-05-22 18:04:49 +02005502 case ST_ADM_ACTION_READY:
5503 srv_adm_set_ready(sv);
5504 altered_servers++;
5505 total_servers++;
5506 break;
5507 case ST_ADM_ACTION_DRAIN:
5508 srv_adm_set_drain(sv);
5509 altered_servers++;
5510 total_servers++;
5511 break;
5512 case ST_ADM_ACTION_MAINT:
5513 srv_adm_set_maint(sv);
5514 altered_servers++;
5515 total_servers++;
5516 break;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005517 case ST_ADM_ACTION_SHUTDOWN:
5518 if (px->state != PR_STSTOPPED) {
Willy Tarreau87b09662015-04-03 00:22:06 +02005519 struct stream *sess, *sess_bck;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005520
5521 list_for_each_entry_safe(sess, sess_bck, &sv->actconns, by_srv)
5522 if (sess->srv_conn == sv)
Willy Tarreaue7dff022015-04-03 01:14:29 +02005523 stream_shutdown(sess, SF_ERR_KILLED);
Willy Tarreau347a35d2013-11-22 17:51:09 +01005524
5525 altered_servers++;
5526 total_servers++;
5527 }
5528 break;
5529 }
5530 } else {
5531 /* the server name is unknown or ambiguous (duplicate names) */
5532 total_servers++;
5533 }
5534 }
5535 if (reprocess && px && action) {
5536 /* Now, we know the backend and the action chosen by the user.
5537 * We can safely restart from the first server parameter
5538 * to reprocess them
5539 */
5540 cur_param = st_cur_param;
5541 next_param = st_next_param;
5542 reprocess = 0;
5543 goto reprocess_servers;
5544 }
5545
5546 next_param = cur_param;
5547 }
5548 }
5549
5550 if (total_servers == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005551 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005552 }
5553 else if (altered_servers == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005554 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005555 }
5556 else if (altered_servers == total_servers) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005557 appctx->ctx.stats.st_code = STAT_STATUS_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005558 }
5559 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005560 appctx->ctx.stats.st_code = STAT_STATUS_PART;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005561 }
5562 out:
5563 return 1;
5564}
5565
5566
5567static int stats_send_http_headers(struct stream_interface *si)
5568{
Willy Tarreau87b09662015-04-03 00:22:06 +02005569 struct stream *s = si_strm(si);
Willy Tarreau347a35d2013-11-22 17:51:09 +01005570 struct uri_auth *uri = s->be->uri_auth;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005571 struct appctx *appctx = objt_appctx(si->end);
Willy Tarreau347a35d2013-11-22 17:51:09 +01005572
5573 chunk_printf(&trash,
Willy Tarreau8b8995f2014-04-24 22:51:54 +02005574 "HTTP/1.1 200 OK\r\n"
Willy Tarreau347a35d2013-11-22 17:51:09 +01005575 "Cache-Control: no-cache\r\n"
5576 "Connection: close\r\n"
5577 "Content-Type: %s\r\n",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005578 (appctx->ctx.stats.flags & STAT_FMT_HTML) ? "text/html" : "text/plain");
Willy Tarreau347a35d2013-11-22 17:51:09 +01005579
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005580 if (uri->refresh > 0 && !(appctx->ctx.stats.flags & STAT_NO_REFRESH))
Willy Tarreau347a35d2013-11-22 17:51:09 +01005581 chunk_appendf(&trash, "Refresh: %d\r\n",
5582 uri->refresh);
5583
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005584 /* we don't send the CRLF in chunked mode, it will be sent with the first chunk's size */
5585
5586 if (appctx->ctx.stats.flags & STAT_CHUNKED)
5587 chunk_appendf(&trash, "Transfer-Encoding: chunked\r\n");
5588 else
5589 chunk_appendf(&trash, "\r\n");
Willy Tarreau347a35d2013-11-22 17:51:09 +01005590
Willy Tarreaueee5b512015-04-03 23:46:31 +02005591 s->txn->status = 200;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005592 s->logs.tv_request = now;
5593
Willy Tarreaubc18da12015-03-13 14:00:47 +01005594 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02005595 si_applet_cant_put(si);
Willy Tarreau347a35d2013-11-22 17:51:09 +01005596 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01005597 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01005598
5599 return 1;
5600}
5601
5602static int stats_send_http_redirect(struct stream_interface *si)
5603{
5604 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
Willy Tarreau87b09662015-04-03 00:22:06 +02005605 struct stream *s = si_strm(si);
Willy Tarreau347a35d2013-11-22 17:51:09 +01005606 struct uri_auth *uri = s->be->uri_auth;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005607 struct appctx *appctx = objt_appctx(si->end);
Willy Tarreau347a35d2013-11-22 17:51:09 +01005608
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005609 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Willy Tarreau347a35d2013-11-22 17:51:09 +01005610 scope_txt[0] = 0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005611 if (appctx->ctx.stats.scope_len) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01005612 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau4e4292b2014-11-28 12:18:45 +01005613 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 +01005614 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005615 }
5616
5617 /* We don't want to land on the posted stats page because a refresh will
5618 * repost the data. We don't want this to happen on accident so we redirect
5619 * the browse to the stats page with a GET.
5620 */
5621 chunk_printf(&trash,
5622 "HTTP/1.1 303 See Other\r\n"
5623 "Cache-Control: no-cache\r\n"
5624 "Content-Type: text/plain\r\n"
5625 "Connection: close\r\n"
5626 "Location: %s;st=%s%s%s%s\r\n"
Willy Tarreaufdfcc9d2016-01-26 13:57:29 +01005627 "Content-length: 0\r\n"
Willy Tarreau347a35d2013-11-22 17:51:09 +01005628 "\r\n",
5629 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005630 ((appctx->ctx.stats.st_code > STAT_STATUS_INIT) &&
5631 (appctx->ctx.stats.st_code < STAT_STATUS_SIZE) &&
5632 stat_status_codes[appctx->ctx.stats.st_code]) ?
5633 stat_status_codes[appctx->ctx.stats.st_code] :
Willy Tarreau347a35d2013-11-22 17:51:09 +01005634 stat_status_codes[STAT_STATUS_UNKN],
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005635 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
5636 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreau347a35d2013-11-22 17:51:09 +01005637 scope_txt);
5638
Willy Tarreaueee5b512015-04-03 23:46:31 +02005639 s->txn->status = 303;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005640 s->logs.tv_request = now;
5641
Willy Tarreaubc18da12015-03-13 14:00:47 +01005642 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02005643 si_applet_cant_put(si);
Willy Tarreau347a35d2013-11-22 17:51:09 +01005644 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01005645 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01005646
5647 return 1;
5648}
5649
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005650/* This I/O handler runs as an applet embedded in a stream interface. It is
5651 * used to send HTTP stats over a TCP socket. The mechanism is very simple.
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005652 * appctx->st0 contains the operation in progress (dump, done). The handler
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005653 * automatically unregisters itself once transfer is complete.
5654 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02005655static void http_stats_io_handler(struct appctx *appctx)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005656{
Willy Tarreau00a37f02015-04-13 12:05:19 +02005657 struct stream_interface *si = appctx->owner;
Willy Tarreau87b09662015-04-03 00:22:06 +02005658 struct stream *s = si_strm(si);
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005659 struct channel *req = si_oc(si);
5660 struct channel *res = si_ic(si);
Willy Tarreau55058a72012-11-21 08:27:21 +01005661
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005662 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
5663 goto out;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01005664
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005665 /* check that the output is not closed */
5666 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005667 appctx->st0 = STAT_HTTP_DONE;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005668
Willy Tarreau347a35d2013-11-22 17:51:09 +01005669 /* all states are processed in sequence */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005670 if (appctx->st0 == STAT_HTTP_HEAD) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01005671 if (stats_send_http_headers(si)) {
Willy Tarreaueee5b512015-04-03 23:46:31 +02005672 if (s->txn->meth == HTTP_METH_HEAD)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005673 appctx->st0 = STAT_HTTP_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005674 else
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005675 appctx->st0 = STAT_HTTP_DUMP;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005676 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01005677 }
5678
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005679 if (appctx->st0 == STAT_HTTP_DUMP) {
Willy Tarreau4e4292b2014-11-28 12:18:45 +01005680 unsigned int prev_len = si_ib(si)->i;
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005681 unsigned int data_len;
5682 unsigned int last_len;
Willy Tarreaucce36482014-04-24 20:26:41 +02005683 unsigned int last_fwd = 0;
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005684
5685 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
5686 /* One difficulty we're facing is that we must prevent
5687 * the input data from being automatically forwarded to
5688 * the output area. For this, we temporarily disable
5689 * forwarding on the channel.
5690 */
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005691 last_fwd = si_ic(si)->to_forward;
5692 si_ic(si)->to_forward = 0;
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005693 chunk_printf(&trash, "\r\n000000\r\n");
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005694 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02005695 si_applet_cant_put(si);
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005696 si_ic(si)->to_forward = last_fwd;
Willy Tarreau828824a2015-04-19 17:20:03 +02005697 goto out;
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005698 }
5699 }
5700
Willy Tarreau4e4292b2014-11-28 12:18:45 +01005701 data_len = si_ib(si)->i;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005702 if (stats_dump_stat_to_buffer(si, s->be->uri_auth))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005703 appctx->st0 = STAT_HTTP_DONE;
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005704
Willy Tarreau4e4292b2014-11-28 12:18:45 +01005705 last_len = si_ib(si)->i;
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005706
5707 /* Now we must either adjust or remove the chunk size. This is
5708 * not easy because the chunk size might wrap at the end of the
5709 * buffer, so we pretend we have nothing in the buffer, we write
5710 * the size, then restore the buffer's contents. Note that we can
5711 * only do that because no forwarding is scheduled on the stats
5712 * applet.
5713 */
5714 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
Willy Tarreau4e4292b2014-11-28 12:18:45 +01005715 si_ic(si)->total -= (last_len - prev_len);
5716 si_ib(si)->i -= (last_len - prev_len);
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005717
5718 if (last_len != data_len) {
5719 chunk_printf(&trash, "\r\n%06x\r\n", (last_len - data_len));
Willy Tarreaubc18da12015-03-13 14:00:47 +01005720 if (bi_putchk(si_ic(si), &trash) == -1)
Willy Tarreaufe127932015-04-21 19:23:39 +02005721 si_applet_cant_put(si);
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005722
Willy Tarreau4e4292b2014-11-28 12:18:45 +01005723 si_ic(si)->total += (last_len - data_len);
5724 si_ib(si)->i += (last_len - data_len);
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005725 }
5726 /* now re-enable forwarding */
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005727 channel_forward(si_ic(si), last_fwd);
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005728 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005729 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02005730
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005731 if (appctx->st0 == STAT_HTTP_POST) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01005732 if (stats_process_http_post(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005733 appctx->st0 = STAT_HTTP_LAST;
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005734 else if (si_oc(si)->flags & CF_SHUTR)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005735 appctx->st0 = STAT_HTTP_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005736 }
5737
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005738 if (appctx->st0 == STAT_HTTP_LAST) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01005739 if (stats_send_http_redirect(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005740 appctx->st0 = STAT_HTTP_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005741 }
5742
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005743 if (appctx->st0 == STAT_HTTP_DONE) {
5744 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
5745 chunk_printf(&trash, "\r\n0\r\n\r\n");
Willy Tarreaubc18da12015-03-13 14:00:47 +01005746 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02005747 si_applet_cant_put(si);
Willy Tarreau828824a2015-04-19 17:20:03 +02005748 goto out;
Willy Tarreaubc18da12015-03-13 14:00:47 +01005749 }
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005750 }
5751 /* eat the whole request */
Willy Tarreau4e4292b2014-11-28 12:18:45 +01005752 bo_skip(si_oc(si), si_ob(si)->o);
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005753 res->flags |= CF_READ_NULL;
5754 si_shutr(si);
5755 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01005756
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005757 if ((res->flags & CF_SHUTR) && (si->state == SI_ST_EST))
5758 si_shutw(si);
Willy Tarreau91861262007-10-17 17:06:05 +02005759
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005760 if (appctx->st0 == STAT_HTTP_DONE) {
Willy Tarreau96d44912013-11-22 12:25:24 +01005761 if ((req->flags & CF_SHUTW) && (si->state == SI_ST_EST)) {
5762 si_shutr(si);
5763 res->flags |= CF_READ_NULL;
5764 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005765 }
Willy Tarreau828824a2015-04-19 17:20:03 +02005766 out:
Willy Tarreaud4da1962015-04-20 01:31:23 +02005767 /* just to make gcc happy */ ;
Willy Tarreau91861262007-10-17 17:06:05 +02005768}
5769
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005770
Willy Tarreau909d5172012-11-26 03:04:41 +01005771static inline const char *get_conn_ctrl_name(const struct connection *conn)
5772{
Willy Tarreau3c728722014-01-23 13:50:42 +01005773 if (!conn_ctrl_ready(conn))
Willy Tarreau909d5172012-11-26 03:04:41 +01005774 return "NONE";
5775 return conn->ctrl->name;
5776}
5777
5778static inline const char *get_conn_xprt_name(const struct connection *conn)
5779{
5780 static char ptr[17];
5781
Willy Tarreauaad69382014-01-23 14:21:42 +01005782 if (!conn_xprt_ready(conn))
Willy Tarreau909d5172012-11-26 03:04:41 +01005783 return "NONE";
5784
5785 if (conn->xprt == &raw_sock)
5786 return "RAW";
5787
5788#ifdef USE_OPENSSL
5789 if (conn->xprt == &ssl_sock)
5790 return "SSL";
5791#endif
5792 snprintf(ptr, sizeof(ptr), "%p", conn->xprt);
5793 return ptr;
5794}
5795
5796static inline const char *get_conn_data_name(const struct connection *conn)
5797{
5798 static char ptr[17];
5799
5800 if (!conn->data)
5801 return "NONE";
5802
5803 if (conn->data == &sess_conn_cb)
5804 return "SESS";
5805
5806 if (conn->data == &si_conn_cb)
5807 return "STRM";
5808
5809 if (conn->data == &check_conn_cb)
5810 return "CHCK";
5811
5812 snprintf(ptr, sizeof(ptr), "%p", conn->data);
5813 return ptr;
5814}
5815
Willy Tarreau87b09662015-04-03 00:22:06 +02005816/* This function dumps a complete stream state onto the stream interface's
5817 * read buffer. The stream has to be set in sess->target. It returns
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01005818 * 0 if the output buffer is full and it needs to be called again, otherwise
5819 * non-zero. It is designed to be called from stats_dump_sess_to_buffer() below.
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005820 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005821static int stats_dump_full_sess_to_buffer(struct stream_interface *si, struct stream *sess)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005822{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005823 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005824 struct tm tm;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005825 extern const char *monthname[12];
5826 char pn[INET6_ADDRSTRLEN];
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005827 struct connection *conn;
Willy Tarreau284ddbf2013-12-01 20:45:00 +01005828 struct appctx *tmpctx;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005829
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005830 chunk_reset(&trash);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005831
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005832 if (appctx->ctx.sess.section > 0 && appctx->ctx.sess.uid != sess->uniq_id) {
Willy Tarreau87b09662015-04-03 00:22:06 +02005833 /* stream changed, no need to go any further */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005834 chunk_appendf(&trash, " *** session terminated while we were watching it ***\n");
Willy Tarreaubc18da12015-03-13 14:00:47 +01005835 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02005836 si_applet_cant_put(si);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005837 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01005838 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005839 appctx->ctx.sess.uid = 0;
5840 appctx->ctx.sess.section = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005841 return 1;
5842 }
5843
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005844 switch (appctx->ctx.sess.section) {
Willy Tarreau87b09662015-04-03 00:22:06 +02005845 case 0: /* main status of the stream */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005846 appctx->ctx.sess.uid = sess->uniq_id;
5847 appctx->ctx.sess.section = 1;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005848 /* fall through */
5849
5850 case 1:
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005851 get_localtime(sess->logs.accept_date.tv_sec, &tm);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005852 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005853 "%p: [%02d/%s/%04d:%02d:%02d:%02d.%06d] id=%u proto=%s",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005854 sess,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005855 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
5856 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(sess->logs.accept_date.tv_usec),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005857 sess->uniq_id,
Willy Tarreau666f5042015-06-17 19:49:52 +02005858 strm_li(sess) ? strm_li(sess)->proto->name : "?");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005859
Willy Tarreaud0d8da92015-04-04 02:10:38 +02005860 conn = objt_conn(strm_orig(sess));
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005861 switch (conn ? addr_to_str(&conn->addr.from, pn, sizeof(pn)) : AF_UNSPEC) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005862 case AF_INET:
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005863 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005864 chunk_appendf(&trash, " source=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005865 pn, get_host_port(&conn->addr.from));
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005866 break;
5867 case AF_UNIX:
Willy Tarreaud0d8da92015-04-04 02:10:38 +02005868 chunk_appendf(&trash, " source=unix:%d\n", strm_li(sess)->luid);
Emeric Brun837ca522010-10-22 16:19:01 +02005869 break;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005870 default:
5871 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005872 chunk_appendf(&trash, "\n");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005873 break;
5874 }
5875
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005876 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005877 " flags=0x%x, conn_retries=%d, srv_conn=%p, pend_pos=%p\n",
Willy Tarreauee28de02010-06-01 09:51:00 +02005878 sess->flags, sess->si[1].conn_retries, sess->srv_conn, sess->pend_pos);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005879
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005880 chunk_appendf(&trash,
Daniel Schultze90690c72012-03-23 10:53:36 -07005881 " frontend=%s (id=%u mode=%s), listener=%s (id=%u)",
Willy Tarreaud0d8da92015-04-04 02:10:38 +02005882 strm_fe(sess)->id, strm_fe(sess)->uuid, strm_fe(sess)->mode ? "http" : "tcp",
5883 strm_li(sess) ? strm_li(sess)->name ? strm_li(sess)->name : "?" : "?",
5884 strm_li(sess) ? strm_li(sess)->luid : 0);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005885
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005886 if (conn)
5887 conn_get_to_addr(conn);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005888
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005889 switch (conn ? addr_to_str(&conn->addr.to, pn, sizeof(pn)) : AF_UNSPEC) {
Daniel Schultze90690c72012-03-23 10:53:36 -07005890 case AF_INET:
5891 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005892 chunk_appendf(&trash, " addr=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005893 pn, get_host_port(&conn->addr.to));
Daniel Schultze90690c72012-03-23 10:53:36 -07005894 break;
5895 case AF_UNIX:
Willy Tarreaud0d8da92015-04-04 02:10:38 +02005896 chunk_appendf(&trash, " addr=unix:%d\n", strm_li(sess)->luid);
Daniel Schultze90690c72012-03-23 10:53:36 -07005897 break;
5898 default:
5899 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005900 chunk_appendf(&trash, "\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07005901 break;
5902 }
5903
Willy Tarreau50943332011-09-02 17:33:05 +02005904 if (sess->be->cap & PR_CAP_BE)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005905 chunk_appendf(&trash,
Daniel Schultze90690c72012-03-23 10:53:36 -07005906 " backend=%s (id=%u mode=%s)",
Willy Tarreau50943332011-09-02 17:33:05 +02005907 sess->be->id,
Daniel Schultze90690c72012-03-23 10:53:36 -07005908 sess->be->uuid, sess->be->mode ? "http" : "tcp");
5909 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005910 chunk_appendf(&trash, " backend=<NONE> (id=-1 mode=-)");
Daniel Schultze90690c72012-03-23 10:53:36 -07005911
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005912 conn = objt_conn(sess->si[1].end);
5913 if (conn)
5914 conn_get_from_addr(conn);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005915
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005916 switch (conn ? addr_to_str(&conn->addr.from, pn, sizeof(pn)) : AF_UNSPEC) {
Daniel Schultze90690c72012-03-23 10:53:36 -07005917 case AF_INET:
5918 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005919 chunk_appendf(&trash, " addr=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005920 pn, get_host_port(&conn->addr.from));
Daniel Schultze90690c72012-03-23 10:53:36 -07005921 break;
5922 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005923 chunk_appendf(&trash, " addr=unix\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07005924 break;
5925 default:
5926 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005927 chunk_appendf(&trash, "\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07005928 break;
5929 }
5930
5931 if (sess->be->cap & PR_CAP_BE)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005932 chunk_appendf(&trash,
Daniel Schultze90690c72012-03-23 10:53:36 -07005933 " server=%s (id=%u)",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005934 objt_server(sess->target) ? objt_server(sess->target)->id : "<none>",
5935 objt_server(sess->target) ? objt_server(sess->target)->puid : 0);
Willy Tarreau50943332011-09-02 17:33:05 +02005936 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005937 chunk_appendf(&trash, " server=<NONE> (id=-1)");
Daniel Schultze90690c72012-03-23 10:53:36 -07005938
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005939 if (conn)
5940 conn_get_to_addr(conn);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005941
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005942 switch (conn ? addr_to_str(&conn->addr.to, pn, sizeof(pn)) : AF_UNSPEC) {
Daniel Schultze90690c72012-03-23 10:53:36 -07005943 case AF_INET:
5944 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005945 chunk_appendf(&trash, " addr=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005946 pn, get_host_port(&conn->addr.to));
Daniel Schultze90690c72012-03-23 10:53:36 -07005947 break;
5948 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005949 chunk_appendf(&trash, " addr=unix\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07005950 break;
5951 default:
5952 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005953 chunk_appendf(&trash, "\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07005954 break;
5955 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005956
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005957 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005958 " task=%p (state=0x%02x nice=%d calls=%d exp=%s%s",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005959 sess->task,
5960 sess->task->state,
5961 sess->task->nice, sess->task->calls,
5962 sess->task->expire ?
5963 tick_is_expired(sess->task->expire, now_ms) ? "<PAST>" :
5964 human_time(TICKS_TO_MS(sess->task->expire - now_ms),
5965 TICKS_TO_MS(1000)) : "<NEVER>",
5966 task_in_rq(sess->task) ? ", running" : "");
5967
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005968 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005969 " age=%s)\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005970 human_time(now.tv_sec - sess->logs.accept_date.tv_sec, 1));
5971
Willy Tarreaueee5b512015-04-03 23:46:31 +02005972 if (sess->txn)
5973 chunk_appendf(&trash,
Willy Tarreau98410192014-11-26 18:05:38 +01005974 " 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 +02005975 sess->txn, sess->txn->flags, sess->txn->meth, sess->txn->status,
5976 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 +01005977
5978 chunk_appendf(&trash,
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02005979 " si[0]=%p (state=%s flags=0x%02x endp0=%s:%p exp=%s, et=0x%03x)\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005980 &sess->si[0],
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005981 si_state_str(sess->si[0].state),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005982 sess->si[0].flags,
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005983 obj_type_name(sess->si[0].end),
5984 obj_base_ptr(sess->si[0].end),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005985 sess->si[0].exp ?
5986 tick_is_expired(sess->si[0].exp, now_ms) ? "<PAST>" :
5987 human_time(TICKS_TO_MS(sess->si[0].exp - now_ms),
5988 TICKS_TO_MS(1000)) : "<NEVER>",
5989 sess->si[0].err_type);
5990
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005991 chunk_appendf(&trash,
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02005992 " si[1]=%p (state=%s flags=0x%02x endp1=%s:%p exp=%s, et=0x%03x)\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005993 &sess->si[1],
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005994 si_state_str(sess->si[1].state),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005995 sess->si[1].flags,
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005996 obj_type_name(sess->si[1].end),
5997 obj_base_ptr(sess->si[1].end),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005998 sess->si[1].exp ?
5999 tick_is_expired(sess->si[1].exp, now_ms) ? "<PAST>" :
6000 human_time(TICKS_TO_MS(sess->si[1].exp - now_ms),
6001 TICKS_TO_MS(1000)) : "<NEVER>",
6002 sess->si[1].err_type);
6003
Willy Tarreau284ddbf2013-12-01 20:45:00 +01006004 if ((conn = objt_conn(sess->si[0].end)) != NULL) {
Willy Tarreaucf644ed2013-09-29 17:19:56 +02006005 chunk_appendf(&trash,
6006 " co0=%p ctrl=%s xprt=%s data=%s target=%s:%p\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006007 conn,
6008 get_conn_ctrl_name(conn),
6009 get_conn_xprt_name(conn),
6010 get_conn_data_name(conn),
6011 obj_type_name(conn->target),
6012 obj_base_ptr(conn->target));
Willy Tarreau909d5172012-11-26 03:04:41 +01006013
Willy Tarreaucf644ed2013-09-29 17:19:56 +02006014 chunk_appendf(&trash,
Willy Tarreau16f649c2014-01-25 19:10:48 +01006015 " flags=0x%08x fd=%d fd.state=%02x fd.cache=%d updt=%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006016 conn->flags,
6017 conn->t.sock.fd,
Willy Tarreau69a41fa2014-01-20 11:02:59 +01006018 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].state : 0,
Willy Tarreau15a4dec2014-01-20 11:09:39 +01006019 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].cache : 0,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006020 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].updated : 0);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02006021 }
Willy Tarreau284ddbf2013-12-01 20:45:00 +01006022 else if ((tmpctx = objt_appctx(sess->si[0].end)) != NULL) {
6023 chunk_appendf(&trash,
6024 " app0=%p st0=%d st1=%d st2=%d applet=%s\n",
6025 tmpctx,
6026 tmpctx->st0,
6027 tmpctx->st1,
6028 tmpctx->st2,
6029 tmpctx->applet->name);
6030 }
Willy Tarreaubc174aa2012-11-19 16:10:32 +01006031
Willy Tarreau284ddbf2013-12-01 20:45:00 +01006032 if ((conn = objt_conn(sess->si[1].end)) != NULL) {
Willy Tarreaucf644ed2013-09-29 17:19:56 +02006033 chunk_appendf(&trash,
6034 " co1=%p ctrl=%s xprt=%s data=%s target=%s:%p\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006035 conn,
6036 get_conn_ctrl_name(conn),
6037 get_conn_xprt_name(conn),
6038 get_conn_data_name(conn),
6039 obj_type_name(conn->target),
6040 obj_base_ptr(conn->target));
Willy Tarreau909d5172012-11-26 03:04:41 +01006041
Willy Tarreaucf644ed2013-09-29 17:19:56 +02006042 chunk_appendf(&trash,
Willy Tarreauceeafb52016-01-25 15:27:17 +01006043 " flags=0x%08x fd=%d fd.state=%02x fd.cache=%d updt=%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006044 conn->flags,
6045 conn->t.sock.fd,
Willy Tarreau69a41fa2014-01-20 11:02:59 +01006046 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].state : 0,
Willy Tarreau15a4dec2014-01-20 11:09:39 +01006047 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].cache : 0,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006048 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].updated : 0);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02006049 }
Willy Tarreau284ddbf2013-12-01 20:45:00 +01006050 else if ((tmpctx = objt_appctx(sess->si[1].end)) != NULL) {
6051 chunk_appendf(&trash,
6052 " app1=%p st0=%d st1=%d st2=%d applet=%s\n",
6053 tmpctx,
6054 tmpctx->st0,
6055 tmpctx->st1,
6056 tmpctx->st2,
6057 tmpctx->applet->name);
6058 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006059
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006060 chunk_appendf(&trash,
Willy Tarreau909d5172012-11-26 03:04:41 +01006061 " req=%p (f=0x%06x an=0x%x pipe=%d tofwd=%d total=%lld)\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006062 " an_exp=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006063 &sess->req,
6064 sess->req.flags, sess->req.analysers,
6065 sess->req.pipe ? sess->req.pipe->data : 0,
6066 sess->req.to_forward, sess->req.total,
6067 sess->req.analyse_exp ?
6068 human_time(TICKS_TO_MS(sess->req.analyse_exp - now_ms),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006069 TICKS_TO_MS(1000)) : "<NEVER>");
6070
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006071 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006072 " rex=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006073 sess->req.rex ?
6074 human_time(TICKS_TO_MS(sess->req.rex - now_ms),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006075 TICKS_TO_MS(1000)) : "<NEVER>");
6076
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006077 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006078 " wex=%s\n"
Willy Tarreau909d5172012-11-26 03:04:41 +01006079 " buf=%p data=%p o=%d p=%d req.next=%d i=%d size=%d\n",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006080 sess->req.wex ?
6081 human_time(TICKS_TO_MS(sess->req.wex - now_ms),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006082 TICKS_TO_MS(1000)) : "<NEVER>",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006083 sess->req.buf,
6084 sess->req.buf->data, sess->req.buf->o,
6085 (int)(sess->req.buf->p - sess->req.buf->data),
Willy Tarreaueee5b512015-04-03 23:46:31 +02006086 sess->txn ? sess->txn->req.next : 0, sess->req.buf->i,
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006087 sess->req.buf->size);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006088
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006089 chunk_appendf(&trash,
Willy Tarreau909d5172012-11-26 03:04:41 +01006090 " res=%p (f=0x%06x an=0x%x pipe=%d tofwd=%d total=%lld)\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006091 " an_exp=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006092 &sess->res,
6093 sess->res.flags, sess->res.analysers,
6094 sess->res.pipe ? sess->res.pipe->data : 0,
6095 sess->res.to_forward, sess->res.total,
6096 sess->res.analyse_exp ?
6097 human_time(TICKS_TO_MS(sess->res.analyse_exp - now_ms),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006098 TICKS_TO_MS(1000)) : "<NEVER>");
6099
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006100 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006101 " rex=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006102 sess->res.rex ?
6103 human_time(TICKS_TO_MS(sess->res.rex - now_ms),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006104 TICKS_TO_MS(1000)) : "<NEVER>");
6105
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006106 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006107 " wex=%s\n"
Willy Tarreau909d5172012-11-26 03:04:41 +01006108 " buf=%p data=%p o=%d p=%d rsp.next=%d i=%d size=%d\n",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006109 sess->res.wex ?
6110 human_time(TICKS_TO_MS(sess->res.wex - now_ms),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006111 TICKS_TO_MS(1000)) : "<NEVER>",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006112 sess->res.buf,
6113 sess->res.buf->data, sess->res.buf->o,
6114 (int)(sess->res.buf->p - sess->res.buf->data),
Willy Tarreaueee5b512015-04-03 23:46:31 +02006115 sess->txn ? sess->txn->rsp.next : 0, sess->res.buf->i,
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006116 sess->res.buf->size);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006117
Willy Tarreaubc18da12015-03-13 14:00:47 +01006118 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02006119 si_applet_cant_put(si);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006120 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01006121 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006122
6123 /* use other states to dump the contents */
6124 }
6125 /* end of dump */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006126 appctx->ctx.sess.uid = 0;
6127 appctx->ctx.sess.section = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006128 return 1;
6129}
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006130
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02006131#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
6132static int stats_tlskeys_list(struct stream_interface *si) {
6133 struct appctx *appctx = __objt_appctx(si->end);
William Lallemand1d0b36a2016-05-20 17:40:26 +02006134 struct tls_keys_ref *ref;
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02006135
6136 switch (appctx->st2) {
6137 case STAT_ST_INIT:
6138 /* Display the column headers. If the message cannot be sent,
6139 * quit the fucntion with returning 0. The function is called
6140 * later and restart at the state "STAT_ST_INIT".
6141 */
6142 chunk_reset(&trash);
William Lallemand1d0b36a2016-05-20 17:40:26 +02006143
6144 if (appctx->st0 == STAT_CLI_O_TLSK_ENT)
6145 chunk_appendf(&trash, "# id secret\n");
6146 else
6147 chunk_appendf(&trash, "# id (file)\n");
6148
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02006149 if (bi_putchk(si_ic(si), &trash) == -1) {
6150 si_applet_cant_put(si);
6151 return 0;
6152 }
6153
William Lallemand1d0b36a2016-05-20 17:40:26 +02006154 ref = appctx->ctx.tlskeys.ref;
6155
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02006156 /* Now, we start the browsing of the references lists.
6157 * Note that the following call to LIST_ELEM return bad pointer. The only
Cyril Bontédb98eb32016-05-06 12:18:50 +02006158 * available field of this pointer is <list>. It is used with the function
6159 * tlskeys_list_get_next() for retruning the first available entry
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02006160 */
William Lallemand1d0b36a2016-05-20 17:40:26 +02006161 if (ref == NULL) {
6162 ref = LIST_ELEM(&tlskeys_reference, struct tls_keys_ref *, list);
6163 ref = tlskeys_list_get_next(ref, &tlskeys_reference);
6164 }
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02006165
6166 appctx->st2 = STAT_ST_LIST;
6167 /* fall through */
6168
6169 case STAT_ST_LIST:
William Lallemand1d0b36a2016-05-20 17:40:26 +02006170 while (ref) {
6171 int i;
6172 int head = ref->tls_ticket_enc_index;
6173
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02006174 chunk_reset(&trash);
William Lallemand1d0b36a2016-05-20 17:40:26 +02006175 if (appctx->st0 == STAT_CLI_O_TLSK_ENT)
6176 chunk_appendf(&trash, "# ");
6177 chunk_appendf(&trash, "%d (%s)\n", ref->unique_id,
6178 ref->filename);
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02006179
William Lallemand1d0b36a2016-05-20 17:40:26 +02006180 if (appctx->st0 == STAT_CLI_O_TLSK_ENT) {
6181 for (i = 0; i < TLS_TICKETS_NO; i++) {
6182 struct chunk *t2 = get_trash_chunk();
6183 int b64_len;
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02006184
William Lallemand1d0b36a2016-05-20 17:40:26 +02006185 chunk_reset(t2);
6186 b64_len = a2base64((char *)(ref->tlskeys + (head + 2 + i) % TLS_TICKETS_NO),
6187 sizeof(struct tls_sess_key), t2->str, t2->size);
6188 if (b64_len < 0)
6189 return 0;
6190 t2->len = b64_len;
6191 chunk_appendf(&trash, "%d.%d %s\n", ref->unique_id, i, t2->str);
6192 }
6193 }
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02006194 if (bi_putchk(si_ic(si), &trash) == -1) {
6195 /* let's try again later from this stream. We add ourselves into
6196 * this stream's users so that it can remove us upon termination.
6197 */
6198 si_applet_cant_put(si);
6199 return 0;
6200 }
6201
William Lallemand1d0b36a2016-05-20 17:40:26 +02006202 if (appctx->ctx.tlskeys.ref) /* don't display everything if don't null */
6203 break;
6204
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02006205 /* get next list entry and check the end of the list */
William Lallemand1d0b36a2016-05-20 17:40:26 +02006206 ref = tlskeys_list_get_next(ref, &tlskeys_reference);
6207
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02006208 }
6209
6210 appctx->st2 = STAT_ST_FIN;
6211 /* fall through */
6212
6213 default:
6214 appctx->st2 = STAT_ST_FIN;
6215 return 1;
6216 }
6217 return 0;
6218}
6219#endif
6220
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006221static int stats_pats_list(struct stream_interface *si)
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006222{
6223 struct appctx *appctx = __objt_appctx(si->end);
6224
6225 switch (appctx->st2) {
6226 case STAT_ST_INIT:
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01006227 /* Display the column headers. If the message cannot be sent,
6228 * quit the fucntion with returning 0. The function is called
6229 * later and restart at the state "STAT_ST_INIT".
6230 */
6231 chunk_reset(&trash);
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01006232 chunk_appendf(&trash, "# id (file) description\n");
Willy Tarreaubc18da12015-03-13 14:00:47 +01006233 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02006234 si_applet_cant_put(si);
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01006235 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01006236 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006237
6238 /* Now, we start the browsing of the references lists.
6239 * Note that the following call to LIST_ELEM return bad pointer. The only
Cyril Bontédb98eb32016-05-06 12:18:50 +02006240 * available field of this pointer is <list>. It is used with the function
6241 * pat_list_get_next() for retruning the first available entry
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006242 */
6243 appctx->ctx.map.ref = LIST_ELEM(&pattern_reference, struct pat_ref *, list);
6244 appctx->ctx.map.ref = pat_list_get_next(appctx->ctx.map.ref, &pattern_reference,
6245 appctx->ctx.map.display_flags);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006246 appctx->st2 = STAT_ST_LIST;
6247 /* fall through */
6248
6249 case STAT_ST_LIST:
6250 while (appctx->ctx.map.ref) {
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006251 chunk_reset(&trash);
6252
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006253 /* Build messages. If the reference is used by another category than
6254 * the listed categorie, display the information in the massage.
6255 */
Thierry FOURNIERf7e04e92014-03-20 11:45:47 +01006256 chunk_appendf(&trash, "%d (%s) %s\n", appctx->ctx.map.ref->unique_id,
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01006257 appctx->ctx.map.ref->reference ? appctx->ctx.map.ref->reference : "",
6258 appctx->ctx.map.ref->display);
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01006259
Willy Tarreau2bb4a962014-11-28 11:11:05 +01006260 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau87b09662015-04-03 00:22:06 +02006261 /* let's try again later from this stream. We add ourselves into
6262 * this stream's users so that it can remove us upon termination.
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006263 */
Willy Tarreaufe127932015-04-21 19:23:39 +02006264 si_applet_cant_put(si);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006265 return 0;
6266 }
6267
6268 /* get next list entry and check the end of the list */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006269 appctx->ctx.map.ref = pat_list_get_next(appctx->ctx.map.ref, &pattern_reference,
6270 appctx->ctx.map.display_flags);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006271 }
6272
6273 appctx->st2 = STAT_ST_FIN;
6274 /* fall through */
6275
6276 default:
6277 appctx->st2 = STAT_ST_FIN;
6278 return 1;
6279 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006280 return 0;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006281}
6282
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006283static int stats_map_lookup(struct stream_interface *si)
6284{
6285 struct appctx *appctx = __objt_appctx(si->end);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006286 struct sample sample;
6287 struct pattern *pat;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006288 int match_method;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006289
6290 switch (appctx->st2) {
6291 case STAT_ST_INIT:
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006292 /* Init to the first entry. The list cannot be change */
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01006293 appctx->ctx.map.expr = LIST_ELEM(&appctx->ctx.map.ref->pat, struct pattern_expr *, list);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006294 appctx->ctx.map.expr = pat_expr_get_next(appctx->ctx.map.expr, &appctx->ctx.map.ref->pat);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006295 appctx->st2 = STAT_ST_LIST;
6296 /* fall through */
6297
6298 case STAT_ST_LIST:
6299 /* for each lookup type */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006300 while (appctx->ctx.map.expr) {
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006301 /* initialise chunk to build new message */
6302 chunk_reset(&trash);
6303
6304 /* execute pattern matching */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02006305 sample.data.type = SMP_T_STR;
Andreas Seltenreich9727cf42016-03-03 19:32:25 +01006306 sample.flags = SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02006307 sample.data.u.str.len = appctx->ctx.map.chunk.len;
6308 sample.data.u.str.str = appctx->ctx.map.chunk.str;
Thierry FOURNIER5d344082014-01-27 14:19:53 +01006309 if (appctx->ctx.map.expr->pat_head->match &&
6310 sample_convert(&sample, appctx->ctx.map.expr->pat_head->expect_type))
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006311 pat = appctx->ctx.map.expr->pat_head->match(&sample, appctx->ctx.map.expr, 1);
6312 else
6313 pat = NULL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006314
6315 /* build return message: set type of match */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006316 for (match_method=0; match_method<PAT_MATCH_NUM; match_method++)
6317 if (appctx->ctx.map.expr->pat_head->match == pat_match_fcts[match_method])
6318 break;
6319 if (match_method >= PAT_MATCH_NUM)
6320 chunk_appendf(&trash, "type=unknown(%p)", appctx->ctx.map.expr->pat_head->match);
6321 else
6322 chunk_appendf(&trash, "type=%s", pat_match_names[match_method]);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006323
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02006324 /* case sensitive */
6325 if (appctx->ctx.map.expr->mflags & PAT_MF_IGNORE_CASE)
6326 chunk_appendf(&trash, ", case=insensitive");
6327 else
6328 chunk_appendf(&trash, ", case=sensitive");
6329
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006330 /* Display no match, and set default value */
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01006331 if (!pat) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006332 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
6333 chunk_appendf(&trash, ", found=no");
6334 else
6335 chunk_appendf(&trash, ", match=no");
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006336 }
6337
6338 /* Display match and match info */
6339 else {
6340 /* display match */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006341 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
6342 chunk_appendf(&trash, ", found=yes");
6343 else
6344 chunk_appendf(&trash, ", match=yes");
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006345
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01006346 /* display index mode */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02006347 if (pat->sflags & PAT_SF_TREE)
Thierry FOURNIERb9903842014-03-11 18:48:17 +01006348 chunk_appendf(&trash, ", idx=tree");
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006349 else
Thierry FOURNIERb9903842014-03-11 18:48:17 +01006350 chunk_appendf(&trash, ", idx=list");
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006351
Thierry FOURNIER86db66a2014-03-11 18:37:58 +01006352 /* display pattern */
6353 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
6354 if (pat->ref && pat->ref->pattern)
6355 chunk_appendf(&trash, ", key=\"%s\"", pat->ref->pattern);
6356 else
6357 chunk_appendf(&trash, ", key=unknown");
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006358 }
6359 else {
Thierry FOURNIER86db66a2014-03-11 18:37:58 +01006360 if (pat->ref && pat->ref->pattern)
6361 chunk_appendf(&trash, ", pattern=\"%s\"", pat->ref->pattern);
6362 else
6363 chunk_appendf(&trash, ", pattern=unknown");
6364 }
6365
6366 /* display return value */
6367 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
Thierry FOURNIER503bb092015-08-19 08:35:43 +02006368 if (pat->data && pat->ref && pat->ref->sample)
Thierry FOURNIER03d0e452015-07-23 18:33:41 +02006369 chunk_appendf(&trash, ", value=\"%s\", type=\"%s\"", pat->ref->sample,
Thierry FOURNIER7d4335c2015-08-19 09:05:25 +02006370 smp_to_type[pat->data->type]);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006371 else
Thierry FOURNIER86db66a2014-03-11 18:37:58 +01006372 chunk_appendf(&trash, ", value=none");
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006373 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006374 }
6375
Thierry FOURNIERb9903842014-03-11 18:48:17 +01006376 chunk_appendf(&trash, "\n");
6377
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006378 /* display response */
Willy Tarreau2bb4a962014-11-28 11:11:05 +01006379 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau87b09662015-04-03 00:22:06 +02006380 /* let's try again later from this stream. We add ourselves into
6381 * this stream's users so that it can remove us upon termination.
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006382 */
Willy Tarreaufe127932015-04-21 19:23:39 +02006383 si_applet_cant_put(si);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006384 return 0;
6385 }
6386
6387 /* get next entry */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006388 appctx->ctx.map.expr = pat_expr_get_next(appctx->ctx.map.expr,
6389 &appctx->ctx.map.ref->pat);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006390 }
6391
6392 appctx->st2 = STAT_ST_FIN;
6393 /* fall through */
6394
6395 default:
6396 appctx->st2 = STAT_ST_FIN;
6397 free(appctx->ctx.map.chunk.str);
6398 return 1;
6399 }
6400}
6401
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006402static int stats_pat_list(struct stream_interface *si)
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006403{
6404 struct appctx *appctx = __objt_appctx(si->end);
6405
6406 switch (appctx->st2) {
6407
6408 case STAT_ST_INIT:
6409 /* Init to the first entry. The list cannot be change */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006410 appctx->ctx.map.elt = LIST_NEXT(&appctx->ctx.map.ref->head,
6411 struct pat_ref_elt *, list);
6412 if (&appctx->ctx.map.elt->list == &appctx->ctx.map.ref->head)
6413 appctx->ctx.map.elt = NULL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006414 appctx->st2 = STAT_ST_LIST;
6415 /* fall through */
6416
6417 case STAT_ST_LIST:
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006418 while (appctx->ctx.map.elt) {
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006419 chunk_reset(&trash);
6420
6421 /* build messages */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006422 if (appctx->ctx.map.elt->sample)
Thierry FOURNIER9356c682014-01-28 15:55:37 +01006423 chunk_appendf(&trash, "%p %s %s\n",
6424 appctx->ctx.map.elt, appctx->ctx.map.elt->pattern,
6425 appctx->ctx.map.elt->sample);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006426 else
Thierry FOURNIER9356c682014-01-28 15:55:37 +01006427 chunk_appendf(&trash, "%p %s\n",
6428 appctx->ctx.map.elt, appctx->ctx.map.elt->pattern);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006429
Willy Tarreau2bb4a962014-11-28 11:11:05 +01006430 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau87b09662015-04-03 00:22:06 +02006431 /* let's try again later from this stream. We add ourselves into
6432 * this stream's users so that it can remove us upon termination.
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006433 */
Willy Tarreaufe127932015-04-21 19:23:39 +02006434 si_applet_cant_put(si);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006435 return 0;
6436 }
6437
6438 /* get next list entry and check the end of the list */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006439 appctx->ctx.map.elt = LIST_NEXT(&appctx->ctx.map.elt->list,
6440 struct pat_ref_elt *, list);
6441 if (&appctx->ctx.map.elt->list == &appctx->ctx.map.ref->head)
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006442 break;
6443 }
6444
6445 appctx->st2 = STAT_ST_FIN;
6446 /* fall through */
6447
6448 default:
6449 appctx->st2 = STAT_ST_FIN;
6450 return 1;
6451 }
6452}
6453
Willy Tarreau87b09662015-04-03 00:22:06 +02006454/* This function dumps all streams' states onto the stream interface's
Willy Tarreau306f8302013-07-08 15:53:06 +02006455 * read buffer. It returns 0 if the output buffer is full and it needs
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01006456 * to be called again, otherwise non-zero. It is designed to be called
6457 * from stats_dump_sess_to_buffer() below.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006458 */
Simon Horman9bd2c732011-06-15 15:18:44 +09006459static int stats_dump_sess_to_buffer(struct stream_interface *si)
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006460{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006461 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006462 struct connection *conn;
6463
Willy Tarreau2bb4a962014-11-28 11:11:05 +01006464 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW))) {
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006465 /* If we're forced to shut down, we might have to remove our
Willy Tarreau87b09662015-04-03 00:22:06 +02006466 * reference to the last stream being dumped.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006467 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006468 if (appctx->st2 == STAT_ST_LIST) {
6469 if (!LIST_ISEMPTY(&appctx->ctx.sess.bref.users)) {
6470 LIST_DEL(&appctx->ctx.sess.bref.users);
6471 LIST_INIT(&appctx->ctx.sess.bref.users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +01006472 }
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006473 }
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02006474 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006475 }
6476
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006477 chunk_reset(&trash);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006478
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006479 switch (appctx->st2) {
Willy Tarreau295a8372011-03-10 11:25:07 +01006480 case STAT_ST_INIT:
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006481 /* the function had not been called yet, let's prepare the
Willy Tarreau87b09662015-04-03 00:22:06 +02006482 * buffer for a response. We initialize the current stream
Willy Tarreaufd3828e2009-02-22 15:17:24 +01006483 * pointer to the first in the global list. When a target
Willy Tarreau87b09662015-04-03 00:22:06 +02006484 * stream is being destroyed, it is responsible for updating
Willy Tarreaufd3828e2009-02-22 15:17:24 +01006485 * this pointer. We know we have reached the end when this
Willy Tarreau87b09662015-04-03 00:22:06 +02006486 * pointer points back to the head of the streams list.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006487 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006488 LIST_INIT(&appctx->ctx.sess.bref.users);
Willy Tarreau87b09662015-04-03 00:22:06 +02006489 appctx->ctx.sess.bref.ref = streams.n;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006490 appctx->st2 = STAT_ST_LIST;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006491 /* fall through */
6492
Willy Tarreau295a8372011-03-10 11:25:07 +01006493 case STAT_ST_LIST:
Willy Tarreau87b09662015-04-03 00:22:06 +02006494 /* first, let's detach the back-ref from a possible previous stream */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006495 if (!LIST_ISEMPTY(&appctx->ctx.sess.bref.users)) {
6496 LIST_DEL(&appctx->ctx.sess.bref.users);
6497 LIST_INIT(&appctx->ctx.sess.bref.users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +01006498 }
6499
6500 /* and start from where we stopped */
Willy Tarreau87b09662015-04-03 00:22:06 +02006501 while (appctx->ctx.sess.bref.ref != &streams) {
Cyril Bontéacd7d632010-11-01 19:26:02 +01006502 char pn[INET6_ADDRSTRLEN];
Willy Tarreau87b09662015-04-03 00:22:06 +02006503 struct stream *curr_sess;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006504
Willy Tarreau87b09662015-04-03 00:22:06 +02006505 curr_sess = LIST_ELEM(appctx->ctx.sess.bref.ref, struct stream *, list);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006506
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006507 if (appctx->ctx.sess.target) {
6508 if (appctx->ctx.sess.target != (void *)-1 && appctx->ctx.sess.target != curr_sess)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006509 goto next_sess;
6510
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006511 LIST_ADDQ(&curr_sess->back_refs, &appctx->ctx.sess.bref.users);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006512 /* call the proper dump() function and return if we're missing space */
Willy Tarreau76153662012-11-26 01:16:39 +01006513 if (!stats_dump_full_sess_to_buffer(si, curr_sess))
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006514 return 0;
6515
Willy Tarreau87b09662015-04-03 00:22:06 +02006516 /* stream dump complete */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006517 LIST_DEL(&appctx->ctx.sess.bref.users);
6518 LIST_INIT(&appctx->ctx.sess.bref.users);
6519 if (appctx->ctx.sess.target != (void *)-1) {
6520 appctx->ctx.sess.target = NULL;
Willy Tarreau76153662012-11-26 01:16:39 +01006521 break;
6522 }
6523 else
6524 goto next_sess;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006525 }
6526
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006527 chunk_appendf(&trash,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006528 "%p: proto=%s",
6529 curr_sess,
Willy Tarreauf1e02122015-09-23 12:16:43 +02006530 strm_li(curr_sess) ? strm_li(curr_sess)->proto->name : "?");
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006531
Willy Tarreaud0d8da92015-04-04 02:10:38 +02006532 conn = objt_conn(strm_orig(curr_sess));
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006533 switch (conn ? addr_to_str(&conn->addr.from, pn, sizeof(pn)) : AF_UNSPEC) {
Willy Tarreau631f01c2011-09-05 00:36:48 +02006534 case AF_INET:
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006535 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006536 chunk_appendf(&trash,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006537 " src=%s:%d fe=%s be=%s srv=%s",
6538 pn,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006539 get_host_port(&conn->addr.from),
Willy Tarreaud0d8da92015-04-04 02:10:38 +02006540 strm_fe(curr_sess)->id,
Willy Tarreau50943332011-09-02 17:33:05 +02006541 (curr_sess->be->cap & PR_CAP_BE) ? curr_sess->be->id : "<NONE>",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006542 objt_server(curr_sess->target) ? objt_server(curr_sess->target)->id : "<none>"
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006543 );
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006544 break;
6545 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006546 chunk_appendf(&trash,
Emeric Brun837ca522010-10-22 16:19:01 +02006547 " src=unix:%d fe=%s be=%s srv=%s",
Willy Tarreaud0d8da92015-04-04 02:10:38 +02006548 strm_li(curr_sess)->luid,
6549 strm_fe(curr_sess)->id,
Willy Tarreau50943332011-09-02 17:33:05 +02006550 (curr_sess->be->cap & PR_CAP_BE) ? curr_sess->be->id : "<NONE>",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006551 objt_server(curr_sess->target) ? objt_server(curr_sess->target)->id : "<none>"
Emeric Brun837ca522010-10-22 16:19:01 +02006552 );
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006553 break;
6554 }
6555
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006556 chunk_appendf(&trash,
Willy Tarreau65671ab2009-10-04 14:24:59 +02006557 " ts=%02x age=%s calls=%d",
6558 curr_sess->task->state,
Willy Tarreau3884cba2009-03-28 17:54:35 +01006559 human_time(now.tv_sec - curr_sess->logs.tv_accept.tv_sec, 1),
6560 curr_sess->task->calls);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006561
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006562 chunk_appendf(&trash,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01006563 " rq[f=%06xh,i=%d,an=%02xh,rx=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006564 curr_sess->req.flags,
6565 curr_sess->req.buf->i,
6566 curr_sess->req.analysers,
6567 curr_sess->req.rex ?
6568 human_time(TICKS_TO_MS(curr_sess->req.rex - now_ms),
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006569 TICKS_TO_MS(1000)) : "");
6570
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006571 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006572 ",wx=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006573 curr_sess->req.wex ?
6574 human_time(TICKS_TO_MS(curr_sess->req.wex - now_ms),
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006575 TICKS_TO_MS(1000)) : "");
6576
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006577 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006578 ",ax=%s]",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006579 curr_sess->req.analyse_exp ?
6580 human_time(TICKS_TO_MS(curr_sess->req.analyse_exp - now_ms),
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006581 TICKS_TO_MS(1000)) : "");
6582
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006583 chunk_appendf(&trash,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01006584 " rp[f=%06xh,i=%d,an=%02xh,rx=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006585 curr_sess->res.flags,
6586 curr_sess->res.buf->i,
6587 curr_sess->res.analysers,
6588 curr_sess->res.rex ?
6589 human_time(TICKS_TO_MS(curr_sess->res.rex - now_ms),
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006590 TICKS_TO_MS(1000)) : "");
6591
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006592 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006593 ",wx=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006594 curr_sess->res.wex ?
6595 human_time(TICKS_TO_MS(curr_sess->res.wex - now_ms),
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006596 TICKS_TO_MS(1000)) : "");
6597
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006598 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006599 ",ax=%s]",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006600 curr_sess->res.analyse_exp ?
6601 human_time(TICKS_TO_MS(curr_sess->res.analyse_exp - now_ms),
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006602 TICKS_TO_MS(1000)) : "");
6603
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006604 conn = objt_conn(curr_sess->si[0].end);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006605 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006606 " s0=[%d,%1xh,fd=%d,ex=%s]",
6607 curr_sess->si[0].state,
6608 curr_sess->si[0].flags,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006609 conn ? conn->t.sock.fd : -1,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006610 curr_sess->si[0].exp ?
6611 human_time(TICKS_TO_MS(curr_sess->si[0].exp - now_ms),
6612 TICKS_TO_MS(1000)) : "");
6613
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006614 conn = objt_conn(curr_sess->si[1].end);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006615 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006616 " s1=[%d,%1xh,fd=%d,ex=%s]",
6617 curr_sess->si[1].state,
6618 curr_sess->si[1].flags,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006619 conn ? conn->t.sock.fd : -1,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006620 curr_sess->si[1].exp ?
6621 human_time(TICKS_TO_MS(curr_sess->si[1].exp - now_ms),
6622 TICKS_TO_MS(1000)) : "");
6623
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006624 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006625 " exp=%s",
6626 curr_sess->task->expire ?
6627 human_time(TICKS_TO_MS(curr_sess->task->expire - now_ms),
6628 TICKS_TO_MS(1000)) : "");
Willy Tarreau4726f532009-03-07 17:25:21 +01006629 if (task_in_rq(curr_sess->task))
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006630 chunk_appendf(&trash, " run(nice=%d)", curr_sess->task->nice);
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006631
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006632 chunk_appendf(&trash, "\n");
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006633
Willy Tarreau2bb4a962014-11-28 11:11:05 +01006634 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau87b09662015-04-03 00:22:06 +02006635 /* let's try again later from this stream. We add ourselves into
6636 * this stream's users so that it can remove us upon termination.
Willy Tarreaufd3828e2009-02-22 15:17:24 +01006637 */
Willy Tarreaufe127932015-04-21 19:23:39 +02006638 si_applet_cant_put(si);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006639 LIST_ADDQ(&curr_sess->back_refs, &appctx->ctx.sess.bref.users);
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02006640 return 0;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006641 }
6642
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006643 next_sess:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006644 appctx->ctx.sess.bref.ref = curr_sess->list.n;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006645 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006646
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006647 if (appctx->ctx.sess.target && appctx->ctx.sess.target != (void *)-1) {
Willy Tarreau87b09662015-04-03 00:22:06 +02006648 /* specified stream not found */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006649 if (appctx->ctx.sess.section > 0)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006650 chunk_appendf(&trash, " *** session terminated while we were watching it ***\n");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006651 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006652 chunk_appendf(&trash, "Session not found.\n");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006653
Willy Tarreaubc18da12015-03-13 14:00:47 +01006654 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02006655 si_applet_cant_put(si);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006656 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01006657 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006658
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006659 appctx->ctx.sess.target = NULL;
6660 appctx->ctx.sess.uid = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006661 return 1;
6662 }
6663
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006664 appctx->st2 = STAT_ST_FIN;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006665 /* fall through */
6666
6667 default:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006668 appctx->st2 = STAT_ST_FIN;
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02006669 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006670 }
Emeric Brun1e029aa2010-09-23 18:12:53 +02006671}
6672
Willy Tarreau5f9a8772012-11-26 02:22:40 +01006673/* This is called when the stream interface is closed. For instance, upon an
6674 * external abort, we won't call the i/o handler anymore so we may need to
Willy Tarreau87b09662015-04-03 00:22:06 +02006675 * remove back references to the stream currently being dumped.
Willy Tarreau5f9a8772012-11-26 02:22:40 +01006676 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02006677static void cli_release_handler(struct appctx *appctx)
Willy Tarreau5f9a8772012-11-26 02:22:40 +01006678{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006679 if (appctx->st0 == STAT_CLI_O_SESS && appctx->st2 == STAT_ST_LIST) {
6680 if (!LIST_ISEMPTY(&appctx->ctx.sess.bref.users))
6681 LIST_DEL(&appctx->ctx.sess.bref.users);
Willy Tarreau5f9a8772012-11-26 02:22:40 +01006682 }
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01006683 else if (appctx->st0 == STAT_CLI_PRINT_FREE) {
6684 free(appctx->ctx.cli.err);
Willy Tarreau6457d0f2015-10-07 20:00:24 +02006685 appctx->ctx.cli.err = NULL;
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01006686 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006687 else if (appctx->st0 == STAT_CLI_O_MLOOK) {
6688 free(appctx->ctx.map.chunk.str);
Willy Tarreau6457d0f2015-10-07 20:00:24 +02006689 appctx->ctx.map.chunk.str = NULL;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006690 }
Willy Tarreau5f9a8772012-11-26 02:22:40 +01006691}
6692
Willy Tarreau20e99322013-04-13 09:22:25 +02006693/* This function is used to either dump tables states (when action is set
6694 * to STAT_CLI_O_TAB) or clear tables (when action is STAT_CLI_O_CLR).
Willy Tarreau20e99322013-04-13 09:22:25 +02006695 * It returns 0 if the output buffer is full and it needs to be called
6696 * again, otherwise non-zero.
Willy Tarreau69f58c82010-07-12 17:55:33 +02006697 */
Willy Tarreau20e99322013-04-13 09:22:25 +02006698static int stats_table_request(struct stream_interface *si, int action)
Willy Tarreau69f58c82010-07-12 17:55:33 +02006699{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006700 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau87b09662015-04-03 00:22:06 +02006701 struct stream *s = si_strm(si);
Willy Tarreau69f58c82010-07-12 17:55:33 +02006702 struct ebmb_node *eb;
6703 int dt;
Willy Tarreau44455022012-12-05 23:01:12 +01006704 int skip_entry;
Willy Tarreau20e99322013-04-13 09:22:25 +02006705 int show = action == STAT_CLI_O_TAB;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006706
6707 /*
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006708 * We have 3 possible states in appctx->st2 :
Willy Tarreau295a8372011-03-10 11:25:07 +01006709 * - STAT_ST_INIT : the first call
6710 * - STAT_ST_INFO : the proxy pointer points to the next table to
Willy Tarreau69f58c82010-07-12 17:55:33 +02006711 * dump, the entry pointer is NULL ;
Willy Tarreau295a8372011-03-10 11:25:07 +01006712 * - STAT_ST_LIST : the proxy pointer points to the current table
Willy Tarreau69f58c82010-07-12 17:55:33 +02006713 * and the entry pointer points to the next entry to be dumped,
6714 * and the refcount on the next entry is held ;
Willy Tarreau295a8372011-03-10 11:25:07 +01006715 * - STAT_ST_END : nothing left to dump, the buffer may contain some
Willy Tarreau69f58c82010-07-12 17:55:33 +02006716 * data though.
6717 */
6718
Willy Tarreau2bb4a962014-11-28 11:11:05 +01006719 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW))) {
Willy Tarreau69f58c82010-07-12 17:55:33 +02006720 /* in case of abort, remove any refcount we might have set on an entry */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006721 if (appctx->st2 == STAT_ST_LIST) {
6722 appctx->ctx.table.entry->ref_cnt--;
6723 stksess_kill_if_expired(&appctx->ctx.table.proxy->table, appctx->ctx.table.entry);
Willy Tarreauf6efda12010-08-03 20:34:06 +02006724 }
Willy Tarreau69f58c82010-07-12 17:55:33 +02006725 return 1;
6726 }
6727
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006728 chunk_reset(&trash);
Willy Tarreau69f58c82010-07-12 17:55:33 +02006729
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006730 while (appctx->st2 != STAT_ST_FIN) {
6731 switch (appctx->st2) {
Willy Tarreau295a8372011-03-10 11:25:07 +01006732 case STAT_ST_INIT:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006733 appctx->ctx.table.proxy = appctx->ctx.table.target;
6734 if (!appctx->ctx.table.proxy)
6735 appctx->ctx.table.proxy = proxy;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006736
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006737 appctx->ctx.table.entry = NULL;
6738 appctx->st2 = STAT_ST_INFO;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006739 break;
6740
Willy Tarreau295a8372011-03-10 11:25:07 +01006741 case STAT_ST_INFO:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006742 if (!appctx->ctx.table.proxy ||
6743 (appctx->ctx.table.target &&
6744 appctx->ctx.table.proxy != appctx->ctx.table.target)) {
6745 appctx->st2 = STAT_ST_END;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006746 break;
6747 }
6748
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006749 if (appctx->ctx.table.proxy->table.size) {
6750 if (show && !stats_dump_table_head_to_buffer(&trash, si, appctx->ctx.table.proxy,
6751 appctx->ctx.table.target))
Willy Tarreau69f58c82010-07-12 17:55:33 +02006752 return 0;
6753
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006754 if (appctx->ctx.table.target &&
Willy Tarreaud0d8da92015-04-04 02:10:38 +02006755 strm_li(s)->bind_conf->level >= ACCESS_LVL_OPER) {
Willy Tarreau69f58c82010-07-12 17:55:33 +02006756 /* dump entries only if table explicitly requested */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006757 eb = ebmb_first(&appctx->ctx.table.proxy->table.keys);
Willy Tarreau69f58c82010-07-12 17:55:33 +02006758 if (eb) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006759 appctx->ctx.table.entry = ebmb_entry(eb, struct stksess, key);
6760 appctx->ctx.table.entry->ref_cnt++;
6761 appctx->st2 = STAT_ST_LIST;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006762 break;
6763 }
6764 }
6765 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006766 appctx->ctx.table.proxy = appctx->ctx.table.proxy->next;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006767 break;
6768
Willy Tarreau295a8372011-03-10 11:25:07 +01006769 case STAT_ST_LIST:
Willy Tarreau44455022012-12-05 23:01:12 +01006770 skip_entry = 0;
Simon Hormanc88b8872011-06-15 15:18:49 +09006771
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006772 if (appctx->ctx.table.data_type >= 0) {
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02006773 /* we're filtering on some data contents */
6774 void *ptr;
6775 long long data;
6776
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006777 dt = appctx->ctx.table.data_type;
6778 ptr = stktable_data_ptr(&appctx->ctx.table.proxy->table,
6779 appctx->ctx.table.entry,
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02006780 dt);
6781
6782 data = 0;
6783 switch (stktable_data_types[dt].std_type) {
6784 case STD_T_SINT:
6785 data = stktable_data_cast(ptr, std_t_sint);
6786 break;
6787 case STD_T_UINT:
6788 data = stktable_data_cast(ptr, std_t_uint);
6789 break;
6790 case STD_T_ULL:
6791 data = stktable_data_cast(ptr, std_t_ull);
6792 break;
6793 case STD_T_FRQP:
6794 data = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006795 appctx->ctx.table.proxy->table.data_arg[dt].u);
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02006796 break;
6797 }
6798
6799 /* skip the entry if the data does not match the test and the value */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006800 if ((data < appctx->ctx.table.value &&
6801 (appctx->ctx.table.data_op == STD_OP_EQ ||
6802 appctx->ctx.table.data_op == STD_OP_GT ||
6803 appctx->ctx.table.data_op == STD_OP_GE)) ||
6804 (data == appctx->ctx.table.value &&
6805 (appctx->ctx.table.data_op == STD_OP_NE ||
6806 appctx->ctx.table.data_op == STD_OP_GT ||
6807 appctx->ctx.table.data_op == STD_OP_LT)) ||
6808 (data > appctx->ctx.table.value &&
6809 (appctx->ctx.table.data_op == STD_OP_EQ ||
6810 appctx->ctx.table.data_op == STD_OP_LT ||
6811 appctx->ctx.table.data_op == STD_OP_LE)))
Willy Tarreau44455022012-12-05 23:01:12 +01006812 skip_entry = 1;
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02006813 }
6814
Simon Hormanc88b8872011-06-15 15:18:49 +09006815 if (show && !skip_entry &&
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006816 !stats_dump_table_entry_to_buffer(&trash, si, appctx->ctx.table.proxy,
6817 appctx->ctx.table.entry))
Simon Hormand9366582011-06-15 15:18:45 +09006818 return 0;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006819
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006820 appctx->ctx.table.entry->ref_cnt--;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006821
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006822 eb = ebmb_next(&appctx->ctx.table.entry->key);
Willy Tarreau69f58c82010-07-12 17:55:33 +02006823 if (eb) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006824 struct stksess *old = appctx->ctx.table.entry;
6825 appctx->ctx.table.entry = ebmb_entry(eb, struct stksess, key);
Willy Tarreau8fa52f42012-01-09 11:50:03 +01006826 if (show)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006827 stksess_kill_if_expired(&appctx->ctx.table.proxy->table, old);
6828 else if (!skip_entry && !appctx->ctx.table.entry->ref_cnt)
6829 stksess_kill(&appctx->ctx.table.proxy->table, old);
6830 appctx->ctx.table.entry->ref_cnt++;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006831 break;
6832 }
6833
Simon Hormanc88b8872011-06-15 15:18:49 +09006834
6835 if (show)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006836 stksess_kill_if_expired(&appctx->ctx.table.proxy->table, appctx->ctx.table.entry);
6837 else if (!skip_entry && !appctx->ctx.table.entry->ref_cnt)
6838 stksess_kill(&appctx->ctx.table.proxy->table, appctx->ctx.table.entry);
Simon Hormanc88b8872011-06-15 15:18:49 +09006839
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006840 appctx->ctx.table.proxy = appctx->ctx.table.proxy->next;
6841 appctx->st2 = STAT_ST_INFO;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006842 break;
6843
Willy Tarreau295a8372011-03-10 11:25:07 +01006844 case STAT_ST_END:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006845 appctx->st2 = STAT_ST_FIN;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006846 break;
6847 }
6848 }
6849 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006850}
6851
Willy Tarreaud426a182010-03-05 14:58:26 +01006852/* print a line of text buffer (limited to 70 bytes) to <out>. The format is :
Willy Tarreau74808cb2009-03-04 15:53:18 +01006853 * <2 spaces> <offset=5 digits> <space or plus> <space> <70 chars max> <\n>
6854 * which is 60 chars per line. Non-printable chars \t, \n, \r and \e are
6855 * encoded in C format. Other non-printable chars are encoded "\xHH". Original
6856 * lines are respected within the limit of 70 output chars. Lines that are
6857 * continuation of a previous truncated line begin with "+" instead of " "
6858 * after the offset. The new pointer is returned.
6859 */
Willy Tarreaud426a182010-03-05 14:58:26 +01006860static int dump_text_line(struct chunk *out, const char *buf, int bsize, int len,
6861 int *line, int ptr)
Willy Tarreau74808cb2009-03-04 15:53:18 +01006862{
6863 int end;
6864 unsigned char c;
6865
6866 end = out->len + 80;
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02006867 if (end > out->size)
Willy Tarreau74808cb2009-03-04 15:53:18 +01006868 return ptr;
6869
Willy Tarreau77804732012-10-29 16:14:26 +01006870 chunk_appendf(out, " %05d%c ", ptr, (ptr == *line) ? ' ' : '+');
Willy Tarreau74808cb2009-03-04 15:53:18 +01006871
Willy Tarreaud426a182010-03-05 14:58:26 +01006872 while (ptr < len && ptr < bsize) {
6873 c = buf[ptr];
Willy Tarreau787bbd92009-03-12 08:18:33 +01006874 if (isprint(c) && isascii(c) && c != '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006875 if (out->len > end - 2)
6876 break;
6877 out->str[out->len++] = c;
Willy Tarreau787bbd92009-03-12 08:18:33 +01006878 } else if (c == '\t' || c == '\n' || c == '\r' || c == '\e' || c == '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006879 if (out->len > end - 3)
6880 break;
6881 out->str[out->len++] = '\\';
6882 switch (c) {
6883 case '\t': c = 't'; break;
6884 case '\n': c = 'n'; break;
6885 case '\r': c = 'r'; break;
6886 case '\e': c = 'e'; break;
Willy Tarreau787bbd92009-03-12 08:18:33 +01006887 case '\\': c = '\\'; break;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006888 }
6889 out->str[out->len++] = c;
6890 } else {
6891 if (out->len > end - 5)
6892 break;
6893 out->str[out->len++] = '\\';
6894 out->str[out->len++] = 'x';
6895 out->str[out->len++] = hextab[(c >> 4) & 0xF];
6896 out->str[out->len++] = hextab[c & 0xF];
6897 }
Willy Tarreaud426a182010-03-05 14:58:26 +01006898 if (buf[ptr++] == '\n') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006899 /* we had a line break, let's return now */
6900 out->str[out->len++] = '\n';
6901 *line = ptr;
6902 return ptr;
6903 }
6904 }
6905 /* we have an incomplete line, we return it as-is */
6906 out->str[out->len++] = '\n';
6907 return ptr;
6908}
6909
Baptiste Assmann3863f972015-05-17 00:33:24 +02006910/* This function dumps counters from all resolvers section and associated name servers.
6911 * It returns 0 if the output buffer is full and it needs
6912 * to be called again, otherwise non-zero.
6913 */
6914static int stats_dump_resolvers_to_buffer(struct stream_interface *si)
6915{
6916 struct appctx *appctx = __objt_appctx(si->end);
6917 struct dns_resolvers *presolvers;
6918 struct dns_nameserver *pnameserver;
6919
6920 chunk_reset(&trash);
6921
6922 switch (appctx->st2) {
6923 case STAT_ST_INIT:
6924 appctx->st2 = STAT_ST_LIST; /* let's start producing data */
6925 /* fall through */
6926
6927 case STAT_ST_LIST:
Andrew Hayworth68d05342015-10-02 20:33:01 +00006928 if (LIST_ISEMPTY(&dns_resolvers)) {
6929 chunk_appendf(&trash, "No resolvers found\n");
6930 }
6931 else {
6932 list_for_each_entry(presolvers, &dns_resolvers, list) {
6933 if (appctx->ctx.resolvers.ptr != NULL && appctx->ctx.resolvers.ptr != presolvers)
6934 continue;
6935
6936 chunk_appendf(&trash, "Resolvers section %s\n", presolvers->id);
6937 list_for_each_entry(pnameserver, &presolvers->nameserver_list, list) {
6938 chunk_appendf(&trash, " nameserver %s:\n", pnameserver->id);
6939 chunk_appendf(&trash, " sent: %ld\n", pnameserver->counters.sent);
6940 chunk_appendf(&trash, " valid: %ld\n", pnameserver->counters.valid);
6941 chunk_appendf(&trash, " update: %ld\n", pnameserver->counters.update);
6942 chunk_appendf(&trash, " cname: %ld\n", pnameserver->counters.cname);
6943 chunk_appendf(&trash, " cname_error: %ld\n", pnameserver->counters.cname_error);
6944 chunk_appendf(&trash, " any_err: %ld\n", pnameserver->counters.any_err);
6945 chunk_appendf(&trash, " nx: %ld\n", pnameserver->counters.nx);
6946 chunk_appendf(&trash, " timeout: %ld\n", pnameserver->counters.timeout);
6947 chunk_appendf(&trash, " refused: %ld\n", pnameserver->counters.refused);
6948 chunk_appendf(&trash, " other: %ld\n", pnameserver->counters.other);
6949 chunk_appendf(&trash, " invalid: %ld\n", pnameserver->counters.invalid);
6950 chunk_appendf(&trash, " too_big: %ld\n", pnameserver->counters.too_big);
6951 chunk_appendf(&trash, " truncated: %ld\n", pnameserver->counters.truncated);
6952 chunk_appendf(&trash, " outdated: %ld\n", pnameserver->counters.outdated);
6953 }
6954 }
Baptiste Assmann3863f972015-05-17 00:33:24 +02006955 }
6956
6957 /* display response */
6958 if (bi_putchk(si_ic(si), &trash) == -1) {
6959 /* let's try again later from this session. We add ourselves into
6960 * this session's users so that it can remove us upon termination.
6961 */
6962 si->flags |= SI_FL_WAIT_ROOM;
6963 return 0;
6964 }
6965
6966 appctx->st2 = STAT_ST_FIN;
6967 /* fall through */
6968
6969 default:
6970 appctx->st2 = STAT_ST_FIN;
6971 return 1;
6972 }
6973}
6974
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02006975/* This function dumps all captured errors onto the stream interface's
Willy Tarreau306f8302013-07-08 15:53:06 +02006976 * read buffer. It returns 0 if the output buffer is full and it needs
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01006977 * to be called again, otherwise non-zero.
Willy Tarreau74808cb2009-03-04 15:53:18 +01006978 */
Simon Horman9bd2c732011-06-15 15:18:44 +09006979static int stats_dump_errors_to_buffer(struct stream_interface *si)
Willy Tarreau74808cb2009-03-04 15:53:18 +01006980{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006981 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau74808cb2009-03-04 15:53:18 +01006982 extern const char *monthname[12];
Willy Tarreau74808cb2009-03-04 15:53:18 +01006983
Willy Tarreau2bb4a962014-11-28 11:11:05 +01006984 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
Willy Tarreau61b34732009-10-03 23:49:35 +02006985 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006986
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006987 chunk_reset(&trash);
Willy Tarreau74808cb2009-03-04 15:53:18 +01006988
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006989 if (!appctx->ctx.errors.px) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006990 /* the function had not been called yet, let's prepare the
6991 * buffer for a response.
6992 */
Willy Tarreau10479e42010-12-12 14:00:34 +01006993 struct tm tm;
6994
6995 get_localtime(date.tv_sec, &tm);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006996 chunk_appendf(&trash, "Total events captured on [%02d/%s/%04d:%02d:%02d:%02d.%03d] : %u\n",
Willy Tarreau10479e42010-12-12 14:00:34 +01006997 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
6998 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(date.tv_usec/1000),
6999 error_snapshot_id);
7000
Willy Tarreau2bb4a962014-11-28 11:11:05 +01007001 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau10479e42010-12-12 14:00:34 +01007002 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreaufe127932015-04-21 19:23:39 +02007003 si_applet_cant_put(si);
Willy Tarreau10479e42010-12-12 14:00:34 +01007004 return 0;
7005 }
7006
Willy Tarreau7b4b4992013-12-01 09:15:12 +01007007 appctx->ctx.errors.px = proxy;
7008 appctx->ctx.errors.buf = 0;
7009 appctx->ctx.errors.bol = 0;
7010 appctx->ctx.errors.ptr = -1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01007011 }
7012
7013 /* we have two inner loops here, one for the proxy, the other one for
7014 * the buffer.
7015 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01007016 while (appctx->ctx.errors.px) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01007017 struct error_snapshot *es;
7018
Willy Tarreau7b4b4992013-12-01 09:15:12 +01007019 if (appctx->ctx.errors.buf == 0)
7020 es = &appctx->ctx.errors.px->invalid_req;
Willy Tarreau74808cb2009-03-04 15:53:18 +01007021 else
Willy Tarreau7b4b4992013-12-01 09:15:12 +01007022 es = &appctx->ctx.errors.px->invalid_rep;
Willy Tarreau74808cb2009-03-04 15:53:18 +01007023
7024 if (!es->when.tv_sec)
7025 goto next;
7026
Willy Tarreau7b4b4992013-12-01 09:15:12 +01007027 if (appctx->ctx.errors.iid >= 0 &&
7028 appctx->ctx.errors.px->uuid != appctx->ctx.errors.iid &&
7029 es->oe->uuid != appctx->ctx.errors.iid)
Willy Tarreau74808cb2009-03-04 15:53:18 +01007030 goto next;
7031
Willy Tarreau7b4b4992013-12-01 09:15:12 +01007032 if (appctx->ctx.errors.ptr < 0) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01007033 /* just print headers now */
7034
7035 char pn[INET6_ADDRSTRLEN];
7036 struct tm tm;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02007037 int port;
Willy Tarreau74808cb2009-03-04 15:53:18 +01007038
7039 get_localtime(es->when.tv_sec, &tm);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007040 chunk_appendf(&trash, " \n[%02d/%s/%04d:%02d:%02d:%02d.%03d]",
Willy Tarreau74808cb2009-03-04 15:53:18 +01007041 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
Willy Tarreau1772ece2009-04-03 14:49:12 +02007042 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(es->when.tv_usec/1000));
Willy Tarreau74808cb2009-03-04 15:53:18 +01007043
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02007044 switch (addr_to_str(&es->src, pn, sizeof(pn))) {
7045 case AF_INET:
7046 case AF_INET6:
7047 port = get_host_port(&es->src);
7048 break;
7049 default:
7050 port = 0;
7051 }
7052
Willy Tarreau7b4b4992013-12-01 09:15:12 +01007053 switch (appctx->ctx.errors.buf) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01007054 case 0:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007055 chunk_appendf(&trash,
Willy Tarreau74808cb2009-03-04 15:53:18 +01007056 " frontend %s (#%d): invalid request\n"
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02007057 " backend %s (#%d)",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01007058 appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02007059 (es->oe->cap & PR_CAP_BE) ? es->oe->id : "<NONE>",
7060 (es->oe->cap & PR_CAP_BE) ? es->oe->uuid : -1);
Willy Tarreau74808cb2009-03-04 15:53:18 +01007061 break;
7062 case 1:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007063 chunk_appendf(&trash,
Olivier Doucet08afdcb2014-09-08 11:23:00 +02007064 " backend %s (#%d): invalid response\n"
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02007065 " frontend %s (#%d)",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01007066 appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02007067 es->oe->id, es->oe->uuid);
Willy Tarreau74808cb2009-03-04 15:53:18 +01007068 break;
7069 }
7070
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007071 chunk_appendf(&trash,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02007072 ", server %s (#%d), event #%u\n"
7073 " src %s:%d, session #%d, session flags 0x%08x\n"
7074 " HTTP msg state %d, msg flags 0x%08x, tx flags 0x%08x\n"
7075 " HTTP chunk len %lld bytes, HTTP body len %lld bytes\n"
7076 " buffer flags 0x%08x, out %d bytes, total %lld bytes\n"
7077 " pending %d bytes, wrapping at %d, error at position %d:\n \n",
7078 es->srv ? es->srv->id : "<NONE>", es->srv ? es->srv->puid : -1,
7079 es->ev_id,
7080 pn, port, es->sid, es->s_flags,
7081 es->state, es->m_flags, es->t_flags,
7082 es->m_clen, es->m_blen,
7083 es->b_flags, es->b_out, es->b_tot,
7084 es->len, es->b_wrap, es->pos);
7085
Willy Tarreau2bb4a962014-11-28 11:11:05 +01007086 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01007087 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreaufe127932015-04-21 19:23:39 +02007088 si_applet_cant_put(si);
Willy Tarreau61b34732009-10-03 23:49:35 +02007089 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01007090 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01007091 appctx->ctx.errors.ptr = 0;
7092 appctx->ctx.errors.sid = es->sid;
Willy Tarreau74808cb2009-03-04 15:53:18 +01007093 }
7094
Willy Tarreau7b4b4992013-12-01 09:15:12 +01007095 if (appctx->ctx.errors.sid != es->sid) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01007096 /* the snapshot changed while we were dumping it */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007097 chunk_appendf(&trash,
Willy Tarreau74808cb2009-03-04 15:53:18 +01007098 " WARNING! update detected on this snapshot, dump interrupted. Please re-check!\n");
Willy Tarreaubc18da12015-03-13 14:00:47 +01007099 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02007100 si_applet_cant_put(si);
Willy Tarreau61b34732009-10-03 23:49:35 +02007101 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01007102 }
Willy Tarreau74808cb2009-03-04 15:53:18 +01007103 goto next;
7104 }
7105
7106 /* OK, ptr >= 0, so we have to dump the current line */
Willy Tarreauf3764b72016-03-31 13:45:10 +02007107 while (es->buf && appctx->ctx.errors.ptr < es->len && appctx->ctx.errors.ptr < global.tune.bufsize) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01007108 int newptr;
7109 int newline;
7110
Willy Tarreau7b4b4992013-12-01 09:15:12 +01007111 newline = appctx->ctx.errors.bol;
Willy Tarreauf3764b72016-03-31 13:45:10 +02007112 newptr = dump_text_line(&trash, es->buf, global.tune.bufsize, es->len, &newline, appctx->ctx.errors.ptr);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01007113 if (newptr == appctx->ctx.errors.ptr)
Willy Tarreau61b34732009-10-03 23:49:35 +02007114 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01007115
Willy Tarreau2bb4a962014-11-28 11:11:05 +01007116 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01007117 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreaufe127932015-04-21 19:23:39 +02007118 si_applet_cant_put(si);
Willy Tarreau61b34732009-10-03 23:49:35 +02007119 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01007120 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01007121 appctx->ctx.errors.ptr = newptr;
7122 appctx->ctx.errors.bol = newline;
Willy Tarreau74808cb2009-03-04 15:53:18 +01007123 };
7124 next:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01007125 appctx->ctx.errors.bol = 0;
7126 appctx->ctx.errors.ptr = -1;
7127 appctx->ctx.errors.buf++;
7128 if (appctx->ctx.errors.buf > 1) {
7129 appctx->ctx.errors.buf = 0;
7130 appctx->ctx.errors.px = appctx->ctx.errors.px->next;
Willy Tarreau74808cb2009-03-04 15:53:18 +01007131 }
Willy Tarreauae795722016-02-16 11:27:28 +01007132 }
7133
7134 /* dump complete */
7135 return 1;
7136}
7137
7138/* This function dumps all environmnent variables to the buffer. It returns 0
7139 * if the output buffer is full and it needs to be called again, otherwise
7140 * non-zero. Dumps only one entry if st2 == STAT_ST_END.
7141 */
7142static int stats_dump_env_to_buffer(struct stream_interface *si)
7143{
7144 struct appctx *appctx = __objt_appctx(si->end);
7145
7146 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
7147 return 1;
7148
7149 chunk_reset(&trash);
7150
7151 /* we have two inner loops here, one for the proxy, the other one for
7152 * the buffer.
7153 */
7154 while (*appctx->ctx.env.var) {
7155 chunk_printf(&trash, "%s\n", *appctx->ctx.env.var);
7156
7157 if (bi_putchk(si_ic(si), &trash) == -1) {
7158 si_applet_cant_put(si);
7159 return 0;
7160 }
7161 if (appctx->st2 == STAT_ST_END)
7162 break;
7163 appctx->ctx.env.var++;
Willy Tarreau74808cb2009-03-04 15:53:18 +01007164 }
7165
7166 /* dump complete */
Willy Tarreau61b34732009-10-03 23:49:35 +02007167 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01007168}
7169
Willy Tarreaud5781202012-09-22 19:32:35 +02007170/* parse the "level" argument on the bind lines */
7171static int bind_parse_level(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
7172{
7173 if (!*args[cur_arg + 1]) {
7174 memprintf(err, "'%s' : missing level", args[cur_arg]);
7175 return ERR_ALERT | ERR_FATAL;
7176 }
7177
7178 if (!strcmp(args[cur_arg+1], "user"))
7179 conf->level = ACCESS_LVL_USER;
7180 else if (!strcmp(args[cur_arg+1], "operator"))
7181 conf->level = ACCESS_LVL_OPER;
7182 else if (!strcmp(args[cur_arg+1], "admin"))
7183 conf->level = ACCESS_LVL_ADMIN;
7184 else {
7185 memprintf(err, "'%s' only supports 'user', 'operator', and 'admin' (got '%s')",
7186 args[cur_arg], args[cur_arg+1]);
7187 return ERR_ALERT | ERR_FATAL;
7188 }
7189
7190 return 0;
7191}
7192
Willy Tarreau30576452015-04-13 13:50:30 +02007193struct applet http_stats_applet = {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007194 .obj_type = OBJ_TYPE_APPLET,
Willy Tarreaub24281b2011-02-13 13:16:36 +01007195 .name = "<STATS>", /* used for logging */
7196 .fct = http_stats_io_handler,
Aman Gupta9a13e842012-04-02 18:57:53 -07007197 .release = NULL,
Willy Tarreaub24281b2011-02-13 13:16:36 +01007198};
7199
Willy Tarreau30576452015-04-13 13:50:30 +02007200static struct applet cli_applet = {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007201 .obj_type = OBJ_TYPE_APPLET,
Willy Tarreaub24281b2011-02-13 13:16:36 +01007202 .name = "<CLI>", /* used for logging */
7203 .fct = cli_io_handler,
Willy Tarreau5f9a8772012-11-26 02:22:40 +01007204 .release = cli_release_handler,
Willy Tarreaub24281b2011-02-13 13:16:36 +01007205};
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01007206
Willy Tarreaudc13c112013-06-21 23:16:39 +02007207static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreau10522fd2008-07-09 20:12:41 +02007208 { CFG_GLOBAL, "stats", stats_parse_global },
7209 { 0, NULL, NULL },
7210}};
7211
Willy Tarreaud5781202012-09-22 19:32:35 +02007212static struct bind_kw_list bind_kws = { "STAT", { }, {
7213 { "level", bind_parse_level, 1 }, /* set the unix socket admin level */
7214 { NULL, NULL, 0 },
7215}};
7216
Willy Tarreau10522fd2008-07-09 20:12:41 +02007217__attribute__((constructor))
7218static void __dumpstats_module_init(void)
7219{
7220 cfg_register_keywords(&cfg_kws);
Willy Tarreaud5781202012-09-22 19:32:35 +02007221 bind_register_keywords(&bind_kws);
Willy Tarreau10522fd2008-07-09 20:12:41 +02007222}
7223
Willy Tarreau91861262007-10-17 17:06:05 +02007224/*
7225 * Local variables:
7226 * c-indent-level: 8
7227 * c-basic-offset: 8
7228 * End:
7229 */