blob: 4647fb8491b771f2fd3ff9ffc7e6c42b2c883c83 [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 Tarreauc4832de2016-01-11 18:12:26 +010073/* These are the field names for each INF_* field position. Please pay attention
74 * to always use the exact same name except that the strings for new names must
75 * be lower case or CamelCase while the enum entries must be upper case.
76 */
77const char *info_field_names[INF_TOTAL_FIELDS] = {
78 [INF_NAME] = "Name",
79 [INF_VERSION] = "Version",
80 [INF_RELEASE_DATE] = "Release_date",
81 [INF_NBPROC] = "Nbproc",
82 [INF_PROCESS_NUM] = "Process_num",
83 [INF_PID] = "Pid",
84 [INF_UPTIME] = "Uptime",
85 [INF_UPTIME_SEC] = "Uptime_sec",
86 [INF_MEMMAX_MB] = "Memmax_MB",
87 [INF_POOL_ALLOC_MB] = "PoolAlloc_MB",
88 [INF_POOL_USED_MB] = "PoolUsed_MB",
89 [INF_POOL_FAILED] = "PoolFailed",
90 [INF_ULIMIT_N] = "Ulimit-n",
91 [INF_MAXSOCK] = "Maxsock",
92 [INF_MAXCONN] = "Maxconn",
93 [INF_HARD_MAXCONN] = "Hard_maxconn",
94 [INF_CURR_CONN] = "CurrConns",
95 [INF_CUM_CONN] = "CumConns",
96 [INF_CUM_REQ] = "CumReq",
97 [INF_MAX_SSL_CONNS] = "MaxSslConns",
98 [INF_CURR_SSL_CONNS] = "CurrSslConns",
99 [INF_CUM_SSL_CONNS] = "CumSslConns",
100 [INF_MAXPIPES] = "Maxpipes",
101 [INF_PIPES_USED] = "PipesUsed",
102 [INF_PIPES_FREE] = "PipesFree",
103 [INF_CONN_RATE] = "ConnRate",
104 [INF_CONN_RATE_LIMIT] = "ConnRateLimit",
105 [INF_MAX_CONN_RATE] = "MaxConnRate",
106 [INF_SESS_RATE] = "SessRate",
107 [INF_SESS_RATE_LIMIT] = "SessRateLimit",
108 [INF_MAX_SESS_RATE] = "MaxSessRate",
109 [INF_SSL_RATE] = "SslRate",
110 [INF_SSL_RATE_LIMIT] = "SslRateLimit",
111 [INF_MAX_SSL_RATE] = "MaxSslRate",
112 [INF_SSL_FRONTEND_KEY_RATE] = "SslFrontendKeyRate",
113 [INF_SSL_FRONTEND_MAX_KEY_RATE] = "SslFrontendMaxKeyRate",
114 [INF_SSL_FRONTEND_SESSION_REUSE_PCT] = "SslFrontendSessionReuse_pct",
115 [INF_SSL_BACKEND_KEY_RATE] = "SslBackendKeyRate",
116 [INF_SSL_BACKEND_MAX_KEY_RATE] = "SslBackendMaxKeyRate",
117 [INF_SSL_CACHE_LOOKUPS] = "SslCacheLookups",
118 [INF_SSL_CACHE_MISSES] = "SslCacheMisses",
119 [INF_COMPRESS_BPS_IN] = "CompressBpsIn",
120 [INF_COMPRESS_BPS_OUT] = "CompressBpsOut",
121 [INF_COMPRESS_BPS_RATE_LIM] = "CompressBpsRateLim",
122 [INF_ZLIB_MEM_USAGE] = "ZlibMemUsage",
123 [INF_MAX_ZLIB_MEM_USAGE] = "MaxZlibMemUsage",
124 [INF_TASKS] = "Tasks",
125 [INF_RUN_QUEUE] = "Run_queue",
126 [INF_IDLE_PCT] = "Idle_pct",
127 [INF_NODE] = "node",
128 [INF_DESCRIPTION] = "description",
129};
130
131/* one line of stats */
132static struct field info[INF_TOTAL_FIELDS];
133
Willy Tarreau8e205142016-01-04 17:23:25 +0100134/* These are the field names for each ST_F_* field position. Please pay attention
135 * to always use the exact same name except that the strings must be lower case
136 * while the enum entries must be upper case.
137 */
138const char *stat_field_names[ST_F_TOTAL_FIELDS] = {
139 [ST_F_PXNAME] = "pxname",
140 [ST_F_SVNAME] = "svname",
141 [ST_F_QCUR] = "qcur",
142 [ST_F_QMAX] = "qmax",
143 [ST_F_SCUR] = "scur",
144 [ST_F_SMAX] = "smax",
145 [ST_F_SLIM] = "slim",
146 [ST_F_STOT] = "stot",
147 [ST_F_BIN] = "bin",
148 [ST_F_BOUT] = "bout",
149 [ST_F_DREQ] = "dreq",
150 [ST_F_DRESP] = "dresp",
151 [ST_F_EREQ] = "ereq",
152 [ST_F_ECON] = "econ",
153 [ST_F_ERESP] = "eresp",
154 [ST_F_WRETR] = "wretr",
155 [ST_F_WREDIS] = "wredis",
156 [ST_F_STATUS] = "status",
157 [ST_F_WEIGHT] = "weight",
158 [ST_F_ACT] = "act",
159 [ST_F_BCK] = "bck",
160 [ST_F_CHKFAIL] = "chkfail",
161 [ST_F_CHKDOWN] = "chkdown",
162 [ST_F_LASTCHG] = "lastchg",
163 [ST_F_DOWNTIME] = "downtime",
164 [ST_F_QLIMIT] = "qlimit",
165 [ST_F_PID] = "pid",
166 [ST_F_IID] = "iid",
167 [ST_F_SID] = "sid",
168 [ST_F_THROTTLE] = "throttle",
169 [ST_F_LBTOT] = "lbtot",
170 [ST_F_TRACKED] = "tracked",
171 [ST_F_TYPE] = "type",
172 [ST_F_RATE] = "rate",
173 [ST_F_RATE_LIM] = "rate_lim",
174 [ST_F_RATE_MAX] = "rate_max",
175 [ST_F_CHECK_STATUS] = "check_status",
176 [ST_F_CHECK_CODE] = "check_code",
177 [ST_F_CHECK_DURATION] = "check_duration",
178 [ST_F_HRSP_1XX] = "hrsp_1xx",
179 [ST_F_HRSP_2XX] = "hrsp_2xx",
180 [ST_F_HRSP_3XX] = "hrsp_3xx",
181 [ST_F_HRSP_4XX] = "hrsp_4xx",
182 [ST_F_HRSP_5XX] = "hrsp_5xx",
183 [ST_F_HRSP_OTHER] = "hrsp_other",
184 [ST_F_HANAFAIL] = "hanafail",
185 [ST_F_REQ_RATE] = "req_rate",
186 [ST_F_REQ_RATE_MAX] = "req_rate_max",
187 [ST_F_REQ_TOT] = "req_tot",
188 [ST_F_CLI_ABRT] = "cli_abrt",
189 [ST_F_SRV_ABRT] = "srv_abrt",
190 [ST_F_COMP_IN] = "comp_in",
191 [ST_F_COMP_OUT] = "comp_out",
192 [ST_F_COMP_BYP] = "comp_byp",
193 [ST_F_COMP_RSP] = "comp_rsp",
194 [ST_F_LASTSESS] = "lastsess",
195 [ST_F_LAST_CHK] = "last_chk",
196 [ST_F_LAST_AGT] = "last_agt",
197 [ST_F_QTIME] = "qtime",
198 [ST_F_CTIME] = "ctime",
199 [ST_F_RTIME] = "rtime",
200 [ST_F_TTIME] = "ttime",
Willy Tarreau7f618842016-01-08 11:40:03 +0100201 [ST_F_AGENT_STATUS] = "agent_status",
202 [ST_F_AGENT_CODE] = "agent_code",
203 [ST_F_AGENT_DURATION] = "agent_duration",
Willy Tarreaudd7354b2016-01-08 13:47:26 +0100204 [ST_F_CHECK_DESC] = "check_desc",
205 [ST_F_AGENT_DESC] = "agent_desc",
Willy Tarreau3141f592016-01-08 14:25:28 +0100206 [ST_F_CHECK_RISE] = "check_rise",
207 [ST_F_CHECK_FALL] = "check_fall",
208 [ST_F_CHECK_HEALTH] = "check_health",
209 [ST_F_AGENT_RISE] = "agent_rise",
210 [ST_F_AGENT_FALL] = "agent_fall",
211 [ST_F_AGENT_HEALTH] = "agent_health",
Willy Tarreau3a4ec3a2016-01-08 15:35:43 +0100212 [ST_F_ADDR] = "addr",
Willy Tarreaue4847c62016-01-08 15:43:54 +0100213 [ST_F_COOKIE] = "cookie",
Willy Tarreauf8211df2016-01-11 14:09:38 +0100214 [ST_F_MODE] = "mode",
Willy Tarreauf1516d92016-01-11 14:48:36 +0100215 [ST_F_ALGO] = "algo",
Willy Tarreauc73810f2016-01-11 13:52:04 +0100216 [ST_F_CONN_RATE] = "conn_rate",
217 [ST_F_CONN_RATE_MAX] = "conn_rate_max",
218 [ST_F_CONN_TOT] = "conn_tot",
Willy Tarreau5b9bdff2016-01-11 14:40:47 +0100219 [ST_F_INTERCEPTED] = "intercepted",
Willy Tarreau8a90b8e2016-10-21 18:15:32 +0200220 [ST_F_DCON] = "dcon",
Willy Tarreaua5bc36b2016-10-21 18:16:27 +0200221 [ST_F_DSES] = "dses",
Willy Tarreau8e205142016-01-04 17:23:25 +0100222};
223
Willy Tarreau82a86022016-01-04 19:04:18 +0100224/* one line of stats */
225static struct field stats[ST_F_TOTAL_FIELDS];
226
Baptiste Assmann9b6857e2015-09-18 14:49:12 +0200227static int stats_dump_backend_to_buffer(struct stream_interface *si);
Willy Tarreauae795722016-02-16 11:27:28 +0100228static int stats_dump_env_to_buffer(struct stream_interface *si);
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +0100229static int stats_dump_info_to_buffer(struct stream_interface *si);
Baptiste Assmann28289462015-07-03 08:01:20 +0200230static int stats_dump_servers_state_to_buffer(struct stream_interface *si);
Willy Tarreau12833bb2014-01-28 16:49:56 +0100231static int stats_dump_pools_to_buffer(struct stream_interface *si);
Willy Tarreau87b09662015-04-03 00:22:06 +0200232static int stats_dump_full_sess_to_buffer(struct stream_interface *si, struct stream *sess);
Simon Horman9bd2c732011-06-15 15:18:44 +0900233static int stats_dump_sess_to_buffer(struct stream_interface *si);
234static int stats_dump_errors_to_buffer(struct stream_interface *si);
Willy Tarreau44455022012-12-05 23:01:12 +0100235static int stats_table_request(struct stream_interface *si, int show);
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +0100236static int stats_dump_proxy_to_buffer(struct stream_interface *si, struct proxy *px, struct uri_auth *uri);
237static int stats_dump_stat_to_buffer(struct stream_interface *si, struct uri_auth *uri);
Baptiste Assmann3863f972015-05-17 00:33:24 +0200238static int stats_dump_resolvers_to_buffer(struct stream_interface *si);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100239static int stats_pats_list(struct stream_interface *si);
240static int stats_pat_list(struct stream_interface *si);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +0100241static int stats_map_lookup(struct stream_interface *si);
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +0200242#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
243static int stats_tlskeys_list(struct stream_interface *si);
244#endif
Willy Tarreau00a37f02015-04-13 12:05:19 +0200245static void cli_release_handler(struct appctx *appctx);
Simon Horman9bd2c732011-06-15 15:18:44 +0900246
Cyril Bonté76a99782016-05-06 12:18:48 +0200247static int dump_servers_state(struct stream_interface *si, struct chunk *buf);
Baptiste Assmann28289462015-07-03 08:01:20 +0200248
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100249/*
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +0100250 * cli_io_handler()
251 * -> stats_dump_sess_to_buffer() // "show sess"
252 * -> stats_dump_errors_to_buffer() // "show errors"
253 * -> stats_dump_info_to_buffer() // "show info"
Baptiste Assmann9b6857e2015-09-18 14:49:12 +0200254 * -> stats_dump_backend_to_buffer() // "show backend"
Baptiste Assmann28289462015-07-03 08:01:20 +0200255 * -> stats_dump_servers_state_to_buffer() // "show servers state [<backend name>]"
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +0100256 * -> stats_dump_stat_to_buffer() // "show stat"
Cyril Bontéa8322f92016-05-06 12:18:51 +0200257 * -> stats_dump_resolvers_to_buffer() // "show stat resolvers <id>"
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +0100258 * -> stats_dump_csv_header()
259 * -> stats_dump_proxy_to_buffer()
260 * -> stats_dump_fe_stats()
261 * -> stats_dump_li_stats()
262 * -> stats_dump_sv_stats()
263 * -> stats_dump_be_stats()
264 *
265 * http_stats_io_handler()
266 * -> stats_dump_stat_to_buffer() // same as above, but used for CSV or HTML
267 * -> stats_dump_csv_header() // emits the CSV headers (same as above)
268 * -> stats_dump_html_head() // emits the HTML headers
269 * -> stats_dump_html_info() // emits the equivalent of "show info" at the top
270 * -> stats_dump_proxy_to_buffer() // same as above, valid for CSV and HTML
271 * -> stats_dump_html_px_hdr()
272 * -> stats_dump_fe_stats()
273 * -> stats_dump_li_stats()
274 * -> stats_dump_sv_stats()
275 * -> stats_dump_be_stats()
276 * -> stats_dump_html_px_end()
277 * -> stats_dump_html_end() // emits HTML trailer
Willy Tarreaud9bdcd52012-12-22 20:31:10 +0100278 */
279
Willy Tarreau30576452015-04-13 13:50:30 +0200280static struct applet cli_applet;
Simon Horman9bd2c732011-06-15 15:18:44 +0900281
282static const char stats_sock_usage_msg[] =
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200283 "Unknown command. Please enter one of the following commands only :\n"
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +0200284 " clear counters : clear max statistics counters (add 'all' for all counters)\n"
Willy Tarreau88ee3972010-07-13 13:48:00 +0200285 " clear table : remove an entry from a table\n"
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200286 " help : this message\n"
287 " prompt : toggle interactive mode with prompt\n"
288 " quit : disconnect\n"
Baptiste Assmann9b6857e2015-09-18 14:49:12 +0200289 " show backend : list backends in the current running config\n"
Willy Tarreauae795722016-02-16 11:27:28 +0100290 " show env [var] : dump environment variables known to the process\n"
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200291 " show info : report information about the running process\n"
Willy Tarreau12833bb2014-01-28 16:49:56 +0100292 " show pools : report information about the memory pools usage\n"
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200293 " show stat : report counters for each proxy and server\n"
Cyril Bontéa8322f92016-05-06 12:18:51 +0200294 " show stat resolvers [id]: dumps counters from all resolvers section and\n"
295 " associated name servers\n"
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +0200296 " show errors : report last request and response errors for each proxy\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +0100297 " show sess [id] : report the list of current sessions or dump this session\n"
Willy Tarreau69f58c82010-07-12 17:55:33 +0200298 " show table [id]: report table usage stats or dump this table's contents\n"
Baptiste Assmann28289462015-07-03 08:01:20 +0200299 " show servers state [id]: dump volatile server information (for backend <id>)\n"
Willy Tarreau38338fa2009-10-10 18:37:29 +0200300 " get weight : report a server's current weight\n"
Willy Tarreau4483d432009-10-10 19:30:08 +0200301 " set weight : change a server's weight\n"
Baptiste Assmann3d8f8312015-04-13 22:54:33 +0200302 " set server : change a server's state, weight or address\n"
Willy Tarreau654694e2012-06-07 01:03:16 +0200303 " set table [id] : update or create a table entry's data\n"
Willy Tarreau7aabd112010-01-26 10:59:06 +0100304 " set timeout : change a timeout setting\n"
Willy Tarreau2a0f4d22011-08-02 11:49:05 +0200305 " set maxconn : change a maxconn setting\n"
Willy Tarreauf5b22872011-09-07 16:13:44 +0200306 " set rate-limit : change a rate limiting value\n"
Willy Tarreaua295edc2011-09-07 23:21:03 +0200307 " disable : put a server or frontend in maintenance mode\n"
308 " enable : re-enable a server or frontend which is in maintenance mode\n"
309 " shutdown : kill a session or a frontend (eg:to release listening ports)\n"
Cyril Bontédb98eb32016-05-06 12:18:50 +0200310 " show acl [id] : report available acls or dump an acl's contents\n"
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100311 " get acl : reports the patterns matching a sample for an ACL\n"
312 " add acl : add acl entry\n"
313 " del acl : delete acl entry\n"
314 " clear acl <id> : clear the content of this acl\n"
Cyril Bontédb98eb32016-05-06 12:18:50 +0200315 " show map [id] : report available maps or dump a map's contents\n"
Thierry FOURNIER1432a0c2014-03-11 13:42:38 +0100316 " get map : reports the keys and values matching a sample for a map\n"
317 " set map : modify map entry\n"
318 " add map : add map entry\n"
319 " del map : delete map entry\n"
320 " clear map <id> : clear the content of this map\n"
Emeric Brun4147b2e2014-06-16 18:36:30 +0200321 " set ssl <stmt> : set statement for ssl\n"
William Lallemand1d0b36a2016-05-20 17:40:26 +0200322#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
323 " show tls-keys [id|*]: show tls keys references or dump tls ticket keys when id specified\n"
324#endif
Willy Tarreau9a42c0d2009-09-22 19:31:03 +0200325 "";
Willy Tarreau5ca791d2009-08-16 19:06:42 +0200326
Simon Horman9bd2c732011-06-15 15:18:44 +0900327static const char stats_permission_denied_msg[] =
Willy Tarreau6162db22009-10-10 17:13:00 +0200328 "Permission denied\n"
329 "";
330
Willy Tarreau295a8372011-03-10 11:25:07 +0100331
William Lallemand1e08cd82016-10-13 17:57:55 +0200332static char *dynamic_usage_msg = NULL;
333
334/* List head of cli keywords */
335struct cli_kw_list cli_keywords = {
336 .list = LIST_HEAD_INIT(cli_keywords.list)
Willy Tarreau295a8372011-03-10 11:25:07 +0100337};
338
Cyril Bonté19979e12012-04-04 12:57:21 +0200339extern const char *stat_status_codes[];
340
William Lallemand1e08cd82016-10-13 17:57:55 +0200341char *cli_gen_usage_msg()
342{
343 struct cli_kw_list *kw_list;
344 struct cli_kw *kw;
345 struct chunk *tmp = get_trash_chunk();
346 struct chunk out;
347
348 free(dynamic_usage_msg);
349 dynamic_usage_msg = NULL;
350
351 if (LIST_ISEMPTY(&cli_keywords.list))
352 return NULL;
353
354 chunk_reset(tmp);
355 chunk_strcat(tmp, stats_sock_usage_msg);
356 list_for_each_entry(kw_list, &cli_keywords.list, list) {
357 kw = &kw_list->kw[0];
358 while (kw->usage) {
359 chunk_appendf(tmp, " %s\n", kw->usage);
360 kw++;
361 }
362 }
363 chunk_init(&out, NULL, 0);
364 chunk_dup(&out, tmp);
365 dynamic_usage_msg = out.str;
366 return dynamic_usage_msg;
367}
368
369struct cli_kw* cli_find_kw(char **args)
370{
371 struct cli_kw_list *kw_list;
372 struct cli_kw *kw;/* current cli_kw */
373 char **tmp_args;
374 const char **tmp_str_kw;
375 int found = 0;
376
377 if (LIST_ISEMPTY(&cli_keywords.list))
378 return NULL;
379
380 list_for_each_entry(kw_list, &cli_keywords.list, list) {
381 kw = &kw_list->kw[0];
382 while (*kw->str_kw) {
383 tmp_args = args;
384 tmp_str_kw = kw->str_kw;
385 while (*tmp_str_kw) {
386 if (strcmp(*tmp_str_kw, *tmp_args) == 0) {
387 found = 1;
388 } else {
389 found = 0;
390 break;
391 }
392 tmp_args++;
393 tmp_str_kw++;
394 }
395 if (found)
396 return (kw);
397 kw++;
398 }
399 }
400 return NULL;
401}
402
403void cli_register_kw(struct cli_kw_list *kw_list)
404{
405 LIST_ADDQ(&cli_keywords.list, &kw_list->list);
406}
407
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
Vincent Bernat02779b62016-04-03 13:48:43 +0200416 fe = calloc(1, sizeof(*fe));
Willy Tarreau07e9e642010-08-17 21:48:17 +0200417 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];
William Lallemand1e08cd82016-10-13 17:57:55 +02001257 struct cli_kw *kw;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001258 int arg;
Thierry FOURNIER48bcfda2013-12-10 18:54:58 +01001259 int i, j;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001260
1261 while (isspace((unsigned char)*line))
1262 line++;
1263
1264 arg = 0;
1265 args[arg] = line;
1266
1267 while (*line && arg < MAX_STATS_ARGS) {
Thierry FOURNIER48bcfda2013-12-10 18:54:58 +01001268 if (*line == '\\') {
1269 line++;
1270 if (*line == '\0')
1271 break;
1272 }
1273 else if (isspace((unsigned char)*line)) {
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001274 *line++ = '\0';
1275
1276 while (isspace((unsigned char)*line))
1277 line++;
1278
1279 args[++arg] = line;
1280 continue;
1281 }
1282
1283 line++;
1284 }
1285
1286 while (++arg <= MAX_STATS_ARGS)
1287 args[arg] = line;
1288
Thierry FOURNIER48bcfda2013-12-10 18:54:58 +01001289 /* remove \ */
1290 arg = 0;
1291 while (*args[arg] != '\0') {
1292 j = 0;
1293 for (i=0; args[arg][i] != '\0'; i++) {
1294 if (args[arg][i] == '\\')
1295 continue;
1296 args[arg][j] = args[arg][i];
1297 j++;
1298 }
1299 args[arg][j] = '\0';
1300 arg++;
1301 }
1302
Willy Tarreau6bcb95d2015-05-04 18:07:56 +02001303 appctx->ctx.stats.scope_str = 0;
1304 appctx->ctx.stats.scope_len = 0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001305 appctx->ctx.stats.flags = 0;
William Lallemand1e08cd82016-10-13 17:57:55 +02001306 if ((kw = cli_find_kw(args))) {
1307 if (kw->parse) {
1308 if (kw->parse(args, appctx) == 0 && kw->io_handler) {
1309 appctx->st0 = STAT_CLI_O_CUSTOM;
1310 appctx->io_handler = kw->io_handler;
1311 }
1312 }
1313 } else if (strcmp(args[0], "show") == 0) {
Baptiste Assmann9b6857e2015-09-18 14:49:12 +02001314 if (strcmp(args[1], "backend") == 0) {
Cyril Bonté6ca9e012016-05-06 12:18:49 +02001315 appctx->ctx.be.px = NULL;
Baptiste Assmann9b6857e2015-09-18 14:49:12 +02001316 appctx->st2 = STAT_ST_INIT;
1317 appctx->st0 = STAT_CLI_O_BACKEND;
1318 }
Willy Tarreauae795722016-02-16 11:27:28 +01001319 else if (strcmp(args[1], "env") == 0) {
1320 extern char **environ;
1321
1322 if (strm_li(s)->bind_conf->level < ACCESS_LVL_OPER) {
1323 appctx->ctx.cli.msg = stats_permission_denied_msg;
1324 appctx->st0 = STAT_CLI_PRINT;
1325 return 1;
1326 }
1327 appctx->ctx.env.var = environ;
1328 appctx->st2 = STAT_ST_INIT;
1329 appctx->st0 = STAT_CLI_O_ENV; // stats_dump_env_to_buffer
1330
1331 if (*args[2]) {
1332 int len = strlen(args[2]);
1333
1334 for (; *appctx->ctx.env.var; appctx->ctx.env.var++) {
1335 if (strncmp(*appctx->ctx.env.var, args[2], len) == 0 &&
1336 (*appctx->ctx.env.var)[len] == '=')
1337 break;
1338 }
1339 if (!*appctx->ctx.env.var) {
1340 appctx->ctx.cli.msg = "Variable not found\n";
1341 appctx->st0 = STAT_CLI_PRINT;
1342 return 1;
1343 }
1344 appctx->st2 = STAT_ST_END;
1345 }
1346 }
1347 else if (strcmp(args[1], "stat") == 0) {
Baptiste Assmann3863f972015-05-17 00:33:24 +02001348 if (strcmp(args[2], "resolvers") == 0) {
1349 struct dns_resolvers *presolvers;
1350
Andrew Hayworth68d05342015-10-02 20:33:01 +00001351 if (*args[3]) {
1352 appctx->ctx.resolvers.ptr = NULL;
1353 list_for_each_entry(presolvers, &dns_resolvers, list) {
1354 if (strcmp(presolvers->id, args[3]) == 0) {
1355 appctx->ctx.resolvers.ptr = presolvers;
1356 break;
1357 }
Baptiste Assmann3863f972015-05-17 00:33:24 +02001358 }
Andrew Hayworth68d05342015-10-02 20:33:01 +00001359 if (appctx->ctx.resolvers.ptr == NULL) {
1360 appctx->ctx.cli.msg = "Can't find that resolvers section\n";
1361 appctx->st0 = STAT_CLI_PRINT;
1362 return 1;
1363 }
Baptiste Assmann3863f972015-05-17 00:33:24 +02001364 }
1365
1366 appctx->st2 = STAT_ST_INIT;
1367 appctx->st0 = STAT_CLI_O_RESOLVERS;
1368 return 1;
1369 }
1370 else if (*args[2] && *args[3] && *args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001371 appctx->ctx.stats.flags |= STAT_BOUND;
1372 appctx->ctx.stats.iid = atoi(args[2]);
1373 appctx->ctx.stats.type = atoi(args[3]);
1374 appctx->ctx.stats.sid = atoi(args[4]);
Willy Tarreau1e62df92016-01-11 18:57:53 +01001375 if (strcmp(args[5], "typed") == 0)
1376 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001377 }
Willy Tarreau1e62df92016-01-11 18:57:53 +01001378 else if (strcmp(args[2], "typed") == 0)
1379 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001380
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001381 appctx->st2 = STAT_ST_INIT;
1382 appctx->st0 = STAT_CLI_O_STAT; // stats_dump_stat_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001383 }
1384 else if (strcmp(args[1], "info") == 0) {
Willy Tarreaucb809122016-01-11 20:08:42 +01001385 if (strcmp(args[2], "typed") == 0)
1386 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001387 appctx->st2 = STAT_ST_INIT;
1388 appctx->st0 = STAT_CLI_O_INFO; // stats_dump_info_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001389 }
Baptiste Assmann28289462015-07-03 08:01:20 +02001390 else if (strcmp(args[1], "servers") == 0 && strcmp(args[2], "state") == 0) {
Cyril Bonté76a99782016-05-06 12:18:48 +02001391 appctx->ctx.server_state.iid = 0;
1392 appctx->ctx.server_state.px = NULL;
1393 appctx->ctx.server_state.sv = NULL;
Baptiste Assmann28289462015-07-03 08:01:20 +02001394
1395 /* check if a backend name has been provided */
1396 if (*args[3]) {
1397 /* read server state from local file */
Cyril Bonté76a99782016-05-06 12:18:48 +02001398 appctx->ctx.server_state.px = proxy_be_by_name(args[3]);
Baptiste Assmann28289462015-07-03 08:01:20 +02001399
Cyril Bonté76a99782016-05-06 12:18:48 +02001400 if (!appctx->ctx.server_state.px) {
Baptiste Assmann28289462015-07-03 08:01:20 +02001401 appctx->ctx.cli.msg = "Can't find backend.\n";
1402 appctx->st0 = STAT_CLI_PRINT;
1403 return 1;
1404 }
Cyril Bonté76a99782016-05-06 12:18:48 +02001405 appctx->ctx.server_state.iid = appctx->ctx.server_state.px->uuid;
Baptiste Assmann28289462015-07-03 08:01:20 +02001406 }
1407 appctx->st2 = STAT_ST_INIT;
1408 appctx->st0 = STAT_CLI_O_SERVERS_STATE; // stats_dump_servers_state_to_buffer
1409 return 1;
1410 }
Willy Tarreau12833bb2014-01-28 16:49:56 +01001411 else if (strcmp(args[1], "pools") == 0) {
1412 appctx->st2 = STAT_ST_INIT;
1413 appctx->st0 = STAT_CLI_O_POOLS; // stats_dump_pools_to_buffer
1414 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001415 else if (strcmp(args[1], "sess") == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001416 appctx->st2 = STAT_ST_INIT;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001417 if (strm_li(s)->bind_conf->level < ACCESS_LVL_OPER) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001418 appctx->ctx.cli.msg = stats_permission_denied_msg;
1419 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +02001420 return 1;
1421 }
Willy Tarreau76153662012-11-26 01:16:39 +01001422 if (*args[2] && strcmp(args[2], "all") == 0)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001423 appctx->ctx.sess.target = (void *)-1;
Willy Tarreau76153662012-11-26 01:16:39 +01001424 else if (*args[2])
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001425 appctx->ctx.sess.target = (void *)strtoul(args[2], NULL, 0);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01001426 else
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001427 appctx->ctx.sess.target = NULL;
Willy Tarreau87b09662015-04-03 00:22:06 +02001428 appctx->ctx.sess.section = 0; /* start with stream status */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001429 appctx->ctx.sess.pos = 0;
1430 appctx->st0 = STAT_CLI_O_SESS; // stats_dump_sess_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001431 }
1432 else if (strcmp(args[1], "errors") == 0) {
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001433 if (strm_li(s)->bind_conf->level < ACCESS_LVL_OPER) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001434 appctx->ctx.cli.msg = stats_permission_denied_msg;
1435 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +02001436 return 1;
1437 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001438 if (*args[2])
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001439 appctx->ctx.errors.iid = atoi(args[2]);
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001440 else
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001441 appctx->ctx.errors.iid = -1;
1442 appctx->ctx.errors.px = NULL;
1443 appctx->st2 = STAT_ST_INIT;
1444 appctx->st0 = STAT_CLI_O_ERR; // stats_dump_errors_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001445 }
Willy Tarreau69f58c82010-07-12 17:55:33 +02001446 else if (strcmp(args[1], "table") == 0) {
Willy Tarreaudec98142012-06-06 23:37:08 +02001447 stats_sock_table_request(si, args, STAT_CLI_O_TAB);
Willy Tarreau69f58c82010-07-12 17:55:33 +02001448 }
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02001449 else if (strcmp(args[1], "tls-keys") == 0) {
1450#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
William Lallemandcf9e7882016-06-14 17:45:18 +02001451 appctx->ctx.tlskeys.dump_all = 0;
William Lallemand1d0b36a2016-05-20 17:40:26 +02001452 /* no parameter, shows only file list */
1453 if (!*args[2]) {
William Lallemandcf9e7882016-06-14 17:45:18 +02001454 appctx->ctx.tlskeys.dump_all = 1;
William Lallemand1d0b36a2016-05-20 17:40:26 +02001455 appctx->st2 = STAT_ST_INIT;
1456 appctx->st0 = STAT_CLI_O_TLSK;
1457 return 1;
1458 }
1459
1460 if (args[2][0] == '*') {
1461 /* list every TLS ticket keys */
1462 appctx->ctx.tlskeys.ref = NULL;
William Lallemandcf9e7882016-06-14 17:45:18 +02001463 appctx->ctx.tlskeys.dump_all = 1;
William Lallemand1d0b36a2016-05-20 17:40:26 +02001464 } else {
1465 appctx->ctx.tlskeys.ref = tlskeys_ref_lookup_ref(args[2]);
1466 if(!appctx->ctx.tlskeys.ref) {
1467 appctx->ctx.cli.msg = "'show tls-keys' unable to locate referenced filename\n";
1468 appctx->st0 = STAT_CLI_PRINT;
1469 return 1;
1470 }
1471 }
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02001472 appctx->st2 = STAT_ST_INIT;
William Lallemand1d0b36a2016-05-20 17:40:26 +02001473 appctx->st0 = STAT_CLI_O_TLSK_ENT;
1474
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02001475#else
1476 appctx->ctx.cli.msg = "HAProxy was compiled against a version of OpenSSL "
1477 "that doesn't support specifying TLS ticket keys\n";
1478 appctx->st0 = STAT_CLI_PRINT;
1479#endif
1480 return 1;
1481 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001482 else if (strcmp(args[1], "map") == 0 ||
1483 strcmp(args[1], "acl") == 0) {
1484
1485 /* Set ACL or MAP flags. */
1486 if (args[1][0] == 'm')
1487 appctx->ctx.map.display_flags = PAT_REF_MAP;
1488 else
1489 appctx->ctx.map.display_flags = PAT_REF_ACL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001490
Cyril Bontédb98eb32016-05-06 12:18:50 +02001491 /* no parameter: display all map available */
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001492 if (!*args[2]) {
1493 appctx->st2 = STAT_ST_INIT;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001494 appctx->st0 = STAT_CLI_O_PATS;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001495 return 1;
1496 }
1497
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001498 /* lookup into the refs and check the map flag */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001499 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001500 if (!appctx->ctx.map.ref ||
1501 !(appctx->ctx.map.ref->flags & appctx->ctx.map.display_flags)) {
1502 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001503 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001504 else
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001505 appctx->ctx.cli.msg = "Unknown ACL identifier. Please use #<id> or <file>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001506 appctx->st0 = STAT_CLI_PRINT;
1507 return 1;
1508 }
1509 appctx->st2 = STAT_ST_INIT;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001510 appctx->st0 = STAT_CLI_O_PAT;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001511 }
Aman Guptaceafb4a2012-04-02 18:57:54 -07001512 else { /* neither "stat" nor "info" nor "sess" nor "errors" nor "table" */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001513 return 0;
1514 }
1515 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001516 else if (strcmp(args[0], "clear") == 0) {
1517 if (strcmp(args[1], "counters") == 0) {
1518 struct proxy *px;
1519 struct server *sv;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001520 struct listener *li;
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001521 int clrall = 0;
1522
1523 if (strcmp(args[2], "all") == 0)
1524 clrall = 1;
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001525
Willy Tarreau6162db22009-10-10 17:13:00 +02001526 /* check permissions */
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001527 if (strm_li(s)->bind_conf->level < ACCESS_LVL_OPER ||
1528 (clrall && strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001529 appctx->ctx.cli.msg = stats_permission_denied_msg;
1530 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +02001531 return 1;
1532 }
1533
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001534 for (px = proxy; px; px = px->next) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001535 if (clrall) {
1536 memset(&px->be_counters, 0, sizeof(px->be_counters));
1537 memset(&px->fe_counters, 0, sizeof(px->fe_counters));
1538 }
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001539 else {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001540 px->be_counters.conn_max = 0;
1541 px->be_counters.p.http.rps_max = 0;
1542 px->be_counters.sps_max = 0;
1543 px->be_counters.cps_max = 0;
1544 px->be_counters.nbpend_max = 0;
1545
1546 px->fe_counters.conn_max = 0;
1547 px->fe_counters.p.http.rps_max = 0;
1548 px->fe_counters.sps_max = 0;
1549 px->fe_counters.cps_max = 0;
1550 px->fe_counters.nbpend_max = 0;
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001551 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001552
1553 for (sv = px->srv; sv; sv = sv->next)
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001554 if (clrall)
1555 memset(&sv->counters, 0, sizeof(sv->counters));
1556 else {
1557 sv->counters.cur_sess_max = 0;
1558 sv->counters.nbpend_max = 0;
1559 sv->counters.sps_max = 0;
1560 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001561
Willy Tarreaua7944ad2012-09-26 21:03:11 +02001562 list_for_each_entry(li, &px->conf.listeners, by_fe)
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001563 if (li->counters) {
1564 if (clrall)
1565 memset(li->counters, 0, sizeof(*li->counters));
1566 else
1567 li->counters->conn_max = 0;
1568 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001569 }
1570
Willy Tarreau81c25d02011-09-07 15:17:21 +02001571 global.cps_max = 0;
Willy Tarreau93e7c002013-10-07 18:51:07 +02001572 global.sps_max = 0;
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001573 return 1;
1574 }
Willy Tarreau88ee3972010-07-13 13:48:00 +02001575 else if (strcmp(args[1], "table") == 0) {
Willy Tarreaudec98142012-06-06 23:37:08 +02001576 stats_sock_table_request(si, args, STAT_CLI_O_CLR);
Willy Tarreau88ee3972010-07-13 13:48:00 +02001577 /* end of processing */
1578 return 1;
1579 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001580 else if (strcmp(args[1], "map") == 0 || strcmp(args[1], "acl") == 0) {
1581 /* Set ACL or MAP flags. */
1582 if (args[1][0] == 'm')
1583 appctx->ctx.map.display_flags = PAT_REF_MAP;
1584 else
1585 appctx->ctx.map.display_flags = PAT_REF_ACL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001586
1587 /* no parameter */
1588 if (!*args[2]) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001589 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
1590 appctx->ctx.cli.msg = "Missing map identifier.\n";
1591 else
1592 appctx->ctx.cli.msg = "Missing ACL identifier.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001593 appctx->st0 = STAT_CLI_PRINT;
1594 return 1;
1595 }
1596
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001597 /* lookup into the refs and check the map flag */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001598 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001599 if (!appctx->ctx.map.ref ||
1600 !(appctx->ctx.map.ref->flags & appctx->ctx.map.display_flags)) {
1601 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001602 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001603 else
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001604 appctx->ctx.cli.msg = "Unknown ACL identifier. Please use #<id> or <file>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001605 appctx->st0 = STAT_CLI_PRINT;
1606 return 1;
1607 }
1608
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001609 /* Clear all. */
1610 pat_ref_prune(appctx->ctx.map.ref);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001611
1612 /* return response */
Thierry FOURNIER07e78c52014-12-18 15:28:01 +01001613 appctx->st0 = STAT_CLI_PROMPT;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001614 return 1;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001615 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001616 else {
Willy Tarreau88ee3972010-07-13 13:48:00 +02001617 /* unknown "clear" argument */
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001618 return 0;
1619 }
1620 }
Willy Tarreau38338fa2009-10-10 18:37:29 +02001621 else if (strcmp(args[0], "get") == 0) {
1622 if (strcmp(args[1], "weight") == 0) {
1623 struct proxy *px;
1624 struct server *sv;
1625
1626 /* split "backend/server" and make <line> point to server */
1627 for (line = args[2]; *line; line++)
1628 if (*line == '/') {
1629 *line++ = '\0';
1630 break;
1631 }
1632
1633 if (!*line) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001634 appctx->ctx.cli.msg = "Require 'backend/server'.\n";
1635 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau38338fa2009-10-10 18:37:29 +02001636 return 1;
1637 }
1638
1639 if (!get_backend_server(args[2], line, &px, &sv)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001640 appctx->ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
1641 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau38338fa2009-10-10 18:37:29 +02001642 return 1;
1643 }
1644
1645 /* return server's effective weight at the moment */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001646 snprintf(trash.str, trash.size, "%d (initial %d)\n", sv->uweight, sv->iweight);
Willy Tarreaubc18da12015-03-13 14:00:47 +01001647 if (bi_putstr(si_ic(si), trash.str) == -1)
Willy Tarreaufe127932015-04-21 19:23:39 +02001648 si_applet_cant_put(si);
Willy Tarreaubc18da12015-03-13 14:00:47 +01001649
Willy Tarreau38338fa2009-10-10 18:37:29 +02001650 return 1;
1651 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001652 else if (strcmp(args[1], "map") == 0 || strcmp(args[1], "acl") == 0) {
1653 /* Set flags. */
1654 if (args[1][0] == 'm')
1655 appctx->ctx.map.display_flags = PAT_REF_MAP;
1656 else
1657 appctx->ctx.map.display_flags = PAT_REF_ACL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001658
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001659 /* No parameter. */
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001660 if (!*args[2] || !*args[3]) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001661 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
1662 appctx->ctx.cli.msg = "Missing map identifier and/or key.\n";
1663 else
1664 appctx->ctx.cli.msg = "Missing ACL identifier and/or key.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001665 appctx->st0 = STAT_CLI_PRINT;
1666 return 1;
1667 }
1668
1669 /* lookup into the maps */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001670 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001671 if (!appctx->ctx.map.ref) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001672 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001673 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001674 else
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001675 appctx->ctx.cli.msg = "Unknown ACL identifier. Please use #<id> or <file>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001676 appctx->st0 = STAT_CLI_PRINT;
1677 return 1;
1678 }
1679
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001680 /* copy input string. The string must be allocated because
1681 * it may be used over multiple iterations. It's released
1682 * at the end and upon abort anyway.
1683 */
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001684 appctx->ctx.map.chunk.len = strlen(args[3]);
1685 appctx->ctx.map.chunk.size = appctx->ctx.map.chunk.len + 1;
1686 appctx->ctx.map.chunk.str = strdup(args[3]);
1687 if (!appctx->ctx.map.chunk.str) {
1688 appctx->ctx.cli.msg = "Out of memory error.\n";
1689 appctx->st0 = STAT_CLI_PRINT;
1690 return 1;
1691 }
1692
1693 /* prepare response */
1694 appctx->st2 = STAT_ST_INIT;
1695 appctx->st0 = STAT_CLI_O_MLOOK;
1696 }
Willy Tarreau38338fa2009-10-10 18:37:29 +02001697 else { /* not "get weight" */
1698 return 0;
1699 }
1700 }
Willy Tarreau4483d432009-10-10 19:30:08 +02001701 else if (strcmp(args[0], "set") == 0) {
1702 if (strcmp(args[1], "weight") == 0) {
Willy Tarreau4483d432009-10-10 19:30:08 +02001703 struct server *sv;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001704 const char *warning;
Willy Tarreau4483d432009-10-10 19:30:08 +02001705
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001706 sv = expect_server_admin(s, si, args[2]);
1707 if (!sv)
Willy Tarreau4483d432009-10-10 19:30:08 +02001708 return 1;
Willy Tarreau4483d432009-10-10 19:30:08 +02001709
Simon Horman7d09b9a2013-02-12 10:45:51 +09001710 warning = server_parse_weight_change_request(sv, args[3]);
1711 if (warning) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001712 appctx->ctx.cli.msg = warning;
1713 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +02001714 }
Willy Tarreau4483d432009-10-10 19:30:08 +02001715 return 1;
1716 }
Willy Tarreau2a4b70f2014-05-22 18:42:35 +02001717 else if (strcmp(args[1], "server") == 0) {
1718 struct server *sv;
1719 const char *warning;
1720
1721 sv = expect_server_admin(s, si, args[2]);
1722 if (!sv)
1723 return 1;
1724
1725 if (strcmp(args[3], "weight") == 0) {
1726 warning = server_parse_weight_change_request(sv, args[4]);
1727 if (warning) {
1728 appctx->ctx.cli.msg = warning;
1729 appctx->st0 = STAT_CLI_PRINT;
1730 }
1731 }
1732 else if (strcmp(args[3], "state") == 0) {
1733 if (strcmp(args[4], "ready") == 0)
1734 srv_adm_set_ready(sv);
1735 else if (strcmp(args[4], "drain") == 0)
1736 srv_adm_set_drain(sv);
1737 else if (strcmp(args[4], "maint") == 0)
1738 srv_adm_set_maint(sv);
1739 else {
1740 appctx->ctx.cli.msg = "'set server <srv> state' expects 'ready', 'drain' and 'maint'.\n";
1741 appctx->st0 = STAT_CLI_PRINT;
1742 }
1743 }
1744 else if (strcmp(args[3], "health") == 0) {
1745 if (sv->track) {
1746 appctx->ctx.cli.msg = "cannot change health on a tracking server.\n";
1747 appctx->st0 = STAT_CLI_PRINT;
1748 }
1749 else if (strcmp(args[4], "up") == 0) {
1750 sv->check.health = sv->check.rise + sv->check.fall - 1;
1751 srv_set_running(sv, "changed from CLI");
1752 }
1753 else if (strcmp(args[4], "stopping") == 0) {
1754 sv->check.health = sv->check.rise + sv->check.fall - 1;
1755 srv_set_stopping(sv, "changed from CLI");
1756 }
1757 else if (strcmp(args[4], "down") == 0) {
1758 sv->check.health = 0;
1759 srv_set_stopped(sv, "changed from CLI");
1760 }
1761 else {
1762 appctx->ctx.cli.msg = "'set server <srv> health' expects 'up', 'stopping', or 'down'.\n";
1763 appctx->st0 = STAT_CLI_PRINT;
1764 }
1765 }
1766 else if (strcmp(args[3], "agent") == 0) {
1767 if (!(sv->agent.state & CHK_ST_ENABLED)) {
1768 appctx->ctx.cli.msg = "agent checks are not enabled on this server.\n";
1769 appctx->st0 = STAT_CLI_PRINT;
1770 }
1771 else if (strcmp(args[4], "up") == 0) {
1772 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
1773 srv_set_running(sv, "changed from CLI");
1774 }
1775 else if (strcmp(args[4], "down") == 0) {
1776 sv->agent.health = 0;
1777 srv_set_stopped(sv, "changed from CLI");
1778 }
1779 else {
1780 appctx->ctx.cli.msg = "'set server <srv> agent' expects 'up' or 'down'.\n";
1781 appctx->st0 = STAT_CLI_PRINT;
1782 }
1783 }
Baptiste Assmann50946562016-08-31 23:26:29 +02001784 else if (strcmp(args[3], "check-port") == 0) {
1785 int i = 0;
1786 if (strl2irc(args[4], strlen(args[4]), &i) != 0) {
1787 appctx->ctx.cli.msg = "'set server <srv> check-port' expects an integer as argument.\n";
1788 appctx->st0 = STAT_CLI_PRINT;
1789 }
1790 if ((i < 0) || (i > 65535)) {
1791 appctx->ctx.cli.msg = "provided port is not valid.\n";
1792 appctx->st0 = STAT_CLI_PRINT;
1793 }
1794 /* prevent the update of port to 0 if MAPPORTS are in use */
1795 if ((sv->flags & SRV_F_MAPPORTS) && (i == 0)) {
1796 appctx->ctx.cli.msg = "can't unset 'port' since MAPPORTS is in use.\n";
1797 appctx->st0 = STAT_CLI_PRINT;
1798 return 1;
1799 }
1800 sv->check.port = i;
1801 appctx->ctx.cli.msg = "health check port updated.\n";
1802 appctx->st0 = STAT_CLI_PRINT;
1803 }
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001804 else if (strcmp(args[3], "addr") == 0) {
Baptiste Assmann3749ebf2016-08-03 22:34:12 +02001805 char *addr = NULL;
1806 char *port = NULL;
1807 if (strlen(args[4]) == 0) {
1808 appctx->ctx.cli.msg = "set server <b>/<s> requires an <addr> .\n";
1809 appctx->st0 = STAT_CLI_PRINT;
1810 return 1;
1811 }
1812 else {
1813 addr = args[4];
1814 }
1815 if (strcmp(args[5], "port") == 0) {
1816 port = args[6];
1817 }
1818 warning = update_server_addr_port(sv, addr, port, "stats socket command");
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001819 if (warning) {
1820 appctx->ctx.cli.msg = warning;
1821 appctx->st0 = STAT_CLI_PRINT;
1822 }
1823 }
Willy Tarreau2a4b70f2014-05-22 18:42:35 +02001824 else {
Baptiste Assmann50946562016-08-31 23:26:29 +02001825 appctx->ctx.cli.msg = "'set server <srv>' only supports 'agent', 'health', 'state', 'weight', 'addr' and 'check-port'.\n";
Willy Tarreau2a4b70f2014-05-22 18:42:35 +02001826 appctx->st0 = STAT_CLI_PRINT;
1827 }
1828 return 1;
1829 }
Willy Tarreau7aabd112010-01-26 10:59:06 +01001830 else if (strcmp(args[1], "timeout") == 0) {
1831 if (strcmp(args[2], "cli") == 0) {
1832 unsigned timeout;
1833 const char *res;
1834
1835 if (!*args[3]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001836 appctx->ctx.cli.msg = "Expects an integer value.\n";
1837 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001838 return 1;
1839 }
1840
1841 res = parse_time_err(args[3], &timeout, TIME_UNIT_S);
1842 if (res || timeout < 1) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001843 appctx->ctx.cli.msg = "Invalid timeout value.\n";
1844 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001845 return 1;
1846 }
1847
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001848 s->req.rto = s->res.wto = 1 + MS_TO_TICKS(timeout*1000);
Willy Tarreau7aabd112010-01-26 10:59:06 +01001849 return 1;
1850 }
1851 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001852 appctx->ctx.cli.msg = "'set timeout' only supports 'cli'.\n";
1853 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001854 return 1;
1855 }
1856 }
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001857 else if (strcmp(args[1], "maxconn") == 0) {
1858 if (strcmp(args[2], "frontend") == 0) {
1859 struct proxy *px;
1860 struct listener *l;
1861 int v;
1862
Willy Tarreau532a4502011-09-07 22:37:44 +02001863 px = expect_frontend_admin(s, si, args[3]);
1864 if (!px)
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001865 return 1;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001866
1867 if (!*args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001868 appctx->ctx.cli.msg = "Integer value expected.\n";
1869 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001870 return 1;
1871 }
1872
1873 v = atoi(args[4]);
Willy Tarreau3c7a79d2012-09-26 21:07:15 +02001874 if (v < 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001875 appctx->ctx.cli.msg = "Value out of range.\n";
1876 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001877 return 1;
1878 }
1879
1880 /* OK, the value is fine, so we assign it to the proxy and to all of
1881 * its listeners. The blocked ones will be dequeued.
1882 */
1883 px->maxconn = v;
Willy Tarreaua7944ad2012-09-26 21:03:11 +02001884 list_for_each_entry(l, &px->conf.listeners, by_fe) {
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001885 l->maxconn = v;
1886 if (l->state == LI_FULL)
1887 resume_listener(l);
1888 }
1889
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001890 if (px->maxconn > px->feconn && !LIST_ISEMPTY(&strm_fe(s)->listener_queue))
1891 dequeue_all_listeners(&strm_fe(s)->listener_queue);
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001892
1893 return 1;
1894 }
Andrew Hayworthedb93a72015-10-27 21:46:25 +00001895 else if (strcmp(args[2], "server") == 0) {
1896 struct server *sv;
Nenad Merdanovic5c3ed342016-04-24 23:10:07 +02001897 const char *warning;
Andrew Hayworthedb93a72015-10-27 21:46:25 +00001898
1899 sv = expect_server_admin(s, si, args[3]);
1900 if (!sv)
1901 return 1;
1902
Nenad Merdanovic5c3ed342016-04-24 23:10:07 +02001903 warning = server_parse_maxconn_change_request(sv, args[4]);
1904 if (warning) {
1905 appctx->ctx.cli.msg = warning;
Andrew Hayworthedb93a72015-10-27 21:46:25 +00001906 appctx->st0 = STAT_CLI_PRINT;
Andrew Hayworthedb93a72015-10-27 21:46:25 +00001907 }
1908
Andrew Hayworthedb93a72015-10-27 21:46:25 +00001909 return 1;
1910 }
Willy Tarreau91886b62011-09-07 14:38:31 +02001911 else if (strcmp(args[2], "global") == 0) {
1912 int v;
1913
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001914 if (strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001915 appctx->ctx.cli.msg = stats_permission_denied_msg;
1916 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau91886b62011-09-07 14:38:31 +02001917 return 1;
1918 }
1919
1920 if (!*args[3]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001921 appctx->ctx.cli.msg = "Expects an integer value.\n";
1922 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau91886b62011-09-07 14:38:31 +02001923 return 1;
1924 }
1925
1926 v = atoi(args[3]);
1927 if (v > global.hardmaxconn) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001928 appctx->ctx.cli.msg = "Value out of range.\n";
1929 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau91886b62011-09-07 14:38:31 +02001930 return 1;
1931 }
1932
1933 /* check for unlimited values */
1934 if (v <= 0)
1935 v = global.hardmaxconn;
1936
1937 global.maxconn = v;
1938
1939 /* Dequeues all of the listeners waiting for a resource */
1940 if (!LIST_ISEMPTY(&global_listener_queue))
1941 dequeue_all_listeners(&global_listener_queue);
1942
1943 return 1;
1944 }
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001945 else {
Andrew Hayworthedb93a72015-10-27 21:46:25 +00001946 appctx->ctx.cli.msg = "'set maxconn' only supports 'frontend', 'server', and 'global'.\n";
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001947 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001948 return 1;
1949 }
1950 }
Willy Tarreauf5b22872011-09-07 16:13:44 +02001951 else if (strcmp(args[1], "rate-limit") == 0) {
1952 if (strcmp(args[2], "connections") == 0) {
1953 if (strcmp(args[3], "global") == 0) {
1954 int v;
1955
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001956 if (strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001957 appctx->ctx.cli.msg = stats_permission_denied_msg;
1958 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001959 return 1;
1960 }
1961
1962 if (!*args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001963 appctx->ctx.cli.msg = "Expects an integer value.\n";
1964 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001965 return 1;
1966 }
1967
1968 v = atoi(args[4]);
1969 if (v < 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001970 appctx->ctx.cli.msg = "Value out of range.\n";
1971 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001972 return 1;
1973 }
1974
1975 global.cps_lim = v;
1976
1977 /* Dequeues all of the listeners waiting for a resource */
1978 if (!LIST_ISEMPTY(&global_listener_queue))
1979 dequeue_all_listeners(&global_listener_queue);
1980
1981 return 1;
1982 }
1983 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001984 appctx->ctx.cli.msg = "'set rate-limit connections' only supports 'global'.\n";
1985 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001986 return 1;
1987 }
1988 }
Willy Tarreau93e7c002013-10-07 18:51:07 +02001989 else if (strcmp(args[2], "sessions") == 0) {
1990 if (strcmp(args[3], "global") == 0) {
1991 int v;
1992
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001993 if (strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau93e7c002013-10-07 18:51:07 +02001994 appctx->ctx.cli.msg = stats_permission_denied_msg;
1995 appctx->st0 = STAT_CLI_PRINT;
1996 return 1;
1997 }
1998
1999 if (!*args[4]) {
2000 appctx->ctx.cli.msg = "Expects an integer value.\n";
2001 appctx->st0 = STAT_CLI_PRINT;
2002 return 1;
2003 }
2004
2005 v = atoi(args[4]);
2006 if (v < 0) {
2007 appctx->ctx.cli.msg = "Value out of range.\n";
2008 appctx->st0 = STAT_CLI_PRINT;
2009 return 1;
2010 }
2011
2012 global.sps_lim = v;
2013
2014 /* Dequeues all of the listeners waiting for a resource */
2015 if (!LIST_ISEMPTY(&global_listener_queue))
2016 dequeue_all_listeners(&global_listener_queue);
2017
2018 return 1;
2019 }
2020 else {
2021 appctx->ctx.cli.msg = "'set rate-limit sessions' only supports 'global'.\n";
2022 appctx->st0 = STAT_CLI_PRINT;
2023 return 1;
2024 }
2025 }
Willy Tarreaue43d5322013-10-07 20:01:52 +02002026#ifdef USE_OPENSSL
2027 else if (strcmp(args[2], "ssl-sessions") == 0) {
2028 if (strcmp(args[3], "global") == 0) {
2029 int v;
2030
Willy Tarreaud0d8da92015-04-04 02:10:38 +02002031 if (strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreaue43d5322013-10-07 20:01:52 +02002032 appctx->ctx.cli.msg = stats_permission_denied_msg;
2033 appctx->st0 = STAT_CLI_PRINT;
2034 return 1;
2035 }
2036
2037 if (!*args[4]) {
2038 appctx->ctx.cli.msg = "Expects an integer value.\n";
2039 appctx->st0 = STAT_CLI_PRINT;
2040 return 1;
2041 }
2042
2043 v = atoi(args[4]);
2044 if (v < 0) {
2045 appctx->ctx.cli.msg = "Value out of range.\n";
2046 appctx->st0 = STAT_CLI_PRINT;
2047 return 1;
2048 }
2049
2050 global.ssl_lim = v;
2051
2052 /* Dequeues all of the listeners waiting for a resource */
2053 if (!LIST_ISEMPTY(&global_listener_queue))
2054 dequeue_all_listeners(&global_listener_queue);
2055
2056 return 1;
2057 }
2058 else {
2059 appctx->ctx.cli.msg = "'set rate-limit ssl-sessions' only supports 'global'.\n";
2060 appctx->st0 = STAT_CLI_PRINT;
2061 return 1;
2062 }
2063 }
2064#endif
William Lallemandd85f9172012-11-09 17:05:39 +01002065 else if (strcmp(args[2], "http-compression") == 0) {
2066 if (strcmp(args[3], "global") == 0) {
2067 int v;
2068
Willy Tarreaua1c2b2c2015-11-26 18:32:39 +01002069 if (strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN) {
2070 appctx->ctx.cli.msg = stats_permission_denied_msg;
2071 appctx->st0 = STAT_CLI_PRINT;
2072 return 1;
2073 }
2074
Willy Tarreau85d47f92012-11-21 00:29:50 +01002075 if (!*args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002076 appctx->ctx.cli.msg = "Expects a maximum input byte rate in kB/s.\n";
2077 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau85d47f92012-11-21 00:29:50 +01002078 return 1;
2079 }
2080
William Lallemandd85f9172012-11-09 17:05:39 +01002081 v = atoi(args[4]);
2082 global.comp_rate_lim = v * 1024; /* Kilo to bytes. */
2083 }
2084 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002085 appctx->ctx.cli.msg = "'set rate-limit http-compression' only supports 'global'.\n";
2086 appctx->st0 = STAT_CLI_PRINT;
William Lallemandd85f9172012-11-09 17:05:39 +01002087 return 1;
2088 }
2089 }
Willy Tarreauf5b22872011-09-07 16:13:44 +02002090 else {
Willy Tarreaue43d5322013-10-07 20:01:52 +02002091 appctx->ctx.cli.msg = "'set rate-limit' supports 'connections', 'sessions', 'ssl-sessions', and 'http-compression'.\n";
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002092 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02002093 return 1;
2094 }
2095 }
Willy Tarreau654694e2012-06-07 01:03:16 +02002096 else if (strcmp(args[1], "table") == 0) {
2097 stats_sock_table_request(si, args, STAT_CLI_O_SET);
2098 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002099 else if (strcmp(args[1], "map") == 0) {
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002100 char *err;
2101
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002102 /* Set flags. */
2103 appctx->ctx.map.display_flags = PAT_REF_MAP;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002104
2105 /* Expect three parameters: map name, key and new value. */
2106 if (!*args[2] || !*args[3] || !*args[4]) {
Thierry FOURNIERd5723432014-03-11 13:52:44 +01002107 appctx->ctx.cli.msg = "'set map' expects three parameters: map identifier, key and value.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002108 appctx->st0 = STAT_CLI_PRINT;
2109 return 1;
2110 }
2111
2112 /* Lookup the reference in the maps. */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002113 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002114 if (!appctx->ctx.map.ref) {
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01002115 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002116 appctx->st0 = STAT_CLI_PRINT;
2117 return 1;
2118 }
2119
Thierry FOURNIER9356c682014-01-28 15:55:37 +01002120 /* If the entry identifier start with a '#', it is considered as
2121 * pointer id
2122 */
2123 if (args[3][0] == '#' && args[3][1] == '0' && args[3][2] == 'x') {
2124 struct pat_ref_elt *ref;
2125 long long int conv;
2126 char *error;
2127
2128 /* Convert argument to integer value. */
2129 conv = strtoll(&args[3][1], &error, 16);
2130 if (*error != '\0') {
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01002131 appctx->ctx.cli.msg = "Malformed identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER9356c682014-01-28 15:55:37 +01002132 appctx->st0 = STAT_CLI_PRINT;
2133 return 1;
2134 }
2135
2136 /* Convert and check integer to pointer. */
2137 ref = (struct pat_ref_elt *)(long)conv;
2138 if ((long long int)(long)ref != conv) {
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01002139 appctx->ctx.cli.msg = "Malformed identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER9356c682014-01-28 15:55:37 +01002140 appctx->st0 = STAT_CLI_PRINT;
2141 return 1;
2142 }
2143
2144 /* Try to delete the entry. */
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002145 err = NULL;
2146 if (!pat_ref_set_by_id(appctx->ctx.map.ref, ref, args[4], &err)) {
2147 if (err)
2148 memprintf(&err, "%s.\n", err);
2149 appctx->ctx.cli.err = err;
2150 appctx->st0 = STAT_CLI_PRINT_FREE;
Thierry FOURNIER9356c682014-01-28 15:55:37 +01002151 return 1;
2152 }
2153 }
2154 else {
2155 /* Else, use the entry identifier as pattern
2156 * string, and update the value.
2157 */
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002158 err = NULL;
2159 if (!pat_ref_set(appctx->ctx.map.ref, args[3], args[4], &err)) {
2160 if (err)
2161 memprintf(&err, "%s.\n", err);
2162 appctx->ctx.cli.err = err;
2163 appctx->st0 = STAT_CLI_PRINT_FREE;
Thierry FOURNIER9356c682014-01-28 15:55:37 +01002164 return 1;
2165 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002166 }
2167
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002168 /* The set is done, send message. */
Thierry FOURNIER07e78c52014-12-18 15:28:01 +01002169 appctx->st0 = STAT_CLI_PROMPT;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002170 return 1;
2171 }
Emeric Brun4147b2e2014-06-16 18:36:30 +02002172#ifdef USE_OPENSSL
2173 else if (strcmp(args[1], "ssl") == 0) {
2174 if (strcmp(args[2], "ocsp-response") == 0) {
Lukas Tribuse4e30f72014-12-09 16:32:51 +01002175#if (defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP)
Emeric Brun4147b2e2014-06-16 18:36:30 +02002176 char *err = NULL;
2177
Emeric Brunaf4ef742014-06-19 14:10:45 +02002178 /* Expect one parameter: the new response in base64 encoding */
Emeric Brun4147b2e2014-06-16 18:36:30 +02002179 if (!*args[3]) {
2180 appctx->ctx.cli.msg = "'set ssl ocsp-response' expects response in base64 encoding.\n";
2181 appctx->st0 = STAT_CLI_PRINT;
2182 return 1;
2183 }
2184
2185 trash.len = base64dec(args[3], strlen(args[3]), trash.str, trash.size);
2186 if (trash.len < 0) {
2187 appctx->ctx.cli.msg = "'set ssl ocsp-response' received invalid base64 encoded response.\n";
2188 appctx->st0 = STAT_CLI_PRINT;
2189 return 1;
2190 }
2191
2192 if (ssl_sock_update_ocsp_response(&trash, &err)) {
2193 if (err) {
2194 memprintf(&err, "%s.\n", err);
2195 appctx->ctx.cli.err = err;
2196 appctx->st0 = STAT_CLI_PRINT_FREE;
2197 }
2198 return 1;
2199 }
2200 appctx->ctx.cli.msg = "OCSP Response updated!";
2201 appctx->st0 = STAT_CLI_PRINT;
2202 return 1;
2203#else
2204 appctx->ctx.cli.msg = "HAProxy was compiled against a version of OpenSSL that doesn't support OCSP stapling.\n";
2205 appctx->st0 = STAT_CLI_PRINT;
2206 return 1;
2207#endif
2208 }
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02002209 else if (strcmp(args[2], "tls-key") == 0) {
2210#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
2211 /* Expect two parameters: the filename and the new new TLS key in encoding */
2212 if (!*args[3] || !*args[4]) {
2213 appctx->ctx.cli.msg = "'set ssl tls-key' expects a filename and the new TLS key in base64 encoding.\n";
2214 appctx->st0 = STAT_CLI_PRINT;
2215 return 1;
2216 }
2217
2218 appctx->ctx.tlskeys.ref = tlskeys_ref_lookup_ref(args[3]);
2219 if(!appctx->ctx.tlskeys.ref) {
2220 appctx->ctx.cli.msg = "'set ssl tls-key' unable to locate referenced filename\n";
2221 appctx->st0 = STAT_CLI_PRINT;
2222 return 1;
2223 }
2224
2225 trash.len = base64dec(args[4], strlen(args[4]), trash.str, trash.size);
2226 if (trash.len != sizeof(struct tls_sess_key)) {
2227 appctx->ctx.cli.msg = "'set ssl tls-key' received invalid base64 encoded TLS key.\n";
2228 appctx->st0 = STAT_CLI_PRINT;
2229 return 1;
2230 }
2231
Pradeep Jindalcc79b002015-08-20 18:25:17 +05302232 memcpy(appctx->ctx.tlskeys.ref->tlskeys + ((appctx->ctx.tlskeys.ref->tls_ticket_enc_index + 2) % TLS_TICKETS_NO), trash.str, trash.len);
2233 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 +02002234
2235 appctx->ctx.cli.msg = "TLS ticket key updated!";
2236 appctx->st0 = STAT_CLI_PRINT;
2237 return 1;
2238#else
2239 appctx->ctx.cli.msg = "HAProxy was compiled against a version of OpenSSL "
2240 "that doesn't support specifying TLS ticket keys\n";
2241 appctx->st0 = STAT_CLI_PRINT;
2242 return 1;
2243#endif
2244 }
Emeric Brun4147b2e2014-06-16 18:36:30 +02002245 else {
2246 appctx->ctx.cli.msg = "'set ssl' only supports 'ocsp-response'.\n";
2247 appctx->st0 = STAT_CLI_PRINT;
2248 return 1;
2249 }
2250 }
2251#endif
Willy Tarreau7aabd112010-01-26 10:59:06 +01002252 else { /* unknown "set" parameter */
Willy Tarreau4483d432009-10-10 19:30:08 +02002253 return 0;
2254 }
2255 }
Cyril Bontécd19e512010-01-31 22:34:03 +01002256 else if (strcmp(args[0], "enable") == 0) {
Simon Horman671b6f02013-11-25 10:46:39 +09002257 if (strcmp(args[1], "agent") == 0) {
2258 struct server *sv;
2259
2260 sv = expect_server_admin(s, si, args[2]);
2261 if (!sv)
2262 return 1;
2263
Willy Tarreau2e10f5a2013-12-11 20:11:55 +01002264 if (!(sv->agent.state & CHK_ST_CONFIGURED)) {
2265 appctx->ctx.cli.msg = "Agent was not configured on this server, cannot enable.\n";
2266 appctx->st0 = STAT_CLI_PRINT;
2267 return 1;
2268 }
2269
2270 sv->agent.state |= CHK_ST_ENABLED;
Simon Horman671b6f02013-11-25 10:46:39 +09002271 return 1;
2272 }
Willy Tarreau9b5aecd2014-05-23 11:53:10 +02002273 else if (strcmp(args[1], "health") == 0) {
Cyril Bontécd19e512010-01-31 22:34:03 +01002274 struct server *sv;
2275
Willy Tarreaud52c41e2011-09-07 23:41:01 +02002276 sv = expect_server_admin(s, si, args[2]);
2277 if (!sv)
Cyril Bonté613f0df2011-03-03 20:49:04 +01002278 return 1;
Cyril Bonté613f0df2011-03-03 20:49:04 +01002279
Willy Tarreau9b5aecd2014-05-23 11:53:10 +02002280 if (!(sv->check.state & CHK_ST_CONFIGURED)) {
2281 appctx->ctx.cli.msg = "Health checks are not configured on this server, cannot enable.\n";
2282 appctx->st0 = STAT_CLI_PRINT;
2283 return 1;
2284 }
2285
2286 sv->check.state |= CHK_ST_ENABLED;
2287 return 1;
2288 }
2289 else if (strcmp(args[1], "server") == 0) {
2290 struct server *sv;
2291
2292 sv = expect_server_admin(s, si, args[2]);
2293 if (!sv)
2294 return 1;
2295
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02002296 srv_adm_set_ready(sv);
Willy Tarreau532a4502011-09-07 22:37:44 +02002297 return 1;
2298 }
2299 else if (strcmp(args[1], "frontend") == 0) {
2300 struct proxy *px;
2301
2302 px = expect_frontend_admin(s, si, args[2]);
2303 if (!px)
2304 return 1;
2305
2306 if (px->state == PR_STSTOPPED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002307 appctx->ctx.cli.msg = "Frontend was previously shut down, cannot enable.\n";
2308 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002309 return 1;
2310 }
2311
2312 if (px->state != PR_STPAUSED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002313 appctx->ctx.cli.msg = "Frontend is already enabled.\n";
2314 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002315 return 1;
2316 }
2317
2318 if (!resume_proxy(px)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002319 appctx->ctx.cli.msg = "Failed to resume frontend, check logs for precise cause (port conflict?).\n";
2320 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002321 return 1;
2322 }
Cyril Bontécd19e512010-01-31 22:34:03 +01002323 return 1;
2324 }
2325 else { /* unknown "enable" parameter */
Willy Tarreau9b5aecd2014-05-23 11:53:10 +02002326 appctx->ctx.cli.msg = "'enable' only supports 'agent', 'frontend', 'health', and 'server'.\n";
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002327 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002328 return 1;
Cyril Bontécd19e512010-01-31 22:34:03 +01002329 }
2330 }
2331 else if (strcmp(args[0], "disable") == 0) {
Simon Horman671b6f02013-11-25 10:46:39 +09002332 if (strcmp(args[1], "agent") == 0) {
2333 struct server *sv;
2334
2335 sv = expect_server_admin(s, si, args[2]);
2336 if (!sv)
2337 return 1;
2338
Willy Tarreau2e10f5a2013-12-11 20:11:55 +01002339 sv->agent.state &= ~CHK_ST_ENABLED;
Simon Horman671b6f02013-11-25 10:46:39 +09002340 return 1;
2341 }
Willy Tarreau9b5aecd2014-05-23 11:53:10 +02002342 else if (strcmp(args[1], "health") == 0) {
2343 struct server *sv;
2344
2345 sv = expect_server_admin(s, si, args[2]);
2346 if (!sv)
2347 return 1;
2348
2349 sv->check.state &= ~CHK_ST_ENABLED;
2350 return 1;
2351 }
Simon Horman671b6f02013-11-25 10:46:39 +09002352 else if (strcmp(args[1], "server") == 0) {
Cyril Bontécd19e512010-01-31 22:34:03 +01002353 struct server *sv;
2354
Willy Tarreaud52c41e2011-09-07 23:41:01 +02002355 sv = expect_server_admin(s, si, args[2]);
2356 if (!sv)
Cyril Bonté613f0df2011-03-03 20:49:04 +01002357 return 1;
Cyril Bonté613f0df2011-03-03 20:49:04 +01002358
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02002359 srv_adm_set_maint(sv);
Willy Tarreau532a4502011-09-07 22:37:44 +02002360 return 1;
2361 }
2362 else if (strcmp(args[1], "frontend") == 0) {
2363 struct proxy *px;
2364
2365 px = expect_frontend_admin(s, si, args[2]);
2366 if (!px)
2367 return 1;
2368
2369 if (px->state == PR_STSTOPPED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002370 appctx->ctx.cli.msg = "Frontend was previously shut down, cannot disable.\n";
2371 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002372 return 1;
2373 }
2374
2375 if (px->state == PR_STPAUSED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002376 appctx->ctx.cli.msg = "Frontend is already disabled.\n";
2377 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002378 return 1;
2379 }
2380
2381 if (!pause_proxy(px)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002382 appctx->ctx.cli.msg = "Failed to pause frontend, check logs for precise cause.\n";
2383 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002384 return 1;
2385 }
Cyril Bontécd19e512010-01-31 22:34:03 +01002386 return 1;
2387 }
2388 else { /* unknown "disable" parameter */
Willy Tarreau9b5aecd2014-05-23 11:53:10 +02002389 appctx->ctx.cli.msg = "'disable' only supports 'agent', 'frontend', 'health', and 'server'.\n";
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002390 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002391 return 1;
2392 }
2393 }
2394 else if (strcmp(args[0], "shutdown") == 0) {
2395 if (strcmp(args[1], "frontend") == 0) {
2396 struct proxy *px;
2397
2398 px = expect_frontend_admin(s, si, args[2]);
2399 if (!px)
2400 return 1;
2401
2402 if (px->state == PR_STSTOPPED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002403 appctx->ctx.cli.msg = "Frontend was already shut down.\n";
2404 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002405 return 1;
2406 }
2407
2408 Warning("Proxy %s stopped (FE: %lld conns, BE: %lld conns).\n",
2409 px->id, px->fe_counters.cum_conn, px->be_counters.cum_conn);
2410 send_log(px, LOG_WARNING, "Proxy %s stopped (FE: %lld conns, BE: %lld conns).\n",
2411 px->id, px->fe_counters.cum_conn, px->be_counters.cum_conn);
2412 stop_proxy(px);
2413 return 1;
2414 }
Willy Tarreaua295edc2011-09-07 23:21:03 +02002415 else if (strcmp(args[1], "session") == 0) {
Willy Tarreau87b09662015-04-03 00:22:06 +02002416 struct stream *sess, *ptr;
Willy Tarreaua295edc2011-09-07 23:21:03 +02002417
Willy Tarreaud0d8da92015-04-04 02:10:38 +02002418 if (strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002419 appctx->ctx.cli.msg = stats_permission_denied_msg;
2420 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaua295edc2011-09-07 23:21:03 +02002421 return 1;
2422 }
2423
2424 if (!*args[2]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002425 appctx->ctx.cli.msg = "Session pointer expected (use 'show sess').\n";
2426 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaua295edc2011-09-07 23:21:03 +02002427 return 1;
2428 }
2429
2430 ptr = (void *)strtoul(args[2], NULL, 0);
2431
Willy Tarreau87b09662015-04-03 00:22:06 +02002432 /* first, look for the requested stream in the stream table */
2433 list_for_each_entry(sess, &streams, list) {
Willy Tarreaua295edc2011-09-07 23:21:03 +02002434 if (sess == ptr)
2435 break;
2436 }
2437
Willy Tarreau87b09662015-04-03 00:22:06 +02002438 /* do we have the stream ? */
Willy Tarreaua295edc2011-09-07 23:21:03 +02002439 if (sess != ptr) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002440 appctx->ctx.cli.msg = "No such session (use 'show sess').\n";
2441 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaua295edc2011-09-07 23:21:03 +02002442 return 1;
2443 }
2444
Willy Tarreaue7dff022015-04-03 01:14:29 +02002445 stream_shutdown(sess, SF_ERR_KILLED);
Willy Tarreaua295edc2011-09-07 23:21:03 +02002446 return 1;
2447 }
Willy Tarreau52b2d222011-09-07 23:48:48 +02002448 else if (strcmp(args[1], "sessions") == 0) {
2449 if (strcmp(args[2], "server") == 0) {
2450 struct server *sv;
Willy Tarreau87b09662015-04-03 00:22:06 +02002451 struct stream *sess, *sess_bck;
Willy Tarreau52b2d222011-09-07 23:48:48 +02002452
2453 sv = expect_server_admin(s, si, args[3]);
2454 if (!sv)
2455 return 1;
2456
Willy Tarreau87b09662015-04-03 00:22:06 +02002457 /* kill all the stream that are on this server */
Willy Tarreau52b2d222011-09-07 23:48:48 +02002458 list_for_each_entry_safe(sess, sess_bck, &sv->actconns, by_srv)
2459 if (sess->srv_conn == sv)
Willy Tarreaue7dff022015-04-03 01:14:29 +02002460 stream_shutdown(sess, SF_ERR_KILLED);
Willy Tarreau52b2d222011-09-07 23:48:48 +02002461
2462 return 1;
2463 }
2464 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002465 appctx->ctx.cli.msg = "'shutdown sessions' only supports 'server'.\n";
2466 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau52b2d222011-09-07 23:48:48 +02002467 return 1;
2468 }
2469 }
Willy Tarreau532a4502011-09-07 22:37:44 +02002470 else { /* unknown "disable" parameter */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002471 appctx->ctx.cli.msg = "'shutdown' only supports 'frontend', 'session' and 'sessions'.\n";
2472 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002473 return 1;
Cyril Bontécd19e512010-01-31 22:34:03 +01002474 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002475 }
2476 else if (strcmp(args[0], "del") == 0) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002477 if (strcmp(args[1], "map") == 0 || strcmp(args[1], "acl") == 0) {
2478 if (args[1][0] == 'm')
2479 appctx->ctx.map.display_flags = PAT_REF_MAP;
2480 else
2481 appctx->ctx.map.display_flags = PAT_REF_ACL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002482
2483 /* Expect two parameters: map name and key. */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002484 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
2485 if (!*args[2] || !*args[3]) {
2486 appctx->ctx.cli.msg = "This command expects two parameters: map identifier and key.\n";
2487 appctx->st0 = STAT_CLI_PRINT;
2488 return 1;
2489 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002490 }
2491
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002492 else {
2493 if (!*args[2] || !*args[3]) {
2494 appctx->ctx.cli.msg = "This command expects two parameters: ACL identifier and key.\n";
2495 appctx->st0 = STAT_CLI_PRINT;
2496 return 1;
2497 }
2498 }
2499
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002500 /* Lookup the reference in the maps. */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002501 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002502 if (!appctx->ctx.map.ref ||
2503 !(appctx->ctx.map.ref->flags & appctx->ctx.map.display_flags)) {
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01002504 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002505 appctx->st0 = STAT_CLI_PRINT;
2506 return 1;
2507 }
2508
Thierry FOURNIER9356c682014-01-28 15:55:37 +01002509 /* If the entry identifier start with a '#', it is considered as
2510 * pointer id
2511 */
2512 if (args[3][0] == '#' && args[3][1] == '0' && args[3][2] == 'x') {
2513 struct pat_ref_elt *ref;
2514 long long int conv;
2515 char *error;
2516
2517 /* Convert argument to integer value. */
2518 conv = strtoll(&args[3][1], &error, 16);
2519 if (*error != '\0') {
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01002520 appctx->ctx.cli.msg = "Malformed identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER9356c682014-01-28 15:55:37 +01002521 appctx->st0 = STAT_CLI_PRINT;
2522 return 1;
2523 }
2524
2525 /* Convert and check integer to pointer. */
2526 ref = (struct pat_ref_elt *)(long)conv;
2527 if ((long long int)(long)ref != conv) {
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01002528 appctx->ctx.cli.msg = "Malformed identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER9356c682014-01-28 15:55:37 +01002529 appctx->st0 = STAT_CLI_PRINT;
2530 return 1;
2531 }
2532
2533 /* Try to delete the entry. */
2534 if (!pat_ref_delete_by_id(appctx->ctx.map.ref, ref)) {
2535 /* The entry is not found, send message. */
2536 appctx->ctx.cli.msg = "Key not found.\n";
2537 appctx->st0 = STAT_CLI_PRINT;
2538 return 1;
2539 }
2540 }
2541 else {
2542 /* Else, use the entry identifier as pattern
2543 * string and try to delete the entry.
2544 */
2545 if (!pat_ref_delete(appctx->ctx.map.ref, args[3])) {
2546 /* The entry is not found, send message. */
2547 appctx->ctx.cli.msg = "Key not found.\n";
2548 appctx->st0 = STAT_CLI_PRINT;
2549 return 1;
2550 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002551 }
2552
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002553 /* The deletion is done, send message. */
Thierry FOURNIER07e78c52014-12-18 15:28:01 +01002554 appctx->st0 = STAT_CLI_PROMPT;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002555 return 1;
2556 }
2557 else { /* unknown "del" parameter */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002558 appctx->ctx.cli.msg = "'del' only supports 'map' or 'acl'.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002559 appctx->st0 = STAT_CLI_PRINT;
2560 return 1;
2561 }
2562 }
2563 else if (strcmp(args[0], "add") == 0) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002564 if (strcmp(args[1], "map") == 0 ||
2565 strcmp(args[1], "acl") == 0) {
2566 int ret;
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002567 char *err;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002568
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002569 /* Set flags. */
2570 if (args[1][0] == 'm')
2571 appctx->ctx.map.display_flags = PAT_REF_MAP;
2572 else
2573 appctx->ctx.map.display_flags = PAT_REF_ACL;
2574
2575 /* If the keywork is "map", we expect three parameters, if it
2576 * is "acl", we expect only two parameters
2577 */
2578 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
2579 if (!*args[2] || !*args[3] || !*args[4]) {
2580 appctx->ctx.cli.msg = "'add map' expects three parameters: map identifier, key and value.\n";
2581 appctx->st0 = STAT_CLI_PRINT;
2582 return 1;
2583 }
2584 }
2585 else {
2586 if (!*args[2] || !*args[3]) {
2587 appctx->ctx.cli.msg = "'add acl' expects two parameters: ACL identifier and pattern.\n";
2588 appctx->st0 = STAT_CLI_PRINT;
2589 return 1;
2590 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002591 }
2592
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002593 /* Lookup for the reference. */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002594 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002595 if (!appctx->ctx.map.ref) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002596 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01002597 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002598 else
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01002599 appctx->ctx.cli.msg = "Unknown ACL identifier. Please use #<id> or <file>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002600 appctx->st0 = STAT_CLI_PRINT;
2601 return 1;
2602 }
2603
Thierry FOURNIER64c585f2014-01-29 20:02:36 +01002604 /* The command "add acl" is prohibited if the reference
2605 * use samples.
2606 */
2607 if ((appctx->ctx.map.display_flags & PAT_REF_ACL) &&
2608 (appctx->ctx.map.ref->flags & PAT_REF_SMP)) {
2609 appctx->ctx.cli.msg = "This ACL is shared with a map containing samples. "
2610 "You must use the command 'add map' to add values.\n";
2611 appctx->st0 = STAT_CLI_PRINT;
2612 return 1;
2613 }
2614
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002615 /* Add value. */
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002616 err = NULL;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002617 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02002618 ret = pat_ref_add(appctx->ctx.map.ref, args[3], args[4], &err);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002619 else
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02002620 ret = pat_ref_add(appctx->ctx.map.ref, args[3], NULL, &err);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002621 if (!ret) {
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002622 if (err)
2623 memprintf(&err, "%s.\n", err);
2624 appctx->ctx.cli.err = err;
2625 appctx->st0 = STAT_CLI_PRINT_FREE;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002626 return 1;
2627 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002628
2629 /* The add is done, send message. */
Thierry FOURNIER07e78c52014-12-18 15:28:01 +01002630 appctx->st0 = STAT_CLI_PROMPT;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002631 return 1;
2632 }
2633 else { /* unknown "del" parameter */
2634 appctx->ctx.cli.msg = "'add' only supports 'map'.\n";
2635 appctx->st0 = STAT_CLI_PRINT;
2636 return 1;
2637 }
Cyril Bontécd19e512010-01-31 22:34:03 +01002638 }
2639 else { /* not "show" nor "clear" nor "get" nor "set" nor "enable" nor "disable" */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002640 return 0;
2641 }
2642 return 1;
2643}
2644
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002645/* This I/O handler runs as an applet embedded in a stream interface. It is
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002646 * used to processes I/O from/to the stats unix socket. The system relies on a
2647 * state machine handling requests and various responses. We read a request,
2648 * then we process it and send the response, and we possibly display a prompt.
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002649 * Then we can read again. The state is stored in appctx->st0 and is one of the
2650 * STAT_CLI_* constants. appctx->st1 is used to indicate whether prompt is enabled
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002651 * or not.
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002652 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02002653static void cli_io_handler(struct appctx *appctx)
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002654{
Willy Tarreau00a37f02015-04-13 12:05:19 +02002655 struct stream_interface *si = appctx->owner;
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002656 struct channel *req = si_oc(si);
2657 struct channel *res = si_ic(si);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002658 int reql;
2659 int len;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002660
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002661 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
2662 goto out;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002663
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002664 while (1) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002665 if (appctx->st0 == STAT_CLI_INIT) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002666 /* Stats output not initialized yet */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002667 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
2668 appctx->st0 = STAT_CLI_GETREQ;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002669 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002670 else if (appctx->st0 == STAT_CLI_END) {
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002671 /* Let's close for real now. We just close the request
2672 * side, the conditions below will complete if needed.
2673 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02002674 si_shutw(si);
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002675 break;
2676 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002677 else if (appctx->st0 == STAT_CLI_GETREQ) {
Willy Tarreau4e33d862009-10-11 23:35:10 +02002678 /* ensure we have some output room left in the event we
2679 * would want to return some info right after parsing.
2680 */
Willy Tarreau4e4292b2014-11-28 12:18:45 +01002681 if (buffer_almost_full(si_ib(si))) {
Willy Tarreaufe127932015-04-21 19:23:39 +02002682 si_applet_cant_put(si);
Willy Tarreau4e33d862009-10-11 23:35:10 +02002683 break;
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01002684 }
Willy Tarreau4e33d862009-10-11 23:35:10 +02002685
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002686 reql = bo_getline(si_oc(si), trash.str, trash.size);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002687 if (reql <= 0) { /* closed or EOL not found */
2688 if (reql == 0)
2689 break;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002690 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002691 continue;
2692 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002693
Chad Lavoiee3f50312016-05-26 16:42:25 -04002694 /* seek for a possible unescaped semi-colon. If we find
2695 * one, we replace it with an LF and skip only this part.
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002696 */
Chad Lavoiee3f50312016-05-26 16:42:25 -04002697 for (len = 0; len < reql; len++) {
2698 if (trash.str[len] == '\\') {
2699 len++;
2700 continue;
2701 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002702 if (trash.str[len] == ';') {
2703 trash.str[len] = '\n';
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002704 reql = len + 1;
2705 break;
2706 }
Chad Lavoiee3f50312016-05-26 16:42:25 -04002707 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002708
Willy Tarreau816fc222009-10-04 07:36:58 +02002709 /* now it is time to check that we have a full line,
2710 * remove the trailing \n and possibly \r, then cut the
2711 * line.
2712 */
2713 len = reql - 1;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002714 if (trash.str[len] != '\n') {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002715 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002716 continue;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002717 }
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002718
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002719 if (len && trash.str[len-1] == '\r')
Willy Tarreau816fc222009-10-04 07:36:58 +02002720 len--;
2721
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002722 trash.str[len] = '\0';
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002723
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002724 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002725 if (len) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002726 if (strcmp(trash.str, "quit") == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002727 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002728 continue;
2729 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002730 else if (strcmp(trash.str, "prompt") == 0)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002731 appctx->st1 = !appctx->st1;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002732 else if (strcmp(trash.str, "help") == 0 ||
2733 !stats_sock_parse_request(si, trash.str)) {
William Lallemand1e08cd82016-10-13 17:57:55 +02002734 cli_gen_usage_msg();
2735 if (dynamic_usage_msg)
2736 appctx->ctx.cli.msg = dynamic_usage_msg;
2737 else
2738 appctx->ctx.cli.msg = stats_sock_usage_msg;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002739 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02002740 }
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002741 /* NB: stats_sock_parse_request() may have put
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002742 * another STAT_CLI_O_* into appctx->st0.
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002743 */
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002744 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002745 else if (!appctx->st1) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002746 /* if prompt is disabled, print help on empty lines,
2747 * so that the user at least knows how to enable
2748 * prompt and find help.
2749 */
William Lallemand1e08cd82016-10-13 17:57:55 +02002750 cli_gen_usage_msg();
2751 if (dynamic_usage_msg)
2752 appctx->ctx.cli.msg = dynamic_usage_msg;
2753 else
2754 appctx->ctx.cli.msg = stats_sock_usage_msg;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002755 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002756 }
2757
2758 /* re-adjust req buffer */
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002759 bo_skip(si_oc(si), reql);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002760 req->flags |= CF_READ_DONTWAIT; /* we plan to read small requests */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002761 }
Willy Tarreau68c00c72015-09-25 19:21:19 +02002762 else { /* output functions */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002763 switch (appctx->st0) {
Andrew Hayworthe32d1862015-10-02 15:08:10 +00002764 case STAT_CLI_PROMPT:
2765 break;
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02002766 case STAT_CLI_PRINT:
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002767 if (bi_putstr(si_ic(si), appctx->ctx.cli.msg) != -1)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002768 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreaubc18da12015-03-13 14:00:47 +01002769 else
Willy Tarreaufe127932015-04-21 19:23:39 +02002770 si_applet_cant_put(si);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002771 break;
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002772 case STAT_CLI_PRINT_FREE:
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002773 if (bi_putstr(si_ic(si), appctx->ctx.cli.err) != -1) {
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002774 free(appctx->ctx.cli.err);
2775 appctx->st0 = STAT_CLI_PROMPT;
2776 }
Willy Tarreaubc18da12015-03-13 14:00:47 +01002777 else
Willy Tarreaufe127932015-04-21 19:23:39 +02002778 si_applet_cant_put(si);
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002779 break;
Baptiste Assmann9b6857e2015-09-18 14:49:12 +02002780 case STAT_CLI_O_BACKEND:
2781 if (stats_dump_backend_to_buffer(si))
2782 appctx->st0 = STAT_CLI_PROMPT;
2783 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002784 case STAT_CLI_O_INFO:
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002785 if (stats_dump_info_to_buffer(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002786 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002787 break;
Baptiste Assmann28289462015-07-03 08:01:20 +02002788 case STAT_CLI_O_SERVERS_STATE:
2789 if (stats_dump_servers_state_to_buffer(si))
2790 appctx->st0 = STAT_CLI_PROMPT;
2791 break;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002792 case STAT_CLI_O_STAT:
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002793 if (stats_dump_stat_to_buffer(si, NULL))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002794 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002795 break;
Baptiste Assmann3863f972015-05-17 00:33:24 +02002796 case STAT_CLI_O_RESOLVERS:
2797 if (stats_dump_resolvers_to_buffer(si))
2798 appctx->st0 = STAT_CLI_PROMPT;
2799 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002800 case STAT_CLI_O_SESS:
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01002801 if (stats_dump_sess_to_buffer(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002802 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002803 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002804 case STAT_CLI_O_ERR: /* errors dump */
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01002805 if (stats_dump_errors_to_buffer(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002806 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002807 break;
Willy Tarreau69f58c82010-07-12 17:55:33 +02002808 case STAT_CLI_O_TAB:
Simon Hormanc88b8872011-06-15 15:18:49 +09002809 case STAT_CLI_O_CLR:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002810 if (stats_table_request(si, appctx->st0))
2811 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau69f58c82010-07-12 17:55:33 +02002812 break;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002813 case STAT_CLI_O_PATS:
2814 if (stats_pats_list(si))
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002815 appctx->st0 = STAT_CLI_PROMPT;
2816 break;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002817 case STAT_CLI_O_PAT:
2818 if (stats_pat_list(si))
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002819 appctx->st0 = STAT_CLI_PROMPT;
2820 break;
2821 case STAT_CLI_O_MLOOK:
2822 if (stats_map_lookup(si))
2823 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau4efb3532014-01-29 12:13:39 +01002824 break;
Willy Tarreau12833bb2014-01-28 16:49:56 +01002825 case STAT_CLI_O_POOLS:
2826 if (stats_dump_pools_to_buffer(si))
2827 appctx->st0 = STAT_CLI_PROMPT;
2828 break;
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02002829#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
2830 case STAT_CLI_O_TLSK:
2831 if (stats_tlskeys_list(si))
2832 appctx->st0 = STAT_CLI_PROMPT;
2833 break;
William Lallemand1d0b36a2016-05-20 17:40:26 +02002834 case STAT_CLI_O_TLSK_ENT:
2835 if (stats_tlskeys_list(si))
2836 appctx->st0 = STAT_CLI_PROMPT;
2837 break;
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02002838#endif
Willy Tarreauae795722016-02-16 11:27:28 +01002839 case STAT_CLI_O_ENV: /* environment dump */
2840 if (stats_dump_env_to_buffer(si))
2841 appctx->st0 = STAT_CLI_PROMPT;
2842 break;
William Lallemand1e08cd82016-10-13 17:57:55 +02002843 case STAT_CLI_O_CUSTOM: /* use custom pointer */
2844 if (appctx->io_handler)
2845 if (appctx->io_handler(appctx))
2846 appctx->st0 = STAT_CLI_PROMPT;
2847 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002848 default: /* abnormal state */
Willy Tarreau5cfa3bc2015-09-25 20:08:51 +02002849 si->flags |= SI_FL_ERR;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002850 break;
2851 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002852
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002853 /* The post-command prompt is either LF alone or LF + '> ' in interactive mode */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002854 if (appctx->st0 == STAT_CLI_PROMPT) {
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002855 if (bi_putstr(si_ic(si), appctx->st1 ? "\n> " : "\n") != -1)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002856 appctx->st0 = STAT_CLI_GETREQ;
Willy Tarreaubc18da12015-03-13 14:00:47 +01002857 else
Willy Tarreaufe127932015-04-21 19:23:39 +02002858 si_applet_cant_put(si);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002859 }
2860
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002861 /* If the output functions are still there, it means they require more room. */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002862 if (appctx->st0 >= STAT_CLI_OUTPUT)
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002863 break;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002864
2865 /* Now we close the output if one of the writers did so,
2866 * or if we're not in interactive mode and the request
2867 * buffer is empty. This still allows pipelined requests
2868 * to be sent in non-interactive mode.
2869 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002870 if ((res->flags & (CF_SHUTW|CF_SHUTW_NOW)) || (!appctx->st1 && !req->buf->o)) {
2871 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002872 continue;
2873 }
2874
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002875 /* switch state back to GETREQ to read next requests */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002876 appctx->st0 = STAT_CLI_GETREQ;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002877 }
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002878 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002879
Willy Tarreauc9e930a2015-09-25 20:06:08 +02002880 if ((res->flags & CF_SHUTR) && (si->state == SI_ST_EST)) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002881 DPRINTF(stderr, "%s@%d: si to buf closed. req=%08x, res=%08x, st=%d\n",
2882 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
Aman Guptaceafb4a2012-04-02 18:57:54 -07002883 /* Other side has closed, let's abort if we have no more processing to do
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002884 * and nothing more to consume. This is comparable to a broken pipe, so
2885 * we forward the close to the request side so that it flows upstream to
2886 * the client.
2887 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02002888 si_shutw(si);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002889 }
2890
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002891 if ((req->flags & CF_SHUTW) && (si->state == SI_ST_EST) && (appctx->st0 < STAT_CLI_OUTPUT)) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002892 DPRINTF(stderr, "%s@%d: buf to si closed. req=%08x, res=%08x, st=%d\n",
2893 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
2894 /* We have no more processing to do, and nothing more to send, and
2895 * the client side has closed. So we'll forward this state downstream
2896 * on the response buffer.
2897 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02002898 si_shutr(si);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002899 res->flags |= CF_READ_NULL;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002900 }
2901
Willy Tarreau828824a2015-04-19 17:20:03 +02002902 out:
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01002903 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 +02002904 __FUNCTION__, __LINE__,
Willy Tarreau9b28e032012-10-12 23:49:43 +02002905 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 +02002906}
2907
Willy Tarreau638d40a2016-02-24 23:11:01 +01002908/* Emits a stats field without any surrounding element and properly encoded to
2909 * resist CSV output. Returns non-zero on success, 0 if the buffer is full.
2910 */
Willy Tarreaub47785f2016-02-24 23:28:31 +01002911int stats_emit_raw_data_field(struct chunk *out, const struct field *f)
Willy Tarreau638d40a2016-02-24 23:11:01 +01002912{
2913 switch (field_format(f, 0)) {
2914 case FF_EMPTY: return 1;
2915 case FF_S32: return chunk_appendf(out, "%d", f->u.s32);
2916 case FF_U32: return chunk_appendf(out, "%u", f->u.u32);
2917 case FF_S64: return chunk_appendf(out, "%lld", (long long)f->u.s64);
2918 case FF_U64: return chunk_appendf(out, "%llu", (unsigned long long)f->u.u64);
2919 case FF_STR: return csv_enc_append(field_str(f, 0), 1, out) != NULL;
2920 default: return chunk_appendf(out, "[INCORRECT_FIELD_TYPE_%08x]", f->type);
2921 }
2922}
2923
Willy Tarreaub47785f2016-02-24 23:28:31 +01002924/* Emits a stats field prefixed with its type. No CSV encoding is prepared, the
2925 * output is supposed to be used on its own line. Returns non-zero on success, 0
2926 * if the buffer is full.
2927 */
2928int stats_emit_typed_data_field(struct chunk *out, const struct field *f)
2929{
2930 switch (field_format(f, 0)) {
2931 case FF_EMPTY: return 1;
2932 case FF_S32: return chunk_appendf(out, "s32:%d", f->u.s32);
2933 case FF_U32: return chunk_appendf(out, "u32:%u", f->u.u32);
2934 case FF_S64: return chunk_appendf(out, "s64:%lld", (long long)f->u.s64);
2935 case FF_U64: return chunk_appendf(out, "u64:%llu", (unsigned long long)f->u.u64);
2936 case FF_STR: return chunk_appendf(out, "str:%s", field_str(f, 0));
2937 default: return chunk_appendf(out, "%08x:?", f->type);
2938 }
2939}
2940
2941/* Emits an encoding of the field type on 3 characters followed by a delimiter.
2942 * Returns non-zero on success, 0 if the buffer is full.
2943 */
2944int stats_emit_field_tags(struct chunk *out, const struct field *f, char delim)
2945{
2946 char origin, nature, scope;
2947
2948 switch (field_origin(f, 0)) {
2949 case FO_METRIC: origin = 'M'; break;
2950 case FO_STATUS: origin = 'S'; break;
2951 case FO_KEY: origin = 'K'; break;
2952 case FO_CONFIG: origin = 'C'; break;
2953 case FO_PRODUCT: origin = 'P'; break;
2954 default: origin = '?'; break;
2955 }
2956
2957 switch (field_nature(f, 0)) {
2958 case FN_GAUGE: nature = 'G'; break;
2959 case FN_LIMIT: nature = 'L'; break;
2960 case FN_MIN: nature = 'm'; break;
2961 case FN_MAX: nature = 'M'; break;
2962 case FN_RATE: nature = 'R'; break;
2963 case FN_COUNTER: nature = 'C'; break;
2964 case FN_DURATION: nature = 'D'; break;
2965 case FN_AGE: nature = 'A'; break;
2966 case FN_TIME: nature = 'T'; break;
2967 case FN_NAME: nature = 'N'; break;
2968 case FN_OUTPUT: nature = 'O'; break;
2969 case FN_AVG: nature = 'a'; break;
2970 default: nature = '?'; break;
2971 }
2972
2973 switch (field_scope(f, 0)) {
2974 case FS_PROCESS: scope = 'P'; break;
2975 case FS_SERVICE: scope = 'S'; break;
2976 case FS_SYSTEM: scope = 's'; break;
2977 case FS_CLUSTER: scope = 'C'; break;
2978 default: scope = '?'; break;
2979 }
2980
2981 return chunk_appendf(out, "%c%c%c%c", origin, nature, scope, delim);
2982}
2983
Willy Tarreaubf95cba2016-01-11 18:27:29 +01002984/* Dump all fields from <info> into <out> using the "show info" format (name: value) */
2985static int stats_dump_info_fields(struct chunk *out, const struct field *info)
2986{
2987 int field;
2988
2989 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
2990 if (!field_format(info, field))
2991 continue;
2992
2993 if (!chunk_appendf(out, "%s: ", info_field_names[field]))
2994 return 0;
2995 if (!stats_emit_raw_data_field(out, &info[field]))
2996 return 0;
2997 if (!chunk_strcat(out, "\n"))
2998 return 0;
2999 }
3000 return 1;
3001}
3002
Willy Tarreaucb809122016-01-11 20:08:42 +01003003/* Dump all fields from <info> into <out> using the "show info typed" format */
3004static int stats_dump_typed_info_fields(struct chunk *out, const struct field *info)
3005{
3006 int field;
3007
3008 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
3009 if (!field_format(info, field))
3010 continue;
3011
3012 if (!chunk_appendf(out, "%d.%s.%u:", field, info_field_names[field], info[INF_PROCESS_NUM].u.u32))
3013 return 0;
3014 if (!stats_emit_field_tags(out, &info[field], ':'))
3015 return 0;
3016 if (!stats_emit_typed_data_field(out, &info[field]))
3017 return 0;
3018 if (!chunk_strcat(out, "\n"))
3019 return 0;
3020 }
3021 return 1;
3022}
3023
Thierry Fourniercb2c7672016-03-25 08:19:23 +01003024/* Fill <info> with HAProxy global info. <info> is preallocated
3025 * array of length <len>. The length of the aray must be
3026 * INF_TOTAL_FIELDS. If this length is less then this value, the
3027 * function returns 0, otherwise, it returns 1.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003028 */
Thierry Fourniercb2c7672016-03-25 08:19:23 +01003029int stats_fill_info(struct field *info, int len)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003030{
3031 unsigned int up = (now.tv_sec - start_date.tv_sec);
Willy Tarreau4529c072016-01-11 18:17:07 +01003032 struct chunk *out = get_trash_chunk();
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003033
Willy Tarreau0c9c2722014-05-28 12:28:58 +02003034#ifdef USE_OPENSSL
3035 int ssl_sess_rate = read_freq_ctr(&global.ssl_per_sec);
3036 int ssl_key_rate = read_freq_ctr(&global.ssl_fe_keys_per_sec);
3037 int ssl_reuse = 0;
3038
3039 if (ssl_key_rate < ssl_sess_rate) {
3040 /* count the ssl reuse ratio and avoid overflows in both directions */
3041 ssl_reuse = 100 - (100 * ssl_key_rate + (ssl_sess_rate - 1) / 2) / ssl_sess_rate;
3042 }
3043#endif
3044
Thierry Fourniercb2c7672016-03-25 08:19:23 +01003045 if (len < INF_TOTAL_FIELDS)
3046 return 0;
3047
Willy Tarreau4529c072016-01-11 18:17:07 +01003048 chunk_reset(out);
Thierry Fourniercb2c7672016-03-25 08:19:23 +01003049 memset(info, 0, sizeof(*info) * len);
Willy Tarreau4529c072016-01-11 18:17:07 +01003050
3051 info[INF_NAME] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, PRODUCT_NAME);
3052 info[INF_VERSION] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, HAPROXY_VERSION);
3053 info[INF_RELEASE_DATE] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, HAPROXY_DATE);
3054
3055 info[INF_NBPROC] = mkf_u32(FO_CONFIG|FS_SERVICE, global.nbproc);
3056 info[INF_PROCESS_NUM] = mkf_u32(FO_KEY, relative_pid);
3057 info[INF_PID] = mkf_u32(FO_STATUS, pid);
3058
3059 info[INF_UPTIME] = mkf_str(FN_DURATION, chunk_newstr(out));
3060 chunk_appendf(out, "%ud %uh%02um%02us", up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60));
3061
3062 info[INF_UPTIME_SEC] = mkf_u32(FN_DURATION, up);
3063 info[INF_MEMMAX_MB] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_memmax);
3064 info[INF_POOL_ALLOC_MB] = mkf_u32(0, (unsigned)(pool_total_allocated() / 1048576L));
3065 info[INF_POOL_USED_MB] = mkf_u32(0, (unsigned)(pool_total_used() / 1048576L));
3066 info[INF_POOL_FAILED] = mkf_u32(FN_COUNTER, pool_total_failures());
3067 info[INF_ULIMIT_N] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_nofile);
3068 info[INF_MAXSOCK] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxsock);
3069 info[INF_MAXCONN] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxconn);
3070 info[INF_HARD_MAXCONN] = mkf_u32(FO_CONFIG|FN_LIMIT, global.hardmaxconn);
3071 info[INF_CURR_CONN] = mkf_u32(0, actconn);
3072 info[INF_CUM_CONN] = mkf_u32(FN_COUNTER, totalconn);
3073 info[INF_CUM_REQ] = mkf_u32(FN_COUNTER, global.req_count);
3074#ifdef USE_OPENSSL
3075 info[INF_MAX_SSL_CONNS] = mkf_u32(FN_MAX, global.maxsslconn);
3076 info[INF_CURR_SSL_CONNS] = mkf_u32(0, sslconns);
3077 info[INF_CUM_SSL_CONNS] = mkf_u32(FN_COUNTER, totalsslconns);
3078#endif
3079 info[INF_MAXPIPES] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxpipes);
3080 info[INF_PIPES_USED] = mkf_u32(0, pipes_used);
3081 info[INF_PIPES_FREE] = mkf_u32(0, pipes_free);
3082 info[INF_CONN_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.conn_per_sec));
3083 info[INF_CONN_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.cps_lim);
3084 info[INF_MAX_CONN_RATE] = mkf_u32(FN_MAX, global.cps_max);
3085 info[INF_SESS_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.sess_per_sec));
3086 info[INF_SESS_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.sps_lim);
3087 info[INF_MAX_SESS_RATE] = mkf_u32(FN_RATE, global.sps_max);
3088
3089#ifdef USE_OPENSSL
3090 info[INF_SSL_RATE] = mkf_u32(FN_RATE, ssl_sess_rate);
3091 info[INF_SSL_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.ssl_lim);
3092 info[INF_MAX_SSL_RATE] = mkf_u32(FN_MAX, global.ssl_max);
3093 info[INF_SSL_FRONTEND_KEY_RATE] = mkf_u32(0, ssl_key_rate);
3094 info[INF_SSL_FRONTEND_MAX_KEY_RATE] = mkf_u32(FN_MAX, global.ssl_fe_keys_max);
3095 info[INF_SSL_FRONTEND_SESSION_REUSE_PCT] = mkf_u32(0, ssl_reuse);
3096 info[INF_SSL_BACKEND_KEY_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.ssl_be_keys_per_sec));
3097 info[INF_SSL_BACKEND_MAX_KEY_RATE] = mkf_u32(FN_MAX, global.ssl_be_keys_max);
3098 info[INF_SSL_CACHE_LOOKUPS] = mkf_u32(FN_COUNTER, global.shctx_lookups);
3099 info[INF_SSL_CACHE_MISSES] = mkf_u32(FN_COUNTER, global.shctx_misses);
3100#endif
3101 info[INF_COMPRESS_BPS_IN] = mkf_u32(FN_RATE, read_freq_ctr(&global.comp_bps_in));
3102 info[INF_COMPRESS_BPS_OUT] = mkf_u32(FN_RATE, read_freq_ctr(&global.comp_bps_out));
3103 info[INF_COMPRESS_BPS_RATE_LIM] = mkf_u32(FO_CONFIG|FN_LIMIT, global.comp_rate_lim);
3104#ifdef USE_ZLIB
3105 info[INF_ZLIB_MEM_USAGE] = mkf_u32(0, zlib_used_memory);
3106 info[INF_MAX_ZLIB_MEM_USAGE] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxzlibmem);
3107#endif
3108 info[INF_TASKS] = mkf_u32(0, nb_tasks_cur);
3109 info[INF_RUN_QUEUE] = mkf_u32(0, run_queue_cur);
3110 info[INF_IDLE_PCT] = mkf_u32(FN_AVG, idle_pct);
3111 info[INF_NODE] = mkf_str(FO_CONFIG|FN_OUTPUT|FS_SERVICE, global.node);
3112 if (global.desc)
3113 info[INF_DESCRIPTION] = mkf_str(FO_CONFIG|FN_OUTPUT|FS_SERVICE, global.desc);
3114
Thierry Fourniercb2c7672016-03-25 08:19:23 +01003115 return 1;
3116}
3117
3118/* This function dumps information onto the stream interface's read buffer.
3119 * It returns 0 as long as it does not complete, non-zero upon completion.
3120 * No state is used.
3121 */
3122static int stats_dump_info_to_buffer(struct stream_interface *si)
3123{
3124 struct appctx *appctx = __objt_appctx(si->end);
3125
3126 if (!stats_fill_info(info, INF_TOTAL_FIELDS))
3127 return 0;
3128
Willy Tarreau1b4ba1e2016-01-11 18:29:04 +01003129 chunk_reset(&trash);
Willy Tarreaucb809122016-01-11 20:08:42 +01003130
3131 if (appctx->ctx.stats.flags & STAT_FMT_TYPED)
3132 stats_dump_typed_info_fields(&trash, info);
3133 else
3134 stats_dump_info_fields(&trash, info);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003135
Willy Tarreaubc18da12015-03-13 14:00:47 +01003136 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02003137 si_applet_cant_put(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003138 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01003139 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003140
3141 return 1;
3142}
3143
Baptiste Assmann28289462015-07-03 08:01:20 +02003144/* dumps server state information into <buf> for all the servers found in <backend>
3145 * These information are all the parameters which may change during HAProxy runtime.
3146 * By default, we only export to the last known server state file format.
3147 * These information can be used at next startup to recover same level of server state.
3148 */
Cyril Bonté76a99782016-05-06 12:18:48 +02003149static int dump_servers_state(struct stream_interface *si, struct chunk *buf)
Baptiste Assmann28289462015-07-03 08:01:20 +02003150{
Cyril Bonté76a99782016-05-06 12:18:48 +02003151 struct appctx *appctx = __objt_appctx(si->end);
Baptiste Assmann28289462015-07-03 08:01:20 +02003152 struct server *srv;
3153 char srv_addr[INET6_ADDRSTRLEN + 1];
3154 time_t srv_time_since_last_change;
3155 int bk_f_forced_id, srv_f_forced_id;
3156
Baptiste Assmann28289462015-07-03 08:01:20 +02003157
Cyril Bonté76a99782016-05-06 12:18:48 +02003158 /* we don't want to report any state if the backend is not enabled on this process */
3159 if (appctx->ctx.server_state.px->bind_proc && !(appctx->ctx.server_state.px->bind_proc & (1UL << (relative_pid - 1))))
3160 return 1;
Baptiste Assmann28289462015-07-03 08:01:20 +02003161
Cyril Bontéd55bd7a2016-05-27 00:06:45 +02003162 if (!appctx->ctx.server_state.sv)
3163 appctx->ctx.server_state.sv = appctx->ctx.server_state.px->srv;
3164
Cyril Bonté76a99782016-05-06 12:18:48 +02003165 for (; appctx->ctx.server_state.sv != NULL; appctx->ctx.server_state.sv = srv->next) {
3166 srv = appctx->ctx.server_state.sv;
Baptiste Assmann28289462015-07-03 08:01:20 +02003167 srv_addr[0] = '\0';
Baptiste Assmann28289462015-07-03 08:01:20 +02003168
3169 switch (srv->addr.ss_family) {
3170 case AF_INET:
3171 inet_ntop(srv->addr.ss_family, &((struct sockaddr_in *)&srv->addr)->sin_addr,
3172 srv_addr, INET_ADDRSTRLEN + 1);
3173 break;
3174 case AF_INET6:
3175 inet_ntop(srv->addr.ss_family, &((struct sockaddr_in6 *)&srv->addr)->sin6_addr,
3176 srv_addr, INET6_ADDRSTRLEN + 1);
3177 break;
3178 }
3179 srv_time_since_last_change = now.tv_sec - srv->last_change;
Cyril Bonté76a99782016-05-06 12:18:48 +02003180 bk_f_forced_id = appctx->ctx.server_state.px->options & PR_O_FORCED_ID ? 1 : 0;
Baptiste Assmann28289462015-07-03 08:01:20 +02003181 srv_f_forced_id = srv->flags & SRV_F_FORCED_ID ? 1 : 0;
3182
3183 chunk_appendf(buf,
3184 "%d %s "
3185 "%d %s %s "
3186 "%d %d %d %d %ld "
3187 "%d %d %d %d %d "
3188 "%d %d"
3189 "\n",
Cyril Bonté76a99782016-05-06 12:18:48 +02003190 appctx->ctx.server_state.px->uuid, appctx->ctx.server_state.px->id,
Baptiste Assmann28289462015-07-03 08:01:20 +02003191 srv->puid, srv->id, srv_addr,
David Carlier081b3362015-11-18 06:10:22 +00003192 srv->state, srv->admin, srv->uweight, srv->iweight, (long int)srv_time_since_last_change,
Baptiste Assmann28289462015-07-03 08:01:20 +02003193 srv->check.status, srv->check.result, srv->check.health, srv->check.state, srv->agent.state,
3194 bk_f_forced_id, srv_f_forced_id);
Cyril Bonté76a99782016-05-06 12:18:48 +02003195 if (bi_putchk(si_ic(si), &trash) == -1) {
3196 si_applet_cant_put(si);
3197 return 0;
3198 }
Baptiste Assmann28289462015-07-03 08:01:20 +02003199 }
Cyril Bonté76a99782016-05-06 12:18:48 +02003200 return 1;
Baptiste Assmann28289462015-07-03 08:01:20 +02003201}
3202
Baptiste Assmann9b6857e2015-09-18 14:49:12 +02003203/* Parses backend list and simply report backend names */
3204static int stats_dump_backend_to_buffer(struct stream_interface *si)
3205{
Cyril Bonté6ca9e012016-05-06 12:18:49 +02003206 struct appctx *appctx = __objt_appctx(si->end);
Baptiste Assmann9b6857e2015-09-18 14:49:12 +02003207 extern struct proxy *proxy;
3208 struct proxy *curproxy;
3209
3210 chunk_reset(&trash);
Baptiste Assmann9b6857e2015-09-18 14:49:12 +02003211
Cyril Bonté6ca9e012016-05-06 12:18:49 +02003212 if (!appctx->ctx.be.px) {
3213 chunk_printf(&trash, "# name\n");
3214 if (bi_putchk(si_ic(si), &trash) == -1) {
3215 si_applet_cant_put(si);
3216 return 0;
3217 }
3218 appctx->ctx.be.px = proxy;
3219 }
3220
3221 for (; appctx->ctx.be.px != NULL; appctx->ctx.be.px = curproxy->next) {
3222 curproxy = appctx->ctx.be.px;
3223
Baptiste Assmann9b6857e2015-09-18 14:49:12 +02003224 /* looking for backends only */
3225 if (!(curproxy->cap & PR_CAP_BE))
3226 continue;
3227
3228 /* we don't want to list a backend which is bound to this process */
3229 if (curproxy->bind_proc && !(curproxy->bind_proc & (1UL << (relative_pid - 1))))
3230 continue;
3231
3232 chunk_appendf(&trash, "%s\n", curproxy->id);
Cyril Bonté6ca9e012016-05-06 12:18:49 +02003233 if (bi_putchk(si_ic(si), &trash) == -1) {
3234 si_applet_cant_put(si);
3235 return 0;
3236 }
Baptiste Assmann9b6857e2015-09-18 14:49:12 +02003237 }
3238
3239 return 1;
3240}
3241
Baptiste Assmann28289462015-07-03 08:01:20 +02003242/* Parses backend list or simply use backend name provided by the user to return
3243 * states of servers to stdout.
3244 */
3245static int stats_dump_servers_state_to_buffer(struct stream_interface *si)
3246{
3247 struct appctx *appctx = __objt_appctx(si->end);
3248 extern struct proxy *proxy;
3249 struct proxy *curproxy;
3250
3251 chunk_reset(&trash);
3252
Willy Tarreaua58c4352016-06-22 14:51:40 +02003253 if (appctx->st2 == STAT_ST_INIT) {
3254 if (!appctx->ctx.server_state.px)
3255 appctx->ctx.server_state.px = proxy;
3256 appctx->st2 = STAT_ST_HEAD;
3257 }
3258
3259 if (appctx->st2 == STAT_ST_HEAD) {
Cyril Bonté76a99782016-05-06 12:18:48 +02003260 chunk_printf(&trash, "%d\n# %s\n", SRV_STATE_FILE_VERSION, SRV_STATE_FILE_FIELD_NAMES);
3261 if (bi_putchk(si_ic(si), &trash) == -1) {
3262 si_applet_cant_put(si);
3263 return 0;
Baptiste Assmann28289462015-07-03 08:01:20 +02003264 }
Willy Tarreaua58c4352016-06-22 14:51:40 +02003265 appctx->st2 = STAT_ST_INFO;
Baptiste Assmann28289462015-07-03 08:01:20 +02003266 }
3267
Willy Tarreaua58c4352016-06-22 14:51:40 +02003268 /* STAT_ST_INFO */
Cyril Bonté76a99782016-05-06 12:18:48 +02003269 for (; appctx->ctx.server_state.px != NULL; appctx->ctx.server_state.px = curproxy->next) {
3270 curproxy = appctx->ctx.server_state.px;
Cyril Bonté76a99782016-05-06 12:18:48 +02003271 /* servers are only in backends */
3272 if (curproxy->cap & PR_CAP_BE) {
3273 if (!dump_servers_state(si, &trash))
3274 return 0;
3275
3276 if (bi_putchk(si_ic(si), &trash) == -1) {
3277 si_applet_cant_put(si);
3278 return 0;
3279 }
3280 }
3281 /* only the selected proxy is dumped */
3282 if (appctx->ctx.server_state.iid)
3283 break;
Baptiste Assmann28289462015-07-03 08:01:20 +02003284 }
3285
3286 return 1;
3287}
3288
Willy Tarreau12833bb2014-01-28 16:49:56 +01003289/* This function dumps memory usage information onto the stream interface's
3290 * read buffer. It returns 0 as long as it does not complete, non-zero upon
3291 * completion. No state is used.
3292 */
3293static int stats_dump_pools_to_buffer(struct stream_interface *si)
3294{
3295 dump_pools_to_trash();
Willy Tarreaubc18da12015-03-13 14:00:47 +01003296 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02003297 si_applet_cant_put(si);
Willy Tarreau12833bb2014-01-28 16:49:56 +01003298 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01003299 }
Willy Tarreau82a86022016-01-04 19:04:18 +01003300 return 1;
3301}
3302
3303/* Dump all fields from <stats> into <out> using CSV format */
3304static int stats_dump_fields_csv(struct chunk *out, const struct field *stats)
3305{
3306 int field;
3307
3308 for (field = 0; field < ST_F_TOTAL_FIELDS; field++) {
3309 if (!stats_emit_raw_data_field(out, &stats[field]))
3310 return 0;
3311 if (!chunk_strcat(out, ","))
3312 return 0;
3313 }
Conrad Hoffmann692c9382016-04-01 20:40:58 +02003314 chunk_strcat(out, "\n");
Willy Tarreau12833bb2014-01-28 16:49:56 +01003315 return 1;
3316}
3317
Willy Tarreau1e62df92016-01-11 18:57:53 +01003318/* Dump all fields from <stats> into <out> using a typed "field:desc:type:value" format */
3319static int stats_dump_fields_typed(struct chunk *out, const struct field *stats)
3320{
3321 int field;
3322
3323 for (field = 0; field < ST_F_TOTAL_FIELDS; field++) {
3324 if (!stats[field].type)
3325 continue;
3326
3327 chunk_appendf(out, "%c.%u.%u.%d.%s.%u:",
3328 stats[ST_F_TYPE].u.u32 == STATS_TYPE_FE ? 'F' :
3329 stats[ST_F_TYPE].u.u32 == STATS_TYPE_BE ? 'B' :
3330 stats[ST_F_TYPE].u.u32 == STATS_TYPE_SO ? 'L' :
3331 stats[ST_F_TYPE].u.u32 == STATS_TYPE_SV ? 'S' :
3332 '?',
3333 stats[ST_F_IID].u.u32, stats[ST_F_SID].u.u32,
3334 field, stat_field_names[field], stats[ST_F_PID].u.u32);
3335
3336 if (!stats_emit_field_tags(out, &stats[field], ':'))
3337 return 0;
3338 if (!stats_emit_typed_data_field(out, &stats[field]))
3339 return 0;
3340 if (!chunk_strcat(out, "\n"))
3341 return 0;
3342 }
3343 return 1;
3344}
3345
Willy Tarreau60600742016-01-11 15:32:30 +01003346/* Dump all fields from <stats> into <out> using the HTML format. A column is
Willy Tarreau508a63f2016-01-11 15:28:40 +01003347 * reserved for the checkbox is ST_SHOWADMIN is set in <flags>. Some extra info
3348 * are provided if ST_SHLGNDS is present in <flags>.
Cyril Bonté70be45d2010-10-12 00:14:35 +02003349 */
Willy Tarreau60600742016-01-11 15:32:30 +01003350static int stats_dump_fields_html(struct chunk *out, const struct field *stats, unsigned int flags)
Cyril Bonté70be45d2010-10-12 00:14:35 +02003351{
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003352 struct chunk src;
3353
Willy Tarreaub5f66b82016-01-08 17:15:39 +01003354 if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_FE) {
Willy Tarreau60600742016-01-11 15:32:30 +01003355 chunk_appendf(out,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003356 /* name, queue */
3357 "<tr class=\"frontend\">");
Cyril Bonté70be45d2010-10-12 00:14:35 +02003358
Willy Tarreau508a63f2016-01-11 15:28:40 +01003359 if (flags & ST_SHOWADMIN) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003360 /* Column sub-heading for Enable or Disable server */
Willy Tarreau60600742016-01-11 15:32:30 +01003361 chunk_appendf(out, "<td></td>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003362 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02003363
Willy Tarreau60600742016-01-11 15:32:30 +01003364 chunk_appendf(out,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003365 "<td class=ac>"
3366 "<a name=\"%s/Frontend\"></a>"
3367 "<a class=lfsb href=\"#%s/Frontend\">Frontend</a></td>"
3368 "<td colspan=3></td>"
3369 "",
Willy Tarreaud72c9172016-01-06 15:26:40 +01003370 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_PXNAME));
Cyril Bonté70be45d2010-10-12 00:14:35 +02003371
Willy Tarreau60600742016-01-11 15:32:30 +01003372 chunk_appendf(out,
Willy Tarreau466c9b52012-12-23 02:25:03 +01003373 /* sessions rate : current */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003374 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01003375 "<tr><th>Current connection rate:</th><td>%s/s</td></tr>"
3376 "<tr><th>Current session rate:</th><td>%s/s</td></tr>"
3377 "",
Willy Tarreaud72c9172016-01-06 15:26:40 +01003378 U2H(stats[ST_F_RATE].u.u32),
Willy Tarreauc73810f2016-01-11 13:52:04 +01003379 U2H(stats[ST_F_CONN_RATE].u.u32),
Willy Tarreaud72c9172016-01-06 15:26:40 +01003380 U2H(stats[ST_F_RATE].u.u32));
Willy Tarreau466c9b52012-12-23 02:25:03 +01003381
Willy Tarreauf8211df2016-01-11 14:09:38 +01003382 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
Willy Tarreau60600742016-01-11 15:32:30 +01003383 chunk_appendf(out,
Willy Tarreau466c9b52012-12-23 02:25:03 +01003384 "<tr><th>Current request rate:</th><td>%s/s</td></tr>",
Willy Tarreaud72c9172016-01-06 15:26:40 +01003385 U2H(stats[ST_F_REQ_RATE].u.u32));
Willy Tarreau466c9b52012-12-23 02:25:03 +01003386
Willy Tarreau60600742016-01-11 15:32:30 +01003387 chunk_appendf(out,
Willy Tarreau466c9b52012-12-23 02:25:03 +01003388 "</table></div></u></td>"
3389 /* sessions rate : max */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003390 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01003391 "<tr><th>Max connection rate:</th><td>%s/s</td></tr>"
3392 "<tr><th>Max session rate:</th><td>%s/s</td></tr>"
3393 "",
Willy Tarreaud72c9172016-01-06 15:26:40 +01003394 U2H(stats[ST_F_RATE_MAX].u.u32),
Willy Tarreauc73810f2016-01-11 13:52:04 +01003395 U2H(stats[ST_F_CONN_RATE_MAX].u.u32),
Willy Tarreaud72c9172016-01-06 15:26:40 +01003396 U2H(stats[ST_F_RATE_MAX].u.u32));
Willy Tarreau466c9b52012-12-23 02:25:03 +01003397
Willy Tarreauf8211df2016-01-11 14:09:38 +01003398 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
Willy Tarreau60600742016-01-11 15:32:30 +01003399 chunk_appendf(out,
Willy Tarreau466c9b52012-12-23 02:25:03 +01003400 "<tr><th>Max request rate:</th><td>%s/s</td></tr>",
Willy Tarreaud72c9172016-01-06 15:26:40 +01003401 U2H(stats[ST_F_REQ_RATE_MAX].u.u32));
Willy Tarreau466c9b52012-12-23 02:25:03 +01003402
Willy Tarreau60600742016-01-11 15:32:30 +01003403 chunk_appendf(out,
Willy Tarreau466c9b52012-12-23 02:25:03 +01003404 "</table></div></u></td>"
3405 /* sessions rate : limit */
3406 "<td>%s</td>",
Willy Tarreaud72c9172016-01-06 15:26:40 +01003407 LIM2A(stats[ST_F_RATE_LIM].u.u32, "-"));
Cyril Bonté70be45d2010-10-12 00:14:35 +02003408
Willy Tarreau60600742016-01-11 15:32:30 +01003409 chunk_appendf(out,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003410 /* sessions: current, max, limit, total */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003411 "<td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003412 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01003413 "<tr><th>Cum. connections:</th><td>%s</td></tr>"
3414 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003415 "",
Willy Tarreauc73810f2016-01-11 13:52:04 +01003416 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 +01003417 U2H(stats[ST_F_STOT].u.u64),
Willy Tarreauc73810f2016-01-11 13:52:04 +01003418 U2H(stats[ST_F_CONN_TOT].u.u64),
Willy Tarreaud72c9172016-01-06 15:26:40 +01003419 U2H(stats[ST_F_STOT].u.u64));
Cyril Bonté70be45d2010-10-12 00:14:35 +02003420
Willy Tarreau466c9b52012-12-23 02:25:03 +01003421 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
Willy Tarreauf8211df2016-01-11 14:09:38 +01003422 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
Willy Tarreau60600742016-01-11 15:32:30 +01003423 chunk_appendf(out,
Willy Tarreau466c9b52012-12-23 02:25:03 +01003424 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
3425 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
3426 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
3427 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
3428 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
3429 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
3430 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
3431 "<tr><th>- other responses:</th><td>%s</td></tr>"
3432 "<tr><th>Intercepted requests:</th><td>%s</td></tr>"
3433 "",
Willy Tarreaud72c9172016-01-06 15:26:40 +01003434 U2H(stats[ST_F_REQ_TOT].u.u64),
3435 U2H(stats[ST_F_HRSP_1XX].u.u64),
3436 U2H(stats[ST_F_HRSP_2XX].u.u64),
3437 U2H(stats[ST_F_COMP_RSP].u.u64),
3438 stats[ST_F_HRSP_2XX].u.u64 ?
3439 (int)(100 * stats[ST_F_COMP_RSP].u.u64 / stats[ST_F_HRSP_2XX].u.u64) : 0,
3440 U2H(stats[ST_F_HRSP_3XX].u.u64),
3441 U2H(stats[ST_F_HRSP_4XX].u.u64),
3442 U2H(stats[ST_F_HRSP_5XX].u.u64),
3443 U2H(stats[ST_F_HRSP_OTHER].u.u64),
Willy Tarreau5b9bdff2016-01-11 14:40:47 +01003444 U2H(stats[ST_F_INTERCEPTED].u.u64));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003445 }
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02003446
Willy Tarreau60600742016-01-11 15:32:30 +01003447 chunk_appendf(out,
Willy Tarreau466c9b52012-12-23 02:25:03 +01003448 "</table></div></u></td>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05003449 /* sessions: lbtot, lastsess */
3450 "<td></td><td></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003451 /* bytes : in */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003452 "<td>%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003453 "",
Willy Tarreaud72c9172016-01-06 15:26:40 +01003454 U2H(stats[ST_F_BIN].u.u64));
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02003455
Willy Tarreau60600742016-01-11 15:32:30 +01003456 chunk_appendf(out,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003457 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
Willy Tarreau4e5d58e2015-07-04 14:35:15 +02003458 "<td>%s%s<div class=tips><table class=det>"
3459 "<tr><th>Response bytes in:</th><td>%s</td></tr>"
3460 "<tr><th>Compression in:</th><td>%s</td></tr>"
3461 "<tr><th>Compression out:</th><td>%s</td><td>(%d%%)</td></tr>"
3462 "<tr><th>Compression bypass:</th><td>%s</td></tr>"
3463 "<tr><th>Total bytes saved:</th><td>%s</td><td>(%d%%)</td></tr>"
3464 "</table></div>%s</td>",
Willy Tarreaud72c9172016-01-06 15:26:40 +01003465 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "<u>":"",
3466 U2H(stats[ST_F_BOUT].u.u64),
3467 U2H(stats[ST_F_BOUT].u.u64),
3468 U2H(stats[ST_F_COMP_IN].u.u64),
3469 U2H(stats[ST_F_COMP_OUT].u.u64),
3470 stats[ST_F_COMP_IN].u.u64 ? (int)(stats[ST_F_COMP_OUT].u.u64 * 100 / stats[ST_F_COMP_IN].u.u64) : 0,
3471 U2H(stats[ST_F_COMP_BYP].u.u64),
3472 U2H(stats[ST_F_COMP_IN].u.u64 - stats[ST_F_COMP_OUT].u.u64),
3473 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,
3474 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "</u>":"");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02003475
Willy Tarreau60600742016-01-11 15:32:30 +01003476 chunk_appendf(out,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003477 /* denied: req, resp */
3478 "<td>%s</td><td>%s</td>"
3479 /* errors : request, connect, response */
3480 "<td>%s</td><td></td><td></td>"
3481 /* warnings: retries, redispatches */
3482 "<td></td><td></td>"
3483 /* server status : reflect frontend status */
3484 "<td class=ac>%s</td>"
3485 /* rest of server: nothing */
3486 "<td class=ac colspan=8></td></tr>"
3487 "",
Willy Tarreaud72c9172016-01-06 15:26:40 +01003488 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
3489 U2H(stats[ST_F_EREQ].u.u64),
3490 field_str(stats, ST_F_STATUS));
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02003491 }
Willy Tarreaufa9512f2016-01-08 17:20:51 +01003492 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_SO) {
Willy Tarreau60600742016-01-11 15:32:30 +01003493 chunk_appendf(out, "<tr class=socket>");
Willy Tarreau508a63f2016-01-11 15:28:40 +01003494 if (flags & ST_SHOWADMIN) {
Willy Tarreaufa9512f2016-01-08 17:20:51 +01003495 /* Column sub-heading for Enable or Disable server */
Willy Tarreau60600742016-01-11 15:32:30 +01003496 chunk_appendf(out, "<td></td>");
Willy Tarreaufa9512f2016-01-08 17:20:51 +01003497 }
3498
Willy Tarreau60600742016-01-11 15:32:30 +01003499 chunk_appendf(out,
Willy Tarreaufa9512f2016-01-08 17:20:51 +01003500 /* frontend name, listener name */
3501 "<td class=ac><a name=\"%s/+%s\"></a>%s"
3502 "<a class=lfsb href=\"#%s/+%s\">%s</a>"
3503 "",
3504 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME),
3505 (flags & ST_SHLGNDS)?"<u>":"",
3506 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME), field_str(stats, ST_F_SVNAME));
3507
3508 if (flags & ST_SHLGNDS) {
Willy Tarreau60600742016-01-11 15:32:30 +01003509 chunk_appendf(out, "<div class=tips>");
Willy Tarreaufa9512f2016-01-08 17:20:51 +01003510
3511 if (isdigit(*field_str(stats, ST_F_ADDR)))
Willy Tarreau60600742016-01-11 15:32:30 +01003512 chunk_appendf(out, "IPv4: %s, ", field_str(stats, ST_F_ADDR));
Willy Tarreaufa9512f2016-01-08 17:20:51 +01003513 else if (*field_str(stats, ST_F_ADDR) == '[')
Willy Tarreau60600742016-01-11 15:32:30 +01003514 chunk_appendf(out, "IPv6: %s, ", field_str(stats, ST_F_ADDR));
Willy Tarreaufa9512f2016-01-08 17:20:51 +01003515 else if (*field_str(stats, ST_F_ADDR))
Willy Tarreau60600742016-01-11 15:32:30 +01003516 chunk_appendf(out, "%s, ", field_str(stats, ST_F_ADDR));
Willy Tarreaufa9512f2016-01-08 17:20:51 +01003517
3518 /* id */
Willy Tarreau60600742016-01-11 15:32:30 +01003519 chunk_appendf(out, "id: %d</div>", stats[ST_F_SID].u.u32);
Willy Tarreaufa9512f2016-01-08 17:20:51 +01003520 }
3521
Willy Tarreau60600742016-01-11 15:32:30 +01003522 chunk_appendf(out,
Willy Tarreaufa9512f2016-01-08 17:20:51 +01003523 /* queue */
3524 "%s</td><td colspan=3></td>"
3525 /* sessions rate: current, max, limit */
3526 "<td colspan=3>&nbsp;</td>"
3527 /* sessions: current, max, limit, total, lbtot, lastsess */
3528 "<td>%s</td><td>%s</td><td>%s</td>"
3529 "<td>%s</td><td>&nbsp;</td><td>&nbsp;</td>"
3530 /* bytes: in, out */
3531 "<td>%s</td><td>%s</td>"
3532 "",
3533 (flags & ST_SHLGNDS)?"</u>":"",
3534 U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SMAX].u.u32), U2H(stats[ST_F_SLIM].u.u32),
3535 U2H(stats[ST_F_STOT].u.u64), U2H(stats[ST_F_BIN].u.u64), U2H(stats[ST_F_BOUT].u.u64));
3536
Willy Tarreau60600742016-01-11 15:32:30 +01003537 chunk_appendf(out,
Willy Tarreaufa9512f2016-01-08 17:20:51 +01003538 /* denied: req, resp */
3539 "<td>%s</td><td>%s</td>"
3540 /* errors: request, connect, response */
3541 "<td>%s</td><td></td><td></td>"
3542 /* warnings: retries, redispatches */
3543 "<td></td><td></td>"
3544 /* server status: reflect listener status */
3545 "<td class=ac>%s</td>"
3546 /* rest of server: nothing */
3547 "<td class=ac colspan=8></td></tr>"
3548 "",
3549 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
3550 U2H(stats[ST_F_EREQ].u.u64),
3551 field_str(stats, ST_F_STATUS));
3552 }
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003553 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_SV) {
3554 const char *style;
Willy Tarreaub5f66b82016-01-08 17:15:39 +01003555
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003556 /* determine the style to use depending on the server's state,
3557 * its health and weight. There isn't a 1-to-1 mapping between
3558 * state and styles for the cases where the server is (still)
3559 * up. The reason is that we don't want to report nolb and
3560 * drain with the same color.
3561 */
Willy Tarreaub5f66b82016-01-08 17:15:39 +01003562
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003563 if (strcmp(field_str(stats, ST_F_STATUS), "DOWN") == 0 ||
3564 strcmp(field_str(stats, ST_F_STATUS), "DOWN (agent)") == 0) {
3565 style = "down";
3566 }
3567 else if (strcmp(field_str(stats, ST_F_STATUS), "DOWN ") == 0) {
3568 style = "going_up";
3569 }
3570 else if (strcmp(field_str(stats, ST_F_STATUS), "NOLB ") == 0) {
3571 style = "going_down";
3572 }
3573 else if (strcmp(field_str(stats, ST_F_STATUS), "NOLB") == 0) {
3574 style = "nolb";
3575 }
3576 else if (strcmp(field_str(stats, ST_F_STATUS), "no check") == 0) {
3577 style = "no_check";
3578 }
3579 else if (!stats[ST_F_CHKFAIL].type ||
3580 stats[ST_F_CHECK_HEALTH].u.u32 == stats[ST_F_CHECK_RISE].u.u32 + stats[ST_F_CHECK_FALL].u.u32 - 1) {
3581 /* no check or max health = UP */
3582 if (stats[ST_F_WEIGHT].u.u32)
3583 style = "up";
3584 else
3585 style = "draining";
3586 }
3587 else {
3588 style = "going_down";
3589 }
Willy Tarreaub5f66b82016-01-08 17:15:39 +01003590
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003591 if (memcmp(field_str(stats, ST_F_STATUS), "MAINT", 5) == 0)
Willy Tarreau60600742016-01-11 15:32:30 +01003592 chunk_appendf(out, "<tr class=\"maintain\">");
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003593 else
Willy Tarreau60600742016-01-11 15:32:30 +01003594 chunk_appendf(out,
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003595 "<tr class=\"%s_%s\">",
3596 (stats[ST_F_BCK].u.u32) ? "backup" : "active", style);
Willy Tarreaub5f66b82016-01-08 17:15:39 +01003597
Willy Tarreaub5f66b82016-01-08 17:15:39 +01003598
Willy Tarreau508a63f2016-01-11 15:28:40 +01003599 if (flags & ST_SHOWADMIN)
Willy Tarreau60600742016-01-11 15:32:30 +01003600 chunk_appendf(out,
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003601 "<td><input type=\"checkbox\" name=\"s\" value=\"%s\"></td>",
3602 field_str(stats, ST_F_SVNAME));
Willy Tarreaub5f66b82016-01-08 17:15:39 +01003603
Willy Tarreau60600742016-01-11 15:32:30 +01003604 chunk_appendf(out,
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003605 "<td class=ac><a name=\"%s/%s\"></a>%s"
3606 "<a class=lfsb href=\"#%s/%s\">%s</a>"
3607 "",
3608 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME),
3609 (flags & ST_SHLGNDS) ? "<u>" : "",
3610 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 +01003611
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003612 if (flags & ST_SHLGNDS) {
Willy Tarreau60600742016-01-11 15:32:30 +01003613 chunk_appendf(out, "<div class=tips>");
Willy Tarreaub5f66b82016-01-08 17:15:39 +01003614
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003615 if (isdigit(*field_str(stats, ST_F_ADDR)))
Willy Tarreau60600742016-01-11 15:32:30 +01003616 chunk_appendf(out, "IPv4: %s, ", field_str(stats, ST_F_ADDR));
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003617 else if (*field_str(stats, ST_F_ADDR) == '[')
Willy Tarreau60600742016-01-11 15:32:30 +01003618 chunk_appendf(out, "IPv6: %s, ", field_str(stats, ST_F_ADDR));
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003619 else if (*field_str(stats, ST_F_ADDR))
Willy Tarreau60600742016-01-11 15:32:30 +01003620 chunk_appendf(out, "%s, ", field_str(stats, ST_F_ADDR));
Willy Tarreaub5f66b82016-01-08 17:15:39 +01003621
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003622 /* id */
Willy Tarreau60600742016-01-11 15:32:30 +01003623 chunk_appendf(out, "id: %d", stats[ST_F_SID].u.u32);
Willy Tarreaub5f66b82016-01-08 17:15:39 +01003624
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003625 /* cookie */
3626 if (stats[ST_F_COOKIE].type) {
Willy Tarreau60600742016-01-11 15:32:30 +01003627 chunk_appendf(out, ", cookie: '");
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003628 chunk_initstr(&src, field_str(stats, ST_F_COOKIE));
Willy Tarreau60600742016-01-11 15:32:30 +01003629 chunk_htmlencode(out, &src);
3630 chunk_appendf(out, "'");
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003631 }
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02003632
Willy Tarreau60600742016-01-11 15:32:30 +01003633 chunk_appendf(out, "</div>");
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003634 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003635
Willy Tarreau60600742016-01-11 15:32:30 +01003636 chunk_appendf(out,
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003637 /* queue : current, max, limit */
3638 "%s</td><td>%s</td><td>%s</td><td>%s</td>"
3639 /* sessions rate : current, max, limit */
3640 "<td>%s</td><td>%s</td><td></td>"
3641 "",
3642 (flags & ST_SHLGNDS) ? "</u>" : "",
3643 U2H(stats[ST_F_QCUR].u.u32), U2H(stats[ST_F_QMAX].u.u32), LIM2A(stats[ST_F_QLIMIT].u.u32, "-"),
3644 U2H(stats[ST_F_RATE].u.u32), U2H(stats[ST_F_RATE_MAX].u.u32));
Willy Tarreau4607fad2016-01-06 15:41:29 +01003645
Willy Tarreau60600742016-01-11 15:32:30 +01003646 chunk_appendf(out,
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003647 /* sessions: current, max, limit, total */
3648 "<td>%s</td><td>%s</td><td>%s</td>"
3649 "<td><u>%s<div class=tips><table class=det>"
3650 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
3651 "",
3652 U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SMAX].u.u32), LIM2A(stats[ST_F_SLIM].u.u32, "-"),
3653 U2H(stats[ST_F_STOT].u.u64),
3654 U2H(stats[ST_F_STOT].u.u64));
Willy Tarreau4607fad2016-01-06 15:41:29 +01003655
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003656 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
Willy Tarreauf8211df2016-01-11 14:09:38 +01003657 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003658 unsigned long long tot;
Willy Tarreaua6f5a732016-01-08 16:59:56 +01003659
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003660 tot = stats[ST_F_HRSP_OTHER].u.u64;
3661 tot += stats[ST_F_HRSP_1XX].u.u64;
3662 tot += stats[ST_F_HRSP_2XX].u.u64;
3663 tot += stats[ST_F_HRSP_3XX].u.u64;
3664 tot += stats[ST_F_HRSP_4XX].u.u64;
3665 tot += stats[ST_F_HRSP_5XX].u.u64;
3666
Willy Tarreau60600742016-01-11 15:32:30 +01003667 chunk_appendf(out,
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003668 "<tr><th>Cum. HTTP responses:</th><td>%s</td></tr>"
3669 "<tr><th>- HTTP 1xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
3670 "<tr><th>- HTTP 2xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
3671 "<tr><th>- HTTP 3xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
3672 "<tr><th>- HTTP 4xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
3673 "<tr><th>- HTTP 5xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
3674 "<tr><th>- other responses:</th><td>%s</td><td>(%d%%)</td></tr>"
3675 "",
3676 U2H(tot),
3677 U2H(stats[ST_F_HRSP_1XX].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_1XX].u.u64 / tot) : 0,
3678 U2H(stats[ST_F_HRSP_2XX].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_2XX].u.u64 / tot) : 0,
3679 U2H(stats[ST_F_HRSP_3XX].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_3XX].u.u64 / tot) : 0,
3680 U2H(stats[ST_F_HRSP_4XX].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_4XX].u.u64 / tot) : 0,
3681 U2H(stats[ST_F_HRSP_5XX].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_5XX].u.u64 / tot) : 0,
3682 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 +01003683 }
Willy Tarreaua6f5a732016-01-08 16:59:56 +01003684
Willy Tarreau60600742016-01-11 15:32:30 +01003685 chunk_appendf(out, "<tr><th colspan=3>Avg over last 1024 success. conn.</th></tr>");
3686 chunk_appendf(out, "<tr><th>- Queue time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_QTIME].u.u32));
3687 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 +01003688 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
Willy Tarreau60600742016-01-11 15:32:30 +01003689 chunk_appendf(out, "<tr><th>- Response time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_RTIME].u.u32));
3690 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 +02003691
Willy Tarreau60600742016-01-11 15:32:30 +01003692 chunk_appendf(out,
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003693 "</table></div></u></td>"
3694 /* sessions: lbtot, last */
3695 "<td>%s</td><td>%s</td>",
3696 U2H(stats[ST_F_LBTOT].u.u64),
3697 human_time(stats[ST_F_LASTSESS].u.s32, 1));
Willy Tarreau91861262007-10-17 17:06:05 +02003698
Willy Tarreau60600742016-01-11 15:32:30 +01003699 chunk_appendf(out,
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003700 /* bytes : in, out */
3701 "<td>%s</td><td>%s</td>"
3702 /* denied: req, resp */
3703 "<td></td><td>%s</td>"
3704 /* errors : request, connect */
3705 "<td></td><td>%s</td>"
3706 /* errors : response */
3707 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
3708 /* warnings: retries, redispatches */
3709 "<td>%lld</td><td>%lld</td>"
3710 "",
3711 U2H(stats[ST_F_BIN].u.u64), U2H(stats[ST_F_BOUT].u.u64),
3712 U2H(stats[ST_F_DRESP].u.u64),
3713 U2H(stats[ST_F_ECON].u.u64),
3714 U2H(stats[ST_F_ERESP].u.u64),
3715 (long long)stats[ST_F_CLI_ABRT].u.u64,
3716 (long long)stats[ST_F_SRV_ABRT].u.u64,
3717 (long long)stats[ST_F_WRETR].u.u64,
3718 (long long)stats[ST_F_WREDIS].u.u64);
3719
3720 /* status, last change */
Willy Tarreau60600742016-01-11 15:32:30 +01003721 chunk_appendf(out, "<td class=ac>");
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003722
3723 /* FIXME!!!!
3724 * LASTCHG should contain the last change for *this* server and must be computed
3725 * properly above, as was done below, ie: this server if maint, otherwise ref server
3726 * if tracking. Note that ref is either local or remote depending on tracking.
3727 */
3728
3729
3730 if (memcmp(field_str(stats, ST_F_STATUS), "MAINT", 5) == 0) {
Willy Tarreau60600742016-01-11 15:32:30 +01003731 chunk_appendf(out, "%s MAINT", human_time(stats[ST_F_LASTCHG].u.u32, 1));
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003732 }
3733 else if (memcmp(field_str(stats, ST_F_STATUS), "no check", 5) == 0) {
Willy Tarreau60600742016-01-11 15:32:30 +01003734 chunk_strcat(out, "<i>no check</i>");
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003735 }
3736 else {
Willy Tarreau60600742016-01-11 15:32:30 +01003737 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 +01003738 if (memcmp(field_str(stats, ST_F_STATUS), "DOWN", 4) == 0) {
3739 if (stats[ST_F_CHECK_HEALTH].u.u32)
Willy Tarreau60600742016-01-11 15:32:30 +01003740 chunk_strcat(out, " &uarr;");
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003741 }
3742 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 +01003743 chunk_strcat(out, " &darr;");
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003744 }
3745
3746 if (memcmp(field_str(stats, ST_F_STATUS), "DOWN", 4) == 0 &&
3747 stats[ST_F_AGENT_STATUS].type && !stats[ST_F_AGENT_HEALTH].u.u32) {
Willy Tarreau60600742016-01-11 15:32:30 +01003748 chunk_appendf(out,
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003749 "</td><td class=ac><u> %s",
3750 field_str(stats, ST_F_AGENT_STATUS));
3751
3752 if (stats[ST_F_AGENT_CODE].type)
Willy Tarreau60600742016-01-11 15:32:30 +01003753 chunk_appendf(out, "/%d", stats[ST_F_AGENT_CODE].u.u32);
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003754
David Carlier08d35de2016-06-27 14:12:59 +01003755 if (stats[ST_F_AGENT_DURATION].type)
Willy Tarreau60600742016-01-11 15:32:30 +01003756 chunk_appendf(out, " in %lums", (long)stats[ST_F_AGENT_DURATION].u.u64);
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003757
Willy Tarreau60600742016-01-11 15:32:30 +01003758 chunk_appendf(out, "<div class=tips>%s", field_str(stats, ST_F_AGENT_DESC));
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003759
3760 if (*field_str(stats, ST_F_LAST_AGT)) {
Willy Tarreau60600742016-01-11 15:32:30 +01003761 chunk_appendf(out, ": ");
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003762 chunk_initstr(&src, field_str(stats, ST_F_LAST_AGT));
Willy Tarreau60600742016-01-11 15:32:30 +01003763 chunk_htmlencode(out, &src);
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003764 }
Willy Tarreau60600742016-01-11 15:32:30 +01003765 chunk_appendf(out, "</div></u>");
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003766 }
3767 else if (stats[ST_F_CHECK_STATUS].type) {
Willy Tarreau60600742016-01-11 15:32:30 +01003768 chunk_appendf(out,
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003769 "</td><td class=ac><u> %s",
3770 field_str(stats, ST_F_CHECK_STATUS));
3771
3772 if (stats[ST_F_CHECK_CODE].type)
Willy Tarreau60600742016-01-11 15:32:30 +01003773 chunk_appendf(out, "/%d", stats[ST_F_CHECK_CODE].u.u32);
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003774
David Carlier08d35de2016-06-27 14:12:59 +01003775 if (stats[ST_F_CHECK_DURATION].type)
Willy Tarreau60600742016-01-11 15:32:30 +01003776 chunk_appendf(out, " in %lums", (long)stats[ST_F_CHECK_DURATION].u.u64);
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003777
Willy Tarreau60600742016-01-11 15:32:30 +01003778 chunk_appendf(out, "<div class=tips>%s", field_str(stats, ST_F_CHECK_DESC));
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003779
3780 if (*field_str(stats, ST_F_LAST_CHK)) {
Willy Tarreau60600742016-01-11 15:32:30 +01003781 chunk_appendf(out, ": ");
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003782 chunk_initstr(&src, field_str(stats, ST_F_LAST_CHK));
Willy Tarreau60600742016-01-11 15:32:30 +01003783 chunk_htmlencode(out, &src);
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003784 }
Willy Tarreau60600742016-01-11 15:32:30 +01003785 chunk_appendf(out, "</div></u>");
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003786 }
3787 else
Willy Tarreau60600742016-01-11 15:32:30 +01003788 chunk_appendf(out, "</td><td>");
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003789
Willy Tarreau60600742016-01-11 15:32:30 +01003790 chunk_appendf(out,
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003791 /* weight */
3792 "</td><td class=ac>%d</td>"
3793 /* act, bck */
3794 "<td class=ac>%s</td><td class=ac>%s</td>"
3795 "",
3796 stats[ST_F_WEIGHT].u.u32,
3797 stats[ST_F_BCK].u.u32 ? "-" : "Y",
3798 stats[ST_F_BCK].u.u32 ? "Y" : "-");
3799
3800 /* check failures: unique, fatal, down time */
3801 if (stats[ST_F_CHKFAIL].type) {
Willy Tarreau60600742016-01-11 15:32:30 +01003802 chunk_appendf(out, "<td><u>%lld", (long long)stats[ST_F_CHKFAIL].u.u64);
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003803
3804 if (stats[ST_F_HANAFAIL].type)
Willy Tarreau60600742016-01-11 15:32:30 +01003805 chunk_appendf(out, "/%lld", (long long)stats[ST_F_HANAFAIL].u.u64);
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003806
Willy Tarreau60600742016-01-11 15:32:30 +01003807 chunk_appendf(out,
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003808 "<div class=tips>Failed Health Checks%s</div></u></td>"
3809 "<td>%lld</td><td>%s</td>"
3810 "",
3811 stats[ST_F_HANAFAIL].type ? "/Health Analyses" : "",
3812 (long long)stats[ST_F_CHKDOWN].u.u64, human_time(stats[ST_F_DOWNTIME].u.u32, 1));
3813 }
3814 else if (strcmp(field_str(stats, ST_F_STATUS), "MAINT") != 0 && field_format(stats, ST_F_TRACKED) == FF_STR) {
3815 /* tracking a server (hence inherited maint would appear as "MAINT (via...)" */
Willy Tarreau60600742016-01-11 15:32:30 +01003816 chunk_appendf(out,
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003817 "<td class=ac colspan=3><a class=lfsb href=\"#%s\">via %s</a></td>",
3818 field_str(stats, ST_F_TRACKED), field_str(stats, ST_F_TRACKED));
3819 }
3820 else
Willy Tarreau60600742016-01-11 15:32:30 +01003821 chunk_appendf(out, "<td colspan=3></td>");
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003822
3823 /* throttle */
3824 if (stats[ST_F_THROTTLE].type)
Willy Tarreau60600742016-01-11 15:32:30 +01003825 chunk_appendf(out, "<td class=ac>%d %%</td></tr>\n", stats[ST_F_THROTTLE].u.u32);
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003826 else
Willy Tarreau60600742016-01-11 15:32:30 +01003827 chunk_appendf(out, "<td class=ac>-</td></tr>\n");
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003828 }
Willy Tarreaubbf84502016-01-08 17:25:50 +01003829 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_BE) {
Willy Tarreau60600742016-01-11 15:32:30 +01003830 chunk_appendf(out, "<tr class=\"backend\">");
Willy Tarreau508a63f2016-01-11 15:28:40 +01003831 if (flags & ST_SHOWADMIN) {
Willy Tarreaubbf84502016-01-08 17:25:50 +01003832 /* Column sub-heading for Enable or Disable server */
Willy Tarreau60600742016-01-11 15:32:30 +01003833 chunk_appendf(out, "<td></td>");
Willy Tarreaubbf84502016-01-08 17:25:50 +01003834 }
Willy Tarreau60600742016-01-11 15:32:30 +01003835 chunk_appendf(out,
Willy Tarreaubbf84502016-01-08 17:25:50 +01003836 "<td class=ac>"
3837 /* name */
3838 "%s<a name=\"%s/Backend\"></a>"
3839 "<a class=lfsb href=\"#%s/Backend\">Backend</a>"
3840 "",
3841 (flags & ST_SHLGNDS)?"<u>":"",
3842 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_PXNAME));
3843
3844 if (flags & ST_SHLGNDS) {
3845 /* balancing */
Willy Tarreau60600742016-01-11 15:32:30 +01003846 chunk_appendf(out, "<div class=tips>balancing: %s",
Willy Tarreauf1516d92016-01-11 14:48:36 +01003847 field_str(stats, ST_F_ALGO));
Willy Tarreaubbf84502016-01-08 17:25:50 +01003848
3849 /* cookie */
3850 if (stats[ST_F_COOKIE].type) {
Willy Tarreau60600742016-01-11 15:32:30 +01003851 chunk_appendf(out, ", cookie: '");
Willy Tarreaubbf84502016-01-08 17:25:50 +01003852 chunk_initstr(&src, field_str(stats, ST_F_COOKIE));
Willy Tarreau60600742016-01-11 15:32:30 +01003853 chunk_htmlencode(out, &src);
3854 chunk_appendf(out, "'");
Willy Tarreaubbf84502016-01-08 17:25:50 +01003855 }
Willy Tarreau60600742016-01-11 15:32:30 +01003856 chunk_appendf(out, "</div>");
Willy Tarreaubbf84502016-01-08 17:25:50 +01003857 }
3858
Willy Tarreau60600742016-01-11 15:32:30 +01003859 chunk_appendf(out,
Willy Tarreaubbf84502016-01-08 17:25:50 +01003860 "%s</td>"
3861 /* queue : current, max */
3862 "<td>%s</td><td>%s</td><td></td>"
3863 /* sessions rate : current, max, limit */
3864 "<td>%s</td><td>%s</td><td></td>"
3865 "",
3866 (flags & ST_SHLGNDS)?"</u>":"",
3867 U2H(stats[ST_F_QCUR].u.u32), U2H(stats[ST_F_QMAX].u.u32),
3868 U2H(stats[ST_F_RATE].u.u32), U2H(stats[ST_F_RATE_MAX].u.u32));
3869
Willy Tarreau60600742016-01-11 15:32:30 +01003870 chunk_appendf(out,
Willy Tarreaubbf84502016-01-08 17:25:50 +01003871 /* sessions: current, max, limit, total */
3872 "<td>%s</td><td>%s</td><td>%s</td>"
3873 "<td><u>%s<div class=tips><table class=det>"
3874 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
3875 "",
3876 U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SLIM].u.u32),
3877 U2H(stats[ST_F_STOT].u.u64),
3878 U2H(stats[ST_F_STOT].u.u64));
3879
3880 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
Willy Tarreauf8211df2016-01-11 14:09:38 +01003881 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
Willy Tarreau60600742016-01-11 15:32:30 +01003882 chunk_appendf(out,
Willy Tarreaubbf84502016-01-08 17:25:50 +01003883 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
3884 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
3885 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
3886 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
3887 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
3888 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
3889 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
3890 "<tr><th>- other responses:</th><td>%s</td></tr>"
3891 "<tr><th>Intercepted requests:</th><td>%s</td></tr>"
3892 "<tr><th colspan=3>Avg over last 1024 success. conn.</th></tr>"
3893 "",
3894 U2H(stats[ST_F_REQ_TOT].u.u64),
3895 U2H(stats[ST_F_HRSP_1XX].u.u64),
3896 U2H(stats[ST_F_HRSP_2XX].u.u64),
3897 U2H(stats[ST_F_COMP_RSP].u.u64),
3898 stats[ST_F_HRSP_2XX].u.u64 ?
3899 (int)(100 * stats[ST_F_COMP_RSP].u.u64 / stats[ST_F_HRSP_2XX].u.u64) : 0,
3900 U2H(stats[ST_F_HRSP_3XX].u.u64),
3901 U2H(stats[ST_F_HRSP_4XX].u.u64),
3902 U2H(stats[ST_F_HRSP_5XX].u.u64),
3903 U2H(stats[ST_F_HRSP_OTHER].u.u64),
Willy Tarreau5b9bdff2016-01-11 14:40:47 +01003904 U2H(stats[ST_F_INTERCEPTED].u.u64));
Willy Tarreaubbf84502016-01-08 17:25:50 +01003905 }
3906
Willy Tarreau60600742016-01-11 15:32:30 +01003907 chunk_appendf(out, "<tr><th>- Queue time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_QTIME].u.u32));
Willy Tarreau1f6367f2016-10-01 09:12:08 +02003908 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 +01003909 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
Willy Tarreau60600742016-01-11 15:32:30 +01003910 chunk_appendf(out, "<tr><th>- Response time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_RTIME].u.u32));
3911 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 +01003912
Willy Tarreau60600742016-01-11 15:32:30 +01003913 chunk_appendf(out,
Willy Tarreaubbf84502016-01-08 17:25:50 +01003914 "</table></div></u></td>"
3915 /* sessions: lbtot, last */
3916 "<td>%s</td><td>%s</td>"
3917 /* bytes: in */
3918 "<td>%s</td>"
3919 "",
3920 U2H(stats[ST_F_LBTOT].u.u64),
3921 human_time(stats[ST_F_LASTSESS].u.s32, 1),
3922 U2H(stats[ST_F_BIN].u.u64));
3923
Willy Tarreau60600742016-01-11 15:32:30 +01003924 chunk_appendf(out,
Willy Tarreaubbf84502016-01-08 17:25:50 +01003925 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
3926 "<td>%s%s<div class=tips><table class=det>"
3927 "<tr><th>Response bytes in:</th><td>%s</td></tr>"
3928 "<tr><th>Compression in:</th><td>%s</td></tr>"
3929 "<tr><th>Compression out:</th><td>%s</td><td>(%d%%)</td></tr>"
3930 "<tr><th>Compression bypass:</th><td>%s</td></tr>"
3931 "<tr><th>Total bytes saved:</th><td>%s</td><td>(%d%%)</td></tr>"
3932 "</table></div>%s</td>",
3933 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "<u>":"",
3934 U2H(stats[ST_F_BOUT].u.u64),
3935 U2H(stats[ST_F_BOUT].u.u64),
3936 U2H(stats[ST_F_COMP_IN].u.u64),
3937 U2H(stats[ST_F_COMP_OUT].u.u64),
3938 stats[ST_F_COMP_IN].u.u64 ? (int)(stats[ST_F_COMP_OUT].u.u64 * 100 / stats[ST_F_COMP_IN].u.u64) : 0,
3939 U2H(stats[ST_F_COMP_BYP].u.u64),
Christopher Faulet0b64f622016-04-28 15:09:31 +02003940 U2H(stats[ST_F_COMP_IN].u.u64 - stats[ST_F_COMP_OUT].u.u64),
Willy Tarreaubbf84502016-01-08 17:25:50 +01003941 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,
3942 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "</u>":"");
3943
Willy Tarreau60600742016-01-11 15:32:30 +01003944 chunk_appendf(out,
Willy Tarreaubbf84502016-01-08 17:25:50 +01003945 /* denied: req, resp */
3946 "<td>%s</td><td>%s</td>"
3947 /* errors : request, connect */
3948 "<td></td><td>%s</td>"
3949 /* errors : response */
3950 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
3951 /* warnings: retries, redispatches */
3952 "<td>%lld</td><td>%lld</td>"
3953 /* backend status: reflect backend status (up/down): we display UP
3954 * if the backend has known working servers or if it has no server at
3955 * all (eg: for stats). Then we display the total weight, number of
3956 * active and backups. */
3957 "<td class=ac>%s %s</td><td class=ac>&nbsp;</td><td class=ac>%d</td>"
3958 "<td class=ac>%d</td><td class=ac>%d</td>"
3959 "",
3960 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
3961 U2H(stats[ST_F_ECON].u.u64),
3962 U2H(stats[ST_F_ERESP].u.u64),
3963 (long long)stats[ST_F_CLI_ABRT].u.u64,
3964 (long long)stats[ST_F_SRV_ABRT].u.u64,
3965 (long long)stats[ST_F_WRETR].u.u64, (long long)stats[ST_F_WREDIS].u.u64,
3966 human_time(stats[ST_F_LASTCHG].u.u32, 1),
3967 strcmp(field_str(stats, ST_F_STATUS), "DOWN") ? field_str(stats, ST_F_STATUS) : "<font color=\"red\"><b>DOWN</b></font>",
3968 stats[ST_F_WEIGHT].u.u32,
3969 stats[ST_F_ACT].u.u32, stats[ST_F_BCK].u.u32);
3970
Willy Tarreau60600742016-01-11 15:32:30 +01003971 chunk_appendf(out,
Willy Tarreaubbf84502016-01-08 17:25:50 +01003972 /* rest of backend: nothing, down transitions, total downtime, throttle */
3973 "<td class=ac>&nbsp;</td><td>%d</td>"
3974 "<td>%s</td>"
3975 "<td></td>"
3976 "</tr>",
3977 stats[ST_F_CHKDOWN].u.u32,
3978 stats[ST_F_DOWNTIME].type ? human_time(stats[ST_F_DOWNTIME].u.u32, 1) : "&nbsp;");
3979 }
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003980 return 1;
3981}
3982
Willy Tarreau501f6022016-01-08 17:37:22 +01003983static int stats_dump_one_line(const struct field *stats, unsigned int flags, struct proxy *px, struct appctx *appctx)
3984{
Willy Tarreau508a63f2016-01-11 15:28:40 +01003985 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN))
3986 flags |= ST_SHOWADMIN;
Willy Tarreau501f6022016-01-08 17:37:22 +01003987
3988 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
Willy Tarreau60600742016-01-11 15:32:30 +01003989 return stats_dump_fields_html(&trash, stats, flags);
Willy Tarreau1e62df92016-01-11 18:57:53 +01003990 else if (appctx->ctx.stats.flags & STAT_FMT_TYPED)
3991 return stats_dump_fields_typed(&trash, stats);
Willy Tarreau501f6022016-01-08 17:37:22 +01003992 else
3993 return stats_dump_fields_csv(&trash, stats);
3994}
3995
Thierry Fournier23d2d642016-03-25 08:20:11 +01003996/* Fill <stats> with the frontend statistics. <stats> is
3997 * preallocated array of length <len>. The length of the array
3998 * must be at least ST_F_TOTAL_FIELDS. If this length is less then
3999 * this value, the function returns 0, otherwise, it returns 1.
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004000 */
Thierry Fournier23d2d642016-03-25 08:20:11 +01004001int stats_fill_fe_stats(struct proxy *px, struct field *stats, int len)
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004002{
Thierry Fournier23d2d642016-03-25 08:20:11 +01004003 if (len < ST_F_TOTAL_FIELDS)
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004004 return 0;
4005
Thierry Fournier23d2d642016-03-25 08:20:11 +01004006 memset(stats, 0, sizeof(*stats) * len);
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004007
4008 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
4009 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, "FRONTEND");
Willy Tarreauf8211df2016-01-11 14:09:38 +01004010 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004011 stats[ST_F_SCUR] = mkf_u32(0, px->feconn);
4012 stats[ST_F_SMAX] = mkf_u32(FN_MAX, px->fe_counters.conn_max);
4013 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, px->maxconn);
4014 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, px->fe_counters.cum_sess);
4015 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, px->fe_counters.bytes_in);
4016 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, px->fe_counters.bytes_out);
4017 stats[ST_F_DREQ] = mkf_u64(FN_COUNTER, px->fe_counters.denied_req);
4018 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, px->fe_counters.denied_resp);
4019 stats[ST_F_EREQ] = mkf_u64(FN_COUNTER, px->fe_counters.failed_req);
Willy Tarreau8a90b8e2016-10-21 18:15:32 +02004020 stats[ST_F_DCON] = mkf_u64(FN_COUNTER, px->fe_counters.denied_conn);
Willy Tarreaua5bc36b2016-10-21 18:16:27 +02004021 stats[ST_F_DSES] = mkf_u64(FN_COUNTER, px->fe_counters.denied_sess);
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004022 stats[ST_F_STATUS] = mkf_str(FO_STATUS, px->state == PR_STREADY ? "OPEN" : px->state == PR_STFULL ? "FULL" : "STOP");
4023 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
4024 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
4025 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, 0);
4026 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_FE);
4027 stats[ST_F_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_sess_per_sec));
4028 stats[ST_F_RATE_LIM] = mkf_u32(FO_CONFIG|FN_LIMIT, px->fe_sps_lim);
4029 stats[ST_F_RATE_MAX] = mkf_u32(FN_MAX, px->fe_counters.sps_max);
4030
4031 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
4032 if (px->mode == PR_MODE_HTTP) {
4033 stats[ST_F_HRSP_1XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[1]);
4034 stats[ST_F_HRSP_2XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[2]);
4035 stats[ST_F_HRSP_3XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[3]);
4036 stats[ST_F_HRSP_4XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[4]);
4037 stats[ST_F_HRSP_5XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[5]);
4038 stats[ST_F_HRSP_OTHER] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[0]);
Willy Tarreau5b9bdff2016-01-11 14:40:47 +01004039 stats[ST_F_INTERCEPTED] = mkf_u64(FN_COUNTER, px->fe_counters.intercepted_req);
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004040 }
4041
4042 /* requests : req_rate, req_rate_max, req_tot, */
4043 stats[ST_F_REQ_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_req_per_sec));
4044 stats[ST_F_REQ_RATE_MAX] = mkf_u32(FN_MAX, px->fe_counters.p.http.rps_max);
4045 stats[ST_F_REQ_TOT] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cum_req);
4046
4047 /* compression: in, out, bypassed, responses */
4048 stats[ST_F_COMP_IN] = mkf_u64(FN_COUNTER, px->fe_counters.comp_in);
4049 stats[ST_F_COMP_OUT] = mkf_u64(FN_COUNTER, px->fe_counters.comp_out);
4050 stats[ST_F_COMP_BYP] = mkf_u64(FN_COUNTER, px->fe_counters.comp_byp);
4051 stats[ST_F_COMP_RSP] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.comp_rsp);
4052
Willy Tarreauc73810f2016-01-11 13:52:04 +01004053 /* connections : conn_rate, conn_rate_max, conn_tot, conn_max */
4054 stats[ST_F_CONN_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_conn_per_sec));
4055 stats[ST_F_CONN_RATE_MAX] = mkf_u32(FN_MAX, px->fe_counters.cps_max);
4056 stats[ST_F_CONN_TOT] = mkf_u64(FN_COUNTER, px->fe_counters.cum_conn);
4057
Thierry Fournier23d2d642016-03-25 08:20:11 +01004058 return 1;
4059}
4060
4061/* Dumps a frontend's line to the trash for the current proxy <px> and uses
4062 * the state from stream interface <si>. The caller is responsible for clearing
4063 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
4064 */
4065static int stats_dump_fe_stats(struct stream_interface *si, struct proxy *px)
4066{
4067 struct appctx *appctx = __objt_appctx(si->end);
4068
4069 if (!(px->cap & PR_CAP_FE))
4070 return 0;
4071
4072 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_FE)))
4073 return 0;
4074
4075 if (!stats_fill_fe_stats(px, stats, ST_F_TOTAL_FIELDS))
4076 return 0;
4077
Willy Tarreau501f6022016-01-08 17:37:22 +01004078 return stats_dump_one_line(stats, 0, px, appctx);
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004079}
4080
Thierry Fournierc4456852016-03-25 08:20:49 +01004081/* Fill <stats> with the listener statistics. <stats> is
4082 * preallocated array of length <len>. The length of the array
4083 * must be at least ST_F_TOTAL_FIELDS. If this length is less
4084 * then this value, the function returns 0, otherwise, it
4085 * returns 1. <flags> can take the value ST_SHLGNDS.
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004086 */
Thierry Fournierc4456852016-03-25 08:20:49 +01004087int stats_fill_li_stats(struct proxy *px, struct listener *l, int flags,
4088 struct field *stats, int len)
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004089{
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004090 struct chunk *out = get_trash_chunk();
4091
Thierry Fournierc4456852016-03-25 08:20:49 +01004092 if (len < ST_F_TOTAL_FIELDS)
4093 return 0;
4094
Thierry Fournierac9d4672016-03-25 08:43:46 +01004095 if (!l->counters)
4096 return 0;
4097
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004098 chunk_reset(out);
Thierry Fournierc4456852016-03-25 08:20:49 +01004099 memset(stats, 0, sizeof(*stats) * len);
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004100
4101 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
4102 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, l->name);
Willy Tarreauf8211df2016-01-11 14:09:38 +01004103 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004104 stats[ST_F_SCUR] = mkf_u32(0, l->nbconn);
4105 stats[ST_F_SMAX] = mkf_u32(FN_MAX, l->counters->conn_max);
4106 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, l->maxconn);
4107 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, l->counters->cum_conn);
4108 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, l->counters->bytes_in);
4109 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, l->counters->bytes_out);
4110 stats[ST_F_DREQ] = mkf_u64(FN_COUNTER, l->counters->denied_req);
4111 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, l->counters->denied_resp);
4112 stats[ST_F_EREQ] = mkf_u64(FN_COUNTER, l->counters->failed_req);
Willy Tarreau8a90b8e2016-10-21 18:15:32 +02004113 stats[ST_F_DCON] = mkf_u64(FN_COUNTER, l->counters->denied_conn);
Willy Tarreaua5bc36b2016-10-21 18:16:27 +02004114 stats[ST_F_DSES] = mkf_u64(FN_COUNTER, l->counters->denied_sess);
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004115 stats[ST_F_STATUS] = mkf_str(FO_STATUS, (l->nbconn < l->maxconn) ? (l->state == LI_LIMITED) ? "WAITING" : "OPEN" : "FULL");
4116 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
4117 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
4118 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, l->luid);
4119 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_SO);
4120
4121 if (flags & ST_SHLGNDS) {
4122 char str[INET6_ADDRSTRLEN];
4123 int port;
4124
4125 port = get_host_port(&l->addr);
4126 switch (addr_to_str(&l->addr, str, sizeof(str))) {
4127 case AF_INET:
4128 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
4129 chunk_appendf(out, "%s:%d", str, port);
4130 break;
4131 case AF_INET6:
4132 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
4133 chunk_appendf(out, "[%s]:%d", str, port);
4134 break;
4135 case AF_UNIX:
4136 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, "unix");
4137 break;
4138 case -1:
4139 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
4140 chunk_strcat(out, strerror(errno));
4141 break;
4142 default: /* address family not supported */
4143 break;
4144 }
4145 }
4146
Thierry Fournierc4456852016-03-25 08:20:49 +01004147 return 1;
4148}
4149
4150/* Dumps a line for listener <l> and proxy <px> to the trash and uses the state
4151 * from stream interface <si>, and stats flags <flags>. The caller is responsible
4152 * for clearing the trash if needed. Returns non-zero if it emits anything, zero
4153 * otherwise.
4154 */
4155static int stats_dump_li_stats(struct stream_interface *si, struct proxy *px, struct listener *l, int flags)
4156{
4157 struct appctx *appctx = __objt_appctx(si->end);
4158
4159 if (!stats_fill_li_stats(px, l, flags, stats, ST_F_TOTAL_FIELDS))
4160 return 0;
4161
Willy Tarreau501f6022016-01-08 17:37:22 +01004162 return stats_dump_one_line(stats, flags, px, appctx);
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004163}
4164
4165enum srv_stats_state {
4166 SRV_STATS_STATE_DOWN = 0,
4167 SRV_STATS_STATE_DOWN_AGENT,
4168 SRV_STATS_STATE_GOING_UP,
4169 SRV_STATS_STATE_UP_GOING_DOWN,
4170 SRV_STATS_STATE_UP,
4171 SRV_STATS_STATE_NOLB_GOING_DOWN,
Simon Horman4d2eab62015-04-23 14:51:26 +09004172 SRV_STATS_STATE_NOLB,
4173 SRV_STATS_STATE_DRAIN_GOING_DOWN,
4174 SRV_STATS_STATE_DRAIN,
Simon Hormanb167b6b2015-04-23 14:51:29 +09004175 SRV_STATS_STATE_DRAIN_AGENT,
Simon Horman4d2eab62015-04-23 14:51:26 +09004176 SRV_STATS_STATE_NO_CHECK,
4177
4178 SRV_STATS_STATE_COUNT, /* Must be last */
4179};
4180
Willy Tarreau2b96cf12016-01-06 19:25:38 +01004181static const char *srv_hlt_st[SRV_STATS_STATE_COUNT] = {
4182 [SRV_STATS_STATE_DOWN] = "DOWN",
4183 [SRV_STATS_STATE_DOWN_AGENT] = "DOWN (agent)",
4184 [SRV_STATS_STATE_GOING_UP] = "DOWN %d/%d",
4185 [SRV_STATS_STATE_UP_GOING_DOWN] = "UP %d/%d",
4186 [SRV_STATS_STATE_UP] = "UP",
4187 [SRV_STATS_STATE_NOLB_GOING_DOWN] = "NOLB %d/%d",
4188 [SRV_STATS_STATE_NOLB] = "NOLB",
4189 [SRV_STATS_STATE_DRAIN_GOING_DOWN] = "DRAIN %d/%d",
4190 [SRV_STATS_STATE_DRAIN] = "DRAIN",
4191 [SRV_STATS_STATE_DRAIN_AGENT] = "DRAIN (agent)",
4192 [SRV_STATS_STATE_NO_CHECK] = "no check"
4193};
4194
Thierry Fournier61fe6c02016-03-25 08:21:21 +01004195/* Fill <stats> with the server statistics. <stats> is
4196 * preallocated array of length <len>. The length of the array
4197 * must be at least ST_F_TOTAL_FIELDS. If this length is less
4198 * then this value, the function returns 0, otherwise, it
4199 * returns 1. <flags> can take the value ST_SHLGNDS.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004200 */
Thierry Fournier61fe6c02016-03-25 08:21:21 +01004201int stats_fill_sv_stats(struct proxy *px, struct server *sv, int flags,
4202 struct field *stats, int len)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004203{
Willy Tarreau32091232014-05-16 13:52:00 +02004204 struct server *via, *ref;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004205 char str[INET6_ADDRSTRLEN];
Willy Tarreau2b96cf12016-01-06 19:25:38 +01004206 struct chunk *out = get_trash_chunk();
Willy Tarreauba2f2642016-01-07 09:54:40 +01004207 enum srv_stats_state state;
Willy Tarreau2b96cf12016-01-06 19:25:38 +01004208 char *fld_status;
Thierry Fournier61fe6c02016-03-25 08:21:21 +01004209
4210 if (len < ST_F_TOTAL_FIELDS)
4211 return 0;
4212
4213 memset(stats, 0, sizeof(*stats) * len);
4214
Willy Tarreau32091232014-05-16 13:52:00 +02004215 /* we have "via" which is the tracked server as described in the configuration,
4216 * and "ref" which is the checked server and the end of the chain.
4217 */
4218 via = sv->track ? sv->track : sv;
4219 ref = via;
4220 while (ref->track)
4221 ref = ref->track;
4222
Willy Tarreauba2f2642016-01-07 09:54:40 +01004223 if (sv->state == SRV_ST_RUNNING || sv->state == SRV_ST_STARTING) {
4224 if ((ref->check.state & CHK_ST_ENABLED) &&
4225 (ref->check.health < ref->check.rise + ref->check.fall - 1)) {
4226 state = SRV_STATS_STATE_UP_GOING_DOWN;
Willy Tarreauba2f2642016-01-07 09:54:40 +01004227 } else {
4228 state = SRV_STATS_STATE_UP;
Willy Tarreauba2f2642016-01-07 09:54:40 +01004229 }
4230
Willy Tarreauba2f2642016-01-07 09:54:40 +01004231 if (sv->admin & SRV_ADMF_DRAIN) {
4232 if (ref->agent.state & CHK_ST_ENABLED)
4233 state = SRV_STATS_STATE_DRAIN_AGENT;
4234 else if (state == SRV_STATS_STATE_UP_GOING_DOWN)
4235 state = SRV_STATS_STATE_DRAIN_GOING_DOWN;
4236 else
4237 state = SRV_STATS_STATE_DRAIN;
4238 }
4239
4240 if (state == SRV_STATS_STATE_UP && !(ref->check.state & CHK_ST_ENABLED)) {
4241 state = SRV_STATS_STATE_NO_CHECK;
Willy Tarreauba2f2642016-01-07 09:54:40 +01004242 }
4243 }
4244 else if (sv->state == SRV_ST_STOPPING) {
4245 if ((!(sv->check.state & CHK_ST_ENABLED) && !sv->track) ||
4246 (ref->check.health == ref->check.rise + ref->check.fall - 1)) {
4247 state = SRV_STATS_STATE_NOLB;
Willy Tarreauba2f2642016-01-07 09:54:40 +01004248 } else {
4249 state = SRV_STATS_STATE_NOLB_GOING_DOWN;
Willy Tarreauba2f2642016-01-07 09:54:40 +01004250 }
4251 }
4252 else { /* stopped */
4253 if ((ref->agent.state & CHK_ST_ENABLED) && !ref->agent.health) {
4254 state = SRV_STATS_STATE_DOWN_AGENT;
Willy Tarreauba2f2642016-01-07 09:54:40 +01004255 } else if ((ref->check.state & CHK_ST_ENABLED) && !ref->check.health) {
4256 state = SRV_STATS_STATE_DOWN; /* DOWN */
Willy Tarreauba2f2642016-01-07 09:54:40 +01004257 } else if ((ref->agent.state & CHK_ST_ENABLED) || (ref->check.state & CHK_ST_ENABLED)) {
4258 state = SRV_STATS_STATE_GOING_UP;
Willy Tarreauba2f2642016-01-07 09:54:40 +01004259 } else {
4260 state = SRV_STATS_STATE_DOWN; /* DOWN, unchecked */
Willy Tarreauba2f2642016-01-07 09:54:40 +01004261 }
4262 }
4263
Willy Tarreau2b96cf12016-01-06 19:25:38 +01004264 chunk_reset(out);
Willy Tarreau2b96cf12016-01-06 19:25:38 +01004265
4266 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
4267 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, sv->id);
Willy Tarreauf8211df2016-01-11 14:09:38 +01004268 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
Willy Tarreau2b96cf12016-01-06 19:25:38 +01004269 stats[ST_F_QCUR] = mkf_u32(0, sv->nbpend);
4270 stats[ST_F_QMAX] = mkf_u32(FN_MAX, sv->counters.nbpend_max);
4271 stats[ST_F_SCUR] = mkf_u32(0, sv->cur_sess);
4272 stats[ST_F_SMAX] = mkf_u32(FN_MAX, sv->counters.cur_sess_max);
4273
4274 if (sv->maxconn)
4275 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, sv->maxconn);
4276
4277 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, sv->counters.cum_sess);
4278 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, sv->counters.bytes_in);
4279 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, sv->counters.bytes_out);
4280 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, sv->counters.failed_secu);
4281 stats[ST_F_ECON] = mkf_u64(FN_COUNTER, sv->counters.failed_conns);
4282 stats[ST_F_ERESP] = mkf_u64(FN_COUNTER, sv->counters.failed_resp);
4283 stats[ST_F_WRETR] = mkf_u64(FN_COUNTER, sv->counters.retries);
4284 stats[ST_F_WREDIS] = mkf_u64(FN_COUNTER, sv->counters.redispatches);
4285
4286 /* status */
4287 fld_status = chunk_newstr(out);
4288 if (sv->admin & SRV_ADMF_IMAINT)
4289 chunk_appendf(out, "MAINT (via %s/%s)", via->proxy->id, via->id);
4290 else if (sv->admin & SRV_ADMF_MAINT)
4291 chunk_appendf(out, "MAINT");
4292 else
4293 chunk_appendf(out,
4294 srv_hlt_st[state],
4295 (ref->state != SRV_ST_STOPPED) ? (ref->check.health - ref->check.rise + 1) : (ref->check.health),
4296 (ref->state != SRV_ST_STOPPED) ? (ref->check.fall) : (ref->check.rise));
4297
4298 stats[ST_F_STATUS] = mkf_str(FO_STATUS, fld_status);
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004299 stats[ST_F_LASTCHG] = mkf_u32(FN_AGE, now.tv_sec - sv->last_change);
4300 stats[ST_F_WEIGHT] = mkf_u32(FN_AVG, (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv);
4301 stats[ST_F_ACT] = mkf_u32(FO_STATUS, (sv->flags & SRV_F_BACKUP) ? 0 : 1);
4302 stats[ST_F_BCK] = mkf_u32(FO_STATUS, (sv->flags & SRV_F_BACKUP) ? 1 : 0);
Willy Tarreau91861262007-10-17 17:06:05 +02004303
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004304 /* check failures: unique, fatal; last change, total downtime */
4305 if (sv->check.state & CHK_ST_ENABLED) {
4306 stats[ST_F_CHKFAIL] = mkf_u64(FN_COUNTER, sv->counters.failed_checks);
4307 stats[ST_F_CHKDOWN] = mkf_u64(FN_COUNTER, sv->counters.down_trans);
4308 stats[ST_F_DOWNTIME] = mkf_u32(FN_COUNTER, srv_downtime(sv));
4309 }
Willy Tarreau164d4a92016-01-06 19:48:21 +01004310
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004311 if (sv->maxqueue)
4312 stats[ST_F_QLIMIT] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->maxqueue);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004313
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004314 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
4315 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
4316 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, sv->puid);
Willy Tarreau91861262007-10-17 17:06:05 +02004317
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004318 if (sv->state == SRV_ST_STARTING && !server_is_draining(sv))
4319 stats[ST_F_THROTTLE] = mkf_u32(FN_AVG, server_throttle_rate(sv));
Willy Tarreauf5b1cc32014-06-17 12:20:59 +02004320
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004321 stats[ST_F_LBTOT] = mkf_u64(FN_COUNTER, sv->counters.cum_lbconn);
Willy Tarreau91861262007-10-17 17:06:05 +02004322
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004323 if (sv->track) {
4324 char *fld_track = chunk_newstr(out);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004325
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004326 chunk_appendf(out, "%s/%s", sv->track->proxy->id, sv->track->id);
4327 stats[ST_F_TRACKED] = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, fld_track);
4328 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004329
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004330 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_SV);
4331 stats[ST_F_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&sv->sess_per_sec));
4332 stats[ST_F_RATE_MAX] = mkf_u32(FN_MAX, sv->counters.sps_max);
Willy Tarreau164d4a92016-01-06 19:48:21 +01004333
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004334 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) {
4335 const char *fld_chksts;
Willy Tarreau164d4a92016-01-06 19:48:21 +01004336
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004337 fld_chksts = chunk_newstr(out);
4338 chunk_strcat(out, "* "); // for check in progress
4339 chunk_strcat(out, get_check_status_info(sv->check.status));
4340 if (!(sv->check.state & CHK_ST_INPROGRESS))
4341 fld_chksts += 2; // skip "* "
4342 stats[ST_F_CHECK_STATUS] = mkf_str(FN_OUTPUT, fld_chksts);
Willy Tarreau91861262007-10-17 17:06:05 +02004343
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004344 if (sv->check.status >= HCHK_STATUS_L57DATA)
4345 stats[ST_F_CHECK_CODE] = mkf_u32(FN_OUTPUT, sv->check.code);
Willy Tarreaucf2924b2014-05-23 12:15:15 +02004346
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004347 if (sv->check.status >= HCHK_STATUS_CHECKED)
4348 stats[ST_F_CHECK_DURATION] = mkf_u64(FN_DURATION, sv->check.duration);
Willy Tarreaucf2924b2014-05-23 12:15:15 +02004349
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004350 stats[ST_F_CHECK_DESC] = mkf_str(FN_OUTPUT, get_check_status_description(sv->check.status));
4351 stats[ST_F_LAST_CHK] = mkf_str(FN_OUTPUT, sv->check.desc);
4352 stats[ST_F_CHECK_RISE] = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.rise);
4353 stats[ST_F_CHECK_FALL] = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.fall);
4354 stats[ST_F_CHECK_HEALTH] = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.health);
4355 }
Willy Tarreaucf2924b2014-05-23 12:15:15 +02004356
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004357 if ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) {
4358 const char *fld_chksts;
Willy Tarreaudd7354b2016-01-08 13:47:26 +01004359
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004360 fld_chksts = chunk_newstr(out);
4361 chunk_strcat(out, "* "); // for check in progress
4362 chunk_strcat(out, get_check_status_info(sv->agent.status));
4363 if (!(sv->agent.state & CHK_ST_INPROGRESS))
4364 fld_chksts += 2; // skip "* "
4365 stats[ST_F_AGENT_STATUS] = mkf_str(FN_OUTPUT, fld_chksts);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004366
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004367 if (sv->agent.status >= HCHK_STATUS_L57DATA)
4368 stats[ST_F_AGENT_CODE] = mkf_u32(FN_OUTPUT, sv->agent.code);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004369
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004370 if (sv->agent.status >= HCHK_STATUS_CHECKED)
4371 stats[ST_F_AGENT_DURATION] = mkf_u64(FN_DURATION, sv->agent.duration);
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01004372
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004373 stats[ST_F_AGENT_DESC] = mkf_str(FN_OUTPUT, get_check_status_description(sv->agent.status));
4374 stats[ST_F_LAST_AGT] = mkf_str(FN_OUTPUT, sv->agent.desc);
4375 stats[ST_F_AGENT_RISE] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.rise);
4376 stats[ST_F_AGENT_FALL] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.fall);
4377 stats[ST_F_AGENT_HEALTH] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.health);
4378 }
Willy Tarreaudd7354b2016-01-08 13:47:26 +01004379
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004380 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
4381 if (px->mode == PR_MODE_HTTP) {
4382 stats[ST_F_HRSP_1XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[1]);
4383 stats[ST_F_HRSP_2XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[2]);
4384 stats[ST_F_HRSP_3XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[3]);
4385 stats[ST_F_HRSP_4XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[4]);
4386 stats[ST_F_HRSP_5XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[5]);
4387 stats[ST_F_HRSP_OTHER] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[0]);
4388 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004389
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004390 if (ref->observe)
4391 stats[ST_F_HANAFAIL] = mkf_u64(FN_COUNTER, sv->counters.failed_hana);
Willy Tarreau164d4a92016-01-06 19:48:21 +01004392
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004393 stats[ST_F_CLI_ABRT] = mkf_u64(FN_COUNTER, sv->counters.cli_aborts);
4394 stats[ST_F_SRV_ABRT] = mkf_u64(FN_COUNTER, sv->counters.srv_aborts);
4395 stats[ST_F_LASTSESS] = mkf_s32(FN_AGE, srv_lastsession(sv));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004396
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004397 stats[ST_F_QTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.q_time, TIME_STATS_SAMPLES));
4398 stats[ST_F_CTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.c_time, TIME_STATS_SAMPLES));
4399 stats[ST_F_RTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.d_time, TIME_STATS_SAMPLES));
4400 stats[ST_F_TTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.t_time, TIME_STATS_SAMPLES));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004401
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004402 if (flags & ST_SHLGNDS) {
4403 switch (addr_to_str(&sv->addr, str, sizeof(str))) {
4404 case AF_INET:
4405 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
4406 chunk_appendf(out, "%s:%d", str, get_host_port(&sv->addr));
4407 break;
4408 case AF_INET6:
4409 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
4410 chunk_appendf(out, "[%s]:%d", str, get_host_port(&sv->addr));
4411 break;
4412 case AF_UNIX:
4413 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, "unix");
4414 break;
4415 case -1:
4416 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
4417 chunk_strcat(out, strerror(errno));
4418 break;
4419 default: /* address family not supported */
4420 break;
Willy Tarreauf4659942013-11-28 10:50:06 +01004421 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004422
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004423 if (sv->cookie)
4424 stats[ST_F_COOKIE] = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, sv->cookie);
4425 }
4426
Thierry Fournier61fe6c02016-03-25 08:21:21 +01004427 return 1;
4428}
4429
4430/* Dumps a line for server <sv> and proxy <px> to the trash and uses the state
4431 * from stream interface <si>, stats flags <flags>, and server state <state>.
4432 * The caller is responsible for clearing the trash if needed. Returns non-zero
4433 * if it emits anything, zero otherwise.
4434 */
4435static int stats_dump_sv_stats(struct stream_interface *si, struct proxy *px, int flags, struct server *sv)
4436{
4437 struct appctx *appctx = __objt_appctx(si->end);
4438
4439 if (!stats_fill_sv_stats(px, sv, flags, stats, ST_F_TOTAL_FIELDS))
4440 return 0;
4441
Willy Tarreau501f6022016-01-08 17:37:22 +01004442 return stats_dump_one_line(stats, flags, px, appctx);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004443}
4444
Thierry Fournierd0a56c22016-03-25 08:21:51 +01004445/* Fill <stats> with the backend statistics. <stats> is
4446 * preallocated array of length <len>. The length of the array
4447 * must be at least ST_F_TOTAL_FIELDS. If this length is less
4448 * then this value, the function returns 0, otherwise, it
4449 * returns 1. <flags> can take the value ST_SHLGNDS.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004450 */
Thierry Fournierd0a56c22016-03-25 08:21:51 +01004451int stats_fill_be_stats(struct proxy *px, int flags, struct field *stats, int len)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004452{
Thierry Fournierd0a56c22016-03-25 08:21:51 +01004453 if (len < ST_F_TOTAL_FIELDS)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004454 return 0;
4455
Thierry Fournierd0a56c22016-03-25 08:21:51 +01004456 memset(stats, 0, sizeof(*stats) * len);
Willy Tarreauf6eecbe2016-01-06 16:14:50 +01004457
4458 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
4459 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, "BACKEND");
Willy Tarreauf8211df2016-01-11 14:09:38 +01004460 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
Willy Tarreauf6eecbe2016-01-06 16:14:50 +01004461 stats[ST_F_QCUR] = mkf_u32(0, px->nbpend);
4462 stats[ST_F_QMAX] = mkf_u32(FN_MAX, px->be_counters.nbpend_max);
4463 stats[ST_F_SCUR] = mkf_u32(FO_CONFIG|FN_LIMIT, px->beconn);
4464 stats[ST_F_SMAX] = mkf_u32(FN_MAX, px->be_counters.conn_max);
4465 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, px->fullconn);
4466 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, px->be_counters.cum_conn);
4467 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, px->be_counters.bytes_in);
4468 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, px->be_counters.bytes_out);
4469 stats[ST_F_DREQ] = mkf_u64(FN_COUNTER, px->be_counters.denied_req);
4470 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, px->be_counters.denied_resp);
4471 stats[ST_F_ECON] = mkf_u64(FN_COUNTER, px->be_counters.failed_conns);
4472 stats[ST_F_ERESP] = mkf_u64(FN_COUNTER, px->be_counters.failed_resp);
4473 stats[ST_F_WRETR] = mkf_u64(FN_COUNTER, px->be_counters.retries);
4474 stats[ST_F_WREDIS] = mkf_u64(FN_COUNTER, px->be_counters.redispatches);
4475 stats[ST_F_STATUS] = mkf_str(FO_STATUS, (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" : "DOWN");
4476 stats[ST_F_WEIGHT] = mkf_u32(FN_AVG, (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv);
4477 stats[ST_F_ACT] = mkf_u32(0, px->srv_act);
4478 stats[ST_F_BCK] = mkf_u32(0, px->srv_bck);
4479 stats[ST_F_CHKDOWN] = mkf_u64(FN_COUNTER, px->down_trans);
4480 stats[ST_F_LASTCHG] = mkf_u32(FN_AGE, now.tv_sec - px->last_change);
Willy Tarreau7344f472016-01-11 12:04:02 +01004481 if (px->srv)
4482 stats[ST_F_DOWNTIME] = mkf_u32(FN_COUNTER, be_downtime(px));
4483
Willy Tarreauf6eecbe2016-01-06 16:14:50 +01004484 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
4485 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
4486 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, 0);
4487 stats[ST_F_LBTOT] = mkf_u64(FN_COUNTER, px->be_counters.cum_lbconn);
4488 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_BE);
4489 stats[ST_F_RATE] = mkf_u32(0, read_freq_ctr(&px->be_sess_per_sec));
4490 stats[ST_F_RATE_MAX] = mkf_u32(0, px->be_counters.sps_max);
Willy Tarreauf1516d92016-01-11 14:48:36 +01004491
Willy Tarreaue4847c62016-01-08 15:43:54 +01004492 if (flags & ST_SHLGNDS) {
4493 if (px->cookie_name)
4494 stats[ST_F_COOKIE] = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, px->cookie_name);
Willy Tarreauf1516d92016-01-11 14:48:36 +01004495 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 +01004496 }
Willy Tarreauf6eecbe2016-01-06 16:14:50 +01004497
4498 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
4499 if (px->mode == PR_MODE_HTTP) {
4500 stats[ST_F_REQ_TOT] = mkf_u64(FN_COUNTER, px->be_counters.p.http.cum_req);
4501 stats[ST_F_HRSP_1XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[1]);
4502 stats[ST_F_HRSP_2XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[2]);
4503 stats[ST_F_HRSP_3XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[3]);
4504 stats[ST_F_HRSP_4XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[4]);
4505 stats[ST_F_HRSP_5XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[5]);
4506 stats[ST_F_HRSP_OTHER] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[0]);
Willy Tarreau5b9bdff2016-01-11 14:40:47 +01004507 stats[ST_F_INTERCEPTED] = mkf_u64(FN_COUNTER, px->be_counters.intercepted_req);
Willy Tarreauf6eecbe2016-01-06 16:14:50 +01004508 }
4509
4510 stats[ST_F_CLI_ABRT] = mkf_u64(FN_COUNTER, px->be_counters.cli_aborts);
4511 stats[ST_F_SRV_ABRT] = mkf_u64(FN_COUNTER, px->be_counters.srv_aborts);
4512
4513 /* compression: in, out, bypassed, responses */
4514 stats[ST_F_COMP_IN] = mkf_u64(FN_COUNTER, px->be_counters.comp_in);
4515 stats[ST_F_COMP_OUT] = mkf_u64(FN_COUNTER, px->be_counters.comp_out);
4516 stats[ST_F_COMP_BYP] = mkf_u64(FN_COUNTER, px->be_counters.comp_byp);
4517 stats[ST_F_COMP_RSP] = mkf_u64(FN_COUNTER, px->be_counters.p.http.comp_rsp);
4518 stats[ST_F_LASTSESS] = mkf_s32(FN_AGE, be_lastsession(px));
4519
4520 stats[ST_F_QTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.q_time, TIME_STATS_SAMPLES));
4521 stats[ST_F_CTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.c_time, TIME_STATS_SAMPLES));
4522 stats[ST_F_RTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.d_time, TIME_STATS_SAMPLES));
4523 stats[ST_F_TTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.t_time, TIME_STATS_SAMPLES));
4524
Thierry Fournierd0a56c22016-03-25 08:21:51 +01004525 return 1;
4526}
4527
4528/* Dumps a line for backend <px> to the trash for and uses the state from stream
4529 * interface <si> and stats flags <flags>. The caller is responsible for clearing
4530 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
4531 */
4532static int stats_dump_be_stats(struct stream_interface *si, struct proxy *px, int flags)
4533{
4534 struct appctx *appctx = __objt_appctx(si->end);
4535
4536 if (!(px->cap & PR_CAP_BE))
4537 return 0;
4538
4539 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_BE)))
4540 return 0;
4541
4542 if (!stats_fill_be_stats(px, flags, stats, ST_F_TOTAL_FIELDS))
4543 return 0;
4544
Willy Tarreau501f6022016-01-08 17:37:22 +01004545 return stats_dump_one_line(stats, flags, px, appctx);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004546}
4547
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004548/* Dumps the HTML table header for proxy <px> to the trash for and uses the state from
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004549 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004550 * for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004551 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004552static void stats_dump_html_px_hdr(struct stream_interface *si, struct proxy *px, struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004553{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004554 struct appctx *appctx = __objt_appctx(si->end);
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004555 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
4556
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004557 if (px->cap & PR_CAP_BE && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004558 /* A form to enable/disable this proxy servers */
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004559
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004560 /* 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 +02004561 scope_txt[0] = 0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004562 if (appctx->ctx.stats.scope_len) {
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004563 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau4e4292b2014-11-28 12:18:45 +01004564 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 +01004565 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004566 }
4567
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004568 chunk_appendf(&trash,
Jeff Buchbinder2dbbf4d2014-08-29 15:10:08 -05004569 "<form method=\"post\">");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004570 }
4571
4572 /* print a new table */
4573 chunk_appendf(&trash,
4574 "<table class=\"tbl\" width=\"100%%\">\n"
4575 "<tr class=\"titre\">"
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01004576 "<th class=\"pxname\" width=\"10%%\">");
4577
4578 chunk_appendf(&trash,
4579 "<a name=\"%s\"></a>%s"
4580 "<a class=px href=\"#%s\">%s</a>",
4581 px->id,
4582 (uri->flags & ST_SHLGNDS) ? "<u>":"",
4583 px->id, px->id);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004584
4585 if (uri->flags & ST_SHLGNDS) {
4586 /* cap, mode, id */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02004587 chunk_appendf(&trash, "<div class=tips>cap: %s, mode: %s, id: %d",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004588 proxy_cap_str(px->cap), proxy_mode_str(px->mode),
4589 px->uuid);
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01004590 chunk_appendf(&trash, "</div>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004591 }
4592
4593 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01004594 "%s</th>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004595 "<th class=\"%s\" width=\"90%%\">%s</th>"
4596 "</tr>\n"
4597 "</table>\n"
4598 "<table class=\"tbl\" width=\"100%%\">\n"
4599 "<tr class=\"titre\">",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004600 (uri->flags & ST_SHLGNDS) ? "</u>":"",
4601 px->desc ? "desc" : "empty", px->desc ? px->desc : "");
4602
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004603 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004604 /* Column heading for Enable or Disable server */
4605 chunk_appendf(&trash, "<th rowspan=2 width=1></th>");
Willy Tarreau91861262007-10-17 17:06:05 +02004606 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004607
4608 chunk_appendf(&trash,
4609 "<th rowspan=2></th>"
4610 "<th colspan=3>Queue</th>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05004611 "<th colspan=3>Session rate</th><th colspan=6>Sessions</th>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004612 "<th colspan=2>Bytes</th><th colspan=2>Denied</th>"
4613 "<th colspan=3>Errors</th><th colspan=2>Warnings</th>"
4614 "<th colspan=9>Server</th>"
4615 "</tr>\n"
4616 "<tr class=\"titre\">"
4617 "<th>Cur</th><th>Max</th><th>Limit</th>"
4618 "<th>Cur</th><th>Max</th><th>Limit</th><th>Cur</th><th>Max</th>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05004619 "<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 +01004620 "<th>Req</th><th>Resp</th><th>Req</th><th>Conn</th>"
4621 "<th>Resp</th><th>Retr</th><th>Redis</th>"
4622 "<th>Status</th><th>LastChk</th><th>Wght</th><th>Act</th>"
4623 "<th>Bck</th><th>Chk</th><th>Dwn</th><th>Dwntme</th>"
4624 "<th>Thrtle</th>\n"
4625 "</tr>");
Willy Tarreau91861262007-10-17 17:06:05 +02004626}
4627
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004628/* Dumps the HTML table trailer for proxy <px> to the trash for and uses the state from
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004629 * stream interface <si>. The caller is responsible for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004630 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004631static void stats_dump_html_px_end(struct stream_interface *si, struct proxy *px)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004632{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004633 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004634 chunk_appendf(&trash, "</table>");
4635
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004636 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004637 /* close the form used to enable/disable this proxy servers */
4638 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004639 "Choose the action to perform on the checked servers : "
4640 "<select name=action>"
4641 "<option value=\"\"></option>"
Willy Tarreaued7df902014-05-22 18:04:49 +02004642 "<option value=\"ready\">Set state to READY</option>"
4643 "<option value=\"drain\">Set state to DRAIN</option>"
Marco Corte8c27bca2014-07-02 17:49:34 +02004644 "<option value=\"maint\">Set state to MAINT</option>"
Willy Tarreau248a60e2014-05-23 14:59:48 +02004645 "<option value=\"dhlth\">Health: disable checks</option>"
4646 "<option value=\"ehlth\">Health: enable checks</option>"
4647 "<option value=\"hrunn\">Health: force UP</option>"
4648 "<option value=\"hnolb\">Health: force NOLB</option>"
4649 "<option value=\"hdown\">Health: force DOWN</option>"
4650 "<option value=\"dagent\">Agent: disable checks</option>"
4651 "<option value=\"eagent\">Agent: enable checks</option>"
4652 "<option value=\"arunn\">Agent: force UP</option>"
4653 "<option value=\"adown\">Agent: force DOWN</option>"
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004654 "<option value=\"shutdown\">Kill Sessions</option>"
4655 "</select>"
4656 "<input type=\"hidden\" name=\"b\" value=\"#%d\">"
4657 "&nbsp;<input type=\"submit\" value=\"Apply\">"
4658 "</form>",
4659 px->uuid);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004660 }
4661
4662 chunk_appendf(&trash, "<p>\n");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004663}
Willy Tarreau91861262007-10-17 17:06:05 +02004664
4665/*
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004666 * Dumps statistics for a proxy. The output is sent to the stream interface's
4667 * input buffer. Returns 0 if it had to stop dumping data because of lack of
4668 * buffer space, or non-zero if everything completed. This function is used
4669 * both by the CLI and the HTTP entry points, and is able to dump the output
4670 * in HTML or CSV formats. If the later, <uri> must be NULL.
Willy Tarreau91861262007-10-17 17:06:05 +02004671 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004672static int stats_dump_proxy_to_buffer(struct stream_interface *si, struct proxy *px, struct uri_auth *uri)
Willy Tarreau91861262007-10-17 17:06:05 +02004673{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004674 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau87b09662015-04-03 00:22:06 +02004675 struct stream *s = si_strm(si);
Willy Tarreau2bb4a962014-11-28 11:11:05 +01004676 struct channel *rep = si_ic(si);
Willy Tarreau44267702011-10-28 15:35:33 +02004677 struct server *sv, *svs; /* server and server-state, server-state=server or server->track */
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004678 struct listener *l;
Willy Tarreau0deb85a2016-01-08 15:33:18 +01004679 unsigned int flags;
4680
4681 if (uri)
4682 flags = uri->flags;
4683 else if (strm_li(s)->bind_conf->level >= ACCESS_LVL_OPER)
4684 flags = ST_SHLGNDS | ST_SHNODE | ST_SHDESC;
4685 else
4686 flags = ST_SHNODE | ST_SHDESC;
Willy Tarreau91861262007-10-17 17:06:05 +02004687
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004688 chunk_reset(&trash);
Willy Tarreau91861262007-10-17 17:06:05 +02004689
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004690 switch (appctx->ctx.stats.px_st) {
Willy Tarreau295a8372011-03-10 11:25:07 +01004691 case STAT_PX_ST_INIT:
Willy Tarreau91861262007-10-17 17:06:05 +02004692 /* we are on a new proxy */
Willy Tarreau91861262007-10-17 17:06:05 +02004693 if (uri && uri->scope) {
4694 /* we have a limited scope, we have to check the proxy name */
4695 struct stat_scope *scope;
4696 int len;
4697
4698 len = strlen(px->id);
4699 scope = uri->scope;
4700
4701 while (scope) {
4702 /* match exact proxy name */
4703 if (scope->px_len == len && !memcmp(px->id, scope->px_id, len))
4704 break;
4705
4706 /* match '.' which means 'self' proxy */
Willy Tarreau1388a3a2007-10-18 16:38:37 +02004707 if (!strcmp(scope->px_id, ".") && px == s->be)
Willy Tarreau91861262007-10-17 17:06:05 +02004708 break;
4709 scope = scope->next;
4710 }
4711
4712 /* proxy name not found : don't dump anything */
4713 if (scope == NULL)
4714 return 1;
4715 }
4716
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004717 /* if the user has requested a limited output and the proxy
4718 * name does not match, skip it.
4719 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004720 if (appctx->ctx.stats.scope_len &&
Willy Tarreau4e4292b2014-11-28 12:18:45 +01004721 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 +02004722 return 1;
4723
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004724 if ((appctx->ctx.stats.flags & STAT_BOUND) &&
4725 (appctx->ctx.stats.iid != -1) &&
4726 (px->uuid != appctx->ctx.stats.iid))
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01004727 return 1;
4728
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004729 appctx->ctx.stats.px_st = STAT_PX_ST_TH;
Willy Tarreau91861262007-10-17 17:06:05 +02004730 /* fall through */
4731
Willy Tarreau295a8372011-03-10 11:25:07 +01004732 case STAT_PX_ST_TH:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004733 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004734 stats_dump_html_px_hdr(si, px, uri);
Willy Tarreaubc18da12015-03-13 14:00:47 +01004735 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004736 si_applet_cant_put(si);
Willy Tarreau55bb8452007-10-17 18:44:57 +02004737 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01004738 }
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004739 }
Willy Tarreau91861262007-10-17 17:06:05 +02004740
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004741 appctx->ctx.stats.px_st = STAT_PX_ST_FE;
Willy Tarreau91861262007-10-17 17:06:05 +02004742 /* fall through */
4743
Willy Tarreau295a8372011-03-10 11:25:07 +01004744 case STAT_PX_ST_FE:
Willy Tarreau91861262007-10-17 17:06:05 +02004745 /* print the frontend */
Willy Tarreaubc18da12015-03-13 14:00:47 +01004746 if (stats_dump_fe_stats(si, px)) {
4747 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004748 si_applet_cant_put(si);
Willy Tarreau91861262007-10-17 17:06:05 +02004749 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01004750 }
4751 }
Willy Tarreau91861262007-10-17 17:06:05 +02004752
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004753 appctx->ctx.stats.l = px->conf.listeners.n;
4754 appctx->ctx.stats.px_st = STAT_PX_ST_LI;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004755 /* fall through */
4756
Willy Tarreau295a8372011-03-10 11:25:07 +01004757 case STAT_PX_ST_LI:
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004758 /* stats.l has been initialized above */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004759 for (; appctx->ctx.stats.l != &px->conf.listeners; appctx->ctx.stats.l = l->by_fe.n) {
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01004760 if (buffer_almost_full(rep->buf)) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004761 si_applet_cant_put(si);
Willy Tarreau4e33d862009-10-11 23:35:10 +02004762 return 0;
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01004763 }
Willy Tarreau4e33d862009-10-11 23:35:10 +02004764
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004765 l = LIST_ELEM(appctx->ctx.stats.l, struct listener *, by_fe);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004766 if (!l->counters)
4767 continue;
4768
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004769 if (appctx->ctx.stats.flags & STAT_BOUND) {
4770 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SO)))
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004771 break;
4772
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004773 if (appctx->ctx.stats.sid != -1 && l->luid != appctx->ctx.stats.sid)
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004774 continue;
4775 }
4776
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004777 /* print the frontend */
Willy Tarreau0deb85a2016-01-08 15:33:18 +01004778 if (stats_dump_li_stats(si, px, l, flags)) {
Willy Tarreaubc18da12015-03-13 14:00:47 +01004779 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004780 si_applet_cant_put(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004781 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01004782 }
4783 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004784 }
4785
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004786 appctx->ctx.stats.sv = px->srv; /* may be NULL */
4787 appctx->ctx.stats.px_st = STAT_PX_ST_SV;
Willy Tarreau91861262007-10-17 17:06:05 +02004788 /* fall through */
4789
Willy Tarreau295a8372011-03-10 11:25:07 +01004790 case STAT_PX_ST_SV:
Willy Tarreau91861262007-10-17 17:06:05 +02004791 /* stats.sv has been initialized above */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004792 for (; appctx->ctx.stats.sv != NULL; appctx->ctx.stats.sv = sv->next) {
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01004793 if (buffer_almost_full(rep->buf)) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004794 si_applet_cant_put(si);
Willy Tarreau4e33d862009-10-11 23:35:10 +02004795 return 0;
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01004796 }
Willy Tarreau4e33d862009-10-11 23:35:10 +02004797
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004798 sv = appctx->ctx.stats.sv;
Willy Tarreau91861262007-10-17 17:06:05 +02004799
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004800 if (appctx->ctx.stats.flags & STAT_BOUND) {
4801 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SV)))
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01004802 break;
4803
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004804 if (appctx->ctx.stats.sid != -1 && sv->puid != appctx->ctx.stats.sid)
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01004805 continue;
4806 }
4807
Willy Tarreau32091232014-05-16 13:52:00 +02004808 svs = sv;
4809 while (svs->track)
4810 svs = svs->track;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01004811
Willy Tarreauba2f2642016-01-07 09:54:40 +01004812 /* do not report servers which are DOWN and not changing state */
4813 if ((appctx->ctx.stats.flags & STAT_HIDE_DOWN) &&
4814 ((sv->admin & SRV_ADMF_MAINT) || /* server is in maintenance */
4815 (sv->state == SRV_ST_STOPPED && /* server is down */
4816 (!((svs->agent.state | svs->check.state) & CHK_ST_ENABLED) ||
4817 ((svs->agent.state & CHK_ST_ENABLED) && !svs->agent.health) ||
4818 ((svs->check.state & CHK_ST_ENABLED) && !svs->check.health))))) {
Willy Tarreau91861262007-10-17 17:06:05 +02004819 continue;
4820 }
4821
Willy Tarreau0deb85a2016-01-08 15:33:18 +01004822 if (stats_dump_sv_stats(si, px, flags, sv)) {
Willy Tarreaubc18da12015-03-13 14:00:47 +01004823 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004824 si_applet_cant_put(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004825 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01004826 }
4827 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004828 } /* for sv */
Cyril Bonté474be412010-10-12 00:14:36 +02004829
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004830 appctx->ctx.stats.px_st = STAT_PX_ST_BE;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004831 /* fall through */
Cyril Bonté70be45d2010-10-12 00:14:35 +02004832
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004833 case STAT_PX_ST_BE:
4834 /* print the backend */
Willy Tarreau0deb85a2016-01-08 15:33:18 +01004835 if (stats_dump_be_stats(si, px, flags)) {
Willy Tarreaubc18da12015-03-13 14:00:47 +01004836 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004837 si_applet_cant_put(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004838 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01004839 }
4840 }
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01004841
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004842 appctx->ctx.stats.px_st = STAT_PX_ST_END;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004843 /* fall through */
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01004844
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004845 case STAT_PX_ST_END:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004846 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004847 stats_dump_html_px_end(si, px);
Willy Tarreaubc18da12015-03-13 14:00:47 +01004848 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004849 si_applet_cant_put(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004850 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01004851 }
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004852 }
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01004853
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004854 appctx->ctx.stats.px_st = STAT_PX_ST_FIN;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004855 /* fall through */
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01004856
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004857 case STAT_PX_ST_FIN:
4858 return 1;
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01004859
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004860 default:
4861 /* unknown state, we should put an abort() here ! */
4862 return 1;
4863 }
4864}
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01004865
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004866/* Dumps the HTTP stats head block to the trash for and uses the per-uri
4867 * parameters <uri>. The caller is responsible for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004868 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004869static void stats_dump_html_head(struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004870{
4871 /* WARNING! This must fit in the first buffer !!! */
4872 chunk_appendf(&trash,
4873 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n"
4874 "\"http://www.w3.org/TR/html4/loose.dtd\">\n"
4875 "<html><head><title>Statistics Report for " PRODUCT_NAME "%s%s</title>\n"
4876 "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n"
4877 "<style type=\"text/css\"><!--\n"
4878 "body {"
4879 " font-family: arial, helvetica, sans-serif;"
4880 " font-size: 12px;"
4881 " font-weight: normal;"
4882 " color: black;"
4883 " background: white;"
4884 "}\n"
4885 "th,td {"
4886 " font-size: 10px;"
4887 "}\n"
4888 "h1 {"
4889 " font-size: x-large;"
4890 " margin-bottom: 0.5em;"
4891 "}\n"
4892 "h2 {"
4893 " font-family: helvetica, arial;"
4894 " font-size: x-large;"
4895 " font-weight: bold;"
4896 " font-style: italic;"
4897 " color: #6020a0;"
4898 " margin-top: 0em;"
4899 " margin-bottom: 0em;"
4900 "}\n"
4901 "h3 {"
4902 " font-family: helvetica, arial;"
4903 " font-size: 16px;"
4904 " font-weight: bold;"
4905 " color: #b00040;"
4906 " background: #e8e8d0;"
4907 " margin-top: 0em;"
4908 " margin-bottom: 0em;"
4909 "}\n"
4910 "li {"
4911 " margin-top: 0.25em;"
4912 " margin-right: 2em;"
4913 "}\n"
4914 ".hr {margin-top: 0.25em;"
4915 " border-color: black;"
4916 " border-bottom-style: solid;"
4917 "}\n"
4918 ".titre {background: #20D0D0;color: #000000; font-weight: bold; text-align: center;}\n"
4919 ".total {background: #20D0D0;color: #ffff80;}\n"
4920 ".frontend {background: #e8e8d0;}\n"
4921 ".socket {background: #d0d0d0;}\n"
4922 ".backend {background: #e8e8d0;}\n"
Simon Horman837bfa72015-04-23 14:51:27 +09004923 ".active_down {background: #ff9090;}\n"
4924 ".active_going_up {background: #ffd020;}\n"
4925 ".active_going_down {background: #ffffa0;}\n"
4926 ".active_up {background: #c0ffc0;}\n"
4927 ".active_nolb {background: #20a0ff;}\n"
4928 ".active_draining {background: #20a0FF;}\n"
4929 ".active_no_check {background: #e0e0e0;}\n"
4930 ".backup_down {background: #ff9090;}\n"
4931 ".backup_going_up {background: #ff80ff;}\n"
4932 ".backup_going_down {background: #c060ff;}\n"
4933 ".backup_up {background: #b0d0ff;}\n"
4934 ".backup_nolb {background: #90b0e0;}\n"
4935 ".backup_draining {background: #cc9900;}\n"
4936 ".backup_no_check {background: #e0e0e0;}\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004937 ".maintain {background: #c07820;}\n"
4938 ".rls {letter-spacing: 0.2em; margin-right: 1px;}\n" /* right letter spacing (used for grouping digits) */
4939 "\n"
4940 "a.px:link {color: #ffff40; text-decoration: none;}"
4941 "a.px:visited {color: #ffff40; text-decoration: none;}"
4942 "a.px:hover {color: #ffffff; text-decoration: none;}"
4943 "a.lfsb:link {color: #000000; text-decoration: none;}"
4944 "a.lfsb:visited {color: #000000; text-decoration: none;}"
4945 "a.lfsb:hover {color: #505050; text-decoration: none;}"
4946 "\n"
4947 "table.tbl { border-collapse: collapse; border-style: none;}\n"
4948 "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"
4949 "table.tbl td.ac { text-align: center;}\n"
4950 "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}\n"
4951 "table.tbl th.pxname { background: #b00040; color: #ffff40; font-weight: bold; border-style: solid solid none solid; padding: 2px 3px; white-space: nowrap;}\n"
4952 "table.tbl th.empty { border-style: none; empty-cells: hide; background: white;}\n"
4953 "table.tbl th.desc { background: white; border-style: solid solid none solid; text-align: left; padding: 2px 3px;}\n"
4954 "\n"
4955 "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n"
4956 "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n"
4957 "table.lgd td.noborder { border-style: none; padding: 2px; white-space: nowrap;}\n"
Willy Tarreau466c9b52012-12-23 02:25:03 +01004958 "table.det { border-collapse: collapse; border-style: none; }\n"
4959 "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 +01004960 "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 +01004961 "u {text-decoration:none; border-bottom: 1px dotted black;}\n"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02004962 "div.tips {\n"
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01004963 " display:block;\n"
4964 " visibility:hidden;\n"
4965 " z-index:2147483647;\n"
4966 " position:absolute;\n"
4967 " padding:2px 4px 3px;\n"
4968 " background:#f0f060; color:#000000;\n"
4969 " border:1px solid #7040c0;\n"
4970 " white-space:nowrap;\n"
4971 " font-style:normal;font-size:11px;font-weight:normal;\n"
4972 " -moz-border-radius:3px;-webkit-border-radius:3px;border-radius:3px;\n"
4973 " -moz-box-shadow:gray 2px 2px 3px;-webkit-box-shadow:gray 2px 2px 3px;box-shadow:gray 2px 2px 3px;\n"
4974 "}\n"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02004975 "u:hover div.tips {visibility:visible;}\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004976 "-->\n"
4977 "</style></head>\n",
4978 (uri->flags & ST_SHNODE) ? " on " : "",
4979 (uri->flags & ST_SHNODE) ? (uri->node ? uri->node : global.node) : ""
4980 );
4981}
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01004982
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004983/* Dumps the HTML stats information block to the trash for and uses the state from
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004984 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004985 * for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004986 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004987static void stats_dump_html_info(struct stream_interface *si, struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004988{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004989 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004990 unsigned int up = (now.tv_sec - start_date.tv_sec);
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004991 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01004992
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004993 /* WARNING! this has to fit the first packet too.
4994 * We are around 3.5 kB, add adding entries will
4995 * become tricky if we want to support 4kB buffers !
4996 */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004997 chunk_appendf(&trash,
4998 "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">"
4999 PRODUCT_NAME "%s</a></h1>\n"
5000 "<h2>Statistics Report for pid %d%s%s%s%s</h2>\n"
5001 "<hr width=\"100%%\" class=\"hr\">\n"
5002 "<h3>&gt; General process information</h3>\n"
5003 "<table border=0><tr><td align=\"left\" nowrap width=\"1%%\">\n"
5004 "<p><b>pid = </b> %d (process #%d, nbproc = %d)<br>\n"
5005 "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n"
5006 "<b>system limits:</b> memmax = %s%s; ulimit-n = %d<br>\n"
5007 "<b>maxsock = </b> %d; <b>maxconn = </b> %d; <b>maxpipes = </b> %d<br>\n"
5008 "current conns = %d; current pipes = %d/%d; conn rate = %d/sec<br>\n"
5009 "Running tasks: %d/%d; idle = %d %%<br>\n"
5010 "</td><td align=\"center\" nowrap>\n"
5011 "<table class=\"lgd\"><tr>\n"
Simon Horman837bfa72015-04-23 14:51:27 +09005012 "<td class=\"active_up\">&nbsp;</td><td class=\"noborder\">active UP </td>"
5013 "<td class=\"backup_up\">&nbsp;</td><td class=\"noborder\">backup UP </td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005014 "</tr><tr>\n"
Simon Horman837bfa72015-04-23 14:51:27 +09005015 "<td class=\"active_going_down\"></td><td class=\"noborder\">active UP, going down </td>"
5016 "<td class=\"backup_going_down\"></td><td class=\"noborder\">backup UP, going down </td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005017 "</tr><tr>\n"
Simon Horman837bfa72015-04-23 14:51:27 +09005018 "<td class=\"active_going_up\"></td><td class=\"noborder\">active DOWN, going up </td>"
5019 "<td class=\"backup_going_up\"></td><td class=\"noborder\">backup DOWN, going up </td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005020 "</tr><tr>\n"
Simon Horman837bfa72015-04-23 14:51:27 +09005021 "<td class=\"active_down\"></td><td class=\"noborder\">active or backup DOWN &nbsp;</td>"
5022 "<td class=\"active_no_check\"></td><td class=\"noborder\">not checked </td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005023 "</tr><tr>\n"
5024 "<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 -07005025 "</tr><tr>\n"
Simon Horman837bfa72015-04-23 14:51:27 +09005026 "<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 +01005027 "</tr></table>\n"
Willy Tarreau6b7764a2013-12-04 00:43:21 +01005028 "Note: \"NOLB\"/\"DRAIN\" = UP with load-balancing disabled."
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005029 "</td>"
5030 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
5031 "<b>Display option:</b><ul style=\"margin-top: 0.25em;\">"
5032 "",
5033 (uri->flags & ST_HIDEVER) ? "" : (STATS_VERSION_STRING),
5034 pid, (uri->flags & ST_SHNODE) ? " on " : "",
5035 (uri->flags & ST_SHNODE) ? (uri->node ? uri->node : global.node) : "",
5036 (uri->flags & ST_SHDESC) ? ": " : "",
5037 (uri->flags & ST_SHDESC) ? (uri->desc ? uri->desc : global.desc) : "",
5038 pid, relative_pid, global.nbproc,
5039 up / 86400, (up % 86400) / 3600,
5040 (up % 3600) / 60, (up % 60),
5041 global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited",
5042 global.rlimit_memmax ? " MB" : "",
5043 global.rlimit_nofile,
5044 global.maxsock, global.maxconn, global.maxpipes,
5045 actconn, pipes_used, pipes_used+pipes_free, read_freq_ctr(&global.conn_per_sec),
5046 run_queue_cur, nb_tasks_cur, idle_pct
5047 );
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005048
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005049 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Willy Tarreau4e4292b2014-11-28 12:18:45 +01005050 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 +01005051 scope_txt[appctx->ctx.stats.scope_len] = '\0';
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02005052
5053 chunk_appendf(&trash,
Jeff Buchbinder2dbbf4d2014-08-29 15:10:08 -05005054 "<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 +01005055 (appctx->ctx.stats.scope_len > 0) ? scope_txt : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02005056 STAT_SCOPE_TXT_MAXLEN);
5057
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005058 /* 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 +02005059 scope_txt[0] = 0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005060 if (appctx->ctx.stats.scope_len) {
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02005061 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau4e4292b2014-11-28 12:18:45 +01005062 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 +01005063 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02005064 }
5065
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005066 if (appctx->ctx.stats.flags & STAT_HIDE_DOWN)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005067 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02005068 "<li><a href=\"%s%s%s%s\">Show all servers</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005069 uri->uri_prefix,
5070 "",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005071 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02005072 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005073 else
5074 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02005075 "<li><a href=\"%s%s%s%s\">Hide 'DOWN' servers</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005076 uri->uri_prefix,
5077 ";up",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005078 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02005079 scope_txt);
Willy Tarreau91861262007-10-17 17:06:05 +02005080
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005081 if (uri->refresh > 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005082 if (appctx->ctx.stats.flags & STAT_NO_REFRESH)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005083 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02005084 "<li><a href=\"%s%s%s%s\">Enable refresh</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005085 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005086 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02005087 "",
5088 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005089 else
5090 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02005091 "<li><a href=\"%s%s%s%s\">Disable refresh</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005092 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005093 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02005094 ";norefresh",
5095 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005096 }
Willy Tarreau55bb8452007-10-17 18:44:57 +02005097
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005098 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02005099 "<li><a href=\"%s%s%s%s\">Refresh now</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005100 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005101 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
5102 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02005103 scope_txt);
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02005104
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005105 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02005106 "<li><a href=\"%s;csv%s%s\">CSV export</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005107 uri->uri_prefix,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02005108 (uri->refresh > 0) ? ";norefresh" : "",
5109 scope_txt);
Willy Tarreau4bab24d2007-11-30 18:16:29 +01005110
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005111 chunk_appendf(&trash,
5112 "</ul></td>"
5113 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
5114 "<b>External resources:</b><ul style=\"margin-top: 0.25em;\">\n"
5115 "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>\n"
5116 "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>\n"
5117 "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>\n"
5118 "</ul>"
5119 "</td>"
5120 "</tr></table>\n"
5121 ""
5122 );
Willy Tarreau4bab24d2007-11-30 18:16:29 +01005123
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005124 if (appctx->ctx.stats.st_code) {
5125 switch (appctx->ctx.stats.st_code) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005126 case STAT_STATUS_DONE:
5127 chunk_appendf(&trash,
Simon Horman837bfa72015-04-23 14:51:27 +09005128 "<p><div class=active_up>"
Willy Tarreauba6be982013-04-19 12:16:55 +02005129 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005130 "Action processed successfully."
Willy Tarreauba6be982013-04-19 12:16:55 +02005131 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005132 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
5133 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02005134 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005135 break;
5136 case STAT_STATUS_NONE:
5137 chunk_appendf(&trash,
Simon Horman837bfa72015-04-23 14:51:27 +09005138 "<p><div class=active_going_down>"
Willy Tarreauba6be982013-04-19 12:16:55 +02005139 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005140 "Nothing has changed."
Willy Tarreauba6be982013-04-19 12:16:55 +02005141 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005142 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
5143 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02005144 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005145 break;
5146 case STAT_STATUS_PART:
5147 chunk_appendf(&trash,
Simon Horman837bfa72015-04-23 14:51:27 +09005148 "<p><div class=active_going_down>"
Willy Tarreauba6be982013-04-19 12:16:55 +02005149 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005150 "Action partially processed.<br>"
5151 "Some server names are probably unknown or ambiguous (duplicated names in the backend)."
Willy Tarreauba6be982013-04-19 12:16:55 +02005152 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005153 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
5154 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02005155 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005156 break;
5157 case STAT_STATUS_ERRP:
5158 chunk_appendf(&trash,
Simon Horman837bfa72015-04-23 14:51:27 +09005159 "<p><div class=active_down>"
Willy Tarreauba6be982013-04-19 12:16:55 +02005160 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005161 "Action not processed because of invalid parameters."
5162 "<ul>"
5163 "<li>The action is maybe unknown.</li>"
5164 "<li>The backend name is probably unknown or ambiguous (duplicated names).</li>"
5165 "<li>Some server names are probably unknown or ambiguous (duplicated names in the backend).</li>"
5166 "</ul>"
Willy Tarreauba6be982013-04-19 12:16:55 +02005167 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005168 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
5169 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02005170 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005171 break;
5172 case STAT_STATUS_EXCD:
5173 chunk_appendf(&trash,
Simon Horman837bfa72015-04-23 14:51:27 +09005174 "<p><div class=active_down>"
Willy Tarreauba6be982013-04-19 12:16:55 +02005175 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005176 "<b>Action not processed : the buffer couldn't store all the data.<br>"
5177 "You should retry with less servers at a time.</b>"
Willy Tarreauba6be982013-04-19 12:16:55 +02005178 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005179 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
5180 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02005181 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005182 break;
5183 case STAT_STATUS_DENY:
5184 chunk_appendf(&trash,
Simon Horman837bfa72015-04-23 14:51:27 +09005185 "<p><div class=active_down>"
Willy Tarreauba6be982013-04-19 12:16:55 +02005186 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005187 "<b>Action denied.</b>"
Willy Tarreauba6be982013-04-19 12:16:55 +02005188 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005189 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
5190 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02005191 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005192 break;
5193 default:
5194 chunk_appendf(&trash,
Simon Horman837bfa72015-04-23 14:51:27 +09005195 "<p><div class=active_no_check>"
Willy Tarreauba6be982013-04-19 12:16:55 +02005196 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005197 "Unexpected result."
Willy Tarreauba6be982013-04-19 12:16:55 +02005198 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005199 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
5200 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02005201 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005202 }
5203 chunk_appendf(&trash, "<p>\n");
5204 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005205}
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01005206
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01005207/* Dumps the HTML stats trailer block to the trash. The caller is responsible
5208 * for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005209 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01005210static void stats_dump_html_end()
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005211{
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005212 chunk_appendf(&trash, "</body></html>\n");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005213}
Willy Tarreau7f062c42009-03-05 18:43:00 +01005214
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01005215/* This function dumps statistics onto the stream interface's read buffer in
5216 * either CSV or HTML format. <uri> contains some HTML-specific parameters that
Willy Tarreau306f8302013-07-08 15:53:06 +02005217 * are ignored for CSV format (hence <uri> may be NULL there). It returns 0 if
5218 * 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 +02005219 * and the stream must be closed, or -1 in case of any error. This function is
Willy Tarreau306f8302013-07-08 15:53:06 +02005220 * used by both the CLI and the HTTP handlers.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005221 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01005222static int stats_dump_stat_to_buffer(struct stream_interface *si, struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005223{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005224 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005225 struct channel *rep = si_ic(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005226 struct proxy *px;
Willy Tarreau7f062c42009-03-05 18:43:00 +01005227
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005228 chunk_reset(&trash);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02005229
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005230 switch (appctx->st2) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005231 case STAT_ST_INIT:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005232 appctx->st2 = STAT_ST_HEAD; /* let's start producing data */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005233 /* fall through */
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005234
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005235 case STAT_ST_HEAD:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005236 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01005237 stats_dump_html_head(uri);
Willy Tarreau1e62df92016-01-11 18:57:53 +01005238 else if (!(appctx->ctx.stats.flags & STAT_FMT_TYPED))
Willy Tarreau354898b2012-12-23 18:15:23 +01005239 stats_dump_csv_header();
Willy Tarreaud9b587f2010-02-26 10:05:55 +01005240
Willy Tarreaubc18da12015-03-13 14:00:47 +01005241 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02005242 si_applet_cant_put(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005243 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01005244 }
Willy Tarreauae526782010-03-04 20:34:23 +01005245
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005246 appctx->st2 = STAT_ST_INFO;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005247 /* fall through */
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01005248
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005249 case STAT_ST_INFO:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005250 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01005251 stats_dump_html_info(si, uri);
Willy Tarreaubc18da12015-03-13 14:00:47 +01005252 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02005253 si_applet_cant_put(si);
Willy Tarreau91861262007-10-17 17:06:05 +02005254 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01005255 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005256 }
Willy Tarreau91861262007-10-17 17:06:05 +02005257
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005258 appctx->ctx.stats.px = proxy;
5259 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
5260 appctx->st2 = STAT_ST_LIST;
Willy Tarreau91861262007-10-17 17:06:05 +02005261 /* fall through */
5262
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005263 case STAT_ST_LIST:
5264 /* dump proxies */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005265 while (appctx->ctx.stats.px) {
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01005266 if (buffer_almost_full(rep->buf)) {
Willy Tarreaufe127932015-04-21 19:23:39 +02005267 si_applet_cant_put(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005268 return 0;
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01005269 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005270
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005271 px = appctx->ctx.stats.px;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005272 /* skip the disabled proxies, global frontend and non-networked ones */
5273 if (px->state != PR_STSTOPPED && px->uuid > 0 && (px->cap & (PR_CAP_FE | PR_CAP_BE)))
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01005274 if (stats_dump_proxy_to_buffer(si, px, uri) == 0)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005275 return 0;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005276
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005277 appctx->ctx.stats.px = px->next;
5278 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005279 }
5280 /* here, we just have reached the last proxy */
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005281
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005282 appctx->st2 = STAT_ST_END;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005283 /* fall through */
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005284
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005285 case STAT_ST_END:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005286 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01005287 stats_dump_html_end();
Willy Tarreaubc18da12015-03-13 14:00:47 +01005288 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02005289 si_applet_cant_put(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005290 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01005291 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005292 }
Willy Tarreau55058a72012-11-21 08:27:21 +01005293
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005294 appctx->st2 = STAT_ST_FIN;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005295 /* fall through */
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02005296
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005297 case STAT_ST_FIN:
5298 return 1;
Willy Tarreau55058a72012-11-21 08:27:21 +01005299
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005300 default:
5301 /* unknown state ! */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005302 appctx->st2 = STAT_ST_FIN;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005303 return -1;
5304 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005305}
Willy Tarreauae526782010-03-04 20:34:23 +01005306
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005307/* We reached the stats page through a POST request. The appctx is
5308 * expected to have already been allocated by the caller.
Willy Tarreau347a35d2013-11-22 17:51:09 +01005309 * Parse the posted data and enable/disable servers if necessary.
5310 * Returns 1 if request was parsed or zero if it needs more data.
5311 */
5312static int stats_process_http_post(struct stream_interface *si)
5313{
Willy Tarreau87b09662015-04-03 00:22:06 +02005314 struct stream *s = si_strm(si);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005315 struct appctx *appctx = objt_appctx(si->end);
Willy Tarreau347a35d2013-11-22 17:51:09 +01005316
5317 struct proxy *px = NULL;
5318 struct server *sv = NULL;
5319
5320 char key[LINESIZE];
5321 int action = ST_ADM_ACTION_NONE;
5322 int reprocess = 0;
5323
5324 int total_servers = 0;
5325 int altered_servers = 0;
5326
5327 char *first_param, *cur_param, *next_param, *end_params;
5328 char *st_cur_param = NULL;
5329 char *st_next_param = NULL;
5330
5331 struct chunk *temp;
5332 int reql;
5333
5334 temp = get_trash_chunk();
Willy Tarreaueee5b512015-04-03 23:46:31 +02005335 if (temp->size < s->txn->req.body_len) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01005336 /* too large request */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005337 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005338 goto out;
5339 }
5340
Willy Tarreaueee5b512015-04-03 23:46:31 +02005341 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 +01005342 if (reql <= 0) {
5343 /* we need more data */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005344 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005345 return 0;
5346 }
5347
5348 first_param = temp->str;
5349 end_params = temp->str + reql;
5350 cur_param = next_param = end_params;
5351 *end_params = '\0';
5352
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005353 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005354
5355 /*
5356 * Parse the parameters in reverse order to only store the last value.
5357 * From the html form, the backend and the action are at the end.
5358 */
5359 while (cur_param > first_param) {
5360 char *value;
5361 int poffset, plen;
5362
5363 cur_param--;
5364
5365 if ((*cur_param == '&') || (cur_param == first_param)) {
5366 reprocess_servers:
5367 /* Parse the key */
5368 poffset = (cur_param != first_param ? 1 : 0);
5369 plen = next_param - cur_param + (cur_param == first_param ? 1 : 0);
5370 if ((plen > 0) && (plen <= sizeof(key))) {
5371 strncpy(key, cur_param + poffset, plen);
5372 key[plen - 1] = '\0';
5373 } else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005374 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005375 goto out;
5376 }
5377
5378 /* Parse the value */
5379 value = key;
5380 while (*value != '\0' && *value != '=') {
5381 value++;
5382 }
5383 if (*value == '=') {
5384 /* Ok, a value is found, we can mark the end of the key */
5385 *value++ = '\0';
5386 }
5387 if (url_decode(key) < 0 || url_decode(value) < 0)
5388 break;
5389
5390 /* Now we can check the key to see what to do */
5391 if (!px && (strcmp(key, "b") == 0)) {
Willy Tarreau9e0bb102015-05-26 11:24:42 +02005392 if ((px = proxy_be_by_name(value)) == NULL) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01005393 /* the backend name is unknown or ambiguous (duplicate names) */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005394 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005395 goto out;
5396 }
5397 }
5398 else if (!action && (strcmp(key, "action") == 0)) {
Willy Tarreaued7df902014-05-22 18:04:49 +02005399 if (strcmp(value, "ready") == 0) {
5400 action = ST_ADM_ACTION_READY;
5401 }
5402 else if (strcmp(value, "drain") == 0) {
5403 action = ST_ADM_ACTION_DRAIN;
5404 }
5405 else if (strcmp(value, "maint") == 0) {
5406 action = ST_ADM_ACTION_MAINT;
5407 }
5408 else if (strcmp(value, "shutdown") == 0) {
5409 action = ST_ADM_ACTION_SHUTDOWN;
5410 }
Willy Tarreau248a60e2014-05-23 14:59:48 +02005411 else if (strcmp(value, "dhlth") == 0) {
5412 action = ST_ADM_ACTION_DHLTH;
5413 }
5414 else if (strcmp(value, "ehlth") == 0) {
5415 action = ST_ADM_ACTION_EHLTH;
5416 }
5417 else if (strcmp(value, "hrunn") == 0) {
5418 action = ST_ADM_ACTION_HRUNN;
5419 }
5420 else if (strcmp(value, "hnolb") == 0) {
5421 action = ST_ADM_ACTION_HNOLB;
5422 }
5423 else if (strcmp(value, "hdown") == 0) {
5424 action = ST_ADM_ACTION_HDOWN;
5425 }
5426 else if (strcmp(value, "dagent") == 0) {
5427 action = ST_ADM_ACTION_DAGENT;
5428 }
5429 else if (strcmp(value, "eagent") == 0) {
5430 action = ST_ADM_ACTION_EAGENT;
5431 }
5432 else if (strcmp(value, "arunn") == 0) {
5433 action = ST_ADM_ACTION_ARUNN;
5434 }
5435 else if (strcmp(value, "adown") == 0) {
5436 action = ST_ADM_ACTION_ADOWN;
5437 }
Willy Tarreaued7df902014-05-22 18:04:49 +02005438 /* else these are the old supported methods */
5439 else if (strcmp(value, "disable") == 0) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01005440 action = ST_ADM_ACTION_DISABLE;
5441 }
5442 else if (strcmp(value, "enable") == 0) {
5443 action = ST_ADM_ACTION_ENABLE;
5444 }
5445 else if (strcmp(value, "stop") == 0) {
5446 action = ST_ADM_ACTION_STOP;
5447 }
5448 else if (strcmp(value, "start") == 0) {
5449 action = ST_ADM_ACTION_START;
5450 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01005451 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005452 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005453 goto out;
5454 }
5455 }
5456 else if (strcmp(key, "s") == 0) {
5457 if (!(px && action)) {
5458 /*
5459 * Indicates that we'll need to reprocess the parameters
5460 * as soon as backend and action are known
5461 */
5462 if (!reprocess) {
5463 st_cur_param = cur_param;
5464 st_next_param = next_param;
5465 }
5466 reprocess = 1;
5467 }
5468 else if ((sv = findserver(px, value)) != NULL) {
5469 switch (action) {
5470 case ST_ADM_ACTION_DISABLE:
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02005471 if (!(sv->admin & SRV_ADMF_FMAINT)) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01005472 altered_servers++;
5473 total_servers++;
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02005474 srv_set_admin_flag(sv, SRV_ADMF_FMAINT);
Willy Tarreau347a35d2013-11-22 17:51:09 +01005475 }
5476 break;
5477 case ST_ADM_ACTION_ENABLE:
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02005478 if (sv->admin & SRV_ADMF_FMAINT) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01005479 altered_servers++;
5480 total_servers++;
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02005481 srv_clr_admin_flag(sv, SRV_ADMF_FMAINT);
Willy Tarreau347a35d2013-11-22 17:51:09 +01005482 }
5483 break;
5484 case ST_ADM_ACTION_STOP:
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02005485 if (!(sv->admin & SRV_ADMF_FDRAIN)) {
5486 srv_set_admin_flag(sv, SRV_ADMF_FDRAIN);
5487 altered_servers++;
5488 total_servers++;
5489 }
5490 break;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005491 case ST_ADM_ACTION_START:
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02005492 if (sv->admin & SRV_ADMF_FDRAIN) {
5493 srv_clr_admin_flag(sv, SRV_ADMF_FDRAIN);
5494 altered_servers++;
5495 total_servers++;
5496 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01005497 break;
Willy Tarreau248a60e2014-05-23 14:59:48 +02005498 case ST_ADM_ACTION_DHLTH:
5499 if (sv->check.state & CHK_ST_CONFIGURED) {
5500 sv->check.state &= ~CHK_ST_ENABLED;
5501 altered_servers++;
5502 total_servers++;
5503 }
5504 break;
5505 case ST_ADM_ACTION_EHLTH:
5506 if (sv->check.state & CHK_ST_CONFIGURED) {
5507 sv->check.state |= CHK_ST_ENABLED;
5508 altered_servers++;
5509 total_servers++;
5510 }
5511 break;
5512 case ST_ADM_ACTION_HRUNN:
5513 if (!(sv->track)) {
5514 sv->check.health = sv->check.rise + sv->check.fall - 1;
5515 srv_set_running(sv, "changed from Web interface");
5516 altered_servers++;
5517 total_servers++;
5518 }
5519 break;
5520 case ST_ADM_ACTION_HNOLB:
5521 if (!(sv->track)) {
5522 sv->check.health = sv->check.rise + sv->check.fall - 1;
5523 srv_set_stopping(sv, "changed from Web interface");
5524 altered_servers++;
5525 total_servers++;
5526 }
5527 break;
5528 case ST_ADM_ACTION_HDOWN:
5529 if (!(sv->track)) {
5530 sv->check.health = 0;
5531 srv_set_stopped(sv, "changed from Web interface");
5532 altered_servers++;
5533 total_servers++;
5534 }
5535 break;
5536 case ST_ADM_ACTION_DAGENT:
5537 if (sv->agent.state & CHK_ST_CONFIGURED) {
5538 sv->agent.state &= ~CHK_ST_ENABLED;
5539 altered_servers++;
5540 total_servers++;
5541 }
5542 break;
5543 case ST_ADM_ACTION_EAGENT:
5544 if (sv->agent.state & CHK_ST_CONFIGURED) {
5545 sv->agent.state |= CHK_ST_ENABLED;
5546 altered_servers++;
5547 total_servers++;
5548 }
5549 break;
5550 case ST_ADM_ACTION_ARUNN:
5551 if (sv->agent.state & CHK_ST_ENABLED) {
5552 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
5553 srv_set_running(sv, "changed from Web interface");
5554 altered_servers++;
5555 total_servers++;
5556 }
5557 break;
5558 case ST_ADM_ACTION_ADOWN:
5559 if (sv->agent.state & CHK_ST_ENABLED) {
5560 sv->agent.health = 0;
5561 srv_set_stopped(sv, "changed from Web interface");
5562 altered_servers++;
5563 total_servers++;
5564 }
5565 break;
Willy Tarreaued7df902014-05-22 18:04:49 +02005566 case ST_ADM_ACTION_READY:
5567 srv_adm_set_ready(sv);
5568 altered_servers++;
5569 total_servers++;
5570 break;
5571 case ST_ADM_ACTION_DRAIN:
5572 srv_adm_set_drain(sv);
5573 altered_servers++;
5574 total_servers++;
5575 break;
5576 case ST_ADM_ACTION_MAINT:
5577 srv_adm_set_maint(sv);
5578 altered_servers++;
5579 total_servers++;
5580 break;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005581 case ST_ADM_ACTION_SHUTDOWN:
5582 if (px->state != PR_STSTOPPED) {
Willy Tarreau87b09662015-04-03 00:22:06 +02005583 struct stream *sess, *sess_bck;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005584
5585 list_for_each_entry_safe(sess, sess_bck, &sv->actconns, by_srv)
5586 if (sess->srv_conn == sv)
Willy Tarreaue7dff022015-04-03 01:14:29 +02005587 stream_shutdown(sess, SF_ERR_KILLED);
Willy Tarreau347a35d2013-11-22 17:51:09 +01005588
5589 altered_servers++;
5590 total_servers++;
5591 }
5592 break;
5593 }
5594 } else {
5595 /* the server name is unknown or ambiguous (duplicate names) */
5596 total_servers++;
5597 }
5598 }
5599 if (reprocess && px && action) {
5600 /* Now, we know the backend and the action chosen by the user.
5601 * We can safely restart from the first server parameter
5602 * to reprocess them
5603 */
5604 cur_param = st_cur_param;
5605 next_param = st_next_param;
5606 reprocess = 0;
5607 goto reprocess_servers;
5608 }
5609
5610 next_param = cur_param;
5611 }
5612 }
5613
5614 if (total_servers == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005615 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005616 }
5617 else if (altered_servers == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005618 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005619 }
5620 else if (altered_servers == total_servers) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005621 appctx->ctx.stats.st_code = STAT_STATUS_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005622 }
5623 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005624 appctx->ctx.stats.st_code = STAT_STATUS_PART;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005625 }
5626 out:
5627 return 1;
5628}
5629
5630
5631static int stats_send_http_headers(struct stream_interface *si)
5632{
Willy Tarreau87b09662015-04-03 00:22:06 +02005633 struct stream *s = si_strm(si);
Willy Tarreau347a35d2013-11-22 17:51:09 +01005634 struct uri_auth *uri = s->be->uri_auth;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005635 struct appctx *appctx = objt_appctx(si->end);
Willy Tarreau347a35d2013-11-22 17:51:09 +01005636
5637 chunk_printf(&trash,
Willy Tarreau8b8995f2014-04-24 22:51:54 +02005638 "HTTP/1.1 200 OK\r\n"
Willy Tarreau347a35d2013-11-22 17:51:09 +01005639 "Cache-Control: no-cache\r\n"
5640 "Connection: close\r\n"
5641 "Content-Type: %s\r\n",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005642 (appctx->ctx.stats.flags & STAT_FMT_HTML) ? "text/html" : "text/plain");
Willy Tarreau347a35d2013-11-22 17:51:09 +01005643
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005644 if (uri->refresh > 0 && !(appctx->ctx.stats.flags & STAT_NO_REFRESH))
Willy Tarreau347a35d2013-11-22 17:51:09 +01005645 chunk_appendf(&trash, "Refresh: %d\r\n",
5646 uri->refresh);
5647
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005648 /* we don't send the CRLF in chunked mode, it will be sent with the first chunk's size */
5649
5650 if (appctx->ctx.stats.flags & STAT_CHUNKED)
5651 chunk_appendf(&trash, "Transfer-Encoding: chunked\r\n");
5652 else
5653 chunk_appendf(&trash, "\r\n");
Willy Tarreau347a35d2013-11-22 17:51:09 +01005654
Willy Tarreaueee5b512015-04-03 23:46:31 +02005655 s->txn->status = 200;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005656 s->logs.tv_request = now;
5657
Willy Tarreaubc18da12015-03-13 14:00:47 +01005658 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02005659 si_applet_cant_put(si);
Willy Tarreau347a35d2013-11-22 17:51:09 +01005660 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01005661 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01005662
5663 return 1;
5664}
5665
5666static int stats_send_http_redirect(struct stream_interface *si)
5667{
5668 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
Willy Tarreau87b09662015-04-03 00:22:06 +02005669 struct stream *s = si_strm(si);
Willy Tarreau347a35d2013-11-22 17:51:09 +01005670 struct uri_auth *uri = s->be->uri_auth;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005671 struct appctx *appctx = objt_appctx(si->end);
Willy Tarreau347a35d2013-11-22 17:51:09 +01005672
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005673 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Willy Tarreau347a35d2013-11-22 17:51:09 +01005674 scope_txt[0] = 0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005675 if (appctx->ctx.stats.scope_len) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01005676 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau4e4292b2014-11-28 12:18:45 +01005677 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 +01005678 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005679 }
5680
5681 /* We don't want to land on the posted stats page because a refresh will
5682 * repost the data. We don't want this to happen on accident so we redirect
5683 * the browse to the stats page with a GET.
5684 */
5685 chunk_printf(&trash,
5686 "HTTP/1.1 303 See Other\r\n"
5687 "Cache-Control: no-cache\r\n"
5688 "Content-Type: text/plain\r\n"
5689 "Connection: close\r\n"
5690 "Location: %s;st=%s%s%s%s\r\n"
Willy Tarreaufdfcc9d2016-01-26 13:57:29 +01005691 "Content-length: 0\r\n"
Willy Tarreau347a35d2013-11-22 17:51:09 +01005692 "\r\n",
5693 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005694 ((appctx->ctx.stats.st_code > STAT_STATUS_INIT) &&
5695 (appctx->ctx.stats.st_code < STAT_STATUS_SIZE) &&
5696 stat_status_codes[appctx->ctx.stats.st_code]) ?
5697 stat_status_codes[appctx->ctx.stats.st_code] :
Willy Tarreau347a35d2013-11-22 17:51:09 +01005698 stat_status_codes[STAT_STATUS_UNKN],
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005699 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
5700 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreau347a35d2013-11-22 17:51:09 +01005701 scope_txt);
5702
Willy Tarreaueee5b512015-04-03 23:46:31 +02005703 s->txn->status = 303;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005704 s->logs.tv_request = now;
5705
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 Tarreau347a35d2013-11-22 17:51:09 +01005708 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01005709 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01005710
5711 return 1;
5712}
5713
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005714/* This I/O handler runs as an applet embedded in a stream interface. It is
5715 * used to send HTTP stats over a TCP socket. The mechanism is very simple.
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005716 * appctx->st0 contains the operation in progress (dump, done). The handler
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005717 * automatically unregisters itself once transfer is complete.
5718 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02005719static void http_stats_io_handler(struct appctx *appctx)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005720{
Willy Tarreau00a37f02015-04-13 12:05:19 +02005721 struct stream_interface *si = appctx->owner;
Willy Tarreau87b09662015-04-03 00:22:06 +02005722 struct stream *s = si_strm(si);
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005723 struct channel *req = si_oc(si);
5724 struct channel *res = si_ic(si);
Willy Tarreau55058a72012-11-21 08:27:21 +01005725
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005726 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
5727 goto out;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01005728
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005729 /* check that the output is not closed */
5730 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005731 appctx->st0 = STAT_HTTP_DONE;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005732
Willy Tarreau347a35d2013-11-22 17:51:09 +01005733 /* all states are processed in sequence */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005734 if (appctx->st0 == STAT_HTTP_HEAD) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01005735 if (stats_send_http_headers(si)) {
Willy Tarreaueee5b512015-04-03 23:46:31 +02005736 if (s->txn->meth == HTTP_METH_HEAD)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005737 appctx->st0 = STAT_HTTP_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005738 else
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005739 appctx->st0 = STAT_HTTP_DUMP;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005740 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01005741 }
5742
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005743 if (appctx->st0 == STAT_HTTP_DUMP) {
Willy Tarreau4e4292b2014-11-28 12:18:45 +01005744 unsigned int prev_len = si_ib(si)->i;
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005745 unsigned int data_len;
5746 unsigned int last_len;
Willy Tarreaucce36482014-04-24 20:26:41 +02005747 unsigned int last_fwd = 0;
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005748
5749 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
5750 /* One difficulty we're facing is that we must prevent
5751 * the input data from being automatically forwarded to
5752 * the output area. For this, we temporarily disable
5753 * forwarding on the channel.
5754 */
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005755 last_fwd = si_ic(si)->to_forward;
5756 si_ic(si)->to_forward = 0;
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005757 chunk_printf(&trash, "\r\n000000\r\n");
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005758 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02005759 si_applet_cant_put(si);
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005760 si_ic(si)->to_forward = last_fwd;
Willy Tarreau828824a2015-04-19 17:20:03 +02005761 goto out;
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005762 }
5763 }
5764
Willy Tarreau4e4292b2014-11-28 12:18:45 +01005765 data_len = si_ib(si)->i;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005766 if (stats_dump_stat_to_buffer(si, s->be->uri_auth))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005767 appctx->st0 = STAT_HTTP_DONE;
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005768
Willy Tarreau4e4292b2014-11-28 12:18:45 +01005769 last_len = si_ib(si)->i;
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005770
5771 /* Now we must either adjust or remove the chunk size. This is
5772 * not easy because the chunk size might wrap at the end of the
5773 * buffer, so we pretend we have nothing in the buffer, we write
5774 * the size, then restore the buffer's contents. Note that we can
5775 * only do that because no forwarding is scheduled on the stats
5776 * applet.
5777 */
5778 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
Willy Tarreau4e4292b2014-11-28 12:18:45 +01005779 si_ic(si)->total -= (last_len - prev_len);
5780 si_ib(si)->i -= (last_len - prev_len);
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005781
5782 if (last_len != data_len) {
5783 chunk_printf(&trash, "\r\n%06x\r\n", (last_len - data_len));
Willy Tarreaubc18da12015-03-13 14:00:47 +01005784 if (bi_putchk(si_ic(si), &trash) == -1)
Willy Tarreaufe127932015-04-21 19:23:39 +02005785 si_applet_cant_put(si);
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005786
Willy Tarreau4e4292b2014-11-28 12:18:45 +01005787 si_ic(si)->total += (last_len - data_len);
5788 si_ib(si)->i += (last_len - data_len);
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005789 }
5790 /* now re-enable forwarding */
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005791 channel_forward(si_ic(si), last_fwd);
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005792 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005793 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02005794
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005795 if (appctx->st0 == STAT_HTTP_POST) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01005796 if (stats_process_http_post(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005797 appctx->st0 = STAT_HTTP_LAST;
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005798 else if (si_oc(si)->flags & CF_SHUTR)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005799 appctx->st0 = STAT_HTTP_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005800 }
5801
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005802 if (appctx->st0 == STAT_HTTP_LAST) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01005803 if (stats_send_http_redirect(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005804 appctx->st0 = STAT_HTTP_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005805 }
5806
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005807 if (appctx->st0 == STAT_HTTP_DONE) {
5808 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
5809 chunk_printf(&trash, "\r\n0\r\n\r\n");
Willy Tarreaubc18da12015-03-13 14:00:47 +01005810 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02005811 si_applet_cant_put(si);
Willy Tarreau828824a2015-04-19 17:20:03 +02005812 goto out;
Willy Tarreaubc18da12015-03-13 14:00:47 +01005813 }
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005814 }
5815 /* eat the whole request */
Willy Tarreau4e4292b2014-11-28 12:18:45 +01005816 bo_skip(si_oc(si), si_ob(si)->o);
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005817 res->flags |= CF_READ_NULL;
5818 si_shutr(si);
5819 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01005820
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005821 if ((res->flags & CF_SHUTR) && (si->state == SI_ST_EST))
5822 si_shutw(si);
Willy Tarreau91861262007-10-17 17:06:05 +02005823
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005824 if (appctx->st0 == STAT_HTTP_DONE) {
Willy Tarreau96d44912013-11-22 12:25:24 +01005825 if ((req->flags & CF_SHUTW) && (si->state == SI_ST_EST)) {
5826 si_shutr(si);
5827 res->flags |= CF_READ_NULL;
5828 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005829 }
Willy Tarreau828824a2015-04-19 17:20:03 +02005830 out:
Willy Tarreaud4da1962015-04-20 01:31:23 +02005831 /* just to make gcc happy */ ;
Willy Tarreau91861262007-10-17 17:06:05 +02005832}
5833
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005834
Willy Tarreau909d5172012-11-26 03:04:41 +01005835static inline const char *get_conn_ctrl_name(const struct connection *conn)
5836{
Willy Tarreau3c728722014-01-23 13:50:42 +01005837 if (!conn_ctrl_ready(conn))
Willy Tarreau909d5172012-11-26 03:04:41 +01005838 return "NONE";
5839 return conn->ctrl->name;
5840}
5841
5842static inline const char *get_conn_xprt_name(const struct connection *conn)
5843{
5844 static char ptr[17];
5845
Willy Tarreauaad69382014-01-23 14:21:42 +01005846 if (!conn_xprt_ready(conn))
Willy Tarreau909d5172012-11-26 03:04:41 +01005847 return "NONE";
5848
5849 if (conn->xprt == &raw_sock)
5850 return "RAW";
5851
5852#ifdef USE_OPENSSL
5853 if (conn->xprt == &ssl_sock)
5854 return "SSL";
5855#endif
5856 snprintf(ptr, sizeof(ptr), "%p", conn->xprt);
5857 return ptr;
5858}
5859
5860static inline const char *get_conn_data_name(const struct connection *conn)
5861{
5862 static char ptr[17];
5863
5864 if (!conn->data)
5865 return "NONE";
5866
5867 if (conn->data == &sess_conn_cb)
5868 return "SESS";
5869
5870 if (conn->data == &si_conn_cb)
5871 return "STRM";
5872
5873 if (conn->data == &check_conn_cb)
5874 return "CHCK";
5875
5876 snprintf(ptr, sizeof(ptr), "%p", conn->data);
5877 return ptr;
5878}
5879
Willy Tarreau87b09662015-04-03 00:22:06 +02005880/* This function dumps a complete stream state onto the stream interface's
5881 * read buffer. The stream has to be set in sess->target. It returns
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01005882 * 0 if the output buffer is full and it needs to be called again, otherwise
5883 * non-zero. It is designed to be called from stats_dump_sess_to_buffer() below.
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005884 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005885static int stats_dump_full_sess_to_buffer(struct stream_interface *si, struct stream *sess)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005886{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005887 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005888 struct tm tm;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005889 extern const char *monthname[12];
5890 char pn[INET6_ADDRSTRLEN];
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005891 struct connection *conn;
Willy Tarreau284ddbf2013-12-01 20:45:00 +01005892 struct appctx *tmpctx;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005893
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005894 chunk_reset(&trash);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005895
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005896 if (appctx->ctx.sess.section > 0 && appctx->ctx.sess.uid != sess->uniq_id) {
Willy Tarreau87b09662015-04-03 00:22:06 +02005897 /* stream changed, no need to go any further */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005898 chunk_appendf(&trash, " *** session terminated while we were watching it ***\n");
Willy Tarreaubc18da12015-03-13 14:00:47 +01005899 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02005900 si_applet_cant_put(si);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005901 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01005902 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005903 appctx->ctx.sess.uid = 0;
5904 appctx->ctx.sess.section = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005905 return 1;
5906 }
5907
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005908 switch (appctx->ctx.sess.section) {
Willy Tarreau87b09662015-04-03 00:22:06 +02005909 case 0: /* main status of the stream */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005910 appctx->ctx.sess.uid = sess->uniq_id;
5911 appctx->ctx.sess.section = 1;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005912 /* fall through */
5913
5914 case 1:
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005915 get_localtime(sess->logs.accept_date.tv_sec, &tm);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005916 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005917 "%p: [%02d/%s/%04d:%02d:%02d:%02d.%06d] id=%u proto=%s",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005918 sess,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005919 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
5920 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(sess->logs.accept_date.tv_usec),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005921 sess->uniq_id,
Willy Tarreau666f5042015-06-17 19:49:52 +02005922 strm_li(sess) ? strm_li(sess)->proto->name : "?");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005923
Willy Tarreaud0d8da92015-04-04 02:10:38 +02005924 conn = objt_conn(strm_orig(sess));
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005925 switch (conn ? addr_to_str(&conn->addr.from, pn, sizeof(pn)) : AF_UNSPEC) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005926 case AF_INET:
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005927 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005928 chunk_appendf(&trash, " source=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005929 pn, get_host_port(&conn->addr.from));
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005930 break;
5931 case AF_UNIX:
Willy Tarreaud0d8da92015-04-04 02:10:38 +02005932 chunk_appendf(&trash, " source=unix:%d\n", strm_li(sess)->luid);
Emeric Brun837ca522010-10-22 16:19:01 +02005933 break;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005934 default:
5935 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005936 chunk_appendf(&trash, "\n");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005937 break;
5938 }
5939
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005940 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005941 " flags=0x%x, conn_retries=%d, srv_conn=%p, pend_pos=%p\n",
Willy Tarreauee28de02010-06-01 09:51:00 +02005942 sess->flags, sess->si[1].conn_retries, sess->srv_conn, sess->pend_pos);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005943
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005944 chunk_appendf(&trash,
Daniel Schultze90690c72012-03-23 10:53:36 -07005945 " frontend=%s (id=%u mode=%s), listener=%s (id=%u)",
Willy Tarreaud0d8da92015-04-04 02:10:38 +02005946 strm_fe(sess)->id, strm_fe(sess)->uuid, strm_fe(sess)->mode ? "http" : "tcp",
5947 strm_li(sess) ? strm_li(sess)->name ? strm_li(sess)->name : "?" : "?",
5948 strm_li(sess) ? strm_li(sess)->luid : 0);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005949
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005950 if (conn)
5951 conn_get_to_addr(conn);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005952
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005953 switch (conn ? addr_to_str(&conn->addr.to, pn, sizeof(pn)) : AF_UNSPEC) {
Daniel Schultze90690c72012-03-23 10:53:36 -07005954 case AF_INET:
5955 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005956 chunk_appendf(&trash, " addr=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005957 pn, get_host_port(&conn->addr.to));
Daniel Schultze90690c72012-03-23 10:53:36 -07005958 break;
5959 case AF_UNIX:
Willy Tarreaud0d8da92015-04-04 02:10:38 +02005960 chunk_appendf(&trash, " addr=unix:%d\n", strm_li(sess)->luid);
Daniel Schultze90690c72012-03-23 10:53:36 -07005961 break;
5962 default:
5963 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005964 chunk_appendf(&trash, "\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07005965 break;
5966 }
5967
Willy Tarreau50943332011-09-02 17:33:05 +02005968 if (sess->be->cap & PR_CAP_BE)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005969 chunk_appendf(&trash,
Daniel Schultze90690c72012-03-23 10:53:36 -07005970 " backend=%s (id=%u mode=%s)",
Willy Tarreau50943332011-09-02 17:33:05 +02005971 sess->be->id,
Daniel Schultze90690c72012-03-23 10:53:36 -07005972 sess->be->uuid, sess->be->mode ? "http" : "tcp");
5973 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005974 chunk_appendf(&trash, " backend=<NONE> (id=-1 mode=-)");
Daniel Schultze90690c72012-03-23 10:53:36 -07005975
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005976 conn = objt_conn(sess->si[1].end);
5977 if (conn)
5978 conn_get_from_addr(conn);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005979
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005980 switch (conn ? addr_to_str(&conn->addr.from, pn, sizeof(pn)) : AF_UNSPEC) {
Daniel Schultze90690c72012-03-23 10:53:36 -07005981 case AF_INET:
5982 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005983 chunk_appendf(&trash, " addr=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005984 pn, get_host_port(&conn->addr.from));
Daniel Schultze90690c72012-03-23 10:53:36 -07005985 break;
5986 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005987 chunk_appendf(&trash, " addr=unix\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07005988 break;
5989 default:
5990 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005991 chunk_appendf(&trash, "\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07005992 break;
5993 }
5994
5995 if (sess->be->cap & PR_CAP_BE)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005996 chunk_appendf(&trash,
Daniel Schultze90690c72012-03-23 10:53:36 -07005997 " server=%s (id=%u)",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005998 objt_server(sess->target) ? objt_server(sess->target)->id : "<none>",
5999 objt_server(sess->target) ? objt_server(sess->target)->puid : 0);
Willy Tarreau50943332011-09-02 17:33:05 +02006000 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006001 chunk_appendf(&trash, " server=<NONE> (id=-1)");
Daniel Schultze90690c72012-03-23 10:53:36 -07006002
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006003 if (conn)
6004 conn_get_to_addr(conn);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02006005
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006006 switch (conn ? addr_to_str(&conn->addr.to, pn, sizeof(pn)) : AF_UNSPEC) {
Daniel Schultze90690c72012-03-23 10:53:36 -07006007 case AF_INET:
6008 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006009 chunk_appendf(&trash, " addr=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006010 pn, get_host_port(&conn->addr.to));
Daniel Schultze90690c72012-03-23 10:53:36 -07006011 break;
6012 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006013 chunk_appendf(&trash, " addr=unix\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07006014 break;
6015 default:
6016 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006017 chunk_appendf(&trash, "\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07006018 break;
6019 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006020
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006021 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01006022 " task=%p (state=0x%02x nice=%d calls=%d exp=%s%s",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006023 sess->task,
6024 sess->task->state,
6025 sess->task->nice, sess->task->calls,
6026 sess->task->expire ?
6027 tick_is_expired(sess->task->expire, now_ms) ? "<PAST>" :
6028 human_time(TICKS_TO_MS(sess->task->expire - now_ms),
6029 TICKS_TO_MS(1000)) : "<NEVER>",
6030 task_in_rq(sess->task) ? ", running" : "");
6031
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006032 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01006033 " age=%s)\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006034 human_time(now.tv_sec - sess->logs.accept_date.tv_sec, 1));
6035
Willy Tarreaueee5b512015-04-03 23:46:31 +02006036 if (sess->txn)
6037 chunk_appendf(&trash,
Willy Tarreau98410192014-11-26 18:05:38 +01006038 " 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 +02006039 sess->txn, sess->txn->flags, sess->txn->meth, sess->txn->status,
6040 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 +01006041
6042 chunk_appendf(&trash,
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02006043 " si[0]=%p (state=%s flags=0x%02x endp0=%s:%p exp=%s, et=0x%03x)\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006044 &sess->si[0],
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01006045 si_state_str(sess->si[0].state),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006046 sess->si[0].flags,
Willy Tarreaucf644ed2013-09-29 17:19:56 +02006047 obj_type_name(sess->si[0].end),
6048 obj_base_ptr(sess->si[0].end),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006049 sess->si[0].exp ?
6050 tick_is_expired(sess->si[0].exp, now_ms) ? "<PAST>" :
6051 human_time(TICKS_TO_MS(sess->si[0].exp - now_ms),
6052 TICKS_TO_MS(1000)) : "<NEVER>",
6053 sess->si[0].err_type);
6054
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006055 chunk_appendf(&trash,
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02006056 " si[1]=%p (state=%s flags=0x%02x endp1=%s:%p exp=%s, et=0x%03x)\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006057 &sess->si[1],
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01006058 si_state_str(sess->si[1].state),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006059 sess->si[1].flags,
Willy Tarreaucf644ed2013-09-29 17:19:56 +02006060 obj_type_name(sess->si[1].end),
6061 obj_base_ptr(sess->si[1].end),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006062 sess->si[1].exp ?
6063 tick_is_expired(sess->si[1].exp, now_ms) ? "<PAST>" :
6064 human_time(TICKS_TO_MS(sess->si[1].exp - now_ms),
6065 TICKS_TO_MS(1000)) : "<NEVER>",
6066 sess->si[1].err_type);
6067
Willy Tarreau284ddbf2013-12-01 20:45:00 +01006068 if ((conn = objt_conn(sess->si[0].end)) != NULL) {
Willy Tarreaucf644ed2013-09-29 17:19:56 +02006069 chunk_appendf(&trash,
6070 " co0=%p ctrl=%s xprt=%s data=%s target=%s:%p\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006071 conn,
6072 get_conn_ctrl_name(conn),
6073 get_conn_xprt_name(conn),
6074 get_conn_data_name(conn),
6075 obj_type_name(conn->target),
6076 obj_base_ptr(conn->target));
Willy Tarreau909d5172012-11-26 03:04:41 +01006077
Willy Tarreaucf644ed2013-09-29 17:19:56 +02006078 chunk_appendf(&trash,
Willy Tarreau16f649c2014-01-25 19:10:48 +01006079 " flags=0x%08x fd=%d fd.state=%02x fd.cache=%d updt=%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006080 conn->flags,
6081 conn->t.sock.fd,
Willy Tarreau69a41fa2014-01-20 11:02:59 +01006082 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].state : 0,
Willy Tarreau15a4dec2014-01-20 11:09:39 +01006083 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].cache : 0,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006084 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].updated : 0);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02006085 }
Willy Tarreau284ddbf2013-12-01 20:45:00 +01006086 else if ((tmpctx = objt_appctx(sess->si[0].end)) != NULL) {
6087 chunk_appendf(&trash,
6088 " app0=%p st0=%d st1=%d st2=%d applet=%s\n",
6089 tmpctx,
6090 tmpctx->st0,
6091 tmpctx->st1,
6092 tmpctx->st2,
6093 tmpctx->applet->name);
6094 }
Willy Tarreaubc174aa2012-11-19 16:10:32 +01006095
Willy Tarreau284ddbf2013-12-01 20:45:00 +01006096 if ((conn = objt_conn(sess->si[1].end)) != NULL) {
Willy Tarreaucf644ed2013-09-29 17:19:56 +02006097 chunk_appendf(&trash,
6098 " co1=%p ctrl=%s xprt=%s data=%s target=%s:%p\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006099 conn,
6100 get_conn_ctrl_name(conn),
6101 get_conn_xprt_name(conn),
6102 get_conn_data_name(conn),
6103 obj_type_name(conn->target),
6104 obj_base_ptr(conn->target));
Willy Tarreau909d5172012-11-26 03:04:41 +01006105
Willy Tarreaucf644ed2013-09-29 17:19:56 +02006106 chunk_appendf(&trash,
Willy Tarreauceeafb52016-01-25 15:27:17 +01006107 " flags=0x%08x fd=%d fd.state=%02x fd.cache=%d updt=%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006108 conn->flags,
6109 conn->t.sock.fd,
Willy Tarreau69a41fa2014-01-20 11:02:59 +01006110 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].state : 0,
Willy Tarreau15a4dec2014-01-20 11:09:39 +01006111 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].cache : 0,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006112 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].updated : 0);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02006113 }
Willy Tarreau284ddbf2013-12-01 20:45:00 +01006114 else if ((tmpctx = objt_appctx(sess->si[1].end)) != NULL) {
6115 chunk_appendf(&trash,
6116 " app1=%p st0=%d st1=%d st2=%d applet=%s\n",
6117 tmpctx,
6118 tmpctx->st0,
6119 tmpctx->st1,
6120 tmpctx->st2,
6121 tmpctx->applet->name);
6122 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006123
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006124 chunk_appendf(&trash,
Willy Tarreau909d5172012-11-26 03:04:41 +01006125 " req=%p (f=0x%06x an=0x%x pipe=%d tofwd=%d total=%lld)\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006126 " an_exp=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006127 &sess->req,
6128 sess->req.flags, sess->req.analysers,
6129 sess->req.pipe ? sess->req.pipe->data : 0,
6130 sess->req.to_forward, sess->req.total,
6131 sess->req.analyse_exp ?
6132 human_time(TICKS_TO_MS(sess->req.analyse_exp - now_ms),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006133 TICKS_TO_MS(1000)) : "<NEVER>");
6134
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006135 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006136 " rex=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006137 sess->req.rex ?
6138 human_time(TICKS_TO_MS(sess->req.rex - now_ms),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006139 TICKS_TO_MS(1000)) : "<NEVER>");
6140
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006141 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006142 " wex=%s\n"
Willy Tarreau909d5172012-11-26 03:04:41 +01006143 " buf=%p data=%p o=%d p=%d req.next=%d i=%d size=%d\n",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006144 sess->req.wex ?
6145 human_time(TICKS_TO_MS(sess->req.wex - now_ms),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006146 TICKS_TO_MS(1000)) : "<NEVER>",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006147 sess->req.buf,
6148 sess->req.buf->data, sess->req.buf->o,
6149 (int)(sess->req.buf->p - sess->req.buf->data),
Willy Tarreaueee5b512015-04-03 23:46:31 +02006150 sess->txn ? sess->txn->req.next : 0, sess->req.buf->i,
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006151 sess->req.buf->size);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006152
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006153 chunk_appendf(&trash,
Willy Tarreau909d5172012-11-26 03:04:41 +01006154 " res=%p (f=0x%06x an=0x%x pipe=%d tofwd=%d total=%lld)\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006155 " an_exp=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006156 &sess->res,
6157 sess->res.flags, sess->res.analysers,
6158 sess->res.pipe ? sess->res.pipe->data : 0,
6159 sess->res.to_forward, sess->res.total,
6160 sess->res.analyse_exp ?
6161 human_time(TICKS_TO_MS(sess->res.analyse_exp - now_ms),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006162 TICKS_TO_MS(1000)) : "<NEVER>");
6163
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006164 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006165 " rex=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006166 sess->res.rex ?
6167 human_time(TICKS_TO_MS(sess->res.rex - now_ms),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006168 TICKS_TO_MS(1000)) : "<NEVER>");
6169
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006170 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006171 " wex=%s\n"
Willy Tarreau909d5172012-11-26 03:04:41 +01006172 " buf=%p data=%p o=%d p=%d rsp.next=%d i=%d size=%d\n",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006173 sess->res.wex ?
6174 human_time(TICKS_TO_MS(sess->res.wex - now_ms),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006175 TICKS_TO_MS(1000)) : "<NEVER>",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006176 sess->res.buf,
6177 sess->res.buf->data, sess->res.buf->o,
6178 (int)(sess->res.buf->p - sess->res.buf->data),
Willy Tarreaueee5b512015-04-03 23:46:31 +02006179 sess->txn ? sess->txn->rsp.next : 0, sess->res.buf->i,
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006180 sess->res.buf->size);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006181
Willy Tarreaubc18da12015-03-13 14:00:47 +01006182 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02006183 si_applet_cant_put(si);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006184 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01006185 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006186
6187 /* use other states to dump the contents */
6188 }
6189 /* end of dump */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006190 appctx->ctx.sess.uid = 0;
6191 appctx->ctx.sess.section = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006192 return 1;
6193}
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006194
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02006195#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
6196static int stats_tlskeys_list(struct stream_interface *si) {
6197 struct appctx *appctx = __objt_appctx(si->end);
6198
6199 switch (appctx->st2) {
6200 case STAT_ST_INIT:
6201 /* Display the column headers. If the message cannot be sent,
6202 * quit the fucntion with returning 0. The function is called
6203 * later and restart at the state "STAT_ST_INIT".
6204 */
6205 chunk_reset(&trash);
William Lallemand1d0b36a2016-05-20 17:40:26 +02006206
6207 if (appctx->st0 == STAT_CLI_O_TLSK_ENT)
6208 chunk_appendf(&trash, "# id secret\n");
6209 else
6210 chunk_appendf(&trash, "# id (file)\n");
6211
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02006212 if (bi_putchk(si_ic(si), &trash) == -1) {
6213 si_applet_cant_put(si);
6214 return 0;
6215 }
6216
William Lallemand72a8a182016-06-14 18:58:55 +02006217 appctx->ctx.tlskeys.dump_keys_index = 0;
William Lallemand1d0b36a2016-05-20 17:40:26 +02006218
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02006219 /* Now, we start the browsing of the references lists.
6220 * Note that the following call to LIST_ELEM return bad pointer. The only
Cyril Bontédb98eb32016-05-06 12:18:50 +02006221 * available field of this pointer is <list>. It is used with the function
6222 * tlskeys_list_get_next() for retruning the first available entry
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02006223 */
William Lallemandcf9e7882016-06-14 17:45:18 +02006224 if (appctx->ctx.tlskeys.ref == NULL) {
6225 appctx->ctx.tlskeys.ref = LIST_ELEM(&tlskeys_reference, struct tls_keys_ref *, list);
6226 appctx->ctx.tlskeys.ref = tlskeys_list_get_next(appctx->ctx.tlskeys.ref, &tlskeys_reference);
William Lallemand1d0b36a2016-05-20 17:40:26 +02006227 }
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02006228
6229 appctx->st2 = STAT_ST_LIST;
6230 /* fall through */
6231
6232 case STAT_ST_LIST:
William Lallemandcf9e7882016-06-14 17:45:18 +02006233 while (appctx->ctx.tlskeys.ref) {
William Lallemandcf9e7882016-06-14 17:45:18 +02006234 int head = appctx->ctx.tlskeys.ref->tls_ticket_enc_index;
William Lallemand1d0b36a2016-05-20 17:40:26 +02006235
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02006236 chunk_reset(&trash);
William Lallemand72a8a182016-06-14 18:58:55 +02006237 if (appctx->st0 == STAT_CLI_O_TLSK_ENT && appctx->ctx.tlskeys.dump_keys_index == 0)
William Lallemand1d0b36a2016-05-20 17:40:26 +02006238 chunk_appendf(&trash, "# ");
William Lallemand72a8a182016-06-14 18:58:55 +02006239 if (appctx->ctx.tlskeys.dump_keys_index == 0)
6240 chunk_appendf(&trash, "%d (%s)\n", appctx->ctx.tlskeys.ref->unique_id,
6241 appctx->ctx.tlskeys.ref->filename);
William Lallemand1d0b36a2016-05-20 17:40:26 +02006242 if (appctx->st0 == STAT_CLI_O_TLSK_ENT) {
William Lallemand72a8a182016-06-14 18:58:55 +02006243 while (appctx->ctx.tlskeys.dump_keys_index < TLS_TICKETS_NO) {
William Lallemand1d0b36a2016-05-20 17:40:26 +02006244 struct chunk *t2 = get_trash_chunk();
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02006245
William Lallemand1d0b36a2016-05-20 17:40:26 +02006246 chunk_reset(t2);
William Lallemand72a8a182016-06-14 18:58:55 +02006247 /* should never fail here because we dump only a key in the t2 buffer */
6248 t2->len = a2base64((char *)(appctx->ctx.tlskeys.ref->tlskeys + (head + 2 + appctx->ctx.tlskeys.dump_keys_index) % TLS_TICKETS_NO),
William Lallemand1d0b36a2016-05-20 17:40:26 +02006249 sizeof(struct tls_sess_key), t2->str, t2->size);
William Lallemand72a8a182016-06-14 18:58:55 +02006250 chunk_appendf(&trash, "%d.%d %s\n", appctx->ctx.tlskeys.ref->unique_id, appctx->ctx.tlskeys.dump_keys_index, t2->str);
6251
6252 if (bi_putchk(si_ic(si), &trash) == -1) {
6253 /* let's try again later from this stream. We add ourselves into
6254 * this stream's users so that it can remove us upon termination.
6255 */
6256 si_applet_cant_put(si);
William Lallemand1d0b36a2016-05-20 17:40:26 +02006257 return 0;
William Lallemand72a8a182016-06-14 18:58:55 +02006258 }
6259 appctx->ctx.tlskeys.dump_keys_index++;
William Lallemand1d0b36a2016-05-20 17:40:26 +02006260 }
William Lallemand72a8a182016-06-14 18:58:55 +02006261 appctx->ctx.tlskeys.dump_keys_index = 0;
William Lallemand1d0b36a2016-05-20 17:40:26 +02006262 }
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02006263 if (bi_putchk(si_ic(si), &trash) == -1) {
6264 /* let's try again later from this stream. We add ourselves into
6265 * this stream's users so that it can remove us upon termination.
6266 */
6267 si_applet_cant_put(si);
6268 return 0;
6269 }
6270
William Lallemandcf9e7882016-06-14 17:45:18 +02006271 if (appctx->ctx.tlskeys.dump_all == 0) /* don't display everything if not necessary */
William Lallemand1d0b36a2016-05-20 17:40:26 +02006272 break;
6273
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02006274 /* get next list entry and check the end of the list */
William Lallemandcf9e7882016-06-14 17:45:18 +02006275 appctx->ctx.tlskeys.ref = tlskeys_list_get_next(appctx->ctx.tlskeys.ref, &tlskeys_reference);
William Lallemand1d0b36a2016-05-20 17:40:26 +02006276
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02006277 }
6278
6279 appctx->st2 = STAT_ST_FIN;
6280 /* fall through */
6281
6282 default:
6283 appctx->st2 = STAT_ST_FIN;
6284 return 1;
6285 }
6286 return 0;
6287}
6288#endif
6289
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006290static int stats_pats_list(struct stream_interface *si)
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006291{
6292 struct appctx *appctx = __objt_appctx(si->end);
6293
6294 switch (appctx->st2) {
6295 case STAT_ST_INIT:
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01006296 /* Display the column headers. If the message cannot be sent,
6297 * quit the fucntion with returning 0. The function is called
6298 * later and restart at the state "STAT_ST_INIT".
6299 */
6300 chunk_reset(&trash);
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01006301 chunk_appendf(&trash, "# id (file) description\n");
Willy Tarreaubc18da12015-03-13 14:00:47 +01006302 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02006303 si_applet_cant_put(si);
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01006304 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01006305 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006306
6307 /* Now, we start the browsing of the references lists.
6308 * Note that the following call to LIST_ELEM return bad pointer. The only
Cyril Bontédb98eb32016-05-06 12:18:50 +02006309 * available field of this pointer is <list>. It is used with the function
6310 * pat_list_get_next() for retruning the first available entry
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006311 */
6312 appctx->ctx.map.ref = LIST_ELEM(&pattern_reference, struct pat_ref *, list);
6313 appctx->ctx.map.ref = pat_list_get_next(appctx->ctx.map.ref, &pattern_reference,
6314 appctx->ctx.map.display_flags);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006315 appctx->st2 = STAT_ST_LIST;
6316 /* fall through */
6317
6318 case STAT_ST_LIST:
6319 while (appctx->ctx.map.ref) {
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006320 chunk_reset(&trash);
6321
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006322 /* Build messages. If the reference is used by another category than
6323 * the listed categorie, display the information in the massage.
6324 */
Thierry FOURNIERf7e04e92014-03-20 11:45:47 +01006325 chunk_appendf(&trash, "%d (%s) %s\n", appctx->ctx.map.ref->unique_id,
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01006326 appctx->ctx.map.ref->reference ? appctx->ctx.map.ref->reference : "",
6327 appctx->ctx.map.ref->display);
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01006328
Willy Tarreau2bb4a962014-11-28 11:11:05 +01006329 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau87b09662015-04-03 00:22:06 +02006330 /* let's try again later from this stream. We add ourselves into
6331 * this stream's users so that it can remove us upon termination.
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006332 */
Willy Tarreaufe127932015-04-21 19:23:39 +02006333 si_applet_cant_put(si);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006334 return 0;
6335 }
6336
6337 /* get next list entry and check the end of the list */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006338 appctx->ctx.map.ref = pat_list_get_next(appctx->ctx.map.ref, &pattern_reference,
6339 appctx->ctx.map.display_flags);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006340 }
6341
6342 appctx->st2 = STAT_ST_FIN;
6343 /* fall through */
6344
6345 default:
6346 appctx->st2 = STAT_ST_FIN;
6347 return 1;
6348 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006349 return 0;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006350}
6351
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006352static int stats_map_lookup(struct stream_interface *si)
6353{
6354 struct appctx *appctx = __objt_appctx(si->end);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006355 struct sample sample;
6356 struct pattern *pat;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006357 int match_method;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006358
6359 switch (appctx->st2) {
6360 case STAT_ST_INIT:
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006361 /* Init to the first entry. The list cannot be change */
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01006362 appctx->ctx.map.expr = LIST_ELEM(&appctx->ctx.map.ref->pat, struct pattern_expr *, list);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006363 appctx->ctx.map.expr = pat_expr_get_next(appctx->ctx.map.expr, &appctx->ctx.map.ref->pat);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006364 appctx->st2 = STAT_ST_LIST;
6365 /* fall through */
6366
6367 case STAT_ST_LIST:
6368 /* for each lookup type */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006369 while (appctx->ctx.map.expr) {
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006370 /* initialise chunk to build new message */
6371 chunk_reset(&trash);
6372
6373 /* execute pattern matching */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02006374 sample.data.type = SMP_T_STR;
Andreas Seltenreich9727cf42016-03-03 19:32:25 +01006375 sample.flags = SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02006376 sample.data.u.str.len = appctx->ctx.map.chunk.len;
6377 sample.data.u.str.str = appctx->ctx.map.chunk.str;
Thierry FOURNIER5d344082014-01-27 14:19:53 +01006378 if (appctx->ctx.map.expr->pat_head->match &&
6379 sample_convert(&sample, appctx->ctx.map.expr->pat_head->expect_type))
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006380 pat = appctx->ctx.map.expr->pat_head->match(&sample, appctx->ctx.map.expr, 1);
6381 else
6382 pat = NULL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006383
6384 /* build return message: set type of match */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006385 for (match_method=0; match_method<PAT_MATCH_NUM; match_method++)
6386 if (appctx->ctx.map.expr->pat_head->match == pat_match_fcts[match_method])
6387 break;
6388 if (match_method >= PAT_MATCH_NUM)
6389 chunk_appendf(&trash, "type=unknown(%p)", appctx->ctx.map.expr->pat_head->match);
6390 else
6391 chunk_appendf(&trash, "type=%s", pat_match_names[match_method]);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006392
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02006393 /* case sensitive */
6394 if (appctx->ctx.map.expr->mflags & PAT_MF_IGNORE_CASE)
6395 chunk_appendf(&trash, ", case=insensitive");
6396 else
6397 chunk_appendf(&trash, ", case=sensitive");
6398
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006399 /* Display no match, and set default value */
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01006400 if (!pat) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006401 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
6402 chunk_appendf(&trash, ", found=no");
6403 else
6404 chunk_appendf(&trash, ", match=no");
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006405 }
6406
6407 /* Display match and match info */
6408 else {
6409 /* display match */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006410 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
6411 chunk_appendf(&trash, ", found=yes");
6412 else
6413 chunk_appendf(&trash, ", match=yes");
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006414
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01006415 /* display index mode */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02006416 if (pat->sflags & PAT_SF_TREE)
Thierry FOURNIERb9903842014-03-11 18:48:17 +01006417 chunk_appendf(&trash, ", idx=tree");
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006418 else
Thierry FOURNIERb9903842014-03-11 18:48:17 +01006419 chunk_appendf(&trash, ", idx=list");
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006420
Thierry FOURNIER86db66a2014-03-11 18:37:58 +01006421 /* display pattern */
6422 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
6423 if (pat->ref && pat->ref->pattern)
6424 chunk_appendf(&trash, ", key=\"%s\"", pat->ref->pattern);
6425 else
6426 chunk_appendf(&trash, ", key=unknown");
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006427 }
6428 else {
Thierry FOURNIER86db66a2014-03-11 18:37:58 +01006429 if (pat->ref && pat->ref->pattern)
6430 chunk_appendf(&trash, ", pattern=\"%s\"", pat->ref->pattern);
6431 else
6432 chunk_appendf(&trash, ", pattern=unknown");
6433 }
6434
6435 /* display return value */
6436 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
Thierry FOURNIER503bb092015-08-19 08:35:43 +02006437 if (pat->data && pat->ref && pat->ref->sample)
Thierry FOURNIER03d0e452015-07-23 18:33:41 +02006438 chunk_appendf(&trash, ", value=\"%s\", type=\"%s\"", pat->ref->sample,
Thierry FOURNIER7d4335c2015-08-19 09:05:25 +02006439 smp_to_type[pat->data->type]);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006440 else
Thierry FOURNIER86db66a2014-03-11 18:37:58 +01006441 chunk_appendf(&trash, ", value=none");
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006442 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006443 }
6444
Thierry FOURNIERb9903842014-03-11 18:48:17 +01006445 chunk_appendf(&trash, "\n");
6446
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006447 /* display response */
Willy Tarreau2bb4a962014-11-28 11:11:05 +01006448 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau87b09662015-04-03 00:22:06 +02006449 /* let's try again later from this stream. We add ourselves into
6450 * this stream's users so that it can remove us upon termination.
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006451 */
Willy Tarreaufe127932015-04-21 19:23:39 +02006452 si_applet_cant_put(si);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006453 return 0;
6454 }
6455
6456 /* get next entry */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006457 appctx->ctx.map.expr = pat_expr_get_next(appctx->ctx.map.expr,
6458 &appctx->ctx.map.ref->pat);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006459 }
6460
6461 appctx->st2 = STAT_ST_FIN;
6462 /* fall through */
6463
6464 default:
6465 appctx->st2 = STAT_ST_FIN;
6466 free(appctx->ctx.map.chunk.str);
6467 return 1;
6468 }
6469}
6470
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006471static int stats_pat_list(struct stream_interface *si)
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006472{
6473 struct appctx *appctx = __objt_appctx(si->end);
6474
6475 switch (appctx->st2) {
6476
6477 case STAT_ST_INIT:
6478 /* Init to the first entry. The list cannot be change */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006479 appctx->ctx.map.elt = LIST_NEXT(&appctx->ctx.map.ref->head,
6480 struct pat_ref_elt *, list);
6481 if (&appctx->ctx.map.elt->list == &appctx->ctx.map.ref->head)
6482 appctx->ctx.map.elt = NULL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006483 appctx->st2 = STAT_ST_LIST;
6484 /* fall through */
6485
6486 case STAT_ST_LIST:
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006487 while (appctx->ctx.map.elt) {
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006488 chunk_reset(&trash);
6489
6490 /* build messages */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006491 if (appctx->ctx.map.elt->sample)
Thierry FOURNIER9356c682014-01-28 15:55:37 +01006492 chunk_appendf(&trash, "%p %s %s\n",
6493 appctx->ctx.map.elt, appctx->ctx.map.elt->pattern,
6494 appctx->ctx.map.elt->sample);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006495 else
Thierry FOURNIER9356c682014-01-28 15:55:37 +01006496 chunk_appendf(&trash, "%p %s\n",
6497 appctx->ctx.map.elt, appctx->ctx.map.elt->pattern);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006498
Willy Tarreau2bb4a962014-11-28 11:11:05 +01006499 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau87b09662015-04-03 00:22:06 +02006500 /* let's try again later from this stream. We add ourselves into
6501 * this stream's users so that it can remove us upon termination.
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006502 */
Willy Tarreaufe127932015-04-21 19:23:39 +02006503 si_applet_cant_put(si);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006504 return 0;
6505 }
6506
6507 /* get next list entry and check the end of the list */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006508 appctx->ctx.map.elt = LIST_NEXT(&appctx->ctx.map.elt->list,
6509 struct pat_ref_elt *, list);
6510 if (&appctx->ctx.map.elt->list == &appctx->ctx.map.ref->head)
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006511 break;
6512 }
6513
6514 appctx->st2 = STAT_ST_FIN;
6515 /* fall through */
6516
6517 default:
6518 appctx->st2 = STAT_ST_FIN;
6519 return 1;
6520 }
6521}
6522
Willy Tarreau87b09662015-04-03 00:22:06 +02006523/* This function dumps all streams' states onto the stream interface's
Willy Tarreau306f8302013-07-08 15:53:06 +02006524 * read buffer. It returns 0 if the output buffer is full and it needs
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01006525 * to be called again, otherwise non-zero. It is designed to be called
6526 * from stats_dump_sess_to_buffer() below.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006527 */
Simon Horman9bd2c732011-06-15 15:18:44 +09006528static int stats_dump_sess_to_buffer(struct stream_interface *si)
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006529{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006530 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006531 struct connection *conn;
6532
Willy Tarreau2bb4a962014-11-28 11:11:05 +01006533 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW))) {
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006534 /* If we're forced to shut down, we might have to remove our
Willy Tarreau87b09662015-04-03 00:22:06 +02006535 * reference to the last stream being dumped.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006536 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006537 if (appctx->st2 == STAT_ST_LIST) {
6538 if (!LIST_ISEMPTY(&appctx->ctx.sess.bref.users)) {
6539 LIST_DEL(&appctx->ctx.sess.bref.users);
6540 LIST_INIT(&appctx->ctx.sess.bref.users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +01006541 }
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006542 }
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02006543 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006544 }
6545
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006546 chunk_reset(&trash);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006547
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006548 switch (appctx->st2) {
Willy Tarreau295a8372011-03-10 11:25:07 +01006549 case STAT_ST_INIT:
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006550 /* the function had not been called yet, let's prepare the
Willy Tarreau87b09662015-04-03 00:22:06 +02006551 * buffer for a response. We initialize the current stream
Willy Tarreaufd3828e2009-02-22 15:17:24 +01006552 * pointer to the first in the global list. When a target
Willy Tarreau87b09662015-04-03 00:22:06 +02006553 * stream is being destroyed, it is responsible for updating
Willy Tarreaufd3828e2009-02-22 15:17:24 +01006554 * this pointer. We know we have reached the end when this
Willy Tarreau87b09662015-04-03 00:22:06 +02006555 * pointer points back to the head of the streams list.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006556 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006557 LIST_INIT(&appctx->ctx.sess.bref.users);
Willy Tarreau87b09662015-04-03 00:22:06 +02006558 appctx->ctx.sess.bref.ref = streams.n;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006559 appctx->st2 = STAT_ST_LIST;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006560 /* fall through */
6561
Willy Tarreau295a8372011-03-10 11:25:07 +01006562 case STAT_ST_LIST:
Willy Tarreau87b09662015-04-03 00:22:06 +02006563 /* first, let's detach the back-ref from a possible previous stream */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006564 if (!LIST_ISEMPTY(&appctx->ctx.sess.bref.users)) {
6565 LIST_DEL(&appctx->ctx.sess.bref.users);
6566 LIST_INIT(&appctx->ctx.sess.bref.users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +01006567 }
6568
6569 /* and start from where we stopped */
Willy Tarreau87b09662015-04-03 00:22:06 +02006570 while (appctx->ctx.sess.bref.ref != &streams) {
Cyril Bontéacd7d632010-11-01 19:26:02 +01006571 char pn[INET6_ADDRSTRLEN];
Willy Tarreau87b09662015-04-03 00:22:06 +02006572 struct stream *curr_sess;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006573
Willy Tarreau87b09662015-04-03 00:22:06 +02006574 curr_sess = LIST_ELEM(appctx->ctx.sess.bref.ref, struct stream *, list);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006575
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006576 if (appctx->ctx.sess.target) {
6577 if (appctx->ctx.sess.target != (void *)-1 && appctx->ctx.sess.target != curr_sess)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006578 goto next_sess;
6579
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006580 LIST_ADDQ(&curr_sess->back_refs, &appctx->ctx.sess.bref.users);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006581 /* call the proper dump() function and return if we're missing space */
Willy Tarreau76153662012-11-26 01:16:39 +01006582 if (!stats_dump_full_sess_to_buffer(si, curr_sess))
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006583 return 0;
6584
Willy Tarreau87b09662015-04-03 00:22:06 +02006585 /* stream dump complete */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006586 LIST_DEL(&appctx->ctx.sess.bref.users);
6587 LIST_INIT(&appctx->ctx.sess.bref.users);
6588 if (appctx->ctx.sess.target != (void *)-1) {
6589 appctx->ctx.sess.target = NULL;
Willy Tarreau76153662012-11-26 01:16:39 +01006590 break;
6591 }
6592 else
6593 goto next_sess;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006594 }
6595
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006596 chunk_appendf(&trash,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006597 "%p: proto=%s",
6598 curr_sess,
Willy Tarreauf1e02122015-09-23 12:16:43 +02006599 strm_li(curr_sess) ? strm_li(curr_sess)->proto->name : "?");
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006600
Willy Tarreaud0d8da92015-04-04 02:10:38 +02006601 conn = objt_conn(strm_orig(curr_sess));
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006602 switch (conn ? addr_to_str(&conn->addr.from, pn, sizeof(pn)) : AF_UNSPEC) {
Willy Tarreau631f01c2011-09-05 00:36:48 +02006603 case AF_INET:
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006604 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006605 chunk_appendf(&trash,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006606 " src=%s:%d fe=%s be=%s srv=%s",
6607 pn,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006608 get_host_port(&conn->addr.from),
Willy Tarreaud0d8da92015-04-04 02:10:38 +02006609 strm_fe(curr_sess)->id,
Willy Tarreau50943332011-09-02 17:33:05 +02006610 (curr_sess->be->cap & PR_CAP_BE) ? curr_sess->be->id : "<NONE>",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006611 objt_server(curr_sess->target) ? objt_server(curr_sess->target)->id : "<none>"
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006612 );
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006613 break;
6614 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006615 chunk_appendf(&trash,
Emeric Brun837ca522010-10-22 16:19:01 +02006616 " src=unix:%d fe=%s be=%s srv=%s",
Willy Tarreaud0d8da92015-04-04 02:10:38 +02006617 strm_li(curr_sess)->luid,
6618 strm_fe(curr_sess)->id,
Willy Tarreau50943332011-09-02 17:33:05 +02006619 (curr_sess->be->cap & PR_CAP_BE) ? curr_sess->be->id : "<NONE>",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006620 objt_server(curr_sess->target) ? objt_server(curr_sess->target)->id : "<none>"
Emeric Brun837ca522010-10-22 16:19:01 +02006621 );
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006622 break;
6623 }
6624
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006625 chunk_appendf(&trash,
Willy Tarreau65671ab2009-10-04 14:24:59 +02006626 " ts=%02x age=%s calls=%d",
6627 curr_sess->task->state,
Willy Tarreau3884cba2009-03-28 17:54:35 +01006628 human_time(now.tv_sec - curr_sess->logs.tv_accept.tv_sec, 1),
6629 curr_sess->task->calls);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006630
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006631 chunk_appendf(&trash,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01006632 " rq[f=%06xh,i=%d,an=%02xh,rx=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006633 curr_sess->req.flags,
6634 curr_sess->req.buf->i,
6635 curr_sess->req.analysers,
6636 curr_sess->req.rex ?
6637 human_time(TICKS_TO_MS(curr_sess->req.rex - now_ms),
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006638 TICKS_TO_MS(1000)) : "");
6639
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006640 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006641 ",wx=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006642 curr_sess->req.wex ?
6643 human_time(TICKS_TO_MS(curr_sess->req.wex - now_ms),
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006644 TICKS_TO_MS(1000)) : "");
6645
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006646 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006647 ",ax=%s]",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006648 curr_sess->req.analyse_exp ?
6649 human_time(TICKS_TO_MS(curr_sess->req.analyse_exp - now_ms),
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006650 TICKS_TO_MS(1000)) : "");
6651
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006652 chunk_appendf(&trash,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01006653 " rp[f=%06xh,i=%d,an=%02xh,rx=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006654 curr_sess->res.flags,
6655 curr_sess->res.buf->i,
6656 curr_sess->res.analysers,
6657 curr_sess->res.rex ?
6658 human_time(TICKS_TO_MS(curr_sess->res.rex - now_ms),
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006659 TICKS_TO_MS(1000)) : "");
6660
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006661 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006662 ",wx=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006663 curr_sess->res.wex ?
6664 human_time(TICKS_TO_MS(curr_sess->res.wex - now_ms),
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006665 TICKS_TO_MS(1000)) : "");
6666
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006667 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006668 ",ax=%s]",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006669 curr_sess->res.analyse_exp ?
6670 human_time(TICKS_TO_MS(curr_sess->res.analyse_exp - now_ms),
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006671 TICKS_TO_MS(1000)) : "");
6672
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006673 conn = objt_conn(curr_sess->si[0].end);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006674 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006675 " s0=[%d,%1xh,fd=%d,ex=%s]",
6676 curr_sess->si[0].state,
6677 curr_sess->si[0].flags,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006678 conn ? conn->t.sock.fd : -1,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006679 curr_sess->si[0].exp ?
6680 human_time(TICKS_TO_MS(curr_sess->si[0].exp - now_ms),
6681 TICKS_TO_MS(1000)) : "");
6682
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006683 conn = objt_conn(curr_sess->si[1].end);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006684 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006685 " s1=[%d,%1xh,fd=%d,ex=%s]",
6686 curr_sess->si[1].state,
6687 curr_sess->si[1].flags,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006688 conn ? conn->t.sock.fd : -1,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006689 curr_sess->si[1].exp ?
6690 human_time(TICKS_TO_MS(curr_sess->si[1].exp - now_ms),
6691 TICKS_TO_MS(1000)) : "");
6692
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006693 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006694 " exp=%s",
6695 curr_sess->task->expire ?
6696 human_time(TICKS_TO_MS(curr_sess->task->expire - now_ms),
6697 TICKS_TO_MS(1000)) : "");
Willy Tarreau4726f532009-03-07 17:25:21 +01006698 if (task_in_rq(curr_sess->task))
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006699 chunk_appendf(&trash, " run(nice=%d)", curr_sess->task->nice);
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006700
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006701 chunk_appendf(&trash, "\n");
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006702
Willy Tarreau2bb4a962014-11-28 11:11:05 +01006703 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau87b09662015-04-03 00:22:06 +02006704 /* let's try again later from this stream. We add ourselves into
6705 * this stream's users so that it can remove us upon termination.
Willy Tarreaufd3828e2009-02-22 15:17:24 +01006706 */
Willy Tarreaufe127932015-04-21 19:23:39 +02006707 si_applet_cant_put(si);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006708 LIST_ADDQ(&curr_sess->back_refs, &appctx->ctx.sess.bref.users);
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02006709 return 0;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006710 }
6711
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006712 next_sess:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006713 appctx->ctx.sess.bref.ref = curr_sess->list.n;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006714 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006715
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006716 if (appctx->ctx.sess.target && appctx->ctx.sess.target != (void *)-1) {
Willy Tarreau87b09662015-04-03 00:22:06 +02006717 /* specified stream not found */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006718 if (appctx->ctx.sess.section > 0)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006719 chunk_appendf(&trash, " *** session terminated while we were watching it ***\n");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006720 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006721 chunk_appendf(&trash, "Session not found.\n");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006722
Willy Tarreaubc18da12015-03-13 14:00:47 +01006723 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02006724 si_applet_cant_put(si);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006725 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01006726 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006727
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006728 appctx->ctx.sess.target = NULL;
6729 appctx->ctx.sess.uid = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006730 return 1;
6731 }
6732
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006733 appctx->st2 = STAT_ST_FIN;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006734 /* fall through */
6735
6736 default:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006737 appctx->st2 = STAT_ST_FIN;
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02006738 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006739 }
Emeric Brun1e029aa2010-09-23 18:12:53 +02006740}
6741
Willy Tarreau5f9a8772012-11-26 02:22:40 +01006742/* This is called when the stream interface is closed. For instance, upon an
6743 * external abort, we won't call the i/o handler anymore so we may need to
Willy Tarreau87b09662015-04-03 00:22:06 +02006744 * remove back references to the stream currently being dumped.
Willy Tarreau5f9a8772012-11-26 02:22:40 +01006745 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02006746static void cli_release_handler(struct appctx *appctx)
Willy Tarreau5f9a8772012-11-26 02:22:40 +01006747{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006748 if (appctx->st0 == STAT_CLI_O_SESS && appctx->st2 == STAT_ST_LIST) {
6749 if (!LIST_ISEMPTY(&appctx->ctx.sess.bref.users))
6750 LIST_DEL(&appctx->ctx.sess.bref.users);
Willy Tarreau5f9a8772012-11-26 02:22:40 +01006751 }
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01006752 else if (appctx->st0 == STAT_CLI_PRINT_FREE) {
6753 free(appctx->ctx.cli.err);
Willy Tarreau6457d0f2015-10-07 20:00:24 +02006754 appctx->ctx.cli.err = NULL;
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01006755 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006756 else if (appctx->st0 == STAT_CLI_O_MLOOK) {
6757 free(appctx->ctx.map.chunk.str);
Willy Tarreau6457d0f2015-10-07 20:00:24 +02006758 appctx->ctx.map.chunk.str = NULL;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006759 }
Willy Tarreau5f9a8772012-11-26 02:22:40 +01006760}
6761
Willy Tarreau20e99322013-04-13 09:22:25 +02006762/* This function is used to either dump tables states (when action is set
6763 * to STAT_CLI_O_TAB) or clear tables (when action is STAT_CLI_O_CLR).
Willy Tarreau20e99322013-04-13 09:22:25 +02006764 * It returns 0 if the output buffer is full and it needs to be called
6765 * again, otherwise non-zero.
Willy Tarreau69f58c82010-07-12 17:55:33 +02006766 */
Willy Tarreau20e99322013-04-13 09:22:25 +02006767static int stats_table_request(struct stream_interface *si, int action)
Willy Tarreau69f58c82010-07-12 17:55:33 +02006768{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006769 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau87b09662015-04-03 00:22:06 +02006770 struct stream *s = si_strm(si);
Willy Tarreau69f58c82010-07-12 17:55:33 +02006771 struct ebmb_node *eb;
6772 int dt;
Willy Tarreau44455022012-12-05 23:01:12 +01006773 int skip_entry;
Willy Tarreau20e99322013-04-13 09:22:25 +02006774 int show = action == STAT_CLI_O_TAB;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006775
6776 /*
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006777 * We have 3 possible states in appctx->st2 :
Willy Tarreau295a8372011-03-10 11:25:07 +01006778 * - STAT_ST_INIT : the first call
6779 * - STAT_ST_INFO : the proxy pointer points to the next table to
Willy Tarreau69f58c82010-07-12 17:55:33 +02006780 * dump, the entry pointer is NULL ;
Willy Tarreau295a8372011-03-10 11:25:07 +01006781 * - STAT_ST_LIST : the proxy pointer points to the current table
Willy Tarreau69f58c82010-07-12 17:55:33 +02006782 * and the entry pointer points to the next entry to be dumped,
6783 * and the refcount on the next entry is held ;
Willy Tarreau295a8372011-03-10 11:25:07 +01006784 * - STAT_ST_END : nothing left to dump, the buffer may contain some
Willy Tarreau69f58c82010-07-12 17:55:33 +02006785 * data though.
6786 */
6787
Willy Tarreau2bb4a962014-11-28 11:11:05 +01006788 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW))) {
Willy Tarreau69f58c82010-07-12 17:55:33 +02006789 /* in case of abort, remove any refcount we might have set on an entry */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006790 if (appctx->st2 == STAT_ST_LIST) {
6791 appctx->ctx.table.entry->ref_cnt--;
6792 stksess_kill_if_expired(&appctx->ctx.table.proxy->table, appctx->ctx.table.entry);
Willy Tarreauf6efda12010-08-03 20:34:06 +02006793 }
Willy Tarreau69f58c82010-07-12 17:55:33 +02006794 return 1;
6795 }
6796
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006797 chunk_reset(&trash);
Willy Tarreau69f58c82010-07-12 17:55:33 +02006798
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006799 while (appctx->st2 != STAT_ST_FIN) {
6800 switch (appctx->st2) {
Willy Tarreau295a8372011-03-10 11:25:07 +01006801 case STAT_ST_INIT:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006802 appctx->ctx.table.proxy = appctx->ctx.table.target;
6803 if (!appctx->ctx.table.proxy)
6804 appctx->ctx.table.proxy = proxy;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006805
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006806 appctx->ctx.table.entry = NULL;
6807 appctx->st2 = STAT_ST_INFO;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006808 break;
6809
Willy Tarreau295a8372011-03-10 11:25:07 +01006810 case STAT_ST_INFO:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006811 if (!appctx->ctx.table.proxy ||
6812 (appctx->ctx.table.target &&
6813 appctx->ctx.table.proxy != appctx->ctx.table.target)) {
6814 appctx->st2 = STAT_ST_END;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006815 break;
6816 }
6817
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006818 if (appctx->ctx.table.proxy->table.size) {
6819 if (show && !stats_dump_table_head_to_buffer(&trash, si, appctx->ctx.table.proxy,
6820 appctx->ctx.table.target))
Willy Tarreau69f58c82010-07-12 17:55:33 +02006821 return 0;
6822
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006823 if (appctx->ctx.table.target &&
Willy Tarreaud0d8da92015-04-04 02:10:38 +02006824 strm_li(s)->bind_conf->level >= ACCESS_LVL_OPER) {
Willy Tarreau69f58c82010-07-12 17:55:33 +02006825 /* dump entries only if table explicitly requested */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006826 eb = ebmb_first(&appctx->ctx.table.proxy->table.keys);
Willy Tarreau69f58c82010-07-12 17:55:33 +02006827 if (eb) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006828 appctx->ctx.table.entry = ebmb_entry(eb, struct stksess, key);
6829 appctx->ctx.table.entry->ref_cnt++;
6830 appctx->st2 = STAT_ST_LIST;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006831 break;
6832 }
6833 }
6834 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006835 appctx->ctx.table.proxy = appctx->ctx.table.proxy->next;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006836 break;
6837
Willy Tarreau295a8372011-03-10 11:25:07 +01006838 case STAT_ST_LIST:
Willy Tarreau44455022012-12-05 23:01:12 +01006839 skip_entry = 0;
Simon Hormanc88b8872011-06-15 15:18:49 +09006840
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006841 if (appctx->ctx.table.data_type >= 0) {
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02006842 /* we're filtering on some data contents */
6843 void *ptr;
6844 long long data;
6845
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006846 dt = appctx->ctx.table.data_type;
6847 ptr = stktable_data_ptr(&appctx->ctx.table.proxy->table,
6848 appctx->ctx.table.entry,
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02006849 dt);
6850
6851 data = 0;
6852 switch (stktable_data_types[dt].std_type) {
6853 case STD_T_SINT:
6854 data = stktable_data_cast(ptr, std_t_sint);
6855 break;
6856 case STD_T_UINT:
6857 data = stktable_data_cast(ptr, std_t_uint);
6858 break;
6859 case STD_T_ULL:
6860 data = stktable_data_cast(ptr, std_t_ull);
6861 break;
6862 case STD_T_FRQP:
6863 data = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006864 appctx->ctx.table.proxy->table.data_arg[dt].u);
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02006865 break;
6866 }
6867
6868 /* skip the entry if the data does not match the test and the value */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006869 if ((data < appctx->ctx.table.value &&
6870 (appctx->ctx.table.data_op == STD_OP_EQ ||
6871 appctx->ctx.table.data_op == STD_OP_GT ||
6872 appctx->ctx.table.data_op == STD_OP_GE)) ||
6873 (data == appctx->ctx.table.value &&
6874 (appctx->ctx.table.data_op == STD_OP_NE ||
6875 appctx->ctx.table.data_op == STD_OP_GT ||
6876 appctx->ctx.table.data_op == STD_OP_LT)) ||
6877 (data > appctx->ctx.table.value &&
6878 (appctx->ctx.table.data_op == STD_OP_EQ ||
6879 appctx->ctx.table.data_op == STD_OP_LT ||
6880 appctx->ctx.table.data_op == STD_OP_LE)))
Willy Tarreau44455022012-12-05 23:01:12 +01006881 skip_entry = 1;
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02006882 }
6883
Simon Hormanc88b8872011-06-15 15:18:49 +09006884 if (show && !skip_entry &&
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006885 !stats_dump_table_entry_to_buffer(&trash, si, appctx->ctx.table.proxy,
6886 appctx->ctx.table.entry))
Simon Hormand9366582011-06-15 15:18:45 +09006887 return 0;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006888
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006889 appctx->ctx.table.entry->ref_cnt--;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006890
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006891 eb = ebmb_next(&appctx->ctx.table.entry->key);
Willy Tarreau69f58c82010-07-12 17:55:33 +02006892 if (eb) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006893 struct stksess *old = appctx->ctx.table.entry;
6894 appctx->ctx.table.entry = ebmb_entry(eb, struct stksess, key);
Willy Tarreau8fa52f42012-01-09 11:50:03 +01006895 if (show)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006896 stksess_kill_if_expired(&appctx->ctx.table.proxy->table, old);
6897 else if (!skip_entry && !appctx->ctx.table.entry->ref_cnt)
6898 stksess_kill(&appctx->ctx.table.proxy->table, old);
6899 appctx->ctx.table.entry->ref_cnt++;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006900 break;
6901 }
6902
Simon Hormanc88b8872011-06-15 15:18:49 +09006903
6904 if (show)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006905 stksess_kill_if_expired(&appctx->ctx.table.proxy->table, appctx->ctx.table.entry);
6906 else if (!skip_entry && !appctx->ctx.table.entry->ref_cnt)
6907 stksess_kill(&appctx->ctx.table.proxy->table, appctx->ctx.table.entry);
Simon Hormanc88b8872011-06-15 15:18:49 +09006908
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006909 appctx->ctx.table.proxy = appctx->ctx.table.proxy->next;
6910 appctx->st2 = STAT_ST_INFO;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006911 break;
6912
Willy Tarreau295a8372011-03-10 11:25:07 +01006913 case STAT_ST_END:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006914 appctx->st2 = STAT_ST_FIN;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006915 break;
6916 }
6917 }
6918 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006919}
6920
Willy Tarreaud426a182010-03-05 14:58:26 +01006921/* print a line of text buffer (limited to 70 bytes) to <out>. The format is :
Willy Tarreau74808cb2009-03-04 15:53:18 +01006922 * <2 spaces> <offset=5 digits> <space or plus> <space> <70 chars max> <\n>
6923 * which is 60 chars per line. Non-printable chars \t, \n, \r and \e are
6924 * encoded in C format. Other non-printable chars are encoded "\xHH". Original
6925 * lines are respected within the limit of 70 output chars. Lines that are
6926 * continuation of a previous truncated line begin with "+" instead of " "
6927 * after the offset. The new pointer is returned.
6928 */
Willy Tarreaud426a182010-03-05 14:58:26 +01006929static int dump_text_line(struct chunk *out, const char *buf, int bsize, int len,
6930 int *line, int ptr)
Willy Tarreau74808cb2009-03-04 15:53:18 +01006931{
6932 int end;
6933 unsigned char c;
6934
6935 end = out->len + 80;
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02006936 if (end > out->size)
Willy Tarreau74808cb2009-03-04 15:53:18 +01006937 return ptr;
6938
Willy Tarreau77804732012-10-29 16:14:26 +01006939 chunk_appendf(out, " %05d%c ", ptr, (ptr == *line) ? ' ' : '+');
Willy Tarreau74808cb2009-03-04 15:53:18 +01006940
Willy Tarreaud426a182010-03-05 14:58:26 +01006941 while (ptr < len && ptr < bsize) {
6942 c = buf[ptr];
Willy Tarreau787bbd92009-03-12 08:18:33 +01006943 if (isprint(c) && isascii(c) && c != '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006944 if (out->len > end - 2)
6945 break;
6946 out->str[out->len++] = c;
Willy Tarreau787bbd92009-03-12 08:18:33 +01006947 } else if (c == '\t' || c == '\n' || c == '\r' || c == '\e' || c == '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006948 if (out->len > end - 3)
6949 break;
6950 out->str[out->len++] = '\\';
6951 switch (c) {
6952 case '\t': c = 't'; break;
6953 case '\n': c = 'n'; break;
6954 case '\r': c = 'r'; break;
6955 case '\e': c = 'e'; break;
Willy Tarreau787bbd92009-03-12 08:18:33 +01006956 case '\\': c = '\\'; break;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006957 }
6958 out->str[out->len++] = c;
6959 } else {
6960 if (out->len > end - 5)
6961 break;
6962 out->str[out->len++] = '\\';
6963 out->str[out->len++] = 'x';
6964 out->str[out->len++] = hextab[(c >> 4) & 0xF];
6965 out->str[out->len++] = hextab[c & 0xF];
6966 }
Willy Tarreaud426a182010-03-05 14:58:26 +01006967 if (buf[ptr++] == '\n') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006968 /* we had a line break, let's return now */
6969 out->str[out->len++] = '\n';
6970 *line = ptr;
6971 return ptr;
6972 }
6973 }
6974 /* we have an incomplete line, we return it as-is */
6975 out->str[out->len++] = '\n';
6976 return ptr;
6977}
6978
Baptiste Assmann3863f972015-05-17 00:33:24 +02006979/* This function dumps counters from all resolvers section and associated name servers.
6980 * It returns 0 if the output buffer is full and it needs
6981 * to be called again, otherwise non-zero.
6982 */
6983static int stats_dump_resolvers_to_buffer(struct stream_interface *si)
6984{
6985 struct appctx *appctx = __objt_appctx(si->end);
6986 struct dns_resolvers *presolvers;
6987 struct dns_nameserver *pnameserver;
6988
6989 chunk_reset(&trash);
6990
6991 switch (appctx->st2) {
6992 case STAT_ST_INIT:
6993 appctx->st2 = STAT_ST_LIST; /* let's start producing data */
6994 /* fall through */
6995
6996 case STAT_ST_LIST:
Andrew Hayworth68d05342015-10-02 20:33:01 +00006997 if (LIST_ISEMPTY(&dns_resolvers)) {
6998 chunk_appendf(&trash, "No resolvers found\n");
6999 }
7000 else {
7001 list_for_each_entry(presolvers, &dns_resolvers, list) {
7002 if (appctx->ctx.resolvers.ptr != NULL && appctx->ctx.resolvers.ptr != presolvers)
7003 continue;
7004
7005 chunk_appendf(&trash, "Resolvers section %s\n", presolvers->id);
7006 list_for_each_entry(pnameserver, &presolvers->nameserver_list, list) {
7007 chunk_appendf(&trash, " nameserver %s:\n", pnameserver->id);
7008 chunk_appendf(&trash, " sent: %ld\n", pnameserver->counters.sent);
7009 chunk_appendf(&trash, " valid: %ld\n", pnameserver->counters.valid);
7010 chunk_appendf(&trash, " update: %ld\n", pnameserver->counters.update);
7011 chunk_appendf(&trash, " cname: %ld\n", pnameserver->counters.cname);
7012 chunk_appendf(&trash, " cname_error: %ld\n", pnameserver->counters.cname_error);
7013 chunk_appendf(&trash, " any_err: %ld\n", pnameserver->counters.any_err);
7014 chunk_appendf(&trash, " nx: %ld\n", pnameserver->counters.nx);
7015 chunk_appendf(&trash, " timeout: %ld\n", pnameserver->counters.timeout);
7016 chunk_appendf(&trash, " refused: %ld\n", pnameserver->counters.refused);
7017 chunk_appendf(&trash, " other: %ld\n", pnameserver->counters.other);
7018 chunk_appendf(&trash, " invalid: %ld\n", pnameserver->counters.invalid);
7019 chunk_appendf(&trash, " too_big: %ld\n", pnameserver->counters.too_big);
7020 chunk_appendf(&trash, " truncated: %ld\n", pnameserver->counters.truncated);
7021 chunk_appendf(&trash, " outdated: %ld\n", pnameserver->counters.outdated);
7022 }
7023 }
Baptiste Assmann3863f972015-05-17 00:33:24 +02007024 }
7025
7026 /* display response */
7027 if (bi_putchk(si_ic(si), &trash) == -1) {
7028 /* let's try again later from this session. We add ourselves into
7029 * this session's users so that it can remove us upon termination.
7030 */
7031 si->flags |= SI_FL_WAIT_ROOM;
7032 return 0;
7033 }
7034
7035 appctx->st2 = STAT_ST_FIN;
7036 /* fall through */
7037
7038 default:
7039 appctx->st2 = STAT_ST_FIN;
7040 return 1;
7041 }
7042}
7043
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02007044/* This function dumps all captured errors onto the stream interface's
Willy Tarreau306f8302013-07-08 15:53:06 +02007045 * read buffer. It returns 0 if the output buffer is full and it needs
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01007046 * to be called again, otherwise non-zero.
Willy Tarreau74808cb2009-03-04 15:53:18 +01007047 */
Simon Horman9bd2c732011-06-15 15:18:44 +09007048static int stats_dump_errors_to_buffer(struct stream_interface *si)
Willy Tarreau74808cb2009-03-04 15:53:18 +01007049{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01007050 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau74808cb2009-03-04 15:53:18 +01007051 extern const char *monthname[12];
Willy Tarreau74808cb2009-03-04 15:53:18 +01007052
Willy Tarreau2bb4a962014-11-28 11:11:05 +01007053 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
Willy Tarreau61b34732009-10-03 23:49:35 +02007054 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01007055
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007056 chunk_reset(&trash);
Willy Tarreau74808cb2009-03-04 15:53:18 +01007057
Willy Tarreau7b4b4992013-12-01 09:15:12 +01007058 if (!appctx->ctx.errors.px) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01007059 /* the function had not been called yet, let's prepare the
7060 * buffer for a response.
7061 */
Willy Tarreau10479e42010-12-12 14:00:34 +01007062 struct tm tm;
7063
7064 get_localtime(date.tv_sec, &tm);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007065 chunk_appendf(&trash, "Total events captured on [%02d/%s/%04d:%02d:%02d:%02d.%03d] : %u\n",
Willy Tarreau10479e42010-12-12 14:00:34 +01007066 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
7067 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(date.tv_usec/1000),
7068 error_snapshot_id);
7069
Willy Tarreau2bb4a962014-11-28 11:11:05 +01007070 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau10479e42010-12-12 14:00:34 +01007071 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreaufe127932015-04-21 19:23:39 +02007072 si_applet_cant_put(si);
Willy Tarreau10479e42010-12-12 14:00:34 +01007073 return 0;
7074 }
7075
Willy Tarreau7b4b4992013-12-01 09:15:12 +01007076 appctx->ctx.errors.px = proxy;
7077 appctx->ctx.errors.buf = 0;
7078 appctx->ctx.errors.bol = 0;
7079 appctx->ctx.errors.ptr = -1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01007080 }
7081
7082 /* we have two inner loops here, one for the proxy, the other one for
7083 * the buffer.
7084 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01007085 while (appctx->ctx.errors.px) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01007086 struct error_snapshot *es;
7087
Willy Tarreau7b4b4992013-12-01 09:15:12 +01007088 if (appctx->ctx.errors.buf == 0)
7089 es = &appctx->ctx.errors.px->invalid_req;
Willy Tarreau74808cb2009-03-04 15:53:18 +01007090 else
Willy Tarreau7b4b4992013-12-01 09:15:12 +01007091 es = &appctx->ctx.errors.px->invalid_rep;
Willy Tarreau74808cb2009-03-04 15:53:18 +01007092
7093 if (!es->when.tv_sec)
7094 goto next;
7095
Willy Tarreau7b4b4992013-12-01 09:15:12 +01007096 if (appctx->ctx.errors.iid >= 0 &&
7097 appctx->ctx.errors.px->uuid != appctx->ctx.errors.iid &&
7098 es->oe->uuid != appctx->ctx.errors.iid)
Willy Tarreau74808cb2009-03-04 15:53:18 +01007099 goto next;
7100
Willy Tarreau7b4b4992013-12-01 09:15:12 +01007101 if (appctx->ctx.errors.ptr < 0) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01007102 /* just print headers now */
7103
7104 char pn[INET6_ADDRSTRLEN];
7105 struct tm tm;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02007106 int port;
Willy Tarreau74808cb2009-03-04 15:53:18 +01007107
7108 get_localtime(es->when.tv_sec, &tm);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007109 chunk_appendf(&trash, " \n[%02d/%s/%04d:%02d:%02d:%02d.%03d]",
Willy Tarreau74808cb2009-03-04 15:53:18 +01007110 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
Willy Tarreau1772ece2009-04-03 14:49:12 +02007111 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(es->when.tv_usec/1000));
Willy Tarreau74808cb2009-03-04 15:53:18 +01007112
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02007113 switch (addr_to_str(&es->src, pn, sizeof(pn))) {
7114 case AF_INET:
7115 case AF_INET6:
7116 port = get_host_port(&es->src);
7117 break;
7118 default:
7119 port = 0;
7120 }
7121
Willy Tarreau7b4b4992013-12-01 09:15:12 +01007122 switch (appctx->ctx.errors.buf) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01007123 case 0:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007124 chunk_appendf(&trash,
Willy Tarreau74808cb2009-03-04 15:53:18 +01007125 " frontend %s (#%d): invalid request\n"
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02007126 " backend %s (#%d)",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01007127 appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02007128 (es->oe->cap & PR_CAP_BE) ? es->oe->id : "<NONE>",
7129 (es->oe->cap & PR_CAP_BE) ? es->oe->uuid : -1);
Willy Tarreau74808cb2009-03-04 15:53:18 +01007130 break;
7131 case 1:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007132 chunk_appendf(&trash,
Olivier Doucet08afdcb2014-09-08 11:23:00 +02007133 " backend %s (#%d): invalid response\n"
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02007134 " frontend %s (#%d)",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01007135 appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02007136 es->oe->id, es->oe->uuid);
Willy Tarreau74808cb2009-03-04 15:53:18 +01007137 break;
7138 }
7139
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007140 chunk_appendf(&trash,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02007141 ", server %s (#%d), event #%u\n"
7142 " src %s:%d, session #%d, session flags 0x%08x\n"
7143 " HTTP msg state %d, msg flags 0x%08x, tx flags 0x%08x\n"
7144 " HTTP chunk len %lld bytes, HTTP body len %lld bytes\n"
7145 " buffer flags 0x%08x, out %d bytes, total %lld bytes\n"
7146 " pending %d bytes, wrapping at %d, error at position %d:\n \n",
7147 es->srv ? es->srv->id : "<NONE>", es->srv ? es->srv->puid : -1,
7148 es->ev_id,
7149 pn, port, es->sid, es->s_flags,
7150 es->state, es->m_flags, es->t_flags,
7151 es->m_clen, es->m_blen,
7152 es->b_flags, es->b_out, es->b_tot,
7153 es->len, es->b_wrap, es->pos);
7154
Willy Tarreau2bb4a962014-11-28 11:11:05 +01007155 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01007156 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreaufe127932015-04-21 19:23:39 +02007157 si_applet_cant_put(si);
Willy Tarreau61b34732009-10-03 23:49:35 +02007158 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01007159 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01007160 appctx->ctx.errors.ptr = 0;
7161 appctx->ctx.errors.sid = es->sid;
Willy Tarreau74808cb2009-03-04 15:53:18 +01007162 }
7163
Willy Tarreau7b4b4992013-12-01 09:15:12 +01007164 if (appctx->ctx.errors.sid != es->sid) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01007165 /* the snapshot changed while we were dumping it */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007166 chunk_appendf(&trash,
Willy Tarreau74808cb2009-03-04 15:53:18 +01007167 " WARNING! update detected on this snapshot, dump interrupted. Please re-check!\n");
Willy Tarreaubc18da12015-03-13 14:00:47 +01007168 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02007169 si_applet_cant_put(si);
Willy Tarreau61b34732009-10-03 23:49:35 +02007170 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01007171 }
Willy Tarreau74808cb2009-03-04 15:53:18 +01007172 goto next;
7173 }
7174
7175 /* OK, ptr >= 0, so we have to dump the current line */
Willy Tarreauf3764b72016-03-31 13:45:10 +02007176 while (es->buf && appctx->ctx.errors.ptr < es->len && appctx->ctx.errors.ptr < global.tune.bufsize) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01007177 int newptr;
7178 int newline;
7179
Willy Tarreau7b4b4992013-12-01 09:15:12 +01007180 newline = appctx->ctx.errors.bol;
Willy Tarreauf3764b72016-03-31 13:45:10 +02007181 newptr = dump_text_line(&trash, es->buf, global.tune.bufsize, es->len, &newline, appctx->ctx.errors.ptr);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01007182 if (newptr == appctx->ctx.errors.ptr)
Willy Tarreau61b34732009-10-03 23:49:35 +02007183 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01007184
Willy Tarreau2bb4a962014-11-28 11:11:05 +01007185 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01007186 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreaufe127932015-04-21 19:23:39 +02007187 si_applet_cant_put(si);
Willy Tarreau61b34732009-10-03 23:49:35 +02007188 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01007189 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01007190 appctx->ctx.errors.ptr = newptr;
7191 appctx->ctx.errors.bol = newline;
Willy Tarreau74808cb2009-03-04 15:53:18 +01007192 };
7193 next:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01007194 appctx->ctx.errors.bol = 0;
7195 appctx->ctx.errors.ptr = -1;
7196 appctx->ctx.errors.buf++;
7197 if (appctx->ctx.errors.buf > 1) {
7198 appctx->ctx.errors.buf = 0;
7199 appctx->ctx.errors.px = appctx->ctx.errors.px->next;
Willy Tarreau74808cb2009-03-04 15:53:18 +01007200 }
Willy Tarreauae795722016-02-16 11:27:28 +01007201 }
7202
7203 /* dump complete */
7204 return 1;
7205}
7206
7207/* This function dumps all environmnent variables to the buffer. It returns 0
7208 * if the output buffer is full and it needs to be called again, otherwise
7209 * non-zero. Dumps only one entry if st2 == STAT_ST_END.
7210 */
7211static int stats_dump_env_to_buffer(struct stream_interface *si)
7212{
7213 struct appctx *appctx = __objt_appctx(si->end);
7214
7215 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
7216 return 1;
7217
7218 chunk_reset(&trash);
7219
7220 /* we have two inner loops here, one for the proxy, the other one for
7221 * the buffer.
7222 */
7223 while (*appctx->ctx.env.var) {
7224 chunk_printf(&trash, "%s\n", *appctx->ctx.env.var);
7225
7226 if (bi_putchk(si_ic(si), &trash) == -1) {
7227 si_applet_cant_put(si);
7228 return 0;
7229 }
7230 if (appctx->st2 == STAT_ST_END)
7231 break;
7232 appctx->ctx.env.var++;
Willy Tarreau74808cb2009-03-04 15:53:18 +01007233 }
7234
7235 /* dump complete */
Willy Tarreau61b34732009-10-03 23:49:35 +02007236 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01007237}
7238
Willy Tarreaud5781202012-09-22 19:32:35 +02007239/* parse the "level" argument on the bind lines */
7240static int bind_parse_level(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
7241{
7242 if (!*args[cur_arg + 1]) {
7243 memprintf(err, "'%s' : missing level", args[cur_arg]);
7244 return ERR_ALERT | ERR_FATAL;
7245 }
7246
7247 if (!strcmp(args[cur_arg+1], "user"))
7248 conf->level = ACCESS_LVL_USER;
7249 else if (!strcmp(args[cur_arg+1], "operator"))
7250 conf->level = ACCESS_LVL_OPER;
7251 else if (!strcmp(args[cur_arg+1], "admin"))
7252 conf->level = ACCESS_LVL_ADMIN;
7253 else {
7254 memprintf(err, "'%s' only supports 'user', 'operator', and 'admin' (got '%s')",
7255 args[cur_arg], args[cur_arg+1]);
7256 return ERR_ALERT | ERR_FATAL;
7257 }
7258
7259 return 0;
7260}
7261
Willy Tarreau30576452015-04-13 13:50:30 +02007262struct applet http_stats_applet = {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007263 .obj_type = OBJ_TYPE_APPLET,
Willy Tarreaub24281b2011-02-13 13:16:36 +01007264 .name = "<STATS>", /* used for logging */
7265 .fct = http_stats_io_handler,
Aman Gupta9a13e842012-04-02 18:57:53 -07007266 .release = NULL,
Willy Tarreaub24281b2011-02-13 13:16:36 +01007267};
7268
Willy Tarreau30576452015-04-13 13:50:30 +02007269static struct applet cli_applet = {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007270 .obj_type = OBJ_TYPE_APPLET,
Willy Tarreaub24281b2011-02-13 13:16:36 +01007271 .name = "<CLI>", /* used for logging */
7272 .fct = cli_io_handler,
Willy Tarreau5f9a8772012-11-26 02:22:40 +01007273 .release = cli_release_handler,
Willy Tarreaub24281b2011-02-13 13:16:36 +01007274};
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01007275
Willy Tarreaudc13c112013-06-21 23:16:39 +02007276static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreau10522fd2008-07-09 20:12:41 +02007277 { CFG_GLOBAL, "stats", stats_parse_global },
7278 { 0, NULL, NULL },
7279}};
7280
Willy Tarreaud5781202012-09-22 19:32:35 +02007281static struct bind_kw_list bind_kws = { "STAT", { }, {
7282 { "level", bind_parse_level, 1 }, /* set the unix socket admin level */
7283 { NULL, NULL, 0 },
7284}};
7285
Willy Tarreau10522fd2008-07-09 20:12:41 +02007286__attribute__((constructor))
7287static void __dumpstats_module_init(void)
7288{
7289 cfg_register_keywords(&cfg_kws);
Willy Tarreaud5781202012-09-22 19:32:35 +02007290 bind_register_keywords(&bind_kws);
Willy Tarreau10522fd2008-07-09 20:12:41 +02007291}
7292
Willy Tarreau91861262007-10-17 17:06:05 +02007293/*
7294 * Local variables:
7295 * c-indent-level: 8
7296 * c-basic-offset: 8
7297 * End:
7298 */