blob: 6525939a5477e196efb6ecad9ff0656dd54cdd54 [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.
Chad Lavoie16669302016-10-04 16:10:40 -0400642 * Other non-printable chars are encoded "\xHH". Space, '\', and '=' are also escaped.
Simon Hormand9366582011-06-15 15:18:45 +0900643 * 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];
Chad Lavoie16669302016-10-04 16:10:40 -0400652 if (isprint(c) && isascii(c) && c != '\\' && c != ' ' && c != '=') {
Simon Hormand9366582011-06-15 15:18:45 +0900653 if (out->len > out->size - 1)
654 break;
655 out->str[out->len++] = c;
656 }
Chad Lavoie16669302016-10-04 16:10:40 -0400657 else if (c == '\t' || c == '\n' || c == '\r' || c == '\e' || c == '\\' || c == ' ' || c == '=') {
Simon Hormand9366582011-06-15 15:18:45 +0900658 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;
Chad Lavoie16669302016-10-04 16:10:40 -0400668 case '=': c = '='; break;
Simon Hormand9366582011-06-15 15:18:45 +0900669 }
670 out->str[out->len++] = c;
671 }
672 else {
673 if (out->len > out->size - 4)
674 break;
675 out->str[out->len++] = '\\';
676 out->str[out->len++] = 'x';
677 out->str[out->len++] = hextab[(c >> 4) & 0xF];
678 out->str[out->len++] = hextab[c & 0xF];
679 }
680 ptr++;
681 }
682
683 return ptr;
684}
685
686/* print a buffer in hexa.
687 * Print stopped if <bsize> is reached, or if no more place in the chunk.
688 */
689static int dump_binary(struct chunk *out, const char *buf, int bsize)
690{
691 unsigned char c;
692 int ptr = 0;
693
694 while (ptr < bsize) {
695 c = buf[ptr];
696
697 if (out->len > out->size - 2)
698 break;
699 out->str[out->len++] = hextab[(c >> 4) & 0xF];
700 out->str[out->len++] = hextab[c & 0xF];
701
702 ptr++;
703 }
704 return ptr;
705}
706
707/* Dump the status of a table to a stream interface's
708 * read buffer. It returns 0 if the output buffer is full
709 * and needs to be called again, otherwise non-zero.
710 */
711static int stats_dump_table_head_to_buffer(struct chunk *msg, struct stream_interface *si,
712 struct proxy *proxy, struct proxy *target)
713{
Willy Tarreau87b09662015-04-03 00:22:06 +0200714 struct stream *s = si_strm(si);
Simon Hormand9366582011-06-15 15:18:45 +0900715
Willy Tarreau77804732012-10-29 16:14:26 +0100716 chunk_appendf(msg, "# table: %s, type: %s, size:%d, used:%d\n",
Simon Hormand9366582011-06-15 15:18:45 +0900717 proxy->id, stktable_types[proxy->table.type].kw, proxy->table.size, proxy->table.current);
718
719 /* any other information should be dumped here */
720
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200721 if (target && strm_li(s)->bind_conf->level < ACCESS_LVL_OPER)
Willy Tarreau77804732012-10-29 16:14:26 +0100722 chunk_appendf(msg, "# contents not dumped due to insufficient privileges\n");
Simon Hormand9366582011-06-15 15:18:45 +0900723
Willy Tarreaubc18da12015-03-13 14:00:47 +0100724 if (bi_putchk(si_ic(si), msg) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +0200725 si_applet_cant_put(si);
Simon Hormand9366582011-06-15 15:18:45 +0900726 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +0100727 }
Simon Hormand9366582011-06-15 15:18:45 +0900728
729 return 1;
730}
731
732/* Dump the a table entry to a stream interface's
733 * read buffer. It returns 0 if the output buffer is full
734 * and needs to be called again, otherwise non-zero.
735 */
736static int stats_dump_table_entry_to_buffer(struct chunk *msg, struct stream_interface *si,
737 struct proxy *proxy, struct stksess *entry)
738{
739 int dt;
740
Willy Tarreau77804732012-10-29 16:14:26 +0100741 chunk_appendf(msg, "%p:", entry);
Simon Hormand9366582011-06-15 15:18:45 +0900742
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200743 if (proxy->table.type == SMP_T_IPV4) {
Simon Hormand9366582011-06-15 15:18:45 +0900744 char addr[INET_ADDRSTRLEN];
745 inet_ntop(AF_INET, (const void *)&entry->key.key, addr, sizeof(addr));
Willy Tarreau77804732012-10-29 16:14:26 +0100746 chunk_appendf(msg, " key=%s", addr);
Simon Hormand9366582011-06-15 15:18:45 +0900747 }
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200748 else if (proxy->table.type == SMP_T_IPV6) {
Simon Hormand9366582011-06-15 15:18:45 +0900749 char addr[INET6_ADDRSTRLEN];
750 inet_ntop(AF_INET6, (const void *)&entry->key.key, addr, sizeof(addr));
Willy Tarreau77804732012-10-29 16:14:26 +0100751 chunk_appendf(msg, " key=%s", addr);
Simon Hormand9366582011-06-15 15:18:45 +0900752 }
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200753 else if (proxy->table.type == SMP_T_SINT) {
Willy Tarreau77804732012-10-29 16:14:26 +0100754 chunk_appendf(msg, " key=%u", *(unsigned int *)entry->key.key);
Simon Hormand9366582011-06-15 15:18:45 +0900755 }
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200756 else if (proxy->table.type == SMP_T_STR) {
Willy Tarreau77804732012-10-29 16:14:26 +0100757 chunk_appendf(msg, " key=");
Simon Hormand9366582011-06-15 15:18:45 +0900758 dump_text(msg, (const char *)entry->key.key, proxy->table.key_size);
759 }
760 else {
Willy Tarreau77804732012-10-29 16:14:26 +0100761 chunk_appendf(msg, " key=");
Simon Hormand9366582011-06-15 15:18:45 +0900762 dump_binary(msg, (const char *)entry->key.key, proxy->table.key_size);
763 }
764
Willy Tarreau77804732012-10-29 16:14:26 +0100765 chunk_appendf(msg, " use=%d exp=%d", entry->ref_cnt - 1, tick_remain(now_ms, entry->expire));
Simon Hormand9366582011-06-15 15:18:45 +0900766
767 for (dt = 0; dt < STKTABLE_DATA_TYPES; dt++) {
768 void *ptr;
769
770 if (proxy->table.data_ofs[dt] == 0)
771 continue;
772 if (stktable_data_types[dt].arg_type == ARG_T_DELAY)
Willy Tarreau77804732012-10-29 16:14:26 +0100773 chunk_appendf(msg, " %s(%d)=", stktable_data_types[dt].name, proxy->table.data_arg[dt].u);
Simon Hormand9366582011-06-15 15:18:45 +0900774 else
Willy Tarreau77804732012-10-29 16:14:26 +0100775 chunk_appendf(msg, " %s=", stktable_data_types[dt].name);
Simon Hormand9366582011-06-15 15:18:45 +0900776
777 ptr = stktable_data_ptr(&proxy->table, entry, dt);
778 switch (stktable_data_types[dt].std_type) {
779 case STD_T_SINT:
Willy Tarreau77804732012-10-29 16:14:26 +0100780 chunk_appendf(msg, "%d", stktable_data_cast(ptr, std_t_sint));
Simon Hormand9366582011-06-15 15:18:45 +0900781 break;
782 case STD_T_UINT:
Willy Tarreau77804732012-10-29 16:14:26 +0100783 chunk_appendf(msg, "%u", stktable_data_cast(ptr, std_t_uint));
Simon Hormand9366582011-06-15 15:18:45 +0900784 break;
785 case STD_T_ULL:
Willy Tarreau77804732012-10-29 16:14:26 +0100786 chunk_appendf(msg, "%lld", stktable_data_cast(ptr, std_t_ull));
Simon Hormand9366582011-06-15 15:18:45 +0900787 break;
788 case STD_T_FRQP:
Willy Tarreau77804732012-10-29 16:14:26 +0100789 chunk_appendf(msg, "%d",
Simon Hormand9366582011-06-15 15:18:45 +0900790 read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
791 proxy->table.data_arg[dt].u));
792 break;
793 }
794 }
Willy Tarreau77804732012-10-29 16:14:26 +0100795 chunk_appendf(msg, "\n");
Simon Hormand9366582011-06-15 15:18:45 +0900796
Willy Tarreaubc18da12015-03-13 14:00:47 +0100797 if (bi_putchk(si_ic(si), msg) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +0200798 si_applet_cant_put(si);
Simon Hormand9366582011-06-15 15:18:45 +0900799 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +0100800 }
Simon Hormand9366582011-06-15 15:18:45 +0900801
802 return 1;
803}
804
Willy Tarreaudec98142012-06-06 23:37:08 +0200805static void stats_sock_table_key_request(struct stream_interface *si, char **args, int action)
Simon Horman121f3052011-06-15 15:18:46 +0900806{
Willy Tarreau87b09662015-04-03 00:22:06 +0200807 struct stream *s = si_strm(si);
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100808 struct appctx *appctx = __objt_appctx(si->end);
809 struct proxy *px = appctx->ctx.table.target;
Simon Horman121f3052011-06-15 15:18:46 +0900810 struct stksess *ts;
Simon Hormancec9a222011-06-15 15:18:51 +0900811 uint32_t uint32_key;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900812 unsigned char ip6_key[sizeof(struct in6_addr)];
Willy Tarreau654694e2012-06-07 01:03:16 +0200813 long long value;
814 int data_type;
Willy Tarreau47060b62013-08-01 21:11:42 +0200815 int cur_arg;
Willy Tarreau654694e2012-06-07 01:03:16 +0200816 void *ptr;
817 struct freq_ctr_period *frqp;
Simon Horman121f3052011-06-15 15:18:46 +0900818
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100819 appctx->st0 = STAT_CLI_OUTPUT;
Simon Horman121f3052011-06-15 15:18:46 +0900820
821 if (!*args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100822 appctx->ctx.cli.msg = "Key value expected\n";
823 appctx->st0 = STAT_CLI_PRINT;
Simon Horman121f3052011-06-15 15:18:46 +0900824 return;
825 }
826
Simon Hormanc5b89f62011-06-15 15:18:50 +0900827 switch (px->table.type) {
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200828 case SMP_T_IPV4:
Simon Hormancec9a222011-06-15 15:18:51 +0900829 uint32_key = htonl(inetaddr_host(args[4]));
Willy Tarreau07115412012-10-29 21:56:59 +0100830 static_table_key->key = &uint32_key;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900831 break;
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200832 case SMP_T_IPV6:
Simon Hormanc5b89f62011-06-15 15:18:50 +0900833 inet_pton(AF_INET6, args[4], ip6_key);
Willy Tarreau07115412012-10-29 21:56:59 +0100834 static_table_key->key = &ip6_key;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900835 break;
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200836 case SMP_T_SINT:
Simon Hormancec9a222011-06-15 15:18:51 +0900837 {
838 char *endptr;
839 unsigned long val;
840 errno = 0;
841 val = strtoul(args[4], &endptr, 10);
842 if ((errno == ERANGE && val == ULONG_MAX) ||
843 (errno != 0 && val == 0) || endptr == args[4] ||
844 val > 0xffffffff) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100845 appctx->ctx.cli.msg = "Invalid key\n";
846 appctx->st0 = STAT_CLI_PRINT;
Simon Hormancec9a222011-06-15 15:18:51 +0900847 return;
848 }
849 uint32_key = (uint32_t) val;
Willy Tarreau07115412012-10-29 21:56:59 +0100850 static_table_key->key = &uint32_key;
Simon Hormancec9a222011-06-15 15:18:51 +0900851 break;
852 }
853 break;
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200854 case SMP_T_STR:
Willy Tarreau07115412012-10-29 21:56:59 +0100855 static_table_key->key = args[4];
856 static_table_key->key_len = strlen(args[4]);
Simon Horman619e3cc2011-06-15 15:18:52 +0900857 break;
Simon Hormanc5b89f62011-06-15 15:18:50 +0900858 default:
Willy Tarreaudec98142012-06-06 23:37:08 +0200859 switch (action) {
860 case STAT_CLI_O_TAB:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100861 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 +0200862 break;
863 case STAT_CLI_O_CLR:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100864 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 +0200865 break;
866 default:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100867 appctx->ctx.cli.msg = "Unknown action\n";
Willy Tarreaudec98142012-06-06 23:37:08 +0200868 break;
869 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100870 appctx->st0 = STAT_CLI_PRINT;
Simon Horman121f3052011-06-15 15:18:46 +0900871 return;
872 }
873
874 /* check permissions */
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200875 if (strm_li(s)->bind_conf->level < ACCESS_LVL_OPER) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100876 appctx->ctx.cli.msg = stats_permission_denied_msg;
877 appctx->st0 = STAT_CLI_PRINT;
Simon Horman121f3052011-06-15 15:18:46 +0900878 return;
879 }
880
Willy Tarreau07115412012-10-29 21:56:59 +0100881 ts = stktable_lookup_key(&px->table, static_table_key);
Simon Horman17bce342011-06-15 15:18:47 +0900882
Willy Tarreaudec98142012-06-06 23:37:08 +0200883 switch (action) {
884 case STAT_CLI_O_TAB:
885 if (!ts)
886 return;
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100887 chunk_reset(&trash);
888 if (!stats_dump_table_head_to_buffer(&trash, si, px, px))
Simon Horman17bce342011-06-15 15:18:47 +0900889 return;
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100890 stats_dump_table_entry_to_buffer(&trash, si, px, ts);
Simon Horman121f3052011-06-15 15:18:46 +0900891 return;
Willy Tarreaudec98142012-06-06 23:37:08 +0200892
893 case STAT_CLI_O_CLR:
894 if (!ts)
895 return;
896 if (ts->ref_cnt) {
897 /* don't delete an entry which is currently referenced */
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100898 appctx->ctx.cli.msg = "Entry currently in use, cannot remove\n";
899 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaudec98142012-06-06 23:37:08 +0200900 return;
901 }
902 stksess_kill(&px->table, ts);
903 break;
Simon Horman17bce342011-06-15 15:18:47 +0900904
Willy Tarreau654694e2012-06-07 01:03:16 +0200905 case STAT_CLI_O_SET:
Willy Tarreau654694e2012-06-07 01:03:16 +0200906 if (ts)
907 stktable_touch(&px->table, ts, 1);
908 else {
Willy Tarreau07115412012-10-29 21:56:59 +0100909 ts = stksess_new(&px->table, static_table_key);
Willy Tarreau654694e2012-06-07 01:03:16 +0200910 if (!ts) {
911 /* don't delete an entry which is currently referenced */
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100912 appctx->ctx.cli.msg = "Unable to allocate a new entry\n";
913 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau654694e2012-06-07 01:03:16 +0200914 return;
915 }
916 stktable_store(&px->table, ts, 1);
917 }
918
Willy Tarreau47060b62013-08-01 21:11:42 +0200919 for (cur_arg = 5; *args[cur_arg]; cur_arg += 2) {
920 if (strncmp(args[cur_arg], "data.", 5) != 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100921 appctx->ctx.cli.msg = "\"data.<type>\" followed by a value expected\n";
922 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau47060b62013-08-01 21:11:42 +0200923 return;
924 }
925
926 data_type = stktable_get_data_type(args[cur_arg] + 5);
927 if (data_type < 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100928 appctx->ctx.cli.msg = "Unknown data type\n";
929 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau47060b62013-08-01 21:11:42 +0200930 return;
931 }
932
933 if (!px->table.data_ofs[data_type]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100934 appctx->ctx.cli.msg = "Data type not stored in this table\n";
935 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau47060b62013-08-01 21:11:42 +0200936 return;
937 }
938
939 if (!*args[cur_arg+1] || strl2llrc(args[cur_arg+1], strlen(args[cur_arg+1]), &value) != 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100940 appctx->ctx.cli.msg = "Require a valid integer value to store\n";
941 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau47060b62013-08-01 21:11:42 +0200942 return;
943 }
944
945 ptr = stktable_data_ptr(&px->table, ts, data_type);
946
947 switch (stktable_data_types[data_type].std_type) {
948 case STD_T_SINT:
949 stktable_data_cast(ptr, std_t_sint) = value;
950 break;
951 case STD_T_UINT:
952 stktable_data_cast(ptr, std_t_uint) = value;
953 break;
954 case STD_T_ULL:
955 stktable_data_cast(ptr, std_t_ull) = value;
956 break;
957 case STD_T_FRQP:
958 /* We set both the current and previous values. That way
959 * the reported frequency is stable during all the period
960 * then slowly fades out. This allows external tools to
961 * push measures without having to update them too often.
962 */
963 frqp = &stktable_data_cast(ptr, std_t_frqp);
964 frqp->curr_tick = now_ms;
965 frqp->prev_ctr = 0;
966 frqp->curr_ctr = value;
967 break;
968 }
Willy Tarreau654694e2012-06-07 01:03:16 +0200969 }
970 break;
971
Willy Tarreaudec98142012-06-06 23:37:08 +0200972 default:
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100973 appctx->ctx.cli.msg = "Unknown action\n";
974 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaudec98142012-06-06 23:37:08 +0200975 break;
Simon Horman121f3052011-06-15 15:18:46 +0900976 }
Simon Horman121f3052011-06-15 15:18:46 +0900977}
978
Willy Tarreau654694e2012-06-07 01:03:16 +0200979static void stats_sock_table_data_request(struct stream_interface *si, char **args, int action)
Simon Hormand5b9fd92011-06-15 15:18:48 +0900980{
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100981 struct appctx *appctx = __objt_appctx(si->end);
982
Willy Tarreau04b3a192013-04-13 09:41:37 +0200983 if (action != STAT_CLI_O_TAB && action != STAT_CLI_O_CLR) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100984 appctx->ctx.cli.msg = "content-based lookup is only supported with the \"show\" and \"clear\" actions";
985 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau654694e2012-06-07 01:03:16 +0200986 return;
987 }
988
Simon Hormand5b9fd92011-06-15 15:18:48 +0900989 /* condition on stored data value */
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100990 appctx->ctx.table.data_type = stktable_get_data_type(args[3] + 5);
991 if (appctx->ctx.table.data_type < 0) {
992 appctx->ctx.cli.msg = "Unknown data type\n";
993 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +0900994 return;
995 }
996
Willy Tarreau7b4b4992013-12-01 09:15:12 +0100997 if (!((struct proxy *)appctx->ctx.table.target)->table.data_ofs[appctx->ctx.table.data_type]) {
998 appctx->ctx.cli.msg = "Data type not stored in this table\n";
999 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +09001000 return;
1001 }
1002
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001003 appctx->ctx.table.data_op = get_std_op(args[4]);
1004 if (appctx->ctx.table.data_op < 0) {
1005 appctx->ctx.cli.msg = "Require and operator among \"eq\", \"ne\", \"le\", \"ge\", \"lt\", \"gt\"\n";
1006 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +09001007 return;
1008 }
1009
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001010 if (!*args[5] || strl2llrc(args[5], strlen(args[5]), &appctx->ctx.table.value) != 0) {
1011 appctx->ctx.cli.msg = "Require a valid integer value to compare against\n";
1012 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +09001013 return;
1014 }
1015}
1016
Willy Tarreaudec98142012-06-06 23:37:08 +02001017static void stats_sock_table_request(struct stream_interface *si, char **args, int action)
Simon Hormand5b9fd92011-06-15 15:18:48 +09001018{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001019 struct appctx *appctx = __objt_appctx(si->end);
1020
1021 appctx->ctx.table.data_type = -1;
1022 appctx->st2 = STAT_ST_INIT;
1023 appctx->ctx.table.target = NULL;
1024 appctx->ctx.table.proxy = NULL;
1025 appctx->ctx.table.entry = NULL;
1026 appctx->st0 = action;
Simon Hormand5b9fd92011-06-15 15:18:48 +09001027
1028 if (*args[2]) {
Willy Tarreaue2dc1fa2015-05-26 12:08:07 +02001029 appctx->ctx.table.target = proxy_tbl_by_name(args[2]);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001030 if (!appctx->ctx.table.target) {
1031 appctx->ctx.cli.msg = "No such table\n";
1032 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +09001033 return;
1034 }
1035 }
1036 else {
Willy Tarreaudec98142012-06-06 23:37:08 +02001037 if (action != STAT_CLI_O_TAB)
Simon Hormand5b9fd92011-06-15 15:18:48 +09001038 goto err_args;
1039 return;
1040 }
1041
1042 if (strcmp(args[3], "key") == 0)
Willy Tarreaudec98142012-06-06 23:37:08 +02001043 stats_sock_table_key_request(si, args, action);
Simon Hormanc88b8872011-06-15 15:18:49 +09001044 else if (strncmp(args[3], "data.", 5) == 0)
Willy Tarreau654694e2012-06-07 01:03:16 +02001045 stats_sock_table_data_request(si, args, action);
Simon Hormanc88b8872011-06-15 15:18:49 +09001046 else if (*args[3])
Simon Hormand5b9fd92011-06-15 15:18:48 +09001047 goto err_args;
1048
1049 return;
1050
1051err_args:
Willy Tarreaudec98142012-06-06 23:37:08 +02001052 switch (action) {
1053 case STAT_CLI_O_TAB:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001054 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 +02001055 break;
1056 case STAT_CLI_O_CLR:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001057 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 +02001058 break;
1059 default:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001060 appctx->ctx.cli.msg = "Unknown action\n";
Willy Tarreaudec98142012-06-06 23:37:08 +02001061 break;
1062 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001063 appctx->st0 = STAT_CLI_PRINT;
Simon Hormand5b9fd92011-06-15 15:18:48 +09001064}
1065
Willy Tarreau532a4502011-09-07 22:37:44 +02001066/* Expects to find a frontend named <arg> and returns it, otherwise displays various
Willy Tarreau87b09662015-04-03 00:22:06 +02001067 * adequate error messages and returns NULL. This function also expects the stream
Willy Tarreau532a4502011-09-07 22:37:44 +02001068 * level to be admin.
1069 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001070static struct proxy *expect_frontend_admin(struct stream *s, struct stream_interface *si, const char *arg)
Willy Tarreau532a4502011-09-07 22:37:44 +02001071{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001072 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau532a4502011-09-07 22:37:44 +02001073 struct proxy *px;
1074
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001075 if (strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001076 appctx->ctx.cli.msg = stats_permission_denied_msg;
1077 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001078 return NULL;
1079 }
1080
1081 if (!*arg) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001082 appctx->ctx.cli.msg = "A frontend name is expected.\n";
1083 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001084 return NULL;
1085 }
1086
Willy Tarreau9e0bb102015-05-26 11:24:42 +02001087 px = proxy_fe_by_name(arg);
Willy Tarreau532a4502011-09-07 22:37:44 +02001088 if (!px) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001089 appctx->ctx.cli.msg = "No such frontend.\n";
1090 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02001091 return NULL;
1092 }
1093 return px;
1094}
1095
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001096/* Expects to find a backend and a server in <arg> under the form <backend>/<server>,
1097 * and returns the pointer to the server. Otherwise, display adequate error messages
Willy Tarreau87b09662015-04-03 00:22:06 +02001098 * and returns NULL. This function also expects the stream level to be admin. Note:
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001099 * the <arg> is modified to remove the '/'.
1100 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001101static struct server *expect_server_admin(struct stream *s, struct stream_interface *si, char *arg)
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001102{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001103 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001104 struct proxy *px;
1105 struct server *sv;
1106 char *line;
1107
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001108 if (strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001109 appctx->ctx.cli.msg = stats_permission_denied_msg;
1110 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001111 return NULL;
1112 }
1113
1114 /* split "backend/server" and make <line> point to server */
1115 for (line = arg; *line; line++)
1116 if (*line == '/') {
1117 *line++ = '\0';
1118 break;
1119 }
1120
1121 if (!*line || !*arg) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001122 appctx->ctx.cli.msg = "Require 'backend/server'.\n";
1123 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001124 return NULL;
1125 }
1126
1127 if (!get_backend_server(arg, line, &px, &sv)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001128 appctx->ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
1129 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001130 return NULL;
1131 }
1132
1133 if (px->state == PR_STSTOPPED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001134 appctx->ctx.cli.msg = "Proxy is disabled.\n";
1135 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001136 return NULL;
1137 }
1138
1139 return sv;
1140}
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02001141
1142/* This function is used with TLS ticket keys management. It permits to browse
1143 * each reference. The variable <getnext> must contain the current node,
1144 * <end> point to the root node.
1145 */
1146#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
1147static inline
1148struct tls_keys_ref *tlskeys_list_get_next(struct tls_keys_ref *getnext, struct list *end)
1149{
1150 struct tls_keys_ref *ref = getnext;
1151
1152 while (1) {
1153
1154 /* Get next list entry. */
1155 ref = LIST_NEXT(&ref->list, struct tls_keys_ref *, list);
1156
1157 /* If the entry is the last of the list, return NULL. */
1158 if (&ref->list == end)
1159 return NULL;
1160
1161 return ref;
1162 }
1163}
1164
1165static inline
1166struct tls_keys_ref *tlskeys_ref_lookup_ref(const char *reference)
1167{
1168 int id;
1169 char *error;
1170
1171 /* If the reference starts by a '#', this is numeric id. */
1172 if (reference[0] == '#') {
1173 /* Try to convert the numeric id. If the conversion fails, the lookup fails. */
1174 id = strtol(reference + 1, &error, 10);
1175 if (*error != '\0')
1176 return NULL;
1177
1178 /* Perform the unique id lookup. */
1179 return tlskeys_ref_lookupid(id);
1180 }
1181
1182 /* Perform the string lookup. */
1183 return tlskeys_ref_lookup(reference);
1184}
1185#endif
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001186
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001187/* This function is used with map and acl management. It permits to browse
1188 * each reference. The variable <getnext> must contain the current node,
1189 * <end> point to the root node and the <flags> permit to filter required
1190 * nodes.
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001191 */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001192static inline
1193struct pat_ref *pat_list_get_next(struct pat_ref *getnext, struct list *end,
1194 unsigned int flags)
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001195{
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001196 struct pat_ref *ref = getnext;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001197
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001198 while (1) {
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001199
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001200 /* Get next list entry. */
1201 ref = LIST_NEXT(&ref->list, struct pat_ref *, list);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001202
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001203 /* If the entry is the last of the list, return NULL. */
1204 if (&ref->list == end)
1205 return NULL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001206
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001207 /* If the entry match the flag, return it. */
1208 if (ref->flags & flags)
1209 return ref;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001210 }
1211}
1212
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001213static inline
1214struct pat_ref *pat_ref_lookup_ref(const char *reference)
1215{
1216 int id;
1217 char *error;
1218
1219 /* If the reference starts by a '#', this is numeric id. */
1220 if (reference[0] == '#') {
1221 /* Try to convert the numeric id. If the conversion fails, the lookup fails. */
1222 id = strtol(reference + 1, &error, 10);
1223 if (*error != '\0')
1224 return NULL;
1225
1226 /* Perform the unique id lookup. */
1227 return pat_ref_lookupid(id);
1228 }
1229
1230 /* Perform the string lookup. */
1231 return pat_ref_lookup(reference);
1232}
1233
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001234/* This function is used with map and acl management. It permits to browse
1235 * each reference.
1236 */
1237static inline
1238struct pattern_expr *pat_expr_get_next(struct pattern_expr *getnext, struct list *end)
1239{
1240 struct pattern_expr *expr;
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01001241 expr = LIST_NEXT(&getnext->list, struct pattern_expr *, list);
1242 if (&expr->list == end)
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001243 return NULL;
1244 return expr;
1245}
1246
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02001247/* Processes the stats interpreter on the statistics socket. This function is
Willy Tarreauf5a885f2009-10-04 14:22:18 +02001248 * called from an applet running in a stream interface. The function returns 1
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001249 * if the request was understood, otherwise zero. It sets appctx->st0 to a value
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02001250 * designating the function which will have to process the request, which can
1251 * also be the print function to display the return message set into cli.msg.
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001252 */
Simon Horman9bd2c732011-06-15 15:18:44 +09001253static int stats_sock_parse_request(struct stream_interface *si, char *line)
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001254{
Willy Tarreau87b09662015-04-03 00:22:06 +02001255 struct stream *s = si_strm(si);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001256 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001257 char *args[MAX_STATS_ARGS + 1];
William Lallemand1e08cd82016-10-13 17:57:55 +02001258 struct cli_kw *kw;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001259 int arg;
Thierry FOURNIER48bcfda2013-12-10 18:54:58 +01001260 int i, j;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001261
1262 while (isspace((unsigned char)*line))
1263 line++;
1264
1265 arg = 0;
1266 args[arg] = line;
1267
1268 while (*line && arg < MAX_STATS_ARGS) {
Thierry FOURNIER48bcfda2013-12-10 18:54:58 +01001269 if (*line == '\\') {
1270 line++;
1271 if (*line == '\0')
1272 break;
1273 }
1274 else if (isspace((unsigned char)*line)) {
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001275 *line++ = '\0';
1276
1277 while (isspace((unsigned char)*line))
1278 line++;
1279
1280 args[++arg] = line;
1281 continue;
1282 }
1283
1284 line++;
1285 }
1286
1287 while (++arg <= MAX_STATS_ARGS)
1288 args[arg] = line;
1289
Thierry FOURNIER48bcfda2013-12-10 18:54:58 +01001290 /* remove \ */
1291 arg = 0;
1292 while (*args[arg] != '\0') {
1293 j = 0;
1294 for (i=0; args[arg][i] != '\0'; i++) {
1295 if (args[arg][i] == '\\')
1296 continue;
1297 args[arg][j] = args[arg][i];
1298 j++;
1299 }
1300 args[arg][j] = '\0';
1301 arg++;
1302 }
1303
Willy Tarreau6bcb95d2015-05-04 18:07:56 +02001304 appctx->ctx.stats.scope_str = 0;
1305 appctx->ctx.stats.scope_len = 0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001306 appctx->ctx.stats.flags = 0;
William Lallemand1e08cd82016-10-13 17:57:55 +02001307 if ((kw = cli_find_kw(args))) {
1308 if (kw->parse) {
Thierry FOURNIER / OZON.IO6a22dcb2016-11-12 10:51:33 +01001309 if (kw->parse(args, appctx, kw->private) == 0 && kw->io_handler) {
William Lallemand1e08cd82016-10-13 17:57:55 +02001310 appctx->st0 = STAT_CLI_O_CUSTOM;
1311 appctx->io_handler = kw->io_handler;
Thierry FOURNIER / OZON.IO6a22dcb2016-11-12 10:51:33 +01001312 appctx->io_release = kw->io_release;
William Lallemand1e08cd82016-10-13 17:57:55 +02001313 }
1314 }
1315 } else if (strcmp(args[0], "show") == 0) {
Baptiste Assmann9b6857e2015-09-18 14:49:12 +02001316 if (strcmp(args[1], "backend") == 0) {
Cyril Bonté6ca9e012016-05-06 12:18:49 +02001317 appctx->ctx.be.px = NULL;
Baptiste Assmann9b6857e2015-09-18 14:49:12 +02001318 appctx->st2 = STAT_ST_INIT;
1319 appctx->st0 = STAT_CLI_O_BACKEND;
1320 }
Willy Tarreauae795722016-02-16 11:27:28 +01001321 else if (strcmp(args[1], "env") == 0) {
1322 extern char **environ;
1323
1324 if (strm_li(s)->bind_conf->level < ACCESS_LVL_OPER) {
1325 appctx->ctx.cli.msg = stats_permission_denied_msg;
1326 appctx->st0 = STAT_CLI_PRINT;
1327 return 1;
1328 }
1329 appctx->ctx.env.var = environ;
1330 appctx->st2 = STAT_ST_INIT;
1331 appctx->st0 = STAT_CLI_O_ENV; // stats_dump_env_to_buffer
1332
1333 if (*args[2]) {
1334 int len = strlen(args[2]);
1335
1336 for (; *appctx->ctx.env.var; appctx->ctx.env.var++) {
1337 if (strncmp(*appctx->ctx.env.var, args[2], len) == 0 &&
1338 (*appctx->ctx.env.var)[len] == '=')
1339 break;
1340 }
1341 if (!*appctx->ctx.env.var) {
1342 appctx->ctx.cli.msg = "Variable not found\n";
1343 appctx->st0 = STAT_CLI_PRINT;
1344 return 1;
1345 }
1346 appctx->st2 = STAT_ST_END;
1347 }
1348 }
1349 else if (strcmp(args[1], "stat") == 0) {
Baptiste Assmann3863f972015-05-17 00:33:24 +02001350 if (strcmp(args[2], "resolvers") == 0) {
1351 struct dns_resolvers *presolvers;
1352
Andrew Hayworth68d05342015-10-02 20:33:01 +00001353 if (*args[3]) {
1354 appctx->ctx.resolvers.ptr = NULL;
1355 list_for_each_entry(presolvers, &dns_resolvers, list) {
1356 if (strcmp(presolvers->id, args[3]) == 0) {
1357 appctx->ctx.resolvers.ptr = presolvers;
1358 break;
1359 }
Baptiste Assmann3863f972015-05-17 00:33:24 +02001360 }
Andrew Hayworth68d05342015-10-02 20:33:01 +00001361 if (appctx->ctx.resolvers.ptr == NULL) {
1362 appctx->ctx.cli.msg = "Can't find that resolvers section\n";
1363 appctx->st0 = STAT_CLI_PRINT;
1364 return 1;
1365 }
Baptiste Assmann3863f972015-05-17 00:33:24 +02001366 }
1367
1368 appctx->st2 = STAT_ST_INIT;
1369 appctx->st0 = STAT_CLI_O_RESOLVERS;
1370 return 1;
1371 }
1372 else if (*args[2] && *args[3] && *args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001373 appctx->ctx.stats.flags |= STAT_BOUND;
1374 appctx->ctx.stats.iid = atoi(args[2]);
1375 appctx->ctx.stats.type = atoi(args[3]);
1376 appctx->ctx.stats.sid = atoi(args[4]);
Willy Tarreau1e62df92016-01-11 18:57:53 +01001377 if (strcmp(args[5], "typed") == 0)
1378 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001379 }
Willy Tarreau1e62df92016-01-11 18:57:53 +01001380 else if (strcmp(args[2], "typed") == 0)
1381 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001382
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001383 appctx->st2 = STAT_ST_INIT;
1384 appctx->st0 = STAT_CLI_O_STAT; // stats_dump_stat_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001385 }
1386 else if (strcmp(args[1], "info") == 0) {
Willy Tarreaucb809122016-01-11 20:08:42 +01001387 if (strcmp(args[2], "typed") == 0)
1388 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001389 appctx->st2 = STAT_ST_INIT;
1390 appctx->st0 = STAT_CLI_O_INFO; // stats_dump_info_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001391 }
Baptiste Assmann28289462015-07-03 08:01:20 +02001392 else if (strcmp(args[1], "servers") == 0 && strcmp(args[2], "state") == 0) {
Cyril Bonté76a99782016-05-06 12:18:48 +02001393 appctx->ctx.server_state.iid = 0;
1394 appctx->ctx.server_state.px = NULL;
1395 appctx->ctx.server_state.sv = NULL;
Baptiste Assmann28289462015-07-03 08:01:20 +02001396
1397 /* check if a backend name has been provided */
1398 if (*args[3]) {
1399 /* read server state from local file */
Cyril Bonté76a99782016-05-06 12:18:48 +02001400 appctx->ctx.server_state.px = proxy_be_by_name(args[3]);
Baptiste Assmann28289462015-07-03 08:01:20 +02001401
Cyril Bonté76a99782016-05-06 12:18:48 +02001402 if (!appctx->ctx.server_state.px) {
Baptiste Assmann28289462015-07-03 08:01:20 +02001403 appctx->ctx.cli.msg = "Can't find backend.\n";
1404 appctx->st0 = STAT_CLI_PRINT;
1405 return 1;
1406 }
Cyril Bonté76a99782016-05-06 12:18:48 +02001407 appctx->ctx.server_state.iid = appctx->ctx.server_state.px->uuid;
Baptiste Assmann28289462015-07-03 08:01:20 +02001408 }
1409 appctx->st2 = STAT_ST_INIT;
1410 appctx->st0 = STAT_CLI_O_SERVERS_STATE; // stats_dump_servers_state_to_buffer
1411 return 1;
1412 }
Willy Tarreau12833bb2014-01-28 16:49:56 +01001413 else if (strcmp(args[1], "pools") == 0) {
1414 appctx->st2 = STAT_ST_INIT;
1415 appctx->st0 = STAT_CLI_O_POOLS; // stats_dump_pools_to_buffer
1416 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001417 else if (strcmp(args[1], "sess") == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001418 appctx->st2 = STAT_ST_INIT;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001419 if (strm_li(s)->bind_conf->level < ACCESS_LVL_OPER) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001420 appctx->ctx.cli.msg = stats_permission_denied_msg;
1421 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +02001422 return 1;
1423 }
Willy Tarreau76153662012-11-26 01:16:39 +01001424 if (*args[2] && strcmp(args[2], "all") == 0)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001425 appctx->ctx.sess.target = (void *)-1;
Willy Tarreau76153662012-11-26 01:16:39 +01001426 else if (*args[2])
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001427 appctx->ctx.sess.target = (void *)strtoul(args[2], NULL, 0);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01001428 else
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001429 appctx->ctx.sess.target = NULL;
Willy Tarreau87b09662015-04-03 00:22:06 +02001430 appctx->ctx.sess.section = 0; /* start with stream status */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001431 appctx->ctx.sess.pos = 0;
1432 appctx->st0 = STAT_CLI_O_SESS; // stats_dump_sess_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001433 }
1434 else if (strcmp(args[1], "errors") == 0) {
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001435 if (strm_li(s)->bind_conf->level < ACCESS_LVL_OPER) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001436 appctx->ctx.cli.msg = stats_permission_denied_msg;
1437 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +02001438 return 1;
1439 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001440 if (*args[2])
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001441 appctx->ctx.errors.iid = atoi(args[2]);
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001442 else
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001443 appctx->ctx.errors.iid = -1;
1444 appctx->ctx.errors.px = NULL;
1445 appctx->st2 = STAT_ST_INIT;
1446 appctx->st0 = STAT_CLI_O_ERR; // stats_dump_errors_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001447 }
Willy Tarreau69f58c82010-07-12 17:55:33 +02001448 else if (strcmp(args[1], "table") == 0) {
Willy Tarreaudec98142012-06-06 23:37:08 +02001449 stats_sock_table_request(si, args, STAT_CLI_O_TAB);
Willy Tarreau69f58c82010-07-12 17:55:33 +02001450 }
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02001451 else if (strcmp(args[1], "tls-keys") == 0) {
1452#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
William Lallemandcf9e7882016-06-14 17:45:18 +02001453 appctx->ctx.tlskeys.dump_all = 0;
William Lallemand1d0b36a2016-05-20 17:40:26 +02001454 /* no parameter, shows only file list */
1455 if (!*args[2]) {
William Lallemandcf9e7882016-06-14 17:45:18 +02001456 appctx->ctx.tlskeys.dump_all = 1;
William Lallemand1d0b36a2016-05-20 17:40:26 +02001457 appctx->st2 = STAT_ST_INIT;
1458 appctx->st0 = STAT_CLI_O_TLSK;
1459 return 1;
1460 }
1461
1462 if (args[2][0] == '*') {
1463 /* list every TLS ticket keys */
1464 appctx->ctx.tlskeys.ref = NULL;
William Lallemandcf9e7882016-06-14 17:45:18 +02001465 appctx->ctx.tlskeys.dump_all = 1;
William Lallemand1d0b36a2016-05-20 17:40:26 +02001466 } else {
1467 appctx->ctx.tlskeys.ref = tlskeys_ref_lookup_ref(args[2]);
1468 if(!appctx->ctx.tlskeys.ref) {
1469 appctx->ctx.cli.msg = "'show tls-keys' unable to locate referenced filename\n";
1470 appctx->st0 = STAT_CLI_PRINT;
1471 return 1;
1472 }
1473 }
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02001474 appctx->st2 = STAT_ST_INIT;
William Lallemand1d0b36a2016-05-20 17:40:26 +02001475 appctx->st0 = STAT_CLI_O_TLSK_ENT;
1476
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02001477#else
1478 appctx->ctx.cli.msg = "HAProxy was compiled against a version of OpenSSL "
1479 "that doesn't support specifying TLS ticket keys\n";
1480 appctx->st0 = STAT_CLI_PRINT;
1481#endif
1482 return 1;
1483 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001484 else if (strcmp(args[1], "map") == 0 ||
1485 strcmp(args[1], "acl") == 0) {
1486
1487 /* Set ACL or MAP flags. */
1488 if (args[1][0] == 'm')
1489 appctx->ctx.map.display_flags = PAT_REF_MAP;
1490 else
1491 appctx->ctx.map.display_flags = PAT_REF_ACL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001492
Cyril Bontédb98eb32016-05-06 12:18:50 +02001493 /* no parameter: display all map available */
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001494 if (!*args[2]) {
1495 appctx->st2 = STAT_ST_INIT;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001496 appctx->st0 = STAT_CLI_O_PATS;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001497 return 1;
1498 }
1499
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001500 /* lookup into the refs and check the map flag */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001501 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001502 if (!appctx->ctx.map.ref ||
1503 !(appctx->ctx.map.ref->flags & appctx->ctx.map.display_flags)) {
1504 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001505 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001506 else
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001507 appctx->ctx.cli.msg = "Unknown ACL identifier. Please use #<id> or <file>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001508 appctx->st0 = STAT_CLI_PRINT;
1509 return 1;
1510 }
1511 appctx->st2 = STAT_ST_INIT;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001512 appctx->st0 = STAT_CLI_O_PAT;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001513 }
Aman Guptaceafb4a2012-04-02 18:57:54 -07001514 else { /* neither "stat" nor "info" nor "sess" nor "errors" nor "table" */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001515 return 0;
1516 }
1517 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001518 else if (strcmp(args[0], "clear") == 0) {
1519 if (strcmp(args[1], "counters") == 0) {
1520 struct proxy *px;
1521 struct server *sv;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001522 struct listener *li;
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001523 int clrall = 0;
1524
1525 if (strcmp(args[2], "all") == 0)
1526 clrall = 1;
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001527
Willy Tarreau6162db22009-10-10 17:13:00 +02001528 /* check permissions */
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001529 if (strm_li(s)->bind_conf->level < ACCESS_LVL_OPER ||
1530 (clrall && strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001531 appctx->ctx.cli.msg = stats_permission_denied_msg;
1532 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +02001533 return 1;
1534 }
1535
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001536 for (px = proxy; px; px = px->next) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001537 if (clrall) {
1538 memset(&px->be_counters, 0, sizeof(px->be_counters));
1539 memset(&px->fe_counters, 0, sizeof(px->fe_counters));
1540 }
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001541 else {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001542 px->be_counters.conn_max = 0;
1543 px->be_counters.p.http.rps_max = 0;
1544 px->be_counters.sps_max = 0;
1545 px->be_counters.cps_max = 0;
1546 px->be_counters.nbpend_max = 0;
1547
1548 px->fe_counters.conn_max = 0;
1549 px->fe_counters.p.http.rps_max = 0;
1550 px->fe_counters.sps_max = 0;
1551 px->fe_counters.cps_max = 0;
1552 px->fe_counters.nbpend_max = 0;
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001553 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001554
1555 for (sv = px->srv; sv; sv = sv->next)
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001556 if (clrall)
1557 memset(&sv->counters, 0, sizeof(sv->counters));
1558 else {
1559 sv->counters.cur_sess_max = 0;
1560 sv->counters.nbpend_max = 0;
1561 sv->counters.sps_max = 0;
1562 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001563
Willy Tarreaua7944ad2012-09-26 21:03:11 +02001564 list_for_each_entry(li, &px->conf.listeners, by_fe)
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001565 if (li->counters) {
1566 if (clrall)
1567 memset(li->counters, 0, sizeof(*li->counters));
1568 else
1569 li->counters->conn_max = 0;
1570 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001571 }
1572
Willy Tarreau81c25d02011-09-07 15:17:21 +02001573 global.cps_max = 0;
Willy Tarreau93e7c002013-10-07 18:51:07 +02001574 global.sps_max = 0;
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001575 return 1;
1576 }
Willy Tarreau88ee3972010-07-13 13:48:00 +02001577 else if (strcmp(args[1], "table") == 0) {
Willy Tarreaudec98142012-06-06 23:37:08 +02001578 stats_sock_table_request(si, args, STAT_CLI_O_CLR);
Willy Tarreau88ee3972010-07-13 13:48:00 +02001579 /* end of processing */
1580 return 1;
1581 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001582 else if (strcmp(args[1], "map") == 0 || strcmp(args[1], "acl") == 0) {
1583 /* Set ACL or MAP flags. */
1584 if (args[1][0] == 'm')
1585 appctx->ctx.map.display_flags = PAT_REF_MAP;
1586 else
1587 appctx->ctx.map.display_flags = PAT_REF_ACL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001588
1589 /* no parameter */
1590 if (!*args[2]) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001591 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
1592 appctx->ctx.cli.msg = "Missing map identifier.\n";
1593 else
1594 appctx->ctx.cli.msg = "Missing ACL identifier.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001595 appctx->st0 = STAT_CLI_PRINT;
1596 return 1;
1597 }
1598
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001599 /* lookup into the refs and check the map flag */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001600 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001601 if (!appctx->ctx.map.ref ||
1602 !(appctx->ctx.map.ref->flags & appctx->ctx.map.display_flags)) {
1603 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001604 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001605 else
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001606 appctx->ctx.cli.msg = "Unknown ACL identifier. Please use #<id> or <file>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001607 appctx->st0 = STAT_CLI_PRINT;
1608 return 1;
1609 }
1610
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001611 /* Clear all. */
1612 pat_ref_prune(appctx->ctx.map.ref);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001613
1614 /* return response */
Thierry FOURNIER07e78c52014-12-18 15:28:01 +01001615 appctx->st0 = STAT_CLI_PROMPT;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001616 return 1;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001617 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001618 else {
Willy Tarreau88ee3972010-07-13 13:48:00 +02001619 /* unknown "clear" argument */
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001620 return 0;
1621 }
1622 }
Willy Tarreau38338fa2009-10-10 18:37:29 +02001623 else if (strcmp(args[0], "get") == 0) {
1624 if (strcmp(args[1], "weight") == 0) {
1625 struct proxy *px;
1626 struct server *sv;
1627
1628 /* split "backend/server" and make <line> point to server */
1629 for (line = args[2]; *line; line++)
1630 if (*line == '/') {
1631 *line++ = '\0';
1632 break;
1633 }
1634
1635 if (!*line) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001636 appctx->ctx.cli.msg = "Require 'backend/server'.\n";
1637 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau38338fa2009-10-10 18:37:29 +02001638 return 1;
1639 }
1640
1641 if (!get_backend_server(args[2], line, &px, &sv)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001642 appctx->ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
1643 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau38338fa2009-10-10 18:37:29 +02001644 return 1;
1645 }
1646
1647 /* return server's effective weight at the moment */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001648 snprintf(trash.str, trash.size, "%d (initial %d)\n", sv->uweight, sv->iweight);
Willy Tarreaubc18da12015-03-13 14:00:47 +01001649 if (bi_putstr(si_ic(si), trash.str) == -1)
Willy Tarreaufe127932015-04-21 19:23:39 +02001650 si_applet_cant_put(si);
Willy Tarreaubc18da12015-03-13 14:00:47 +01001651
Willy Tarreau38338fa2009-10-10 18:37:29 +02001652 return 1;
1653 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001654 else if (strcmp(args[1], "map") == 0 || strcmp(args[1], "acl") == 0) {
1655 /* Set flags. */
1656 if (args[1][0] == 'm')
1657 appctx->ctx.map.display_flags = PAT_REF_MAP;
1658 else
1659 appctx->ctx.map.display_flags = PAT_REF_ACL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001660
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001661 /* No parameter. */
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001662 if (!*args[2] || !*args[3]) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001663 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
1664 appctx->ctx.cli.msg = "Missing map identifier and/or key.\n";
1665 else
1666 appctx->ctx.cli.msg = "Missing ACL identifier and/or key.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001667 appctx->st0 = STAT_CLI_PRINT;
1668 return 1;
1669 }
1670
1671 /* lookup into the maps */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001672 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001673 if (!appctx->ctx.map.ref) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001674 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001675 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001676 else
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001677 appctx->ctx.cli.msg = "Unknown ACL identifier. Please use #<id> or <file>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001678 appctx->st0 = STAT_CLI_PRINT;
1679 return 1;
1680 }
1681
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001682 /* copy input string. The string must be allocated because
1683 * it may be used over multiple iterations. It's released
1684 * at the end and upon abort anyway.
1685 */
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001686 appctx->ctx.map.chunk.len = strlen(args[3]);
1687 appctx->ctx.map.chunk.size = appctx->ctx.map.chunk.len + 1;
1688 appctx->ctx.map.chunk.str = strdup(args[3]);
1689 if (!appctx->ctx.map.chunk.str) {
1690 appctx->ctx.cli.msg = "Out of memory error.\n";
1691 appctx->st0 = STAT_CLI_PRINT;
1692 return 1;
1693 }
1694
1695 /* prepare response */
1696 appctx->st2 = STAT_ST_INIT;
1697 appctx->st0 = STAT_CLI_O_MLOOK;
1698 }
Willy Tarreau38338fa2009-10-10 18:37:29 +02001699 else { /* not "get weight" */
1700 return 0;
1701 }
1702 }
Willy Tarreau4483d432009-10-10 19:30:08 +02001703 else if (strcmp(args[0], "set") == 0) {
1704 if (strcmp(args[1], "weight") == 0) {
Willy Tarreau4483d432009-10-10 19:30:08 +02001705 struct server *sv;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001706 const char *warning;
Willy Tarreau4483d432009-10-10 19:30:08 +02001707
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001708 sv = expect_server_admin(s, si, args[2]);
1709 if (!sv)
Willy Tarreau4483d432009-10-10 19:30:08 +02001710 return 1;
Willy Tarreau4483d432009-10-10 19:30:08 +02001711
Simon Horman7d09b9a2013-02-12 10:45:51 +09001712 warning = server_parse_weight_change_request(sv, args[3]);
1713 if (warning) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001714 appctx->ctx.cli.msg = warning;
1715 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +02001716 }
Willy Tarreau4483d432009-10-10 19:30:08 +02001717 return 1;
1718 }
Willy Tarreau2a4b70f2014-05-22 18:42:35 +02001719 else if (strcmp(args[1], "server") == 0) {
1720 struct server *sv;
1721 const char *warning;
1722
1723 sv = expect_server_admin(s, si, args[2]);
1724 if (!sv)
1725 return 1;
1726
1727 if (strcmp(args[3], "weight") == 0) {
1728 warning = server_parse_weight_change_request(sv, args[4]);
1729 if (warning) {
1730 appctx->ctx.cli.msg = warning;
1731 appctx->st0 = STAT_CLI_PRINT;
1732 }
1733 }
1734 else if (strcmp(args[3], "state") == 0) {
1735 if (strcmp(args[4], "ready") == 0)
1736 srv_adm_set_ready(sv);
1737 else if (strcmp(args[4], "drain") == 0)
1738 srv_adm_set_drain(sv);
1739 else if (strcmp(args[4], "maint") == 0)
1740 srv_adm_set_maint(sv);
1741 else {
1742 appctx->ctx.cli.msg = "'set server <srv> state' expects 'ready', 'drain' and 'maint'.\n";
1743 appctx->st0 = STAT_CLI_PRINT;
1744 }
1745 }
1746 else if (strcmp(args[3], "health") == 0) {
1747 if (sv->track) {
1748 appctx->ctx.cli.msg = "cannot change health on a tracking server.\n";
1749 appctx->st0 = STAT_CLI_PRINT;
1750 }
1751 else if (strcmp(args[4], "up") == 0) {
1752 sv->check.health = sv->check.rise + sv->check.fall - 1;
1753 srv_set_running(sv, "changed from CLI");
1754 }
1755 else if (strcmp(args[4], "stopping") == 0) {
1756 sv->check.health = sv->check.rise + sv->check.fall - 1;
1757 srv_set_stopping(sv, "changed from CLI");
1758 }
1759 else if (strcmp(args[4], "down") == 0) {
1760 sv->check.health = 0;
1761 srv_set_stopped(sv, "changed from CLI");
1762 }
1763 else {
1764 appctx->ctx.cli.msg = "'set server <srv> health' expects 'up', 'stopping', or 'down'.\n";
1765 appctx->st0 = STAT_CLI_PRINT;
1766 }
1767 }
1768 else if (strcmp(args[3], "agent") == 0) {
1769 if (!(sv->agent.state & CHK_ST_ENABLED)) {
1770 appctx->ctx.cli.msg = "agent checks are not enabled on this server.\n";
1771 appctx->st0 = STAT_CLI_PRINT;
1772 }
1773 else if (strcmp(args[4], "up") == 0) {
1774 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
1775 srv_set_running(sv, "changed from CLI");
1776 }
1777 else if (strcmp(args[4], "down") == 0) {
1778 sv->agent.health = 0;
1779 srv_set_stopped(sv, "changed from CLI");
1780 }
1781 else {
1782 appctx->ctx.cli.msg = "'set server <srv> agent' expects 'up' or 'down'.\n";
1783 appctx->st0 = STAT_CLI_PRINT;
1784 }
1785 }
Baptiste Assmann50946562016-08-31 23:26:29 +02001786 else if (strcmp(args[3], "check-port") == 0) {
1787 int i = 0;
1788 if (strl2irc(args[4], strlen(args[4]), &i) != 0) {
1789 appctx->ctx.cli.msg = "'set server <srv> check-port' expects an integer as argument.\n";
1790 appctx->st0 = STAT_CLI_PRINT;
1791 }
1792 if ((i < 0) || (i > 65535)) {
1793 appctx->ctx.cli.msg = "provided port is not valid.\n";
1794 appctx->st0 = STAT_CLI_PRINT;
1795 }
1796 /* prevent the update of port to 0 if MAPPORTS are in use */
1797 if ((sv->flags & SRV_F_MAPPORTS) && (i == 0)) {
1798 appctx->ctx.cli.msg = "can't unset 'port' since MAPPORTS is in use.\n";
1799 appctx->st0 = STAT_CLI_PRINT;
1800 return 1;
1801 }
1802 sv->check.port = i;
1803 appctx->ctx.cli.msg = "health check port updated.\n";
1804 appctx->st0 = STAT_CLI_PRINT;
1805 }
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001806 else if (strcmp(args[3], "addr") == 0) {
Baptiste Assmann3749ebf2016-08-03 22:34:12 +02001807 char *addr = NULL;
1808 char *port = NULL;
1809 if (strlen(args[4]) == 0) {
Willy Tarreaua33ec242016-11-04 18:21:32 +01001810 appctx->ctx.cli.msg = "set server <b>/<s> addr requires an address and optionally a port.\n";
Baptiste Assmann3749ebf2016-08-03 22:34:12 +02001811 appctx->st0 = STAT_CLI_PRINT;
1812 return 1;
1813 }
1814 else {
1815 addr = args[4];
1816 }
1817 if (strcmp(args[5], "port") == 0) {
1818 port = args[6];
1819 }
1820 warning = update_server_addr_port(sv, addr, port, "stats socket command");
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001821 if (warning) {
1822 appctx->ctx.cli.msg = warning;
1823 appctx->st0 = STAT_CLI_PRINT;
1824 }
Willy Tarreaua33ec242016-11-04 18:21:32 +01001825 srv_clr_admin_flag(sv, SRV_ADMF_RMAINT);
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001826 }
Willy Tarreau2a4b70f2014-05-22 18:42:35 +02001827 else {
Baptiste Assmann50946562016-08-31 23:26:29 +02001828 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 +02001829 appctx->st0 = STAT_CLI_PRINT;
1830 }
1831 return 1;
1832 }
Willy Tarreau7aabd112010-01-26 10:59:06 +01001833 else if (strcmp(args[1], "timeout") == 0) {
1834 if (strcmp(args[2], "cli") == 0) {
1835 unsigned timeout;
1836 const char *res;
1837
1838 if (!*args[3]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001839 appctx->ctx.cli.msg = "Expects an integer value.\n";
1840 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001841 return 1;
1842 }
1843
1844 res = parse_time_err(args[3], &timeout, TIME_UNIT_S);
1845 if (res || timeout < 1) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001846 appctx->ctx.cli.msg = "Invalid timeout value.\n";
1847 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001848 return 1;
1849 }
1850
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001851 s->req.rto = s->res.wto = 1 + MS_TO_TICKS(timeout*1000);
Willy Tarreau2b5e6312016-11-24 15:35:16 +01001852 task_wakeup(s->task, TASK_WOKEN_MSG); // recompute timeouts
Willy Tarreau7aabd112010-01-26 10:59:06 +01001853 return 1;
1854 }
1855 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001856 appctx->ctx.cli.msg = "'set timeout' only supports 'cli'.\n";
1857 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001858 return 1;
1859 }
1860 }
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001861 else if (strcmp(args[1], "maxconn") == 0) {
1862 if (strcmp(args[2], "frontend") == 0) {
1863 struct proxy *px;
1864 struct listener *l;
1865 int v;
1866
Willy Tarreau532a4502011-09-07 22:37:44 +02001867 px = expect_frontend_admin(s, si, args[3]);
1868 if (!px)
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001869 return 1;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001870
1871 if (!*args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001872 appctx->ctx.cli.msg = "Integer value expected.\n";
1873 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001874 return 1;
1875 }
1876
1877 v = atoi(args[4]);
Willy Tarreau3c7a79d2012-09-26 21:07:15 +02001878 if (v < 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001879 appctx->ctx.cli.msg = "Value out of range.\n";
1880 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001881 return 1;
1882 }
1883
1884 /* OK, the value is fine, so we assign it to the proxy and to all of
1885 * its listeners. The blocked ones will be dequeued.
1886 */
1887 px->maxconn = v;
Willy Tarreaua7944ad2012-09-26 21:03:11 +02001888 list_for_each_entry(l, &px->conf.listeners, by_fe) {
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001889 l->maxconn = v;
1890 if (l->state == LI_FULL)
1891 resume_listener(l);
1892 }
1893
Willy Tarreau74a5a982016-11-24 15:25:39 +01001894 if (px->maxconn > px->feconn && !LIST_ISEMPTY(&px->listener_queue))
1895 dequeue_all_listeners(&px->listener_queue);
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001896
1897 return 1;
1898 }
Andrew Hayworthedb93a72015-10-27 21:46:25 +00001899 else if (strcmp(args[2], "server") == 0) {
1900 struct server *sv;
Nenad Merdanovic5c3ed342016-04-24 23:10:07 +02001901 const char *warning;
Andrew Hayworthedb93a72015-10-27 21:46:25 +00001902
1903 sv = expect_server_admin(s, si, args[3]);
1904 if (!sv)
1905 return 1;
1906
Nenad Merdanovic5c3ed342016-04-24 23:10:07 +02001907 warning = server_parse_maxconn_change_request(sv, args[4]);
1908 if (warning) {
1909 appctx->ctx.cli.msg = warning;
Andrew Hayworthedb93a72015-10-27 21:46:25 +00001910 appctx->st0 = STAT_CLI_PRINT;
Andrew Hayworthedb93a72015-10-27 21:46:25 +00001911 }
1912
Andrew Hayworthedb93a72015-10-27 21:46:25 +00001913 return 1;
1914 }
Willy Tarreau91886b62011-09-07 14:38:31 +02001915 else if (strcmp(args[2], "global") == 0) {
1916 int v;
1917
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001918 if (strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001919 appctx->ctx.cli.msg = stats_permission_denied_msg;
1920 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau91886b62011-09-07 14:38:31 +02001921 return 1;
1922 }
1923
1924 if (!*args[3]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001925 appctx->ctx.cli.msg = "Expects an integer value.\n";
1926 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau91886b62011-09-07 14:38:31 +02001927 return 1;
1928 }
1929
1930 v = atoi(args[3]);
1931 if (v > global.hardmaxconn) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001932 appctx->ctx.cli.msg = "Value out of range.\n";
1933 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau91886b62011-09-07 14:38:31 +02001934 return 1;
1935 }
1936
1937 /* check for unlimited values */
1938 if (v <= 0)
1939 v = global.hardmaxconn;
1940
1941 global.maxconn = v;
1942
1943 /* Dequeues all of the listeners waiting for a resource */
1944 if (!LIST_ISEMPTY(&global_listener_queue))
1945 dequeue_all_listeners(&global_listener_queue);
1946
1947 return 1;
1948 }
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001949 else {
Andrew Hayworthedb93a72015-10-27 21:46:25 +00001950 appctx->ctx.cli.msg = "'set maxconn' only supports 'frontend', 'server', and 'global'.\n";
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001951 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001952 return 1;
1953 }
1954 }
Willy Tarreauf5b22872011-09-07 16:13:44 +02001955 else if (strcmp(args[1], "rate-limit") == 0) {
1956 if (strcmp(args[2], "connections") == 0) {
1957 if (strcmp(args[3], "global") == 0) {
1958 int v;
1959
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001960 if (strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001961 appctx->ctx.cli.msg = stats_permission_denied_msg;
1962 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001963 return 1;
1964 }
1965
1966 if (!*args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001967 appctx->ctx.cli.msg = "Expects an integer value.\n";
1968 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001969 return 1;
1970 }
1971
1972 v = atoi(args[4]);
1973 if (v < 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001974 appctx->ctx.cli.msg = "Value out of range.\n";
1975 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001976 return 1;
1977 }
1978
1979 global.cps_lim = v;
1980
1981 /* Dequeues all of the listeners waiting for a resource */
1982 if (!LIST_ISEMPTY(&global_listener_queue))
1983 dequeue_all_listeners(&global_listener_queue);
1984
1985 return 1;
1986 }
1987 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001988 appctx->ctx.cli.msg = "'set rate-limit connections' only supports 'global'.\n";
1989 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001990 return 1;
1991 }
1992 }
Willy Tarreau93e7c002013-10-07 18:51:07 +02001993 else if (strcmp(args[2], "sessions") == 0) {
1994 if (strcmp(args[3], "global") == 0) {
1995 int v;
1996
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001997 if (strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau93e7c002013-10-07 18:51:07 +02001998 appctx->ctx.cli.msg = stats_permission_denied_msg;
1999 appctx->st0 = STAT_CLI_PRINT;
2000 return 1;
2001 }
2002
2003 if (!*args[4]) {
2004 appctx->ctx.cli.msg = "Expects an integer value.\n";
2005 appctx->st0 = STAT_CLI_PRINT;
2006 return 1;
2007 }
2008
2009 v = atoi(args[4]);
2010 if (v < 0) {
2011 appctx->ctx.cli.msg = "Value out of range.\n";
2012 appctx->st0 = STAT_CLI_PRINT;
2013 return 1;
2014 }
2015
2016 global.sps_lim = v;
2017
2018 /* Dequeues all of the listeners waiting for a resource */
2019 if (!LIST_ISEMPTY(&global_listener_queue))
2020 dequeue_all_listeners(&global_listener_queue);
2021
2022 return 1;
2023 }
2024 else {
2025 appctx->ctx.cli.msg = "'set rate-limit sessions' only supports 'global'.\n";
2026 appctx->st0 = STAT_CLI_PRINT;
2027 return 1;
2028 }
2029 }
Willy Tarreaue43d5322013-10-07 20:01:52 +02002030#ifdef USE_OPENSSL
2031 else if (strcmp(args[2], "ssl-sessions") == 0) {
2032 if (strcmp(args[3], "global") == 0) {
2033 int v;
2034
Willy Tarreaud0d8da92015-04-04 02:10:38 +02002035 if (strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreaue43d5322013-10-07 20:01:52 +02002036 appctx->ctx.cli.msg = stats_permission_denied_msg;
2037 appctx->st0 = STAT_CLI_PRINT;
2038 return 1;
2039 }
2040
2041 if (!*args[4]) {
2042 appctx->ctx.cli.msg = "Expects an integer value.\n";
2043 appctx->st0 = STAT_CLI_PRINT;
2044 return 1;
2045 }
2046
2047 v = atoi(args[4]);
2048 if (v < 0) {
2049 appctx->ctx.cli.msg = "Value out of range.\n";
2050 appctx->st0 = STAT_CLI_PRINT;
2051 return 1;
2052 }
2053
2054 global.ssl_lim = v;
2055
2056 /* Dequeues all of the listeners waiting for a resource */
2057 if (!LIST_ISEMPTY(&global_listener_queue))
2058 dequeue_all_listeners(&global_listener_queue);
2059
2060 return 1;
2061 }
2062 else {
2063 appctx->ctx.cli.msg = "'set rate-limit ssl-sessions' only supports 'global'.\n";
2064 appctx->st0 = STAT_CLI_PRINT;
2065 return 1;
2066 }
2067 }
2068#endif
William Lallemandd85f9172012-11-09 17:05:39 +01002069 else if (strcmp(args[2], "http-compression") == 0) {
2070 if (strcmp(args[3], "global") == 0) {
2071 int v;
2072
Willy Tarreaua1c2b2c2015-11-26 18:32:39 +01002073 if (strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN) {
2074 appctx->ctx.cli.msg = stats_permission_denied_msg;
2075 appctx->st0 = STAT_CLI_PRINT;
2076 return 1;
2077 }
2078
Willy Tarreau85d47f92012-11-21 00:29:50 +01002079 if (!*args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002080 appctx->ctx.cli.msg = "Expects a maximum input byte rate in kB/s.\n";
2081 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau85d47f92012-11-21 00:29:50 +01002082 return 1;
2083 }
2084
William Lallemandd85f9172012-11-09 17:05:39 +01002085 v = atoi(args[4]);
2086 global.comp_rate_lim = v * 1024; /* Kilo to bytes. */
2087 }
2088 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002089 appctx->ctx.cli.msg = "'set rate-limit http-compression' only supports 'global'.\n";
2090 appctx->st0 = STAT_CLI_PRINT;
William Lallemandd85f9172012-11-09 17:05:39 +01002091 return 1;
2092 }
2093 }
Willy Tarreauf5b22872011-09-07 16:13:44 +02002094 else {
Willy Tarreaue43d5322013-10-07 20:01:52 +02002095 appctx->ctx.cli.msg = "'set rate-limit' supports 'connections', 'sessions', 'ssl-sessions', and 'http-compression'.\n";
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002096 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02002097 return 1;
2098 }
2099 }
Willy Tarreau654694e2012-06-07 01:03:16 +02002100 else if (strcmp(args[1], "table") == 0) {
2101 stats_sock_table_request(si, args, STAT_CLI_O_SET);
2102 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002103 else if (strcmp(args[1], "map") == 0) {
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002104 char *err;
2105
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002106 /* Set flags. */
2107 appctx->ctx.map.display_flags = PAT_REF_MAP;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002108
2109 /* Expect three parameters: map name, key and new value. */
2110 if (!*args[2] || !*args[3] || !*args[4]) {
Thierry FOURNIERd5723432014-03-11 13:52:44 +01002111 appctx->ctx.cli.msg = "'set map' expects three parameters: map identifier, key and value.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002112 appctx->st0 = STAT_CLI_PRINT;
2113 return 1;
2114 }
2115
2116 /* Lookup the reference in the maps. */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002117 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002118 if (!appctx->ctx.map.ref) {
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01002119 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002120 appctx->st0 = STAT_CLI_PRINT;
2121 return 1;
2122 }
2123
Thierry FOURNIER9356c682014-01-28 15:55:37 +01002124 /* If the entry identifier start with a '#', it is considered as
2125 * pointer id
2126 */
2127 if (args[3][0] == '#' && args[3][1] == '0' && args[3][2] == 'x') {
2128 struct pat_ref_elt *ref;
2129 long long int conv;
2130 char *error;
2131
2132 /* Convert argument to integer value. */
2133 conv = strtoll(&args[3][1], &error, 16);
2134 if (*error != '\0') {
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01002135 appctx->ctx.cli.msg = "Malformed identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER9356c682014-01-28 15:55:37 +01002136 appctx->st0 = STAT_CLI_PRINT;
2137 return 1;
2138 }
2139
2140 /* Convert and check integer to pointer. */
2141 ref = (struct pat_ref_elt *)(long)conv;
2142 if ((long long int)(long)ref != conv) {
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01002143 appctx->ctx.cli.msg = "Malformed identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER9356c682014-01-28 15:55:37 +01002144 appctx->st0 = STAT_CLI_PRINT;
2145 return 1;
2146 }
2147
2148 /* Try to delete the entry. */
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002149 err = NULL;
2150 if (!pat_ref_set_by_id(appctx->ctx.map.ref, ref, args[4], &err)) {
2151 if (err)
2152 memprintf(&err, "%s.\n", err);
2153 appctx->ctx.cli.err = err;
2154 appctx->st0 = STAT_CLI_PRINT_FREE;
Thierry FOURNIER9356c682014-01-28 15:55:37 +01002155 return 1;
2156 }
2157 }
2158 else {
2159 /* Else, use the entry identifier as pattern
2160 * string, and update the value.
2161 */
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002162 err = NULL;
2163 if (!pat_ref_set(appctx->ctx.map.ref, args[3], args[4], &err)) {
2164 if (err)
2165 memprintf(&err, "%s.\n", err);
2166 appctx->ctx.cli.err = err;
2167 appctx->st0 = STAT_CLI_PRINT_FREE;
Thierry FOURNIER9356c682014-01-28 15:55:37 +01002168 return 1;
2169 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002170 }
2171
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002172 /* The set is done, send message. */
Thierry FOURNIER07e78c52014-12-18 15:28:01 +01002173 appctx->st0 = STAT_CLI_PROMPT;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002174 return 1;
2175 }
Emeric Brun4147b2e2014-06-16 18:36:30 +02002176#ifdef USE_OPENSSL
2177 else if (strcmp(args[1], "ssl") == 0) {
2178 if (strcmp(args[2], "ocsp-response") == 0) {
Lukas Tribuse4e30f72014-12-09 16:32:51 +01002179#if (defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP)
Emeric Brun4147b2e2014-06-16 18:36:30 +02002180 char *err = NULL;
2181
Emeric Brunaf4ef742014-06-19 14:10:45 +02002182 /* Expect one parameter: the new response in base64 encoding */
Emeric Brun4147b2e2014-06-16 18:36:30 +02002183 if (!*args[3]) {
2184 appctx->ctx.cli.msg = "'set ssl ocsp-response' expects response in base64 encoding.\n";
2185 appctx->st0 = STAT_CLI_PRINT;
2186 return 1;
2187 }
2188
2189 trash.len = base64dec(args[3], strlen(args[3]), trash.str, trash.size);
2190 if (trash.len < 0) {
2191 appctx->ctx.cli.msg = "'set ssl ocsp-response' received invalid base64 encoded response.\n";
2192 appctx->st0 = STAT_CLI_PRINT;
2193 return 1;
2194 }
2195
2196 if (ssl_sock_update_ocsp_response(&trash, &err)) {
2197 if (err) {
2198 memprintf(&err, "%s.\n", err);
2199 appctx->ctx.cli.err = err;
2200 appctx->st0 = STAT_CLI_PRINT_FREE;
2201 }
2202 return 1;
2203 }
2204 appctx->ctx.cli.msg = "OCSP Response updated!";
2205 appctx->st0 = STAT_CLI_PRINT;
2206 return 1;
2207#else
2208 appctx->ctx.cli.msg = "HAProxy was compiled against a version of OpenSSL that doesn't support OCSP stapling.\n";
2209 appctx->st0 = STAT_CLI_PRINT;
2210 return 1;
2211#endif
2212 }
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02002213 else if (strcmp(args[2], "tls-key") == 0) {
2214#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
2215 /* Expect two parameters: the filename and the new new TLS key in encoding */
2216 if (!*args[3] || !*args[4]) {
2217 appctx->ctx.cli.msg = "'set ssl tls-key' expects a filename and the new TLS key in base64 encoding.\n";
2218 appctx->st0 = STAT_CLI_PRINT;
2219 return 1;
2220 }
2221
2222 appctx->ctx.tlskeys.ref = tlskeys_ref_lookup_ref(args[3]);
2223 if(!appctx->ctx.tlskeys.ref) {
2224 appctx->ctx.cli.msg = "'set ssl tls-key' unable to locate referenced filename\n";
2225 appctx->st0 = STAT_CLI_PRINT;
2226 return 1;
2227 }
2228
2229 trash.len = base64dec(args[4], strlen(args[4]), trash.str, trash.size);
2230 if (trash.len != sizeof(struct tls_sess_key)) {
2231 appctx->ctx.cli.msg = "'set ssl tls-key' received invalid base64 encoded TLS key.\n";
2232 appctx->st0 = STAT_CLI_PRINT;
2233 return 1;
2234 }
2235
Pradeep Jindalcc79b002015-08-20 18:25:17 +05302236 memcpy(appctx->ctx.tlskeys.ref->tlskeys + ((appctx->ctx.tlskeys.ref->tls_ticket_enc_index + 2) % TLS_TICKETS_NO), trash.str, trash.len);
2237 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 +02002238
2239 appctx->ctx.cli.msg = "TLS ticket key updated!";
2240 appctx->st0 = STAT_CLI_PRINT;
2241 return 1;
2242#else
2243 appctx->ctx.cli.msg = "HAProxy was compiled against a version of OpenSSL "
2244 "that doesn't support specifying TLS ticket keys\n";
2245 appctx->st0 = STAT_CLI_PRINT;
2246 return 1;
2247#endif
2248 }
Emeric Brun4147b2e2014-06-16 18:36:30 +02002249 else {
2250 appctx->ctx.cli.msg = "'set ssl' only supports 'ocsp-response'.\n";
2251 appctx->st0 = STAT_CLI_PRINT;
2252 return 1;
2253 }
2254 }
2255#endif
Willy Tarreau7aabd112010-01-26 10:59:06 +01002256 else { /* unknown "set" parameter */
Willy Tarreau4483d432009-10-10 19:30:08 +02002257 return 0;
2258 }
2259 }
Cyril Bontécd19e512010-01-31 22:34:03 +01002260 else if (strcmp(args[0], "enable") == 0) {
Simon Horman671b6f02013-11-25 10:46:39 +09002261 if (strcmp(args[1], "agent") == 0) {
2262 struct server *sv;
2263
2264 sv = expect_server_admin(s, si, args[2]);
2265 if (!sv)
2266 return 1;
2267
Willy Tarreau2e10f5a2013-12-11 20:11:55 +01002268 if (!(sv->agent.state & CHK_ST_CONFIGURED)) {
2269 appctx->ctx.cli.msg = "Agent was not configured on this server, cannot enable.\n";
2270 appctx->st0 = STAT_CLI_PRINT;
2271 return 1;
2272 }
2273
2274 sv->agent.state |= CHK_ST_ENABLED;
Simon Horman671b6f02013-11-25 10:46:39 +09002275 return 1;
2276 }
Willy Tarreau9b5aecd2014-05-23 11:53:10 +02002277 else if (strcmp(args[1], "health") == 0) {
Cyril Bontécd19e512010-01-31 22:34:03 +01002278 struct server *sv;
2279
Willy Tarreaud52c41e2011-09-07 23:41:01 +02002280 sv = expect_server_admin(s, si, args[2]);
2281 if (!sv)
Cyril Bonté613f0df2011-03-03 20:49:04 +01002282 return 1;
Cyril Bonté613f0df2011-03-03 20:49:04 +01002283
Willy Tarreau9b5aecd2014-05-23 11:53:10 +02002284 if (!(sv->check.state & CHK_ST_CONFIGURED)) {
2285 appctx->ctx.cli.msg = "Health checks are not configured on this server, cannot enable.\n";
2286 appctx->st0 = STAT_CLI_PRINT;
2287 return 1;
2288 }
2289
2290 sv->check.state |= CHK_ST_ENABLED;
2291 return 1;
2292 }
2293 else if (strcmp(args[1], "server") == 0) {
2294 struct server *sv;
2295
2296 sv = expect_server_admin(s, si, args[2]);
2297 if (!sv)
2298 return 1;
2299
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02002300 srv_adm_set_ready(sv);
Willy Tarreau532a4502011-09-07 22:37:44 +02002301 return 1;
2302 }
2303 else if (strcmp(args[1], "frontend") == 0) {
2304 struct proxy *px;
2305
2306 px = expect_frontend_admin(s, si, args[2]);
2307 if (!px)
2308 return 1;
2309
2310 if (px->state == PR_STSTOPPED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002311 appctx->ctx.cli.msg = "Frontend was previously shut down, cannot enable.\n";
2312 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002313 return 1;
2314 }
2315
2316 if (px->state != PR_STPAUSED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002317 appctx->ctx.cli.msg = "Frontend is already enabled.\n";
2318 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002319 return 1;
2320 }
2321
2322 if (!resume_proxy(px)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002323 appctx->ctx.cli.msg = "Failed to resume frontend, check logs for precise cause (port conflict?).\n";
2324 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002325 return 1;
2326 }
Cyril Bontécd19e512010-01-31 22:34:03 +01002327 return 1;
2328 }
2329 else { /* unknown "enable" parameter */
Willy Tarreau9b5aecd2014-05-23 11:53:10 +02002330 appctx->ctx.cli.msg = "'enable' only supports 'agent', 'frontend', 'health', and 'server'.\n";
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002331 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002332 return 1;
Cyril Bontécd19e512010-01-31 22:34:03 +01002333 }
2334 }
2335 else if (strcmp(args[0], "disable") == 0) {
Simon Horman671b6f02013-11-25 10:46:39 +09002336 if (strcmp(args[1], "agent") == 0) {
2337 struct server *sv;
2338
2339 sv = expect_server_admin(s, si, args[2]);
2340 if (!sv)
2341 return 1;
2342
Willy Tarreau2e10f5a2013-12-11 20:11:55 +01002343 sv->agent.state &= ~CHK_ST_ENABLED;
Simon Horman671b6f02013-11-25 10:46:39 +09002344 return 1;
2345 }
Willy Tarreau9b5aecd2014-05-23 11:53:10 +02002346 else if (strcmp(args[1], "health") == 0) {
2347 struct server *sv;
2348
2349 sv = expect_server_admin(s, si, args[2]);
2350 if (!sv)
2351 return 1;
2352
2353 sv->check.state &= ~CHK_ST_ENABLED;
2354 return 1;
2355 }
Simon Horman671b6f02013-11-25 10:46:39 +09002356 else if (strcmp(args[1], "server") == 0) {
Cyril Bontécd19e512010-01-31 22:34:03 +01002357 struct server *sv;
2358
Willy Tarreaud52c41e2011-09-07 23:41:01 +02002359 sv = expect_server_admin(s, si, args[2]);
2360 if (!sv)
Cyril Bonté613f0df2011-03-03 20:49:04 +01002361 return 1;
Cyril Bonté613f0df2011-03-03 20:49:04 +01002362
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02002363 srv_adm_set_maint(sv);
Willy Tarreau532a4502011-09-07 22:37:44 +02002364 return 1;
2365 }
2366 else if (strcmp(args[1], "frontend") == 0) {
2367 struct proxy *px;
2368
2369 px = expect_frontend_admin(s, si, args[2]);
2370 if (!px)
2371 return 1;
2372
2373 if (px->state == PR_STSTOPPED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002374 appctx->ctx.cli.msg = "Frontend was previously shut down, cannot disable.\n";
2375 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002376 return 1;
2377 }
2378
2379 if (px->state == PR_STPAUSED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002380 appctx->ctx.cli.msg = "Frontend is already disabled.\n";
2381 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002382 return 1;
2383 }
2384
2385 if (!pause_proxy(px)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002386 appctx->ctx.cli.msg = "Failed to pause frontend, check logs for precise cause.\n";
2387 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002388 return 1;
2389 }
Cyril Bontécd19e512010-01-31 22:34:03 +01002390 return 1;
2391 }
2392 else { /* unknown "disable" parameter */
Willy Tarreau9b5aecd2014-05-23 11:53:10 +02002393 appctx->ctx.cli.msg = "'disable' only supports 'agent', 'frontend', 'health', and 'server'.\n";
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002394 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002395 return 1;
2396 }
2397 }
2398 else if (strcmp(args[0], "shutdown") == 0) {
2399 if (strcmp(args[1], "frontend") == 0) {
2400 struct proxy *px;
2401
2402 px = expect_frontend_admin(s, si, args[2]);
2403 if (!px)
2404 return 1;
2405
2406 if (px->state == PR_STSTOPPED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002407 appctx->ctx.cli.msg = "Frontend was already shut down.\n";
2408 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002409 return 1;
2410 }
2411
2412 Warning("Proxy %s stopped (FE: %lld conns, BE: %lld conns).\n",
2413 px->id, px->fe_counters.cum_conn, px->be_counters.cum_conn);
2414 send_log(px, LOG_WARNING, "Proxy %s stopped (FE: %lld conns, BE: %lld conns).\n",
2415 px->id, px->fe_counters.cum_conn, px->be_counters.cum_conn);
2416 stop_proxy(px);
2417 return 1;
2418 }
Willy Tarreaua295edc2011-09-07 23:21:03 +02002419 else if (strcmp(args[1], "session") == 0) {
Willy Tarreau87b09662015-04-03 00:22:06 +02002420 struct stream *sess, *ptr;
Willy Tarreaua295edc2011-09-07 23:21:03 +02002421
Willy Tarreaud0d8da92015-04-04 02:10:38 +02002422 if (strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002423 appctx->ctx.cli.msg = stats_permission_denied_msg;
2424 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaua295edc2011-09-07 23:21:03 +02002425 return 1;
2426 }
2427
2428 if (!*args[2]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002429 appctx->ctx.cli.msg = "Session pointer expected (use 'show sess').\n";
2430 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaua295edc2011-09-07 23:21:03 +02002431 return 1;
2432 }
2433
2434 ptr = (void *)strtoul(args[2], NULL, 0);
2435
Willy Tarreau87b09662015-04-03 00:22:06 +02002436 /* first, look for the requested stream in the stream table */
2437 list_for_each_entry(sess, &streams, list) {
Willy Tarreaua295edc2011-09-07 23:21:03 +02002438 if (sess == ptr)
2439 break;
2440 }
2441
Willy Tarreau87b09662015-04-03 00:22:06 +02002442 /* do we have the stream ? */
Willy Tarreaua295edc2011-09-07 23:21:03 +02002443 if (sess != ptr) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002444 appctx->ctx.cli.msg = "No such session (use 'show sess').\n";
2445 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaua295edc2011-09-07 23:21:03 +02002446 return 1;
2447 }
2448
Willy Tarreaue7dff022015-04-03 01:14:29 +02002449 stream_shutdown(sess, SF_ERR_KILLED);
Willy Tarreaua295edc2011-09-07 23:21:03 +02002450 return 1;
2451 }
Willy Tarreau52b2d222011-09-07 23:48:48 +02002452 else if (strcmp(args[1], "sessions") == 0) {
2453 if (strcmp(args[2], "server") == 0) {
2454 struct server *sv;
Willy Tarreau87b09662015-04-03 00:22:06 +02002455 struct stream *sess, *sess_bck;
Willy Tarreau52b2d222011-09-07 23:48:48 +02002456
2457 sv = expect_server_admin(s, si, args[3]);
2458 if (!sv)
2459 return 1;
2460
Willy Tarreau87b09662015-04-03 00:22:06 +02002461 /* kill all the stream that are on this server */
Willy Tarreau52b2d222011-09-07 23:48:48 +02002462 list_for_each_entry_safe(sess, sess_bck, &sv->actconns, by_srv)
2463 if (sess->srv_conn == sv)
Willy Tarreaue7dff022015-04-03 01:14:29 +02002464 stream_shutdown(sess, SF_ERR_KILLED);
Willy Tarreau52b2d222011-09-07 23:48:48 +02002465
2466 return 1;
2467 }
2468 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002469 appctx->ctx.cli.msg = "'shutdown sessions' only supports 'server'.\n";
2470 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau52b2d222011-09-07 23:48:48 +02002471 return 1;
2472 }
2473 }
Willy Tarreau532a4502011-09-07 22:37:44 +02002474 else { /* unknown "disable" parameter */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002475 appctx->ctx.cli.msg = "'shutdown' only supports 'frontend', 'session' and 'sessions'.\n";
2476 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002477 return 1;
Cyril Bontécd19e512010-01-31 22:34:03 +01002478 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002479 }
2480 else if (strcmp(args[0], "del") == 0) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002481 if (strcmp(args[1], "map") == 0 || strcmp(args[1], "acl") == 0) {
2482 if (args[1][0] == 'm')
2483 appctx->ctx.map.display_flags = PAT_REF_MAP;
2484 else
2485 appctx->ctx.map.display_flags = PAT_REF_ACL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002486
2487 /* Expect two parameters: map name and key. */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002488 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
2489 if (!*args[2] || !*args[3]) {
2490 appctx->ctx.cli.msg = "This command expects two parameters: map identifier and key.\n";
2491 appctx->st0 = STAT_CLI_PRINT;
2492 return 1;
2493 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002494 }
2495
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002496 else {
2497 if (!*args[2] || !*args[3]) {
2498 appctx->ctx.cli.msg = "This command expects two parameters: ACL identifier and key.\n";
2499 appctx->st0 = STAT_CLI_PRINT;
2500 return 1;
2501 }
2502 }
2503
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002504 /* Lookup the reference in the maps. */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002505 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002506 if (!appctx->ctx.map.ref ||
2507 !(appctx->ctx.map.ref->flags & appctx->ctx.map.display_flags)) {
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01002508 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002509 appctx->st0 = STAT_CLI_PRINT;
2510 return 1;
2511 }
2512
Thierry FOURNIER9356c682014-01-28 15:55:37 +01002513 /* If the entry identifier start with a '#', it is considered as
2514 * pointer id
2515 */
2516 if (args[3][0] == '#' && args[3][1] == '0' && args[3][2] == 'x') {
2517 struct pat_ref_elt *ref;
2518 long long int conv;
2519 char *error;
2520
2521 /* Convert argument to integer value. */
2522 conv = strtoll(&args[3][1], &error, 16);
2523 if (*error != '\0') {
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01002524 appctx->ctx.cli.msg = "Malformed identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER9356c682014-01-28 15:55:37 +01002525 appctx->st0 = STAT_CLI_PRINT;
2526 return 1;
2527 }
2528
2529 /* Convert and check integer to pointer. */
2530 ref = (struct pat_ref_elt *)(long)conv;
2531 if ((long long int)(long)ref != conv) {
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01002532 appctx->ctx.cli.msg = "Malformed identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER9356c682014-01-28 15:55:37 +01002533 appctx->st0 = STAT_CLI_PRINT;
2534 return 1;
2535 }
2536
2537 /* Try to delete the entry. */
2538 if (!pat_ref_delete_by_id(appctx->ctx.map.ref, ref)) {
2539 /* The entry is not found, send message. */
2540 appctx->ctx.cli.msg = "Key not found.\n";
2541 appctx->st0 = STAT_CLI_PRINT;
2542 return 1;
2543 }
2544 }
2545 else {
2546 /* Else, use the entry identifier as pattern
2547 * string and try to delete the entry.
2548 */
2549 if (!pat_ref_delete(appctx->ctx.map.ref, args[3])) {
2550 /* The entry is not found, send message. */
2551 appctx->ctx.cli.msg = "Key not found.\n";
2552 appctx->st0 = STAT_CLI_PRINT;
2553 return 1;
2554 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002555 }
2556
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002557 /* The deletion is done, send message. */
Thierry FOURNIER07e78c52014-12-18 15:28:01 +01002558 appctx->st0 = STAT_CLI_PROMPT;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002559 return 1;
2560 }
2561 else { /* unknown "del" parameter */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002562 appctx->ctx.cli.msg = "'del' only supports 'map' or 'acl'.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002563 appctx->st0 = STAT_CLI_PRINT;
2564 return 1;
2565 }
2566 }
2567 else if (strcmp(args[0], "add") == 0) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002568 if (strcmp(args[1], "map") == 0 ||
2569 strcmp(args[1], "acl") == 0) {
2570 int ret;
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002571 char *err;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002572
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002573 /* Set flags. */
2574 if (args[1][0] == 'm')
2575 appctx->ctx.map.display_flags = PAT_REF_MAP;
2576 else
2577 appctx->ctx.map.display_flags = PAT_REF_ACL;
2578
2579 /* If the keywork is "map", we expect three parameters, if it
2580 * is "acl", we expect only two parameters
2581 */
2582 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
2583 if (!*args[2] || !*args[3] || !*args[4]) {
2584 appctx->ctx.cli.msg = "'add map' expects three parameters: map identifier, key and value.\n";
2585 appctx->st0 = STAT_CLI_PRINT;
2586 return 1;
2587 }
2588 }
2589 else {
2590 if (!*args[2] || !*args[3]) {
2591 appctx->ctx.cli.msg = "'add acl' expects two parameters: ACL identifier and pattern.\n";
2592 appctx->st0 = STAT_CLI_PRINT;
2593 return 1;
2594 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002595 }
2596
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002597 /* Lookup for the reference. */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002598 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002599 if (!appctx->ctx.map.ref) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002600 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01002601 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002602 else
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01002603 appctx->ctx.cli.msg = "Unknown ACL identifier. Please use #<id> or <file>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002604 appctx->st0 = STAT_CLI_PRINT;
2605 return 1;
2606 }
2607
Thierry FOURNIER64c585f2014-01-29 20:02:36 +01002608 /* The command "add acl" is prohibited if the reference
2609 * use samples.
2610 */
2611 if ((appctx->ctx.map.display_flags & PAT_REF_ACL) &&
2612 (appctx->ctx.map.ref->flags & PAT_REF_SMP)) {
2613 appctx->ctx.cli.msg = "This ACL is shared with a map containing samples. "
2614 "You must use the command 'add map' to add values.\n";
2615 appctx->st0 = STAT_CLI_PRINT;
2616 return 1;
2617 }
2618
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002619 /* Add value. */
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002620 err = NULL;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002621 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02002622 ret = pat_ref_add(appctx->ctx.map.ref, args[3], args[4], &err);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002623 else
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02002624 ret = pat_ref_add(appctx->ctx.map.ref, args[3], NULL, &err);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002625 if (!ret) {
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002626 if (err)
2627 memprintf(&err, "%s.\n", err);
2628 appctx->ctx.cli.err = err;
2629 appctx->st0 = STAT_CLI_PRINT_FREE;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002630 return 1;
2631 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002632
2633 /* The add is done, send message. */
Thierry FOURNIER07e78c52014-12-18 15:28:01 +01002634 appctx->st0 = STAT_CLI_PROMPT;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002635 return 1;
2636 }
2637 else { /* unknown "del" parameter */
2638 appctx->ctx.cli.msg = "'add' only supports 'map'.\n";
2639 appctx->st0 = STAT_CLI_PRINT;
2640 return 1;
2641 }
Cyril Bontécd19e512010-01-31 22:34:03 +01002642 }
2643 else { /* not "show" nor "clear" nor "get" nor "set" nor "enable" nor "disable" */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002644 return 0;
2645 }
2646 return 1;
2647}
2648
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002649/* This I/O handler runs as an applet embedded in a stream interface. It is
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002650 * used to processes I/O from/to the stats unix socket. The system relies on a
2651 * state machine handling requests and various responses. We read a request,
2652 * then we process it and send the response, and we possibly display a prompt.
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002653 * Then we can read again. The state is stored in appctx->st0 and is one of the
2654 * STAT_CLI_* constants. appctx->st1 is used to indicate whether prompt is enabled
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002655 * or not.
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002656 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02002657static void cli_io_handler(struct appctx *appctx)
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002658{
Willy Tarreau00a37f02015-04-13 12:05:19 +02002659 struct stream_interface *si = appctx->owner;
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002660 struct channel *req = si_oc(si);
2661 struct channel *res = si_ic(si);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002662 int reql;
2663 int len;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002664
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002665 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
2666 goto out;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002667
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002668 while (1) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002669 if (appctx->st0 == STAT_CLI_INIT) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002670 /* Stats output not initialized yet */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002671 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
2672 appctx->st0 = STAT_CLI_GETREQ;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002673 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002674 else if (appctx->st0 == STAT_CLI_END) {
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002675 /* Let's close for real now. We just close the request
2676 * side, the conditions below will complete if needed.
2677 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02002678 si_shutw(si);
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002679 break;
2680 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002681 else if (appctx->st0 == STAT_CLI_GETREQ) {
Willy Tarreau4e33d862009-10-11 23:35:10 +02002682 /* ensure we have some output room left in the event we
2683 * would want to return some info right after parsing.
2684 */
Willy Tarreau4e4292b2014-11-28 12:18:45 +01002685 if (buffer_almost_full(si_ib(si))) {
Willy Tarreaufe127932015-04-21 19:23:39 +02002686 si_applet_cant_put(si);
Willy Tarreau4e33d862009-10-11 23:35:10 +02002687 break;
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01002688 }
Willy Tarreau4e33d862009-10-11 23:35:10 +02002689
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002690 reql = bo_getline(si_oc(si), trash.str, trash.size);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002691 if (reql <= 0) { /* closed or EOL not found */
2692 if (reql == 0)
2693 break;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002694 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002695 continue;
2696 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002697
Chad Lavoiee3f50312016-05-26 16:42:25 -04002698 /* seek for a possible unescaped semi-colon. If we find
2699 * one, we replace it with an LF and skip only this part.
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002700 */
Chad Lavoiee3f50312016-05-26 16:42:25 -04002701 for (len = 0; len < reql; len++) {
2702 if (trash.str[len] == '\\') {
2703 len++;
2704 continue;
2705 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002706 if (trash.str[len] == ';') {
2707 trash.str[len] = '\n';
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002708 reql = len + 1;
2709 break;
2710 }
Chad Lavoiee3f50312016-05-26 16:42:25 -04002711 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002712
Willy Tarreau816fc222009-10-04 07:36:58 +02002713 /* now it is time to check that we have a full line,
2714 * remove the trailing \n and possibly \r, then cut the
2715 * line.
2716 */
2717 len = reql - 1;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002718 if (trash.str[len] != '\n') {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002719 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002720 continue;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002721 }
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002722
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002723 if (len && trash.str[len-1] == '\r')
Willy Tarreau816fc222009-10-04 07:36:58 +02002724 len--;
2725
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002726 trash.str[len] = '\0';
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002727
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002728 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002729 if (len) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002730 if (strcmp(trash.str, "quit") == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002731 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002732 continue;
2733 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002734 else if (strcmp(trash.str, "prompt") == 0)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002735 appctx->st1 = !appctx->st1;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002736 else if (strcmp(trash.str, "help") == 0 ||
2737 !stats_sock_parse_request(si, trash.str)) {
William Lallemand1e08cd82016-10-13 17:57:55 +02002738 cli_gen_usage_msg();
2739 if (dynamic_usage_msg)
2740 appctx->ctx.cli.msg = dynamic_usage_msg;
2741 else
2742 appctx->ctx.cli.msg = stats_sock_usage_msg;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002743 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02002744 }
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002745 /* NB: stats_sock_parse_request() may have put
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002746 * another STAT_CLI_O_* into appctx->st0.
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002747 */
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002748 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002749 else if (!appctx->st1) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002750 /* if prompt is disabled, print help on empty lines,
2751 * so that the user at least knows how to enable
2752 * prompt and find help.
2753 */
William Lallemand1e08cd82016-10-13 17:57:55 +02002754 cli_gen_usage_msg();
2755 if (dynamic_usage_msg)
2756 appctx->ctx.cli.msg = dynamic_usage_msg;
2757 else
2758 appctx->ctx.cli.msg = stats_sock_usage_msg;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002759 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002760 }
2761
2762 /* re-adjust req buffer */
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002763 bo_skip(si_oc(si), reql);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002764 req->flags |= CF_READ_DONTWAIT; /* we plan to read small requests */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002765 }
Willy Tarreau68c00c72015-09-25 19:21:19 +02002766 else { /* output functions */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002767 switch (appctx->st0) {
Andrew Hayworthe32d1862015-10-02 15:08:10 +00002768 case STAT_CLI_PROMPT:
2769 break;
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02002770 case STAT_CLI_PRINT:
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002771 if (bi_putstr(si_ic(si), appctx->ctx.cli.msg) != -1)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002772 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreaubc18da12015-03-13 14:00:47 +01002773 else
Willy Tarreaufe127932015-04-21 19:23:39 +02002774 si_applet_cant_put(si);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002775 break;
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002776 case STAT_CLI_PRINT_FREE:
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002777 if (bi_putstr(si_ic(si), appctx->ctx.cli.err) != -1) {
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002778 free(appctx->ctx.cli.err);
2779 appctx->st0 = STAT_CLI_PROMPT;
2780 }
Willy Tarreaubc18da12015-03-13 14:00:47 +01002781 else
Willy Tarreaufe127932015-04-21 19:23:39 +02002782 si_applet_cant_put(si);
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002783 break;
Baptiste Assmann9b6857e2015-09-18 14:49:12 +02002784 case STAT_CLI_O_BACKEND:
2785 if (stats_dump_backend_to_buffer(si))
2786 appctx->st0 = STAT_CLI_PROMPT;
2787 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002788 case STAT_CLI_O_INFO:
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002789 if (stats_dump_info_to_buffer(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002790 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002791 break;
Baptiste Assmann28289462015-07-03 08:01:20 +02002792 case STAT_CLI_O_SERVERS_STATE:
2793 if (stats_dump_servers_state_to_buffer(si))
2794 appctx->st0 = STAT_CLI_PROMPT;
2795 break;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002796 case STAT_CLI_O_STAT:
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002797 if (stats_dump_stat_to_buffer(si, NULL))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002798 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002799 break;
Baptiste Assmann3863f972015-05-17 00:33:24 +02002800 case STAT_CLI_O_RESOLVERS:
2801 if (stats_dump_resolvers_to_buffer(si))
2802 appctx->st0 = STAT_CLI_PROMPT;
2803 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002804 case STAT_CLI_O_SESS:
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01002805 if (stats_dump_sess_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 Tarreauf5a885f2009-10-04 14:22:18 +02002808 case STAT_CLI_O_ERR: /* errors dump */
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01002809 if (stats_dump_errors_to_buffer(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002810 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002811 break;
Willy Tarreau69f58c82010-07-12 17:55:33 +02002812 case STAT_CLI_O_TAB:
Simon Hormanc88b8872011-06-15 15:18:49 +09002813 case STAT_CLI_O_CLR:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002814 if (stats_table_request(si, appctx->st0))
2815 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau69f58c82010-07-12 17:55:33 +02002816 break;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002817 case STAT_CLI_O_PATS:
2818 if (stats_pats_list(si))
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002819 appctx->st0 = STAT_CLI_PROMPT;
2820 break;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002821 case STAT_CLI_O_PAT:
2822 if (stats_pat_list(si))
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002823 appctx->st0 = STAT_CLI_PROMPT;
2824 break;
2825 case STAT_CLI_O_MLOOK:
2826 if (stats_map_lookup(si))
2827 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau4efb3532014-01-29 12:13:39 +01002828 break;
Willy Tarreau12833bb2014-01-28 16:49:56 +01002829 case STAT_CLI_O_POOLS:
2830 if (stats_dump_pools_to_buffer(si))
2831 appctx->st0 = STAT_CLI_PROMPT;
2832 break;
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02002833#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
2834 case STAT_CLI_O_TLSK:
2835 if (stats_tlskeys_list(si))
2836 appctx->st0 = STAT_CLI_PROMPT;
2837 break;
William Lallemand1d0b36a2016-05-20 17:40:26 +02002838 case STAT_CLI_O_TLSK_ENT:
2839 if (stats_tlskeys_list(si))
2840 appctx->st0 = STAT_CLI_PROMPT;
2841 break;
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02002842#endif
Willy Tarreauae795722016-02-16 11:27:28 +01002843 case STAT_CLI_O_ENV: /* environment dump */
2844 if (stats_dump_env_to_buffer(si))
2845 appctx->st0 = STAT_CLI_PROMPT;
2846 break;
William Lallemand1e08cd82016-10-13 17:57:55 +02002847 case STAT_CLI_O_CUSTOM: /* use custom pointer */
2848 if (appctx->io_handler)
Thierry FOURNIER / OZON.IO6a22dcb2016-11-12 10:51:33 +01002849 if (appctx->io_handler(appctx)) {
William Lallemand1e08cd82016-10-13 17:57:55 +02002850 appctx->st0 = STAT_CLI_PROMPT;
Thierry FOURNIER / OZON.IO6a22dcb2016-11-12 10:51:33 +01002851 if (appctx->io_release) {
2852 appctx->io_release(appctx);
2853 appctx->io_release = NULL;
2854 }
2855 }
William Lallemand1e08cd82016-10-13 17:57:55 +02002856 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002857 default: /* abnormal state */
Willy Tarreau5cfa3bc2015-09-25 20:08:51 +02002858 si->flags |= SI_FL_ERR;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002859 break;
2860 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002861
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002862 /* The post-command prompt is either LF alone or LF + '> ' in interactive mode */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002863 if (appctx->st0 == STAT_CLI_PROMPT) {
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002864 if (bi_putstr(si_ic(si), appctx->st1 ? "\n> " : "\n") != -1)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002865 appctx->st0 = STAT_CLI_GETREQ;
Willy Tarreaubc18da12015-03-13 14:00:47 +01002866 else
Willy Tarreaufe127932015-04-21 19:23:39 +02002867 si_applet_cant_put(si);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002868 }
2869
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002870 /* If the output functions are still there, it means they require more room. */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002871 if (appctx->st0 >= STAT_CLI_OUTPUT)
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002872 break;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002873
2874 /* Now we close the output if one of the writers did so,
2875 * or if we're not in interactive mode and the request
2876 * buffer is empty. This still allows pipelined requests
2877 * to be sent in non-interactive mode.
2878 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002879 if ((res->flags & (CF_SHUTW|CF_SHUTW_NOW)) || (!appctx->st1 && !req->buf->o)) {
2880 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002881 continue;
2882 }
2883
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002884 /* switch state back to GETREQ to read next requests */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002885 appctx->st0 = STAT_CLI_GETREQ;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002886 }
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002887 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002888
Willy Tarreauc9e930a2015-09-25 20:06:08 +02002889 if ((res->flags & CF_SHUTR) && (si->state == SI_ST_EST)) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002890 DPRINTF(stderr, "%s@%d: si to buf closed. req=%08x, res=%08x, st=%d\n",
2891 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
Aman Guptaceafb4a2012-04-02 18:57:54 -07002892 /* Other side has closed, let's abort if we have no more processing to do
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002893 * and nothing more to consume. This is comparable to a broken pipe, so
2894 * we forward the close to the request side so that it flows upstream to
2895 * the client.
2896 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02002897 si_shutw(si);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002898 }
2899
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002900 if ((req->flags & CF_SHUTW) && (si->state == SI_ST_EST) && (appctx->st0 < STAT_CLI_OUTPUT)) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002901 DPRINTF(stderr, "%s@%d: buf to si closed. req=%08x, res=%08x, st=%d\n",
2902 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
2903 /* We have no more processing to do, and nothing more to send, and
2904 * the client side has closed. So we'll forward this state downstream
2905 * on the response buffer.
2906 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02002907 si_shutr(si);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002908 res->flags |= CF_READ_NULL;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002909 }
2910
Willy Tarreau828824a2015-04-19 17:20:03 +02002911 out:
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01002912 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 +02002913 __FUNCTION__, __LINE__,
Willy Tarreau9b28e032012-10-12 23:49:43 +02002914 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 +02002915}
2916
Willy Tarreau638d40a2016-02-24 23:11:01 +01002917/* Emits a stats field without any surrounding element and properly encoded to
2918 * resist CSV output. Returns non-zero on success, 0 if the buffer is full.
2919 */
Willy Tarreaub47785f2016-02-24 23:28:31 +01002920int stats_emit_raw_data_field(struct chunk *out, const struct field *f)
Willy Tarreau638d40a2016-02-24 23:11:01 +01002921{
2922 switch (field_format(f, 0)) {
2923 case FF_EMPTY: return 1;
2924 case FF_S32: return chunk_appendf(out, "%d", f->u.s32);
2925 case FF_U32: return chunk_appendf(out, "%u", f->u.u32);
2926 case FF_S64: return chunk_appendf(out, "%lld", (long long)f->u.s64);
2927 case FF_U64: return chunk_appendf(out, "%llu", (unsigned long long)f->u.u64);
2928 case FF_STR: return csv_enc_append(field_str(f, 0), 1, out) != NULL;
2929 default: return chunk_appendf(out, "[INCORRECT_FIELD_TYPE_%08x]", f->type);
2930 }
2931}
2932
Willy Tarreaub47785f2016-02-24 23:28:31 +01002933/* Emits a stats field prefixed with its type. No CSV encoding is prepared, the
2934 * output is supposed to be used on its own line. Returns non-zero on success, 0
2935 * if the buffer is full.
2936 */
2937int stats_emit_typed_data_field(struct chunk *out, const struct field *f)
2938{
2939 switch (field_format(f, 0)) {
2940 case FF_EMPTY: return 1;
2941 case FF_S32: return chunk_appendf(out, "s32:%d", f->u.s32);
2942 case FF_U32: return chunk_appendf(out, "u32:%u", f->u.u32);
2943 case FF_S64: return chunk_appendf(out, "s64:%lld", (long long)f->u.s64);
2944 case FF_U64: return chunk_appendf(out, "u64:%llu", (unsigned long long)f->u.u64);
2945 case FF_STR: return chunk_appendf(out, "str:%s", field_str(f, 0));
2946 default: return chunk_appendf(out, "%08x:?", f->type);
2947 }
2948}
2949
2950/* Emits an encoding of the field type on 3 characters followed by a delimiter.
2951 * Returns non-zero on success, 0 if the buffer is full.
2952 */
2953int stats_emit_field_tags(struct chunk *out, const struct field *f, char delim)
2954{
2955 char origin, nature, scope;
2956
2957 switch (field_origin(f, 0)) {
2958 case FO_METRIC: origin = 'M'; break;
2959 case FO_STATUS: origin = 'S'; break;
2960 case FO_KEY: origin = 'K'; break;
2961 case FO_CONFIG: origin = 'C'; break;
2962 case FO_PRODUCT: origin = 'P'; break;
2963 default: origin = '?'; break;
2964 }
2965
2966 switch (field_nature(f, 0)) {
2967 case FN_GAUGE: nature = 'G'; break;
2968 case FN_LIMIT: nature = 'L'; break;
2969 case FN_MIN: nature = 'm'; break;
2970 case FN_MAX: nature = 'M'; break;
2971 case FN_RATE: nature = 'R'; break;
2972 case FN_COUNTER: nature = 'C'; break;
2973 case FN_DURATION: nature = 'D'; break;
2974 case FN_AGE: nature = 'A'; break;
2975 case FN_TIME: nature = 'T'; break;
2976 case FN_NAME: nature = 'N'; break;
2977 case FN_OUTPUT: nature = 'O'; break;
2978 case FN_AVG: nature = 'a'; break;
2979 default: nature = '?'; break;
2980 }
2981
2982 switch (field_scope(f, 0)) {
2983 case FS_PROCESS: scope = 'P'; break;
2984 case FS_SERVICE: scope = 'S'; break;
2985 case FS_SYSTEM: scope = 's'; break;
2986 case FS_CLUSTER: scope = 'C'; break;
2987 default: scope = '?'; break;
2988 }
2989
2990 return chunk_appendf(out, "%c%c%c%c", origin, nature, scope, delim);
2991}
2992
Willy Tarreaubf95cba2016-01-11 18:27:29 +01002993/* Dump all fields from <info> into <out> using the "show info" format (name: value) */
2994static int stats_dump_info_fields(struct chunk *out, const struct field *info)
2995{
2996 int field;
2997
2998 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
2999 if (!field_format(info, field))
3000 continue;
3001
3002 if (!chunk_appendf(out, "%s: ", info_field_names[field]))
3003 return 0;
3004 if (!stats_emit_raw_data_field(out, &info[field]))
3005 return 0;
3006 if (!chunk_strcat(out, "\n"))
3007 return 0;
3008 }
3009 return 1;
3010}
3011
Willy Tarreaucb809122016-01-11 20:08:42 +01003012/* Dump all fields from <info> into <out> using the "show info typed" format */
3013static int stats_dump_typed_info_fields(struct chunk *out, const struct field *info)
3014{
3015 int field;
3016
3017 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
3018 if (!field_format(info, field))
3019 continue;
3020
3021 if (!chunk_appendf(out, "%d.%s.%u:", field, info_field_names[field], info[INF_PROCESS_NUM].u.u32))
3022 return 0;
3023 if (!stats_emit_field_tags(out, &info[field], ':'))
3024 return 0;
3025 if (!stats_emit_typed_data_field(out, &info[field]))
3026 return 0;
3027 if (!chunk_strcat(out, "\n"))
3028 return 0;
3029 }
3030 return 1;
3031}
3032
Thierry Fourniercb2c7672016-03-25 08:19:23 +01003033/* Fill <info> with HAProxy global info. <info> is preallocated
3034 * array of length <len>. The length of the aray must be
3035 * INF_TOTAL_FIELDS. If this length is less then this value, the
3036 * function returns 0, otherwise, it returns 1.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003037 */
Thierry Fourniercb2c7672016-03-25 08:19:23 +01003038int stats_fill_info(struct field *info, int len)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003039{
3040 unsigned int up = (now.tv_sec - start_date.tv_sec);
Willy Tarreau4529c072016-01-11 18:17:07 +01003041 struct chunk *out = get_trash_chunk();
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003042
Willy Tarreau0c9c2722014-05-28 12:28:58 +02003043#ifdef USE_OPENSSL
3044 int ssl_sess_rate = read_freq_ctr(&global.ssl_per_sec);
3045 int ssl_key_rate = read_freq_ctr(&global.ssl_fe_keys_per_sec);
3046 int ssl_reuse = 0;
3047
3048 if (ssl_key_rate < ssl_sess_rate) {
3049 /* count the ssl reuse ratio and avoid overflows in both directions */
3050 ssl_reuse = 100 - (100 * ssl_key_rate + (ssl_sess_rate - 1) / 2) / ssl_sess_rate;
3051 }
3052#endif
3053
Thierry Fourniercb2c7672016-03-25 08:19:23 +01003054 if (len < INF_TOTAL_FIELDS)
3055 return 0;
3056
Willy Tarreau4529c072016-01-11 18:17:07 +01003057 chunk_reset(out);
Thierry Fourniercb2c7672016-03-25 08:19:23 +01003058 memset(info, 0, sizeof(*info) * len);
Willy Tarreau4529c072016-01-11 18:17:07 +01003059
3060 info[INF_NAME] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, PRODUCT_NAME);
3061 info[INF_VERSION] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, HAPROXY_VERSION);
3062 info[INF_RELEASE_DATE] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, HAPROXY_DATE);
3063
3064 info[INF_NBPROC] = mkf_u32(FO_CONFIG|FS_SERVICE, global.nbproc);
3065 info[INF_PROCESS_NUM] = mkf_u32(FO_KEY, relative_pid);
3066 info[INF_PID] = mkf_u32(FO_STATUS, pid);
3067
3068 info[INF_UPTIME] = mkf_str(FN_DURATION, chunk_newstr(out));
3069 chunk_appendf(out, "%ud %uh%02um%02us", up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60));
3070
3071 info[INF_UPTIME_SEC] = mkf_u32(FN_DURATION, up);
3072 info[INF_MEMMAX_MB] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_memmax);
3073 info[INF_POOL_ALLOC_MB] = mkf_u32(0, (unsigned)(pool_total_allocated() / 1048576L));
3074 info[INF_POOL_USED_MB] = mkf_u32(0, (unsigned)(pool_total_used() / 1048576L));
3075 info[INF_POOL_FAILED] = mkf_u32(FN_COUNTER, pool_total_failures());
3076 info[INF_ULIMIT_N] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_nofile);
3077 info[INF_MAXSOCK] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxsock);
3078 info[INF_MAXCONN] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxconn);
3079 info[INF_HARD_MAXCONN] = mkf_u32(FO_CONFIG|FN_LIMIT, global.hardmaxconn);
3080 info[INF_CURR_CONN] = mkf_u32(0, actconn);
3081 info[INF_CUM_CONN] = mkf_u32(FN_COUNTER, totalconn);
3082 info[INF_CUM_REQ] = mkf_u32(FN_COUNTER, global.req_count);
3083#ifdef USE_OPENSSL
3084 info[INF_MAX_SSL_CONNS] = mkf_u32(FN_MAX, global.maxsslconn);
3085 info[INF_CURR_SSL_CONNS] = mkf_u32(0, sslconns);
3086 info[INF_CUM_SSL_CONNS] = mkf_u32(FN_COUNTER, totalsslconns);
3087#endif
3088 info[INF_MAXPIPES] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxpipes);
3089 info[INF_PIPES_USED] = mkf_u32(0, pipes_used);
3090 info[INF_PIPES_FREE] = mkf_u32(0, pipes_free);
3091 info[INF_CONN_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.conn_per_sec));
3092 info[INF_CONN_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.cps_lim);
3093 info[INF_MAX_CONN_RATE] = mkf_u32(FN_MAX, global.cps_max);
3094 info[INF_SESS_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.sess_per_sec));
3095 info[INF_SESS_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.sps_lim);
3096 info[INF_MAX_SESS_RATE] = mkf_u32(FN_RATE, global.sps_max);
3097
3098#ifdef USE_OPENSSL
3099 info[INF_SSL_RATE] = mkf_u32(FN_RATE, ssl_sess_rate);
3100 info[INF_SSL_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.ssl_lim);
3101 info[INF_MAX_SSL_RATE] = mkf_u32(FN_MAX, global.ssl_max);
3102 info[INF_SSL_FRONTEND_KEY_RATE] = mkf_u32(0, ssl_key_rate);
3103 info[INF_SSL_FRONTEND_MAX_KEY_RATE] = mkf_u32(FN_MAX, global.ssl_fe_keys_max);
3104 info[INF_SSL_FRONTEND_SESSION_REUSE_PCT] = mkf_u32(0, ssl_reuse);
3105 info[INF_SSL_BACKEND_KEY_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.ssl_be_keys_per_sec));
3106 info[INF_SSL_BACKEND_MAX_KEY_RATE] = mkf_u32(FN_MAX, global.ssl_be_keys_max);
3107 info[INF_SSL_CACHE_LOOKUPS] = mkf_u32(FN_COUNTER, global.shctx_lookups);
3108 info[INF_SSL_CACHE_MISSES] = mkf_u32(FN_COUNTER, global.shctx_misses);
3109#endif
3110 info[INF_COMPRESS_BPS_IN] = mkf_u32(FN_RATE, read_freq_ctr(&global.comp_bps_in));
3111 info[INF_COMPRESS_BPS_OUT] = mkf_u32(FN_RATE, read_freq_ctr(&global.comp_bps_out));
3112 info[INF_COMPRESS_BPS_RATE_LIM] = mkf_u32(FO_CONFIG|FN_LIMIT, global.comp_rate_lim);
3113#ifdef USE_ZLIB
3114 info[INF_ZLIB_MEM_USAGE] = mkf_u32(0, zlib_used_memory);
3115 info[INF_MAX_ZLIB_MEM_USAGE] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxzlibmem);
3116#endif
3117 info[INF_TASKS] = mkf_u32(0, nb_tasks_cur);
3118 info[INF_RUN_QUEUE] = mkf_u32(0, run_queue_cur);
3119 info[INF_IDLE_PCT] = mkf_u32(FN_AVG, idle_pct);
3120 info[INF_NODE] = mkf_str(FO_CONFIG|FN_OUTPUT|FS_SERVICE, global.node);
3121 if (global.desc)
3122 info[INF_DESCRIPTION] = mkf_str(FO_CONFIG|FN_OUTPUT|FS_SERVICE, global.desc);
3123
Thierry Fourniercb2c7672016-03-25 08:19:23 +01003124 return 1;
3125}
3126
3127/* This function dumps information onto the stream interface's read buffer.
3128 * It returns 0 as long as it does not complete, non-zero upon completion.
3129 * No state is used.
3130 */
3131static int stats_dump_info_to_buffer(struct stream_interface *si)
3132{
3133 struct appctx *appctx = __objt_appctx(si->end);
3134
3135 if (!stats_fill_info(info, INF_TOTAL_FIELDS))
3136 return 0;
3137
Willy Tarreau1b4ba1e2016-01-11 18:29:04 +01003138 chunk_reset(&trash);
Willy Tarreaucb809122016-01-11 20:08:42 +01003139
3140 if (appctx->ctx.stats.flags & STAT_FMT_TYPED)
3141 stats_dump_typed_info_fields(&trash, info);
3142 else
3143 stats_dump_info_fields(&trash, info);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003144
Willy Tarreaubc18da12015-03-13 14:00:47 +01003145 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02003146 si_applet_cant_put(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003147 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01003148 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003149
3150 return 1;
3151}
3152
Baptiste Assmann28289462015-07-03 08:01:20 +02003153/* dumps server state information into <buf> for all the servers found in <backend>
3154 * These information are all the parameters which may change during HAProxy runtime.
3155 * By default, we only export to the last known server state file format.
3156 * These information can be used at next startup to recover same level of server state.
3157 */
Cyril Bonté76a99782016-05-06 12:18:48 +02003158static int dump_servers_state(struct stream_interface *si, struct chunk *buf)
Baptiste Assmann28289462015-07-03 08:01:20 +02003159{
Cyril Bonté76a99782016-05-06 12:18:48 +02003160 struct appctx *appctx = __objt_appctx(si->end);
Baptiste Assmann28289462015-07-03 08:01:20 +02003161 struct server *srv;
3162 char srv_addr[INET6_ADDRSTRLEN + 1];
3163 time_t srv_time_since_last_change;
3164 int bk_f_forced_id, srv_f_forced_id;
3165
Baptiste Assmann28289462015-07-03 08:01:20 +02003166
Cyril Bonté76a99782016-05-06 12:18:48 +02003167 /* we don't want to report any state if the backend is not enabled on this process */
3168 if (appctx->ctx.server_state.px->bind_proc && !(appctx->ctx.server_state.px->bind_proc & (1UL << (relative_pid - 1))))
3169 return 1;
Baptiste Assmann28289462015-07-03 08:01:20 +02003170
Cyril Bontéd55bd7a2016-05-27 00:06:45 +02003171 if (!appctx->ctx.server_state.sv)
3172 appctx->ctx.server_state.sv = appctx->ctx.server_state.px->srv;
3173
Cyril Bonté76a99782016-05-06 12:18:48 +02003174 for (; appctx->ctx.server_state.sv != NULL; appctx->ctx.server_state.sv = srv->next) {
3175 srv = appctx->ctx.server_state.sv;
Baptiste Assmann28289462015-07-03 08:01:20 +02003176 srv_addr[0] = '\0';
Baptiste Assmann28289462015-07-03 08:01:20 +02003177
3178 switch (srv->addr.ss_family) {
3179 case AF_INET:
3180 inet_ntop(srv->addr.ss_family, &((struct sockaddr_in *)&srv->addr)->sin_addr,
3181 srv_addr, INET_ADDRSTRLEN + 1);
3182 break;
3183 case AF_INET6:
3184 inet_ntop(srv->addr.ss_family, &((struct sockaddr_in6 *)&srv->addr)->sin6_addr,
3185 srv_addr, INET6_ADDRSTRLEN + 1);
3186 break;
3187 }
3188 srv_time_since_last_change = now.tv_sec - srv->last_change;
Cyril Bonté76a99782016-05-06 12:18:48 +02003189 bk_f_forced_id = appctx->ctx.server_state.px->options & PR_O_FORCED_ID ? 1 : 0;
Baptiste Assmann28289462015-07-03 08:01:20 +02003190 srv_f_forced_id = srv->flags & SRV_F_FORCED_ID ? 1 : 0;
3191
3192 chunk_appendf(buf,
3193 "%d %s "
3194 "%d %s %s "
3195 "%d %d %d %d %ld "
3196 "%d %d %d %d %d "
3197 "%d %d"
3198 "\n",
Cyril Bonté76a99782016-05-06 12:18:48 +02003199 appctx->ctx.server_state.px->uuid, appctx->ctx.server_state.px->id,
Baptiste Assmann28289462015-07-03 08:01:20 +02003200 srv->puid, srv->id, srv_addr,
David Carlier081b3362015-11-18 06:10:22 +00003201 srv->state, srv->admin, srv->uweight, srv->iweight, (long int)srv_time_since_last_change,
Baptiste Assmann28289462015-07-03 08:01:20 +02003202 srv->check.status, srv->check.result, srv->check.health, srv->check.state, srv->agent.state,
3203 bk_f_forced_id, srv_f_forced_id);
Cyril Bonté76a99782016-05-06 12:18:48 +02003204 if (bi_putchk(si_ic(si), &trash) == -1) {
3205 si_applet_cant_put(si);
3206 return 0;
3207 }
Baptiste Assmann28289462015-07-03 08:01:20 +02003208 }
Cyril Bonté76a99782016-05-06 12:18:48 +02003209 return 1;
Baptiste Assmann28289462015-07-03 08:01:20 +02003210}
3211
Baptiste Assmann9b6857e2015-09-18 14:49:12 +02003212/* Parses backend list and simply report backend names */
3213static int stats_dump_backend_to_buffer(struct stream_interface *si)
3214{
Cyril Bonté6ca9e012016-05-06 12:18:49 +02003215 struct appctx *appctx = __objt_appctx(si->end);
Baptiste Assmann9b6857e2015-09-18 14:49:12 +02003216 extern struct proxy *proxy;
3217 struct proxy *curproxy;
3218
3219 chunk_reset(&trash);
Baptiste Assmann9b6857e2015-09-18 14:49:12 +02003220
Cyril Bonté6ca9e012016-05-06 12:18:49 +02003221 if (!appctx->ctx.be.px) {
3222 chunk_printf(&trash, "# name\n");
3223 if (bi_putchk(si_ic(si), &trash) == -1) {
3224 si_applet_cant_put(si);
3225 return 0;
3226 }
3227 appctx->ctx.be.px = proxy;
3228 }
3229
3230 for (; appctx->ctx.be.px != NULL; appctx->ctx.be.px = curproxy->next) {
3231 curproxy = appctx->ctx.be.px;
3232
Baptiste Assmann9b6857e2015-09-18 14:49:12 +02003233 /* looking for backends only */
3234 if (!(curproxy->cap & PR_CAP_BE))
3235 continue;
3236
3237 /* we don't want to list a backend which is bound to this process */
3238 if (curproxy->bind_proc && !(curproxy->bind_proc & (1UL << (relative_pid - 1))))
3239 continue;
3240
3241 chunk_appendf(&trash, "%s\n", curproxy->id);
Cyril Bonté6ca9e012016-05-06 12:18:49 +02003242 if (bi_putchk(si_ic(si), &trash) == -1) {
3243 si_applet_cant_put(si);
3244 return 0;
3245 }
Baptiste Assmann9b6857e2015-09-18 14:49:12 +02003246 }
3247
3248 return 1;
3249}
3250
Baptiste Assmann28289462015-07-03 08:01:20 +02003251/* Parses backend list or simply use backend name provided by the user to return
3252 * states of servers to stdout.
3253 */
3254static int stats_dump_servers_state_to_buffer(struct stream_interface *si)
3255{
3256 struct appctx *appctx = __objt_appctx(si->end);
3257 extern struct proxy *proxy;
3258 struct proxy *curproxy;
3259
3260 chunk_reset(&trash);
3261
Willy Tarreaua58c4352016-06-22 14:51:40 +02003262 if (appctx->st2 == STAT_ST_INIT) {
3263 if (!appctx->ctx.server_state.px)
3264 appctx->ctx.server_state.px = proxy;
3265 appctx->st2 = STAT_ST_HEAD;
3266 }
3267
3268 if (appctx->st2 == STAT_ST_HEAD) {
Cyril Bonté76a99782016-05-06 12:18:48 +02003269 chunk_printf(&trash, "%d\n# %s\n", SRV_STATE_FILE_VERSION, SRV_STATE_FILE_FIELD_NAMES);
3270 if (bi_putchk(si_ic(si), &trash) == -1) {
3271 si_applet_cant_put(si);
3272 return 0;
Baptiste Assmann28289462015-07-03 08:01:20 +02003273 }
Willy Tarreaua58c4352016-06-22 14:51:40 +02003274 appctx->st2 = STAT_ST_INFO;
Baptiste Assmann28289462015-07-03 08:01:20 +02003275 }
3276
Willy Tarreaua58c4352016-06-22 14:51:40 +02003277 /* STAT_ST_INFO */
Cyril Bonté76a99782016-05-06 12:18:48 +02003278 for (; appctx->ctx.server_state.px != NULL; appctx->ctx.server_state.px = curproxy->next) {
3279 curproxy = appctx->ctx.server_state.px;
Cyril Bonté76a99782016-05-06 12:18:48 +02003280 /* servers are only in backends */
3281 if (curproxy->cap & PR_CAP_BE) {
3282 if (!dump_servers_state(si, &trash))
3283 return 0;
3284
3285 if (bi_putchk(si_ic(si), &trash) == -1) {
3286 si_applet_cant_put(si);
3287 return 0;
3288 }
3289 }
3290 /* only the selected proxy is dumped */
3291 if (appctx->ctx.server_state.iid)
3292 break;
Baptiste Assmann28289462015-07-03 08:01:20 +02003293 }
3294
3295 return 1;
3296}
3297
Willy Tarreau12833bb2014-01-28 16:49:56 +01003298/* This function dumps memory usage information onto the stream interface's
3299 * read buffer. It returns 0 as long as it does not complete, non-zero upon
3300 * completion. No state is used.
3301 */
3302static int stats_dump_pools_to_buffer(struct stream_interface *si)
3303{
3304 dump_pools_to_trash();
Willy Tarreaubc18da12015-03-13 14:00:47 +01003305 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02003306 si_applet_cant_put(si);
Willy Tarreau12833bb2014-01-28 16:49:56 +01003307 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01003308 }
Willy Tarreau82a86022016-01-04 19:04:18 +01003309 return 1;
3310}
3311
3312/* Dump all fields from <stats> into <out> using CSV format */
3313static int stats_dump_fields_csv(struct chunk *out, const struct field *stats)
3314{
3315 int field;
3316
3317 for (field = 0; field < ST_F_TOTAL_FIELDS; field++) {
3318 if (!stats_emit_raw_data_field(out, &stats[field]))
3319 return 0;
3320 if (!chunk_strcat(out, ","))
3321 return 0;
3322 }
Conrad Hoffmann692c9382016-04-01 20:40:58 +02003323 chunk_strcat(out, "\n");
Willy Tarreau12833bb2014-01-28 16:49:56 +01003324 return 1;
3325}
3326
Willy Tarreau1e62df92016-01-11 18:57:53 +01003327/* Dump all fields from <stats> into <out> using a typed "field:desc:type:value" format */
3328static int stats_dump_fields_typed(struct chunk *out, const struct field *stats)
3329{
3330 int field;
3331
3332 for (field = 0; field < ST_F_TOTAL_FIELDS; field++) {
3333 if (!stats[field].type)
3334 continue;
3335
3336 chunk_appendf(out, "%c.%u.%u.%d.%s.%u:",
3337 stats[ST_F_TYPE].u.u32 == STATS_TYPE_FE ? 'F' :
3338 stats[ST_F_TYPE].u.u32 == STATS_TYPE_BE ? 'B' :
3339 stats[ST_F_TYPE].u.u32 == STATS_TYPE_SO ? 'L' :
3340 stats[ST_F_TYPE].u.u32 == STATS_TYPE_SV ? 'S' :
3341 '?',
3342 stats[ST_F_IID].u.u32, stats[ST_F_SID].u.u32,
3343 field, stat_field_names[field], stats[ST_F_PID].u.u32);
3344
3345 if (!stats_emit_field_tags(out, &stats[field], ':'))
3346 return 0;
3347 if (!stats_emit_typed_data_field(out, &stats[field]))
3348 return 0;
3349 if (!chunk_strcat(out, "\n"))
3350 return 0;
3351 }
3352 return 1;
3353}
3354
Willy Tarreau60600742016-01-11 15:32:30 +01003355/* Dump all fields from <stats> into <out> using the HTML format. A column is
Willy Tarreau508a63f2016-01-11 15:28:40 +01003356 * reserved for the checkbox is ST_SHOWADMIN is set in <flags>. Some extra info
3357 * are provided if ST_SHLGNDS is present in <flags>.
Cyril Bonté70be45d2010-10-12 00:14:35 +02003358 */
Willy Tarreau60600742016-01-11 15:32:30 +01003359static int stats_dump_fields_html(struct chunk *out, const struct field *stats, unsigned int flags)
Cyril Bonté70be45d2010-10-12 00:14:35 +02003360{
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003361 struct chunk src;
3362
Willy Tarreaub5f66b82016-01-08 17:15:39 +01003363 if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_FE) {
Willy Tarreau60600742016-01-11 15:32:30 +01003364 chunk_appendf(out,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003365 /* name, queue */
3366 "<tr class=\"frontend\">");
Cyril Bonté70be45d2010-10-12 00:14:35 +02003367
Willy Tarreau508a63f2016-01-11 15:28:40 +01003368 if (flags & ST_SHOWADMIN) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003369 /* Column sub-heading for Enable or Disable server */
Willy Tarreau60600742016-01-11 15:32:30 +01003370 chunk_appendf(out, "<td></td>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003371 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02003372
Willy Tarreau60600742016-01-11 15:32:30 +01003373 chunk_appendf(out,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003374 "<td class=ac>"
3375 "<a name=\"%s/Frontend\"></a>"
3376 "<a class=lfsb href=\"#%s/Frontend\">Frontend</a></td>"
3377 "<td colspan=3></td>"
3378 "",
Willy Tarreaud72c9172016-01-06 15:26:40 +01003379 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_PXNAME));
Cyril Bonté70be45d2010-10-12 00:14:35 +02003380
Willy Tarreau60600742016-01-11 15:32:30 +01003381 chunk_appendf(out,
Willy Tarreau466c9b52012-12-23 02:25:03 +01003382 /* sessions rate : current */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003383 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01003384 "<tr><th>Current connection rate:</th><td>%s/s</td></tr>"
3385 "<tr><th>Current session rate:</th><td>%s/s</td></tr>"
3386 "",
Willy Tarreaud72c9172016-01-06 15:26:40 +01003387 U2H(stats[ST_F_RATE].u.u32),
Willy Tarreauc73810f2016-01-11 13:52:04 +01003388 U2H(stats[ST_F_CONN_RATE].u.u32),
Willy Tarreaud72c9172016-01-06 15:26:40 +01003389 U2H(stats[ST_F_RATE].u.u32));
Willy Tarreau466c9b52012-12-23 02:25:03 +01003390
Willy Tarreauf8211df2016-01-11 14:09:38 +01003391 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
Willy Tarreau60600742016-01-11 15:32:30 +01003392 chunk_appendf(out,
Willy Tarreau466c9b52012-12-23 02:25:03 +01003393 "<tr><th>Current request rate:</th><td>%s/s</td></tr>",
Willy Tarreaud72c9172016-01-06 15:26:40 +01003394 U2H(stats[ST_F_REQ_RATE].u.u32));
Willy Tarreau466c9b52012-12-23 02:25:03 +01003395
Willy Tarreau60600742016-01-11 15:32:30 +01003396 chunk_appendf(out,
Willy Tarreau466c9b52012-12-23 02:25:03 +01003397 "</table></div></u></td>"
3398 /* sessions rate : max */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003399 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01003400 "<tr><th>Max connection rate:</th><td>%s/s</td></tr>"
3401 "<tr><th>Max session rate:</th><td>%s/s</td></tr>"
3402 "",
Willy Tarreaud72c9172016-01-06 15:26:40 +01003403 U2H(stats[ST_F_RATE_MAX].u.u32),
Willy Tarreauc73810f2016-01-11 13:52:04 +01003404 U2H(stats[ST_F_CONN_RATE_MAX].u.u32),
Willy Tarreaud72c9172016-01-06 15:26:40 +01003405 U2H(stats[ST_F_RATE_MAX].u.u32));
Willy Tarreau466c9b52012-12-23 02:25:03 +01003406
Willy Tarreauf8211df2016-01-11 14:09:38 +01003407 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
Willy Tarreau60600742016-01-11 15:32:30 +01003408 chunk_appendf(out,
Willy Tarreau466c9b52012-12-23 02:25:03 +01003409 "<tr><th>Max request rate:</th><td>%s/s</td></tr>",
Willy Tarreaud72c9172016-01-06 15:26:40 +01003410 U2H(stats[ST_F_REQ_RATE_MAX].u.u32));
Willy Tarreau466c9b52012-12-23 02:25:03 +01003411
Willy Tarreau60600742016-01-11 15:32:30 +01003412 chunk_appendf(out,
Willy Tarreau466c9b52012-12-23 02:25:03 +01003413 "</table></div></u></td>"
3414 /* sessions rate : limit */
3415 "<td>%s</td>",
Willy Tarreaud72c9172016-01-06 15:26:40 +01003416 LIM2A(stats[ST_F_RATE_LIM].u.u32, "-"));
Cyril Bonté70be45d2010-10-12 00:14:35 +02003417
Willy Tarreau60600742016-01-11 15:32:30 +01003418 chunk_appendf(out,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003419 /* sessions: current, max, limit, total */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003420 "<td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003421 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01003422 "<tr><th>Cum. connections:</th><td>%s</td></tr>"
3423 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003424 "",
Willy Tarreauc73810f2016-01-11 13:52:04 +01003425 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 +01003426 U2H(stats[ST_F_STOT].u.u64),
Willy Tarreauc73810f2016-01-11 13:52:04 +01003427 U2H(stats[ST_F_CONN_TOT].u.u64),
Willy Tarreaud72c9172016-01-06 15:26:40 +01003428 U2H(stats[ST_F_STOT].u.u64));
Cyril Bonté70be45d2010-10-12 00:14:35 +02003429
Willy Tarreau466c9b52012-12-23 02:25:03 +01003430 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
Willy Tarreauf8211df2016-01-11 14:09:38 +01003431 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
Willy Tarreau60600742016-01-11 15:32:30 +01003432 chunk_appendf(out,
Willy Tarreau466c9b52012-12-23 02:25:03 +01003433 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
3434 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
3435 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
3436 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
3437 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
3438 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
3439 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
3440 "<tr><th>- other responses:</th><td>%s</td></tr>"
3441 "<tr><th>Intercepted requests:</th><td>%s</td></tr>"
3442 "",
Willy Tarreaud72c9172016-01-06 15:26:40 +01003443 U2H(stats[ST_F_REQ_TOT].u.u64),
3444 U2H(stats[ST_F_HRSP_1XX].u.u64),
3445 U2H(stats[ST_F_HRSP_2XX].u.u64),
3446 U2H(stats[ST_F_COMP_RSP].u.u64),
3447 stats[ST_F_HRSP_2XX].u.u64 ?
3448 (int)(100 * stats[ST_F_COMP_RSP].u.u64 / stats[ST_F_HRSP_2XX].u.u64) : 0,
3449 U2H(stats[ST_F_HRSP_3XX].u.u64),
3450 U2H(stats[ST_F_HRSP_4XX].u.u64),
3451 U2H(stats[ST_F_HRSP_5XX].u.u64),
3452 U2H(stats[ST_F_HRSP_OTHER].u.u64),
Willy Tarreau5b9bdff2016-01-11 14:40:47 +01003453 U2H(stats[ST_F_INTERCEPTED].u.u64));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003454 }
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02003455
Willy Tarreau60600742016-01-11 15:32:30 +01003456 chunk_appendf(out,
Willy Tarreau466c9b52012-12-23 02:25:03 +01003457 "</table></div></u></td>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05003458 /* sessions: lbtot, lastsess */
3459 "<td></td><td></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003460 /* bytes : in */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003461 "<td>%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003462 "",
Willy Tarreaud72c9172016-01-06 15:26:40 +01003463 U2H(stats[ST_F_BIN].u.u64));
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02003464
Willy Tarreau60600742016-01-11 15:32:30 +01003465 chunk_appendf(out,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003466 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
Willy Tarreau4e5d58e2015-07-04 14:35:15 +02003467 "<td>%s%s<div class=tips><table class=det>"
3468 "<tr><th>Response bytes in:</th><td>%s</td></tr>"
3469 "<tr><th>Compression in:</th><td>%s</td></tr>"
3470 "<tr><th>Compression out:</th><td>%s</td><td>(%d%%)</td></tr>"
3471 "<tr><th>Compression bypass:</th><td>%s</td></tr>"
3472 "<tr><th>Total bytes saved:</th><td>%s</td><td>(%d%%)</td></tr>"
3473 "</table></div>%s</td>",
Willy Tarreaud72c9172016-01-06 15:26:40 +01003474 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "<u>":"",
3475 U2H(stats[ST_F_BOUT].u.u64),
3476 U2H(stats[ST_F_BOUT].u.u64),
3477 U2H(stats[ST_F_COMP_IN].u.u64),
3478 U2H(stats[ST_F_COMP_OUT].u.u64),
3479 stats[ST_F_COMP_IN].u.u64 ? (int)(stats[ST_F_COMP_OUT].u.u64 * 100 / stats[ST_F_COMP_IN].u.u64) : 0,
3480 U2H(stats[ST_F_COMP_BYP].u.u64),
3481 U2H(stats[ST_F_COMP_IN].u.u64 - stats[ST_F_COMP_OUT].u.u64),
3482 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,
3483 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "</u>":"");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02003484
Willy Tarreau60600742016-01-11 15:32:30 +01003485 chunk_appendf(out,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003486 /* denied: req, resp */
3487 "<td>%s</td><td>%s</td>"
3488 /* errors : request, connect, response */
3489 "<td>%s</td><td></td><td></td>"
3490 /* warnings: retries, redispatches */
3491 "<td></td><td></td>"
3492 /* server status : reflect frontend status */
3493 "<td class=ac>%s</td>"
3494 /* rest of server: nothing */
3495 "<td class=ac colspan=8></td></tr>"
3496 "",
Willy Tarreaud72c9172016-01-06 15:26:40 +01003497 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
3498 U2H(stats[ST_F_EREQ].u.u64),
3499 field_str(stats, ST_F_STATUS));
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02003500 }
Willy Tarreaufa9512f2016-01-08 17:20:51 +01003501 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_SO) {
Willy Tarreau60600742016-01-11 15:32:30 +01003502 chunk_appendf(out, "<tr class=socket>");
Willy Tarreau508a63f2016-01-11 15:28:40 +01003503 if (flags & ST_SHOWADMIN) {
Willy Tarreaufa9512f2016-01-08 17:20:51 +01003504 /* Column sub-heading for Enable or Disable server */
Willy Tarreau60600742016-01-11 15:32:30 +01003505 chunk_appendf(out, "<td></td>");
Willy Tarreaufa9512f2016-01-08 17:20:51 +01003506 }
3507
Willy Tarreau60600742016-01-11 15:32:30 +01003508 chunk_appendf(out,
Willy Tarreaufa9512f2016-01-08 17:20:51 +01003509 /* frontend name, listener name */
3510 "<td class=ac><a name=\"%s/+%s\"></a>%s"
3511 "<a class=lfsb href=\"#%s/+%s\">%s</a>"
3512 "",
3513 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME),
3514 (flags & ST_SHLGNDS)?"<u>":"",
3515 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME), field_str(stats, ST_F_SVNAME));
3516
3517 if (flags & ST_SHLGNDS) {
Willy Tarreau60600742016-01-11 15:32:30 +01003518 chunk_appendf(out, "<div class=tips>");
Willy Tarreaufa9512f2016-01-08 17:20:51 +01003519
3520 if (isdigit(*field_str(stats, ST_F_ADDR)))
Willy Tarreau60600742016-01-11 15:32:30 +01003521 chunk_appendf(out, "IPv4: %s, ", field_str(stats, ST_F_ADDR));
Willy Tarreaufa9512f2016-01-08 17:20:51 +01003522 else if (*field_str(stats, ST_F_ADDR) == '[')
Willy Tarreau60600742016-01-11 15:32:30 +01003523 chunk_appendf(out, "IPv6: %s, ", field_str(stats, ST_F_ADDR));
Willy Tarreaufa9512f2016-01-08 17:20:51 +01003524 else if (*field_str(stats, ST_F_ADDR))
Willy Tarreau60600742016-01-11 15:32:30 +01003525 chunk_appendf(out, "%s, ", field_str(stats, ST_F_ADDR));
Willy Tarreaufa9512f2016-01-08 17:20:51 +01003526
3527 /* id */
Willy Tarreau60600742016-01-11 15:32:30 +01003528 chunk_appendf(out, "id: %d</div>", stats[ST_F_SID].u.u32);
Willy Tarreaufa9512f2016-01-08 17:20:51 +01003529 }
3530
Willy Tarreau60600742016-01-11 15:32:30 +01003531 chunk_appendf(out,
Willy Tarreaufa9512f2016-01-08 17:20:51 +01003532 /* queue */
3533 "%s</td><td colspan=3></td>"
3534 /* sessions rate: current, max, limit */
3535 "<td colspan=3>&nbsp;</td>"
3536 /* sessions: current, max, limit, total, lbtot, lastsess */
3537 "<td>%s</td><td>%s</td><td>%s</td>"
3538 "<td>%s</td><td>&nbsp;</td><td>&nbsp;</td>"
3539 /* bytes: in, out */
3540 "<td>%s</td><td>%s</td>"
3541 "",
3542 (flags & ST_SHLGNDS)?"</u>":"",
3543 U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SMAX].u.u32), U2H(stats[ST_F_SLIM].u.u32),
3544 U2H(stats[ST_F_STOT].u.u64), U2H(stats[ST_F_BIN].u.u64), U2H(stats[ST_F_BOUT].u.u64));
3545
Willy Tarreau60600742016-01-11 15:32:30 +01003546 chunk_appendf(out,
Willy Tarreaufa9512f2016-01-08 17:20:51 +01003547 /* denied: req, resp */
3548 "<td>%s</td><td>%s</td>"
3549 /* errors: request, connect, response */
3550 "<td>%s</td><td></td><td></td>"
3551 /* warnings: retries, redispatches */
3552 "<td></td><td></td>"
3553 /* server status: reflect listener status */
3554 "<td class=ac>%s</td>"
3555 /* rest of server: nothing */
3556 "<td class=ac colspan=8></td></tr>"
3557 "",
3558 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
3559 U2H(stats[ST_F_EREQ].u.u64),
3560 field_str(stats, ST_F_STATUS));
3561 }
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003562 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_SV) {
3563 const char *style;
Willy Tarreaub5f66b82016-01-08 17:15:39 +01003564
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003565 /* determine the style to use depending on the server's state,
3566 * its health and weight. There isn't a 1-to-1 mapping between
3567 * state and styles for the cases where the server is (still)
3568 * up. The reason is that we don't want to report nolb and
3569 * drain with the same color.
3570 */
Willy Tarreaub5f66b82016-01-08 17:15:39 +01003571
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003572 if (strcmp(field_str(stats, ST_F_STATUS), "DOWN") == 0 ||
3573 strcmp(field_str(stats, ST_F_STATUS), "DOWN (agent)") == 0) {
3574 style = "down";
3575 }
3576 else if (strcmp(field_str(stats, ST_F_STATUS), "DOWN ") == 0) {
3577 style = "going_up";
3578 }
3579 else if (strcmp(field_str(stats, ST_F_STATUS), "NOLB ") == 0) {
3580 style = "going_down";
3581 }
3582 else if (strcmp(field_str(stats, ST_F_STATUS), "NOLB") == 0) {
3583 style = "nolb";
3584 }
3585 else if (strcmp(field_str(stats, ST_F_STATUS), "no check") == 0) {
3586 style = "no_check";
3587 }
3588 else if (!stats[ST_F_CHKFAIL].type ||
3589 stats[ST_F_CHECK_HEALTH].u.u32 == stats[ST_F_CHECK_RISE].u.u32 + stats[ST_F_CHECK_FALL].u.u32 - 1) {
3590 /* no check or max health = UP */
3591 if (stats[ST_F_WEIGHT].u.u32)
3592 style = "up";
3593 else
3594 style = "draining";
3595 }
3596 else {
3597 style = "going_down";
3598 }
Willy Tarreaub5f66b82016-01-08 17:15:39 +01003599
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003600 if (memcmp(field_str(stats, ST_F_STATUS), "MAINT", 5) == 0)
Willy Tarreau60600742016-01-11 15:32:30 +01003601 chunk_appendf(out, "<tr class=\"maintain\">");
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003602 else
Willy Tarreau60600742016-01-11 15:32:30 +01003603 chunk_appendf(out,
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003604 "<tr class=\"%s_%s\">",
3605 (stats[ST_F_BCK].u.u32) ? "backup" : "active", style);
Willy Tarreaub5f66b82016-01-08 17:15:39 +01003606
Willy Tarreaub5f66b82016-01-08 17:15:39 +01003607
Willy Tarreau508a63f2016-01-11 15:28:40 +01003608 if (flags & ST_SHOWADMIN)
Willy Tarreau60600742016-01-11 15:32:30 +01003609 chunk_appendf(out,
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003610 "<td><input type=\"checkbox\" name=\"s\" value=\"%s\"></td>",
3611 field_str(stats, ST_F_SVNAME));
Willy Tarreaub5f66b82016-01-08 17:15:39 +01003612
Willy Tarreau60600742016-01-11 15:32:30 +01003613 chunk_appendf(out,
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003614 "<td class=ac><a name=\"%s/%s\"></a>%s"
3615 "<a class=lfsb href=\"#%s/%s\">%s</a>"
3616 "",
3617 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME),
3618 (flags & ST_SHLGNDS) ? "<u>" : "",
3619 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 +01003620
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003621 if (flags & ST_SHLGNDS) {
Willy Tarreau60600742016-01-11 15:32:30 +01003622 chunk_appendf(out, "<div class=tips>");
Willy Tarreaub5f66b82016-01-08 17:15:39 +01003623
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003624 if (isdigit(*field_str(stats, ST_F_ADDR)))
Willy Tarreau60600742016-01-11 15:32:30 +01003625 chunk_appendf(out, "IPv4: %s, ", field_str(stats, ST_F_ADDR));
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003626 else if (*field_str(stats, ST_F_ADDR) == '[')
Willy Tarreau60600742016-01-11 15:32:30 +01003627 chunk_appendf(out, "IPv6: %s, ", field_str(stats, ST_F_ADDR));
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003628 else if (*field_str(stats, ST_F_ADDR))
Willy Tarreau60600742016-01-11 15:32:30 +01003629 chunk_appendf(out, "%s, ", field_str(stats, ST_F_ADDR));
Willy Tarreaub5f66b82016-01-08 17:15:39 +01003630
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003631 /* id */
Willy Tarreau60600742016-01-11 15:32:30 +01003632 chunk_appendf(out, "id: %d", stats[ST_F_SID].u.u32);
Willy Tarreaub5f66b82016-01-08 17:15:39 +01003633
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003634 /* cookie */
3635 if (stats[ST_F_COOKIE].type) {
Willy Tarreau60600742016-01-11 15:32:30 +01003636 chunk_appendf(out, ", cookie: '");
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003637 chunk_initstr(&src, field_str(stats, ST_F_COOKIE));
Willy Tarreau60600742016-01-11 15:32:30 +01003638 chunk_htmlencode(out, &src);
3639 chunk_appendf(out, "'");
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003640 }
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02003641
Willy Tarreau60600742016-01-11 15:32:30 +01003642 chunk_appendf(out, "</div>");
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003643 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003644
Willy Tarreau60600742016-01-11 15:32:30 +01003645 chunk_appendf(out,
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003646 /* queue : current, max, limit */
3647 "%s</td><td>%s</td><td>%s</td><td>%s</td>"
3648 /* sessions rate : current, max, limit */
3649 "<td>%s</td><td>%s</td><td></td>"
3650 "",
3651 (flags & ST_SHLGNDS) ? "</u>" : "",
3652 U2H(stats[ST_F_QCUR].u.u32), U2H(stats[ST_F_QMAX].u.u32), LIM2A(stats[ST_F_QLIMIT].u.u32, "-"),
3653 U2H(stats[ST_F_RATE].u.u32), U2H(stats[ST_F_RATE_MAX].u.u32));
Willy Tarreau4607fad2016-01-06 15:41:29 +01003654
Willy Tarreau60600742016-01-11 15:32:30 +01003655 chunk_appendf(out,
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003656 /* sessions: current, max, limit, total */
3657 "<td>%s</td><td>%s</td><td>%s</td>"
3658 "<td><u>%s<div class=tips><table class=det>"
3659 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
3660 "",
3661 U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SMAX].u.u32), LIM2A(stats[ST_F_SLIM].u.u32, "-"),
3662 U2H(stats[ST_F_STOT].u.u64),
3663 U2H(stats[ST_F_STOT].u.u64));
Willy Tarreau4607fad2016-01-06 15:41:29 +01003664
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003665 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
Willy Tarreauf8211df2016-01-11 14:09:38 +01003666 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003667 unsigned long long tot;
Willy Tarreaua6f5a732016-01-08 16:59:56 +01003668
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003669 tot = stats[ST_F_HRSP_OTHER].u.u64;
3670 tot += stats[ST_F_HRSP_1XX].u.u64;
3671 tot += stats[ST_F_HRSP_2XX].u.u64;
3672 tot += stats[ST_F_HRSP_3XX].u.u64;
3673 tot += stats[ST_F_HRSP_4XX].u.u64;
3674 tot += stats[ST_F_HRSP_5XX].u.u64;
3675
Willy Tarreau60600742016-01-11 15:32:30 +01003676 chunk_appendf(out,
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003677 "<tr><th>Cum. HTTP responses:</th><td>%s</td></tr>"
3678 "<tr><th>- HTTP 1xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
3679 "<tr><th>- HTTP 2xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
3680 "<tr><th>- HTTP 3xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
3681 "<tr><th>- HTTP 4xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
3682 "<tr><th>- HTTP 5xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
3683 "<tr><th>- other responses:</th><td>%s</td><td>(%d%%)</td></tr>"
3684 "",
3685 U2H(tot),
3686 U2H(stats[ST_F_HRSP_1XX].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_1XX].u.u64 / tot) : 0,
3687 U2H(stats[ST_F_HRSP_2XX].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_2XX].u.u64 / tot) : 0,
3688 U2H(stats[ST_F_HRSP_3XX].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_3XX].u.u64 / tot) : 0,
3689 U2H(stats[ST_F_HRSP_4XX].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_4XX].u.u64 / tot) : 0,
3690 U2H(stats[ST_F_HRSP_5XX].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_5XX].u.u64 / tot) : 0,
3691 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 +01003692 }
Willy Tarreaua6f5a732016-01-08 16:59:56 +01003693
Willy Tarreau60600742016-01-11 15:32:30 +01003694 chunk_appendf(out, "<tr><th colspan=3>Avg over last 1024 success. conn.</th></tr>");
3695 chunk_appendf(out, "<tr><th>- Queue time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_QTIME].u.u32));
3696 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 +01003697 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
Willy Tarreau60600742016-01-11 15:32:30 +01003698 chunk_appendf(out, "<tr><th>- Response time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_RTIME].u.u32));
3699 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 +02003700
Willy Tarreau60600742016-01-11 15:32:30 +01003701 chunk_appendf(out,
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003702 "</table></div></u></td>"
3703 /* sessions: lbtot, last */
3704 "<td>%s</td><td>%s</td>",
3705 U2H(stats[ST_F_LBTOT].u.u64),
3706 human_time(stats[ST_F_LASTSESS].u.s32, 1));
Willy Tarreau91861262007-10-17 17:06:05 +02003707
Willy Tarreau60600742016-01-11 15:32:30 +01003708 chunk_appendf(out,
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003709 /* bytes : in, out */
3710 "<td>%s</td><td>%s</td>"
3711 /* denied: req, resp */
3712 "<td></td><td>%s</td>"
3713 /* errors : request, connect */
3714 "<td></td><td>%s</td>"
3715 /* errors : response */
3716 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
3717 /* warnings: retries, redispatches */
3718 "<td>%lld</td><td>%lld</td>"
3719 "",
3720 U2H(stats[ST_F_BIN].u.u64), U2H(stats[ST_F_BOUT].u.u64),
3721 U2H(stats[ST_F_DRESP].u.u64),
3722 U2H(stats[ST_F_ECON].u.u64),
3723 U2H(stats[ST_F_ERESP].u.u64),
3724 (long long)stats[ST_F_CLI_ABRT].u.u64,
3725 (long long)stats[ST_F_SRV_ABRT].u.u64,
3726 (long long)stats[ST_F_WRETR].u.u64,
3727 (long long)stats[ST_F_WREDIS].u.u64);
3728
3729 /* status, last change */
Willy Tarreau60600742016-01-11 15:32:30 +01003730 chunk_appendf(out, "<td class=ac>");
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003731
3732 /* FIXME!!!!
3733 * LASTCHG should contain the last change for *this* server and must be computed
3734 * properly above, as was done below, ie: this server if maint, otherwise ref server
3735 * if tracking. Note that ref is either local or remote depending on tracking.
3736 */
3737
3738
3739 if (memcmp(field_str(stats, ST_F_STATUS), "MAINT", 5) == 0) {
Willy Tarreau60600742016-01-11 15:32:30 +01003740 chunk_appendf(out, "%s MAINT", human_time(stats[ST_F_LASTCHG].u.u32, 1));
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003741 }
3742 else if (memcmp(field_str(stats, ST_F_STATUS), "no check", 5) == 0) {
Willy Tarreau60600742016-01-11 15:32:30 +01003743 chunk_strcat(out, "<i>no check</i>");
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003744 }
3745 else {
Willy Tarreau60600742016-01-11 15:32:30 +01003746 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 +01003747 if (memcmp(field_str(stats, ST_F_STATUS), "DOWN", 4) == 0) {
3748 if (stats[ST_F_CHECK_HEALTH].u.u32)
Willy Tarreau60600742016-01-11 15:32:30 +01003749 chunk_strcat(out, " &uarr;");
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003750 }
3751 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 +01003752 chunk_strcat(out, " &darr;");
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003753 }
3754
3755 if (memcmp(field_str(stats, ST_F_STATUS), "DOWN", 4) == 0 &&
3756 stats[ST_F_AGENT_STATUS].type && !stats[ST_F_AGENT_HEALTH].u.u32) {
Willy Tarreau60600742016-01-11 15:32:30 +01003757 chunk_appendf(out,
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003758 "</td><td class=ac><u> %s",
3759 field_str(stats, ST_F_AGENT_STATUS));
3760
3761 if (stats[ST_F_AGENT_CODE].type)
Willy Tarreau60600742016-01-11 15:32:30 +01003762 chunk_appendf(out, "/%d", stats[ST_F_AGENT_CODE].u.u32);
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003763
David Carlier08d35de2016-06-27 14:12:59 +01003764 if (stats[ST_F_AGENT_DURATION].type)
Willy Tarreau60600742016-01-11 15:32:30 +01003765 chunk_appendf(out, " in %lums", (long)stats[ST_F_AGENT_DURATION].u.u64);
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003766
Willy Tarreau60600742016-01-11 15:32:30 +01003767 chunk_appendf(out, "<div class=tips>%s", field_str(stats, ST_F_AGENT_DESC));
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003768
3769 if (*field_str(stats, ST_F_LAST_AGT)) {
Willy Tarreau60600742016-01-11 15:32:30 +01003770 chunk_appendf(out, ": ");
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003771 chunk_initstr(&src, field_str(stats, ST_F_LAST_AGT));
Willy Tarreau60600742016-01-11 15:32:30 +01003772 chunk_htmlencode(out, &src);
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003773 }
Willy Tarreau60600742016-01-11 15:32:30 +01003774 chunk_appendf(out, "</div></u>");
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003775 }
3776 else if (stats[ST_F_CHECK_STATUS].type) {
Willy Tarreau60600742016-01-11 15:32:30 +01003777 chunk_appendf(out,
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003778 "</td><td class=ac><u> %s",
3779 field_str(stats, ST_F_CHECK_STATUS));
3780
3781 if (stats[ST_F_CHECK_CODE].type)
Willy Tarreau60600742016-01-11 15:32:30 +01003782 chunk_appendf(out, "/%d", stats[ST_F_CHECK_CODE].u.u32);
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003783
David Carlier08d35de2016-06-27 14:12:59 +01003784 if (stats[ST_F_CHECK_DURATION].type)
Willy Tarreau60600742016-01-11 15:32:30 +01003785 chunk_appendf(out, " in %lums", (long)stats[ST_F_CHECK_DURATION].u.u64);
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003786
Willy Tarreau60600742016-01-11 15:32:30 +01003787 chunk_appendf(out, "<div class=tips>%s", field_str(stats, ST_F_CHECK_DESC));
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003788
3789 if (*field_str(stats, ST_F_LAST_CHK)) {
Willy Tarreau60600742016-01-11 15:32:30 +01003790 chunk_appendf(out, ": ");
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003791 chunk_initstr(&src, field_str(stats, ST_F_LAST_CHK));
Willy Tarreau60600742016-01-11 15:32:30 +01003792 chunk_htmlencode(out, &src);
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003793 }
Willy Tarreau60600742016-01-11 15:32:30 +01003794 chunk_appendf(out, "</div></u>");
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003795 }
3796 else
Willy Tarreau60600742016-01-11 15:32:30 +01003797 chunk_appendf(out, "</td><td>");
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003798
Willy Tarreau60600742016-01-11 15:32:30 +01003799 chunk_appendf(out,
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003800 /* weight */
3801 "</td><td class=ac>%d</td>"
3802 /* act, bck */
3803 "<td class=ac>%s</td><td class=ac>%s</td>"
3804 "",
3805 stats[ST_F_WEIGHT].u.u32,
3806 stats[ST_F_BCK].u.u32 ? "-" : "Y",
3807 stats[ST_F_BCK].u.u32 ? "Y" : "-");
3808
3809 /* check failures: unique, fatal, down time */
Willy Tarreaub96dd282016-11-09 14:45:51 +01003810 if (strcmp(field_str(stats, ST_F_STATUS), "MAINT (resolution)") == 0) {
3811 chunk_appendf(out, "<td class=ac colspan=3>resolution</td>");
3812 }
3813 else if (stats[ST_F_CHKFAIL].type) {
Willy Tarreau60600742016-01-11 15:32:30 +01003814 chunk_appendf(out, "<td><u>%lld", (long long)stats[ST_F_CHKFAIL].u.u64);
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003815
3816 if (stats[ST_F_HANAFAIL].type)
Willy Tarreau60600742016-01-11 15:32:30 +01003817 chunk_appendf(out, "/%lld", (long long)stats[ST_F_HANAFAIL].u.u64);
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003818
Willy Tarreau60600742016-01-11 15:32:30 +01003819 chunk_appendf(out,
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003820 "<div class=tips>Failed Health Checks%s</div></u></td>"
3821 "<td>%lld</td><td>%s</td>"
3822 "",
3823 stats[ST_F_HANAFAIL].type ? "/Health Analyses" : "",
3824 (long long)stats[ST_F_CHKDOWN].u.u64, human_time(stats[ST_F_DOWNTIME].u.u32, 1));
3825 }
3826 else if (strcmp(field_str(stats, ST_F_STATUS), "MAINT") != 0 && field_format(stats, ST_F_TRACKED) == FF_STR) {
3827 /* tracking a server (hence inherited maint would appear as "MAINT (via...)" */
Willy Tarreau60600742016-01-11 15:32:30 +01003828 chunk_appendf(out,
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003829 "<td class=ac colspan=3><a class=lfsb href=\"#%s\">via %s</a></td>",
3830 field_str(stats, ST_F_TRACKED), field_str(stats, ST_F_TRACKED));
3831 }
3832 else
Willy Tarreau60600742016-01-11 15:32:30 +01003833 chunk_appendf(out, "<td colspan=3></td>");
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003834
3835 /* throttle */
3836 if (stats[ST_F_THROTTLE].type)
Willy Tarreau60600742016-01-11 15:32:30 +01003837 chunk_appendf(out, "<td class=ac>%d %%</td></tr>\n", stats[ST_F_THROTTLE].u.u32);
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003838 else
Willy Tarreau60600742016-01-11 15:32:30 +01003839 chunk_appendf(out, "<td class=ac>-</td></tr>\n");
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003840 }
Willy Tarreaubbf84502016-01-08 17:25:50 +01003841 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_BE) {
Willy Tarreau60600742016-01-11 15:32:30 +01003842 chunk_appendf(out, "<tr class=\"backend\">");
Willy Tarreau508a63f2016-01-11 15:28:40 +01003843 if (flags & ST_SHOWADMIN) {
Willy Tarreaubbf84502016-01-08 17:25:50 +01003844 /* Column sub-heading for Enable or Disable server */
Willy Tarreau60600742016-01-11 15:32:30 +01003845 chunk_appendf(out, "<td></td>");
Willy Tarreaubbf84502016-01-08 17:25:50 +01003846 }
Willy Tarreau60600742016-01-11 15:32:30 +01003847 chunk_appendf(out,
Willy Tarreaubbf84502016-01-08 17:25:50 +01003848 "<td class=ac>"
3849 /* name */
3850 "%s<a name=\"%s/Backend\"></a>"
3851 "<a class=lfsb href=\"#%s/Backend\">Backend</a>"
3852 "",
3853 (flags & ST_SHLGNDS)?"<u>":"",
3854 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_PXNAME));
3855
3856 if (flags & ST_SHLGNDS) {
3857 /* balancing */
Willy Tarreau60600742016-01-11 15:32:30 +01003858 chunk_appendf(out, "<div class=tips>balancing: %s",
Willy Tarreauf1516d92016-01-11 14:48:36 +01003859 field_str(stats, ST_F_ALGO));
Willy Tarreaubbf84502016-01-08 17:25:50 +01003860
3861 /* cookie */
3862 if (stats[ST_F_COOKIE].type) {
Willy Tarreau60600742016-01-11 15:32:30 +01003863 chunk_appendf(out, ", cookie: '");
Willy Tarreaubbf84502016-01-08 17:25:50 +01003864 chunk_initstr(&src, field_str(stats, ST_F_COOKIE));
Willy Tarreau60600742016-01-11 15:32:30 +01003865 chunk_htmlencode(out, &src);
3866 chunk_appendf(out, "'");
Willy Tarreaubbf84502016-01-08 17:25:50 +01003867 }
Willy Tarreau60600742016-01-11 15:32:30 +01003868 chunk_appendf(out, "</div>");
Willy Tarreaubbf84502016-01-08 17:25:50 +01003869 }
3870
Willy Tarreau60600742016-01-11 15:32:30 +01003871 chunk_appendf(out,
Willy Tarreaubbf84502016-01-08 17:25:50 +01003872 "%s</td>"
3873 /* queue : current, max */
3874 "<td>%s</td><td>%s</td><td></td>"
3875 /* sessions rate : current, max, limit */
3876 "<td>%s</td><td>%s</td><td></td>"
3877 "",
3878 (flags & ST_SHLGNDS)?"</u>":"",
3879 U2H(stats[ST_F_QCUR].u.u32), U2H(stats[ST_F_QMAX].u.u32),
3880 U2H(stats[ST_F_RATE].u.u32), U2H(stats[ST_F_RATE_MAX].u.u32));
3881
Willy Tarreau60600742016-01-11 15:32:30 +01003882 chunk_appendf(out,
Willy Tarreaubbf84502016-01-08 17:25:50 +01003883 /* sessions: current, max, limit, total */
3884 "<td>%s</td><td>%s</td><td>%s</td>"
3885 "<td><u>%s<div class=tips><table class=det>"
3886 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
3887 "",
3888 U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SLIM].u.u32),
3889 U2H(stats[ST_F_STOT].u.u64),
3890 U2H(stats[ST_F_STOT].u.u64));
3891
3892 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
Willy Tarreauf8211df2016-01-11 14:09:38 +01003893 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
Willy Tarreau60600742016-01-11 15:32:30 +01003894 chunk_appendf(out,
Willy Tarreaubbf84502016-01-08 17:25:50 +01003895 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
3896 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
3897 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
3898 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
3899 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
3900 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
3901 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
3902 "<tr><th>- other responses:</th><td>%s</td></tr>"
3903 "<tr><th>Intercepted requests:</th><td>%s</td></tr>"
3904 "<tr><th colspan=3>Avg over last 1024 success. conn.</th></tr>"
3905 "",
3906 U2H(stats[ST_F_REQ_TOT].u.u64),
3907 U2H(stats[ST_F_HRSP_1XX].u.u64),
3908 U2H(stats[ST_F_HRSP_2XX].u.u64),
3909 U2H(stats[ST_F_COMP_RSP].u.u64),
3910 stats[ST_F_HRSP_2XX].u.u64 ?
3911 (int)(100 * stats[ST_F_COMP_RSP].u.u64 / stats[ST_F_HRSP_2XX].u.u64) : 0,
3912 U2H(stats[ST_F_HRSP_3XX].u.u64),
3913 U2H(stats[ST_F_HRSP_4XX].u.u64),
3914 U2H(stats[ST_F_HRSP_5XX].u.u64),
3915 U2H(stats[ST_F_HRSP_OTHER].u.u64),
Willy Tarreau5b9bdff2016-01-11 14:40:47 +01003916 U2H(stats[ST_F_INTERCEPTED].u.u64));
Willy Tarreaubbf84502016-01-08 17:25:50 +01003917 }
3918
Willy Tarreau60600742016-01-11 15:32:30 +01003919 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 +02003920 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 +01003921 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
Willy Tarreau60600742016-01-11 15:32:30 +01003922 chunk_appendf(out, "<tr><th>- Response time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_RTIME].u.u32));
3923 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 +01003924
Willy Tarreau60600742016-01-11 15:32:30 +01003925 chunk_appendf(out,
Willy Tarreaubbf84502016-01-08 17:25:50 +01003926 "</table></div></u></td>"
3927 /* sessions: lbtot, last */
3928 "<td>%s</td><td>%s</td>"
3929 /* bytes: in */
3930 "<td>%s</td>"
3931 "",
3932 U2H(stats[ST_F_LBTOT].u.u64),
3933 human_time(stats[ST_F_LASTSESS].u.s32, 1),
3934 U2H(stats[ST_F_BIN].u.u64));
3935
Willy Tarreau60600742016-01-11 15:32:30 +01003936 chunk_appendf(out,
Willy Tarreaubbf84502016-01-08 17:25:50 +01003937 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
3938 "<td>%s%s<div class=tips><table class=det>"
3939 "<tr><th>Response bytes in:</th><td>%s</td></tr>"
3940 "<tr><th>Compression in:</th><td>%s</td></tr>"
3941 "<tr><th>Compression out:</th><td>%s</td><td>(%d%%)</td></tr>"
3942 "<tr><th>Compression bypass:</th><td>%s</td></tr>"
3943 "<tr><th>Total bytes saved:</th><td>%s</td><td>(%d%%)</td></tr>"
3944 "</table></div>%s</td>",
3945 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "<u>":"",
3946 U2H(stats[ST_F_BOUT].u.u64),
3947 U2H(stats[ST_F_BOUT].u.u64),
3948 U2H(stats[ST_F_COMP_IN].u.u64),
3949 U2H(stats[ST_F_COMP_OUT].u.u64),
3950 stats[ST_F_COMP_IN].u.u64 ? (int)(stats[ST_F_COMP_OUT].u.u64 * 100 / stats[ST_F_COMP_IN].u.u64) : 0,
3951 U2H(stats[ST_F_COMP_BYP].u.u64),
Christopher Faulet0b64f622016-04-28 15:09:31 +02003952 U2H(stats[ST_F_COMP_IN].u.u64 - stats[ST_F_COMP_OUT].u.u64),
Willy Tarreaubbf84502016-01-08 17:25:50 +01003953 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,
3954 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "</u>":"");
3955
Willy Tarreau60600742016-01-11 15:32:30 +01003956 chunk_appendf(out,
Willy Tarreaubbf84502016-01-08 17:25:50 +01003957 /* denied: req, resp */
3958 "<td>%s</td><td>%s</td>"
3959 /* errors : request, connect */
3960 "<td></td><td>%s</td>"
3961 /* errors : response */
3962 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
3963 /* warnings: retries, redispatches */
3964 "<td>%lld</td><td>%lld</td>"
3965 /* backend status: reflect backend status (up/down): we display UP
3966 * if the backend has known working servers or if it has no server at
3967 * all (eg: for stats). Then we display the total weight, number of
3968 * active and backups. */
3969 "<td class=ac>%s %s</td><td class=ac>&nbsp;</td><td class=ac>%d</td>"
3970 "<td class=ac>%d</td><td class=ac>%d</td>"
3971 "",
3972 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
3973 U2H(stats[ST_F_ECON].u.u64),
3974 U2H(stats[ST_F_ERESP].u.u64),
3975 (long long)stats[ST_F_CLI_ABRT].u.u64,
3976 (long long)stats[ST_F_SRV_ABRT].u.u64,
3977 (long long)stats[ST_F_WRETR].u.u64, (long long)stats[ST_F_WREDIS].u.u64,
3978 human_time(stats[ST_F_LASTCHG].u.u32, 1),
3979 strcmp(field_str(stats, ST_F_STATUS), "DOWN") ? field_str(stats, ST_F_STATUS) : "<font color=\"red\"><b>DOWN</b></font>",
3980 stats[ST_F_WEIGHT].u.u32,
3981 stats[ST_F_ACT].u.u32, stats[ST_F_BCK].u.u32);
3982
Willy Tarreau60600742016-01-11 15:32:30 +01003983 chunk_appendf(out,
Willy Tarreaubbf84502016-01-08 17:25:50 +01003984 /* rest of backend: nothing, down transitions, total downtime, throttle */
3985 "<td class=ac>&nbsp;</td><td>%d</td>"
3986 "<td>%s</td>"
3987 "<td></td>"
3988 "</tr>",
3989 stats[ST_F_CHKDOWN].u.u32,
3990 stats[ST_F_DOWNTIME].type ? human_time(stats[ST_F_DOWNTIME].u.u32, 1) : "&nbsp;");
3991 }
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003992 return 1;
3993}
3994
Willy Tarreau501f6022016-01-08 17:37:22 +01003995static int stats_dump_one_line(const struct field *stats, unsigned int flags, struct proxy *px, struct appctx *appctx)
3996{
Willy Tarreau508a63f2016-01-11 15:28:40 +01003997 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN))
3998 flags |= ST_SHOWADMIN;
Willy Tarreau501f6022016-01-08 17:37:22 +01003999
4000 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
Willy Tarreau60600742016-01-11 15:32:30 +01004001 return stats_dump_fields_html(&trash, stats, flags);
Willy Tarreau1e62df92016-01-11 18:57:53 +01004002 else if (appctx->ctx.stats.flags & STAT_FMT_TYPED)
4003 return stats_dump_fields_typed(&trash, stats);
Willy Tarreau501f6022016-01-08 17:37:22 +01004004 else
4005 return stats_dump_fields_csv(&trash, stats);
4006}
4007
Thierry Fournier23d2d642016-03-25 08:20:11 +01004008/* Fill <stats> with the frontend statistics. <stats> is
4009 * preallocated array of length <len>. The length of the array
4010 * must be at least ST_F_TOTAL_FIELDS. If this length is less then
4011 * this value, the function returns 0, otherwise, it returns 1.
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004012 */
Thierry Fournier23d2d642016-03-25 08:20:11 +01004013int stats_fill_fe_stats(struct proxy *px, struct field *stats, int len)
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004014{
Thierry Fournier23d2d642016-03-25 08:20:11 +01004015 if (len < ST_F_TOTAL_FIELDS)
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004016 return 0;
4017
Thierry Fournier23d2d642016-03-25 08:20:11 +01004018 memset(stats, 0, sizeof(*stats) * len);
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004019
4020 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
4021 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, "FRONTEND");
Willy Tarreauf8211df2016-01-11 14:09:38 +01004022 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004023 stats[ST_F_SCUR] = mkf_u32(0, px->feconn);
4024 stats[ST_F_SMAX] = mkf_u32(FN_MAX, px->fe_counters.conn_max);
4025 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, px->maxconn);
4026 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, px->fe_counters.cum_sess);
4027 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, px->fe_counters.bytes_in);
4028 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, px->fe_counters.bytes_out);
4029 stats[ST_F_DREQ] = mkf_u64(FN_COUNTER, px->fe_counters.denied_req);
4030 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, px->fe_counters.denied_resp);
4031 stats[ST_F_EREQ] = mkf_u64(FN_COUNTER, px->fe_counters.failed_req);
Willy Tarreau8a90b8e2016-10-21 18:15:32 +02004032 stats[ST_F_DCON] = mkf_u64(FN_COUNTER, px->fe_counters.denied_conn);
Willy Tarreaua5bc36b2016-10-21 18:16:27 +02004033 stats[ST_F_DSES] = mkf_u64(FN_COUNTER, px->fe_counters.denied_sess);
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004034 stats[ST_F_STATUS] = mkf_str(FO_STATUS, px->state == PR_STREADY ? "OPEN" : px->state == PR_STFULL ? "FULL" : "STOP");
4035 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
4036 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
4037 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, 0);
4038 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_FE);
4039 stats[ST_F_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_sess_per_sec));
4040 stats[ST_F_RATE_LIM] = mkf_u32(FO_CONFIG|FN_LIMIT, px->fe_sps_lim);
4041 stats[ST_F_RATE_MAX] = mkf_u32(FN_MAX, px->fe_counters.sps_max);
4042
4043 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
4044 if (px->mode == PR_MODE_HTTP) {
4045 stats[ST_F_HRSP_1XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[1]);
4046 stats[ST_F_HRSP_2XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[2]);
4047 stats[ST_F_HRSP_3XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[3]);
4048 stats[ST_F_HRSP_4XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[4]);
4049 stats[ST_F_HRSP_5XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[5]);
4050 stats[ST_F_HRSP_OTHER] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[0]);
Willy Tarreau5b9bdff2016-01-11 14:40:47 +01004051 stats[ST_F_INTERCEPTED] = mkf_u64(FN_COUNTER, px->fe_counters.intercepted_req);
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004052 }
4053
4054 /* requests : req_rate, req_rate_max, req_tot, */
4055 stats[ST_F_REQ_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_req_per_sec));
4056 stats[ST_F_REQ_RATE_MAX] = mkf_u32(FN_MAX, px->fe_counters.p.http.rps_max);
4057 stats[ST_F_REQ_TOT] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cum_req);
4058
4059 /* compression: in, out, bypassed, responses */
4060 stats[ST_F_COMP_IN] = mkf_u64(FN_COUNTER, px->fe_counters.comp_in);
4061 stats[ST_F_COMP_OUT] = mkf_u64(FN_COUNTER, px->fe_counters.comp_out);
4062 stats[ST_F_COMP_BYP] = mkf_u64(FN_COUNTER, px->fe_counters.comp_byp);
4063 stats[ST_F_COMP_RSP] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.comp_rsp);
4064
Willy Tarreauc73810f2016-01-11 13:52:04 +01004065 /* connections : conn_rate, conn_rate_max, conn_tot, conn_max */
4066 stats[ST_F_CONN_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_conn_per_sec));
4067 stats[ST_F_CONN_RATE_MAX] = mkf_u32(FN_MAX, px->fe_counters.cps_max);
4068 stats[ST_F_CONN_TOT] = mkf_u64(FN_COUNTER, px->fe_counters.cum_conn);
4069
Thierry Fournier23d2d642016-03-25 08:20:11 +01004070 return 1;
4071}
4072
4073/* Dumps a frontend's line to the trash for the current proxy <px> and uses
4074 * the state from stream interface <si>. The caller is responsible for clearing
4075 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
4076 */
4077static int stats_dump_fe_stats(struct stream_interface *si, struct proxy *px)
4078{
4079 struct appctx *appctx = __objt_appctx(si->end);
4080
4081 if (!(px->cap & PR_CAP_FE))
4082 return 0;
4083
4084 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_FE)))
4085 return 0;
4086
4087 if (!stats_fill_fe_stats(px, stats, ST_F_TOTAL_FIELDS))
4088 return 0;
4089
Willy Tarreau501f6022016-01-08 17:37:22 +01004090 return stats_dump_one_line(stats, 0, px, appctx);
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004091}
4092
Thierry Fournierc4456852016-03-25 08:20:49 +01004093/* Fill <stats> with the listener statistics. <stats> is
4094 * preallocated array of length <len>. The length of the array
4095 * must be at least ST_F_TOTAL_FIELDS. If this length is less
4096 * then this value, the function returns 0, otherwise, it
4097 * returns 1. <flags> can take the value ST_SHLGNDS.
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004098 */
Thierry Fournierc4456852016-03-25 08:20:49 +01004099int stats_fill_li_stats(struct proxy *px, struct listener *l, int flags,
4100 struct field *stats, int len)
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004101{
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004102 struct chunk *out = get_trash_chunk();
4103
Thierry Fournierc4456852016-03-25 08:20:49 +01004104 if (len < ST_F_TOTAL_FIELDS)
4105 return 0;
4106
Thierry Fournierac9d4672016-03-25 08:43:46 +01004107 if (!l->counters)
4108 return 0;
4109
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004110 chunk_reset(out);
Thierry Fournierc4456852016-03-25 08:20:49 +01004111 memset(stats, 0, sizeof(*stats) * len);
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004112
4113 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
4114 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, l->name);
Willy Tarreauf8211df2016-01-11 14:09:38 +01004115 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004116 stats[ST_F_SCUR] = mkf_u32(0, l->nbconn);
4117 stats[ST_F_SMAX] = mkf_u32(FN_MAX, l->counters->conn_max);
4118 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, l->maxconn);
4119 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, l->counters->cum_conn);
4120 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, l->counters->bytes_in);
4121 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, l->counters->bytes_out);
4122 stats[ST_F_DREQ] = mkf_u64(FN_COUNTER, l->counters->denied_req);
4123 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, l->counters->denied_resp);
4124 stats[ST_F_EREQ] = mkf_u64(FN_COUNTER, l->counters->failed_req);
Willy Tarreau8a90b8e2016-10-21 18:15:32 +02004125 stats[ST_F_DCON] = mkf_u64(FN_COUNTER, l->counters->denied_conn);
Willy Tarreaua5bc36b2016-10-21 18:16:27 +02004126 stats[ST_F_DSES] = mkf_u64(FN_COUNTER, l->counters->denied_sess);
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004127 stats[ST_F_STATUS] = mkf_str(FO_STATUS, (l->nbconn < l->maxconn) ? (l->state == LI_LIMITED) ? "WAITING" : "OPEN" : "FULL");
4128 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
4129 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
4130 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, l->luid);
4131 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_SO);
4132
4133 if (flags & ST_SHLGNDS) {
4134 char str[INET6_ADDRSTRLEN];
4135 int port;
4136
4137 port = get_host_port(&l->addr);
4138 switch (addr_to_str(&l->addr, str, sizeof(str))) {
4139 case AF_INET:
4140 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
4141 chunk_appendf(out, "%s:%d", str, port);
4142 break;
4143 case AF_INET6:
4144 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
4145 chunk_appendf(out, "[%s]:%d", str, port);
4146 break;
4147 case AF_UNIX:
4148 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, "unix");
4149 break;
4150 case -1:
4151 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
4152 chunk_strcat(out, strerror(errno));
4153 break;
4154 default: /* address family not supported */
4155 break;
4156 }
4157 }
4158
Thierry Fournierc4456852016-03-25 08:20:49 +01004159 return 1;
4160}
4161
4162/* Dumps a line for listener <l> and proxy <px> to the trash and uses the state
4163 * from stream interface <si>, and stats flags <flags>. The caller is responsible
4164 * for clearing the trash if needed. Returns non-zero if it emits anything, zero
4165 * otherwise.
4166 */
4167static int stats_dump_li_stats(struct stream_interface *si, struct proxy *px, struct listener *l, int flags)
4168{
4169 struct appctx *appctx = __objt_appctx(si->end);
4170
4171 if (!stats_fill_li_stats(px, l, flags, stats, ST_F_TOTAL_FIELDS))
4172 return 0;
4173
Willy Tarreau501f6022016-01-08 17:37:22 +01004174 return stats_dump_one_line(stats, flags, px, appctx);
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004175}
4176
4177enum srv_stats_state {
4178 SRV_STATS_STATE_DOWN = 0,
4179 SRV_STATS_STATE_DOWN_AGENT,
4180 SRV_STATS_STATE_GOING_UP,
4181 SRV_STATS_STATE_UP_GOING_DOWN,
4182 SRV_STATS_STATE_UP,
4183 SRV_STATS_STATE_NOLB_GOING_DOWN,
Simon Horman4d2eab62015-04-23 14:51:26 +09004184 SRV_STATS_STATE_NOLB,
4185 SRV_STATS_STATE_DRAIN_GOING_DOWN,
4186 SRV_STATS_STATE_DRAIN,
Simon Hormanb167b6b2015-04-23 14:51:29 +09004187 SRV_STATS_STATE_DRAIN_AGENT,
Simon Horman4d2eab62015-04-23 14:51:26 +09004188 SRV_STATS_STATE_NO_CHECK,
4189
4190 SRV_STATS_STATE_COUNT, /* Must be last */
4191};
4192
Willy Tarreau2b96cf12016-01-06 19:25:38 +01004193static const char *srv_hlt_st[SRV_STATS_STATE_COUNT] = {
4194 [SRV_STATS_STATE_DOWN] = "DOWN",
4195 [SRV_STATS_STATE_DOWN_AGENT] = "DOWN (agent)",
4196 [SRV_STATS_STATE_GOING_UP] = "DOWN %d/%d",
4197 [SRV_STATS_STATE_UP_GOING_DOWN] = "UP %d/%d",
4198 [SRV_STATS_STATE_UP] = "UP",
4199 [SRV_STATS_STATE_NOLB_GOING_DOWN] = "NOLB %d/%d",
4200 [SRV_STATS_STATE_NOLB] = "NOLB",
4201 [SRV_STATS_STATE_DRAIN_GOING_DOWN] = "DRAIN %d/%d",
4202 [SRV_STATS_STATE_DRAIN] = "DRAIN",
4203 [SRV_STATS_STATE_DRAIN_AGENT] = "DRAIN (agent)",
4204 [SRV_STATS_STATE_NO_CHECK] = "no check"
4205};
4206
Thierry Fournier61fe6c02016-03-25 08:21:21 +01004207/* Fill <stats> with the server statistics. <stats> is
4208 * preallocated array of length <len>. The length of the array
4209 * must be at least ST_F_TOTAL_FIELDS. If this length is less
4210 * then this value, the function returns 0, otherwise, it
4211 * returns 1. <flags> can take the value ST_SHLGNDS.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004212 */
Thierry Fournier61fe6c02016-03-25 08:21:21 +01004213int stats_fill_sv_stats(struct proxy *px, struct server *sv, int flags,
4214 struct field *stats, int len)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004215{
Willy Tarreau32091232014-05-16 13:52:00 +02004216 struct server *via, *ref;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004217 char str[INET6_ADDRSTRLEN];
Willy Tarreau2b96cf12016-01-06 19:25:38 +01004218 struct chunk *out = get_trash_chunk();
Willy Tarreauba2f2642016-01-07 09:54:40 +01004219 enum srv_stats_state state;
Willy Tarreau2b96cf12016-01-06 19:25:38 +01004220 char *fld_status;
Thierry Fournier61fe6c02016-03-25 08:21:21 +01004221
4222 if (len < ST_F_TOTAL_FIELDS)
4223 return 0;
4224
4225 memset(stats, 0, sizeof(*stats) * len);
4226
Willy Tarreau32091232014-05-16 13:52:00 +02004227 /* we have "via" which is the tracked server as described in the configuration,
4228 * and "ref" which is the checked server and the end of the chain.
4229 */
4230 via = sv->track ? sv->track : sv;
4231 ref = via;
4232 while (ref->track)
4233 ref = ref->track;
4234
Willy Tarreauba2f2642016-01-07 09:54:40 +01004235 if (sv->state == SRV_ST_RUNNING || sv->state == SRV_ST_STARTING) {
4236 if ((ref->check.state & CHK_ST_ENABLED) &&
4237 (ref->check.health < ref->check.rise + ref->check.fall - 1)) {
4238 state = SRV_STATS_STATE_UP_GOING_DOWN;
Willy Tarreauba2f2642016-01-07 09:54:40 +01004239 } else {
4240 state = SRV_STATS_STATE_UP;
Willy Tarreauba2f2642016-01-07 09:54:40 +01004241 }
4242
Willy Tarreauba2f2642016-01-07 09:54:40 +01004243 if (sv->admin & SRV_ADMF_DRAIN) {
4244 if (ref->agent.state & CHK_ST_ENABLED)
4245 state = SRV_STATS_STATE_DRAIN_AGENT;
4246 else if (state == SRV_STATS_STATE_UP_GOING_DOWN)
4247 state = SRV_STATS_STATE_DRAIN_GOING_DOWN;
4248 else
4249 state = SRV_STATS_STATE_DRAIN;
4250 }
4251
4252 if (state == SRV_STATS_STATE_UP && !(ref->check.state & CHK_ST_ENABLED)) {
4253 state = SRV_STATS_STATE_NO_CHECK;
Willy Tarreauba2f2642016-01-07 09:54:40 +01004254 }
4255 }
4256 else if (sv->state == SRV_ST_STOPPING) {
4257 if ((!(sv->check.state & CHK_ST_ENABLED) && !sv->track) ||
4258 (ref->check.health == ref->check.rise + ref->check.fall - 1)) {
4259 state = SRV_STATS_STATE_NOLB;
Willy Tarreauba2f2642016-01-07 09:54:40 +01004260 } else {
4261 state = SRV_STATS_STATE_NOLB_GOING_DOWN;
Willy Tarreauba2f2642016-01-07 09:54:40 +01004262 }
4263 }
4264 else { /* stopped */
4265 if ((ref->agent.state & CHK_ST_ENABLED) && !ref->agent.health) {
4266 state = SRV_STATS_STATE_DOWN_AGENT;
Willy Tarreauba2f2642016-01-07 09:54:40 +01004267 } else if ((ref->check.state & CHK_ST_ENABLED) && !ref->check.health) {
4268 state = SRV_STATS_STATE_DOWN; /* DOWN */
Willy Tarreauba2f2642016-01-07 09:54:40 +01004269 } else if ((ref->agent.state & CHK_ST_ENABLED) || (ref->check.state & CHK_ST_ENABLED)) {
4270 state = SRV_STATS_STATE_GOING_UP;
Willy Tarreauba2f2642016-01-07 09:54:40 +01004271 } else {
4272 state = SRV_STATS_STATE_DOWN; /* DOWN, unchecked */
Willy Tarreauba2f2642016-01-07 09:54:40 +01004273 }
4274 }
4275
Willy Tarreau2b96cf12016-01-06 19:25:38 +01004276 chunk_reset(out);
Willy Tarreau2b96cf12016-01-06 19:25:38 +01004277
4278 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
4279 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, sv->id);
Willy Tarreauf8211df2016-01-11 14:09:38 +01004280 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
Willy Tarreau2b96cf12016-01-06 19:25:38 +01004281 stats[ST_F_QCUR] = mkf_u32(0, sv->nbpend);
4282 stats[ST_F_QMAX] = mkf_u32(FN_MAX, sv->counters.nbpend_max);
4283 stats[ST_F_SCUR] = mkf_u32(0, sv->cur_sess);
4284 stats[ST_F_SMAX] = mkf_u32(FN_MAX, sv->counters.cur_sess_max);
4285
4286 if (sv->maxconn)
4287 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, sv->maxconn);
4288
4289 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, sv->counters.cum_sess);
4290 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, sv->counters.bytes_in);
4291 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, sv->counters.bytes_out);
4292 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, sv->counters.failed_secu);
4293 stats[ST_F_ECON] = mkf_u64(FN_COUNTER, sv->counters.failed_conns);
4294 stats[ST_F_ERESP] = mkf_u64(FN_COUNTER, sv->counters.failed_resp);
4295 stats[ST_F_WRETR] = mkf_u64(FN_COUNTER, sv->counters.retries);
4296 stats[ST_F_WREDIS] = mkf_u64(FN_COUNTER, sv->counters.redispatches);
4297
4298 /* status */
4299 fld_status = chunk_newstr(out);
Willy Tarreaub96dd282016-11-09 14:45:51 +01004300 if (sv->admin & SRV_ADMF_RMAINT)
4301 chunk_appendf(out, "MAINT (resolution)");
4302 else if (sv->admin & SRV_ADMF_IMAINT)
Willy Tarreau2b96cf12016-01-06 19:25:38 +01004303 chunk_appendf(out, "MAINT (via %s/%s)", via->proxy->id, via->id);
4304 else if (sv->admin & SRV_ADMF_MAINT)
4305 chunk_appendf(out, "MAINT");
4306 else
4307 chunk_appendf(out,
4308 srv_hlt_st[state],
4309 (ref->state != SRV_ST_STOPPED) ? (ref->check.health - ref->check.rise + 1) : (ref->check.health),
4310 (ref->state != SRV_ST_STOPPED) ? (ref->check.fall) : (ref->check.rise));
4311
4312 stats[ST_F_STATUS] = mkf_str(FO_STATUS, fld_status);
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004313 stats[ST_F_LASTCHG] = mkf_u32(FN_AGE, now.tv_sec - sv->last_change);
4314 stats[ST_F_WEIGHT] = mkf_u32(FN_AVG, (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv);
4315 stats[ST_F_ACT] = mkf_u32(FO_STATUS, (sv->flags & SRV_F_BACKUP) ? 0 : 1);
4316 stats[ST_F_BCK] = mkf_u32(FO_STATUS, (sv->flags & SRV_F_BACKUP) ? 1 : 0);
Willy Tarreau91861262007-10-17 17:06:05 +02004317
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004318 /* check failures: unique, fatal; last change, total downtime */
4319 if (sv->check.state & CHK_ST_ENABLED) {
4320 stats[ST_F_CHKFAIL] = mkf_u64(FN_COUNTER, sv->counters.failed_checks);
4321 stats[ST_F_CHKDOWN] = mkf_u64(FN_COUNTER, sv->counters.down_trans);
4322 stats[ST_F_DOWNTIME] = mkf_u32(FN_COUNTER, srv_downtime(sv));
4323 }
Willy Tarreau164d4a92016-01-06 19:48:21 +01004324
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004325 if (sv->maxqueue)
4326 stats[ST_F_QLIMIT] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->maxqueue);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004327
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004328 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
4329 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
4330 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, sv->puid);
Willy Tarreau91861262007-10-17 17:06:05 +02004331
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004332 if (sv->state == SRV_ST_STARTING && !server_is_draining(sv))
4333 stats[ST_F_THROTTLE] = mkf_u32(FN_AVG, server_throttle_rate(sv));
Willy Tarreauf5b1cc32014-06-17 12:20:59 +02004334
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004335 stats[ST_F_LBTOT] = mkf_u64(FN_COUNTER, sv->counters.cum_lbconn);
Willy Tarreau91861262007-10-17 17:06:05 +02004336
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004337 if (sv->track) {
4338 char *fld_track = chunk_newstr(out);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004339
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004340 chunk_appendf(out, "%s/%s", sv->track->proxy->id, sv->track->id);
4341 stats[ST_F_TRACKED] = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, fld_track);
4342 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004343
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004344 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_SV);
4345 stats[ST_F_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&sv->sess_per_sec));
4346 stats[ST_F_RATE_MAX] = mkf_u32(FN_MAX, sv->counters.sps_max);
Willy Tarreau164d4a92016-01-06 19:48:21 +01004347
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004348 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) {
4349 const char *fld_chksts;
Willy Tarreau164d4a92016-01-06 19:48:21 +01004350
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004351 fld_chksts = chunk_newstr(out);
4352 chunk_strcat(out, "* "); // for check in progress
4353 chunk_strcat(out, get_check_status_info(sv->check.status));
4354 if (!(sv->check.state & CHK_ST_INPROGRESS))
4355 fld_chksts += 2; // skip "* "
4356 stats[ST_F_CHECK_STATUS] = mkf_str(FN_OUTPUT, fld_chksts);
Willy Tarreau91861262007-10-17 17:06:05 +02004357
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004358 if (sv->check.status >= HCHK_STATUS_L57DATA)
4359 stats[ST_F_CHECK_CODE] = mkf_u32(FN_OUTPUT, sv->check.code);
Willy Tarreaucf2924b2014-05-23 12:15:15 +02004360
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004361 if (sv->check.status >= HCHK_STATUS_CHECKED)
4362 stats[ST_F_CHECK_DURATION] = mkf_u64(FN_DURATION, sv->check.duration);
Willy Tarreaucf2924b2014-05-23 12:15:15 +02004363
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004364 stats[ST_F_CHECK_DESC] = mkf_str(FN_OUTPUT, get_check_status_description(sv->check.status));
4365 stats[ST_F_LAST_CHK] = mkf_str(FN_OUTPUT, sv->check.desc);
4366 stats[ST_F_CHECK_RISE] = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.rise);
4367 stats[ST_F_CHECK_FALL] = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.fall);
4368 stats[ST_F_CHECK_HEALTH] = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.health);
4369 }
Willy Tarreaucf2924b2014-05-23 12:15:15 +02004370
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004371 if ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) {
4372 const char *fld_chksts;
Willy Tarreaudd7354b2016-01-08 13:47:26 +01004373
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004374 fld_chksts = chunk_newstr(out);
4375 chunk_strcat(out, "* "); // for check in progress
4376 chunk_strcat(out, get_check_status_info(sv->agent.status));
4377 if (!(sv->agent.state & CHK_ST_INPROGRESS))
4378 fld_chksts += 2; // skip "* "
4379 stats[ST_F_AGENT_STATUS] = mkf_str(FN_OUTPUT, fld_chksts);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004380
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004381 if (sv->agent.status >= HCHK_STATUS_L57DATA)
4382 stats[ST_F_AGENT_CODE] = mkf_u32(FN_OUTPUT, sv->agent.code);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004383
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004384 if (sv->agent.status >= HCHK_STATUS_CHECKED)
4385 stats[ST_F_AGENT_DURATION] = mkf_u64(FN_DURATION, sv->agent.duration);
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01004386
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004387 stats[ST_F_AGENT_DESC] = mkf_str(FN_OUTPUT, get_check_status_description(sv->agent.status));
4388 stats[ST_F_LAST_AGT] = mkf_str(FN_OUTPUT, sv->agent.desc);
4389 stats[ST_F_AGENT_RISE] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.rise);
4390 stats[ST_F_AGENT_FALL] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.fall);
4391 stats[ST_F_AGENT_HEALTH] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.health);
4392 }
Willy Tarreaudd7354b2016-01-08 13:47:26 +01004393
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004394 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
4395 if (px->mode == PR_MODE_HTTP) {
4396 stats[ST_F_HRSP_1XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[1]);
4397 stats[ST_F_HRSP_2XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[2]);
4398 stats[ST_F_HRSP_3XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[3]);
4399 stats[ST_F_HRSP_4XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[4]);
4400 stats[ST_F_HRSP_5XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[5]);
4401 stats[ST_F_HRSP_OTHER] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[0]);
4402 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004403
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004404 if (ref->observe)
4405 stats[ST_F_HANAFAIL] = mkf_u64(FN_COUNTER, sv->counters.failed_hana);
Willy Tarreau164d4a92016-01-06 19:48:21 +01004406
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004407 stats[ST_F_CLI_ABRT] = mkf_u64(FN_COUNTER, sv->counters.cli_aborts);
4408 stats[ST_F_SRV_ABRT] = mkf_u64(FN_COUNTER, sv->counters.srv_aborts);
4409 stats[ST_F_LASTSESS] = mkf_s32(FN_AGE, srv_lastsession(sv));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004410
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004411 stats[ST_F_QTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.q_time, TIME_STATS_SAMPLES));
4412 stats[ST_F_CTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.c_time, TIME_STATS_SAMPLES));
4413 stats[ST_F_RTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.d_time, TIME_STATS_SAMPLES));
4414 stats[ST_F_TTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.t_time, TIME_STATS_SAMPLES));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004415
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004416 if (flags & ST_SHLGNDS) {
4417 switch (addr_to_str(&sv->addr, str, sizeof(str))) {
4418 case AF_INET:
4419 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
4420 chunk_appendf(out, "%s:%d", str, get_host_port(&sv->addr));
4421 break;
4422 case AF_INET6:
4423 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
4424 chunk_appendf(out, "[%s]:%d", str, get_host_port(&sv->addr));
4425 break;
4426 case AF_UNIX:
4427 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, "unix");
4428 break;
4429 case -1:
4430 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
4431 chunk_strcat(out, strerror(errno));
4432 break;
4433 default: /* address family not supported */
4434 break;
Willy Tarreauf4659942013-11-28 10:50:06 +01004435 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004436
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004437 if (sv->cookie)
4438 stats[ST_F_COOKIE] = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, sv->cookie);
4439 }
4440
Thierry Fournier61fe6c02016-03-25 08:21:21 +01004441 return 1;
4442}
4443
4444/* Dumps a line for server <sv> and proxy <px> to the trash and uses the state
4445 * from stream interface <si>, stats flags <flags>, and server state <state>.
4446 * The caller is responsible for clearing the trash if needed. Returns non-zero
4447 * if it emits anything, zero otherwise.
4448 */
4449static int stats_dump_sv_stats(struct stream_interface *si, struct proxy *px, int flags, struct server *sv)
4450{
4451 struct appctx *appctx = __objt_appctx(si->end);
4452
4453 if (!stats_fill_sv_stats(px, sv, flags, stats, ST_F_TOTAL_FIELDS))
4454 return 0;
4455
Willy Tarreau501f6022016-01-08 17:37:22 +01004456 return stats_dump_one_line(stats, flags, px, appctx);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004457}
4458
Thierry Fournierd0a56c22016-03-25 08:21:51 +01004459/* Fill <stats> with the backend statistics. <stats> is
4460 * preallocated array of length <len>. The length of the array
4461 * must be at least ST_F_TOTAL_FIELDS. If this length is less
4462 * then this value, the function returns 0, otherwise, it
4463 * returns 1. <flags> can take the value ST_SHLGNDS.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004464 */
Thierry Fournierd0a56c22016-03-25 08:21:51 +01004465int stats_fill_be_stats(struct proxy *px, int flags, struct field *stats, int len)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004466{
Thierry Fournierd0a56c22016-03-25 08:21:51 +01004467 if (len < ST_F_TOTAL_FIELDS)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004468 return 0;
4469
Thierry Fournierd0a56c22016-03-25 08:21:51 +01004470 memset(stats, 0, sizeof(*stats) * len);
Willy Tarreauf6eecbe2016-01-06 16:14:50 +01004471
4472 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
4473 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, "BACKEND");
Willy Tarreauf8211df2016-01-11 14:09:38 +01004474 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
Willy Tarreauf6eecbe2016-01-06 16:14:50 +01004475 stats[ST_F_QCUR] = mkf_u32(0, px->nbpend);
4476 stats[ST_F_QMAX] = mkf_u32(FN_MAX, px->be_counters.nbpend_max);
4477 stats[ST_F_SCUR] = mkf_u32(FO_CONFIG|FN_LIMIT, px->beconn);
4478 stats[ST_F_SMAX] = mkf_u32(FN_MAX, px->be_counters.conn_max);
4479 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, px->fullconn);
4480 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, px->be_counters.cum_conn);
4481 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, px->be_counters.bytes_in);
4482 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, px->be_counters.bytes_out);
4483 stats[ST_F_DREQ] = mkf_u64(FN_COUNTER, px->be_counters.denied_req);
4484 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, px->be_counters.denied_resp);
4485 stats[ST_F_ECON] = mkf_u64(FN_COUNTER, px->be_counters.failed_conns);
4486 stats[ST_F_ERESP] = mkf_u64(FN_COUNTER, px->be_counters.failed_resp);
4487 stats[ST_F_WRETR] = mkf_u64(FN_COUNTER, px->be_counters.retries);
4488 stats[ST_F_WREDIS] = mkf_u64(FN_COUNTER, px->be_counters.redispatches);
4489 stats[ST_F_STATUS] = mkf_str(FO_STATUS, (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" : "DOWN");
4490 stats[ST_F_WEIGHT] = mkf_u32(FN_AVG, (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv);
4491 stats[ST_F_ACT] = mkf_u32(0, px->srv_act);
4492 stats[ST_F_BCK] = mkf_u32(0, px->srv_bck);
4493 stats[ST_F_CHKDOWN] = mkf_u64(FN_COUNTER, px->down_trans);
4494 stats[ST_F_LASTCHG] = mkf_u32(FN_AGE, now.tv_sec - px->last_change);
Willy Tarreau7344f472016-01-11 12:04:02 +01004495 if (px->srv)
4496 stats[ST_F_DOWNTIME] = mkf_u32(FN_COUNTER, be_downtime(px));
4497
Willy Tarreauf6eecbe2016-01-06 16:14:50 +01004498 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
4499 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
4500 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, 0);
4501 stats[ST_F_LBTOT] = mkf_u64(FN_COUNTER, px->be_counters.cum_lbconn);
4502 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_BE);
4503 stats[ST_F_RATE] = mkf_u32(0, read_freq_ctr(&px->be_sess_per_sec));
4504 stats[ST_F_RATE_MAX] = mkf_u32(0, px->be_counters.sps_max);
Willy Tarreauf1516d92016-01-11 14:48:36 +01004505
Willy Tarreaue4847c62016-01-08 15:43:54 +01004506 if (flags & ST_SHLGNDS) {
4507 if (px->cookie_name)
4508 stats[ST_F_COOKIE] = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, px->cookie_name);
Willy Tarreauf1516d92016-01-11 14:48:36 +01004509 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 +01004510 }
Willy Tarreauf6eecbe2016-01-06 16:14:50 +01004511
4512 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
4513 if (px->mode == PR_MODE_HTTP) {
4514 stats[ST_F_REQ_TOT] = mkf_u64(FN_COUNTER, px->be_counters.p.http.cum_req);
4515 stats[ST_F_HRSP_1XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[1]);
4516 stats[ST_F_HRSP_2XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[2]);
4517 stats[ST_F_HRSP_3XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[3]);
4518 stats[ST_F_HRSP_4XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[4]);
4519 stats[ST_F_HRSP_5XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[5]);
4520 stats[ST_F_HRSP_OTHER] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[0]);
Willy Tarreau5b9bdff2016-01-11 14:40:47 +01004521 stats[ST_F_INTERCEPTED] = mkf_u64(FN_COUNTER, px->be_counters.intercepted_req);
Willy Tarreauf6eecbe2016-01-06 16:14:50 +01004522 }
4523
4524 stats[ST_F_CLI_ABRT] = mkf_u64(FN_COUNTER, px->be_counters.cli_aborts);
4525 stats[ST_F_SRV_ABRT] = mkf_u64(FN_COUNTER, px->be_counters.srv_aborts);
4526
4527 /* compression: in, out, bypassed, responses */
4528 stats[ST_F_COMP_IN] = mkf_u64(FN_COUNTER, px->be_counters.comp_in);
4529 stats[ST_F_COMP_OUT] = mkf_u64(FN_COUNTER, px->be_counters.comp_out);
4530 stats[ST_F_COMP_BYP] = mkf_u64(FN_COUNTER, px->be_counters.comp_byp);
4531 stats[ST_F_COMP_RSP] = mkf_u64(FN_COUNTER, px->be_counters.p.http.comp_rsp);
4532 stats[ST_F_LASTSESS] = mkf_s32(FN_AGE, be_lastsession(px));
4533
4534 stats[ST_F_QTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.q_time, TIME_STATS_SAMPLES));
4535 stats[ST_F_CTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.c_time, TIME_STATS_SAMPLES));
4536 stats[ST_F_RTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.d_time, TIME_STATS_SAMPLES));
4537 stats[ST_F_TTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.t_time, TIME_STATS_SAMPLES));
4538
Thierry Fournierd0a56c22016-03-25 08:21:51 +01004539 return 1;
4540}
4541
4542/* Dumps a line for backend <px> to the trash for and uses the state from stream
4543 * interface <si> and stats flags <flags>. The caller is responsible for clearing
4544 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
4545 */
4546static int stats_dump_be_stats(struct stream_interface *si, struct proxy *px, int flags)
4547{
4548 struct appctx *appctx = __objt_appctx(si->end);
4549
4550 if (!(px->cap & PR_CAP_BE))
4551 return 0;
4552
4553 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_BE)))
4554 return 0;
4555
4556 if (!stats_fill_be_stats(px, flags, stats, ST_F_TOTAL_FIELDS))
4557 return 0;
4558
Willy Tarreau501f6022016-01-08 17:37:22 +01004559 return stats_dump_one_line(stats, flags, px, appctx);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004560}
4561
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004562/* Dumps the HTML table header for proxy <px> to the trash for and uses the state from
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004563 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004564 * for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004565 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004566static void stats_dump_html_px_hdr(struct stream_interface *si, struct proxy *px, struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004567{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004568 struct appctx *appctx = __objt_appctx(si->end);
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004569 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
4570
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004571 if (px->cap & PR_CAP_BE && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004572 /* A form to enable/disable this proxy servers */
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004573
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004574 /* 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 +02004575 scope_txt[0] = 0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004576 if (appctx->ctx.stats.scope_len) {
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004577 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau4e4292b2014-11-28 12:18:45 +01004578 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 +01004579 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004580 }
4581
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004582 chunk_appendf(&trash,
Jeff Buchbinder2dbbf4d2014-08-29 15:10:08 -05004583 "<form method=\"post\">");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004584 }
4585
4586 /* print a new table */
4587 chunk_appendf(&trash,
4588 "<table class=\"tbl\" width=\"100%%\">\n"
4589 "<tr class=\"titre\">"
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01004590 "<th class=\"pxname\" width=\"10%%\">");
4591
4592 chunk_appendf(&trash,
4593 "<a name=\"%s\"></a>%s"
4594 "<a class=px href=\"#%s\">%s</a>",
4595 px->id,
4596 (uri->flags & ST_SHLGNDS) ? "<u>":"",
4597 px->id, px->id);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004598
4599 if (uri->flags & ST_SHLGNDS) {
4600 /* cap, mode, id */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02004601 chunk_appendf(&trash, "<div class=tips>cap: %s, mode: %s, id: %d",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004602 proxy_cap_str(px->cap), proxy_mode_str(px->mode),
4603 px->uuid);
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01004604 chunk_appendf(&trash, "</div>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004605 }
4606
4607 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01004608 "%s</th>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004609 "<th class=\"%s\" width=\"90%%\">%s</th>"
4610 "</tr>\n"
4611 "</table>\n"
4612 "<table class=\"tbl\" width=\"100%%\">\n"
4613 "<tr class=\"titre\">",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004614 (uri->flags & ST_SHLGNDS) ? "</u>":"",
4615 px->desc ? "desc" : "empty", px->desc ? px->desc : "");
4616
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004617 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004618 /* Column heading for Enable or Disable server */
4619 chunk_appendf(&trash, "<th rowspan=2 width=1></th>");
Willy Tarreau91861262007-10-17 17:06:05 +02004620 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004621
4622 chunk_appendf(&trash,
4623 "<th rowspan=2></th>"
4624 "<th colspan=3>Queue</th>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05004625 "<th colspan=3>Session rate</th><th colspan=6>Sessions</th>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004626 "<th colspan=2>Bytes</th><th colspan=2>Denied</th>"
4627 "<th colspan=3>Errors</th><th colspan=2>Warnings</th>"
4628 "<th colspan=9>Server</th>"
4629 "</tr>\n"
4630 "<tr class=\"titre\">"
4631 "<th>Cur</th><th>Max</th><th>Limit</th>"
4632 "<th>Cur</th><th>Max</th><th>Limit</th><th>Cur</th><th>Max</th>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05004633 "<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 +01004634 "<th>Req</th><th>Resp</th><th>Req</th><th>Conn</th>"
4635 "<th>Resp</th><th>Retr</th><th>Redis</th>"
4636 "<th>Status</th><th>LastChk</th><th>Wght</th><th>Act</th>"
4637 "<th>Bck</th><th>Chk</th><th>Dwn</th><th>Dwntme</th>"
4638 "<th>Thrtle</th>\n"
4639 "</tr>");
Willy Tarreau91861262007-10-17 17:06:05 +02004640}
4641
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004642/* Dumps the HTML table trailer for proxy <px> to the trash for and uses the state from
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004643 * stream interface <si>. The caller is responsible for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004644 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004645static void stats_dump_html_px_end(struct stream_interface *si, struct proxy *px)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004646{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004647 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004648 chunk_appendf(&trash, "</table>");
4649
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004650 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004651 /* close the form used to enable/disable this proxy servers */
4652 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004653 "Choose the action to perform on the checked servers : "
4654 "<select name=action>"
4655 "<option value=\"\"></option>"
Willy Tarreaued7df902014-05-22 18:04:49 +02004656 "<option value=\"ready\">Set state to READY</option>"
4657 "<option value=\"drain\">Set state to DRAIN</option>"
Marco Corte8c27bca2014-07-02 17:49:34 +02004658 "<option value=\"maint\">Set state to MAINT</option>"
Willy Tarreau248a60e2014-05-23 14:59:48 +02004659 "<option value=\"dhlth\">Health: disable checks</option>"
4660 "<option value=\"ehlth\">Health: enable checks</option>"
4661 "<option value=\"hrunn\">Health: force UP</option>"
4662 "<option value=\"hnolb\">Health: force NOLB</option>"
4663 "<option value=\"hdown\">Health: force DOWN</option>"
4664 "<option value=\"dagent\">Agent: disable checks</option>"
4665 "<option value=\"eagent\">Agent: enable checks</option>"
4666 "<option value=\"arunn\">Agent: force UP</option>"
4667 "<option value=\"adown\">Agent: force DOWN</option>"
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004668 "<option value=\"shutdown\">Kill Sessions</option>"
4669 "</select>"
4670 "<input type=\"hidden\" name=\"b\" value=\"#%d\">"
4671 "&nbsp;<input type=\"submit\" value=\"Apply\">"
4672 "</form>",
4673 px->uuid);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004674 }
4675
4676 chunk_appendf(&trash, "<p>\n");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004677}
Willy Tarreau91861262007-10-17 17:06:05 +02004678
4679/*
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004680 * Dumps statistics for a proxy. The output is sent to the stream interface's
4681 * input buffer. Returns 0 if it had to stop dumping data because of lack of
4682 * buffer space, or non-zero if everything completed. This function is used
4683 * both by the CLI and the HTTP entry points, and is able to dump the output
4684 * in HTML or CSV formats. If the later, <uri> must be NULL.
Willy Tarreau91861262007-10-17 17:06:05 +02004685 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004686static int stats_dump_proxy_to_buffer(struct stream_interface *si, struct proxy *px, struct uri_auth *uri)
Willy Tarreau91861262007-10-17 17:06:05 +02004687{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004688 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau87b09662015-04-03 00:22:06 +02004689 struct stream *s = si_strm(si);
Willy Tarreau2bb4a962014-11-28 11:11:05 +01004690 struct channel *rep = si_ic(si);
Willy Tarreau44267702011-10-28 15:35:33 +02004691 struct server *sv, *svs; /* server and server-state, server-state=server or server->track */
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004692 struct listener *l;
Willy Tarreau0deb85a2016-01-08 15:33:18 +01004693 unsigned int flags;
4694
4695 if (uri)
4696 flags = uri->flags;
4697 else if (strm_li(s)->bind_conf->level >= ACCESS_LVL_OPER)
4698 flags = ST_SHLGNDS | ST_SHNODE | ST_SHDESC;
4699 else
4700 flags = ST_SHNODE | ST_SHDESC;
Willy Tarreau91861262007-10-17 17:06:05 +02004701
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004702 chunk_reset(&trash);
Willy Tarreau91861262007-10-17 17:06:05 +02004703
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004704 switch (appctx->ctx.stats.px_st) {
Willy Tarreau295a8372011-03-10 11:25:07 +01004705 case STAT_PX_ST_INIT:
Willy Tarreau91861262007-10-17 17:06:05 +02004706 /* we are on a new proxy */
Willy Tarreau91861262007-10-17 17:06:05 +02004707 if (uri && uri->scope) {
4708 /* we have a limited scope, we have to check the proxy name */
4709 struct stat_scope *scope;
4710 int len;
4711
4712 len = strlen(px->id);
4713 scope = uri->scope;
4714
4715 while (scope) {
4716 /* match exact proxy name */
4717 if (scope->px_len == len && !memcmp(px->id, scope->px_id, len))
4718 break;
4719
4720 /* match '.' which means 'self' proxy */
Willy Tarreau1388a3a2007-10-18 16:38:37 +02004721 if (!strcmp(scope->px_id, ".") && px == s->be)
Willy Tarreau91861262007-10-17 17:06:05 +02004722 break;
4723 scope = scope->next;
4724 }
4725
4726 /* proxy name not found : don't dump anything */
4727 if (scope == NULL)
4728 return 1;
4729 }
4730
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004731 /* if the user has requested a limited output and the proxy
4732 * name does not match, skip it.
4733 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004734 if (appctx->ctx.stats.scope_len &&
Willy Tarreau4e4292b2014-11-28 12:18:45 +01004735 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 +02004736 return 1;
4737
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004738 if ((appctx->ctx.stats.flags & STAT_BOUND) &&
4739 (appctx->ctx.stats.iid != -1) &&
4740 (px->uuid != appctx->ctx.stats.iid))
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01004741 return 1;
4742
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004743 appctx->ctx.stats.px_st = STAT_PX_ST_TH;
Willy Tarreau91861262007-10-17 17:06:05 +02004744 /* fall through */
4745
Willy Tarreau295a8372011-03-10 11:25:07 +01004746 case STAT_PX_ST_TH:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004747 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004748 stats_dump_html_px_hdr(si, px, uri);
Willy Tarreaubc18da12015-03-13 14:00:47 +01004749 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004750 si_applet_cant_put(si);
Willy Tarreau55bb8452007-10-17 18:44:57 +02004751 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01004752 }
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004753 }
Willy Tarreau91861262007-10-17 17:06:05 +02004754
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004755 appctx->ctx.stats.px_st = STAT_PX_ST_FE;
Willy Tarreau91861262007-10-17 17:06:05 +02004756 /* fall through */
4757
Willy Tarreau295a8372011-03-10 11:25:07 +01004758 case STAT_PX_ST_FE:
Willy Tarreau91861262007-10-17 17:06:05 +02004759 /* print the frontend */
Willy Tarreaubc18da12015-03-13 14:00:47 +01004760 if (stats_dump_fe_stats(si, px)) {
4761 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004762 si_applet_cant_put(si);
Willy Tarreau91861262007-10-17 17:06:05 +02004763 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01004764 }
4765 }
Willy Tarreau91861262007-10-17 17:06:05 +02004766
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004767 appctx->ctx.stats.l = px->conf.listeners.n;
4768 appctx->ctx.stats.px_st = STAT_PX_ST_LI;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004769 /* fall through */
4770
Willy Tarreau295a8372011-03-10 11:25:07 +01004771 case STAT_PX_ST_LI:
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004772 /* stats.l has been initialized above */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004773 for (; appctx->ctx.stats.l != &px->conf.listeners; appctx->ctx.stats.l = l->by_fe.n) {
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01004774 if (buffer_almost_full(rep->buf)) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004775 si_applet_cant_put(si);
Willy Tarreau4e33d862009-10-11 23:35:10 +02004776 return 0;
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01004777 }
Willy Tarreau4e33d862009-10-11 23:35:10 +02004778
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004779 l = LIST_ELEM(appctx->ctx.stats.l, struct listener *, by_fe);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004780 if (!l->counters)
4781 continue;
4782
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004783 if (appctx->ctx.stats.flags & STAT_BOUND) {
4784 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SO)))
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004785 break;
4786
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004787 if (appctx->ctx.stats.sid != -1 && l->luid != appctx->ctx.stats.sid)
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004788 continue;
4789 }
4790
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004791 /* print the frontend */
Willy Tarreau0deb85a2016-01-08 15:33:18 +01004792 if (stats_dump_li_stats(si, px, l, flags)) {
Willy Tarreaubc18da12015-03-13 14:00:47 +01004793 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004794 si_applet_cant_put(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004795 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01004796 }
4797 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004798 }
4799
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004800 appctx->ctx.stats.sv = px->srv; /* may be NULL */
4801 appctx->ctx.stats.px_st = STAT_PX_ST_SV;
Willy Tarreau91861262007-10-17 17:06:05 +02004802 /* fall through */
4803
Willy Tarreau295a8372011-03-10 11:25:07 +01004804 case STAT_PX_ST_SV:
Willy Tarreau91861262007-10-17 17:06:05 +02004805 /* stats.sv has been initialized above */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004806 for (; appctx->ctx.stats.sv != NULL; appctx->ctx.stats.sv = sv->next) {
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01004807 if (buffer_almost_full(rep->buf)) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004808 si_applet_cant_put(si);
Willy Tarreau4e33d862009-10-11 23:35:10 +02004809 return 0;
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01004810 }
Willy Tarreau4e33d862009-10-11 23:35:10 +02004811
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004812 sv = appctx->ctx.stats.sv;
Willy Tarreau91861262007-10-17 17:06:05 +02004813
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004814 if (appctx->ctx.stats.flags & STAT_BOUND) {
4815 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SV)))
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01004816 break;
4817
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004818 if (appctx->ctx.stats.sid != -1 && sv->puid != appctx->ctx.stats.sid)
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01004819 continue;
4820 }
4821
Willy Tarreau32091232014-05-16 13:52:00 +02004822 svs = sv;
4823 while (svs->track)
4824 svs = svs->track;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01004825
Willy Tarreauba2f2642016-01-07 09:54:40 +01004826 /* do not report servers which are DOWN and not changing state */
4827 if ((appctx->ctx.stats.flags & STAT_HIDE_DOWN) &&
4828 ((sv->admin & SRV_ADMF_MAINT) || /* server is in maintenance */
4829 (sv->state == SRV_ST_STOPPED && /* server is down */
4830 (!((svs->agent.state | svs->check.state) & CHK_ST_ENABLED) ||
4831 ((svs->agent.state & CHK_ST_ENABLED) && !svs->agent.health) ||
4832 ((svs->check.state & CHK_ST_ENABLED) && !svs->check.health))))) {
Willy Tarreau91861262007-10-17 17:06:05 +02004833 continue;
4834 }
4835
Willy Tarreau0deb85a2016-01-08 15:33:18 +01004836 if (stats_dump_sv_stats(si, px, flags, sv)) {
Willy Tarreaubc18da12015-03-13 14:00:47 +01004837 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004838 si_applet_cant_put(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004839 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01004840 }
4841 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004842 } /* for sv */
Cyril Bonté474be412010-10-12 00:14:36 +02004843
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004844 appctx->ctx.stats.px_st = STAT_PX_ST_BE;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004845 /* fall through */
Cyril Bonté70be45d2010-10-12 00:14:35 +02004846
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004847 case STAT_PX_ST_BE:
4848 /* print the backend */
Willy Tarreau0deb85a2016-01-08 15:33:18 +01004849 if (stats_dump_be_stats(si, px, flags)) {
Willy Tarreaubc18da12015-03-13 14:00:47 +01004850 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004851 si_applet_cant_put(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004852 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01004853 }
4854 }
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01004855
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004856 appctx->ctx.stats.px_st = STAT_PX_ST_END;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004857 /* fall through */
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01004858
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004859 case STAT_PX_ST_END:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004860 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004861 stats_dump_html_px_end(si, px);
Willy Tarreaubc18da12015-03-13 14:00:47 +01004862 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004863 si_applet_cant_put(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004864 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01004865 }
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004866 }
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01004867
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004868 appctx->ctx.stats.px_st = STAT_PX_ST_FIN;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004869 /* fall through */
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01004870
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004871 case STAT_PX_ST_FIN:
4872 return 1;
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01004873
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004874 default:
4875 /* unknown state, we should put an abort() here ! */
4876 return 1;
4877 }
4878}
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01004879
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004880/* Dumps the HTTP stats head block to the trash for and uses the per-uri
4881 * parameters <uri>. The caller is responsible for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004882 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004883static void stats_dump_html_head(struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004884{
4885 /* WARNING! This must fit in the first buffer !!! */
4886 chunk_appendf(&trash,
4887 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n"
4888 "\"http://www.w3.org/TR/html4/loose.dtd\">\n"
4889 "<html><head><title>Statistics Report for " PRODUCT_NAME "%s%s</title>\n"
4890 "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n"
4891 "<style type=\"text/css\"><!--\n"
4892 "body {"
4893 " font-family: arial, helvetica, sans-serif;"
4894 " font-size: 12px;"
4895 " font-weight: normal;"
4896 " color: black;"
4897 " background: white;"
4898 "}\n"
4899 "th,td {"
4900 " font-size: 10px;"
4901 "}\n"
4902 "h1 {"
4903 " font-size: x-large;"
4904 " margin-bottom: 0.5em;"
4905 "}\n"
4906 "h2 {"
4907 " font-family: helvetica, arial;"
4908 " font-size: x-large;"
4909 " font-weight: bold;"
4910 " font-style: italic;"
4911 " color: #6020a0;"
4912 " margin-top: 0em;"
4913 " margin-bottom: 0em;"
4914 "}\n"
4915 "h3 {"
4916 " font-family: helvetica, arial;"
4917 " font-size: 16px;"
4918 " font-weight: bold;"
4919 " color: #b00040;"
4920 " background: #e8e8d0;"
4921 " margin-top: 0em;"
4922 " margin-bottom: 0em;"
4923 "}\n"
4924 "li {"
4925 " margin-top: 0.25em;"
4926 " margin-right: 2em;"
4927 "}\n"
4928 ".hr {margin-top: 0.25em;"
4929 " border-color: black;"
4930 " border-bottom-style: solid;"
4931 "}\n"
4932 ".titre {background: #20D0D0;color: #000000; font-weight: bold; text-align: center;}\n"
4933 ".total {background: #20D0D0;color: #ffff80;}\n"
4934 ".frontend {background: #e8e8d0;}\n"
4935 ".socket {background: #d0d0d0;}\n"
4936 ".backend {background: #e8e8d0;}\n"
Simon Horman837bfa72015-04-23 14:51:27 +09004937 ".active_down {background: #ff9090;}\n"
4938 ".active_going_up {background: #ffd020;}\n"
4939 ".active_going_down {background: #ffffa0;}\n"
4940 ".active_up {background: #c0ffc0;}\n"
4941 ".active_nolb {background: #20a0ff;}\n"
4942 ".active_draining {background: #20a0FF;}\n"
4943 ".active_no_check {background: #e0e0e0;}\n"
4944 ".backup_down {background: #ff9090;}\n"
4945 ".backup_going_up {background: #ff80ff;}\n"
4946 ".backup_going_down {background: #c060ff;}\n"
4947 ".backup_up {background: #b0d0ff;}\n"
4948 ".backup_nolb {background: #90b0e0;}\n"
4949 ".backup_draining {background: #cc9900;}\n"
4950 ".backup_no_check {background: #e0e0e0;}\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004951 ".maintain {background: #c07820;}\n"
4952 ".rls {letter-spacing: 0.2em; margin-right: 1px;}\n" /* right letter spacing (used for grouping digits) */
4953 "\n"
4954 "a.px:link {color: #ffff40; text-decoration: none;}"
4955 "a.px:visited {color: #ffff40; text-decoration: none;}"
4956 "a.px:hover {color: #ffffff; text-decoration: none;}"
4957 "a.lfsb:link {color: #000000; text-decoration: none;}"
4958 "a.lfsb:visited {color: #000000; text-decoration: none;}"
4959 "a.lfsb:hover {color: #505050; text-decoration: none;}"
4960 "\n"
4961 "table.tbl { border-collapse: collapse; border-style: none;}\n"
4962 "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"
4963 "table.tbl td.ac { text-align: center;}\n"
4964 "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}\n"
4965 "table.tbl th.pxname { background: #b00040; color: #ffff40; font-weight: bold; border-style: solid solid none solid; padding: 2px 3px; white-space: nowrap;}\n"
4966 "table.tbl th.empty { border-style: none; empty-cells: hide; background: white;}\n"
4967 "table.tbl th.desc { background: white; border-style: solid solid none solid; text-align: left; padding: 2px 3px;}\n"
4968 "\n"
4969 "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n"
4970 "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n"
4971 "table.lgd td.noborder { border-style: none; padding: 2px; white-space: nowrap;}\n"
Willy Tarreau466c9b52012-12-23 02:25:03 +01004972 "table.det { border-collapse: collapse; border-style: none; }\n"
4973 "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 +01004974 "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 +01004975 "u {text-decoration:none; border-bottom: 1px dotted black;}\n"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02004976 "div.tips {\n"
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01004977 " display:block;\n"
4978 " visibility:hidden;\n"
4979 " z-index:2147483647;\n"
4980 " position:absolute;\n"
4981 " padding:2px 4px 3px;\n"
4982 " background:#f0f060; color:#000000;\n"
4983 " border:1px solid #7040c0;\n"
4984 " white-space:nowrap;\n"
4985 " font-style:normal;font-size:11px;font-weight:normal;\n"
4986 " -moz-border-radius:3px;-webkit-border-radius:3px;border-radius:3px;\n"
4987 " -moz-box-shadow:gray 2px 2px 3px;-webkit-box-shadow:gray 2px 2px 3px;box-shadow:gray 2px 2px 3px;\n"
4988 "}\n"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02004989 "u:hover div.tips {visibility:visible;}\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004990 "-->\n"
4991 "</style></head>\n",
4992 (uri->flags & ST_SHNODE) ? " on " : "",
4993 (uri->flags & ST_SHNODE) ? (uri->node ? uri->node : global.node) : ""
4994 );
4995}
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01004996
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004997/* Dumps the HTML stats information block to the trash for and uses the state from
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004998 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004999 * for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005000 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01005001static void stats_dump_html_info(struct stream_interface *si, struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005002{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005003 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005004 unsigned int up = (now.tv_sec - start_date.tv_sec);
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02005005 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01005006
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005007 /* WARNING! this has to fit the first packet too.
5008 * We are around 3.5 kB, add adding entries will
5009 * become tricky if we want to support 4kB buffers !
5010 */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005011 chunk_appendf(&trash,
5012 "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">"
5013 PRODUCT_NAME "%s</a></h1>\n"
5014 "<h2>Statistics Report for pid %d%s%s%s%s</h2>\n"
5015 "<hr width=\"100%%\" class=\"hr\">\n"
5016 "<h3>&gt; General process information</h3>\n"
5017 "<table border=0><tr><td align=\"left\" nowrap width=\"1%%\">\n"
5018 "<p><b>pid = </b> %d (process #%d, nbproc = %d)<br>\n"
5019 "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n"
5020 "<b>system limits:</b> memmax = %s%s; ulimit-n = %d<br>\n"
5021 "<b>maxsock = </b> %d; <b>maxconn = </b> %d; <b>maxpipes = </b> %d<br>\n"
5022 "current conns = %d; current pipes = %d/%d; conn rate = %d/sec<br>\n"
5023 "Running tasks: %d/%d; idle = %d %%<br>\n"
5024 "</td><td align=\"center\" nowrap>\n"
5025 "<table class=\"lgd\"><tr>\n"
Simon Horman837bfa72015-04-23 14:51:27 +09005026 "<td class=\"active_up\">&nbsp;</td><td class=\"noborder\">active UP </td>"
5027 "<td class=\"backup_up\">&nbsp;</td><td class=\"noborder\">backup UP </td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005028 "</tr><tr>\n"
Simon Horman837bfa72015-04-23 14:51:27 +09005029 "<td class=\"active_going_down\"></td><td class=\"noborder\">active UP, going down </td>"
5030 "<td class=\"backup_going_down\"></td><td class=\"noborder\">backup UP, going down </td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005031 "</tr><tr>\n"
Simon Horman837bfa72015-04-23 14:51:27 +09005032 "<td class=\"active_going_up\"></td><td class=\"noborder\">active DOWN, going up </td>"
5033 "<td class=\"backup_going_up\"></td><td class=\"noborder\">backup DOWN, going up </td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005034 "</tr><tr>\n"
Simon Horman837bfa72015-04-23 14:51:27 +09005035 "<td class=\"active_down\"></td><td class=\"noborder\">active or backup DOWN &nbsp;</td>"
5036 "<td class=\"active_no_check\"></td><td class=\"noborder\">not checked </td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005037 "</tr><tr>\n"
5038 "<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 -07005039 "</tr><tr>\n"
Simon Horman837bfa72015-04-23 14:51:27 +09005040 "<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 +01005041 "</tr></table>\n"
Willy Tarreau6b7764a2013-12-04 00:43:21 +01005042 "Note: \"NOLB\"/\"DRAIN\" = UP with load-balancing disabled."
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005043 "</td>"
5044 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
5045 "<b>Display option:</b><ul style=\"margin-top: 0.25em;\">"
5046 "",
5047 (uri->flags & ST_HIDEVER) ? "" : (STATS_VERSION_STRING),
5048 pid, (uri->flags & ST_SHNODE) ? " on " : "",
5049 (uri->flags & ST_SHNODE) ? (uri->node ? uri->node : global.node) : "",
5050 (uri->flags & ST_SHDESC) ? ": " : "",
5051 (uri->flags & ST_SHDESC) ? (uri->desc ? uri->desc : global.desc) : "",
5052 pid, relative_pid, global.nbproc,
5053 up / 86400, (up % 86400) / 3600,
5054 (up % 3600) / 60, (up % 60),
5055 global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited",
5056 global.rlimit_memmax ? " MB" : "",
5057 global.rlimit_nofile,
5058 global.maxsock, global.maxconn, global.maxpipes,
5059 actconn, pipes_used, pipes_used+pipes_free, read_freq_ctr(&global.conn_per_sec),
5060 run_queue_cur, nb_tasks_cur, idle_pct
5061 );
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005062
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005063 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Willy Tarreau4e4292b2014-11-28 12:18:45 +01005064 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 +01005065 scope_txt[appctx->ctx.stats.scope_len] = '\0';
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02005066
5067 chunk_appendf(&trash,
Jeff Buchbinder2dbbf4d2014-08-29 15:10:08 -05005068 "<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 +01005069 (appctx->ctx.stats.scope_len > 0) ? scope_txt : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02005070 STAT_SCOPE_TXT_MAXLEN);
5071
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005072 /* 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 +02005073 scope_txt[0] = 0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005074 if (appctx->ctx.stats.scope_len) {
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02005075 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau4e4292b2014-11-28 12:18:45 +01005076 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 +01005077 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02005078 }
5079
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005080 if (appctx->ctx.stats.flags & STAT_HIDE_DOWN)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005081 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02005082 "<li><a href=\"%s%s%s%s\">Show all servers</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005083 uri->uri_prefix,
5084 "",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005085 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02005086 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005087 else
5088 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02005089 "<li><a href=\"%s%s%s%s\">Hide 'DOWN' servers</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005090 uri->uri_prefix,
5091 ";up",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005092 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02005093 scope_txt);
Willy Tarreau91861262007-10-17 17:06:05 +02005094
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005095 if (uri->refresh > 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005096 if (appctx->ctx.stats.flags & STAT_NO_REFRESH)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005097 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02005098 "<li><a href=\"%s%s%s%s\">Enable refresh</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005099 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005100 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02005101 "",
5102 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005103 else
5104 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02005105 "<li><a href=\"%s%s%s%s\">Disable refresh</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005106 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005107 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02005108 ";norefresh",
5109 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005110 }
Willy Tarreau55bb8452007-10-17 18:44:57 +02005111
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005112 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02005113 "<li><a href=\"%s%s%s%s\">Refresh now</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005114 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005115 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
5116 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02005117 scope_txt);
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02005118
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005119 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02005120 "<li><a href=\"%s;csv%s%s\">CSV export</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005121 uri->uri_prefix,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02005122 (uri->refresh > 0) ? ";norefresh" : "",
5123 scope_txt);
Willy Tarreau4bab24d2007-11-30 18:16:29 +01005124
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005125 chunk_appendf(&trash,
5126 "</ul></td>"
5127 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
5128 "<b>External resources:</b><ul style=\"margin-top: 0.25em;\">\n"
5129 "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>\n"
5130 "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>\n"
5131 "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>\n"
5132 "</ul>"
5133 "</td>"
5134 "</tr></table>\n"
5135 ""
5136 );
Willy Tarreau4bab24d2007-11-30 18:16:29 +01005137
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005138 if (appctx->ctx.stats.st_code) {
5139 switch (appctx->ctx.stats.st_code) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005140 case STAT_STATUS_DONE:
5141 chunk_appendf(&trash,
Simon Horman837bfa72015-04-23 14:51:27 +09005142 "<p><div class=active_up>"
Willy Tarreauba6be982013-04-19 12:16:55 +02005143 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005144 "Action processed successfully."
Willy Tarreauba6be982013-04-19 12:16:55 +02005145 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005146 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
5147 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02005148 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005149 break;
5150 case STAT_STATUS_NONE:
5151 chunk_appendf(&trash,
Simon Horman837bfa72015-04-23 14:51:27 +09005152 "<p><div class=active_going_down>"
Willy Tarreauba6be982013-04-19 12:16:55 +02005153 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005154 "Nothing has changed."
Willy Tarreauba6be982013-04-19 12:16:55 +02005155 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005156 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
5157 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02005158 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005159 break;
5160 case STAT_STATUS_PART:
5161 chunk_appendf(&trash,
Simon Horman837bfa72015-04-23 14:51:27 +09005162 "<p><div class=active_going_down>"
Willy Tarreauba6be982013-04-19 12:16:55 +02005163 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005164 "Action partially processed.<br>"
5165 "Some server names are probably unknown or ambiguous (duplicated names in the backend)."
Willy Tarreauba6be982013-04-19 12:16:55 +02005166 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005167 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
5168 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02005169 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005170 break;
5171 case STAT_STATUS_ERRP:
5172 chunk_appendf(&trash,
Simon Horman837bfa72015-04-23 14:51:27 +09005173 "<p><div class=active_down>"
Willy Tarreauba6be982013-04-19 12:16:55 +02005174 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005175 "Action not processed because of invalid parameters."
5176 "<ul>"
5177 "<li>The action is maybe unknown.</li>"
5178 "<li>The backend name is probably unknown or ambiguous (duplicated names).</li>"
5179 "<li>Some server names are probably unknown or ambiguous (duplicated names in the backend).</li>"
5180 "</ul>"
Willy Tarreauba6be982013-04-19 12:16:55 +02005181 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005182 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
5183 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02005184 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005185 break;
5186 case STAT_STATUS_EXCD:
5187 chunk_appendf(&trash,
Simon Horman837bfa72015-04-23 14:51:27 +09005188 "<p><div class=active_down>"
Willy Tarreauba6be982013-04-19 12:16:55 +02005189 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005190 "<b>Action not processed : the buffer couldn't store all the data.<br>"
5191 "You should retry with less servers at a time.</b>"
Willy Tarreauba6be982013-04-19 12:16:55 +02005192 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005193 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
5194 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02005195 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005196 break;
5197 case STAT_STATUS_DENY:
5198 chunk_appendf(&trash,
Simon Horman837bfa72015-04-23 14:51:27 +09005199 "<p><div class=active_down>"
Willy Tarreauba6be982013-04-19 12:16:55 +02005200 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005201 "<b>Action denied.</b>"
Willy Tarreauba6be982013-04-19 12:16:55 +02005202 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005203 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
5204 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02005205 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005206 break;
5207 default:
5208 chunk_appendf(&trash,
Simon Horman837bfa72015-04-23 14:51:27 +09005209 "<p><div class=active_no_check>"
Willy Tarreauba6be982013-04-19 12:16:55 +02005210 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005211 "Unexpected result."
Willy Tarreauba6be982013-04-19 12:16:55 +02005212 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005213 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
5214 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02005215 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005216 }
5217 chunk_appendf(&trash, "<p>\n");
5218 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005219}
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01005220
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01005221/* Dumps the HTML stats trailer block to the trash. The caller is responsible
5222 * for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005223 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01005224static void stats_dump_html_end()
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005225{
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005226 chunk_appendf(&trash, "</body></html>\n");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005227}
Willy Tarreau7f062c42009-03-05 18:43:00 +01005228
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01005229/* This function dumps statistics onto the stream interface's read buffer in
5230 * either CSV or HTML format. <uri> contains some HTML-specific parameters that
Willy Tarreau306f8302013-07-08 15:53:06 +02005231 * are ignored for CSV format (hence <uri> may be NULL there). It returns 0 if
5232 * 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 +02005233 * and the stream must be closed, or -1 in case of any error. This function is
Willy Tarreau306f8302013-07-08 15:53:06 +02005234 * used by both the CLI and the HTTP handlers.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005235 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01005236static int stats_dump_stat_to_buffer(struct stream_interface *si, struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005237{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005238 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005239 struct channel *rep = si_ic(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005240 struct proxy *px;
Willy Tarreau7f062c42009-03-05 18:43:00 +01005241
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005242 chunk_reset(&trash);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02005243
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005244 switch (appctx->st2) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005245 case STAT_ST_INIT:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005246 appctx->st2 = STAT_ST_HEAD; /* let's start producing data */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005247 /* fall through */
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005248
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005249 case STAT_ST_HEAD:
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_head(uri);
Willy Tarreau1e62df92016-01-11 18:57:53 +01005252 else if (!(appctx->ctx.stats.flags & STAT_FMT_TYPED))
Willy Tarreau354898b2012-12-23 18:15:23 +01005253 stats_dump_csv_header();
Willy Tarreaud9b587f2010-02-26 10:05:55 +01005254
Willy Tarreaubc18da12015-03-13 14:00:47 +01005255 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02005256 si_applet_cant_put(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005257 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01005258 }
Willy Tarreauae526782010-03-04 20:34:23 +01005259
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005260 appctx->st2 = STAT_ST_INFO;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005261 /* fall through */
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01005262
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005263 case STAT_ST_INFO:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005264 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01005265 stats_dump_html_info(si, uri);
Willy Tarreaubc18da12015-03-13 14:00:47 +01005266 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02005267 si_applet_cant_put(si);
Willy Tarreau91861262007-10-17 17:06:05 +02005268 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01005269 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005270 }
Willy Tarreau91861262007-10-17 17:06:05 +02005271
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005272 appctx->ctx.stats.px = proxy;
5273 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
5274 appctx->st2 = STAT_ST_LIST;
Willy Tarreau91861262007-10-17 17:06:05 +02005275 /* fall through */
5276
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005277 case STAT_ST_LIST:
5278 /* dump proxies */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005279 while (appctx->ctx.stats.px) {
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01005280 if (buffer_almost_full(rep->buf)) {
Willy Tarreaufe127932015-04-21 19:23:39 +02005281 si_applet_cant_put(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005282 return 0;
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01005283 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005284
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005285 px = appctx->ctx.stats.px;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005286 /* skip the disabled proxies, global frontend and non-networked ones */
5287 if (px->state != PR_STSTOPPED && px->uuid > 0 && (px->cap & (PR_CAP_FE | PR_CAP_BE)))
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01005288 if (stats_dump_proxy_to_buffer(si, px, uri) == 0)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005289 return 0;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005290
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005291 appctx->ctx.stats.px = px->next;
5292 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005293 }
5294 /* here, we just have reached the last proxy */
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005295
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005296 appctx->st2 = STAT_ST_END;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005297 /* fall through */
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005298
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005299 case STAT_ST_END:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005300 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01005301 stats_dump_html_end();
Willy Tarreaubc18da12015-03-13 14:00:47 +01005302 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02005303 si_applet_cant_put(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005304 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01005305 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005306 }
Willy Tarreau55058a72012-11-21 08:27:21 +01005307
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005308 appctx->st2 = STAT_ST_FIN;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005309 /* fall through */
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02005310
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005311 case STAT_ST_FIN:
5312 return 1;
Willy Tarreau55058a72012-11-21 08:27:21 +01005313
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005314 default:
5315 /* unknown state ! */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005316 appctx->st2 = STAT_ST_FIN;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005317 return -1;
5318 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005319}
Willy Tarreauae526782010-03-04 20:34:23 +01005320
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005321/* We reached the stats page through a POST request. The appctx is
5322 * expected to have already been allocated by the caller.
Willy Tarreau347a35d2013-11-22 17:51:09 +01005323 * Parse the posted data and enable/disable servers if necessary.
5324 * Returns 1 if request was parsed or zero if it needs more data.
5325 */
5326static int stats_process_http_post(struct stream_interface *si)
5327{
Willy Tarreau87b09662015-04-03 00:22:06 +02005328 struct stream *s = si_strm(si);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005329 struct appctx *appctx = objt_appctx(si->end);
Willy Tarreau347a35d2013-11-22 17:51:09 +01005330
5331 struct proxy *px = NULL;
5332 struct server *sv = NULL;
5333
5334 char key[LINESIZE];
5335 int action = ST_ADM_ACTION_NONE;
5336 int reprocess = 0;
5337
5338 int total_servers = 0;
5339 int altered_servers = 0;
5340
5341 char *first_param, *cur_param, *next_param, *end_params;
5342 char *st_cur_param = NULL;
5343 char *st_next_param = NULL;
5344
5345 struct chunk *temp;
5346 int reql;
5347
5348 temp = get_trash_chunk();
Willy Tarreaueee5b512015-04-03 23:46:31 +02005349 if (temp->size < s->txn->req.body_len) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01005350 /* too large request */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005351 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005352 goto out;
5353 }
5354
Willy Tarreaueee5b512015-04-03 23:46:31 +02005355 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 +01005356 if (reql <= 0) {
5357 /* we need more data */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005358 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005359 return 0;
5360 }
5361
5362 first_param = temp->str;
5363 end_params = temp->str + reql;
5364 cur_param = next_param = end_params;
5365 *end_params = '\0';
5366
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005367 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005368
5369 /*
5370 * Parse the parameters in reverse order to only store the last value.
5371 * From the html form, the backend and the action are at the end.
5372 */
5373 while (cur_param > first_param) {
5374 char *value;
5375 int poffset, plen;
5376
5377 cur_param--;
5378
5379 if ((*cur_param == '&') || (cur_param == first_param)) {
5380 reprocess_servers:
5381 /* Parse the key */
5382 poffset = (cur_param != first_param ? 1 : 0);
5383 plen = next_param - cur_param + (cur_param == first_param ? 1 : 0);
5384 if ((plen > 0) && (plen <= sizeof(key))) {
5385 strncpy(key, cur_param + poffset, plen);
5386 key[plen - 1] = '\0';
5387 } else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005388 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005389 goto out;
5390 }
5391
5392 /* Parse the value */
5393 value = key;
5394 while (*value != '\0' && *value != '=') {
5395 value++;
5396 }
5397 if (*value == '=') {
5398 /* Ok, a value is found, we can mark the end of the key */
5399 *value++ = '\0';
5400 }
5401 if (url_decode(key) < 0 || url_decode(value) < 0)
5402 break;
5403
5404 /* Now we can check the key to see what to do */
5405 if (!px && (strcmp(key, "b") == 0)) {
Willy Tarreau9e0bb102015-05-26 11:24:42 +02005406 if ((px = proxy_be_by_name(value)) == NULL) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01005407 /* the backend name is unknown or ambiguous (duplicate names) */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005408 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005409 goto out;
5410 }
5411 }
5412 else if (!action && (strcmp(key, "action") == 0)) {
Willy Tarreaued7df902014-05-22 18:04:49 +02005413 if (strcmp(value, "ready") == 0) {
5414 action = ST_ADM_ACTION_READY;
5415 }
5416 else if (strcmp(value, "drain") == 0) {
5417 action = ST_ADM_ACTION_DRAIN;
5418 }
5419 else if (strcmp(value, "maint") == 0) {
5420 action = ST_ADM_ACTION_MAINT;
5421 }
5422 else if (strcmp(value, "shutdown") == 0) {
5423 action = ST_ADM_ACTION_SHUTDOWN;
5424 }
Willy Tarreau248a60e2014-05-23 14:59:48 +02005425 else if (strcmp(value, "dhlth") == 0) {
5426 action = ST_ADM_ACTION_DHLTH;
5427 }
5428 else if (strcmp(value, "ehlth") == 0) {
5429 action = ST_ADM_ACTION_EHLTH;
5430 }
5431 else if (strcmp(value, "hrunn") == 0) {
5432 action = ST_ADM_ACTION_HRUNN;
5433 }
5434 else if (strcmp(value, "hnolb") == 0) {
5435 action = ST_ADM_ACTION_HNOLB;
5436 }
5437 else if (strcmp(value, "hdown") == 0) {
5438 action = ST_ADM_ACTION_HDOWN;
5439 }
5440 else if (strcmp(value, "dagent") == 0) {
5441 action = ST_ADM_ACTION_DAGENT;
5442 }
5443 else if (strcmp(value, "eagent") == 0) {
5444 action = ST_ADM_ACTION_EAGENT;
5445 }
5446 else if (strcmp(value, "arunn") == 0) {
5447 action = ST_ADM_ACTION_ARUNN;
5448 }
5449 else if (strcmp(value, "adown") == 0) {
5450 action = ST_ADM_ACTION_ADOWN;
5451 }
Willy Tarreaued7df902014-05-22 18:04:49 +02005452 /* else these are the old supported methods */
5453 else if (strcmp(value, "disable") == 0) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01005454 action = ST_ADM_ACTION_DISABLE;
5455 }
5456 else if (strcmp(value, "enable") == 0) {
5457 action = ST_ADM_ACTION_ENABLE;
5458 }
5459 else if (strcmp(value, "stop") == 0) {
5460 action = ST_ADM_ACTION_STOP;
5461 }
5462 else if (strcmp(value, "start") == 0) {
5463 action = ST_ADM_ACTION_START;
5464 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01005465 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005466 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005467 goto out;
5468 }
5469 }
5470 else if (strcmp(key, "s") == 0) {
5471 if (!(px && action)) {
5472 /*
5473 * Indicates that we'll need to reprocess the parameters
5474 * as soon as backend and action are known
5475 */
5476 if (!reprocess) {
5477 st_cur_param = cur_param;
5478 st_next_param = next_param;
5479 }
5480 reprocess = 1;
5481 }
5482 else if ((sv = findserver(px, value)) != NULL) {
5483 switch (action) {
5484 case ST_ADM_ACTION_DISABLE:
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02005485 if (!(sv->admin & SRV_ADMF_FMAINT)) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01005486 altered_servers++;
5487 total_servers++;
Willy Tarreau8b428482016-11-07 15:53:43 +01005488 srv_set_admin_flag(sv, SRV_ADMF_FMAINT, "'disable' on stats page");
Willy Tarreau347a35d2013-11-22 17:51:09 +01005489 }
5490 break;
5491 case ST_ADM_ACTION_ENABLE:
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02005492 if (sv->admin & SRV_ADMF_FMAINT) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01005493 altered_servers++;
5494 total_servers++;
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02005495 srv_clr_admin_flag(sv, SRV_ADMF_FMAINT);
Willy Tarreau347a35d2013-11-22 17:51:09 +01005496 }
5497 break;
5498 case ST_ADM_ACTION_STOP:
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02005499 if (!(sv->admin & SRV_ADMF_FDRAIN)) {
Willy Tarreau8b428482016-11-07 15:53:43 +01005500 srv_set_admin_flag(sv, SRV_ADMF_FDRAIN, "'stop' on stats page");
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02005501 altered_servers++;
5502 total_servers++;
5503 }
5504 break;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005505 case ST_ADM_ACTION_START:
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02005506 if (sv->admin & SRV_ADMF_FDRAIN) {
5507 srv_clr_admin_flag(sv, SRV_ADMF_FDRAIN);
5508 altered_servers++;
5509 total_servers++;
5510 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01005511 break;
Willy Tarreau248a60e2014-05-23 14:59:48 +02005512 case ST_ADM_ACTION_DHLTH:
5513 if (sv->check.state & CHK_ST_CONFIGURED) {
5514 sv->check.state &= ~CHK_ST_ENABLED;
5515 altered_servers++;
5516 total_servers++;
5517 }
5518 break;
5519 case ST_ADM_ACTION_EHLTH:
5520 if (sv->check.state & CHK_ST_CONFIGURED) {
5521 sv->check.state |= CHK_ST_ENABLED;
5522 altered_servers++;
5523 total_servers++;
5524 }
5525 break;
5526 case ST_ADM_ACTION_HRUNN:
5527 if (!(sv->track)) {
5528 sv->check.health = sv->check.rise + sv->check.fall - 1;
5529 srv_set_running(sv, "changed from Web interface");
5530 altered_servers++;
5531 total_servers++;
5532 }
5533 break;
5534 case ST_ADM_ACTION_HNOLB:
5535 if (!(sv->track)) {
5536 sv->check.health = sv->check.rise + sv->check.fall - 1;
5537 srv_set_stopping(sv, "changed from Web interface");
5538 altered_servers++;
5539 total_servers++;
5540 }
5541 break;
5542 case ST_ADM_ACTION_HDOWN:
5543 if (!(sv->track)) {
5544 sv->check.health = 0;
5545 srv_set_stopped(sv, "changed from Web interface");
5546 altered_servers++;
5547 total_servers++;
5548 }
5549 break;
5550 case ST_ADM_ACTION_DAGENT:
5551 if (sv->agent.state & CHK_ST_CONFIGURED) {
5552 sv->agent.state &= ~CHK_ST_ENABLED;
5553 altered_servers++;
5554 total_servers++;
5555 }
5556 break;
5557 case ST_ADM_ACTION_EAGENT:
5558 if (sv->agent.state & CHK_ST_CONFIGURED) {
5559 sv->agent.state |= CHK_ST_ENABLED;
5560 altered_servers++;
5561 total_servers++;
5562 }
5563 break;
5564 case ST_ADM_ACTION_ARUNN:
5565 if (sv->agent.state & CHK_ST_ENABLED) {
5566 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
5567 srv_set_running(sv, "changed from Web interface");
5568 altered_servers++;
5569 total_servers++;
5570 }
5571 break;
5572 case ST_ADM_ACTION_ADOWN:
5573 if (sv->agent.state & CHK_ST_ENABLED) {
5574 sv->agent.health = 0;
5575 srv_set_stopped(sv, "changed from Web interface");
5576 altered_servers++;
5577 total_servers++;
5578 }
5579 break;
Willy Tarreaued7df902014-05-22 18:04:49 +02005580 case ST_ADM_ACTION_READY:
5581 srv_adm_set_ready(sv);
5582 altered_servers++;
5583 total_servers++;
5584 break;
5585 case ST_ADM_ACTION_DRAIN:
5586 srv_adm_set_drain(sv);
5587 altered_servers++;
5588 total_servers++;
5589 break;
5590 case ST_ADM_ACTION_MAINT:
5591 srv_adm_set_maint(sv);
5592 altered_servers++;
5593 total_servers++;
5594 break;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005595 case ST_ADM_ACTION_SHUTDOWN:
5596 if (px->state != PR_STSTOPPED) {
Willy Tarreau87b09662015-04-03 00:22:06 +02005597 struct stream *sess, *sess_bck;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005598
5599 list_for_each_entry_safe(sess, sess_bck, &sv->actconns, by_srv)
5600 if (sess->srv_conn == sv)
Willy Tarreaue7dff022015-04-03 01:14:29 +02005601 stream_shutdown(sess, SF_ERR_KILLED);
Willy Tarreau347a35d2013-11-22 17:51:09 +01005602
5603 altered_servers++;
5604 total_servers++;
5605 }
5606 break;
5607 }
5608 } else {
5609 /* the server name is unknown or ambiguous (duplicate names) */
5610 total_servers++;
5611 }
5612 }
5613 if (reprocess && px && action) {
5614 /* Now, we know the backend and the action chosen by the user.
5615 * We can safely restart from the first server parameter
5616 * to reprocess them
5617 */
5618 cur_param = st_cur_param;
5619 next_param = st_next_param;
5620 reprocess = 0;
5621 goto reprocess_servers;
5622 }
5623
5624 next_param = cur_param;
5625 }
5626 }
5627
5628 if (total_servers == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005629 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005630 }
5631 else if (altered_servers == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005632 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005633 }
5634 else if (altered_servers == total_servers) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005635 appctx->ctx.stats.st_code = STAT_STATUS_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005636 }
5637 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005638 appctx->ctx.stats.st_code = STAT_STATUS_PART;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005639 }
5640 out:
5641 return 1;
5642}
5643
5644
5645static int stats_send_http_headers(struct stream_interface *si)
5646{
Willy Tarreau87b09662015-04-03 00:22:06 +02005647 struct stream *s = si_strm(si);
Willy Tarreau347a35d2013-11-22 17:51:09 +01005648 struct uri_auth *uri = s->be->uri_auth;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005649 struct appctx *appctx = objt_appctx(si->end);
Willy Tarreau347a35d2013-11-22 17:51:09 +01005650
5651 chunk_printf(&trash,
Willy Tarreau8b8995f2014-04-24 22:51:54 +02005652 "HTTP/1.1 200 OK\r\n"
Willy Tarreau347a35d2013-11-22 17:51:09 +01005653 "Cache-Control: no-cache\r\n"
5654 "Connection: close\r\n"
5655 "Content-Type: %s\r\n",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005656 (appctx->ctx.stats.flags & STAT_FMT_HTML) ? "text/html" : "text/plain");
Willy Tarreau347a35d2013-11-22 17:51:09 +01005657
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005658 if (uri->refresh > 0 && !(appctx->ctx.stats.flags & STAT_NO_REFRESH))
Willy Tarreau347a35d2013-11-22 17:51:09 +01005659 chunk_appendf(&trash, "Refresh: %d\r\n",
5660 uri->refresh);
5661
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005662 /* we don't send the CRLF in chunked mode, it will be sent with the first chunk's size */
5663
5664 if (appctx->ctx.stats.flags & STAT_CHUNKED)
5665 chunk_appendf(&trash, "Transfer-Encoding: chunked\r\n");
5666 else
5667 chunk_appendf(&trash, "\r\n");
Willy Tarreau347a35d2013-11-22 17:51:09 +01005668
Willy Tarreaueee5b512015-04-03 23:46:31 +02005669 s->txn->status = 200;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005670 s->logs.tv_request = now;
5671
Willy Tarreaubc18da12015-03-13 14:00:47 +01005672 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02005673 si_applet_cant_put(si);
Willy Tarreau347a35d2013-11-22 17:51:09 +01005674 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01005675 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01005676
5677 return 1;
5678}
5679
5680static int stats_send_http_redirect(struct stream_interface *si)
5681{
5682 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
Willy Tarreau87b09662015-04-03 00:22:06 +02005683 struct stream *s = si_strm(si);
Willy Tarreau347a35d2013-11-22 17:51:09 +01005684 struct uri_auth *uri = s->be->uri_auth;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005685 struct appctx *appctx = objt_appctx(si->end);
Willy Tarreau347a35d2013-11-22 17:51:09 +01005686
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005687 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Willy Tarreau347a35d2013-11-22 17:51:09 +01005688 scope_txt[0] = 0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005689 if (appctx->ctx.stats.scope_len) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01005690 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau4e4292b2014-11-28 12:18:45 +01005691 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 +01005692 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005693 }
5694
5695 /* We don't want to land on the posted stats page because a refresh will
5696 * repost the data. We don't want this to happen on accident so we redirect
5697 * the browse to the stats page with a GET.
5698 */
5699 chunk_printf(&trash,
5700 "HTTP/1.1 303 See Other\r\n"
5701 "Cache-Control: no-cache\r\n"
5702 "Content-Type: text/plain\r\n"
5703 "Connection: close\r\n"
5704 "Location: %s;st=%s%s%s%s\r\n"
Willy Tarreaufdfcc9d2016-01-26 13:57:29 +01005705 "Content-length: 0\r\n"
Willy Tarreau347a35d2013-11-22 17:51:09 +01005706 "\r\n",
5707 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005708 ((appctx->ctx.stats.st_code > STAT_STATUS_INIT) &&
5709 (appctx->ctx.stats.st_code < STAT_STATUS_SIZE) &&
5710 stat_status_codes[appctx->ctx.stats.st_code]) ?
5711 stat_status_codes[appctx->ctx.stats.st_code] :
Willy Tarreau347a35d2013-11-22 17:51:09 +01005712 stat_status_codes[STAT_STATUS_UNKN],
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005713 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
5714 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreau347a35d2013-11-22 17:51:09 +01005715 scope_txt);
5716
Willy Tarreaueee5b512015-04-03 23:46:31 +02005717 s->txn->status = 303;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005718 s->logs.tv_request = now;
5719
Willy Tarreaubc18da12015-03-13 14:00:47 +01005720 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02005721 si_applet_cant_put(si);
Willy Tarreau347a35d2013-11-22 17:51:09 +01005722 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01005723 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01005724
5725 return 1;
5726}
5727
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005728/* This I/O handler runs as an applet embedded in a stream interface. It is
5729 * used to send HTTP stats over a TCP socket. The mechanism is very simple.
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005730 * appctx->st0 contains the operation in progress (dump, done). The handler
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005731 * automatically unregisters itself once transfer is complete.
5732 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02005733static void http_stats_io_handler(struct appctx *appctx)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005734{
Willy Tarreau00a37f02015-04-13 12:05:19 +02005735 struct stream_interface *si = appctx->owner;
Willy Tarreau87b09662015-04-03 00:22:06 +02005736 struct stream *s = si_strm(si);
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005737 struct channel *req = si_oc(si);
5738 struct channel *res = si_ic(si);
Willy Tarreau55058a72012-11-21 08:27:21 +01005739
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005740 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
5741 goto out;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01005742
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005743 /* check that the output is not closed */
5744 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005745 appctx->st0 = STAT_HTTP_DONE;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005746
Willy Tarreau347a35d2013-11-22 17:51:09 +01005747 /* all states are processed in sequence */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005748 if (appctx->st0 == STAT_HTTP_HEAD) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01005749 if (stats_send_http_headers(si)) {
Willy Tarreaueee5b512015-04-03 23:46:31 +02005750 if (s->txn->meth == HTTP_METH_HEAD)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005751 appctx->st0 = STAT_HTTP_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005752 else
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005753 appctx->st0 = STAT_HTTP_DUMP;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005754 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01005755 }
5756
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005757 if (appctx->st0 == STAT_HTTP_DUMP) {
Willy Tarreau4e4292b2014-11-28 12:18:45 +01005758 unsigned int prev_len = si_ib(si)->i;
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005759 unsigned int data_len;
5760 unsigned int last_len;
Willy Tarreaucce36482014-04-24 20:26:41 +02005761 unsigned int last_fwd = 0;
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005762
5763 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
5764 /* One difficulty we're facing is that we must prevent
5765 * the input data from being automatically forwarded to
5766 * the output area. For this, we temporarily disable
5767 * forwarding on the channel.
5768 */
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005769 last_fwd = si_ic(si)->to_forward;
5770 si_ic(si)->to_forward = 0;
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005771 chunk_printf(&trash, "\r\n000000\r\n");
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005772 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02005773 si_applet_cant_put(si);
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005774 si_ic(si)->to_forward = last_fwd;
Willy Tarreau828824a2015-04-19 17:20:03 +02005775 goto out;
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005776 }
5777 }
5778
Willy Tarreau4e4292b2014-11-28 12:18:45 +01005779 data_len = si_ib(si)->i;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005780 if (stats_dump_stat_to_buffer(si, s->be->uri_auth))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005781 appctx->st0 = STAT_HTTP_DONE;
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005782
Willy Tarreau4e4292b2014-11-28 12:18:45 +01005783 last_len = si_ib(si)->i;
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005784
5785 /* Now we must either adjust or remove the chunk size. This is
5786 * not easy because the chunk size might wrap at the end of the
5787 * buffer, so we pretend we have nothing in the buffer, we write
5788 * the size, then restore the buffer's contents. Note that we can
5789 * only do that because no forwarding is scheduled on the stats
5790 * applet.
5791 */
5792 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
Willy Tarreau4e4292b2014-11-28 12:18:45 +01005793 si_ic(si)->total -= (last_len - prev_len);
5794 si_ib(si)->i -= (last_len - prev_len);
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005795
5796 if (last_len != data_len) {
5797 chunk_printf(&trash, "\r\n%06x\r\n", (last_len - data_len));
Willy Tarreaubc18da12015-03-13 14:00:47 +01005798 if (bi_putchk(si_ic(si), &trash) == -1)
Willy Tarreaufe127932015-04-21 19:23:39 +02005799 si_applet_cant_put(si);
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005800
Willy Tarreau4e4292b2014-11-28 12:18:45 +01005801 si_ic(si)->total += (last_len - data_len);
5802 si_ib(si)->i += (last_len - data_len);
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005803 }
5804 /* now re-enable forwarding */
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005805 channel_forward(si_ic(si), last_fwd);
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005806 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005807 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02005808
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005809 if (appctx->st0 == STAT_HTTP_POST) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01005810 if (stats_process_http_post(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005811 appctx->st0 = STAT_HTTP_LAST;
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005812 else if (si_oc(si)->flags & CF_SHUTR)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005813 appctx->st0 = STAT_HTTP_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005814 }
5815
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005816 if (appctx->st0 == STAT_HTTP_LAST) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01005817 if (stats_send_http_redirect(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005818 appctx->st0 = STAT_HTTP_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005819 }
5820
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005821 if (appctx->st0 == STAT_HTTP_DONE) {
5822 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
5823 chunk_printf(&trash, "\r\n0\r\n\r\n");
Willy Tarreaubc18da12015-03-13 14:00:47 +01005824 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02005825 si_applet_cant_put(si);
Willy Tarreau828824a2015-04-19 17:20:03 +02005826 goto out;
Willy Tarreaubc18da12015-03-13 14:00:47 +01005827 }
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005828 }
5829 /* eat the whole request */
Willy Tarreau4e4292b2014-11-28 12:18:45 +01005830 bo_skip(si_oc(si), si_ob(si)->o);
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005831 res->flags |= CF_READ_NULL;
5832 si_shutr(si);
5833 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01005834
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005835 if ((res->flags & CF_SHUTR) && (si->state == SI_ST_EST))
5836 si_shutw(si);
Willy Tarreau91861262007-10-17 17:06:05 +02005837
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005838 if (appctx->st0 == STAT_HTTP_DONE) {
Willy Tarreau96d44912013-11-22 12:25:24 +01005839 if ((req->flags & CF_SHUTW) && (si->state == SI_ST_EST)) {
5840 si_shutr(si);
5841 res->flags |= CF_READ_NULL;
5842 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005843 }
Willy Tarreau828824a2015-04-19 17:20:03 +02005844 out:
Willy Tarreaud4da1962015-04-20 01:31:23 +02005845 /* just to make gcc happy */ ;
Willy Tarreau91861262007-10-17 17:06:05 +02005846}
5847
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005848
Willy Tarreau909d5172012-11-26 03:04:41 +01005849static inline const char *get_conn_ctrl_name(const struct connection *conn)
5850{
Willy Tarreau3c728722014-01-23 13:50:42 +01005851 if (!conn_ctrl_ready(conn))
Willy Tarreau909d5172012-11-26 03:04:41 +01005852 return "NONE";
5853 return conn->ctrl->name;
5854}
5855
5856static inline const char *get_conn_xprt_name(const struct connection *conn)
5857{
Willy Tarreau578fa022016-11-24 15:21:26 +01005858 static char ptr[19];
Willy Tarreau909d5172012-11-26 03:04:41 +01005859
Willy Tarreauaad69382014-01-23 14:21:42 +01005860 if (!conn_xprt_ready(conn))
Willy Tarreau909d5172012-11-26 03:04:41 +01005861 return "NONE";
5862
5863 if (conn->xprt == &raw_sock)
5864 return "RAW";
5865
5866#ifdef USE_OPENSSL
5867 if (conn->xprt == &ssl_sock)
5868 return "SSL";
5869#endif
5870 snprintf(ptr, sizeof(ptr), "%p", conn->xprt);
5871 return ptr;
5872}
5873
5874static inline const char *get_conn_data_name(const struct connection *conn)
5875{
Willy Tarreau578fa022016-11-24 15:21:26 +01005876 static char ptr[19];
Willy Tarreau909d5172012-11-26 03:04:41 +01005877
5878 if (!conn->data)
5879 return "NONE";
5880
5881 if (conn->data == &sess_conn_cb)
5882 return "SESS";
5883
5884 if (conn->data == &si_conn_cb)
5885 return "STRM";
5886
5887 if (conn->data == &check_conn_cb)
5888 return "CHCK";
5889
5890 snprintf(ptr, sizeof(ptr), "%p", conn->data);
5891 return ptr;
5892}
5893
Willy Tarreau87b09662015-04-03 00:22:06 +02005894/* This function dumps a complete stream state onto the stream interface's
5895 * read buffer. The stream has to be set in sess->target. It returns
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01005896 * 0 if the output buffer is full and it needs to be called again, otherwise
5897 * non-zero. It is designed to be called from stats_dump_sess_to_buffer() below.
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005898 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005899static int stats_dump_full_sess_to_buffer(struct stream_interface *si, struct stream *sess)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005900{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005901 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005902 struct tm tm;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005903 extern const char *monthname[12];
5904 char pn[INET6_ADDRSTRLEN];
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005905 struct connection *conn;
Willy Tarreau284ddbf2013-12-01 20:45:00 +01005906 struct appctx *tmpctx;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005907
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005908 chunk_reset(&trash);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005909
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005910 if (appctx->ctx.sess.section > 0 && appctx->ctx.sess.uid != sess->uniq_id) {
Willy Tarreau87b09662015-04-03 00:22:06 +02005911 /* stream changed, no need to go any further */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005912 chunk_appendf(&trash, " *** session terminated while we were watching it ***\n");
Willy Tarreaubc18da12015-03-13 14:00:47 +01005913 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02005914 si_applet_cant_put(si);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005915 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01005916 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005917 appctx->ctx.sess.uid = 0;
5918 appctx->ctx.sess.section = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005919 return 1;
5920 }
5921
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005922 switch (appctx->ctx.sess.section) {
Willy Tarreau87b09662015-04-03 00:22:06 +02005923 case 0: /* main status of the stream */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005924 appctx->ctx.sess.uid = sess->uniq_id;
5925 appctx->ctx.sess.section = 1;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005926 /* fall through */
5927
5928 case 1:
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005929 get_localtime(sess->logs.accept_date.tv_sec, &tm);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005930 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005931 "%p: [%02d/%s/%04d:%02d:%02d:%02d.%06d] id=%u proto=%s",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005932 sess,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005933 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
5934 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(sess->logs.accept_date.tv_usec),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005935 sess->uniq_id,
Willy Tarreau666f5042015-06-17 19:49:52 +02005936 strm_li(sess) ? strm_li(sess)->proto->name : "?");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005937
Willy Tarreaud0d8da92015-04-04 02:10:38 +02005938 conn = objt_conn(strm_orig(sess));
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005939 switch (conn ? addr_to_str(&conn->addr.from, pn, sizeof(pn)) : AF_UNSPEC) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005940 case AF_INET:
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005941 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005942 chunk_appendf(&trash, " source=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005943 pn, get_host_port(&conn->addr.from));
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005944 break;
5945 case AF_UNIX:
Willy Tarreaud0d8da92015-04-04 02:10:38 +02005946 chunk_appendf(&trash, " source=unix:%d\n", strm_li(sess)->luid);
Emeric Brun837ca522010-10-22 16:19:01 +02005947 break;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005948 default:
5949 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005950 chunk_appendf(&trash, "\n");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005951 break;
5952 }
5953
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005954 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005955 " flags=0x%x, conn_retries=%d, srv_conn=%p, pend_pos=%p\n",
Willy Tarreauee28de02010-06-01 09:51:00 +02005956 sess->flags, sess->si[1].conn_retries, sess->srv_conn, sess->pend_pos);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005957
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005958 chunk_appendf(&trash,
Daniel Schultze90690c72012-03-23 10:53:36 -07005959 " frontend=%s (id=%u mode=%s), listener=%s (id=%u)",
Willy Tarreaud0d8da92015-04-04 02:10:38 +02005960 strm_fe(sess)->id, strm_fe(sess)->uuid, strm_fe(sess)->mode ? "http" : "tcp",
5961 strm_li(sess) ? strm_li(sess)->name ? strm_li(sess)->name : "?" : "?",
5962 strm_li(sess) ? strm_li(sess)->luid : 0);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005963
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005964 if (conn)
5965 conn_get_to_addr(conn);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005966
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005967 switch (conn ? addr_to_str(&conn->addr.to, pn, sizeof(pn)) : AF_UNSPEC) {
Daniel Schultze90690c72012-03-23 10:53:36 -07005968 case AF_INET:
5969 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005970 chunk_appendf(&trash, " addr=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005971 pn, get_host_port(&conn->addr.to));
Daniel Schultze90690c72012-03-23 10:53:36 -07005972 break;
5973 case AF_UNIX:
Willy Tarreaud0d8da92015-04-04 02:10:38 +02005974 chunk_appendf(&trash, " addr=unix:%d\n", strm_li(sess)->luid);
Daniel Schultze90690c72012-03-23 10:53:36 -07005975 break;
5976 default:
5977 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005978 chunk_appendf(&trash, "\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07005979 break;
5980 }
5981
Willy Tarreau50943332011-09-02 17:33:05 +02005982 if (sess->be->cap & PR_CAP_BE)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005983 chunk_appendf(&trash,
Daniel Schultze90690c72012-03-23 10:53:36 -07005984 " backend=%s (id=%u mode=%s)",
Willy Tarreau50943332011-09-02 17:33:05 +02005985 sess->be->id,
Daniel Schultze90690c72012-03-23 10:53:36 -07005986 sess->be->uuid, sess->be->mode ? "http" : "tcp");
5987 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005988 chunk_appendf(&trash, " backend=<NONE> (id=-1 mode=-)");
Daniel Schultze90690c72012-03-23 10:53:36 -07005989
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005990 conn = objt_conn(sess->si[1].end);
5991 if (conn)
5992 conn_get_from_addr(conn);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005993
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005994 switch (conn ? addr_to_str(&conn->addr.from, pn, sizeof(pn)) : AF_UNSPEC) {
Daniel Schultze90690c72012-03-23 10:53:36 -07005995 case AF_INET:
5996 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005997 chunk_appendf(&trash, " addr=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005998 pn, get_host_port(&conn->addr.from));
Daniel Schultze90690c72012-03-23 10:53:36 -07005999 break;
6000 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006001 chunk_appendf(&trash, " addr=unix\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07006002 break;
6003 default:
6004 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006005 chunk_appendf(&trash, "\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07006006 break;
6007 }
6008
6009 if (sess->be->cap & PR_CAP_BE)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006010 chunk_appendf(&trash,
Daniel Schultze90690c72012-03-23 10:53:36 -07006011 " server=%s (id=%u)",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006012 objt_server(sess->target) ? objt_server(sess->target)->id : "<none>",
6013 objt_server(sess->target) ? objt_server(sess->target)->puid : 0);
Willy Tarreau50943332011-09-02 17:33:05 +02006014 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006015 chunk_appendf(&trash, " server=<NONE> (id=-1)");
Daniel Schultze90690c72012-03-23 10:53:36 -07006016
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006017 if (conn)
6018 conn_get_to_addr(conn);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02006019
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006020 switch (conn ? addr_to_str(&conn->addr.to, pn, sizeof(pn)) : AF_UNSPEC) {
Daniel Schultze90690c72012-03-23 10:53:36 -07006021 case AF_INET:
6022 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006023 chunk_appendf(&trash, " addr=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006024 pn, get_host_port(&conn->addr.to));
Daniel Schultze90690c72012-03-23 10:53:36 -07006025 break;
6026 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006027 chunk_appendf(&trash, " addr=unix\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07006028 break;
6029 default:
6030 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006031 chunk_appendf(&trash, "\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07006032 break;
6033 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006034
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006035 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01006036 " task=%p (state=0x%02x nice=%d calls=%d exp=%s%s",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006037 sess->task,
6038 sess->task->state,
6039 sess->task->nice, sess->task->calls,
6040 sess->task->expire ?
6041 tick_is_expired(sess->task->expire, now_ms) ? "<PAST>" :
6042 human_time(TICKS_TO_MS(sess->task->expire - now_ms),
6043 TICKS_TO_MS(1000)) : "<NEVER>",
6044 task_in_rq(sess->task) ? ", running" : "");
6045
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006046 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01006047 " age=%s)\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006048 human_time(now.tv_sec - sess->logs.accept_date.tv_sec, 1));
6049
Willy Tarreaueee5b512015-04-03 23:46:31 +02006050 if (sess->txn)
6051 chunk_appendf(&trash,
Willy Tarreau98410192014-11-26 18:05:38 +01006052 " 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 +02006053 sess->txn, sess->txn->flags, sess->txn->meth, sess->txn->status,
6054 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 +01006055
6056 chunk_appendf(&trash,
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02006057 " si[0]=%p (state=%s flags=0x%02x endp0=%s:%p exp=%s, et=0x%03x)\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006058 &sess->si[0],
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01006059 si_state_str(sess->si[0].state),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006060 sess->si[0].flags,
Willy Tarreaucf644ed2013-09-29 17:19:56 +02006061 obj_type_name(sess->si[0].end),
6062 obj_base_ptr(sess->si[0].end),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006063 sess->si[0].exp ?
6064 tick_is_expired(sess->si[0].exp, now_ms) ? "<PAST>" :
6065 human_time(TICKS_TO_MS(sess->si[0].exp - now_ms),
6066 TICKS_TO_MS(1000)) : "<NEVER>",
6067 sess->si[0].err_type);
6068
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006069 chunk_appendf(&trash,
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02006070 " si[1]=%p (state=%s flags=0x%02x endp1=%s:%p exp=%s, et=0x%03x)\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006071 &sess->si[1],
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01006072 si_state_str(sess->si[1].state),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006073 sess->si[1].flags,
Willy Tarreaucf644ed2013-09-29 17:19:56 +02006074 obj_type_name(sess->si[1].end),
6075 obj_base_ptr(sess->si[1].end),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006076 sess->si[1].exp ?
6077 tick_is_expired(sess->si[1].exp, now_ms) ? "<PAST>" :
6078 human_time(TICKS_TO_MS(sess->si[1].exp - now_ms),
6079 TICKS_TO_MS(1000)) : "<NEVER>",
6080 sess->si[1].err_type);
6081
Willy Tarreau284ddbf2013-12-01 20:45:00 +01006082 if ((conn = objt_conn(sess->si[0].end)) != NULL) {
Willy Tarreaucf644ed2013-09-29 17:19:56 +02006083 chunk_appendf(&trash,
6084 " co0=%p ctrl=%s xprt=%s data=%s target=%s:%p\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006085 conn,
6086 get_conn_ctrl_name(conn),
6087 get_conn_xprt_name(conn),
6088 get_conn_data_name(conn),
6089 obj_type_name(conn->target),
6090 obj_base_ptr(conn->target));
Willy Tarreau909d5172012-11-26 03:04:41 +01006091
Willy Tarreau733b1322016-11-17 14:22:52 +01006092 chunk_appendf(&trash, " flags=0x%08x", conn->flags);
6093
6094 if (conn->t.sock.fd >= 0) {
6095 chunk_appendf(&trash, " fd=%d fd.state=%02x fd.cache=%d updt=%d\n",
6096 conn->t.sock.fd, fdtab[conn->t.sock.fd].state,
6097 fdtab[conn->t.sock.fd].cache, fdtab[conn->t.sock.fd].updated);
6098 }
6099 else
6100 chunk_appendf(&trash, " fd=<dead>\n");
Willy Tarreaucf644ed2013-09-29 17:19:56 +02006101 }
Willy Tarreau284ddbf2013-12-01 20:45:00 +01006102 else if ((tmpctx = objt_appctx(sess->si[0].end)) != NULL) {
6103 chunk_appendf(&trash,
6104 " app0=%p st0=%d st1=%d st2=%d applet=%s\n",
6105 tmpctx,
6106 tmpctx->st0,
6107 tmpctx->st1,
6108 tmpctx->st2,
6109 tmpctx->applet->name);
6110 }
Willy Tarreaubc174aa2012-11-19 16:10:32 +01006111
Willy Tarreau284ddbf2013-12-01 20:45:00 +01006112 if ((conn = objt_conn(sess->si[1].end)) != NULL) {
Willy Tarreaucf644ed2013-09-29 17:19:56 +02006113 chunk_appendf(&trash,
6114 " co1=%p ctrl=%s xprt=%s data=%s target=%s:%p\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006115 conn,
6116 get_conn_ctrl_name(conn),
6117 get_conn_xprt_name(conn),
6118 get_conn_data_name(conn),
6119 obj_type_name(conn->target),
6120 obj_base_ptr(conn->target));
Willy Tarreau909d5172012-11-26 03:04:41 +01006121
Willy Tarreau733b1322016-11-17 14:22:52 +01006122 chunk_appendf(&trash, " flags=0x%08x", conn->flags);
6123
6124 if (conn->t.sock.fd >= 0) {
6125 chunk_appendf(&trash, " fd=%d fd.state=%02x fd.cache=%d updt=%d\n",
6126 conn->t.sock.fd, fdtab[conn->t.sock.fd].state,
6127 fdtab[conn->t.sock.fd].cache, fdtab[conn->t.sock.fd].updated);
6128 }
6129 else
6130 chunk_appendf(&trash, " fd=<dead>\n");
Willy Tarreaucf644ed2013-09-29 17:19:56 +02006131 }
Willy Tarreau284ddbf2013-12-01 20:45:00 +01006132 else if ((tmpctx = objt_appctx(sess->si[1].end)) != NULL) {
6133 chunk_appendf(&trash,
6134 " app1=%p st0=%d st1=%d st2=%d applet=%s\n",
6135 tmpctx,
6136 tmpctx->st0,
6137 tmpctx->st1,
6138 tmpctx->st2,
6139 tmpctx->applet->name);
6140 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006141
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006142 chunk_appendf(&trash,
Willy Tarreau909d5172012-11-26 03:04:41 +01006143 " req=%p (f=0x%06x an=0x%x pipe=%d tofwd=%d total=%lld)\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006144 " an_exp=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006145 &sess->req,
6146 sess->req.flags, sess->req.analysers,
6147 sess->req.pipe ? sess->req.pipe->data : 0,
6148 sess->req.to_forward, sess->req.total,
6149 sess->req.analyse_exp ?
6150 human_time(TICKS_TO_MS(sess->req.analyse_exp - now_ms),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006151 TICKS_TO_MS(1000)) : "<NEVER>");
6152
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006153 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006154 " rex=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006155 sess->req.rex ?
6156 human_time(TICKS_TO_MS(sess->req.rex - now_ms),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006157 TICKS_TO_MS(1000)) : "<NEVER>");
6158
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006159 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006160 " wex=%s\n"
Willy Tarreau909d5172012-11-26 03:04:41 +01006161 " buf=%p data=%p o=%d p=%d req.next=%d i=%d size=%d\n",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006162 sess->req.wex ?
6163 human_time(TICKS_TO_MS(sess->req.wex - now_ms),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006164 TICKS_TO_MS(1000)) : "<NEVER>",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006165 sess->req.buf,
6166 sess->req.buf->data, sess->req.buf->o,
6167 (int)(sess->req.buf->p - sess->req.buf->data),
Willy Tarreaueee5b512015-04-03 23:46:31 +02006168 sess->txn ? sess->txn->req.next : 0, sess->req.buf->i,
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006169 sess->req.buf->size);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006170
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006171 chunk_appendf(&trash,
Willy Tarreau909d5172012-11-26 03:04:41 +01006172 " res=%p (f=0x%06x an=0x%x pipe=%d tofwd=%d total=%lld)\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006173 " an_exp=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006174 &sess->res,
6175 sess->res.flags, sess->res.analysers,
6176 sess->res.pipe ? sess->res.pipe->data : 0,
6177 sess->res.to_forward, sess->res.total,
6178 sess->res.analyse_exp ?
6179 human_time(TICKS_TO_MS(sess->res.analyse_exp - now_ms),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006180 TICKS_TO_MS(1000)) : "<NEVER>");
6181
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006182 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006183 " rex=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006184 sess->res.rex ?
6185 human_time(TICKS_TO_MS(sess->res.rex - now_ms),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006186 TICKS_TO_MS(1000)) : "<NEVER>");
6187
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006188 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006189 " wex=%s\n"
Willy Tarreau909d5172012-11-26 03:04:41 +01006190 " buf=%p data=%p o=%d p=%d rsp.next=%d i=%d size=%d\n",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006191 sess->res.wex ?
6192 human_time(TICKS_TO_MS(sess->res.wex - now_ms),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006193 TICKS_TO_MS(1000)) : "<NEVER>",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006194 sess->res.buf,
6195 sess->res.buf->data, sess->res.buf->o,
6196 (int)(sess->res.buf->p - sess->res.buf->data),
Willy Tarreaueee5b512015-04-03 23:46:31 +02006197 sess->txn ? sess->txn->rsp.next : 0, sess->res.buf->i,
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006198 sess->res.buf->size);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006199
Willy Tarreaubc18da12015-03-13 14:00:47 +01006200 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02006201 si_applet_cant_put(si);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006202 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01006203 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006204
6205 /* use other states to dump the contents */
6206 }
6207 /* end of dump */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006208 appctx->ctx.sess.uid = 0;
6209 appctx->ctx.sess.section = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006210 return 1;
6211}
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006212
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02006213#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
6214static int stats_tlskeys_list(struct stream_interface *si) {
6215 struct appctx *appctx = __objt_appctx(si->end);
6216
6217 switch (appctx->st2) {
6218 case STAT_ST_INIT:
6219 /* Display the column headers. If the message cannot be sent,
6220 * quit the fucntion with returning 0. The function is called
6221 * later and restart at the state "STAT_ST_INIT".
6222 */
6223 chunk_reset(&trash);
William Lallemand1d0b36a2016-05-20 17:40:26 +02006224
6225 if (appctx->st0 == STAT_CLI_O_TLSK_ENT)
6226 chunk_appendf(&trash, "# id secret\n");
6227 else
6228 chunk_appendf(&trash, "# id (file)\n");
6229
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02006230 if (bi_putchk(si_ic(si), &trash) == -1) {
6231 si_applet_cant_put(si);
6232 return 0;
6233 }
6234
William Lallemand72a8a182016-06-14 18:58:55 +02006235 appctx->ctx.tlskeys.dump_keys_index = 0;
William Lallemand1d0b36a2016-05-20 17:40:26 +02006236
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02006237 /* Now, we start the browsing of the references lists.
6238 * Note that the following call to LIST_ELEM return bad pointer. The only
Cyril Bontédb98eb32016-05-06 12:18:50 +02006239 * available field of this pointer is <list>. It is used with the function
6240 * tlskeys_list_get_next() for retruning the first available entry
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02006241 */
William Lallemandcf9e7882016-06-14 17:45:18 +02006242 if (appctx->ctx.tlskeys.ref == NULL) {
6243 appctx->ctx.tlskeys.ref = LIST_ELEM(&tlskeys_reference, struct tls_keys_ref *, list);
6244 appctx->ctx.tlskeys.ref = tlskeys_list_get_next(appctx->ctx.tlskeys.ref, &tlskeys_reference);
William Lallemand1d0b36a2016-05-20 17:40:26 +02006245 }
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02006246
6247 appctx->st2 = STAT_ST_LIST;
6248 /* fall through */
6249
6250 case STAT_ST_LIST:
William Lallemandcf9e7882016-06-14 17:45:18 +02006251 while (appctx->ctx.tlskeys.ref) {
William Lallemandcf9e7882016-06-14 17:45:18 +02006252 int head = appctx->ctx.tlskeys.ref->tls_ticket_enc_index;
William Lallemand1d0b36a2016-05-20 17:40:26 +02006253
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02006254 chunk_reset(&trash);
William Lallemand72a8a182016-06-14 18:58:55 +02006255 if (appctx->st0 == STAT_CLI_O_TLSK_ENT && appctx->ctx.tlskeys.dump_keys_index == 0)
William Lallemand1d0b36a2016-05-20 17:40:26 +02006256 chunk_appendf(&trash, "# ");
William Lallemand72a8a182016-06-14 18:58:55 +02006257 if (appctx->ctx.tlskeys.dump_keys_index == 0)
6258 chunk_appendf(&trash, "%d (%s)\n", appctx->ctx.tlskeys.ref->unique_id,
6259 appctx->ctx.tlskeys.ref->filename);
William Lallemand1d0b36a2016-05-20 17:40:26 +02006260 if (appctx->st0 == STAT_CLI_O_TLSK_ENT) {
William Lallemand72a8a182016-06-14 18:58:55 +02006261 while (appctx->ctx.tlskeys.dump_keys_index < TLS_TICKETS_NO) {
William Lallemand1d0b36a2016-05-20 17:40:26 +02006262 struct chunk *t2 = get_trash_chunk();
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02006263
William Lallemand1d0b36a2016-05-20 17:40:26 +02006264 chunk_reset(t2);
William Lallemand72a8a182016-06-14 18:58:55 +02006265 /* should never fail here because we dump only a key in the t2 buffer */
6266 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 +02006267 sizeof(struct tls_sess_key), t2->str, t2->size);
William Lallemand72a8a182016-06-14 18:58:55 +02006268 chunk_appendf(&trash, "%d.%d %s\n", appctx->ctx.tlskeys.ref->unique_id, appctx->ctx.tlskeys.dump_keys_index, t2->str);
6269
6270 if (bi_putchk(si_ic(si), &trash) == -1) {
6271 /* let's try again later from this stream. We add ourselves into
6272 * this stream's users so that it can remove us upon termination.
6273 */
6274 si_applet_cant_put(si);
William Lallemand1d0b36a2016-05-20 17:40:26 +02006275 return 0;
William Lallemand72a8a182016-06-14 18:58:55 +02006276 }
6277 appctx->ctx.tlskeys.dump_keys_index++;
William Lallemand1d0b36a2016-05-20 17:40:26 +02006278 }
William Lallemand72a8a182016-06-14 18:58:55 +02006279 appctx->ctx.tlskeys.dump_keys_index = 0;
William Lallemand1d0b36a2016-05-20 17:40:26 +02006280 }
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02006281 if (bi_putchk(si_ic(si), &trash) == -1) {
6282 /* let's try again later from this stream. We add ourselves into
6283 * this stream's users so that it can remove us upon termination.
6284 */
6285 si_applet_cant_put(si);
6286 return 0;
6287 }
6288
William Lallemandcf9e7882016-06-14 17:45:18 +02006289 if (appctx->ctx.tlskeys.dump_all == 0) /* don't display everything if not necessary */
William Lallemand1d0b36a2016-05-20 17:40:26 +02006290 break;
6291
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02006292 /* get next list entry and check the end of the list */
William Lallemandcf9e7882016-06-14 17:45:18 +02006293 appctx->ctx.tlskeys.ref = tlskeys_list_get_next(appctx->ctx.tlskeys.ref, &tlskeys_reference);
William Lallemand1d0b36a2016-05-20 17:40:26 +02006294
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02006295 }
6296
6297 appctx->st2 = STAT_ST_FIN;
6298 /* fall through */
6299
6300 default:
6301 appctx->st2 = STAT_ST_FIN;
6302 return 1;
6303 }
6304 return 0;
6305}
6306#endif
6307
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006308static int stats_pats_list(struct stream_interface *si)
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006309{
6310 struct appctx *appctx = __objt_appctx(si->end);
6311
6312 switch (appctx->st2) {
6313 case STAT_ST_INIT:
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01006314 /* Display the column headers. If the message cannot be sent,
6315 * quit the fucntion with returning 0. The function is called
6316 * later and restart at the state "STAT_ST_INIT".
6317 */
6318 chunk_reset(&trash);
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01006319 chunk_appendf(&trash, "# id (file) description\n");
Willy Tarreaubc18da12015-03-13 14:00:47 +01006320 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02006321 si_applet_cant_put(si);
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01006322 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01006323 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006324
6325 /* Now, we start the browsing of the references lists.
6326 * Note that the following call to LIST_ELEM return bad pointer. The only
Cyril Bontédb98eb32016-05-06 12:18:50 +02006327 * available field of this pointer is <list>. It is used with the function
6328 * pat_list_get_next() for retruning the first available entry
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006329 */
6330 appctx->ctx.map.ref = LIST_ELEM(&pattern_reference, struct pat_ref *, list);
6331 appctx->ctx.map.ref = pat_list_get_next(appctx->ctx.map.ref, &pattern_reference,
6332 appctx->ctx.map.display_flags);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006333 appctx->st2 = STAT_ST_LIST;
6334 /* fall through */
6335
6336 case STAT_ST_LIST:
6337 while (appctx->ctx.map.ref) {
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006338 chunk_reset(&trash);
6339
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006340 /* Build messages. If the reference is used by another category than
6341 * the listed categorie, display the information in the massage.
6342 */
Thierry FOURNIERf7e04e92014-03-20 11:45:47 +01006343 chunk_appendf(&trash, "%d (%s) %s\n", appctx->ctx.map.ref->unique_id,
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01006344 appctx->ctx.map.ref->reference ? appctx->ctx.map.ref->reference : "",
6345 appctx->ctx.map.ref->display);
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01006346
Willy Tarreau2bb4a962014-11-28 11:11:05 +01006347 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau87b09662015-04-03 00:22:06 +02006348 /* let's try again later from this stream. We add ourselves into
6349 * this stream's users so that it can remove us upon termination.
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006350 */
Willy Tarreaufe127932015-04-21 19:23:39 +02006351 si_applet_cant_put(si);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006352 return 0;
6353 }
6354
6355 /* get next list entry and check the end of the list */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006356 appctx->ctx.map.ref = pat_list_get_next(appctx->ctx.map.ref, &pattern_reference,
6357 appctx->ctx.map.display_flags);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006358 }
6359
6360 appctx->st2 = STAT_ST_FIN;
6361 /* fall through */
6362
6363 default:
6364 appctx->st2 = STAT_ST_FIN;
6365 return 1;
6366 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006367 return 0;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006368}
6369
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006370static int stats_map_lookup(struct stream_interface *si)
6371{
6372 struct appctx *appctx = __objt_appctx(si->end);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006373 struct sample sample;
6374 struct pattern *pat;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006375 int match_method;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006376
6377 switch (appctx->st2) {
6378 case STAT_ST_INIT:
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006379 /* Init to the first entry. The list cannot be change */
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01006380 appctx->ctx.map.expr = LIST_ELEM(&appctx->ctx.map.ref->pat, struct pattern_expr *, list);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006381 appctx->ctx.map.expr = pat_expr_get_next(appctx->ctx.map.expr, &appctx->ctx.map.ref->pat);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006382 appctx->st2 = STAT_ST_LIST;
6383 /* fall through */
6384
6385 case STAT_ST_LIST:
6386 /* for each lookup type */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006387 while (appctx->ctx.map.expr) {
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006388 /* initialise chunk to build new message */
6389 chunk_reset(&trash);
6390
6391 /* execute pattern matching */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02006392 sample.data.type = SMP_T_STR;
Andreas Seltenreich9727cf42016-03-03 19:32:25 +01006393 sample.flags = SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02006394 sample.data.u.str.len = appctx->ctx.map.chunk.len;
6395 sample.data.u.str.str = appctx->ctx.map.chunk.str;
Thierry FOURNIER5d344082014-01-27 14:19:53 +01006396 if (appctx->ctx.map.expr->pat_head->match &&
6397 sample_convert(&sample, appctx->ctx.map.expr->pat_head->expect_type))
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006398 pat = appctx->ctx.map.expr->pat_head->match(&sample, appctx->ctx.map.expr, 1);
6399 else
6400 pat = NULL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006401
6402 /* build return message: set type of match */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006403 for (match_method=0; match_method<PAT_MATCH_NUM; match_method++)
6404 if (appctx->ctx.map.expr->pat_head->match == pat_match_fcts[match_method])
6405 break;
6406 if (match_method >= PAT_MATCH_NUM)
6407 chunk_appendf(&trash, "type=unknown(%p)", appctx->ctx.map.expr->pat_head->match);
6408 else
6409 chunk_appendf(&trash, "type=%s", pat_match_names[match_method]);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006410
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02006411 /* case sensitive */
6412 if (appctx->ctx.map.expr->mflags & PAT_MF_IGNORE_CASE)
6413 chunk_appendf(&trash, ", case=insensitive");
6414 else
6415 chunk_appendf(&trash, ", case=sensitive");
6416
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006417 /* Display no match, and set default value */
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01006418 if (!pat) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006419 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
6420 chunk_appendf(&trash, ", found=no");
6421 else
6422 chunk_appendf(&trash, ", match=no");
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006423 }
6424
6425 /* Display match and match info */
6426 else {
6427 /* display match */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006428 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
6429 chunk_appendf(&trash, ", found=yes");
6430 else
6431 chunk_appendf(&trash, ", match=yes");
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006432
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01006433 /* display index mode */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02006434 if (pat->sflags & PAT_SF_TREE)
Thierry FOURNIERb9903842014-03-11 18:48:17 +01006435 chunk_appendf(&trash, ", idx=tree");
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006436 else
Thierry FOURNIERb9903842014-03-11 18:48:17 +01006437 chunk_appendf(&trash, ", idx=list");
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006438
Thierry FOURNIER86db66a2014-03-11 18:37:58 +01006439 /* display pattern */
6440 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
6441 if (pat->ref && pat->ref->pattern)
6442 chunk_appendf(&trash, ", key=\"%s\"", pat->ref->pattern);
6443 else
6444 chunk_appendf(&trash, ", key=unknown");
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006445 }
6446 else {
Thierry FOURNIER86db66a2014-03-11 18:37:58 +01006447 if (pat->ref && pat->ref->pattern)
6448 chunk_appendf(&trash, ", pattern=\"%s\"", pat->ref->pattern);
6449 else
6450 chunk_appendf(&trash, ", pattern=unknown");
6451 }
6452
6453 /* display return value */
6454 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
Thierry FOURNIER503bb092015-08-19 08:35:43 +02006455 if (pat->data && pat->ref && pat->ref->sample)
Thierry FOURNIER03d0e452015-07-23 18:33:41 +02006456 chunk_appendf(&trash, ", value=\"%s\", type=\"%s\"", pat->ref->sample,
Thierry FOURNIER7d4335c2015-08-19 09:05:25 +02006457 smp_to_type[pat->data->type]);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006458 else
Thierry FOURNIER86db66a2014-03-11 18:37:58 +01006459 chunk_appendf(&trash, ", value=none");
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006460 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006461 }
6462
Thierry FOURNIERb9903842014-03-11 18:48:17 +01006463 chunk_appendf(&trash, "\n");
6464
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006465 /* display response */
Willy Tarreau2bb4a962014-11-28 11:11:05 +01006466 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau87b09662015-04-03 00:22:06 +02006467 /* let's try again later from this stream. We add ourselves into
6468 * this stream's users so that it can remove us upon termination.
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006469 */
Willy Tarreaufe127932015-04-21 19:23:39 +02006470 si_applet_cant_put(si);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006471 return 0;
6472 }
6473
6474 /* get next entry */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006475 appctx->ctx.map.expr = pat_expr_get_next(appctx->ctx.map.expr,
6476 &appctx->ctx.map.ref->pat);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006477 }
6478
6479 appctx->st2 = STAT_ST_FIN;
6480 /* fall through */
6481
6482 default:
6483 appctx->st2 = STAT_ST_FIN;
6484 free(appctx->ctx.map.chunk.str);
6485 return 1;
6486 }
6487}
6488
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006489static int stats_pat_list(struct stream_interface *si)
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006490{
6491 struct appctx *appctx = __objt_appctx(si->end);
6492
6493 switch (appctx->st2) {
6494
6495 case STAT_ST_INIT:
6496 /* Init to the first entry. The list cannot be change */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006497 appctx->ctx.map.elt = LIST_NEXT(&appctx->ctx.map.ref->head,
6498 struct pat_ref_elt *, list);
6499 if (&appctx->ctx.map.elt->list == &appctx->ctx.map.ref->head)
6500 appctx->ctx.map.elt = NULL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006501 appctx->st2 = STAT_ST_LIST;
6502 /* fall through */
6503
6504 case STAT_ST_LIST:
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006505 while (appctx->ctx.map.elt) {
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006506 chunk_reset(&trash);
6507
6508 /* build messages */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006509 if (appctx->ctx.map.elt->sample)
Thierry FOURNIER9356c682014-01-28 15:55:37 +01006510 chunk_appendf(&trash, "%p %s %s\n",
6511 appctx->ctx.map.elt, appctx->ctx.map.elt->pattern,
6512 appctx->ctx.map.elt->sample);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006513 else
Thierry FOURNIER9356c682014-01-28 15:55:37 +01006514 chunk_appendf(&trash, "%p %s\n",
6515 appctx->ctx.map.elt, appctx->ctx.map.elt->pattern);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006516
Willy Tarreau2bb4a962014-11-28 11:11:05 +01006517 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau87b09662015-04-03 00:22:06 +02006518 /* let's try again later from this stream. We add ourselves into
6519 * this stream's users so that it can remove us upon termination.
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006520 */
Willy Tarreaufe127932015-04-21 19:23:39 +02006521 si_applet_cant_put(si);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006522 return 0;
6523 }
6524
6525 /* get next list entry and check the end of the list */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006526 appctx->ctx.map.elt = LIST_NEXT(&appctx->ctx.map.elt->list,
6527 struct pat_ref_elt *, list);
6528 if (&appctx->ctx.map.elt->list == &appctx->ctx.map.ref->head)
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006529 break;
6530 }
6531
6532 appctx->st2 = STAT_ST_FIN;
6533 /* fall through */
6534
6535 default:
6536 appctx->st2 = STAT_ST_FIN;
6537 return 1;
6538 }
6539}
6540
Willy Tarreau87b09662015-04-03 00:22:06 +02006541/* This function dumps all streams' states onto the stream interface's
Willy Tarreau306f8302013-07-08 15:53:06 +02006542 * read buffer. It returns 0 if the output buffer is full and it needs
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01006543 * to be called again, otherwise non-zero. It is designed to be called
6544 * from stats_dump_sess_to_buffer() below.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006545 */
Simon Horman9bd2c732011-06-15 15:18:44 +09006546static int stats_dump_sess_to_buffer(struct stream_interface *si)
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006547{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006548 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006549 struct connection *conn;
6550
Willy Tarreau2bb4a962014-11-28 11:11:05 +01006551 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW))) {
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006552 /* If we're forced to shut down, we might have to remove our
Willy Tarreau87b09662015-04-03 00:22:06 +02006553 * reference to the last stream being dumped.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006554 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006555 if (appctx->st2 == STAT_ST_LIST) {
6556 if (!LIST_ISEMPTY(&appctx->ctx.sess.bref.users)) {
6557 LIST_DEL(&appctx->ctx.sess.bref.users);
6558 LIST_INIT(&appctx->ctx.sess.bref.users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +01006559 }
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006560 }
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02006561 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006562 }
6563
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006564 chunk_reset(&trash);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006565
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006566 switch (appctx->st2) {
Willy Tarreau295a8372011-03-10 11:25:07 +01006567 case STAT_ST_INIT:
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006568 /* the function had not been called yet, let's prepare the
Willy Tarreau87b09662015-04-03 00:22:06 +02006569 * buffer for a response. We initialize the current stream
Willy Tarreaufd3828e2009-02-22 15:17:24 +01006570 * pointer to the first in the global list. When a target
Willy Tarreau87b09662015-04-03 00:22:06 +02006571 * stream is being destroyed, it is responsible for updating
Willy Tarreaufd3828e2009-02-22 15:17:24 +01006572 * this pointer. We know we have reached the end when this
Willy Tarreau87b09662015-04-03 00:22:06 +02006573 * pointer points back to the head of the streams list.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006574 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006575 LIST_INIT(&appctx->ctx.sess.bref.users);
Willy Tarreau87b09662015-04-03 00:22:06 +02006576 appctx->ctx.sess.bref.ref = streams.n;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006577 appctx->st2 = STAT_ST_LIST;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006578 /* fall through */
6579
Willy Tarreau295a8372011-03-10 11:25:07 +01006580 case STAT_ST_LIST:
Willy Tarreau87b09662015-04-03 00:22:06 +02006581 /* first, let's detach the back-ref from a possible previous stream */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006582 if (!LIST_ISEMPTY(&appctx->ctx.sess.bref.users)) {
6583 LIST_DEL(&appctx->ctx.sess.bref.users);
6584 LIST_INIT(&appctx->ctx.sess.bref.users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +01006585 }
6586
6587 /* and start from where we stopped */
Willy Tarreau87b09662015-04-03 00:22:06 +02006588 while (appctx->ctx.sess.bref.ref != &streams) {
Cyril Bontéacd7d632010-11-01 19:26:02 +01006589 char pn[INET6_ADDRSTRLEN];
Willy Tarreau87b09662015-04-03 00:22:06 +02006590 struct stream *curr_sess;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006591
Willy Tarreau87b09662015-04-03 00:22:06 +02006592 curr_sess = LIST_ELEM(appctx->ctx.sess.bref.ref, struct stream *, list);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006593
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006594 if (appctx->ctx.sess.target) {
6595 if (appctx->ctx.sess.target != (void *)-1 && appctx->ctx.sess.target != curr_sess)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006596 goto next_sess;
6597
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006598 LIST_ADDQ(&curr_sess->back_refs, &appctx->ctx.sess.bref.users);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006599 /* call the proper dump() function and return if we're missing space */
Willy Tarreau76153662012-11-26 01:16:39 +01006600 if (!stats_dump_full_sess_to_buffer(si, curr_sess))
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006601 return 0;
6602
Willy Tarreau87b09662015-04-03 00:22:06 +02006603 /* stream dump complete */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006604 LIST_DEL(&appctx->ctx.sess.bref.users);
6605 LIST_INIT(&appctx->ctx.sess.bref.users);
6606 if (appctx->ctx.sess.target != (void *)-1) {
6607 appctx->ctx.sess.target = NULL;
Willy Tarreau76153662012-11-26 01:16:39 +01006608 break;
6609 }
6610 else
6611 goto next_sess;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006612 }
6613
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006614 chunk_appendf(&trash,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006615 "%p: proto=%s",
6616 curr_sess,
Willy Tarreauf1e02122015-09-23 12:16:43 +02006617 strm_li(curr_sess) ? strm_li(curr_sess)->proto->name : "?");
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006618
Willy Tarreaud0d8da92015-04-04 02:10:38 +02006619 conn = objt_conn(strm_orig(curr_sess));
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006620 switch (conn ? addr_to_str(&conn->addr.from, pn, sizeof(pn)) : AF_UNSPEC) {
Willy Tarreau631f01c2011-09-05 00:36:48 +02006621 case AF_INET:
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006622 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006623 chunk_appendf(&trash,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006624 " src=%s:%d fe=%s be=%s srv=%s",
6625 pn,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006626 get_host_port(&conn->addr.from),
Willy Tarreaud0d8da92015-04-04 02:10:38 +02006627 strm_fe(curr_sess)->id,
Willy Tarreau50943332011-09-02 17:33:05 +02006628 (curr_sess->be->cap & PR_CAP_BE) ? curr_sess->be->id : "<NONE>",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006629 objt_server(curr_sess->target) ? objt_server(curr_sess->target)->id : "<none>"
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006630 );
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006631 break;
6632 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006633 chunk_appendf(&trash,
Emeric Brun837ca522010-10-22 16:19:01 +02006634 " src=unix:%d fe=%s be=%s srv=%s",
Willy Tarreaud0d8da92015-04-04 02:10:38 +02006635 strm_li(curr_sess)->luid,
6636 strm_fe(curr_sess)->id,
Willy Tarreau50943332011-09-02 17:33:05 +02006637 (curr_sess->be->cap & PR_CAP_BE) ? curr_sess->be->id : "<NONE>",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006638 objt_server(curr_sess->target) ? objt_server(curr_sess->target)->id : "<none>"
Emeric Brun837ca522010-10-22 16:19:01 +02006639 );
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006640 break;
6641 }
6642
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006643 chunk_appendf(&trash,
Willy Tarreau65671ab2009-10-04 14:24:59 +02006644 " ts=%02x age=%s calls=%d",
6645 curr_sess->task->state,
Willy Tarreau3884cba2009-03-28 17:54:35 +01006646 human_time(now.tv_sec - curr_sess->logs.tv_accept.tv_sec, 1),
6647 curr_sess->task->calls);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006648
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006649 chunk_appendf(&trash,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01006650 " rq[f=%06xh,i=%d,an=%02xh,rx=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006651 curr_sess->req.flags,
6652 curr_sess->req.buf->i,
6653 curr_sess->req.analysers,
6654 curr_sess->req.rex ?
6655 human_time(TICKS_TO_MS(curr_sess->req.rex - now_ms),
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006656 TICKS_TO_MS(1000)) : "");
6657
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006658 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006659 ",wx=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006660 curr_sess->req.wex ?
6661 human_time(TICKS_TO_MS(curr_sess->req.wex - now_ms),
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006662 TICKS_TO_MS(1000)) : "");
6663
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006664 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006665 ",ax=%s]",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006666 curr_sess->req.analyse_exp ?
6667 human_time(TICKS_TO_MS(curr_sess->req.analyse_exp - now_ms),
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006668 TICKS_TO_MS(1000)) : "");
6669
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006670 chunk_appendf(&trash,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01006671 " rp[f=%06xh,i=%d,an=%02xh,rx=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006672 curr_sess->res.flags,
6673 curr_sess->res.buf->i,
6674 curr_sess->res.analysers,
6675 curr_sess->res.rex ?
6676 human_time(TICKS_TO_MS(curr_sess->res.rex - now_ms),
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006677 TICKS_TO_MS(1000)) : "");
6678
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006679 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006680 ",wx=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006681 curr_sess->res.wex ?
6682 human_time(TICKS_TO_MS(curr_sess->res.wex - now_ms),
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006683 TICKS_TO_MS(1000)) : "");
6684
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006685 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006686 ",ax=%s]",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006687 curr_sess->res.analyse_exp ?
6688 human_time(TICKS_TO_MS(curr_sess->res.analyse_exp - now_ms),
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006689 TICKS_TO_MS(1000)) : "");
6690
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006691 conn = objt_conn(curr_sess->si[0].end);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006692 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006693 " s0=[%d,%1xh,fd=%d,ex=%s]",
6694 curr_sess->si[0].state,
6695 curr_sess->si[0].flags,
Willy Tarreau733b1322016-11-17 14:22:52 +01006696 (conn && conn->t.sock.fd >= 0) ? conn->t.sock.fd : -1,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006697 curr_sess->si[0].exp ?
6698 human_time(TICKS_TO_MS(curr_sess->si[0].exp - now_ms),
6699 TICKS_TO_MS(1000)) : "");
6700
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006701 conn = objt_conn(curr_sess->si[1].end);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006702 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006703 " s1=[%d,%1xh,fd=%d,ex=%s]",
6704 curr_sess->si[1].state,
6705 curr_sess->si[1].flags,
Willy Tarreau733b1322016-11-17 14:22:52 +01006706 (conn && conn->t.sock.fd >= 0) ? conn->t.sock.fd : -1,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006707 curr_sess->si[1].exp ?
6708 human_time(TICKS_TO_MS(curr_sess->si[1].exp - now_ms),
6709 TICKS_TO_MS(1000)) : "");
6710
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006711 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006712 " exp=%s",
6713 curr_sess->task->expire ?
6714 human_time(TICKS_TO_MS(curr_sess->task->expire - now_ms),
6715 TICKS_TO_MS(1000)) : "");
Willy Tarreau4726f532009-03-07 17:25:21 +01006716 if (task_in_rq(curr_sess->task))
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006717 chunk_appendf(&trash, " run(nice=%d)", curr_sess->task->nice);
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006718
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006719 chunk_appendf(&trash, "\n");
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006720
Willy Tarreau2bb4a962014-11-28 11:11:05 +01006721 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau87b09662015-04-03 00:22:06 +02006722 /* let's try again later from this stream. We add ourselves into
6723 * this stream's users so that it can remove us upon termination.
Willy Tarreaufd3828e2009-02-22 15:17:24 +01006724 */
Willy Tarreaufe127932015-04-21 19:23:39 +02006725 si_applet_cant_put(si);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006726 LIST_ADDQ(&curr_sess->back_refs, &appctx->ctx.sess.bref.users);
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02006727 return 0;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006728 }
6729
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006730 next_sess:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006731 appctx->ctx.sess.bref.ref = curr_sess->list.n;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006732 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006733
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006734 if (appctx->ctx.sess.target && appctx->ctx.sess.target != (void *)-1) {
Willy Tarreau87b09662015-04-03 00:22:06 +02006735 /* specified stream not found */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006736 if (appctx->ctx.sess.section > 0)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006737 chunk_appendf(&trash, " *** session terminated while we were watching it ***\n");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006738 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006739 chunk_appendf(&trash, "Session not found.\n");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006740
Willy Tarreaubc18da12015-03-13 14:00:47 +01006741 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02006742 si_applet_cant_put(si);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006743 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01006744 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006745
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006746 appctx->ctx.sess.target = NULL;
6747 appctx->ctx.sess.uid = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006748 return 1;
6749 }
6750
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006751 appctx->st2 = STAT_ST_FIN;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006752 /* fall through */
6753
6754 default:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006755 appctx->st2 = STAT_ST_FIN;
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02006756 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006757 }
Emeric Brun1e029aa2010-09-23 18:12:53 +02006758}
6759
Willy Tarreau5f9a8772012-11-26 02:22:40 +01006760/* This is called when the stream interface is closed. For instance, upon an
6761 * external abort, we won't call the i/o handler anymore so we may need to
Willy Tarreau87b09662015-04-03 00:22:06 +02006762 * remove back references to the stream currently being dumped.
Willy Tarreau5f9a8772012-11-26 02:22:40 +01006763 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02006764static void cli_release_handler(struct appctx *appctx)
Willy Tarreau5f9a8772012-11-26 02:22:40 +01006765{
Thierry FOURNIER / OZON.IO6a22dcb2016-11-12 10:51:33 +01006766 if (appctx->io_release) {
6767 appctx->io_release(appctx);
6768 appctx->io_release = NULL;
6769 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006770 if (appctx->st0 == STAT_CLI_O_SESS && appctx->st2 == STAT_ST_LIST) {
6771 if (!LIST_ISEMPTY(&appctx->ctx.sess.bref.users))
6772 LIST_DEL(&appctx->ctx.sess.bref.users);
Willy Tarreau5f9a8772012-11-26 02:22:40 +01006773 }
Willy Tarreau2fe1b922016-11-18 19:17:40 +01006774 else if ((appctx->st0 == STAT_CLI_O_TAB || appctx->st0 == STAT_CLI_O_CLR) &&
6775 appctx->st2 == STAT_ST_LIST) {
6776 appctx->ctx.table.entry->ref_cnt--;
6777 stksess_kill_if_expired(&appctx->ctx.table.proxy->table, appctx->ctx.table.entry);
6778 }
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01006779 else if (appctx->st0 == STAT_CLI_PRINT_FREE) {
6780 free(appctx->ctx.cli.err);
Willy Tarreau6457d0f2015-10-07 20:00:24 +02006781 appctx->ctx.cli.err = NULL;
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01006782 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006783 else if (appctx->st0 == STAT_CLI_O_MLOOK) {
6784 free(appctx->ctx.map.chunk.str);
Willy Tarreau6457d0f2015-10-07 20:00:24 +02006785 appctx->ctx.map.chunk.str = NULL;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006786 }
Willy Tarreau5f9a8772012-11-26 02:22:40 +01006787}
6788
Willy Tarreau20e99322013-04-13 09:22:25 +02006789/* This function is used to either dump tables states (when action is set
6790 * to STAT_CLI_O_TAB) or clear tables (when action is STAT_CLI_O_CLR).
Willy Tarreau20e99322013-04-13 09:22:25 +02006791 * It returns 0 if the output buffer is full and it needs to be called
6792 * again, otherwise non-zero.
Willy Tarreau69f58c82010-07-12 17:55:33 +02006793 */
Willy Tarreau20e99322013-04-13 09:22:25 +02006794static int stats_table_request(struct stream_interface *si, int action)
Willy Tarreau69f58c82010-07-12 17:55:33 +02006795{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006796 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau87b09662015-04-03 00:22:06 +02006797 struct stream *s = si_strm(si);
Willy Tarreau69f58c82010-07-12 17:55:33 +02006798 struct ebmb_node *eb;
6799 int dt;
Willy Tarreau44455022012-12-05 23:01:12 +01006800 int skip_entry;
Willy Tarreau20e99322013-04-13 09:22:25 +02006801 int show = action == STAT_CLI_O_TAB;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006802
6803 /*
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006804 * We have 3 possible states in appctx->st2 :
Willy Tarreau295a8372011-03-10 11:25:07 +01006805 * - STAT_ST_INIT : the first call
6806 * - STAT_ST_INFO : the proxy pointer points to the next table to
Willy Tarreau69f58c82010-07-12 17:55:33 +02006807 * dump, the entry pointer is NULL ;
Willy Tarreau295a8372011-03-10 11:25:07 +01006808 * - STAT_ST_LIST : the proxy pointer points to the current table
Willy Tarreau69f58c82010-07-12 17:55:33 +02006809 * and the entry pointer points to the next entry to be dumped,
6810 * and the refcount on the next entry is held ;
Willy Tarreau295a8372011-03-10 11:25:07 +01006811 * - STAT_ST_END : nothing left to dump, the buffer may contain some
Willy Tarreau69f58c82010-07-12 17:55:33 +02006812 * data though.
6813 */
6814
Willy Tarreau2bb4a962014-11-28 11:11:05 +01006815 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW))) {
Willy Tarreau69f58c82010-07-12 17:55:33 +02006816 /* in case of abort, remove any refcount we might have set on an entry */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006817 if (appctx->st2 == STAT_ST_LIST) {
6818 appctx->ctx.table.entry->ref_cnt--;
6819 stksess_kill_if_expired(&appctx->ctx.table.proxy->table, appctx->ctx.table.entry);
Willy Tarreauf6efda12010-08-03 20:34:06 +02006820 }
Willy Tarreau69f58c82010-07-12 17:55:33 +02006821 return 1;
6822 }
6823
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006824 chunk_reset(&trash);
Willy Tarreau69f58c82010-07-12 17:55:33 +02006825
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006826 while (appctx->st2 != STAT_ST_FIN) {
6827 switch (appctx->st2) {
Willy Tarreau295a8372011-03-10 11:25:07 +01006828 case STAT_ST_INIT:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006829 appctx->ctx.table.proxy = appctx->ctx.table.target;
6830 if (!appctx->ctx.table.proxy)
6831 appctx->ctx.table.proxy = proxy;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006832
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006833 appctx->ctx.table.entry = NULL;
6834 appctx->st2 = STAT_ST_INFO;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006835 break;
6836
Willy Tarreau295a8372011-03-10 11:25:07 +01006837 case STAT_ST_INFO:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006838 if (!appctx->ctx.table.proxy ||
6839 (appctx->ctx.table.target &&
6840 appctx->ctx.table.proxy != appctx->ctx.table.target)) {
6841 appctx->st2 = STAT_ST_END;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006842 break;
6843 }
6844
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006845 if (appctx->ctx.table.proxy->table.size) {
6846 if (show && !stats_dump_table_head_to_buffer(&trash, si, appctx->ctx.table.proxy,
6847 appctx->ctx.table.target))
Willy Tarreau69f58c82010-07-12 17:55:33 +02006848 return 0;
6849
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006850 if (appctx->ctx.table.target &&
Willy Tarreaud0d8da92015-04-04 02:10:38 +02006851 strm_li(s)->bind_conf->level >= ACCESS_LVL_OPER) {
Willy Tarreau69f58c82010-07-12 17:55:33 +02006852 /* dump entries only if table explicitly requested */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006853 eb = ebmb_first(&appctx->ctx.table.proxy->table.keys);
Willy Tarreau69f58c82010-07-12 17:55:33 +02006854 if (eb) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006855 appctx->ctx.table.entry = ebmb_entry(eb, struct stksess, key);
6856 appctx->ctx.table.entry->ref_cnt++;
6857 appctx->st2 = STAT_ST_LIST;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006858 break;
6859 }
6860 }
6861 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006862 appctx->ctx.table.proxy = appctx->ctx.table.proxy->next;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006863 break;
6864
Willy Tarreau295a8372011-03-10 11:25:07 +01006865 case STAT_ST_LIST:
Willy Tarreau44455022012-12-05 23:01:12 +01006866 skip_entry = 0;
Simon Hormanc88b8872011-06-15 15:18:49 +09006867
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006868 if (appctx->ctx.table.data_type >= 0) {
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02006869 /* we're filtering on some data contents */
6870 void *ptr;
6871 long long data;
6872
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006873 dt = appctx->ctx.table.data_type;
6874 ptr = stktable_data_ptr(&appctx->ctx.table.proxy->table,
6875 appctx->ctx.table.entry,
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02006876 dt);
6877
6878 data = 0;
6879 switch (stktable_data_types[dt].std_type) {
6880 case STD_T_SINT:
6881 data = stktable_data_cast(ptr, std_t_sint);
6882 break;
6883 case STD_T_UINT:
6884 data = stktable_data_cast(ptr, std_t_uint);
6885 break;
6886 case STD_T_ULL:
6887 data = stktable_data_cast(ptr, std_t_ull);
6888 break;
6889 case STD_T_FRQP:
6890 data = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006891 appctx->ctx.table.proxy->table.data_arg[dt].u);
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02006892 break;
6893 }
6894
6895 /* skip the entry if the data does not match the test and the value */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006896 if ((data < appctx->ctx.table.value &&
6897 (appctx->ctx.table.data_op == STD_OP_EQ ||
6898 appctx->ctx.table.data_op == STD_OP_GT ||
6899 appctx->ctx.table.data_op == STD_OP_GE)) ||
6900 (data == appctx->ctx.table.value &&
6901 (appctx->ctx.table.data_op == STD_OP_NE ||
6902 appctx->ctx.table.data_op == STD_OP_GT ||
6903 appctx->ctx.table.data_op == STD_OP_LT)) ||
6904 (data > appctx->ctx.table.value &&
6905 (appctx->ctx.table.data_op == STD_OP_EQ ||
6906 appctx->ctx.table.data_op == STD_OP_LT ||
6907 appctx->ctx.table.data_op == STD_OP_LE)))
Willy Tarreau44455022012-12-05 23:01:12 +01006908 skip_entry = 1;
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02006909 }
6910
Simon Hormanc88b8872011-06-15 15:18:49 +09006911 if (show && !skip_entry &&
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006912 !stats_dump_table_entry_to_buffer(&trash, si, appctx->ctx.table.proxy,
6913 appctx->ctx.table.entry))
Simon Hormand9366582011-06-15 15:18:45 +09006914 return 0;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006915
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006916 appctx->ctx.table.entry->ref_cnt--;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006917
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006918 eb = ebmb_next(&appctx->ctx.table.entry->key);
Willy Tarreau69f58c82010-07-12 17:55:33 +02006919 if (eb) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006920 struct stksess *old = appctx->ctx.table.entry;
6921 appctx->ctx.table.entry = ebmb_entry(eb, struct stksess, key);
Willy Tarreau8fa52f42012-01-09 11:50:03 +01006922 if (show)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006923 stksess_kill_if_expired(&appctx->ctx.table.proxy->table, old);
6924 else if (!skip_entry && !appctx->ctx.table.entry->ref_cnt)
6925 stksess_kill(&appctx->ctx.table.proxy->table, old);
6926 appctx->ctx.table.entry->ref_cnt++;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006927 break;
6928 }
6929
Simon Hormanc88b8872011-06-15 15:18:49 +09006930
6931 if (show)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006932 stksess_kill_if_expired(&appctx->ctx.table.proxy->table, appctx->ctx.table.entry);
6933 else if (!skip_entry && !appctx->ctx.table.entry->ref_cnt)
6934 stksess_kill(&appctx->ctx.table.proxy->table, appctx->ctx.table.entry);
Simon Hormanc88b8872011-06-15 15:18:49 +09006935
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006936 appctx->ctx.table.proxy = appctx->ctx.table.proxy->next;
6937 appctx->st2 = STAT_ST_INFO;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006938 break;
6939
Willy Tarreau295a8372011-03-10 11:25:07 +01006940 case STAT_ST_END:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006941 appctx->st2 = STAT_ST_FIN;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006942 break;
6943 }
6944 }
6945 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006946}
6947
Willy Tarreaud426a182010-03-05 14:58:26 +01006948/* print a line of text buffer (limited to 70 bytes) to <out>. The format is :
Willy Tarreau74808cb2009-03-04 15:53:18 +01006949 * <2 spaces> <offset=5 digits> <space or plus> <space> <70 chars max> <\n>
6950 * which is 60 chars per line. Non-printable chars \t, \n, \r and \e are
6951 * encoded in C format. Other non-printable chars are encoded "\xHH". Original
6952 * lines are respected within the limit of 70 output chars. Lines that are
6953 * continuation of a previous truncated line begin with "+" instead of " "
6954 * after the offset. The new pointer is returned.
6955 */
Willy Tarreaud426a182010-03-05 14:58:26 +01006956static int dump_text_line(struct chunk *out, const char *buf, int bsize, int len,
6957 int *line, int ptr)
Willy Tarreau74808cb2009-03-04 15:53:18 +01006958{
6959 int end;
6960 unsigned char c;
6961
6962 end = out->len + 80;
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02006963 if (end > out->size)
Willy Tarreau74808cb2009-03-04 15:53:18 +01006964 return ptr;
6965
Willy Tarreau77804732012-10-29 16:14:26 +01006966 chunk_appendf(out, " %05d%c ", ptr, (ptr == *line) ? ' ' : '+');
Willy Tarreau74808cb2009-03-04 15:53:18 +01006967
Willy Tarreaud426a182010-03-05 14:58:26 +01006968 while (ptr < len && ptr < bsize) {
6969 c = buf[ptr];
Willy Tarreau787bbd92009-03-12 08:18:33 +01006970 if (isprint(c) && isascii(c) && c != '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006971 if (out->len > end - 2)
6972 break;
6973 out->str[out->len++] = c;
Willy Tarreau787bbd92009-03-12 08:18:33 +01006974 } else if (c == '\t' || c == '\n' || c == '\r' || c == '\e' || c == '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006975 if (out->len > end - 3)
6976 break;
6977 out->str[out->len++] = '\\';
6978 switch (c) {
6979 case '\t': c = 't'; break;
6980 case '\n': c = 'n'; break;
6981 case '\r': c = 'r'; break;
6982 case '\e': c = 'e'; break;
Willy Tarreau787bbd92009-03-12 08:18:33 +01006983 case '\\': c = '\\'; break;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006984 }
6985 out->str[out->len++] = c;
6986 } else {
6987 if (out->len > end - 5)
6988 break;
6989 out->str[out->len++] = '\\';
6990 out->str[out->len++] = 'x';
6991 out->str[out->len++] = hextab[(c >> 4) & 0xF];
6992 out->str[out->len++] = hextab[c & 0xF];
6993 }
Willy Tarreaud426a182010-03-05 14:58:26 +01006994 if (buf[ptr++] == '\n') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006995 /* we had a line break, let's return now */
6996 out->str[out->len++] = '\n';
6997 *line = ptr;
6998 return ptr;
6999 }
7000 }
7001 /* we have an incomplete line, we return it as-is */
7002 out->str[out->len++] = '\n';
7003 return ptr;
7004}
7005
Baptiste Assmann3863f972015-05-17 00:33:24 +02007006/* This function dumps counters from all resolvers section and associated name servers.
7007 * It returns 0 if the output buffer is full and it needs
7008 * to be called again, otherwise non-zero.
7009 */
7010static int stats_dump_resolvers_to_buffer(struct stream_interface *si)
7011{
7012 struct appctx *appctx = __objt_appctx(si->end);
7013 struct dns_resolvers *presolvers;
7014 struct dns_nameserver *pnameserver;
7015
7016 chunk_reset(&trash);
7017
7018 switch (appctx->st2) {
7019 case STAT_ST_INIT:
7020 appctx->st2 = STAT_ST_LIST; /* let's start producing data */
7021 /* fall through */
7022
7023 case STAT_ST_LIST:
Andrew Hayworth68d05342015-10-02 20:33:01 +00007024 if (LIST_ISEMPTY(&dns_resolvers)) {
7025 chunk_appendf(&trash, "No resolvers found\n");
7026 }
7027 else {
7028 list_for_each_entry(presolvers, &dns_resolvers, list) {
7029 if (appctx->ctx.resolvers.ptr != NULL && appctx->ctx.resolvers.ptr != presolvers)
7030 continue;
7031
7032 chunk_appendf(&trash, "Resolvers section %s\n", presolvers->id);
7033 list_for_each_entry(pnameserver, &presolvers->nameserver_list, list) {
7034 chunk_appendf(&trash, " nameserver %s:\n", pnameserver->id);
7035 chunk_appendf(&trash, " sent: %ld\n", pnameserver->counters.sent);
7036 chunk_appendf(&trash, " valid: %ld\n", pnameserver->counters.valid);
7037 chunk_appendf(&trash, " update: %ld\n", pnameserver->counters.update);
7038 chunk_appendf(&trash, " cname: %ld\n", pnameserver->counters.cname);
7039 chunk_appendf(&trash, " cname_error: %ld\n", pnameserver->counters.cname_error);
7040 chunk_appendf(&trash, " any_err: %ld\n", pnameserver->counters.any_err);
7041 chunk_appendf(&trash, " nx: %ld\n", pnameserver->counters.nx);
7042 chunk_appendf(&trash, " timeout: %ld\n", pnameserver->counters.timeout);
7043 chunk_appendf(&trash, " refused: %ld\n", pnameserver->counters.refused);
7044 chunk_appendf(&trash, " other: %ld\n", pnameserver->counters.other);
7045 chunk_appendf(&trash, " invalid: %ld\n", pnameserver->counters.invalid);
7046 chunk_appendf(&trash, " too_big: %ld\n", pnameserver->counters.too_big);
7047 chunk_appendf(&trash, " truncated: %ld\n", pnameserver->counters.truncated);
7048 chunk_appendf(&trash, " outdated: %ld\n", pnameserver->counters.outdated);
7049 }
7050 }
Baptiste Assmann3863f972015-05-17 00:33:24 +02007051 }
7052
7053 /* display response */
7054 if (bi_putchk(si_ic(si), &trash) == -1) {
7055 /* let's try again later from this session. We add ourselves into
7056 * this session's users so that it can remove us upon termination.
7057 */
7058 si->flags |= SI_FL_WAIT_ROOM;
7059 return 0;
7060 }
7061
7062 appctx->st2 = STAT_ST_FIN;
7063 /* fall through */
7064
7065 default:
7066 appctx->st2 = STAT_ST_FIN;
7067 return 1;
7068 }
7069}
7070
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02007071/* This function dumps all captured errors onto the stream interface's
Willy Tarreau306f8302013-07-08 15:53:06 +02007072 * read buffer. It returns 0 if the output buffer is full and it needs
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01007073 * to be called again, otherwise non-zero.
Willy Tarreau74808cb2009-03-04 15:53:18 +01007074 */
Simon Horman9bd2c732011-06-15 15:18:44 +09007075static int stats_dump_errors_to_buffer(struct stream_interface *si)
Willy Tarreau74808cb2009-03-04 15:53:18 +01007076{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01007077 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau74808cb2009-03-04 15:53:18 +01007078 extern const char *monthname[12];
Willy Tarreau74808cb2009-03-04 15:53:18 +01007079
Willy Tarreau2bb4a962014-11-28 11:11:05 +01007080 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
Willy Tarreau61b34732009-10-03 23:49:35 +02007081 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01007082
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007083 chunk_reset(&trash);
Willy Tarreau74808cb2009-03-04 15:53:18 +01007084
Willy Tarreau7b4b4992013-12-01 09:15:12 +01007085 if (!appctx->ctx.errors.px) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01007086 /* the function had not been called yet, let's prepare the
7087 * buffer for a response.
7088 */
Willy Tarreau10479e42010-12-12 14:00:34 +01007089 struct tm tm;
7090
7091 get_localtime(date.tv_sec, &tm);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007092 chunk_appendf(&trash, "Total events captured on [%02d/%s/%04d:%02d:%02d:%02d.%03d] : %u\n",
Willy Tarreau10479e42010-12-12 14:00:34 +01007093 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
7094 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(date.tv_usec/1000),
7095 error_snapshot_id);
7096
Willy Tarreau2bb4a962014-11-28 11:11:05 +01007097 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau10479e42010-12-12 14:00:34 +01007098 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreaufe127932015-04-21 19:23:39 +02007099 si_applet_cant_put(si);
Willy Tarreau10479e42010-12-12 14:00:34 +01007100 return 0;
7101 }
7102
Willy Tarreau7b4b4992013-12-01 09:15:12 +01007103 appctx->ctx.errors.px = proxy;
7104 appctx->ctx.errors.buf = 0;
7105 appctx->ctx.errors.bol = 0;
7106 appctx->ctx.errors.ptr = -1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01007107 }
7108
7109 /* we have two inner loops here, one for the proxy, the other one for
7110 * the buffer.
7111 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01007112 while (appctx->ctx.errors.px) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01007113 struct error_snapshot *es;
7114
Willy Tarreau7b4b4992013-12-01 09:15:12 +01007115 if (appctx->ctx.errors.buf == 0)
7116 es = &appctx->ctx.errors.px->invalid_req;
Willy Tarreau74808cb2009-03-04 15:53:18 +01007117 else
Willy Tarreau7b4b4992013-12-01 09:15:12 +01007118 es = &appctx->ctx.errors.px->invalid_rep;
Willy Tarreau74808cb2009-03-04 15:53:18 +01007119
7120 if (!es->when.tv_sec)
7121 goto next;
7122
Willy Tarreau7b4b4992013-12-01 09:15:12 +01007123 if (appctx->ctx.errors.iid >= 0 &&
7124 appctx->ctx.errors.px->uuid != appctx->ctx.errors.iid &&
7125 es->oe->uuid != appctx->ctx.errors.iid)
Willy Tarreau74808cb2009-03-04 15:53:18 +01007126 goto next;
7127
Willy Tarreau7b4b4992013-12-01 09:15:12 +01007128 if (appctx->ctx.errors.ptr < 0) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01007129 /* just print headers now */
7130
7131 char pn[INET6_ADDRSTRLEN];
7132 struct tm tm;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02007133 int port;
Willy Tarreau74808cb2009-03-04 15:53:18 +01007134
7135 get_localtime(es->when.tv_sec, &tm);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007136 chunk_appendf(&trash, " \n[%02d/%s/%04d:%02d:%02d:%02d.%03d]",
Willy Tarreau74808cb2009-03-04 15:53:18 +01007137 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
Willy Tarreau1772ece2009-04-03 14:49:12 +02007138 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(es->when.tv_usec/1000));
Willy Tarreau74808cb2009-03-04 15:53:18 +01007139
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02007140 switch (addr_to_str(&es->src, pn, sizeof(pn))) {
7141 case AF_INET:
7142 case AF_INET6:
7143 port = get_host_port(&es->src);
7144 break;
7145 default:
7146 port = 0;
7147 }
7148
Willy Tarreau7b4b4992013-12-01 09:15:12 +01007149 switch (appctx->ctx.errors.buf) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01007150 case 0:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007151 chunk_appendf(&trash,
Willy Tarreau74808cb2009-03-04 15:53:18 +01007152 " frontend %s (#%d): invalid request\n"
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02007153 " backend %s (#%d)",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01007154 appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02007155 (es->oe->cap & PR_CAP_BE) ? es->oe->id : "<NONE>",
7156 (es->oe->cap & PR_CAP_BE) ? es->oe->uuid : -1);
Willy Tarreau74808cb2009-03-04 15:53:18 +01007157 break;
7158 case 1:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007159 chunk_appendf(&trash,
Olivier Doucet08afdcb2014-09-08 11:23:00 +02007160 " backend %s (#%d): invalid response\n"
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02007161 " frontend %s (#%d)",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01007162 appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02007163 es->oe->id, es->oe->uuid);
Willy Tarreau74808cb2009-03-04 15:53:18 +01007164 break;
7165 }
7166
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007167 chunk_appendf(&trash,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02007168 ", server %s (#%d), event #%u\n"
7169 " src %s:%d, session #%d, session flags 0x%08x\n"
7170 " HTTP msg state %d, msg flags 0x%08x, tx flags 0x%08x\n"
7171 " HTTP chunk len %lld bytes, HTTP body len %lld bytes\n"
7172 " buffer flags 0x%08x, out %d bytes, total %lld bytes\n"
7173 " pending %d bytes, wrapping at %d, error at position %d:\n \n",
7174 es->srv ? es->srv->id : "<NONE>", es->srv ? es->srv->puid : -1,
7175 es->ev_id,
7176 pn, port, es->sid, es->s_flags,
7177 es->state, es->m_flags, es->t_flags,
7178 es->m_clen, es->m_blen,
7179 es->b_flags, es->b_out, es->b_tot,
7180 es->len, es->b_wrap, es->pos);
7181
Willy Tarreau2bb4a962014-11-28 11:11:05 +01007182 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01007183 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreaufe127932015-04-21 19:23:39 +02007184 si_applet_cant_put(si);
Willy Tarreau61b34732009-10-03 23:49:35 +02007185 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01007186 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01007187 appctx->ctx.errors.ptr = 0;
7188 appctx->ctx.errors.sid = es->sid;
Willy Tarreau74808cb2009-03-04 15:53:18 +01007189 }
7190
Willy Tarreau7b4b4992013-12-01 09:15:12 +01007191 if (appctx->ctx.errors.sid != es->sid) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01007192 /* the snapshot changed while we were dumping it */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007193 chunk_appendf(&trash,
Willy Tarreau74808cb2009-03-04 15:53:18 +01007194 " WARNING! update detected on this snapshot, dump interrupted. Please re-check!\n");
Willy Tarreaubc18da12015-03-13 14:00:47 +01007195 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02007196 si_applet_cant_put(si);
Willy Tarreau61b34732009-10-03 23:49:35 +02007197 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01007198 }
Willy Tarreau74808cb2009-03-04 15:53:18 +01007199 goto next;
7200 }
7201
7202 /* OK, ptr >= 0, so we have to dump the current line */
Willy Tarreauf3764b72016-03-31 13:45:10 +02007203 while (es->buf && appctx->ctx.errors.ptr < es->len && appctx->ctx.errors.ptr < global.tune.bufsize) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01007204 int newptr;
7205 int newline;
7206
Willy Tarreau7b4b4992013-12-01 09:15:12 +01007207 newline = appctx->ctx.errors.bol;
Willy Tarreauf3764b72016-03-31 13:45:10 +02007208 newptr = dump_text_line(&trash, es->buf, global.tune.bufsize, es->len, &newline, appctx->ctx.errors.ptr);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01007209 if (newptr == appctx->ctx.errors.ptr)
Willy Tarreau61b34732009-10-03 23:49:35 +02007210 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01007211
Willy Tarreau2bb4a962014-11-28 11:11:05 +01007212 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01007213 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreaufe127932015-04-21 19:23:39 +02007214 si_applet_cant_put(si);
Willy Tarreau61b34732009-10-03 23:49:35 +02007215 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01007216 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01007217 appctx->ctx.errors.ptr = newptr;
7218 appctx->ctx.errors.bol = newline;
Willy Tarreau74808cb2009-03-04 15:53:18 +01007219 };
7220 next:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01007221 appctx->ctx.errors.bol = 0;
7222 appctx->ctx.errors.ptr = -1;
7223 appctx->ctx.errors.buf++;
7224 if (appctx->ctx.errors.buf > 1) {
7225 appctx->ctx.errors.buf = 0;
7226 appctx->ctx.errors.px = appctx->ctx.errors.px->next;
Willy Tarreau74808cb2009-03-04 15:53:18 +01007227 }
Willy Tarreauae795722016-02-16 11:27:28 +01007228 }
7229
7230 /* dump complete */
7231 return 1;
7232}
7233
7234/* This function dumps all environmnent variables to the buffer. It returns 0
7235 * if the output buffer is full and it needs to be called again, otherwise
7236 * non-zero. Dumps only one entry if st2 == STAT_ST_END.
7237 */
7238static int stats_dump_env_to_buffer(struct stream_interface *si)
7239{
7240 struct appctx *appctx = __objt_appctx(si->end);
7241
7242 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
7243 return 1;
7244
7245 chunk_reset(&trash);
7246
7247 /* we have two inner loops here, one for the proxy, the other one for
7248 * the buffer.
7249 */
7250 while (*appctx->ctx.env.var) {
7251 chunk_printf(&trash, "%s\n", *appctx->ctx.env.var);
7252
7253 if (bi_putchk(si_ic(si), &trash) == -1) {
7254 si_applet_cant_put(si);
7255 return 0;
7256 }
7257 if (appctx->st2 == STAT_ST_END)
7258 break;
7259 appctx->ctx.env.var++;
Willy Tarreau74808cb2009-03-04 15:53:18 +01007260 }
7261
7262 /* dump complete */
Willy Tarreau61b34732009-10-03 23:49:35 +02007263 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01007264}
7265
Willy Tarreaud5781202012-09-22 19:32:35 +02007266/* parse the "level" argument on the bind lines */
7267static int bind_parse_level(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
7268{
7269 if (!*args[cur_arg + 1]) {
7270 memprintf(err, "'%s' : missing level", args[cur_arg]);
7271 return ERR_ALERT | ERR_FATAL;
7272 }
7273
7274 if (!strcmp(args[cur_arg+1], "user"))
7275 conf->level = ACCESS_LVL_USER;
7276 else if (!strcmp(args[cur_arg+1], "operator"))
7277 conf->level = ACCESS_LVL_OPER;
7278 else if (!strcmp(args[cur_arg+1], "admin"))
7279 conf->level = ACCESS_LVL_ADMIN;
7280 else {
7281 memprintf(err, "'%s' only supports 'user', 'operator', and 'admin' (got '%s')",
7282 args[cur_arg], args[cur_arg+1]);
7283 return ERR_ALERT | ERR_FATAL;
7284 }
7285
7286 return 0;
7287}
7288
Willy Tarreau30576452015-04-13 13:50:30 +02007289struct applet http_stats_applet = {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007290 .obj_type = OBJ_TYPE_APPLET,
Willy Tarreaub24281b2011-02-13 13:16:36 +01007291 .name = "<STATS>", /* used for logging */
7292 .fct = http_stats_io_handler,
Aman Gupta9a13e842012-04-02 18:57:53 -07007293 .release = NULL,
Willy Tarreaub24281b2011-02-13 13:16:36 +01007294};
7295
Willy Tarreau30576452015-04-13 13:50:30 +02007296static struct applet cli_applet = {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007297 .obj_type = OBJ_TYPE_APPLET,
Willy Tarreaub24281b2011-02-13 13:16:36 +01007298 .name = "<CLI>", /* used for logging */
7299 .fct = cli_io_handler,
Willy Tarreau5f9a8772012-11-26 02:22:40 +01007300 .release = cli_release_handler,
Willy Tarreaub24281b2011-02-13 13:16:36 +01007301};
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01007302
Willy Tarreaudc13c112013-06-21 23:16:39 +02007303static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreau10522fd2008-07-09 20:12:41 +02007304 { CFG_GLOBAL, "stats", stats_parse_global },
7305 { 0, NULL, NULL },
7306}};
7307
Willy Tarreaud5781202012-09-22 19:32:35 +02007308static struct bind_kw_list bind_kws = { "STAT", { }, {
7309 { "level", bind_parse_level, 1 }, /* set the unix socket admin level */
7310 { NULL, NULL, 0 },
7311}};
7312
Willy Tarreau10522fd2008-07-09 20:12:41 +02007313__attribute__((constructor))
7314static void __dumpstats_module_init(void)
7315{
7316 cfg_register_keywords(&cfg_kws);
Willy Tarreaud5781202012-09-22 19:32:35 +02007317 bind_register_keywords(&bind_kws);
Willy Tarreau10522fd2008-07-09 20:12:41 +02007318}
7319
Willy Tarreau91861262007-10-17 17:06:05 +02007320/*
7321 * Local variables:
7322 * c-indent-level: 8
7323 * c-basic-offset: 8
7324 * End:
7325 */