blob: 19e26887ebf071b3a306369920e744ee912fc5c0 [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 */
Baptiste Assmann3863f972015-05-17 00:33:24 +020094 STAT_CLI_O_RESOLVERS,/* dump a resolver's section nameservers counters */
Baptiste Assmann28289462015-07-03 08:01:20 +020095 STAT_CLI_O_SERVERS_STATE, /* dump server state and changing information */
Baptiste Assmann9b6857e2015-09-18 14:49:12 +020096 STAT_CLI_O_BACKEND, /* dump backend list */
Willy Tarreauae795722016-02-16 11:27:28 +010097 STAT_CLI_O_ENV, /* dump environment */
Willy Tarreau91b843d2014-01-28 16:27:17 +010098};
99
Willy Tarreaued7df902014-05-22 18:04:49 +0200100/* Actions available for the stats admin forms */
101enum {
102 ST_ADM_ACTION_NONE = 0,
Willy Tarreau248a60e2014-05-23 14:59:48 +0200103
104 /* enable/disable health checks */
105 ST_ADM_ACTION_DHLTH,
106 ST_ADM_ACTION_EHLTH,
107
108 /* force health check status */
109 ST_ADM_ACTION_HRUNN,
110 ST_ADM_ACTION_HNOLB,
111 ST_ADM_ACTION_HDOWN,
112
113 /* enable/disable agent checks */
114 ST_ADM_ACTION_DAGENT,
115 ST_ADM_ACTION_EAGENT,
116
117 /* force agent check status */
118 ST_ADM_ACTION_ARUNN,
119 ST_ADM_ACTION_ADOWN,
120
121 /* set admin state */
Willy Tarreaued7df902014-05-22 18:04:49 +0200122 ST_ADM_ACTION_READY,
123 ST_ADM_ACTION_DRAIN,
124 ST_ADM_ACTION_MAINT,
125 ST_ADM_ACTION_SHUTDOWN,
126 /* these are the ancient actions, still available for compatibility */
127 ST_ADM_ACTION_DISABLE,
128 ST_ADM_ACTION_ENABLE,
129 ST_ADM_ACTION_STOP,
130 ST_ADM_ACTION_START,
131};
132
Willy Tarreauc4832de2016-01-11 18:12:26 +0100133
Willy Tarreauc4832de2016-01-11 18:12:26 +0100134/* These are the field names for each INF_* field position. Please pay attention
135 * to always use the exact same name except that the strings for new names must
136 * be lower case or CamelCase while the enum entries must be upper case.
137 */
138const char *info_field_names[INF_TOTAL_FIELDS] = {
139 [INF_NAME] = "Name",
140 [INF_VERSION] = "Version",
141 [INF_RELEASE_DATE] = "Release_date",
142 [INF_NBPROC] = "Nbproc",
143 [INF_PROCESS_NUM] = "Process_num",
144 [INF_PID] = "Pid",
145 [INF_UPTIME] = "Uptime",
146 [INF_UPTIME_SEC] = "Uptime_sec",
147 [INF_MEMMAX_MB] = "Memmax_MB",
148 [INF_POOL_ALLOC_MB] = "PoolAlloc_MB",
149 [INF_POOL_USED_MB] = "PoolUsed_MB",
150 [INF_POOL_FAILED] = "PoolFailed",
151 [INF_ULIMIT_N] = "Ulimit-n",
152 [INF_MAXSOCK] = "Maxsock",
153 [INF_MAXCONN] = "Maxconn",
154 [INF_HARD_MAXCONN] = "Hard_maxconn",
155 [INF_CURR_CONN] = "CurrConns",
156 [INF_CUM_CONN] = "CumConns",
157 [INF_CUM_REQ] = "CumReq",
158 [INF_MAX_SSL_CONNS] = "MaxSslConns",
159 [INF_CURR_SSL_CONNS] = "CurrSslConns",
160 [INF_CUM_SSL_CONNS] = "CumSslConns",
161 [INF_MAXPIPES] = "Maxpipes",
162 [INF_PIPES_USED] = "PipesUsed",
163 [INF_PIPES_FREE] = "PipesFree",
164 [INF_CONN_RATE] = "ConnRate",
165 [INF_CONN_RATE_LIMIT] = "ConnRateLimit",
166 [INF_MAX_CONN_RATE] = "MaxConnRate",
167 [INF_SESS_RATE] = "SessRate",
168 [INF_SESS_RATE_LIMIT] = "SessRateLimit",
169 [INF_MAX_SESS_RATE] = "MaxSessRate",
170 [INF_SSL_RATE] = "SslRate",
171 [INF_SSL_RATE_LIMIT] = "SslRateLimit",
172 [INF_MAX_SSL_RATE] = "MaxSslRate",
173 [INF_SSL_FRONTEND_KEY_RATE] = "SslFrontendKeyRate",
174 [INF_SSL_FRONTEND_MAX_KEY_RATE] = "SslFrontendMaxKeyRate",
175 [INF_SSL_FRONTEND_SESSION_REUSE_PCT] = "SslFrontendSessionReuse_pct",
176 [INF_SSL_BACKEND_KEY_RATE] = "SslBackendKeyRate",
177 [INF_SSL_BACKEND_MAX_KEY_RATE] = "SslBackendMaxKeyRate",
178 [INF_SSL_CACHE_LOOKUPS] = "SslCacheLookups",
179 [INF_SSL_CACHE_MISSES] = "SslCacheMisses",
180 [INF_COMPRESS_BPS_IN] = "CompressBpsIn",
181 [INF_COMPRESS_BPS_OUT] = "CompressBpsOut",
182 [INF_COMPRESS_BPS_RATE_LIM] = "CompressBpsRateLim",
183 [INF_ZLIB_MEM_USAGE] = "ZlibMemUsage",
184 [INF_MAX_ZLIB_MEM_USAGE] = "MaxZlibMemUsage",
185 [INF_TASKS] = "Tasks",
186 [INF_RUN_QUEUE] = "Run_queue",
187 [INF_IDLE_PCT] = "Idle_pct",
188 [INF_NODE] = "node",
189 [INF_DESCRIPTION] = "description",
190};
191
192/* one line of stats */
193static struct field info[INF_TOTAL_FIELDS];
194
Willy Tarreau8e205142016-01-04 17:23:25 +0100195/* These are the field names for each ST_F_* field position. Please pay attention
196 * to always use the exact same name except that the strings must be lower case
197 * while the enum entries must be upper case.
198 */
199const char *stat_field_names[ST_F_TOTAL_FIELDS] = {
200 [ST_F_PXNAME] = "pxname",
201 [ST_F_SVNAME] = "svname",
202 [ST_F_QCUR] = "qcur",
203 [ST_F_QMAX] = "qmax",
204 [ST_F_SCUR] = "scur",
205 [ST_F_SMAX] = "smax",
206 [ST_F_SLIM] = "slim",
207 [ST_F_STOT] = "stot",
208 [ST_F_BIN] = "bin",
209 [ST_F_BOUT] = "bout",
210 [ST_F_DREQ] = "dreq",
211 [ST_F_DRESP] = "dresp",
212 [ST_F_EREQ] = "ereq",
213 [ST_F_ECON] = "econ",
214 [ST_F_ERESP] = "eresp",
215 [ST_F_WRETR] = "wretr",
216 [ST_F_WREDIS] = "wredis",
217 [ST_F_STATUS] = "status",
218 [ST_F_WEIGHT] = "weight",
219 [ST_F_ACT] = "act",
220 [ST_F_BCK] = "bck",
221 [ST_F_CHKFAIL] = "chkfail",
222 [ST_F_CHKDOWN] = "chkdown",
223 [ST_F_LASTCHG] = "lastchg",
224 [ST_F_DOWNTIME] = "downtime",
225 [ST_F_QLIMIT] = "qlimit",
226 [ST_F_PID] = "pid",
227 [ST_F_IID] = "iid",
228 [ST_F_SID] = "sid",
229 [ST_F_THROTTLE] = "throttle",
230 [ST_F_LBTOT] = "lbtot",
231 [ST_F_TRACKED] = "tracked",
232 [ST_F_TYPE] = "type",
233 [ST_F_RATE] = "rate",
234 [ST_F_RATE_LIM] = "rate_lim",
235 [ST_F_RATE_MAX] = "rate_max",
236 [ST_F_CHECK_STATUS] = "check_status",
237 [ST_F_CHECK_CODE] = "check_code",
238 [ST_F_CHECK_DURATION] = "check_duration",
239 [ST_F_HRSP_1XX] = "hrsp_1xx",
240 [ST_F_HRSP_2XX] = "hrsp_2xx",
241 [ST_F_HRSP_3XX] = "hrsp_3xx",
242 [ST_F_HRSP_4XX] = "hrsp_4xx",
243 [ST_F_HRSP_5XX] = "hrsp_5xx",
244 [ST_F_HRSP_OTHER] = "hrsp_other",
245 [ST_F_HANAFAIL] = "hanafail",
246 [ST_F_REQ_RATE] = "req_rate",
247 [ST_F_REQ_RATE_MAX] = "req_rate_max",
248 [ST_F_REQ_TOT] = "req_tot",
249 [ST_F_CLI_ABRT] = "cli_abrt",
250 [ST_F_SRV_ABRT] = "srv_abrt",
251 [ST_F_COMP_IN] = "comp_in",
252 [ST_F_COMP_OUT] = "comp_out",
253 [ST_F_COMP_BYP] = "comp_byp",
254 [ST_F_COMP_RSP] = "comp_rsp",
255 [ST_F_LASTSESS] = "lastsess",
256 [ST_F_LAST_CHK] = "last_chk",
257 [ST_F_LAST_AGT] = "last_agt",
258 [ST_F_QTIME] = "qtime",
259 [ST_F_CTIME] = "ctime",
260 [ST_F_RTIME] = "rtime",
261 [ST_F_TTIME] = "ttime",
Willy Tarreau7f618842016-01-08 11:40:03 +0100262 [ST_F_AGENT_STATUS] = "agent_status",
263 [ST_F_AGENT_CODE] = "agent_code",
264 [ST_F_AGENT_DURATION] = "agent_duration",
Willy Tarreaudd7354b2016-01-08 13:47:26 +0100265 [ST_F_CHECK_DESC] = "check_desc",
266 [ST_F_AGENT_DESC] = "agent_desc",
Willy Tarreau3141f592016-01-08 14:25:28 +0100267 [ST_F_CHECK_RISE] = "check_rise",
268 [ST_F_CHECK_FALL] = "check_fall",
269 [ST_F_CHECK_HEALTH] = "check_health",
270 [ST_F_AGENT_RISE] = "agent_rise",
271 [ST_F_AGENT_FALL] = "agent_fall",
272 [ST_F_AGENT_HEALTH] = "agent_health",
Willy Tarreau3a4ec3a2016-01-08 15:35:43 +0100273 [ST_F_ADDR] = "addr",
Willy Tarreaue4847c62016-01-08 15:43:54 +0100274 [ST_F_COOKIE] = "cookie",
Willy Tarreauf8211df2016-01-11 14:09:38 +0100275 [ST_F_MODE] = "mode",
Willy Tarreauf1516d92016-01-11 14:48:36 +0100276 [ST_F_ALGO] = "algo",
Willy Tarreauc73810f2016-01-11 13:52:04 +0100277 [ST_F_CONN_RATE] = "conn_rate",
278 [ST_F_CONN_RATE_MAX] = "conn_rate_max",
279 [ST_F_CONN_TOT] = "conn_tot",
Willy Tarreau5b9bdff2016-01-11 14:40:47 +0100280 [ST_F_INTERCEPTED] = "intercepted",
Willy Tarreau8e205142016-01-04 17:23:25 +0100281};
282
Willy Tarreau82a86022016-01-04 19:04:18 +0100283/* one line of stats */
284static struct field stats[ST_F_TOTAL_FIELDS];
285
Baptiste Assmann9b6857e2015-09-18 14:49:12 +0200286static int stats_dump_backend_to_buffer(struct stream_interface *si);
Willy Tarreauae795722016-02-16 11:27:28 +0100287static int stats_dump_env_to_buffer(struct stream_interface *si);
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +0100288static int stats_dump_info_to_buffer(struct stream_interface *si);
Baptiste Assmann28289462015-07-03 08:01:20 +0200289static int stats_dump_servers_state_to_buffer(struct stream_interface *si);
Willy Tarreau12833bb2014-01-28 16:49:56 +0100290static int stats_dump_pools_to_buffer(struct stream_interface *si);
Willy Tarreau87b09662015-04-03 00:22:06 +0200291static int stats_dump_full_sess_to_buffer(struct stream_interface *si, struct stream *sess);
Simon Horman9bd2c732011-06-15 15:18:44 +0900292static int stats_dump_sess_to_buffer(struct stream_interface *si);
293static int stats_dump_errors_to_buffer(struct stream_interface *si);
Willy Tarreau44455022012-12-05 23:01:12 +0100294static int stats_table_request(struct stream_interface *si, int show);
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +0100295static int stats_dump_proxy_to_buffer(struct stream_interface *si, struct proxy *px, struct uri_auth *uri);
296static int stats_dump_stat_to_buffer(struct stream_interface *si, struct uri_auth *uri);
Baptiste Assmann3863f972015-05-17 00:33:24 +0200297static int stats_dump_resolvers_to_buffer(struct stream_interface *si);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100298static int stats_pats_list(struct stream_interface *si);
299static int stats_pat_list(struct stream_interface *si);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +0100300static int stats_map_lookup(struct stream_interface *si);
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +0200301#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
302static int stats_tlskeys_list(struct stream_interface *si);
303#endif
Willy Tarreau00a37f02015-04-13 12:05:19 +0200304static void cli_release_handler(struct appctx *appctx);
Simon Horman9bd2c732011-06-15 15:18:44 +0900305
Cyril Bonté76a99782016-05-06 12:18:48 +0200306static int dump_servers_state(struct stream_interface *si, struct chunk *buf);
Baptiste Assmann28289462015-07-03 08:01:20 +0200307
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100308/*
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +0100309 * cli_io_handler()
310 * -> stats_dump_sess_to_buffer() // "show sess"
311 * -> stats_dump_errors_to_buffer() // "show errors"
312 * -> stats_dump_info_to_buffer() // "show info"
Baptiste Assmann9b6857e2015-09-18 14:49:12 +0200313 * -> stats_dump_backend_to_buffer() // "show backend"
Baptiste Assmann28289462015-07-03 08:01:20 +0200314 * -> stats_dump_servers_state_to_buffer() // "show servers state [<backend name>]"
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +0100315 * -> stats_dump_stat_to_buffer() // "show stat"
Cyril Bontéa8322f92016-05-06 12:18:51 +0200316 * -> stats_dump_resolvers_to_buffer() // "show stat resolvers <id>"
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +0100317 * -> stats_dump_csv_header()
318 * -> stats_dump_proxy_to_buffer()
319 * -> stats_dump_fe_stats()
320 * -> stats_dump_li_stats()
321 * -> stats_dump_sv_stats()
322 * -> stats_dump_be_stats()
323 *
324 * http_stats_io_handler()
325 * -> stats_dump_stat_to_buffer() // same as above, but used for CSV or HTML
326 * -> stats_dump_csv_header() // emits the CSV headers (same as above)
327 * -> stats_dump_html_head() // emits the HTML headers
328 * -> stats_dump_html_info() // emits the equivalent of "show info" at the top
329 * -> stats_dump_proxy_to_buffer() // same as above, valid for CSV and HTML
330 * -> stats_dump_html_px_hdr()
331 * -> stats_dump_fe_stats()
332 * -> stats_dump_li_stats()
333 * -> stats_dump_sv_stats()
334 * -> stats_dump_be_stats()
335 * -> stats_dump_html_px_end()
336 * -> stats_dump_html_end() // emits HTML trailer
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100337 */
338
Willy Tarreau30576452015-04-13 13:50:30 +0200339static struct applet cli_applet;
Simon Horman9bd2c732011-06-15 15:18:44 +0900340
341static const char stats_sock_usage_msg[] =
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200342 "Unknown command. Please enter one of the following commands only :\n"
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200343 " clear counters : clear max statistics counters (add 'all' for all counters)\n"
Willy Tarreau88ee3972010-07-13 13:48:00 +0200344 " clear table : remove an entry from a table\n"
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200345 " help : this message\n"
346 " prompt : toggle interactive mode with prompt\n"
347 " quit : disconnect\n"
Baptiste Assmann9b6857e2015-09-18 14:49:12 +0200348 " show backend : list backends in the current running config\n"
Willy Tarreauae795722016-02-16 11:27:28 +0100349 " show env [var] : dump environment variables known to the process\n"
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200350 " show info : report information about the running process\n"
Willy Tarreau12833bb2014-01-28 16:49:56 +0100351 " show pools : report information about the memory pools usage\n"
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200352 " show stat : report counters for each proxy and server\n"
Cyril Bontéa8322f92016-05-06 12:18:51 +0200353 " show stat resolvers [id]: dumps counters from all resolvers section and\n"
354 " associated name servers\n"
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200355 " show errors : report last request and response errors for each proxy\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +0100356 " show sess [id] : report the list of current sessions or dump this session\n"
Willy Tarreau69f58c82010-07-12 17:55:33 +0200357 " show table [id]: report table usage stats or dump this table's contents\n"
Baptiste Assmann28289462015-07-03 08:01:20 +0200358 " show servers state [id]: dump volatile server information (for backend <id>)\n"
Willy Tarreau38338fa2009-10-10 18:37:29 +0200359 " get weight : report a server's current weight\n"
Willy Tarreau4483d432009-10-10 19:30:08 +0200360 " set weight : change a server's weight\n"
Baptiste Assmann3d8f8312015-04-13 22:54:33 +0200361 " set server : change a server's state, weight or address\n"
Willy Tarreau654694e2012-06-07 01:03:16 +0200362 " set table [id] : update or create a table entry's data\n"
Willy Tarreau7aabd112010-01-26 10:59:06 +0100363 " set timeout : change a timeout setting\n"
Willy Tarreau2a0f4d22011-08-02 11:49:05 +0200364 " set maxconn : change a maxconn setting\n"
Willy Tarreauf5b22872011-09-07 16:13:44 +0200365 " set rate-limit : change a rate limiting value\n"
Willy Tarreaua295edc2011-09-07 23:21:03 +0200366 " disable : put a server or frontend in maintenance mode\n"
367 " enable : re-enable a server or frontend which is in maintenance mode\n"
368 " shutdown : kill a session or a frontend (eg:to release listening ports)\n"
Cyril Bontédb98eb32016-05-06 12:18:50 +0200369 " show acl [id] : report available acls or dump an acl's contents\n"
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100370 " get acl : reports the patterns matching a sample for an ACL\n"
371 " add acl : add acl entry\n"
372 " del acl : delete acl entry\n"
373 " clear acl <id> : clear the content of this acl\n"
Cyril Bontédb98eb32016-05-06 12:18:50 +0200374 " show map [id] : report available maps or dump a map's contents\n"
Thierry FOURNIER1432a0c2014-03-11 13:42:38 +0100375 " get map : reports the keys and values matching a sample for a map\n"
376 " set map : modify map entry\n"
377 " add map : add map entry\n"
378 " del map : delete map entry\n"
379 " clear map <id> : clear the content of this map\n"
Emeric Brun4147b2e2014-06-16 18:36:30 +0200380 " set ssl <stmt> : set statement for ssl\n"
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200381 "";
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200382
Simon Horman9bd2c732011-06-15 15:18:44 +0900383static const char stats_permission_denied_msg[] =
Willy Tarreau6162db22009-10-10 17:13:00 +0200384 "Permission denied\n"
385 "";
386
Willy Tarreau295a8372011-03-10 11:25:07 +0100387/* data transmission states for the stats responses */
388enum {
389 STAT_ST_INIT = 0,
390 STAT_ST_HEAD,
391 STAT_ST_INFO,
392 STAT_ST_LIST,
393 STAT_ST_END,
394 STAT_ST_FIN,
395};
396
397/* data transmission states for the stats responses inside a proxy */
398enum {
399 STAT_PX_ST_INIT = 0,
400 STAT_PX_ST_TH,
401 STAT_PX_ST_FE,
402 STAT_PX_ST_LI,
403 STAT_PX_ST_SV,
404 STAT_PX_ST_BE,
405 STAT_PX_ST_END,
406 STAT_PX_ST_FIN,
407};
408
Cyril Bonté19979e12012-04-04 12:57:21 +0200409extern const char *stat_status_codes[];
410
Willy Tarreau07e9e642010-08-17 21:48:17 +0200411/* allocate a new stats frontend named <name>, and return it
412 * (or NULL in case of lack of memory).
413 */
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200414static struct proxy *alloc_stats_fe(const char *name, const char *file, int line)
Willy Tarreau07e9e642010-08-17 21:48:17 +0200415{
416 struct proxy *fe;
417
Vincent Bernat02779b62016-04-03 13:48:43 +0200418 fe = calloc(1, sizeof(*fe));
Willy Tarreau07e9e642010-08-17 21:48:17 +0200419 if (!fe)
420 return NULL;
421
Willy Tarreau237250c2011-07-29 01:49:03 +0200422 init_new_proxy(fe);
Willy Tarreau050536d2012-10-04 08:47:34 +0200423 fe->next = proxy;
424 proxy = fe;
Willy Tarreau07e9e642010-08-17 21:48:17 +0200425 fe->last_change = now.tv_sec;
426 fe->id = strdup("GLOBAL");
427 fe->cap = PR_CAP_FE;
Willy Tarreauc2adf8b2011-09-07 12:13:34 +0200428 fe->maxconn = 10; /* default to 10 concurrent connections */
429 fe->timeout.client = MS_TO_TICKS(10000); /* default timeout of 10 seconds */
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200430 fe->conf.file = strdup(file);
431 fe->conf.line = line;
Willy Tarreau32b60d42015-03-13 16:14:57 +0100432 fe->accept = frontend_accept;
Willy Tarreauf87ab942015-03-13 15:55:16 +0100433 fe->default_target = &cli_applet.obj_type;
Willy Tarreau050536d2012-10-04 08:47:34 +0200434
435 /* the stats frontend is the only one able to assign ID #0 */
436 fe->conf.id.key = fe->uuid = 0;
437 eb32_insert(&used_proxy_id, &fe->conf.id);
Willy Tarreau07e9e642010-08-17 21:48:17 +0200438 return fe;
439}
440
Willy Tarreaufbee7132007-10-18 13:53:22 +0200441/* This function parses a "stats" statement in the "global" section. It returns
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200442 * -1 if there is any error, otherwise zero. If it returns -1, it will write an
443 * error message into the <err> buffer which will be preallocated. The trailing
444 * '\n' must not be written. The function must be called with <args> pointing to
445 * the first word after "stats".
Willy Tarreaufbee7132007-10-18 13:53:22 +0200446 */
Willy Tarreau10522fd2008-07-09 20:12:41 +0200447static int stats_parse_global(char **args, int section_type, struct proxy *curpx,
Willy Tarreau28a47d62012-09-18 20:02:48 +0200448 struct proxy *defpx, const char *file, int line,
449 char **err)
Willy Tarreaufbee7132007-10-18 13:53:22 +0200450{
Willy Tarreau4348fad2012-09-20 16:48:07 +0200451 struct bind_conf *bind_conf;
Willy Tarreauc53d4222012-09-20 20:19:28 +0200452 struct listener *l;
Willy Tarreau4348fad2012-09-20 16:48:07 +0200453
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200454 if (!strcmp(args[1], "socket")) {
Willy Tarreaufbee7132007-10-18 13:53:22 +0200455 int cur_arg;
456
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200457 if (*args[2] == 0) {
Willy Tarreauc53d4222012-09-20 20:19:28 +0200458 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 +0200459 return -1;
460 }
461
Willy Tarreau89a63132009-08-16 17:41:45 +0200462 if (!global.stats_fe) {
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200463 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200464 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
Willy Tarreau89a63132009-08-16 17:41:45 +0200465 return -1;
466 }
Willy Tarreau89a63132009-08-16 17:41:45 +0200467 }
468
Willy Tarreau4348fad2012-09-20 16:48:07 +0200469 bind_conf = bind_conf_alloc(&global.stats_fe->conf.bind, file, line, args[2]);
Willy Tarreau290e63a2012-09-20 18:07:14 +0200470 bind_conf->level = ACCESS_LVL_OPER; /* default access level */
Willy Tarreau4348fad2012-09-20 16:48:07 +0200471
Willy Tarreauc53d4222012-09-20 20:19:28 +0200472 if (!str2listener(args[2], global.stats_fe, bind_conf, file, line, err)) {
473 memprintf(err, "parsing [%s:%d] : '%s %s' : %s\n",
474 file, line, args[0], args[1], err && *err ? *err : "error");
475 return -1;
476 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200477
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200478 cur_arg = 3;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200479 while (*args[cur_arg]) {
Willy Tarreaud5781202012-09-22 19:32:35 +0200480 static int bind_dumped;
481 struct bind_kw *kw;
482
483 kw = bind_find_kw(args[cur_arg]);
484 if (kw) {
485 if (!kw->parse) {
486 memprintf(err, "'%s %s' : '%s' option is not implemented in this version (check build options).",
487 args[0], args[1], args[cur_arg]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200488 return -1;
489 }
Willy Tarreaud5781202012-09-22 19:32:35 +0200490
Willy Tarreau061b5de2015-10-13 15:06:57 +0200491 if (kw->parse(args, cur_arg, global.stats_fe, bind_conf, err) != 0) {
Willy Tarreaud5781202012-09-22 19:32:35 +0200492 if (err && *err)
493 memprintf(err, "'%s %s' : '%s'", args[0], args[1], *err);
494 else
495 memprintf(err, "'%s %s' : error encountered while processing '%s'",
496 args[0], args[1], args[cur_arg]);
Willy Tarreau6162db22009-10-10 17:13:00 +0200497 return -1;
498 }
Willy Tarreaud5781202012-09-22 19:32:35 +0200499
500 cur_arg += 1 + kw->skip;
501 continue;
Willy Tarreau6162db22009-10-10 17:13:00 +0200502 }
Willy Tarreaud5781202012-09-22 19:32:35 +0200503
504 if (!bind_dumped) {
505 bind_dump_kws(err);
506 indent_msg(err, 4);
507 bind_dumped = 1;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200508 }
Willy Tarreaud5781202012-09-22 19:32:35 +0200509
510 memprintf(err, "'%s %s' : unknown keyword '%s'.%s%s",
511 args[0], args[1], args[cur_arg],
512 err && *err ? " Registered keywords :" : "", err && *err ? *err : "");
513 return -1;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200514 }
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100515
Willy Tarreauc53d4222012-09-20 20:19:28 +0200516 list_for_each_entry(l, &bind_conf->listeners, by_bind) {
517 l->maxconn = global.stats_fe->maxconn;
518 l->backlog = global.stats_fe->backlog;
Willy Tarreau9903f0e2015-04-04 18:50:31 +0200519 l->accept = session_accept_fd;
Willy Tarreau87b09662015-04-03 00:22:06 +0200520 l->handler = process_stream;
Willy Tarreau10b688f2015-03-13 16:43:12 +0100521 l->default_target = global.stats_fe->default_target;
Willy Tarreauc53d4222012-09-20 20:19:28 +0200522 l->options |= LI_O_UNLIMITED; /* don't make the peers subject to global limits */
523 l->nice = -64; /* we want to boost priority for local stats */
524 global.maxsock += l->maxconn;
525 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200526 }
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200527 else if (!strcmp(args[1], "timeout")) {
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100528 unsigned timeout;
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200529 const char *res = parse_time_err(args[2], &timeout, TIME_UNIT_MS);
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100530
531 if (res) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200532 memprintf(err, "'%s %s' : unexpected character '%c'", args[0], args[1], *res);
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100533 return -1;
534 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200535
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100536 if (!timeout) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200537 memprintf(err, "'%s %s' expects a positive value", args[0], args[1]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200538 return -1;
539 }
Willy Tarreau07e9e642010-08-17 21:48:17 +0200540 if (!global.stats_fe) {
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200541 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200542 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
Willy Tarreau07e9e642010-08-17 21:48:17 +0200543 return -1;
544 }
545 }
Willy Tarreau89a63132009-08-16 17:41:45 +0200546 global.stats_fe->timeout.client = MS_TO_TICKS(timeout);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200547 }
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200548 else if (!strcmp(args[1], "maxconn")) {
549 int maxconn = atol(args[2]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200550
551 if (maxconn <= 0) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200552 memprintf(err, "'%s %s' expects a positive value", args[0], args[1]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200553 return -1;
554 }
Willy Tarreauc2adf8b2011-09-07 12:13:34 +0200555
556 if (!global.stats_fe) {
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200557 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200558 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
Willy Tarreauc2adf8b2011-09-07 12:13:34 +0200559 return -1;
560 }
561 }
562 global.stats_fe->maxconn = maxconn;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200563 }
Willy Tarreau35b7b162012-10-22 23:17:18 +0200564 else if (!strcmp(args[1], "bind-process")) { /* enable the socket only on some processes */
565 int cur_arg = 2;
Willy Tarreaua9db57e2013-01-18 11:29:29 +0100566 unsigned long set = 0;
Willy Tarreau35b7b162012-10-22 23:17:18 +0200567
Willy Tarreau91319572013-04-20 09:48:50 +0200568 if (!global.stats_fe) {
569 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
570 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
571 return -1;
572 }
573 }
574
Willy Tarreau35b7b162012-10-22 23:17:18 +0200575 while (*args[cur_arg]) {
Willy Tarreau110ecc12012-11-15 17:50:01 +0100576 unsigned int low, high;
577
Willy Tarreau35b7b162012-10-22 23:17:18 +0200578 if (strcmp(args[cur_arg], "all") == 0) {
579 set = 0;
580 break;
581 }
582 else if (strcmp(args[cur_arg], "odd") == 0) {
Willy Tarreaua9db57e2013-01-18 11:29:29 +0100583 set |= ~0UL/3UL; /* 0x555....555 */
Willy Tarreau35b7b162012-10-22 23:17:18 +0200584 }
585 else if (strcmp(args[cur_arg], "even") == 0) {
Willy Tarreaua9db57e2013-01-18 11:29:29 +0100586 set |= (~0UL/3UL) << 1; /* 0xAAA...AAA */
Willy Tarreau35b7b162012-10-22 23:17:18 +0200587 }
Willy Tarreau83d84cf2012-11-22 01:04:31 +0100588 else if (isdigit((int)*args[cur_arg])) {
Willy Tarreau110ecc12012-11-15 17:50:01 +0100589 char *dash = strchr(args[cur_arg], '-');
590
591 low = high = str2uic(args[cur_arg]);
592 if (dash)
593 high = str2uic(dash + 1);
594
595 if (high < low) {
596 unsigned int swap = low;
597 low = high;
598 high = swap;
599 }
600
Willy Tarreaua9db57e2013-01-18 11:29:29 +0100601 if (low < 1 || high > LONGBITS) {
602 memprintf(err, "'%s %s' supports process numbers from 1 to %d.\n",
603 args[0], args[1], LONGBITS);
Willy Tarreau35b7b162012-10-22 23:17:18 +0200604 return -1;
605 }
Willy Tarreau110ecc12012-11-15 17:50:01 +0100606 while (low <= high)
Willy Tarreaua9db57e2013-01-18 11:29:29 +0100607 set |= 1UL << (low++ - 1);
Willy Tarreau110ecc12012-11-15 17:50:01 +0100608 }
609 else {
610 memprintf(err,
Willy Tarreaua9db57e2013-01-18 11:29:29 +0100611 "'%s %s' expects 'all', 'odd', 'even', or a list of process ranges with numbers from 1 to %d.\n",
612 args[0], args[1], LONGBITS);
Willy Tarreau110ecc12012-11-15 17:50:01 +0100613 return -1;
Willy Tarreau35b7b162012-10-22 23:17:18 +0200614 }
615 cur_arg++;
616 }
617 global.stats_fe->bind_proc = set;
618 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200619 else {
Willy Tarreau35b7b162012-10-22 23:17:18 +0200620 memprintf(err, "'%s' only supports 'socket', 'maxconn', 'bind-process' and 'timeout' (got '%s')", args[0], args[1]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200621 return -1;
622 }
623 return 0;
624}
625
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100626/* Dumps the stats CSV header to the trash buffer which. The caller is responsible
627 * for clearing it if needed.
Willy Tarreauf522f3d2014-02-10 22:22:49 +0100628 * NOTE: Some tools happen to rely on the field position instead of its name,
629 * so please only append new fields at the end, never in the middle.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100630 */
631static void stats_dump_csv_header()
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100632{
Willy Tarreauf6142292016-01-04 17:24:29 +0100633 int field;
634
635 chunk_appendf(&trash, "# ");
636 for (field = 0; field < ST_F_TOTAL_FIELDS; field++)
637 chunk_appendf(&trash, "%s,", stat_field_names[field]);
638
639 chunk_appendf(&trash, "\n");
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100640}
641
Simon Hormand9366582011-06-15 15:18:45 +0900642/* print a string of text buffer to <out>. The format is :
643 * Non-printable chars \t, \n, \r and \e are * encoded in C format.
644 * Other non-printable chars are encoded "\xHH". Space and '\' are also escaped.
645 * Print stopped if null char or <bsize> is reached, or if no more place in the chunk.
646 */
647static int dump_text(struct chunk *out, const char *buf, int bsize)
648{
649 unsigned char c;
650 int ptr = 0;
651
652 while (buf[ptr] && ptr < bsize) {
653 c = buf[ptr];
654 if (isprint(c) && isascii(c) && c != '\\' && c != ' ') {
655 if (out->len > out->size - 1)
656 break;
657 out->str[out->len++] = c;
658 }
659 else if (c == '\t' || c == '\n' || c == '\r' || c == '\e' || c == '\\' || c == ' ') {
660 if (out->len > out->size - 2)
661 break;
662 out->str[out->len++] = '\\';
663 switch (c) {
664 case ' ': c = ' '; break;
665 case '\t': c = 't'; break;
666 case '\n': c = 'n'; break;
667 case '\r': c = 'r'; break;
668 case '\e': c = 'e'; break;
669 case '\\': c = '\\'; break;
670 }
671 out->str[out->len++] = c;
672 }
673 else {
674 if (out->len > out->size - 4)
675 break;
676 out->str[out->len++] = '\\';
677 out->str[out->len++] = 'x';
678 out->str[out->len++] = hextab[(c >> 4) & 0xF];
679 out->str[out->len++] = hextab[c & 0xF];
680 }
681 ptr++;
682 }
683
684 return ptr;
685}
686
687/* print a buffer in hexa.
688 * Print stopped if <bsize> is reached, or if no more place in the chunk.
689 */
690static int dump_binary(struct chunk *out, const char *buf, int bsize)
691{
692 unsigned char c;
693 int ptr = 0;
694
695 while (ptr < bsize) {
696 c = buf[ptr];
697
698 if (out->len > out->size - 2)
699 break;
700 out->str[out->len++] = hextab[(c >> 4) & 0xF];
701 out->str[out->len++] = hextab[c & 0xF];
702
703 ptr++;
704 }
705 return ptr;
706}
707
708/* Dump the status of a table to a stream interface's
709 * read buffer. It returns 0 if the output buffer is full
710 * and needs to be called again, otherwise non-zero.
711 */
712static int stats_dump_table_head_to_buffer(struct chunk *msg, struct stream_interface *si,
713 struct proxy *proxy, struct proxy *target)
714{
Willy Tarreau87b09662015-04-03 00:22:06 +0200715 struct stream *s = si_strm(si);
Simon Hormand9366582011-06-15 15:18:45 +0900716
Willy Tarreau77804732012-10-29 16:14:26 +0100717 chunk_appendf(msg, "# table: %s, type: %s, size:%d, used:%d\n",
Simon Hormand9366582011-06-15 15:18:45 +0900718 proxy->id, stktable_types[proxy->table.type].kw, proxy->table.size, proxy->table.current);
719
720 /* any other information should be dumped here */
721
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200722 if (target && strm_li(s)->bind_conf->level < ACCESS_LVL_OPER)
Willy Tarreau77804732012-10-29 16:14:26 +0100723 chunk_appendf(msg, "# contents not dumped due to insufficient privileges\n");
Simon Hormand9366582011-06-15 15:18:45 +0900724
Willy Tarreaubc18da12015-03-13 14:00:47 +0100725 if (bi_putchk(si_ic(si), msg) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +0200726 si_applet_cant_put(si);
Simon Hormand9366582011-06-15 15:18:45 +0900727 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +0100728 }
Simon Hormand9366582011-06-15 15:18:45 +0900729
730 return 1;
731}
732
733/* Dump the a table entry to a stream interface's
734 * read buffer. It returns 0 if the output buffer is full
735 * and needs to be called again, otherwise non-zero.
736 */
737static int stats_dump_table_entry_to_buffer(struct chunk *msg, struct stream_interface *si,
738 struct proxy *proxy, struct stksess *entry)
739{
740 int dt;
741
Willy Tarreau77804732012-10-29 16:14:26 +0100742 chunk_appendf(msg, "%p:", entry);
Simon Hormand9366582011-06-15 15:18:45 +0900743
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200744 if (proxy->table.type == SMP_T_IPV4) {
Simon Hormand9366582011-06-15 15:18:45 +0900745 char addr[INET_ADDRSTRLEN];
746 inet_ntop(AF_INET, (const void *)&entry->key.key, addr, sizeof(addr));
Willy Tarreau77804732012-10-29 16:14:26 +0100747 chunk_appendf(msg, " key=%s", addr);
Simon Hormand9366582011-06-15 15:18:45 +0900748 }
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200749 else if (proxy->table.type == SMP_T_IPV6) {
Simon Hormand9366582011-06-15 15:18:45 +0900750 char addr[INET6_ADDRSTRLEN];
751 inet_ntop(AF_INET6, (const void *)&entry->key.key, addr, sizeof(addr));
Willy Tarreau77804732012-10-29 16:14:26 +0100752 chunk_appendf(msg, " key=%s", addr);
Simon Hormand9366582011-06-15 15:18:45 +0900753 }
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200754 else if (proxy->table.type == SMP_T_SINT) {
Willy Tarreau77804732012-10-29 16:14:26 +0100755 chunk_appendf(msg, " key=%u", *(unsigned int *)entry->key.key);
Simon Hormand9366582011-06-15 15:18:45 +0900756 }
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200757 else if (proxy->table.type == SMP_T_STR) {
Willy Tarreau77804732012-10-29 16:14:26 +0100758 chunk_appendf(msg, " key=");
Simon Hormand9366582011-06-15 15:18:45 +0900759 dump_text(msg, (const char *)entry->key.key, proxy->table.key_size);
760 }
761 else {
Willy Tarreau77804732012-10-29 16:14:26 +0100762 chunk_appendf(msg, " key=");
Simon Hormand9366582011-06-15 15:18:45 +0900763 dump_binary(msg, (const char *)entry->key.key, proxy->table.key_size);
764 }
765
Willy Tarreau77804732012-10-29 16:14:26 +0100766 chunk_appendf(msg, " use=%d exp=%d", entry->ref_cnt - 1, tick_remain(now_ms, entry->expire));
Simon Hormand9366582011-06-15 15:18:45 +0900767
768 for (dt = 0; dt < STKTABLE_DATA_TYPES; dt++) {
769 void *ptr;
770
771 if (proxy->table.data_ofs[dt] == 0)
772 continue;
773 if (stktable_data_types[dt].arg_type == ARG_T_DELAY)
Willy Tarreau77804732012-10-29 16:14:26 +0100774 chunk_appendf(msg, " %s(%d)=", stktable_data_types[dt].name, proxy->table.data_arg[dt].u);
Simon Hormand9366582011-06-15 15:18:45 +0900775 else
Willy Tarreau77804732012-10-29 16:14:26 +0100776 chunk_appendf(msg, " %s=", stktable_data_types[dt].name);
Simon Hormand9366582011-06-15 15:18:45 +0900777
778 ptr = stktable_data_ptr(&proxy->table, entry, dt);
779 switch (stktable_data_types[dt].std_type) {
780 case STD_T_SINT:
Willy Tarreau77804732012-10-29 16:14:26 +0100781 chunk_appendf(msg, "%d", stktable_data_cast(ptr, std_t_sint));
Simon Hormand9366582011-06-15 15:18:45 +0900782 break;
783 case STD_T_UINT:
Willy Tarreau77804732012-10-29 16:14:26 +0100784 chunk_appendf(msg, "%u", stktable_data_cast(ptr, std_t_uint));
Simon Hormand9366582011-06-15 15:18:45 +0900785 break;
786 case STD_T_ULL:
Willy Tarreau77804732012-10-29 16:14:26 +0100787 chunk_appendf(msg, "%lld", stktable_data_cast(ptr, std_t_ull));
Simon Hormand9366582011-06-15 15:18:45 +0900788 break;
789 case STD_T_FRQP:
Willy Tarreau77804732012-10-29 16:14:26 +0100790 chunk_appendf(msg, "%d",
Simon Hormand9366582011-06-15 15:18:45 +0900791 read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
792 proxy->table.data_arg[dt].u));
793 break;
794 }
795 }
Willy Tarreau77804732012-10-29 16:14:26 +0100796 chunk_appendf(msg, "\n");
Simon Hormand9366582011-06-15 15:18:45 +0900797
Willy Tarreaubc18da12015-03-13 14:00:47 +0100798 if (bi_putchk(si_ic(si), msg) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +0200799 si_applet_cant_put(si);
Simon Hormand9366582011-06-15 15:18:45 +0900800 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +0100801 }
Simon Hormand9366582011-06-15 15:18:45 +0900802
803 return 1;
804}
805
Willy Tarreaudec98142012-06-06 23:37:08 +0200806static void stats_sock_table_key_request(struct stream_interface *si, char **args, int action)
Simon Horman121f3052011-06-15 15:18:46 +0900807{
Willy Tarreau87b09662015-04-03 00:22:06 +0200808 struct stream *s = si_strm(si);
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100809 struct appctx *appctx = __objt_appctx(si->end);
810 struct proxy *px = appctx->ctx.table.target;
Simon Horman121f3052011-06-15 15:18:46 +0900811 struct stksess *ts;
Simon Hormancec9a222011-06-15 15:18:51 +0900812 uint32_t uint32_key;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900813 unsigned char ip6_key[sizeof(struct in6_addr)];
Willy Tarreau654694e2012-06-07 01:03:16 +0200814 long long value;
815 int data_type;
Willy Tarreau47060b62013-08-01 21:11:42 +0200816 int cur_arg;
Willy Tarreau654694e2012-06-07 01:03:16 +0200817 void *ptr;
818 struct freq_ctr_period *frqp;
Simon Horman121f3052011-06-15 15:18:46 +0900819
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100820 appctx->st0 = STAT_CLI_OUTPUT;
Simon Horman121f3052011-06-15 15:18:46 +0900821
822 if (!*args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100823 appctx->ctx.cli.msg = "Key value expected\n";
824 appctx->st0 = STAT_CLI_PRINT;
Simon Horman121f3052011-06-15 15:18:46 +0900825 return;
826 }
827
Simon Hormanc5b89f62011-06-15 15:18:50 +0900828 switch (px->table.type) {
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200829 case SMP_T_IPV4:
Simon Hormancec9a222011-06-15 15:18:51 +0900830 uint32_key = htonl(inetaddr_host(args[4]));
Willy Tarreau07115412012-10-29 21:56:59 +0100831 static_table_key->key = &uint32_key;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900832 break;
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200833 case SMP_T_IPV6:
Simon Hormanc5b89f62011-06-15 15:18:50 +0900834 inet_pton(AF_INET6, args[4], ip6_key);
Willy Tarreau07115412012-10-29 21:56:59 +0100835 static_table_key->key = &ip6_key;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900836 break;
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200837 case SMP_T_SINT:
Simon Hormancec9a222011-06-15 15:18:51 +0900838 {
839 char *endptr;
840 unsigned long val;
841 errno = 0;
842 val = strtoul(args[4], &endptr, 10);
843 if ((errno == ERANGE && val == ULONG_MAX) ||
844 (errno != 0 && val == 0) || endptr == args[4] ||
845 val > 0xffffffff) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100846 appctx->ctx.cli.msg = "Invalid key\n";
847 appctx->st0 = STAT_CLI_PRINT;
Simon Hormancec9a222011-06-15 15:18:51 +0900848 return;
849 }
850 uint32_key = (uint32_t) val;
Willy Tarreau07115412012-10-29 21:56:59 +0100851 static_table_key->key = &uint32_key;
Simon Hormancec9a222011-06-15 15:18:51 +0900852 break;
853 }
854 break;
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200855 case SMP_T_STR:
Willy Tarreau07115412012-10-29 21:56:59 +0100856 static_table_key->key = args[4];
857 static_table_key->key_len = strlen(args[4]);
Simon Horman619e3cc2011-06-15 15:18:52 +0900858 break;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900859 default:
Willy Tarreaudec98142012-06-06 23:37:08 +0200860 switch (action) {
861 case STAT_CLI_O_TAB:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100862 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 +0200863 break;
864 case STAT_CLI_O_CLR:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100865 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 +0200866 break;
867 default:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100868 appctx->ctx.cli.msg = "Unknown action\n";
Willy Tarreaudec98142012-06-06 23:37:08 +0200869 break;
870 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100871 appctx->st0 = STAT_CLI_PRINT;
Simon Horman121f3052011-06-15 15:18:46 +0900872 return;
873 }
874
875 /* check permissions */
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200876 if (strm_li(s)->bind_conf->level < ACCESS_LVL_OPER) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100877 appctx->ctx.cli.msg = stats_permission_denied_msg;
878 appctx->st0 = STAT_CLI_PRINT;
Simon Horman121f3052011-06-15 15:18:46 +0900879 return;
880 }
881
Willy Tarreau07115412012-10-29 21:56:59 +0100882 ts = stktable_lookup_key(&px->table, static_table_key);
Simon Horman17bce342011-06-15 15:18:47 +0900883
Willy Tarreaudec98142012-06-06 23:37:08 +0200884 switch (action) {
885 case STAT_CLI_O_TAB:
886 if (!ts)
887 return;
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100888 chunk_reset(&trash);
889 if (!stats_dump_table_head_to_buffer(&trash, si, px, px))
Simon Horman17bce342011-06-15 15:18:47 +0900890 return;
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100891 stats_dump_table_entry_to_buffer(&trash, si, px, ts);
Simon Horman121f3052011-06-15 15:18:46 +0900892 return;
Willy Tarreaudec98142012-06-06 23:37:08 +0200893
894 case STAT_CLI_O_CLR:
895 if (!ts)
896 return;
897 if (ts->ref_cnt) {
898 /* don't delete an entry which is currently referenced */
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100899 appctx->ctx.cli.msg = "Entry currently in use, cannot remove\n";
900 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaudec98142012-06-06 23:37:08 +0200901 return;
902 }
903 stksess_kill(&px->table, ts);
904 break;
Simon Horman17bce342011-06-15 15:18:47 +0900905
Willy Tarreau654694e2012-06-07 01:03:16 +0200906 case STAT_CLI_O_SET:
Willy Tarreau654694e2012-06-07 01:03:16 +0200907 if (ts)
908 stktable_touch(&px->table, ts, 1);
909 else {
Willy Tarreau07115412012-10-29 21:56:59 +0100910 ts = stksess_new(&px->table, static_table_key);
Willy Tarreau654694e2012-06-07 01:03:16 +0200911 if (!ts) {
912 /* don't delete an entry which is currently referenced */
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100913 appctx->ctx.cli.msg = "Unable to allocate a new entry\n";
914 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau654694e2012-06-07 01:03:16 +0200915 return;
916 }
917 stktable_store(&px->table, ts, 1);
918 }
919
Willy Tarreau47060b62013-08-01 21:11:42 +0200920 for (cur_arg = 5; *args[cur_arg]; cur_arg += 2) {
921 if (strncmp(args[cur_arg], "data.", 5) != 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100922 appctx->ctx.cli.msg = "\"data.<type>\" followed by a value expected\n";
923 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau47060b62013-08-01 21:11:42 +0200924 return;
925 }
926
927 data_type = stktable_get_data_type(args[cur_arg] + 5);
928 if (data_type < 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100929 appctx->ctx.cli.msg = "Unknown data type\n";
930 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau47060b62013-08-01 21:11:42 +0200931 return;
932 }
933
934 if (!px->table.data_ofs[data_type]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100935 appctx->ctx.cli.msg = "Data type not stored in this table\n";
936 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau47060b62013-08-01 21:11:42 +0200937 return;
938 }
939
940 if (!*args[cur_arg+1] || strl2llrc(args[cur_arg+1], strlen(args[cur_arg+1]), &value) != 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100941 appctx->ctx.cli.msg = "Require a valid integer value to store\n";
942 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau47060b62013-08-01 21:11:42 +0200943 return;
944 }
945
946 ptr = stktable_data_ptr(&px->table, ts, data_type);
947
948 switch (stktable_data_types[data_type].std_type) {
949 case STD_T_SINT:
950 stktable_data_cast(ptr, std_t_sint) = value;
951 break;
952 case STD_T_UINT:
953 stktable_data_cast(ptr, std_t_uint) = value;
954 break;
955 case STD_T_ULL:
956 stktable_data_cast(ptr, std_t_ull) = value;
957 break;
958 case STD_T_FRQP:
959 /* We set both the current and previous values. That way
960 * the reported frequency is stable during all the period
961 * then slowly fades out. This allows external tools to
962 * push measures without having to update them too often.
963 */
964 frqp = &stktable_data_cast(ptr, std_t_frqp);
965 frqp->curr_tick = now_ms;
966 frqp->prev_ctr = 0;
967 frqp->curr_ctr = value;
968 break;
969 }
Willy Tarreau654694e2012-06-07 01:03:16 +0200970 }
971 break;
972
Willy Tarreaudec98142012-06-06 23:37:08 +0200973 default:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100974 appctx->ctx.cli.msg = "Unknown action\n";
975 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaudec98142012-06-06 23:37:08 +0200976 break;
Simon Horman121f3052011-06-15 15:18:46 +0900977 }
Simon Horman121f3052011-06-15 15:18:46 +0900978}
979
Willy Tarreau654694e2012-06-07 01:03:16 +0200980static void stats_sock_table_data_request(struct stream_interface *si, char **args, int action)
Simon Hormand5b9fd92011-06-15 15:18:48 +0900981{
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100982 struct appctx *appctx = __objt_appctx(si->end);
983
Willy Tarreau04b3a192013-04-13 09:41:37 +0200984 if (action != STAT_CLI_O_TAB && action != STAT_CLI_O_CLR) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100985 appctx->ctx.cli.msg = "content-based lookup is only supported with the \"show\" and \"clear\" actions";
986 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau654694e2012-06-07 01:03:16 +0200987 return;
988 }
989
Simon Hormand5b9fd92011-06-15 15:18:48 +0900990 /* condition on stored data value */
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100991 appctx->ctx.table.data_type = stktable_get_data_type(args[3] + 5);
992 if (appctx->ctx.table.data_type < 0) {
993 appctx->ctx.cli.msg = "Unknown data type\n";
994 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900995 return;
996 }
997
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100998 if (!((struct proxy *)appctx->ctx.table.target)->table.data_ofs[appctx->ctx.table.data_type]) {
999 appctx->ctx.cli.msg = "Data type not stored in this table\n";
1000 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +09001001 return;
1002 }
1003
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001004 appctx->ctx.table.data_op = get_std_op(args[4]);
1005 if (appctx->ctx.table.data_op < 0) {
1006 appctx->ctx.cli.msg = "Require and operator among \"eq\", \"ne\", \"le\", \"ge\", \"lt\", \"gt\"\n";
1007 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +09001008 return;
1009 }
1010
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001011 if (!*args[5] || strl2llrc(args[5], strlen(args[5]), &appctx->ctx.table.value) != 0) {
1012 appctx->ctx.cli.msg = "Require a valid integer value to compare against\n";
1013 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +09001014 return;
1015 }
1016}
1017
Willy Tarreaudec98142012-06-06 23:37:08 +02001018static void stats_sock_table_request(struct stream_interface *si, char **args, int action)
Simon Hormand5b9fd92011-06-15 15:18:48 +09001019{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001020 struct appctx *appctx = __objt_appctx(si->end);
1021
1022 appctx->ctx.table.data_type = -1;
1023 appctx->st2 = STAT_ST_INIT;
1024 appctx->ctx.table.target = NULL;
1025 appctx->ctx.table.proxy = NULL;
1026 appctx->ctx.table.entry = NULL;
1027 appctx->st0 = action;
Simon Hormand5b9fd92011-06-15 15:18:48 +09001028
1029 if (*args[2]) {
Willy Tarreaue2dc1fa2015-05-26 12:08:07 +02001030 appctx->ctx.table.target = proxy_tbl_by_name(args[2]);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001031 if (!appctx->ctx.table.target) {
1032 appctx->ctx.cli.msg = "No such table\n";
1033 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +09001034 return;
1035 }
1036 }
1037 else {
Willy Tarreaudec98142012-06-06 23:37:08 +02001038 if (action != STAT_CLI_O_TAB)
Simon Hormand5b9fd92011-06-15 15:18:48 +09001039 goto err_args;
1040 return;
1041 }
1042
1043 if (strcmp(args[3], "key") == 0)
Willy Tarreaudec98142012-06-06 23:37:08 +02001044 stats_sock_table_key_request(si, args, action);
Simon Hormanc88b8872011-06-15 15:18:49 +09001045 else if (strncmp(args[3], "data.", 5) == 0)
Willy Tarreau654694e2012-06-07 01:03:16 +02001046 stats_sock_table_data_request(si, args, action);
Simon Hormanc88b8872011-06-15 15:18:49 +09001047 else if (*args[3])
Simon Hormand5b9fd92011-06-15 15:18:48 +09001048 goto err_args;
1049
1050 return;
1051
1052err_args:
Willy Tarreaudec98142012-06-06 23:37:08 +02001053 switch (action) {
1054 case STAT_CLI_O_TAB:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001055 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 +02001056 break;
1057 case STAT_CLI_O_CLR:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001058 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 +02001059 break;
1060 default:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001061 appctx->ctx.cli.msg = "Unknown action\n";
Willy Tarreaudec98142012-06-06 23:37:08 +02001062 break;
1063 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001064 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +09001065}
1066
Willy Tarreau532a4502011-09-07 22:37:44 +02001067/* Expects to find a frontend named <arg> and returns it, otherwise displays various
Willy Tarreau87b09662015-04-03 00:22:06 +02001068 * adequate error messages and returns NULL. This function also expects the stream
Willy Tarreau532a4502011-09-07 22:37:44 +02001069 * level to be admin.
1070 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001071static struct proxy *expect_frontend_admin(struct stream *s, struct stream_interface *si, const char *arg)
Willy Tarreau532a4502011-09-07 22:37:44 +02001072{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001073 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau532a4502011-09-07 22:37:44 +02001074 struct proxy *px;
1075
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001076 if (strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001077 appctx->ctx.cli.msg = stats_permission_denied_msg;
1078 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001079 return NULL;
1080 }
1081
1082 if (!*arg) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001083 appctx->ctx.cli.msg = "A frontend name is expected.\n";
1084 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001085 return NULL;
1086 }
1087
Willy Tarreau9e0bb102015-05-26 11:24:42 +02001088 px = proxy_fe_by_name(arg);
Willy Tarreau532a4502011-09-07 22:37:44 +02001089 if (!px) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001090 appctx->ctx.cli.msg = "No such frontend.\n";
1091 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001092 return NULL;
1093 }
1094 return px;
1095}
1096
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001097/* Expects to find a backend and a server in <arg> under the form <backend>/<server>,
1098 * and returns the pointer to the server. Otherwise, display adequate error messages
Willy Tarreau87b09662015-04-03 00:22:06 +02001099 * and returns NULL. This function also expects the stream level to be admin. Note:
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001100 * the <arg> is modified to remove the '/'.
1101 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001102static struct server *expect_server_admin(struct stream *s, struct stream_interface *si, char *arg)
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001103{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001104 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001105 struct proxy *px;
1106 struct server *sv;
1107 char *line;
1108
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001109 if (strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001110 appctx->ctx.cli.msg = stats_permission_denied_msg;
1111 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001112 return NULL;
1113 }
1114
1115 /* split "backend/server" and make <line> point to server */
1116 for (line = arg; *line; line++)
1117 if (*line == '/') {
1118 *line++ = '\0';
1119 break;
1120 }
1121
1122 if (!*line || !*arg) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001123 appctx->ctx.cli.msg = "Require 'backend/server'.\n";
1124 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001125 return NULL;
1126 }
1127
1128 if (!get_backend_server(arg, line, &px, &sv)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001129 appctx->ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
1130 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001131 return NULL;
1132 }
1133
1134 if (px->state == PR_STSTOPPED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001135 appctx->ctx.cli.msg = "Proxy is disabled.\n";
1136 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001137 return NULL;
1138 }
1139
1140 return sv;
1141}
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02001142
1143/* This function is used with TLS ticket keys management. It permits to browse
1144 * each reference. The variable <getnext> must contain the current node,
1145 * <end> point to the root node.
1146 */
1147#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
1148static inline
1149struct tls_keys_ref *tlskeys_list_get_next(struct tls_keys_ref *getnext, struct list *end)
1150{
1151 struct tls_keys_ref *ref = getnext;
1152
1153 while (1) {
1154
1155 /* Get next list entry. */
1156 ref = LIST_NEXT(&ref->list, struct tls_keys_ref *, list);
1157
1158 /* If the entry is the last of the list, return NULL. */
1159 if (&ref->list == end)
1160 return NULL;
1161
1162 return ref;
1163 }
1164}
1165
1166static inline
1167struct tls_keys_ref *tlskeys_ref_lookup_ref(const char *reference)
1168{
1169 int id;
1170 char *error;
1171
1172 /* If the reference starts by a '#', this is numeric id. */
1173 if (reference[0] == '#') {
1174 /* Try to convert the numeric id. If the conversion fails, the lookup fails. */
1175 id = strtol(reference + 1, &error, 10);
1176 if (*error != '\0')
1177 return NULL;
1178
1179 /* Perform the unique id lookup. */
1180 return tlskeys_ref_lookupid(id);
1181 }
1182
1183 /* Perform the string lookup. */
1184 return tlskeys_ref_lookup(reference);
1185}
1186#endif
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001187
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001188/* This function is used with map and acl management. It permits to browse
1189 * each reference. The variable <getnext> must contain the current node,
1190 * <end> point to the root node and the <flags> permit to filter required
1191 * nodes.
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001192 */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001193static inline
1194struct pat_ref *pat_list_get_next(struct pat_ref *getnext, struct list *end,
1195 unsigned int flags)
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001196{
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001197 struct pat_ref *ref = getnext;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001198
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001199 while (1) {
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001200
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001201 /* Get next list entry. */
1202 ref = LIST_NEXT(&ref->list, struct pat_ref *, list);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001203
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001204 /* If the entry is the last of the list, return NULL. */
1205 if (&ref->list == end)
1206 return NULL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001207
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001208 /* If the entry match the flag, return it. */
1209 if (ref->flags & flags)
1210 return ref;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001211 }
1212}
1213
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001214static inline
1215struct pat_ref *pat_ref_lookup_ref(const char *reference)
1216{
1217 int id;
1218 char *error;
1219
1220 /* If the reference starts by a '#', this is numeric id. */
1221 if (reference[0] == '#') {
1222 /* Try to convert the numeric id. If the conversion fails, the lookup fails. */
1223 id = strtol(reference + 1, &error, 10);
1224 if (*error != '\0')
1225 return NULL;
1226
1227 /* Perform the unique id lookup. */
1228 return pat_ref_lookupid(id);
1229 }
1230
1231 /* Perform the string lookup. */
1232 return pat_ref_lookup(reference);
1233}
1234
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001235/* This function is used with map and acl management. It permits to browse
1236 * each reference.
1237 */
1238static inline
1239struct pattern_expr *pat_expr_get_next(struct pattern_expr *getnext, struct list *end)
1240{
1241 struct pattern_expr *expr;
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01001242 expr = LIST_NEXT(&getnext->list, struct pattern_expr *, list);
1243 if (&expr->list == end)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001244 return NULL;
1245 return expr;
1246}
1247
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001248/* Processes the stats interpreter on the statistics socket. This function is
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001249 * called from an applet running in a stream interface. The function returns 1
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001250 * if the request was understood, otherwise zero. It sets appctx->st0 to a value
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02001251 * designating the function which will have to process the request, which can
1252 * also be the print function to display the return message set into cli.msg.
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001253 */
Simon Horman9bd2c732011-06-15 15:18:44 +09001254static int stats_sock_parse_request(struct stream_interface *si, char *line)
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001255{
Willy Tarreau87b09662015-04-03 00:22:06 +02001256 struct stream *s = si_strm(si);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001257 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001258 char *args[MAX_STATS_ARGS + 1];
1259 int arg;
Thierry FOURNIER48bcfda2013-12-10 18:54:58 +01001260 int i, j;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001261
1262 while (isspace((unsigned char)*line))
1263 line++;
1264
1265 arg = 0;
1266 args[arg] = line;
1267
1268 while (*line && arg < MAX_STATS_ARGS) {
Thierry FOURNIER48bcfda2013-12-10 18:54:58 +01001269 if (*line == '\\') {
1270 line++;
1271 if (*line == '\0')
1272 break;
1273 }
1274 else if (isspace((unsigned char)*line)) {
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001275 *line++ = '\0';
1276
1277 while (isspace((unsigned char)*line))
1278 line++;
1279
1280 args[++arg] = line;
1281 continue;
1282 }
1283
1284 line++;
1285 }
1286
1287 while (++arg <= MAX_STATS_ARGS)
1288 args[arg] = line;
1289
Thierry FOURNIER48bcfda2013-12-10 18:54:58 +01001290 /* remove \ */
1291 arg = 0;
1292 while (*args[arg] != '\0') {
1293 j = 0;
1294 for (i=0; args[arg][i] != '\0'; i++) {
1295 if (args[arg][i] == '\\')
1296 continue;
1297 args[arg][j] = args[arg][i];
1298 j++;
1299 }
1300 args[arg][j] = '\0';
1301 arg++;
1302 }
1303
Willy Tarreau6bcb95d2015-05-04 18:07:56 +02001304 appctx->ctx.stats.scope_str = 0;
1305 appctx->ctx.stats.scope_len = 0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001306 appctx->ctx.stats.flags = 0;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001307 if (strcmp(args[0], "show") == 0) {
Baptiste Assmann9b6857e2015-09-18 14:49:12 +02001308 if (strcmp(args[1], "backend") == 0) {
Cyril Bonté6ca9e012016-05-06 12:18:49 +02001309 appctx->ctx.be.px = NULL;
Baptiste Assmann9b6857e2015-09-18 14:49:12 +02001310 appctx->st2 = STAT_ST_INIT;
1311 appctx->st0 = STAT_CLI_O_BACKEND;
1312 }
Willy Tarreauae795722016-02-16 11:27:28 +01001313 else if (strcmp(args[1], "env") == 0) {
1314 extern char **environ;
1315
1316 if (strm_li(s)->bind_conf->level < ACCESS_LVL_OPER) {
1317 appctx->ctx.cli.msg = stats_permission_denied_msg;
1318 appctx->st0 = STAT_CLI_PRINT;
1319 return 1;
1320 }
1321 appctx->ctx.env.var = environ;
1322 appctx->st2 = STAT_ST_INIT;
1323 appctx->st0 = STAT_CLI_O_ENV; // stats_dump_env_to_buffer
1324
1325 if (*args[2]) {
1326 int len = strlen(args[2]);
1327
1328 for (; *appctx->ctx.env.var; appctx->ctx.env.var++) {
1329 if (strncmp(*appctx->ctx.env.var, args[2], len) == 0 &&
1330 (*appctx->ctx.env.var)[len] == '=')
1331 break;
1332 }
1333 if (!*appctx->ctx.env.var) {
1334 appctx->ctx.cli.msg = "Variable not found\n";
1335 appctx->st0 = STAT_CLI_PRINT;
1336 return 1;
1337 }
1338 appctx->st2 = STAT_ST_END;
1339 }
1340 }
1341 else if (strcmp(args[1], "stat") == 0) {
Baptiste Assmann3863f972015-05-17 00:33:24 +02001342 if (strcmp(args[2], "resolvers") == 0) {
1343 struct dns_resolvers *presolvers;
1344
Andrew Hayworth68d05342015-10-02 20:33:01 +00001345 if (*args[3]) {
1346 appctx->ctx.resolvers.ptr = NULL;
1347 list_for_each_entry(presolvers, &dns_resolvers, list) {
1348 if (strcmp(presolvers->id, args[3]) == 0) {
1349 appctx->ctx.resolvers.ptr = presolvers;
1350 break;
1351 }
Baptiste Assmann3863f972015-05-17 00:33:24 +02001352 }
Andrew Hayworth68d05342015-10-02 20:33:01 +00001353 if (appctx->ctx.resolvers.ptr == NULL) {
1354 appctx->ctx.cli.msg = "Can't find that resolvers section\n";
1355 appctx->st0 = STAT_CLI_PRINT;
1356 return 1;
1357 }
Baptiste Assmann3863f972015-05-17 00:33:24 +02001358 }
1359
1360 appctx->st2 = STAT_ST_INIT;
1361 appctx->st0 = STAT_CLI_O_RESOLVERS;
1362 return 1;
1363 }
1364 else if (*args[2] && *args[3] && *args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001365 appctx->ctx.stats.flags |= STAT_BOUND;
1366 appctx->ctx.stats.iid = atoi(args[2]);
1367 appctx->ctx.stats.type = atoi(args[3]);
1368 appctx->ctx.stats.sid = atoi(args[4]);
Willy Tarreau1e62df92016-01-11 18:57:53 +01001369 if (strcmp(args[5], "typed") == 0)
1370 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001371 }
Willy Tarreau1e62df92016-01-11 18:57:53 +01001372 else if (strcmp(args[2], "typed") == 0)
1373 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001374
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001375 appctx->st2 = STAT_ST_INIT;
1376 appctx->st0 = STAT_CLI_O_STAT; // stats_dump_stat_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001377 }
1378 else if (strcmp(args[1], "info") == 0) {
Willy Tarreaucb809122016-01-11 20:08:42 +01001379 if (strcmp(args[2], "typed") == 0)
1380 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001381 appctx->st2 = STAT_ST_INIT;
1382 appctx->st0 = STAT_CLI_O_INFO; // stats_dump_info_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001383 }
Baptiste Assmann28289462015-07-03 08:01:20 +02001384 else if (strcmp(args[1], "servers") == 0 && strcmp(args[2], "state") == 0) {
Cyril Bonté76a99782016-05-06 12:18:48 +02001385 appctx->ctx.server_state.iid = 0;
1386 appctx->ctx.server_state.px = NULL;
1387 appctx->ctx.server_state.sv = NULL;
Baptiste Assmann28289462015-07-03 08:01:20 +02001388
1389 /* check if a backend name has been provided */
1390 if (*args[3]) {
1391 /* read server state from local file */
Cyril Bonté76a99782016-05-06 12:18:48 +02001392 appctx->ctx.server_state.px = proxy_be_by_name(args[3]);
Baptiste Assmann28289462015-07-03 08:01:20 +02001393
Cyril Bonté76a99782016-05-06 12:18:48 +02001394 if (!appctx->ctx.server_state.px) {
Baptiste Assmann28289462015-07-03 08:01:20 +02001395 appctx->ctx.cli.msg = "Can't find backend.\n";
1396 appctx->st0 = STAT_CLI_PRINT;
1397 return 1;
1398 }
Cyril Bonté76a99782016-05-06 12:18:48 +02001399 appctx->ctx.server_state.iid = appctx->ctx.server_state.px->uuid;
Baptiste Assmann28289462015-07-03 08:01:20 +02001400 }
1401 appctx->st2 = STAT_ST_INIT;
1402 appctx->st0 = STAT_CLI_O_SERVERS_STATE; // stats_dump_servers_state_to_buffer
1403 return 1;
1404 }
Willy Tarreau12833bb2014-01-28 16:49:56 +01001405 else if (strcmp(args[1], "pools") == 0) {
1406 appctx->st2 = STAT_ST_INIT;
1407 appctx->st0 = STAT_CLI_O_POOLS; // stats_dump_pools_to_buffer
1408 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001409 else if (strcmp(args[1], "sess") == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001410 appctx->st2 = STAT_ST_INIT;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001411 if (strm_li(s)->bind_conf->level < ACCESS_LVL_OPER) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001412 appctx->ctx.cli.msg = stats_permission_denied_msg;
1413 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +02001414 return 1;
1415 }
Willy Tarreau76153662012-11-26 01:16:39 +01001416 if (*args[2] && strcmp(args[2], "all") == 0)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001417 appctx->ctx.sess.target = (void *)-1;
Willy Tarreau76153662012-11-26 01:16:39 +01001418 else if (*args[2])
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001419 appctx->ctx.sess.target = (void *)strtoul(args[2], NULL, 0);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01001420 else
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001421 appctx->ctx.sess.target = NULL;
Willy Tarreau87b09662015-04-03 00:22:06 +02001422 appctx->ctx.sess.section = 0; /* start with stream status */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001423 appctx->ctx.sess.pos = 0;
1424 appctx->st0 = STAT_CLI_O_SESS; // stats_dump_sess_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001425 }
1426 else if (strcmp(args[1], "errors") == 0) {
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001427 if (strm_li(s)->bind_conf->level < ACCESS_LVL_OPER) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001428 appctx->ctx.cli.msg = stats_permission_denied_msg;
1429 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +02001430 return 1;
1431 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001432 if (*args[2])
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001433 appctx->ctx.errors.iid = atoi(args[2]);
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001434 else
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001435 appctx->ctx.errors.iid = -1;
1436 appctx->ctx.errors.px = NULL;
1437 appctx->st2 = STAT_ST_INIT;
1438 appctx->st0 = STAT_CLI_O_ERR; // stats_dump_errors_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001439 }
Willy Tarreau69f58c82010-07-12 17:55:33 +02001440 else if (strcmp(args[1], "table") == 0) {
Willy Tarreaudec98142012-06-06 23:37:08 +02001441 stats_sock_table_request(si, args, STAT_CLI_O_TAB);
Willy Tarreau69f58c82010-07-12 17:55:33 +02001442 }
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02001443 else if (strcmp(args[1], "tls-keys") == 0) {
1444#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
1445 appctx->st2 = STAT_ST_INIT;
1446 appctx->st0 = STAT_CLI_O_TLSK;
1447#else
1448 appctx->ctx.cli.msg = "HAProxy was compiled against a version of OpenSSL "
1449 "that doesn't support specifying TLS ticket keys\n";
1450 appctx->st0 = STAT_CLI_PRINT;
1451#endif
1452 return 1;
1453 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001454 else if (strcmp(args[1], "map") == 0 ||
1455 strcmp(args[1], "acl") == 0) {
1456
1457 /* Set ACL or MAP flags. */
1458 if (args[1][0] == 'm')
1459 appctx->ctx.map.display_flags = PAT_REF_MAP;
1460 else
1461 appctx->ctx.map.display_flags = PAT_REF_ACL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001462
Cyril Bontédb98eb32016-05-06 12:18:50 +02001463 /* no parameter: display all map available */
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001464 if (!*args[2]) {
1465 appctx->st2 = STAT_ST_INIT;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001466 appctx->st0 = STAT_CLI_O_PATS;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001467 return 1;
1468 }
1469
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001470 /* lookup into the refs and check the map flag */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001471 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001472 if (!appctx->ctx.map.ref ||
1473 !(appctx->ctx.map.ref->flags & appctx->ctx.map.display_flags)) {
1474 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001475 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001476 else
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001477 appctx->ctx.cli.msg = "Unknown ACL identifier. Please use #<id> or <file>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001478 appctx->st0 = STAT_CLI_PRINT;
1479 return 1;
1480 }
1481 appctx->st2 = STAT_ST_INIT;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001482 appctx->st0 = STAT_CLI_O_PAT;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001483 }
Aman Guptaceafb4a2012-04-02 18:57:54 -07001484 else { /* neither "stat" nor "info" nor "sess" nor "errors" nor "table" */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001485 return 0;
1486 }
1487 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001488 else if (strcmp(args[0], "clear") == 0) {
1489 if (strcmp(args[1], "counters") == 0) {
1490 struct proxy *px;
1491 struct server *sv;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001492 struct listener *li;
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001493 int clrall = 0;
1494
1495 if (strcmp(args[2], "all") == 0)
1496 clrall = 1;
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001497
Willy Tarreau6162db22009-10-10 17:13:00 +02001498 /* check permissions */
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001499 if (strm_li(s)->bind_conf->level < ACCESS_LVL_OPER ||
1500 (clrall && strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001501 appctx->ctx.cli.msg = stats_permission_denied_msg;
1502 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +02001503 return 1;
1504 }
1505
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001506 for (px = proxy; px; px = px->next) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001507 if (clrall) {
1508 memset(&px->be_counters, 0, sizeof(px->be_counters));
1509 memset(&px->fe_counters, 0, sizeof(px->fe_counters));
1510 }
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001511 else {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001512 px->be_counters.conn_max = 0;
1513 px->be_counters.p.http.rps_max = 0;
1514 px->be_counters.sps_max = 0;
1515 px->be_counters.cps_max = 0;
1516 px->be_counters.nbpend_max = 0;
1517
1518 px->fe_counters.conn_max = 0;
1519 px->fe_counters.p.http.rps_max = 0;
1520 px->fe_counters.sps_max = 0;
1521 px->fe_counters.cps_max = 0;
1522 px->fe_counters.nbpend_max = 0;
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001523 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001524
1525 for (sv = px->srv; sv; sv = sv->next)
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001526 if (clrall)
1527 memset(&sv->counters, 0, sizeof(sv->counters));
1528 else {
1529 sv->counters.cur_sess_max = 0;
1530 sv->counters.nbpend_max = 0;
1531 sv->counters.sps_max = 0;
1532 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001533
Willy Tarreaua7944ad2012-09-26 21:03:11 +02001534 list_for_each_entry(li, &px->conf.listeners, by_fe)
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001535 if (li->counters) {
1536 if (clrall)
1537 memset(li->counters, 0, sizeof(*li->counters));
1538 else
1539 li->counters->conn_max = 0;
1540 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001541 }
1542
Willy Tarreau81c25d02011-09-07 15:17:21 +02001543 global.cps_max = 0;
Willy Tarreau93e7c002013-10-07 18:51:07 +02001544 global.sps_max = 0;
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001545 return 1;
1546 }
Willy Tarreau88ee3972010-07-13 13:48:00 +02001547 else if (strcmp(args[1], "table") == 0) {
Willy Tarreaudec98142012-06-06 23:37:08 +02001548 stats_sock_table_request(si, args, STAT_CLI_O_CLR);
Willy Tarreau88ee3972010-07-13 13:48:00 +02001549 /* end of processing */
1550 return 1;
1551 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001552 else if (strcmp(args[1], "map") == 0 || strcmp(args[1], "acl") == 0) {
1553 /* Set ACL or MAP flags. */
1554 if (args[1][0] == 'm')
1555 appctx->ctx.map.display_flags = PAT_REF_MAP;
1556 else
1557 appctx->ctx.map.display_flags = PAT_REF_ACL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001558
1559 /* no parameter */
1560 if (!*args[2]) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001561 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
1562 appctx->ctx.cli.msg = "Missing map identifier.\n";
1563 else
1564 appctx->ctx.cli.msg = "Missing ACL identifier.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001565 appctx->st0 = STAT_CLI_PRINT;
1566 return 1;
1567 }
1568
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001569 /* lookup into the refs and check the map flag */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001570 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001571 if (!appctx->ctx.map.ref ||
1572 !(appctx->ctx.map.ref->flags & appctx->ctx.map.display_flags)) {
1573 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001574 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001575 else
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001576 appctx->ctx.cli.msg = "Unknown ACL identifier. Please use #<id> or <file>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001577 appctx->st0 = STAT_CLI_PRINT;
1578 return 1;
1579 }
1580
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001581 /* Clear all. */
1582 pat_ref_prune(appctx->ctx.map.ref);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001583
1584 /* return response */
Thierry FOURNIER07e78c52014-12-18 15:28:01 +01001585 appctx->st0 = STAT_CLI_PROMPT;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001586 return 1;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001587 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001588 else {
Willy Tarreau88ee3972010-07-13 13:48:00 +02001589 /* unknown "clear" argument */
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001590 return 0;
1591 }
1592 }
Willy Tarreau38338fa2009-10-10 18:37:29 +02001593 else if (strcmp(args[0], "get") == 0) {
1594 if (strcmp(args[1], "weight") == 0) {
1595 struct proxy *px;
1596 struct server *sv;
1597
1598 /* split "backend/server" and make <line> point to server */
1599 for (line = args[2]; *line; line++)
1600 if (*line == '/') {
1601 *line++ = '\0';
1602 break;
1603 }
1604
1605 if (!*line) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001606 appctx->ctx.cli.msg = "Require 'backend/server'.\n";
1607 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau38338fa2009-10-10 18:37:29 +02001608 return 1;
1609 }
1610
1611 if (!get_backend_server(args[2], line, &px, &sv)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001612 appctx->ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
1613 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau38338fa2009-10-10 18:37:29 +02001614 return 1;
1615 }
1616
1617 /* return server's effective weight at the moment */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001618 snprintf(trash.str, trash.size, "%d (initial %d)\n", sv->uweight, sv->iweight);
Willy Tarreaubc18da12015-03-13 14:00:47 +01001619 if (bi_putstr(si_ic(si), trash.str) == -1)
Willy Tarreaufe127932015-04-21 19:23:39 +02001620 si_applet_cant_put(si);
Willy Tarreaubc18da12015-03-13 14:00:47 +01001621
Willy Tarreau38338fa2009-10-10 18:37:29 +02001622 return 1;
1623 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001624 else if (strcmp(args[1], "map") == 0 || strcmp(args[1], "acl") == 0) {
1625 /* Set flags. */
1626 if (args[1][0] == 'm')
1627 appctx->ctx.map.display_flags = PAT_REF_MAP;
1628 else
1629 appctx->ctx.map.display_flags = PAT_REF_ACL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001630
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001631 /* No parameter. */
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001632 if (!*args[2] || !*args[3]) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001633 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
1634 appctx->ctx.cli.msg = "Missing map identifier and/or key.\n";
1635 else
1636 appctx->ctx.cli.msg = "Missing ACL identifier and/or key.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001637 appctx->st0 = STAT_CLI_PRINT;
1638 return 1;
1639 }
1640
1641 /* lookup into the maps */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001642 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001643 if (!appctx->ctx.map.ref) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001644 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001645 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001646 else
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001647 appctx->ctx.cli.msg = "Unknown ACL identifier. Please use #<id> or <file>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001648 appctx->st0 = STAT_CLI_PRINT;
1649 return 1;
1650 }
1651
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001652 /* copy input string. The string must be allocated because
1653 * it may be used over multiple iterations. It's released
1654 * at the end and upon abort anyway.
1655 */
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001656 appctx->ctx.map.chunk.len = strlen(args[3]);
1657 appctx->ctx.map.chunk.size = appctx->ctx.map.chunk.len + 1;
1658 appctx->ctx.map.chunk.str = strdup(args[3]);
1659 if (!appctx->ctx.map.chunk.str) {
1660 appctx->ctx.cli.msg = "Out of memory error.\n";
1661 appctx->st0 = STAT_CLI_PRINT;
1662 return 1;
1663 }
1664
1665 /* prepare response */
1666 appctx->st2 = STAT_ST_INIT;
1667 appctx->st0 = STAT_CLI_O_MLOOK;
1668 }
Willy Tarreau38338fa2009-10-10 18:37:29 +02001669 else { /* not "get weight" */
1670 return 0;
1671 }
1672 }
Willy Tarreau4483d432009-10-10 19:30:08 +02001673 else if (strcmp(args[0], "set") == 0) {
1674 if (strcmp(args[1], "weight") == 0) {
Willy Tarreau4483d432009-10-10 19:30:08 +02001675 struct server *sv;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001676 const char *warning;
Willy Tarreau4483d432009-10-10 19:30:08 +02001677
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001678 sv = expect_server_admin(s, si, args[2]);
1679 if (!sv)
Willy Tarreau4483d432009-10-10 19:30:08 +02001680 return 1;
Willy Tarreau4483d432009-10-10 19:30:08 +02001681
Simon Horman7d09b9a2013-02-12 10:45:51 +09001682 warning = server_parse_weight_change_request(sv, args[3]);
1683 if (warning) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001684 appctx->ctx.cli.msg = warning;
1685 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +02001686 }
Willy Tarreau4483d432009-10-10 19:30:08 +02001687 return 1;
1688 }
Willy Tarreau2a4b70f2014-05-22 18:42:35 +02001689 else if (strcmp(args[1], "server") == 0) {
1690 struct server *sv;
1691 const char *warning;
1692
1693 sv = expect_server_admin(s, si, args[2]);
1694 if (!sv)
1695 return 1;
1696
1697 if (strcmp(args[3], "weight") == 0) {
1698 warning = server_parse_weight_change_request(sv, args[4]);
1699 if (warning) {
1700 appctx->ctx.cli.msg = warning;
1701 appctx->st0 = STAT_CLI_PRINT;
1702 }
1703 }
1704 else if (strcmp(args[3], "state") == 0) {
1705 if (strcmp(args[4], "ready") == 0)
1706 srv_adm_set_ready(sv);
1707 else if (strcmp(args[4], "drain") == 0)
1708 srv_adm_set_drain(sv);
1709 else if (strcmp(args[4], "maint") == 0)
1710 srv_adm_set_maint(sv);
1711 else {
1712 appctx->ctx.cli.msg = "'set server <srv> state' expects 'ready', 'drain' and 'maint'.\n";
1713 appctx->st0 = STAT_CLI_PRINT;
1714 }
1715 }
1716 else if (strcmp(args[3], "health") == 0) {
1717 if (sv->track) {
1718 appctx->ctx.cli.msg = "cannot change health on a tracking server.\n";
1719 appctx->st0 = STAT_CLI_PRINT;
1720 }
1721 else if (strcmp(args[4], "up") == 0) {
1722 sv->check.health = sv->check.rise + sv->check.fall - 1;
1723 srv_set_running(sv, "changed from CLI");
1724 }
1725 else if (strcmp(args[4], "stopping") == 0) {
1726 sv->check.health = sv->check.rise + sv->check.fall - 1;
1727 srv_set_stopping(sv, "changed from CLI");
1728 }
1729 else if (strcmp(args[4], "down") == 0) {
1730 sv->check.health = 0;
1731 srv_set_stopped(sv, "changed from CLI");
1732 }
1733 else {
1734 appctx->ctx.cli.msg = "'set server <srv> health' expects 'up', 'stopping', or 'down'.\n";
1735 appctx->st0 = STAT_CLI_PRINT;
1736 }
1737 }
1738 else if (strcmp(args[3], "agent") == 0) {
1739 if (!(sv->agent.state & CHK_ST_ENABLED)) {
1740 appctx->ctx.cli.msg = "agent checks are not enabled on this server.\n";
1741 appctx->st0 = STAT_CLI_PRINT;
1742 }
1743 else if (strcmp(args[4], "up") == 0) {
1744 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
1745 srv_set_running(sv, "changed from CLI");
1746 }
1747 else if (strcmp(args[4], "down") == 0) {
1748 sv->agent.health = 0;
1749 srv_set_stopped(sv, "changed from CLI");
1750 }
1751 else {
1752 appctx->ctx.cli.msg = "'set server <srv> agent' expects 'up' or 'down'.\n";
1753 appctx->st0 = STAT_CLI_PRINT;
1754 }
1755 }
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001756 else if (strcmp(args[3], "addr") == 0) {
Thierry Fournier09a91782016-02-24 08:25:39 +01001757 warning = server_parse_addr_change_request(sv, args[4], "stats command");
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001758 if (warning) {
1759 appctx->ctx.cli.msg = warning;
1760 appctx->st0 = STAT_CLI_PRINT;
1761 }
1762 }
Willy Tarreau2a4b70f2014-05-22 18:42:35 +02001763 else {
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001764 appctx->ctx.cli.msg = "'set server <srv>' only supports 'agent', 'health', 'state', 'weight' and 'addr'.\n";
Willy Tarreau2a4b70f2014-05-22 18:42:35 +02001765 appctx->st0 = STAT_CLI_PRINT;
1766 }
1767 return 1;
1768 }
Willy Tarreau7aabd112010-01-26 10:59:06 +01001769 else if (strcmp(args[1], "timeout") == 0) {
1770 if (strcmp(args[2], "cli") == 0) {
1771 unsigned timeout;
1772 const char *res;
1773
1774 if (!*args[3]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001775 appctx->ctx.cli.msg = "Expects an integer value.\n";
1776 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001777 return 1;
1778 }
1779
1780 res = parse_time_err(args[3], &timeout, TIME_UNIT_S);
1781 if (res || timeout < 1) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001782 appctx->ctx.cli.msg = "Invalid timeout value.\n";
1783 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001784 return 1;
1785 }
1786
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001787 s->req.rto = s->res.wto = 1 + MS_TO_TICKS(timeout*1000);
Willy Tarreau7aabd112010-01-26 10:59:06 +01001788 return 1;
1789 }
1790 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001791 appctx->ctx.cli.msg = "'set timeout' only supports 'cli'.\n";
1792 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001793 return 1;
1794 }
1795 }
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001796 else if (strcmp(args[1], "maxconn") == 0) {
1797 if (strcmp(args[2], "frontend") == 0) {
1798 struct proxy *px;
1799 struct listener *l;
1800 int v;
1801
Willy Tarreau532a4502011-09-07 22:37:44 +02001802 px = expect_frontend_admin(s, si, args[3]);
1803 if (!px)
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001804 return 1;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001805
1806 if (!*args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001807 appctx->ctx.cli.msg = "Integer value expected.\n";
1808 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001809 return 1;
1810 }
1811
1812 v = atoi(args[4]);
Willy Tarreau3c7a79d2012-09-26 21:07:15 +02001813 if (v < 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001814 appctx->ctx.cli.msg = "Value out of range.\n";
1815 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001816 return 1;
1817 }
1818
1819 /* OK, the value is fine, so we assign it to the proxy and to all of
1820 * its listeners. The blocked ones will be dequeued.
1821 */
1822 px->maxconn = v;
Willy Tarreaua7944ad2012-09-26 21:03:11 +02001823 list_for_each_entry(l, &px->conf.listeners, by_fe) {
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001824 l->maxconn = v;
1825 if (l->state == LI_FULL)
1826 resume_listener(l);
1827 }
1828
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001829 if (px->maxconn > px->feconn && !LIST_ISEMPTY(&strm_fe(s)->listener_queue))
1830 dequeue_all_listeners(&strm_fe(s)->listener_queue);
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001831
1832 return 1;
1833 }
Andrew Hayworthedb93a72015-10-27 21:46:25 +00001834 else if (strcmp(args[2], "server") == 0) {
1835 struct server *sv;
Nenad Merdanovic5c3ed342016-04-24 23:10:07 +02001836 const char *warning;
Andrew Hayworthedb93a72015-10-27 21:46:25 +00001837
1838 sv = expect_server_admin(s, si, args[3]);
1839 if (!sv)
1840 return 1;
1841
Nenad Merdanovic5c3ed342016-04-24 23:10:07 +02001842 warning = server_parse_maxconn_change_request(sv, args[4]);
1843 if (warning) {
1844 appctx->ctx.cli.msg = warning;
Andrew Hayworthedb93a72015-10-27 21:46:25 +00001845 appctx->st0 = STAT_CLI_PRINT;
Andrew Hayworthedb93a72015-10-27 21:46:25 +00001846 }
1847
Andrew Hayworthedb93a72015-10-27 21:46:25 +00001848 return 1;
1849 }
Willy Tarreau91886b62011-09-07 14:38:31 +02001850 else if (strcmp(args[2], "global") == 0) {
1851 int v;
1852
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001853 if (strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001854 appctx->ctx.cli.msg = stats_permission_denied_msg;
1855 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau91886b62011-09-07 14:38:31 +02001856 return 1;
1857 }
1858
1859 if (!*args[3]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001860 appctx->ctx.cli.msg = "Expects an integer value.\n";
1861 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau91886b62011-09-07 14:38:31 +02001862 return 1;
1863 }
1864
1865 v = atoi(args[3]);
1866 if (v > global.hardmaxconn) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001867 appctx->ctx.cli.msg = "Value out of range.\n";
1868 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau91886b62011-09-07 14:38:31 +02001869 return 1;
1870 }
1871
1872 /* check for unlimited values */
1873 if (v <= 0)
1874 v = global.hardmaxconn;
1875
1876 global.maxconn = v;
1877
1878 /* Dequeues all of the listeners waiting for a resource */
1879 if (!LIST_ISEMPTY(&global_listener_queue))
1880 dequeue_all_listeners(&global_listener_queue);
1881
1882 return 1;
1883 }
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001884 else {
Andrew Hayworthedb93a72015-10-27 21:46:25 +00001885 appctx->ctx.cli.msg = "'set maxconn' only supports 'frontend', 'server', and 'global'.\n";
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001886 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001887 return 1;
1888 }
1889 }
Willy Tarreauf5b22872011-09-07 16:13:44 +02001890 else if (strcmp(args[1], "rate-limit") == 0) {
1891 if (strcmp(args[2], "connections") == 0) {
1892 if (strcmp(args[3], "global") == 0) {
1893 int v;
1894
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001895 if (strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001896 appctx->ctx.cli.msg = stats_permission_denied_msg;
1897 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001898 return 1;
1899 }
1900
1901 if (!*args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001902 appctx->ctx.cli.msg = "Expects an integer value.\n";
1903 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001904 return 1;
1905 }
1906
1907 v = atoi(args[4]);
1908 if (v < 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001909 appctx->ctx.cli.msg = "Value out of range.\n";
1910 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001911 return 1;
1912 }
1913
1914 global.cps_lim = v;
1915
1916 /* Dequeues all of the listeners waiting for a resource */
1917 if (!LIST_ISEMPTY(&global_listener_queue))
1918 dequeue_all_listeners(&global_listener_queue);
1919
1920 return 1;
1921 }
1922 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001923 appctx->ctx.cli.msg = "'set rate-limit connections' only supports 'global'.\n";
1924 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001925 return 1;
1926 }
1927 }
Willy Tarreau93e7c002013-10-07 18:51:07 +02001928 else if (strcmp(args[2], "sessions") == 0) {
1929 if (strcmp(args[3], "global") == 0) {
1930 int v;
1931
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001932 if (strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau93e7c002013-10-07 18:51:07 +02001933 appctx->ctx.cli.msg = stats_permission_denied_msg;
1934 appctx->st0 = STAT_CLI_PRINT;
1935 return 1;
1936 }
1937
1938 if (!*args[4]) {
1939 appctx->ctx.cli.msg = "Expects an integer value.\n";
1940 appctx->st0 = STAT_CLI_PRINT;
1941 return 1;
1942 }
1943
1944 v = atoi(args[4]);
1945 if (v < 0) {
1946 appctx->ctx.cli.msg = "Value out of range.\n";
1947 appctx->st0 = STAT_CLI_PRINT;
1948 return 1;
1949 }
1950
1951 global.sps_lim = v;
1952
1953 /* Dequeues all of the listeners waiting for a resource */
1954 if (!LIST_ISEMPTY(&global_listener_queue))
1955 dequeue_all_listeners(&global_listener_queue);
1956
1957 return 1;
1958 }
1959 else {
1960 appctx->ctx.cli.msg = "'set rate-limit sessions' only supports 'global'.\n";
1961 appctx->st0 = STAT_CLI_PRINT;
1962 return 1;
1963 }
1964 }
Willy Tarreaue43d5322013-10-07 20:01:52 +02001965#ifdef USE_OPENSSL
1966 else if (strcmp(args[2], "ssl-sessions") == 0) {
1967 if (strcmp(args[3], "global") == 0) {
1968 int v;
1969
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001970 if (strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreaue43d5322013-10-07 20:01:52 +02001971 appctx->ctx.cli.msg = stats_permission_denied_msg;
1972 appctx->st0 = STAT_CLI_PRINT;
1973 return 1;
1974 }
1975
1976 if (!*args[4]) {
1977 appctx->ctx.cli.msg = "Expects an integer value.\n";
1978 appctx->st0 = STAT_CLI_PRINT;
1979 return 1;
1980 }
1981
1982 v = atoi(args[4]);
1983 if (v < 0) {
1984 appctx->ctx.cli.msg = "Value out of range.\n";
1985 appctx->st0 = STAT_CLI_PRINT;
1986 return 1;
1987 }
1988
1989 global.ssl_lim = v;
1990
1991 /* Dequeues all of the listeners waiting for a resource */
1992 if (!LIST_ISEMPTY(&global_listener_queue))
1993 dequeue_all_listeners(&global_listener_queue);
1994
1995 return 1;
1996 }
1997 else {
1998 appctx->ctx.cli.msg = "'set rate-limit ssl-sessions' only supports 'global'.\n";
1999 appctx->st0 = STAT_CLI_PRINT;
2000 return 1;
2001 }
2002 }
2003#endif
William Lallemandd85f9172012-11-09 17:05:39 +01002004 else if (strcmp(args[2], "http-compression") == 0) {
2005 if (strcmp(args[3], "global") == 0) {
2006 int v;
2007
Willy Tarreaua1c2b2c2015-11-26 18:32:39 +01002008 if (strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN) {
2009 appctx->ctx.cli.msg = stats_permission_denied_msg;
2010 appctx->st0 = STAT_CLI_PRINT;
2011 return 1;
2012 }
2013
Willy Tarreau85d47f92012-11-21 00:29:50 +01002014 if (!*args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002015 appctx->ctx.cli.msg = "Expects a maximum input byte rate in kB/s.\n";
2016 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau85d47f92012-11-21 00:29:50 +01002017 return 1;
2018 }
2019
William Lallemandd85f9172012-11-09 17:05:39 +01002020 v = atoi(args[4]);
2021 global.comp_rate_lim = v * 1024; /* Kilo to bytes. */
2022 }
2023 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002024 appctx->ctx.cli.msg = "'set rate-limit http-compression' only supports 'global'.\n";
2025 appctx->st0 = STAT_CLI_PRINT;
William Lallemandd85f9172012-11-09 17:05:39 +01002026 return 1;
2027 }
2028 }
Willy Tarreauf5b22872011-09-07 16:13:44 +02002029 else {
Willy Tarreaue43d5322013-10-07 20:01:52 +02002030 appctx->ctx.cli.msg = "'set rate-limit' supports 'connections', 'sessions', 'ssl-sessions', and 'http-compression'.\n";
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002031 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02002032 return 1;
2033 }
2034 }
Willy Tarreau654694e2012-06-07 01:03:16 +02002035 else if (strcmp(args[1], "table") == 0) {
2036 stats_sock_table_request(si, args, STAT_CLI_O_SET);
2037 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002038 else if (strcmp(args[1], "map") == 0) {
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002039 char *err;
2040
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002041 /* Set flags. */
2042 appctx->ctx.map.display_flags = PAT_REF_MAP;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002043
2044 /* Expect three parameters: map name, key and new value. */
2045 if (!*args[2] || !*args[3] || !*args[4]) {
Thierry FOURNIERd5723432014-03-11 13:52:44 +01002046 appctx->ctx.cli.msg = "'set map' expects three parameters: map identifier, key and value.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002047 appctx->st0 = STAT_CLI_PRINT;
2048 return 1;
2049 }
2050
2051 /* Lookup the reference in the maps. */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002052 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002053 if (!appctx->ctx.map.ref) {
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01002054 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002055 appctx->st0 = STAT_CLI_PRINT;
2056 return 1;
2057 }
2058
Thierry FOURNIER9356c682014-01-28 15:55:37 +01002059 /* If the entry identifier start with a '#', it is considered as
2060 * pointer id
2061 */
2062 if (args[3][0] == '#' && args[3][1] == '0' && args[3][2] == 'x') {
2063 struct pat_ref_elt *ref;
2064 long long int conv;
2065 char *error;
2066
2067 /* Convert argument to integer value. */
2068 conv = strtoll(&args[3][1], &error, 16);
2069 if (*error != '\0') {
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01002070 appctx->ctx.cli.msg = "Malformed identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER9356c682014-01-28 15:55:37 +01002071 appctx->st0 = STAT_CLI_PRINT;
2072 return 1;
2073 }
2074
2075 /* Convert and check integer to pointer. */
2076 ref = (struct pat_ref_elt *)(long)conv;
2077 if ((long long int)(long)ref != conv) {
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01002078 appctx->ctx.cli.msg = "Malformed identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER9356c682014-01-28 15:55:37 +01002079 appctx->st0 = STAT_CLI_PRINT;
2080 return 1;
2081 }
2082
2083 /* Try to delete the entry. */
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002084 err = NULL;
2085 if (!pat_ref_set_by_id(appctx->ctx.map.ref, ref, args[4], &err)) {
2086 if (err)
2087 memprintf(&err, "%s.\n", err);
2088 appctx->ctx.cli.err = err;
2089 appctx->st0 = STAT_CLI_PRINT_FREE;
Thierry FOURNIER9356c682014-01-28 15:55:37 +01002090 return 1;
2091 }
2092 }
2093 else {
2094 /* Else, use the entry identifier as pattern
2095 * string, and update the value.
2096 */
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002097 err = NULL;
2098 if (!pat_ref_set(appctx->ctx.map.ref, args[3], args[4], &err)) {
2099 if (err)
2100 memprintf(&err, "%s.\n", err);
2101 appctx->ctx.cli.err = err;
2102 appctx->st0 = STAT_CLI_PRINT_FREE;
Thierry FOURNIER9356c682014-01-28 15:55:37 +01002103 return 1;
2104 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002105 }
2106
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002107 /* The set is done, send message. */
Thierry FOURNIER07e78c52014-12-18 15:28:01 +01002108 appctx->st0 = STAT_CLI_PROMPT;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002109 return 1;
2110 }
Emeric Brun4147b2e2014-06-16 18:36:30 +02002111#ifdef USE_OPENSSL
2112 else if (strcmp(args[1], "ssl") == 0) {
2113 if (strcmp(args[2], "ocsp-response") == 0) {
Lukas Tribuse4e30f72014-12-09 16:32:51 +01002114#if (defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP)
Emeric Brun4147b2e2014-06-16 18:36:30 +02002115 char *err = NULL;
2116
Emeric Brunaf4ef742014-06-19 14:10:45 +02002117 /* Expect one parameter: the new response in base64 encoding */
Emeric Brun4147b2e2014-06-16 18:36:30 +02002118 if (!*args[3]) {
2119 appctx->ctx.cli.msg = "'set ssl ocsp-response' expects response in base64 encoding.\n";
2120 appctx->st0 = STAT_CLI_PRINT;
2121 return 1;
2122 }
2123
2124 trash.len = base64dec(args[3], strlen(args[3]), trash.str, trash.size);
2125 if (trash.len < 0) {
2126 appctx->ctx.cli.msg = "'set ssl ocsp-response' received invalid base64 encoded response.\n";
2127 appctx->st0 = STAT_CLI_PRINT;
2128 return 1;
2129 }
2130
2131 if (ssl_sock_update_ocsp_response(&trash, &err)) {
2132 if (err) {
2133 memprintf(&err, "%s.\n", err);
2134 appctx->ctx.cli.err = err;
2135 appctx->st0 = STAT_CLI_PRINT_FREE;
2136 }
2137 return 1;
2138 }
2139 appctx->ctx.cli.msg = "OCSP Response updated!";
2140 appctx->st0 = STAT_CLI_PRINT;
2141 return 1;
2142#else
2143 appctx->ctx.cli.msg = "HAProxy was compiled against a version of OpenSSL that doesn't support OCSP stapling.\n";
2144 appctx->st0 = STAT_CLI_PRINT;
2145 return 1;
2146#endif
2147 }
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02002148 else if (strcmp(args[2], "tls-key") == 0) {
2149#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
2150 /* Expect two parameters: the filename and the new new TLS key in encoding */
2151 if (!*args[3] || !*args[4]) {
2152 appctx->ctx.cli.msg = "'set ssl tls-key' expects a filename and the new TLS key in base64 encoding.\n";
2153 appctx->st0 = STAT_CLI_PRINT;
2154 return 1;
2155 }
2156
2157 appctx->ctx.tlskeys.ref = tlskeys_ref_lookup_ref(args[3]);
2158 if(!appctx->ctx.tlskeys.ref) {
2159 appctx->ctx.cli.msg = "'set ssl tls-key' unable to locate referenced filename\n";
2160 appctx->st0 = STAT_CLI_PRINT;
2161 return 1;
2162 }
2163
2164 trash.len = base64dec(args[4], strlen(args[4]), trash.str, trash.size);
2165 if (trash.len != sizeof(struct tls_sess_key)) {
2166 appctx->ctx.cli.msg = "'set ssl tls-key' received invalid base64 encoded TLS key.\n";
2167 appctx->st0 = STAT_CLI_PRINT;
2168 return 1;
2169 }
2170
Pradeep Jindalcc79b002015-08-20 18:25:17 +05302171 memcpy(appctx->ctx.tlskeys.ref->tlskeys + ((appctx->ctx.tlskeys.ref->tls_ticket_enc_index + 2) % TLS_TICKETS_NO), trash.str, trash.len);
2172 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 +02002173
2174 appctx->ctx.cli.msg = "TLS ticket key updated!";
2175 appctx->st0 = STAT_CLI_PRINT;
2176 return 1;
2177#else
2178 appctx->ctx.cli.msg = "HAProxy was compiled against a version of OpenSSL "
2179 "that doesn't support specifying TLS ticket keys\n";
2180 appctx->st0 = STAT_CLI_PRINT;
2181 return 1;
2182#endif
2183 }
Emeric Brun4147b2e2014-06-16 18:36:30 +02002184 else {
2185 appctx->ctx.cli.msg = "'set ssl' only supports 'ocsp-response'.\n";
2186 appctx->st0 = STAT_CLI_PRINT;
2187 return 1;
2188 }
2189 }
2190#endif
Willy Tarreau7aabd112010-01-26 10:59:06 +01002191 else { /* unknown "set" parameter */
Willy Tarreau4483d432009-10-10 19:30:08 +02002192 return 0;
2193 }
2194 }
Cyril Bontécd19e512010-01-31 22:34:03 +01002195 else if (strcmp(args[0], "enable") == 0) {
Simon Horman671b6f02013-11-25 10:46:39 +09002196 if (strcmp(args[1], "agent") == 0) {
2197 struct server *sv;
2198
2199 sv = expect_server_admin(s, si, args[2]);
2200 if (!sv)
2201 return 1;
2202
Willy Tarreau2e10f5a2013-12-11 20:11:55 +01002203 if (!(sv->agent.state & CHK_ST_CONFIGURED)) {
2204 appctx->ctx.cli.msg = "Agent was not configured on this server, cannot enable.\n";
2205 appctx->st0 = STAT_CLI_PRINT;
2206 return 1;
2207 }
2208
2209 sv->agent.state |= CHK_ST_ENABLED;
Simon Horman671b6f02013-11-25 10:46:39 +09002210 return 1;
2211 }
Willy Tarreau9b5aecd2014-05-23 11:53:10 +02002212 else if (strcmp(args[1], "health") == 0) {
Cyril Bontécd19e512010-01-31 22:34:03 +01002213 struct server *sv;
2214
Willy Tarreaud52c41e2011-09-07 23:41:01 +02002215 sv = expect_server_admin(s, si, args[2]);
2216 if (!sv)
Cyril Bonté613f0df2011-03-03 20:49:04 +01002217 return 1;
Cyril Bonté613f0df2011-03-03 20:49:04 +01002218
Willy Tarreau9b5aecd2014-05-23 11:53:10 +02002219 if (!(sv->check.state & CHK_ST_CONFIGURED)) {
2220 appctx->ctx.cli.msg = "Health checks are not configured on this server, cannot enable.\n";
2221 appctx->st0 = STAT_CLI_PRINT;
2222 return 1;
2223 }
2224
2225 sv->check.state |= CHK_ST_ENABLED;
2226 return 1;
2227 }
2228 else if (strcmp(args[1], "server") == 0) {
2229 struct server *sv;
2230
2231 sv = expect_server_admin(s, si, args[2]);
2232 if (!sv)
2233 return 1;
2234
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02002235 srv_adm_set_ready(sv);
Willy Tarreau532a4502011-09-07 22:37:44 +02002236 return 1;
2237 }
2238 else if (strcmp(args[1], "frontend") == 0) {
2239 struct proxy *px;
2240
2241 px = expect_frontend_admin(s, si, args[2]);
2242 if (!px)
2243 return 1;
2244
2245 if (px->state == PR_STSTOPPED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002246 appctx->ctx.cli.msg = "Frontend was previously shut down, cannot enable.\n";
2247 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002248 return 1;
2249 }
2250
2251 if (px->state != PR_STPAUSED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002252 appctx->ctx.cli.msg = "Frontend is already enabled.\n";
2253 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002254 return 1;
2255 }
2256
2257 if (!resume_proxy(px)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002258 appctx->ctx.cli.msg = "Failed to resume frontend, check logs for precise cause (port conflict?).\n";
2259 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002260 return 1;
2261 }
Cyril Bontécd19e512010-01-31 22:34:03 +01002262 return 1;
2263 }
2264 else { /* unknown "enable" parameter */
Willy Tarreau9b5aecd2014-05-23 11:53:10 +02002265 appctx->ctx.cli.msg = "'enable' only supports 'agent', 'frontend', 'health', and 'server'.\n";
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002266 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002267 return 1;
Cyril Bontécd19e512010-01-31 22:34:03 +01002268 }
2269 }
2270 else if (strcmp(args[0], "disable") == 0) {
Simon Horman671b6f02013-11-25 10:46:39 +09002271 if (strcmp(args[1], "agent") == 0) {
2272 struct server *sv;
2273
2274 sv = expect_server_admin(s, si, args[2]);
2275 if (!sv)
2276 return 1;
2277
Willy Tarreau2e10f5a2013-12-11 20:11:55 +01002278 sv->agent.state &= ~CHK_ST_ENABLED;
Simon Horman671b6f02013-11-25 10:46:39 +09002279 return 1;
2280 }
Willy Tarreau9b5aecd2014-05-23 11:53:10 +02002281 else if (strcmp(args[1], "health") == 0) {
2282 struct server *sv;
2283
2284 sv = expect_server_admin(s, si, args[2]);
2285 if (!sv)
2286 return 1;
2287
2288 sv->check.state &= ~CHK_ST_ENABLED;
2289 return 1;
2290 }
Simon Horman671b6f02013-11-25 10:46:39 +09002291 else if (strcmp(args[1], "server") == 0) {
Cyril Bontécd19e512010-01-31 22:34:03 +01002292 struct server *sv;
2293
Willy Tarreaud52c41e2011-09-07 23:41:01 +02002294 sv = expect_server_admin(s, si, args[2]);
2295 if (!sv)
Cyril Bonté613f0df2011-03-03 20:49:04 +01002296 return 1;
Cyril Bonté613f0df2011-03-03 20:49:04 +01002297
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02002298 srv_adm_set_maint(sv);
Willy Tarreau532a4502011-09-07 22:37:44 +02002299 return 1;
2300 }
2301 else if (strcmp(args[1], "frontend") == 0) {
2302 struct proxy *px;
2303
2304 px = expect_frontend_admin(s, si, args[2]);
2305 if (!px)
2306 return 1;
2307
2308 if (px->state == PR_STSTOPPED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002309 appctx->ctx.cli.msg = "Frontend was previously shut down, cannot disable.\n";
2310 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002311 return 1;
2312 }
2313
2314 if (px->state == PR_STPAUSED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002315 appctx->ctx.cli.msg = "Frontend is already disabled.\n";
2316 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002317 return 1;
2318 }
2319
2320 if (!pause_proxy(px)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002321 appctx->ctx.cli.msg = "Failed to pause frontend, check logs for precise cause.\n";
2322 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002323 return 1;
2324 }
Cyril Bontécd19e512010-01-31 22:34:03 +01002325 return 1;
2326 }
2327 else { /* unknown "disable" parameter */
Willy Tarreau9b5aecd2014-05-23 11:53:10 +02002328 appctx->ctx.cli.msg = "'disable' only supports 'agent', 'frontend', 'health', and 'server'.\n";
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002329 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002330 return 1;
2331 }
2332 }
2333 else if (strcmp(args[0], "shutdown") == 0) {
2334 if (strcmp(args[1], "frontend") == 0) {
2335 struct proxy *px;
2336
2337 px = expect_frontend_admin(s, si, args[2]);
2338 if (!px)
2339 return 1;
2340
2341 if (px->state == PR_STSTOPPED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002342 appctx->ctx.cli.msg = "Frontend was already shut down.\n";
2343 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002344 return 1;
2345 }
2346
2347 Warning("Proxy %s stopped (FE: %lld conns, BE: %lld conns).\n",
2348 px->id, px->fe_counters.cum_conn, px->be_counters.cum_conn);
2349 send_log(px, LOG_WARNING, "Proxy %s stopped (FE: %lld conns, BE: %lld conns).\n",
2350 px->id, px->fe_counters.cum_conn, px->be_counters.cum_conn);
2351 stop_proxy(px);
2352 return 1;
2353 }
Willy Tarreaua295edc2011-09-07 23:21:03 +02002354 else if (strcmp(args[1], "session") == 0) {
Willy Tarreau87b09662015-04-03 00:22:06 +02002355 struct stream *sess, *ptr;
Willy Tarreaua295edc2011-09-07 23:21:03 +02002356
Willy Tarreaud0d8da92015-04-04 02:10:38 +02002357 if (strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002358 appctx->ctx.cli.msg = stats_permission_denied_msg;
2359 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaua295edc2011-09-07 23:21:03 +02002360 return 1;
2361 }
2362
2363 if (!*args[2]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002364 appctx->ctx.cli.msg = "Session pointer expected (use 'show sess').\n";
2365 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaua295edc2011-09-07 23:21:03 +02002366 return 1;
2367 }
2368
2369 ptr = (void *)strtoul(args[2], NULL, 0);
2370
Willy Tarreau87b09662015-04-03 00:22:06 +02002371 /* first, look for the requested stream in the stream table */
2372 list_for_each_entry(sess, &streams, list) {
Willy Tarreaua295edc2011-09-07 23:21:03 +02002373 if (sess == ptr)
2374 break;
2375 }
2376
Willy Tarreau87b09662015-04-03 00:22:06 +02002377 /* do we have the stream ? */
Willy Tarreaua295edc2011-09-07 23:21:03 +02002378 if (sess != ptr) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002379 appctx->ctx.cli.msg = "No such session (use 'show sess').\n";
2380 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaua295edc2011-09-07 23:21:03 +02002381 return 1;
2382 }
2383
Willy Tarreaue7dff022015-04-03 01:14:29 +02002384 stream_shutdown(sess, SF_ERR_KILLED);
Willy Tarreaua295edc2011-09-07 23:21:03 +02002385 return 1;
2386 }
Willy Tarreau52b2d222011-09-07 23:48:48 +02002387 else if (strcmp(args[1], "sessions") == 0) {
2388 if (strcmp(args[2], "server") == 0) {
2389 struct server *sv;
Willy Tarreau87b09662015-04-03 00:22:06 +02002390 struct stream *sess, *sess_bck;
Willy Tarreau52b2d222011-09-07 23:48:48 +02002391
2392 sv = expect_server_admin(s, si, args[3]);
2393 if (!sv)
2394 return 1;
2395
Willy Tarreau87b09662015-04-03 00:22:06 +02002396 /* kill all the stream that are on this server */
Willy Tarreau52b2d222011-09-07 23:48:48 +02002397 list_for_each_entry_safe(sess, sess_bck, &sv->actconns, by_srv)
2398 if (sess->srv_conn == sv)
Willy Tarreaue7dff022015-04-03 01:14:29 +02002399 stream_shutdown(sess, SF_ERR_KILLED);
Willy Tarreau52b2d222011-09-07 23:48:48 +02002400
2401 return 1;
2402 }
2403 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002404 appctx->ctx.cli.msg = "'shutdown sessions' only supports 'server'.\n";
2405 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau52b2d222011-09-07 23:48:48 +02002406 return 1;
2407 }
2408 }
Willy Tarreau532a4502011-09-07 22:37:44 +02002409 else { /* unknown "disable" parameter */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002410 appctx->ctx.cli.msg = "'shutdown' only supports 'frontend', 'session' and 'sessions'.\n";
2411 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002412 return 1;
Cyril Bontécd19e512010-01-31 22:34:03 +01002413 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002414 }
2415 else if (strcmp(args[0], "del") == 0) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002416 if (strcmp(args[1], "map") == 0 || strcmp(args[1], "acl") == 0) {
2417 if (args[1][0] == 'm')
2418 appctx->ctx.map.display_flags = PAT_REF_MAP;
2419 else
2420 appctx->ctx.map.display_flags = PAT_REF_ACL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002421
2422 /* Expect two parameters: map name and key. */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002423 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
2424 if (!*args[2] || !*args[3]) {
2425 appctx->ctx.cli.msg = "This command expects two parameters: map identifier and key.\n";
2426 appctx->st0 = STAT_CLI_PRINT;
2427 return 1;
2428 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002429 }
2430
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002431 else {
2432 if (!*args[2] || !*args[3]) {
2433 appctx->ctx.cli.msg = "This command expects two parameters: ACL identifier and key.\n";
2434 appctx->st0 = STAT_CLI_PRINT;
2435 return 1;
2436 }
2437 }
2438
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002439 /* Lookup the reference in the maps. */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002440 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002441 if (!appctx->ctx.map.ref ||
2442 !(appctx->ctx.map.ref->flags & appctx->ctx.map.display_flags)) {
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01002443 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002444 appctx->st0 = STAT_CLI_PRINT;
2445 return 1;
2446 }
2447
Thierry FOURNIER9356c682014-01-28 15:55:37 +01002448 /* If the entry identifier start with a '#', it is considered as
2449 * pointer id
2450 */
2451 if (args[3][0] == '#' && args[3][1] == '0' && args[3][2] == 'x') {
2452 struct pat_ref_elt *ref;
2453 long long int conv;
2454 char *error;
2455
2456 /* Convert argument to integer value. */
2457 conv = strtoll(&args[3][1], &error, 16);
2458 if (*error != '\0') {
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01002459 appctx->ctx.cli.msg = "Malformed identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER9356c682014-01-28 15:55:37 +01002460 appctx->st0 = STAT_CLI_PRINT;
2461 return 1;
2462 }
2463
2464 /* Convert and check integer to pointer. */
2465 ref = (struct pat_ref_elt *)(long)conv;
2466 if ((long long int)(long)ref != conv) {
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01002467 appctx->ctx.cli.msg = "Malformed identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER9356c682014-01-28 15:55:37 +01002468 appctx->st0 = STAT_CLI_PRINT;
2469 return 1;
2470 }
2471
2472 /* Try to delete the entry. */
2473 if (!pat_ref_delete_by_id(appctx->ctx.map.ref, ref)) {
2474 /* The entry is not found, send message. */
2475 appctx->ctx.cli.msg = "Key not found.\n";
2476 appctx->st0 = STAT_CLI_PRINT;
2477 return 1;
2478 }
2479 }
2480 else {
2481 /* Else, use the entry identifier as pattern
2482 * string and try to delete the entry.
2483 */
2484 if (!pat_ref_delete(appctx->ctx.map.ref, args[3])) {
2485 /* The entry is not found, send message. */
2486 appctx->ctx.cli.msg = "Key not found.\n";
2487 appctx->st0 = STAT_CLI_PRINT;
2488 return 1;
2489 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002490 }
2491
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002492 /* The deletion is done, send message. */
Thierry FOURNIER07e78c52014-12-18 15:28:01 +01002493 appctx->st0 = STAT_CLI_PROMPT;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002494 return 1;
2495 }
2496 else { /* unknown "del" parameter */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002497 appctx->ctx.cli.msg = "'del' only supports 'map' or 'acl'.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002498 appctx->st0 = STAT_CLI_PRINT;
2499 return 1;
2500 }
2501 }
2502 else if (strcmp(args[0], "add") == 0) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002503 if (strcmp(args[1], "map") == 0 ||
2504 strcmp(args[1], "acl") == 0) {
2505 int ret;
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002506 char *err;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002507
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002508 /* Set flags. */
2509 if (args[1][0] == 'm')
2510 appctx->ctx.map.display_flags = PAT_REF_MAP;
2511 else
2512 appctx->ctx.map.display_flags = PAT_REF_ACL;
2513
2514 /* If the keywork is "map", we expect three parameters, if it
2515 * is "acl", we expect only two parameters
2516 */
2517 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
2518 if (!*args[2] || !*args[3] || !*args[4]) {
2519 appctx->ctx.cli.msg = "'add map' expects three parameters: map identifier, key and value.\n";
2520 appctx->st0 = STAT_CLI_PRINT;
2521 return 1;
2522 }
2523 }
2524 else {
2525 if (!*args[2] || !*args[3]) {
2526 appctx->ctx.cli.msg = "'add acl' expects two parameters: ACL identifier and pattern.\n";
2527 appctx->st0 = STAT_CLI_PRINT;
2528 return 1;
2529 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002530 }
2531
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002532 /* Lookup for the reference. */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002533 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002534 if (!appctx->ctx.map.ref) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002535 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01002536 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002537 else
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01002538 appctx->ctx.cli.msg = "Unknown ACL identifier. Please use #<id> or <file>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002539 appctx->st0 = STAT_CLI_PRINT;
2540 return 1;
2541 }
2542
Thierry FOURNIER64c585f2014-01-29 20:02:36 +01002543 /* The command "add acl" is prohibited if the reference
2544 * use samples.
2545 */
2546 if ((appctx->ctx.map.display_flags & PAT_REF_ACL) &&
2547 (appctx->ctx.map.ref->flags & PAT_REF_SMP)) {
2548 appctx->ctx.cli.msg = "This ACL is shared with a map containing samples. "
2549 "You must use the command 'add map' to add values.\n";
2550 appctx->st0 = STAT_CLI_PRINT;
2551 return 1;
2552 }
2553
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002554 /* Add value. */
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002555 err = NULL;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002556 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02002557 ret = pat_ref_add(appctx->ctx.map.ref, args[3], args[4], &err);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002558 else
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02002559 ret = pat_ref_add(appctx->ctx.map.ref, args[3], NULL, &err);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002560 if (!ret) {
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002561 if (err)
2562 memprintf(&err, "%s.\n", err);
2563 appctx->ctx.cli.err = err;
2564 appctx->st0 = STAT_CLI_PRINT_FREE;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002565 return 1;
2566 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002567
2568 /* The add is done, send message. */
Thierry FOURNIER07e78c52014-12-18 15:28:01 +01002569 appctx->st0 = STAT_CLI_PROMPT;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002570 return 1;
2571 }
2572 else { /* unknown "del" parameter */
2573 appctx->ctx.cli.msg = "'add' only supports 'map'.\n";
2574 appctx->st0 = STAT_CLI_PRINT;
2575 return 1;
2576 }
Cyril Bontécd19e512010-01-31 22:34:03 +01002577 }
2578 else { /* not "show" nor "clear" nor "get" nor "set" nor "enable" nor "disable" */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002579 return 0;
2580 }
2581 return 1;
2582}
2583
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002584/* This I/O handler runs as an applet embedded in a stream interface. It is
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002585 * used to processes I/O from/to the stats unix socket. The system relies on a
2586 * state machine handling requests and various responses. We read a request,
2587 * then we process it and send the response, and we possibly display a prompt.
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002588 * Then we can read again. The state is stored in appctx->st0 and is one of the
2589 * STAT_CLI_* constants. appctx->st1 is used to indicate whether prompt is enabled
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002590 * or not.
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002591 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02002592static void cli_io_handler(struct appctx *appctx)
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002593{
Willy Tarreau00a37f02015-04-13 12:05:19 +02002594 struct stream_interface *si = appctx->owner;
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002595 struct channel *req = si_oc(si);
2596 struct channel *res = si_ic(si);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002597 int reql;
2598 int len;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002599
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002600 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
2601 goto out;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002602
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002603 while (1) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002604 if (appctx->st0 == STAT_CLI_INIT) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002605 /* Stats output not initialized yet */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002606 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
2607 appctx->st0 = STAT_CLI_GETREQ;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002608 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002609 else if (appctx->st0 == STAT_CLI_END) {
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002610 /* Let's close for real now. We just close the request
2611 * side, the conditions below will complete if needed.
2612 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02002613 si_shutw(si);
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002614 break;
2615 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002616 else if (appctx->st0 == STAT_CLI_GETREQ) {
Willy Tarreau4e33d862009-10-11 23:35:10 +02002617 /* ensure we have some output room left in the event we
2618 * would want to return some info right after parsing.
2619 */
Willy Tarreau4e4292b2014-11-28 12:18:45 +01002620 if (buffer_almost_full(si_ib(si))) {
Willy Tarreaufe127932015-04-21 19:23:39 +02002621 si_applet_cant_put(si);
Willy Tarreau4e33d862009-10-11 23:35:10 +02002622 break;
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01002623 }
Willy Tarreau4e33d862009-10-11 23:35:10 +02002624
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002625 reql = bo_getline(si_oc(si), trash.str, trash.size);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002626 if (reql <= 0) { /* closed or EOL not found */
2627 if (reql == 0)
2628 break;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002629 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002630 continue;
2631 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002632
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002633 /* seek for a possible semi-colon. If we find one, we
2634 * replace it with an LF and skip only this part.
2635 */
2636 for (len = 0; len < reql; len++)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002637 if (trash.str[len] == ';') {
2638 trash.str[len] = '\n';
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002639 reql = len + 1;
2640 break;
2641 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002642
Willy Tarreau816fc222009-10-04 07:36:58 +02002643 /* now it is time to check that we have a full line,
2644 * remove the trailing \n and possibly \r, then cut the
2645 * line.
2646 */
2647 len = reql - 1;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002648 if (trash.str[len] != '\n') {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002649 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002650 continue;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002651 }
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002652
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002653 if (len && trash.str[len-1] == '\r')
Willy Tarreau816fc222009-10-04 07:36:58 +02002654 len--;
2655
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002656 trash.str[len] = '\0';
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002657
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002658 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002659 if (len) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002660 if (strcmp(trash.str, "quit") == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002661 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002662 continue;
2663 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002664 else if (strcmp(trash.str, "prompt") == 0)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002665 appctx->st1 = !appctx->st1;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002666 else if (strcmp(trash.str, "help") == 0 ||
2667 !stats_sock_parse_request(si, trash.str)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002668 appctx->ctx.cli.msg = stats_sock_usage_msg;
2669 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02002670 }
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002671 /* NB: stats_sock_parse_request() may have put
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002672 * another STAT_CLI_O_* into appctx->st0.
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002673 */
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002674 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002675 else if (!appctx->st1) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002676 /* if prompt is disabled, print help on empty lines,
2677 * so that the user at least knows how to enable
2678 * prompt and find help.
2679 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002680 appctx->ctx.cli.msg = stats_sock_usage_msg;
2681 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002682 }
2683
2684 /* re-adjust req buffer */
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002685 bo_skip(si_oc(si), reql);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002686 req->flags |= CF_READ_DONTWAIT; /* we plan to read small requests */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002687 }
Willy Tarreau68c00c72015-09-25 19:21:19 +02002688 else { /* output functions */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002689 switch (appctx->st0) {
Andrew Hayworthe32d1862015-10-02 15:08:10 +00002690 case STAT_CLI_PROMPT:
2691 break;
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02002692 case STAT_CLI_PRINT:
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002693 if (bi_putstr(si_ic(si), appctx->ctx.cli.msg) != -1)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002694 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreaubc18da12015-03-13 14:00:47 +01002695 else
Willy Tarreaufe127932015-04-21 19:23:39 +02002696 si_applet_cant_put(si);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002697 break;
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002698 case STAT_CLI_PRINT_FREE:
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002699 if (bi_putstr(si_ic(si), appctx->ctx.cli.err) != -1) {
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002700 free(appctx->ctx.cli.err);
2701 appctx->st0 = STAT_CLI_PROMPT;
2702 }
Willy Tarreaubc18da12015-03-13 14:00:47 +01002703 else
Willy Tarreaufe127932015-04-21 19:23:39 +02002704 si_applet_cant_put(si);
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002705 break;
Baptiste Assmann9b6857e2015-09-18 14:49:12 +02002706 case STAT_CLI_O_BACKEND:
2707 if (stats_dump_backend_to_buffer(si))
2708 appctx->st0 = STAT_CLI_PROMPT;
2709 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002710 case STAT_CLI_O_INFO:
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002711 if (stats_dump_info_to_buffer(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002712 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002713 break;
Baptiste Assmann28289462015-07-03 08:01:20 +02002714 case STAT_CLI_O_SERVERS_STATE:
2715 if (stats_dump_servers_state_to_buffer(si))
2716 appctx->st0 = STAT_CLI_PROMPT;
2717 break;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002718 case STAT_CLI_O_STAT:
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002719 if (stats_dump_stat_to_buffer(si, NULL))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002720 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002721 break;
Baptiste Assmann3863f972015-05-17 00:33:24 +02002722 case STAT_CLI_O_RESOLVERS:
2723 if (stats_dump_resolvers_to_buffer(si))
2724 appctx->st0 = STAT_CLI_PROMPT;
2725 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002726 case STAT_CLI_O_SESS:
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01002727 if (stats_dump_sess_to_buffer(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002728 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002729 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002730 case STAT_CLI_O_ERR: /* errors dump */
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01002731 if (stats_dump_errors_to_buffer(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002732 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002733 break;
Willy Tarreau69f58c82010-07-12 17:55:33 +02002734 case STAT_CLI_O_TAB:
Simon Hormanc88b8872011-06-15 15:18:49 +09002735 case STAT_CLI_O_CLR:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002736 if (stats_table_request(si, appctx->st0))
2737 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau69f58c82010-07-12 17:55:33 +02002738 break;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002739 case STAT_CLI_O_PATS:
2740 if (stats_pats_list(si))
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002741 appctx->st0 = STAT_CLI_PROMPT;
2742 break;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002743 case STAT_CLI_O_PAT:
2744 if (stats_pat_list(si))
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002745 appctx->st0 = STAT_CLI_PROMPT;
2746 break;
2747 case STAT_CLI_O_MLOOK:
2748 if (stats_map_lookup(si))
2749 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau4efb3532014-01-29 12:13:39 +01002750 break;
Willy Tarreau12833bb2014-01-28 16:49:56 +01002751 case STAT_CLI_O_POOLS:
2752 if (stats_dump_pools_to_buffer(si))
2753 appctx->st0 = STAT_CLI_PROMPT;
2754 break;
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02002755#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
2756 case STAT_CLI_O_TLSK:
2757 if (stats_tlskeys_list(si))
2758 appctx->st0 = STAT_CLI_PROMPT;
2759 break;
2760#endif
Willy Tarreauae795722016-02-16 11:27:28 +01002761 case STAT_CLI_O_ENV: /* environment dump */
2762 if (stats_dump_env_to_buffer(si))
2763 appctx->st0 = STAT_CLI_PROMPT;
2764 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002765 default: /* abnormal state */
Willy Tarreau5cfa3bc2015-09-25 20:08:51 +02002766 si->flags |= SI_FL_ERR;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002767 break;
2768 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002769
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002770 /* The post-command prompt is either LF alone or LF + '> ' in interactive mode */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002771 if (appctx->st0 == STAT_CLI_PROMPT) {
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002772 if (bi_putstr(si_ic(si), appctx->st1 ? "\n> " : "\n") != -1)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002773 appctx->st0 = STAT_CLI_GETREQ;
Willy Tarreaubc18da12015-03-13 14:00:47 +01002774 else
Willy Tarreaufe127932015-04-21 19:23:39 +02002775 si_applet_cant_put(si);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002776 }
2777
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002778 /* If the output functions are still there, it means they require more room. */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002779 if (appctx->st0 >= STAT_CLI_OUTPUT)
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002780 break;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002781
2782 /* Now we close the output if one of the writers did so,
2783 * or if we're not in interactive mode and the request
2784 * buffer is empty. This still allows pipelined requests
2785 * to be sent in non-interactive mode.
2786 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002787 if ((res->flags & (CF_SHUTW|CF_SHUTW_NOW)) || (!appctx->st1 && !req->buf->o)) {
2788 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002789 continue;
2790 }
2791
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002792 /* switch state back to GETREQ to read next requests */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002793 appctx->st0 = STAT_CLI_GETREQ;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002794 }
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002795 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002796
Willy Tarreauc9e930a2015-09-25 20:06:08 +02002797 if ((res->flags & CF_SHUTR) && (si->state == SI_ST_EST)) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002798 DPRINTF(stderr, "%s@%d: si to buf closed. req=%08x, res=%08x, st=%d\n",
2799 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
Aman Guptaceafb4a2012-04-02 18:57:54 -07002800 /* Other side has closed, let's abort if we have no more processing to do
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002801 * and nothing more to consume. This is comparable to a broken pipe, so
2802 * we forward the close to the request side so that it flows upstream to
2803 * the client.
2804 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02002805 si_shutw(si);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002806 }
2807
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002808 if ((req->flags & CF_SHUTW) && (si->state == SI_ST_EST) && (appctx->st0 < STAT_CLI_OUTPUT)) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002809 DPRINTF(stderr, "%s@%d: buf to si closed. req=%08x, res=%08x, st=%d\n",
2810 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
2811 /* We have no more processing to do, and nothing more to send, and
2812 * the client side has closed. So we'll forward this state downstream
2813 * on the response buffer.
2814 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02002815 si_shutr(si);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002816 res->flags |= CF_READ_NULL;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002817 }
2818
Willy Tarreau828824a2015-04-19 17:20:03 +02002819 out:
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01002820 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 +02002821 __FUNCTION__, __LINE__,
Willy Tarreau9b28e032012-10-12 23:49:43 +02002822 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 +02002823}
2824
Willy Tarreau638d40a2016-02-24 23:11:01 +01002825/* Emits a stats field without any surrounding element and properly encoded to
2826 * resist CSV output. Returns non-zero on success, 0 if the buffer is full.
2827 */
Willy Tarreaub47785f2016-02-24 23:28:31 +01002828int stats_emit_raw_data_field(struct chunk *out, const struct field *f)
Willy Tarreau638d40a2016-02-24 23:11:01 +01002829{
2830 switch (field_format(f, 0)) {
2831 case FF_EMPTY: return 1;
2832 case FF_S32: return chunk_appendf(out, "%d", f->u.s32);
2833 case FF_U32: return chunk_appendf(out, "%u", f->u.u32);
2834 case FF_S64: return chunk_appendf(out, "%lld", (long long)f->u.s64);
2835 case FF_U64: return chunk_appendf(out, "%llu", (unsigned long long)f->u.u64);
2836 case FF_STR: return csv_enc_append(field_str(f, 0), 1, out) != NULL;
2837 default: return chunk_appendf(out, "[INCORRECT_FIELD_TYPE_%08x]", f->type);
2838 }
2839}
2840
Willy Tarreaub47785f2016-02-24 23:28:31 +01002841/* Emits a stats field prefixed with its type. No CSV encoding is prepared, the
2842 * output is supposed to be used on its own line. Returns non-zero on success, 0
2843 * if the buffer is full.
2844 */
2845int stats_emit_typed_data_field(struct chunk *out, const struct field *f)
2846{
2847 switch (field_format(f, 0)) {
2848 case FF_EMPTY: return 1;
2849 case FF_S32: return chunk_appendf(out, "s32:%d", f->u.s32);
2850 case FF_U32: return chunk_appendf(out, "u32:%u", f->u.u32);
2851 case FF_S64: return chunk_appendf(out, "s64:%lld", (long long)f->u.s64);
2852 case FF_U64: return chunk_appendf(out, "u64:%llu", (unsigned long long)f->u.u64);
2853 case FF_STR: return chunk_appendf(out, "str:%s", field_str(f, 0));
2854 default: return chunk_appendf(out, "%08x:?", f->type);
2855 }
2856}
2857
2858/* Emits an encoding of the field type on 3 characters followed by a delimiter.
2859 * Returns non-zero on success, 0 if the buffer is full.
2860 */
2861int stats_emit_field_tags(struct chunk *out, const struct field *f, char delim)
2862{
2863 char origin, nature, scope;
2864
2865 switch (field_origin(f, 0)) {
2866 case FO_METRIC: origin = 'M'; break;
2867 case FO_STATUS: origin = 'S'; break;
2868 case FO_KEY: origin = 'K'; break;
2869 case FO_CONFIG: origin = 'C'; break;
2870 case FO_PRODUCT: origin = 'P'; break;
2871 default: origin = '?'; break;
2872 }
2873
2874 switch (field_nature(f, 0)) {
2875 case FN_GAUGE: nature = 'G'; break;
2876 case FN_LIMIT: nature = 'L'; break;
2877 case FN_MIN: nature = 'm'; break;
2878 case FN_MAX: nature = 'M'; break;
2879 case FN_RATE: nature = 'R'; break;
2880 case FN_COUNTER: nature = 'C'; break;
2881 case FN_DURATION: nature = 'D'; break;
2882 case FN_AGE: nature = 'A'; break;
2883 case FN_TIME: nature = 'T'; break;
2884 case FN_NAME: nature = 'N'; break;
2885 case FN_OUTPUT: nature = 'O'; break;
2886 case FN_AVG: nature = 'a'; break;
2887 default: nature = '?'; break;
2888 }
2889
2890 switch (field_scope(f, 0)) {
2891 case FS_PROCESS: scope = 'P'; break;
2892 case FS_SERVICE: scope = 'S'; break;
2893 case FS_SYSTEM: scope = 's'; break;
2894 case FS_CLUSTER: scope = 'C'; break;
2895 default: scope = '?'; break;
2896 }
2897
2898 return chunk_appendf(out, "%c%c%c%c", origin, nature, scope, delim);
2899}
2900
Willy Tarreaubf95cba2016-01-11 18:27:29 +01002901/* Dump all fields from <info> into <out> using the "show info" format (name: value) */
2902static int stats_dump_info_fields(struct chunk *out, const struct field *info)
2903{
2904 int field;
2905
2906 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
2907 if (!field_format(info, field))
2908 continue;
2909
2910 if (!chunk_appendf(out, "%s: ", info_field_names[field]))
2911 return 0;
2912 if (!stats_emit_raw_data_field(out, &info[field]))
2913 return 0;
2914 if (!chunk_strcat(out, "\n"))
2915 return 0;
2916 }
2917 return 1;
2918}
2919
Willy Tarreaucb809122016-01-11 20:08:42 +01002920/* Dump all fields from <info> into <out> using the "show info typed" format */
2921static int stats_dump_typed_info_fields(struct chunk *out, const struct field *info)
2922{
2923 int field;
2924
2925 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
2926 if (!field_format(info, field))
2927 continue;
2928
2929 if (!chunk_appendf(out, "%d.%s.%u:", field, info_field_names[field], info[INF_PROCESS_NUM].u.u32))
2930 return 0;
2931 if (!stats_emit_field_tags(out, &info[field], ':'))
2932 return 0;
2933 if (!stats_emit_typed_data_field(out, &info[field]))
2934 return 0;
2935 if (!chunk_strcat(out, "\n"))
2936 return 0;
2937 }
2938 return 1;
2939}
2940
Thierry Fourniercb2c7672016-03-25 08:19:23 +01002941/* Fill <info> with HAProxy global info. <info> is preallocated
2942 * array of length <len>. The length of the aray must be
2943 * INF_TOTAL_FIELDS. If this length is less then this value, the
2944 * function returns 0, otherwise, it returns 1.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002945 */
Thierry Fourniercb2c7672016-03-25 08:19:23 +01002946int stats_fill_info(struct field *info, int len)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002947{
2948 unsigned int up = (now.tv_sec - start_date.tv_sec);
Willy Tarreau4529c072016-01-11 18:17:07 +01002949 struct chunk *out = get_trash_chunk();
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002950
Willy Tarreau0c9c2722014-05-28 12:28:58 +02002951#ifdef USE_OPENSSL
2952 int ssl_sess_rate = read_freq_ctr(&global.ssl_per_sec);
2953 int ssl_key_rate = read_freq_ctr(&global.ssl_fe_keys_per_sec);
2954 int ssl_reuse = 0;
2955
2956 if (ssl_key_rate < ssl_sess_rate) {
2957 /* count the ssl reuse ratio and avoid overflows in both directions */
2958 ssl_reuse = 100 - (100 * ssl_key_rate + (ssl_sess_rate - 1) / 2) / ssl_sess_rate;
2959 }
2960#endif
2961
Thierry Fourniercb2c7672016-03-25 08:19:23 +01002962 if (len < INF_TOTAL_FIELDS)
2963 return 0;
2964
Willy Tarreau4529c072016-01-11 18:17:07 +01002965 chunk_reset(out);
Thierry Fourniercb2c7672016-03-25 08:19:23 +01002966 memset(info, 0, sizeof(*info) * len);
Willy Tarreau4529c072016-01-11 18:17:07 +01002967
2968 info[INF_NAME] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, PRODUCT_NAME);
2969 info[INF_VERSION] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, HAPROXY_VERSION);
2970 info[INF_RELEASE_DATE] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, HAPROXY_DATE);
2971
2972 info[INF_NBPROC] = mkf_u32(FO_CONFIG|FS_SERVICE, global.nbproc);
2973 info[INF_PROCESS_NUM] = mkf_u32(FO_KEY, relative_pid);
2974 info[INF_PID] = mkf_u32(FO_STATUS, pid);
2975
2976 info[INF_UPTIME] = mkf_str(FN_DURATION, chunk_newstr(out));
2977 chunk_appendf(out, "%ud %uh%02um%02us", up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60));
2978
2979 info[INF_UPTIME_SEC] = mkf_u32(FN_DURATION, up);
2980 info[INF_MEMMAX_MB] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_memmax);
2981 info[INF_POOL_ALLOC_MB] = mkf_u32(0, (unsigned)(pool_total_allocated() / 1048576L));
2982 info[INF_POOL_USED_MB] = mkf_u32(0, (unsigned)(pool_total_used() / 1048576L));
2983 info[INF_POOL_FAILED] = mkf_u32(FN_COUNTER, pool_total_failures());
2984 info[INF_ULIMIT_N] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_nofile);
2985 info[INF_MAXSOCK] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxsock);
2986 info[INF_MAXCONN] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxconn);
2987 info[INF_HARD_MAXCONN] = mkf_u32(FO_CONFIG|FN_LIMIT, global.hardmaxconn);
2988 info[INF_CURR_CONN] = mkf_u32(0, actconn);
2989 info[INF_CUM_CONN] = mkf_u32(FN_COUNTER, totalconn);
2990 info[INF_CUM_REQ] = mkf_u32(FN_COUNTER, global.req_count);
2991#ifdef USE_OPENSSL
2992 info[INF_MAX_SSL_CONNS] = mkf_u32(FN_MAX, global.maxsslconn);
2993 info[INF_CURR_SSL_CONNS] = mkf_u32(0, sslconns);
2994 info[INF_CUM_SSL_CONNS] = mkf_u32(FN_COUNTER, totalsslconns);
2995#endif
2996 info[INF_MAXPIPES] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxpipes);
2997 info[INF_PIPES_USED] = mkf_u32(0, pipes_used);
2998 info[INF_PIPES_FREE] = mkf_u32(0, pipes_free);
2999 info[INF_CONN_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.conn_per_sec));
3000 info[INF_CONN_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.cps_lim);
3001 info[INF_MAX_CONN_RATE] = mkf_u32(FN_MAX, global.cps_max);
3002 info[INF_SESS_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.sess_per_sec));
3003 info[INF_SESS_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.sps_lim);
3004 info[INF_MAX_SESS_RATE] = mkf_u32(FN_RATE, global.sps_max);
3005
3006#ifdef USE_OPENSSL
3007 info[INF_SSL_RATE] = mkf_u32(FN_RATE, ssl_sess_rate);
3008 info[INF_SSL_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.ssl_lim);
3009 info[INF_MAX_SSL_RATE] = mkf_u32(FN_MAX, global.ssl_max);
3010 info[INF_SSL_FRONTEND_KEY_RATE] = mkf_u32(0, ssl_key_rate);
3011 info[INF_SSL_FRONTEND_MAX_KEY_RATE] = mkf_u32(FN_MAX, global.ssl_fe_keys_max);
3012 info[INF_SSL_FRONTEND_SESSION_REUSE_PCT] = mkf_u32(0, ssl_reuse);
3013 info[INF_SSL_BACKEND_KEY_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.ssl_be_keys_per_sec));
3014 info[INF_SSL_BACKEND_MAX_KEY_RATE] = mkf_u32(FN_MAX, global.ssl_be_keys_max);
3015 info[INF_SSL_CACHE_LOOKUPS] = mkf_u32(FN_COUNTER, global.shctx_lookups);
3016 info[INF_SSL_CACHE_MISSES] = mkf_u32(FN_COUNTER, global.shctx_misses);
3017#endif
3018 info[INF_COMPRESS_BPS_IN] = mkf_u32(FN_RATE, read_freq_ctr(&global.comp_bps_in));
3019 info[INF_COMPRESS_BPS_OUT] = mkf_u32(FN_RATE, read_freq_ctr(&global.comp_bps_out));
3020 info[INF_COMPRESS_BPS_RATE_LIM] = mkf_u32(FO_CONFIG|FN_LIMIT, global.comp_rate_lim);
3021#ifdef USE_ZLIB
3022 info[INF_ZLIB_MEM_USAGE] = mkf_u32(0, zlib_used_memory);
3023 info[INF_MAX_ZLIB_MEM_USAGE] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxzlibmem);
3024#endif
3025 info[INF_TASKS] = mkf_u32(0, nb_tasks_cur);
3026 info[INF_RUN_QUEUE] = mkf_u32(0, run_queue_cur);
3027 info[INF_IDLE_PCT] = mkf_u32(FN_AVG, idle_pct);
3028 info[INF_NODE] = mkf_str(FO_CONFIG|FN_OUTPUT|FS_SERVICE, global.node);
3029 if (global.desc)
3030 info[INF_DESCRIPTION] = mkf_str(FO_CONFIG|FN_OUTPUT|FS_SERVICE, global.desc);
3031
Thierry Fourniercb2c7672016-03-25 08:19:23 +01003032 return 1;
3033}
3034
3035/* This function dumps information onto the stream interface's read buffer.
3036 * It returns 0 as long as it does not complete, non-zero upon completion.
3037 * No state is used.
3038 */
3039static int stats_dump_info_to_buffer(struct stream_interface *si)
3040{
3041 struct appctx *appctx = __objt_appctx(si->end);
3042
3043 if (!stats_fill_info(info, INF_TOTAL_FIELDS))
3044 return 0;
3045
Willy Tarreau1b4ba1e2016-01-11 18:29:04 +01003046 chunk_reset(&trash);
Willy Tarreaucb809122016-01-11 20:08:42 +01003047
3048 if (appctx->ctx.stats.flags & STAT_FMT_TYPED)
3049 stats_dump_typed_info_fields(&trash, info);
3050 else
3051 stats_dump_info_fields(&trash, info);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003052
Willy Tarreaubc18da12015-03-13 14:00:47 +01003053 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02003054 si_applet_cant_put(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003055 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01003056 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003057
3058 return 1;
3059}
3060
Baptiste Assmann28289462015-07-03 08:01:20 +02003061/* dumps server state information into <buf> for all the servers found in <backend>
3062 * These information are all the parameters which may change during HAProxy runtime.
3063 * By default, we only export to the last known server state file format.
3064 * These information can be used at next startup to recover same level of server state.
3065 */
Cyril Bonté76a99782016-05-06 12:18:48 +02003066static int dump_servers_state(struct stream_interface *si, struct chunk *buf)
Baptiste Assmann28289462015-07-03 08:01:20 +02003067{
Cyril Bonté76a99782016-05-06 12:18:48 +02003068 struct appctx *appctx = __objt_appctx(si->end);
Baptiste Assmann28289462015-07-03 08:01:20 +02003069 struct server *srv;
3070 char srv_addr[INET6_ADDRSTRLEN + 1];
3071 time_t srv_time_since_last_change;
3072 int bk_f_forced_id, srv_f_forced_id;
3073
Baptiste Assmann28289462015-07-03 08:01:20 +02003074
Cyril Bonté76a99782016-05-06 12:18:48 +02003075 /* we don't want to report any state if the backend is not enabled on this process */
3076 if (appctx->ctx.server_state.px->bind_proc && !(appctx->ctx.server_state.px->bind_proc & (1UL << (relative_pid - 1))))
3077 return 1;
Baptiste Assmann28289462015-07-03 08:01:20 +02003078
Cyril Bonté76a99782016-05-06 12:18:48 +02003079 for (; appctx->ctx.server_state.sv != NULL; appctx->ctx.server_state.sv = srv->next) {
3080 srv = appctx->ctx.server_state.sv;
Baptiste Assmann28289462015-07-03 08:01:20 +02003081 srv_addr[0] = '\0';
3082 srv_time_since_last_change = 0;
3083 bk_f_forced_id = 0;
3084 srv_f_forced_id = 0;
3085
3086 switch (srv->addr.ss_family) {
3087 case AF_INET:
3088 inet_ntop(srv->addr.ss_family, &((struct sockaddr_in *)&srv->addr)->sin_addr,
3089 srv_addr, INET_ADDRSTRLEN + 1);
3090 break;
3091 case AF_INET6:
3092 inet_ntop(srv->addr.ss_family, &((struct sockaddr_in6 *)&srv->addr)->sin6_addr,
3093 srv_addr, INET6_ADDRSTRLEN + 1);
3094 break;
3095 }
3096 srv_time_since_last_change = now.tv_sec - srv->last_change;
Cyril Bonté76a99782016-05-06 12:18:48 +02003097 bk_f_forced_id = appctx->ctx.server_state.px->options & PR_O_FORCED_ID ? 1 : 0;
Baptiste Assmann28289462015-07-03 08:01:20 +02003098 srv_f_forced_id = srv->flags & SRV_F_FORCED_ID ? 1 : 0;
3099
3100 chunk_appendf(buf,
3101 "%d %s "
3102 "%d %s %s "
3103 "%d %d %d %d %ld "
3104 "%d %d %d %d %d "
3105 "%d %d"
3106 "\n",
Cyril Bonté76a99782016-05-06 12:18:48 +02003107 appctx->ctx.server_state.px->uuid, appctx->ctx.server_state.px->id,
Baptiste Assmann28289462015-07-03 08:01:20 +02003108 srv->puid, srv->id, srv_addr,
David Carlier081b3362015-11-18 06:10:22 +00003109 srv->state, srv->admin, srv->uweight, srv->iweight, (long int)srv_time_since_last_change,
Baptiste Assmann28289462015-07-03 08:01:20 +02003110 srv->check.status, srv->check.result, srv->check.health, srv->check.state, srv->agent.state,
3111 bk_f_forced_id, srv_f_forced_id);
Cyril Bonté76a99782016-05-06 12:18:48 +02003112 if (bi_putchk(si_ic(si), &trash) == -1) {
3113 si_applet_cant_put(si);
3114 return 0;
3115 }
Baptiste Assmann28289462015-07-03 08:01:20 +02003116 }
Cyril Bonté76a99782016-05-06 12:18:48 +02003117 return 1;
Baptiste Assmann28289462015-07-03 08:01:20 +02003118}
3119
Baptiste Assmann9b6857e2015-09-18 14:49:12 +02003120/* Parses backend list and simply report backend names */
3121static int stats_dump_backend_to_buffer(struct stream_interface *si)
3122{
Cyril Bonté6ca9e012016-05-06 12:18:49 +02003123 struct appctx *appctx = __objt_appctx(si->end);
Baptiste Assmann9b6857e2015-09-18 14:49:12 +02003124 extern struct proxy *proxy;
3125 struct proxy *curproxy;
3126
3127 chunk_reset(&trash);
Baptiste Assmann9b6857e2015-09-18 14:49:12 +02003128
Cyril Bonté6ca9e012016-05-06 12:18:49 +02003129 if (!appctx->ctx.be.px) {
3130 chunk_printf(&trash, "# name\n");
3131 if (bi_putchk(si_ic(si), &trash) == -1) {
3132 si_applet_cant_put(si);
3133 return 0;
3134 }
3135 appctx->ctx.be.px = proxy;
3136 }
3137
3138 for (; appctx->ctx.be.px != NULL; appctx->ctx.be.px = curproxy->next) {
3139 curproxy = appctx->ctx.be.px;
3140
Baptiste Assmann9b6857e2015-09-18 14:49:12 +02003141 /* looking for backends only */
3142 if (!(curproxy->cap & PR_CAP_BE))
3143 continue;
3144
3145 /* we don't want to list a backend which is bound to this process */
3146 if (curproxy->bind_proc && !(curproxy->bind_proc & (1UL << (relative_pid - 1))))
3147 continue;
3148
3149 chunk_appendf(&trash, "%s\n", curproxy->id);
Cyril Bonté6ca9e012016-05-06 12:18:49 +02003150 if (bi_putchk(si_ic(si), &trash) == -1) {
3151 si_applet_cant_put(si);
3152 return 0;
3153 }
Baptiste Assmann9b6857e2015-09-18 14:49:12 +02003154 }
3155
3156 return 1;
3157}
3158
Baptiste Assmann28289462015-07-03 08:01:20 +02003159/* Parses backend list or simply use backend name provided by the user to return
3160 * states of servers to stdout.
3161 */
3162static int stats_dump_servers_state_to_buffer(struct stream_interface *si)
3163{
3164 struct appctx *appctx = __objt_appctx(si->end);
3165 extern struct proxy *proxy;
3166 struct proxy *curproxy;
3167
3168 chunk_reset(&trash);
3169
Cyril Bonté76a99782016-05-06 12:18:48 +02003170 if (!appctx->ctx.server_state.px) {
3171 chunk_printf(&trash, "%d\n# %s\n", SRV_STATE_FILE_VERSION, SRV_STATE_FILE_FIELD_NAMES);
3172 if (bi_putchk(si_ic(si), &trash) == -1) {
3173 si_applet_cant_put(si);
3174 return 0;
Baptiste Assmann28289462015-07-03 08:01:20 +02003175 }
Cyril Bonté76a99782016-05-06 12:18:48 +02003176 appctx->ctx.server_state.px = proxy;
Baptiste Assmann28289462015-07-03 08:01:20 +02003177 }
3178
Cyril Bonté76a99782016-05-06 12:18:48 +02003179 for (; appctx->ctx.server_state.px != NULL; appctx->ctx.server_state.px = curproxy->next) {
3180 curproxy = appctx->ctx.server_state.px;
3181 if (!appctx->ctx.server_state.sv)
3182 appctx->ctx.server_state.sv = appctx->ctx.server_state.px->srv;
3183 /* servers are only in backends */
3184 if (curproxy->cap & PR_CAP_BE) {
3185 if (!dump_servers_state(si, &trash))
3186 return 0;
3187
3188 if (bi_putchk(si_ic(si), &trash) == -1) {
3189 si_applet_cant_put(si);
3190 return 0;
3191 }
3192 }
3193 /* only the selected proxy is dumped */
3194 if (appctx->ctx.server_state.iid)
3195 break;
Baptiste Assmann28289462015-07-03 08:01:20 +02003196 }
3197
3198 return 1;
3199}
3200
Willy Tarreau12833bb2014-01-28 16:49:56 +01003201/* This function dumps memory usage information onto the stream interface's
3202 * read buffer. It returns 0 as long as it does not complete, non-zero upon
3203 * completion. No state is used.
3204 */
3205static int stats_dump_pools_to_buffer(struct stream_interface *si)
3206{
3207 dump_pools_to_trash();
Willy Tarreaubc18da12015-03-13 14:00:47 +01003208 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02003209 si_applet_cant_put(si);
Willy Tarreau12833bb2014-01-28 16:49:56 +01003210 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01003211 }
Willy Tarreau82a86022016-01-04 19:04:18 +01003212 return 1;
3213}
3214
3215/* Dump all fields from <stats> into <out> using CSV format */
3216static int stats_dump_fields_csv(struct chunk *out, const struct field *stats)
3217{
3218 int field;
3219
3220 for (field = 0; field < ST_F_TOTAL_FIELDS; field++) {
3221 if (!stats_emit_raw_data_field(out, &stats[field]))
3222 return 0;
3223 if (!chunk_strcat(out, ","))
3224 return 0;
3225 }
Conrad Hoffmann692c9382016-04-01 20:40:58 +02003226 chunk_strcat(out, "\n");
Willy Tarreau12833bb2014-01-28 16:49:56 +01003227 return 1;
3228}
3229
Willy Tarreau1e62df92016-01-11 18:57:53 +01003230/* Dump all fields from <stats> into <out> using a typed "field:desc:type:value" format */
3231static int stats_dump_fields_typed(struct chunk *out, const struct field *stats)
3232{
3233 int field;
3234
3235 for (field = 0; field < ST_F_TOTAL_FIELDS; field++) {
3236 if (!stats[field].type)
3237 continue;
3238
3239 chunk_appendf(out, "%c.%u.%u.%d.%s.%u:",
3240 stats[ST_F_TYPE].u.u32 == STATS_TYPE_FE ? 'F' :
3241 stats[ST_F_TYPE].u.u32 == STATS_TYPE_BE ? 'B' :
3242 stats[ST_F_TYPE].u.u32 == STATS_TYPE_SO ? 'L' :
3243 stats[ST_F_TYPE].u.u32 == STATS_TYPE_SV ? 'S' :
3244 '?',
3245 stats[ST_F_IID].u.u32, stats[ST_F_SID].u.u32,
3246 field, stat_field_names[field], stats[ST_F_PID].u.u32);
3247
3248 if (!stats_emit_field_tags(out, &stats[field], ':'))
3249 return 0;
3250 if (!stats_emit_typed_data_field(out, &stats[field]))
3251 return 0;
3252 if (!chunk_strcat(out, "\n"))
3253 return 0;
3254 }
3255 return 1;
3256}
3257
Willy Tarreau60600742016-01-11 15:32:30 +01003258/* Dump all fields from <stats> into <out> using the HTML format. A column is
Willy Tarreau508a63f2016-01-11 15:28:40 +01003259 * reserved for the checkbox is ST_SHOWADMIN is set in <flags>. Some extra info
3260 * are provided if ST_SHLGNDS is present in <flags>.
Cyril Bonté70be45d2010-10-12 00:14:35 +02003261 */
Willy Tarreau60600742016-01-11 15:32:30 +01003262static int stats_dump_fields_html(struct chunk *out, const struct field *stats, unsigned int flags)
Cyril Bonté70be45d2010-10-12 00:14:35 +02003263{
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003264 struct chunk src;
3265
Willy Tarreaub5f66b82016-01-08 17:15:39 +01003266 if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_FE) {
Willy Tarreau60600742016-01-11 15:32:30 +01003267 chunk_appendf(out,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003268 /* name, queue */
3269 "<tr class=\"frontend\">");
Cyril Bonté70be45d2010-10-12 00:14:35 +02003270
Willy Tarreau508a63f2016-01-11 15:28:40 +01003271 if (flags & ST_SHOWADMIN) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003272 /* Column sub-heading for Enable or Disable server */
Willy Tarreau60600742016-01-11 15:32:30 +01003273 chunk_appendf(out, "<td></td>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003274 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02003275
Willy Tarreau60600742016-01-11 15:32:30 +01003276 chunk_appendf(out,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003277 "<td class=ac>"
3278 "<a name=\"%s/Frontend\"></a>"
3279 "<a class=lfsb href=\"#%s/Frontend\">Frontend</a></td>"
3280 "<td colspan=3></td>"
3281 "",
Willy Tarreaud72c9172016-01-06 15:26:40 +01003282 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_PXNAME));
Cyril Bonté70be45d2010-10-12 00:14:35 +02003283
Willy Tarreau60600742016-01-11 15:32:30 +01003284 chunk_appendf(out,
Willy Tarreau466c9b52012-12-23 02:25:03 +01003285 /* sessions rate : current */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003286 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01003287 "<tr><th>Current connection rate:</th><td>%s/s</td></tr>"
3288 "<tr><th>Current session rate:</th><td>%s/s</td></tr>"
3289 "",
Willy Tarreaud72c9172016-01-06 15:26:40 +01003290 U2H(stats[ST_F_RATE].u.u32),
Willy Tarreauc73810f2016-01-11 13:52:04 +01003291 U2H(stats[ST_F_CONN_RATE].u.u32),
Willy Tarreaud72c9172016-01-06 15:26:40 +01003292 U2H(stats[ST_F_RATE].u.u32));
Willy Tarreau466c9b52012-12-23 02:25:03 +01003293
Willy Tarreauf8211df2016-01-11 14:09:38 +01003294 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
Willy Tarreau60600742016-01-11 15:32:30 +01003295 chunk_appendf(out,
Willy Tarreau466c9b52012-12-23 02:25:03 +01003296 "<tr><th>Current request rate:</th><td>%s/s</td></tr>",
Willy Tarreaud72c9172016-01-06 15:26:40 +01003297 U2H(stats[ST_F_REQ_RATE].u.u32));
Willy Tarreau466c9b52012-12-23 02:25:03 +01003298
Willy Tarreau60600742016-01-11 15:32:30 +01003299 chunk_appendf(out,
Willy Tarreau466c9b52012-12-23 02:25:03 +01003300 "</table></div></u></td>"
3301 /* sessions rate : max */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003302 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01003303 "<tr><th>Max connection rate:</th><td>%s/s</td></tr>"
3304 "<tr><th>Max session rate:</th><td>%s/s</td></tr>"
3305 "",
Willy Tarreaud72c9172016-01-06 15:26:40 +01003306 U2H(stats[ST_F_RATE_MAX].u.u32),
Willy Tarreauc73810f2016-01-11 13:52:04 +01003307 U2H(stats[ST_F_CONN_RATE_MAX].u.u32),
Willy Tarreaud72c9172016-01-06 15:26:40 +01003308 U2H(stats[ST_F_RATE_MAX].u.u32));
Willy Tarreau466c9b52012-12-23 02:25:03 +01003309
Willy Tarreauf8211df2016-01-11 14:09:38 +01003310 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
Willy Tarreau60600742016-01-11 15:32:30 +01003311 chunk_appendf(out,
Willy Tarreau466c9b52012-12-23 02:25:03 +01003312 "<tr><th>Max request rate:</th><td>%s/s</td></tr>",
Willy Tarreaud72c9172016-01-06 15:26:40 +01003313 U2H(stats[ST_F_REQ_RATE_MAX].u.u32));
Willy Tarreau466c9b52012-12-23 02:25:03 +01003314
Willy Tarreau60600742016-01-11 15:32:30 +01003315 chunk_appendf(out,
Willy Tarreau466c9b52012-12-23 02:25:03 +01003316 "</table></div></u></td>"
3317 /* sessions rate : limit */
3318 "<td>%s</td>",
Willy Tarreaud72c9172016-01-06 15:26:40 +01003319 LIM2A(stats[ST_F_RATE_LIM].u.u32, "-"));
Cyril Bonté70be45d2010-10-12 00:14:35 +02003320
Willy Tarreau60600742016-01-11 15:32:30 +01003321 chunk_appendf(out,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003322 /* sessions: current, max, limit, total */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003323 "<td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003324 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01003325 "<tr><th>Cum. connections:</th><td>%s</td></tr>"
3326 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003327 "",
Willy Tarreauc73810f2016-01-11 13:52:04 +01003328 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 +01003329 U2H(stats[ST_F_STOT].u.u64),
Willy Tarreauc73810f2016-01-11 13:52:04 +01003330 U2H(stats[ST_F_CONN_TOT].u.u64),
Willy Tarreaud72c9172016-01-06 15:26:40 +01003331 U2H(stats[ST_F_STOT].u.u64));
Cyril Bonté70be45d2010-10-12 00:14:35 +02003332
Willy Tarreau466c9b52012-12-23 02:25:03 +01003333 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
Willy Tarreauf8211df2016-01-11 14:09:38 +01003334 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
Willy Tarreau60600742016-01-11 15:32:30 +01003335 chunk_appendf(out,
Willy Tarreau466c9b52012-12-23 02:25:03 +01003336 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
3337 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
3338 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
3339 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
3340 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
3341 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
3342 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
3343 "<tr><th>- other responses:</th><td>%s</td></tr>"
3344 "<tr><th>Intercepted requests:</th><td>%s</td></tr>"
3345 "",
Willy Tarreaud72c9172016-01-06 15:26:40 +01003346 U2H(stats[ST_F_REQ_TOT].u.u64),
3347 U2H(stats[ST_F_HRSP_1XX].u.u64),
3348 U2H(stats[ST_F_HRSP_2XX].u.u64),
3349 U2H(stats[ST_F_COMP_RSP].u.u64),
3350 stats[ST_F_HRSP_2XX].u.u64 ?
3351 (int)(100 * stats[ST_F_COMP_RSP].u.u64 / stats[ST_F_HRSP_2XX].u.u64) : 0,
3352 U2H(stats[ST_F_HRSP_3XX].u.u64),
3353 U2H(stats[ST_F_HRSP_4XX].u.u64),
3354 U2H(stats[ST_F_HRSP_5XX].u.u64),
3355 U2H(stats[ST_F_HRSP_OTHER].u.u64),
Willy Tarreau5b9bdff2016-01-11 14:40:47 +01003356 U2H(stats[ST_F_INTERCEPTED].u.u64));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003357 }
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02003358
Willy Tarreau60600742016-01-11 15:32:30 +01003359 chunk_appendf(out,
Willy Tarreau466c9b52012-12-23 02:25:03 +01003360 "</table></div></u></td>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05003361 /* sessions: lbtot, lastsess */
3362 "<td></td><td></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003363 /* bytes : in */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003364 "<td>%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003365 "",
Willy Tarreaud72c9172016-01-06 15:26:40 +01003366 U2H(stats[ST_F_BIN].u.u64));
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02003367
Willy Tarreau60600742016-01-11 15:32:30 +01003368 chunk_appendf(out,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003369 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
Willy Tarreau4e5d58e2015-07-04 14:35:15 +02003370 "<td>%s%s<div class=tips><table class=det>"
3371 "<tr><th>Response bytes in:</th><td>%s</td></tr>"
3372 "<tr><th>Compression in:</th><td>%s</td></tr>"
3373 "<tr><th>Compression out:</th><td>%s</td><td>(%d%%)</td></tr>"
3374 "<tr><th>Compression bypass:</th><td>%s</td></tr>"
3375 "<tr><th>Total bytes saved:</th><td>%s</td><td>(%d%%)</td></tr>"
3376 "</table></div>%s</td>",
Willy Tarreaud72c9172016-01-06 15:26:40 +01003377 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "<u>":"",
3378 U2H(stats[ST_F_BOUT].u.u64),
3379 U2H(stats[ST_F_BOUT].u.u64),
3380 U2H(stats[ST_F_COMP_IN].u.u64),
3381 U2H(stats[ST_F_COMP_OUT].u.u64),
3382 stats[ST_F_COMP_IN].u.u64 ? (int)(stats[ST_F_COMP_OUT].u.u64 * 100 / stats[ST_F_COMP_IN].u.u64) : 0,
3383 U2H(stats[ST_F_COMP_BYP].u.u64),
3384 U2H(stats[ST_F_COMP_IN].u.u64 - stats[ST_F_COMP_OUT].u.u64),
3385 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,
3386 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "</u>":"");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02003387
Willy Tarreau60600742016-01-11 15:32:30 +01003388 chunk_appendf(out,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003389 /* denied: req, resp */
3390 "<td>%s</td><td>%s</td>"
3391 /* errors : request, connect, response */
3392 "<td>%s</td><td></td><td></td>"
3393 /* warnings: retries, redispatches */
3394 "<td></td><td></td>"
3395 /* server status : reflect frontend status */
3396 "<td class=ac>%s</td>"
3397 /* rest of server: nothing */
3398 "<td class=ac colspan=8></td></tr>"
3399 "",
Willy Tarreaud72c9172016-01-06 15:26:40 +01003400 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
3401 U2H(stats[ST_F_EREQ].u.u64),
3402 field_str(stats, ST_F_STATUS));
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02003403 }
Willy Tarreaufa9512f2016-01-08 17:20:51 +01003404 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_SO) {
Willy Tarreau60600742016-01-11 15:32:30 +01003405 chunk_appendf(out, "<tr class=socket>");
Willy Tarreau508a63f2016-01-11 15:28:40 +01003406 if (flags & ST_SHOWADMIN) {
Willy Tarreaufa9512f2016-01-08 17:20:51 +01003407 /* Column sub-heading for Enable or Disable server */
Willy Tarreau60600742016-01-11 15:32:30 +01003408 chunk_appendf(out, "<td></td>");
Willy Tarreaufa9512f2016-01-08 17:20:51 +01003409 }
3410
Willy Tarreau60600742016-01-11 15:32:30 +01003411 chunk_appendf(out,
Willy Tarreaufa9512f2016-01-08 17:20:51 +01003412 /* frontend name, listener name */
3413 "<td class=ac><a name=\"%s/+%s\"></a>%s"
3414 "<a class=lfsb href=\"#%s/+%s\">%s</a>"
3415 "",
3416 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME),
3417 (flags & ST_SHLGNDS)?"<u>":"",
3418 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME), field_str(stats, ST_F_SVNAME));
3419
3420 if (flags & ST_SHLGNDS) {
Willy Tarreau60600742016-01-11 15:32:30 +01003421 chunk_appendf(out, "<div class=tips>");
Willy Tarreaufa9512f2016-01-08 17:20:51 +01003422
3423 if (isdigit(*field_str(stats, ST_F_ADDR)))
Willy Tarreau60600742016-01-11 15:32:30 +01003424 chunk_appendf(out, "IPv4: %s, ", field_str(stats, ST_F_ADDR));
Willy Tarreaufa9512f2016-01-08 17:20:51 +01003425 else if (*field_str(stats, ST_F_ADDR) == '[')
Willy Tarreau60600742016-01-11 15:32:30 +01003426 chunk_appendf(out, "IPv6: %s, ", field_str(stats, ST_F_ADDR));
Willy Tarreaufa9512f2016-01-08 17:20:51 +01003427 else if (*field_str(stats, ST_F_ADDR))
Willy Tarreau60600742016-01-11 15:32:30 +01003428 chunk_appendf(out, "%s, ", field_str(stats, ST_F_ADDR));
Willy Tarreaufa9512f2016-01-08 17:20:51 +01003429
3430 /* id */
Willy Tarreau60600742016-01-11 15:32:30 +01003431 chunk_appendf(out, "id: %d</div>", stats[ST_F_SID].u.u32);
Willy Tarreaufa9512f2016-01-08 17:20:51 +01003432 }
3433
Willy Tarreau60600742016-01-11 15:32:30 +01003434 chunk_appendf(out,
Willy Tarreaufa9512f2016-01-08 17:20:51 +01003435 /* queue */
3436 "%s</td><td colspan=3></td>"
3437 /* sessions rate: current, max, limit */
3438 "<td colspan=3>&nbsp;</td>"
3439 /* sessions: current, max, limit, total, lbtot, lastsess */
3440 "<td>%s</td><td>%s</td><td>%s</td>"
3441 "<td>%s</td><td>&nbsp;</td><td>&nbsp;</td>"
3442 /* bytes: in, out */
3443 "<td>%s</td><td>%s</td>"
3444 "",
3445 (flags & ST_SHLGNDS)?"</u>":"",
3446 U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SMAX].u.u32), U2H(stats[ST_F_SLIM].u.u32),
3447 U2H(stats[ST_F_STOT].u.u64), U2H(stats[ST_F_BIN].u.u64), U2H(stats[ST_F_BOUT].u.u64));
3448
Willy Tarreau60600742016-01-11 15:32:30 +01003449 chunk_appendf(out,
Willy Tarreaufa9512f2016-01-08 17:20:51 +01003450 /* denied: req, resp */
3451 "<td>%s</td><td>%s</td>"
3452 /* errors: request, connect, response */
3453 "<td>%s</td><td></td><td></td>"
3454 /* warnings: retries, redispatches */
3455 "<td></td><td></td>"
3456 /* server status: reflect listener status */
3457 "<td class=ac>%s</td>"
3458 /* rest of server: nothing */
3459 "<td class=ac colspan=8></td></tr>"
3460 "",
3461 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
3462 U2H(stats[ST_F_EREQ].u.u64),
3463 field_str(stats, ST_F_STATUS));
3464 }
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003465 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_SV) {
3466 const char *style;
Willy Tarreaub5f66b82016-01-08 17:15:39 +01003467
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003468 /* determine the style to use depending on the server's state,
3469 * its health and weight. There isn't a 1-to-1 mapping between
3470 * state and styles for the cases where the server is (still)
3471 * up. The reason is that we don't want to report nolb and
3472 * drain with the same color.
3473 */
Willy Tarreaub5f66b82016-01-08 17:15:39 +01003474
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003475 if (strcmp(field_str(stats, ST_F_STATUS), "DOWN") == 0 ||
3476 strcmp(field_str(stats, ST_F_STATUS), "DOWN (agent)") == 0) {
3477 style = "down";
3478 }
3479 else if (strcmp(field_str(stats, ST_F_STATUS), "DOWN ") == 0) {
3480 style = "going_up";
3481 }
3482 else if (strcmp(field_str(stats, ST_F_STATUS), "NOLB ") == 0) {
3483 style = "going_down";
3484 }
3485 else if (strcmp(field_str(stats, ST_F_STATUS), "NOLB") == 0) {
3486 style = "nolb";
3487 }
3488 else if (strcmp(field_str(stats, ST_F_STATUS), "no check") == 0) {
3489 style = "no_check";
3490 }
3491 else if (!stats[ST_F_CHKFAIL].type ||
3492 stats[ST_F_CHECK_HEALTH].u.u32 == stats[ST_F_CHECK_RISE].u.u32 + stats[ST_F_CHECK_FALL].u.u32 - 1) {
3493 /* no check or max health = UP */
3494 if (stats[ST_F_WEIGHT].u.u32)
3495 style = "up";
3496 else
3497 style = "draining";
3498 }
3499 else {
3500 style = "going_down";
3501 }
Willy Tarreaub5f66b82016-01-08 17:15:39 +01003502
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003503 if (memcmp(field_str(stats, ST_F_STATUS), "MAINT", 5) == 0)
Willy Tarreau60600742016-01-11 15:32:30 +01003504 chunk_appendf(out, "<tr class=\"maintain\">");
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003505 else
Willy Tarreau60600742016-01-11 15:32:30 +01003506 chunk_appendf(out,
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003507 "<tr class=\"%s_%s\">",
3508 (stats[ST_F_BCK].u.u32) ? "backup" : "active", style);
Willy Tarreaub5f66b82016-01-08 17:15:39 +01003509
Willy Tarreaub5f66b82016-01-08 17:15:39 +01003510
Willy Tarreau508a63f2016-01-11 15:28:40 +01003511 if (flags & ST_SHOWADMIN)
Willy Tarreau60600742016-01-11 15:32:30 +01003512 chunk_appendf(out,
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003513 "<td><input type=\"checkbox\" name=\"s\" value=\"%s\"></td>",
3514 field_str(stats, ST_F_SVNAME));
Willy Tarreaub5f66b82016-01-08 17:15:39 +01003515
Willy Tarreau60600742016-01-11 15:32:30 +01003516 chunk_appendf(out,
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003517 "<td class=ac><a name=\"%s/%s\"></a>%s"
3518 "<a class=lfsb href=\"#%s/%s\">%s</a>"
3519 "",
3520 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME),
3521 (flags & ST_SHLGNDS) ? "<u>" : "",
3522 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 +01003523
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003524 if (flags & ST_SHLGNDS) {
Willy Tarreau60600742016-01-11 15:32:30 +01003525 chunk_appendf(out, "<div class=tips>");
Willy Tarreaub5f66b82016-01-08 17:15:39 +01003526
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003527 if (isdigit(*field_str(stats, ST_F_ADDR)))
Willy Tarreau60600742016-01-11 15:32:30 +01003528 chunk_appendf(out, "IPv4: %s, ", field_str(stats, ST_F_ADDR));
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003529 else if (*field_str(stats, ST_F_ADDR) == '[')
Willy Tarreau60600742016-01-11 15:32:30 +01003530 chunk_appendf(out, "IPv6: %s, ", field_str(stats, ST_F_ADDR));
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003531 else if (*field_str(stats, ST_F_ADDR))
Willy Tarreau60600742016-01-11 15:32:30 +01003532 chunk_appendf(out, "%s, ", field_str(stats, ST_F_ADDR));
Willy Tarreaub5f66b82016-01-08 17:15:39 +01003533
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003534 /* id */
Willy Tarreau60600742016-01-11 15:32:30 +01003535 chunk_appendf(out, "id: %d", stats[ST_F_SID].u.u32);
Willy Tarreaub5f66b82016-01-08 17:15:39 +01003536
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003537 /* cookie */
3538 if (stats[ST_F_COOKIE].type) {
Willy Tarreau60600742016-01-11 15:32:30 +01003539 chunk_appendf(out, ", cookie: '");
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003540 chunk_initstr(&src, field_str(stats, ST_F_COOKIE));
Willy Tarreau60600742016-01-11 15:32:30 +01003541 chunk_htmlencode(out, &src);
3542 chunk_appendf(out, "'");
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003543 }
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02003544
Willy Tarreau60600742016-01-11 15:32:30 +01003545 chunk_appendf(out, "</div>");
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003546 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003547
Willy Tarreau60600742016-01-11 15:32:30 +01003548 chunk_appendf(out,
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003549 /* queue : current, max, limit */
3550 "%s</td><td>%s</td><td>%s</td><td>%s</td>"
3551 /* sessions rate : current, max, limit */
3552 "<td>%s</td><td>%s</td><td></td>"
3553 "",
3554 (flags & ST_SHLGNDS) ? "</u>" : "",
3555 U2H(stats[ST_F_QCUR].u.u32), U2H(stats[ST_F_QMAX].u.u32), LIM2A(stats[ST_F_QLIMIT].u.u32, "-"),
3556 U2H(stats[ST_F_RATE].u.u32), U2H(stats[ST_F_RATE_MAX].u.u32));
Willy Tarreau4607fad2016-01-06 15:41:29 +01003557
Willy Tarreau60600742016-01-11 15:32:30 +01003558 chunk_appendf(out,
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003559 /* sessions: current, max, limit, total */
3560 "<td>%s</td><td>%s</td><td>%s</td>"
3561 "<td><u>%s<div class=tips><table class=det>"
3562 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
3563 "",
3564 U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SMAX].u.u32), LIM2A(stats[ST_F_SLIM].u.u32, "-"),
3565 U2H(stats[ST_F_STOT].u.u64),
3566 U2H(stats[ST_F_STOT].u.u64));
Willy Tarreau4607fad2016-01-06 15:41:29 +01003567
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003568 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
Willy Tarreauf8211df2016-01-11 14:09:38 +01003569 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003570 unsigned long long tot;
Willy Tarreaua6f5a732016-01-08 16:59:56 +01003571
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003572 tot = stats[ST_F_HRSP_OTHER].u.u64;
3573 tot += stats[ST_F_HRSP_1XX].u.u64;
3574 tot += stats[ST_F_HRSP_2XX].u.u64;
3575 tot += stats[ST_F_HRSP_3XX].u.u64;
3576 tot += stats[ST_F_HRSP_4XX].u.u64;
3577 tot += stats[ST_F_HRSP_5XX].u.u64;
3578
Willy Tarreau60600742016-01-11 15:32:30 +01003579 chunk_appendf(out,
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003580 "<tr><th>Cum. HTTP responses:</th><td>%s</td></tr>"
3581 "<tr><th>- HTTP 1xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
3582 "<tr><th>- HTTP 2xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
3583 "<tr><th>- HTTP 3xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
3584 "<tr><th>- HTTP 4xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
3585 "<tr><th>- HTTP 5xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
3586 "<tr><th>- other responses:</th><td>%s</td><td>(%d%%)</td></tr>"
3587 "",
3588 U2H(tot),
3589 U2H(stats[ST_F_HRSP_1XX].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_1XX].u.u64 / tot) : 0,
3590 U2H(stats[ST_F_HRSP_2XX].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_2XX].u.u64 / tot) : 0,
3591 U2H(stats[ST_F_HRSP_3XX].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_3XX].u.u64 / tot) : 0,
3592 U2H(stats[ST_F_HRSP_4XX].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_4XX].u.u64 / tot) : 0,
3593 U2H(stats[ST_F_HRSP_5XX].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_5XX].u.u64 / tot) : 0,
3594 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 +01003595 }
Willy Tarreaua6f5a732016-01-08 16:59:56 +01003596
Willy Tarreau60600742016-01-11 15:32:30 +01003597 chunk_appendf(out, "<tr><th colspan=3>Avg over last 1024 success. conn.</th></tr>");
3598 chunk_appendf(out, "<tr><th>- Queue time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_QTIME].u.u32));
3599 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 +01003600 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
Willy Tarreau60600742016-01-11 15:32:30 +01003601 chunk_appendf(out, "<tr><th>- Response time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_RTIME].u.u32));
3602 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 +02003603
Willy Tarreau60600742016-01-11 15:32:30 +01003604 chunk_appendf(out,
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003605 "</table></div></u></td>"
3606 /* sessions: lbtot, last */
3607 "<td>%s</td><td>%s</td>",
3608 U2H(stats[ST_F_LBTOT].u.u64),
3609 human_time(stats[ST_F_LASTSESS].u.s32, 1));
Willy Tarreau91861262007-10-17 17:06:05 +02003610
Willy Tarreau60600742016-01-11 15:32:30 +01003611 chunk_appendf(out,
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003612 /* bytes : in, out */
3613 "<td>%s</td><td>%s</td>"
3614 /* denied: req, resp */
3615 "<td></td><td>%s</td>"
3616 /* errors : request, connect */
3617 "<td></td><td>%s</td>"
3618 /* errors : response */
3619 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
3620 /* warnings: retries, redispatches */
3621 "<td>%lld</td><td>%lld</td>"
3622 "",
3623 U2H(stats[ST_F_BIN].u.u64), U2H(stats[ST_F_BOUT].u.u64),
3624 U2H(stats[ST_F_DRESP].u.u64),
3625 U2H(stats[ST_F_ECON].u.u64),
3626 U2H(stats[ST_F_ERESP].u.u64),
3627 (long long)stats[ST_F_CLI_ABRT].u.u64,
3628 (long long)stats[ST_F_SRV_ABRT].u.u64,
3629 (long long)stats[ST_F_WRETR].u.u64,
3630 (long long)stats[ST_F_WREDIS].u.u64);
3631
3632 /* status, last change */
Willy Tarreau60600742016-01-11 15:32:30 +01003633 chunk_appendf(out, "<td class=ac>");
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003634
3635 /* FIXME!!!!
3636 * LASTCHG should contain the last change for *this* server and must be computed
3637 * properly above, as was done below, ie: this server if maint, otherwise ref server
3638 * if tracking. Note that ref is either local or remote depending on tracking.
3639 */
3640
3641
3642 if (memcmp(field_str(stats, ST_F_STATUS), "MAINT", 5) == 0) {
Willy Tarreau60600742016-01-11 15:32:30 +01003643 chunk_appendf(out, "%s MAINT", human_time(stats[ST_F_LASTCHG].u.u32, 1));
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003644 }
3645 else if (memcmp(field_str(stats, ST_F_STATUS), "no check", 5) == 0) {
Willy Tarreau60600742016-01-11 15:32:30 +01003646 chunk_strcat(out, "<i>no check</i>");
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003647 }
3648 else {
Willy Tarreau60600742016-01-11 15:32:30 +01003649 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 +01003650 if (memcmp(field_str(stats, ST_F_STATUS), "DOWN", 4) == 0) {
3651 if (stats[ST_F_CHECK_HEALTH].u.u32)
Willy Tarreau60600742016-01-11 15:32:30 +01003652 chunk_strcat(out, " &uarr;");
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003653 }
3654 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 +01003655 chunk_strcat(out, " &darr;");
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003656 }
3657
3658 if (memcmp(field_str(stats, ST_F_STATUS), "DOWN", 4) == 0 &&
3659 stats[ST_F_AGENT_STATUS].type && !stats[ST_F_AGENT_HEALTH].u.u32) {
Willy Tarreau60600742016-01-11 15:32:30 +01003660 chunk_appendf(out,
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003661 "</td><td class=ac><u> %s",
3662 field_str(stats, ST_F_AGENT_STATUS));
3663
3664 if (stats[ST_F_AGENT_CODE].type)
Willy Tarreau60600742016-01-11 15:32:30 +01003665 chunk_appendf(out, "/%d", stats[ST_F_AGENT_CODE].u.u32);
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003666
3667 if (stats[ST_F_AGENT_DURATION].type && stats[ST_F_AGENT_DURATION].u.u64 >= 0)
Willy Tarreau60600742016-01-11 15:32:30 +01003668 chunk_appendf(out, " in %lums", (long)stats[ST_F_AGENT_DURATION].u.u64);
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003669
Willy Tarreau60600742016-01-11 15:32:30 +01003670 chunk_appendf(out, "<div class=tips>%s", field_str(stats, ST_F_AGENT_DESC));
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003671
3672 if (*field_str(stats, ST_F_LAST_AGT)) {
Willy Tarreau60600742016-01-11 15:32:30 +01003673 chunk_appendf(out, ": ");
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003674 chunk_initstr(&src, field_str(stats, ST_F_LAST_AGT));
Willy Tarreau60600742016-01-11 15:32:30 +01003675 chunk_htmlencode(out, &src);
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003676 }
Willy Tarreau60600742016-01-11 15:32:30 +01003677 chunk_appendf(out, "</div></u>");
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003678 }
3679 else if (stats[ST_F_CHECK_STATUS].type) {
Willy Tarreau60600742016-01-11 15:32:30 +01003680 chunk_appendf(out,
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003681 "</td><td class=ac><u> %s",
3682 field_str(stats, ST_F_CHECK_STATUS));
3683
3684 if (stats[ST_F_CHECK_CODE].type)
Willy Tarreau60600742016-01-11 15:32:30 +01003685 chunk_appendf(out, "/%d", stats[ST_F_CHECK_CODE].u.u32);
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003686
3687 if (stats[ST_F_CHECK_DURATION].type && stats[ST_F_CHECK_DURATION].u.u64 >= 0)
Willy Tarreau60600742016-01-11 15:32:30 +01003688 chunk_appendf(out, " in %lums", (long)stats[ST_F_CHECK_DURATION].u.u64);
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003689
Willy Tarreau60600742016-01-11 15:32:30 +01003690 chunk_appendf(out, "<div class=tips>%s", field_str(stats, ST_F_CHECK_DESC));
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003691
3692 if (*field_str(stats, ST_F_LAST_CHK)) {
Willy Tarreau60600742016-01-11 15:32:30 +01003693 chunk_appendf(out, ": ");
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003694 chunk_initstr(&src, field_str(stats, ST_F_LAST_CHK));
Willy Tarreau60600742016-01-11 15:32:30 +01003695 chunk_htmlencode(out, &src);
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003696 }
Willy Tarreau60600742016-01-11 15:32:30 +01003697 chunk_appendf(out, "</div></u>");
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003698 }
3699 else
Willy Tarreau60600742016-01-11 15:32:30 +01003700 chunk_appendf(out, "</td><td>");
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003701
Willy Tarreau60600742016-01-11 15:32:30 +01003702 chunk_appendf(out,
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003703 /* weight */
3704 "</td><td class=ac>%d</td>"
3705 /* act, bck */
3706 "<td class=ac>%s</td><td class=ac>%s</td>"
3707 "",
3708 stats[ST_F_WEIGHT].u.u32,
3709 stats[ST_F_BCK].u.u32 ? "-" : "Y",
3710 stats[ST_F_BCK].u.u32 ? "Y" : "-");
3711
3712 /* check failures: unique, fatal, down time */
3713 if (stats[ST_F_CHKFAIL].type) {
Willy Tarreau60600742016-01-11 15:32:30 +01003714 chunk_appendf(out, "<td><u>%lld", (long long)stats[ST_F_CHKFAIL].u.u64);
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003715
3716 if (stats[ST_F_HANAFAIL].type)
Willy Tarreau60600742016-01-11 15:32:30 +01003717 chunk_appendf(out, "/%lld", (long long)stats[ST_F_HANAFAIL].u.u64);
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003718
Willy Tarreau60600742016-01-11 15:32:30 +01003719 chunk_appendf(out,
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003720 "<div class=tips>Failed Health Checks%s</div></u></td>"
3721 "<td>%lld</td><td>%s</td>"
3722 "",
3723 stats[ST_F_HANAFAIL].type ? "/Health Analyses" : "",
3724 (long long)stats[ST_F_CHKDOWN].u.u64, human_time(stats[ST_F_DOWNTIME].u.u32, 1));
3725 }
3726 else if (strcmp(field_str(stats, ST_F_STATUS), "MAINT") != 0 && field_format(stats, ST_F_TRACKED) == FF_STR) {
3727 /* tracking a server (hence inherited maint would appear as "MAINT (via...)" */
Willy Tarreau60600742016-01-11 15:32:30 +01003728 chunk_appendf(out,
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003729 "<td class=ac colspan=3><a class=lfsb href=\"#%s\">via %s</a></td>",
3730 field_str(stats, ST_F_TRACKED), field_str(stats, ST_F_TRACKED));
3731 }
3732 else
Willy Tarreau60600742016-01-11 15:32:30 +01003733 chunk_appendf(out, "<td colspan=3></td>");
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003734
3735 /* throttle */
3736 if (stats[ST_F_THROTTLE].type)
Willy Tarreau60600742016-01-11 15:32:30 +01003737 chunk_appendf(out, "<td class=ac>%d %%</td></tr>\n", stats[ST_F_THROTTLE].u.u32);
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003738 else
Willy Tarreau60600742016-01-11 15:32:30 +01003739 chunk_appendf(out, "<td class=ac>-</td></tr>\n");
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003740 }
Willy Tarreaubbf84502016-01-08 17:25:50 +01003741 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_BE) {
Willy Tarreau60600742016-01-11 15:32:30 +01003742 chunk_appendf(out, "<tr class=\"backend\">");
Willy Tarreau508a63f2016-01-11 15:28:40 +01003743 if (flags & ST_SHOWADMIN) {
Willy Tarreaubbf84502016-01-08 17:25:50 +01003744 /* Column sub-heading for Enable or Disable server */
Willy Tarreau60600742016-01-11 15:32:30 +01003745 chunk_appendf(out, "<td></td>");
Willy Tarreaubbf84502016-01-08 17:25:50 +01003746 }
Willy Tarreau60600742016-01-11 15:32:30 +01003747 chunk_appendf(out,
Willy Tarreaubbf84502016-01-08 17:25:50 +01003748 "<td class=ac>"
3749 /* name */
3750 "%s<a name=\"%s/Backend\"></a>"
3751 "<a class=lfsb href=\"#%s/Backend\">Backend</a>"
3752 "",
3753 (flags & ST_SHLGNDS)?"<u>":"",
3754 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_PXNAME));
3755
3756 if (flags & ST_SHLGNDS) {
3757 /* balancing */
Willy Tarreau60600742016-01-11 15:32:30 +01003758 chunk_appendf(out, "<div class=tips>balancing: %s",
Willy Tarreauf1516d92016-01-11 14:48:36 +01003759 field_str(stats, ST_F_ALGO));
Willy Tarreaubbf84502016-01-08 17:25:50 +01003760
3761 /* cookie */
3762 if (stats[ST_F_COOKIE].type) {
Willy Tarreau60600742016-01-11 15:32:30 +01003763 chunk_appendf(out, ", cookie: '");
Willy Tarreaubbf84502016-01-08 17:25:50 +01003764 chunk_initstr(&src, field_str(stats, ST_F_COOKIE));
Willy Tarreau60600742016-01-11 15:32:30 +01003765 chunk_htmlencode(out, &src);
3766 chunk_appendf(out, "'");
Willy Tarreaubbf84502016-01-08 17:25:50 +01003767 }
Willy Tarreau60600742016-01-11 15:32:30 +01003768 chunk_appendf(out, "</div>");
Willy Tarreaubbf84502016-01-08 17:25:50 +01003769 }
3770
Willy Tarreau60600742016-01-11 15:32:30 +01003771 chunk_appendf(out,
Willy Tarreaubbf84502016-01-08 17:25:50 +01003772 "%s</td>"
3773 /* queue : current, max */
3774 "<td>%s</td><td>%s</td><td></td>"
3775 /* sessions rate : current, max, limit */
3776 "<td>%s</td><td>%s</td><td></td>"
3777 "",
3778 (flags & ST_SHLGNDS)?"</u>":"",
3779 U2H(stats[ST_F_QCUR].u.u32), U2H(stats[ST_F_QMAX].u.u32),
3780 U2H(stats[ST_F_RATE].u.u32), U2H(stats[ST_F_RATE_MAX].u.u32));
3781
Willy Tarreau60600742016-01-11 15:32:30 +01003782 chunk_appendf(out,
Willy Tarreaubbf84502016-01-08 17:25:50 +01003783 /* sessions: current, max, limit, total */
3784 "<td>%s</td><td>%s</td><td>%s</td>"
3785 "<td><u>%s<div class=tips><table class=det>"
3786 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
3787 "",
3788 U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SLIM].u.u32),
3789 U2H(stats[ST_F_STOT].u.u64),
3790 U2H(stats[ST_F_STOT].u.u64));
3791
3792 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
Willy Tarreauf8211df2016-01-11 14:09:38 +01003793 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
Willy Tarreau60600742016-01-11 15:32:30 +01003794 chunk_appendf(out,
Willy Tarreaubbf84502016-01-08 17:25:50 +01003795 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
3796 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
3797 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
3798 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
3799 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
3800 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
3801 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
3802 "<tr><th>- other responses:</th><td>%s</td></tr>"
3803 "<tr><th>Intercepted requests:</th><td>%s</td></tr>"
3804 "<tr><th colspan=3>Avg over last 1024 success. conn.</th></tr>"
3805 "",
3806 U2H(stats[ST_F_REQ_TOT].u.u64),
3807 U2H(stats[ST_F_HRSP_1XX].u.u64),
3808 U2H(stats[ST_F_HRSP_2XX].u.u64),
3809 U2H(stats[ST_F_COMP_RSP].u.u64),
3810 stats[ST_F_HRSP_2XX].u.u64 ?
3811 (int)(100 * stats[ST_F_COMP_RSP].u.u64 / stats[ST_F_HRSP_2XX].u.u64) : 0,
3812 U2H(stats[ST_F_HRSP_3XX].u.u64),
3813 U2H(stats[ST_F_HRSP_4XX].u.u64),
3814 U2H(stats[ST_F_HRSP_5XX].u.u64),
3815 U2H(stats[ST_F_HRSP_OTHER].u.u64),
Willy Tarreau5b9bdff2016-01-11 14:40:47 +01003816 U2H(stats[ST_F_INTERCEPTED].u.u64));
Willy Tarreaubbf84502016-01-08 17:25:50 +01003817 }
3818
Willy Tarreau60600742016-01-11 15:32:30 +01003819 chunk_appendf(out, "<tr><th>- Queue time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_QTIME].u.u32));
3820 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 +01003821 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
Willy Tarreau60600742016-01-11 15:32:30 +01003822 chunk_appendf(out, "<tr><th>- Response time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_RTIME].u.u32));
3823 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 +01003824
Willy Tarreau60600742016-01-11 15:32:30 +01003825 chunk_appendf(out,
Willy Tarreaubbf84502016-01-08 17:25:50 +01003826 "</table></div></u></td>"
3827 /* sessions: lbtot, last */
3828 "<td>%s</td><td>%s</td>"
3829 /* bytes: in */
3830 "<td>%s</td>"
3831 "",
3832 U2H(stats[ST_F_LBTOT].u.u64),
3833 human_time(stats[ST_F_LASTSESS].u.s32, 1),
3834 U2H(stats[ST_F_BIN].u.u64));
3835
Willy Tarreau60600742016-01-11 15:32:30 +01003836 chunk_appendf(out,
Willy Tarreaubbf84502016-01-08 17:25:50 +01003837 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
3838 "<td>%s%s<div class=tips><table class=det>"
3839 "<tr><th>Response bytes in:</th><td>%s</td></tr>"
3840 "<tr><th>Compression in:</th><td>%s</td></tr>"
3841 "<tr><th>Compression out:</th><td>%s</td><td>(%d%%)</td></tr>"
3842 "<tr><th>Compression bypass:</th><td>%s</td></tr>"
3843 "<tr><th>Total bytes saved:</th><td>%s</td><td>(%d%%)</td></tr>"
3844 "</table></div>%s</td>",
3845 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "<u>":"",
3846 U2H(stats[ST_F_BOUT].u.u64),
3847 U2H(stats[ST_F_BOUT].u.u64),
3848 U2H(stats[ST_F_COMP_IN].u.u64),
3849 U2H(stats[ST_F_COMP_OUT].u.u64),
3850 stats[ST_F_COMP_IN].u.u64 ? (int)(stats[ST_F_COMP_OUT].u.u64 * 100 / stats[ST_F_COMP_IN].u.u64) : 0,
3851 U2H(stats[ST_F_COMP_BYP].u.u64),
Christopher Faulet0b64f622016-04-28 15:09:31 +02003852 U2H(stats[ST_F_COMP_IN].u.u64 - stats[ST_F_COMP_OUT].u.u64),
Willy Tarreaubbf84502016-01-08 17:25:50 +01003853 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,
3854 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "</u>":"");
3855
Willy Tarreau60600742016-01-11 15:32:30 +01003856 chunk_appendf(out,
Willy Tarreaubbf84502016-01-08 17:25:50 +01003857 /* denied: req, resp */
3858 "<td>%s</td><td>%s</td>"
3859 /* errors : request, connect */
3860 "<td></td><td>%s</td>"
3861 /* errors : response */
3862 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
3863 /* warnings: retries, redispatches */
3864 "<td>%lld</td><td>%lld</td>"
3865 /* backend status: reflect backend status (up/down): we display UP
3866 * if the backend has known working servers or if it has no server at
3867 * all (eg: for stats). Then we display the total weight, number of
3868 * active and backups. */
3869 "<td class=ac>%s %s</td><td class=ac>&nbsp;</td><td class=ac>%d</td>"
3870 "<td class=ac>%d</td><td class=ac>%d</td>"
3871 "",
3872 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
3873 U2H(stats[ST_F_ECON].u.u64),
3874 U2H(stats[ST_F_ERESP].u.u64),
3875 (long long)stats[ST_F_CLI_ABRT].u.u64,
3876 (long long)stats[ST_F_SRV_ABRT].u.u64,
3877 (long long)stats[ST_F_WRETR].u.u64, (long long)stats[ST_F_WREDIS].u.u64,
3878 human_time(stats[ST_F_LASTCHG].u.u32, 1),
3879 strcmp(field_str(stats, ST_F_STATUS), "DOWN") ? field_str(stats, ST_F_STATUS) : "<font color=\"red\"><b>DOWN</b></font>",
3880 stats[ST_F_WEIGHT].u.u32,
3881 stats[ST_F_ACT].u.u32, stats[ST_F_BCK].u.u32);
3882
Willy Tarreau60600742016-01-11 15:32:30 +01003883 chunk_appendf(out,
Willy Tarreaubbf84502016-01-08 17:25:50 +01003884 /* rest of backend: nothing, down transitions, total downtime, throttle */
3885 "<td class=ac>&nbsp;</td><td>%d</td>"
3886 "<td>%s</td>"
3887 "<td></td>"
3888 "</tr>",
3889 stats[ST_F_CHKDOWN].u.u32,
3890 stats[ST_F_DOWNTIME].type ? human_time(stats[ST_F_DOWNTIME].u.u32, 1) : "&nbsp;");
3891 }
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003892 return 1;
3893}
3894
Willy Tarreau501f6022016-01-08 17:37:22 +01003895static int stats_dump_one_line(const struct field *stats, unsigned int flags, struct proxy *px, struct appctx *appctx)
3896{
Willy Tarreau508a63f2016-01-11 15:28:40 +01003897 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN))
3898 flags |= ST_SHOWADMIN;
Willy Tarreau501f6022016-01-08 17:37:22 +01003899
3900 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
Willy Tarreau60600742016-01-11 15:32:30 +01003901 return stats_dump_fields_html(&trash, stats, flags);
Willy Tarreau1e62df92016-01-11 18:57:53 +01003902 else if (appctx->ctx.stats.flags & STAT_FMT_TYPED)
3903 return stats_dump_fields_typed(&trash, stats);
Willy Tarreau501f6022016-01-08 17:37:22 +01003904 else
3905 return stats_dump_fields_csv(&trash, stats);
3906}
3907
Thierry Fournier23d2d642016-03-25 08:20:11 +01003908/* Fill <stats> with the frontend statistics. <stats> is
3909 * preallocated array of length <len>. The length of the array
3910 * must be at least ST_F_TOTAL_FIELDS. If this length is less then
3911 * this value, the function returns 0, otherwise, it returns 1.
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003912 */
Thierry Fournier23d2d642016-03-25 08:20:11 +01003913int stats_fill_fe_stats(struct proxy *px, struct field *stats, int len)
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003914{
Thierry Fournier23d2d642016-03-25 08:20:11 +01003915 if (len < ST_F_TOTAL_FIELDS)
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003916 return 0;
3917
Thierry Fournier23d2d642016-03-25 08:20:11 +01003918 memset(stats, 0, sizeof(*stats) * len);
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003919
3920 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
3921 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, "FRONTEND");
Willy Tarreauf8211df2016-01-11 14:09:38 +01003922 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003923 stats[ST_F_SCUR] = mkf_u32(0, px->feconn);
3924 stats[ST_F_SMAX] = mkf_u32(FN_MAX, px->fe_counters.conn_max);
3925 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, px->maxconn);
3926 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, px->fe_counters.cum_sess);
3927 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, px->fe_counters.bytes_in);
3928 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, px->fe_counters.bytes_out);
3929 stats[ST_F_DREQ] = mkf_u64(FN_COUNTER, px->fe_counters.denied_req);
3930 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, px->fe_counters.denied_resp);
3931 stats[ST_F_EREQ] = mkf_u64(FN_COUNTER, px->fe_counters.failed_req);
3932 stats[ST_F_STATUS] = mkf_str(FO_STATUS, px->state == PR_STREADY ? "OPEN" : px->state == PR_STFULL ? "FULL" : "STOP");
3933 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
3934 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
3935 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, 0);
3936 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_FE);
3937 stats[ST_F_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_sess_per_sec));
3938 stats[ST_F_RATE_LIM] = mkf_u32(FO_CONFIG|FN_LIMIT, px->fe_sps_lim);
3939 stats[ST_F_RATE_MAX] = mkf_u32(FN_MAX, px->fe_counters.sps_max);
3940
3941 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
3942 if (px->mode == PR_MODE_HTTP) {
3943 stats[ST_F_HRSP_1XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[1]);
3944 stats[ST_F_HRSP_2XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[2]);
3945 stats[ST_F_HRSP_3XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[3]);
3946 stats[ST_F_HRSP_4XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[4]);
3947 stats[ST_F_HRSP_5XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[5]);
3948 stats[ST_F_HRSP_OTHER] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[0]);
Willy Tarreau5b9bdff2016-01-11 14:40:47 +01003949 stats[ST_F_INTERCEPTED] = mkf_u64(FN_COUNTER, px->fe_counters.intercepted_req);
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003950 }
3951
3952 /* requests : req_rate, req_rate_max, req_tot, */
3953 stats[ST_F_REQ_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_req_per_sec));
3954 stats[ST_F_REQ_RATE_MAX] = mkf_u32(FN_MAX, px->fe_counters.p.http.rps_max);
3955 stats[ST_F_REQ_TOT] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cum_req);
3956
3957 /* compression: in, out, bypassed, responses */
3958 stats[ST_F_COMP_IN] = mkf_u64(FN_COUNTER, px->fe_counters.comp_in);
3959 stats[ST_F_COMP_OUT] = mkf_u64(FN_COUNTER, px->fe_counters.comp_out);
3960 stats[ST_F_COMP_BYP] = mkf_u64(FN_COUNTER, px->fe_counters.comp_byp);
3961 stats[ST_F_COMP_RSP] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.comp_rsp);
3962
Willy Tarreauc73810f2016-01-11 13:52:04 +01003963 /* connections : conn_rate, conn_rate_max, conn_tot, conn_max */
3964 stats[ST_F_CONN_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_conn_per_sec));
3965 stats[ST_F_CONN_RATE_MAX] = mkf_u32(FN_MAX, px->fe_counters.cps_max);
3966 stats[ST_F_CONN_TOT] = mkf_u64(FN_COUNTER, px->fe_counters.cum_conn);
3967
Thierry Fournier23d2d642016-03-25 08:20:11 +01003968 return 1;
3969}
3970
3971/* Dumps a frontend's line to the trash for the current proxy <px> and uses
3972 * the state from stream interface <si>. The caller is responsible for clearing
3973 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
3974 */
3975static int stats_dump_fe_stats(struct stream_interface *si, struct proxy *px)
3976{
3977 struct appctx *appctx = __objt_appctx(si->end);
3978
3979 if (!(px->cap & PR_CAP_FE))
3980 return 0;
3981
3982 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_FE)))
3983 return 0;
3984
3985 if (!stats_fill_fe_stats(px, stats, ST_F_TOTAL_FIELDS))
3986 return 0;
3987
Willy Tarreau501f6022016-01-08 17:37:22 +01003988 return stats_dump_one_line(stats, 0, px, appctx);
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003989}
3990
Thierry Fournierc4456852016-03-25 08:20:49 +01003991/* Fill <stats> with the listener statistics. <stats> is
3992 * preallocated array of length <len>. The length of the array
3993 * must be at least ST_F_TOTAL_FIELDS. If this length is less
3994 * then this value, the function returns 0, otherwise, it
3995 * returns 1. <flags> can take the value ST_SHLGNDS.
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003996 */
Thierry Fournierc4456852016-03-25 08:20:49 +01003997int stats_fill_li_stats(struct proxy *px, struct listener *l, int flags,
3998 struct field *stats, int len)
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003999{
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004000 struct chunk *out = get_trash_chunk();
4001
Thierry Fournierc4456852016-03-25 08:20:49 +01004002 if (len < ST_F_TOTAL_FIELDS)
4003 return 0;
4004
Thierry Fournierac9d4672016-03-25 08:43:46 +01004005 if (!l->counters)
4006 return 0;
4007
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004008 chunk_reset(out);
Thierry Fournierc4456852016-03-25 08:20:49 +01004009 memset(stats, 0, sizeof(*stats) * len);
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004010
4011 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
4012 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, l->name);
Willy Tarreauf8211df2016-01-11 14:09:38 +01004013 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004014 stats[ST_F_SCUR] = mkf_u32(0, l->nbconn);
4015 stats[ST_F_SMAX] = mkf_u32(FN_MAX, l->counters->conn_max);
4016 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, l->maxconn);
4017 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, l->counters->cum_conn);
4018 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, l->counters->bytes_in);
4019 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, l->counters->bytes_out);
4020 stats[ST_F_DREQ] = mkf_u64(FN_COUNTER, l->counters->denied_req);
4021 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, l->counters->denied_resp);
4022 stats[ST_F_EREQ] = mkf_u64(FN_COUNTER, l->counters->failed_req);
4023 stats[ST_F_STATUS] = mkf_str(FO_STATUS, (l->nbconn < l->maxconn) ? (l->state == LI_LIMITED) ? "WAITING" : "OPEN" : "FULL");
4024 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
4025 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
4026 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, l->luid);
4027 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_SO);
4028
4029 if (flags & ST_SHLGNDS) {
4030 char str[INET6_ADDRSTRLEN];
4031 int port;
4032
4033 port = get_host_port(&l->addr);
4034 switch (addr_to_str(&l->addr, str, sizeof(str))) {
4035 case AF_INET:
4036 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
4037 chunk_appendf(out, "%s:%d", str, port);
4038 break;
4039 case AF_INET6:
4040 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
4041 chunk_appendf(out, "[%s]:%d", str, port);
4042 break;
4043 case AF_UNIX:
4044 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, "unix");
4045 break;
4046 case -1:
4047 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
4048 chunk_strcat(out, strerror(errno));
4049 break;
4050 default: /* address family not supported */
4051 break;
4052 }
4053 }
4054
Thierry Fournierc4456852016-03-25 08:20:49 +01004055 return 1;
4056}
4057
4058/* Dumps a line for listener <l> and proxy <px> to the trash and uses the state
4059 * from stream interface <si>, and stats flags <flags>. The caller is responsible
4060 * for clearing the trash if needed. Returns non-zero if it emits anything, zero
4061 * otherwise.
4062 */
4063static int stats_dump_li_stats(struct stream_interface *si, struct proxy *px, struct listener *l, int flags)
4064{
4065 struct appctx *appctx = __objt_appctx(si->end);
4066
4067 if (!stats_fill_li_stats(px, l, flags, stats, ST_F_TOTAL_FIELDS))
4068 return 0;
4069
Willy Tarreau501f6022016-01-08 17:37:22 +01004070 return stats_dump_one_line(stats, flags, px, appctx);
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004071}
4072
4073enum srv_stats_state {
4074 SRV_STATS_STATE_DOWN = 0,
4075 SRV_STATS_STATE_DOWN_AGENT,
4076 SRV_STATS_STATE_GOING_UP,
4077 SRV_STATS_STATE_UP_GOING_DOWN,
4078 SRV_STATS_STATE_UP,
4079 SRV_STATS_STATE_NOLB_GOING_DOWN,
Simon Horman4d2eab62015-04-23 14:51:26 +09004080 SRV_STATS_STATE_NOLB,
4081 SRV_STATS_STATE_DRAIN_GOING_DOWN,
4082 SRV_STATS_STATE_DRAIN,
Simon Hormanb167b6b2015-04-23 14:51:29 +09004083 SRV_STATS_STATE_DRAIN_AGENT,
Simon Horman4d2eab62015-04-23 14:51:26 +09004084 SRV_STATS_STATE_NO_CHECK,
4085
4086 SRV_STATS_STATE_COUNT, /* Must be last */
4087};
4088
Willy Tarreau2b96cf12016-01-06 19:25:38 +01004089static const char *srv_hlt_st[SRV_STATS_STATE_COUNT] = {
4090 [SRV_STATS_STATE_DOWN] = "DOWN",
4091 [SRV_STATS_STATE_DOWN_AGENT] = "DOWN (agent)",
4092 [SRV_STATS_STATE_GOING_UP] = "DOWN %d/%d",
4093 [SRV_STATS_STATE_UP_GOING_DOWN] = "UP %d/%d",
4094 [SRV_STATS_STATE_UP] = "UP",
4095 [SRV_STATS_STATE_NOLB_GOING_DOWN] = "NOLB %d/%d",
4096 [SRV_STATS_STATE_NOLB] = "NOLB",
4097 [SRV_STATS_STATE_DRAIN_GOING_DOWN] = "DRAIN %d/%d",
4098 [SRV_STATS_STATE_DRAIN] = "DRAIN",
4099 [SRV_STATS_STATE_DRAIN_AGENT] = "DRAIN (agent)",
4100 [SRV_STATS_STATE_NO_CHECK] = "no check"
4101};
4102
Thierry Fournier61fe6c02016-03-25 08:21:21 +01004103/* Fill <stats> with the server statistics. <stats> is
4104 * preallocated array of length <len>. The length of the array
4105 * must be at least ST_F_TOTAL_FIELDS. If this length is less
4106 * then this value, the function returns 0, otherwise, it
4107 * returns 1. <flags> can take the value ST_SHLGNDS.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004108 */
Thierry Fournier61fe6c02016-03-25 08:21:21 +01004109int stats_fill_sv_stats(struct proxy *px, struct server *sv, int flags,
4110 struct field *stats, int len)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004111{
Willy Tarreau32091232014-05-16 13:52:00 +02004112 struct server *via, *ref;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004113 char str[INET6_ADDRSTRLEN];
Willy Tarreau2b96cf12016-01-06 19:25:38 +01004114 struct chunk *out = get_trash_chunk();
Willy Tarreauba2f2642016-01-07 09:54:40 +01004115 enum srv_stats_state state;
Willy Tarreau2b96cf12016-01-06 19:25:38 +01004116 char *fld_status;
Thierry Fournier61fe6c02016-03-25 08:21:21 +01004117
4118 if (len < ST_F_TOTAL_FIELDS)
4119 return 0;
4120
4121 memset(stats, 0, sizeof(*stats) * len);
4122
Willy Tarreau32091232014-05-16 13:52:00 +02004123 /* we have "via" which is the tracked server as described in the configuration,
4124 * and "ref" which is the checked server and the end of the chain.
4125 */
4126 via = sv->track ? sv->track : sv;
4127 ref = via;
4128 while (ref->track)
4129 ref = ref->track;
4130
Willy Tarreauba2f2642016-01-07 09:54:40 +01004131 if (sv->state == SRV_ST_RUNNING || sv->state == SRV_ST_STARTING) {
4132 if ((ref->check.state & CHK_ST_ENABLED) &&
4133 (ref->check.health < ref->check.rise + ref->check.fall - 1)) {
4134 state = SRV_STATS_STATE_UP_GOING_DOWN;
Willy Tarreauba2f2642016-01-07 09:54:40 +01004135 } else {
4136 state = SRV_STATS_STATE_UP;
Willy Tarreauba2f2642016-01-07 09:54:40 +01004137 }
4138
Willy Tarreauba2f2642016-01-07 09:54:40 +01004139 if (sv->admin & SRV_ADMF_DRAIN) {
4140 if (ref->agent.state & CHK_ST_ENABLED)
4141 state = SRV_STATS_STATE_DRAIN_AGENT;
4142 else if (state == SRV_STATS_STATE_UP_GOING_DOWN)
4143 state = SRV_STATS_STATE_DRAIN_GOING_DOWN;
4144 else
4145 state = SRV_STATS_STATE_DRAIN;
4146 }
4147
4148 if (state == SRV_STATS_STATE_UP && !(ref->check.state & CHK_ST_ENABLED)) {
4149 state = SRV_STATS_STATE_NO_CHECK;
Willy Tarreauba2f2642016-01-07 09:54:40 +01004150 }
4151 }
4152 else if (sv->state == SRV_ST_STOPPING) {
4153 if ((!(sv->check.state & CHK_ST_ENABLED) && !sv->track) ||
4154 (ref->check.health == ref->check.rise + ref->check.fall - 1)) {
4155 state = SRV_STATS_STATE_NOLB;
Willy Tarreauba2f2642016-01-07 09:54:40 +01004156 } else {
4157 state = SRV_STATS_STATE_NOLB_GOING_DOWN;
Willy Tarreauba2f2642016-01-07 09:54:40 +01004158 }
4159 }
4160 else { /* stopped */
4161 if ((ref->agent.state & CHK_ST_ENABLED) && !ref->agent.health) {
4162 state = SRV_STATS_STATE_DOWN_AGENT;
Willy Tarreauba2f2642016-01-07 09:54:40 +01004163 } else if ((ref->check.state & CHK_ST_ENABLED) && !ref->check.health) {
4164 state = SRV_STATS_STATE_DOWN; /* DOWN */
Willy Tarreauba2f2642016-01-07 09:54:40 +01004165 } else if ((ref->agent.state & CHK_ST_ENABLED) || (ref->check.state & CHK_ST_ENABLED)) {
4166 state = SRV_STATS_STATE_GOING_UP;
Willy Tarreauba2f2642016-01-07 09:54:40 +01004167 } else {
4168 state = SRV_STATS_STATE_DOWN; /* DOWN, unchecked */
Willy Tarreauba2f2642016-01-07 09:54:40 +01004169 }
4170 }
4171
Willy Tarreau2b96cf12016-01-06 19:25:38 +01004172 chunk_reset(out);
Willy Tarreau2b96cf12016-01-06 19:25:38 +01004173
4174 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
4175 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, sv->id);
Willy Tarreauf8211df2016-01-11 14:09:38 +01004176 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
Willy Tarreau2b96cf12016-01-06 19:25:38 +01004177 stats[ST_F_QCUR] = mkf_u32(0, sv->nbpend);
4178 stats[ST_F_QMAX] = mkf_u32(FN_MAX, sv->counters.nbpend_max);
4179 stats[ST_F_SCUR] = mkf_u32(0, sv->cur_sess);
4180 stats[ST_F_SMAX] = mkf_u32(FN_MAX, sv->counters.cur_sess_max);
4181
4182 if (sv->maxconn)
4183 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, sv->maxconn);
4184
4185 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, sv->counters.cum_sess);
4186 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, sv->counters.bytes_in);
4187 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, sv->counters.bytes_out);
4188 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, sv->counters.failed_secu);
4189 stats[ST_F_ECON] = mkf_u64(FN_COUNTER, sv->counters.failed_conns);
4190 stats[ST_F_ERESP] = mkf_u64(FN_COUNTER, sv->counters.failed_resp);
4191 stats[ST_F_WRETR] = mkf_u64(FN_COUNTER, sv->counters.retries);
4192 stats[ST_F_WREDIS] = mkf_u64(FN_COUNTER, sv->counters.redispatches);
4193
4194 /* status */
4195 fld_status = chunk_newstr(out);
4196 if (sv->admin & SRV_ADMF_IMAINT)
4197 chunk_appendf(out, "MAINT (via %s/%s)", via->proxy->id, via->id);
4198 else if (sv->admin & SRV_ADMF_MAINT)
4199 chunk_appendf(out, "MAINT");
4200 else
4201 chunk_appendf(out,
4202 srv_hlt_st[state],
4203 (ref->state != SRV_ST_STOPPED) ? (ref->check.health - ref->check.rise + 1) : (ref->check.health),
4204 (ref->state != SRV_ST_STOPPED) ? (ref->check.fall) : (ref->check.rise));
4205
4206 stats[ST_F_STATUS] = mkf_str(FO_STATUS, fld_status);
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004207 stats[ST_F_LASTCHG] = mkf_u32(FN_AGE, now.tv_sec - sv->last_change);
4208 stats[ST_F_WEIGHT] = mkf_u32(FN_AVG, (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv);
4209 stats[ST_F_ACT] = mkf_u32(FO_STATUS, (sv->flags & SRV_F_BACKUP) ? 0 : 1);
4210 stats[ST_F_BCK] = mkf_u32(FO_STATUS, (sv->flags & SRV_F_BACKUP) ? 1 : 0);
Willy Tarreau91861262007-10-17 17:06:05 +02004211
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004212 /* check failures: unique, fatal; last change, total downtime */
4213 if (sv->check.state & CHK_ST_ENABLED) {
4214 stats[ST_F_CHKFAIL] = mkf_u64(FN_COUNTER, sv->counters.failed_checks);
4215 stats[ST_F_CHKDOWN] = mkf_u64(FN_COUNTER, sv->counters.down_trans);
4216 stats[ST_F_DOWNTIME] = mkf_u32(FN_COUNTER, srv_downtime(sv));
4217 }
Willy Tarreau164d4a92016-01-06 19:48:21 +01004218
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004219 if (sv->maxqueue)
4220 stats[ST_F_QLIMIT] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->maxqueue);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004221
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004222 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
4223 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
4224 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, sv->puid);
Willy Tarreau91861262007-10-17 17:06:05 +02004225
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004226 if (sv->state == SRV_ST_STARTING && !server_is_draining(sv))
4227 stats[ST_F_THROTTLE] = mkf_u32(FN_AVG, server_throttle_rate(sv));
Willy Tarreauf5b1cc32014-06-17 12:20:59 +02004228
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004229 stats[ST_F_LBTOT] = mkf_u64(FN_COUNTER, sv->counters.cum_lbconn);
Willy Tarreau91861262007-10-17 17:06:05 +02004230
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004231 if (sv->track) {
4232 char *fld_track = chunk_newstr(out);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004233
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004234 chunk_appendf(out, "%s/%s", sv->track->proxy->id, sv->track->id);
4235 stats[ST_F_TRACKED] = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, fld_track);
4236 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004237
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004238 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_SV);
4239 stats[ST_F_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&sv->sess_per_sec));
4240 stats[ST_F_RATE_MAX] = mkf_u32(FN_MAX, sv->counters.sps_max);
Willy Tarreau164d4a92016-01-06 19:48:21 +01004241
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004242 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) {
4243 const char *fld_chksts;
Willy Tarreau164d4a92016-01-06 19:48:21 +01004244
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004245 fld_chksts = chunk_newstr(out);
4246 chunk_strcat(out, "* "); // for check in progress
4247 chunk_strcat(out, get_check_status_info(sv->check.status));
4248 if (!(sv->check.state & CHK_ST_INPROGRESS))
4249 fld_chksts += 2; // skip "* "
4250 stats[ST_F_CHECK_STATUS] = mkf_str(FN_OUTPUT, fld_chksts);
Willy Tarreau91861262007-10-17 17:06:05 +02004251
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004252 if (sv->check.status >= HCHK_STATUS_L57DATA)
4253 stats[ST_F_CHECK_CODE] = mkf_u32(FN_OUTPUT, sv->check.code);
Willy Tarreaucf2924b2014-05-23 12:15:15 +02004254
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004255 if (sv->check.status >= HCHK_STATUS_CHECKED)
4256 stats[ST_F_CHECK_DURATION] = mkf_u64(FN_DURATION, sv->check.duration);
Willy Tarreaucf2924b2014-05-23 12:15:15 +02004257
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004258 stats[ST_F_CHECK_DESC] = mkf_str(FN_OUTPUT, get_check_status_description(sv->check.status));
4259 stats[ST_F_LAST_CHK] = mkf_str(FN_OUTPUT, sv->check.desc);
4260 stats[ST_F_CHECK_RISE] = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.rise);
4261 stats[ST_F_CHECK_FALL] = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.fall);
4262 stats[ST_F_CHECK_HEALTH] = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.health);
4263 }
Willy Tarreaucf2924b2014-05-23 12:15:15 +02004264
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004265 if ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) {
4266 const char *fld_chksts;
Willy Tarreaudd7354b2016-01-08 13:47:26 +01004267
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004268 fld_chksts = chunk_newstr(out);
4269 chunk_strcat(out, "* "); // for check in progress
4270 chunk_strcat(out, get_check_status_info(sv->agent.status));
4271 if (!(sv->agent.state & CHK_ST_INPROGRESS))
4272 fld_chksts += 2; // skip "* "
4273 stats[ST_F_AGENT_STATUS] = mkf_str(FN_OUTPUT, fld_chksts);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004274
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004275 if (sv->agent.status >= HCHK_STATUS_L57DATA)
4276 stats[ST_F_AGENT_CODE] = mkf_u32(FN_OUTPUT, sv->agent.code);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004277
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004278 if (sv->agent.status >= HCHK_STATUS_CHECKED)
4279 stats[ST_F_AGENT_DURATION] = mkf_u64(FN_DURATION, sv->agent.duration);
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01004280
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004281 stats[ST_F_AGENT_DESC] = mkf_str(FN_OUTPUT, get_check_status_description(sv->agent.status));
4282 stats[ST_F_LAST_AGT] = mkf_str(FN_OUTPUT, sv->agent.desc);
4283 stats[ST_F_AGENT_RISE] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.rise);
4284 stats[ST_F_AGENT_FALL] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.fall);
4285 stats[ST_F_AGENT_HEALTH] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.health);
4286 }
Willy Tarreaudd7354b2016-01-08 13:47:26 +01004287
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004288 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
4289 if (px->mode == PR_MODE_HTTP) {
4290 stats[ST_F_HRSP_1XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[1]);
4291 stats[ST_F_HRSP_2XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[2]);
4292 stats[ST_F_HRSP_3XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[3]);
4293 stats[ST_F_HRSP_4XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[4]);
4294 stats[ST_F_HRSP_5XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[5]);
4295 stats[ST_F_HRSP_OTHER] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[0]);
4296 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004297
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004298 if (ref->observe)
4299 stats[ST_F_HANAFAIL] = mkf_u64(FN_COUNTER, sv->counters.failed_hana);
Willy Tarreau164d4a92016-01-06 19:48:21 +01004300
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004301 stats[ST_F_CLI_ABRT] = mkf_u64(FN_COUNTER, sv->counters.cli_aborts);
4302 stats[ST_F_SRV_ABRT] = mkf_u64(FN_COUNTER, sv->counters.srv_aborts);
4303 stats[ST_F_LASTSESS] = mkf_s32(FN_AGE, srv_lastsession(sv));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004304
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004305 stats[ST_F_QTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.q_time, TIME_STATS_SAMPLES));
4306 stats[ST_F_CTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.c_time, TIME_STATS_SAMPLES));
4307 stats[ST_F_RTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.d_time, TIME_STATS_SAMPLES));
4308 stats[ST_F_TTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.t_time, TIME_STATS_SAMPLES));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004309
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004310 if (flags & ST_SHLGNDS) {
4311 switch (addr_to_str(&sv->addr, str, sizeof(str))) {
4312 case AF_INET:
4313 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
4314 chunk_appendf(out, "%s:%d", str, get_host_port(&sv->addr));
4315 break;
4316 case AF_INET6:
4317 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
4318 chunk_appendf(out, "[%s]:%d", str, get_host_port(&sv->addr));
4319 break;
4320 case AF_UNIX:
4321 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, "unix");
4322 break;
4323 case -1:
4324 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
4325 chunk_strcat(out, strerror(errno));
4326 break;
4327 default: /* address family not supported */
4328 break;
Willy Tarreauf4659942013-11-28 10:50:06 +01004329 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004330
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004331 if (sv->cookie)
4332 stats[ST_F_COOKIE] = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, sv->cookie);
4333 }
4334
Thierry Fournier61fe6c02016-03-25 08:21:21 +01004335 return 1;
4336}
4337
4338/* Dumps a line for server <sv> and proxy <px> to the trash and uses the state
4339 * from stream interface <si>, stats flags <flags>, and server state <state>.
4340 * The caller is responsible for clearing the trash if needed. Returns non-zero
4341 * if it emits anything, zero otherwise.
4342 */
4343static int stats_dump_sv_stats(struct stream_interface *si, struct proxy *px, int flags, struct server *sv)
4344{
4345 struct appctx *appctx = __objt_appctx(si->end);
4346
4347 if (!stats_fill_sv_stats(px, sv, flags, stats, ST_F_TOTAL_FIELDS))
4348 return 0;
4349
Willy Tarreau501f6022016-01-08 17:37:22 +01004350 return stats_dump_one_line(stats, flags, px, appctx);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004351}
4352
Thierry Fournierd0a56c22016-03-25 08:21:51 +01004353/* Fill <stats> with the backend statistics. <stats> is
4354 * preallocated array of length <len>. The length of the array
4355 * must be at least ST_F_TOTAL_FIELDS. If this length is less
4356 * then this value, the function returns 0, otherwise, it
4357 * returns 1. <flags> can take the value ST_SHLGNDS.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004358 */
Thierry Fournierd0a56c22016-03-25 08:21:51 +01004359int stats_fill_be_stats(struct proxy *px, int flags, struct field *stats, int len)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004360{
Thierry Fournierd0a56c22016-03-25 08:21:51 +01004361 if (len < ST_F_TOTAL_FIELDS)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004362 return 0;
4363
Thierry Fournierd0a56c22016-03-25 08:21:51 +01004364 memset(stats, 0, sizeof(*stats) * len);
Willy Tarreauf6eecbe2016-01-06 16:14:50 +01004365
4366 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
4367 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, "BACKEND");
Willy Tarreauf8211df2016-01-11 14:09:38 +01004368 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
Willy Tarreauf6eecbe2016-01-06 16:14:50 +01004369 stats[ST_F_QCUR] = mkf_u32(0, px->nbpend);
4370 stats[ST_F_QMAX] = mkf_u32(FN_MAX, px->be_counters.nbpend_max);
4371 stats[ST_F_SCUR] = mkf_u32(FO_CONFIG|FN_LIMIT, px->beconn);
4372 stats[ST_F_SMAX] = mkf_u32(FN_MAX, px->be_counters.conn_max);
4373 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, px->fullconn);
4374 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, px->be_counters.cum_conn);
4375 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, px->be_counters.bytes_in);
4376 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, px->be_counters.bytes_out);
4377 stats[ST_F_DREQ] = mkf_u64(FN_COUNTER, px->be_counters.denied_req);
4378 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, px->be_counters.denied_resp);
4379 stats[ST_F_ECON] = mkf_u64(FN_COUNTER, px->be_counters.failed_conns);
4380 stats[ST_F_ERESP] = mkf_u64(FN_COUNTER, px->be_counters.failed_resp);
4381 stats[ST_F_WRETR] = mkf_u64(FN_COUNTER, px->be_counters.retries);
4382 stats[ST_F_WREDIS] = mkf_u64(FN_COUNTER, px->be_counters.redispatches);
4383 stats[ST_F_STATUS] = mkf_str(FO_STATUS, (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" : "DOWN");
4384 stats[ST_F_WEIGHT] = mkf_u32(FN_AVG, (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv);
4385 stats[ST_F_ACT] = mkf_u32(0, px->srv_act);
4386 stats[ST_F_BCK] = mkf_u32(0, px->srv_bck);
4387 stats[ST_F_CHKDOWN] = mkf_u64(FN_COUNTER, px->down_trans);
4388 stats[ST_F_LASTCHG] = mkf_u32(FN_AGE, now.tv_sec - px->last_change);
Willy Tarreau7344f472016-01-11 12:04:02 +01004389 if (px->srv)
4390 stats[ST_F_DOWNTIME] = mkf_u32(FN_COUNTER, be_downtime(px));
4391
Willy Tarreauf6eecbe2016-01-06 16:14:50 +01004392 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
4393 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
4394 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, 0);
4395 stats[ST_F_LBTOT] = mkf_u64(FN_COUNTER, px->be_counters.cum_lbconn);
4396 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_BE);
4397 stats[ST_F_RATE] = mkf_u32(0, read_freq_ctr(&px->be_sess_per_sec));
4398 stats[ST_F_RATE_MAX] = mkf_u32(0, px->be_counters.sps_max);
Willy Tarreauf1516d92016-01-11 14:48:36 +01004399
Willy Tarreaue4847c62016-01-08 15:43:54 +01004400 if (flags & ST_SHLGNDS) {
4401 if (px->cookie_name)
4402 stats[ST_F_COOKIE] = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, px->cookie_name);
Willy Tarreauf1516d92016-01-11 14:48:36 +01004403 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 +01004404 }
Willy Tarreauf6eecbe2016-01-06 16:14:50 +01004405
4406 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
4407 if (px->mode == PR_MODE_HTTP) {
4408 stats[ST_F_REQ_TOT] = mkf_u64(FN_COUNTER, px->be_counters.p.http.cum_req);
4409 stats[ST_F_HRSP_1XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[1]);
4410 stats[ST_F_HRSP_2XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[2]);
4411 stats[ST_F_HRSP_3XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[3]);
4412 stats[ST_F_HRSP_4XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[4]);
4413 stats[ST_F_HRSP_5XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[5]);
4414 stats[ST_F_HRSP_OTHER] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[0]);
Willy Tarreau5b9bdff2016-01-11 14:40:47 +01004415 stats[ST_F_INTERCEPTED] = mkf_u64(FN_COUNTER, px->be_counters.intercepted_req);
Willy Tarreauf6eecbe2016-01-06 16:14:50 +01004416 }
4417
4418 stats[ST_F_CLI_ABRT] = mkf_u64(FN_COUNTER, px->be_counters.cli_aborts);
4419 stats[ST_F_SRV_ABRT] = mkf_u64(FN_COUNTER, px->be_counters.srv_aborts);
4420
4421 /* compression: in, out, bypassed, responses */
4422 stats[ST_F_COMP_IN] = mkf_u64(FN_COUNTER, px->be_counters.comp_in);
4423 stats[ST_F_COMP_OUT] = mkf_u64(FN_COUNTER, px->be_counters.comp_out);
4424 stats[ST_F_COMP_BYP] = mkf_u64(FN_COUNTER, px->be_counters.comp_byp);
4425 stats[ST_F_COMP_RSP] = mkf_u64(FN_COUNTER, px->be_counters.p.http.comp_rsp);
4426 stats[ST_F_LASTSESS] = mkf_s32(FN_AGE, be_lastsession(px));
4427
4428 stats[ST_F_QTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.q_time, TIME_STATS_SAMPLES));
4429 stats[ST_F_CTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.c_time, TIME_STATS_SAMPLES));
4430 stats[ST_F_RTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.d_time, TIME_STATS_SAMPLES));
4431 stats[ST_F_TTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.t_time, TIME_STATS_SAMPLES));
4432
Thierry Fournierd0a56c22016-03-25 08:21:51 +01004433 return 1;
4434}
4435
4436/* Dumps a line for backend <px> to the trash for and uses the state from stream
4437 * interface <si> and stats flags <flags>. The caller is responsible for clearing
4438 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
4439 */
4440static int stats_dump_be_stats(struct stream_interface *si, struct proxy *px, int flags)
4441{
4442 struct appctx *appctx = __objt_appctx(si->end);
4443
4444 if (!(px->cap & PR_CAP_BE))
4445 return 0;
4446
4447 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_BE)))
4448 return 0;
4449
4450 if (!stats_fill_be_stats(px, flags, stats, ST_F_TOTAL_FIELDS))
4451 return 0;
4452
Willy Tarreau501f6022016-01-08 17:37:22 +01004453 return stats_dump_one_line(stats, flags, px, appctx);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004454}
4455
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004456/* Dumps the HTML table header for proxy <px> to the trash for and uses the state from
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004457 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004458 * for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004459 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004460static void stats_dump_html_px_hdr(struct stream_interface *si, struct proxy *px, struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004461{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004462 struct appctx *appctx = __objt_appctx(si->end);
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004463 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
4464
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004465 if (px->cap & PR_CAP_BE && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004466 /* A form to enable/disable this proxy servers */
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004467
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004468 /* 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 +02004469 scope_txt[0] = 0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004470 if (appctx->ctx.stats.scope_len) {
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004471 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau4e4292b2014-11-28 12:18:45 +01004472 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 +01004473 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004474 }
4475
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004476 chunk_appendf(&trash,
Jeff Buchbinder2dbbf4d2014-08-29 15:10:08 -05004477 "<form method=\"post\">");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004478 }
4479
4480 /* print a new table */
4481 chunk_appendf(&trash,
4482 "<table class=\"tbl\" width=\"100%%\">\n"
4483 "<tr class=\"titre\">"
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01004484 "<th class=\"pxname\" width=\"10%%\">");
4485
4486 chunk_appendf(&trash,
4487 "<a name=\"%s\"></a>%s"
4488 "<a class=px href=\"#%s\">%s</a>",
4489 px->id,
4490 (uri->flags & ST_SHLGNDS) ? "<u>":"",
4491 px->id, px->id);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004492
4493 if (uri->flags & ST_SHLGNDS) {
4494 /* cap, mode, id */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02004495 chunk_appendf(&trash, "<div class=tips>cap: %s, mode: %s, id: %d",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004496 proxy_cap_str(px->cap), proxy_mode_str(px->mode),
4497 px->uuid);
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01004498 chunk_appendf(&trash, "</div>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004499 }
4500
4501 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01004502 "%s</th>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004503 "<th class=\"%s\" width=\"90%%\">%s</th>"
4504 "</tr>\n"
4505 "</table>\n"
4506 "<table class=\"tbl\" width=\"100%%\">\n"
4507 "<tr class=\"titre\">",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004508 (uri->flags & ST_SHLGNDS) ? "</u>":"",
4509 px->desc ? "desc" : "empty", px->desc ? px->desc : "");
4510
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004511 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004512 /* Column heading for Enable or Disable server */
4513 chunk_appendf(&trash, "<th rowspan=2 width=1></th>");
Willy Tarreau91861262007-10-17 17:06:05 +02004514 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004515
4516 chunk_appendf(&trash,
4517 "<th rowspan=2></th>"
4518 "<th colspan=3>Queue</th>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05004519 "<th colspan=3>Session rate</th><th colspan=6>Sessions</th>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004520 "<th colspan=2>Bytes</th><th colspan=2>Denied</th>"
4521 "<th colspan=3>Errors</th><th colspan=2>Warnings</th>"
4522 "<th colspan=9>Server</th>"
4523 "</tr>\n"
4524 "<tr class=\"titre\">"
4525 "<th>Cur</th><th>Max</th><th>Limit</th>"
4526 "<th>Cur</th><th>Max</th><th>Limit</th><th>Cur</th><th>Max</th>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05004527 "<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 +01004528 "<th>Req</th><th>Resp</th><th>Req</th><th>Conn</th>"
4529 "<th>Resp</th><th>Retr</th><th>Redis</th>"
4530 "<th>Status</th><th>LastChk</th><th>Wght</th><th>Act</th>"
4531 "<th>Bck</th><th>Chk</th><th>Dwn</th><th>Dwntme</th>"
4532 "<th>Thrtle</th>\n"
4533 "</tr>");
Willy Tarreau91861262007-10-17 17:06:05 +02004534}
4535
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004536/* Dumps the HTML table trailer for proxy <px> to the trash for and uses the state from
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004537 * stream interface <si>. The caller is responsible for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004538 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004539static void stats_dump_html_px_end(struct stream_interface *si, struct proxy *px)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004540{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004541 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004542 chunk_appendf(&trash, "</table>");
4543
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004544 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004545 /* close the form used to enable/disable this proxy servers */
4546 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004547 "Choose the action to perform on the checked servers : "
4548 "<select name=action>"
4549 "<option value=\"\"></option>"
Willy Tarreaued7df902014-05-22 18:04:49 +02004550 "<option value=\"ready\">Set state to READY</option>"
4551 "<option value=\"drain\">Set state to DRAIN</option>"
Marco Corte8c27bca2014-07-02 17:49:34 +02004552 "<option value=\"maint\">Set state to MAINT</option>"
Willy Tarreau248a60e2014-05-23 14:59:48 +02004553 "<option value=\"dhlth\">Health: disable checks</option>"
4554 "<option value=\"ehlth\">Health: enable checks</option>"
4555 "<option value=\"hrunn\">Health: force UP</option>"
4556 "<option value=\"hnolb\">Health: force NOLB</option>"
4557 "<option value=\"hdown\">Health: force DOWN</option>"
4558 "<option value=\"dagent\">Agent: disable checks</option>"
4559 "<option value=\"eagent\">Agent: enable checks</option>"
4560 "<option value=\"arunn\">Agent: force UP</option>"
4561 "<option value=\"adown\">Agent: force DOWN</option>"
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004562 "<option value=\"shutdown\">Kill Sessions</option>"
4563 "</select>"
4564 "<input type=\"hidden\" name=\"b\" value=\"#%d\">"
4565 "&nbsp;<input type=\"submit\" value=\"Apply\">"
4566 "</form>",
4567 px->uuid);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004568 }
4569
4570 chunk_appendf(&trash, "<p>\n");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004571}
Willy Tarreau91861262007-10-17 17:06:05 +02004572
4573/*
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004574 * Dumps statistics for a proxy. The output is sent to the stream interface's
4575 * input buffer. Returns 0 if it had to stop dumping data because of lack of
4576 * buffer space, or non-zero if everything completed. This function is used
4577 * both by the CLI and the HTTP entry points, and is able to dump the output
4578 * in HTML or CSV formats. If the later, <uri> must be NULL.
Willy Tarreau91861262007-10-17 17:06:05 +02004579 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004580static int stats_dump_proxy_to_buffer(struct stream_interface *si, struct proxy *px, struct uri_auth *uri)
Willy Tarreau91861262007-10-17 17:06:05 +02004581{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004582 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau87b09662015-04-03 00:22:06 +02004583 struct stream *s = si_strm(si);
Willy Tarreau2bb4a962014-11-28 11:11:05 +01004584 struct channel *rep = si_ic(si);
Willy Tarreau44267702011-10-28 15:35:33 +02004585 struct server *sv, *svs; /* server and server-state, server-state=server or server->track */
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004586 struct listener *l;
Willy Tarreau0deb85a2016-01-08 15:33:18 +01004587 unsigned int flags;
4588
4589 if (uri)
4590 flags = uri->flags;
4591 else if (strm_li(s)->bind_conf->level >= ACCESS_LVL_OPER)
4592 flags = ST_SHLGNDS | ST_SHNODE | ST_SHDESC;
4593 else
4594 flags = ST_SHNODE | ST_SHDESC;
Willy Tarreau91861262007-10-17 17:06:05 +02004595
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004596 chunk_reset(&trash);
Willy Tarreau91861262007-10-17 17:06:05 +02004597
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004598 switch (appctx->ctx.stats.px_st) {
Willy Tarreau295a8372011-03-10 11:25:07 +01004599 case STAT_PX_ST_INIT:
Willy Tarreau91861262007-10-17 17:06:05 +02004600 /* we are on a new proxy */
Willy Tarreau91861262007-10-17 17:06:05 +02004601 if (uri && uri->scope) {
4602 /* we have a limited scope, we have to check the proxy name */
4603 struct stat_scope *scope;
4604 int len;
4605
4606 len = strlen(px->id);
4607 scope = uri->scope;
4608
4609 while (scope) {
4610 /* match exact proxy name */
4611 if (scope->px_len == len && !memcmp(px->id, scope->px_id, len))
4612 break;
4613
4614 /* match '.' which means 'self' proxy */
Willy Tarreau1388a3a2007-10-18 16:38:37 +02004615 if (!strcmp(scope->px_id, ".") && px == s->be)
Willy Tarreau91861262007-10-17 17:06:05 +02004616 break;
4617 scope = scope->next;
4618 }
4619
4620 /* proxy name not found : don't dump anything */
4621 if (scope == NULL)
4622 return 1;
4623 }
4624
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004625 /* if the user has requested a limited output and the proxy
4626 * name does not match, skip it.
4627 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004628 if (appctx->ctx.stats.scope_len &&
Willy Tarreau4e4292b2014-11-28 12:18:45 +01004629 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 +02004630 return 1;
4631
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004632 if ((appctx->ctx.stats.flags & STAT_BOUND) &&
4633 (appctx->ctx.stats.iid != -1) &&
4634 (px->uuid != appctx->ctx.stats.iid))
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01004635 return 1;
4636
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004637 appctx->ctx.stats.px_st = STAT_PX_ST_TH;
Willy Tarreau91861262007-10-17 17:06:05 +02004638 /* fall through */
4639
Willy Tarreau295a8372011-03-10 11:25:07 +01004640 case STAT_PX_ST_TH:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004641 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004642 stats_dump_html_px_hdr(si, px, uri);
Willy Tarreaubc18da12015-03-13 14:00:47 +01004643 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004644 si_applet_cant_put(si);
Willy Tarreau55bb8452007-10-17 18:44:57 +02004645 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01004646 }
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004647 }
Willy Tarreau91861262007-10-17 17:06:05 +02004648
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004649 appctx->ctx.stats.px_st = STAT_PX_ST_FE;
Willy Tarreau91861262007-10-17 17:06:05 +02004650 /* fall through */
4651
Willy Tarreau295a8372011-03-10 11:25:07 +01004652 case STAT_PX_ST_FE:
Willy Tarreau91861262007-10-17 17:06:05 +02004653 /* print the frontend */
Willy Tarreaubc18da12015-03-13 14:00:47 +01004654 if (stats_dump_fe_stats(si, px)) {
4655 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004656 si_applet_cant_put(si);
Willy Tarreau91861262007-10-17 17:06:05 +02004657 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01004658 }
4659 }
Willy Tarreau91861262007-10-17 17:06:05 +02004660
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004661 appctx->ctx.stats.l = px->conf.listeners.n;
4662 appctx->ctx.stats.px_st = STAT_PX_ST_LI;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004663 /* fall through */
4664
Willy Tarreau295a8372011-03-10 11:25:07 +01004665 case STAT_PX_ST_LI:
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004666 /* stats.l has been initialized above */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004667 for (; appctx->ctx.stats.l != &px->conf.listeners; appctx->ctx.stats.l = l->by_fe.n) {
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01004668 if (buffer_almost_full(rep->buf)) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004669 si_applet_cant_put(si);
Willy Tarreau4e33d862009-10-11 23:35:10 +02004670 return 0;
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01004671 }
Willy Tarreau4e33d862009-10-11 23:35:10 +02004672
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004673 l = LIST_ELEM(appctx->ctx.stats.l, struct listener *, by_fe);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004674 if (!l->counters)
4675 continue;
4676
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004677 if (appctx->ctx.stats.flags & STAT_BOUND) {
4678 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SO)))
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004679 break;
4680
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004681 if (appctx->ctx.stats.sid != -1 && l->luid != appctx->ctx.stats.sid)
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004682 continue;
4683 }
4684
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004685 /* print the frontend */
Willy Tarreau0deb85a2016-01-08 15:33:18 +01004686 if (stats_dump_li_stats(si, px, l, flags)) {
Willy Tarreaubc18da12015-03-13 14:00:47 +01004687 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004688 si_applet_cant_put(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004689 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01004690 }
4691 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004692 }
4693
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004694 appctx->ctx.stats.sv = px->srv; /* may be NULL */
4695 appctx->ctx.stats.px_st = STAT_PX_ST_SV;
Willy Tarreau91861262007-10-17 17:06:05 +02004696 /* fall through */
4697
Willy Tarreau295a8372011-03-10 11:25:07 +01004698 case STAT_PX_ST_SV:
Willy Tarreau91861262007-10-17 17:06:05 +02004699 /* stats.sv has been initialized above */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004700 for (; appctx->ctx.stats.sv != NULL; appctx->ctx.stats.sv = sv->next) {
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01004701 if (buffer_almost_full(rep->buf)) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004702 si_applet_cant_put(si);
Willy Tarreau4e33d862009-10-11 23:35:10 +02004703 return 0;
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01004704 }
Willy Tarreau4e33d862009-10-11 23:35:10 +02004705
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004706 sv = appctx->ctx.stats.sv;
Willy Tarreau91861262007-10-17 17:06:05 +02004707
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004708 if (appctx->ctx.stats.flags & STAT_BOUND) {
4709 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SV)))
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01004710 break;
4711
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004712 if (appctx->ctx.stats.sid != -1 && sv->puid != appctx->ctx.stats.sid)
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01004713 continue;
4714 }
4715
Willy Tarreau32091232014-05-16 13:52:00 +02004716 svs = sv;
4717 while (svs->track)
4718 svs = svs->track;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01004719
Willy Tarreauba2f2642016-01-07 09:54:40 +01004720 /* do not report servers which are DOWN and not changing state */
4721 if ((appctx->ctx.stats.flags & STAT_HIDE_DOWN) &&
4722 ((sv->admin & SRV_ADMF_MAINT) || /* server is in maintenance */
4723 (sv->state == SRV_ST_STOPPED && /* server is down */
4724 (!((svs->agent.state | svs->check.state) & CHK_ST_ENABLED) ||
4725 ((svs->agent.state & CHK_ST_ENABLED) && !svs->agent.health) ||
4726 ((svs->check.state & CHK_ST_ENABLED) && !svs->check.health))))) {
Willy Tarreau91861262007-10-17 17:06:05 +02004727 continue;
4728 }
4729
Willy Tarreau0deb85a2016-01-08 15:33:18 +01004730 if (stats_dump_sv_stats(si, px, flags, sv)) {
Willy Tarreaubc18da12015-03-13 14:00:47 +01004731 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004732 si_applet_cant_put(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004733 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01004734 }
4735 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004736 } /* for sv */
Cyril Bonté474be412010-10-12 00:14:36 +02004737
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004738 appctx->ctx.stats.px_st = STAT_PX_ST_BE;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004739 /* fall through */
Cyril Bonté70be45d2010-10-12 00:14:35 +02004740
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004741 case STAT_PX_ST_BE:
4742 /* print the backend */
Willy Tarreau0deb85a2016-01-08 15:33:18 +01004743 if (stats_dump_be_stats(si, px, flags)) {
Willy Tarreaubc18da12015-03-13 14:00:47 +01004744 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004745 si_applet_cant_put(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004746 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01004747 }
4748 }
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01004749
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004750 appctx->ctx.stats.px_st = STAT_PX_ST_END;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004751 /* fall through */
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01004752
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004753 case STAT_PX_ST_END:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004754 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004755 stats_dump_html_px_end(si, px);
Willy Tarreaubc18da12015-03-13 14:00:47 +01004756 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004757 si_applet_cant_put(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004758 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01004759 }
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004760 }
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01004761
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004762 appctx->ctx.stats.px_st = STAT_PX_ST_FIN;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004763 /* fall through */
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01004764
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004765 case STAT_PX_ST_FIN:
4766 return 1;
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01004767
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004768 default:
4769 /* unknown state, we should put an abort() here ! */
4770 return 1;
4771 }
4772}
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01004773
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004774/* Dumps the HTTP stats head block to the trash for and uses the per-uri
4775 * parameters <uri>. The caller is responsible for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004776 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004777static void stats_dump_html_head(struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004778{
4779 /* WARNING! This must fit in the first buffer !!! */
4780 chunk_appendf(&trash,
4781 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n"
4782 "\"http://www.w3.org/TR/html4/loose.dtd\">\n"
4783 "<html><head><title>Statistics Report for " PRODUCT_NAME "%s%s</title>\n"
4784 "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n"
4785 "<style type=\"text/css\"><!--\n"
4786 "body {"
4787 " font-family: arial, helvetica, sans-serif;"
4788 " font-size: 12px;"
4789 " font-weight: normal;"
4790 " color: black;"
4791 " background: white;"
4792 "}\n"
4793 "th,td {"
4794 " font-size: 10px;"
4795 "}\n"
4796 "h1 {"
4797 " font-size: x-large;"
4798 " margin-bottom: 0.5em;"
4799 "}\n"
4800 "h2 {"
4801 " font-family: helvetica, arial;"
4802 " font-size: x-large;"
4803 " font-weight: bold;"
4804 " font-style: italic;"
4805 " color: #6020a0;"
4806 " margin-top: 0em;"
4807 " margin-bottom: 0em;"
4808 "}\n"
4809 "h3 {"
4810 " font-family: helvetica, arial;"
4811 " font-size: 16px;"
4812 " font-weight: bold;"
4813 " color: #b00040;"
4814 " background: #e8e8d0;"
4815 " margin-top: 0em;"
4816 " margin-bottom: 0em;"
4817 "}\n"
4818 "li {"
4819 " margin-top: 0.25em;"
4820 " margin-right: 2em;"
4821 "}\n"
4822 ".hr {margin-top: 0.25em;"
4823 " border-color: black;"
4824 " border-bottom-style: solid;"
4825 "}\n"
4826 ".titre {background: #20D0D0;color: #000000; font-weight: bold; text-align: center;}\n"
4827 ".total {background: #20D0D0;color: #ffff80;}\n"
4828 ".frontend {background: #e8e8d0;}\n"
4829 ".socket {background: #d0d0d0;}\n"
4830 ".backend {background: #e8e8d0;}\n"
Simon Horman837bfa72015-04-23 14:51:27 +09004831 ".active_down {background: #ff9090;}\n"
4832 ".active_going_up {background: #ffd020;}\n"
4833 ".active_going_down {background: #ffffa0;}\n"
4834 ".active_up {background: #c0ffc0;}\n"
4835 ".active_nolb {background: #20a0ff;}\n"
4836 ".active_draining {background: #20a0FF;}\n"
4837 ".active_no_check {background: #e0e0e0;}\n"
4838 ".backup_down {background: #ff9090;}\n"
4839 ".backup_going_up {background: #ff80ff;}\n"
4840 ".backup_going_down {background: #c060ff;}\n"
4841 ".backup_up {background: #b0d0ff;}\n"
4842 ".backup_nolb {background: #90b0e0;}\n"
4843 ".backup_draining {background: #cc9900;}\n"
4844 ".backup_no_check {background: #e0e0e0;}\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004845 ".maintain {background: #c07820;}\n"
4846 ".rls {letter-spacing: 0.2em; margin-right: 1px;}\n" /* right letter spacing (used for grouping digits) */
4847 "\n"
4848 "a.px:link {color: #ffff40; text-decoration: none;}"
4849 "a.px:visited {color: #ffff40; text-decoration: none;}"
4850 "a.px:hover {color: #ffffff; text-decoration: none;}"
4851 "a.lfsb:link {color: #000000; text-decoration: none;}"
4852 "a.lfsb:visited {color: #000000; text-decoration: none;}"
4853 "a.lfsb:hover {color: #505050; text-decoration: none;}"
4854 "\n"
4855 "table.tbl { border-collapse: collapse; border-style: none;}\n"
4856 "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"
4857 "table.tbl td.ac { text-align: center;}\n"
4858 "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}\n"
4859 "table.tbl th.pxname { background: #b00040; color: #ffff40; font-weight: bold; border-style: solid solid none solid; padding: 2px 3px; white-space: nowrap;}\n"
4860 "table.tbl th.empty { border-style: none; empty-cells: hide; background: white;}\n"
4861 "table.tbl th.desc { background: white; border-style: solid solid none solid; text-align: left; padding: 2px 3px;}\n"
4862 "\n"
4863 "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n"
4864 "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n"
4865 "table.lgd td.noborder { border-style: none; padding: 2px; white-space: nowrap;}\n"
Willy Tarreau466c9b52012-12-23 02:25:03 +01004866 "table.det { border-collapse: collapse; border-style: none; }\n"
4867 "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 +01004868 "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 +01004869 "u {text-decoration:none; border-bottom: 1px dotted black;}\n"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02004870 "div.tips {\n"
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01004871 " display:block;\n"
4872 " visibility:hidden;\n"
4873 " z-index:2147483647;\n"
4874 " position:absolute;\n"
4875 " padding:2px 4px 3px;\n"
4876 " background:#f0f060; color:#000000;\n"
4877 " border:1px solid #7040c0;\n"
4878 " white-space:nowrap;\n"
4879 " font-style:normal;font-size:11px;font-weight:normal;\n"
4880 " -moz-border-radius:3px;-webkit-border-radius:3px;border-radius:3px;\n"
4881 " -moz-box-shadow:gray 2px 2px 3px;-webkit-box-shadow:gray 2px 2px 3px;box-shadow:gray 2px 2px 3px;\n"
4882 "}\n"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02004883 "u:hover div.tips {visibility:visible;}\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004884 "-->\n"
4885 "</style></head>\n",
4886 (uri->flags & ST_SHNODE) ? " on " : "",
4887 (uri->flags & ST_SHNODE) ? (uri->node ? uri->node : global.node) : ""
4888 );
4889}
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01004890
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004891/* Dumps the HTML stats information block to the trash for and uses the state from
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004892 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004893 * for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004894 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004895static void stats_dump_html_info(struct stream_interface *si, struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004896{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004897 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004898 unsigned int up = (now.tv_sec - start_date.tv_sec);
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004899 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01004900
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004901 /* WARNING! this has to fit the first packet too.
4902 * We are around 3.5 kB, add adding entries will
4903 * become tricky if we want to support 4kB buffers !
4904 */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004905 chunk_appendf(&trash,
4906 "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">"
4907 PRODUCT_NAME "%s</a></h1>\n"
4908 "<h2>Statistics Report for pid %d%s%s%s%s</h2>\n"
4909 "<hr width=\"100%%\" class=\"hr\">\n"
4910 "<h3>&gt; General process information</h3>\n"
4911 "<table border=0><tr><td align=\"left\" nowrap width=\"1%%\">\n"
4912 "<p><b>pid = </b> %d (process #%d, nbproc = %d)<br>\n"
4913 "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n"
4914 "<b>system limits:</b> memmax = %s%s; ulimit-n = %d<br>\n"
4915 "<b>maxsock = </b> %d; <b>maxconn = </b> %d; <b>maxpipes = </b> %d<br>\n"
4916 "current conns = %d; current pipes = %d/%d; conn rate = %d/sec<br>\n"
4917 "Running tasks: %d/%d; idle = %d %%<br>\n"
4918 "</td><td align=\"center\" nowrap>\n"
4919 "<table class=\"lgd\"><tr>\n"
Simon Horman837bfa72015-04-23 14:51:27 +09004920 "<td class=\"active_up\">&nbsp;</td><td class=\"noborder\">active UP </td>"
4921 "<td class=\"backup_up\">&nbsp;</td><td class=\"noborder\">backup UP </td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004922 "</tr><tr>\n"
Simon Horman837bfa72015-04-23 14:51:27 +09004923 "<td class=\"active_going_down\"></td><td class=\"noborder\">active UP, going down </td>"
4924 "<td class=\"backup_going_down\"></td><td class=\"noborder\">backup UP, going down </td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004925 "</tr><tr>\n"
Simon Horman837bfa72015-04-23 14:51:27 +09004926 "<td class=\"active_going_up\"></td><td class=\"noborder\">active DOWN, going up </td>"
4927 "<td class=\"backup_going_up\"></td><td class=\"noborder\">backup DOWN, going up </td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004928 "</tr><tr>\n"
Simon Horman837bfa72015-04-23 14:51:27 +09004929 "<td class=\"active_down\"></td><td class=\"noborder\">active or backup DOWN &nbsp;</td>"
4930 "<td class=\"active_no_check\"></td><td class=\"noborder\">not checked </td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004931 "</tr><tr>\n"
4932 "<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 -07004933 "</tr><tr>\n"
Simon Horman837bfa72015-04-23 14:51:27 +09004934 "<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 +01004935 "</tr></table>\n"
Willy Tarreau6b7764a2013-12-04 00:43:21 +01004936 "Note: \"NOLB\"/\"DRAIN\" = UP with load-balancing disabled."
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004937 "</td>"
4938 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
4939 "<b>Display option:</b><ul style=\"margin-top: 0.25em;\">"
4940 "",
4941 (uri->flags & ST_HIDEVER) ? "" : (STATS_VERSION_STRING),
4942 pid, (uri->flags & ST_SHNODE) ? " on " : "",
4943 (uri->flags & ST_SHNODE) ? (uri->node ? uri->node : global.node) : "",
4944 (uri->flags & ST_SHDESC) ? ": " : "",
4945 (uri->flags & ST_SHDESC) ? (uri->desc ? uri->desc : global.desc) : "",
4946 pid, relative_pid, global.nbproc,
4947 up / 86400, (up % 86400) / 3600,
4948 (up % 3600) / 60, (up % 60),
4949 global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited",
4950 global.rlimit_memmax ? " MB" : "",
4951 global.rlimit_nofile,
4952 global.maxsock, global.maxconn, global.maxpipes,
4953 actconn, pipes_used, pipes_used+pipes_free, read_freq_ctr(&global.conn_per_sec),
4954 run_queue_cur, nb_tasks_cur, idle_pct
4955 );
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004956
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004957 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Willy Tarreau4e4292b2014-11-28 12:18:45 +01004958 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 +01004959 scope_txt[appctx->ctx.stats.scope_len] = '\0';
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004960
4961 chunk_appendf(&trash,
Jeff Buchbinder2dbbf4d2014-08-29 15:10:08 -05004962 "<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 +01004963 (appctx->ctx.stats.scope_len > 0) ? scope_txt : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004964 STAT_SCOPE_TXT_MAXLEN);
4965
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004966 /* 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 +02004967 scope_txt[0] = 0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004968 if (appctx->ctx.stats.scope_len) {
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004969 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau4e4292b2014-11-28 12:18:45 +01004970 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 +01004971 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004972 }
4973
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004974 if (appctx->ctx.stats.flags & STAT_HIDE_DOWN)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004975 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004976 "<li><a href=\"%s%s%s%s\">Show all servers</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004977 uri->uri_prefix,
4978 "",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004979 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004980 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004981 else
4982 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004983 "<li><a href=\"%s%s%s%s\">Hide 'DOWN' servers</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004984 uri->uri_prefix,
4985 ";up",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004986 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004987 scope_txt);
Willy Tarreau91861262007-10-17 17:06:05 +02004988
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004989 if (uri->refresh > 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004990 if (appctx->ctx.stats.flags & STAT_NO_REFRESH)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004991 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004992 "<li><a href=\"%s%s%s%s\">Enable refresh</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004993 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004994 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004995 "",
4996 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004997 else
4998 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004999 "<li><a href=\"%s%s%s%s\">Disable refresh</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005000 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005001 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02005002 ";norefresh",
5003 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005004 }
Willy Tarreau55bb8452007-10-17 18:44:57 +02005005
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005006 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02005007 "<li><a href=\"%s%s%s%s\">Refresh now</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005008 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005009 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
5010 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02005011 scope_txt);
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02005012
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005013 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02005014 "<li><a href=\"%s;csv%s%s\">CSV export</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005015 uri->uri_prefix,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02005016 (uri->refresh > 0) ? ";norefresh" : "",
5017 scope_txt);
Willy Tarreau4bab24d2007-11-30 18:16:29 +01005018
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005019 chunk_appendf(&trash,
5020 "</ul></td>"
5021 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
5022 "<b>External resources:</b><ul style=\"margin-top: 0.25em;\">\n"
5023 "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>\n"
5024 "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>\n"
5025 "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>\n"
5026 "</ul>"
5027 "</td>"
5028 "</tr></table>\n"
5029 ""
5030 );
Willy Tarreau4bab24d2007-11-30 18:16:29 +01005031
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005032 if (appctx->ctx.stats.st_code) {
5033 switch (appctx->ctx.stats.st_code) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005034 case STAT_STATUS_DONE:
5035 chunk_appendf(&trash,
Simon Horman837bfa72015-04-23 14:51:27 +09005036 "<p><div class=active_up>"
Willy Tarreauba6be982013-04-19 12:16:55 +02005037 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005038 "Action processed successfully."
Willy Tarreauba6be982013-04-19 12:16:55 +02005039 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005040 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
5041 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02005042 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005043 break;
5044 case STAT_STATUS_NONE:
5045 chunk_appendf(&trash,
Simon Horman837bfa72015-04-23 14:51:27 +09005046 "<p><div class=active_going_down>"
Willy Tarreauba6be982013-04-19 12:16:55 +02005047 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005048 "Nothing has changed."
Willy Tarreauba6be982013-04-19 12:16:55 +02005049 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005050 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
5051 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02005052 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005053 break;
5054 case STAT_STATUS_PART:
5055 chunk_appendf(&trash,
Simon Horman837bfa72015-04-23 14:51:27 +09005056 "<p><div class=active_going_down>"
Willy Tarreauba6be982013-04-19 12:16:55 +02005057 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005058 "Action partially processed.<br>"
5059 "Some server names are probably unknown or ambiguous (duplicated names in the backend)."
Willy Tarreauba6be982013-04-19 12:16:55 +02005060 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005061 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
5062 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02005063 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005064 break;
5065 case STAT_STATUS_ERRP:
5066 chunk_appendf(&trash,
Simon Horman837bfa72015-04-23 14:51:27 +09005067 "<p><div class=active_down>"
Willy Tarreauba6be982013-04-19 12:16:55 +02005068 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005069 "Action not processed because of invalid parameters."
5070 "<ul>"
5071 "<li>The action is maybe unknown.</li>"
5072 "<li>The backend name is probably unknown or ambiguous (duplicated names).</li>"
5073 "<li>Some server names are probably unknown or ambiguous (duplicated names in the backend).</li>"
5074 "</ul>"
Willy Tarreauba6be982013-04-19 12:16:55 +02005075 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005076 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
5077 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02005078 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005079 break;
5080 case STAT_STATUS_EXCD:
5081 chunk_appendf(&trash,
Simon Horman837bfa72015-04-23 14:51:27 +09005082 "<p><div class=active_down>"
Willy Tarreauba6be982013-04-19 12:16:55 +02005083 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005084 "<b>Action not processed : the buffer couldn't store all the data.<br>"
5085 "You should retry with less servers at a time.</b>"
Willy Tarreauba6be982013-04-19 12:16:55 +02005086 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005087 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
5088 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02005089 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005090 break;
5091 case STAT_STATUS_DENY:
5092 chunk_appendf(&trash,
Simon Horman837bfa72015-04-23 14:51:27 +09005093 "<p><div class=active_down>"
Willy Tarreauba6be982013-04-19 12:16:55 +02005094 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005095 "<b>Action denied.</b>"
Willy Tarreauba6be982013-04-19 12:16:55 +02005096 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005097 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
5098 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02005099 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005100 break;
5101 default:
5102 chunk_appendf(&trash,
Simon Horman837bfa72015-04-23 14:51:27 +09005103 "<p><div class=active_no_check>"
Willy Tarreauba6be982013-04-19 12:16:55 +02005104 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005105 "Unexpected result."
Willy Tarreauba6be982013-04-19 12:16:55 +02005106 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005107 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
5108 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02005109 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005110 }
5111 chunk_appendf(&trash, "<p>\n");
5112 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005113}
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01005114
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01005115/* Dumps the HTML stats trailer block to the trash. The caller is responsible
5116 * for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005117 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01005118static void stats_dump_html_end()
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005119{
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005120 chunk_appendf(&trash, "</body></html>\n");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005121}
Willy Tarreau7f062c42009-03-05 18:43:00 +01005122
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01005123/* This function dumps statistics onto the stream interface's read buffer in
5124 * either CSV or HTML format. <uri> contains some HTML-specific parameters that
Willy Tarreau306f8302013-07-08 15:53:06 +02005125 * are ignored for CSV format (hence <uri> may be NULL there). It returns 0 if
5126 * 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 +02005127 * and the stream must be closed, or -1 in case of any error. This function is
Willy Tarreau306f8302013-07-08 15:53:06 +02005128 * used by both the CLI and the HTTP handlers.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005129 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01005130static int stats_dump_stat_to_buffer(struct stream_interface *si, struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005131{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005132 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005133 struct channel *rep = si_ic(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005134 struct proxy *px;
Willy Tarreau7f062c42009-03-05 18:43:00 +01005135
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005136 chunk_reset(&trash);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02005137
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005138 switch (appctx->st2) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005139 case STAT_ST_INIT:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005140 appctx->st2 = STAT_ST_HEAD; /* let's start producing data */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005141 /* fall through */
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005142
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005143 case STAT_ST_HEAD:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005144 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01005145 stats_dump_html_head(uri);
Willy Tarreau1e62df92016-01-11 18:57:53 +01005146 else if (!(appctx->ctx.stats.flags & STAT_FMT_TYPED))
Willy Tarreau354898b2012-12-23 18:15:23 +01005147 stats_dump_csv_header();
Willy Tarreaud9b587f2010-02-26 10:05:55 +01005148
Willy Tarreaubc18da12015-03-13 14:00:47 +01005149 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02005150 si_applet_cant_put(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005151 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01005152 }
Willy Tarreauae526782010-03-04 20:34:23 +01005153
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005154 appctx->st2 = STAT_ST_INFO;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005155 /* fall through */
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01005156
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005157 case STAT_ST_INFO:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005158 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01005159 stats_dump_html_info(si, uri);
Willy Tarreaubc18da12015-03-13 14:00:47 +01005160 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02005161 si_applet_cant_put(si);
Willy Tarreau91861262007-10-17 17:06:05 +02005162 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01005163 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005164 }
Willy Tarreau91861262007-10-17 17:06:05 +02005165
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005166 appctx->ctx.stats.px = proxy;
5167 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
5168 appctx->st2 = STAT_ST_LIST;
Willy Tarreau91861262007-10-17 17:06:05 +02005169 /* fall through */
5170
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005171 case STAT_ST_LIST:
5172 /* dump proxies */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005173 while (appctx->ctx.stats.px) {
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01005174 if (buffer_almost_full(rep->buf)) {
Willy Tarreaufe127932015-04-21 19:23:39 +02005175 si_applet_cant_put(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005176 return 0;
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01005177 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005178
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005179 px = appctx->ctx.stats.px;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005180 /* skip the disabled proxies, global frontend and non-networked ones */
5181 if (px->state != PR_STSTOPPED && px->uuid > 0 && (px->cap & (PR_CAP_FE | PR_CAP_BE)))
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01005182 if (stats_dump_proxy_to_buffer(si, px, uri) == 0)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005183 return 0;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005184
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005185 appctx->ctx.stats.px = px->next;
5186 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005187 }
5188 /* here, we just have reached the last proxy */
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005189
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005190 appctx->st2 = STAT_ST_END;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005191 /* fall through */
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005192
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005193 case STAT_ST_END:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005194 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01005195 stats_dump_html_end();
Willy Tarreaubc18da12015-03-13 14:00:47 +01005196 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02005197 si_applet_cant_put(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005198 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01005199 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005200 }
Willy Tarreau55058a72012-11-21 08:27:21 +01005201
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005202 appctx->st2 = STAT_ST_FIN;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005203 /* fall through */
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02005204
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005205 case STAT_ST_FIN:
5206 return 1;
Willy Tarreau55058a72012-11-21 08:27:21 +01005207
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005208 default:
5209 /* unknown state ! */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005210 appctx->st2 = STAT_ST_FIN;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005211 return -1;
5212 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005213}
Willy Tarreauae526782010-03-04 20:34:23 +01005214
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005215/* We reached the stats page through a POST request. The appctx is
5216 * expected to have already been allocated by the caller.
Willy Tarreau347a35d2013-11-22 17:51:09 +01005217 * Parse the posted data and enable/disable servers if necessary.
5218 * Returns 1 if request was parsed or zero if it needs more data.
5219 */
5220static int stats_process_http_post(struct stream_interface *si)
5221{
Willy Tarreau87b09662015-04-03 00:22:06 +02005222 struct stream *s = si_strm(si);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005223 struct appctx *appctx = objt_appctx(si->end);
Willy Tarreau347a35d2013-11-22 17:51:09 +01005224
5225 struct proxy *px = NULL;
5226 struct server *sv = NULL;
5227
5228 char key[LINESIZE];
5229 int action = ST_ADM_ACTION_NONE;
5230 int reprocess = 0;
5231
5232 int total_servers = 0;
5233 int altered_servers = 0;
5234
5235 char *first_param, *cur_param, *next_param, *end_params;
5236 char *st_cur_param = NULL;
5237 char *st_next_param = NULL;
5238
5239 struct chunk *temp;
5240 int reql;
5241
5242 temp = get_trash_chunk();
Willy Tarreaueee5b512015-04-03 23:46:31 +02005243 if (temp->size < s->txn->req.body_len) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01005244 /* too large request */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005245 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005246 goto out;
5247 }
5248
Willy Tarreaueee5b512015-04-03 23:46:31 +02005249 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 +01005250 if (reql <= 0) {
5251 /* we need more data */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005252 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005253 return 0;
5254 }
5255
5256 first_param = temp->str;
5257 end_params = temp->str + reql;
5258 cur_param = next_param = end_params;
5259 *end_params = '\0';
5260
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005261 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005262
5263 /*
5264 * Parse the parameters in reverse order to only store the last value.
5265 * From the html form, the backend and the action are at the end.
5266 */
5267 while (cur_param > first_param) {
5268 char *value;
5269 int poffset, plen;
5270
5271 cur_param--;
5272
5273 if ((*cur_param == '&') || (cur_param == first_param)) {
5274 reprocess_servers:
5275 /* Parse the key */
5276 poffset = (cur_param != first_param ? 1 : 0);
5277 plen = next_param - cur_param + (cur_param == first_param ? 1 : 0);
5278 if ((plen > 0) && (plen <= sizeof(key))) {
5279 strncpy(key, cur_param + poffset, plen);
5280 key[plen - 1] = '\0';
5281 } else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005282 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005283 goto out;
5284 }
5285
5286 /* Parse the value */
5287 value = key;
5288 while (*value != '\0' && *value != '=') {
5289 value++;
5290 }
5291 if (*value == '=') {
5292 /* Ok, a value is found, we can mark the end of the key */
5293 *value++ = '\0';
5294 }
5295 if (url_decode(key) < 0 || url_decode(value) < 0)
5296 break;
5297
5298 /* Now we can check the key to see what to do */
5299 if (!px && (strcmp(key, "b") == 0)) {
Willy Tarreau9e0bb102015-05-26 11:24:42 +02005300 if ((px = proxy_be_by_name(value)) == NULL) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01005301 /* the backend name is unknown or ambiguous (duplicate names) */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005302 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005303 goto out;
5304 }
5305 }
5306 else if (!action && (strcmp(key, "action") == 0)) {
Willy Tarreaued7df902014-05-22 18:04:49 +02005307 if (strcmp(value, "ready") == 0) {
5308 action = ST_ADM_ACTION_READY;
5309 }
5310 else if (strcmp(value, "drain") == 0) {
5311 action = ST_ADM_ACTION_DRAIN;
5312 }
5313 else if (strcmp(value, "maint") == 0) {
5314 action = ST_ADM_ACTION_MAINT;
5315 }
5316 else if (strcmp(value, "shutdown") == 0) {
5317 action = ST_ADM_ACTION_SHUTDOWN;
5318 }
Willy Tarreau248a60e2014-05-23 14:59:48 +02005319 else if (strcmp(value, "dhlth") == 0) {
5320 action = ST_ADM_ACTION_DHLTH;
5321 }
5322 else if (strcmp(value, "ehlth") == 0) {
5323 action = ST_ADM_ACTION_EHLTH;
5324 }
5325 else if (strcmp(value, "hrunn") == 0) {
5326 action = ST_ADM_ACTION_HRUNN;
5327 }
5328 else if (strcmp(value, "hnolb") == 0) {
5329 action = ST_ADM_ACTION_HNOLB;
5330 }
5331 else if (strcmp(value, "hdown") == 0) {
5332 action = ST_ADM_ACTION_HDOWN;
5333 }
5334 else if (strcmp(value, "dagent") == 0) {
5335 action = ST_ADM_ACTION_DAGENT;
5336 }
5337 else if (strcmp(value, "eagent") == 0) {
5338 action = ST_ADM_ACTION_EAGENT;
5339 }
5340 else if (strcmp(value, "arunn") == 0) {
5341 action = ST_ADM_ACTION_ARUNN;
5342 }
5343 else if (strcmp(value, "adown") == 0) {
5344 action = ST_ADM_ACTION_ADOWN;
5345 }
Willy Tarreaued7df902014-05-22 18:04:49 +02005346 /* else these are the old supported methods */
5347 else if (strcmp(value, "disable") == 0) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01005348 action = ST_ADM_ACTION_DISABLE;
5349 }
5350 else if (strcmp(value, "enable") == 0) {
5351 action = ST_ADM_ACTION_ENABLE;
5352 }
5353 else if (strcmp(value, "stop") == 0) {
5354 action = ST_ADM_ACTION_STOP;
5355 }
5356 else if (strcmp(value, "start") == 0) {
5357 action = ST_ADM_ACTION_START;
5358 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01005359 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005360 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005361 goto out;
5362 }
5363 }
5364 else if (strcmp(key, "s") == 0) {
5365 if (!(px && action)) {
5366 /*
5367 * Indicates that we'll need to reprocess the parameters
5368 * as soon as backend and action are known
5369 */
5370 if (!reprocess) {
5371 st_cur_param = cur_param;
5372 st_next_param = next_param;
5373 }
5374 reprocess = 1;
5375 }
5376 else if ((sv = findserver(px, value)) != NULL) {
5377 switch (action) {
5378 case ST_ADM_ACTION_DISABLE:
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02005379 if (!(sv->admin & SRV_ADMF_FMAINT)) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01005380 altered_servers++;
5381 total_servers++;
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02005382 srv_set_admin_flag(sv, SRV_ADMF_FMAINT);
Willy Tarreau347a35d2013-11-22 17:51:09 +01005383 }
5384 break;
5385 case ST_ADM_ACTION_ENABLE:
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02005386 if (sv->admin & SRV_ADMF_FMAINT) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01005387 altered_servers++;
5388 total_servers++;
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02005389 srv_clr_admin_flag(sv, SRV_ADMF_FMAINT);
Willy Tarreau347a35d2013-11-22 17:51:09 +01005390 }
5391 break;
5392 case ST_ADM_ACTION_STOP:
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02005393 if (!(sv->admin & SRV_ADMF_FDRAIN)) {
5394 srv_set_admin_flag(sv, SRV_ADMF_FDRAIN);
5395 altered_servers++;
5396 total_servers++;
5397 }
5398 break;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005399 case ST_ADM_ACTION_START:
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02005400 if (sv->admin & SRV_ADMF_FDRAIN) {
5401 srv_clr_admin_flag(sv, SRV_ADMF_FDRAIN);
5402 altered_servers++;
5403 total_servers++;
5404 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01005405 break;
Willy Tarreau248a60e2014-05-23 14:59:48 +02005406 case ST_ADM_ACTION_DHLTH:
5407 if (sv->check.state & CHK_ST_CONFIGURED) {
5408 sv->check.state &= ~CHK_ST_ENABLED;
5409 altered_servers++;
5410 total_servers++;
5411 }
5412 break;
5413 case ST_ADM_ACTION_EHLTH:
5414 if (sv->check.state & CHK_ST_CONFIGURED) {
5415 sv->check.state |= CHK_ST_ENABLED;
5416 altered_servers++;
5417 total_servers++;
5418 }
5419 break;
5420 case ST_ADM_ACTION_HRUNN:
5421 if (!(sv->track)) {
5422 sv->check.health = sv->check.rise + sv->check.fall - 1;
5423 srv_set_running(sv, "changed from Web interface");
5424 altered_servers++;
5425 total_servers++;
5426 }
5427 break;
5428 case ST_ADM_ACTION_HNOLB:
5429 if (!(sv->track)) {
5430 sv->check.health = sv->check.rise + sv->check.fall - 1;
5431 srv_set_stopping(sv, "changed from Web interface");
5432 altered_servers++;
5433 total_servers++;
5434 }
5435 break;
5436 case ST_ADM_ACTION_HDOWN:
5437 if (!(sv->track)) {
5438 sv->check.health = 0;
5439 srv_set_stopped(sv, "changed from Web interface");
5440 altered_servers++;
5441 total_servers++;
5442 }
5443 break;
5444 case ST_ADM_ACTION_DAGENT:
5445 if (sv->agent.state & CHK_ST_CONFIGURED) {
5446 sv->agent.state &= ~CHK_ST_ENABLED;
5447 altered_servers++;
5448 total_servers++;
5449 }
5450 break;
5451 case ST_ADM_ACTION_EAGENT:
5452 if (sv->agent.state & CHK_ST_CONFIGURED) {
5453 sv->agent.state |= CHK_ST_ENABLED;
5454 altered_servers++;
5455 total_servers++;
5456 }
5457 break;
5458 case ST_ADM_ACTION_ARUNN:
5459 if (sv->agent.state & CHK_ST_ENABLED) {
5460 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
5461 srv_set_running(sv, "changed from Web interface");
5462 altered_servers++;
5463 total_servers++;
5464 }
5465 break;
5466 case ST_ADM_ACTION_ADOWN:
5467 if (sv->agent.state & CHK_ST_ENABLED) {
5468 sv->agent.health = 0;
5469 srv_set_stopped(sv, "changed from Web interface");
5470 altered_servers++;
5471 total_servers++;
5472 }
5473 break;
Willy Tarreaued7df902014-05-22 18:04:49 +02005474 case ST_ADM_ACTION_READY:
5475 srv_adm_set_ready(sv);
5476 altered_servers++;
5477 total_servers++;
5478 break;
5479 case ST_ADM_ACTION_DRAIN:
5480 srv_adm_set_drain(sv);
5481 altered_servers++;
5482 total_servers++;
5483 break;
5484 case ST_ADM_ACTION_MAINT:
5485 srv_adm_set_maint(sv);
5486 altered_servers++;
5487 total_servers++;
5488 break;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005489 case ST_ADM_ACTION_SHUTDOWN:
5490 if (px->state != PR_STSTOPPED) {
Willy Tarreau87b09662015-04-03 00:22:06 +02005491 struct stream *sess, *sess_bck;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005492
5493 list_for_each_entry_safe(sess, sess_bck, &sv->actconns, by_srv)
5494 if (sess->srv_conn == sv)
Willy Tarreaue7dff022015-04-03 01:14:29 +02005495 stream_shutdown(sess, SF_ERR_KILLED);
Willy Tarreau347a35d2013-11-22 17:51:09 +01005496
5497 altered_servers++;
5498 total_servers++;
5499 }
5500 break;
5501 }
5502 } else {
5503 /* the server name is unknown or ambiguous (duplicate names) */
5504 total_servers++;
5505 }
5506 }
5507 if (reprocess && px && action) {
5508 /* Now, we know the backend and the action chosen by the user.
5509 * We can safely restart from the first server parameter
5510 * to reprocess them
5511 */
5512 cur_param = st_cur_param;
5513 next_param = st_next_param;
5514 reprocess = 0;
5515 goto reprocess_servers;
5516 }
5517
5518 next_param = cur_param;
5519 }
5520 }
5521
5522 if (total_servers == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005523 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005524 }
5525 else if (altered_servers == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005526 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005527 }
5528 else if (altered_servers == total_servers) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005529 appctx->ctx.stats.st_code = STAT_STATUS_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005530 }
5531 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005532 appctx->ctx.stats.st_code = STAT_STATUS_PART;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005533 }
5534 out:
5535 return 1;
5536}
5537
5538
5539static int stats_send_http_headers(struct stream_interface *si)
5540{
Willy Tarreau87b09662015-04-03 00:22:06 +02005541 struct stream *s = si_strm(si);
Willy Tarreau347a35d2013-11-22 17:51:09 +01005542 struct uri_auth *uri = s->be->uri_auth;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005543 struct appctx *appctx = objt_appctx(si->end);
Willy Tarreau347a35d2013-11-22 17:51:09 +01005544
5545 chunk_printf(&trash,
Willy Tarreau8b8995f2014-04-24 22:51:54 +02005546 "HTTP/1.1 200 OK\r\n"
Willy Tarreau347a35d2013-11-22 17:51:09 +01005547 "Cache-Control: no-cache\r\n"
5548 "Connection: close\r\n"
5549 "Content-Type: %s\r\n",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005550 (appctx->ctx.stats.flags & STAT_FMT_HTML) ? "text/html" : "text/plain");
Willy Tarreau347a35d2013-11-22 17:51:09 +01005551
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005552 if (uri->refresh > 0 && !(appctx->ctx.stats.flags & STAT_NO_REFRESH))
Willy Tarreau347a35d2013-11-22 17:51:09 +01005553 chunk_appendf(&trash, "Refresh: %d\r\n",
5554 uri->refresh);
5555
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005556 /* we don't send the CRLF in chunked mode, it will be sent with the first chunk's size */
5557
5558 if (appctx->ctx.stats.flags & STAT_CHUNKED)
5559 chunk_appendf(&trash, "Transfer-Encoding: chunked\r\n");
5560 else
5561 chunk_appendf(&trash, "\r\n");
Willy Tarreau347a35d2013-11-22 17:51:09 +01005562
Willy Tarreaueee5b512015-04-03 23:46:31 +02005563 s->txn->status = 200;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005564 s->logs.tv_request = now;
5565
Willy Tarreaubc18da12015-03-13 14:00:47 +01005566 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02005567 si_applet_cant_put(si);
Willy Tarreau347a35d2013-11-22 17:51:09 +01005568 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01005569 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01005570
5571 return 1;
5572}
5573
5574static int stats_send_http_redirect(struct stream_interface *si)
5575{
5576 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
Willy Tarreau87b09662015-04-03 00:22:06 +02005577 struct stream *s = si_strm(si);
Willy Tarreau347a35d2013-11-22 17:51:09 +01005578 struct uri_auth *uri = s->be->uri_auth;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005579 struct appctx *appctx = objt_appctx(si->end);
Willy Tarreau347a35d2013-11-22 17:51:09 +01005580
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005581 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Willy Tarreau347a35d2013-11-22 17:51:09 +01005582 scope_txt[0] = 0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005583 if (appctx->ctx.stats.scope_len) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01005584 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau4e4292b2014-11-28 12:18:45 +01005585 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 +01005586 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005587 }
5588
5589 /* We don't want to land on the posted stats page because a refresh will
5590 * repost the data. We don't want this to happen on accident so we redirect
5591 * the browse to the stats page with a GET.
5592 */
5593 chunk_printf(&trash,
5594 "HTTP/1.1 303 See Other\r\n"
5595 "Cache-Control: no-cache\r\n"
5596 "Content-Type: text/plain\r\n"
5597 "Connection: close\r\n"
5598 "Location: %s;st=%s%s%s%s\r\n"
Willy Tarreaufdfcc9d2016-01-26 13:57:29 +01005599 "Content-length: 0\r\n"
Willy Tarreau347a35d2013-11-22 17:51:09 +01005600 "\r\n",
5601 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005602 ((appctx->ctx.stats.st_code > STAT_STATUS_INIT) &&
5603 (appctx->ctx.stats.st_code < STAT_STATUS_SIZE) &&
5604 stat_status_codes[appctx->ctx.stats.st_code]) ?
5605 stat_status_codes[appctx->ctx.stats.st_code] :
Willy Tarreau347a35d2013-11-22 17:51:09 +01005606 stat_status_codes[STAT_STATUS_UNKN],
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005607 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
5608 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreau347a35d2013-11-22 17:51:09 +01005609 scope_txt);
5610
Willy Tarreaueee5b512015-04-03 23:46:31 +02005611 s->txn->status = 303;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005612 s->logs.tv_request = now;
5613
Willy Tarreaubc18da12015-03-13 14:00:47 +01005614 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02005615 si_applet_cant_put(si);
Willy Tarreau347a35d2013-11-22 17:51:09 +01005616 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01005617 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01005618
5619 return 1;
5620}
5621
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005622/* This I/O handler runs as an applet embedded in a stream interface. It is
5623 * used to send HTTP stats over a TCP socket. The mechanism is very simple.
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005624 * appctx->st0 contains the operation in progress (dump, done). The handler
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005625 * automatically unregisters itself once transfer is complete.
5626 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02005627static void http_stats_io_handler(struct appctx *appctx)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005628{
Willy Tarreau00a37f02015-04-13 12:05:19 +02005629 struct stream_interface *si = appctx->owner;
Willy Tarreau87b09662015-04-03 00:22:06 +02005630 struct stream *s = si_strm(si);
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005631 struct channel *req = si_oc(si);
5632 struct channel *res = si_ic(si);
Willy Tarreau55058a72012-11-21 08:27:21 +01005633
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005634 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
5635 goto out;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01005636
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005637 /* check that the output is not closed */
5638 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005639 appctx->st0 = STAT_HTTP_DONE;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005640
Willy Tarreau347a35d2013-11-22 17:51:09 +01005641 /* all states are processed in sequence */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005642 if (appctx->st0 == STAT_HTTP_HEAD) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01005643 if (stats_send_http_headers(si)) {
Willy Tarreaueee5b512015-04-03 23:46:31 +02005644 if (s->txn->meth == HTTP_METH_HEAD)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005645 appctx->st0 = STAT_HTTP_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005646 else
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005647 appctx->st0 = STAT_HTTP_DUMP;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005648 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01005649 }
5650
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005651 if (appctx->st0 == STAT_HTTP_DUMP) {
Willy Tarreau4e4292b2014-11-28 12:18:45 +01005652 unsigned int prev_len = si_ib(si)->i;
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005653 unsigned int data_len;
5654 unsigned int last_len;
Willy Tarreaucce36482014-04-24 20:26:41 +02005655 unsigned int last_fwd = 0;
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005656
5657 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
5658 /* One difficulty we're facing is that we must prevent
5659 * the input data from being automatically forwarded to
5660 * the output area. For this, we temporarily disable
5661 * forwarding on the channel.
5662 */
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005663 last_fwd = si_ic(si)->to_forward;
5664 si_ic(si)->to_forward = 0;
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005665 chunk_printf(&trash, "\r\n000000\r\n");
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005666 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02005667 si_applet_cant_put(si);
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005668 si_ic(si)->to_forward = last_fwd;
Willy Tarreau828824a2015-04-19 17:20:03 +02005669 goto out;
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005670 }
5671 }
5672
Willy Tarreau4e4292b2014-11-28 12:18:45 +01005673 data_len = si_ib(si)->i;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005674 if (stats_dump_stat_to_buffer(si, s->be->uri_auth))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005675 appctx->st0 = STAT_HTTP_DONE;
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005676
Willy Tarreau4e4292b2014-11-28 12:18:45 +01005677 last_len = si_ib(si)->i;
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005678
5679 /* Now we must either adjust or remove the chunk size. This is
5680 * not easy because the chunk size might wrap at the end of the
5681 * buffer, so we pretend we have nothing in the buffer, we write
5682 * the size, then restore the buffer's contents. Note that we can
5683 * only do that because no forwarding is scheduled on the stats
5684 * applet.
5685 */
5686 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
Willy Tarreau4e4292b2014-11-28 12:18:45 +01005687 si_ic(si)->total -= (last_len - prev_len);
5688 si_ib(si)->i -= (last_len - prev_len);
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005689
5690 if (last_len != data_len) {
5691 chunk_printf(&trash, "\r\n%06x\r\n", (last_len - data_len));
Willy Tarreaubc18da12015-03-13 14:00:47 +01005692 if (bi_putchk(si_ic(si), &trash) == -1)
Willy Tarreaufe127932015-04-21 19:23:39 +02005693 si_applet_cant_put(si);
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005694
Willy Tarreau4e4292b2014-11-28 12:18:45 +01005695 si_ic(si)->total += (last_len - data_len);
5696 si_ib(si)->i += (last_len - data_len);
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005697 }
5698 /* now re-enable forwarding */
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005699 channel_forward(si_ic(si), last_fwd);
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005700 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005701 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02005702
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005703 if (appctx->st0 == STAT_HTTP_POST) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01005704 if (stats_process_http_post(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005705 appctx->st0 = STAT_HTTP_LAST;
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005706 else if (si_oc(si)->flags & CF_SHUTR)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005707 appctx->st0 = STAT_HTTP_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005708 }
5709
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005710 if (appctx->st0 == STAT_HTTP_LAST) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01005711 if (stats_send_http_redirect(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005712 appctx->st0 = STAT_HTTP_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005713 }
5714
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005715 if (appctx->st0 == STAT_HTTP_DONE) {
5716 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
5717 chunk_printf(&trash, "\r\n0\r\n\r\n");
Willy Tarreaubc18da12015-03-13 14:00:47 +01005718 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02005719 si_applet_cant_put(si);
Willy Tarreau828824a2015-04-19 17:20:03 +02005720 goto out;
Willy Tarreaubc18da12015-03-13 14:00:47 +01005721 }
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005722 }
5723 /* eat the whole request */
Willy Tarreau4e4292b2014-11-28 12:18:45 +01005724 bo_skip(si_oc(si), si_ob(si)->o);
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005725 res->flags |= CF_READ_NULL;
5726 si_shutr(si);
5727 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01005728
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005729 if ((res->flags & CF_SHUTR) && (si->state == SI_ST_EST))
5730 si_shutw(si);
Willy Tarreau91861262007-10-17 17:06:05 +02005731
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005732 if (appctx->st0 == STAT_HTTP_DONE) {
Willy Tarreau96d44912013-11-22 12:25:24 +01005733 if ((req->flags & CF_SHUTW) && (si->state == SI_ST_EST)) {
5734 si_shutr(si);
5735 res->flags |= CF_READ_NULL;
5736 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005737 }
Willy Tarreau828824a2015-04-19 17:20:03 +02005738 out:
Willy Tarreaud4da1962015-04-20 01:31:23 +02005739 /* just to make gcc happy */ ;
Willy Tarreau91861262007-10-17 17:06:05 +02005740}
5741
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005742
Willy Tarreau909d5172012-11-26 03:04:41 +01005743static inline const char *get_conn_ctrl_name(const struct connection *conn)
5744{
Willy Tarreau3c728722014-01-23 13:50:42 +01005745 if (!conn_ctrl_ready(conn))
Willy Tarreau909d5172012-11-26 03:04:41 +01005746 return "NONE";
5747 return conn->ctrl->name;
5748}
5749
5750static inline const char *get_conn_xprt_name(const struct connection *conn)
5751{
5752 static char ptr[17];
5753
Willy Tarreauaad69382014-01-23 14:21:42 +01005754 if (!conn_xprt_ready(conn))
Willy Tarreau909d5172012-11-26 03:04:41 +01005755 return "NONE";
5756
5757 if (conn->xprt == &raw_sock)
5758 return "RAW";
5759
5760#ifdef USE_OPENSSL
5761 if (conn->xprt == &ssl_sock)
5762 return "SSL";
5763#endif
5764 snprintf(ptr, sizeof(ptr), "%p", conn->xprt);
5765 return ptr;
5766}
5767
5768static inline const char *get_conn_data_name(const struct connection *conn)
5769{
5770 static char ptr[17];
5771
5772 if (!conn->data)
5773 return "NONE";
5774
5775 if (conn->data == &sess_conn_cb)
5776 return "SESS";
5777
5778 if (conn->data == &si_conn_cb)
5779 return "STRM";
5780
5781 if (conn->data == &check_conn_cb)
5782 return "CHCK";
5783
5784 snprintf(ptr, sizeof(ptr), "%p", conn->data);
5785 return ptr;
5786}
5787
Willy Tarreau87b09662015-04-03 00:22:06 +02005788/* This function dumps a complete stream state onto the stream interface's
5789 * read buffer. The stream has to be set in sess->target. It returns
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01005790 * 0 if the output buffer is full and it needs to be called again, otherwise
5791 * non-zero. It is designed to be called from stats_dump_sess_to_buffer() below.
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005792 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005793static int stats_dump_full_sess_to_buffer(struct stream_interface *si, struct stream *sess)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005794{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005795 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005796 struct tm tm;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005797 extern const char *monthname[12];
5798 char pn[INET6_ADDRSTRLEN];
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005799 struct connection *conn;
Willy Tarreau284ddbf2013-12-01 20:45:00 +01005800 struct appctx *tmpctx;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005801
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005802 chunk_reset(&trash);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005803
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005804 if (appctx->ctx.sess.section > 0 && appctx->ctx.sess.uid != sess->uniq_id) {
Willy Tarreau87b09662015-04-03 00:22:06 +02005805 /* stream changed, no need to go any further */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005806 chunk_appendf(&trash, " *** session terminated while we were watching it ***\n");
Willy Tarreaubc18da12015-03-13 14:00:47 +01005807 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02005808 si_applet_cant_put(si);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005809 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01005810 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005811 appctx->ctx.sess.uid = 0;
5812 appctx->ctx.sess.section = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005813 return 1;
5814 }
5815
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005816 switch (appctx->ctx.sess.section) {
Willy Tarreau87b09662015-04-03 00:22:06 +02005817 case 0: /* main status of the stream */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005818 appctx->ctx.sess.uid = sess->uniq_id;
5819 appctx->ctx.sess.section = 1;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005820 /* fall through */
5821
5822 case 1:
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005823 get_localtime(sess->logs.accept_date.tv_sec, &tm);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005824 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005825 "%p: [%02d/%s/%04d:%02d:%02d:%02d.%06d] id=%u proto=%s",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005826 sess,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005827 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
5828 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(sess->logs.accept_date.tv_usec),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005829 sess->uniq_id,
Willy Tarreau666f5042015-06-17 19:49:52 +02005830 strm_li(sess) ? strm_li(sess)->proto->name : "?");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005831
Willy Tarreaud0d8da92015-04-04 02:10:38 +02005832 conn = objt_conn(strm_orig(sess));
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005833 switch (conn ? addr_to_str(&conn->addr.from, pn, sizeof(pn)) : AF_UNSPEC) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005834 case AF_INET:
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005835 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005836 chunk_appendf(&trash, " source=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005837 pn, get_host_port(&conn->addr.from));
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005838 break;
5839 case AF_UNIX:
Willy Tarreaud0d8da92015-04-04 02:10:38 +02005840 chunk_appendf(&trash, " source=unix:%d\n", strm_li(sess)->luid);
Emeric Brun837ca522010-10-22 16:19:01 +02005841 break;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005842 default:
5843 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005844 chunk_appendf(&trash, "\n");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005845 break;
5846 }
5847
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005848 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005849 " flags=0x%x, conn_retries=%d, srv_conn=%p, pend_pos=%p\n",
Willy Tarreauee28de02010-06-01 09:51:00 +02005850 sess->flags, sess->si[1].conn_retries, sess->srv_conn, sess->pend_pos);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005851
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005852 chunk_appendf(&trash,
Daniel Schultze90690c72012-03-23 10:53:36 -07005853 " frontend=%s (id=%u mode=%s), listener=%s (id=%u)",
Willy Tarreaud0d8da92015-04-04 02:10:38 +02005854 strm_fe(sess)->id, strm_fe(sess)->uuid, strm_fe(sess)->mode ? "http" : "tcp",
5855 strm_li(sess) ? strm_li(sess)->name ? strm_li(sess)->name : "?" : "?",
5856 strm_li(sess) ? strm_li(sess)->luid : 0);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005857
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005858 if (conn)
5859 conn_get_to_addr(conn);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005860
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005861 switch (conn ? addr_to_str(&conn->addr.to, pn, sizeof(pn)) : AF_UNSPEC) {
Daniel Schultze90690c72012-03-23 10:53:36 -07005862 case AF_INET:
5863 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005864 chunk_appendf(&trash, " addr=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005865 pn, get_host_port(&conn->addr.to));
Daniel Schultze90690c72012-03-23 10:53:36 -07005866 break;
5867 case AF_UNIX:
Willy Tarreaud0d8da92015-04-04 02:10:38 +02005868 chunk_appendf(&trash, " addr=unix:%d\n", strm_li(sess)->luid);
Daniel Schultze90690c72012-03-23 10:53:36 -07005869 break;
5870 default:
5871 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005872 chunk_appendf(&trash, "\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07005873 break;
5874 }
5875
Willy Tarreau50943332011-09-02 17:33:05 +02005876 if (sess->be->cap & PR_CAP_BE)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005877 chunk_appendf(&trash,
Daniel Schultze90690c72012-03-23 10:53:36 -07005878 " backend=%s (id=%u mode=%s)",
Willy Tarreau50943332011-09-02 17:33:05 +02005879 sess->be->id,
Daniel Schultze90690c72012-03-23 10:53:36 -07005880 sess->be->uuid, sess->be->mode ? "http" : "tcp");
5881 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005882 chunk_appendf(&trash, " backend=<NONE> (id=-1 mode=-)");
Daniel Schultze90690c72012-03-23 10:53:36 -07005883
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005884 conn = objt_conn(sess->si[1].end);
5885 if (conn)
5886 conn_get_from_addr(conn);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005887
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005888 switch (conn ? addr_to_str(&conn->addr.from, pn, sizeof(pn)) : AF_UNSPEC) {
Daniel Schultze90690c72012-03-23 10:53:36 -07005889 case AF_INET:
5890 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005891 chunk_appendf(&trash, " addr=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005892 pn, get_host_port(&conn->addr.from));
Daniel Schultze90690c72012-03-23 10:53:36 -07005893 break;
5894 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005895 chunk_appendf(&trash, " addr=unix\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07005896 break;
5897 default:
5898 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005899 chunk_appendf(&trash, "\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07005900 break;
5901 }
5902
5903 if (sess->be->cap & PR_CAP_BE)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005904 chunk_appendf(&trash,
Daniel Schultze90690c72012-03-23 10:53:36 -07005905 " server=%s (id=%u)",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005906 objt_server(sess->target) ? objt_server(sess->target)->id : "<none>",
5907 objt_server(sess->target) ? objt_server(sess->target)->puid : 0);
Willy Tarreau50943332011-09-02 17:33:05 +02005908 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005909 chunk_appendf(&trash, " server=<NONE> (id=-1)");
Daniel Schultze90690c72012-03-23 10:53:36 -07005910
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005911 if (conn)
5912 conn_get_to_addr(conn);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005913
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005914 switch (conn ? addr_to_str(&conn->addr.to, pn, sizeof(pn)) : AF_UNSPEC) {
Daniel Schultze90690c72012-03-23 10:53:36 -07005915 case AF_INET:
5916 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005917 chunk_appendf(&trash, " addr=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005918 pn, get_host_port(&conn->addr.to));
Daniel Schultze90690c72012-03-23 10:53:36 -07005919 break;
5920 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005921 chunk_appendf(&trash, " addr=unix\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07005922 break;
5923 default:
5924 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005925 chunk_appendf(&trash, "\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07005926 break;
5927 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005928
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005929 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005930 " task=%p (state=0x%02x nice=%d calls=%d exp=%s%s",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005931 sess->task,
5932 sess->task->state,
5933 sess->task->nice, sess->task->calls,
5934 sess->task->expire ?
5935 tick_is_expired(sess->task->expire, now_ms) ? "<PAST>" :
5936 human_time(TICKS_TO_MS(sess->task->expire - now_ms),
5937 TICKS_TO_MS(1000)) : "<NEVER>",
5938 task_in_rq(sess->task) ? ", running" : "");
5939
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005940 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005941 " age=%s)\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005942 human_time(now.tv_sec - sess->logs.accept_date.tv_sec, 1));
5943
Willy Tarreaueee5b512015-04-03 23:46:31 +02005944 if (sess->txn)
5945 chunk_appendf(&trash,
Willy Tarreau98410192014-11-26 18:05:38 +01005946 " 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 +02005947 sess->txn, sess->txn->flags, sess->txn->meth, sess->txn->status,
5948 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 +01005949
5950 chunk_appendf(&trash,
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02005951 " si[0]=%p (state=%s flags=0x%02x endp0=%s:%p exp=%s, et=0x%03x)\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005952 &sess->si[0],
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005953 si_state_str(sess->si[0].state),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005954 sess->si[0].flags,
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005955 obj_type_name(sess->si[0].end),
5956 obj_base_ptr(sess->si[0].end),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005957 sess->si[0].exp ?
5958 tick_is_expired(sess->si[0].exp, now_ms) ? "<PAST>" :
5959 human_time(TICKS_TO_MS(sess->si[0].exp - now_ms),
5960 TICKS_TO_MS(1000)) : "<NEVER>",
5961 sess->si[0].err_type);
5962
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005963 chunk_appendf(&trash,
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02005964 " si[1]=%p (state=%s flags=0x%02x endp1=%s:%p exp=%s, et=0x%03x)\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005965 &sess->si[1],
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005966 si_state_str(sess->si[1].state),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005967 sess->si[1].flags,
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005968 obj_type_name(sess->si[1].end),
5969 obj_base_ptr(sess->si[1].end),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005970 sess->si[1].exp ?
5971 tick_is_expired(sess->si[1].exp, now_ms) ? "<PAST>" :
5972 human_time(TICKS_TO_MS(sess->si[1].exp - now_ms),
5973 TICKS_TO_MS(1000)) : "<NEVER>",
5974 sess->si[1].err_type);
5975
Willy Tarreau284ddbf2013-12-01 20:45:00 +01005976 if ((conn = objt_conn(sess->si[0].end)) != NULL) {
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005977 chunk_appendf(&trash,
5978 " co0=%p ctrl=%s xprt=%s data=%s target=%s:%p\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005979 conn,
5980 get_conn_ctrl_name(conn),
5981 get_conn_xprt_name(conn),
5982 get_conn_data_name(conn),
5983 obj_type_name(conn->target),
5984 obj_base_ptr(conn->target));
Willy Tarreau909d5172012-11-26 03:04:41 +01005985
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005986 chunk_appendf(&trash,
Willy Tarreau16f649c2014-01-25 19:10:48 +01005987 " flags=0x%08x fd=%d fd.state=%02x fd.cache=%d updt=%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005988 conn->flags,
5989 conn->t.sock.fd,
Willy Tarreau69a41fa2014-01-20 11:02:59 +01005990 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].state : 0,
Willy Tarreau15a4dec2014-01-20 11:09:39 +01005991 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].cache : 0,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005992 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].updated : 0);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005993 }
Willy Tarreau284ddbf2013-12-01 20:45:00 +01005994 else if ((tmpctx = objt_appctx(sess->si[0].end)) != NULL) {
5995 chunk_appendf(&trash,
5996 " app0=%p st0=%d st1=%d st2=%d applet=%s\n",
5997 tmpctx,
5998 tmpctx->st0,
5999 tmpctx->st1,
6000 tmpctx->st2,
6001 tmpctx->applet->name);
6002 }
Willy Tarreaubc174aa2012-11-19 16:10:32 +01006003
Willy Tarreau284ddbf2013-12-01 20:45:00 +01006004 if ((conn = objt_conn(sess->si[1].end)) != NULL) {
Willy Tarreaucf644ed2013-09-29 17:19:56 +02006005 chunk_appendf(&trash,
6006 " co1=%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 Tarreauceeafb52016-01-25 15:27:17 +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[1].end)) != NULL) {
6023 chunk_appendf(&trash,
6024 " app1=%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 Tarreau66dc20a2010-03-05 17:53:32 +01006031
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006032 chunk_appendf(&trash,
Willy Tarreau909d5172012-11-26 03:04:41 +01006033 " req=%p (f=0x%06x an=0x%x pipe=%d tofwd=%d total=%lld)\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006034 " an_exp=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006035 &sess->req,
6036 sess->req.flags, sess->req.analysers,
6037 sess->req.pipe ? sess->req.pipe->data : 0,
6038 sess->req.to_forward, sess->req.total,
6039 sess->req.analyse_exp ?
6040 human_time(TICKS_TO_MS(sess->req.analyse_exp - now_ms),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006041 TICKS_TO_MS(1000)) : "<NEVER>");
6042
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006043 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006044 " rex=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006045 sess->req.rex ?
6046 human_time(TICKS_TO_MS(sess->req.rex - now_ms),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006047 TICKS_TO_MS(1000)) : "<NEVER>");
6048
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006049 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006050 " wex=%s\n"
Willy Tarreau909d5172012-11-26 03:04:41 +01006051 " buf=%p data=%p o=%d p=%d req.next=%d i=%d size=%d\n",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006052 sess->req.wex ?
6053 human_time(TICKS_TO_MS(sess->req.wex - now_ms),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006054 TICKS_TO_MS(1000)) : "<NEVER>",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006055 sess->req.buf,
6056 sess->req.buf->data, sess->req.buf->o,
6057 (int)(sess->req.buf->p - sess->req.buf->data),
Willy Tarreaueee5b512015-04-03 23:46:31 +02006058 sess->txn ? sess->txn->req.next : 0, sess->req.buf->i,
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006059 sess->req.buf->size);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006060
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006061 chunk_appendf(&trash,
Willy Tarreau909d5172012-11-26 03:04:41 +01006062 " res=%p (f=0x%06x an=0x%x pipe=%d tofwd=%d total=%lld)\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006063 " an_exp=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006064 &sess->res,
6065 sess->res.flags, sess->res.analysers,
6066 sess->res.pipe ? sess->res.pipe->data : 0,
6067 sess->res.to_forward, sess->res.total,
6068 sess->res.analyse_exp ?
6069 human_time(TICKS_TO_MS(sess->res.analyse_exp - now_ms),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006070 TICKS_TO_MS(1000)) : "<NEVER>");
6071
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006072 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006073 " rex=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006074 sess->res.rex ?
6075 human_time(TICKS_TO_MS(sess->res.rex - now_ms),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006076 TICKS_TO_MS(1000)) : "<NEVER>");
6077
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006078 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006079 " wex=%s\n"
Willy Tarreau909d5172012-11-26 03:04:41 +01006080 " buf=%p data=%p o=%d p=%d rsp.next=%d i=%d size=%d\n",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006081 sess->res.wex ?
6082 human_time(TICKS_TO_MS(sess->res.wex - now_ms),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006083 TICKS_TO_MS(1000)) : "<NEVER>",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006084 sess->res.buf,
6085 sess->res.buf->data, sess->res.buf->o,
6086 (int)(sess->res.buf->p - sess->res.buf->data),
Willy Tarreaueee5b512015-04-03 23:46:31 +02006087 sess->txn ? sess->txn->rsp.next : 0, sess->res.buf->i,
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006088 sess->res.buf->size);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006089
Willy Tarreaubc18da12015-03-13 14:00:47 +01006090 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02006091 si_applet_cant_put(si);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006092 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01006093 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006094
6095 /* use other states to dump the contents */
6096 }
6097 /* end of dump */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006098 appctx->ctx.sess.uid = 0;
6099 appctx->ctx.sess.section = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006100 return 1;
6101}
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006102
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02006103#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
6104static int stats_tlskeys_list(struct stream_interface *si) {
6105 struct appctx *appctx = __objt_appctx(si->end);
6106
6107 switch (appctx->st2) {
6108 case STAT_ST_INIT:
6109 /* Display the column headers. If the message cannot be sent,
6110 * quit the fucntion with returning 0. The function is called
6111 * later and restart at the state "STAT_ST_INIT".
6112 */
6113 chunk_reset(&trash);
6114 chunk_appendf(&trash, "# id (file)\n");
6115 if (bi_putchk(si_ic(si), &trash) == -1) {
6116 si_applet_cant_put(si);
6117 return 0;
6118 }
6119
6120 /* Now, we start the browsing of the references lists.
6121 * Note that the following call to LIST_ELEM return bad pointer. The only
Cyril Bontédb98eb32016-05-06 12:18:50 +02006122 * available field of this pointer is <list>. It is used with the function
6123 * tlskeys_list_get_next() for retruning the first available entry
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02006124 */
6125 appctx->ctx.tlskeys.ref = LIST_ELEM(&tlskeys_reference, struct tls_keys_ref *, list);
6126 appctx->ctx.tlskeys.ref = tlskeys_list_get_next(appctx->ctx.tlskeys.ref, &tlskeys_reference);
6127
6128 appctx->st2 = STAT_ST_LIST;
6129 /* fall through */
6130
6131 case STAT_ST_LIST:
6132 while (appctx->ctx.tlskeys.ref) {
6133 chunk_reset(&trash);
6134
6135 chunk_appendf(&trash, "%d (%s)\n", appctx->ctx.tlskeys.ref->unique_id,
6136 appctx->ctx.tlskeys.ref->filename);
6137
6138 if (bi_putchk(si_ic(si), &trash) == -1) {
6139 /* let's try again later from this stream. We add ourselves into
6140 * this stream's users so that it can remove us upon termination.
6141 */
6142 si_applet_cant_put(si);
6143 return 0;
6144 }
6145
6146 /* get next list entry and check the end of the list */
6147 appctx->ctx.tlskeys.ref = tlskeys_list_get_next(appctx->ctx.tlskeys.ref, &tlskeys_reference);
6148 }
6149
6150 appctx->st2 = STAT_ST_FIN;
6151 /* fall through */
6152
6153 default:
6154 appctx->st2 = STAT_ST_FIN;
6155 return 1;
6156 }
6157 return 0;
6158}
6159#endif
6160
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006161static int stats_pats_list(struct stream_interface *si)
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006162{
6163 struct appctx *appctx = __objt_appctx(si->end);
6164
6165 switch (appctx->st2) {
6166 case STAT_ST_INIT:
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01006167 /* Display the column headers. If the message cannot be sent,
6168 * quit the fucntion with returning 0. The function is called
6169 * later and restart at the state "STAT_ST_INIT".
6170 */
6171 chunk_reset(&trash);
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01006172 chunk_appendf(&trash, "# id (file) description\n");
Willy Tarreaubc18da12015-03-13 14:00:47 +01006173 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02006174 si_applet_cant_put(si);
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01006175 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01006176 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006177
6178 /* Now, we start the browsing of the references lists.
6179 * Note that the following call to LIST_ELEM return bad pointer. The only
Cyril Bontédb98eb32016-05-06 12:18:50 +02006180 * available field of this pointer is <list>. It is used with the function
6181 * pat_list_get_next() for retruning the first available entry
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006182 */
6183 appctx->ctx.map.ref = LIST_ELEM(&pattern_reference, struct pat_ref *, list);
6184 appctx->ctx.map.ref = pat_list_get_next(appctx->ctx.map.ref, &pattern_reference,
6185 appctx->ctx.map.display_flags);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006186 appctx->st2 = STAT_ST_LIST;
6187 /* fall through */
6188
6189 case STAT_ST_LIST:
6190 while (appctx->ctx.map.ref) {
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006191 chunk_reset(&trash);
6192
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006193 /* Build messages. If the reference is used by another category than
6194 * the listed categorie, display the information in the massage.
6195 */
Thierry FOURNIERf7e04e92014-03-20 11:45:47 +01006196 chunk_appendf(&trash, "%d (%s) %s\n", appctx->ctx.map.ref->unique_id,
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01006197 appctx->ctx.map.ref->reference ? appctx->ctx.map.ref->reference : "",
6198 appctx->ctx.map.ref->display);
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01006199
Willy Tarreau2bb4a962014-11-28 11:11:05 +01006200 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau87b09662015-04-03 00:22:06 +02006201 /* let's try again later from this stream. We add ourselves into
6202 * this stream's users so that it can remove us upon termination.
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006203 */
Willy Tarreaufe127932015-04-21 19:23:39 +02006204 si_applet_cant_put(si);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006205 return 0;
6206 }
6207
6208 /* get next list entry and check the end of the list */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006209 appctx->ctx.map.ref = pat_list_get_next(appctx->ctx.map.ref, &pattern_reference,
6210 appctx->ctx.map.display_flags);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006211 }
6212
6213 appctx->st2 = STAT_ST_FIN;
6214 /* fall through */
6215
6216 default:
6217 appctx->st2 = STAT_ST_FIN;
6218 return 1;
6219 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006220 return 0;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006221}
6222
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006223static int stats_map_lookup(struct stream_interface *si)
6224{
6225 struct appctx *appctx = __objt_appctx(si->end);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006226 struct sample sample;
6227 struct pattern *pat;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006228 int match_method;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006229
6230 switch (appctx->st2) {
6231 case STAT_ST_INIT:
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006232 /* Init to the first entry. The list cannot be change */
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01006233 appctx->ctx.map.expr = LIST_ELEM(&appctx->ctx.map.ref->pat, struct pattern_expr *, list);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006234 appctx->ctx.map.expr = pat_expr_get_next(appctx->ctx.map.expr, &appctx->ctx.map.ref->pat);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006235 appctx->st2 = STAT_ST_LIST;
6236 /* fall through */
6237
6238 case STAT_ST_LIST:
6239 /* for each lookup type */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006240 while (appctx->ctx.map.expr) {
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006241 /* initialise chunk to build new message */
6242 chunk_reset(&trash);
6243
6244 /* execute pattern matching */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02006245 sample.data.type = SMP_T_STR;
Andreas Seltenreich9727cf42016-03-03 19:32:25 +01006246 sample.flags = SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02006247 sample.data.u.str.len = appctx->ctx.map.chunk.len;
6248 sample.data.u.str.str = appctx->ctx.map.chunk.str;
Thierry FOURNIER5d344082014-01-27 14:19:53 +01006249 if (appctx->ctx.map.expr->pat_head->match &&
6250 sample_convert(&sample, appctx->ctx.map.expr->pat_head->expect_type))
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006251 pat = appctx->ctx.map.expr->pat_head->match(&sample, appctx->ctx.map.expr, 1);
6252 else
6253 pat = NULL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006254
6255 /* build return message: set type of match */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006256 for (match_method=0; match_method<PAT_MATCH_NUM; match_method++)
6257 if (appctx->ctx.map.expr->pat_head->match == pat_match_fcts[match_method])
6258 break;
6259 if (match_method >= PAT_MATCH_NUM)
6260 chunk_appendf(&trash, "type=unknown(%p)", appctx->ctx.map.expr->pat_head->match);
6261 else
6262 chunk_appendf(&trash, "type=%s", pat_match_names[match_method]);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006263
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02006264 /* case sensitive */
6265 if (appctx->ctx.map.expr->mflags & PAT_MF_IGNORE_CASE)
6266 chunk_appendf(&trash, ", case=insensitive");
6267 else
6268 chunk_appendf(&trash, ", case=sensitive");
6269
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006270 /* Display no match, and set default value */
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01006271 if (!pat) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006272 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
6273 chunk_appendf(&trash, ", found=no");
6274 else
6275 chunk_appendf(&trash, ", match=no");
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006276 }
6277
6278 /* Display match and match info */
6279 else {
6280 /* display match */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006281 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
6282 chunk_appendf(&trash, ", found=yes");
6283 else
6284 chunk_appendf(&trash, ", match=yes");
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006285
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01006286 /* display index mode */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02006287 if (pat->sflags & PAT_SF_TREE)
Thierry FOURNIERb9903842014-03-11 18:48:17 +01006288 chunk_appendf(&trash, ", idx=tree");
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006289 else
Thierry FOURNIERb9903842014-03-11 18:48:17 +01006290 chunk_appendf(&trash, ", idx=list");
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006291
Thierry FOURNIER86db66a2014-03-11 18:37:58 +01006292 /* display pattern */
6293 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
6294 if (pat->ref && pat->ref->pattern)
6295 chunk_appendf(&trash, ", key=\"%s\"", pat->ref->pattern);
6296 else
6297 chunk_appendf(&trash, ", key=unknown");
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006298 }
6299 else {
Thierry FOURNIER86db66a2014-03-11 18:37:58 +01006300 if (pat->ref && pat->ref->pattern)
6301 chunk_appendf(&trash, ", pattern=\"%s\"", pat->ref->pattern);
6302 else
6303 chunk_appendf(&trash, ", pattern=unknown");
6304 }
6305
6306 /* display return value */
6307 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
Thierry FOURNIER503bb092015-08-19 08:35:43 +02006308 if (pat->data && pat->ref && pat->ref->sample)
Thierry FOURNIER03d0e452015-07-23 18:33:41 +02006309 chunk_appendf(&trash, ", value=\"%s\", type=\"%s\"", pat->ref->sample,
Thierry FOURNIER7d4335c2015-08-19 09:05:25 +02006310 smp_to_type[pat->data->type]);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006311 else
Thierry FOURNIER86db66a2014-03-11 18:37:58 +01006312 chunk_appendf(&trash, ", value=none");
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006313 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006314 }
6315
Thierry FOURNIERb9903842014-03-11 18:48:17 +01006316 chunk_appendf(&trash, "\n");
6317
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006318 /* display response */
Willy Tarreau2bb4a962014-11-28 11:11:05 +01006319 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau87b09662015-04-03 00:22:06 +02006320 /* let's try again later from this stream. We add ourselves into
6321 * this stream's users so that it can remove us upon termination.
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006322 */
Willy Tarreaufe127932015-04-21 19:23:39 +02006323 si_applet_cant_put(si);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006324 return 0;
6325 }
6326
6327 /* get next entry */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006328 appctx->ctx.map.expr = pat_expr_get_next(appctx->ctx.map.expr,
6329 &appctx->ctx.map.ref->pat);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006330 }
6331
6332 appctx->st2 = STAT_ST_FIN;
6333 /* fall through */
6334
6335 default:
6336 appctx->st2 = STAT_ST_FIN;
6337 free(appctx->ctx.map.chunk.str);
6338 return 1;
6339 }
6340}
6341
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006342static int stats_pat_list(struct stream_interface *si)
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006343{
6344 struct appctx *appctx = __objt_appctx(si->end);
6345
6346 switch (appctx->st2) {
6347
6348 case STAT_ST_INIT:
6349 /* Init to the first entry. The list cannot be change */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006350 appctx->ctx.map.elt = LIST_NEXT(&appctx->ctx.map.ref->head,
6351 struct pat_ref_elt *, list);
6352 if (&appctx->ctx.map.elt->list == &appctx->ctx.map.ref->head)
6353 appctx->ctx.map.elt = NULL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006354 appctx->st2 = STAT_ST_LIST;
6355 /* fall through */
6356
6357 case STAT_ST_LIST:
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006358 while (appctx->ctx.map.elt) {
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006359 chunk_reset(&trash);
6360
6361 /* build messages */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006362 if (appctx->ctx.map.elt->sample)
Thierry FOURNIER9356c682014-01-28 15:55:37 +01006363 chunk_appendf(&trash, "%p %s %s\n",
6364 appctx->ctx.map.elt, appctx->ctx.map.elt->pattern,
6365 appctx->ctx.map.elt->sample);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006366 else
Thierry FOURNIER9356c682014-01-28 15:55:37 +01006367 chunk_appendf(&trash, "%p %s\n",
6368 appctx->ctx.map.elt, appctx->ctx.map.elt->pattern);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006369
Willy Tarreau2bb4a962014-11-28 11:11:05 +01006370 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau87b09662015-04-03 00:22:06 +02006371 /* let's try again later from this stream. We add ourselves into
6372 * this stream's users so that it can remove us upon termination.
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006373 */
Willy Tarreaufe127932015-04-21 19:23:39 +02006374 si_applet_cant_put(si);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006375 return 0;
6376 }
6377
6378 /* get next list entry and check the end of the list */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006379 appctx->ctx.map.elt = LIST_NEXT(&appctx->ctx.map.elt->list,
6380 struct pat_ref_elt *, list);
6381 if (&appctx->ctx.map.elt->list == &appctx->ctx.map.ref->head)
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006382 break;
6383 }
6384
6385 appctx->st2 = STAT_ST_FIN;
6386 /* fall through */
6387
6388 default:
6389 appctx->st2 = STAT_ST_FIN;
6390 return 1;
6391 }
6392}
6393
Willy Tarreau87b09662015-04-03 00:22:06 +02006394/* This function dumps all streams' states onto the stream interface's
Willy Tarreau306f8302013-07-08 15:53:06 +02006395 * read buffer. It returns 0 if the output buffer is full and it needs
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01006396 * to be called again, otherwise non-zero. It is designed to be called
6397 * from stats_dump_sess_to_buffer() below.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006398 */
Simon Horman9bd2c732011-06-15 15:18:44 +09006399static int stats_dump_sess_to_buffer(struct stream_interface *si)
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006400{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006401 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006402 struct connection *conn;
6403
Willy Tarreau2bb4a962014-11-28 11:11:05 +01006404 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW))) {
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006405 /* If we're forced to shut down, we might have to remove our
Willy Tarreau87b09662015-04-03 00:22:06 +02006406 * reference to the last stream being dumped.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006407 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006408 if (appctx->st2 == STAT_ST_LIST) {
6409 if (!LIST_ISEMPTY(&appctx->ctx.sess.bref.users)) {
6410 LIST_DEL(&appctx->ctx.sess.bref.users);
6411 LIST_INIT(&appctx->ctx.sess.bref.users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +01006412 }
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006413 }
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02006414 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006415 }
6416
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006417 chunk_reset(&trash);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006418
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006419 switch (appctx->st2) {
Willy Tarreau295a8372011-03-10 11:25:07 +01006420 case STAT_ST_INIT:
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006421 /* the function had not been called yet, let's prepare the
Willy Tarreau87b09662015-04-03 00:22:06 +02006422 * buffer for a response. We initialize the current stream
Willy Tarreaufd3828e2009-02-22 15:17:24 +01006423 * pointer to the first in the global list. When a target
Willy Tarreau87b09662015-04-03 00:22:06 +02006424 * stream is being destroyed, it is responsible for updating
Willy Tarreaufd3828e2009-02-22 15:17:24 +01006425 * this pointer. We know we have reached the end when this
Willy Tarreau87b09662015-04-03 00:22:06 +02006426 * pointer points back to the head of the streams list.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006427 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006428 LIST_INIT(&appctx->ctx.sess.bref.users);
Willy Tarreau87b09662015-04-03 00:22:06 +02006429 appctx->ctx.sess.bref.ref = streams.n;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006430 appctx->st2 = STAT_ST_LIST;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006431 /* fall through */
6432
Willy Tarreau295a8372011-03-10 11:25:07 +01006433 case STAT_ST_LIST:
Willy Tarreau87b09662015-04-03 00:22:06 +02006434 /* first, let's detach the back-ref from a possible previous stream */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006435 if (!LIST_ISEMPTY(&appctx->ctx.sess.bref.users)) {
6436 LIST_DEL(&appctx->ctx.sess.bref.users);
6437 LIST_INIT(&appctx->ctx.sess.bref.users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +01006438 }
6439
6440 /* and start from where we stopped */
Willy Tarreau87b09662015-04-03 00:22:06 +02006441 while (appctx->ctx.sess.bref.ref != &streams) {
Cyril Bontéacd7d632010-11-01 19:26:02 +01006442 char pn[INET6_ADDRSTRLEN];
Willy Tarreau87b09662015-04-03 00:22:06 +02006443 struct stream *curr_sess;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006444
Willy Tarreau87b09662015-04-03 00:22:06 +02006445 curr_sess = LIST_ELEM(appctx->ctx.sess.bref.ref, struct stream *, list);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006446
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006447 if (appctx->ctx.sess.target) {
6448 if (appctx->ctx.sess.target != (void *)-1 && appctx->ctx.sess.target != curr_sess)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006449 goto next_sess;
6450
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006451 LIST_ADDQ(&curr_sess->back_refs, &appctx->ctx.sess.bref.users);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006452 /* call the proper dump() function and return if we're missing space */
Willy Tarreau76153662012-11-26 01:16:39 +01006453 if (!stats_dump_full_sess_to_buffer(si, curr_sess))
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006454 return 0;
6455
Willy Tarreau87b09662015-04-03 00:22:06 +02006456 /* stream dump complete */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006457 LIST_DEL(&appctx->ctx.sess.bref.users);
6458 LIST_INIT(&appctx->ctx.sess.bref.users);
6459 if (appctx->ctx.sess.target != (void *)-1) {
6460 appctx->ctx.sess.target = NULL;
Willy Tarreau76153662012-11-26 01:16:39 +01006461 break;
6462 }
6463 else
6464 goto next_sess;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006465 }
6466
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006467 chunk_appendf(&trash,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006468 "%p: proto=%s",
6469 curr_sess,
Willy Tarreauf1e02122015-09-23 12:16:43 +02006470 strm_li(curr_sess) ? strm_li(curr_sess)->proto->name : "?");
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006471
Willy Tarreaud0d8da92015-04-04 02:10:38 +02006472 conn = objt_conn(strm_orig(curr_sess));
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006473 switch (conn ? addr_to_str(&conn->addr.from, pn, sizeof(pn)) : AF_UNSPEC) {
Willy Tarreau631f01c2011-09-05 00:36:48 +02006474 case AF_INET:
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006475 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006476 chunk_appendf(&trash,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006477 " src=%s:%d fe=%s be=%s srv=%s",
6478 pn,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006479 get_host_port(&conn->addr.from),
Willy Tarreaud0d8da92015-04-04 02:10:38 +02006480 strm_fe(curr_sess)->id,
Willy Tarreau50943332011-09-02 17:33:05 +02006481 (curr_sess->be->cap & PR_CAP_BE) ? curr_sess->be->id : "<NONE>",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006482 objt_server(curr_sess->target) ? objt_server(curr_sess->target)->id : "<none>"
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006483 );
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006484 break;
6485 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006486 chunk_appendf(&trash,
Emeric Brun837ca522010-10-22 16:19:01 +02006487 " src=unix:%d fe=%s be=%s srv=%s",
Willy Tarreaud0d8da92015-04-04 02:10:38 +02006488 strm_li(curr_sess)->luid,
6489 strm_fe(curr_sess)->id,
Willy Tarreau50943332011-09-02 17:33:05 +02006490 (curr_sess->be->cap & PR_CAP_BE) ? curr_sess->be->id : "<NONE>",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006491 objt_server(curr_sess->target) ? objt_server(curr_sess->target)->id : "<none>"
Emeric Brun837ca522010-10-22 16:19:01 +02006492 );
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006493 break;
6494 }
6495
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006496 chunk_appendf(&trash,
Willy Tarreau65671ab2009-10-04 14:24:59 +02006497 " ts=%02x age=%s calls=%d",
6498 curr_sess->task->state,
Willy Tarreau3884cba2009-03-28 17:54:35 +01006499 human_time(now.tv_sec - curr_sess->logs.tv_accept.tv_sec, 1),
6500 curr_sess->task->calls);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006501
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006502 chunk_appendf(&trash,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01006503 " rq[f=%06xh,i=%d,an=%02xh,rx=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006504 curr_sess->req.flags,
6505 curr_sess->req.buf->i,
6506 curr_sess->req.analysers,
6507 curr_sess->req.rex ?
6508 human_time(TICKS_TO_MS(curr_sess->req.rex - now_ms),
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006509 TICKS_TO_MS(1000)) : "");
6510
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006511 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006512 ",wx=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006513 curr_sess->req.wex ?
6514 human_time(TICKS_TO_MS(curr_sess->req.wex - now_ms),
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006515 TICKS_TO_MS(1000)) : "");
6516
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006517 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006518 ",ax=%s]",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006519 curr_sess->req.analyse_exp ?
6520 human_time(TICKS_TO_MS(curr_sess->req.analyse_exp - now_ms),
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006521 TICKS_TO_MS(1000)) : "");
6522
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006523 chunk_appendf(&trash,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01006524 " rp[f=%06xh,i=%d,an=%02xh,rx=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006525 curr_sess->res.flags,
6526 curr_sess->res.buf->i,
6527 curr_sess->res.analysers,
6528 curr_sess->res.rex ?
6529 human_time(TICKS_TO_MS(curr_sess->res.rex - now_ms),
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006530 TICKS_TO_MS(1000)) : "");
6531
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006532 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006533 ",wx=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006534 curr_sess->res.wex ?
6535 human_time(TICKS_TO_MS(curr_sess->res.wex - now_ms),
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006536 TICKS_TO_MS(1000)) : "");
6537
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006538 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006539 ",ax=%s]",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006540 curr_sess->res.analyse_exp ?
6541 human_time(TICKS_TO_MS(curr_sess->res.analyse_exp - now_ms),
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006542 TICKS_TO_MS(1000)) : "");
6543
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006544 conn = objt_conn(curr_sess->si[0].end);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006545 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006546 " s0=[%d,%1xh,fd=%d,ex=%s]",
6547 curr_sess->si[0].state,
6548 curr_sess->si[0].flags,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006549 conn ? conn->t.sock.fd : -1,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006550 curr_sess->si[0].exp ?
6551 human_time(TICKS_TO_MS(curr_sess->si[0].exp - now_ms),
6552 TICKS_TO_MS(1000)) : "");
6553
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006554 conn = objt_conn(curr_sess->si[1].end);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006555 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006556 " s1=[%d,%1xh,fd=%d,ex=%s]",
6557 curr_sess->si[1].state,
6558 curr_sess->si[1].flags,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006559 conn ? conn->t.sock.fd : -1,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006560 curr_sess->si[1].exp ?
6561 human_time(TICKS_TO_MS(curr_sess->si[1].exp - now_ms),
6562 TICKS_TO_MS(1000)) : "");
6563
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006564 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006565 " exp=%s",
6566 curr_sess->task->expire ?
6567 human_time(TICKS_TO_MS(curr_sess->task->expire - now_ms),
6568 TICKS_TO_MS(1000)) : "");
Willy Tarreau4726f532009-03-07 17:25:21 +01006569 if (task_in_rq(curr_sess->task))
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006570 chunk_appendf(&trash, " run(nice=%d)", curr_sess->task->nice);
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006571
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006572 chunk_appendf(&trash, "\n");
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006573
Willy Tarreau2bb4a962014-11-28 11:11:05 +01006574 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau87b09662015-04-03 00:22:06 +02006575 /* let's try again later from this stream. We add ourselves into
6576 * this stream's users so that it can remove us upon termination.
Willy Tarreaufd3828e2009-02-22 15:17:24 +01006577 */
Willy Tarreaufe127932015-04-21 19:23:39 +02006578 si_applet_cant_put(si);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006579 LIST_ADDQ(&curr_sess->back_refs, &appctx->ctx.sess.bref.users);
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02006580 return 0;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006581 }
6582
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006583 next_sess:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006584 appctx->ctx.sess.bref.ref = curr_sess->list.n;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006585 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006586
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006587 if (appctx->ctx.sess.target && appctx->ctx.sess.target != (void *)-1) {
Willy Tarreau87b09662015-04-03 00:22:06 +02006588 /* specified stream not found */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006589 if (appctx->ctx.sess.section > 0)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006590 chunk_appendf(&trash, " *** session terminated while we were watching it ***\n");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006591 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006592 chunk_appendf(&trash, "Session not found.\n");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006593
Willy Tarreaubc18da12015-03-13 14:00:47 +01006594 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02006595 si_applet_cant_put(si);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006596 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01006597 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006598
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006599 appctx->ctx.sess.target = NULL;
6600 appctx->ctx.sess.uid = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006601 return 1;
6602 }
6603
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006604 appctx->st2 = STAT_ST_FIN;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006605 /* fall through */
6606
6607 default:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006608 appctx->st2 = STAT_ST_FIN;
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02006609 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006610 }
Emeric Brun1e029aa2010-09-23 18:12:53 +02006611}
6612
Willy Tarreau5f9a8772012-11-26 02:22:40 +01006613/* This is called when the stream interface is closed. For instance, upon an
6614 * external abort, we won't call the i/o handler anymore so we may need to
Willy Tarreau87b09662015-04-03 00:22:06 +02006615 * remove back references to the stream currently being dumped.
Willy Tarreau5f9a8772012-11-26 02:22:40 +01006616 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02006617static void cli_release_handler(struct appctx *appctx)
Willy Tarreau5f9a8772012-11-26 02:22:40 +01006618{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006619 if (appctx->st0 == STAT_CLI_O_SESS && appctx->st2 == STAT_ST_LIST) {
6620 if (!LIST_ISEMPTY(&appctx->ctx.sess.bref.users))
6621 LIST_DEL(&appctx->ctx.sess.bref.users);
Willy Tarreau5f9a8772012-11-26 02:22:40 +01006622 }
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01006623 else if (appctx->st0 == STAT_CLI_PRINT_FREE) {
6624 free(appctx->ctx.cli.err);
Willy Tarreau6457d0f2015-10-07 20:00:24 +02006625 appctx->ctx.cli.err = NULL;
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01006626 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006627 else if (appctx->st0 == STAT_CLI_O_MLOOK) {
6628 free(appctx->ctx.map.chunk.str);
Willy Tarreau6457d0f2015-10-07 20:00:24 +02006629 appctx->ctx.map.chunk.str = NULL;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006630 }
Willy Tarreau5f9a8772012-11-26 02:22:40 +01006631}
6632
Willy Tarreau20e99322013-04-13 09:22:25 +02006633/* This function is used to either dump tables states (when action is set
6634 * to STAT_CLI_O_TAB) or clear tables (when action is STAT_CLI_O_CLR).
Willy Tarreau20e99322013-04-13 09:22:25 +02006635 * It returns 0 if the output buffer is full and it needs to be called
6636 * again, otherwise non-zero.
Willy Tarreau69f58c82010-07-12 17:55:33 +02006637 */
Willy Tarreau20e99322013-04-13 09:22:25 +02006638static int stats_table_request(struct stream_interface *si, int action)
Willy Tarreau69f58c82010-07-12 17:55:33 +02006639{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006640 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau87b09662015-04-03 00:22:06 +02006641 struct stream *s = si_strm(si);
Willy Tarreau69f58c82010-07-12 17:55:33 +02006642 struct ebmb_node *eb;
6643 int dt;
Willy Tarreau44455022012-12-05 23:01:12 +01006644 int skip_entry;
Willy Tarreau20e99322013-04-13 09:22:25 +02006645 int show = action == STAT_CLI_O_TAB;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006646
6647 /*
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006648 * We have 3 possible states in appctx->st2 :
Willy Tarreau295a8372011-03-10 11:25:07 +01006649 * - STAT_ST_INIT : the first call
6650 * - STAT_ST_INFO : the proxy pointer points to the next table to
Willy Tarreau69f58c82010-07-12 17:55:33 +02006651 * dump, the entry pointer is NULL ;
Willy Tarreau295a8372011-03-10 11:25:07 +01006652 * - STAT_ST_LIST : the proxy pointer points to the current table
Willy Tarreau69f58c82010-07-12 17:55:33 +02006653 * and the entry pointer points to the next entry to be dumped,
6654 * and the refcount on the next entry is held ;
Willy Tarreau295a8372011-03-10 11:25:07 +01006655 * - STAT_ST_END : nothing left to dump, the buffer may contain some
Willy Tarreau69f58c82010-07-12 17:55:33 +02006656 * data though.
6657 */
6658
Willy Tarreau2bb4a962014-11-28 11:11:05 +01006659 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW))) {
Willy Tarreau69f58c82010-07-12 17:55:33 +02006660 /* in case of abort, remove any refcount we might have set on an entry */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006661 if (appctx->st2 == STAT_ST_LIST) {
6662 appctx->ctx.table.entry->ref_cnt--;
6663 stksess_kill_if_expired(&appctx->ctx.table.proxy->table, appctx->ctx.table.entry);
Willy Tarreauf6efda12010-08-03 20:34:06 +02006664 }
Willy Tarreau69f58c82010-07-12 17:55:33 +02006665 return 1;
6666 }
6667
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006668 chunk_reset(&trash);
Willy Tarreau69f58c82010-07-12 17:55:33 +02006669
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006670 while (appctx->st2 != STAT_ST_FIN) {
6671 switch (appctx->st2) {
Willy Tarreau295a8372011-03-10 11:25:07 +01006672 case STAT_ST_INIT:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006673 appctx->ctx.table.proxy = appctx->ctx.table.target;
6674 if (!appctx->ctx.table.proxy)
6675 appctx->ctx.table.proxy = proxy;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006676
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006677 appctx->ctx.table.entry = NULL;
6678 appctx->st2 = STAT_ST_INFO;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006679 break;
6680
Willy Tarreau295a8372011-03-10 11:25:07 +01006681 case STAT_ST_INFO:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006682 if (!appctx->ctx.table.proxy ||
6683 (appctx->ctx.table.target &&
6684 appctx->ctx.table.proxy != appctx->ctx.table.target)) {
6685 appctx->st2 = STAT_ST_END;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006686 break;
6687 }
6688
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006689 if (appctx->ctx.table.proxy->table.size) {
6690 if (show && !stats_dump_table_head_to_buffer(&trash, si, appctx->ctx.table.proxy,
6691 appctx->ctx.table.target))
Willy Tarreau69f58c82010-07-12 17:55:33 +02006692 return 0;
6693
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006694 if (appctx->ctx.table.target &&
Willy Tarreaud0d8da92015-04-04 02:10:38 +02006695 strm_li(s)->bind_conf->level >= ACCESS_LVL_OPER) {
Willy Tarreau69f58c82010-07-12 17:55:33 +02006696 /* dump entries only if table explicitly requested */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006697 eb = ebmb_first(&appctx->ctx.table.proxy->table.keys);
Willy Tarreau69f58c82010-07-12 17:55:33 +02006698 if (eb) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006699 appctx->ctx.table.entry = ebmb_entry(eb, struct stksess, key);
6700 appctx->ctx.table.entry->ref_cnt++;
6701 appctx->st2 = STAT_ST_LIST;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006702 break;
6703 }
6704 }
6705 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006706 appctx->ctx.table.proxy = appctx->ctx.table.proxy->next;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006707 break;
6708
Willy Tarreau295a8372011-03-10 11:25:07 +01006709 case STAT_ST_LIST:
Willy Tarreau44455022012-12-05 23:01:12 +01006710 skip_entry = 0;
Simon Hormanc88b8872011-06-15 15:18:49 +09006711
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006712 if (appctx->ctx.table.data_type >= 0) {
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02006713 /* we're filtering on some data contents */
6714 void *ptr;
6715 long long data;
6716
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006717 dt = appctx->ctx.table.data_type;
6718 ptr = stktable_data_ptr(&appctx->ctx.table.proxy->table,
6719 appctx->ctx.table.entry,
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02006720 dt);
6721
6722 data = 0;
6723 switch (stktable_data_types[dt].std_type) {
6724 case STD_T_SINT:
6725 data = stktable_data_cast(ptr, std_t_sint);
6726 break;
6727 case STD_T_UINT:
6728 data = stktable_data_cast(ptr, std_t_uint);
6729 break;
6730 case STD_T_ULL:
6731 data = stktable_data_cast(ptr, std_t_ull);
6732 break;
6733 case STD_T_FRQP:
6734 data = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006735 appctx->ctx.table.proxy->table.data_arg[dt].u);
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02006736 break;
6737 }
6738
6739 /* skip the entry if the data does not match the test and the value */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006740 if ((data < appctx->ctx.table.value &&
6741 (appctx->ctx.table.data_op == STD_OP_EQ ||
6742 appctx->ctx.table.data_op == STD_OP_GT ||
6743 appctx->ctx.table.data_op == STD_OP_GE)) ||
6744 (data == appctx->ctx.table.value &&
6745 (appctx->ctx.table.data_op == STD_OP_NE ||
6746 appctx->ctx.table.data_op == STD_OP_GT ||
6747 appctx->ctx.table.data_op == STD_OP_LT)) ||
6748 (data > appctx->ctx.table.value &&
6749 (appctx->ctx.table.data_op == STD_OP_EQ ||
6750 appctx->ctx.table.data_op == STD_OP_LT ||
6751 appctx->ctx.table.data_op == STD_OP_LE)))
Willy Tarreau44455022012-12-05 23:01:12 +01006752 skip_entry = 1;
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02006753 }
6754
Simon Hormanc88b8872011-06-15 15:18:49 +09006755 if (show && !skip_entry &&
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006756 !stats_dump_table_entry_to_buffer(&trash, si, appctx->ctx.table.proxy,
6757 appctx->ctx.table.entry))
Simon Hormand9366582011-06-15 15:18:45 +09006758 return 0;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006759
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006760 appctx->ctx.table.entry->ref_cnt--;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006761
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006762 eb = ebmb_next(&appctx->ctx.table.entry->key);
Willy Tarreau69f58c82010-07-12 17:55:33 +02006763 if (eb) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006764 struct stksess *old = appctx->ctx.table.entry;
6765 appctx->ctx.table.entry = ebmb_entry(eb, struct stksess, key);
Willy Tarreau8fa52f42012-01-09 11:50:03 +01006766 if (show)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006767 stksess_kill_if_expired(&appctx->ctx.table.proxy->table, old);
6768 else if (!skip_entry && !appctx->ctx.table.entry->ref_cnt)
6769 stksess_kill(&appctx->ctx.table.proxy->table, old);
6770 appctx->ctx.table.entry->ref_cnt++;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006771 break;
6772 }
6773
Simon Hormanc88b8872011-06-15 15:18:49 +09006774
6775 if (show)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006776 stksess_kill_if_expired(&appctx->ctx.table.proxy->table, appctx->ctx.table.entry);
6777 else if (!skip_entry && !appctx->ctx.table.entry->ref_cnt)
6778 stksess_kill(&appctx->ctx.table.proxy->table, appctx->ctx.table.entry);
Simon Hormanc88b8872011-06-15 15:18:49 +09006779
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006780 appctx->ctx.table.proxy = appctx->ctx.table.proxy->next;
6781 appctx->st2 = STAT_ST_INFO;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006782 break;
6783
Willy Tarreau295a8372011-03-10 11:25:07 +01006784 case STAT_ST_END:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006785 appctx->st2 = STAT_ST_FIN;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006786 break;
6787 }
6788 }
6789 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006790}
6791
Willy Tarreaud426a182010-03-05 14:58:26 +01006792/* print a line of text buffer (limited to 70 bytes) to <out>. The format is :
Willy Tarreau74808cb2009-03-04 15:53:18 +01006793 * <2 spaces> <offset=5 digits> <space or plus> <space> <70 chars max> <\n>
6794 * which is 60 chars per line. Non-printable chars \t, \n, \r and \e are
6795 * encoded in C format. Other non-printable chars are encoded "\xHH". Original
6796 * lines are respected within the limit of 70 output chars. Lines that are
6797 * continuation of a previous truncated line begin with "+" instead of " "
6798 * after the offset. The new pointer is returned.
6799 */
Willy Tarreaud426a182010-03-05 14:58:26 +01006800static int dump_text_line(struct chunk *out, const char *buf, int bsize, int len,
6801 int *line, int ptr)
Willy Tarreau74808cb2009-03-04 15:53:18 +01006802{
6803 int end;
6804 unsigned char c;
6805
6806 end = out->len + 80;
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02006807 if (end > out->size)
Willy Tarreau74808cb2009-03-04 15:53:18 +01006808 return ptr;
6809
Willy Tarreau77804732012-10-29 16:14:26 +01006810 chunk_appendf(out, " %05d%c ", ptr, (ptr == *line) ? ' ' : '+');
Willy Tarreau74808cb2009-03-04 15:53:18 +01006811
Willy Tarreaud426a182010-03-05 14:58:26 +01006812 while (ptr < len && ptr < bsize) {
6813 c = buf[ptr];
Willy Tarreau787bbd92009-03-12 08:18:33 +01006814 if (isprint(c) && isascii(c) && c != '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006815 if (out->len > end - 2)
6816 break;
6817 out->str[out->len++] = c;
Willy Tarreau787bbd92009-03-12 08:18:33 +01006818 } else if (c == '\t' || c == '\n' || c == '\r' || c == '\e' || c == '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006819 if (out->len > end - 3)
6820 break;
6821 out->str[out->len++] = '\\';
6822 switch (c) {
6823 case '\t': c = 't'; break;
6824 case '\n': c = 'n'; break;
6825 case '\r': c = 'r'; break;
6826 case '\e': c = 'e'; break;
Willy Tarreau787bbd92009-03-12 08:18:33 +01006827 case '\\': c = '\\'; break;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006828 }
6829 out->str[out->len++] = c;
6830 } else {
6831 if (out->len > end - 5)
6832 break;
6833 out->str[out->len++] = '\\';
6834 out->str[out->len++] = 'x';
6835 out->str[out->len++] = hextab[(c >> 4) & 0xF];
6836 out->str[out->len++] = hextab[c & 0xF];
6837 }
Willy Tarreaud426a182010-03-05 14:58:26 +01006838 if (buf[ptr++] == '\n') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006839 /* we had a line break, let's return now */
6840 out->str[out->len++] = '\n';
6841 *line = ptr;
6842 return ptr;
6843 }
6844 }
6845 /* we have an incomplete line, we return it as-is */
6846 out->str[out->len++] = '\n';
6847 return ptr;
6848}
6849
Baptiste Assmann3863f972015-05-17 00:33:24 +02006850/* This function dumps counters from all resolvers section and associated name servers.
6851 * It returns 0 if the output buffer is full and it needs
6852 * to be called again, otherwise non-zero.
6853 */
6854static int stats_dump_resolvers_to_buffer(struct stream_interface *si)
6855{
6856 struct appctx *appctx = __objt_appctx(si->end);
6857 struct dns_resolvers *presolvers;
6858 struct dns_nameserver *pnameserver;
6859
6860 chunk_reset(&trash);
6861
6862 switch (appctx->st2) {
6863 case STAT_ST_INIT:
6864 appctx->st2 = STAT_ST_LIST; /* let's start producing data */
6865 /* fall through */
6866
6867 case STAT_ST_LIST:
Andrew Hayworth68d05342015-10-02 20:33:01 +00006868 if (LIST_ISEMPTY(&dns_resolvers)) {
6869 chunk_appendf(&trash, "No resolvers found\n");
6870 }
6871 else {
6872 list_for_each_entry(presolvers, &dns_resolvers, list) {
6873 if (appctx->ctx.resolvers.ptr != NULL && appctx->ctx.resolvers.ptr != presolvers)
6874 continue;
6875
6876 chunk_appendf(&trash, "Resolvers section %s\n", presolvers->id);
6877 list_for_each_entry(pnameserver, &presolvers->nameserver_list, list) {
6878 chunk_appendf(&trash, " nameserver %s:\n", pnameserver->id);
6879 chunk_appendf(&trash, " sent: %ld\n", pnameserver->counters.sent);
6880 chunk_appendf(&trash, " valid: %ld\n", pnameserver->counters.valid);
6881 chunk_appendf(&trash, " update: %ld\n", pnameserver->counters.update);
6882 chunk_appendf(&trash, " cname: %ld\n", pnameserver->counters.cname);
6883 chunk_appendf(&trash, " cname_error: %ld\n", pnameserver->counters.cname_error);
6884 chunk_appendf(&trash, " any_err: %ld\n", pnameserver->counters.any_err);
6885 chunk_appendf(&trash, " nx: %ld\n", pnameserver->counters.nx);
6886 chunk_appendf(&trash, " timeout: %ld\n", pnameserver->counters.timeout);
6887 chunk_appendf(&trash, " refused: %ld\n", pnameserver->counters.refused);
6888 chunk_appendf(&trash, " other: %ld\n", pnameserver->counters.other);
6889 chunk_appendf(&trash, " invalid: %ld\n", pnameserver->counters.invalid);
6890 chunk_appendf(&trash, " too_big: %ld\n", pnameserver->counters.too_big);
6891 chunk_appendf(&trash, " truncated: %ld\n", pnameserver->counters.truncated);
6892 chunk_appendf(&trash, " outdated: %ld\n", pnameserver->counters.outdated);
6893 }
6894 }
Baptiste Assmann3863f972015-05-17 00:33:24 +02006895 }
6896
6897 /* display response */
6898 if (bi_putchk(si_ic(si), &trash) == -1) {
6899 /* let's try again later from this session. We add ourselves into
6900 * this session's users so that it can remove us upon termination.
6901 */
6902 si->flags |= SI_FL_WAIT_ROOM;
6903 return 0;
6904 }
6905
6906 appctx->st2 = STAT_ST_FIN;
6907 /* fall through */
6908
6909 default:
6910 appctx->st2 = STAT_ST_FIN;
6911 return 1;
6912 }
6913}
6914
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02006915/* This function dumps all captured errors onto the stream interface's
Willy Tarreau306f8302013-07-08 15:53:06 +02006916 * read buffer. It returns 0 if the output buffer is full and it needs
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01006917 * to be called again, otherwise non-zero.
Willy Tarreau74808cb2009-03-04 15:53:18 +01006918 */
Simon Horman9bd2c732011-06-15 15:18:44 +09006919static int stats_dump_errors_to_buffer(struct stream_interface *si)
Willy Tarreau74808cb2009-03-04 15:53:18 +01006920{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006921 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau74808cb2009-03-04 15:53:18 +01006922 extern const char *monthname[12];
Willy Tarreau74808cb2009-03-04 15:53:18 +01006923
Willy Tarreau2bb4a962014-11-28 11:11:05 +01006924 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
Willy Tarreau61b34732009-10-03 23:49:35 +02006925 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006926
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006927 chunk_reset(&trash);
Willy Tarreau74808cb2009-03-04 15:53:18 +01006928
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006929 if (!appctx->ctx.errors.px) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006930 /* the function had not been called yet, let's prepare the
6931 * buffer for a response.
6932 */
Willy Tarreau10479e42010-12-12 14:00:34 +01006933 struct tm tm;
6934
6935 get_localtime(date.tv_sec, &tm);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006936 chunk_appendf(&trash, "Total events captured on [%02d/%s/%04d:%02d:%02d:%02d.%03d] : %u\n",
Willy Tarreau10479e42010-12-12 14:00:34 +01006937 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
6938 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(date.tv_usec/1000),
6939 error_snapshot_id);
6940
Willy Tarreau2bb4a962014-11-28 11:11:05 +01006941 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau10479e42010-12-12 14:00:34 +01006942 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreaufe127932015-04-21 19:23:39 +02006943 si_applet_cant_put(si);
Willy Tarreau10479e42010-12-12 14:00:34 +01006944 return 0;
6945 }
6946
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006947 appctx->ctx.errors.px = proxy;
6948 appctx->ctx.errors.buf = 0;
6949 appctx->ctx.errors.bol = 0;
6950 appctx->ctx.errors.ptr = -1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006951 }
6952
6953 /* we have two inner loops here, one for the proxy, the other one for
6954 * the buffer.
6955 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006956 while (appctx->ctx.errors.px) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006957 struct error_snapshot *es;
6958
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006959 if (appctx->ctx.errors.buf == 0)
6960 es = &appctx->ctx.errors.px->invalid_req;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006961 else
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006962 es = &appctx->ctx.errors.px->invalid_rep;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006963
6964 if (!es->when.tv_sec)
6965 goto next;
6966
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006967 if (appctx->ctx.errors.iid >= 0 &&
6968 appctx->ctx.errors.px->uuid != appctx->ctx.errors.iid &&
6969 es->oe->uuid != appctx->ctx.errors.iid)
Willy Tarreau74808cb2009-03-04 15:53:18 +01006970 goto next;
6971
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006972 if (appctx->ctx.errors.ptr < 0) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006973 /* just print headers now */
6974
6975 char pn[INET6_ADDRSTRLEN];
6976 struct tm tm;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02006977 int port;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006978
6979 get_localtime(es->when.tv_sec, &tm);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006980 chunk_appendf(&trash, " \n[%02d/%s/%04d:%02d:%02d:%02d.%03d]",
Willy Tarreau74808cb2009-03-04 15:53:18 +01006981 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
Willy Tarreau1772ece2009-04-03 14:49:12 +02006982 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(es->when.tv_usec/1000));
Willy Tarreau74808cb2009-03-04 15:53:18 +01006983
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02006984 switch (addr_to_str(&es->src, pn, sizeof(pn))) {
6985 case AF_INET:
6986 case AF_INET6:
6987 port = get_host_port(&es->src);
6988 break;
6989 default:
6990 port = 0;
6991 }
6992
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006993 switch (appctx->ctx.errors.buf) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006994 case 0:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006995 chunk_appendf(&trash,
Willy Tarreau74808cb2009-03-04 15:53:18 +01006996 " frontend %s (#%d): invalid request\n"
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02006997 " backend %s (#%d)",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006998 appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02006999 (es->oe->cap & PR_CAP_BE) ? es->oe->id : "<NONE>",
7000 (es->oe->cap & PR_CAP_BE) ? es->oe->uuid : -1);
Willy Tarreau74808cb2009-03-04 15:53:18 +01007001 break;
7002 case 1:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007003 chunk_appendf(&trash,
Olivier Doucet08afdcb2014-09-08 11:23:00 +02007004 " backend %s (#%d): invalid response\n"
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02007005 " frontend %s (#%d)",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01007006 appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02007007 es->oe->id, es->oe->uuid);
Willy Tarreau74808cb2009-03-04 15:53:18 +01007008 break;
7009 }
7010
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007011 chunk_appendf(&trash,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02007012 ", server %s (#%d), event #%u\n"
7013 " src %s:%d, session #%d, session flags 0x%08x\n"
7014 " HTTP msg state %d, msg flags 0x%08x, tx flags 0x%08x\n"
7015 " HTTP chunk len %lld bytes, HTTP body len %lld bytes\n"
7016 " buffer flags 0x%08x, out %d bytes, total %lld bytes\n"
7017 " pending %d bytes, wrapping at %d, error at position %d:\n \n",
7018 es->srv ? es->srv->id : "<NONE>", es->srv ? es->srv->puid : -1,
7019 es->ev_id,
7020 pn, port, es->sid, es->s_flags,
7021 es->state, es->m_flags, es->t_flags,
7022 es->m_clen, es->m_blen,
7023 es->b_flags, es->b_out, es->b_tot,
7024 es->len, es->b_wrap, es->pos);
7025
Willy Tarreau2bb4a962014-11-28 11:11:05 +01007026 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01007027 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreaufe127932015-04-21 19:23:39 +02007028 si_applet_cant_put(si);
Willy Tarreau61b34732009-10-03 23:49:35 +02007029 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01007030 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01007031 appctx->ctx.errors.ptr = 0;
7032 appctx->ctx.errors.sid = es->sid;
Willy Tarreau74808cb2009-03-04 15:53:18 +01007033 }
7034
Willy Tarreau7b4b4992013-12-01 09:15:12 +01007035 if (appctx->ctx.errors.sid != es->sid) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01007036 /* the snapshot changed while we were dumping it */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007037 chunk_appendf(&trash,
Willy Tarreau74808cb2009-03-04 15:53:18 +01007038 " WARNING! update detected on this snapshot, dump interrupted. Please re-check!\n");
Willy Tarreaubc18da12015-03-13 14:00:47 +01007039 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02007040 si_applet_cant_put(si);
Willy Tarreau61b34732009-10-03 23:49:35 +02007041 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01007042 }
Willy Tarreau74808cb2009-03-04 15:53:18 +01007043 goto next;
7044 }
7045
7046 /* OK, ptr >= 0, so we have to dump the current line */
Willy Tarreauf3764b72016-03-31 13:45:10 +02007047 while (es->buf && appctx->ctx.errors.ptr < es->len && appctx->ctx.errors.ptr < global.tune.bufsize) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01007048 int newptr;
7049 int newline;
7050
Willy Tarreau7b4b4992013-12-01 09:15:12 +01007051 newline = appctx->ctx.errors.bol;
Willy Tarreauf3764b72016-03-31 13:45:10 +02007052 newptr = dump_text_line(&trash, es->buf, global.tune.bufsize, es->len, &newline, appctx->ctx.errors.ptr);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01007053 if (newptr == appctx->ctx.errors.ptr)
Willy Tarreau61b34732009-10-03 23:49:35 +02007054 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01007055
Willy Tarreau2bb4a962014-11-28 11:11:05 +01007056 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01007057 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreaufe127932015-04-21 19:23:39 +02007058 si_applet_cant_put(si);
Willy Tarreau61b34732009-10-03 23:49:35 +02007059 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01007060 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01007061 appctx->ctx.errors.ptr = newptr;
7062 appctx->ctx.errors.bol = newline;
Willy Tarreau74808cb2009-03-04 15:53:18 +01007063 };
7064 next:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01007065 appctx->ctx.errors.bol = 0;
7066 appctx->ctx.errors.ptr = -1;
7067 appctx->ctx.errors.buf++;
7068 if (appctx->ctx.errors.buf > 1) {
7069 appctx->ctx.errors.buf = 0;
7070 appctx->ctx.errors.px = appctx->ctx.errors.px->next;
Willy Tarreau74808cb2009-03-04 15:53:18 +01007071 }
Willy Tarreauae795722016-02-16 11:27:28 +01007072 }
7073
7074 /* dump complete */
7075 return 1;
7076}
7077
7078/* This function dumps all environmnent variables to the buffer. It returns 0
7079 * if the output buffer is full and it needs to be called again, otherwise
7080 * non-zero. Dumps only one entry if st2 == STAT_ST_END.
7081 */
7082static int stats_dump_env_to_buffer(struct stream_interface *si)
7083{
7084 struct appctx *appctx = __objt_appctx(si->end);
7085
7086 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
7087 return 1;
7088
7089 chunk_reset(&trash);
7090
7091 /* we have two inner loops here, one for the proxy, the other one for
7092 * the buffer.
7093 */
7094 while (*appctx->ctx.env.var) {
7095 chunk_printf(&trash, "%s\n", *appctx->ctx.env.var);
7096
7097 if (bi_putchk(si_ic(si), &trash) == -1) {
7098 si_applet_cant_put(si);
7099 return 0;
7100 }
7101 if (appctx->st2 == STAT_ST_END)
7102 break;
7103 appctx->ctx.env.var++;
Willy Tarreau74808cb2009-03-04 15:53:18 +01007104 }
7105
7106 /* dump complete */
Willy Tarreau61b34732009-10-03 23:49:35 +02007107 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01007108}
7109
Willy Tarreaud5781202012-09-22 19:32:35 +02007110/* parse the "level" argument on the bind lines */
7111static int bind_parse_level(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
7112{
7113 if (!*args[cur_arg + 1]) {
7114 memprintf(err, "'%s' : missing level", args[cur_arg]);
7115 return ERR_ALERT | ERR_FATAL;
7116 }
7117
7118 if (!strcmp(args[cur_arg+1], "user"))
7119 conf->level = ACCESS_LVL_USER;
7120 else if (!strcmp(args[cur_arg+1], "operator"))
7121 conf->level = ACCESS_LVL_OPER;
7122 else if (!strcmp(args[cur_arg+1], "admin"))
7123 conf->level = ACCESS_LVL_ADMIN;
7124 else {
7125 memprintf(err, "'%s' only supports 'user', 'operator', and 'admin' (got '%s')",
7126 args[cur_arg], args[cur_arg+1]);
7127 return ERR_ALERT | ERR_FATAL;
7128 }
7129
7130 return 0;
7131}
7132
Willy Tarreau30576452015-04-13 13:50:30 +02007133struct applet http_stats_applet = {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007134 .obj_type = OBJ_TYPE_APPLET,
Willy Tarreaub24281b2011-02-13 13:16:36 +01007135 .name = "<STATS>", /* used for logging */
7136 .fct = http_stats_io_handler,
Aman Gupta9a13e842012-04-02 18:57:53 -07007137 .release = NULL,
Willy Tarreaub24281b2011-02-13 13:16:36 +01007138};
7139
Willy Tarreau30576452015-04-13 13:50:30 +02007140static struct applet cli_applet = {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007141 .obj_type = OBJ_TYPE_APPLET,
Willy Tarreaub24281b2011-02-13 13:16:36 +01007142 .name = "<CLI>", /* used for logging */
7143 .fct = cli_io_handler,
Willy Tarreau5f9a8772012-11-26 02:22:40 +01007144 .release = cli_release_handler,
Willy Tarreaub24281b2011-02-13 13:16:36 +01007145};
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01007146
Willy Tarreaudc13c112013-06-21 23:16:39 +02007147static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreau10522fd2008-07-09 20:12:41 +02007148 { CFG_GLOBAL, "stats", stats_parse_global },
7149 { 0, NULL, NULL },
7150}};
7151
Willy Tarreaud5781202012-09-22 19:32:35 +02007152static struct bind_kw_list bind_kws = { "STAT", { }, {
7153 { "level", bind_parse_level, 1 }, /* set the unix socket admin level */
7154 { NULL, NULL, 0 },
7155}};
7156
Willy Tarreau10522fd2008-07-09 20:12:41 +02007157__attribute__((constructor))
7158static void __dumpstats_module_init(void)
7159{
7160 cfg_register_keywords(&cfg_kws);
Willy Tarreaud5781202012-09-22 19:32:35 +02007161 bind_register_keywords(&bind_kws);
Willy Tarreau10522fd2008-07-09 20:12:41 +02007162}
7163
Willy Tarreau91861262007-10-17 17:06:05 +02007164/*
7165 * Local variables:
7166 * c-indent-level: 8
7167 * c-basic-offset: 8
7168 * End:
7169 */