blob: 447e6f0cc37e7e6de51c374d3a82d20fb0b23ada [file] [log] [blame]
Willy Tarreau91861262007-10-17 17:06:05 +02001/*
Willy Tarreaueb472682010-05-28 18:46:57 +02002 * Functions dedicated to statistics output and the stats socket
Willy Tarreau91861262007-10-17 17:06:05 +02003 *
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004 * Copyright 2000-2012 Willy Tarreau <w@1wt.eu>
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02005 * Copyright 2007-2009 Krzysztof Piotr Oledzki <ole@ans.pl>
Willy Tarreau91861262007-10-17 17:06:05 +02006 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
11 *
12 */
13
14#include <ctype.h>
15#include <errno.h>
16#include <fcntl.h>
17#include <stdio.h>
18#include <stdlib.h>
19#include <string.h>
Willy Tarreaufbee7132007-10-18 13:53:22 +020020#include <pwd.h>
21#include <grp.h>
Willy Tarreau91861262007-10-17 17:06:05 +020022
23#include <sys/socket.h>
24#include <sys/stat.h>
25#include <sys/types.h>
26
Willy Tarreau10522fd2008-07-09 20:12:41 +020027#include <common/cfgparse.h>
Willy Tarreau91861262007-10-17 17:06:05 +020028#include <common/compat.h>
29#include <common/config.h>
30#include <common/debug.h>
31#include <common/memory.h>
32#include <common/mini-clist.h>
33#include <common/standard.h>
Willy Tarreau0c303ee2008-07-07 00:09:58 +020034#include <common/ticks.h>
Willy Tarreau91861262007-10-17 17:06:05 +020035#include <common/time.h>
36#include <common/uri_auth.h>
37#include <common/version.h>
Emeric Brun4147b2e2014-06-16 18:36:30 +020038#include <common/base64.h>
Willy Tarreau91861262007-10-17 17:06:05 +020039
Willy Tarreau8a8d83b2015-04-13 13:24:54 +020040#include <types/applet.h>
Willy Tarreau91861262007-10-17 17:06:05 +020041#include <types/global.h>
Baptiste Assmann3863f972015-05-17 00:33:24 +020042#include <types/dns.h>
Willy Tarreau91861262007-10-17 17:06:05 +020043
44#include <proto/backend.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020045#include <proto/channel.h>
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +020046#include <proto/checks.h>
William Lallemande3a7d992012-11-20 11:25:20 +010047#include <proto/compression.h>
Willy Tarreau91861262007-10-17 17:06:05 +020048#include <proto/dumpstats.h>
49#include <proto/fd.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010050#include <proto/freq_ctr.h>
Willy Tarreau32b60d42015-03-13 16:14:57 +010051#include <proto/frontend.h>
Willy Tarreaueb472682010-05-28 18:46:57 +020052#include <proto/log.h>
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +010053#include <proto/pattern.h>
Willy Tarreaua206fa92009-01-25 14:02:00 +010054#include <proto/pipe.h>
Willy Tarreaud1d54542012-09-12 22:58:11 +020055#include <proto/listener.h>
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +010056#include <proto/map.h>
Willy Tarreaue6d97022012-11-23 11:19:33 +010057#include <proto/proto_http.h>
Willy Tarreaufbee7132007-10-18 13:53:22 +020058#include <proto/proto_uxst.h>
Willy Tarreau89a63132009-08-16 17:41:45 +020059#include <proto/proxy.h>
Willy Tarreau1cf8f082014-02-07 12:14:54 +010060#include <proto/sample.h>
Willy Tarreau9903f0e2015-04-04 18:50:31 +020061#include <proto/session.h>
Willy Tarreau87b09662015-04-03 00:22:06 +020062#include <proto/stream.h>
Krzysztof Oledzki85130942007-10-22 16:21:10 +020063#include <proto/server.h>
Willy Tarreau75bf2c92012-08-20 17:01:35 +020064#include <proto/raw_sock.h>
Willy Tarreaudded32d2008-11-30 19:48:07 +010065#include <proto/stream_interface.h>
Willy Tarreau4726f532009-03-07 17:25:21 +010066#include <proto/task.h>
Willy Tarreau91861262007-10-17 17:06:05 +020067
Willy Tarreau7a0169a2012-11-19 17:13:16 +010068#ifdef USE_OPENSSL
69#include <proto/ssl_sock.h>
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +020070#include <types/ssl_sock.h>
Willy Tarreau7a0169a2012-11-19 17:13:16 +010071#endif
72
Willy Tarreau91b843d2014-01-28 16:27:17 +010073/* stats socket states */
74enum {
75 STAT_CLI_INIT = 0, /* initial state, must leave to zero ! */
76 STAT_CLI_END, /* final state, let's close */
77 STAT_CLI_GETREQ, /* wait for a request */
78 STAT_CLI_OUTPUT, /* all states after this one are responses */
79 STAT_CLI_PROMPT, /* display the prompt (first output, same code) */
80 STAT_CLI_PRINT, /* display message in cli->msg */
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +010081 STAT_CLI_PRINT_FREE, /* display message in cli->msg. After the display, free the pointer */
Willy Tarreau91b843d2014-01-28 16:27:17 +010082 STAT_CLI_O_INFO, /* dump info */
Willy Tarreau87b09662015-04-03 00:22:06 +020083 STAT_CLI_O_SESS, /* dump streams */
Willy Tarreau91b843d2014-01-28 16:27:17 +010084 STAT_CLI_O_ERR, /* dump errors */
85 STAT_CLI_O_TAB, /* dump tables */
86 STAT_CLI_O_CLR, /* clear tables */
87 STAT_CLI_O_SET, /* set entries in tables */
88 STAT_CLI_O_STAT, /* dump stats */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +010089 STAT_CLI_O_PATS, /* list all pattern reference avalaible */
90 STAT_CLI_O_PAT, /* list all entries of a pattern */
Willy Tarreau91b843d2014-01-28 16:27:17 +010091 STAT_CLI_O_MLOOK, /* lookup a map entry */
Willy Tarreau12833bb2014-01-28 16:49:56 +010092 STAT_CLI_O_POOLS, /* dump memory pools */
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +020093 STAT_CLI_O_TLSK, /* list all TLS ticket keys references */
Baptiste Assmann3863f972015-05-17 00:33:24 +020094 STAT_CLI_O_RESOLVERS,/* dump a resolver's section nameservers counters */
Baptiste Assmann28289462015-07-03 08:01:20 +020095 STAT_CLI_O_SERVERS_STATE, /* dump server state and changing information */
Baptiste Assmann9b6857e2015-09-18 14:49:12 +020096 STAT_CLI_O_BACKEND, /* dump backend list */
Willy Tarreauae795722016-02-16 11:27:28 +010097 STAT_CLI_O_ENV, /* dump environment */
Willy Tarreau91b843d2014-01-28 16:27:17 +010098};
99
Willy Tarreaued7df902014-05-22 18:04:49 +0200100/* Actions available for the stats admin forms */
101enum {
102 ST_ADM_ACTION_NONE = 0,
Willy Tarreau248a60e2014-05-23 14:59:48 +0200103
104 /* enable/disable health checks */
105 ST_ADM_ACTION_DHLTH,
106 ST_ADM_ACTION_EHLTH,
107
108 /* force health check status */
109 ST_ADM_ACTION_HRUNN,
110 ST_ADM_ACTION_HNOLB,
111 ST_ADM_ACTION_HDOWN,
112
113 /* enable/disable agent checks */
114 ST_ADM_ACTION_DAGENT,
115 ST_ADM_ACTION_EAGENT,
116
117 /* force agent check status */
118 ST_ADM_ACTION_ARUNN,
119 ST_ADM_ACTION_ADOWN,
120
121 /* set admin state */
Willy Tarreaued7df902014-05-22 18:04:49 +0200122 ST_ADM_ACTION_READY,
123 ST_ADM_ACTION_DRAIN,
124 ST_ADM_ACTION_MAINT,
125 ST_ADM_ACTION_SHUTDOWN,
126 /* these are the ancient actions, still available for compatibility */
127 ST_ADM_ACTION_DISABLE,
128 ST_ADM_ACTION_ENABLE,
129 ST_ADM_ACTION_STOP,
130 ST_ADM_ACTION_START,
131};
132
Willy Tarreauc4832de2016-01-11 18:12:26 +0100133
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
Baptiste Assmann28289462015-07-03 08:01:20 +0200306static void dump_servers_state(struct proxy *backend, struct chunk *buf);
307
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"
Baptiste Assmann3863f972015-05-17 00:33:24 +0200316 * -> stats_dump_resolvers_to_buffer() // "show stat resolver <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"
353 " show errors : report last request and response errors for each proxy\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +0100354 " show sess [id] : report the list of current sessions or dump this session\n"
Willy Tarreau69f58c82010-07-12 17:55:33 +0200355 " show table [id]: report table usage stats or dump this table's contents\n"
Baptiste Assmann28289462015-07-03 08:01:20 +0200356 " show servers state [id]: dump volatile server information (for backend <id>)\n"
Willy Tarreau38338fa2009-10-10 18:37:29 +0200357 " get weight : report a server's current weight\n"
Willy Tarreau4483d432009-10-10 19:30:08 +0200358 " set weight : change a server's weight\n"
Baptiste Assmann3d8f8312015-04-13 22:54:33 +0200359 " set server : change a server's state, weight or address\n"
Willy Tarreau654694e2012-06-07 01:03:16 +0200360 " set table [id] : update or create a table entry's data\n"
Willy Tarreau7aabd112010-01-26 10:59:06 +0100361 " set timeout : change a timeout setting\n"
Willy Tarreau2a0f4d22011-08-02 11:49:05 +0200362 " set maxconn : change a maxconn setting\n"
Willy Tarreauf5b22872011-09-07 16:13:44 +0200363 " set rate-limit : change a rate limiting value\n"
Willy Tarreaua295edc2011-09-07 23:21:03 +0200364 " disable : put a server or frontend in maintenance mode\n"
365 " enable : re-enable a server or frontend which is in maintenance mode\n"
366 " shutdown : kill a session or a frontend (eg:to release listening ports)\n"
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100367 " show acl [id] : report avalaible acls or dump an acl's contents\n"
368 " get acl : reports the patterns matching a sample for an ACL\n"
369 " add acl : add acl entry\n"
370 " del acl : delete acl entry\n"
371 " clear acl <id> : clear the content of this acl\n"
Thierry FOURNIER1432a0c2014-03-11 13:42:38 +0100372 " show map [id] : report avalaible maps or dump a map's contents\n"
373 " get map : reports the keys and values matching a sample for a map\n"
374 " set map : modify map entry\n"
375 " add map : add map entry\n"
376 " del map : delete map entry\n"
377 " clear map <id> : clear the content of this map\n"
Emeric Brun4147b2e2014-06-16 18:36:30 +0200378 " set ssl <stmt> : set statement for ssl\n"
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200379 "";
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200380
Simon Horman9bd2c732011-06-15 15:18:44 +0900381static const char stats_permission_denied_msg[] =
Willy Tarreau6162db22009-10-10 17:13:00 +0200382 "Permission denied\n"
383 "";
384
Willy Tarreau295a8372011-03-10 11:25:07 +0100385/* data transmission states for the stats responses */
386enum {
387 STAT_ST_INIT = 0,
388 STAT_ST_HEAD,
389 STAT_ST_INFO,
390 STAT_ST_LIST,
391 STAT_ST_END,
392 STAT_ST_FIN,
393};
394
395/* data transmission states for the stats responses inside a proxy */
396enum {
397 STAT_PX_ST_INIT = 0,
398 STAT_PX_ST_TH,
399 STAT_PX_ST_FE,
400 STAT_PX_ST_LI,
401 STAT_PX_ST_SV,
402 STAT_PX_ST_BE,
403 STAT_PX_ST_END,
404 STAT_PX_ST_FIN,
405};
406
Cyril Bonté19979e12012-04-04 12:57:21 +0200407extern const char *stat_status_codes[];
408
Willy Tarreau07e9e642010-08-17 21:48:17 +0200409/* allocate a new stats frontend named <name>, and return it
410 * (or NULL in case of lack of memory).
411 */
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200412static struct proxy *alloc_stats_fe(const char *name, const char *file, int line)
Willy Tarreau07e9e642010-08-17 21:48:17 +0200413{
414 struct proxy *fe;
415
416 fe = (struct proxy *)calloc(1, sizeof(struct proxy));
417 if (!fe)
418 return NULL;
419
Willy Tarreau237250c2011-07-29 01:49:03 +0200420 init_new_proxy(fe);
Willy Tarreau050536d2012-10-04 08:47:34 +0200421 fe->next = proxy;
422 proxy = fe;
Willy Tarreau07e9e642010-08-17 21:48:17 +0200423 fe->last_change = now.tv_sec;
424 fe->id = strdup("GLOBAL");
425 fe->cap = PR_CAP_FE;
Willy Tarreauc2adf8b2011-09-07 12:13:34 +0200426 fe->maxconn = 10; /* default to 10 concurrent connections */
427 fe->timeout.client = MS_TO_TICKS(10000); /* default timeout of 10 seconds */
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200428 fe->conf.file = strdup(file);
429 fe->conf.line = line;
Willy Tarreau32b60d42015-03-13 16:14:57 +0100430 fe->accept = frontend_accept;
Willy Tarreauf87ab942015-03-13 15:55:16 +0100431 fe->default_target = &cli_applet.obj_type;
Willy Tarreau050536d2012-10-04 08:47:34 +0200432
433 /* the stats frontend is the only one able to assign ID #0 */
434 fe->conf.id.key = fe->uuid = 0;
435 eb32_insert(&used_proxy_id, &fe->conf.id);
Willy Tarreau07e9e642010-08-17 21:48:17 +0200436 return fe;
437}
438
Willy Tarreaufbee7132007-10-18 13:53:22 +0200439/* This function parses a "stats" statement in the "global" section. It returns
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200440 * -1 if there is any error, otherwise zero. If it returns -1, it will write an
441 * error message into the <err> buffer which will be preallocated. The trailing
442 * '\n' must not be written. The function must be called with <args> pointing to
443 * the first word after "stats".
Willy Tarreaufbee7132007-10-18 13:53:22 +0200444 */
Willy Tarreau10522fd2008-07-09 20:12:41 +0200445static int stats_parse_global(char **args, int section_type, struct proxy *curpx,
Willy Tarreau28a47d62012-09-18 20:02:48 +0200446 struct proxy *defpx, const char *file, int line,
447 char **err)
Willy Tarreaufbee7132007-10-18 13:53:22 +0200448{
Willy Tarreau4348fad2012-09-20 16:48:07 +0200449 struct bind_conf *bind_conf;
Willy Tarreauc53d4222012-09-20 20:19:28 +0200450 struct listener *l;
Willy Tarreau4348fad2012-09-20 16:48:07 +0200451
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200452 if (!strcmp(args[1], "socket")) {
Willy Tarreaufbee7132007-10-18 13:53:22 +0200453 int cur_arg;
454
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200455 if (*args[2] == 0) {
Willy Tarreauc53d4222012-09-20 20:19:28 +0200456 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 +0200457 return -1;
458 }
459
Willy Tarreau89a63132009-08-16 17:41:45 +0200460 if (!global.stats_fe) {
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200461 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200462 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
Willy Tarreau89a63132009-08-16 17:41:45 +0200463 return -1;
464 }
Willy Tarreau89a63132009-08-16 17:41:45 +0200465 }
466
Willy Tarreau4348fad2012-09-20 16:48:07 +0200467 bind_conf = bind_conf_alloc(&global.stats_fe->conf.bind, file, line, args[2]);
Willy Tarreau290e63a2012-09-20 18:07:14 +0200468 bind_conf->level = ACCESS_LVL_OPER; /* default access level */
Willy Tarreau4348fad2012-09-20 16:48:07 +0200469
Willy Tarreauc53d4222012-09-20 20:19:28 +0200470 if (!str2listener(args[2], global.stats_fe, bind_conf, file, line, err)) {
471 memprintf(err, "parsing [%s:%d] : '%s %s' : %s\n",
472 file, line, args[0], args[1], err && *err ? *err : "error");
473 return -1;
474 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200475
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200476 cur_arg = 3;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200477 while (*args[cur_arg]) {
Willy Tarreaud5781202012-09-22 19:32:35 +0200478 static int bind_dumped;
479 struct bind_kw *kw;
480
481 kw = bind_find_kw(args[cur_arg]);
482 if (kw) {
483 if (!kw->parse) {
484 memprintf(err, "'%s %s' : '%s' option is not implemented in this version (check build options).",
485 args[0], args[1], args[cur_arg]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200486 return -1;
487 }
Willy Tarreaud5781202012-09-22 19:32:35 +0200488
Willy Tarreau061b5de2015-10-13 15:06:57 +0200489 if (kw->parse(args, cur_arg, global.stats_fe, bind_conf, err) != 0) {
Willy Tarreaud5781202012-09-22 19:32:35 +0200490 if (err && *err)
491 memprintf(err, "'%s %s' : '%s'", args[0], args[1], *err);
492 else
493 memprintf(err, "'%s %s' : error encountered while processing '%s'",
494 args[0], args[1], args[cur_arg]);
Willy Tarreau6162db22009-10-10 17:13:00 +0200495 return -1;
496 }
Willy Tarreaud5781202012-09-22 19:32:35 +0200497
498 cur_arg += 1 + kw->skip;
499 continue;
Willy Tarreau6162db22009-10-10 17:13:00 +0200500 }
Willy Tarreaud5781202012-09-22 19:32:35 +0200501
502 if (!bind_dumped) {
503 bind_dump_kws(err);
504 indent_msg(err, 4);
505 bind_dumped = 1;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200506 }
Willy Tarreaud5781202012-09-22 19:32:35 +0200507
508 memprintf(err, "'%s %s' : unknown keyword '%s'.%s%s",
509 args[0], args[1], args[cur_arg],
510 err && *err ? " Registered keywords :" : "", err && *err ? *err : "");
511 return -1;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200512 }
Willy Tarreaub1356cf2008-12-07 16:06:43 +0100513
Willy Tarreauc53d4222012-09-20 20:19:28 +0200514 list_for_each_entry(l, &bind_conf->listeners, by_bind) {
515 l->maxconn = global.stats_fe->maxconn;
516 l->backlog = global.stats_fe->backlog;
Willy Tarreau9903f0e2015-04-04 18:50:31 +0200517 l->accept = session_accept_fd;
Willy Tarreau87b09662015-04-03 00:22:06 +0200518 l->handler = process_stream;
Willy Tarreau10b688f2015-03-13 16:43:12 +0100519 l->default_target = global.stats_fe->default_target;
Willy Tarreauc53d4222012-09-20 20:19:28 +0200520 l->options |= LI_O_UNLIMITED; /* don't make the peers subject to global limits */
521 l->nice = -64; /* we want to boost priority for local stats */
522 global.maxsock += l->maxconn;
523 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200524 }
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200525 else if (!strcmp(args[1], "timeout")) {
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100526 unsigned timeout;
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200527 const char *res = parse_time_err(args[2], &timeout, TIME_UNIT_MS);
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100528
529 if (res) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200530 memprintf(err, "'%s %s' : unexpected character '%c'", args[0], args[1], *res);
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100531 return -1;
532 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200533
Willy Tarreaub3f32f52007-12-02 22:15:14 +0100534 if (!timeout) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200535 memprintf(err, "'%s %s' expects a positive value", args[0], args[1]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200536 return -1;
537 }
Willy Tarreau07e9e642010-08-17 21:48:17 +0200538 if (!global.stats_fe) {
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200539 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200540 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
Willy Tarreau07e9e642010-08-17 21:48:17 +0200541 return -1;
542 }
543 }
Willy Tarreau89a63132009-08-16 17:41:45 +0200544 global.stats_fe->timeout.client = MS_TO_TICKS(timeout);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200545 }
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200546 else if (!strcmp(args[1], "maxconn")) {
547 int maxconn = atol(args[2]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200548
549 if (maxconn <= 0) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200550 memprintf(err, "'%s %s' expects a positive value", args[0], args[1]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200551 return -1;
552 }
Willy Tarreauc2adf8b2011-09-07 12:13:34 +0200553
554 if (!global.stats_fe) {
Willy Tarreaua020fbd2012-09-18 20:05:00 +0200555 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200556 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
Willy Tarreauc2adf8b2011-09-07 12:13:34 +0200557 return -1;
558 }
559 }
560 global.stats_fe->maxconn = maxconn;
Willy Tarreaufbee7132007-10-18 13:53:22 +0200561 }
Willy Tarreau35b7b162012-10-22 23:17:18 +0200562 else if (!strcmp(args[1], "bind-process")) { /* enable the socket only on some processes */
563 int cur_arg = 2;
Willy Tarreaua9db57e2013-01-18 11:29:29 +0100564 unsigned long set = 0;
Willy Tarreau35b7b162012-10-22 23:17:18 +0200565
Willy Tarreau91319572013-04-20 09:48:50 +0200566 if (!global.stats_fe) {
567 if ((global.stats_fe = alloc_stats_fe("GLOBAL", file, line)) == NULL) {
568 memprintf(err, "'%s %s' : out of memory trying to allocate a frontend", args[0], args[1]);
569 return -1;
570 }
571 }
572
Willy Tarreau35b7b162012-10-22 23:17:18 +0200573 while (*args[cur_arg]) {
Willy Tarreau110ecc12012-11-15 17:50:01 +0100574 unsigned int low, high;
575
Willy Tarreau35b7b162012-10-22 23:17:18 +0200576 if (strcmp(args[cur_arg], "all") == 0) {
577 set = 0;
578 break;
579 }
580 else if (strcmp(args[cur_arg], "odd") == 0) {
Willy Tarreaua9db57e2013-01-18 11:29:29 +0100581 set |= ~0UL/3UL; /* 0x555....555 */
Willy Tarreau35b7b162012-10-22 23:17:18 +0200582 }
583 else if (strcmp(args[cur_arg], "even") == 0) {
Willy Tarreaua9db57e2013-01-18 11:29:29 +0100584 set |= (~0UL/3UL) << 1; /* 0xAAA...AAA */
Willy Tarreau35b7b162012-10-22 23:17:18 +0200585 }
Willy Tarreau83d84cf2012-11-22 01:04:31 +0100586 else if (isdigit((int)*args[cur_arg])) {
Willy Tarreau110ecc12012-11-15 17:50:01 +0100587 char *dash = strchr(args[cur_arg], '-');
588
589 low = high = str2uic(args[cur_arg]);
590 if (dash)
591 high = str2uic(dash + 1);
592
593 if (high < low) {
594 unsigned int swap = low;
595 low = high;
596 high = swap;
597 }
598
Willy Tarreaua9db57e2013-01-18 11:29:29 +0100599 if (low < 1 || high > LONGBITS) {
600 memprintf(err, "'%s %s' supports process numbers from 1 to %d.\n",
601 args[0], args[1], LONGBITS);
Willy Tarreau35b7b162012-10-22 23:17:18 +0200602 return -1;
603 }
Willy Tarreau110ecc12012-11-15 17:50:01 +0100604 while (low <= high)
Willy Tarreaua9db57e2013-01-18 11:29:29 +0100605 set |= 1UL << (low++ - 1);
Willy Tarreau110ecc12012-11-15 17:50:01 +0100606 }
607 else {
608 memprintf(err,
Willy Tarreaua9db57e2013-01-18 11:29:29 +0100609 "'%s %s' expects 'all', 'odd', 'even', or a list of process ranges with numbers from 1 to %d.\n",
610 args[0], args[1], LONGBITS);
Willy Tarreau110ecc12012-11-15 17:50:01 +0100611 return -1;
Willy Tarreau35b7b162012-10-22 23:17:18 +0200612 }
613 cur_arg++;
614 }
615 global.stats_fe->bind_proc = set;
616 }
Willy Tarreaufbee7132007-10-18 13:53:22 +0200617 else {
Willy Tarreau35b7b162012-10-22 23:17:18 +0200618 memprintf(err, "'%s' only supports 'socket', 'maxconn', 'bind-process' and 'timeout' (got '%s')", args[0], args[1]);
Willy Tarreaufbee7132007-10-18 13:53:22 +0200619 return -1;
620 }
621 return 0;
622}
623
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100624/* Dumps the stats CSV header to the trash buffer which. The caller is responsible
625 * for clearing it if needed.
Willy Tarreauf522f3d2014-02-10 22:22:49 +0100626 * NOTE: Some tools happen to rely on the field position instead of its name,
627 * so please only append new fields at the end, never in the middle.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100628 */
629static void stats_dump_csv_header()
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100630{
Willy Tarreauf6142292016-01-04 17:24:29 +0100631 int field;
632
633 chunk_appendf(&trash, "# ");
634 for (field = 0; field < ST_F_TOTAL_FIELDS; field++)
635 chunk_appendf(&trash, "%s,", stat_field_names[field]);
636
637 chunk_appendf(&trash, "\n");
Willy Tarreau4bab24d2007-11-30 18:16:29 +0100638}
639
Simon Hormand9366582011-06-15 15:18:45 +0900640/* print a string of text buffer to <out>. The format is :
641 * Non-printable chars \t, \n, \r and \e are * encoded in C format.
642 * Other non-printable chars are encoded "\xHH". Space and '\' are also escaped.
643 * Print stopped if null char or <bsize> is reached, or if no more place in the chunk.
644 */
645static int dump_text(struct chunk *out, const char *buf, int bsize)
646{
647 unsigned char c;
648 int ptr = 0;
649
650 while (buf[ptr] && ptr < bsize) {
651 c = buf[ptr];
652 if (isprint(c) && isascii(c) && c != '\\' && c != ' ') {
653 if (out->len > out->size - 1)
654 break;
655 out->str[out->len++] = c;
656 }
657 else if (c == '\t' || c == '\n' || c == '\r' || c == '\e' || c == '\\' || c == ' ') {
658 if (out->len > out->size - 2)
659 break;
660 out->str[out->len++] = '\\';
661 switch (c) {
662 case ' ': c = ' '; break;
663 case '\t': c = 't'; break;
664 case '\n': c = 'n'; break;
665 case '\r': c = 'r'; break;
666 case '\e': c = 'e'; break;
667 case '\\': c = '\\'; break;
668 }
669 out->str[out->len++] = c;
670 }
671 else {
672 if (out->len > out->size - 4)
673 break;
674 out->str[out->len++] = '\\';
675 out->str[out->len++] = 'x';
676 out->str[out->len++] = hextab[(c >> 4) & 0xF];
677 out->str[out->len++] = hextab[c & 0xF];
678 }
679 ptr++;
680 }
681
682 return ptr;
683}
684
685/* print a buffer in hexa.
686 * Print stopped if <bsize> is reached, or if no more place in the chunk.
687 */
688static int dump_binary(struct chunk *out, const char *buf, int bsize)
689{
690 unsigned char c;
691 int ptr = 0;
692
693 while (ptr < bsize) {
694 c = buf[ptr];
695
696 if (out->len > out->size - 2)
697 break;
698 out->str[out->len++] = hextab[(c >> 4) & 0xF];
699 out->str[out->len++] = hextab[c & 0xF];
700
701 ptr++;
702 }
703 return ptr;
704}
705
706/* Dump the status of a table to a stream interface's
707 * read buffer. It returns 0 if the output buffer is full
708 * and needs to be called again, otherwise non-zero.
709 */
710static int stats_dump_table_head_to_buffer(struct chunk *msg, struct stream_interface *si,
711 struct proxy *proxy, struct proxy *target)
712{
Willy Tarreau87b09662015-04-03 00:22:06 +0200713 struct stream *s = si_strm(si);
Simon Hormand9366582011-06-15 15:18:45 +0900714
Willy Tarreau77804732012-10-29 16:14:26 +0100715 chunk_appendf(msg, "# table: %s, type: %s, size:%d, used:%d\n",
Simon Hormand9366582011-06-15 15:18:45 +0900716 proxy->id, stktable_types[proxy->table.type].kw, proxy->table.size, proxy->table.current);
717
718 /* any other information should be dumped here */
719
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200720 if (target && strm_li(s)->bind_conf->level < ACCESS_LVL_OPER)
Willy Tarreau77804732012-10-29 16:14:26 +0100721 chunk_appendf(msg, "# contents not dumped due to insufficient privileges\n");
Simon Hormand9366582011-06-15 15:18:45 +0900722
Willy Tarreaubc18da12015-03-13 14:00:47 +0100723 if (bi_putchk(si_ic(si), msg) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +0200724 si_applet_cant_put(si);
Simon Hormand9366582011-06-15 15:18:45 +0900725 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +0100726 }
Simon Hormand9366582011-06-15 15:18:45 +0900727
728 return 1;
729}
730
731/* Dump the a table entry to a stream interface's
732 * read buffer. It returns 0 if the output buffer is full
733 * and needs to be called again, otherwise non-zero.
734 */
735static int stats_dump_table_entry_to_buffer(struct chunk *msg, struct stream_interface *si,
736 struct proxy *proxy, struct stksess *entry)
737{
738 int dt;
739
Willy Tarreau77804732012-10-29 16:14:26 +0100740 chunk_appendf(msg, "%p:", entry);
Simon Hormand9366582011-06-15 15:18:45 +0900741
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200742 if (proxy->table.type == SMP_T_IPV4) {
Simon Hormand9366582011-06-15 15:18:45 +0900743 char addr[INET_ADDRSTRLEN];
744 inet_ntop(AF_INET, (const void *)&entry->key.key, addr, sizeof(addr));
Willy Tarreau77804732012-10-29 16:14:26 +0100745 chunk_appendf(msg, " key=%s", addr);
Simon Hormand9366582011-06-15 15:18:45 +0900746 }
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200747 else if (proxy->table.type == SMP_T_IPV6) {
Simon Hormand9366582011-06-15 15:18:45 +0900748 char addr[INET6_ADDRSTRLEN];
749 inet_ntop(AF_INET6, (const void *)&entry->key.key, addr, sizeof(addr));
Willy Tarreau77804732012-10-29 16:14:26 +0100750 chunk_appendf(msg, " key=%s", addr);
Simon Hormand9366582011-06-15 15:18:45 +0900751 }
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200752 else if (proxy->table.type == SMP_T_SINT) {
Willy Tarreau77804732012-10-29 16:14:26 +0100753 chunk_appendf(msg, " key=%u", *(unsigned int *)entry->key.key);
Simon Hormand9366582011-06-15 15:18:45 +0900754 }
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200755 else if (proxy->table.type == SMP_T_STR) {
Willy Tarreau77804732012-10-29 16:14:26 +0100756 chunk_appendf(msg, " key=");
Simon Hormand9366582011-06-15 15:18:45 +0900757 dump_text(msg, (const char *)entry->key.key, proxy->table.key_size);
758 }
759 else {
Willy Tarreau77804732012-10-29 16:14:26 +0100760 chunk_appendf(msg, " key=");
Simon Hormand9366582011-06-15 15:18:45 +0900761 dump_binary(msg, (const char *)entry->key.key, proxy->table.key_size);
762 }
763
Willy Tarreau77804732012-10-29 16:14:26 +0100764 chunk_appendf(msg, " use=%d exp=%d", entry->ref_cnt - 1, tick_remain(now_ms, entry->expire));
Simon Hormand9366582011-06-15 15:18:45 +0900765
766 for (dt = 0; dt < STKTABLE_DATA_TYPES; dt++) {
767 void *ptr;
768
769 if (proxy->table.data_ofs[dt] == 0)
770 continue;
771 if (stktable_data_types[dt].arg_type == ARG_T_DELAY)
Willy Tarreau77804732012-10-29 16:14:26 +0100772 chunk_appendf(msg, " %s(%d)=", stktable_data_types[dt].name, proxy->table.data_arg[dt].u);
Simon Hormand9366582011-06-15 15:18:45 +0900773 else
Willy Tarreau77804732012-10-29 16:14:26 +0100774 chunk_appendf(msg, " %s=", stktable_data_types[dt].name);
Simon Hormand9366582011-06-15 15:18:45 +0900775
776 ptr = stktable_data_ptr(&proxy->table, entry, dt);
777 switch (stktable_data_types[dt].std_type) {
778 case STD_T_SINT:
Willy Tarreau77804732012-10-29 16:14:26 +0100779 chunk_appendf(msg, "%d", stktable_data_cast(ptr, std_t_sint));
Simon Hormand9366582011-06-15 15:18:45 +0900780 break;
781 case STD_T_UINT:
Willy Tarreau77804732012-10-29 16:14:26 +0100782 chunk_appendf(msg, "%u", stktable_data_cast(ptr, std_t_uint));
Simon Hormand9366582011-06-15 15:18:45 +0900783 break;
784 case STD_T_ULL:
Willy Tarreau77804732012-10-29 16:14:26 +0100785 chunk_appendf(msg, "%lld", stktable_data_cast(ptr, std_t_ull));
Simon Hormand9366582011-06-15 15:18:45 +0900786 break;
787 case STD_T_FRQP:
Willy Tarreau77804732012-10-29 16:14:26 +0100788 chunk_appendf(msg, "%d",
Simon Hormand9366582011-06-15 15:18:45 +0900789 read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
790 proxy->table.data_arg[dt].u));
791 break;
792 }
793 }
Willy Tarreau77804732012-10-29 16:14:26 +0100794 chunk_appendf(msg, "\n");
Simon Hormand9366582011-06-15 15:18:45 +0900795
Willy Tarreaubc18da12015-03-13 14:00:47 +0100796 if (bi_putchk(si_ic(si), msg) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +0200797 si_applet_cant_put(si);
Simon Hormand9366582011-06-15 15:18:45 +0900798 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +0100799 }
Simon Hormand9366582011-06-15 15:18:45 +0900800
801 return 1;
802}
803
Willy Tarreaudec98142012-06-06 23:37:08 +0200804static void stats_sock_table_key_request(struct stream_interface *si, char **args, int action)
Simon Horman121f3052011-06-15 15:18:46 +0900805{
Willy Tarreau87b09662015-04-03 00:22:06 +0200806 struct stream *s = si_strm(si);
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100807 struct appctx *appctx = __objt_appctx(si->end);
808 struct proxy *px = appctx->ctx.table.target;
Simon Horman121f3052011-06-15 15:18:46 +0900809 struct stksess *ts;
Simon Hormancec9a222011-06-15 15:18:51 +0900810 uint32_t uint32_key;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900811 unsigned char ip6_key[sizeof(struct in6_addr)];
Willy Tarreau654694e2012-06-07 01:03:16 +0200812 long long value;
813 int data_type;
Willy Tarreau47060b62013-08-01 21:11:42 +0200814 int cur_arg;
Willy Tarreau654694e2012-06-07 01:03:16 +0200815 void *ptr;
816 struct freq_ctr_period *frqp;
Simon Horman121f3052011-06-15 15:18:46 +0900817
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100818 appctx->st0 = STAT_CLI_OUTPUT;
Simon Horman121f3052011-06-15 15:18:46 +0900819
820 if (!*args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100821 appctx->ctx.cli.msg = "Key value expected\n";
822 appctx->st0 = STAT_CLI_PRINT;
Simon Horman121f3052011-06-15 15:18:46 +0900823 return;
824 }
825
Simon Hormanc5b89f62011-06-15 15:18:50 +0900826 switch (px->table.type) {
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200827 case SMP_T_IPV4:
Simon Hormancec9a222011-06-15 15:18:51 +0900828 uint32_key = htonl(inetaddr_host(args[4]));
Willy Tarreau07115412012-10-29 21:56:59 +0100829 static_table_key->key = &uint32_key;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900830 break;
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200831 case SMP_T_IPV6:
Simon Hormanc5b89f62011-06-15 15:18:50 +0900832 inet_pton(AF_INET6, args[4], ip6_key);
Willy Tarreau07115412012-10-29 21:56:59 +0100833 static_table_key->key = &ip6_key;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900834 break;
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200835 case SMP_T_SINT:
Simon Hormancec9a222011-06-15 15:18:51 +0900836 {
837 char *endptr;
838 unsigned long val;
839 errno = 0;
840 val = strtoul(args[4], &endptr, 10);
841 if ((errno == ERANGE && val == ULONG_MAX) ||
842 (errno != 0 && val == 0) || endptr == args[4] ||
843 val > 0xffffffff) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100844 appctx->ctx.cli.msg = "Invalid key\n";
845 appctx->st0 = STAT_CLI_PRINT;
Simon Hormancec9a222011-06-15 15:18:51 +0900846 return;
847 }
848 uint32_key = (uint32_t) val;
Willy Tarreau07115412012-10-29 21:56:59 +0100849 static_table_key->key = &uint32_key;
Simon Hormancec9a222011-06-15 15:18:51 +0900850 break;
851 }
852 break;
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200853 case SMP_T_STR:
Willy Tarreau07115412012-10-29 21:56:59 +0100854 static_table_key->key = args[4];
855 static_table_key->key_len = strlen(args[4]);
Simon Horman619e3cc2011-06-15 15:18:52 +0900856 break;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900857 default:
Willy Tarreaudec98142012-06-06 23:37:08 +0200858 switch (action) {
859 case STAT_CLI_O_TAB:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100860 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 +0200861 break;
862 case STAT_CLI_O_CLR:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100863 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 +0200864 break;
865 default:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100866 appctx->ctx.cli.msg = "Unknown action\n";
Willy Tarreaudec98142012-06-06 23:37:08 +0200867 break;
868 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100869 appctx->st0 = STAT_CLI_PRINT;
Simon Horman121f3052011-06-15 15:18:46 +0900870 return;
871 }
872
873 /* check permissions */
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200874 if (strm_li(s)->bind_conf->level < ACCESS_LVL_OPER) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100875 appctx->ctx.cli.msg = stats_permission_denied_msg;
876 appctx->st0 = STAT_CLI_PRINT;
Simon Horman121f3052011-06-15 15:18:46 +0900877 return;
878 }
879
Willy Tarreau07115412012-10-29 21:56:59 +0100880 ts = stktable_lookup_key(&px->table, static_table_key);
Simon Horman17bce342011-06-15 15:18:47 +0900881
Willy Tarreaudec98142012-06-06 23:37:08 +0200882 switch (action) {
883 case STAT_CLI_O_TAB:
884 if (!ts)
885 return;
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100886 chunk_reset(&trash);
887 if (!stats_dump_table_head_to_buffer(&trash, si, px, px))
Simon Horman17bce342011-06-15 15:18:47 +0900888 return;
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100889 stats_dump_table_entry_to_buffer(&trash, si, px, ts);
Simon Horman121f3052011-06-15 15:18:46 +0900890 return;
Willy Tarreaudec98142012-06-06 23:37:08 +0200891
892 case STAT_CLI_O_CLR:
893 if (!ts)
894 return;
895 if (ts->ref_cnt) {
896 /* don't delete an entry which is currently referenced */
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100897 appctx->ctx.cli.msg = "Entry currently in use, cannot remove\n";
898 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaudec98142012-06-06 23:37:08 +0200899 return;
900 }
901 stksess_kill(&px->table, ts);
902 break;
Simon Horman17bce342011-06-15 15:18:47 +0900903
Willy Tarreau654694e2012-06-07 01:03:16 +0200904 case STAT_CLI_O_SET:
Willy Tarreau654694e2012-06-07 01:03:16 +0200905 if (ts)
906 stktable_touch(&px->table, ts, 1);
907 else {
Willy Tarreau07115412012-10-29 21:56:59 +0100908 ts = stksess_new(&px->table, static_table_key);
Willy Tarreau654694e2012-06-07 01:03:16 +0200909 if (!ts) {
910 /* don't delete an entry which is currently referenced */
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100911 appctx->ctx.cli.msg = "Unable to allocate a new entry\n";
912 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau654694e2012-06-07 01:03:16 +0200913 return;
914 }
915 stktable_store(&px->table, ts, 1);
916 }
917
Willy Tarreau47060b62013-08-01 21:11:42 +0200918 for (cur_arg = 5; *args[cur_arg]; cur_arg += 2) {
919 if (strncmp(args[cur_arg], "data.", 5) != 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100920 appctx->ctx.cli.msg = "\"data.<type>\" followed by a value expected\n";
921 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau47060b62013-08-01 21:11:42 +0200922 return;
923 }
924
925 data_type = stktable_get_data_type(args[cur_arg] + 5);
926 if (data_type < 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100927 appctx->ctx.cli.msg = "Unknown data type\n";
928 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau47060b62013-08-01 21:11:42 +0200929 return;
930 }
931
932 if (!px->table.data_ofs[data_type]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100933 appctx->ctx.cli.msg = "Data type not stored in this table\n";
934 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau47060b62013-08-01 21:11:42 +0200935 return;
936 }
937
938 if (!*args[cur_arg+1] || strl2llrc(args[cur_arg+1], strlen(args[cur_arg+1]), &value) != 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100939 appctx->ctx.cli.msg = "Require a valid integer value to store\n";
940 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau47060b62013-08-01 21:11:42 +0200941 return;
942 }
943
944 ptr = stktable_data_ptr(&px->table, ts, data_type);
945
946 switch (stktable_data_types[data_type].std_type) {
947 case STD_T_SINT:
948 stktable_data_cast(ptr, std_t_sint) = value;
949 break;
950 case STD_T_UINT:
951 stktable_data_cast(ptr, std_t_uint) = value;
952 break;
953 case STD_T_ULL:
954 stktable_data_cast(ptr, std_t_ull) = value;
955 break;
956 case STD_T_FRQP:
957 /* We set both the current and previous values. That way
958 * the reported frequency is stable during all the period
959 * then slowly fades out. This allows external tools to
960 * push measures without having to update them too often.
961 */
962 frqp = &stktable_data_cast(ptr, std_t_frqp);
963 frqp->curr_tick = now_ms;
964 frqp->prev_ctr = 0;
965 frqp->curr_ctr = value;
966 break;
967 }
Willy Tarreau654694e2012-06-07 01:03:16 +0200968 }
969 break;
970
Willy Tarreaudec98142012-06-06 23:37:08 +0200971 default:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100972 appctx->ctx.cli.msg = "Unknown action\n";
973 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaudec98142012-06-06 23:37:08 +0200974 break;
Simon Horman121f3052011-06-15 15:18:46 +0900975 }
Simon Horman121f3052011-06-15 15:18:46 +0900976}
977
Willy Tarreau654694e2012-06-07 01:03:16 +0200978static void stats_sock_table_data_request(struct stream_interface *si, char **args, int action)
Simon Hormand5b9fd92011-06-15 15:18:48 +0900979{
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100980 struct appctx *appctx = __objt_appctx(si->end);
981
Willy Tarreau04b3a192013-04-13 09:41:37 +0200982 if (action != STAT_CLI_O_TAB && action != STAT_CLI_O_CLR) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100983 appctx->ctx.cli.msg = "content-based lookup is only supported with the \"show\" and \"clear\" actions";
984 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau654694e2012-06-07 01:03:16 +0200985 return;
986 }
987
Simon Hormand5b9fd92011-06-15 15:18:48 +0900988 /* condition on stored data value */
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100989 appctx->ctx.table.data_type = stktable_get_data_type(args[3] + 5);
990 if (appctx->ctx.table.data_type < 0) {
991 appctx->ctx.cli.msg = "Unknown data type\n";
992 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900993 return;
994 }
995
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100996 if (!((struct proxy *)appctx->ctx.table.target)->table.data_ofs[appctx->ctx.table.data_type]) {
997 appctx->ctx.cli.msg = "Data type not stored in this table\n";
998 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900999 return;
1000 }
1001
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001002 appctx->ctx.table.data_op = get_std_op(args[4]);
1003 if (appctx->ctx.table.data_op < 0) {
1004 appctx->ctx.cli.msg = "Require and operator among \"eq\", \"ne\", \"le\", \"ge\", \"lt\", \"gt\"\n";
1005 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +09001006 return;
1007 }
1008
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001009 if (!*args[5] || strl2llrc(args[5], strlen(args[5]), &appctx->ctx.table.value) != 0) {
1010 appctx->ctx.cli.msg = "Require a valid integer value to compare against\n";
1011 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +09001012 return;
1013 }
1014}
1015
Willy Tarreaudec98142012-06-06 23:37:08 +02001016static void stats_sock_table_request(struct stream_interface *si, char **args, int action)
Simon Hormand5b9fd92011-06-15 15:18:48 +09001017{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001018 struct appctx *appctx = __objt_appctx(si->end);
1019
1020 appctx->ctx.table.data_type = -1;
1021 appctx->st2 = STAT_ST_INIT;
1022 appctx->ctx.table.target = NULL;
1023 appctx->ctx.table.proxy = NULL;
1024 appctx->ctx.table.entry = NULL;
1025 appctx->st0 = action;
Simon Hormand5b9fd92011-06-15 15:18:48 +09001026
1027 if (*args[2]) {
Willy Tarreaue2dc1fa2015-05-26 12:08:07 +02001028 appctx->ctx.table.target = proxy_tbl_by_name(args[2]);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001029 if (!appctx->ctx.table.target) {
1030 appctx->ctx.cli.msg = "No such table\n";
1031 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +09001032 return;
1033 }
1034 }
1035 else {
Willy Tarreaudec98142012-06-06 23:37:08 +02001036 if (action != STAT_CLI_O_TAB)
Simon Hormand5b9fd92011-06-15 15:18:48 +09001037 goto err_args;
1038 return;
1039 }
1040
1041 if (strcmp(args[3], "key") == 0)
Willy Tarreaudec98142012-06-06 23:37:08 +02001042 stats_sock_table_key_request(si, args, action);
Simon Hormanc88b8872011-06-15 15:18:49 +09001043 else if (strncmp(args[3], "data.", 5) == 0)
Willy Tarreau654694e2012-06-07 01:03:16 +02001044 stats_sock_table_data_request(si, args, action);
Simon Hormanc88b8872011-06-15 15:18:49 +09001045 else if (*args[3])
Simon Hormand5b9fd92011-06-15 15:18:48 +09001046 goto err_args;
1047
1048 return;
1049
1050err_args:
Willy Tarreaudec98142012-06-06 23:37:08 +02001051 switch (action) {
1052 case STAT_CLI_O_TAB:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001053 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 +02001054 break;
1055 case STAT_CLI_O_CLR:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001056 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 +02001057 break;
1058 default:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001059 appctx->ctx.cli.msg = "Unknown action\n";
Willy Tarreaudec98142012-06-06 23:37:08 +02001060 break;
1061 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001062 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +09001063}
1064
Willy Tarreau532a4502011-09-07 22:37:44 +02001065/* Expects to find a frontend named <arg> and returns it, otherwise displays various
Willy Tarreau87b09662015-04-03 00:22:06 +02001066 * adequate error messages and returns NULL. This function also expects the stream
Willy Tarreau532a4502011-09-07 22:37:44 +02001067 * level to be admin.
1068 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001069static struct proxy *expect_frontend_admin(struct stream *s, struct stream_interface *si, const char *arg)
Willy Tarreau532a4502011-09-07 22:37:44 +02001070{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001071 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau532a4502011-09-07 22:37:44 +02001072 struct proxy *px;
1073
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001074 if (strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001075 appctx->ctx.cli.msg = stats_permission_denied_msg;
1076 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001077 return NULL;
1078 }
1079
1080 if (!*arg) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001081 appctx->ctx.cli.msg = "A frontend name is expected.\n";
1082 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001083 return NULL;
1084 }
1085
Willy Tarreau9e0bb102015-05-26 11:24:42 +02001086 px = proxy_fe_by_name(arg);
Willy Tarreau532a4502011-09-07 22:37:44 +02001087 if (!px) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001088 appctx->ctx.cli.msg = "No such frontend.\n";
1089 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001090 return NULL;
1091 }
1092 return px;
1093}
1094
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001095/* Expects to find a backend and a server in <arg> under the form <backend>/<server>,
1096 * and returns the pointer to the server. Otherwise, display adequate error messages
Willy Tarreau87b09662015-04-03 00:22:06 +02001097 * and returns NULL. This function also expects the stream level to be admin. Note:
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001098 * the <arg> is modified to remove the '/'.
1099 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001100static struct server *expect_server_admin(struct stream *s, struct stream_interface *si, char *arg)
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001101{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001102 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001103 struct proxy *px;
1104 struct server *sv;
1105 char *line;
1106
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001107 if (strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001108 appctx->ctx.cli.msg = stats_permission_denied_msg;
1109 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001110 return NULL;
1111 }
1112
1113 /* split "backend/server" and make <line> point to server */
1114 for (line = arg; *line; line++)
1115 if (*line == '/') {
1116 *line++ = '\0';
1117 break;
1118 }
1119
1120 if (!*line || !*arg) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001121 appctx->ctx.cli.msg = "Require 'backend/server'.\n";
1122 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001123 return NULL;
1124 }
1125
1126 if (!get_backend_server(arg, line, &px, &sv)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001127 appctx->ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
1128 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001129 return NULL;
1130 }
1131
1132 if (px->state == PR_STSTOPPED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001133 appctx->ctx.cli.msg = "Proxy is disabled.\n";
1134 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001135 return NULL;
1136 }
1137
1138 return sv;
1139}
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02001140
1141/* This function is used with TLS ticket keys management. It permits to browse
1142 * each reference. The variable <getnext> must contain the current node,
1143 * <end> point to the root node.
1144 */
1145#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
1146static inline
1147struct tls_keys_ref *tlskeys_list_get_next(struct tls_keys_ref *getnext, struct list *end)
1148{
1149 struct tls_keys_ref *ref = getnext;
1150
1151 while (1) {
1152
1153 /* Get next list entry. */
1154 ref = LIST_NEXT(&ref->list, struct tls_keys_ref *, list);
1155
1156 /* If the entry is the last of the list, return NULL. */
1157 if (&ref->list == end)
1158 return NULL;
1159
1160 return ref;
1161 }
1162}
1163
1164static inline
1165struct tls_keys_ref *tlskeys_ref_lookup_ref(const char *reference)
1166{
1167 int id;
1168 char *error;
1169
1170 /* If the reference starts by a '#', this is numeric id. */
1171 if (reference[0] == '#') {
1172 /* Try to convert the numeric id. If the conversion fails, the lookup fails. */
1173 id = strtol(reference + 1, &error, 10);
1174 if (*error != '\0')
1175 return NULL;
1176
1177 /* Perform the unique id lookup. */
1178 return tlskeys_ref_lookupid(id);
1179 }
1180
1181 /* Perform the string lookup. */
1182 return tlskeys_ref_lookup(reference);
1183}
1184#endif
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001185
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001186/* This function is used with map and acl management. It permits to browse
1187 * each reference. The variable <getnext> must contain the current node,
1188 * <end> point to the root node and the <flags> permit to filter required
1189 * nodes.
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001190 */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001191static inline
1192struct pat_ref *pat_list_get_next(struct pat_ref *getnext, struct list *end,
1193 unsigned int flags)
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001194{
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001195 struct pat_ref *ref = getnext;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001196
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001197 while (1) {
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001198
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001199 /* Get next list entry. */
1200 ref = LIST_NEXT(&ref->list, struct pat_ref *, list);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001201
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001202 /* If the entry is the last of the list, return NULL. */
1203 if (&ref->list == end)
1204 return NULL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001205
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001206 /* If the entry match the flag, return it. */
1207 if (ref->flags & flags)
1208 return ref;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001209 }
1210}
1211
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001212static inline
1213struct pat_ref *pat_ref_lookup_ref(const char *reference)
1214{
1215 int id;
1216 char *error;
1217
1218 /* If the reference starts by a '#', this is numeric id. */
1219 if (reference[0] == '#') {
1220 /* Try to convert the numeric id. If the conversion fails, the lookup fails. */
1221 id = strtol(reference + 1, &error, 10);
1222 if (*error != '\0')
1223 return NULL;
1224
1225 /* Perform the unique id lookup. */
1226 return pat_ref_lookupid(id);
1227 }
1228
1229 /* Perform the string lookup. */
1230 return pat_ref_lookup(reference);
1231}
1232
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001233/* This function is used with map and acl management. It permits to browse
1234 * each reference.
1235 */
1236static inline
1237struct pattern_expr *pat_expr_get_next(struct pattern_expr *getnext, struct list *end)
1238{
1239 struct pattern_expr *expr;
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01001240 expr = LIST_NEXT(&getnext->list, struct pattern_expr *, list);
1241 if (&expr->list == end)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001242 return NULL;
1243 return expr;
1244}
1245
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001246/* Processes the stats interpreter on the statistics socket. This function is
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001247 * called from an applet running in a stream interface. The function returns 1
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001248 * if the request was understood, otherwise zero. It sets appctx->st0 to a value
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02001249 * designating the function which will have to process the request, which can
1250 * also be the print function to display the return message set into cli.msg.
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001251 */
Simon Horman9bd2c732011-06-15 15:18:44 +09001252static int stats_sock_parse_request(struct stream_interface *si, char *line)
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001253{
Willy Tarreau87b09662015-04-03 00:22:06 +02001254 struct stream *s = si_strm(si);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001255 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001256 char *args[MAX_STATS_ARGS + 1];
1257 int arg;
Thierry FOURNIER48bcfda2013-12-10 18:54:58 +01001258 int i, j;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001259
1260 while (isspace((unsigned char)*line))
1261 line++;
1262
1263 arg = 0;
1264 args[arg] = line;
1265
1266 while (*line && arg < MAX_STATS_ARGS) {
Thierry FOURNIER48bcfda2013-12-10 18:54:58 +01001267 if (*line == '\\') {
1268 line++;
1269 if (*line == '\0')
1270 break;
1271 }
1272 else if (isspace((unsigned char)*line)) {
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001273 *line++ = '\0';
1274
1275 while (isspace((unsigned char)*line))
1276 line++;
1277
1278 args[++arg] = line;
1279 continue;
1280 }
1281
1282 line++;
1283 }
1284
1285 while (++arg <= MAX_STATS_ARGS)
1286 args[arg] = line;
1287
Thierry FOURNIER48bcfda2013-12-10 18:54:58 +01001288 /* remove \ */
1289 arg = 0;
1290 while (*args[arg] != '\0') {
1291 j = 0;
1292 for (i=0; args[arg][i] != '\0'; i++) {
1293 if (args[arg][i] == '\\')
1294 continue;
1295 args[arg][j] = args[arg][i];
1296 j++;
1297 }
1298 args[arg][j] = '\0';
1299 arg++;
1300 }
1301
Willy Tarreau6bcb95d2015-05-04 18:07:56 +02001302 appctx->ctx.stats.scope_str = 0;
1303 appctx->ctx.stats.scope_len = 0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001304 appctx->ctx.stats.flags = 0;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001305 if (strcmp(args[0], "show") == 0) {
Baptiste Assmann9b6857e2015-09-18 14:49:12 +02001306 if (strcmp(args[1], "backend") == 0) {
1307 appctx->st2 = STAT_ST_INIT;
1308 appctx->st0 = STAT_CLI_O_BACKEND;
1309 }
Willy Tarreauae795722016-02-16 11:27:28 +01001310 else if (strcmp(args[1], "env") == 0) {
1311 extern char **environ;
1312
1313 if (strm_li(s)->bind_conf->level < ACCESS_LVL_OPER) {
1314 appctx->ctx.cli.msg = stats_permission_denied_msg;
1315 appctx->st0 = STAT_CLI_PRINT;
1316 return 1;
1317 }
1318 appctx->ctx.env.var = environ;
1319 appctx->st2 = STAT_ST_INIT;
1320 appctx->st0 = STAT_CLI_O_ENV; // stats_dump_env_to_buffer
1321
1322 if (*args[2]) {
1323 int len = strlen(args[2]);
1324
1325 for (; *appctx->ctx.env.var; appctx->ctx.env.var++) {
1326 if (strncmp(*appctx->ctx.env.var, args[2], len) == 0 &&
1327 (*appctx->ctx.env.var)[len] == '=')
1328 break;
1329 }
1330 if (!*appctx->ctx.env.var) {
1331 appctx->ctx.cli.msg = "Variable not found\n";
1332 appctx->st0 = STAT_CLI_PRINT;
1333 return 1;
1334 }
1335 appctx->st2 = STAT_ST_END;
1336 }
1337 }
1338 else if (strcmp(args[1], "stat") == 0) {
Baptiste Assmann3863f972015-05-17 00:33:24 +02001339 if (strcmp(args[2], "resolvers") == 0) {
1340 struct dns_resolvers *presolvers;
1341
Andrew Hayworth68d05342015-10-02 20:33:01 +00001342 if (*args[3]) {
1343 appctx->ctx.resolvers.ptr = NULL;
1344 list_for_each_entry(presolvers, &dns_resolvers, list) {
1345 if (strcmp(presolvers->id, args[3]) == 0) {
1346 appctx->ctx.resolvers.ptr = presolvers;
1347 break;
1348 }
Baptiste Assmann3863f972015-05-17 00:33:24 +02001349 }
Andrew Hayworth68d05342015-10-02 20:33:01 +00001350 if (appctx->ctx.resolvers.ptr == NULL) {
1351 appctx->ctx.cli.msg = "Can't find that resolvers section\n";
1352 appctx->st0 = STAT_CLI_PRINT;
1353 return 1;
1354 }
Baptiste Assmann3863f972015-05-17 00:33:24 +02001355 }
1356
1357 appctx->st2 = STAT_ST_INIT;
1358 appctx->st0 = STAT_CLI_O_RESOLVERS;
1359 return 1;
1360 }
1361 else if (*args[2] && *args[3] && *args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001362 appctx->ctx.stats.flags |= STAT_BOUND;
1363 appctx->ctx.stats.iid = atoi(args[2]);
1364 appctx->ctx.stats.type = atoi(args[3]);
1365 appctx->ctx.stats.sid = atoi(args[4]);
Willy Tarreau1e62df92016-01-11 18:57:53 +01001366 if (strcmp(args[5], "typed") == 0)
1367 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001368 }
Willy Tarreau1e62df92016-01-11 18:57:53 +01001369 else if (strcmp(args[2], "typed") == 0)
1370 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001371
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001372 appctx->st2 = STAT_ST_INIT;
1373 appctx->st0 = STAT_CLI_O_STAT; // stats_dump_stat_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001374 }
1375 else if (strcmp(args[1], "info") == 0) {
Willy Tarreaucb809122016-01-11 20:08:42 +01001376 if (strcmp(args[2], "typed") == 0)
1377 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001378 appctx->st2 = STAT_ST_INIT;
1379 appctx->st0 = STAT_CLI_O_INFO; // stats_dump_info_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001380 }
Baptiste Assmann28289462015-07-03 08:01:20 +02001381 else if (strcmp(args[1], "servers") == 0 && strcmp(args[2], "state") == 0) {
1382 appctx->ctx.server_state.backend = NULL;
1383
1384 /* check if a backend name has been provided */
1385 if (*args[3]) {
1386 /* read server state from local file */
1387 appctx->ctx.server_state.backend = proxy_be_by_name(args[3]);
1388
1389 if (appctx->ctx.server_state.backend == NULL) {
1390 appctx->ctx.cli.msg = "Can't find backend.\n";
1391 appctx->st0 = STAT_CLI_PRINT;
1392 return 1;
1393 }
1394 }
1395 appctx->st2 = STAT_ST_INIT;
1396 appctx->st0 = STAT_CLI_O_SERVERS_STATE; // stats_dump_servers_state_to_buffer
1397 return 1;
1398 }
Willy Tarreau12833bb2014-01-28 16:49:56 +01001399 else if (strcmp(args[1], "pools") == 0) {
1400 appctx->st2 = STAT_ST_INIT;
1401 appctx->st0 = STAT_CLI_O_POOLS; // stats_dump_pools_to_buffer
1402 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001403 else if (strcmp(args[1], "sess") == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001404 appctx->st2 = STAT_ST_INIT;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001405 if (strm_li(s)->bind_conf->level < ACCESS_LVL_OPER) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001406 appctx->ctx.cli.msg = stats_permission_denied_msg;
1407 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +02001408 return 1;
1409 }
Willy Tarreau76153662012-11-26 01:16:39 +01001410 if (*args[2] && strcmp(args[2], "all") == 0)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001411 appctx->ctx.sess.target = (void *)-1;
Willy Tarreau76153662012-11-26 01:16:39 +01001412 else if (*args[2])
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001413 appctx->ctx.sess.target = (void *)strtoul(args[2], NULL, 0);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01001414 else
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001415 appctx->ctx.sess.target = NULL;
Willy Tarreau87b09662015-04-03 00:22:06 +02001416 appctx->ctx.sess.section = 0; /* start with stream status */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001417 appctx->ctx.sess.pos = 0;
1418 appctx->st0 = STAT_CLI_O_SESS; // stats_dump_sess_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001419 }
1420 else if (strcmp(args[1], "errors") == 0) {
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001421 if (strm_li(s)->bind_conf->level < ACCESS_LVL_OPER) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001422 appctx->ctx.cli.msg = stats_permission_denied_msg;
1423 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +02001424 return 1;
1425 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001426 if (*args[2])
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001427 appctx->ctx.errors.iid = atoi(args[2]);
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001428 else
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001429 appctx->ctx.errors.iid = -1;
1430 appctx->ctx.errors.px = NULL;
1431 appctx->st2 = STAT_ST_INIT;
1432 appctx->st0 = STAT_CLI_O_ERR; // stats_dump_errors_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001433 }
Willy Tarreau69f58c82010-07-12 17:55:33 +02001434 else if (strcmp(args[1], "table") == 0) {
Willy Tarreaudec98142012-06-06 23:37:08 +02001435 stats_sock_table_request(si, args, STAT_CLI_O_TAB);
Willy Tarreau69f58c82010-07-12 17:55:33 +02001436 }
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02001437 else if (strcmp(args[1], "tls-keys") == 0) {
1438#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
1439 appctx->st2 = STAT_ST_INIT;
1440 appctx->st0 = STAT_CLI_O_TLSK;
1441#else
1442 appctx->ctx.cli.msg = "HAProxy was compiled against a version of OpenSSL "
1443 "that doesn't support specifying TLS ticket keys\n";
1444 appctx->st0 = STAT_CLI_PRINT;
1445#endif
1446 return 1;
1447 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001448 else if (strcmp(args[1], "map") == 0 ||
1449 strcmp(args[1], "acl") == 0) {
1450
1451 /* Set ACL or MAP flags. */
1452 if (args[1][0] == 'm')
1453 appctx->ctx.map.display_flags = PAT_REF_MAP;
1454 else
1455 appctx->ctx.map.display_flags = PAT_REF_ACL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001456
1457 /* no parameter: display all map avalaible */
1458 if (!*args[2]) {
1459 appctx->st2 = STAT_ST_INIT;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001460 appctx->st0 = STAT_CLI_O_PATS;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001461 return 1;
1462 }
1463
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001464 /* lookup into the refs and check the map flag */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001465 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001466 if (!appctx->ctx.map.ref ||
1467 !(appctx->ctx.map.ref->flags & appctx->ctx.map.display_flags)) {
1468 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001469 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001470 else
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001471 appctx->ctx.cli.msg = "Unknown ACL identifier. Please use #<id> or <file>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001472 appctx->st0 = STAT_CLI_PRINT;
1473 return 1;
1474 }
1475 appctx->st2 = STAT_ST_INIT;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001476 appctx->st0 = STAT_CLI_O_PAT;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001477 }
Aman Guptaceafb4a2012-04-02 18:57:54 -07001478 else { /* neither "stat" nor "info" nor "sess" nor "errors" nor "table" */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001479 return 0;
1480 }
1481 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001482 else if (strcmp(args[0], "clear") == 0) {
1483 if (strcmp(args[1], "counters") == 0) {
1484 struct proxy *px;
1485 struct server *sv;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001486 struct listener *li;
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001487 int clrall = 0;
1488
1489 if (strcmp(args[2], "all") == 0)
1490 clrall = 1;
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001491
Willy Tarreau6162db22009-10-10 17:13:00 +02001492 /* check permissions */
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001493 if (strm_li(s)->bind_conf->level < ACCESS_LVL_OPER ||
1494 (clrall && strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001495 appctx->ctx.cli.msg = stats_permission_denied_msg;
1496 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +02001497 return 1;
1498 }
1499
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001500 for (px = proxy; px; px = px->next) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001501 if (clrall) {
1502 memset(&px->be_counters, 0, sizeof(px->be_counters));
1503 memset(&px->fe_counters, 0, sizeof(px->fe_counters));
1504 }
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001505 else {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001506 px->be_counters.conn_max = 0;
1507 px->be_counters.p.http.rps_max = 0;
1508 px->be_counters.sps_max = 0;
1509 px->be_counters.cps_max = 0;
1510 px->be_counters.nbpend_max = 0;
1511
1512 px->fe_counters.conn_max = 0;
1513 px->fe_counters.p.http.rps_max = 0;
1514 px->fe_counters.sps_max = 0;
1515 px->fe_counters.cps_max = 0;
1516 px->fe_counters.nbpend_max = 0;
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001517 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001518
1519 for (sv = px->srv; sv; sv = sv->next)
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001520 if (clrall)
1521 memset(&sv->counters, 0, sizeof(sv->counters));
1522 else {
1523 sv->counters.cur_sess_max = 0;
1524 sv->counters.nbpend_max = 0;
1525 sv->counters.sps_max = 0;
1526 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001527
Willy Tarreaua7944ad2012-09-26 21:03:11 +02001528 list_for_each_entry(li, &px->conf.listeners, by_fe)
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001529 if (li->counters) {
1530 if (clrall)
1531 memset(li->counters, 0, sizeof(*li->counters));
1532 else
1533 li->counters->conn_max = 0;
1534 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001535 }
1536
Willy Tarreau81c25d02011-09-07 15:17:21 +02001537 global.cps_max = 0;
Willy Tarreau93e7c002013-10-07 18:51:07 +02001538 global.sps_max = 0;
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001539 return 1;
1540 }
Willy Tarreau88ee3972010-07-13 13:48:00 +02001541 else if (strcmp(args[1], "table") == 0) {
Willy Tarreaudec98142012-06-06 23:37:08 +02001542 stats_sock_table_request(si, args, STAT_CLI_O_CLR);
Willy Tarreau88ee3972010-07-13 13:48:00 +02001543 /* end of processing */
1544 return 1;
1545 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001546 else if (strcmp(args[1], "map") == 0 || strcmp(args[1], "acl") == 0) {
1547 /* Set ACL or MAP flags. */
1548 if (args[1][0] == 'm')
1549 appctx->ctx.map.display_flags = PAT_REF_MAP;
1550 else
1551 appctx->ctx.map.display_flags = PAT_REF_ACL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001552
1553 /* no parameter */
1554 if (!*args[2]) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001555 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
1556 appctx->ctx.cli.msg = "Missing map identifier.\n";
1557 else
1558 appctx->ctx.cli.msg = "Missing ACL identifier.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001559 appctx->st0 = STAT_CLI_PRINT;
1560 return 1;
1561 }
1562
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001563 /* lookup into the refs and check the map flag */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001564 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001565 if (!appctx->ctx.map.ref ||
1566 !(appctx->ctx.map.ref->flags & appctx->ctx.map.display_flags)) {
1567 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001568 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001569 else
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001570 appctx->ctx.cli.msg = "Unknown ACL identifier. Please use #<id> or <file>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001571 appctx->st0 = STAT_CLI_PRINT;
1572 return 1;
1573 }
1574
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001575 /* Clear all. */
1576 pat_ref_prune(appctx->ctx.map.ref);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001577
1578 /* return response */
Thierry FOURNIER07e78c52014-12-18 15:28:01 +01001579 appctx->st0 = STAT_CLI_PROMPT;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001580 return 1;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001581 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001582 else {
Willy Tarreau88ee3972010-07-13 13:48:00 +02001583 /* unknown "clear" argument */
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001584 return 0;
1585 }
1586 }
Willy Tarreau38338fa2009-10-10 18:37:29 +02001587 else if (strcmp(args[0], "get") == 0) {
1588 if (strcmp(args[1], "weight") == 0) {
1589 struct proxy *px;
1590 struct server *sv;
1591
1592 /* split "backend/server" and make <line> point to server */
1593 for (line = args[2]; *line; line++)
1594 if (*line == '/') {
1595 *line++ = '\0';
1596 break;
1597 }
1598
1599 if (!*line) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001600 appctx->ctx.cli.msg = "Require 'backend/server'.\n";
1601 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau38338fa2009-10-10 18:37:29 +02001602 return 1;
1603 }
1604
1605 if (!get_backend_server(args[2], line, &px, &sv)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001606 appctx->ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
1607 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau38338fa2009-10-10 18:37:29 +02001608 return 1;
1609 }
1610
1611 /* return server's effective weight at the moment */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001612 snprintf(trash.str, trash.size, "%d (initial %d)\n", sv->uweight, sv->iweight);
Willy Tarreaubc18da12015-03-13 14:00:47 +01001613 if (bi_putstr(si_ic(si), trash.str) == -1)
Willy Tarreaufe127932015-04-21 19:23:39 +02001614 si_applet_cant_put(si);
Willy Tarreaubc18da12015-03-13 14:00:47 +01001615
Willy Tarreau38338fa2009-10-10 18:37:29 +02001616 return 1;
1617 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001618 else if (strcmp(args[1], "map") == 0 || strcmp(args[1], "acl") == 0) {
1619 /* Set flags. */
1620 if (args[1][0] == 'm')
1621 appctx->ctx.map.display_flags = PAT_REF_MAP;
1622 else
1623 appctx->ctx.map.display_flags = PAT_REF_ACL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001624
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001625 /* No parameter. */
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001626 if (!*args[2] || !*args[3]) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001627 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
1628 appctx->ctx.cli.msg = "Missing map identifier and/or key.\n";
1629 else
1630 appctx->ctx.cli.msg = "Missing ACL identifier and/or key.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001631 appctx->st0 = STAT_CLI_PRINT;
1632 return 1;
1633 }
1634
1635 /* lookup into the maps */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001636 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001637 if (!appctx->ctx.map.ref) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001638 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001639 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001640 else
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001641 appctx->ctx.cli.msg = "Unknown ACL identifier. Please use #<id> or <file>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001642 appctx->st0 = STAT_CLI_PRINT;
1643 return 1;
1644 }
1645
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001646 /* copy input string. The string must be allocated because
1647 * it may be used over multiple iterations. It's released
1648 * at the end and upon abort anyway.
1649 */
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001650 appctx->ctx.map.chunk.len = strlen(args[3]);
1651 appctx->ctx.map.chunk.size = appctx->ctx.map.chunk.len + 1;
1652 appctx->ctx.map.chunk.str = strdup(args[3]);
1653 if (!appctx->ctx.map.chunk.str) {
1654 appctx->ctx.cli.msg = "Out of memory error.\n";
1655 appctx->st0 = STAT_CLI_PRINT;
1656 return 1;
1657 }
1658
1659 /* prepare response */
1660 appctx->st2 = STAT_ST_INIT;
1661 appctx->st0 = STAT_CLI_O_MLOOK;
1662 }
Willy Tarreau38338fa2009-10-10 18:37:29 +02001663 else { /* not "get weight" */
1664 return 0;
1665 }
1666 }
Willy Tarreau4483d432009-10-10 19:30:08 +02001667 else if (strcmp(args[0], "set") == 0) {
1668 if (strcmp(args[1], "weight") == 0) {
Willy Tarreau4483d432009-10-10 19:30:08 +02001669 struct server *sv;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001670 const char *warning;
Willy Tarreau4483d432009-10-10 19:30:08 +02001671
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001672 sv = expect_server_admin(s, si, args[2]);
1673 if (!sv)
Willy Tarreau4483d432009-10-10 19:30:08 +02001674 return 1;
Willy Tarreau4483d432009-10-10 19:30:08 +02001675
Simon Horman7d09b9a2013-02-12 10:45:51 +09001676 warning = server_parse_weight_change_request(sv, args[3]);
1677 if (warning) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001678 appctx->ctx.cli.msg = warning;
1679 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +02001680 }
Willy Tarreau4483d432009-10-10 19:30:08 +02001681 return 1;
1682 }
Willy Tarreau2a4b70f2014-05-22 18:42:35 +02001683 else if (strcmp(args[1], "server") == 0) {
1684 struct server *sv;
1685 const char *warning;
1686
1687 sv = expect_server_admin(s, si, args[2]);
1688 if (!sv)
1689 return 1;
1690
1691 if (strcmp(args[3], "weight") == 0) {
1692 warning = server_parse_weight_change_request(sv, args[4]);
1693 if (warning) {
1694 appctx->ctx.cli.msg = warning;
1695 appctx->st0 = STAT_CLI_PRINT;
1696 }
1697 }
1698 else if (strcmp(args[3], "state") == 0) {
1699 if (strcmp(args[4], "ready") == 0)
1700 srv_adm_set_ready(sv);
1701 else if (strcmp(args[4], "drain") == 0)
1702 srv_adm_set_drain(sv);
1703 else if (strcmp(args[4], "maint") == 0)
1704 srv_adm_set_maint(sv);
1705 else {
1706 appctx->ctx.cli.msg = "'set server <srv> state' expects 'ready', 'drain' and 'maint'.\n";
1707 appctx->st0 = STAT_CLI_PRINT;
1708 }
1709 }
1710 else if (strcmp(args[3], "health") == 0) {
1711 if (sv->track) {
1712 appctx->ctx.cli.msg = "cannot change health on a tracking server.\n";
1713 appctx->st0 = STAT_CLI_PRINT;
1714 }
1715 else if (strcmp(args[4], "up") == 0) {
1716 sv->check.health = sv->check.rise + sv->check.fall - 1;
1717 srv_set_running(sv, "changed from CLI");
1718 }
1719 else if (strcmp(args[4], "stopping") == 0) {
1720 sv->check.health = sv->check.rise + sv->check.fall - 1;
1721 srv_set_stopping(sv, "changed from CLI");
1722 }
1723 else if (strcmp(args[4], "down") == 0) {
1724 sv->check.health = 0;
1725 srv_set_stopped(sv, "changed from CLI");
1726 }
1727 else {
1728 appctx->ctx.cli.msg = "'set server <srv> health' expects 'up', 'stopping', or 'down'.\n";
1729 appctx->st0 = STAT_CLI_PRINT;
1730 }
1731 }
1732 else if (strcmp(args[3], "agent") == 0) {
1733 if (!(sv->agent.state & CHK_ST_ENABLED)) {
1734 appctx->ctx.cli.msg = "agent checks are not enabled on this server.\n";
1735 appctx->st0 = STAT_CLI_PRINT;
1736 }
1737 else if (strcmp(args[4], "up") == 0) {
1738 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
1739 srv_set_running(sv, "changed from CLI");
1740 }
1741 else if (strcmp(args[4], "down") == 0) {
1742 sv->agent.health = 0;
1743 srv_set_stopped(sv, "changed from CLI");
1744 }
1745 else {
1746 appctx->ctx.cli.msg = "'set server <srv> agent' expects 'up' or 'down'.\n";
1747 appctx->st0 = STAT_CLI_PRINT;
1748 }
1749 }
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001750 else if (strcmp(args[3], "addr") == 0) {
Thierry Fournier09a91782016-02-24 08:25:39 +01001751 warning = server_parse_addr_change_request(sv, args[4], "stats command");
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001752 if (warning) {
1753 appctx->ctx.cli.msg = warning;
1754 appctx->st0 = STAT_CLI_PRINT;
1755 }
1756 }
Willy Tarreau2a4b70f2014-05-22 18:42:35 +02001757 else {
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001758 appctx->ctx.cli.msg = "'set server <srv>' only supports 'agent', 'health', 'state', 'weight' and 'addr'.\n";
Willy Tarreau2a4b70f2014-05-22 18:42:35 +02001759 appctx->st0 = STAT_CLI_PRINT;
1760 }
1761 return 1;
1762 }
Willy Tarreau7aabd112010-01-26 10:59:06 +01001763 else if (strcmp(args[1], "timeout") == 0) {
1764 if (strcmp(args[2], "cli") == 0) {
1765 unsigned timeout;
1766 const char *res;
1767
1768 if (!*args[3]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001769 appctx->ctx.cli.msg = "Expects an integer value.\n";
1770 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001771 return 1;
1772 }
1773
1774 res = parse_time_err(args[3], &timeout, TIME_UNIT_S);
1775 if (res || timeout < 1) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001776 appctx->ctx.cli.msg = "Invalid timeout value.\n";
1777 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001778 return 1;
1779 }
1780
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001781 s->req.rto = s->res.wto = 1 + MS_TO_TICKS(timeout*1000);
Willy Tarreau7aabd112010-01-26 10:59:06 +01001782 return 1;
1783 }
1784 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001785 appctx->ctx.cli.msg = "'set timeout' only supports 'cli'.\n";
1786 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001787 return 1;
1788 }
1789 }
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001790 else if (strcmp(args[1], "maxconn") == 0) {
1791 if (strcmp(args[2], "frontend") == 0) {
1792 struct proxy *px;
1793 struct listener *l;
1794 int v;
1795
Willy Tarreau532a4502011-09-07 22:37:44 +02001796 px = expect_frontend_admin(s, si, args[3]);
1797 if (!px)
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001798 return 1;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001799
1800 if (!*args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001801 appctx->ctx.cli.msg = "Integer value expected.\n";
1802 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001803 return 1;
1804 }
1805
1806 v = atoi(args[4]);
Willy Tarreau3c7a79d2012-09-26 21:07:15 +02001807 if (v < 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001808 appctx->ctx.cli.msg = "Value out of range.\n";
1809 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001810 return 1;
1811 }
1812
1813 /* OK, the value is fine, so we assign it to the proxy and to all of
1814 * its listeners. The blocked ones will be dequeued.
1815 */
1816 px->maxconn = v;
Willy Tarreaua7944ad2012-09-26 21:03:11 +02001817 list_for_each_entry(l, &px->conf.listeners, by_fe) {
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001818 l->maxconn = v;
1819 if (l->state == LI_FULL)
1820 resume_listener(l);
1821 }
1822
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001823 if (px->maxconn > px->feconn && !LIST_ISEMPTY(&strm_fe(s)->listener_queue))
1824 dequeue_all_listeners(&strm_fe(s)->listener_queue);
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001825
1826 return 1;
1827 }
Andrew Hayworthedb93a72015-10-27 21:46:25 +00001828 else if (strcmp(args[2], "server") == 0) {
1829 struct server *sv;
1830 int v;
1831
1832 sv = expect_server_admin(s, si, args[3]);
1833 if (!sv)
1834 return 1;
1835
1836 if (!*args[4]) {
1837 appctx->ctx.cli.msg = "Integer value expected.\n";
1838 appctx->st0 = STAT_CLI_PRINT;
1839 return 1;
1840 }
1841
1842 v = atoi(args[4]);
1843 if (v < 0) {
1844 appctx->ctx.cli.msg = "Value out of range.\n";
1845 appctx->st0 = STAT_CLI_PRINT;
1846 return 1;
1847 }
1848
1849 if (sv->maxconn == sv->minconn) { // static maxconn
1850 sv->maxconn = sv->minconn = v;
1851 } else { // dynamic maxconn
1852 sv->maxconn = v;
1853 }
1854
1855 if (may_dequeue_tasks(sv, sv->proxy))
1856 process_srv_queue(sv);
1857
1858 return 1;
1859 }
Willy Tarreau91886b62011-09-07 14:38:31 +02001860 else if (strcmp(args[2], "global") == 0) {
1861 int v;
1862
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001863 if (strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001864 appctx->ctx.cli.msg = stats_permission_denied_msg;
1865 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau91886b62011-09-07 14:38:31 +02001866 return 1;
1867 }
1868
1869 if (!*args[3]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001870 appctx->ctx.cli.msg = "Expects an integer value.\n";
1871 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau91886b62011-09-07 14:38:31 +02001872 return 1;
1873 }
1874
1875 v = atoi(args[3]);
1876 if (v > global.hardmaxconn) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001877 appctx->ctx.cli.msg = "Value out of range.\n";
1878 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau91886b62011-09-07 14:38:31 +02001879 return 1;
1880 }
1881
1882 /* check for unlimited values */
1883 if (v <= 0)
1884 v = global.hardmaxconn;
1885
1886 global.maxconn = v;
1887
1888 /* Dequeues all of the listeners waiting for a resource */
1889 if (!LIST_ISEMPTY(&global_listener_queue))
1890 dequeue_all_listeners(&global_listener_queue);
1891
1892 return 1;
1893 }
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001894 else {
Andrew Hayworthedb93a72015-10-27 21:46:25 +00001895 appctx->ctx.cli.msg = "'set maxconn' only supports 'frontend', 'server', and 'global'.\n";
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001896 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001897 return 1;
1898 }
1899 }
Willy Tarreauf5b22872011-09-07 16:13:44 +02001900 else if (strcmp(args[1], "rate-limit") == 0) {
1901 if (strcmp(args[2], "connections") == 0) {
1902 if (strcmp(args[3], "global") == 0) {
1903 int v;
1904
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001905 if (strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001906 appctx->ctx.cli.msg = stats_permission_denied_msg;
1907 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001908 return 1;
1909 }
1910
1911 if (!*args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001912 appctx->ctx.cli.msg = "Expects an integer value.\n";
1913 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001914 return 1;
1915 }
1916
1917 v = atoi(args[4]);
1918 if (v < 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001919 appctx->ctx.cli.msg = "Value out of range.\n";
1920 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001921 return 1;
1922 }
1923
1924 global.cps_lim = v;
1925
1926 /* Dequeues all of the listeners waiting for a resource */
1927 if (!LIST_ISEMPTY(&global_listener_queue))
1928 dequeue_all_listeners(&global_listener_queue);
1929
1930 return 1;
1931 }
1932 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001933 appctx->ctx.cli.msg = "'set rate-limit connections' only supports 'global'.\n";
1934 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001935 return 1;
1936 }
1937 }
Willy Tarreau93e7c002013-10-07 18:51:07 +02001938 else if (strcmp(args[2], "sessions") == 0) {
1939 if (strcmp(args[3], "global") == 0) {
1940 int v;
1941
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001942 if (strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau93e7c002013-10-07 18:51:07 +02001943 appctx->ctx.cli.msg = stats_permission_denied_msg;
1944 appctx->st0 = STAT_CLI_PRINT;
1945 return 1;
1946 }
1947
1948 if (!*args[4]) {
1949 appctx->ctx.cli.msg = "Expects an integer value.\n";
1950 appctx->st0 = STAT_CLI_PRINT;
1951 return 1;
1952 }
1953
1954 v = atoi(args[4]);
1955 if (v < 0) {
1956 appctx->ctx.cli.msg = "Value out of range.\n";
1957 appctx->st0 = STAT_CLI_PRINT;
1958 return 1;
1959 }
1960
1961 global.sps_lim = v;
1962
1963 /* Dequeues all of the listeners waiting for a resource */
1964 if (!LIST_ISEMPTY(&global_listener_queue))
1965 dequeue_all_listeners(&global_listener_queue);
1966
1967 return 1;
1968 }
1969 else {
1970 appctx->ctx.cli.msg = "'set rate-limit sessions' only supports 'global'.\n";
1971 appctx->st0 = STAT_CLI_PRINT;
1972 return 1;
1973 }
1974 }
Willy Tarreaue43d5322013-10-07 20:01:52 +02001975#ifdef USE_OPENSSL
1976 else if (strcmp(args[2], "ssl-sessions") == 0) {
1977 if (strcmp(args[3], "global") == 0) {
1978 int v;
1979
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001980 if (strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreaue43d5322013-10-07 20:01:52 +02001981 appctx->ctx.cli.msg = stats_permission_denied_msg;
1982 appctx->st0 = STAT_CLI_PRINT;
1983 return 1;
1984 }
1985
1986 if (!*args[4]) {
1987 appctx->ctx.cli.msg = "Expects an integer value.\n";
1988 appctx->st0 = STAT_CLI_PRINT;
1989 return 1;
1990 }
1991
1992 v = atoi(args[4]);
1993 if (v < 0) {
1994 appctx->ctx.cli.msg = "Value out of range.\n";
1995 appctx->st0 = STAT_CLI_PRINT;
1996 return 1;
1997 }
1998
1999 global.ssl_lim = v;
2000
2001 /* Dequeues all of the listeners waiting for a resource */
2002 if (!LIST_ISEMPTY(&global_listener_queue))
2003 dequeue_all_listeners(&global_listener_queue);
2004
2005 return 1;
2006 }
2007 else {
2008 appctx->ctx.cli.msg = "'set rate-limit ssl-sessions' only supports 'global'.\n";
2009 appctx->st0 = STAT_CLI_PRINT;
2010 return 1;
2011 }
2012 }
2013#endif
William Lallemandd85f9172012-11-09 17:05:39 +01002014 else if (strcmp(args[2], "http-compression") == 0) {
2015 if (strcmp(args[3], "global") == 0) {
2016 int v;
2017
Willy Tarreaua1c2b2c2015-11-26 18:32:39 +01002018 if (strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN) {
2019 appctx->ctx.cli.msg = stats_permission_denied_msg;
2020 appctx->st0 = STAT_CLI_PRINT;
2021 return 1;
2022 }
2023
Willy Tarreau85d47f92012-11-21 00:29:50 +01002024 if (!*args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002025 appctx->ctx.cli.msg = "Expects a maximum input byte rate in kB/s.\n";
2026 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau85d47f92012-11-21 00:29:50 +01002027 return 1;
2028 }
2029
William Lallemandd85f9172012-11-09 17:05:39 +01002030 v = atoi(args[4]);
2031 global.comp_rate_lim = v * 1024; /* Kilo to bytes. */
2032 }
2033 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002034 appctx->ctx.cli.msg = "'set rate-limit http-compression' only supports 'global'.\n";
2035 appctx->st0 = STAT_CLI_PRINT;
William Lallemandd85f9172012-11-09 17:05:39 +01002036 return 1;
2037 }
2038 }
Willy Tarreauf5b22872011-09-07 16:13:44 +02002039 else {
Willy Tarreaue43d5322013-10-07 20:01:52 +02002040 appctx->ctx.cli.msg = "'set rate-limit' supports 'connections', 'sessions', 'ssl-sessions', and 'http-compression'.\n";
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002041 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02002042 return 1;
2043 }
2044 }
Willy Tarreau654694e2012-06-07 01:03:16 +02002045 else if (strcmp(args[1], "table") == 0) {
2046 stats_sock_table_request(si, args, STAT_CLI_O_SET);
2047 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002048 else if (strcmp(args[1], "map") == 0) {
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002049 char *err;
2050
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002051 /* Set flags. */
2052 appctx->ctx.map.display_flags = PAT_REF_MAP;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002053
2054 /* Expect three parameters: map name, key and new value. */
2055 if (!*args[2] || !*args[3] || !*args[4]) {
Thierry FOURNIERd5723432014-03-11 13:52:44 +01002056 appctx->ctx.cli.msg = "'set map' expects three parameters: map identifier, key and value.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002057 appctx->st0 = STAT_CLI_PRINT;
2058 return 1;
2059 }
2060
2061 /* Lookup the reference in the maps. */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002062 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002063 if (!appctx->ctx.map.ref) {
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01002064 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002065 appctx->st0 = STAT_CLI_PRINT;
2066 return 1;
2067 }
2068
Thierry FOURNIER9356c682014-01-28 15:55:37 +01002069 /* If the entry identifier start with a '#', it is considered as
2070 * pointer id
2071 */
2072 if (args[3][0] == '#' && args[3][1] == '0' && args[3][2] == 'x') {
2073 struct pat_ref_elt *ref;
2074 long long int conv;
2075 char *error;
2076
2077 /* Convert argument to integer value. */
2078 conv = strtoll(&args[3][1], &error, 16);
2079 if (*error != '\0') {
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01002080 appctx->ctx.cli.msg = "Malformed identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER9356c682014-01-28 15:55:37 +01002081 appctx->st0 = STAT_CLI_PRINT;
2082 return 1;
2083 }
2084
2085 /* Convert and check integer to pointer. */
2086 ref = (struct pat_ref_elt *)(long)conv;
2087 if ((long long int)(long)ref != conv) {
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01002088 appctx->ctx.cli.msg = "Malformed identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER9356c682014-01-28 15:55:37 +01002089 appctx->st0 = STAT_CLI_PRINT;
2090 return 1;
2091 }
2092
2093 /* Try to delete the entry. */
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002094 err = NULL;
2095 if (!pat_ref_set_by_id(appctx->ctx.map.ref, ref, args[4], &err)) {
2096 if (err)
2097 memprintf(&err, "%s.\n", err);
2098 appctx->ctx.cli.err = err;
2099 appctx->st0 = STAT_CLI_PRINT_FREE;
Thierry FOURNIER9356c682014-01-28 15:55:37 +01002100 return 1;
2101 }
2102 }
2103 else {
2104 /* Else, use the entry identifier as pattern
2105 * string, and update the value.
2106 */
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002107 err = NULL;
2108 if (!pat_ref_set(appctx->ctx.map.ref, args[3], args[4], &err)) {
2109 if (err)
2110 memprintf(&err, "%s.\n", err);
2111 appctx->ctx.cli.err = err;
2112 appctx->st0 = STAT_CLI_PRINT_FREE;
Thierry FOURNIER9356c682014-01-28 15:55:37 +01002113 return 1;
2114 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002115 }
2116
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002117 /* The set is done, send message. */
Thierry FOURNIER07e78c52014-12-18 15:28:01 +01002118 appctx->st0 = STAT_CLI_PROMPT;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002119 return 1;
2120 }
Emeric Brun4147b2e2014-06-16 18:36:30 +02002121#ifdef USE_OPENSSL
2122 else if (strcmp(args[1], "ssl") == 0) {
2123 if (strcmp(args[2], "ocsp-response") == 0) {
Lukas Tribuse4e30f72014-12-09 16:32:51 +01002124#if (defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP)
Emeric Brun4147b2e2014-06-16 18:36:30 +02002125 char *err = NULL;
2126
Emeric Brunaf4ef742014-06-19 14:10:45 +02002127 /* Expect one parameter: the new response in base64 encoding */
Emeric Brun4147b2e2014-06-16 18:36:30 +02002128 if (!*args[3]) {
2129 appctx->ctx.cli.msg = "'set ssl ocsp-response' expects response in base64 encoding.\n";
2130 appctx->st0 = STAT_CLI_PRINT;
2131 return 1;
2132 }
2133
2134 trash.len = base64dec(args[3], strlen(args[3]), trash.str, trash.size);
2135 if (trash.len < 0) {
2136 appctx->ctx.cli.msg = "'set ssl ocsp-response' received invalid base64 encoded response.\n";
2137 appctx->st0 = STAT_CLI_PRINT;
2138 return 1;
2139 }
2140
2141 if (ssl_sock_update_ocsp_response(&trash, &err)) {
2142 if (err) {
2143 memprintf(&err, "%s.\n", err);
2144 appctx->ctx.cli.err = err;
2145 appctx->st0 = STAT_CLI_PRINT_FREE;
2146 }
2147 return 1;
2148 }
2149 appctx->ctx.cli.msg = "OCSP Response updated!";
2150 appctx->st0 = STAT_CLI_PRINT;
2151 return 1;
2152#else
2153 appctx->ctx.cli.msg = "HAProxy was compiled against a version of OpenSSL that doesn't support OCSP stapling.\n";
2154 appctx->st0 = STAT_CLI_PRINT;
2155 return 1;
2156#endif
2157 }
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02002158 else if (strcmp(args[2], "tls-key") == 0) {
2159#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
2160 /* Expect two parameters: the filename and the new new TLS key in encoding */
2161 if (!*args[3] || !*args[4]) {
2162 appctx->ctx.cli.msg = "'set ssl tls-key' expects a filename and the new TLS key in base64 encoding.\n";
2163 appctx->st0 = STAT_CLI_PRINT;
2164 return 1;
2165 }
2166
2167 appctx->ctx.tlskeys.ref = tlskeys_ref_lookup_ref(args[3]);
2168 if(!appctx->ctx.tlskeys.ref) {
2169 appctx->ctx.cli.msg = "'set ssl tls-key' unable to locate referenced filename\n";
2170 appctx->st0 = STAT_CLI_PRINT;
2171 return 1;
2172 }
2173
2174 trash.len = base64dec(args[4], strlen(args[4]), trash.str, trash.size);
2175 if (trash.len != sizeof(struct tls_sess_key)) {
2176 appctx->ctx.cli.msg = "'set ssl tls-key' received invalid base64 encoded TLS key.\n";
2177 appctx->st0 = STAT_CLI_PRINT;
2178 return 1;
2179 }
2180
Pradeep Jindalcc79b002015-08-20 18:25:17 +05302181 memcpy(appctx->ctx.tlskeys.ref->tlskeys + ((appctx->ctx.tlskeys.ref->tls_ticket_enc_index + 2) % TLS_TICKETS_NO), trash.str, trash.len);
2182 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 +02002183
2184 appctx->ctx.cli.msg = "TLS ticket key updated!";
2185 appctx->st0 = STAT_CLI_PRINT;
2186 return 1;
2187#else
2188 appctx->ctx.cli.msg = "HAProxy was compiled against a version of OpenSSL "
2189 "that doesn't support specifying TLS ticket keys\n";
2190 appctx->st0 = STAT_CLI_PRINT;
2191 return 1;
2192#endif
2193 }
Emeric Brun4147b2e2014-06-16 18:36:30 +02002194 else {
2195 appctx->ctx.cli.msg = "'set ssl' only supports 'ocsp-response'.\n";
2196 appctx->st0 = STAT_CLI_PRINT;
2197 return 1;
2198 }
2199 }
2200#endif
Willy Tarreau7aabd112010-01-26 10:59:06 +01002201 else { /* unknown "set" parameter */
Willy Tarreau4483d432009-10-10 19:30:08 +02002202 return 0;
2203 }
2204 }
Cyril Bontécd19e512010-01-31 22:34:03 +01002205 else if (strcmp(args[0], "enable") == 0) {
Simon Horman671b6f02013-11-25 10:46:39 +09002206 if (strcmp(args[1], "agent") == 0) {
2207 struct server *sv;
2208
2209 sv = expect_server_admin(s, si, args[2]);
2210 if (!sv)
2211 return 1;
2212
Willy Tarreau2e10f5a2013-12-11 20:11:55 +01002213 if (!(sv->agent.state & CHK_ST_CONFIGURED)) {
2214 appctx->ctx.cli.msg = "Agent was not configured on this server, cannot enable.\n";
2215 appctx->st0 = STAT_CLI_PRINT;
2216 return 1;
2217 }
2218
2219 sv->agent.state |= CHK_ST_ENABLED;
Simon Horman671b6f02013-11-25 10:46:39 +09002220 return 1;
2221 }
Willy Tarreau9b5aecd2014-05-23 11:53:10 +02002222 else if (strcmp(args[1], "health") == 0) {
Cyril Bontécd19e512010-01-31 22:34:03 +01002223 struct server *sv;
2224
Willy Tarreaud52c41e2011-09-07 23:41:01 +02002225 sv = expect_server_admin(s, si, args[2]);
2226 if (!sv)
Cyril Bonté613f0df2011-03-03 20:49:04 +01002227 return 1;
Cyril Bonté613f0df2011-03-03 20:49:04 +01002228
Willy Tarreau9b5aecd2014-05-23 11:53:10 +02002229 if (!(sv->check.state & CHK_ST_CONFIGURED)) {
2230 appctx->ctx.cli.msg = "Health checks are not configured on this server, cannot enable.\n";
2231 appctx->st0 = STAT_CLI_PRINT;
2232 return 1;
2233 }
2234
2235 sv->check.state |= CHK_ST_ENABLED;
2236 return 1;
2237 }
2238 else if (strcmp(args[1], "server") == 0) {
2239 struct server *sv;
2240
2241 sv = expect_server_admin(s, si, args[2]);
2242 if (!sv)
2243 return 1;
2244
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02002245 srv_adm_set_ready(sv);
Willy Tarreau532a4502011-09-07 22:37:44 +02002246 return 1;
2247 }
2248 else if (strcmp(args[1], "frontend") == 0) {
2249 struct proxy *px;
2250
2251 px = expect_frontend_admin(s, si, args[2]);
2252 if (!px)
2253 return 1;
2254
2255 if (px->state == PR_STSTOPPED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002256 appctx->ctx.cli.msg = "Frontend was previously shut down, cannot enable.\n";
2257 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002258 return 1;
2259 }
2260
2261 if (px->state != PR_STPAUSED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002262 appctx->ctx.cli.msg = "Frontend is already enabled.\n";
2263 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002264 return 1;
2265 }
2266
2267 if (!resume_proxy(px)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002268 appctx->ctx.cli.msg = "Failed to resume frontend, check logs for precise cause (port conflict?).\n";
2269 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002270 return 1;
2271 }
Cyril Bontécd19e512010-01-31 22:34:03 +01002272 return 1;
2273 }
2274 else { /* unknown "enable" parameter */
Willy Tarreau9b5aecd2014-05-23 11:53:10 +02002275 appctx->ctx.cli.msg = "'enable' only supports 'agent', 'frontend', 'health', and 'server'.\n";
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002276 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002277 return 1;
Cyril Bontécd19e512010-01-31 22:34:03 +01002278 }
2279 }
2280 else if (strcmp(args[0], "disable") == 0) {
Simon Horman671b6f02013-11-25 10:46:39 +09002281 if (strcmp(args[1], "agent") == 0) {
2282 struct server *sv;
2283
2284 sv = expect_server_admin(s, si, args[2]);
2285 if (!sv)
2286 return 1;
2287
Willy Tarreau2e10f5a2013-12-11 20:11:55 +01002288 sv->agent.state &= ~CHK_ST_ENABLED;
Simon Horman671b6f02013-11-25 10:46:39 +09002289 return 1;
2290 }
Willy Tarreau9b5aecd2014-05-23 11:53:10 +02002291 else if (strcmp(args[1], "health") == 0) {
2292 struct server *sv;
2293
2294 sv = expect_server_admin(s, si, args[2]);
2295 if (!sv)
2296 return 1;
2297
2298 sv->check.state &= ~CHK_ST_ENABLED;
2299 return 1;
2300 }
Simon Horman671b6f02013-11-25 10:46:39 +09002301 else if (strcmp(args[1], "server") == 0) {
Cyril Bontécd19e512010-01-31 22:34:03 +01002302 struct server *sv;
2303
Willy Tarreaud52c41e2011-09-07 23:41:01 +02002304 sv = expect_server_admin(s, si, args[2]);
2305 if (!sv)
Cyril Bonté613f0df2011-03-03 20:49:04 +01002306 return 1;
Cyril Bonté613f0df2011-03-03 20:49:04 +01002307
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02002308 srv_adm_set_maint(sv);
Willy Tarreau532a4502011-09-07 22:37:44 +02002309 return 1;
2310 }
2311 else if (strcmp(args[1], "frontend") == 0) {
2312 struct proxy *px;
2313
2314 px = expect_frontend_admin(s, si, args[2]);
2315 if (!px)
2316 return 1;
2317
2318 if (px->state == PR_STSTOPPED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002319 appctx->ctx.cli.msg = "Frontend was previously shut down, cannot disable.\n";
2320 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002321 return 1;
2322 }
2323
2324 if (px->state == PR_STPAUSED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002325 appctx->ctx.cli.msg = "Frontend is already disabled.\n";
2326 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002327 return 1;
2328 }
2329
2330 if (!pause_proxy(px)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002331 appctx->ctx.cli.msg = "Failed to pause frontend, check logs for precise cause.\n";
2332 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002333 return 1;
2334 }
Cyril Bontécd19e512010-01-31 22:34:03 +01002335 return 1;
2336 }
2337 else { /* unknown "disable" parameter */
Willy Tarreau9b5aecd2014-05-23 11:53:10 +02002338 appctx->ctx.cli.msg = "'disable' only supports 'agent', 'frontend', 'health', and 'server'.\n";
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002339 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002340 return 1;
2341 }
2342 }
2343 else if (strcmp(args[0], "shutdown") == 0) {
2344 if (strcmp(args[1], "frontend") == 0) {
2345 struct proxy *px;
2346
2347 px = expect_frontend_admin(s, si, args[2]);
2348 if (!px)
2349 return 1;
2350
2351 if (px->state == PR_STSTOPPED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002352 appctx->ctx.cli.msg = "Frontend was already shut down.\n";
2353 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002354 return 1;
2355 }
2356
2357 Warning("Proxy %s stopped (FE: %lld conns, BE: %lld conns).\n",
2358 px->id, px->fe_counters.cum_conn, px->be_counters.cum_conn);
2359 send_log(px, LOG_WARNING, "Proxy %s stopped (FE: %lld conns, BE: %lld conns).\n",
2360 px->id, px->fe_counters.cum_conn, px->be_counters.cum_conn);
2361 stop_proxy(px);
2362 return 1;
2363 }
Willy Tarreaua295edc2011-09-07 23:21:03 +02002364 else if (strcmp(args[1], "session") == 0) {
Willy Tarreau87b09662015-04-03 00:22:06 +02002365 struct stream *sess, *ptr;
Willy Tarreaua295edc2011-09-07 23:21:03 +02002366
Willy Tarreaud0d8da92015-04-04 02:10:38 +02002367 if (strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002368 appctx->ctx.cli.msg = stats_permission_denied_msg;
2369 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaua295edc2011-09-07 23:21:03 +02002370 return 1;
2371 }
2372
2373 if (!*args[2]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002374 appctx->ctx.cli.msg = "Session pointer expected (use 'show sess').\n";
2375 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaua295edc2011-09-07 23:21:03 +02002376 return 1;
2377 }
2378
2379 ptr = (void *)strtoul(args[2], NULL, 0);
2380
Willy Tarreau87b09662015-04-03 00:22:06 +02002381 /* first, look for the requested stream in the stream table */
2382 list_for_each_entry(sess, &streams, list) {
Willy Tarreaua295edc2011-09-07 23:21:03 +02002383 if (sess == ptr)
2384 break;
2385 }
2386
Willy Tarreau87b09662015-04-03 00:22:06 +02002387 /* do we have the stream ? */
Willy Tarreaua295edc2011-09-07 23:21:03 +02002388 if (sess != ptr) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002389 appctx->ctx.cli.msg = "No such session (use 'show sess').\n";
2390 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaua295edc2011-09-07 23:21:03 +02002391 return 1;
2392 }
2393
Willy Tarreaue7dff022015-04-03 01:14:29 +02002394 stream_shutdown(sess, SF_ERR_KILLED);
Willy Tarreaua295edc2011-09-07 23:21:03 +02002395 return 1;
2396 }
Willy Tarreau52b2d222011-09-07 23:48:48 +02002397 else if (strcmp(args[1], "sessions") == 0) {
2398 if (strcmp(args[2], "server") == 0) {
2399 struct server *sv;
Willy Tarreau87b09662015-04-03 00:22:06 +02002400 struct stream *sess, *sess_bck;
Willy Tarreau52b2d222011-09-07 23:48:48 +02002401
2402 sv = expect_server_admin(s, si, args[3]);
2403 if (!sv)
2404 return 1;
2405
Willy Tarreau87b09662015-04-03 00:22:06 +02002406 /* kill all the stream that are on this server */
Willy Tarreau52b2d222011-09-07 23:48:48 +02002407 list_for_each_entry_safe(sess, sess_bck, &sv->actconns, by_srv)
2408 if (sess->srv_conn == sv)
Willy Tarreaue7dff022015-04-03 01:14:29 +02002409 stream_shutdown(sess, SF_ERR_KILLED);
Willy Tarreau52b2d222011-09-07 23:48:48 +02002410
2411 return 1;
2412 }
2413 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002414 appctx->ctx.cli.msg = "'shutdown sessions' only supports 'server'.\n";
2415 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau52b2d222011-09-07 23:48:48 +02002416 return 1;
2417 }
2418 }
Willy Tarreau532a4502011-09-07 22:37:44 +02002419 else { /* unknown "disable" parameter */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002420 appctx->ctx.cli.msg = "'shutdown' only supports 'frontend', 'session' and 'sessions'.\n";
2421 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002422 return 1;
Cyril Bontécd19e512010-01-31 22:34:03 +01002423 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002424 }
2425 else if (strcmp(args[0], "del") == 0) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002426 if (strcmp(args[1], "map") == 0 || strcmp(args[1], "acl") == 0) {
2427 if (args[1][0] == 'm')
2428 appctx->ctx.map.display_flags = PAT_REF_MAP;
2429 else
2430 appctx->ctx.map.display_flags = PAT_REF_ACL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002431
2432 /* Expect two parameters: map name and key. */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002433 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
2434 if (!*args[2] || !*args[3]) {
2435 appctx->ctx.cli.msg = "This command expects two parameters: map identifier and key.\n";
2436 appctx->st0 = STAT_CLI_PRINT;
2437 return 1;
2438 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002439 }
2440
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002441 else {
2442 if (!*args[2] || !*args[3]) {
2443 appctx->ctx.cli.msg = "This command expects two parameters: ACL identifier and key.\n";
2444 appctx->st0 = STAT_CLI_PRINT;
2445 return 1;
2446 }
2447 }
2448
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002449 /* Lookup the reference in the maps. */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002450 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002451 if (!appctx->ctx.map.ref ||
2452 !(appctx->ctx.map.ref->flags & appctx->ctx.map.display_flags)) {
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01002453 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002454 appctx->st0 = STAT_CLI_PRINT;
2455 return 1;
2456 }
2457
Thierry FOURNIER9356c682014-01-28 15:55:37 +01002458 /* If the entry identifier start with a '#', it is considered as
2459 * pointer id
2460 */
2461 if (args[3][0] == '#' && args[3][1] == '0' && args[3][2] == 'x') {
2462 struct pat_ref_elt *ref;
2463 long long int conv;
2464 char *error;
2465
2466 /* Convert argument to integer value. */
2467 conv = strtoll(&args[3][1], &error, 16);
2468 if (*error != '\0') {
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01002469 appctx->ctx.cli.msg = "Malformed identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER9356c682014-01-28 15:55:37 +01002470 appctx->st0 = STAT_CLI_PRINT;
2471 return 1;
2472 }
2473
2474 /* Convert and check integer to pointer. */
2475 ref = (struct pat_ref_elt *)(long)conv;
2476 if ((long long int)(long)ref != conv) {
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01002477 appctx->ctx.cli.msg = "Malformed identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER9356c682014-01-28 15:55:37 +01002478 appctx->st0 = STAT_CLI_PRINT;
2479 return 1;
2480 }
2481
2482 /* Try to delete the entry. */
2483 if (!pat_ref_delete_by_id(appctx->ctx.map.ref, ref)) {
2484 /* The entry is not found, send message. */
2485 appctx->ctx.cli.msg = "Key not found.\n";
2486 appctx->st0 = STAT_CLI_PRINT;
2487 return 1;
2488 }
2489 }
2490 else {
2491 /* Else, use the entry identifier as pattern
2492 * string and try to delete the entry.
2493 */
2494 if (!pat_ref_delete(appctx->ctx.map.ref, args[3])) {
2495 /* The entry is not found, send message. */
2496 appctx->ctx.cli.msg = "Key not found.\n";
2497 appctx->st0 = STAT_CLI_PRINT;
2498 return 1;
2499 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002500 }
2501
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002502 /* The deletion is done, send message. */
Thierry FOURNIER07e78c52014-12-18 15:28:01 +01002503 appctx->st0 = STAT_CLI_PROMPT;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002504 return 1;
2505 }
2506 else { /* unknown "del" parameter */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002507 appctx->ctx.cli.msg = "'del' only supports 'map' or 'acl'.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002508 appctx->st0 = STAT_CLI_PRINT;
2509 return 1;
2510 }
2511 }
2512 else if (strcmp(args[0], "add") == 0) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002513 if (strcmp(args[1], "map") == 0 ||
2514 strcmp(args[1], "acl") == 0) {
2515 int ret;
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002516 char *err;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002517
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002518 /* Set flags. */
2519 if (args[1][0] == 'm')
2520 appctx->ctx.map.display_flags = PAT_REF_MAP;
2521 else
2522 appctx->ctx.map.display_flags = PAT_REF_ACL;
2523
2524 /* If the keywork is "map", we expect three parameters, if it
2525 * is "acl", we expect only two parameters
2526 */
2527 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
2528 if (!*args[2] || !*args[3] || !*args[4]) {
2529 appctx->ctx.cli.msg = "'add map' expects three parameters: map identifier, key and value.\n";
2530 appctx->st0 = STAT_CLI_PRINT;
2531 return 1;
2532 }
2533 }
2534 else {
2535 if (!*args[2] || !*args[3]) {
2536 appctx->ctx.cli.msg = "'add acl' expects two parameters: ACL identifier and pattern.\n";
2537 appctx->st0 = STAT_CLI_PRINT;
2538 return 1;
2539 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002540 }
2541
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002542 /* Lookup for the reference. */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002543 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002544 if (!appctx->ctx.map.ref) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002545 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01002546 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002547 else
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01002548 appctx->ctx.cli.msg = "Unknown ACL identifier. Please use #<id> or <file>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002549 appctx->st0 = STAT_CLI_PRINT;
2550 return 1;
2551 }
2552
Thierry FOURNIER64c585f2014-01-29 20:02:36 +01002553 /* The command "add acl" is prohibited if the reference
2554 * use samples.
2555 */
2556 if ((appctx->ctx.map.display_flags & PAT_REF_ACL) &&
2557 (appctx->ctx.map.ref->flags & PAT_REF_SMP)) {
2558 appctx->ctx.cli.msg = "This ACL is shared with a map containing samples. "
2559 "You must use the command 'add map' to add values.\n";
2560 appctx->st0 = STAT_CLI_PRINT;
2561 return 1;
2562 }
2563
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002564 /* Add value. */
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002565 err = NULL;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002566 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02002567 ret = pat_ref_add(appctx->ctx.map.ref, args[3], args[4], &err);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002568 else
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02002569 ret = pat_ref_add(appctx->ctx.map.ref, args[3], NULL, &err);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002570 if (!ret) {
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002571 if (err)
2572 memprintf(&err, "%s.\n", err);
2573 appctx->ctx.cli.err = err;
2574 appctx->st0 = STAT_CLI_PRINT_FREE;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002575 return 1;
2576 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002577
2578 /* The add is done, send message. */
Thierry FOURNIER07e78c52014-12-18 15:28:01 +01002579 appctx->st0 = STAT_CLI_PROMPT;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002580 return 1;
2581 }
2582 else { /* unknown "del" parameter */
2583 appctx->ctx.cli.msg = "'add' only supports 'map'.\n";
2584 appctx->st0 = STAT_CLI_PRINT;
2585 return 1;
2586 }
Cyril Bontécd19e512010-01-31 22:34:03 +01002587 }
2588 else { /* not "show" nor "clear" nor "get" nor "set" nor "enable" nor "disable" */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002589 return 0;
2590 }
2591 return 1;
2592}
2593
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002594/* This I/O handler runs as an applet embedded in a stream interface. It is
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002595 * used to processes I/O from/to the stats unix socket. The system relies on a
2596 * state machine handling requests and various responses. We read a request,
2597 * then we process it and send the response, and we possibly display a prompt.
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002598 * Then we can read again. The state is stored in appctx->st0 and is one of the
2599 * STAT_CLI_* constants. appctx->st1 is used to indicate whether prompt is enabled
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002600 * or not.
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002601 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02002602static void cli_io_handler(struct appctx *appctx)
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002603{
Willy Tarreau00a37f02015-04-13 12:05:19 +02002604 struct stream_interface *si = appctx->owner;
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002605 struct channel *req = si_oc(si);
2606 struct channel *res = si_ic(si);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002607 int reql;
2608 int len;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002609
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002610 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
2611 goto out;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002612
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002613 while (1) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002614 if (appctx->st0 == STAT_CLI_INIT) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002615 /* Stats output not initialized yet */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002616 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
2617 appctx->st0 = STAT_CLI_GETREQ;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002618 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002619 else if (appctx->st0 == STAT_CLI_END) {
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002620 /* Let's close for real now. We just close the request
2621 * side, the conditions below will complete if needed.
2622 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02002623 si_shutw(si);
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002624 break;
2625 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002626 else if (appctx->st0 == STAT_CLI_GETREQ) {
Willy Tarreau4e33d862009-10-11 23:35:10 +02002627 /* ensure we have some output room left in the event we
2628 * would want to return some info right after parsing.
2629 */
Willy Tarreau4e4292b2014-11-28 12:18:45 +01002630 if (buffer_almost_full(si_ib(si))) {
Willy Tarreaufe127932015-04-21 19:23:39 +02002631 si_applet_cant_put(si);
Willy Tarreau4e33d862009-10-11 23:35:10 +02002632 break;
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01002633 }
Willy Tarreau4e33d862009-10-11 23:35:10 +02002634
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002635 reql = bo_getline(si_oc(si), trash.str, trash.size);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002636 if (reql <= 0) { /* closed or EOL not found */
2637 if (reql == 0)
2638 break;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002639 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002640 continue;
2641 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002642
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002643 /* seek for a possible semi-colon. If we find one, we
2644 * replace it with an LF and skip only this part.
2645 */
2646 for (len = 0; len < reql; len++)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002647 if (trash.str[len] == ';') {
2648 trash.str[len] = '\n';
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002649 reql = len + 1;
2650 break;
2651 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002652
Willy Tarreau816fc222009-10-04 07:36:58 +02002653 /* now it is time to check that we have a full line,
2654 * remove the trailing \n and possibly \r, then cut the
2655 * line.
2656 */
2657 len = reql - 1;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002658 if (trash.str[len] != '\n') {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002659 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002660 continue;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002661 }
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002662
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002663 if (len && trash.str[len-1] == '\r')
Willy Tarreau816fc222009-10-04 07:36:58 +02002664 len--;
2665
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002666 trash.str[len] = '\0';
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002667
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002668 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002669 if (len) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002670 if (strcmp(trash.str, "quit") == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002671 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002672 continue;
2673 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002674 else if (strcmp(trash.str, "prompt") == 0)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002675 appctx->st1 = !appctx->st1;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002676 else if (strcmp(trash.str, "help") == 0 ||
2677 !stats_sock_parse_request(si, trash.str)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002678 appctx->ctx.cli.msg = stats_sock_usage_msg;
2679 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02002680 }
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002681 /* NB: stats_sock_parse_request() may have put
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002682 * another STAT_CLI_O_* into appctx->st0.
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002683 */
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002684 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002685 else if (!appctx->st1) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002686 /* if prompt is disabled, print help on empty lines,
2687 * so that the user at least knows how to enable
2688 * prompt and find help.
2689 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002690 appctx->ctx.cli.msg = stats_sock_usage_msg;
2691 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002692 }
2693
2694 /* re-adjust req buffer */
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002695 bo_skip(si_oc(si), reql);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002696 req->flags |= CF_READ_DONTWAIT; /* we plan to read small requests */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002697 }
Willy Tarreau68c00c72015-09-25 19:21:19 +02002698 else { /* output functions */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002699 switch (appctx->st0) {
Andrew Hayworthe32d1862015-10-02 15:08:10 +00002700 case STAT_CLI_PROMPT:
2701 break;
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02002702 case STAT_CLI_PRINT:
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002703 if (bi_putstr(si_ic(si), appctx->ctx.cli.msg) != -1)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002704 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreaubc18da12015-03-13 14:00:47 +01002705 else
Willy Tarreaufe127932015-04-21 19:23:39 +02002706 si_applet_cant_put(si);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002707 break;
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002708 case STAT_CLI_PRINT_FREE:
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002709 if (bi_putstr(si_ic(si), appctx->ctx.cli.err) != -1) {
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002710 free(appctx->ctx.cli.err);
2711 appctx->st0 = STAT_CLI_PROMPT;
2712 }
Willy Tarreaubc18da12015-03-13 14:00:47 +01002713 else
Willy Tarreaufe127932015-04-21 19:23:39 +02002714 si_applet_cant_put(si);
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002715 break;
Baptiste Assmann9b6857e2015-09-18 14:49:12 +02002716 case STAT_CLI_O_BACKEND:
2717 if (stats_dump_backend_to_buffer(si))
2718 appctx->st0 = STAT_CLI_PROMPT;
2719 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002720 case STAT_CLI_O_INFO:
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002721 if (stats_dump_info_to_buffer(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002722 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002723 break;
Baptiste Assmann28289462015-07-03 08:01:20 +02002724 case STAT_CLI_O_SERVERS_STATE:
2725 if (stats_dump_servers_state_to_buffer(si))
2726 appctx->st0 = STAT_CLI_PROMPT;
2727 break;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002728 case STAT_CLI_O_STAT:
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002729 if (stats_dump_stat_to_buffer(si, NULL))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002730 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002731 break;
Baptiste Assmann3863f972015-05-17 00:33:24 +02002732 case STAT_CLI_O_RESOLVERS:
2733 if (stats_dump_resolvers_to_buffer(si))
2734 appctx->st0 = STAT_CLI_PROMPT;
2735 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002736 case STAT_CLI_O_SESS:
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01002737 if (stats_dump_sess_to_buffer(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002738 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002739 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002740 case STAT_CLI_O_ERR: /* errors dump */
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01002741 if (stats_dump_errors_to_buffer(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002742 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002743 break;
Willy Tarreau69f58c82010-07-12 17:55:33 +02002744 case STAT_CLI_O_TAB:
Simon Hormanc88b8872011-06-15 15:18:49 +09002745 case STAT_CLI_O_CLR:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002746 if (stats_table_request(si, appctx->st0))
2747 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau69f58c82010-07-12 17:55:33 +02002748 break;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002749 case STAT_CLI_O_PATS:
2750 if (stats_pats_list(si))
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002751 appctx->st0 = STAT_CLI_PROMPT;
2752 break;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002753 case STAT_CLI_O_PAT:
2754 if (stats_pat_list(si))
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002755 appctx->st0 = STAT_CLI_PROMPT;
2756 break;
2757 case STAT_CLI_O_MLOOK:
2758 if (stats_map_lookup(si))
2759 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau4efb3532014-01-29 12:13:39 +01002760 break;
Willy Tarreau12833bb2014-01-28 16:49:56 +01002761 case STAT_CLI_O_POOLS:
2762 if (stats_dump_pools_to_buffer(si))
2763 appctx->st0 = STAT_CLI_PROMPT;
2764 break;
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02002765#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
2766 case STAT_CLI_O_TLSK:
2767 if (stats_tlskeys_list(si))
2768 appctx->st0 = STAT_CLI_PROMPT;
2769 break;
2770#endif
Willy Tarreauae795722016-02-16 11:27:28 +01002771 case STAT_CLI_O_ENV: /* environment dump */
2772 if (stats_dump_env_to_buffer(si))
2773 appctx->st0 = STAT_CLI_PROMPT;
2774 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002775 default: /* abnormal state */
Willy Tarreau5cfa3bc2015-09-25 20:08:51 +02002776 si->flags |= SI_FL_ERR;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002777 break;
2778 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002779
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002780 /* The post-command prompt is either LF alone or LF + '> ' in interactive mode */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002781 if (appctx->st0 == STAT_CLI_PROMPT) {
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002782 if (bi_putstr(si_ic(si), appctx->st1 ? "\n> " : "\n") != -1)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002783 appctx->st0 = STAT_CLI_GETREQ;
Willy Tarreaubc18da12015-03-13 14:00:47 +01002784 else
Willy Tarreaufe127932015-04-21 19:23:39 +02002785 si_applet_cant_put(si);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002786 }
2787
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002788 /* If the output functions are still there, it means they require more room. */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002789 if (appctx->st0 >= STAT_CLI_OUTPUT)
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002790 break;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002791
2792 /* Now we close the output if one of the writers did so,
2793 * or if we're not in interactive mode and the request
2794 * buffer is empty. This still allows pipelined requests
2795 * to be sent in non-interactive mode.
2796 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002797 if ((res->flags & (CF_SHUTW|CF_SHUTW_NOW)) || (!appctx->st1 && !req->buf->o)) {
2798 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002799 continue;
2800 }
2801
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002802 /* switch state back to GETREQ to read next requests */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002803 appctx->st0 = STAT_CLI_GETREQ;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002804 }
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002805 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002806
Willy Tarreauc9e930a2015-09-25 20:06:08 +02002807 if ((res->flags & CF_SHUTR) && (si->state == SI_ST_EST)) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002808 DPRINTF(stderr, "%s@%d: si to buf closed. req=%08x, res=%08x, st=%d\n",
2809 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
Aman Guptaceafb4a2012-04-02 18:57:54 -07002810 /* Other side has closed, let's abort if we have no more processing to do
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002811 * and nothing more to consume. This is comparable to a broken pipe, so
2812 * we forward the close to the request side so that it flows upstream to
2813 * the client.
2814 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02002815 si_shutw(si);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002816 }
2817
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002818 if ((req->flags & CF_SHUTW) && (si->state == SI_ST_EST) && (appctx->st0 < STAT_CLI_OUTPUT)) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002819 DPRINTF(stderr, "%s@%d: buf to si closed. req=%08x, res=%08x, st=%d\n",
2820 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
2821 /* We have no more processing to do, and nothing more to send, and
2822 * the client side has closed. So we'll forward this state downstream
2823 * on the response buffer.
2824 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02002825 si_shutr(si);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002826 res->flags |= CF_READ_NULL;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002827 }
2828
Willy Tarreau828824a2015-04-19 17:20:03 +02002829 out:
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01002830 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 +02002831 __FUNCTION__, __LINE__,
Willy Tarreau9b28e032012-10-12 23:49:43 +02002832 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 +02002833}
2834
Willy Tarreau638d40a2016-02-24 23:11:01 +01002835/* Emits a stats field without any surrounding element and properly encoded to
2836 * resist CSV output. Returns non-zero on success, 0 if the buffer is full.
2837 */
Willy Tarreaub47785f2016-02-24 23:28:31 +01002838int stats_emit_raw_data_field(struct chunk *out, const struct field *f)
Willy Tarreau638d40a2016-02-24 23:11:01 +01002839{
2840 switch (field_format(f, 0)) {
2841 case FF_EMPTY: return 1;
2842 case FF_S32: return chunk_appendf(out, "%d", f->u.s32);
2843 case FF_U32: return chunk_appendf(out, "%u", f->u.u32);
2844 case FF_S64: return chunk_appendf(out, "%lld", (long long)f->u.s64);
2845 case FF_U64: return chunk_appendf(out, "%llu", (unsigned long long)f->u.u64);
2846 case FF_STR: return csv_enc_append(field_str(f, 0), 1, out) != NULL;
2847 default: return chunk_appendf(out, "[INCORRECT_FIELD_TYPE_%08x]", f->type);
2848 }
2849}
2850
Willy Tarreaub47785f2016-02-24 23:28:31 +01002851/* Emits a stats field prefixed with its type. No CSV encoding is prepared, the
2852 * output is supposed to be used on its own line. Returns non-zero on success, 0
2853 * if the buffer is full.
2854 */
2855int stats_emit_typed_data_field(struct chunk *out, const struct field *f)
2856{
2857 switch (field_format(f, 0)) {
2858 case FF_EMPTY: return 1;
2859 case FF_S32: return chunk_appendf(out, "s32:%d", f->u.s32);
2860 case FF_U32: return chunk_appendf(out, "u32:%u", f->u.u32);
2861 case FF_S64: return chunk_appendf(out, "s64:%lld", (long long)f->u.s64);
2862 case FF_U64: return chunk_appendf(out, "u64:%llu", (unsigned long long)f->u.u64);
2863 case FF_STR: return chunk_appendf(out, "str:%s", field_str(f, 0));
2864 default: return chunk_appendf(out, "%08x:?", f->type);
2865 }
2866}
2867
2868/* Emits an encoding of the field type on 3 characters followed by a delimiter.
2869 * Returns non-zero on success, 0 if the buffer is full.
2870 */
2871int stats_emit_field_tags(struct chunk *out, const struct field *f, char delim)
2872{
2873 char origin, nature, scope;
2874
2875 switch (field_origin(f, 0)) {
2876 case FO_METRIC: origin = 'M'; break;
2877 case FO_STATUS: origin = 'S'; break;
2878 case FO_KEY: origin = 'K'; break;
2879 case FO_CONFIG: origin = 'C'; break;
2880 case FO_PRODUCT: origin = 'P'; break;
2881 default: origin = '?'; break;
2882 }
2883
2884 switch (field_nature(f, 0)) {
2885 case FN_GAUGE: nature = 'G'; break;
2886 case FN_LIMIT: nature = 'L'; break;
2887 case FN_MIN: nature = 'm'; break;
2888 case FN_MAX: nature = 'M'; break;
2889 case FN_RATE: nature = 'R'; break;
2890 case FN_COUNTER: nature = 'C'; break;
2891 case FN_DURATION: nature = 'D'; break;
2892 case FN_AGE: nature = 'A'; break;
2893 case FN_TIME: nature = 'T'; break;
2894 case FN_NAME: nature = 'N'; break;
2895 case FN_OUTPUT: nature = 'O'; break;
2896 case FN_AVG: nature = 'a'; break;
2897 default: nature = '?'; break;
2898 }
2899
2900 switch (field_scope(f, 0)) {
2901 case FS_PROCESS: scope = 'P'; break;
2902 case FS_SERVICE: scope = 'S'; break;
2903 case FS_SYSTEM: scope = 's'; break;
2904 case FS_CLUSTER: scope = 'C'; break;
2905 default: scope = '?'; break;
2906 }
2907
2908 return chunk_appendf(out, "%c%c%c%c", origin, nature, scope, delim);
2909}
2910
Willy Tarreaubf95cba2016-01-11 18:27:29 +01002911/* Dump all fields from <info> into <out> using the "show info" format (name: value) */
2912static int stats_dump_info_fields(struct chunk *out, const struct field *info)
2913{
2914 int field;
2915
2916 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
2917 if (!field_format(info, field))
2918 continue;
2919
2920 if (!chunk_appendf(out, "%s: ", info_field_names[field]))
2921 return 0;
2922 if (!stats_emit_raw_data_field(out, &info[field]))
2923 return 0;
2924 if (!chunk_strcat(out, "\n"))
2925 return 0;
2926 }
2927 return 1;
2928}
2929
Willy Tarreaucb809122016-01-11 20:08:42 +01002930/* Dump all fields from <info> into <out> using the "show info typed" format */
2931static int stats_dump_typed_info_fields(struct chunk *out, const struct field *info)
2932{
2933 int field;
2934
2935 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
2936 if (!field_format(info, field))
2937 continue;
2938
2939 if (!chunk_appendf(out, "%d.%s.%u:", field, info_field_names[field], info[INF_PROCESS_NUM].u.u32))
2940 return 0;
2941 if (!stats_emit_field_tags(out, &info[field], ':'))
2942 return 0;
2943 if (!stats_emit_typed_data_field(out, &info[field]))
2944 return 0;
2945 if (!chunk_strcat(out, "\n"))
2946 return 0;
2947 }
2948 return 1;
2949}
2950
Thierry Fourniercb2c7672016-03-25 08:19:23 +01002951/* Fill <info> with HAProxy global info. <info> is preallocated
2952 * array of length <len>. The length of the aray must be
2953 * INF_TOTAL_FIELDS. If this length is less then this value, the
2954 * function returns 0, otherwise, it returns 1.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002955 */
Thierry Fourniercb2c7672016-03-25 08:19:23 +01002956int stats_fill_info(struct field *info, int len)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002957{
2958 unsigned int up = (now.tv_sec - start_date.tv_sec);
Willy Tarreau4529c072016-01-11 18:17:07 +01002959 struct chunk *out = get_trash_chunk();
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002960
Willy Tarreau0c9c2722014-05-28 12:28:58 +02002961#ifdef USE_OPENSSL
2962 int ssl_sess_rate = read_freq_ctr(&global.ssl_per_sec);
2963 int ssl_key_rate = read_freq_ctr(&global.ssl_fe_keys_per_sec);
2964 int ssl_reuse = 0;
2965
2966 if (ssl_key_rate < ssl_sess_rate) {
2967 /* count the ssl reuse ratio and avoid overflows in both directions */
2968 ssl_reuse = 100 - (100 * ssl_key_rate + (ssl_sess_rate - 1) / 2) / ssl_sess_rate;
2969 }
2970#endif
2971
Thierry Fourniercb2c7672016-03-25 08:19:23 +01002972 if (len < INF_TOTAL_FIELDS)
2973 return 0;
2974
Willy Tarreau4529c072016-01-11 18:17:07 +01002975 chunk_reset(out);
Thierry Fourniercb2c7672016-03-25 08:19:23 +01002976 memset(info, 0, sizeof(*info) * len);
Willy Tarreau4529c072016-01-11 18:17:07 +01002977
2978 info[INF_NAME] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, PRODUCT_NAME);
2979 info[INF_VERSION] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, HAPROXY_VERSION);
2980 info[INF_RELEASE_DATE] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, HAPROXY_DATE);
2981
2982 info[INF_NBPROC] = mkf_u32(FO_CONFIG|FS_SERVICE, global.nbproc);
2983 info[INF_PROCESS_NUM] = mkf_u32(FO_KEY, relative_pid);
2984 info[INF_PID] = mkf_u32(FO_STATUS, pid);
2985
2986 info[INF_UPTIME] = mkf_str(FN_DURATION, chunk_newstr(out));
2987 chunk_appendf(out, "%ud %uh%02um%02us", up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60));
2988
2989 info[INF_UPTIME_SEC] = mkf_u32(FN_DURATION, up);
2990 info[INF_MEMMAX_MB] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_memmax);
2991 info[INF_POOL_ALLOC_MB] = mkf_u32(0, (unsigned)(pool_total_allocated() / 1048576L));
2992 info[INF_POOL_USED_MB] = mkf_u32(0, (unsigned)(pool_total_used() / 1048576L));
2993 info[INF_POOL_FAILED] = mkf_u32(FN_COUNTER, pool_total_failures());
2994 info[INF_ULIMIT_N] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_nofile);
2995 info[INF_MAXSOCK] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxsock);
2996 info[INF_MAXCONN] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxconn);
2997 info[INF_HARD_MAXCONN] = mkf_u32(FO_CONFIG|FN_LIMIT, global.hardmaxconn);
2998 info[INF_CURR_CONN] = mkf_u32(0, actconn);
2999 info[INF_CUM_CONN] = mkf_u32(FN_COUNTER, totalconn);
3000 info[INF_CUM_REQ] = mkf_u32(FN_COUNTER, global.req_count);
3001#ifdef USE_OPENSSL
3002 info[INF_MAX_SSL_CONNS] = mkf_u32(FN_MAX, global.maxsslconn);
3003 info[INF_CURR_SSL_CONNS] = mkf_u32(0, sslconns);
3004 info[INF_CUM_SSL_CONNS] = mkf_u32(FN_COUNTER, totalsslconns);
3005#endif
3006 info[INF_MAXPIPES] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxpipes);
3007 info[INF_PIPES_USED] = mkf_u32(0, pipes_used);
3008 info[INF_PIPES_FREE] = mkf_u32(0, pipes_free);
3009 info[INF_CONN_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.conn_per_sec));
3010 info[INF_CONN_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.cps_lim);
3011 info[INF_MAX_CONN_RATE] = mkf_u32(FN_MAX, global.cps_max);
3012 info[INF_SESS_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.sess_per_sec));
3013 info[INF_SESS_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.sps_lim);
3014 info[INF_MAX_SESS_RATE] = mkf_u32(FN_RATE, global.sps_max);
3015
3016#ifdef USE_OPENSSL
3017 info[INF_SSL_RATE] = mkf_u32(FN_RATE, ssl_sess_rate);
3018 info[INF_SSL_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.ssl_lim);
3019 info[INF_MAX_SSL_RATE] = mkf_u32(FN_MAX, global.ssl_max);
3020 info[INF_SSL_FRONTEND_KEY_RATE] = mkf_u32(0, ssl_key_rate);
3021 info[INF_SSL_FRONTEND_MAX_KEY_RATE] = mkf_u32(FN_MAX, global.ssl_fe_keys_max);
3022 info[INF_SSL_FRONTEND_SESSION_REUSE_PCT] = mkf_u32(0, ssl_reuse);
3023 info[INF_SSL_BACKEND_KEY_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.ssl_be_keys_per_sec));
3024 info[INF_SSL_BACKEND_MAX_KEY_RATE] = mkf_u32(FN_MAX, global.ssl_be_keys_max);
3025 info[INF_SSL_CACHE_LOOKUPS] = mkf_u32(FN_COUNTER, global.shctx_lookups);
3026 info[INF_SSL_CACHE_MISSES] = mkf_u32(FN_COUNTER, global.shctx_misses);
3027#endif
3028 info[INF_COMPRESS_BPS_IN] = mkf_u32(FN_RATE, read_freq_ctr(&global.comp_bps_in));
3029 info[INF_COMPRESS_BPS_OUT] = mkf_u32(FN_RATE, read_freq_ctr(&global.comp_bps_out));
3030 info[INF_COMPRESS_BPS_RATE_LIM] = mkf_u32(FO_CONFIG|FN_LIMIT, global.comp_rate_lim);
3031#ifdef USE_ZLIB
3032 info[INF_ZLIB_MEM_USAGE] = mkf_u32(0, zlib_used_memory);
3033 info[INF_MAX_ZLIB_MEM_USAGE] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxzlibmem);
3034#endif
3035 info[INF_TASKS] = mkf_u32(0, nb_tasks_cur);
3036 info[INF_RUN_QUEUE] = mkf_u32(0, run_queue_cur);
3037 info[INF_IDLE_PCT] = mkf_u32(FN_AVG, idle_pct);
3038 info[INF_NODE] = mkf_str(FO_CONFIG|FN_OUTPUT|FS_SERVICE, global.node);
3039 if (global.desc)
3040 info[INF_DESCRIPTION] = mkf_str(FO_CONFIG|FN_OUTPUT|FS_SERVICE, global.desc);
3041
Thierry Fourniercb2c7672016-03-25 08:19:23 +01003042 return 1;
3043}
3044
3045/* This function dumps information onto the stream interface's read buffer.
3046 * It returns 0 as long as it does not complete, non-zero upon completion.
3047 * No state is used.
3048 */
3049static int stats_dump_info_to_buffer(struct stream_interface *si)
3050{
3051 struct appctx *appctx = __objt_appctx(si->end);
3052
3053 if (!stats_fill_info(info, INF_TOTAL_FIELDS))
3054 return 0;
3055
Willy Tarreau1b4ba1e2016-01-11 18:29:04 +01003056 chunk_reset(&trash);
Willy Tarreaucb809122016-01-11 20:08:42 +01003057
3058 if (appctx->ctx.stats.flags & STAT_FMT_TYPED)
3059 stats_dump_typed_info_fields(&trash, info);
3060 else
3061 stats_dump_info_fields(&trash, info);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003062
Willy Tarreaubc18da12015-03-13 14:00:47 +01003063 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02003064 si_applet_cant_put(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003065 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01003066 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003067
3068 return 1;
3069}
3070
Baptiste Assmann28289462015-07-03 08:01:20 +02003071/* dumps server state information into <buf> for all the servers found in <backend>
3072 * These information are all the parameters which may change during HAProxy runtime.
3073 * By default, we only export to the last known server state file format.
3074 * These information can be used at next startup to recover same level of server state.
3075 */
3076static void dump_servers_state(struct proxy *backend, struct chunk *buf)
3077{
3078 struct server *srv;
3079 char srv_addr[INET6_ADDRSTRLEN + 1];
3080 time_t srv_time_since_last_change;
3081 int bk_f_forced_id, srv_f_forced_id;
3082
3083 /* we don't want to report any state if the backend is not enabled on this process */
3084 if (backend->bind_proc && !(backend->bind_proc & (1UL << (relative_pid - 1))))
3085 return;
3086
3087 srv = backend->srv;
3088
3089 while (srv) {
3090 srv_addr[0] = '\0';
3091 srv_time_since_last_change = 0;
3092 bk_f_forced_id = 0;
3093 srv_f_forced_id = 0;
3094
3095 switch (srv->addr.ss_family) {
3096 case AF_INET:
3097 inet_ntop(srv->addr.ss_family, &((struct sockaddr_in *)&srv->addr)->sin_addr,
3098 srv_addr, INET_ADDRSTRLEN + 1);
3099 break;
3100 case AF_INET6:
3101 inet_ntop(srv->addr.ss_family, &((struct sockaddr_in6 *)&srv->addr)->sin6_addr,
3102 srv_addr, INET6_ADDRSTRLEN + 1);
3103 break;
3104 }
3105 srv_time_since_last_change = now.tv_sec - srv->last_change;
3106 bk_f_forced_id = backend->options & PR_O_FORCED_ID ? 1 : 0;
3107 srv_f_forced_id = srv->flags & SRV_F_FORCED_ID ? 1 : 0;
3108
3109 chunk_appendf(buf,
3110 "%d %s "
3111 "%d %s %s "
3112 "%d %d %d %d %ld "
3113 "%d %d %d %d %d "
3114 "%d %d"
3115 "\n",
3116 backend->uuid, backend->id,
3117 srv->puid, srv->id, srv_addr,
David Carlier081b3362015-11-18 06:10:22 +00003118 srv->state, srv->admin, srv->uweight, srv->iweight, (long int)srv_time_since_last_change,
Baptiste Assmann28289462015-07-03 08:01:20 +02003119 srv->check.status, srv->check.result, srv->check.health, srv->check.state, srv->agent.state,
3120 bk_f_forced_id, srv_f_forced_id);
3121
3122 srv = srv->next;
3123 }
3124}
3125
Baptiste Assmann9b6857e2015-09-18 14:49:12 +02003126/* Parses backend list and simply report backend names */
3127static int stats_dump_backend_to_buffer(struct stream_interface *si)
3128{
3129 extern struct proxy *proxy;
3130 struct proxy *curproxy;
3131
3132 chunk_reset(&trash);
3133 chunk_printf(&trash, "# name\n");
3134
3135 for (curproxy = proxy; curproxy != NULL; curproxy = curproxy->next) {
3136 /* looking for backends only */
3137 if (!(curproxy->cap & PR_CAP_BE))
3138 continue;
3139
3140 /* we don't want to list a backend which is bound to this process */
3141 if (curproxy->bind_proc && !(curproxy->bind_proc & (1UL << (relative_pid - 1))))
3142 continue;
3143
3144 chunk_appendf(&trash, "%s\n", curproxy->id);
3145 }
3146
3147 if (bi_putchk(si_ic(si), &trash) == -1) {
3148 si_applet_cant_put(si);
3149 return 0;
3150 }
3151
3152 return 1;
3153}
3154
Baptiste Assmann28289462015-07-03 08:01:20 +02003155/* Parses backend list or simply use backend name provided by the user to return
3156 * states of servers to stdout.
3157 */
3158static int stats_dump_servers_state_to_buffer(struct stream_interface *si)
3159{
3160 struct appctx *appctx = __objt_appctx(si->end);
3161 extern struct proxy *proxy;
3162 struct proxy *curproxy;
3163
3164 chunk_reset(&trash);
3165
3166 chunk_printf(&trash, "%d\n# %s\n", SRV_STATE_FILE_VERSION, SRV_STATE_FILE_FIELD_NAMES);
3167
3168 if (appctx->ctx.server_state.backend) {
3169 dump_servers_state(appctx->ctx.server_state.backend, &trash);
3170 }
3171 else {
3172 for (curproxy = proxy; curproxy != NULL; curproxy = curproxy->next) {
3173 /* servers are only in backends */
3174 if (!(curproxy->cap & PR_CAP_BE))
3175 continue;
3176
3177 dump_servers_state(curproxy, &trash);
3178 }
3179 }
3180
3181 if (bi_putchk(si_ic(si), &trash) == -1) {
3182 si_applet_cant_put(si);
3183 return 0;
3184 }
3185
3186 return 1;
3187}
3188
Willy Tarreau12833bb2014-01-28 16:49:56 +01003189/* This function dumps memory usage information onto the stream interface's
3190 * read buffer. It returns 0 as long as it does not complete, non-zero upon
3191 * completion. No state is used.
3192 */
3193static int stats_dump_pools_to_buffer(struct stream_interface *si)
3194{
3195 dump_pools_to_trash();
Willy Tarreaubc18da12015-03-13 14:00:47 +01003196 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02003197 si_applet_cant_put(si);
Willy Tarreau12833bb2014-01-28 16:49:56 +01003198 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01003199 }
Willy Tarreau82a86022016-01-04 19:04:18 +01003200 return 1;
3201}
3202
3203/* Dump all fields from <stats> into <out> using CSV format */
3204static int stats_dump_fields_csv(struct chunk *out, const struct field *stats)
3205{
3206 int field;
3207
3208 for (field = 0; field < ST_F_TOTAL_FIELDS; field++) {
3209 if (!stats_emit_raw_data_field(out, &stats[field]))
3210 return 0;
3211 if (!chunk_strcat(out, ","))
3212 return 0;
3213 }
3214 chunk_strcat(&trash, "\n");
Willy Tarreau12833bb2014-01-28 16:49:56 +01003215 return 1;
3216}
3217
Willy Tarreau1e62df92016-01-11 18:57:53 +01003218/* Dump all fields from <stats> into <out> using a typed "field:desc:type:value" format */
3219static int stats_dump_fields_typed(struct chunk *out, const struct field *stats)
3220{
3221 int field;
3222
3223 for (field = 0; field < ST_F_TOTAL_FIELDS; field++) {
3224 if (!stats[field].type)
3225 continue;
3226
3227 chunk_appendf(out, "%c.%u.%u.%d.%s.%u:",
3228 stats[ST_F_TYPE].u.u32 == STATS_TYPE_FE ? 'F' :
3229 stats[ST_F_TYPE].u.u32 == STATS_TYPE_BE ? 'B' :
3230 stats[ST_F_TYPE].u.u32 == STATS_TYPE_SO ? 'L' :
3231 stats[ST_F_TYPE].u.u32 == STATS_TYPE_SV ? 'S' :
3232 '?',
3233 stats[ST_F_IID].u.u32, stats[ST_F_SID].u.u32,
3234 field, stat_field_names[field], stats[ST_F_PID].u.u32);
3235
3236 if (!stats_emit_field_tags(out, &stats[field], ':'))
3237 return 0;
3238 if (!stats_emit_typed_data_field(out, &stats[field]))
3239 return 0;
3240 if (!chunk_strcat(out, "\n"))
3241 return 0;
3242 }
3243 return 1;
3244}
3245
Willy Tarreau60600742016-01-11 15:32:30 +01003246/* Dump all fields from <stats> into <out> using the HTML format. A column is
Willy Tarreau508a63f2016-01-11 15:28:40 +01003247 * reserved for the checkbox is ST_SHOWADMIN is set in <flags>. Some extra info
3248 * are provided if ST_SHLGNDS is present in <flags>.
Cyril Bonté70be45d2010-10-12 00:14:35 +02003249 */
Willy Tarreau60600742016-01-11 15:32:30 +01003250static int stats_dump_fields_html(struct chunk *out, const struct field *stats, unsigned int flags)
Cyril Bonté70be45d2010-10-12 00:14:35 +02003251{
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003252 struct chunk src;
3253
Willy Tarreaub5f66b82016-01-08 17:15:39 +01003254 if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_FE) {
Willy Tarreau60600742016-01-11 15:32:30 +01003255 chunk_appendf(out,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003256 /* name, queue */
3257 "<tr class=\"frontend\">");
Cyril Bonté70be45d2010-10-12 00:14:35 +02003258
Willy Tarreau508a63f2016-01-11 15:28:40 +01003259 if (flags & ST_SHOWADMIN) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003260 /* Column sub-heading for Enable or Disable server */
Willy Tarreau60600742016-01-11 15:32:30 +01003261 chunk_appendf(out, "<td></td>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003262 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02003263
Willy Tarreau60600742016-01-11 15:32:30 +01003264 chunk_appendf(out,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003265 "<td class=ac>"
3266 "<a name=\"%s/Frontend\"></a>"
3267 "<a class=lfsb href=\"#%s/Frontend\">Frontend</a></td>"
3268 "<td colspan=3></td>"
3269 "",
Willy Tarreaud72c9172016-01-06 15:26:40 +01003270 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_PXNAME));
Cyril Bonté70be45d2010-10-12 00:14:35 +02003271
Willy Tarreau60600742016-01-11 15:32:30 +01003272 chunk_appendf(out,
Willy Tarreau466c9b52012-12-23 02:25:03 +01003273 /* sessions rate : current */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003274 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01003275 "<tr><th>Current connection rate:</th><td>%s/s</td></tr>"
3276 "<tr><th>Current session rate:</th><td>%s/s</td></tr>"
3277 "",
Willy Tarreaud72c9172016-01-06 15:26:40 +01003278 U2H(stats[ST_F_RATE].u.u32),
Willy Tarreauc73810f2016-01-11 13:52:04 +01003279 U2H(stats[ST_F_CONN_RATE].u.u32),
Willy Tarreaud72c9172016-01-06 15:26:40 +01003280 U2H(stats[ST_F_RATE].u.u32));
Willy Tarreau466c9b52012-12-23 02:25:03 +01003281
Willy Tarreauf8211df2016-01-11 14:09:38 +01003282 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
Willy Tarreau60600742016-01-11 15:32:30 +01003283 chunk_appendf(out,
Willy Tarreau466c9b52012-12-23 02:25:03 +01003284 "<tr><th>Current request rate:</th><td>%s/s</td></tr>",
Willy Tarreaud72c9172016-01-06 15:26:40 +01003285 U2H(stats[ST_F_REQ_RATE].u.u32));
Willy Tarreau466c9b52012-12-23 02:25:03 +01003286
Willy Tarreau60600742016-01-11 15:32:30 +01003287 chunk_appendf(out,
Willy Tarreau466c9b52012-12-23 02:25:03 +01003288 "</table></div></u></td>"
3289 /* sessions rate : max */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003290 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01003291 "<tr><th>Max connection rate:</th><td>%s/s</td></tr>"
3292 "<tr><th>Max session rate:</th><td>%s/s</td></tr>"
3293 "",
Willy Tarreaud72c9172016-01-06 15:26:40 +01003294 U2H(stats[ST_F_RATE_MAX].u.u32),
Willy Tarreauc73810f2016-01-11 13:52:04 +01003295 U2H(stats[ST_F_CONN_RATE_MAX].u.u32),
Willy Tarreaud72c9172016-01-06 15:26:40 +01003296 U2H(stats[ST_F_RATE_MAX].u.u32));
Willy Tarreau466c9b52012-12-23 02:25:03 +01003297
Willy Tarreauf8211df2016-01-11 14:09:38 +01003298 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
Willy Tarreau60600742016-01-11 15:32:30 +01003299 chunk_appendf(out,
Willy Tarreau466c9b52012-12-23 02:25:03 +01003300 "<tr><th>Max request rate:</th><td>%s/s</td></tr>",
Willy Tarreaud72c9172016-01-06 15:26:40 +01003301 U2H(stats[ST_F_REQ_RATE_MAX].u.u32));
Willy Tarreau466c9b52012-12-23 02:25:03 +01003302
Willy Tarreau60600742016-01-11 15:32:30 +01003303 chunk_appendf(out,
Willy Tarreau466c9b52012-12-23 02:25:03 +01003304 "</table></div></u></td>"
3305 /* sessions rate : limit */
3306 "<td>%s</td>",
Willy Tarreaud72c9172016-01-06 15:26:40 +01003307 LIM2A(stats[ST_F_RATE_LIM].u.u32, "-"));
Cyril Bonté70be45d2010-10-12 00:14:35 +02003308
Willy Tarreau60600742016-01-11 15:32:30 +01003309 chunk_appendf(out,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003310 /* sessions: current, max, limit, total */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003311 "<td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003312 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01003313 "<tr><th>Cum. connections:</th><td>%s</td></tr>"
3314 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003315 "",
Willy Tarreauc73810f2016-01-11 13:52:04 +01003316 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 +01003317 U2H(stats[ST_F_STOT].u.u64),
Willy Tarreauc73810f2016-01-11 13:52:04 +01003318 U2H(stats[ST_F_CONN_TOT].u.u64),
Willy Tarreaud72c9172016-01-06 15:26:40 +01003319 U2H(stats[ST_F_STOT].u.u64));
Cyril Bonté70be45d2010-10-12 00:14:35 +02003320
Willy Tarreau466c9b52012-12-23 02:25:03 +01003321 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
Willy Tarreauf8211df2016-01-11 14:09:38 +01003322 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
Willy Tarreau60600742016-01-11 15:32:30 +01003323 chunk_appendf(out,
Willy Tarreau466c9b52012-12-23 02:25:03 +01003324 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
3325 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
3326 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
3327 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
3328 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
3329 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
3330 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
3331 "<tr><th>- other responses:</th><td>%s</td></tr>"
3332 "<tr><th>Intercepted requests:</th><td>%s</td></tr>"
3333 "",
Willy Tarreaud72c9172016-01-06 15:26:40 +01003334 U2H(stats[ST_F_REQ_TOT].u.u64),
3335 U2H(stats[ST_F_HRSP_1XX].u.u64),
3336 U2H(stats[ST_F_HRSP_2XX].u.u64),
3337 U2H(stats[ST_F_COMP_RSP].u.u64),
3338 stats[ST_F_HRSP_2XX].u.u64 ?
3339 (int)(100 * stats[ST_F_COMP_RSP].u.u64 / stats[ST_F_HRSP_2XX].u.u64) : 0,
3340 U2H(stats[ST_F_HRSP_3XX].u.u64),
3341 U2H(stats[ST_F_HRSP_4XX].u.u64),
3342 U2H(stats[ST_F_HRSP_5XX].u.u64),
3343 U2H(stats[ST_F_HRSP_OTHER].u.u64),
Willy Tarreau5b9bdff2016-01-11 14:40:47 +01003344 U2H(stats[ST_F_INTERCEPTED].u.u64));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003345 }
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02003346
Willy Tarreau60600742016-01-11 15:32:30 +01003347 chunk_appendf(out,
Willy Tarreau466c9b52012-12-23 02:25:03 +01003348 "</table></div></u></td>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05003349 /* sessions: lbtot, lastsess */
3350 "<td></td><td></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003351 /* bytes : in */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003352 "<td>%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003353 "",
Willy Tarreaud72c9172016-01-06 15:26:40 +01003354 U2H(stats[ST_F_BIN].u.u64));
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02003355
Willy Tarreau60600742016-01-11 15:32:30 +01003356 chunk_appendf(out,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003357 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
Willy Tarreau4e5d58e2015-07-04 14:35:15 +02003358 "<td>%s%s<div class=tips><table class=det>"
3359 "<tr><th>Response bytes in:</th><td>%s</td></tr>"
3360 "<tr><th>Compression in:</th><td>%s</td></tr>"
3361 "<tr><th>Compression out:</th><td>%s</td><td>(%d%%)</td></tr>"
3362 "<tr><th>Compression bypass:</th><td>%s</td></tr>"
3363 "<tr><th>Total bytes saved:</th><td>%s</td><td>(%d%%)</td></tr>"
3364 "</table></div>%s</td>",
Willy Tarreaud72c9172016-01-06 15:26:40 +01003365 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "<u>":"",
3366 U2H(stats[ST_F_BOUT].u.u64),
3367 U2H(stats[ST_F_BOUT].u.u64),
3368 U2H(stats[ST_F_COMP_IN].u.u64),
3369 U2H(stats[ST_F_COMP_OUT].u.u64),
3370 stats[ST_F_COMP_IN].u.u64 ? (int)(stats[ST_F_COMP_OUT].u.u64 * 100 / stats[ST_F_COMP_IN].u.u64) : 0,
3371 U2H(stats[ST_F_COMP_BYP].u.u64),
3372 U2H(stats[ST_F_COMP_IN].u.u64 - stats[ST_F_COMP_OUT].u.u64),
3373 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,
3374 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "</u>":"");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02003375
Willy Tarreau60600742016-01-11 15:32:30 +01003376 chunk_appendf(out,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003377 /* denied: req, resp */
3378 "<td>%s</td><td>%s</td>"
3379 /* errors : request, connect, response */
3380 "<td>%s</td><td></td><td></td>"
3381 /* warnings: retries, redispatches */
3382 "<td></td><td></td>"
3383 /* server status : reflect frontend status */
3384 "<td class=ac>%s</td>"
3385 /* rest of server: nothing */
3386 "<td class=ac colspan=8></td></tr>"
3387 "",
Willy Tarreaud72c9172016-01-06 15:26:40 +01003388 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
3389 U2H(stats[ST_F_EREQ].u.u64),
3390 field_str(stats, ST_F_STATUS));
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02003391 }
Willy Tarreaufa9512f2016-01-08 17:20:51 +01003392 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_SO) {
Willy Tarreau60600742016-01-11 15:32:30 +01003393 chunk_appendf(out, "<tr class=socket>");
Willy Tarreau508a63f2016-01-11 15:28:40 +01003394 if (flags & ST_SHOWADMIN) {
Willy Tarreaufa9512f2016-01-08 17:20:51 +01003395 /* Column sub-heading for Enable or Disable server */
Willy Tarreau60600742016-01-11 15:32:30 +01003396 chunk_appendf(out, "<td></td>");
Willy Tarreaufa9512f2016-01-08 17:20:51 +01003397 }
3398
Willy Tarreau60600742016-01-11 15:32:30 +01003399 chunk_appendf(out,
Willy Tarreaufa9512f2016-01-08 17:20:51 +01003400 /* frontend name, listener name */
3401 "<td class=ac><a name=\"%s/+%s\"></a>%s"
3402 "<a class=lfsb href=\"#%s/+%s\">%s</a>"
3403 "",
3404 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME),
3405 (flags & ST_SHLGNDS)?"<u>":"",
3406 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME), field_str(stats, ST_F_SVNAME));
3407
3408 if (flags & ST_SHLGNDS) {
Willy Tarreau60600742016-01-11 15:32:30 +01003409 chunk_appendf(out, "<div class=tips>");
Willy Tarreaufa9512f2016-01-08 17:20:51 +01003410
3411 if (isdigit(*field_str(stats, ST_F_ADDR)))
Willy Tarreau60600742016-01-11 15:32:30 +01003412 chunk_appendf(out, "IPv4: %s, ", field_str(stats, ST_F_ADDR));
Willy Tarreaufa9512f2016-01-08 17:20:51 +01003413 else if (*field_str(stats, ST_F_ADDR) == '[')
Willy Tarreau60600742016-01-11 15:32:30 +01003414 chunk_appendf(out, "IPv6: %s, ", field_str(stats, ST_F_ADDR));
Willy Tarreaufa9512f2016-01-08 17:20:51 +01003415 else if (*field_str(stats, ST_F_ADDR))
Willy Tarreau60600742016-01-11 15:32:30 +01003416 chunk_appendf(out, "%s, ", field_str(stats, ST_F_ADDR));
Willy Tarreaufa9512f2016-01-08 17:20:51 +01003417
3418 /* id */
Willy Tarreau60600742016-01-11 15:32:30 +01003419 chunk_appendf(out, "id: %d</div>", stats[ST_F_SID].u.u32);
Willy Tarreaufa9512f2016-01-08 17:20:51 +01003420 }
3421
Willy Tarreau60600742016-01-11 15:32:30 +01003422 chunk_appendf(out,
Willy Tarreaufa9512f2016-01-08 17:20:51 +01003423 /* queue */
3424 "%s</td><td colspan=3></td>"
3425 /* sessions rate: current, max, limit */
3426 "<td colspan=3>&nbsp;</td>"
3427 /* sessions: current, max, limit, total, lbtot, lastsess */
3428 "<td>%s</td><td>%s</td><td>%s</td>"
3429 "<td>%s</td><td>&nbsp;</td><td>&nbsp;</td>"
3430 /* bytes: in, out */
3431 "<td>%s</td><td>%s</td>"
3432 "",
3433 (flags & ST_SHLGNDS)?"</u>":"",
3434 U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SMAX].u.u32), U2H(stats[ST_F_SLIM].u.u32),
3435 U2H(stats[ST_F_STOT].u.u64), U2H(stats[ST_F_BIN].u.u64), U2H(stats[ST_F_BOUT].u.u64));
3436
Willy Tarreau60600742016-01-11 15:32:30 +01003437 chunk_appendf(out,
Willy Tarreaufa9512f2016-01-08 17:20:51 +01003438 /* denied: req, resp */
3439 "<td>%s</td><td>%s</td>"
3440 /* errors: request, connect, response */
3441 "<td>%s</td><td></td><td></td>"
3442 /* warnings: retries, redispatches */
3443 "<td></td><td></td>"
3444 /* server status: reflect listener status */
3445 "<td class=ac>%s</td>"
3446 /* rest of server: nothing */
3447 "<td class=ac colspan=8></td></tr>"
3448 "",
3449 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
3450 U2H(stats[ST_F_EREQ].u.u64),
3451 field_str(stats, ST_F_STATUS));
3452 }
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003453 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_SV) {
3454 const char *style;
Willy Tarreaub5f66b82016-01-08 17:15:39 +01003455
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003456 /* determine the style to use depending on the server's state,
3457 * its health and weight. There isn't a 1-to-1 mapping between
3458 * state and styles for the cases where the server is (still)
3459 * up. The reason is that we don't want to report nolb and
3460 * drain with the same color.
3461 */
Willy Tarreaub5f66b82016-01-08 17:15:39 +01003462
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003463 if (strcmp(field_str(stats, ST_F_STATUS), "DOWN") == 0 ||
3464 strcmp(field_str(stats, ST_F_STATUS), "DOWN (agent)") == 0) {
3465 style = "down";
3466 }
3467 else if (strcmp(field_str(stats, ST_F_STATUS), "DOWN ") == 0) {
3468 style = "going_up";
3469 }
3470 else if (strcmp(field_str(stats, ST_F_STATUS), "NOLB ") == 0) {
3471 style = "going_down";
3472 }
3473 else if (strcmp(field_str(stats, ST_F_STATUS), "NOLB") == 0) {
3474 style = "nolb";
3475 }
3476 else if (strcmp(field_str(stats, ST_F_STATUS), "no check") == 0) {
3477 style = "no_check";
3478 }
3479 else if (!stats[ST_F_CHKFAIL].type ||
3480 stats[ST_F_CHECK_HEALTH].u.u32 == stats[ST_F_CHECK_RISE].u.u32 + stats[ST_F_CHECK_FALL].u.u32 - 1) {
3481 /* no check or max health = UP */
3482 if (stats[ST_F_WEIGHT].u.u32)
3483 style = "up";
3484 else
3485 style = "draining";
3486 }
3487 else {
3488 style = "going_down";
3489 }
Willy Tarreaub5f66b82016-01-08 17:15:39 +01003490
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003491 if (memcmp(field_str(stats, ST_F_STATUS), "MAINT", 5) == 0)
Willy Tarreau60600742016-01-11 15:32:30 +01003492 chunk_appendf(out, "<tr class=\"maintain\">");
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003493 else
Willy Tarreau60600742016-01-11 15:32:30 +01003494 chunk_appendf(out,
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003495 "<tr class=\"%s_%s\">",
3496 (stats[ST_F_BCK].u.u32) ? "backup" : "active", style);
Willy Tarreaub5f66b82016-01-08 17:15:39 +01003497
Willy Tarreaub5f66b82016-01-08 17:15:39 +01003498
Willy Tarreau508a63f2016-01-11 15:28:40 +01003499 if (flags & ST_SHOWADMIN)
Willy Tarreau60600742016-01-11 15:32:30 +01003500 chunk_appendf(out,
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003501 "<td><input type=\"checkbox\" name=\"s\" value=\"%s\"></td>",
3502 field_str(stats, ST_F_SVNAME));
Willy Tarreaub5f66b82016-01-08 17:15:39 +01003503
Willy Tarreau60600742016-01-11 15:32:30 +01003504 chunk_appendf(out,
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003505 "<td class=ac><a name=\"%s/%s\"></a>%s"
3506 "<a class=lfsb href=\"#%s/%s\">%s</a>"
3507 "",
3508 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME),
3509 (flags & ST_SHLGNDS) ? "<u>" : "",
3510 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 +01003511
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003512 if (flags & ST_SHLGNDS) {
Willy Tarreau60600742016-01-11 15:32:30 +01003513 chunk_appendf(out, "<div class=tips>");
Willy Tarreaub5f66b82016-01-08 17:15:39 +01003514
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003515 if (isdigit(*field_str(stats, ST_F_ADDR)))
Willy Tarreau60600742016-01-11 15:32:30 +01003516 chunk_appendf(out, "IPv4: %s, ", field_str(stats, ST_F_ADDR));
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003517 else if (*field_str(stats, ST_F_ADDR) == '[')
Willy Tarreau60600742016-01-11 15:32:30 +01003518 chunk_appendf(out, "IPv6: %s, ", field_str(stats, ST_F_ADDR));
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003519 else if (*field_str(stats, ST_F_ADDR))
Willy Tarreau60600742016-01-11 15:32:30 +01003520 chunk_appendf(out, "%s, ", field_str(stats, ST_F_ADDR));
Willy Tarreaub5f66b82016-01-08 17:15:39 +01003521
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003522 /* id */
Willy Tarreau60600742016-01-11 15:32:30 +01003523 chunk_appendf(out, "id: %d", stats[ST_F_SID].u.u32);
Willy Tarreaub5f66b82016-01-08 17:15:39 +01003524
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003525 /* cookie */
3526 if (stats[ST_F_COOKIE].type) {
Willy Tarreau60600742016-01-11 15:32:30 +01003527 chunk_appendf(out, ", cookie: '");
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003528 chunk_initstr(&src, field_str(stats, ST_F_COOKIE));
Willy Tarreau60600742016-01-11 15:32:30 +01003529 chunk_htmlencode(out, &src);
3530 chunk_appendf(out, "'");
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003531 }
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02003532
Willy Tarreau60600742016-01-11 15:32:30 +01003533 chunk_appendf(out, "</div>");
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003534 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003535
Willy Tarreau60600742016-01-11 15:32:30 +01003536 chunk_appendf(out,
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003537 /* queue : current, max, limit */
3538 "%s</td><td>%s</td><td>%s</td><td>%s</td>"
3539 /* sessions rate : current, max, limit */
3540 "<td>%s</td><td>%s</td><td></td>"
3541 "",
3542 (flags & ST_SHLGNDS) ? "</u>" : "",
3543 U2H(stats[ST_F_QCUR].u.u32), U2H(stats[ST_F_QMAX].u.u32), LIM2A(stats[ST_F_QLIMIT].u.u32, "-"),
3544 U2H(stats[ST_F_RATE].u.u32), U2H(stats[ST_F_RATE_MAX].u.u32));
Willy Tarreau4607fad2016-01-06 15:41:29 +01003545
Willy Tarreau60600742016-01-11 15:32:30 +01003546 chunk_appendf(out,
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003547 /* sessions: current, max, limit, total */
3548 "<td>%s</td><td>%s</td><td>%s</td>"
3549 "<td><u>%s<div class=tips><table class=det>"
3550 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
3551 "",
3552 U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SMAX].u.u32), LIM2A(stats[ST_F_SLIM].u.u32, "-"),
3553 U2H(stats[ST_F_STOT].u.u64),
3554 U2H(stats[ST_F_STOT].u.u64));
Willy Tarreau4607fad2016-01-06 15:41:29 +01003555
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003556 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
Willy Tarreauf8211df2016-01-11 14:09:38 +01003557 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003558 unsigned long long tot;
Willy Tarreaua6f5a732016-01-08 16:59:56 +01003559
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003560 tot = stats[ST_F_HRSP_OTHER].u.u64;
3561 tot += stats[ST_F_HRSP_1XX].u.u64;
3562 tot += stats[ST_F_HRSP_2XX].u.u64;
3563 tot += stats[ST_F_HRSP_3XX].u.u64;
3564 tot += stats[ST_F_HRSP_4XX].u.u64;
3565 tot += stats[ST_F_HRSP_5XX].u.u64;
3566
Willy Tarreau60600742016-01-11 15:32:30 +01003567 chunk_appendf(out,
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003568 "<tr><th>Cum. HTTP responses:</th><td>%s</td></tr>"
3569 "<tr><th>- HTTP 1xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
3570 "<tr><th>- HTTP 2xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
3571 "<tr><th>- HTTP 3xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
3572 "<tr><th>- HTTP 4xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
3573 "<tr><th>- HTTP 5xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
3574 "<tr><th>- other responses:</th><td>%s</td><td>(%d%%)</td></tr>"
3575 "",
3576 U2H(tot),
3577 U2H(stats[ST_F_HRSP_1XX].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_1XX].u.u64 / tot) : 0,
3578 U2H(stats[ST_F_HRSP_2XX].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_2XX].u.u64 / tot) : 0,
3579 U2H(stats[ST_F_HRSP_3XX].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_3XX].u.u64 / tot) : 0,
3580 U2H(stats[ST_F_HRSP_4XX].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_4XX].u.u64 / tot) : 0,
3581 U2H(stats[ST_F_HRSP_5XX].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_5XX].u.u64 / tot) : 0,
3582 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 +01003583 }
Willy Tarreaua6f5a732016-01-08 16:59:56 +01003584
Willy Tarreau60600742016-01-11 15:32:30 +01003585 chunk_appendf(out, "<tr><th colspan=3>Avg over last 1024 success. conn.</th></tr>");
3586 chunk_appendf(out, "<tr><th>- Queue time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_QTIME].u.u32));
3587 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 +01003588 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
Willy Tarreau60600742016-01-11 15:32:30 +01003589 chunk_appendf(out, "<tr><th>- Response time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_RTIME].u.u32));
3590 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 +02003591
Willy Tarreau60600742016-01-11 15:32:30 +01003592 chunk_appendf(out,
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003593 "</table></div></u></td>"
3594 /* sessions: lbtot, last */
3595 "<td>%s</td><td>%s</td>",
3596 U2H(stats[ST_F_LBTOT].u.u64),
3597 human_time(stats[ST_F_LASTSESS].u.s32, 1));
Willy Tarreau91861262007-10-17 17:06:05 +02003598
Willy Tarreau60600742016-01-11 15:32:30 +01003599 chunk_appendf(out,
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003600 /* bytes : in, out */
3601 "<td>%s</td><td>%s</td>"
3602 /* denied: req, resp */
3603 "<td></td><td>%s</td>"
3604 /* errors : request, connect */
3605 "<td></td><td>%s</td>"
3606 /* errors : response */
3607 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
3608 /* warnings: retries, redispatches */
3609 "<td>%lld</td><td>%lld</td>"
3610 "",
3611 U2H(stats[ST_F_BIN].u.u64), U2H(stats[ST_F_BOUT].u.u64),
3612 U2H(stats[ST_F_DRESP].u.u64),
3613 U2H(stats[ST_F_ECON].u.u64),
3614 U2H(stats[ST_F_ERESP].u.u64),
3615 (long long)stats[ST_F_CLI_ABRT].u.u64,
3616 (long long)stats[ST_F_SRV_ABRT].u.u64,
3617 (long long)stats[ST_F_WRETR].u.u64,
3618 (long long)stats[ST_F_WREDIS].u.u64);
3619
3620 /* status, last change */
Willy Tarreau60600742016-01-11 15:32:30 +01003621 chunk_appendf(out, "<td class=ac>");
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003622
3623 /* FIXME!!!!
3624 * LASTCHG should contain the last change for *this* server and must be computed
3625 * properly above, as was done below, ie: this server if maint, otherwise ref server
3626 * if tracking. Note that ref is either local or remote depending on tracking.
3627 */
3628
3629
3630 if (memcmp(field_str(stats, ST_F_STATUS), "MAINT", 5) == 0) {
Willy Tarreau60600742016-01-11 15:32:30 +01003631 chunk_appendf(out, "%s MAINT", human_time(stats[ST_F_LASTCHG].u.u32, 1));
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003632 }
3633 else if (memcmp(field_str(stats, ST_F_STATUS), "no check", 5) == 0) {
Willy Tarreau60600742016-01-11 15:32:30 +01003634 chunk_strcat(out, "<i>no check</i>");
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003635 }
3636 else {
Willy Tarreau60600742016-01-11 15:32:30 +01003637 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 +01003638 if (memcmp(field_str(stats, ST_F_STATUS), "DOWN", 4) == 0) {
3639 if (stats[ST_F_CHECK_HEALTH].u.u32)
Willy Tarreau60600742016-01-11 15:32:30 +01003640 chunk_strcat(out, " &uarr;");
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003641 }
3642 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 +01003643 chunk_strcat(out, " &darr;");
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003644 }
3645
3646 if (memcmp(field_str(stats, ST_F_STATUS), "DOWN", 4) == 0 &&
3647 stats[ST_F_AGENT_STATUS].type && !stats[ST_F_AGENT_HEALTH].u.u32) {
Willy Tarreau60600742016-01-11 15:32:30 +01003648 chunk_appendf(out,
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003649 "</td><td class=ac><u> %s",
3650 field_str(stats, ST_F_AGENT_STATUS));
3651
3652 if (stats[ST_F_AGENT_CODE].type)
Willy Tarreau60600742016-01-11 15:32:30 +01003653 chunk_appendf(out, "/%d", stats[ST_F_AGENT_CODE].u.u32);
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003654
3655 if (stats[ST_F_AGENT_DURATION].type && stats[ST_F_AGENT_DURATION].u.u64 >= 0)
Willy Tarreau60600742016-01-11 15:32:30 +01003656 chunk_appendf(out, " in %lums", (long)stats[ST_F_AGENT_DURATION].u.u64);
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003657
Willy Tarreau60600742016-01-11 15:32:30 +01003658 chunk_appendf(out, "<div class=tips>%s", field_str(stats, ST_F_AGENT_DESC));
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003659
3660 if (*field_str(stats, ST_F_LAST_AGT)) {
Willy Tarreau60600742016-01-11 15:32:30 +01003661 chunk_appendf(out, ": ");
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003662 chunk_initstr(&src, field_str(stats, ST_F_LAST_AGT));
Willy Tarreau60600742016-01-11 15:32:30 +01003663 chunk_htmlencode(out, &src);
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003664 }
Willy Tarreau60600742016-01-11 15:32:30 +01003665 chunk_appendf(out, "</div></u>");
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003666 }
3667 else if (stats[ST_F_CHECK_STATUS].type) {
Willy Tarreau60600742016-01-11 15:32:30 +01003668 chunk_appendf(out,
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003669 "</td><td class=ac><u> %s",
3670 field_str(stats, ST_F_CHECK_STATUS));
3671
3672 if (stats[ST_F_CHECK_CODE].type)
Willy Tarreau60600742016-01-11 15:32:30 +01003673 chunk_appendf(out, "/%d", stats[ST_F_CHECK_CODE].u.u32);
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003674
3675 if (stats[ST_F_CHECK_DURATION].type && stats[ST_F_CHECK_DURATION].u.u64 >= 0)
Willy Tarreau60600742016-01-11 15:32:30 +01003676 chunk_appendf(out, " in %lums", (long)stats[ST_F_CHECK_DURATION].u.u64);
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003677
Willy Tarreau60600742016-01-11 15:32:30 +01003678 chunk_appendf(out, "<div class=tips>%s", field_str(stats, ST_F_CHECK_DESC));
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003679
3680 if (*field_str(stats, ST_F_LAST_CHK)) {
Willy Tarreau60600742016-01-11 15:32:30 +01003681 chunk_appendf(out, ": ");
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003682 chunk_initstr(&src, field_str(stats, ST_F_LAST_CHK));
Willy Tarreau60600742016-01-11 15:32:30 +01003683 chunk_htmlencode(out, &src);
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003684 }
Willy Tarreau60600742016-01-11 15:32:30 +01003685 chunk_appendf(out, "</div></u>");
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003686 }
3687 else
Willy Tarreau60600742016-01-11 15:32:30 +01003688 chunk_appendf(out, "</td><td>");
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003689
Willy Tarreau60600742016-01-11 15:32:30 +01003690 chunk_appendf(out,
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003691 /* weight */
3692 "</td><td class=ac>%d</td>"
3693 /* act, bck */
3694 "<td class=ac>%s</td><td class=ac>%s</td>"
3695 "",
3696 stats[ST_F_WEIGHT].u.u32,
3697 stats[ST_F_BCK].u.u32 ? "-" : "Y",
3698 stats[ST_F_BCK].u.u32 ? "Y" : "-");
3699
3700 /* check failures: unique, fatal, down time */
3701 if (stats[ST_F_CHKFAIL].type) {
Willy Tarreau60600742016-01-11 15:32:30 +01003702 chunk_appendf(out, "<td><u>%lld", (long long)stats[ST_F_CHKFAIL].u.u64);
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003703
3704 if (stats[ST_F_HANAFAIL].type)
Willy Tarreau60600742016-01-11 15:32:30 +01003705 chunk_appendf(out, "/%lld", (long long)stats[ST_F_HANAFAIL].u.u64);
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003706
Willy Tarreau60600742016-01-11 15:32:30 +01003707 chunk_appendf(out,
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003708 "<div class=tips>Failed Health Checks%s</div></u></td>"
3709 "<td>%lld</td><td>%s</td>"
3710 "",
3711 stats[ST_F_HANAFAIL].type ? "/Health Analyses" : "",
3712 (long long)stats[ST_F_CHKDOWN].u.u64, human_time(stats[ST_F_DOWNTIME].u.u32, 1));
3713 }
3714 else if (strcmp(field_str(stats, ST_F_STATUS), "MAINT") != 0 && field_format(stats, ST_F_TRACKED) == FF_STR) {
3715 /* tracking a server (hence inherited maint would appear as "MAINT (via...)" */
Willy Tarreau60600742016-01-11 15:32:30 +01003716 chunk_appendf(out,
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003717 "<td class=ac colspan=3><a class=lfsb href=\"#%s\">via %s</a></td>",
3718 field_str(stats, ST_F_TRACKED), field_str(stats, ST_F_TRACKED));
3719 }
3720 else
Willy Tarreau60600742016-01-11 15:32:30 +01003721 chunk_appendf(out, "<td colspan=3></td>");
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003722
3723 /* throttle */
3724 if (stats[ST_F_THROTTLE].type)
Willy Tarreau60600742016-01-11 15:32:30 +01003725 chunk_appendf(out, "<td class=ac>%d %%</td></tr>\n", stats[ST_F_THROTTLE].u.u32);
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003726 else
Willy Tarreau60600742016-01-11 15:32:30 +01003727 chunk_appendf(out, "<td class=ac>-</td></tr>\n");
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003728 }
Willy Tarreaubbf84502016-01-08 17:25:50 +01003729 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_BE) {
Willy Tarreau60600742016-01-11 15:32:30 +01003730 chunk_appendf(out, "<tr class=\"backend\">");
Willy Tarreau508a63f2016-01-11 15:28:40 +01003731 if (flags & ST_SHOWADMIN) {
Willy Tarreaubbf84502016-01-08 17:25:50 +01003732 /* Column sub-heading for Enable or Disable server */
Willy Tarreau60600742016-01-11 15:32:30 +01003733 chunk_appendf(out, "<td></td>");
Willy Tarreaubbf84502016-01-08 17:25:50 +01003734 }
Willy Tarreau60600742016-01-11 15:32:30 +01003735 chunk_appendf(out,
Willy Tarreaubbf84502016-01-08 17:25:50 +01003736 "<td class=ac>"
3737 /* name */
3738 "%s<a name=\"%s/Backend\"></a>"
3739 "<a class=lfsb href=\"#%s/Backend\">Backend</a>"
3740 "",
3741 (flags & ST_SHLGNDS)?"<u>":"",
3742 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_PXNAME));
3743
3744 if (flags & ST_SHLGNDS) {
3745 /* balancing */
Willy Tarreau60600742016-01-11 15:32:30 +01003746 chunk_appendf(out, "<div class=tips>balancing: %s",
Willy Tarreauf1516d92016-01-11 14:48:36 +01003747 field_str(stats, ST_F_ALGO));
Willy Tarreaubbf84502016-01-08 17:25:50 +01003748
3749 /* cookie */
3750 if (stats[ST_F_COOKIE].type) {
Willy Tarreau60600742016-01-11 15:32:30 +01003751 chunk_appendf(out, ", cookie: '");
Willy Tarreaubbf84502016-01-08 17:25:50 +01003752 chunk_initstr(&src, field_str(stats, ST_F_COOKIE));
Willy Tarreau60600742016-01-11 15:32:30 +01003753 chunk_htmlencode(out, &src);
3754 chunk_appendf(out, "'");
Willy Tarreaubbf84502016-01-08 17:25:50 +01003755 }
Willy Tarreau60600742016-01-11 15:32:30 +01003756 chunk_appendf(out, "</div>");
Willy Tarreaubbf84502016-01-08 17:25:50 +01003757 }
3758
Willy Tarreau60600742016-01-11 15:32:30 +01003759 chunk_appendf(out,
Willy Tarreaubbf84502016-01-08 17:25:50 +01003760 "%s</td>"
3761 /* queue : current, max */
3762 "<td>%s</td><td>%s</td><td></td>"
3763 /* sessions rate : current, max, limit */
3764 "<td>%s</td><td>%s</td><td></td>"
3765 "",
3766 (flags & ST_SHLGNDS)?"</u>":"",
3767 U2H(stats[ST_F_QCUR].u.u32), U2H(stats[ST_F_QMAX].u.u32),
3768 U2H(stats[ST_F_RATE].u.u32), U2H(stats[ST_F_RATE_MAX].u.u32));
3769
Willy Tarreau60600742016-01-11 15:32:30 +01003770 chunk_appendf(out,
Willy Tarreaubbf84502016-01-08 17:25:50 +01003771 /* sessions: current, max, limit, total */
3772 "<td>%s</td><td>%s</td><td>%s</td>"
3773 "<td><u>%s<div class=tips><table class=det>"
3774 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
3775 "",
3776 U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SLIM].u.u32),
3777 U2H(stats[ST_F_STOT].u.u64),
3778 U2H(stats[ST_F_STOT].u.u64));
3779
3780 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
Willy Tarreauf8211df2016-01-11 14:09:38 +01003781 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
Willy Tarreau60600742016-01-11 15:32:30 +01003782 chunk_appendf(out,
Willy Tarreaubbf84502016-01-08 17:25:50 +01003783 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
3784 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
3785 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
3786 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
3787 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
3788 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
3789 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
3790 "<tr><th>- other responses:</th><td>%s</td></tr>"
3791 "<tr><th>Intercepted requests:</th><td>%s</td></tr>"
3792 "<tr><th colspan=3>Avg over last 1024 success. conn.</th></tr>"
3793 "",
3794 U2H(stats[ST_F_REQ_TOT].u.u64),
3795 U2H(stats[ST_F_HRSP_1XX].u.u64),
3796 U2H(stats[ST_F_HRSP_2XX].u.u64),
3797 U2H(stats[ST_F_COMP_RSP].u.u64),
3798 stats[ST_F_HRSP_2XX].u.u64 ?
3799 (int)(100 * stats[ST_F_COMP_RSP].u.u64 / stats[ST_F_HRSP_2XX].u.u64) : 0,
3800 U2H(stats[ST_F_HRSP_3XX].u.u64),
3801 U2H(stats[ST_F_HRSP_4XX].u.u64),
3802 U2H(stats[ST_F_HRSP_5XX].u.u64),
3803 U2H(stats[ST_F_HRSP_OTHER].u.u64),
Willy Tarreau5b9bdff2016-01-11 14:40:47 +01003804 U2H(stats[ST_F_INTERCEPTED].u.u64));
Willy Tarreaubbf84502016-01-08 17:25:50 +01003805 }
3806
Willy Tarreau60600742016-01-11 15:32:30 +01003807 chunk_appendf(out, "<tr><th>- Queue time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_QTIME].u.u32));
3808 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 +01003809 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
Willy Tarreau60600742016-01-11 15:32:30 +01003810 chunk_appendf(out, "<tr><th>- Response time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_RTIME].u.u32));
3811 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 +01003812
Willy Tarreau60600742016-01-11 15:32:30 +01003813 chunk_appendf(out,
Willy Tarreaubbf84502016-01-08 17:25:50 +01003814 "</table></div></u></td>"
3815 /* sessions: lbtot, last */
3816 "<td>%s</td><td>%s</td>"
3817 /* bytes: in */
3818 "<td>%s</td>"
3819 "",
3820 U2H(stats[ST_F_LBTOT].u.u64),
3821 human_time(stats[ST_F_LASTSESS].u.s32, 1),
3822 U2H(stats[ST_F_BIN].u.u64));
3823
Willy Tarreau60600742016-01-11 15:32:30 +01003824 chunk_appendf(out,
Willy Tarreaubbf84502016-01-08 17:25:50 +01003825 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
3826 "<td>%s%s<div class=tips><table class=det>"
3827 "<tr><th>Response bytes in:</th><td>%s</td></tr>"
3828 "<tr><th>Compression in:</th><td>%s</td></tr>"
3829 "<tr><th>Compression out:</th><td>%s</td><td>(%d%%)</td></tr>"
3830 "<tr><th>Compression bypass:</th><td>%s</td></tr>"
3831 "<tr><th>Total bytes saved:</th><td>%s</td><td>(%d%%)</td></tr>"
3832 "</table></div>%s</td>",
3833 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "<u>":"",
3834 U2H(stats[ST_F_BOUT].u.u64),
3835 U2H(stats[ST_F_BOUT].u.u64),
3836 U2H(stats[ST_F_COMP_IN].u.u64),
3837 U2H(stats[ST_F_COMP_OUT].u.u64),
3838 stats[ST_F_COMP_IN].u.u64 ? (int)(stats[ST_F_COMP_OUT].u.u64 * 100 / stats[ST_F_COMP_IN].u.u64) : 0,
3839 U2H(stats[ST_F_COMP_BYP].u.u64),
3840 U2H(stats[ST_F_COMP_IN].u.u64 - stats[ST_F_COMP_BYP].u.u64),
3841 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,
3842 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "</u>":"");
3843
Willy Tarreau60600742016-01-11 15:32:30 +01003844 chunk_appendf(out,
Willy Tarreaubbf84502016-01-08 17:25:50 +01003845 /* denied: req, resp */
3846 "<td>%s</td><td>%s</td>"
3847 /* errors : request, connect */
3848 "<td></td><td>%s</td>"
3849 /* errors : response */
3850 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
3851 /* warnings: retries, redispatches */
3852 "<td>%lld</td><td>%lld</td>"
3853 /* backend status: reflect backend status (up/down): we display UP
3854 * if the backend has known working servers or if it has no server at
3855 * all (eg: for stats). Then we display the total weight, number of
3856 * active and backups. */
3857 "<td class=ac>%s %s</td><td class=ac>&nbsp;</td><td class=ac>%d</td>"
3858 "<td class=ac>%d</td><td class=ac>%d</td>"
3859 "",
3860 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
3861 U2H(stats[ST_F_ECON].u.u64),
3862 U2H(stats[ST_F_ERESP].u.u64),
3863 (long long)stats[ST_F_CLI_ABRT].u.u64,
3864 (long long)stats[ST_F_SRV_ABRT].u.u64,
3865 (long long)stats[ST_F_WRETR].u.u64, (long long)stats[ST_F_WREDIS].u.u64,
3866 human_time(stats[ST_F_LASTCHG].u.u32, 1),
3867 strcmp(field_str(stats, ST_F_STATUS), "DOWN") ? field_str(stats, ST_F_STATUS) : "<font color=\"red\"><b>DOWN</b></font>",
3868 stats[ST_F_WEIGHT].u.u32,
3869 stats[ST_F_ACT].u.u32, stats[ST_F_BCK].u.u32);
3870
Willy Tarreau60600742016-01-11 15:32:30 +01003871 chunk_appendf(out,
Willy Tarreaubbf84502016-01-08 17:25:50 +01003872 /* rest of backend: nothing, down transitions, total downtime, throttle */
3873 "<td class=ac>&nbsp;</td><td>%d</td>"
3874 "<td>%s</td>"
3875 "<td></td>"
3876 "</tr>",
3877 stats[ST_F_CHKDOWN].u.u32,
3878 stats[ST_F_DOWNTIME].type ? human_time(stats[ST_F_DOWNTIME].u.u32, 1) : "&nbsp;");
3879 }
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003880 return 1;
3881}
3882
Willy Tarreau501f6022016-01-08 17:37:22 +01003883static int stats_dump_one_line(const struct field *stats, unsigned int flags, struct proxy *px, struct appctx *appctx)
3884{
Willy Tarreau508a63f2016-01-11 15:28:40 +01003885 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN))
3886 flags |= ST_SHOWADMIN;
Willy Tarreau501f6022016-01-08 17:37:22 +01003887
3888 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
Willy Tarreau60600742016-01-11 15:32:30 +01003889 return stats_dump_fields_html(&trash, stats, flags);
Willy Tarreau1e62df92016-01-11 18:57:53 +01003890 else if (appctx->ctx.stats.flags & STAT_FMT_TYPED)
3891 return stats_dump_fields_typed(&trash, stats);
Willy Tarreau501f6022016-01-08 17:37:22 +01003892 else
3893 return stats_dump_fields_csv(&trash, stats);
3894}
3895
Thierry Fournier23d2d642016-03-25 08:20:11 +01003896/* Fill <stats> with the frontend statistics. <stats> is
3897 * preallocated array of length <len>. The length of the array
3898 * must be at least ST_F_TOTAL_FIELDS. If this length is less then
3899 * this value, the function returns 0, otherwise, it returns 1.
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003900 */
Thierry Fournier23d2d642016-03-25 08:20:11 +01003901int stats_fill_fe_stats(struct proxy *px, struct field *stats, int len)
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003902{
Thierry Fournier23d2d642016-03-25 08:20:11 +01003903 if (len < ST_F_TOTAL_FIELDS)
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003904 return 0;
3905
Thierry Fournier23d2d642016-03-25 08:20:11 +01003906 memset(stats, 0, sizeof(*stats) * len);
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003907
3908 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
3909 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, "FRONTEND");
Willy Tarreauf8211df2016-01-11 14:09:38 +01003910 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003911 stats[ST_F_SCUR] = mkf_u32(0, px->feconn);
3912 stats[ST_F_SMAX] = mkf_u32(FN_MAX, px->fe_counters.conn_max);
3913 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, px->maxconn);
3914 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, px->fe_counters.cum_sess);
3915 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, px->fe_counters.bytes_in);
3916 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, px->fe_counters.bytes_out);
3917 stats[ST_F_DREQ] = mkf_u64(FN_COUNTER, px->fe_counters.denied_req);
3918 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, px->fe_counters.denied_resp);
3919 stats[ST_F_EREQ] = mkf_u64(FN_COUNTER, px->fe_counters.failed_req);
3920 stats[ST_F_STATUS] = mkf_str(FO_STATUS, px->state == PR_STREADY ? "OPEN" : px->state == PR_STFULL ? "FULL" : "STOP");
3921 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
3922 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
3923 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, 0);
3924 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_FE);
3925 stats[ST_F_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_sess_per_sec));
3926 stats[ST_F_RATE_LIM] = mkf_u32(FO_CONFIG|FN_LIMIT, px->fe_sps_lim);
3927 stats[ST_F_RATE_MAX] = mkf_u32(FN_MAX, px->fe_counters.sps_max);
3928
3929 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
3930 if (px->mode == PR_MODE_HTTP) {
3931 stats[ST_F_HRSP_1XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[1]);
3932 stats[ST_F_HRSP_2XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[2]);
3933 stats[ST_F_HRSP_3XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[3]);
3934 stats[ST_F_HRSP_4XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[4]);
3935 stats[ST_F_HRSP_5XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[5]);
3936 stats[ST_F_HRSP_OTHER] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[0]);
Willy Tarreau5b9bdff2016-01-11 14:40:47 +01003937 stats[ST_F_INTERCEPTED] = mkf_u64(FN_COUNTER, px->fe_counters.intercepted_req);
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003938 }
3939
3940 /* requests : req_rate, req_rate_max, req_tot, */
3941 stats[ST_F_REQ_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_req_per_sec));
3942 stats[ST_F_REQ_RATE_MAX] = mkf_u32(FN_MAX, px->fe_counters.p.http.rps_max);
3943 stats[ST_F_REQ_TOT] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cum_req);
3944
3945 /* compression: in, out, bypassed, responses */
3946 stats[ST_F_COMP_IN] = mkf_u64(FN_COUNTER, px->fe_counters.comp_in);
3947 stats[ST_F_COMP_OUT] = mkf_u64(FN_COUNTER, px->fe_counters.comp_out);
3948 stats[ST_F_COMP_BYP] = mkf_u64(FN_COUNTER, px->fe_counters.comp_byp);
3949 stats[ST_F_COMP_RSP] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.comp_rsp);
3950
Willy Tarreauc73810f2016-01-11 13:52:04 +01003951 /* connections : conn_rate, conn_rate_max, conn_tot, conn_max */
3952 stats[ST_F_CONN_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_conn_per_sec));
3953 stats[ST_F_CONN_RATE_MAX] = mkf_u32(FN_MAX, px->fe_counters.cps_max);
3954 stats[ST_F_CONN_TOT] = mkf_u64(FN_COUNTER, px->fe_counters.cum_conn);
3955
Thierry Fournier23d2d642016-03-25 08:20:11 +01003956 return 1;
3957}
3958
3959/* Dumps a frontend's line to the trash for the current proxy <px> and uses
3960 * the state from stream interface <si>. The caller is responsible for clearing
3961 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
3962 */
3963static int stats_dump_fe_stats(struct stream_interface *si, struct proxy *px)
3964{
3965 struct appctx *appctx = __objt_appctx(si->end);
3966
3967 if (!(px->cap & PR_CAP_FE))
3968 return 0;
3969
3970 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_FE)))
3971 return 0;
3972
3973 if (!stats_fill_fe_stats(px, stats, ST_F_TOTAL_FIELDS))
3974 return 0;
3975
Willy Tarreau501f6022016-01-08 17:37:22 +01003976 return stats_dump_one_line(stats, 0, px, appctx);
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003977}
3978
Thierry Fournierc4456852016-03-25 08:20:49 +01003979/* Fill <stats> with the listener statistics. <stats> is
3980 * preallocated array of length <len>. The length of the array
3981 * must be at least ST_F_TOTAL_FIELDS. If this length is less
3982 * then this value, the function returns 0, otherwise, it
3983 * returns 1. <flags> can take the value ST_SHLGNDS.
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003984 */
Thierry Fournierc4456852016-03-25 08:20:49 +01003985int stats_fill_li_stats(struct proxy *px, struct listener *l, int flags,
3986 struct field *stats, int len)
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003987{
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003988 struct chunk *out = get_trash_chunk();
3989
Thierry Fournierc4456852016-03-25 08:20:49 +01003990 if (len < ST_F_TOTAL_FIELDS)
3991 return 0;
3992
Thierry Fournierac9d4672016-03-25 08:43:46 +01003993 if (!l->counters)
3994 return 0;
3995
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003996 chunk_reset(out);
Thierry Fournierc4456852016-03-25 08:20:49 +01003997 memset(stats, 0, sizeof(*stats) * len);
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003998
3999 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
4000 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, l->name);
Willy Tarreauf8211df2016-01-11 14:09:38 +01004001 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004002 stats[ST_F_SCUR] = mkf_u32(0, l->nbconn);
4003 stats[ST_F_SMAX] = mkf_u32(FN_MAX, l->counters->conn_max);
4004 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, l->maxconn);
4005 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, l->counters->cum_conn);
4006 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, l->counters->bytes_in);
4007 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, l->counters->bytes_out);
4008 stats[ST_F_DREQ] = mkf_u64(FN_COUNTER, l->counters->denied_req);
4009 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, l->counters->denied_resp);
4010 stats[ST_F_EREQ] = mkf_u64(FN_COUNTER, l->counters->failed_req);
4011 stats[ST_F_STATUS] = mkf_str(FO_STATUS, (l->nbconn < l->maxconn) ? (l->state == LI_LIMITED) ? "WAITING" : "OPEN" : "FULL");
4012 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
4013 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
4014 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, l->luid);
4015 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_SO);
4016
4017 if (flags & ST_SHLGNDS) {
4018 char str[INET6_ADDRSTRLEN];
4019 int port;
4020
4021 port = get_host_port(&l->addr);
4022 switch (addr_to_str(&l->addr, str, sizeof(str))) {
4023 case AF_INET:
4024 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
4025 chunk_appendf(out, "%s:%d", str, port);
4026 break;
4027 case AF_INET6:
4028 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
4029 chunk_appendf(out, "[%s]:%d", str, port);
4030 break;
4031 case AF_UNIX:
4032 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, "unix");
4033 break;
4034 case -1:
4035 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
4036 chunk_strcat(out, strerror(errno));
4037 break;
4038 default: /* address family not supported */
4039 break;
4040 }
4041 }
4042
Thierry Fournierc4456852016-03-25 08:20:49 +01004043 return 1;
4044}
4045
4046/* Dumps a line for listener <l> and proxy <px> to the trash and uses the state
4047 * from stream interface <si>, and stats flags <flags>. The caller is responsible
4048 * for clearing the trash if needed. Returns non-zero if it emits anything, zero
4049 * otherwise.
4050 */
4051static int stats_dump_li_stats(struct stream_interface *si, struct proxy *px, struct listener *l, int flags)
4052{
4053 struct appctx *appctx = __objt_appctx(si->end);
4054
4055 if (!stats_fill_li_stats(px, l, flags, stats, ST_F_TOTAL_FIELDS))
4056 return 0;
4057
Willy Tarreau501f6022016-01-08 17:37:22 +01004058 return stats_dump_one_line(stats, flags, px, appctx);
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004059}
4060
4061enum srv_stats_state {
4062 SRV_STATS_STATE_DOWN = 0,
4063 SRV_STATS_STATE_DOWN_AGENT,
4064 SRV_STATS_STATE_GOING_UP,
4065 SRV_STATS_STATE_UP_GOING_DOWN,
4066 SRV_STATS_STATE_UP,
4067 SRV_STATS_STATE_NOLB_GOING_DOWN,
Simon Horman4d2eab62015-04-23 14:51:26 +09004068 SRV_STATS_STATE_NOLB,
4069 SRV_STATS_STATE_DRAIN_GOING_DOWN,
4070 SRV_STATS_STATE_DRAIN,
Simon Hormanb167b6b2015-04-23 14:51:29 +09004071 SRV_STATS_STATE_DRAIN_AGENT,
Simon Horman4d2eab62015-04-23 14:51:26 +09004072 SRV_STATS_STATE_NO_CHECK,
4073
4074 SRV_STATS_STATE_COUNT, /* Must be last */
4075};
4076
Willy Tarreau2b96cf12016-01-06 19:25:38 +01004077static const char *srv_hlt_st[SRV_STATS_STATE_COUNT] = {
4078 [SRV_STATS_STATE_DOWN] = "DOWN",
4079 [SRV_STATS_STATE_DOWN_AGENT] = "DOWN (agent)",
4080 [SRV_STATS_STATE_GOING_UP] = "DOWN %d/%d",
4081 [SRV_STATS_STATE_UP_GOING_DOWN] = "UP %d/%d",
4082 [SRV_STATS_STATE_UP] = "UP",
4083 [SRV_STATS_STATE_NOLB_GOING_DOWN] = "NOLB %d/%d",
4084 [SRV_STATS_STATE_NOLB] = "NOLB",
4085 [SRV_STATS_STATE_DRAIN_GOING_DOWN] = "DRAIN %d/%d",
4086 [SRV_STATS_STATE_DRAIN] = "DRAIN",
4087 [SRV_STATS_STATE_DRAIN_AGENT] = "DRAIN (agent)",
4088 [SRV_STATS_STATE_NO_CHECK] = "no check"
4089};
4090
Thierry Fournier61fe6c02016-03-25 08:21:21 +01004091/* Fill <stats> with the server statistics. <stats> is
4092 * preallocated array of length <len>. The length of the array
4093 * must be at least ST_F_TOTAL_FIELDS. If this length is less
4094 * then this value, the function returns 0, otherwise, it
4095 * returns 1. <flags> can take the value ST_SHLGNDS.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004096 */
Thierry Fournier61fe6c02016-03-25 08:21:21 +01004097int stats_fill_sv_stats(struct proxy *px, struct server *sv, int flags,
4098 struct field *stats, int len)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004099{
Willy Tarreau32091232014-05-16 13:52:00 +02004100 struct server *via, *ref;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004101 char str[INET6_ADDRSTRLEN];
Willy Tarreau2b96cf12016-01-06 19:25:38 +01004102 struct chunk *out = get_trash_chunk();
Willy Tarreauba2f2642016-01-07 09:54:40 +01004103 enum srv_stats_state state;
Willy Tarreau2b96cf12016-01-06 19:25:38 +01004104 char *fld_status;
Thierry Fournier61fe6c02016-03-25 08:21:21 +01004105
4106 if (len < ST_F_TOTAL_FIELDS)
4107 return 0;
4108
4109 memset(stats, 0, sizeof(*stats) * len);
4110
Willy Tarreau32091232014-05-16 13:52:00 +02004111 /* we have "via" which is the tracked server as described in the configuration,
4112 * and "ref" which is the checked server and the end of the chain.
4113 */
4114 via = sv->track ? sv->track : sv;
4115 ref = via;
4116 while (ref->track)
4117 ref = ref->track;
4118
Willy Tarreauba2f2642016-01-07 09:54:40 +01004119 if (sv->state == SRV_ST_RUNNING || sv->state == SRV_ST_STARTING) {
4120 if ((ref->check.state & CHK_ST_ENABLED) &&
4121 (ref->check.health < ref->check.rise + ref->check.fall - 1)) {
4122 state = SRV_STATS_STATE_UP_GOING_DOWN;
Willy Tarreauba2f2642016-01-07 09:54:40 +01004123 } else {
4124 state = SRV_STATS_STATE_UP;
Willy Tarreauba2f2642016-01-07 09:54:40 +01004125 }
4126
Willy Tarreauba2f2642016-01-07 09:54:40 +01004127 if (sv->admin & SRV_ADMF_DRAIN) {
4128 if (ref->agent.state & CHK_ST_ENABLED)
4129 state = SRV_STATS_STATE_DRAIN_AGENT;
4130 else if (state == SRV_STATS_STATE_UP_GOING_DOWN)
4131 state = SRV_STATS_STATE_DRAIN_GOING_DOWN;
4132 else
4133 state = SRV_STATS_STATE_DRAIN;
4134 }
4135
4136 if (state == SRV_STATS_STATE_UP && !(ref->check.state & CHK_ST_ENABLED)) {
4137 state = SRV_STATS_STATE_NO_CHECK;
Willy Tarreauba2f2642016-01-07 09:54:40 +01004138 }
4139 }
4140 else if (sv->state == SRV_ST_STOPPING) {
4141 if ((!(sv->check.state & CHK_ST_ENABLED) && !sv->track) ||
4142 (ref->check.health == ref->check.rise + ref->check.fall - 1)) {
4143 state = SRV_STATS_STATE_NOLB;
Willy Tarreauba2f2642016-01-07 09:54:40 +01004144 } else {
4145 state = SRV_STATS_STATE_NOLB_GOING_DOWN;
Willy Tarreauba2f2642016-01-07 09:54:40 +01004146 }
4147 }
4148 else { /* stopped */
4149 if ((ref->agent.state & CHK_ST_ENABLED) && !ref->agent.health) {
4150 state = SRV_STATS_STATE_DOWN_AGENT;
Willy Tarreauba2f2642016-01-07 09:54:40 +01004151 } else if ((ref->check.state & CHK_ST_ENABLED) && !ref->check.health) {
4152 state = SRV_STATS_STATE_DOWN; /* DOWN */
Willy Tarreauba2f2642016-01-07 09:54:40 +01004153 } else if ((ref->agent.state & CHK_ST_ENABLED) || (ref->check.state & CHK_ST_ENABLED)) {
4154 state = SRV_STATS_STATE_GOING_UP;
Willy Tarreauba2f2642016-01-07 09:54:40 +01004155 } else {
4156 state = SRV_STATS_STATE_DOWN; /* DOWN, unchecked */
Willy Tarreauba2f2642016-01-07 09:54:40 +01004157 }
4158 }
4159
Willy Tarreau2b96cf12016-01-06 19:25:38 +01004160 chunk_reset(out);
Willy Tarreau2b96cf12016-01-06 19:25:38 +01004161
4162 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
4163 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, sv->id);
Willy Tarreauf8211df2016-01-11 14:09:38 +01004164 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
Willy Tarreau2b96cf12016-01-06 19:25:38 +01004165 stats[ST_F_QCUR] = mkf_u32(0, sv->nbpend);
4166 stats[ST_F_QMAX] = mkf_u32(FN_MAX, sv->counters.nbpend_max);
4167 stats[ST_F_SCUR] = mkf_u32(0, sv->cur_sess);
4168 stats[ST_F_SMAX] = mkf_u32(FN_MAX, sv->counters.cur_sess_max);
4169
4170 if (sv->maxconn)
4171 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, sv->maxconn);
4172
4173 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, sv->counters.cum_sess);
4174 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, sv->counters.bytes_in);
4175 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, sv->counters.bytes_out);
4176 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, sv->counters.failed_secu);
4177 stats[ST_F_ECON] = mkf_u64(FN_COUNTER, sv->counters.failed_conns);
4178 stats[ST_F_ERESP] = mkf_u64(FN_COUNTER, sv->counters.failed_resp);
4179 stats[ST_F_WRETR] = mkf_u64(FN_COUNTER, sv->counters.retries);
4180 stats[ST_F_WREDIS] = mkf_u64(FN_COUNTER, sv->counters.redispatches);
4181
4182 /* status */
4183 fld_status = chunk_newstr(out);
4184 if (sv->admin & SRV_ADMF_IMAINT)
4185 chunk_appendf(out, "MAINT (via %s/%s)", via->proxy->id, via->id);
4186 else if (sv->admin & SRV_ADMF_MAINT)
4187 chunk_appendf(out, "MAINT");
4188 else
4189 chunk_appendf(out,
4190 srv_hlt_st[state],
4191 (ref->state != SRV_ST_STOPPED) ? (ref->check.health - ref->check.rise + 1) : (ref->check.health),
4192 (ref->state != SRV_ST_STOPPED) ? (ref->check.fall) : (ref->check.rise));
4193
4194 stats[ST_F_STATUS] = mkf_str(FO_STATUS, fld_status);
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004195 stats[ST_F_LASTCHG] = mkf_u32(FN_AGE, now.tv_sec - sv->last_change);
4196 stats[ST_F_WEIGHT] = mkf_u32(FN_AVG, (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv);
4197 stats[ST_F_ACT] = mkf_u32(FO_STATUS, (sv->flags & SRV_F_BACKUP) ? 0 : 1);
4198 stats[ST_F_BCK] = mkf_u32(FO_STATUS, (sv->flags & SRV_F_BACKUP) ? 1 : 0);
Willy Tarreau91861262007-10-17 17:06:05 +02004199
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004200 /* check failures: unique, fatal; last change, total downtime */
4201 if (sv->check.state & CHK_ST_ENABLED) {
4202 stats[ST_F_CHKFAIL] = mkf_u64(FN_COUNTER, sv->counters.failed_checks);
4203 stats[ST_F_CHKDOWN] = mkf_u64(FN_COUNTER, sv->counters.down_trans);
4204 stats[ST_F_DOWNTIME] = mkf_u32(FN_COUNTER, srv_downtime(sv));
4205 }
Willy Tarreau164d4a92016-01-06 19:48:21 +01004206
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004207 if (sv->maxqueue)
4208 stats[ST_F_QLIMIT] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->maxqueue);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004209
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004210 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
4211 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
4212 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, sv->puid);
Willy Tarreau91861262007-10-17 17:06:05 +02004213
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004214 if (sv->state == SRV_ST_STARTING && !server_is_draining(sv))
4215 stats[ST_F_THROTTLE] = mkf_u32(FN_AVG, server_throttle_rate(sv));
Willy Tarreauf5b1cc32014-06-17 12:20:59 +02004216
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004217 stats[ST_F_LBTOT] = mkf_u64(FN_COUNTER, sv->counters.cum_lbconn);
Willy Tarreau91861262007-10-17 17:06:05 +02004218
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004219 if (sv->track) {
4220 char *fld_track = chunk_newstr(out);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004221
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004222 chunk_appendf(out, "%s/%s", sv->track->proxy->id, sv->track->id);
4223 stats[ST_F_TRACKED] = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, fld_track);
4224 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004225
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004226 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_SV);
4227 stats[ST_F_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&sv->sess_per_sec));
4228 stats[ST_F_RATE_MAX] = mkf_u32(FN_MAX, sv->counters.sps_max);
Willy Tarreau164d4a92016-01-06 19:48:21 +01004229
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004230 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) {
4231 const char *fld_chksts;
Willy Tarreau164d4a92016-01-06 19:48:21 +01004232
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004233 fld_chksts = chunk_newstr(out);
4234 chunk_strcat(out, "* "); // for check in progress
4235 chunk_strcat(out, get_check_status_info(sv->check.status));
4236 if (!(sv->check.state & CHK_ST_INPROGRESS))
4237 fld_chksts += 2; // skip "* "
4238 stats[ST_F_CHECK_STATUS] = mkf_str(FN_OUTPUT, fld_chksts);
Willy Tarreau91861262007-10-17 17:06:05 +02004239
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004240 if (sv->check.status >= HCHK_STATUS_L57DATA)
4241 stats[ST_F_CHECK_CODE] = mkf_u32(FN_OUTPUT, sv->check.code);
Willy Tarreaucf2924b2014-05-23 12:15:15 +02004242
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004243 if (sv->check.status >= HCHK_STATUS_CHECKED)
4244 stats[ST_F_CHECK_DURATION] = mkf_u64(FN_DURATION, sv->check.duration);
Willy Tarreaucf2924b2014-05-23 12:15:15 +02004245
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004246 stats[ST_F_CHECK_DESC] = mkf_str(FN_OUTPUT, get_check_status_description(sv->check.status));
4247 stats[ST_F_LAST_CHK] = mkf_str(FN_OUTPUT, sv->check.desc);
4248 stats[ST_F_CHECK_RISE] = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.rise);
4249 stats[ST_F_CHECK_FALL] = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.fall);
4250 stats[ST_F_CHECK_HEALTH] = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.health);
4251 }
Willy Tarreaucf2924b2014-05-23 12:15:15 +02004252
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004253 if ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) {
4254 const char *fld_chksts;
Willy Tarreaudd7354b2016-01-08 13:47:26 +01004255
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004256 fld_chksts = chunk_newstr(out);
4257 chunk_strcat(out, "* "); // for check in progress
4258 chunk_strcat(out, get_check_status_info(sv->agent.status));
4259 if (!(sv->agent.state & CHK_ST_INPROGRESS))
4260 fld_chksts += 2; // skip "* "
4261 stats[ST_F_AGENT_STATUS] = mkf_str(FN_OUTPUT, fld_chksts);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004262
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004263 if (sv->agent.status >= HCHK_STATUS_L57DATA)
4264 stats[ST_F_AGENT_CODE] = mkf_u32(FN_OUTPUT, sv->agent.code);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004265
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004266 if (sv->agent.status >= HCHK_STATUS_CHECKED)
4267 stats[ST_F_AGENT_DURATION] = mkf_u64(FN_DURATION, sv->agent.duration);
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01004268
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004269 stats[ST_F_AGENT_DESC] = mkf_str(FN_OUTPUT, get_check_status_description(sv->agent.status));
4270 stats[ST_F_LAST_AGT] = mkf_str(FN_OUTPUT, sv->agent.desc);
4271 stats[ST_F_AGENT_RISE] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.rise);
4272 stats[ST_F_AGENT_FALL] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.fall);
4273 stats[ST_F_AGENT_HEALTH] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.health);
4274 }
Willy Tarreaudd7354b2016-01-08 13:47:26 +01004275
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004276 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
4277 if (px->mode == PR_MODE_HTTP) {
4278 stats[ST_F_HRSP_1XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[1]);
4279 stats[ST_F_HRSP_2XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[2]);
4280 stats[ST_F_HRSP_3XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[3]);
4281 stats[ST_F_HRSP_4XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[4]);
4282 stats[ST_F_HRSP_5XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[5]);
4283 stats[ST_F_HRSP_OTHER] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[0]);
4284 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004285
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004286 if (ref->observe)
4287 stats[ST_F_HANAFAIL] = mkf_u64(FN_COUNTER, sv->counters.failed_hana);
Willy Tarreau164d4a92016-01-06 19:48:21 +01004288
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004289 stats[ST_F_CLI_ABRT] = mkf_u64(FN_COUNTER, sv->counters.cli_aborts);
4290 stats[ST_F_SRV_ABRT] = mkf_u64(FN_COUNTER, sv->counters.srv_aborts);
4291 stats[ST_F_LASTSESS] = mkf_s32(FN_AGE, srv_lastsession(sv));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004292
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004293 stats[ST_F_QTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.q_time, TIME_STATS_SAMPLES));
4294 stats[ST_F_CTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.c_time, TIME_STATS_SAMPLES));
4295 stats[ST_F_RTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.d_time, TIME_STATS_SAMPLES));
4296 stats[ST_F_TTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.t_time, TIME_STATS_SAMPLES));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004297
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004298 if (flags & ST_SHLGNDS) {
4299 switch (addr_to_str(&sv->addr, str, sizeof(str))) {
4300 case AF_INET:
4301 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
4302 chunk_appendf(out, "%s:%d", str, get_host_port(&sv->addr));
4303 break;
4304 case AF_INET6:
4305 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
4306 chunk_appendf(out, "[%s]:%d", str, get_host_port(&sv->addr));
4307 break;
4308 case AF_UNIX:
4309 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, "unix");
4310 break;
4311 case -1:
4312 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
4313 chunk_strcat(out, strerror(errno));
4314 break;
4315 default: /* address family not supported */
4316 break;
Willy Tarreauf4659942013-11-28 10:50:06 +01004317 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004318
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004319 if (sv->cookie)
4320 stats[ST_F_COOKIE] = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, sv->cookie);
4321 }
4322
Thierry Fournier61fe6c02016-03-25 08:21:21 +01004323 return 1;
4324}
4325
4326/* Dumps a line for server <sv> and proxy <px> to the trash and uses the state
4327 * from stream interface <si>, stats flags <flags>, and server state <state>.
4328 * The caller is responsible for clearing the trash if needed. Returns non-zero
4329 * if it emits anything, zero otherwise.
4330 */
4331static int stats_dump_sv_stats(struct stream_interface *si, struct proxy *px, int flags, struct server *sv)
4332{
4333 struct appctx *appctx = __objt_appctx(si->end);
4334
4335 if (!stats_fill_sv_stats(px, sv, flags, stats, ST_F_TOTAL_FIELDS))
4336 return 0;
4337
Willy Tarreau501f6022016-01-08 17:37:22 +01004338 return stats_dump_one_line(stats, flags, px, appctx);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004339}
4340
Thierry Fournierd0a56c22016-03-25 08:21:51 +01004341/* Fill <stats> with the backend statistics. <stats> is
4342 * preallocated array of length <len>. The length of the array
4343 * must be at least ST_F_TOTAL_FIELDS. If this length is less
4344 * then this value, the function returns 0, otherwise, it
4345 * returns 1. <flags> can take the value ST_SHLGNDS.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004346 */
Thierry Fournierd0a56c22016-03-25 08:21:51 +01004347int stats_fill_be_stats(struct proxy *px, int flags, struct field *stats, int len)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004348{
Thierry Fournierd0a56c22016-03-25 08:21:51 +01004349 if (len < ST_F_TOTAL_FIELDS)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004350 return 0;
4351
Thierry Fournierd0a56c22016-03-25 08:21:51 +01004352 memset(stats, 0, sizeof(*stats) * len);
Willy Tarreauf6eecbe2016-01-06 16:14:50 +01004353
4354 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
4355 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, "BACKEND");
Willy Tarreauf8211df2016-01-11 14:09:38 +01004356 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
Willy Tarreauf6eecbe2016-01-06 16:14:50 +01004357 stats[ST_F_QCUR] = mkf_u32(0, px->nbpend);
4358 stats[ST_F_QMAX] = mkf_u32(FN_MAX, px->be_counters.nbpend_max);
4359 stats[ST_F_SCUR] = mkf_u32(FO_CONFIG|FN_LIMIT, px->beconn);
4360 stats[ST_F_SMAX] = mkf_u32(FN_MAX, px->be_counters.conn_max);
4361 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, px->fullconn);
4362 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, px->be_counters.cum_conn);
4363 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, px->be_counters.bytes_in);
4364 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, px->be_counters.bytes_out);
4365 stats[ST_F_DREQ] = mkf_u64(FN_COUNTER, px->be_counters.denied_req);
4366 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, px->be_counters.denied_resp);
4367 stats[ST_F_ECON] = mkf_u64(FN_COUNTER, px->be_counters.failed_conns);
4368 stats[ST_F_ERESP] = mkf_u64(FN_COUNTER, px->be_counters.failed_resp);
4369 stats[ST_F_WRETR] = mkf_u64(FN_COUNTER, px->be_counters.retries);
4370 stats[ST_F_WREDIS] = mkf_u64(FN_COUNTER, px->be_counters.redispatches);
4371 stats[ST_F_STATUS] = mkf_str(FO_STATUS, (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" : "DOWN");
4372 stats[ST_F_WEIGHT] = mkf_u32(FN_AVG, (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv);
4373 stats[ST_F_ACT] = mkf_u32(0, px->srv_act);
4374 stats[ST_F_BCK] = mkf_u32(0, px->srv_bck);
4375 stats[ST_F_CHKDOWN] = mkf_u64(FN_COUNTER, px->down_trans);
4376 stats[ST_F_LASTCHG] = mkf_u32(FN_AGE, now.tv_sec - px->last_change);
Willy Tarreau7344f472016-01-11 12:04:02 +01004377 if (px->srv)
4378 stats[ST_F_DOWNTIME] = mkf_u32(FN_COUNTER, be_downtime(px));
4379
Willy Tarreauf6eecbe2016-01-06 16:14:50 +01004380 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
4381 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
4382 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, 0);
4383 stats[ST_F_LBTOT] = mkf_u64(FN_COUNTER, px->be_counters.cum_lbconn);
4384 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_BE);
4385 stats[ST_F_RATE] = mkf_u32(0, read_freq_ctr(&px->be_sess_per_sec));
4386 stats[ST_F_RATE_MAX] = mkf_u32(0, px->be_counters.sps_max);
Willy Tarreauf1516d92016-01-11 14:48:36 +01004387
Willy Tarreaue4847c62016-01-08 15:43:54 +01004388 if (flags & ST_SHLGNDS) {
4389 if (px->cookie_name)
4390 stats[ST_F_COOKIE] = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, px->cookie_name);
Willy Tarreauf1516d92016-01-11 14:48:36 +01004391 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 +01004392 }
Willy Tarreauf6eecbe2016-01-06 16:14:50 +01004393
4394 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
4395 if (px->mode == PR_MODE_HTTP) {
4396 stats[ST_F_REQ_TOT] = mkf_u64(FN_COUNTER, px->be_counters.p.http.cum_req);
4397 stats[ST_F_HRSP_1XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[1]);
4398 stats[ST_F_HRSP_2XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[2]);
4399 stats[ST_F_HRSP_3XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[3]);
4400 stats[ST_F_HRSP_4XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[4]);
4401 stats[ST_F_HRSP_5XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[5]);
4402 stats[ST_F_HRSP_OTHER] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[0]);
Willy Tarreau5b9bdff2016-01-11 14:40:47 +01004403 stats[ST_F_INTERCEPTED] = mkf_u64(FN_COUNTER, px->be_counters.intercepted_req);
Willy Tarreauf6eecbe2016-01-06 16:14:50 +01004404 }
4405
4406 stats[ST_F_CLI_ABRT] = mkf_u64(FN_COUNTER, px->be_counters.cli_aborts);
4407 stats[ST_F_SRV_ABRT] = mkf_u64(FN_COUNTER, px->be_counters.srv_aborts);
4408
4409 /* compression: in, out, bypassed, responses */
4410 stats[ST_F_COMP_IN] = mkf_u64(FN_COUNTER, px->be_counters.comp_in);
4411 stats[ST_F_COMP_OUT] = mkf_u64(FN_COUNTER, px->be_counters.comp_out);
4412 stats[ST_F_COMP_BYP] = mkf_u64(FN_COUNTER, px->be_counters.comp_byp);
4413 stats[ST_F_COMP_RSP] = mkf_u64(FN_COUNTER, px->be_counters.p.http.comp_rsp);
4414 stats[ST_F_LASTSESS] = mkf_s32(FN_AGE, be_lastsession(px));
4415
4416 stats[ST_F_QTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.q_time, TIME_STATS_SAMPLES));
4417 stats[ST_F_CTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.c_time, TIME_STATS_SAMPLES));
4418 stats[ST_F_RTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.d_time, TIME_STATS_SAMPLES));
4419 stats[ST_F_TTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.t_time, TIME_STATS_SAMPLES));
4420
Thierry Fournierd0a56c22016-03-25 08:21:51 +01004421 return 1;
4422}
4423
4424/* Dumps a line for backend <px> to the trash for and uses the state from stream
4425 * interface <si> and stats flags <flags>. The caller is responsible for clearing
4426 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
4427 */
4428static int stats_dump_be_stats(struct stream_interface *si, struct proxy *px, int flags)
4429{
4430 struct appctx *appctx = __objt_appctx(si->end);
4431
4432 if (!(px->cap & PR_CAP_BE))
4433 return 0;
4434
4435 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_BE)))
4436 return 0;
4437
4438 if (!stats_fill_be_stats(px, flags, stats, ST_F_TOTAL_FIELDS))
4439 return 0;
4440
Willy Tarreau501f6022016-01-08 17:37:22 +01004441 return stats_dump_one_line(stats, flags, px, appctx);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004442}
4443
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004444/* Dumps the HTML table header for proxy <px> to the trash for and uses the state from
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004445 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004446 * for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004447 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004448static void stats_dump_html_px_hdr(struct stream_interface *si, struct proxy *px, struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004449{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004450 struct appctx *appctx = __objt_appctx(si->end);
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004451 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
4452
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004453 if (px->cap & PR_CAP_BE && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004454 /* A form to enable/disable this proxy servers */
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004455
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004456 /* 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 +02004457 scope_txt[0] = 0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004458 if (appctx->ctx.stats.scope_len) {
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004459 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau4e4292b2014-11-28 12:18:45 +01004460 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 +01004461 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004462 }
4463
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004464 chunk_appendf(&trash,
Jeff Buchbinder2dbbf4d2014-08-29 15:10:08 -05004465 "<form method=\"post\">");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004466 }
4467
4468 /* print a new table */
4469 chunk_appendf(&trash,
4470 "<table class=\"tbl\" width=\"100%%\">\n"
4471 "<tr class=\"titre\">"
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01004472 "<th class=\"pxname\" width=\"10%%\">");
4473
4474 chunk_appendf(&trash,
4475 "<a name=\"%s\"></a>%s"
4476 "<a class=px href=\"#%s\">%s</a>",
4477 px->id,
4478 (uri->flags & ST_SHLGNDS) ? "<u>":"",
4479 px->id, px->id);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004480
4481 if (uri->flags & ST_SHLGNDS) {
4482 /* cap, mode, id */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02004483 chunk_appendf(&trash, "<div class=tips>cap: %s, mode: %s, id: %d",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004484 proxy_cap_str(px->cap), proxy_mode_str(px->mode),
4485 px->uuid);
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01004486 chunk_appendf(&trash, "</div>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004487 }
4488
4489 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01004490 "%s</th>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004491 "<th class=\"%s\" width=\"90%%\">%s</th>"
4492 "</tr>\n"
4493 "</table>\n"
4494 "<table class=\"tbl\" width=\"100%%\">\n"
4495 "<tr class=\"titre\">",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004496 (uri->flags & ST_SHLGNDS) ? "</u>":"",
4497 px->desc ? "desc" : "empty", px->desc ? px->desc : "");
4498
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004499 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004500 /* Column heading for Enable or Disable server */
4501 chunk_appendf(&trash, "<th rowspan=2 width=1></th>");
Willy Tarreau91861262007-10-17 17:06:05 +02004502 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004503
4504 chunk_appendf(&trash,
4505 "<th rowspan=2></th>"
4506 "<th colspan=3>Queue</th>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05004507 "<th colspan=3>Session rate</th><th colspan=6>Sessions</th>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004508 "<th colspan=2>Bytes</th><th colspan=2>Denied</th>"
4509 "<th colspan=3>Errors</th><th colspan=2>Warnings</th>"
4510 "<th colspan=9>Server</th>"
4511 "</tr>\n"
4512 "<tr class=\"titre\">"
4513 "<th>Cur</th><th>Max</th><th>Limit</th>"
4514 "<th>Cur</th><th>Max</th><th>Limit</th><th>Cur</th><th>Max</th>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05004515 "<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 +01004516 "<th>Req</th><th>Resp</th><th>Req</th><th>Conn</th>"
4517 "<th>Resp</th><th>Retr</th><th>Redis</th>"
4518 "<th>Status</th><th>LastChk</th><th>Wght</th><th>Act</th>"
4519 "<th>Bck</th><th>Chk</th><th>Dwn</th><th>Dwntme</th>"
4520 "<th>Thrtle</th>\n"
4521 "</tr>");
Willy Tarreau91861262007-10-17 17:06:05 +02004522}
4523
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004524/* Dumps the HTML table trailer for proxy <px> to the trash for and uses the state from
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004525 * stream interface <si>. The caller is responsible for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004526 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004527static void stats_dump_html_px_end(struct stream_interface *si, struct proxy *px)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004528{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004529 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004530 chunk_appendf(&trash, "</table>");
4531
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004532 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004533 /* close the form used to enable/disable this proxy servers */
4534 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004535 "Choose the action to perform on the checked servers : "
4536 "<select name=action>"
4537 "<option value=\"\"></option>"
Willy Tarreaued7df902014-05-22 18:04:49 +02004538 "<option value=\"ready\">Set state to READY</option>"
4539 "<option value=\"drain\">Set state to DRAIN</option>"
Marco Corte8c27bca2014-07-02 17:49:34 +02004540 "<option value=\"maint\">Set state to MAINT</option>"
Willy Tarreau248a60e2014-05-23 14:59:48 +02004541 "<option value=\"dhlth\">Health: disable checks</option>"
4542 "<option value=\"ehlth\">Health: enable checks</option>"
4543 "<option value=\"hrunn\">Health: force UP</option>"
4544 "<option value=\"hnolb\">Health: force NOLB</option>"
4545 "<option value=\"hdown\">Health: force DOWN</option>"
4546 "<option value=\"dagent\">Agent: disable checks</option>"
4547 "<option value=\"eagent\">Agent: enable checks</option>"
4548 "<option value=\"arunn\">Agent: force UP</option>"
4549 "<option value=\"adown\">Agent: force DOWN</option>"
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004550 "<option value=\"shutdown\">Kill Sessions</option>"
4551 "</select>"
4552 "<input type=\"hidden\" name=\"b\" value=\"#%d\">"
4553 "&nbsp;<input type=\"submit\" value=\"Apply\">"
4554 "</form>",
4555 px->uuid);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004556 }
4557
4558 chunk_appendf(&trash, "<p>\n");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004559}
Willy Tarreau91861262007-10-17 17:06:05 +02004560
4561/*
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004562 * Dumps statistics for a proxy. The output is sent to the stream interface's
4563 * input buffer. Returns 0 if it had to stop dumping data because of lack of
4564 * buffer space, or non-zero if everything completed. This function is used
4565 * both by the CLI and the HTTP entry points, and is able to dump the output
4566 * in HTML or CSV formats. If the later, <uri> must be NULL.
Willy Tarreau91861262007-10-17 17:06:05 +02004567 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004568static int stats_dump_proxy_to_buffer(struct stream_interface *si, struct proxy *px, struct uri_auth *uri)
Willy Tarreau91861262007-10-17 17:06:05 +02004569{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004570 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau87b09662015-04-03 00:22:06 +02004571 struct stream *s = si_strm(si);
Willy Tarreau2bb4a962014-11-28 11:11:05 +01004572 struct channel *rep = si_ic(si);
Willy Tarreau44267702011-10-28 15:35:33 +02004573 struct server *sv, *svs; /* server and server-state, server-state=server or server->track */
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004574 struct listener *l;
Willy Tarreau0deb85a2016-01-08 15:33:18 +01004575 unsigned int flags;
4576
4577 if (uri)
4578 flags = uri->flags;
4579 else if (strm_li(s)->bind_conf->level >= ACCESS_LVL_OPER)
4580 flags = ST_SHLGNDS | ST_SHNODE | ST_SHDESC;
4581 else
4582 flags = ST_SHNODE | ST_SHDESC;
Willy Tarreau91861262007-10-17 17:06:05 +02004583
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004584 chunk_reset(&trash);
Willy Tarreau91861262007-10-17 17:06:05 +02004585
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004586 switch (appctx->ctx.stats.px_st) {
Willy Tarreau295a8372011-03-10 11:25:07 +01004587 case STAT_PX_ST_INIT:
Willy Tarreau91861262007-10-17 17:06:05 +02004588 /* we are on a new proxy */
Willy Tarreau91861262007-10-17 17:06:05 +02004589 if (uri && uri->scope) {
4590 /* we have a limited scope, we have to check the proxy name */
4591 struct stat_scope *scope;
4592 int len;
4593
4594 len = strlen(px->id);
4595 scope = uri->scope;
4596
4597 while (scope) {
4598 /* match exact proxy name */
4599 if (scope->px_len == len && !memcmp(px->id, scope->px_id, len))
4600 break;
4601
4602 /* match '.' which means 'self' proxy */
Willy Tarreau1388a3a2007-10-18 16:38:37 +02004603 if (!strcmp(scope->px_id, ".") && px == s->be)
Willy Tarreau91861262007-10-17 17:06:05 +02004604 break;
4605 scope = scope->next;
4606 }
4607
4608 /* proxy name not found : don't dump anything */
4609 if (scope == NULL)
4610 return 1;
4611 }
4612
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004613 /* if the user has requested a limited output and the proxy
4614 * name does not match, skip it.
4615 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004616 if (appctx->ctx.stats.scope_len &&
Willy Tarreau4e4292b2014-11-28 12:18:45 +01004617 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 +02004618 return 1;
4619
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004620 if ((appctx->ctx.stats.flags & STAT_BOUND) &&
4621 (appctx->ctx.stats.iid != -1) &&
4622 (px->uuid != appctx->ctx.stats.iid))
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01004623 return 1;
4624
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004625 appctx->ctx.stats.px_st = STAT_PX_ST_TH;
Willy Tarreau91861262007-10-17 17:06:05 +02004626 /* fall through */
4627
Willy Tarreau295a8372011-03-10 11:25:07 +01004628 case STAT_PX_ST_TH:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004629 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004630 stats_dump_html_px_hdr(si, px, uri);
Willy Tarreaubc18da12015-03-13 14:00:47 +01004631 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004632 si_applet_cant_put(si);
Willy Tarreau55bb8452007-10-17 18:44:57 +02004633 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01004634 }
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004635 }
Willy Tarreau91861262007-10-17 17:06:05 +02004636
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004637 appctx->ctx.stats.px_st = STAT_PX_ST_FE;
Willy Tarreau91861262007-10-17 17:06:05 +02004638 /* fall through */
4639
Willy Tarreau295a8372011-03-10 11:25:07 +01004640 case STAT_PX_ST_FE:
Willy Tarreau91861262007-10-17 17:06:05 +02004641 /* print the frontend */
Willy Tarreaubc18da12015-03-13 14:00:47 +01004642 if (stats_dump_fe_stats(si, px)) {
4643 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004644 si_applet_cant_put(si);
Willy Tarreau91861262007-10-17 17:06:05 +02004645 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01004646 }
4647 }
Willy Tarreau91861262007-10-17 17:06:05 +02004648
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004649 appctx->ctx.stats.l = px->conf.listeners.n;
4650 appctx->ctx.stats.px_st = STAT_PX_ST_LI;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004651 /* fall through */
4652
Willy Tarreau295a8372011-03-10 11:25:07 +01004653 case STAT_PX_ST_LI:
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004654 /* stats.l has been initialized above */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004655 for (; appctx->ctx.stats.l != &px->conf.listeners; appctx->ctx.stats.l = l->by_fe.n) {
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01004656 if (buffer_almost_full(rep->buf)) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004657 si_applet_cant_put(si);
Willy Tarreau4e33d862009-10-11 23:35:10 +02004658 return 0;
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01004659 }
Willy Tarreau4e33d862009-10-11 23:35:10 +02004660
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004661 l = LIST_ELEM(appctx->ctx.stats.l, struct listener *, by_fe);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004662 if (!l->counters)
4663 continue;
4664
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004665 if (appctx->ctx.stats.flags & STAT_BOUND) {
4666 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SO)))
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004667 break;
4668
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004669 if (appctx->ctx.stats.sid != -1 && l->luid != appctx->ctx.stats.sid)
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004670 continue;
4671 }
4672
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004673 /* print the frontend */
Willy Tarreau0deb85a2016-01-08 15:33:18 +01004674 if (stats_dump_li_stats(si, px, l, flags)) {
Willy Tarreaubc18da12015-03-13 14:00:47 +01004675 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004676 si_applet_cant_put(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004677 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01004678 }
4679 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004680 }
4681
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004682 appctx->ctx.stats.sv = px->srv; /* may be NULL */
4683 appctx->ctx.stats.px_st = STAT_PX_ST_SV;
Willy Tarreau91861262007-10-17 17:06:05 +02004684 /* fall through */
4685
Willy Tarreau295a8372011-03-10 11:25:07 +01004686 case STAT_PX_ST_SV:
Willy Tarreau91861262007-10-17 17:06:05 +02004687 /* stats.sv has been initialized above */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004688 for (; appctx->ctx.stats.sv != NULL; appctx->ctx.stats.sv = sv->next) {
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01004689 if (buffer_almost_full(rep->buf)) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004690 si_applet_cant_put(si);
Willy Tarreau4e33d862009-10-11 23:35:10 +02004691 return 0;
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01004692 }
Willy Tarreau4e33d862009-10-11 23:35:10 +02004693
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004694 sv = appctx->ctx.stats.sv;
Willy Tarreau91861262007-10-17 17:06:05 +02004695
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004696 if (appctx->ctx.stats.flags & STAT_BOUND) {
4697 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SV)))
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01004698 break;
4699
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004700 if (appctx->ctx.stats.sid != -1 && sv->puid != appctx->ctx.stats.sid)
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01004701 continue;
4702 }
4703
Willy Tarreau32091232014-05-16 13:52:00 +02004704 svs = sv;
4705 while (svs->track)
4706 svs = svs->track;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01004707
Willy Tarreauba2f2642016-01-07 09:54:40 +01004708 /* do not report servers which are DOWN and not changing state */
4709 if ((appctx->ctx.stats.flags & STAT_HIDE_DOWN) &&
4710 ((sv->admin & SRV_ADMF_MAINT) || /* server is in maintenance */
4711 (sv->state == SRV_ST_STOPPED && /* server is down */
4712 (!((svs->agent.state | svs->check.state) & CHK_ST_ENABLED) ||
4713 ((svs->agent.state & CHK_ST_ENABLED) && !svs->agent.health) ||
4714 ((svs->check.state & CHK_ST_ENABLED) && !svs->check.health))))) {
Willy Tarreau91861262007-10-17 17:06:05 +02004715 continue;
4716 }
4717
Willy Tarreau0deb85a2016-01-08 15:33:18 +01004718 if (stats_dump_sv_stats(si, px, flags, sv)) {
Willy Tarreaubc18da12015-03-13 14:00:47 +01004719 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004720 si_applet_cant_put(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004721 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01004722 }
4723 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004724 } /* for sv */
Cyril Bonté474be412010-10-12 00:14:36 +02004725
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004726 appctx->ctx.stats.px_st = STAT_PX_ST_BE;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004727 /* fall through */
Cyril Bonté70be45d2010-10-12 00:14:35 +02004728
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004729 case STAT_PX_ST_BE:
4730 /* print the backend */
Willy Tarreau0deb85a2016-01-08 15:33:18 +01004731 if (stats_dump_be_stats(si, px, flags)) {
Willy Tarreaubc18da12015-03-13 14:00:47 +01004732 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004733 si_applet_cant_put(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004734 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01004735 }
4736 }
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01004737
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004738 appctx->ctx.stats.px_st = STAT_PX_ST_END;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004739 /* fall through */
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01004740
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004741 case STAT_PX_ST_END:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004742 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004743 stats_dump_html_px_end(si, px);
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 }
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004748 }
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_FIN;
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_FIN:
4754 return 1;
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01004755
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004756 default:
4757 /* unknown state, we should put an abort() here ! */
4758 return 1;
4759 }
4760}
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01004761
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004762/* Dumps the HTTP stats head block to the trash for and uses the per-uri
4763 * parameters <uri>. The caller is responsible for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004764 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004765static void stats_dump_html_head(struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004766{
4767 /* WARNING! This must fit in the first buffer !!! */
4768 chunk_appendf(&trash,
4769 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n"
4770 "\"http://www.w3.org/TR/html4/loose.dtd\">\n"
4771 "<html><head><title>Statistics Report for " PRODUCT_NAME "%s%s</title>\n"
4772 "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n"
4773 "<style type=\"text/css\"><!--\n"
4774 "body {"
4775 " font-family: arial, helvetica, sans-serif;"
4776 " font-size: 12px;"
4777 " font-weight: normal;"
4778 " color: black;"
4779 " background: white;"
4780 "}\n"
4781 "th,td {"
4782 " font-size: 10px;"
4783 "}\n"
4784 "h1 {"
4785 " font-size: x-large;"
4786 " margin-bottom: 0.5em;"
4787 "}\n"
4788 "h2 {"
4789 " font-family: helvetica, arial;"
4790 " font-size: x-large;"
4791 " font-weight: bold;"
4792 " font-style: italic;"
4793 " color: #6020a0;"
4794 " margin-top: 0em;"
4795 " margin-bottom: 0em;"
4796 "}\n"
4797 "h3 {"
4798 " font-family: helvetica, arial;"
4799 " font-size: 16px;"
4800 " font-weight: bold;"
4801 " color: #b00040;"
4802 " background: #e8e8d0;"
4803 " margin-top: 0em;"
4804 " margin-bottom: 0em;"
4805 "}\n"
4806 "li {"
4807 " margin-top: 0.25em;"
4808 " margin-right: 2em;"
4809 "}\n"
4810 ".hr {margin-top: 0.25em;"
4811 " border-color: black;"
4812 " border-bottom-style: solid;"
4813 "}\n"
4814 ".titre {background: #20D0D0;color: #000000; font-weight: bold; text-align: center;}\n"
4815 ".total {background: #20D0D0;color: #ffff80;}\n"
4816 ".frontend {background: #e8e8d0;}\n"
4817 ".socket {background: #d0d0d0;}\n"
4818 ".backend {background: #e8e8d0;}\n"
Simon Horman837bfa72015-04-23 14:51:27 +09004819 ".active_down {background: #ff9090;}\n"
4820 ".active_going_up {background: #ffd020;}\n"
4821 ".active_going_down {background: #ffffa0;}\n"
4822 ".active_up {background: #c0ffc0;}\n"
4823 ".active_nolb {background: #20a0ff;}\n"
4824 ".active_draining {background: #20a0FF;}\n"
4825 ".active_no_check {background: #e0e0e0;}\n"
4826 ".backup_down {background: #ff9090;}\n"
4827 ".backup_going_up {background: #ff80ff;}\n"
4828 ".backup_going_down {background: #c060ff;}\n"
4829 ".backup_up {background: #b0d0ff;}\n"
4830 ".backup_nolb {background: #90b0e0;}\n"
4831 ".backup_draining {background: #cc9900;}\n"
4832 ".backup_no_check {background: #e0e0e0;}\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004833 ".maintain {background: #c07820;}\n"
4834 ".rls {letter-spacing: 0.2em; margin-right: 1px;}\n" /* right letter spacing (used for grouping digits) */
4835 "\n"
4836 "a.px:link {color: #ffff40; text-decoration: none;}"
4837 "a.px:visited {color: #ffff40; text-decoration: none;}"
4838 "a.px:hover {color: #ffffff; text-decoration: none;}"
4839 "a.lfsb:link {color: #000000; text-decoration: none;}"
4840 "a.lfsb:visited {color: #000000; text-decoration: none;}"
4841 "a.lfsb:hover {color: #505050; text-decoration: none;}"
4842 "\n"
4843 "table.tbl { border-collapse: collapse; border-style: none;}\n"
4844 "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"
4845 "table.tbl td.ac { text-align: center;}\n"
4846 "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}\n"
4847 "table.tbl th.pxname { background: #b00040; color: #ffff40; font-weight: bold; border-style: solid solid none solid; padding: 2px 3px; white-space: nowrap;}\n"
4848 "table.tbl th.empty { border-style: none; empty-cells: hide; background: white;}\n"
4849 "table.tbl th.desc { background: white; border-style: solid solid none solid; text-align: left; padding: 2px 3px;}\n"
4850 "\n"
4851 "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n"
4852 "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n"
4853 "table.lgd td.noborder { border-style: none; padding: 2px; white-space: nowrap;}\n"
Willy Tarreau466c9b52012-12-23 02:25:03 +01004854 "table.det { border-collapse: collapse; border-style: none; }\n"
4855 "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 +01004856 "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 +01004857 "u {text-decoration:none; border-bottom: 1px dotted black;}\n"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02004858 "div.tips {\n"
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01004859 " display:block;\n"
4860 " visibility:hidden;\n"
4861 " z-index:2147483647;\n"
4862 " position:absolute;\n"
4863 " padding:2px 4px 3px;\n"
4864 " background:#f0f060; color:#000000;\n"
4865 " border:1px solid #7040c0;\n"
4866 " white-space:nowrap;\n"
4867 " font-style:normal;font-size:11px;font-weight:normal;\n"
4868 " -moz-border-radius:3px;-webkit-border-radius:3px;border-radius:3px;\n"
4869 " -moz-box-shadow:gray 2px 2px 3px;-webkit-box-shadow:gray 2px 2px 3px;box-shadow:gray 2px 2px 3px;\n"
4870 "}\n"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02004871 "u:hover div.tips {visibility:visible;}\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004872 "-->\n"
4873 "</style></head>\n",
4874 (uri->flags & ST_SHNODE) ? " on " : "",
4875 (uri->flags & ST_SHNODE) ? (uri->node ? uri->node : global.node) : ""
4876 );
4877}
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01004878
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004879/* Dumps the HTML stats information block to the trash for and uses the state from
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004880 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004881 * for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004882 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004883static void stats_dump_html_info(struct stream_interface *si, struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004884{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004885 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004886 unsigned int up = (now.tv_sec - start_date.tv_sec);
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004887 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01004888
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004889 /* WARNING! this has to fit the first packet too.
4890 * We are around 3.5 kB, add adding entries will
4891 * become tricky if we want to support 4kB buffers !
4892 */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004893 chunk_appendf(&trash,
4894 "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">"
4895 PRODUCT_NAME "%s</a></h1>\n"
4896 "<h2>Statistics Report for pid %d%s%s%s%s</h2>\n"
4897 "<hr width=\"100%%\" class=\"hr\">\n"
4898 "<h3>&gt; General process information</h3>\n"
4899 "<table border=0><tr><td align=\"left\" nowrap width=\"1%%\">\n"
4900 "<p><b>pid = </b> %d (process #%d, nbproc = %d)<br>\n"
4901 "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n"
4902 "<b>system limits:</b> memmax = %s%s; ulimit-n = %d<br>\n"
4903 "<b>maxsock = </b> %d; <b>maxconn = </b> %d; <b>maxpipes = </b> %d<br>\n"
4904 "current conns = %d; current pipes = %d/%d; conn rate = %d/sec<br>\n"
4905 "Running tasks: %d/%d; idle = %d %%<br>\n"
4906 "</td><td align=\"center\" nowrap>\n"
4907 "<table class=\"lgd\"><tr>\n"
Simon Horman837bfa72015-04-23 14:51:27 +09004908 "<td class=\"active_up\">&nbsp;</td><td class=\"noborder\">active UP </td>"
4909 "<td class=\"backup_up\">&nbsp;</td><td class=\"noborder\">backup UP </td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004910 "</tr><tr>\n"
Simon Horman837bfa72015-04-23 14:51:27 +09004911 "<td class=\"active_going_down\"></td><td class=\"noborder\">active UP, going down </td>"
4912 "<td class=\"backup_going_down\"></td><td class=\"noborder\">backup UP, going down </td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004913 "</tr><tr>\n"
Simon Horman837bfa72015-04-23 14:51:27 +09004914 "<td class=\"active_going_up\"></td><td class=\"noborder\">active DOWN, going up </td>"
4915 "<td class=\"backup_going_up\"></td><td class=\"noborder\">backup DOWN, going up </td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004916 "</tr><tr>\n"
Simon Horman837bfa72015-04-23 14:51:27 +09004917 "<td class=\"active_down\"></td><td class=\"noborder\">active or backup DOWN &nbsp;</td>"
4918 "<td class=\"active_no_check\"></td><td class=\"noborder\">not checked </td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004919 "</tr><tr>\n"
4920 "<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 -07004921 "</tr><tr>\n"
Simon Horman837bfa72015-04-23 14:51:27 +09004922 "<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 +01004923 "</tr></table>\n"
Willy Tarreau6b7764a2013-12-04 00:43:21 +01004924 "Note: \"NOLB\"/\"DRAIN\" = UP with load-balancing disabled."
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004925 "</td>"
4926 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
4927 "<b>Display option:</b><ul style=\"margin-top: 0.25em;\">"
4928 "",
4929 (uri->flags & ST_HIDEVER) ? "" : (STATS_VERSION_STRING),
4930 pid, (uri->flags & ST_SHNODE) ? " on " : "",
4931 (uri->flags & ST_SHNODE) ? (uri->node ? uri->node : global.node) : "",
4932 (uri->flags & ST_SHDESC) ? ": " : "",
4933 (uri->flags & ST_SHDESC) ? (uri->desc ? uri->desc : global.desc) : "",
4934 pid, relative_pid, global.nbproc,
4935 up / 86400, (up % 86400) / 3600,
4936 (up % 3600) / 60, (up % 60),
4937 global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited",
4938 global.rlimit_memmax ? " MB" : "",
4939 global.rlimit_nofile,
4940 global.maxsock, global.maxconn, global.maxpipes,
4941 actconn, pipes_used, pipes_used+pipes_free, read_freq_ctr(&global.conn_per_sec),
4942 run_queue_cur, nb_tasks_cur, idle_pct
4943 );
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004944
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004945 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Willy Tarreau4e4292b2014-11-28 12:18:45 +01004946 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 +01004947 scope_txt[appctx->ctx.stats.scope_len] = '\0';
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004948
4949 chunk_appendf(&trash,
Jeff Buchbinder2dbbf4d2014-08-29 15:10:08 -05004950 "<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 +01004951 (appctx->ctx.stats.scope_len > 0) ? scope_txt : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004952 STAT_SCOPE_TXT_MAXLEN);
4953
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004954 /* 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 +02004955 scope_txt[0] = 0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004956 if (appctx->ctx.stats.scope_len) {
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004957 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau4e4292b2014-11-28 12:18:45 +01004958 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 +01004959 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004960 }
4961
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004962 if (appctx->ctx.stats.flags & STAT_HIDE_DOWN)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004963 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004964 "<li><a href=\"%s%s%s%s\">Show all servers</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004965 uri->uri_prefix,
4966 "",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004967 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004968 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004969 else
4970 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004971 "<li><a href=\"%s%s%s%s\">Hide 'DOWN' servers</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004972 uri->uri_prefix,
4973 ";up",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004974 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004975 scope_txt);
Willy Tarreau91861262007-10-17 17:06:05 +02004976
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004977 if (uri->refresh > 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004978 if (appctx->ctx.stats.flags & STAT_NO_REFRESH)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004979 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004980 "<li><a href=\"%s%s%s%s\">Enable refresh</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004981 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004982 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004983 "",
4984 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004985 else
4986 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004987 "<li><a href=\"%s%s%s%s\">Disable refresh</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004988 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004989 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004990 ";norefresh",
4991 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004992 }
Willy Tarreau55bb8452007-10-17 18:44:57 +02004993
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004994 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004995 "<li><a href=\"%s%s%s%s\">Refresh now</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004996 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004997 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
4998 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004999 scope_txt);
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02005000
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005001 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02005002 "<li><a href=\"%s;csv%s%s\">CSV export</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005003 uri->uri_prefix,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02005004 (uri->refresh > 0) ? ";norefresh" : "",
5005 scope_txt);
Willy Tarreau4bab24d2007-11-30 18:16:29 +01005006
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005007 chunk_appendf(&trash,
5008 "</ul></td>"
5009 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
5010 "<b>External resources:</b><ul style=\"margin-top: 0.25em;\">\n"
5011 "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>\n"
5012 "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>\n"
5013 "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>\n"
5014 "</ul>"
5015 "</td>"
5016 "</tr></table>\n"
5017 ""
5018 );
Willy Tarreau4bab24d2007-11-30 18:16:29 +01005019
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005020 if (appctx->ctx.stats.st_code) {
5021 switch (appctx->ctx.stats.st_code) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005022 case STAT_STATUS_DONE:
5023 chunk_appendf(&trash,
Simon Horman837bfa72015-04-23 14:51:27 +09005024 "<p><div class=active_up>"
Willy Tarreauba6be982013-04-19 12:16:55 +02005025 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005026 "Action processed successfully."
Willy Tarreauba6be982013-04-19 12:16:55 +02005027 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005028 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
5029 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02005030 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005031 break;
5032 case STAT_STATUS_NONE:
5033 chunk_appendf(&trash,
Simon Horman837bfa72015-04-23 14:51:27 +09005034 "<p><div class=active_going_down>"
Willy Tarreauba6be982013-04-19 12:16:55 +02005035 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005036 "Nothing has changed."
Willy Tarreauba6be982013-04-19 12:16:55 +02005037 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005038 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
5039 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02005040 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005041 break;
5042 case STAT_STATUS_PART:
5043 chunk_appendf(&trash,
Simon Horman837bfa72015-04-23 14:51:27 +09005044 "<p><div class=active_going_down>"
Willy Tarreauba6be982013-04-19 12:16:55 +02005045 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005046 "Action partially processed.<br>"
5047 "Some server names are probably unknown or ambiguous (duplicated names in the backend)."
Willy Tarreauba6be982013-04-19 12:16:55 +02005048 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005049 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
5050 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02005051 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005052 break;
5053 case STAT_STATUS_ERRP:
5054 chunk_appendf(&trash,
Simon Horman837bfa72015-04-23 14:51:27 +09005055 "<p><div class=active_down>"
Willy Tarreauba6be982013-04-19 12:16:55 +02005056 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005057 "Action not processed because of invalid parameters."
5058 "<ul>"
5059 "<li>The action is maybe unknown.</li>"
5060 "<li>The backend name is probably unknown or ambiguous (duplicated names).</li>"
5061 "<li>Some server names are probably unknown or ambiguous (duplicated names in the backend).</li>"
5062 "</ul>"
Willy Tarreauba6be982013-04-19 12:16:55 +02005063 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005064 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
5065 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02005066 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005067 break;
5068 case STAT_STATUS_EXCD:
5069 chunk_appendf(&trash,
Simon Horman837bfa72015-04-23 14:51:27 +09005070 "<p><div class=active_down>"
Willy Tarreauba6be982013-04-19 12:16:55 +02005071 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005072 "<b>Action not processed : the buffer couldn't store all the data.<br>"
5073 "You should retry with less servers at a time.</b>"
Willy Tarreauba6be982013-04-19 12:16:55 +02005074 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005075 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
5076 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02005077 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005078 break;
5079 case STAT_STATUS_DENY:
5080 chunk_appendf(&trash,
Simon Horman837bfa72015-04-23 14:51:27 +09005081 "<p><div class=active_down>"
Willy Tarreauba6be982013-04-19 12:16:55 +02005082 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005083 "<b>Action denied.</b>"
Willy Tarreauba6be982013-04-19 12:16:55 +02005084 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005085 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
5086 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02005087 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005088 break;
5089 default:
5090 chunk_appendf(&trash,
Simon Horman837bfa72015-04-23 14:51:27 +09005091 "<p><div class=active_no_check>"
Willy Tarreauba6be982013-04-19 12:16:55 +02005092 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005093 "Unexpected result."
Willy Tarreauba6be982013-04-19 12:16:55 +02005094 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005095 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
5096 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02005097 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005098 }
5099 chunk_appendf(&trash, "<p>\n");
5100 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005101}
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01005102
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01005103/* Dumps the HTML stats trailer block to the trash. The caller is responsible
5104 * for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005105 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01005106static void stats_dump_html_end()
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005107{
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005108 chunk_appendf(&trash, "</body></html>\n");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005109}
Willy Tarreau7f062c42009-03-05 18:43:00 +01005110
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01005111/* This function dumps statistics onto the stream interface's read buffer in
5112 * either CSV or HTML format. <uri> contains some HTML-specific parameters that
Willy Tarreau306f8302013-07-08 15:53:06 +02005113 * are ignored for CSV format (hence <uri> may be NULL there). It returns 0 if
5114 * 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 +02005115 * and the stream must be closed, or -1 in case of any error. This function is
Willy Tarreau306f8302013-07-08 15:53:06 +02005116 * used by both the CLI and the HTTP handlers.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005117 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01005118static int stats_dump_stat_to_buffer(struct stream_interface *si, struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005119{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005120 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005121 struct channel *rep = si_ic(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005122 struct proxy *px;
Willy Tarreau7f062c42009-03-05 18:43:00 +01005123
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005124 chunk_reset(&trash);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02005125
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005126 switch (appctx->st2) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005127 case STAT_ST_INIT:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005128 appctx->st2 = STAT_ST_HEAD; /* let's start producing data */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005129 /* fall through */
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005130
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005131 case STAT_ST_HEAD:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005132 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01005133 stats_dump_html_head(uri);
Willy Tarreau1e62df92016-01-11 18:57:53 +01005134 else if (!(appctx->ctx.stats.flags & STAT_FMT_TYPED))
Willy Tarreau354898b2012-12-23 18:15:23 +01005135 stats_dump_csv_header();
Willy Tarreaud9b587f2010-02-26 10:05:55 +01005136
Willy Tarreaubc18da12015-03-13 14:00:47 +01005137 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02005138 si_applet_cant_put(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005139 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01005140 }
Willy Tarreauae526782010-03-04 20:34:23 +01005141
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005142 appctx->st2 = STAT_ST_INFO;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005143 /* fall through */
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01005144
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005145 case STAT_ST_INFO:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005146 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01005147 stats_dump_html_info(si, uri);
Willy Tarreaubc18da12015-03-13 14:00:47 +01005148 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02005149 si_applet_cant_put(si);
Willy Tarreau91861262007-10-17 17:06:05 +02005150 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01005151 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005152 }
Willy Tarreau91861262007-10-17 17:06:05 +02005153
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005154 appctx->ctx.stats.px = proxy;
5155 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
5156 appctx->st2 = STAT_ST_LIST;
Willy Tarreau91861262007-10-17 17:06:05 +02005157 /* fall through */
5158
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005159 case STAT_ST_LIST:
5160 /* dump proxies */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005161 while (appctx->ctx.stats.px) {
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01005162 if (buffer_almost_full(rep->buf)) {
Willy Tarreaufe127932015-04-21 19:23:39 +02005163 si_applet_cant_put(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005164 return 0;
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01005165 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005166
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005167 px = appctx->ctx.stats.px;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005168 /* skip the disabled proxies, global frontend and non-networked ones */
5169 if (px->state != PR_STSTOPPED && px->uuid > 0 && (px->cap & (PR_CAP_FE | PR_CAP_BE)))
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01005170 if (stats_dump_proxy_to_buffer(si, px, uri) == 0)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005171 return 0;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005172
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005173 appctx->ctx.stats.px = px->next;
5174 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005175 }
5176 /* here, we just have reached the last proxy */
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005177
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005178 appctx->st2 = STAT_ST_END;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005179 /* fall through */
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005180
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005181 case STAT_ST_END:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005182 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01005183 stats_dump_html_end();
Willy Tarreaubc18da12015-03-13 14:00:47 +01005184 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02005185 si_applet_cant_put(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005186 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01005187 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005188 }
Willy Tarreau55058a72012-11-21 08:27:21 +01005189
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005190 appctx->st2 = STAT_ST_FIN;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005191 /* fall through */
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02005192
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005193 case STAT_ST_FIN:
5194 return 1;
Willy Tarreau55058a72012-11-21 08:27:21 +01005195
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005196 default:
5197 /* unknown state ! */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005198 appctx->st2 = STAT_ST_FIN;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005199 return -1;
5200 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005201}
Willy Tarreauae526782010-03-04 20:34:23 +01005202
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005203/* We reached the stats page through a POST request. The appctx is
5204 * expected to have already been allocated by the caller.
Willy Tarreau347a35d2013-11-22 17:51:09 +01005205 * Parse the posted data and enable/disable servers if necessary.
5206 * Returns 1 if request was parsed or zero if it needs more data.
5207 */
5208static int stats_process_http_post(struct stream_interface *si)
5209{
Willy Tarreau87b09662015-04-03 00:22:06 +02005210 struct stream *s = si_strm(si);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005211 struct appctx *appctx = objt_appctx(si->end);
Willy Tarreau347a35d2013-11-22 17:51:09 +01005212
5213 struct proxy *px = NULL;
5214 struct server *sv = NULL;
5215
5216 char key[LINESIZE];
5217 int action = ST_ADM_ACTION_NONE;
5218 int reprocess = 0;
5219
5220 int total_servers = 0;
5221 int altered_servers = 0;
5222
5223 char *first_param, *cur_param, *next_param, *end_params;
5224 char *st_cur_param = NULL;
5225 char *st_next_param = NULL;
5226
5227 struct chunk *temp;
5228 int reql;
5229
5230 temp = get_trash_chunk();
Willy Tarreaueee5b512015-04-03 23:46:31 +02005231 if (temp->size < s->txn->req.body_len) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01005232 /* too large request */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005233 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005234 goto out;
5235 }
5236
Willy Tarreaueee5b512015-04-03 23:46:31 +02005237 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 +01005238 if (reql <= 0) {
5239 /* we need more data */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005240 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005241 return 0;
5242 }
5243
5244 first_param = temp->str;
5245 end_params = temp->str + reql;
5246 cur_param = next_param = end_params;
5247 *end_params = '\0';
5248
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005249 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005250
5251 /*
5252 * Parse the parameters in reverse order to only store the last value.
5253 * From the html form, the backend and the action are at the end.
5254 */
5255 while (cur_param > first_param) {
5256 char *value;
5257 int poffset, plen;
5258
5259 cur_param--;
5260
5261 if ((*cur_param == '&') || (cur_param == first_param)) {
5262 reprocess_servers:
5263 /* Parse the key */
5264 poffset = (cur_param != first_param ? 1 : 0);
5265 plen = next_param - cur_param + (cur_param == first_param ? 1 : 0);
5266 if ((plen > 0) && (plen <= sizeof(key))) {
5267 strncpy(key, cur_param + poffset, plen);
5268 key[plen - 1] = '\0';
5269 } else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005270 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005271 goto out;
5272 }
5273
5274 /* Parse the value */
5275 value = key;
5276 while (*value != '\0' && *value != '=') {
5277 value++;
5278 }
5279 if (*value == '=') {
5280 /* Ok, a value is found, we can mark the end of the key */
5281 *value++ = '\0';
5282 }
5283 if (url_decode(key) < 0 || url_decode(value) < 0)
5284 break;
5285
5286 /* Now we can check the key to see what to do */
5287 if (!px && (strcmp(key, "b") == 0)) {
Willy Tarreau9e0bb102015-05-26 11:24:42 +02005288 if ((px = proxy_be_by_name(value)) == NULL) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01005289 /* the backend name is unknown or ambiguous (duplicate names) */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005290 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005291 goto out;
5292 }
5293 }
5294 else if (!action && (strcmp(key, "action") == 0)) {
Willy Tarreaued7df902014-05-22 18:04:49 +02005295 if (strcmp(value, "ready") == 0) {
5296 action = ST_ADM_ACTION_READY;
5297 }
5298 else if (strcmp(value, "drain") == 0) {
5299 action = ST_ADM_ACTION_DRAIN;
5300 }
5301 else if (strcmp(value, "maint") == 0) {
5302 action = ST_ADM_ACTION_MAINT;
5303 }
5304 else if (strcmp(value, "shutdown") == 0) {
5305 action = ST_ADM_ACTION_SHUTDOWN;
5306 }
Willy Tarreau248a60e2014-05-23 14:59:48 +02005307 else if (strcmp(value, "dhlth") == 0) {
5308 action = ST_ADM_ACTION_DHLTH;
5309 }
5310 else if (strcmp(value, "ehlth") == 0) {
5311 action = ST_ADM_ACTION_EHLTH;
5312 }
5313 else if (strcmp(value, "hrunn") == 0) {
5314 action = ST_ADM_ACTION_HRUNN;
5315 }
5316 else if (strcmp(value, "hnolb") == 0) {
5317 action = ST_ADM_ACTION_HNOLB;
5318 }
5319 else if (strcmp(value, "hdown") == 0) {
5320 action = ST_ADM_ACTION_HDOWN;
5321 }
5322 else if (strcmp(value, "dagent") == 0) {
5323 action = ST_ADM_ACTION_DAGENT;
5324 }
5325 else if (strcmp(value, "eagent") == 0) {
5326 action = ST_ADM_ACTION_EAGENT;
5327 }
5328 else if (strcmp(value, "arunn") == 0) {
5329 action = ST_ADM_ACTION_ARUNN;
5330 }
5331 else if (strcmp(value, "adown") == 0) {
5332 action = ST_ADM_ACTION_ADOWN;
5333 }
Willy Tarreaued7df902014-05-22 18:04:49 +02005334 /* else these are the old supported methods */
5335 else if (strcmp(value, "disable") == 0) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01005336 action = ST_ADM_ACTION_DISABLE;
5337 }
5338 else if (strcmp(value, "enable") == 0) {
5339 action = ST_ADM_ACTION_ENABLE;
5340 }
5341 else if (strcmp(value, "stop") == 0) {
5342 action = ST_ADM_ACTION_STOP;
5343 }
5344 else if (strcmp(value, "start") == 0) {
5345 action = ST_ADM_ACTION_START;
5346 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01005347 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005348 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005349 goto out;
5350 }
5351 }
5352 else if (strcmp(key, "s") == 0) {
5353 if (!(px && action)) {
5354 /*
5355 * Indicates that we'll need to reprocess the parameters
5356 * as soon as backend and action are known
5357 */
5358 if (!reprocess) {
5359 st_cur_param = cur_param;
5360 st_next_param = next_param;
5361 }
5362 reprocess = 1;
5363 }
5364 else if ((sv = findserver(px, value)) != NULL) {
5365 switch (action) {
5366 case ST_ADM_ACTION_DISABLE:
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02005367 if (!(sv->admin & SRV_ADMF_FMAINT)) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01005368 altered_servers++;
5369 total_servers++;
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02005370 srv_set_admin_flag(sv, SRV_ADMF_FMAINT);
Willy Tarreau347a35d2013-11-22 17:51:09 +01005371 }
5372 break;
5373 case ST_ADM_ACTION_ENABLE:
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02005374 if (sv->admin & SRV_ADMF_FMAINT) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01005375 altered_servers++;
5376 total_servers++;
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02005377 srv_clr_admin_flag(sv, SRV_ADMF_FMAINT);
Willy Tarreau347a35d2013-11-22 17:51:09 +01005378 }
5379 break;
5380 case ST_ADM_ACTION_STOP:
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02005381 if (!(sv->admin & SRV_ADMF_FDRAIN)) {
5382 srv_set_admin_flag(sv, SRV_ADMF_FDRAIN);
5383 altered_servers++;
5384 total_servers++;
5385 }
5386 break;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005387 case ST_ADM_ACTION_START:
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02005388 if (sv->admin & SRV_ADMF_FDRAIN) {
5389 srv_clr_admin_flag(sv, SRV_ADMF_FDRAIN);
5390 altered_servers++;
5391 total_servers++;
5392 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01005393 break;
Willy Tarreau248a60e2014-05-23 14:59:48 +02005394 case ST_ADM_ACTION_DHLTH:
5395 if (sv->check.state & CHK_ST_CONFIGURED) {
5396 sv->check.state &= ~CHK_ST_ENABLED;
5397 altered_servers++;
5398 total_servers++;
5399 }
5400 break;
5401 case ST_ADM_ACTION_EHLTH:
5402 if (sv->check.state & CHK_ST_CONFIGURED) {
5403 sv->check.state |= CHK_ST_ENABLED;
5404 altered_servers++;
5405 total_servers++;
5406 }
5407 break;
5408 case ST_ADM_ACTION_HRUNN:
5409 if (!(sv->track)) {
5410 sv->check.health = sv->check.rise + sv->check.fall - 1;
5411 srv_set_running(sv, "changed from Web interface");
5412 altered_servers++;
5413 total_servers++;
5414 }
5415 break;
5416 case ST_ADM_ACTION_HNOLB:
5417 if (!(sv->track)) {
5418 sv->check.health = sv->check.rise + sv->check.fall - 1;
5419 srv_set_stopping(sv, "changed from Web interface");
5420 altered_servers++;
5421 total_servers++;
5422 }
5423 break;
5424 case ST_ADM_ACTION_HDOWN:
5425 if (!(sv->track)) {
5426 sv->check.health = 0;
5427 srv_set_stopped(sv, "changed from Web interface");
5428 altered_servers++;
5429 total_servers++;
5430 }
5431 break;
5432 case ST_ADM_ACTION_DAGENT:
5433 if (sv->agent.state & CHK_ST_CONFIGURED) {
5434 sv->agent.state &= ~CHK_ST_ENABLED;
5435 altered_servers++;
5436 total_servers++;
5437 }
5438 break;
5439 case ST_ADM_ACTION_EAGENT:
5440 if (sv->agent.state & CHK_ST_CONFIGURED) {
5441 sv->agent.state |= CHK_ST_ENABLED;
5442 altered_servers++;
5443 total_servers++;
5444 }
5445 break;
5446 case ST_ADM_ACTION_ARUNN:
5447 if (sv->agent.state & CHK_ST_ENABLED) {
5448 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
5449 srv_set_running(sv, "changed from Web interface");
5450 altered_servers++;
5451 total_servers++;
5452 }
5453 break;
5454 case ST_ADM_ACTION_ADOWN:
5455 if (sv->agent.state & CHK_ST_ENABLED) {
5456 sv->agent.health = 0;
5457 srv_set_stopped(sv, "changed from Web interface");
5458 altered_servers++;
5459 total_servers++;
5460 }
5461 break;
Willy Tarreaued7df902014-05-22 18:04:49 +02005462 case ST_ADM_ACTION_READY:
5463 srv_adm_set_ready(sv);
5464 altered_servers++;
5465 total_servers++;
5466 break;
5467 case ST_ADM_ACTION_DRAIN:
5468 srv_adm_set_drain(sv);
5469 altered_servers++;
5470 total_servers++;
5471 break;
5472 case ST_ADM_ACTION_MAINT:
5473 srv_adm_set_maint(sv);
5474 altered_servers++;
5475 total_servers++;
5476 break;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005477 case ST_ADM_ACTION_SHUTDOWN:
5478 if (px->state != PR_STSTOPPED) {
Willy Tarreau87b09662015-04-03 00:22:06 +02005479 struct stream *sess, *sess_bck;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005480
5481 list_for_each_entry_safe(sess, sess_bck, &sv->actconns, by_srv)
5482 if (sess->srv_conn == sv)
Willy Tarreaue7dff022015-04-03 01:14:29 +02005483 stream_shutdown(sess, SF_ERR_KILLED);
Willy Tarreau347a35d2013-11-22 17:51:09 +01005484
5485 altered_servers++;
5486 total_servers++;
5487 }
5488 break;
5489 }
5490 } else {
5491 /* the server name is unknown or ambiguous (duplicate names) */
5492 total_servers++;
5493 }
5494 }
5495 if (reprocess && px && action) {
5496 /* Now, we know the backend and the action chosen by the user.
5497 * We can safely restart from the first server parameter
5498 * to reprocess them
5499 */
5500 cur_param = st_cur_param;
5501 next_param = st_next_param;
5502 reprocess = 0;
5503 goto reprocess_servers;
5504 }
5505
5506 next_param = cur_param;
5507 }
5508 }
5509
5510 if (total_servers == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005511 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005512 }
5513 else if (altered_servers == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005514 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005515 }
5516 else if (altered_servers == total_servers) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005517 appctx->ctx.stats.st_code = STAT_STATUS_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005518 }
5519 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005520 appctx->ctx.stats.st_code = STAT_STATUS_PART;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005521 }
5522 out:
5523 return 1;
5524}
5525
5526
5527static int stats_send_http_headers(struct stream_interface *si)
5528{
Willy Tarreau87b09662015-04-03 00:22:06 +02005529 struct stream *s = si_strm(si);
Willy Tarreau347a35d2013-11-22 17:51:09 +01005530 struct uri_auth *uri = s->be->uri_auth;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005531 struct appctx *appctx = objt_appctx(si->end);
Willy Tarreau347a35d2013-11-22 17:51:09 +01005532
5533 chunk_printf(&trash,
Willy Tarreau8b8995f2014-04-24 22:51:54 +02005534 "HTTP/1.1 200 OK\r\n"
Willy Tarreau347a35d2013-11-22 17:51:09 +01005535 "Cache-Control: no-cache\r\n"
5536 "Connection: close\r\n"
5537 "Content-Type: %s\r\n",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005538 (appctx->ctx.stats.flags & STAT_FMT_HTML) ? "text/html" : "text/plain");
Willy Tarreau347a35d2013-11-22 17:51:09 +01005539
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005540 if (uri->refresh > 0 && !(appctx->ctx.stats.flags & STAT_NO_REFRESH))
Willy Tarreau347a35d2013-11-22 17:51:09 +01005541 chunk_appendf(&trash, "Refresh: %d\r\n",
5542 uri->refresh);
5543
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005544 /* we don't send the CRLF in chunked mode, it will be sent with the first chunk's size */
5545
5546 if (appctx->ctx.stats.flags & STAT_CHUNKED)
5547 chunk_appendf(&trash, "Transfer-Encoding: chunked\r\n");
5548 else
5549 chunk_appendf(&trash, "\r\n");
Willy Tarreau347a35d2013-11-22 17:51:09 +01005550
Willy Tarreaueee5b512015-04-03 23:46:31 +02005551 s->txn->status = 200;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005552 s->logs.tv_request = now;
5553
Willy Tarreaubc18da12015-03-13 14:00:47 +01005554 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02005555 si_applet_cant_put(si);
Willy Tarreau347a35d2013-11-22 17:51:09 +01005556 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01005557 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01005558
5559 return 1;
5560}
5561
5562static int stats_send_http_redirect(struct stream_interface *si)
5563{
5564 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
Willy Tarreau87b09662015-04-03 00:22:06 +02005565 struct stream *s = si_strm(si);
Willy Tarreau347a35d2013-11-22 17:51:09 +01005566 struct uri_auth *uri = s->be->uri_auth;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005567 struct appctx *appctx = objt_appctx(si->end);
Willy Tarreau347a35d2013-11-22 17:51:09 +01005568
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005569 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Willy Tarreau347a35d2013-11-22 17:51:09 +01005570 scope_txt[0] = 0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005571 if (appctx->ctx.stats.scope_len) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01005572 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau4e4292b2014-11-28 12:18:45 +01005573 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 +01005574 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005575 }
5576
5577 /* We don't want to land on the posted stats page because a refresh will
5578 * repost the data. We don't want this to happen on accident so we redirect
5579 * the browse to the stats page with a GET.
5580 */
5581 chunk_printf(&trash,
5582 "HTTP/1.1 303 See Other\r\n"
5583 "Cache-Control: no-cache\r\n"
5584 "Content-Type: text/plain\r\n"
5585 "Connection: close\r\n"
5586 "Location: %s;st=%s%s%s%s\r\n"
Willy Tarreaufdfcc9d2016-01-26 13:57:29 +01005587 "Content-length: 0\r\n"
Willy Tarreau347a35d2013-11-22 17:51:09 +01005588 "\r\n",
5589 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005590 ((appctx->ctx.stats.st_code > STAT_STATUS_INIT) &&
5591 (appctx->ctx.stats.st_code < STAT_STATUS_SIZE) &&
5592 stat_status_codes[appctx->ctx.stats.st_code]) ?
5593 stat_status_codes[appctx->ctx.stats.st_code] :
Willy Tarreau347a35d2013-11-22 17:51:09 +01005594 stat_status_codes[STAT_STATUS_UNKN],
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005595 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
5596 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreau347a35d2013-11-22 17:51:09 +01005597 scope_txt);
5598
Willy Tarreaueee5b512015-04-03 23:46:31 +02005599 s->txn->status = 303;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005600 s->logs.tv_request = now;
5601
Willy Tarreaubc18da12015-03-13 14:00:47 +01005602 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02005603 si_applet_cant_put(si);
Willy Tarreau347a35d2013-11-22 17:51:09 +01005604 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01005605 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01005606
5607 return 1;
5608}
5609
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005610/* This I/O handler runs as an applet embedded in a stream interface. It is
5611 * used to send HTTP stats over a TCP socket. The mechanism is very simple.
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005612 * appctx->st0 contains the operation in progress (dump, done). The handler
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005613 * automatically unregisters itself once transfer is complete.
5614 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02005615static void http_stats_io_handler(struct appctx *appctx)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005616{
Willy Tarreau00a37f02015-04-13 12:05:19 +02005617 struct stream_interface *si = appctx->owner;
Willy Tarreau87b09662015-04-03 00:22:06 +02005618 struct stream *s = si_strm(si);
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005619 struct channel *req = si_oc(si);
5620 struct channel *res = si_ic(si);
Willy Tarreau55058a72012-11-21 08:27:21 +01005621
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005622 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
5623 goto out;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01005624
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005625 /* check that the output is not closed */
5626 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005627 appctx->st0 = STAT_HTTP_DONE;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005628
Willy Tarreau347a35d2013-11-22 17:51:09 +01005629 /* all states are processed in sequence */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005630 if (appctx->st0 == STAT_HTTP_HEAD) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01005631 if (stats_send_http_headers(si)) {
Willy Tarreaueee5b512015-04-03 23:46:31 +02005632 if (s->txn->meth == HTTP_METH_HEAD)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005633 appctx->st0 = STAT_HTTP_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005634 else
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005635 appctx->st0 = STAT_HTTP_DUMP;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005636 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01005637 }
5638
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005639 if (appctx->st0 == STAT_HTTP_DUMP) {
Willy Tarreau4e4292b2014-11-28 12:18:45 +01005640 unsigned int prev_len = si_ib(si)->i;
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005641 unsigned int data_len;
5642 unsigned int last_len;
Willy Tarreaucce36482014-04-24 20:26:41 +02005643 unsigned int last_fwd = 0;
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005644
5645 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
5646 /* One difficulty we're facing is that we must prevent
5647 * the input data from being automatically forwarded to
5648 * the output area. For this, we temporarily disable
5649 * forwarding on the channel.
5650 */
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005651 last_fwd = si_ic(si)->to_forward;
5652 si_ic(si)->to_forward = 0;
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005653 chunk_printf(&trash, "\r\n000000\r\n");
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005654 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02005655 si_applet_cant_put(si);
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005656 si_ic(si)->to_forward = last_fwd;
Willy Tarreau828824a2015-04-19 17:20:03 +02005657 goto out;
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005658 }
5659 }
5660
Willy Tarreau4e4292b2014-11-28 12:18:45 +01005661 data_len = si_ib(si)->i;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005662 if (stats_dump_stat_to_buffer(si, s->be->uri_auth))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005663 appctx->st0 = STAT_HTTP_DONE;
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005664
Willy Tarreau4e4292b2014-11-28 12:18:45 +01005665 last_len = si_ib(si)->i;
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005666
5667 /* Now we must either adjust or remove the chunk size. This is
5668 * not easy because the chunk size might wrap at the end of the
5669 * buffer, so we pretend we have nothing in the buffer, we write
5670 * the size, then restore the buffer's contents. Note that we can
5671 * only do that because no forwarding is scheduled on the stats
5672 * applet.
5673 */
5674 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
Willy Tarreau4e4292b2014-11-28 12:18:45 +01005675 si_ic(si)->total -= (last_len - prev_len);
5676 si_ib(si)->i -= (last_len - prev_len);
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005677
5678 if (last_len != data_len) {
5679 chunk_printf(&trash, "\r\n%06x\r\n", (last_len - data_len));
Willy Tarreaubc18da12015-03-13 14:00:47 +01005680 if (bi_putchk(si_ic(si), &trash) == -1)
Willy Tarreaufe127932015-04-21 19:23:39 +02005681 si_applet_cant_put(si);
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005682
Willy Tarreau4e4292b2014-11-28 12:18:45 +01005683 si_ic(si)->total += (last_len - data_len);
5684 si_ib(si)->i += (last_len - data_len);
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005685 }
5686 /* now re-enable forwarding */
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005687 channel_forward(si_ic(si), last_fwd);
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005688 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005689 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02005690
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005691 if (appctx->st0 == STAT_HTTP_POST) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01005692 if (stats_process_http_post(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005693 appctx->st0 = STAT_HTTP_LAST;
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005694 else if (si_oc(si)->flags & CF_SHUTR)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005695 appctx->st0 = STAT_HTTP_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005696 }
5697
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005698 if (appctx->st0 == STAT_HTTP_LAST) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01005699 if (stats_send_http_redirect(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005700 appctx->st0 = STAT_HTTP_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005701 }
5702
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005703 if (appctx->st0 == STAT_HTTP_DONE) {
5704 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
5705 chunk_printf(&trash, "\r\n0\r\n\r\n");
Willy Tarreaubc18da12015-03-13 14:00:47 +01005706 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02005707 si_applet_cant_put(si);
Willy Tarreau828824a2015-04-19 17:20:03 +02005708 goto out;
Willy Tarreaubc18da12015-03-13 14:00:47 +01005709 }
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005710 }
5711 /* eat the whole request */
Willy Tarreau4e4292b2014-11-28 12:18:45 +01005712 bo_skip(si_oc(si), si_ob(si)->o);
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005713 res->flags |= CF_READ_NULL;
5714 si_shutr(si);
5715 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01005716
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005717 if ((res->flags & CF_SHUTR) && (si->state == SI_ST_EST))
5718 si_shutw(si);
Willy Tarreau91861262007-10-17 17:06:05 +02005719
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005720 if (appctx->st0 == STAT_HTTP_DONE) {
Willy Tarreau96d44912013-11-22 12:25:24 +01005721 if ((req->flags & CF_SHUTW) && (si->state == SI_ST_EST)) {
5722 si_shutr(si);
5723 res->flags |= CF_READ_NULL;
5724 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005725 }
Willy Tarreau828824a2015-04-19 17:20:03 +02005726 out:
Willy Tarreaud4da1962015-04-20 01:31:23 +02005727 /* just to make gcc happy */ ;
Willy Tarreau91861262007-10-17 17:06:05 +02005728}
5729
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005730
Willy Tarreau909d5172012-11-26 03:04:41 +01005731static inline const char *get_conn_ctrl_name(const struct connection *conn)
5732{
Willy Tarreau3c728722014-01-23 13:50:42 +01005733 if (!conn_ctrl_ready(conn))
Willy Tarreau909d5172012-11-26 03:04:41 +01005734 return "NONE";
5735 return conn->ctrl->name;
5736}
5737
5738static inline const char *get_conn_xprt_name(const struct connection *conn)
5739{
5740 static char ptr[17];
5741
Willy Tarreauaad69382014-01-23 14:21:42 +01005742 if (!conn_xprt_ready(conn))
Willy Tarreau909d5172012-11-26 03:04:41 +01005743 return "NONE";
5744
5745 if (conn->xprt == &raw_sock)
5746 return "RAW";
5747
5748#ifdef USE_OPENSSL
5749 if (conn->xprt == &ssl_sock)
5750 return "SSL";
5751#endif
5752 snprintf(ptr, sizeof(ptr), "%p", conn->xprt);
5753 return ptr;
5754}
5755
5756static inline const char *get_conn_data_name(const struct connection *conn)
5757{
5758 static char ptr[17];
5759
5760 if (!conn->data)
5761 return "NONE";
5762
5763 if (conn->data == &sess_conn_cb)
5764 return "SESS";
5765
5766 if (conn->data == &si_conn_cb)
5767 return "STRM";
5768
5769 if (conn->data == &check_conn_cb)
5770 return "CHCK";
5771
5772 snprintf(ptr, sizeof(ptr), "%p", conn->data);
5773 return ptr;
5774}
5775
Willy Tarreau87b09662015-04-03 00:22:06 +02005776/* This function dumps a complete stream state onto the stream interface's
5777 * read buffer. The stream has to be set in sess->target. It returns
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01005778 * 0 if the output buffer is full and it needs to be called again, otherwise
5779 * non-zero. It is designed to be called from stats_dump_sess_to_buffer() below.
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005780 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005781static int stats_dump_full_sess_to_buffer(struct stream_interface *si, struct stream *sess)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005782{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005783 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005784 struct tm tm;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005785 extern const char *monthname[12];
5786 char pn[INET6_ADDRSTRLEN];
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005787 struct connection *conn;
Willy Tarreau284ddbf2013-12-01 20:45:00 +01005788 struct appctx *tmpctx;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005789
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005790 chunk_reset(&trash);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005791
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005792 if (appctx->ctx.sess.section > 0 && appctx->ctx.sess.uid != sess->uniq_id) {
Willy Tarreau87b09662015-04-03 00:22:06 +02005793 /* stream changed, no need to go any further */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005794 chunk_appendf(&trash, " *** session terminated while we were watching it ***\n");
Willy Tarreaubc18da12015-03-13 14:00:47 +01005795 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02005796 si_applet_cant_put(si);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005797 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01005798 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005799 appctx->ctx.sess.uid = 0;
5800 appctx->ctx.sess.section = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005801 return 1;
5802 }
5803
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005804 switch (appctx->ctx.sess.section) {
Willy Tarreau87b09662015-04-03 00:22:06 +02005805 case 0: /* main status of the stream */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005806 appctx->ctx.sess.uid = sess->uniq_id;
5807 appctx->ctx.sess.section = 1;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005808 /* fall through */
5809
5810 case 1:
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005811 get_localtime(sess->logs.accept_date.tv_sec, &tm);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005812 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005813 "%p: [%02d/%s/%04d:%02d:%02d:%02d.%06d] id=%u proto=%s",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005814 sess,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005815 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
5816 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(sess->logs.accept_date.tv_usec),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005817 sess->uniq_id,
Willy Tarreau666f5042015-06-17 19:49:52 +02005818 strm_li(sess) ? strm_li(sess)->proto->name : "?");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005819
Willy Tarreaud0d8da92015-04-04 02:10:38 +02005820 conn = objt_conn(strm_orig(sess));
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005821 switch (conn ? addr_to_str(&conn->addr.from, pn, sizeof(pn)) : AF_UNSPEC) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005822 case AF_INET:
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005823 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005824 chunk_appendf(&trash, " source=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005825 pn, get_host_port(&conn->addr.from));
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005826 break;
5827 case AF_UNIX:
Willy Tarreaud0d8da92015-04-04 02:10:38 +02005828 chunk_appendf(&trash, " source=unix:%d\n", strm_li(sess)->luid);
Emeric Brun837ca522010-10-22 16:19:01 +02005829 break;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005830 default:
5831 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005832 chunk_appendf(&trash, "\n");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005833 break;
5834 }
5835
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005836 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005837 " flags=0x%x, conn_retries=%d, srv_conn=%p, pend_pos=%p\n",
Willy Tarreauee28de02010-06-01 09:51:00 +02005838 sess->flags, sess->si[1].conn_retries, sess->srv_conn, sess->pend_pos);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005839
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005840 chunk_appendf(&trash,
Daniel Schultze90690c72012-03-23 10:53:36 -07005841 " frontend=%s (id=%u mode=%s), listener=%s (id=%u)",
Willy Tarreaud0d8da92015-04-04 02:10:38 +02005842 strm_fe(sess)->id, strm_fe(sess)->uuid, strm_fe(sess)->mode ? "http" : "tcp",
5843 strm_li(sess) ? strm_li(sess)->name ? strm_li(sess)->name : "?" : "?",
5844 strm_li(sess) ? strm_li(sess)->luid : 0);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005845
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005846 if (conn)
5847 conn_get_to_addr(conn);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005848
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005849 switch (conn ? addr_to_str(&conn->addr.to, pn, sizeof(pn)) : AF_UNSPEC) {
Daniel Schultze90690c72012-03-23 10:53:36 -07005850 case AF_INET:
5851 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005852 chunk_appendf(&trash, " addr=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005853 pn, get_host_port(&conn->addr.to));
Daniel Schultze90690c72012-03-23 10:53:36 -07005854 break;
5855 case AF_UNIX:
Willy Tarreaud0d8da92015-04-04 02:10:38 +02005856 chunk_appendf(&trash, " addr=unix:%d\n", strm_li(sess)->luid);
Daniel Schultze90690c72012-03-23 10:53:36 -07005857 break;
5858 default:
5859 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005860 chunk_appendf(&trash, "\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07005861 break;
5862 }
5863
Willy Tarreau50943332011-09-02 17:33:05 +02005864 if (sess->be->cap & PR_CAP_BE)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005865 chunk_appendf(&trash,
Daniel Schultze90690c72012-03-23 10:53:36 -07005866 " backend=%s (id=%u mode=%s)",
Willy Tarreau50943332011-09-02 17:33:05 +02005867 sess->be->id,
Daniel Schultze90690c72012-03-23 10:53:36 -07005868 sess->be->uuid, sess->be->mode ? "http" : "tcp");
5869 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005870 chunk_appendf(&trash, " backend=<NONE> (id=-1 mode=-)");
Daniel Schultze90690c72012-03-23 10:53:36 -07005871
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005872 conn = objt_conn(sess->si[1].end);
5873 if (conn)
5874 conn_get_from_addr(conn);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005875
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005876 switch (conn ? addr_to_str(&conn->addr.from, pn, sizeof(pn)) : AF_UNSPEC) {
Daniel Schultze90690c72012-03-23 10:53:36 -07005877 case AF_INET:
5878 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005879 chunk_appendf(&trash, " addr=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005880 pn, get_host_port(&conn->addr.from));
Daniel Schultze90690c72012-03-23 10:53:36 -07005881 break;
5882 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005883 chunk_appendf(&trash, " addr=unix\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07005884 break;
5885 default:
5886 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005887 chunk_appendf(&trash, "\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07005888 break;
5889 }
5890
5891 if (sess->be->cap & PR_CAP_BE)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005892 chunk_appendf(&trash,
Daniel Schultze90690c72012-03-23 10:53:36 -07005893 " server=%s (id=%u)",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005894 objt_server(sess->target) ? objt_server(sess->target)->id : "<none>",
5895 objt_server(sess->target) ? objt_server(sess->target)->puid : 0);
Willy Tarreau50943332011-09-02 17:33:05 +02005896 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005897 chunk_appendf(&trash, " server=<NONE> (id=-1)");
Daniel Schultze90690c72012-03-23 10:53:36 -07005898
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005899 if (conn)
5900 conn_get_to_addr(conn);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005901
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005902 switch (conn ? addr_to_str(&conn->addr.to, pn, sizeof(pn)) : AF_UNSPEC) {
Daniel Schultze90690c72012-03-23 10:53:36 -07005903 case AF_INET:
5904 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005905 chunk_appendf(&trash, " addr=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005906 pn, get_host_port(&conn->addr.to));
Daniel Schultze90690c72012-03-23 10:53:36 -07005907 break;
5908 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005909 chunk_appendf(&trash, " addr=unix\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07005910 break;
5911 default:
5912 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005913 chunk_appendf(&trash, "\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07005914 break;
5915 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005916
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005917 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005918 " task=%p (state=0x%02x nice=%d calls=%d exp=%s%s",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005919 sess->task,
5920 sess->task->state,
5921 sess->task->nice, sess->task->calls,
5922 sess->task->expire ?
5923 tick_is_expired(sess->task->expire, now_ms) ? "<PAST>" :
5924 human_time(TICKS_TO_MS(sess->task->expire - now_ms),
5925 TICKS_TO_MS(1000)) : "<NEVER>",
5926 task_in_rq(sess->task) ? ", running" : "");
5927
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005928 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005929 " age=%s)\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005930 human_time(now.tv_sec - sess->logs.accept_date.tv_sec, 1));
5931
Willy Tarreaueee5b512015-04-03 23:46:31 +02005932 if (sess->txn)
5933 chunk_appendf(&trash,
Willy Tarreau98410192014-11-26 18:05:38 +01005934 " 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 +02005935 sess->txn, sess->txn->flags, sess->txn->meth, sess->txn->status,
5936 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 +01005937
5938 chunk_appendf(&trash,
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02005939 " si[0]=%p (state=%s flags=0x%02x endp0=%s:%p exp=%s, et=0x%03x)\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005940 &sess->si[0],
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005941 si_state_str(sess->si[0].state),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005942 sess->si[0].flags,
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005943 obj_type_name(sess->si[0].end),
5944 obj_base_ptr(sess->si[0].end),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005945 sess->si[0].exp ?
5946 tick_is_expired(sess->si[0].exp, now_ms) ? "<PAST>" :
5947 human_time(TICKS_TO_MS(sess->si[0].exp - now_ms),
5948 TICKS_TO_MS(1000)) : "<NEVER>",
5949 sess->si[0].err_type);
5950
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005951 chunk_appendf(&trash,
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02005952 " si[1]=%p (state=%s flags=0x%02x endp1=%s:%p exp=%s, et=0x%03x)\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005953 &sess->si[1],
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005954 si_state_str(sess->si[1].state),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005955 sess->si[1].flags,
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005956 obj_type_name(sess->si[1].end),
5957 obj_base_ptr(sess->si[1].end),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005958 sess->si[1].exp ?
5959 tick_is_expired(sess->si[1].exp, now_ms) ? "<PAST>" :
5960 human_time(TICKS_TO_MS(sess->si[1].exp - now_ms),
5961 TICKS_TO_MS(1000)) : "<NEVER>",
5962 sess->si[1].err_type);
5963
Willy Tarreau284ddbf2013-12-01 20:45:00 +01005964 if ((conn = objt_conn(sess->si[0].end)) != NULL) {
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005965 chunk_appendf(&trash,
5966 " co0=%p ctrl=%s xprt=%s data=%s target=%s:%p\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005967 conn,
5968 get_conn_ctrl_name(conn),
5969 get_conn_xprt_name(conn),
5970 get_conn_data_name(conn),
5971 obj_type_name(conn->target),
5972 obj_base_ptr(conn->target));
Willy Tarreau909d5172012-11-26 03:04:41 +01005973
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005974 chunk_appendf(&trash,
Willy Tarreau16f649c2014-01-25 19:10:48 +01005975 " flags=0x%08x fd=%d fd.state=%02x fd.cache=%d updt=%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005976 conn->flags,
5977 conn->t.sock.fd,
Willy Tarreau69a41fa2014-01-20 11:02:59 +01005978 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].state : 0,
Willy Tarreau15a4dec2014-01-20 11:09:39 +01005979 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].cache : 0,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005980 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].updated : 0);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005981 }
Willy Tarreau284ddbf2013-12-01 20:45:00 +01005982 else if ((tmpctx = objt_appctx(sess->si[0].end)) != NULL) {
5983 chunk_appendf(&trash,
5984 " app0=%p st0=%d st1=%d st2=%d applet=%s\n",
5985 tmpctx,
5986 tmpctx->st0,
5987 tmpctx->st1,
5988 tmpctx->st2,
5989 tmpctx->applet->name);
5990 }
Willy Tarreaubc174aa2012-11-19 16:10:32 +01005991
Willy Tarreau284ddbf2013-12-01 20:45:00 +01005992 if ((conn = objt_conn(sess->si[1].end)) != NULL) {
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005993 chunk_appendf(&trash,
5994 " co1=%p ctrl=%s xprt=%s data=%s target=%s:%p\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005995 conn,
5996 get_conn_ctrl_name(conn),
5997 get_conn_xprt_name(conn),
5998 get_conn_data_name(conn),
5999 obj_type_name(conn->target),
6000 obj_base_ptr(conn->target));
Willy Tarreau909d5172012-11-26 03:04:41 +01006001
Willy Tarreaucf644ed2013-09-29 17:19:56 +02006002 chunk_appendf(&trash,
Willy Tarreauceeafb52016-01-25 15:27:17 +01006003 " flags=0x%08x fd=%d fd.state=%02x fd.cache=%d updt=%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006004 conn->flags,
6005 conn->t.sock.fd,
Willy Tarreau69a41fa2014-01-20 11:02:59 +01006006 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].state : 0,
Willy Tarreau15a4dec2014-01-20 11:09:39 +01006007 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].cache : 0,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006008 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].updated : 0);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02006009 }
Willy Tarreau284ddbf2013-12-01 20:45:00 +01006010 else if ((tmpctx = objt_appctx(sess->si[1].end)) != NULL) {
6011 chunk_appendf(&trash,
6012 " app1=%p st0=%d st1=%d st2=%d applet=%s\n",
6013 tmpctx,
6014 tmpctx->st0,
6015 tmpctx->st1,
6016 tmpctx->st2,
6017 tmpctx->applet->name);
6018 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006019
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006020 chunk_appendf(&trash,
Willy Tarreau909d5172012-11-26 03:04:41 +01006021 " req=%p (f=0x%06x an=0x%x pipe=%d tofwd=%d total=%lld)\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006022 " an_exp=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006023 &sess->req,
6024 sess->req.flags, sess->req.analysers,
6025 sess->req.pipe ? sess->req.pipe->data : 0,
6026 sess->req.to_forward, sess->req.total,
6027 sess->req.analyse_exp ?
6028 human_time(TICKS_TO_MS(sess->req.analyse_exp - now_ms),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006029 TICKS_TO_MS(1000)) : "<NEVER>");
6030
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006031 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006032 " rex=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006033 sess->req.rex ?
6034 human_time(TICKS_TO_MS(sess->req.rex - now_ms),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006035 TICKS_TO_MS(1000)) : "<NEVER>");
6036
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006037 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006038 " wex=%s\n"
Willy Tarreau909d5172012-11-26 03:04:41 +01006039 " buf=%p data=%p o=%d p=%d req.next=%d i=%d size=%d\n",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006040 sess->req.wex ?
6041 human_time(TICKS_TO_MS(sess->req.wex - now_ms),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006042 TICKS_TO_MS(1000)) : "<NEVER>",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006043 sess->req.buf,
6044 sess->req.buf->data, sess->req.buf->o,
6045 (int)(sess->req.buf->p - sess->req.buf->data),
Willy Tarreaueee5b512015-04-03 23:46:31 +02006046 sess->txn ? sess->txn->req.next : 0, sess->req.buf->i,
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006047 sess->req.buf->size);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006048
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006049 chunk_appendf(&trash,
Willy Tarreau909d5172012-11-26 03:04:41 +01006050 " res=%p (f=0x%06x an=0x%x pipe=%d tofwd=%d total=%lld)\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006051 " an_exp=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006052 &sess->res,
6053 sess->res.flags, sess->res.analysers,
6054 sess->res.pipe ? sess->res.pipe->data : 0,
6055 sess->res.to_forward, sess->res.total,
6056 sess->res.analyse_exp ?
6057 human_time(TICKS_TO_MS(sess->res.analyse_exp - now_ms),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006058 TICKS_TO_MS(1000)) : "<NEVER>");
6059
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006060 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006061 " rex=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006062 sess->res.rex ?
6063 human_time(TICKS_TO_MS(sess->res.rex - now_ms),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006064 TICKS_TO_MS(1000)) : "<NEVER>");
6065
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006066 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006067 " wex=%s\n"
Willy Tarreau909d5172012-11-26 03:04:41 +01006068 " buf=%p data=%p o=%d p=%d rsp.next=%d i=%d size=%d\n",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006069 sess->res.wex ?
6070 human_time(TICKS_TO_MS(sess->res.wex - now_ms),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006071 TICKS_TO_MS(1000)) : "<NEVER>",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006072 sess->res.buf,
6073 sess->res.buf->data, sess->res.buf->o,
6074 (int)(sess->res.buf->p - sess->res.buf->data),
Willy Tarreaueee5b512015-04-03 23:46:31 +02006075 sess->txn ? sess->txn->rsp.next : 0, sess->res.buf->i,
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006076 sess->res.buf->size);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006077
Willy Tarreaubc18da12015-03-13 14:00:47 +01006078 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02006079 si_applet_cant_put(si);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006080 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01006081 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006082
6083 /* use other states to dump the contents */
6084 }
6085 /* end of dump */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006086 appctx->ctx.sess.uid = 0;
6087 appctx->ctx.sess.section = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006088 return 1;
6089}
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006090
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02006091#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
6092static int stats_tlskeys_list(struct stream_interface *si) {
6093 struct appctx *appctx = __objt_appctx(si->end);
6094
6095 switch (appctx->st2) {
6096 case STAT_ST_INIT:
6097 /* Display the column headers. If the message cannot be sent,
6098 * quit the fucntion with returning 0. The function is called
6099 * later and restart at the state "STAT_ST_INIT".
6100 */
6101 chunk_reset(&trash);
6102 chunk_appendf(&trash, "# id (file)\n");
6103 if (bi_putchk(si_ic(si), &trash) == -1) {
6104 si_applet_cant_put(si);
6105 return 0;
6106 }
6107
6108 /* Now, we start the browsing of the references lists.
6109 * Note that the following call to LIST_ELEM return bad pointer. The only
6110 * avalaible field of this pointer is <list>. It is used with the function
6111 * tlskeys_list_get_next() for retruning the first avalaible entry
6112 */
6113 appctx->ctx.tlskeys.ref = LIST_ELEM(&tlskeys_reference, struct tls_keys_ref *, list);
6114 appctx->ctx.tlskeys.ref = tlskeys_list_get_next(appctx->ctx.tlskeys.ref, &tlskeys_reference);
6115
6116 appctx->st2 = STAT_ST_LIST;
6117 /* fall through */
6118
6119 case STAT_ST_LIST:
6120 while (appctx->ctx.tlskeys.ref) {
6121 chunk_reset(&trash);
6122
6123 chunk_appendf(&trash, "%d (%s)\n", appctx->ctx.tlskeys.ref->unique_id,
6124 appctx->ctx.tlskeys.ref->filename);
6125
6126 if (bi_putchk(si_ic(si), &trash) == -1) {
6127 /* let's try again later from this stream. We add ourselves into
6128 * this stream's users so that it can remove us upon termination.
6129 */
6130 si_applet_cant_put(si);
6131 return 0;
6132 }
6133
6134 /* get next list entry and check the end of the list */
6135 appctx->ctx.tlskeys.ref = tlskeys_list_get_next(appctx->ctx.tlskeys.ref, &tlskeys_reference);
6136 }
6137
6138 appctx->st2 = STAT_ST_FIN;
6139 /* fall through */
6140
6141 default:
6142 appctx->st2 = STAT_ST_FIN;
6143 return 1;
6144 }
6145 return 0;
6146}
6147#endif
6148
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006149static int stats_pats_list(struct stream_interface *si)
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006150{
6151 struct appctx *appctx = __objt_appctx(si->end);
6152
6153 switch (appctx->st2) {
6154 case STAT_ST_INIT:
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01006155 /* Display the column headers. If the message cannot be sent,
6156 * quit the fucntion with returning 0. The function is called
6157 * later and restart at the state "STAT_ST_INIT".
6158 */
6159 chunk_reset(&trash);
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01006160 chunk_appendf(&trash, "# id (file) description\n");
Willy Tarreaubc18da12015-03-13 14:00:47 +01006161 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02006162 si_applet_cant_put(si);
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01006163 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01006164 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006165
6166 /* Now, we start the browsing of the references lists.
6167 * Note that the following call to LIST_ELEM return bad pointer. The only
6168 * avalaible field of this pointer is <list>. It is used with the function
6169 * pat_list_get_next() for retruning the first avalaible entry
6170 */
6171 appctx->ctx.map.ref = LIST_ELEM(&pattern_reference, struct pat_ref *, list);
6172 appctx->ctx.map.ref = pat_list_get_next(appctx->ctx.map.ref, &pattern_reference,
6173 appctx->ctx.map.display_flags);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006174 appctx->st2 = STAT_ST_LIST;
6175 /* fall through */
6176
6177 case STAT_ST_LIST:
6178 while (appctx->ctx.map.ref) {
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006179 chunk_reset(&trash);
6180
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006181 /* Build messages. If the reference is used by another category than
6182 * the listed categorie, display the information in the massage.
6183 */
Thierry FOURNIERf7e04e92014-03-20 11:45:47 +01006184 chunk_appendf(&trash, "%d (%s) %s\n", appctx->ctx.map.ref->unique_id,
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01006185 appctx->ctx.map.ref->reference ? appctx->ctx.map.ref->reference : "",
6186 appctx->ctx.map.ref->display);
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01006187
Willy Tarreau2bb4a962014-11-28 11:11:05 +01006188 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau87b09662015-04-03 00:22:06 +02006189 /* let's try again later from this stream. We add ourselves into
6190 * this stream's users so that it can remove us upon termination.
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006191 */
Willy Tarreaufe127932015-04-21 19:23:39 +02006192 si_applet_cant_put(si);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006193 return 0;
6194 }
6195
6196 /* get next list entry and check the end of the list */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006197 appctx->ctx.map.ref = pat_list_get_next(appctx->ctx.map.ref, &pattern_reference,
6198 appctx->ctx.map.display_flags);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006199 }
6200
6201 appctx->st2 = STAT_ST_FIN;
6202 /* fall through */
6203
6204 default:
6205 appctx->st2 = STAT_ST_FIN;
6206 return 1;
6207 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006208 return 0;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006209}
6210
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006211static int stats_map_lookup(struct stream_interface *si)
6212{
6213 struct appctx *appctx = __objt_appctx(si->end);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006214 struct sample sample;
6215 struct pattern *pat;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006216 int match_method;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006217
6218 switch (appctx->st2) {
6219 case STAT_ST_INIT:
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006220 /* Init to the first entry. The list cannot be change */
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01006221 appctx->ctx.map.expr = LIST_ELEM(&appctx->ctx.map.ref->pat, struct pattern_expr *, list);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006222 appctx->ctx.map.expr = pat_expr_get_next(appctx->ctx.map.expr, &appctx->ctx.map.ref->pat);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006223 appctx->st2 = STAT_ST_LIST;
6224 /* fall through */
6225
6226 case STAT_ST_LIST:
6227 /* for each lookup type */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006228 while (appctx->ctx.map.expr) {
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006229 /* initialise chunk to build new message */
6230 chunk_reset(&trash);
6231
6232 /* execute pattern matching */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02006233 sample.data.type = SMP_T_STR;
Andreas Seltenreich9727cf42016-03-03 19:32:25 +01006234 sample.flags = SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02006235 sample.data.u.str.len = appctx->ctx.map.chunk.len;
6236 sample.data.u.str.str = appctx->ctx.map.chunk.str;
Thierry FOURNIER5d344082014-01-27 14:19:53 +01006237 if (appctx->ctx.map.expr->pat_head->match &&
6238 sample_convert(&sample, appctx->ctx.map.expr->pat_head->expect_type))
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006239 pat = appctx->ctx.map.expr->pat_head->match(&sample, appctx->ctx.map.expr, 1);
6240 else
6241 pat = NULL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006242
6243 /* build return message: set type of match */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006244 for (match_method=0; match_method<PAT_MATCH_NUM; match_method++)
6245 if (appctx->ctx.map.expr->pat_head->match == pat_match_fcts[match_method])
6246 break;
6247 if (match_method >= PAT_MATCH_NUM)
6248 chunk_appendf(&trash, "type=unknown(%p)", appctx->ctx.map.expr->pat_head->match);
6249 else
6250 chunk_appendf(&trash, "type=%s", pat_match_names[match_method]);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006251
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02006252 /* case sensitive */
6253 if (appctx->ctx.map.expr->mflags & PAT_MF_IGNORE_CASE)
6254 chunk_appendf(&trash, ", case=insensitive");
6255 else
6256 chunk_appendf(&trash, ", case=sensitive");
6257
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006258 /* Display no match, and set default value */
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01006259 if (!pat) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006260 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
6261 chunk_appendf(&trash, ", found=no");
6262 else
6263 chunk_appendf(&trash, ", match=no");
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006264 }
6265
6266 /* Display match and match info */
6267 else {
6268 /* display match */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006269 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
6270 chunk_appendf(&trash, ", found=yes");
6271 else
6272 chunk_appendf(&trash, ", match=yes");
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006273
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01006274 /* display index mode */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02006275 if (pat->sflags & PAT_SF_TREE)
Thierry FOURNIERb9903842014-03-11 18:48:17 +01006276 chunk_appendf(&trash, ", idx=tree");
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006277 else
Thierry FOURNIERb9903842014-03-11 18:48:17 +01006278 chunk_appendf(&trash, ", idx=list");
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006279
Thierry FOURNIER86db66a2014-03-11 18:37:58 +01006280 /* display pattern */
6281 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
6282 if (pat->ref && pat->ref->pattern)
6283 chunk_appendf(&trash, ", key=\"%s\"", pat->ref->pattern);
6284 else
6285 chunk_appendf(&trash, ", key=unknown");
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006286 }
6287 else {
Thierry FOURNIER86db66a2014-03-11 18:37:58 +01006288 if (pat->ref && pat->ref->pattern)
6289 chunk_appendf(&trash, ", pattern=\"%s\"", pat->ref->pattern);
6290 else
6291 chunk_appendf(&trash, ", pattern=unknown");
6292 }
6293
6294 /* display return value */
6295 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
Thierry FOURNIER503bb092015-08-19 08:35:43 +02006296 if (pat->data && pat->ref && pat->ref->sample)
Thierry FOURNIER03d0e452015-07-23 18:33:41 +02006297 chunk_appendf(&trash, ", value=\"%s\", type=\"%s\"", pat->ref->sample,
Thierry FOURNIER7d4335c2015-08-19 09:05:25 +02006298 smp_to_type[pat->data->type]);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006299 else
Thierry FOURNIER86db66a2014-03-11 18:37:58 +01006300 chunk_appendf(&trash, ", value=none");
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006301 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006302 }
6303
Thierry FOURNIERb9903842014-03-11 18:48:17 +01006304 chunk_appendf(&trash, "\n");
6305
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006306 /* display response */
Willy Tarreau2bb4a962014-11-28 11:11:05 +01006307 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau87b09662015-04-03 00:22:06 +02006308 /* let's try again later from this stream. We add ourselves into
6309 * this stream's users so that it can remove us upon termination.
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006310 */
Willy Tarreaufe127932015-04-21 19:23:39 +02006311 si_applet_cant_put(si);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006312 return 0;
6313 }
6314
6315 /* get next entry */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006316 appctx->ctx.map.expr = pat_expr_get_next(appctx->ctx.map.expr,
6317 &appctx->ctx.map.ref->pat);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006318 }
6319
6320 appctx->st2 = STAT_ST_FIN;
6321 /* fall through */
6322
6323 default:
6324 appctx->st2 = STAT_ST_FIN;
6325 free(appctx->ctx.map.chunk.str);
6326 return 1;
6327 }
6328}
6329
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006330static int stats_pat_list(struct stream_interface *si)
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006331{
6332 struct appctx *appctx = __objt_appctx(si->end);
6333
6334 switch (appctx->st2) {
6335
6336 case STAT_ST_INIT:
6337 /* Init to the first entry. The list cannot be change */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006338 appctx->ctx.map.elt = LIST_NEXT(&appctx->ctx.map.ref->head,
6339 struct pat_ref_elt *, list);
6340 if (&appctx->ctx.map.elt->list == &appctx->ctx.map.ref->head)
6341 appctx->ctx.map.elt = NULL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006342 appctx->st2 = STAT_ST_LIST;
6343 /* fall through */
6344
6345 case STAT_ST_LIST:
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006346 while (appctx->ctx.map.elt) {
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006347 chunk_reset(&trash);
6348
6349 /* build messages */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006350 if (appctx->ctx.map.elt->sample)
Thierry FOURNIER9356c682014-01-28 15:55:37 +01006351 chunk_appendf(&trash, "%p %s %s\n",
6352 appctx->ctx.map.elt, appctx->ctx.map.elt->pattern,
6353 appctx->ctx.map.elt->sample);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006354 else
Thierry FOURNIER9356c682014-01-28 15:55:37 +01006355 chunk_appendf(&trash, "%p %s\n",
6356 appctx->ctx.map.elt, appctx->ctx.map.elt->pattern);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006357
Willy Tarreau2bb4a962014-11-28 11:11:05 +01006358 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau87b09662015-04-03 00:22:06 +02006359 /* let's try again later from this stream. We add ourselves into
6360 * this stream's users so that it can remove us upon termination.
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006361 */
Willy Tarreaufe127932015-04-21 19:23:39 +02006362 si_applet_cant_put(si);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006363 return 0;
6364 }
6365
6366 /* get next list entry and check the end of the list */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006367 appctx->ctx.map.elt = LIST_NEXT(&appctx->ctx.map.elt->list,
6368 struct pat_ref_elt *, list);
6369 if (&appctx->ctx.map.elt->list == &appctx->ctx.map.ref->head)
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006370 break;
6371 }
6372
6373 appctx->st2 = STAT_ST_FIN;
6374 /* fall through */
6375
6376 default:
6377 appctx->st2 = STAT_ST_FIN;
6378 return 1;
6379 }
6380}
6381
Willy Tarreau87b09662015-04-03 00:22:06 +02006382/* This function dumps all streams' states onto the stream interface's
Willy Tarreau306f8302013-07-08 15:53:06 +02006383 * read buffer. It returns 0 if the output buffer is full and it needs
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01006384 * to be called again, otherwise non-zero. It is designed to be called
6385 * from stats_dump_sess_to_buffer() below.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006386 */
Simon Horman9bd2c732011-06-15 15:18:44 +09006387static int stats_dump_sess_to_buffer(struct stream_interface *si)
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006388{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006389 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006390 struct connection *conn;
6391
Willy Tarreau2bb4a962014-11-28 11:11:05 +01006392 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW))) {
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006393 /* If we're forced to shut down, we might have to remove our
Willy Tarreau87b09662015-04-03 00:22:06 +02006394 * reference to the last stream being dumped.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006395 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006396 if (appctx->st2 == STAT_ST_LIST) {
6397 if (!LIST_ISEMPTY(&appctx->ctx.sess.bref.users)) {
6398 LIST_DEL(&appctx->ctx.sess.bref.users);
6399 LIST_INIT(&appctx->ctx.sess.bref.users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +01006400 }
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006401 }
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02006402 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006403 }
6404
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006405 chunk_reset(&trash);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006406
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006407 switch (appctx->st2) {
Willy Tarreau295a8372011-03-10 11:25:07 +01006408 case STAT_ST_INIT:
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006409 /* the function had not been called yet, let's prepare the
Willy Tarreau87b09662015-04-03 00:22:06 +02006410 * buffer for a response. We initialize the current stream
Willy Tarreaufd3828e2009-02-22 15:17:24 +01006411 * pointer to the first in the global list. When a target
Willy Tarreau87b09662015-04-03 00:22:06 +02006412 * stream is being destroyed, it is responsible for updating
Willy Tarreaufd3828e2009-02-22 15:17:24 +01006413 * this pointer. We know we have reached the end when this
Willy Tarreau87b09662015-04-03 00:22:06 +02006414 * pointer points back to the head of the streams list.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006415 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006416 LIST_INIT(&appctx->ctx.sess.bref.users);
Willy Tarreau87b09662015-04-03 00:22:06 +02006417 appctx->ctx.sess.bref.ref = streams.n;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006418 appctx->st2 = STAT_ST_LIST;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006419 /* fall through */
6420
Willy Tarreau295a8372011-03-10 11:25:07 +01006421 case STAT_ST_LIST:
Willy Tarreau87b09662015-04-03 00:22:06 +02006422 /* first, let's detach the back-ref from a possible previous stream */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006423 if (!LIST_ISEMPTY(&appctx->ctx.sess.bref.users)) {
6424 LIST_DEL(&appctx->ctx.sess.bref.users);
6425 LIST_INIT(&appctx->ctx.sess.bref.users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +01006426 }
6427
6428 /* and start from where we stopped */
Willy Tarreau87b09662015-04-03 00:22:06 +02006429 while (appctx->ctx.sess.bref.ref != &streams) {
Cyril Bontéacd7d632010-11-01 19:26:02 +01006430 char pn[INET6_ADDRSTRLEN];
Willy Tarreau87b09662015-04-03 00:22:06 +02006431 struct stream *curr_sess;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006432
Willy Tarreau87b09662015-04-03 00:22:06 +02006433 curr_sess = LIST_ELEM(appctx->ctx.sess.bref.ref, struct stream *, list);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006434
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006435 if (appctx->ctx.sess.target) {
6436 if (appctx->ctx.sess.target != (void *)-1 && appctx->ctx.sess.target != curr_sess)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006437 goto next_sess;
6438
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006439 LIST_ADDQ(&curr_sess->back_refs, &appctx->ctx.sess.bref.users);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006440 /* call the proper dump() function and return if we're missing space */
Willy Tarreau76153662012-11-26 01:16:39 +01006441 if (!stats_dump_full_sess_to_buffer(si, curr_sess))
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006442 return 0;
6443
Willy Tarreau87b09662015-04-03 00:22:06 +02006444 /* stream dump complete */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006445 LIST_DEL(&appctx->ctx.sess.bref.users);
6446 LIST_INIT(&appctx->ctx.sess.bref.users);
6447 if (appctx->ctx.sess.target != (void *)-1) {
6448 appctx->ctx.sess.target = NULL;
Willy Tarreau76153662012-11-26 01:16:39 +01006449 break;
6450 }
6451 else
6452 goto next_sess;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006453 }
6454
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006455 chunk_appendf(&trash,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006456 "%p: proto=%s",
6457 curr_sess,
Willy Tarreauf1e02122015-09-23 12:16:43 +02006458 strm_li(curr_sess) ? strm_li(curr_sess)->proto->name : "?");
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006459
Willy Tarreaud0d8da92015-04-04 02:10:38 +02006460 conn = objt_conn(strm_orig(curr_sess));
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006461 switch (conn ? addr_to_str(&conn->addr.from, pn, sizeof(pn)) : AF_UNSPEC) {
Willy Tarreau631f01c2011-09-05 00:36:48 +02006462 case AF_INET:
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006463 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006464 chunk_appendf(&trash,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006465 " src=%s:%d fe=%s be=%s srv=%s",
6466 pn,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006467 get_host_port(&conn->addr.from),
Willy Tarreaud0d8da92015-04-04 02:10:38 +02006468 strm_fe(curr_sess)->id,
Willy Tarreau50943332011-09-02 17:33:05 +02006469 (curr_sess->be->cap & PR_CAP_BE) ? curr_sess->be->id : "<NONE>",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006470 objt_server(curr_sess->target) ? objt_server(curr_sess->target)->id : "<none>"
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006471 );
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006472 break;
6473 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006474 chunk_appendf(&trash,
Emeric Brun837ca522010-10-22 16:19:01 +02006475 " src=unix:%d fe=%s be=%s srv=%s",
Willy Tarreaud0d8da92015-04-04 02:10:38 +02006476 strm_li(curr_sess)->luid,
6477 strm_fe(curr_sess)->id,
Willy Tarreau50943332011-09-02 17:33:05 +02006478 (curr_sess->be->cap & PR_CAP_BE) ? curr_sess->be->id : "<NONE>",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006479 objt_server(curr_sess->target) ? objt_server(curr_sess->target)->id : "<none>"
Emeric Brun837ca522010-10-22 16:19:01 +02006480 );
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006481 break;
6482 }
6483
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006484 chunk_appendf(&trash,
Willy Tarreau65671ab2009-10-04 14:24:59 +02006485 " ts=%02x age=%s calls=%d",
6486 curr_sess->task->state,
Willy Tarreau3884cba2009-03-28 17:54:35 +01006487 human_time(now.tv_sec - curr_sess->logs.tv_accept.tv_sec, 1),
6488 curr_sess->task->calls);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006489
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006490 chunk_appendf(&trash,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01006491 " rq[f=%06xh,i=%d,an=%02xh,rx=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006492 curr_sess->req.flags,
6493 curr_sess->req.buf->i,
6494 curr_sess->req.analysers,
6495 curr_sess->req.rex ?
6496 human_time(TICKS_TO_MS(curr_sess->req.rex - now_ms),
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006497 TICKS_TO_MS(1000)) : "");
6498
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006499 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006500 ",wx=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006501 curr_sess->req.wex ?
6502 human_time(TICKS_TO_MS(curr_sess->req.wex - now_ms),
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006503 TICKS_TO_MS(1000)) : "");
6504
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006505 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006506 ",ax=%s]",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006507 curr_sess->req.analyse_exp ?
6508 human_time(TICKS_TO_MS(curr_sess->req.analyse_exp - 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 Tarreau02d6cfc2012-03-01 18:19:58 +01006512 " rp[f=%06xh,i=%d,an=%02xh,rx=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006513 curr_sess->res.flags,
6514 curr_sess->res.buf->i,
6515 curr_sess->res.analysers,
6516 curr_sess->res.rex ?
6517 human_time(TICKS_TO_MS(curr_sess->res.rex - now_ms),
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006518 TICKS_TO_MS(1000)) : "");
6519
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006520 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006521 ",wx=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006522 curr_sess->res.wex ?
6523 human_time(TICKS_TO_MS(curr_sess->res.wex - now_ms),
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006524 TICKS_TO_MS(1000)) : "");
6525
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006526 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006527 ",ax=%s]",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006528 curr_sess->res.analyse_exp ?
6529 human_time(TICKS_TO_MS(curr_sess->res.analyse_exp - now_ms),
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006530 TICKS_TO_MS(1000)) : "");
6531
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006532 conn = objt_conn(curr_sess->si[0].end);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006533 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006534 " s0=[%d,%1xh,fd=%d,ex=%s]",
6535 curr_sess->si[0].state,
6536 curr_sess->si[0].flags,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006537 conn ? conn->t.sock.fd : -1,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006538 curr_sess->si[0].exp ?
6539 human_time(TICKS_TO_MS(curr_sess->si[0].exp - now_ms),
6540 TICKS_TO_MS(1000)) : "");
6541
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006542 conn = objt_conn(curr_sess->si[1].end);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006543 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006544 " s1=[%d,%1xh,fd=%d,ex=%s]",
6545 curr_sess->si[1].state,
6546 curr_sess->si[1].flags,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006547 conn ? conn->t.sock.fd : -1,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006548 curr_sess->si[1].exp ?
6549 human_time(TICKS_TO_MS(curr_sess->si[1].exp - now_ms),
6550 TICKS_TO_MS(1000)) : "");
6551
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006552 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006553 " exp=%s",
6554 curr_sess->task->expire ?
6555 human_time(TICKS_TO_MS(curr_sess->task->expire - now_ms),
6556 TICKS_TO_MS(1000)) : "");
Willy Tarreau4726f532009-03-07 17:25:21 +01006557 if (task_in_rq(curr_sess->task))
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006558 chunk_appendf(&trash, " run(nice=%d)", curr_sess->task->nice);
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006559
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006560 chunk_appendf(&trash, "\n");
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006561
Willy Tarreau2bb4a962014-11-28 11:11:05 +01006562 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau87b09662015-04-03 00:22:06 +02006563 /* let's try again later from this stream. We add ourselves into
6564 * this stream's users so that it can remove us upon termination.
Willy Tarreaufd3828e2009-02-22 15:17:24 +01006565 */
Willy Tarreaufe127932015-04-21 19:23:39 +02006566 si_applet_cant_put(si);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006567 LIST_ADDQ(&curr_sess->back_refs, &appctx->ctx.sess.bref.users);
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02006568 return 0;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006569 }
6570
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006571 next_sess:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006572 appctx->ctx.sess.bref.ref = curr_sess->list.n;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006573 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006574
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006575 if (appctx->ctx.sess.target && appctx->ctx.sess.target != (void *)-1) {
Willy Tarreau87b09662015-04-03 00:22:06 +02006576 /* specified stream not found */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006577 if (appctx->ctx.sess.section > 0)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006578 chunk_appendf(&trash, " *** session terminated while we were watching it ***\n");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006579 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006580 chunk_appendf(&trash, "Session not found.\n");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006581
Willy Tarreaubc18da12015-03-13 14:00:47 +01006582 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02006583 si_applet_cant_put(si);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006584 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01006585 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006586
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006587 appctx->ctx.sess.target = NULL;
6588 appctx->ctx.sess.uid = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006589 return 1;
6590 }
6591
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006592 appctx->st2 = STAT_ST_FIN;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006593 /* fall through */
6594
6595 default:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006596 appctx->st2 = STAT_ST_FIN;
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02006597 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006598 }
Emeric Brun1e029aa2010-09-23 18:12:53 +02006599}
6600
Willy Tarreau5f9a8772012-11-26 02:22:40 +01006601/* This is called when the stream interface is closed. For instance, upon an
6602 * external abort, we won't call the i/o handler anymore so we may need to
Willy Tarreau87b09662015-04-03 00:22:06 +02006603 * remove back references to the stream currently being dumped.
Willy Tarreau5f9a8772012-11-26 02:22:40 +01006604 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02006605static void cli_release_handler(struct appctx *appctx)
Willy Tarreau5f9a8772012-11-26 02:22:40 +01006606{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006607 if (appctx->st0 == STAT_CLI_O_SESS && appctx->st2 == STAT_ST_LIST) {
6608 if (!LIST_ISEMPTY(&appctx->ctx.sess.bref.users))
6609 LIST_DEL(&appctx->ctx.sess.bref.users);
Willy Tarreau5f9a8772012-11-26 02:22:40 +01006610 }
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01006611 else if (appctx->st0 == STAT_CLI_PRINT_FREE) {
6612 free(appctx->ctx.cli.err);
Willy Tarreau6457d0f2015-10-07 20:00:24 +02006613 appctx->ctx.cli.err = NULL;
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01006614 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006615 else if (appctx->st0 == STAT_CLI_O_MLOOK) {
6616 free(appctx->ctx.map.chunk.str);
Willy Tarreau6457d0f2015-10-07 20:00:24 +02006617 appctx->ctx.map.chunk.str = NULL;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006618 }
Willy Tarreau5f9a8772012-11-26 02:22:40 +01006619}
6620
Willy Tarreau20e99322013-04-13 09:22:25 +02006621/* This function is used to either dump tables states (when action is set
6622 * to STAT_CLI_O_TAB) or clear tables (when action is STAT_CLI_O_CLR).
Willy Tarreau20e99322013-04-13 09:22:25 +02006623 * It returns 0 if the output buffer is full and it needs to be called
6624 * again, otherwise non-zero.
Willy Tarreau69f58c82010-07-12 17:55:33 +02006625 */
Willy Tarreau20e99322013-04-13 09:22:25 +02006626static int stats_table_request(struct stream_interface *si, int action)
Willy Tarreau69f58c82010-07-12 17:55:33 +02006627{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006628 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau87b09662015-04-03 00:22:06 +02006629 struct stream *s = si_strm(si);
Willy Tarreau69f58c82010-07-12 17:55:33 +02006630 struct ebmb_node *eb;
6631 int dt;
Willy Tarreau44455022012-12-05 23:01:12 +01006632 int skip_entry;
Willy Tarreau20e99322013-04-13 09:22:25 +02006633 int show = action == STAT_CLI_O_TAB;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006634
6635 /*
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006636 * We have 3 possible states in appctx->st2 :
Willy Tarreau295a8372011-03-10 11:25:07 +01006637 * - STAT_ST_INIT : the first call
6638 * - STAT_ST_INFO : the proxy pointer points to the next table to
Willy Tarreau69f58c82010-07-12 17:55:33 +02006639 * dump, the entry pointer is NULL ;
Willy Tarreau295a8372011-03-10 11:25:07 +01006640 * - STAT_ST_LIST : the proxy pointer points to the current table
Willy Tarreau69f58c82010-07-12 17:55:33 +02006641 * and the entry pointer points to the next entry to be dumped,
6642 * and the refcount on the next entry is held ;
Willy Tarreau295a8372011-03-10 11:25:07 +01006643 * - STAT_ST_END : nothing left to dump, the buffer may contain some
Willy Tarreau69f58c82010-07-12 17:55:33 +02006644 * data though.
6645 */
6646
Willy Tarreau2bb4a962014-11-28 11:11:05 +01006647 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW))) {
Willy Tarreau69f58c82010-07-12 17:55:33 +02006648 /* in case of abort, remove any refcount we might have set on an entry */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006649 if (appctx->st2 == STAT_ST_LIST) {
6650 appctx->ctx.table.entry->ref_cnt--;
6651 stksess_kill_if_expired(&appctx->ctx.table.proxy->table, appctx->ctx.table.entry);
Willy Tarreauf6efda12010-08-03 20:34:06 +02006652 }
Willy Tarreau69f58c82010-07-12 17:55:33 +02006653 return 1;
6654 }
6655
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006656 chunk_reset(&trash);
Willy Tarreau69f58c82010-07-12 17:55:33 +02006657
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006658 while (appctx->st2 != STAT_ST_FIN) {
6659 switch (appctx->st2) {
Willy Tarreau295a8372011-03-10 11:25:07 +01006660 case STAT_ST_INIT:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006661 appctx->ctx.table.proxy = appctx->ctx.table.target;
6662 if (!appctx->ctx.table.proxy)
6663 appctx->ctx.table.proxy = proxy;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006664
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006665 appctx->ctx.table.entry = NULL;
6666 appctx->st2 = STAT_ST_INFO;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006667 break;
6668
Willy Tarreau295a8372011-03-10 11:25:07 +01006669 case STAT_ST_INFO:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006670 if (!appctx->ctx.table.proxy ||
6671 (appctx->ctx.table.target &&
6672 appctx->ctx.table.proxy != appctx->ctx.table.target)) {
6673 appctx->st2 = STAT_ST_END;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006674 break;
6675 }
6676
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006677 if (appctx->ctx.table.proxy->table.size) {
6678 if (show && !stats_dump_table_head_to_buffer(&trash, si, appctx->ctx.table.proxy,
6679 appctx->ctx.table.target))
Willy Tarreau69f58c82010-07-12 17:55:33 +02006680 return 0;
6681
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006682 if (appctx->ctx.table.target &&
Willy Tarreaud0d8da92015-04-04 02:10:38 +02006683 strm_li(s)->bind_conf->level >= ACCESS_LVL_OPER) {
Willy Tarreau69f58c82010-07-12 17:55:33 +02006684 /* dump entries only if table explicitly requested */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006685 eb = ebmb_first(&appctx->ctx.table.proxy->table.keys);
Willy Tarreau69f58c82010-07-12 17:55:33 +02006686 if (eb) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006687 appctx->ctx.table.entry = ebmb_entry(eb, struct stksess, key);
6688 appctx->ctx.table.entry->ref_cnt++;
6689 appctx->st2 = STAT_ST_LIST;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006690 break;
6691 }
6692 }
6693 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006694 appctx->ctx.table.proxy = appctx->ctx.table.proxy->next;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006695 break;
6696
Willy Tarreau295a8372011-03-10 11:25:07 +01006697 case STAT_ST_LIST:
Willy Tarreau44455022012-12-05 23:01:12 +01006698 skip_entry = 0;
Simon Hormanc88b8872011-06-15 15:18:49 +09006699
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006700 if (appctx->ctx.table.data_type >= 0) {
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02006701 /* we're filtering on some data contents */
6702 void *ptr;
6703 long long data;
6704
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006705 dt = appctx->ctx.table.data_type;
6706 ptr = stktable_data_ptr(&appctx->ctx.table.proxy->table,
6707 appctx->ctx.table.entry,
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02006708 dt);
6709
6710 data = 0;
6711 switch (stktable_data_types[dt].std_type) {
6712 case STD_T_SINT:
6713 data = stktable_data_cast(ptr, std_t_sint);
6714 break;
6715 case STD_T_UINT:
6716 data = stktable_data_cast(ptr, std_t_uint);
6717 break;
6718 case STD_T_ULL:
6719 data = stktable_data_cast(ptr, std_t_ull);
6720 break;
6721 case STD_T_FRQP:
6722 data = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006723 appctx->ctx.table.proxy->table.data_arg[dt].u);
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02006724 break;
6725 }
6726
6727 /* skip the entry if the data does not match the test and the value */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006728 if ((data < appctx->ctx.table.value &&
6729 (appctx->ctx.table.data_op == STD_OP_EQ ||
6730 appctx->ctx.table.data_op == STD_OP_GT ||
6731 appctx->ctx.table.data_op == STD_OP_GE)) ||
6732 (data == appctx->ctx.table.value &&
6733 (appctx->ctx.table.data_op == STD_OP_NE ||
6734 appctx->ctx.table.data_op == STD_OP_GT ||
6735 appctx->ctx.table.data_op == STD_OP_LT)) ||
6736 (data > appctx->ctx.table.value &&
6737 (appctx->ctx.table.data_op == STD_OP_EQ ||
6738 appctx->ctx.table.data_op == STD_OP_LT ||
6739 appctx->ctx.table.data_op == STD_OP_LE)))
Willy Tarreau44455022012-12-05 23:01:12 +01006740 skip_entry = 1;
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02006741 }
6742
Simon Hormanc88b8872011-06-15 15:18:49 +09006743 if (show && !skip_entry &&
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006744 !stats_dump_table_entry_to_buffer(&trash, si, appctx->ctx.table.proxy,
6745 appctx->ctx.table.entry))
Simon Hormand9366582011-06-15 15:18:45 +09006746 return 0;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006747
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006748 appctx->ctx.table.entry->ref_cnt--;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006749
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006750 eb = ebmb_next(&appctx->ctx.table.entry->key);
Willy Tarreau69f58c82010-07-12 17:55:33 +02006751 if (eb) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006752 struct stksess *old = appctx->ctx.table.entry;
6753 appctx->ctx.table.entry = ebmb_entry(eb, struct stksess, key);
Willy Tarreau8fa52f42012-01-09 11:50:03 +01006754 if (show)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006755 stksess_kill_if_expired(&appctx->ctx.table.proxy->table, old);
6756 else if (!skip_entry && !appctx->ctx.table.entry->ref_cnt)
6757 stksess_kill(&appctx->ctx.table.proxy->table, old);
6758 appctx->ctx.table.entry->ref_cnt++;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006759 break;
6760 }
6761
Simon Hormanc88b8872011-06-15 15:18:49 +09006762
6763 if (show)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006764 stksess_kill_if_expired(&appctx->ctx.table.proxy->table, appctx->ctx.table.entry);
6765 else if (!skip_entry && !appctx->ctx.table.entry->ref_cnt)
6766 stksess_kill(&appctx->ctx.table.proxy->table, appctx->ctx.table.entry);
Simon Hormanc88b8872011-06-15 15:18:49 +09006767
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006768 appctx->ctx.table.proxy = appctx->ctx.table.proxy->next;
6769 appctx->st2 = STAT_ST_INFO;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006770 break;
6771
Willy Tarreau295a8372011-03-10 11:25:07 +01006772 case STAT_ST_END:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006773 appctx->st2 = STAT_ST_FIN;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006774 break;
6775 }
6776 }
6777 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006778}
6779
Willy Tarreaud426a182010-03-05 14:58:26 +01006780/* print a line of text buffer (limited to 70 bytes) to <out>. The format is :
Willy Tarreau74808cb2009-03-04 15:53:18 +01006781 * <2 spaces> <offset=5 digits> <space or plus> <space> <70 chars max> <\n>
6782 * which is 60 chars per line. Non-printable chars \t, \n, \r and \e are
6783 * encoded in C format. Other non-printable chars are encoded "\xHH". Original
6784 * lines are respected within the limit of 70 output chars. Lines that are
6785 * continuation of a previous truncated line begin with "+" instead of " "
6786 * after the offset. The new pointer is returned.
6787 */
Willy Tarreaud426a182010-03-05 14:58:26 +01006788static int dump_text_line(struct chunk *out, const char *buf, int bsize, int len,
6789 int *line, int ptr)
Willy Tarreau74808cb2009-03-04 15:53:18 +01006790{
6791 int end;
6792 unsigned char c;
6793
6794 end = out->len + 80;
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02006795 if (end > out->size)
Willy Tarreau74808cb2009-03-04 15:53:18 +01006796 return ptr;
6797
Willy Tarreau77804732012-10-29 16:14:26 +01006798 chunk_appendf(out, " %05d%c ", ptr, (ptr == *line) ? ' ' : '+');
Willy Tarreau74808cb2009-03-04 15:53:18 +01006799
Willy Tarreaud426a182010-03-05 14:58:26 +01006800 while (ptr < len && ptr < bsize) {
6801 c = buf[ptr];
Willy Tarreau787bbd92009-03-12 08:18:33 +01006802 if (isprint(c) && isascii(c) && c != '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006803 if (out->len > end - 2)
6804 break;
6805 out->str[out->len++] = c;
Willy Tarreau787bbd92009-03-12 08:18:33 +01006806 } else if (c == '\t' || c == '\n' || c == '\r' || c == '\e' || c == '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006807 if (out->len > end - 3)
6808 break;
6809 out->str[out->len++] = '\\';
6810 switch (c) {
6811 case '\t': c = 't'; break;
6812 case '\n': c = 'n'; break;
6813 case '\r': c = 'r'; break;
6814 case '\e': c = 'e'; break;
Willy Tarreau787bbd92009-03-12 08:18:33 +01006815 case '\\': c = '\\'; break;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006816 }
6817 out->str[out->len++] = c;
6818 } else {
6819 if (out->len > end - 5)
6820 break;
6821 out->str[out->len++] = '\\';
6822 out->str[out->len++] = 'x';
6823 out->str[out->len++] = hextab[(c >> 4) & 0xF];
6824 out->str[out->len++] = hextab[c & 0xF];
6825 }
Willy Tarreaud426a182010-03-05 14:58:26 +01006826 if (buf[ptr++] == '\n') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006827 /* we had a line break, let's return now */
6828 out->str[out->len++] = '\n';
6829 *line = ptr;
6830 return ptr;
6831 }
6832 }
6833 /* we have an incomplete line, we return it as-is */
6834 out->str[out->len++] = '\n';
6835 return ptr;
6836}
6837
Baptiste Assmann3863f972015-05-17 00:33:24 +02006838/* This function dumps counters from all resolvers section and associated name servers.
6839 * It returns 0 if the output buffer is full and it needs
6840 * to be called again, otherwise non-zero.
6841 */
6842static int stats_dump_resolvers_to_buffer(struct stream_interface *si)
6843{
6844 struct appctx *appctx = __objt_appctx(si->end);
6845 struct dns_resolvers *presolvers;
6846 struct dns_nameserver *pnameserver;
6847
6848 chunk_reset(&trash);
6849
6850 switch (appctx->st2) {
6851 case STAT_ST_INIT:
6852 appctx->st2 = STAT_ST_LIST; /* let's start producing data */
6853 /* fall through */
6854
6855 case STAT_ST_LIST:
Andrew Hayworth68d05342015-10-02 20:33:01 +00006856 if (LIST_ISEMPTY(&dns_resolvers)) {
6857 chunk_appendf(&trash, "No resolvers found\n");
6858 }
6859 else {
6860 list_for_each_entry(presolvers, &dns_resolvers, list) {
6861 if (appctx->ctx.resolvers.ptr != NULL && appctx->ctx.resolvers.ptr != presolvers)
6862 continue;
6863
6864 chunk_appendf(&trash, "Resolvers section %s\n", presolvers->id);
6865 list_for_each_entry(pnameserver, &presolvers->nameserver_list, list) {
6866 chunk_appendf(&trash, " nameserver %s:\n", pnameserver->id);
6867 chunk_appendf(&trash, " sent: %ld\n", pnameserver->counters.sent);
6868 chunk_appendf(&trash, " valid: %ld\n", pnameserver->counters.valid);
6869 chunk_appendf(&trash, " update: %ld\n", pnameserver->counters.update);
6870 chunk_appendf(&trash, " cname: %ld\n", pnameserver->counters.cname);
6871 chunk_appendf(&trash, " cname_error: %ld\n", pnameserver->counters.cname_error);
6872 chunk_appendf(&trash, " any_err: %ld\n", pnameserver->counters.any_err);
6873 chunk_appendf(&trash, " nx: %ld\n", pnameserver->counters.nx);
6874 chunk_appendf(&trash, " timeout: %ld\n", pnameserver->counters.timeout);
6875 chunk_appendf(&trash, " refused: %ld\n", pnameserver->counters.refused);
6876 chunk_appendf(&trash, " other: %ld\n", pnameserver->counters.other);
6877 chunk_appendf(&trash, " invalid: %ld\n", pnameserver->counters.invalid);
6878 chunk_appendf(&trash, " too_big: %ld\n", pnameserver->counters.too_big);
6879 chunk_appendf(&trash, " truncated: %ld\n", pnameserver->counters.truncated);
6880 chunk_appendf(&trash, " outdated: %ld\n", pnameserver->counters.outdated);
6881 }
6882 }
Baptiste Assmann3863f972015-05-17 00:33:24 +02006883 }
6884
6885 /* display response */
6886 if (bi_putchk(si_ic(si), &trash) == -1) {
6887 /* let's try again later from this session. We add ourselves into
6888 * this session's users so that it can remove us upon termination.
6889 */
6890 si->flags |= SI_FL_WAIT_ROOM;
6891 return 0;
6892 }
6893
6894 appctx->st2 = STAT_ST_FIN;
6895 /* fall through */
6896
6897 default:
6898 appctx->st2 = STAT_ST_FIN;
6899 return 1;
6900 }
6901}
6902
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02006903/* This function dumps all captured errors onto the stream interface's
Willy Tarreau306f8302013-07-08 15:53:06 +02006904 * read buffer. It returns 0 if the output buffer is full and it needs
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01006905 * to be called again, otherwise non-zero.
Willy Tarreau74808cb2009-03-04 15:53:18 +01006906 */
Simon Horman9bd2c732011-06-15 15:18:44 +09006907static int stats_dump_errors_to_buffer(struct stream_interface *si)
Willy Tarreau74808cb2009-03-04 15:53:18 +01006908{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006909 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau74808cb2009-03-04 15:53:18 +01006910 extern const char *monthname[12];
Willy Tarreau74808cb2009-03-04 15:53:18 +01006911
Willy Tarreau2bb4a962014-11-28 11:11:05 +01006912 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
Willy Tarreau61b34732009-10-03 23:49:35 +02006913 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006914
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006915 chunk_reset(&trash);
Willy Tarreau74808cb2009-03-04 15:53:18 +01006916
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006917 if (!appctx->ctx.errors.px) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006918 /* the function had not been called yet, let's prepare the
6919 * buffer for a response.
6920 */
Willy Tarreau10479e42010-12-12 14:00:34 +01006921 struct tm tm;
6922
6923 get_localtime(date.tv_sec, &tm);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006924 chunk_appendf(&trash, "Total events captured on [%02d/%s/%04d:%02d:%02d:%02d.%03d] : %u\n",
Willy Tarreau10479e42010-12-12 14:00:34 +01006925 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
6926 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(date.tv_usec/1000),
6927 error_snapshot_id);
6928
Willy Tarreau2bb4a962014-11-28 11:11:05 +01006929 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau10479e42010-12-12 14:00:34 +01006930 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreaufe127932015-04-21 19:23:39 +02006931 si_applet_cant_put(si);
Willy Tarreau10479e42010-12-12 14:00:34 +01006932 return 0;
6933 }
6934
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006935 appctx->ctx.errors.px = proxy;
6936 appctx->ctx.errors.buf = 0;
6937 appctx->ctx.errors.bol = 0;
6938 appctx->ctx.errors.ptr = -1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006939 }
6940
6941 /* we have two inner loops here, one for the proxy, the other one for
6942 * the buffer.
6943 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006944 while (appctx->ctx.errors.px) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006945 struct error_snapshot *es;
6946
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006947 if (appctx->ctx.errors.buf == 0)
6948 es = &appctx->ctx.errors.px->invalid_req;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006949 else
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006950 es = &appctx->ctx.errors.px->invalid_rep;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006951
6952 if (!es->when.tv_sec)
6953 goto next;
6954
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006955 if (appctx->ctx.errors.iid >= 0 &&
6956 appctx->ctx.errors.px->uuid != appctx->ctx.errors.iid &&
6957 es->oe->uuid != appctx->ctx.errors.iid)
Willy Tarreau74808cb2009-03-04 15:53:18 +01006958 goto next;
6959
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006960 if (appctx->ctx.errors.ptr < 0) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006961 /* just print headers now */
6962
6963 char pn[INET6_ADDRSTRLEN];
6964 struct tm tm;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02006965 int port;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006966
6967 get_localtime(es->when.tv_sec, &tm);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006968 chunk_appendf(&trash, " \n[%02d/%s/%04d:%02d:%02d:%02d.%03d]",
Willy Tarreau74808cb2009-03-04 15:53:18 +01006969 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
Willy Tarreau1772ece2009-04-03 14:49:12 +02006970 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(es->when.tv_usec/1000));
Willy Tarreau74808cb2009-03-04 15:53:18 +01006971
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02006972 switch (addr_to_str(&es->src, pn, sizeof(pn))) {
6973 case AF_INET:
6974 case AF_INET6:
6975 port = get_host_port(&es->src);
6976 break;
6977 default:
6978 port = 0;
6979 }
6980
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006981 switch (appctx->ctx.errors.buf) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006982 case 0:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006983 chunk_appendf(&trash,
Willy Tarreau74808cb2009-03-04 15:53:18 +01006984 " frontend %s (#%d): invalid request\n"
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02006985 " backend %s (#%d)",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006986 appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02006987 (es->oe->cap & PR_CAP_BE) ? es->oe->id : "<NONE>",
6988 (es->oe->cap & PR_CAP_BE) ? es->oe->uuid : -1);
Willy Tarreau74808cb2009-03-04 15:53:18 +01006989 break;
6990 case 1:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006991 chunk_appendf(&trash,
Olivier Doucet08afdcb2014-09-08 11:23:00 +02006992 " backend %s (#%d): invalid response\n"
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02006993 " frontend %s (#%d)",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006994 appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02006995 es->oe->id, es->oe->uuid);
Willy Tarreau74808cb2009-03-04 15:53:18 +01006996 break;
6997 }
6998
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006999 chunk_appendf(&trash,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02007000 ", server %s (#%d), event #%u\n"
7001 " src %s:%d, session #%d, session flags 0x%08x\n"
7002 " HTTP msg state %d, msg flags 0x%08x, tx flags 0x%08x\n"
7003 " HTTP chunk len %lld bytes, HTTP body len %lld bytes\n"
7004 " buffer flags 0x%08x, out %d bytes, total %lld bytes\n"
7005 " pending %d bytes, wrapping at %d, error at position %d:\n \n",
7006 es->srv ? es->srv->id : "<NONE>", es->srv ? es->srv->puid : -1,
7007 es->ev_id,
7008 pn, port, es->sid, es->s_flags,
7009 es->state, es->m_flags, es->t_flags,
7010 es->m_clen, es->m_blen,
7011 es->b_flags, es->b_out, es->b_tot,
7012 es->len, es->b_wrap, es->pos);
7013
Willy Tarreau2bb4a962014-11-28 11:11:05 +01007014 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01007015 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreaufe127932015-04-21 19:23:39 +02007016 si_applet_cant_put(si);
Willy Tarreau61b34732009-10-03 23:49:35 +02007017 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01007018 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01007019 appctx->ctx.errors.ptr = 0;
7020 appctx->ctx.errors.sid = es->sid;
Willy Tarreau74808cb2009-03-04 15:53:18 +01007021 }
7022
Willy Tarreau7b4b4992013-12-01 09:15:12 +01007023 if (appctx->ctx.errors.sid != es->sid) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01007024 /* the snapshot changed while we were dumping it */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007025 chunk_appendf(&trash,
Willy Tarreau74808cb2009-03-04 15:53:18 +01007026 " WARNING! update detected on this snapshot, dump interrupted. Please re-check!\n");
Willy Tarreaubc18da12015-03-13 14:00:47 +01007027 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02007028 si_applet_cant_put(si);
Willy Tarreau61b34732009-10-03 23:49:35 +02007029 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01007030 }
Willy Tarreau74808cb2009-03-04 15:53:18 +01007031 goto next;
7032 }
7033
7034 /* OK, ptr >= 0, so we have to dump the current line */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01007035 while (appctx->ctx.errors.ptr < es->len && appctx->ctx.errors.ptr < sizeof(es->buf)) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01007036 int newptr;
7037 int newline;
7038
Willy Tarreau7b4b4992013-12-01 09:15:12 +01007039 newline = appctx->ctx.errors.bol;
7040 newptr = dump_text_line(&trash, es->buf, sizeof(es->buf), es->len, &newline, appctx->ctx.errors.ptr);
7041 if (newptr == appctx->ctx.errors.ptr)
Willy Tarreau61b34732009-10-03 23:49:35 +02007042 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01007043
Willy Tarreau2bb4a962014-11-28 11:11:05 +01007044 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01007045 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreaufe127932015-04-21 19:23:39 +02007046 si_applet_cant_put(si);
Willy Tarreau61b34732009-10-03 23:49:35 +02007047 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01007048 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01007049 appctx->ctx.errors.ptr = newptr;
7050 appctx->ctx.errors.bol = newline;
Willy Tarreau74808cb2009-03-04 15:53:18 +01007051 };
7052 next:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01007053 appctx->ctx.errors.bol = 0;
7054 appctx->ctx.errors.ptr = -1;
7055 appctx->ctx.errors.buf++;
7056 if (appctx->ctx.errors.buf > 1) {
7057 appctx->ctx.errors.buf = 0;
7058 appctx->ctx.errors.px = appctx->ctx.errors.px->next;
Willy Tarreau74808cb2009-03-04 15:53:18 +01007059 }
Willy Tarreauae795722016-02-16 11:27:28 +01007060 }
7061
7062 /* dump complete */
7063 return 1;
7064}
7065
7066/* This function dumps all environmnent variables to the buffer. It returns 0
7067 * if the output buffer is full and it needs to be called again, otherwise
7068 * non-zero. Dumps only one entry if st2 == STAT_ST_END.
7069 */
7070static int stats_dump_env_to_buffer(struct stream_interface *si)
7071{
7072 struct appctx *appctx = __objt_appctx(si->end);
7073
7074 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
7075 return 1;
7076
7077 chunk_reset(&trash);
7078
7079 /* we have two inner loops here, one for the proxy, the other one for
7080 * the buffer.
7081 */
7082 while (*appctx->ctx.env.var) {
7083 chunk_printf(&trash, "%s\n", *appctx->ctx.env.var);
7084
7085 if (bi_putchk(si_ic(si), &trash) == -1) {
7086 si_applet_cant_put(si);
7087 return 0;
7088 }
7089 if (appctx->st2 == STAT_ST_END)
7090 break;
7091 appctx->ctx.env.var++;
Willy Tarreau74808cb2009-03-04 15:53:18 +01007092 }
7093
7094 /* dump complete */
Willy Tarreau61b34732009-10-03 23:49:35 +02007095 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01007096}
7097
Willy Tarreaud5781202012-09-22 19:32:35 +02007098/* parse the "level" argument on the bind lines */
7099static int bind_parse_level(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
7100{
7101 if (!*args[cur_arg + 1]) {
7102 memprintf(err, "'%s' : missing level", args[cur_arg]);
7103 return ERR_ALERT | ERR_FATAL;
7104 }
7105
7106 if (!strcmp(args[cur_arg+1], "user"))
7107 conf->level = ACCESS_LVL_USER;
7108 else if (!strcmp(args[cur_arg+1], "operator"))
7109 conf->level = ACCESS_LVL_OPER;
7110 else if (!strcmp(args[cur_arg+1], "admin"))
7111 conf->level = ACCESS_LVL_ADMIN;
7112 else {
7113 memprintf(err, "'%s' only supports 'user', 'operator', and 'admin' (got '%s')",
7114 args[cur_arg], args[cur_arg+1]);
7115 return ERR_ALERT | ERR_FATAL;
7116 }
7117
7118 return 0;
7119}
7120
Willy Tarreau30576452015-04-13 13:50:30 +02007121struct applet http_stats_applet = {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007122 .obj_type = OBJ_TYPE_APPLET,
Willy Tarreaub24281b2011-02-13 13:16:36 +01007123 .name = "<STATS>", /* used for logging */
7124 .fct = http_stats_io_handler,
Aman Gupta9a13e842012-04-02 18:57:53 -07007125 .release = NULL,
Willy Tarreaub24281b2011-02-13 13:16:36 +01007126};
7127
Willy Tarreau30576452015-04-13 13:50:30 +02007128static struct applet cli_applet = {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007129 .obj_type = OBJ_TYPE_APPLET,
Willy Tarreaub24281b2011-02-13 13:16:36 +01007130 .name = "<CLI>", /* used for logging */
7131 .fct = cli_io_handler,
Willy Tarreau5f9a8772012-11-26 02:22:40 +01007132 .release = cli_release_handler,
Willy Tarreaub24281b2011-02-13 13:16:36 +01007133};
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01007134
Willy Tarreaudc13c112013-06-21 23:16:39 +02007135static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreau10522fd2008-07-09 20:12:41 +02007136 { CFG_GLOBAL, "stats", stats_parse_global },
7137 { 0, NULL, NULL },
7138}};
7139
Willy Tarreaud5781202012-09-22 19:32:35 +02007140static struct bind_kw_list bind_kws = { "STAT", { }, {
7141 { "level", bind_parse_level, 1 }, /* set the unix socket admin level */
7142 { NULL, NULL, 0 },
7143}};
7144
Willy Tarreau10522fd2008-07-09 20:12:41 +02007145__attribute__((constructor))
7146static void __dumpstats_module_init(void)
7147{
7148 cfg_register_keywords(&cfg_kws);
Willy Tarreaud5781202012-09-22 19:32:35 +02007149 bind_register_keywords(&bind_kws);
Willy Tarreau10522fd2008-07-09 20:12:41 +02007150}
7151
Willy Tarreau91861262007-10-17 17:06:05 +02007152/*
7153 * Local variables:
7154 * c-indent-level: 8
7155 * c-basic-offset: 8
7156 * End:
7157 */