blob: 8d43ce559cf83479cd8f1bb79a1c9f88ad9f1a51 [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) {
1309 if (kw->parse(args, appctx) == 0 && kw->io_handler) {
1310 appctx->st0 = STAT_CLI_O_CUSTOM;
1311 appctx->io_handler = kw->io_handler;
1312 }
1313 }
1314 } else if (strcmp(args[0], "show") == 0) {
Baptiste Assmann9b6857e2015-09-18 14:49:12 +02001315 if (strcmp(args[1], "backend") == 0) {
Cyril Bonté6ca9e012016-05-06 12:18:49 +02001316 appctx->ctx.be.px = NULL;
Baptiste Assmann9b6857e2015-09-18 14:49:12 +02001317 appctx->st2 = STAT_ST_INIT;
1318 appctx->st0 = STAT_CLI_O_BACKEND;
1319 }
Willy Tarreauae795722016-02-16 11:27:28 +01001320 else if (strcmp(args[1], "env") == 0) {
1321 extern char **environ;
1322
1323 if (strm_li(s)->bind_conf->level < ACCESS_LVL_OPER) {
1324 appctx->ctx.cli.msg = stats_permission_denied_msg;
1325 appctx->st0 = STAT_CLI_PRINT;
1326 return 1;
1327 }
1328 appctx->ctx.env.var = environ;
1329 appctx->st2 = STAT_ST_INIT;
1330 appctx->st0 = STAT_CLI_O_ENV; // stats_dump_env_to_buffer
1331
1332 if (*args[2]) {
1333 int len = strlen(args[2]);
1334
1335 for (; *appctx->ctx.env.var; appctx->ctx.env.var++) {
1336 if (strncmp(*appctx->ctx.env.var, args[2], len) == 0 &&
1337 (*appctx->ctx.env.var)[len] == '=')
1338 break;
1339 }
1340 if (!*appctx->ctx.env.var) {
1341 appctx->ctx.cli.msg = "Variable not found\n";
1342 appctx->st0 = STAT_CLI_PRINT;
1343 return 1;
1344 }
1345 appctx->st2 = STAT_ST_END;
1346 }
1347 }
1348 else if (strcmp(args[1], "stat") == 0) {
Baptiste Assmann3863f972015-05-17 00:33:24 +02001349 if (strcmp(args[2], "resolvers") == 0) {
1350 struct dns_resolvers *presolvers;
1351
Andrew Hayworth68d05342015-10-02 20:33:01 +00001352 if (*args[3]) {
1353 appctx->ctx.resolvers.ptr = NULL;
1354 list_for_each_entry(presolvers, &dns_resolvers, list) {
1355 if (strcmp(presolvers->id, args[3]) == 0) {
1356 appctx->ctx.resolvers.ptr = presolvers;
1357 break;
1358 }
Baptiste Assmann3863f972015-05-17 00:33:24 +02001359 }
Andrew Hayworth68d05342015-10-02 20:33:01 +00001360 if (appctx->ctx.resolvers.ptr == NULL) {
1361 appctx->ctx.cli.msg = "Can't find that resolvers section\n";
1362 appctx->st0 = STAT_CLI_PRINT;
1363 return 1;
1364 }
Baptiste Assmann3863f972015-05-17 00:33:24 +02001365 }
1366
1367 appctx->st2 = STAT_ST_INIT;
1368 appctx->st0 = STAT_CLI_O_RESOLVERS;
1369 return 1;
1370 }
1371 else if (*args[2] && *args[3] && *args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001372 appctx->ctx.stats.flags |= STAT_BOUND;
1373 appctx->ctx.stats.iid = atoi(args[2]);
1374 appctx->ctx.stats.type = atoi(args[3]);
1375 appctx->ctx.stats.sid = atoi(args[4]);
Willy Tarreau1e62df92016-01-11 18:57:53 +01001376 if (strcmp(args[5], "typed") == 0)
1377 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001378 }
Willy Tarreau1e62df92016-01-11 18:57:53 +01001379 else if (strcmp(args[2], "typed") == 0)
1380 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001381
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001382 appctx->st2 = STAT_ST_INIT;
1383 appctx->st0 = STAT_CLI_O_STAT; // stats_dump_stat_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001384 }
1385 else if (strcmp(args[1], "info") == 0) {
Willy Tarreaucb809122016-01-11 20:08:42 +01001386 if (strcmp(args[2], "typed") == 0)
1387 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001388 appctx->st2 = STAT_ST_INIT;
1389 appctx->st0 = STAT_CLI_O_INFO; // stats_dump_info_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001390 }
Baptiste Assmann28289462015-07-03 08:01:20 +02001391 else if (strcmp(args[1], "servers") == 0 && strcmp(args[2], "state") == 0) {
Cyril Bonté76a99782016-05-06 12:18:48 +02001392 appctx->ctx.server_state.iid = 0;
1393 appctx->ctx.server_state.px = NULL;
1394 appctx->ctx.server_state.sv = NULL;
Baptiste Assmann28289462015-07-03 08:01:20 +02001395
1396 /* check if a backend name has been provided */
1397 if (*args[3]) {
1398 /* read server state from local file */
Cyril Bonté76a99782016-05-06 12:18:48 +02001399 appctx->ctx.server_state.px = proxy_be_by_name(args[3]);
Baptiste Assmann28289462015-07-03 08:01:20 +02001400
Cyril Bonté76a99782016-05-06 12:18:48 +02001401 if (!appctx->ctx.server_state.px) {
Baptiste Assmann28289462015-07-03 08:01:20 +02001402 appctx->ctx.cli.msg = "Can't find backend.\n";
1403 appctx->st0 = STAT_CLI_PRINT;
1404 return 1;
1405 }
Cyril Bonté76a99782016-05-06 12:18:48 +02001406 appctx->ctx.server_state.iid = appctx->ctx.server_state.px->uuid;
Baptiste Assmann28289462015-07-03 08:01:20 +02001407 }
1408 appctx->st2 = STAT_ST_INIT;
1409 appctx->st0 = STAT_CLI_O_SERVERS_STATE; // stats_dump_servers_state_to_buffer
1410 return 1;
1411 }
Willy Tarreau12833bb2014-01-28 16:49:56 +01001412 else if (strcmp(args[1], "pools") == 0) {
1413 appctx->st2 = STAT_ST_INIT;
1414 appctx->st0 = STAT_CLI_O_POOLS; // stats_dump_pools_to_buffer
1415 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001416 else if (strcmp(args[1], "sess") == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001417 appctx->st2 = STAT_ST_INIT;
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001418 if (strm_li(s)->bind_conf->level < ACCESS_LVL_OPER) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001419 appctx->ctx.cli.msg = stats_permission_denied_msg;
1420 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +02001421 return 1;
1422 }
Willy Tarreau76153662012-11-26 01:16:39 +01001423 if (*args[2] && strcmp(args[2], "all") == 0)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001424 appctx->ctx.sess.target = (void *)-1;
Willy Tarreau76153662012-11-26 01:16:39 +01001425 else if (*args[2])
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001426 appctx->ctx.sess.target = (void *)strtoul(args[2], NULL, 0);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01001427 else
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001428 appctx->ctx.sess.target = NULL;
Willy Tarreau87b09662015-04-03 00:22:06 +02001429 appctx->ctx.sess.section = 0; /* start with stream status */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001430 appctx->ctx.sess.pos = 0;
1431 appctx->st0 = STAT_CLI_O_SESS; // stats_dump_sess_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001432 }
1433 else if (strcmp(args[1], "errors") == 0) {
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001434 if (strm_li(s)->bind_conf->level < ACCESS_LVL_OPER) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001435 appctx->ctx.cli.msg = stats_permission_denied_msg;
1436 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +02001437 return 1;
1438 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001439 if (*args[2])
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001440 appctx->ctx.errors.iid = atoi(args[2]);
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001441 else
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001442 appctx->ctx.errors.iid = -1;
1443 appctx->ctx.errors.px = NULL;
1444 appctx->st2 = STAT_ST_INIT;
1445 appctx->st0 = STAT_CLI_O_ERR; // stats_dump_errors_to_buffer
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001446 }
Willy Tarreau69f58c82010-07-12 17:55:33 +02001447 else if (strcmp(args[1], "table") == 0) {
Willy Tarreaudec98142012-06-06 23:37:08 +02001448 stats_sock_table_request(si, args, STAT_CLI_O_TAB);
Willy Tarreau69f58c82010-07-12 17:55:33 +02001449 }
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02001450 else if (strcmp(args[1], "tls-keys") == 0) {
1451#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
William Lallemandcf9e7882016-06-14 17:45:18 +02001452 appctx->ctx.tlskeys.dump_all = 0;
William Lallemand1d0b36a2016-05-20 17:40:26 +02001453 /* no parameter, shows only file list */
1454 if (!*args[2]) {
William Lallemandcf9e7882016-06-14 17:45:18 +02001455 appctx->ctx.tlskeys.dump_all = 1;
William Lallemand1d0b36a2016-05-20 17:40:26 +02001456 appctx->st2 = STAT_ST_INIT;
1457 appctx->st0 = STAT_CLI_O_TLSK;
1458 return 1;
1459 }
1460
1461 if (args[2][0] == '*') {
1462 /* list every TLS ticket keys */
1463 appctx->ctx.tlskeys.ref = NULL;
William Lallemandcf9e7882016-06-14 17:45:18 +02001464 appctx->ctx.tlskeys.dump_all = 1;
William Lallemand1d0b36a2016-05-20 17:40:26 +02001465 } else {
1466 appctx->ctx.tlskeys.ref = tlskeys_ref_lookup_ref(args[2]);
1467 if(!appctx->ctx.tlskeys.ref) {
1468 appctx->ctx.cli.msg = "'show tls-keys' unable to locate referenced filename\n";
1469 appctx->st0 = STAT_CLI_PRINT;
1470 return 1;
1471 }
1472 }
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02001473 appctx->st2 = STAT_ST_INIT;
William Lallemand1d0b36a2016-05-20 17:40:26 +02001474 appctx->st0 = STAT_CLI_O_TLSK_ENT;
1475
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02001476#else
1477 appctx->ctx.cli.msg = "HAProxy was compiled against a version of OpenSSL "
1478 "that doesn't support specifying TLS ticket keys\n";
1479 appctx->st0 = STAT_CLI_PRINT;
1480#endif
1481 return 1;
1482 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001483 else if (strcmp(args[1], "map") == 0 ||
1484 strcmp(args[1], "acl") == 0) {
1485
1486 /* Set ACL or MAP flags. */
1487 if (args[1][0] == 'm')
1488 appctx->ctx.map.display_flags = PAT_REF_MAP;
1489 else
1490 appctx->ctx.map.display_flags = PAT_REF_ACL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001491
Cyril Bontédb98eb32016-05-06 12:18:50 +02001492 /* no parameter: display all map available */
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001493 if (!*args[2]) {
1494 appctx->st2 = STAT_ST_INIT;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001495 appctx->st0 = STAT_CLI_O_PATS;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001496 return 1;
1497 }
1498
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001499 /* lookup into the refs and check the map flag */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001500 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001501 if (!appctx->ctx.map.ref ||
1502 !(appctx->ctx.map.ref->flags & appctx->ctx.map.display_flags)) {
1503 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001504 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001505 else
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001506 appctx->ctx.cli.msg = "Unknown ACL identifier. Please use #<id> or <file>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001507 appctx->st0 = STAT_CLI_PRINT;
1508 return 1;
1509 }
1510 appctx->st2 = STAT_ST_INIT;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001511 appctx->st0 = STAT_CLI_O_PAT;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001512 }
Aman Guptaceafb4a2012-04-02 18:57:54 -07001513 else { /* neither "stat" nor "info" nor "sess" nor "errors" nor "table" */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02001514 return 0;
1515 }
1516 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001517 else if (strcmp(args[0], "clear") == 0) {
1518 if (strcmp(args[1], "counters") == 0) {
1519 struct proxy *px;
1520 struct server *sv;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001521 struct listener *li;
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001522 int clrall = 0;
1523
1524 if (strcmp(args[2], "all") == 0)
1525 clrall = 1;
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001526
Willy Tarreau6162db22009-10-10 17:13:00 +02001527 /* check permissions */
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001528 if (strm_li(s)->bind_conf->level < ACCESS_LVL_OPER ||
1529 (clrall && strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001530 appctx->ctx.cli.msg = stats_permission_denied_msg;
1531 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau6162db22009-10-10 17:13:00 +02001532 return 1;
1533 }
1534
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001535 for (px = proxy; px; px = px->next) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001536 if (clrall) {
1537 memset(&px->be_counters, 0, sizeof(px->be_counters));
1538 memset(&px->fe_counters, 0, sizeof(px->fe_counters));
1539 }
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001540 else {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001541 px->be_counters.conn_max = 0;
1542 px->be_counters.p.http.rps_max = 0;
1543 px->be_counters.sps_max = 0;
1544 px->be_counters.cps_max = 0;
1545 px->be_counters.nbpend_max = 0;
1546
1547 px->fe_counters.conn_max = 0;
1548 px->fe_counters.p.http.rps_max = 0;
1549 px->fe_counters.sps_max = 0;
1550 px->fe_counters.cps_max = 0;
1551 px->fe_counters.nbpend_max = 0;
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001552 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001553
1554 for (sv = px->srv; sv; sv = sv->next)
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001555 if (clrall)
1556 memset(&sv->counters, 0, sizeof(sv->counters));
1557 else {
1558 sv->counters.cur_sess_max = 0;
1559 sv->counters.nbpend_max = 0;
1560 sv->counters.sps_max = 0;
1561 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02001562
Willy Tarreaua7944ad2012-09-26 21:03:11 +02001563 list_for_each_entry(li, &px->conf.listeners, by_fe)
Willy Tarreau2f6bf2b2009-10-10 15:26:26 +02001564 if (li->counters) {
1565 if (clrall)
1566 memset(li->counters, 0, sizeof(*li->counters));
1567 else
1568 li->counters->conn_max = 0;
1569 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001570 }
1571
Willy Tarreau81c25d02011-09-07 15:17:21 +02001572 global.cps_max = 0;
Willy Tarreau93e7c002013-10-07 18:51:07 +02001573 global.sps_max = 0;
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001574 return 1;
1575 }
Willy Tarreau88ee3972010-07-13 13:48:00 +02001576 else if (strcmp(args[1], "table") == 0) {
Willy Tarreaudec98142012-06-06 23:37:08 +02001577 stats_sock_table_request(si, args, STAT_CLI_O_CLR);
Willy Tarreau88ee3972010-07-13 13:48:00 +02001578 /* end of processing */
1579 return 1;
1580 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001581 else if (strcmp(args[1], "map") == 0 || strcmp(args[1], "acl") == 0) {
1582 /* Set ACL or MAP flags. */
1583 if (args[1][0] == 'm')
1584 appctx->ctx.map.display_flags = PAT_REF_MAP;
1585 else
1586 appctx->ctx.map.display_flags = PAT_REF_ACL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001587
1588 /* no parameter */
1589 if (!*args[2]) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001590 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
1591 appctx->ctx.cli.msg = "Missing map identifier.\n";
1592 else
1593 appctx->ctx.cli.msg = "Missing ACL identifier.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001594 appctx->st0 = STAT_CLI_PRINT;
1595 return 1;
1596 }
1597
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001598 /* lookup into the refs and check the map flag */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001599 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001600 if (!appctx->ctx.map.ref ||
1601 !(appctx->ctx.map.ref->flags & appctx->ctx.map.display_flags)) {
1602 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001603 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001604 else
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001605 appctx->ctx.cli.msg = "Unknown ACL identifier. Please use #<id> or <file>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001606 appctx->st0 = STAT_CLI_PRINT;
1607 return 1;
1608 }
1609
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001610 /* Clear all. */
1611 pat_ref_prune(appctx->ctx.map.ref);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001612
1613 /* return response */
Thierry FOURNIER07e78c52014-12-18 15:28:01 +01001614 appctx->st0 = STAT_CLI_PROMPT;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001615 return 1;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001616 }
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001617 else {
Willy Tarreau88ee3972010-07-13 13:48:00 +02001618 /* unknown "clear" argument */
Krzysztof Piotr Oledzki719e7262009-10-04 15:02:46 +02001619 return 0;
1620 }
1621 }
Willy Tarreau38338fa2009-10-10 18:37:29 +02001622 else if (strcmp(args[0], "get") == 0) {
1623 if (strcmp(args[1], "weight") == 0) {
1624 struct proxy *px;
1625 struct server *sv;
1626
1627 /* split "backend/server" and make <line> point to server */
1628 for (line = args[2]; *line; line++)
1629 if (*line == '/') {
1630 *line++ = '\0';
1631 break;
1632 }
1633
1634 if (!*line) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001635 appctx->ctx.cli.msg = "Require 'backend/server'.\n";
1636 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau38338fa2009-10-10 18:37:29 +02001637 return 1;
1638 }
1639
1640 if (!get_backend_server(args[2], line, &px, &sv)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001641 appctx->ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
1642 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau38338fa2009-10-10 18:37:29 +02001643 return 1;
1644 }
1645
1646 /* return server's effective weight at the moment */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01001647 snprintf(trash.str, trash.size, "%d (initial %d)\n", sv->uweight, sv->iweight);
Willy Tarreaubc18da12015-03-13 14:00:47 +01001648 if (bi_putstr(si_ic(si), trash.str) == -1)
Willy Tarreaufe127932015-04-21 19:23:39 +02001649 si_applet_cant_put(si);
Willy Tarreaubc18da12015-03-13 14:00:47 +01001650
Willy Tarreau38338fa2009-10-10 18:37:29 +02001651 return 1;
1652 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001653 else if (strcmp(args[1], "map") == 0 || strcmp(args[1], "acl") == 0) {
1654 /* Set flags. */
1655 if (args[1][0] == 'm')
1656 appctx->ctx.map.display_flags = PAT_REF_MAP;
1657 else
1658 appctx->ctx.map.display_flags = PAT_REF_ACL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001659
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001660 /* No parameter. */
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001661 if (!*args[2] || !*args[3]) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001662 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
1663 appctx->ctx.cli.msg = "Missing map identifier and/or key.\n";
1664 else
1665 appctx->ctx.cli.msg = "Missing ACL identifier and/or key.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001666 appctx->st0 = STAT_CLI_PRINT;
1667 return 1;
1668 }
1669
1670 /* lookup into the maps */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01001671 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001672 if (!appctx->ctx.map.ref) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001673 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001674 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001675 else
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01001676 appctx->ctx.cli.msg = "Unknown ACL identifier. Please use #<id> or <file>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001677 appctx->st0 = STAT_CLI_PRINT;
1678 return 1;
1679 }
1680
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01001681 /* copy input string. The string must be allocated because
1682 * it may be used over multiple iterations. It's released
1683 * at the end and upon abort anyway.
1684 */
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01001685 appctx->ctx.map.chunk.len = strlen(args[3]);
1686 appctx->ctx.map.chunk.size = appctx->ctx.map.chunk.len + 1;
1687 appctx->ctx.map.chunk.str = strdup(args[3]);
1688 if (!appctx->ctx.map.chunk.str) {
1689 appctx->ctx.cli.msg = "Out of memory error.\n";
1690 appctx->st0 = STAT_CLI_PRINT;
1691 return 1;
1692 }
1693
1694 /* prepare response */
1695 appctx->st2 = STAT_ST_INIT;
1696 appctx->st0 = STAT_CLI_O_MLOOK;
1697 }
Willy Tarreau38338fa2009-10-10 18:37:29 +02001698 else { /* not "get weight" */
1699 return 0;
1700 }
1701 }
Willy Tarreau4483d432009-10-10 19:30:08 +02001702 else if (strcmp(args[0], "set") == 0) {
1703 if (strcmp(args[1], "weight") == 0) {
Willy Tarreau4483d432009-10-10 19:30:08 +02001704 struct server *sv;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001705 const char *warning;
Willy Tarreau4483d432009-10-10 19:30:08 +02001706
Willy Tarreaud52c41e2011-09-07 23:41:01 +02001707 sv = expect_server_admin(s, si, args[2]);
1708 if (!sv)
Willy Tarreau4483d432009-10-10 19:30:08 +02001709 return 1;
Willy Tarreau4483d432009-10-10 19:30:08 +02001710
Simon Horman7d09b9a2013-02-12 10:45:51 +09001711 warning = server_parse_weight_change_request(sv, args[3]);
1712 if (warning) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001713 appctx->ctx.cli.msg = warning;
1714 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau4483d432009-10-10 19:30:08 +02001715 }
Willy Tarreau4483d432009-10-10 19:30:08 +02001716 return 1;
1717 }
Willy Tarreau2a4b70f2014-05-22 18:42:35 +02001718 else if (strcmp(args[1], "server") == 0) {
1719 struct server *sv;
1720 const char *warning;
1721
1722 sv = expect_server_admin(s, si, args[2]);
1723 if (!sv)
1724 return 1;
1725
1726 if (strcmp(args[3], "weight") == 0) {
1727 warning = server_parse_weight_change_request(sv, args[4]);
1728 if (warning) {
1729 appctx->ctx.cli.msg = warning;
1730 appctx->st0 = STAT_CLI_PRINT;
1731 }
1732 }
1733 else if (strcmp(args[3], "state") == 0) {
1734 if (strcmp(args[4], "ready") == 0)
1735 srv_adm_set_ready(sv);
1736 else if (strcmp(args[4], "drain") == 0)
1737 srv_adm_set_drain(sv);
1738 else if (strcmp(args[4], "maint") == 0)
1739 srv_adm_set_maint(sv);
1740 else {
1741 appctx->ctx.cli.msg = "'set server <srv> state' expects 'ready', 'drain' and 'maint'.\n";
1742 appctx->st0 = STAT_CLI_PRINT;
1743 }
1744 }
1745 else if (strcmp(args[3], "health") == 0) {
1746 if (sv->track) {
1747 appctx->ctx.cli.msg = "cannot change health on a tracking server.\n";
1748 appctx->st0 = STAT_CLI_PRINT;
1749 }
1750 else if (strcmp(args[4], "up") == 0) {
1751 sv->check.health = sv->check.rise + sv->check.fall - 1;
1752 srv_set_running(sv, "changed from CLI");
1753 }
1754 else if (strcmp(args[4], "stopping") == 0) {
1755 sv->check.health = sv->check.rise + sv->check.fall - 1;
1756 srv_set_stopping(sv, "changed from CLI");
1757 }
1758 else if (strcmp(args[4], "down") == 0) {
1759 sv->check.health = 0;
1760 srv_set_stopped(sv, "changed from CLI");
1761 }
1762 else {
1763 appctx->ctx.cli.msg = "'set server <srv> health' expects 'up', 'stopping', or 'down'.\n";
1764 appctx->st0 = STAT_CLI_PRINT;
1765 }
1766 }
1767 else if (strcmp(args[3], "agent") == 0) {
1768 if (!(sv->agent.state & CHK_ST_ENABLED)) {
1769 appctx->ctx.cli.msg = "agent checks are not enabled on this server.\n";
1770 appctx->st0 = STAT_CLI_PRINT;
1771 }
1772 else if (strcmp(args[4], "up") == 0) {
1773 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
1774 srv_set_running(sv, "changed from CLI");
1775 }
1776 else if (strcmp(args[4], "down") == 0) {
1777 sv->agent.health = 0;
1778 srv_set_stopped(sv, "changed from CLI");
1779 }
1780 else {
1781 appctx->ctx.cli.msg = "'set server <srv> agent' expects 'up' or 'down'.\n";
1782 appctx->st0 = STAT_CLI_PRINT;
1783 }
1784 }
Baptiste Assmann50946562016-08-31 23:26:29 +02001785 else if (strcmp(args[3], "check-port") == 0) {
1786 int i = 0;
1787 if (strl2irc(args[4], strlen(args[4]), &i) != 0) {
1788 appctx->ctx.cli.msg = "'set server <srv> check-port' expects an integer as argument.\n";
1789 appctx->st0 = STAT_CLI_PRINT;
1790 }
1791 if ((i < 0) || (i > 65535)) {
1792 appctx->ctx.cli.msg = "provided port is not valid.\n";
1793 appctx->st0 = STAT_CLI_PRINT;
1794 }
1795 /* prevent the update of port to 0 if MAPPORTS are in use */
1796 if ((sv->flags & SRV_F_MAPPORTS) && (i == 0)) {
1797 appctx->ctx.cli.msg = "can't unset 'port' since MAPPORTS is in use.\n";
1798 appctx->st0 = STAT_CLI_PRINT;
1799 return 1;
1800 }
1801 sv->check.port = i;
1802 appctx->ctx.cli.msg = "health check port updated.\n";
1803 appctx->st0 = STAT_CLI_PRINT;
1804 }
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001805 else if (strcmp(args[3], "addr") == 0) {
Baptiste Assmann3749ebf2016-08-03 22:34:12 +02001806 char *addr = NULL;
1807 char *port = NULL;
1808 if (strlen(args[4]) == 0) {
1809 appctx->ctx.cli.msg = "set server <b>/<s> requires an <addr> .\n";
1810 appctx->st0 = STAT_CLI_PRINT;
1811 return 1;
1812 }
1813 else {
1814 addr = args[4];
1815 }
1816 if (strcmp(args[5], "port") == 0) {
1817 port = args[6];
1818 }
1819 warning = update_server_addr_port(sv, addr, port, "stats socket command");
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001820 if (warning) {
1821 appctx->ctx.cli.msg = warning;
1822 appctx->st0 = STAT_CLI_PRINT;
1823 }
1824 }
Willy Tarreau2a4b70f2014-05-22 18:42:35 +02001825 else {
Baptiste Assmann50946562016-08-31 23:26:29 +02001826 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 +02001827 appctx->st0 = STAT_CLI_PRINT;
1828 }
1829 return 1;
1830 }
Willy Tarreau7aabd112010-01-26 10:59:06 +01001831 else if (strcmp(args[1], "timeout") == 0) {
1832 if (strcmp(args[2], "cli") == 0) {
1833 unsigned timeout;
1834 const char *res;
1835
1836 if (!*args[3]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001837 appctx->ctx.cli.msg = "Expects an integer value.\n";
1838 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001839 return 1;
1840 }
1841
1842 res = parse_time_err(args[3], &timeout, TIME_UNIT_S);
1843 if (res || timeout < 1) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001844 appctx->ctx.cli.msg = "Invalid timeout value.\n";
1845 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001846 return 1;
1847 }
1848
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001849 s->req.rto = s->res.wto = 1 + MS_TO_TICKS(timeout*1000);
Willy Tarreau7aabd112010-01-26 10:59:06 +01001850 return 1;
1851 }
1852 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001853 appctx->ctx.cli.msg = "'set timeout' only supports 'cli'.\n";
1854 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau7aabd112010-01-26 10:59:06 +01001855 return 1;
1856 }
1857 }
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001858 else if (strcmp(args[1], "maxconn") == 0) {
1859 if (strcmp(args[2], "frontend") == 0) {
1860 struct proxy *px;
1861 struct listener *l;
1862 int v;
1863
Willy Tarreau532a4502011-09-07 22:37:44 +02001864 px = expect_frontend_admin(s, si, args[3]);
1865 if (!px)
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001866 return 1;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001867
1868 if (!*args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001869 appctx->ctx.cli.msg = "Integer value expected.\n";
1870 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001871 return 1;
1872 }
1873
1874 v = atoi(args[4]);
Willy Tarreau3c7a79d2012-09-26 21:07:15 +02001875 if (v < 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001876 appctx->ctx.cli.msg = "Value out of range.\n";
1877 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001878 return 1;
1879 }
1880
1881 /* OK, the value is fine, so we assign it to the proxy and to all of
1882 * its listeners. The blocked ones will be dequeued.
1883 */
1884 px->maxconn = v;
Willy Tarreaua7944ad2012-09-26 21:03:11 +02001885 list_for_each_entry(l, &px->conf.listeners, by_fe) {
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001886 l->maxconn = v;
1887 if (l->state == LI_FULL)
1888 resume_listener(l);
1889 }
1890
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001891 if (px->maxconn > px->feconn && !LIST_ISEMPTY(&strm_fe(s)->listener_queue))
1892 dequeue_all_listeners(&strm_fe(s)->listener_queue);
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001893
1894 return 1;
1895 }
Andrew Hayworthedb93a72015-10-27 21:46:25 +00001896 else if (strcmp(args[2], "server") == 0) {
1897 struct server *sv;
Nenad Merdanovic5c3ed342016-04-24 23:10:07 +02001898 const char *warning;
Andrew Hayworthedb93a72015-10-27 21:46:25 +00001899
1900 sv = expect_server_admin(s, si, args[3]);
1901 if (!sv)
1902 return 1;
1903
Nenad Merdanovic5c3ed342016-04-24 23:10:07 +02001904 warning = server_parse_maxconn_change_request(sv, args[4]);
1905 if (warning) {
1906 appctx->ctx.cli.msg = warning;
Andrew Hayworthedb93a72015-10-27 21:46:25 +00001907 appctx->st0 = STAT_CLI_PRINT;
Andrew Hayworthedb93a72015-10-27 21:46:25 +00001908 }
1909
Andrew Hayworthedb93a72015-10-27 21:46:25 +00001910 return 1;
1911 }
Willy Tarreau91886b62011-09-07 14:38:31 +02001912 else if (strcmp(args[2], "global") == 0) {
1913 int v;
1914
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001915 if (strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001916 appctx->ctx.cli.msg = stats_permission_denied_msg;
1917 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau91886b62011-09-07 14:38:31 +02001918 return 1;
1919 }
1920
1921 if (!*args[3]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001922 appctx->ctx.cli.msg = "Expects an integer value.\n";
1923 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau91886b62011-09-07 14:38:31 +02001924 return 1;
1925 }
1926
1927 v = atoi(args[3]);
1928 if (v > global.hardmaxconn) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001929 appctx->ctx.cli.msg = "Value out of range.\n";
1930 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau91886b62011-09-07 14:38:31 +02001931 return 1;
1932 }
1933
1934 /* check for unlimited values */
1935 if (v <= 0)
1936 v = global.hardmaxconn;
1937
1938 global.maxconn = v;
1939
1940 /* Dequeues all of the listeners waiting for a resource */
1941 if (!LIST_ISEMPTY(&global_listener_queue))
1942 dequeue_all_listeners(&global_listener_queue);
1943
1944 return 1;
1945 }
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001946 else {
Andrew Hayworthedb93a72015-10-27 21:46:25 +00001947 appctx->ctx.cli.msg = "'set maxconn' only supports 'frontend', 'server', and 'global'.\n";
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001948 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau2a0f4d22011-08-02 11:49:05 +02001949 return 1;
1950 }
1951 }
Willy Tarreauf5b22872011-09-07 16:13:44 +02001952 else if (strcmp(args[1], "rate-limit") == 0) {
1953 if (strcmp(args[2], "connections") == 0) {
1954 if (strcmp(args[3], "global") == 0) {
1955 int v;
1956
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001957 if (strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001958 appctx->ctx.cli.msg = stats_permission_denied_msg;
1959 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001960 return 1;
1961 }
1962
1963 if (!*args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001964 appctx->ctx.cli.msg = "Expects an integer value.\n";
1965 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001966 return 1;
1967 }
1968
1969 v = atoi(args[4]);
1970 if (v < 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001971 appctx->ctx.cli.msg = "Value out of range.\n";
1972 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001973 return 1;
1974 }
1975
1976 global.cps_lim = v;
1977
1978 /* Dequeues all of the listeners waiting for a resource */
1979 if (!LIST_ISEMPTY(&global_listener_queue))
1980 dequeue_all_listeners(&global_listener_queue);
1981
1982 return 1;
1983 }
1984 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01001985 appctx->ctx.cli.msg = "'set rate-limit connections' only supports 'global'.\n";
1986 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02001987 return 1;
1988 }
1989 }
Willy Tarreau93e7c002013-10-07 18:51:07 +02001990 else if (strcmp(args[2], "sessions") == 0) {
1991 if (strcmp(args[3], "global") == 0) {
1992 int v;
1993
Willy Tarreaud0d8da92015-04-04 02:10:38 +02001994 if (strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau93e7c002013-10-07 18:51:07 +02001995 appctx->ctx.cli.msg = stats_permission_denied_msg;
1996 appctx->st0 = STAT_CLI_PRINT;
1997 return 1;
1998 }
1999
2000 if (!*args[4]) {
2001 appctx->ctx.cli.msg = "Expects an integer value.\n";
2002 appctx->st0 = STAT_CLI_PRINT;
2003 return 1;
2004 }
2005
2006 v = atoi(args[4]);
2007 if (v < 0) {
2008 appctx->ctx.cli.msg = "Value out of range.\n";
2009 appctx->st0 = STAT_CLI_PRINT;
2010 return 1;
2011 }
2012
2013 global.sps_lim = v;
2014
2015 /* Dequeues all of the listeners waiting for a resource */
2016 if (!LIST_ISEMPTY(&global_listener_queue))
2017 dequeue_all_listeners(&global_listener_queue);
2018
2019 return 1;
2020 }
2021 else {
2022 appctx->ctx.cli.msg = "'set rate-limit sessions' only supports 'global'.\n";
2023 appctx->st0 = STAT_CLI_PRINT;
2024 return 1;
2025 }
2026 }
Willy Tarreaue43d5322013-10-07 20:01:52 +02002027#ifdef USE_OPENSSL
2028 else if (strcmp(args[2], "ssl-sessions") == 0) {
2029 if (strcmp(args[3], "global") == 0) {
2030 int v;
2031
Willy Tarreaud0d8da92015-04-04 02:10:38 +02002032 if (strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreaue43d5322013-10-07 20:01:52 +02002033 appctx->ctx.cli.msg = stats_permission_denied_msg;
2034 appctx->st0 = STAT_CLI_PRINT;
2035 return 1;
2036 }
2037
2038 if (!*args[4]) {
2039 appctx->ctx.cli.msg = "Expects an integer value.\n";
2040 appctx->st0 = STAT_CLI_PRINT;
2041 return 1;
2042 }
2043
2044 v = atoi(args[4]);
2045 if (v < 0) {
2046 appctx->ctx.cli.msg = "Value out of range.\n";
2047 appctx->st0 = STAT_CLI_PRINT;
2048 return 1;
2049 }
2050
2051 global.ssl_lim = v;
2052
2053 /* Dequeues all of the listeners waiting for a resource */
2054 if (!LIST_ISEMPTY(&global_listener_queue))
2055 dequeue_all_listeners(&global_listener_queue);
2056
2057 return 1;
2058 }
2059 else {
2060 appctx->ctx.cli.msg = "'set rate-limit ssl-sessions' only supports 'global'.\n";
2061 appctx->st0 = STAT_CLI_PRINT;
2062 return 1;
2063 }
2064 }
2065#endif
William Lallemandd85f9172012-11-09 17:05:39 +01002066 else if (strcmp(args[2], "http-compression") == 0) {
2067 if (strcmp(args[3], "global") == 0) {
2068 int v;
2069
Willy Tarreaua1c2b2c2015-11-26 18:32:39 +01002070 if (strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN) {
2071 appctx->ctx.cli.msg = stats_permission_denied_msg;
2072 appctx->st0 = STAT_CLI_PRINT;
2073 return 1;
2074 }
2075
Willy Tarreau85d47f92012-11-21 00:29:50 +01002076 if (!*args[4]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002077 appctx->ctx.cli.msg = "Expects a maximum input byte rate in kB/s.\n";
2078 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau85d47f92012-11-21 00:29:50 +01002079 return 1;
2080 }
2081
William Lallemandd85f9172012-11-09 17:05:39 +01002082 v = atoi(args[4]);
2083 global.comp_rate_lim = v * 1024; /* Kilo to bytes. */
2084 }
2085 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002086 appctx->ctx.cli.msg = "'set rate-limit http-compression' only supports 'global'.\n";
2087 appctx->st0 = STAT_CLI_PRINT;
William Lallemandd85f9172012-11-09 17:05:39 +01002088 return 1;
2089 }
2090 }
Willy Tarreauf5b22872011-09-07 16:13:44 +02002091 else {
Willy Tarreaue43d5322013-10-07 20:01:52 +02002092 appctx->ctx.cli.msg = "'set rate-limit' supports 'connections', 'sessions', 'ssl-sessions', and 'http-compression'.\n";
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002093 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauf5b22872011-09-07 16:13:44 +02002094 return 1;
2095 }
2096 }
Willy Tarreau654694e2012-06-07 01:03:16 +02002097 else if (strcmp(args[1], "table") == 0) {
2098 stats_sock_table_request(si, args, STAT_CLI_O_SET);
2099 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002100 else if (strcmp(args[1], "map") == 0) {
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002101 char *err;
2102
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002103 /* Set flags. */
2104 appctx->ctx.map.display_flags = PAT_REF_MAP;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002105
2106 /* Expect three parameters: map name, key and new value. */
2107 if (!*args[2] || !*args[3] || !*args[4]) {
Thierry FOURNIERd5723432014-03-11 13:52:44 +01002108 appctx->ctx.cli.msg = "'set map' expects three parameters: map identifier, key and value.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002109 appctx->st0 = STAT_CLI_PRINT;
2110 return 1;
2111 }
2112
2113 /* Lookup the reference in the maps. */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002114 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002115 if (!appctx->ctx.map.ref) {
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01002116 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002117 appctx->st0 = STAT_CLI_PRINT;
2118 return 1;
2119 }
2120
Thierry FOURNIER9356c682014-01-28 15:55:37 +01002121 /* If the entry identifier start with a '#', it is considered as
2122 * pointer id
2123 */
2124 if (args[3][0] == '#' && args[3][1] == '0' && args[3][2] == 'x') {
2125 struct pat_ref_elt *ref;
2126 long long int conv;
2127 char *error;
2128
2129 /* Convert argument to integer value. */
2130 conv = strtoll(&args[3][1], &error, 16);
2131 if (*error != '\0') {
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01002132 appctx->ctx.cli.msg = "Malformed identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER9356c682014-01-28 15:55:37 +01002133 appctx->st0 = STAT_CLI_PRINT;
2134 return 1;
2135 }
2136
2137 /* Convert and check integer to pointer. */
2138 ref = (struct pat_ref_elt *)(long)conv;
2139 if ((long long int)(long)ref != conv) {
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01002140 appctx->ctx.cli.msg = "Malformed identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER9356c682014-01-28 15:55:37 +01002141 appctx->st0 = STAT_CLI_PRINT;
2142 return 1;
2143 }
2144
2145 /* Try to delete the entry. */
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002146 err = NULL;
2147 if (!pat_ref_set_by_id(appctx->ctx.map.ref, ref, args[4], &err)) {
2148 if (err)
2149 memprintf(&err, "%s.\n", err);
2150 appctx->ctx.cli.err = err;
2151 appctx->st0 = STAT_CLI_PRINT_FREE;
Thierry FOURNIER9356c682014-01-28 15:55:37 +01002152 return 1;
2153 }
2154 }
2155 else {
2156 /* Else, use the entry identifier as pattern
2157 * string, and update the value.
2158 */
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002159 err = NULL;
2160 if (!pat_ref_set(appctx->ctx.map.ref, args[3], args[4], &err)) {
2161 if (err)
2162 memprintf(&err, "%s.\n", err);
2163 appctx->ctx.cli.err = err;
2164 appctx->st0 = STAT_CLI_PRINT_FREE;
Thierry FOURNIER9356c682014-01-28 15:55:37 +01002165 return 1;
2166 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002167 }
2168
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002169 /* The set is done, send message. */
Thierry FOURNIER07e78c52014-12-18 15:28:01 +01002170 appctx->st0 = STAT_CLI_PROMPT;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002171 return 1;
2172 }
Emeric Brun4147b2e2014-06-16 18:36:30 +02002173#ifdef USE_OPENSSL
2174 else if (strcmp(args[1], "ssl") == 0) {
2175 if (strcmp(args[2], "ocsp-response") == 0) {
Lukas Tribuse4e30f72014-12-09 16:32:51 +01002176#if (defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP)
Emeric Brun4147b2e2014-06-16 18:36:30 +02002177 char *err = NULL;
2178
Emeric Brunaf4ef742014-06-19 14:10:45 +02002179 /* Expect one parameter: the new response in base64 encoding */
Emeric Brun4147b2e2014-06-16 18:36:30 +02002180 if (!*args[3]) {
2181 appctx->ctx.cli.msg = "'set ssl ocsp-response' expects response in base64 encoding.\n";
2182 appctx->st0 = STAT_CLI_PRINT;
2183 return 1;
2184 }
2185
2186 trash.len = base64dec(args[3], strlen(args[3]), trash.str, trash.size);
2187 if (trash.len < 0) {
2188 appctx->ctx.cli.msg = "'set ssl ocsp-response' received invalid base64 encoded response.\n";
2189 appctx->st0 = STAT_CLI_PRINT;
2190 return 1;
2191 }
2192
2193 if (ssl_sock_update_ocsp_response(&trash, &err)) {
2194 if (err) {
2195 memprintf(&err, "%s.\n", err);
2196 appctx->ctx.cli.err = err;
2197 appctx->st0 = STAT_CLI_PRINT_FREE;
2198 }
2199 return 1;
2200 }
2201 appctx->ctx.cli.msg = "OCSP Response updated!";
2202 appctx->st0 = STAT_CLI_PRINT;
2203 return 1;
2204#else
2205 appctx->ctx.cli.msg = "HAProxy was compiled against a version of OpenSSL that doesn't support OCSP stapling.\n";
2206 appctx->st0 = STAT_CLI_PRINT;
2207 return 1;
2208#endif
2209 }
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02002210 else if (strcmp(args[2], "tls-key") == 0) {
2211#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
2212 /* Expect two parameters: the filename and the new new TLS key in encoding */
2213 if (!*args[3] || !*args[4]) {
2214 appctx->ctx.cli.msg = "'set ssl tls-key' expects a filename and the new TLS key in base64 encoding.\n";
2215 appctx->st0 = STAT_CLI_PRINT;
2216 return 1;
2217 }
2218
2219 appctx->ctx.tlskeys.ref = tlskeys_ref_lookup_ref(args[3]);
2220 if(!appctx->ctx.tlskeys.ref) {
2221 appctx->ctx.cli.msg = "'set ssl tls-key' unable to locate referenced filename\n";
2222 appctx->st0 = STAT_CLI_PRINT;
2223 return 1;
2224 }
2225
2226 trash.len = base64dec(args[4], strlen(args[4]), trash.str, trash.size);
2227 if (trash.len != sizeof(struct tls_sess_key)) {
2228 appctx->ctx.cli.msg = "'set ssl tls-key' received invalid base64 encoded TLS key.\n";
2229 appctx->st0 = STAT_CLI_PRINT;
2230 return 1;
2231 }
2232
Pradeep Jindalcc79b002015-08-20 18:25:17 +05302233 memcpy(appctx->ctx.tlskeys.ref->tlskeys + ((appctx->ctx.tlskeys.ref->tls_ticket_enc_index + 2) % TLS_TICKETS_NO), trash.str, trash.len);
2234 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 +02002235
2236 appctx->ctx.cli.msg = "TLS ticket key updated!";
2237 appctx->st0 = STAT_CLI_PRINT;
2238 return 1;
2239#else
2240 appctx->ctx.cli.msg = "HAProxy was compiled against a version of OpenSSL "
2241 "that doesn't support specifying TLS ticket keys\n";
2242 appctx->st0 = STAT_CLI_PRINT;
2243 return 1;
2244#endif
2245 }
Emeric Brun4147b2e2014-06-16 18:36:30 +02002246 else {
2247 appctx->ctx.cli.msg = "'set ssl' only supports 'ocsp-response'.\n";
2248 appctx->st0 = STAT_CLI_PRINT;
2249 return 1;
2250 }
2251 }
2252#endif
Willy Tarreau7aabd112010-01-26 10:59:06 +01002253 else { /* unknown "set" parameter */
Willy Tarreau4483d432009-10-10 19:30:08 +02002254 return 0;
2255 }
2256 }
Cyril Bontécd19e512010-01-31 22:34:03 +01002257 else if (strcmp(args[0], "enable") == 0) {
Simon Horman671b6f02013-11-25 10:46:39 +09002258 if (strcmp(args[1], "agent") == 0) {
2259 struct server *sv;
2260
2261 sv = expect_server_admin(s, si, args[2]);
2262 if (!sv)
2263 return 1;
2264
Willy Tarreau2e10f5a2013-12-11 20:11:55 +01002265 if (!(sv->agent.state & CHK_ST_CONFIGURED)) {
2266 appctx->ctx.cli.msg = "Agent was not configured on this server, cannot enable.\n";
2267 appctx->st0 = STAT_CLI_PRINT;
2268 return 1;
2269 }
2270
2271 sv->agent.state |= CHK_ST_ENABLED;
Simon Horman671b6f02013-11-25 10:46:39 +09002272 return 1;
2273 }
Willy Tarreau9b5aecd2014-05-23 11:53:10 +02002274 else if (strcmp(args[1], "health") == 0) {
Cyril Bontécd19e512010-01-31 22:34:03 +01002275 struct server *sv;
2276
Willy Tarreaud52c41e2011-09-07 23:41:01 +02002277 sv = expect_server_admin(s, si, args[2]);
2278 if (!sv)
Cyril Bonté613f0df2011-03-03 20:49:04 +01002279 return 1;
Cyril Bonté613f0df2011-03-03 20:49:04 +01002280
Willy Tarreau9b5aecd2014-05-23 11:53:10 +02002281 if (!(sv->check.state & CHK_ST_CONFIGURED)) {
2282 appctx->ctx.cli.msg = "Health checks are not configured on this server, cannot enable.\n";
2283 appctx->st0 = STAT_CLI_PRINT;
2284 return 1;
2285 }
2286
2287 sv->check.state |= CHK_ST_ENABLED;
2288 return 1;
2289 }
2290 else if (strcmp(args[1], "server") == 0) {
2291 struct server *sv;
2292
2293 sv = expect_server_admin(s, si, args[2]);
2294 if (!sv)
2295 return 1;
2296
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02002297 srv_adm_set_ready(sv);
Willy Tarreau532a4502011-09-07 22:37:44 +02002298 return 1;
2299 }
2300 else if (strcmp(args[1], "frontend") == 0) {
2301 struct proxy *px;
2302
2303 px = expect_frontend_admin(s, si, args[2]);
2304 if (!px)
2305 return 1;
2306
2307 if (px->state == PR_STSTOPPED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002308 appctx->ctx.cli.msg = "Frontend was previously shut down, cannot enable.\n";
2309 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002310 return 1;
2311 }
2312
2313 if (px->state != PR_STPAUSED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002314 appctx->ctx.cli.msg = "Frontend is already enabled.\n";
2315 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002316 return 1;
2317 }
2318
2319 if (!resume_proxy(px)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002320 appctx->ctx.cli.msg = "Failed to resume frontend, check logs for precise cause (port conflict?).\n";
2321 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002322 return 1;
2323 }
Cyril Bontécd19e512010-01-31 22:34:03 +01002324 return 1;
2325 }
2326 else { /* unknown "enable" parameter */
Willy Tarreau9b5aecd2014-05-23 11:53:10 +02002327 appctx->ctx.cli.msg = "'enable' only supports 'agent', 'frontend', 'health', and 'server'.\n";
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002328 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002329 return 1;
Cyril Bontécd19e512010-01-31 22:34:03 +01002330 }
2331 }
2332 else if (strcmp(args[0], "disable") == 0) {
Simon Horman671b6f02013-11-25 10:46:39 +09002333 if (strcmp(args[1], "agent") == 0) {
2334 struct server *sv;
2335
2336 sv = expect_server_admin(s, si, args[2]);
2337 if (!sv)
2338 return 1;
2339
Willy Tarreau2e10f5a2013-12-11 20:11:55 +01002340 sv->agent.state &= ~CHK_ST_ENABLED;
Simon Horman671b6f02013-11-25 10:46:39 +09002341 return 1;
2342 }
Willy Tarreau9b5aecd2014-05-23 11:53:10 +02002343 else if (strcmp(args[1], "health") == 0) {
2344 struct server *sv;
2345
2346 sv = expect_server_admin(s, si, args[2]);
2347 if (!sv)
2348 return 1;
2349
2350 sv->check.state &= ~CHK_ST_ENABLED;
2351 return 1;
2352 }
Simon Horman671b6f02013-11-25 10:46:39 +09002353 else if (strcmp(args[1], "server") == 0) {
Cyril Bontécd19e512010-01-31 22:34:03 +01002354 struct server *sv;
2355
Willy Tarreaud52c41e2011-09-07 23:41:01 +02002356 sv = expect_server_admin(s, si, args[2]);
2357 if (!sv)
Cyril Bonté613f0df2011-03-03 20:49:04 +01002358 return 1;
Cyril Bonté613f0df2011-03-03 20:49:04 +01002359
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02002360 srv_adm_set_maint(sv);
Willy Tarreau532a4502011-09-07 22:37:44 +02002361 return 1;
2362 }
2363 else if (strcmp(args[1], "frontend") == 0) {
2364 struct proxy *px;
2365
2366 px = expect_frontend_admin(s, si, args[2]);
2367 if (!px)
2368 return 1;
2369
2370 if (px->state == PR_STSTOPPED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002371 appctx->ctx.cli.msg = "Frontend was previously shut down, cannot disable.\n";
2372 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002373 return 1;
2374 }
2375
2376 if (px->state == PR_STPAUSED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002377 appctx->ctx.cli.msg = "Frontend is already disabled.\n";
2378 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002379 return 1;
2380 }
2381
2382 if (!pause_proxy(px)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002383 appctx->ctx.cli.msg = "Failed to pause frontend, check logs for precise cause.\n";
2384 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002385 return 1;
2386 }
Cyril Bontécd19e512010-01-31 22:34:03 +01002387 return 1;
2388 }
2389 else { /* unknown "disable" parameter */
Willy Tarreau9b5aecd2014-05-23 11:53:10 +02002390 appctx->ctx.cli.msg = "'disable' only supports 'agent', 'frontend', 'health', and 'server'.\n";
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002391 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002392 return 1;
2393 }
2394 }
2395 else if (strcmp(args[0], "shutdown") == 0) {
2396 if (strcmp(args[1], "frontend") == 0) {
2397 struct proxy *px;
2398
2399 px = expect_frontend_admin(s, si, args[2]);
2400 if (!px)
2401 return 1;
2402
2403 if (px->state == PR_STSTOPPED) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002404 appctx->ctx.cli.msg = "Frontend was already shut down.\n";
2405 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002406 return 1;
2407 }
2408
2409 Warning("Proxy %s stopped (FE: %lld conns, BE: %lld conns).\n",
2410 px->id, px->fe_counters.cum_conn, px->be_counters.cum_conn);
2411 send_log(px, LOG_WARNING, "Proxy %s stopped (FE: %lld conns, BE: %lld conns).\n",
2412 px->id, px->fe_counters.cum_conn, px->be_counters.cum_conn);
2413 stop_proxy(px);
2414 return 1;
2415 }
Willy Tarreaua295edc2011-09-07 23:21:03 +02002416 else if (strcmp(args[1], "session") == 0) {
Willy Tarreau87b09662015-04-03 00:22:06 +02002417 struct stream *sess, *ptr;
Willy Tarreaua295edc2011-09-07 23:21:03 +02002418
Willy Tarreaud0d8da92015-04-04 02:10:38 +02002419 if (strm_li(s)->bind_conf->level < ACCESS_LVL_ADMIN) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002420 appctx->ctx.cli.msg = stats_permission_denied_msg;
2421 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaua295edc2011-09-07 23:21:03 +02002422 return 1;
2423 }
2424
2425 if (!*args[2]) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002426 appctx->ctx.cli.msg = "Session pointer expected (use 'show sess').\n";
2427 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaua295edc2011-09-07 23:21:03 +02002428 return 1;
2429 }
2430
2431 ptr = (void *)strtoul(args[2], NULL, 0);
2432
Willy Tarreau87b09662015-04-03 00:22:06 +02002433 /* first, look for the requested stream in the stream table */
2434 list_for_each_entry(sess, &streams, list) {
Willy Tarreaua295edc2011-09-07 23:21:03 +02002435 if (sess == ptr)
2436 break;
2437 }
2438
Willy Tarreau87b09662015-04-03 00:22:06 +02002439 /* do we have the stream ? */
Willy Tarreaua295edc2011-09-07 23:21:03 +02002440 if (sess != ptr) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002441 appctx->ctx.cli.msg = "No such session (use 'show sess').\n";
2442 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreaua295edc2011-09-07 23:21:03 +02002443 return 1;
2444 }
2445
Willy Tarreaue7dff022015-04-03 01:14:29 +02002446 stream_shutdown(sess, SF_ERR_KILLED);
Willy Tarreaua295edc2011-09-07 23:21:03 +02002447 return 1;
2448 }
Willy Tarreau52b2d222011-09-07 23:48:48 +02002449 else if (strcmp(args[1], "sessions") == 0) {
2450 if (strcmp(args[2], "server") == 0) {
2451 struct server *sv;
Willy Tarreau87b09662015-04-03 00:22:06 +02002452 struct stream *sess, *sess_bck;
Willy Tarreau52b2d222011-09-07 23:48:48 +02002453
2454 sv = expect_server_admin(s, si, args[3]);
2455 if (!sv)
2456 return 1;
2457
Willy Tarreau87b09662015-04-03 00:22:06 +02002458 /* kill all the stream that are on this server */
Willy Tarreau52b2d222011-09-07 23:48:48 +02002459 list_for_each_entry_safe(sess, sess_bck, &sv->actconns, by_srv)
2460 if (sess->srv_conn == sv)
Willy Tarreaue7dff022015-04-03 01:14:29 +02002461 stream_shutdown(sess, SF_ERR_KILLED);
Willy Tarreau52b2d222011-09-07 23:48:48 +02002462
2463 return 1;
2464 }
2465 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002466 appctx->ctx.cli.msg = "'shutdown sessions' only supports 'server'.\n";
2467 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau52b2d222011-09-07 23:48:48 +02002468 return 1;
2469 }
2470 }
Willy Tarreau532a4502011-09-07 22:37:44 +02002471 else { /* unknown "disable" parameter */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002472 appctx->ctx.cli.msg = "'shutdown' only supports 'frontend', 'session' and 'sessions'.\n";
2473 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau532a4502011-09-07 22:37:44 +02002474 return 1;
Cyril Bontécd19e512010-01-31 22:34:03 +01002475 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002476 }
2477 else if (strcmp(args[0], "del") == 0) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002478 if (strcmp(args[1], "map") == 0 || strcmp(args[1], "acl") == 0) {
2479 if (args[1][0] == 'm')
2480 appctx->ctx.map.display_flags = PAT_REF_MAP;
2481 else
2482 appctx->ctx.map.display_flags = PAT_REF_ACL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002483
2484 /* Expect two parameters: map name and key. */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002485 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
2486 if (!*args[2] || !*args[3]) {
2487 appctx->ctx.cli.msg = "This command expects two parameters: map identifier and key.\n";
2488 appctx->st0 = STAT_CLI_PRINT;
2489 return 1;
2490 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002491 }
2492
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002493 else {
2494 if (!*args[2] || !*args[3]) {
2495 appctx->ctx.cli.msg = "This command expects two parameters: ACL identifier and key.\n";
2496 appctx->st0 = STAT_CLI_PRINT;
2497 return 1;
2498 }
2499 }
2500
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002501 /* Lookup the reference in the maps. */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002502 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002503 if (!appctx->ctx.map.ref ||
2504 !(appctx->ctx.map.ref->flags & appctx->ctx.map.display_flags)) {
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01002505 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002506 appctx->st0 = STAT_CLI_PRINT;
2507 return 1;
2508 }
2509
Thierry FOURNIER9356c682014-01-28 15:55:37 +01002510 /* If the entry identifier start with a '#', it is considered as
2511 * pointer id
2512 */
2513 if (args[3][0] == '#' && args[3][1] == '0' && args[3][2] == 'x') {
2514 struct pat_ref_elt *ref;
2515 long long int conv;
2516 char *error;
2517
2518 /* Convert argument to integer value. */
2519 conv = strtoll(&args[3][1], &error, 16);
2520 if (*error != '\0') {
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01002521 appctx->ctx.cli.msg = "Malformed identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER9356c682014-01-28 15:55:37 +01002522 appctx->st0 = STAT_CLI_PRINT;
2523 return 1;
2524 }
2525
2526 /* Convert and check integer to pointer. */
2527 ref = (struct pat_ref_elt *)(long)conv;
2528 if ((long long int)(long)ref != conv) {
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01002529 appctx->ctx.cli.msg = "Malformed identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER9356c682014-01-28 15:55:37 +01002530 appctx->st0 = STAT_CLI_PRINT;
2531 return 1;
2532 }
2533
2534 /* Try to delete the entry. */
2535 if (!pat_ref_delete_by_id(appctx->ctx.map.ref, ref)) {
2536 /* The entry is not found, send message. */
2537 appctx->ctx.cli.msg = "Key not found.\n";
2538 appctx->st0 = STAT_CLI_PRINT;
2539 return 1;
2540 }
2541 }
2542 else {
2543 /* Else, use the entry identifier as pattern
2544 * string and try to delete the entry.
2545 */
2546 if (!pat_ref_delete(appctx->ctx.map.ref, args[3])) {
2547 /* The entry is not found, send message. */
2548 appctx->ctx.cli.msg = "Key not found.\n";
2549 appctx->st0 = STAT_CLI_PRINT;
2550 return 1;
2551 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002552 }
2553
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002554 /* The deletion is done, send message. */
Thierry FOURNIER07e78c52014-12-18 15:28:01 +01002555 appctx->st0 = STAT_CLI_PROMPT;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002556 return 1;
2557 }
2558 else { /* unknown "del" parameter */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002559 appctx->ctx.cli.msg = "'del' only supports 'map' or 'acl'.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002560 appctx->st0 = STAT_CLI_PRINT;
2561 return 1;
2562 }
2563 }
2564 else if (strcmp(args[0], "add") == 0) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002565 if (strcmp(args[1], "map") == 0 ||
2566 strcmp(args[1], "acl") == 0) {
2567 int ret;
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002568 char *err;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002569
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002570 /* Set flags. */
2571 if (args[1][0] == 'm')
2572 appctx->ctx.map.display_flags = PAT_REF_MAP;
2573 else
2574 appctx->ctx.map.display_flags = PAT_REF_ACL;
2575
2576 /* If the keywork is "map", we expect three parameters, if it
2577 * is "acl", we expect only two parameters
2578 */
2579 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
2580 if (!*args[2] || !*args[3] || !*args[4]) {
2581 appctx->ctx.cli.msg = "'add map' expects three parameters: map identifier, key and value.\n";
2582 appctx->st0 = STAT_CLI_PRINT;
2583 return 1;
2584 }
2585 }
2586 else {
2587 if (!*args[2] || !*args[3]) {
2588 appctx->ctx.cli.msg = "'add acl' expects two parameters: ACL identifier and pattern.\n";
2589 appctx->st0 = STAT_CLI_PRINT;
2590 return 1;
2591 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002592 }
2593
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002594 /* Lookup for the reference. */
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01002595 appctx->ctx.map.ref = pat_ref_lookup_ref(args[2]);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002596 if (!appctx->ctx.map.ref) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002597 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01002598 appctx->ctx.cli.msg = "Unknown map identifier. Please use #<id> or <file>.\n";
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002599 else
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01002600 appctx->ctx.cli.msg = "Unknown ACL identifier. Please use #<id> or <file>.\n";
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002601 appctx->st0 = STAT_CLI_PRINT;
2602 return 1;
2603 }
2604
Thierry FOURNIER64c585f2014-01-29 20:02:36 +01002605 /* The command "add acl" is prohibited if the reference
2606 * use samples.
2607 */
2608 if ((appctx->ctx.map.display_flags & PAT_REF_ACL) &&
2609 (appctx->ctx.map.ref->flags & PAT_REF_SMP)) {
2610 appctx->ctx.cli.msg = "This ACL is shared with a map containing samples. "
2611 "You must use the command 'add map' to add values.\n";
2612 appctx->st0 = STAT_CLI_PRINT;
2613 return 1;
2614 }
2615
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002616 /* Add value. */
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002617 err = NULL;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002618 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02002619 ret = pat_ref_add(appctx->ctx.map.ref, args[3], args[4], &err);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002620 else
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02002621 ret = pat_ref_add(appctx->ctx.map.ref, args[3], NULL, &err);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002622 if (!ret) {
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002623 if (err)
2624 memprintf(&err, "%s.\n", err);
2625 appctx->ctx.cli.err = err;
2626 appctx->st0 = STAT_CLI_PRINT_FREE;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002627 return 1;
2628 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002629
2630 /* The add is done, send message. */
Thierry FOURNIER07e78c52014-12-18 15:28:01 +01002631 appctx->st0 = STAT_CLI_PROMPT;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002632 return 1;
2633 }
2634 else { /* unknown "del" parameter */
2635 appctx->ctx.cli.msg = "'add' only supports 'map'.\n";
2636 appctx->st0 = STAT_CLI_PRINT;
2637 return 1;
2638 }
Cyril Bontécd19e512010-01-31 22:34:03 +01002639 }
2640 else { /* not "show" nor "clear" nor "get" nor "set" nor "enable" nor "disable" */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002641 return 0;
2642 }
2643 return 1;
2644}
2645
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002646/* This I/O handler runs as an applet embedded in a stream interface. It is
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002647 * used to processes I/O from/to the stats unix socket. The system relies on a
2648 * state machine handling requests and various responses. We read a request,
2649 * then we process it and send the response, and we possibly display a prompt.
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002650 * Then we can read again. The state is stored in appctx->st0 and is one of the
2651 * STAT_CLI_* constants. appctx->st1 is used to indicate whether prompt is enabled
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002652 * or not.
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002653 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02002654static void cli_io_handler(struct appctx *appctx)
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002655{
Willy Tarreau00a37f02015-04-13 12:05:19 +02002656 struct stream_interface *si = appctx->owner;
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002657 struct channel *req = si_oc(si);
2658 struct channel *res = si_ic(si);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002659 int reql;
2660 int len;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002661
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002662 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
2663 goto out;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002664
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002665 while (1) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002666 if (appctx->st0 == STAT_CLI_INIT) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002667 /* Stats output not initialized yet */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002668 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
2669 appctx->st0 = STAT_CLI_GETREQ;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002670 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002671 else if (appctx->st0 == STAT_CLI_END) {
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002672 /* Let's close for real now. We just close the request
2673 * side, the conditions below will complete if needed.
2674 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02002675 si_shutw(si);
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002676 break;
2677 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002678 else if (appctx->st0 == STAT_CLI_GETREQ) {
Willy Tarreau4e33d862009-10-11 23:35:10 +02002679 /* ensure we have some output room left in the event we
2680 * would want to return some info right after parsing.
2681 */
Willy Tarreau4e4292b2014-11-28 12:18:45 +01002682 if (buffer_almost_full(si_ib(si))) {
Willy Tarreaufe127932015-04-21 19:23:39 +02002683 si_applet_cant_put(si);
Willy Tarreau4e33d862009-10-11 23:35:10 +02002684 break;
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01002685 }
Willy Tarreau4e33d862009-10-11 23:35:10 +02002686
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002687 reql = bo_getline(si_oc(si), trash.str, trash.size);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002688 if (reql <= 0) { /* closed or EOL not found */
2689 if (reql == 0)
2690 break;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002691 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002692 continue;
2693 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002694
Chad Lavoiee3f50312016-05-26 16:42:25 -04002695 /* seek for a possible unescaped semi-colon. If we find
2696 * one, we replace it with an LF and skip only this part.
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002697 */
Chad Lavoiee3f50312016-05-26 16:42:25 -04002698 for (len = 0; len < reql; len++) {
2699 if (trash.str[len] == '\\') {
2700 len++;
2701 continue;
2702 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002703 if (trash.str[len] == ';') {
2704 trash.str[len] = '\n';
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002705 reql = len + 1;
2706 break;
2707 }
Chad Lavoiee3f50312016-05-26 16:42:25 -04002708 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002709
Willy Tarreau816fc222009-10-04 07:36:58 +02002710 /* now it is time to check that we have a full line,
2711 * remove the trailing \n and possibly \r, then cut the
2712 * line.
2713 */
2714 len = reql - 1;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002715 if (trash.str[len] != '\n') {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002716 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002717 continue;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002718 }
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002719
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002720 if (len && trash.str[len-1] == '\r')
Willy Tarreau816fc222009-10-04 07:36:58 +02002721 len--;
2722
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002723 trash.str[len] = '\0';
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002724
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002725 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002726 if (len) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002727 if (strcmp(trash.str, "quit") == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002728 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002729 continue;
2730 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002731 else if (strcmp(trash.str, "prompt") == 0)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002732 appctx->st1 = !appctx->st1;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002733 else if (strcmp(trash.str, "help") == 0 ||
2734 !stats_sock_parse_request(si, trash.str)) {
William Lallemand1e08cd82016-10-13 17:57:55 +02002735 cli_gen_usage_msg();
2736 if (dynamic_usage_msg)
2737 appctx->ctx.cli.msg = dynamic_usage_msg;
2738 else
2739 appctx->ctx.cli.msg = stats_sock_usage_msg;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002740 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02002741 }
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002742 /* NB: stats_sock_parse_request() may have put
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002743 * another STAT_CLI_O_* into appctx->st0.
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002744 */
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002745 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002746 else if (!appctx->st1) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002747 /* if prompt is disabled, print help on empty lines,
2748 * so that the user at least knows how to enable
2749 * prompt and find help.
2750 */
William Lallemand1e08cd82016-10-13 17:57:55 +02002751 cli_gen_usage_msg();
2752 if (dynamic_usage_msg)
2753 appctx->ctx.cli.msg = dynamic_usage_msg;
2754 else
2755 appctx->ctx.cli.msg = stats_sock_usage_msg;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002756 appctx->st0 = STAT_CLI_PRINT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002757 }
2758
2759 /* re-adjust req buffer */
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002760 bo_skip(si_oc(si), reql);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002761 req->flags |= CF_READ_DONTWAIT; /* we plan to read small requests */
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002762 }
Willy Tarreau68c00c72015-09-25 19:21:19 +02002763 else { /* output functions */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002764 switch (appctx->st0) {
Andrew Hayworthe32d1862015-10-02 15:08:10 +00002765 case STAT_CLI_PROMPT:
2766 break;
Willy Tarreauea1f5fe2009-10-11 23:12:51 +02002767 case STAT_CLI_PRINT:
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002768 if (bi_putstr(si_ic(si), appctx->ctx.cli.msg) != -1)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002769 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreaubc18da12015-03-13 14:00:47 +01002770 else
Willy Tarreaufe127932015-04-21 19:23:39 +02002771 si_applet_cant_put(si);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002772 break;
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002773 case STAT_CLI_PRINT_FREE:
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002774 if (bi_putstr(si_ic(si), appctx->ctx.cli.err) != -1) {
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002775 free(appctx->ctx.cli.err);
2776 appctx->st0 = STAT_CLI_PROMPT;
2777 }
Willy Tarreaubc18da12015-03-13 14:00:47 +01002778 else
Willy Tarreaufe127932015-04-21 19:23:39 +02002779 si_applet_cant_put(si);
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01002780 break;
Baptiste Assmann9b6857e2015-09-18 14:49:12 +02002781 case STAT_CLI_O_BACKEND:
2782 if (stats_dump_backend_to_buffer(si))
2783 appctx->st0 = STAT_CLI_PROMPT;
2784 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002785 case STAT_CLI_O_INFO:
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002786 if (stats_dump_info_to_buffer(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002787 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002788 break;
Baptiste Assmann28289462015-07-03 08:01:20 +02002789 case STAT_CLI_O_SERVERS_STATE:
2790 if (stats_dump_servers_state_to_buffer(si))
2791 appctx->st0 = STAT_CLI_PROMPT;
2792 break;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01002793 case STAT_CLI_O_STAT:
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01002794 if (stats_dump_stat_to_buffer(si, NULL))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002795 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002796 break;
Baptiste Assmann3863f972015-05-17 00:33:24 +02002797 case STAT_CLI_O_RESOLVERS:
2798 if (stats_dump_resolvers_to_buffer(si))
2799 appctx->st0 = STAT_CLI_PROMPT;
2800 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002801 case STAT_CLI_O_SESS:
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01002802 if (stats_dump_sess_to_buffer(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002803 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002804 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002805 case STAT_CLI_O_ERR: /* errors dump */
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01002806 if (stats_dump_errors_to_buffer(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002807 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002808 break;
Willy Tarreau69f58c82010-07-12 17:55:33 +02002809 case STAT_CLI_O_TAB:
Simon Hormanc88b8872011-06-15 15:18:49 +09002810 case STAT_CLI_O_CLR:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002811 if (stats_table_request(si, appctx->st0))
2812 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau69f58c82010-07-12 17:55:33 +02002813 break;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002814 case STAT_CLI_O_PATS:
2815 if (stats_pats_list(si))
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002816 appctx->st0 = STAT_CLI_PROMPT;
2817 break;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01002818 case STAT_CLI_O_PAT:
2819 if (stats_pat_list(si))
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01002820 appctx->st0 = STAT_CLI_PROMPT;
2821 break;
2822 case STAT_CLI_O_MLOOK:
2823 if (stats_map_lookup(si))
2824 appctx->st0 = STAT_CLI_PROMPT;
Willy Tarreau4efb3532014-01-29 12:13:39 +01002825 break;
Willy Tarreau12833bb2014-01-28 16:49:56 +01002826 case STAT_CLI_O_POOLS:
2827 if (stats_dump_pools_to_buffer(si))
2828 appctx->st0 = STAT_CLI_PROMPT;
2829 break;
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02002830#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
2831 case STAT_CLI_O_TLSK:
2832 if (stats_tlskeys_list(si))
2833 appctx->st0 = STAT_CLI_PROMPT;
2834 break;
William Lallemand1d0b36a2016-05-20 17:40:26 +02002835 case STAT_CLI_O_TLSK_ENT:
2836 if (stats_tlskeys_list(si))
2837 appctx->st0 = STAT_CLI_PROMPT;
2838 break;
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02002839#endif
Willy Tarreauae795722016-02-16 11:27:28 +01002840 case STAT_CLI_O_ENV: /* environment dump */
2841 if (stats_dump_env_to_buffer(si))
2842 appctx->st0 = STAT_CLI_PROMPT;
2843 break;
William Lallemand1e08cd82016-10-13 17:57:55 +02002844 case STAT_CLI_O_CUSTOM: /* use custom pointer */
2845 if (appctx->io_handler)
2846 if (appctx->io_handler(appctx))
2847 appctx->st0 = STAT_CLI_PROMPT;
2848 break;
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002849 default: /* abnormal state */
Willy Tarreau5cfa3bc2015-09-25 20:08:51 +02002850 si->flags |= SI_FL_ERR;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002851 break;
2852 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002853
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002854 /* The post-command prompt is either LF alone or LF + '> ' in interactive mode */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002855 if (appctx->st0 == STAT_CLI_PROMPT) {
Willy Tarreau2bb4a962014-11-28 11:11:05 +01002856 if (bi_putstr(si_ic(si), appctx->st1 ? "\n> " : "\n") != -1)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002857 appctx->st0 = STAT_CLI_GETREQ;
Willy Tarreaubc18da12015-03-13 14:00:47 +01002858 else
Willy Tarreaufe127932015-04-21 19:23:39 +02002859 si_applet_cant_put(si);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002860 }
2861
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002862 /* If the output functions are still there, it means they require more room. */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002863 if (appctx->st0 >= STAT_CLI_OUTPUT)
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002864 break;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002865
2866 /* Now we close the output if one of the writers did so,
2867 * or if we're not in interactive mode and the request
2868 * buffer is empty. This still allows pipelined requests
2869 * to be sent in non-interactive mode.
2870 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002871 if ((res->flags & (CF_SHUTW|CF_SHUTW_NOW)) || (!appctx->st1 && !req->buf->o)) {
2872 appctx->st0 = STAT_CLI_END;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002873 continue;
2874 }
2875
Willy Tarreauf5a885f2009-10-04 14:22:18 +02002876 /* switch state back to GETREQ to read next requests */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002877 appctx->st0 = STAT_CLI_GETREQ;
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002878 }
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002879 }
Willy Tarreau5ca791d2009-08-16 19:06:42 +02002880
Willy Tarreauc9e930a2015-09-25 20:06:08 +02002881 if ((res->flags & CF_SHUTR) && (si->state == SI_ST_EST)) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002882 DPRINTF(stderr, "%s@%d: si to buf closed. req=%08x, res=%08x, st=%d\n",
2883 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
Aman Guptaceafb4a2012-04-02 18:57:54 -07002884 /* Other side has closed, let's abort if we have no more processing to do
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002885 * and nothing more to consume. This is comparable to a broken pipe, so
2886 * we forward the close to the request side so that it flows upstream to
2887 * the client.
2888 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02002889 si_shutw(si);
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002890 }
2891
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002892 if ((req->flags & CF_SHUTW) && (si->state == SI_ST_EST) && (appctx->st0 < STAT_CLI_OUTPUT)) {
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002893 DPRINTF(stderr, "%s@%d: buf to si closed. req=%08x, res=%08x, st=%d\n",
2894 __FUNCTION__, __LINE__, req->flags, res->flags, si->state);
2895 /* We have no more processing to do, and nothing more to send, and
2896 * the client side has closed. So we'll forward this state downstream
2897 * on the response buffer.
2898 */
Willy Tarreau73b013b2012-05-21 16:31:45 +02002899 si_shutr(si);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002900 res->flags |= CF_READ_NULL;
Willy Tarreau9a42c0d2009-09-22 19:31:03 +02002901 }
2902
Willy Tarreau828824a2015-04-19 17:20:03 +02002903 out:
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01002904 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 +02002905 __FUNCTION__, __LINE__,
Willy Tarreau9b28e032012-10-12 23:49:43 +02002906 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 +02002907}
2908
Willy Tarreau638d40a2016-02-24 23:11:01 +01002909/* Emits a stats field without any surrounding element and properly encoded to
2910 * resist CSV output. Returns non-zero on success, 0 if the buffer is full.
2911 */
Willy Tarreaub47785f2016-02-24 23:28:31 +01002912int stats_emit_raw_data_field(struct chunk *out, const struct field *f)
Willy Tarreau638d40a2016-02-24 23:11:01 +01002913{
2914 switch (field_format(f, 0)) {
2915 case FF_EMPTY: return 1;
2916 case FF_S32: return chunk_appendf(out, "%d", f->u.s32);
2917 case FF_U32: return chunk_appendf(out, "%u", f->u.u32);
2918 case FF_S64: return chunk_appendf(out, "%lld", (long long)f->u.s64);
2919 case FF_U64: return chunk_appendf(out, "%llu", (unsigned long long)f->u.u64);
2920 case FF_STR: return csv_enc_append(field_str(f, 0), 1, out) != NULL;
2921 default: return chunk_appendf(out, "[INCORRECT_FIELD_TYPE_%08x]", f->type);
2922 }
2923}
2924
Willy Tarreaub47785f2016-02-24 23:28:31 +01002925/* Emits a stats field prefixed with its type. No CSV encoding is prepared, the
2926 * output is supposed to be used on its own line. Returns non-zero on success, 0
2927 * if the buffer is full.
2928 */
2929int stats_emit_typed_data_field(struct chunk *out, const struct field *f)
2930{
2931 switch (field_format(f, 0)) {
2932 case FF_EMPTY: return 1;
2933 case FF_S32: return chunk_appendf(out, "s32:%d", f->u.s32);
2934 case FF_U32: return chunk_appendf(out, "u32:%u", f->u.u32);
2935 case FF_S64: return chunk_appendf(out, "s64:%lld", (long long)f->u.s64);
2936 case FF_U64: return chunk_appendf(out, "u64:%llu", (unsigned long long)f->u.u64);
2937 case FF_STR: return chunk_appendf(out, "str:%s", field_str(f, 0));
2938 default: return chunk_appendf(out, "%08x:?", f->type);
2939 }
2940}
2941
2942/* Emits an encoding of the field type on 3 characters followed by a delimiter.
2943 * Returns non-zero on success, 0 if the buffer is full.
2944 */
2945int stats_emit_field_tags(struct chunk *out, const struct field *f, char delim)
2946{
2947 char origin, nature, scope;
2948
2949 switch (field_origin(f, 0)) {
2950 case FO_METRIC: origin = 'M'; break;
2951 case FO_STATUS: origin = 'S'; break;
2952 case FO_KEY: origin = 'K'; break;
2953 case FO_CONFIG: origin = 'C'; break;
2954 case FO_PRODUCT: origin = 'P'; break;
2955 default: origin = '?'; break;
2956 }
2957
2958 switch (field_nature(f, 0)) {
2959 case FN_GAUGE: nature = 'G'; break;
2960 case FN_LIMIT: nature = 'L'; break;
2961 case FN_MIN: nature = 'm'; break;
2962 case FN_MAX: nature = 'M'; break;
2963 case FN_RATE: nature = 'R'; break;
2964 case FN_COUNTER: nature = 'C'; break;
2965 case FN_DURATION: nature = 'D'; break;
2966 case FN_AGE: nature = 'A'; break;
2967 case FN_TIME: nature = 'T'; break;
2968 case FN_NAME: nature = 'N'; break;
2969 case FN_OUTPUT: nature = 'O'; break;
2970 case FN_AVG: nature = 'a'; break;
2971 default: nature = '?'; break;
2972 }
2973
2974 switch (field_scope(f, 0)) {
2975 case FS_PROCESS: scope = 'P'; break;
2976 case FS_SERVICE: scope = 'S'; break;
2977 case FS_SYSTEM: scope = 's'; break;
2978 case FS_CLUSTER: scope = 'C'; break;
2979 default: scope = '?'; break;
2980 }
2981
2982 return chunk_appendf(out, "%c%c%c%c", origin, nature, scope, delim);
2983}
2984
Willy Tarreaubf95cba2016-01-11 18:27:29 +01002985/* Dump all fields from <info> into <out> using the "show info" format (name: value) */
2986static int stats_dump_info_fields(struct chunk *out, const struct field *info)
2987{
2988 int field;
2989
2990 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
2991 if (!field_format(info, field))
2992 continue;
2993
2994 if (!chunk_appendf(out, "%s: ", info_field_names[field]))
2995 return 0;
2996 if (!stats_emit_raw_data_field(out, &info[field]))
2997 return 0;
2998 if (!chunk_strcat(out, "\n"))
2999 return 0;
3000 }
3001 return 1;
3002}
3003
Willy Tarreaucb809122016-01-11 20:08:42 +01003004/* Dump all fields from <info> into <out> using the "show info typed" format */
3005static int stats_dump_typed_info_fields(struct chunk *out, const struct field *info)
3006{
3007 int field;
3008
3009 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
3010 if (!field_format(info, field))
3011 continue;
3012
3013 if (!chunk_appendf(out, "%d.%s.%u:", field, info_field_names[field], info[INF_PROCESS_NUM].u.u32))
3014 return 0;
3015 if (!stats_emit_field_tags(out, &info[field], ':'))
3016 return 0;
3017 if (!stats_emit_typed_data_field(out, &info[field]))
3018 return 0;
3019 if (!chunk_strcat(out, "\n"))
3020 return 0;
3021 }
3022 return 1;
3023}
3024
Thierry Fourniercb2c7672016-03-25 08:19:23 +01003025/* Fill <info> with HAProxy global info. <info> is preallocated
3026 * array of length <len>. The length of the aray must be
3027 * INF_TOTAL_FIELDS. If this length is less then this value, the
3028 * function returns 0, otherwise, it returns 1.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003029 */
Thierry Fourniercb2c7672016-03-25 08:19:23 +01003030int stats_fill_info(struct field *info, int len)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003031{
3032 unsigned int up = (now.tv_sec - start_date.tv_sec);
Willy Tarreau4529c072016-01-11 18:17:07 +01003033 struct chunk *out = get_trash_chunk();
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003034
Willy Tarreau0c9c2722014-05-28 12:28:58 +02003035#ifdef USE_OPENSSL
3036 int ssl_sess_rate = read_freq_ctr(&global.ssl_per_sec);
3037 int ssl_key_rate = read_freq_ctr(&global.ssl_fe_keys_per_sec);
3038 int ssl_reuse = 0;
3039
3040 if (ssl_key_rate < ssl_sess_rate) {
3041 /* count the ssl reuse ratio and avoid overflows in both directions */
3042 ssl_reuse = 100 - (100 * ssl_key_rate + (ssl_sess_rate - 1) / 2) / ssl_sess_rate;
3043 }
3044#endif
3045
Thierry Fourniercb2c7672016-03-25 08:19:23 +01003046 if (len < INF_TOTAL_FIELDS)
3047 return 0;
3048
Willy Tarreau4529c072016-01-11 18:17:07 +01003049 chunk_reset(out);
Thierry Fourniercb2c7672016-03-25 08:19:23 +01003050 memset(info, 0, sizeof(*info) * len);
Willy Tarreau4529c072016-01-11 18:17:07 +01003051
3052 info[INF_NAME] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, PRODUCT_NAME);
3053 info[INF_VERSION] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, HAPROXY_VERSION);
3054 info[INF_RELEASE_DATE] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, HAPROXY_DATE);
3055
3056 info[INF_NBPROC] = mkf_u32(FO_CONFIG|FS_SERVICE, global.nbproc);
3057 info[INF_PROCESS_NUM] = mkf_u32(FO_KEY, relative_pid);
3058 info[INF_PID] = mkf_u32(FO_STATUS, pid);
3059
3060 info[INF_UPTIME] = mkf_str(FN_DURATION, chunk_newstr(out));
3061 chunk_appendf(out, "%ud %uh%02um%02us", up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60));
3062
3063 info[INF_UPTIME_SEC] = mkf_u32(FN_DURATION, up);
3064 info[INF_MEMMAX_MB] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_memmax);
3065 info[INF_POOL_ALLOC_MB] = mkf_u32(0, (unsigned)(pool_total_allocated() / 1048576L));
3066 info[INF_POOL_USED_MB] = mkf_u32(0, (unsigned)(pool_total_used() / 1048576L));
3067 info[INF_POOL_FAILED] = mkf_u32(FN_COUNTER, pool_total_failures());
3068 info[INF_ULIMIT_N] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_nofile);
3069 info[INF_MAXSOCK] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxsock);
3070 info[INF_MAXCONN] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxconn);
3071 info[INF_HARD_MAXCONN] = mkf_u32(FO_CONFIG|FN_LIMIT, global.hardmaxconn);
3072 info[INF_CURR_CONN] = mkf_u32(0, actconn);
3073 info[INF_CUM_CONN] = mkf_u32(FN_COUNTER, totalconn);
3074 info[INF_CUM_REQ] = mkf_u32(FN_COUNTER, global.req_count);
3075#ifdef USE_OPENSSL
3076 info[INF_MAX_SSL_CONNS] = mkf_u32(FN_MAX, global.maxsslconn);
3077 info[INF_CURR_SSL_CONNS] = mkf_u32(0, sslconns);
3078 info[INF_CUM_SSL_CONNS] = mkf_u32(FN_COUNTER, totalsslconns);
3079#endif
3080 info[INF_MAXPIPES] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxpipes);
3081 info[INF_PIPES_USED] = mkf_u32(0, pipes_used);
3082 info[INF_PIPES_FREE] = mkf_u32(0, pipes_free);
3083 info[INF_CONN_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.conn_per_sec));
3084 info[INF_CONN_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.cps_lim);
3085 info[INF_MAX_CONN_RATE] = mkf_u32(FN_MAX, global.cps_max);
3086 info[INF_SESS_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.sess_per_sec));
3087 info[INF_SESS_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.sps_lim);
3088 info[INF_MAX_SESS_RATE] = mkf_u32(FN_RATE, global.sps_max);
3089
3090#ifdef USE_OPENSSL
3091 info[INF_SSL_RATE] = mkf_u32(FN_RATE, ssl_sess_rate);
3092 info[INF_SSL_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.ssl_lim);
3093 info[INF_MAX_SSL_RATE] = mkf_u32(FN_MAX, global.ssl_max);
3094 info[INF_SSL_FRONTEND_KEY_RATE] = mkf_u32(0, ssl_key_rate);
3095 info[INF_SSL_FRONTEND_MAX_KEY_RATE] = mkf_u32(FN_MAX, global.ssl_fe_keys_max);
3096 info[INF_SSL_FRONTEND_SESSION_REUSE_PCT] = mkf_u32(0, ssl_reuse);
3097 info[INF_SSL_BACKEND_KEY_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.ssl_be_keys_per_sec));
3098 info[INF_SSL_BACKEND_MAX_KEY_RATE] = mkf_u32(FN_MAX, global.ssl_be_keys_max);
3099 info[INF_SSL_CACHE_LOOKUPS] = mkf_u32(FN_COUNTER, global.shctx_lookups);
3100 info[INF_SSL_CACHE_MISSES] = mkf_u32(FN_COUNTER, global.shctx_misses);
3101#endif
3102 info[INF_COMPRESS_BPS_IN] = mkf_u32(FN_RATE, read_freq_ctr(&global.comp_bps_in));
3103 info[INF_COMPRESS_BPS_OUT] = mkf_u32(FN_RATE, read_freq_ctr(&global.comp_bps_out));
3104 info[INF_COMPRESS_BPS_RATE_LIM] = mkf_u32(FO_CONFIG|FN_LIMIT, global.comp_rate_lim);
3105#ifdef USE_ZLIB
3106 info[INF_ZLIB_MEM_USAGE] = mkf_u32(0, zlib_used_memory);
3107 info[INF_MAX_ZLIB_MEM_USAGE] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxzlibmem);
3108#endif
3109 info[INF_TASKS] = mkf_u32(0, nb_tasks_cur);
3110 info[INF_RUN_QUEUE] = mkf_u32(0, run_queue_cur);
3111 info[INF_IDLE_PCT] = mkf_u32(FN_AVG, idle_pct);
3112 info[INF_NODE] = mkf_str(FO_CONFIG|FN_OUTPUT|FS_SERVICE, global.node);
3113 if (global.desc)
3114 info[INF_DESCRIPTION] = mkf_str(FO_CONFIG|FN_OUTPUT|FS_SERVICE, global.desc);
3115
Thierry Fourniercb2c7672016-03-25 08:19:23 +01003116 return 1;
3117}
3118
3119/* This function dumps information onto the stream interface's read buffer.
3120 * It returns 0 as long as it does not complete, non-zero upon completion.
3121 * No state is used.
3122 */
3123static int stats_dump_info_to_buffer(struct stream_interface *si)
3124{
3125 struct appctx *appctx = __objt_appctx(si->end);
3126
3127 if (!stats_fill_info(info, INF_TOTAL_FIELDS))
3128 return 0;
3129
Willy Tarreau1b4ba1e2016-01-11 18:29:04 +01003130 chunk_reset(&trash);
Willy Tarreaucb809122016-01-11 20:08:42 +01003131
3132 if (appctx->ctx.stats.flags & STAT_FMT_TYPED)
3133 stats_dump_typed_info_fields(&trash, info);
3134 else
3135 stats_dump_info_fields(&trash, info);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003136
Willy Tarreaubc18da12015-03-13 14:00:47 +01003137 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02003138 si_applet_cant_put(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003139 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01003140 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003141
3142 return 1;
3143}
3144
Baptiste Assmann28289462015-07-03 08:01:20 +02003145/* dumps server state information into <buf> for all the servers found in <backend>
3146 * These information are all the parameters which may change during HAProxy runtime.
3147 * By default, we only export to the last known server state file format.
3148 * These information can be used at next startup to recover same level of server state.
3149 */
Cyril Bonté76a99782016-05-06 12:18:48 +02003150static int dump_servers_state(struct stream_interface *si, struct chunk *buf)
Baptiste Assmann28289462015-07-03 08:01:20 +02003151{
Cyril Bonté76a99782016-05-06 12:18:48 +02003152 struct appctx *appctx = __objt_appctx(si->end);
Baptiste Assmann28289462015-07-03 08:01:20 +02003153 struct server *srv;
3154 char srv_addr[INET6_ADDRSTRLEN + 1];
3155 time_t srv_time_since_last_change;
3156 int bk_f_forced_id, srv_f_forced_id;
3157
Baptiste Assmann28289462015-07-03 08:01:20 +02003158
Cyril Bonté76a99782016-05-06 12:18:48 +02003159 /* we don't want to report any state if the backend is not enabled on this process */
3160 if (appctx->ctx.server_state.px->bind_proc && !(appctx->ctx.server_state.px->bind_proc & (1UL << (relative_pid - 1))))
3161 return 1;
Baptiste Assmann28289462015-07-03 08:01:20 +02003162
Cyril Bontéd55bd7a2016-05-27 00:06:45 +02003163 if (!appctx->ctx.server_state.sv)
3164 appctx->ctx.server_state.sv = appctx->ctx.server_state.px->srv;
3165
Cyril Bonté76a99782016-05-06 12:18:48 +02003166 for (; appctx->ctx.server_state.sv != NULL; appctx->ctx.server_state.sv = srv->next) {
3167 srv = appctx->ctx.server_state.sv;
Baptiste Assmann28289462015-07-03 08:01:20 +02003168 srv_addr[0] = '\0';
Baptiste Assmann28289462015-07-03 08:01:20 +02003169
3170 switch (srv->addr.ss_family) {
3171 case AF_INET:
3172 inet_ntop(srv->addr.ss_family, &((struct sockaddr_in *)&srv->addr)->sin_addr,
3173 srv_addr, INET_ADDRSTRLEN + 1);
3174 break;
3175 case AF_INET6:
3176 inet_ntop(srv->addr.ss_family, &((struct sockaddr_in6 *)&srv->addr)->sin6_addr,
3177 srv_addr, INET6_ADDRSTRLEN + 1);
3178 break;
3179 }
3180 srv_time_since_last_change = now.tv_sec - srv->last_change;
Cyril Bonté76a99782016-05-06 12:18:48 +02003181 bk_f_forced_id = appctx->ctx.server_state.px->options & PR_O_FORCED_ID ? 1 : 0;
Baptiste Assmann28289462015-07-03 08:01:20 +02003182 srv_f_forced_id = srv->flags & SRV_F_FORCED_ID ? 1 : 0;
3183
3184 chunk_appendf(buf,
3185 "%d %s "
3186 "%d %s %s "
3187 "%d %d %d %d %ld "
3188 "%d %d %d %d %d "
3189 "%d %d"
3190 "\n",
Cyril Bonté76a99782016-05-06 12:18:48 +02003191 appctx->ctx.server_state.px->uuid, appctx->ctx.server_state.px->id,
Baptiste Assmann28289462015-07-03 08:01:20 +02003192 srv->puid, srv->id, srv_addr,
David Carlier081b3362015-11-18 06:10:22 +00003193 srv->state, srv->admin, srv->uweight, srv->iweight, (long int)srv_time_since_last_change,
Baptiste Assmann28289462015-07-03 08:01:20 +02003194 srv->check.status, srv->check.result, srv->check.health, srv->check.state, srv->agent.state,
3195 bk_f_forced_id, srv_f_forced_id);
Cyril Bonté76a99782016-05-06 12:18:48 +02003196 if (bi_putchk(si_ic(si), &trash) == -1) {
3197 si_applet_cant_put(si);
3198 return 0;
3199 }
Baptiste Assmann28289462015-07-03 08:01:20 +02003200 }
Cyril Bonté76a99782016-05-06 12:18:48 +02003201 return 1;
Baptiste Assmann28289462015-07-03 08:01:20 +02003202}
3203
Baptiste Assmann9b6857e2015-09-18 14:49:12 +02003204/* Parses backend list and simply report backend names */
3205static int stats_dump_backend_to_buffer(struct stream_interface *si)
3206{
Cyril Bonté6ca9e012016-05-06 12:18:49 +02003207 struct appctx *appctx = __objt_appctx(si->end);
Baptiste Assmann9b6857e2015-09-18 14:49:12 +02003208 extern struct proxy *proxy;
3209 struct proxy *curproxy;
3210
3211 chunk_reset(&trash);
Baptiste Assmann9b6857e2015-09-18 14:49:12 +02003212
Cyril Bonté6ca9e012016-05-06 12:18:49 +02003213 if (!appctx->ctx.be.px) {
3214 chunk_printf(&trash, "# name\n");
3215 if (bi_putchk(si_ic(si), &trash) == -1) {
3216 si_applet_cant_put(si);
3217 return 0;
3218 }
3219 appctx->ctx.be.px = proxy;
3220 }
3221
3222 for (; appctx->ctx.be.px != NULL; appctx->ctx.be.px = curproxy->next) {
3223 curproxy = appctx->ctx.be.px;
3224
Baptiste Assmann9b6857e2015-09-18 14:49:12 +02003225 /* looking for backends only */
3226 if (!(curproxy->cap & PR_CAP_BE))
3227 continue;
3228
3229 /* we don't want to list a backend which is bound to this process */
3230 if (curproxy->bind_proc && !(curproxy->bind_proc & (1UL << (relative_pid - 1))))
3231 continue;
3232
3233 chunk_appendf(&trash, "%s\n", curproxy->id);
Cyril Bonté6ca9e012016-05-06 12:18:49 +02003234 if (bi_putchk(si_ic(si), &trash) == -1) {
3235 si_applet_cant_put(si);
3236 return 0;
3237 }
Baptiste Assmann9b6857e2015-09-18 14:49:12 +02003238 }
3239
3240 return 1;
3241}
3242
Baptiste Assmann28289462015-07-03 08:01:20 +02003243/* Parses backend list or simply use backend name provided by the user to return
3244 * states of servers to stdout.
3245 */
3246static int stats_dump_servers_state_to_buffer(struct stream_interface *si)
3247{
3248 struct appctx *appctx = __objt_appctx(si->end);
3249 extern struct proxy *proxy;
3250 struct proxy *curproxy;
3251
3252 chunk_reset(&trash);
3253
Willy Tarreaua58c4352016-06-22 14:51:40 +02003254 if (appctx->st2 == STAT_ST_INIT) {
3255 if (!appctx->ctx.server_state.px)
3256 appctx->ctx.server_state.px = proxy;
3257 appctx->st2 = STAT_ST_HEAD;
3258 }
3259
3260 if (appctx->st2 == STAT_ST_HEAD) {
Cyril Bonté76a99782016-05-06 12:18:48 +02003261 chunk_printf(&trash, "%d\n# %s\n", SRV_STATE_FILE_VERSION, SRV_STATE_FILE_FIELD_NAMES);
3262 if (bi_putchk(si_ic(si), &trash) == -1) {
3263 si_applet_cant_put(si);
3264 return 0;
Baptiste Assmann28289462015-07-03 08:01:20 +02003265 }
Willy Tarreaua58c4352016-06-22 14:51:40 +02003266 appctx->st2 = STAT_ST_INFO;
Baptiste Assmann28289462015-07-03 08:01:20 +02003267 }
3268
Willy Tarreaua58c4352016-06-22 14:51:40 +02003269 /* STAT_ST_INFO */
Cyril Bonté76a99782016-05-06 12:18:48 +02003270 for (; appctx->ctx.server_state.px != NULL; appctx->ctx.server_state.px = curproxy->next) {
3271 curproxy = appctx->ctx.server_state.px;
Cyril Bonté76a99782016-05-06 12:18:48 +02003272 /* servers are only in backends */
3273 if (curproxy->cap & PR_CAP_BE) {
3274 if (!dump_servers_state(si, &trash))
3275 return 0;
3276
3277 if (bi_putchk(si_ic(si), &trash) == -1) {
3278 si_applet_cant_put(si);
3279 return 0;
3280 }
3281 }
3282 /* only the selected proxy is dumped */
3283 if (appctx->ctx.server_state.iid)
3284 break;
Baptiste Assmann28289462015-07-03 08:01:20 +02003285 }
3286
3287 return 1;
3288}
3289
Willy Tarreau12833bb2014-01-28 16:49:56 +01003290/* This function dumps memory usage information onto the stream interface's
3291 * read buffer. It returns 0 as long as it does not complete, non-zero upon
3292 * completion. No state is used.
3293 */
3294static int stats_dump_pools_to_buffer(struct stream_interface *si)
3295{
3296 dump_pools_to_trash();
Willy Tarreaubc18da12015-03-13 14:00:47 +01003297 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02003298 si_applet_cant_put(si);
Willy Tarreau12833bb2014-01-28 16:49:56 +01003299 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01003300 }
Willy Tarreau82a86022016-01-04 19:04:18 +01003301 return 1;
3302}
3303
3304/* Dump all fields from <stats> into <out> using CSV format */
3305static int stats_dump_fields_csv(struct chunk *out, const struct field *stats)
3306{
3307 int field;
3308
3309 for (field = 0; field < ST_F_TOTAL_FIELDS; field++) {
3310 if (!stats_emit_raw_data_field(out, &stats[field]))
3311 return 0;
3312 if (!chunk_strcat(out, ","))
3313 return 0;
3314 }
Conrad Hoffmann692c9382016-04-01 20:40:58 +02003315 chunk_strcat(out, "\n");
Willy Tarreau12833bb2014-01-28 16:49:56 +01003316 return 1;
3317}
3318
Willy Tarreau1e62df92016-01-11 18:57:53 +01003319/* Dump all fields from <stats> into <out> using a typed "field:desc:type:value" format */
3320static int stats_dump_fields_typed(struct chunk *out, const struct field *stats)
3321{
3322 int field;
3323
3324 for (field = 0; field < ST_F_TOTAL_FIELDS; field++) {
3325 if (!stats[field].type)
3326 continue;
3327
3328 chunk_appendf(out, "%c.%u.%u.%d.%s.%u:",
3329 stats[ST_F_TYPE].u.u32 == STATS_TYPE_FE ? 'F' :
3330 stats[ST_F_TYPE].u.u32 == STATS_TYPE_BE ? 'B' :
3331 stats[ST_F_TYPE].u.u32 == STATS_TYPE_SO ? 'L' :
3332 stats[ST_F_TYPE].u.u32 == STATS_TYPE_SV ? 'S' :
3333 '?',
3334 stats[ST_F_IID].u.u32, stats[ST_F_SID].u.u32,
3335 field, stat_field_names[field], stats[ST_F_PID].u.u32);
3336
3337 if (!stats_emit_field_tags(out, &stats[field], ':'))
3338 return 0;
3339 if (!stats_emit_typed_data_field(out, &stats[field]))
3340 return 0;
3341 if (!chunk_strcat(out, "\n"))
3342 return 0;
3343 }
3344 return 1;
3345}
3346
Willy Tarreau60600742016-01-11 15:32:30 +01003347/* Dump all fields from <stats> into <out> using the HTML format. A column is
Willy Tarreau508a63f2016-01-11 15:28:40 +01003348 * reserved for the checkbox is ST_SHOWADMIN is set in <flags>. Some extra info
3349 * are provided if ST_SHLGNDS is present in <flags>.
Cyril Bonté70be45d2010-10-12 00:14:35 +02003350 */
Willy Tarreau60600742016-01-11 15:32:30 +01003351static int stats_dump_fields_html(struct chunk *out, const struct field *stats, unsigned int flags)
Cyril Bonté70be45d2010-10-12 00:14:35 +02003352{
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003353 struct chunk src;
3354
Willy Tarreaub5f66b82016-01-08 17:15:39 +01003355 if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_FE) {
Willy Tarreau60600742016-01-11 15:32:30 +01003356 chunk_appendf(out,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003357 /* name, queue */
3358 "<tr class=\"frontend\">");
Cyril Bonté70be45d2010-10-12 00:14:35 +02003359
Willy Tarreau508a63f2016-01-11 15:28:40 +01003360 if (flags & ST_SHOWADMIN) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003361 /* Column sub-heading for Enable or Disable server */
Willy Tarreau60600742016-01-11 15:32:30 +01003362 chunk_appendf(out, "<td></td>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003363 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02003364
Willy Tarreau60600742016-01-11 15:32:30 +01003365 chunk_appendf(out,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003366 "<td class=ac>"
3367 "<a name=\"%s/Frontend\"></a>"
3368 "<a class=lfsb href=\"#%s/Frontend\">Frontend</a></td>"
3369 "<td colspan=3></td>"
3370 "",
Willy Tarreaud72c9172016-01-06 15:26:40 +01003371 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_PXNAME));
Cyril Bonté70be45d2010-10-12 00:14:35 +02003372
Willy Tarreau60600742016-01-11 15:32:30 +01003373 chunk_appendf(out,
Willy Tarreau466c9b52012-12-23 02:25:03 +01003374 /* sessions rate : current */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003375 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01003376 "<tr><th>Current connection rate:</th><td>%s/s</td></tr>"
3377 "<tr><th>Current session rate:</th><td>%s/s</td></tr>"
3378 "",
Willy Tarreaud72c9172016-01-06 15:26:40 +01003379 U2H(stats[ST_F_RATE].u.u32),
Willy Tarreauc73810f2016-01-11 13:52:04 +01003380 U2H(stats[ST_F_CONN_RATE].u.u32),
Willy Tarreaud72c9172016-01-06 15:26:40 +01003381 U2H(stats[ST_F_RATE].u.u32));
Willy Tarreau466c9b52012-12-23 02:25:03 +01003382
Willy Tarreauf8211df2016-01-11 14:09:38 +01003383 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
Willy Tarreau60600742016-01-11 15:32:30 +01003384 chunk_appendf(out,
Willy Tarreau466c9b52012-12-23 02:25:03 +01003385 "<tr><th>Current request rate:</th><td>%s/s</td></tr>",
Willy Tarreaud72c9172016-01-06 15:26:40 +01003386 U2H(stats[ST_F_REQ_RATE].u.u32));
Willy Tarreau466c9b52012-12-23 02:25:03 +01003387
Willy Tarreau60600742016-01-11 15:32:30 +01003388 chunk_appendf(out,
Willy Tarreau466c9b52012-12-23 02:25:03 +01003389 "</table></div></u></td>"
3390 /* sessions rate : max */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003391 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01003392 "<tr><th>Max connection rate:</th><td>%s/s</td></tr>"
3393 "<tr><th>Max session rate:</th><td>%s/s</td></tr>"
3394 "",
Willy Tarreaud72c9172016-01-06 15:26:40 +01003395 U2H(stats[ST_F_RATE_MAX].u.u32),
Willy Tarreauc73810f2016-01-11 13:52:04 +01003396 U2H(stats[ST_F_CONN_RATE_MAX].u.u32),
Willy Tarreaud72c9172016-01-06 15:26:40 +01003397 U2H(stats[ST_F_RATE_MAX].u.u32));
Willy Tarreau466c9b52012-12-23 02:25:03 +01003398
Willy Tarreauf8211df2016-01-11 14:09:38 +01003399 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
Willy Tarreau60600742016-01-11 15:32:30 +01003400 chunk_appendf(out,
Willy Tarreau466c9b52012-12-23 02:25:03 +01003401 "<tr><th>Max request rate:</th><td>%s/s</td></tr>",
Willy Tarreaud72c9172016-01-06 15:26:40 +01003402 U2H(stats[ST_F_REQ_RATE_MAX].u.u32));
Willy Tarreau466c9b52012-12-23 02:25:03 +01003403
Willy Tarreau60600742016-01-11 15:32:30 +01003404 chunk_appendf(out,
Willy Tarreau466c9b52012-12-23 02:25:03 +01003405 "</table></div></u></td>"
3406 /* sessions rate : limit */
3407 "<td>%s</td>",
Willy Tarreaud72c9172016-01-06 15:26:40 +01003408 LIM2A(stats[ST_F_RATE_LIM].u.u32, "-"));
Cyril Bonté70be45d2010-10-12 00:14:35 +02003409
Willy Tarreau60600742016-01-11 15:32:30 +01003410 chunk_appendf(out,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003411 /* sessions: current, max, limit, total */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003412 "<td>%s</td><td>%s</td><td>%s</td>"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02003413 "<td><u>%s<div class=tips><table class=det>"
Willy Tarreau466c9b52012-12-23 02:25:03 +01003414 "<tr><th>Cum. connections:</th><td>%s</td></tr>"
3415 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003416 "",
Willy Tarreauc73810f2016-01-11 13:52:04 +01003417 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 +01003418 U2H(stats[ST_F_STOT].u.u64),
Willy Tarreauc73810f2016-01-11 13:52:04 +01003419 U2H(stats[ST_F_CONN_TOT].u.u64),
Willy Tarreaud72c9172016-01-06 15:26:40 +01003420 U2H(stats[ST_F_STOT].u.u64));
Cyril Bonté70be45d2010-10-12 00:14:35 +02003421
Willy Tarreau466c9b52012-12-23 02:25:03 +01003422 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
Willy Tarreauf8211df2016-01-11 14:09:38 +01003423 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
Willy Tarreau60600742016-01-11 15:32:30 +01003424 chunk_appendf(out,
Willy Tarreau466c9b52012-12-23 02:25:03 +01003425 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
3426 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
3427 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
3428 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
3429 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
3430 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
3431 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
3432 "<tr><th>- other responses:</th><td>%s</td></tr>"
3433 "<tr><th>Intercepted requests:</th><td>%s</td></tr>"
3434 "",
Willy Tarreaud72c9172016-01-06 15:26:40 +01003435 U2H(stats[ST_F_REQ_TOT].u.u64),
3436 U2H(stats[ST_F_HRSP_1XX].u.u64),
3437 U2H(stats[ST_F_HRSP_2XX].u.u64),
3438 U2H(stats[ST_F_COMP_RSP].u.u64),
3439 stats[ST_F_HRSP_2XX].u.u64 ?
3440 (int)(100 * stats[ST_F_COMP_RSP].u.u64 / stats[ST_F_HRSP_2XX].u.u64) : 0,
3441 U2H(stats[ST_F_HRSP_3XX].u.u64),
3442 U2H(stats[ST_F_HRSP_4XX].u.u64),
3443 U2H(stats[ST_F_HRSP_5XX].u.u64),
3444 U2H(stats[ST_F_HRSP_OTHER].u.u64),
Willy Tarreau5b9bdff2016-01-11 14:40:47 +01003445 U2H(stats[ST_F_INTERCEPTED].u.u64));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003446 }
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02003447
Willy Tarreau60600742016-01-11 15:32:30 +01003448 chunk_appendf(out,
Willy Tarreau466c9b52012-12-23 02:25:03 +01003449 "</table></div></u></td>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05003450 /* sessions: lbtot, lastsess */
3451 "<td></td><td></td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003452 /* bytes : in */
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003453 "<td>%s</td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003454 "",
Willy Tarreaud72c9172016-01-06 15:26:40 +01003455 U2H(stats[ST_F_BIN].u.u64));
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02003456
Willy Tarreau60600742016-01-11 15:32:30 +01003457 chunk_appendf(out,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01003458 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
Willy Tarreau4e5d58e2015-07-04 14:35:15 +02003459 "<td>%s%s<div class=tips><table class=det>"
3460 "<tr><th>Response bytes in:</th><td>%s</td></tr>"
3461 "<tr><th>Compression in:</th><td>%s</td></tr>"
3462 "<tr><th>Compression out:</th><td>%s</td><td>(%d%%)</td></tr>"
3463 "<tr><th>Compression bypass:</th><td>%s</td></tr>"
3464 "<tr><th>Total bytes saved:</th><td>%s</td><td>(%d%%)</td></tr>"
3465 "</table></div>%s</td>",
Willy Tarreaud72c9172016-01-06 15:26:40 +01003466 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "<u>":"",
3467 U2H(stats[ST_F_BOUT].u.u64),
3468 U2H(stats[ST_F_BOUT].u.u64),
3469 U2H(stats[ST_F_COMP_IN].u.u64),
3470 U2H(stats[ST_F_COMP_OUT].u.u64),
3471 stats[ST_F_COMP_IN].u.u64 ? (int)(stats[ST_F_COMP_OUT].u.u64 * 100 / stats[ST_F_COMP_IN].u.u64) : 0,
3472 U2H(stats[ST_F_COMP_BYP].u.u64),
3473 U2H(stats[ST_F_COMP_IN].u.u64 - stats[ST_F_COMP_OUT].u.u64),
3474 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,
3475 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "</u>":"");
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02003476
Willy Tarreau60600742016-01-11 15:32:30 +01003477 chunk_appendf(out,
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01003478 /* denied: req, resp */
3479 "<td>%s</td><td>%s</td>"
3480 /* errors : request, connect, response */
3481 "<td>%s</td><td></td><td></td>"
3482 /* warnings: retries, redispatches */
3483 "<td></td><td></td>"
3484 /* server status : reflect frontend status */
3485 "<td class=ac>%s</td>"
3486 /* rest of server: nothing */
3487 "<td class=ac colspan=8></td></tr>"
3488 "",
Willy Tarreaud72c9172016-01-06 15:26:40 +01003489 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
3490 U2H(stats[ST_F_EREQ].u.u64),
3491 field_str(stats, ST_F_STATUS));
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02003492 }
Willy Tarreaufa9512f2016-01-08 17:20:51 +01003493 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_SO) {
Willy Tarreau60600742016-01-11 15:32:30 +01003494 chunk_appendf(out, "<tr class=socket>");
Willy Tarreau508a63f2016-01-11 15:28:40 +01003495 if (flags & ST_SHOWADMIN) {
Willy Tarreaufa9512f2016-01-08 17:20:51 +01003496 /* Column sub-heading for Enable or Disable server */
Willy Tarreau60600742016-01-11 15:32:30 +01003497 chunk_appendf(out, "<td></td>");
Willy Tarreaufa9512f2016-01-08 17:20:51 +01003498 }
3499
Willy Tarreau60600742016-01-11 15:32:30 +01003500 chunk_appendf(out,
Willy Tarreaufa9512f2016-01-08 17:20:51 +01003501 /* frontend name, listener name */
3502 "<td class=ac><a name=\"%s/+%s\"></a>%s"
3503 "<a class=lfsb href=\"#%s/+%s\">%s</a>"
3504 "",
3505 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME),
3506 (flags & ST_SHLGNDS)?"<u>":"",
3507 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME), field_str(stats, ST_F_SVNAME));
3508
3509 if (flags & ST_SHLGNDS) {
Willy Tarreau60600742016-01-11 15:32:30 +01003510 chunk_appendf(out, "<div class=tips>");
Willy Tarreaufa9512f2016-01-08 17:20:51 +01003511
3512 if (isdigit(*field_str(stats, ST_F_ADDR)))
Willy Tarreau60600742016-01-11 15:32:30 +01003513 chunk_appendf(out, "IPv4: %s, ", field_str(stats, ST_F_ADDR));
Willy Tarreaufa9512f2016-01-08 17:20:51 +01003514 else if (*field_str(stats, ST_F_ADDR) == '[')
Willy Tarreau60600742016-01-11 15:32:30 +01003515 chunk_appendf(out, "IPv6: %s, ", field_str(stats, ST_F_ADDR));
Willy Tarreaufa9512f2016-01-08 17:20:51 +01003516 else if (*field_str(stats, ST_F_ADDR))
Willy Tarreau60600742016-01-11 15:32:30 +01003517 chunk_appendf(out, "%s, ", field_str(stats, ST_F_ADDR));
Willy Tarreaufa9512f2016-01-08 17:20:51 +01003518
3519 /* id */
Willy Tarreau60600742016-01-11 15:32:30 +01003520 chunk_appendf(out, "id: %d</div>", stats[ST_F_SID].u.u32);
Willy Tarreaufa9512f2016-01-08 17:20:51 +01003521 }
3522
Willy Tarreau60600742016-01-11 15:32:30 +01003523 chunk_appendf(out,
Willy Tarreaufa9512f2016-01-08 17:20:51 +01003524 /* queue */
3525 "%s</td><td colspan=3></td>"
3526 /* sessions rate: current, max, limit */
3527 "<td colspan=3>&nbsp;</td>"
3528 /* sessions: current, max, limit, total, lbtot, lastsess */
3529 "<td>%s</td><td>%s</td><td>%s</td>"
3530 "<td>%s</td><td>&nbsp;</td><td>&nbsp;</td>"
3531 /* bytes: in, out */
3532 "<td>%s</td><td>%s</td>"
3533 "",
3534 (flags & ST_SHLGNDS)?"</u>":"",
3535 U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SMAX].u.u32), U2H(stats[ST_F_SLIM].u.u32),
3536 U2H(stats[ST_F_STOT].u.u64), U2H(stats[ST_F_BIN].u.u64), U2H(stats[ST_F_BOUT].u.u64));
3537
Willy Tarreau60600742016-01-11 15:32:30 +01003538 chunk_appendf(out,
Willy Tarreaufa9512f2016-01-08 17:20:51 +01003539 /* denied: req, resp */
3540 "<td>%s</td><td>%s</td>"
3541 /* errors: request, connect, response */
3542 "<td>%s</td><td></td><td></td>"
3543 /* warnings: retries, redispatches */
3544 "<td></td><td></td>"
3545 /* server status: reflect listener status */
3546 "<td class=ac>%s</td>"
3547 /* rest of server: nothing */
3548 "<td class=ac colspan=8></td></tr>"
3549 "",
3550 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
3551 U2H(stats[ST_F_EREQ].u.u64),
3552 field_str(stats, ST_F_STATUS));
3553 }
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003554 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_SV) {
3555 const char *style;
Willy Tarreaub5f66b82016-01-08 17:15:39 +01003556
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003557 /* determine the style to use depending on the server's state,
3558 * its health and weight. There isn't a 1-to-1 mapping between
3559 * state and styles for the cases where the server is (still)
3560 * up. The reason is that we don't want to report nolb and
3561 * drain with the same color.
3562 */
Willy Tarreaub5f66b82016-01-08 17:15:39 +01003563
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003564 if (strcmp(field_str(stats, ST_F_STATUS), "DOWN") == 0 ||
3565 strcmp(field_str(stats, ST_F_STATUS), "DOWN (agent)") == 0) {
3566 style = "down";
3567 }
3568 else if (strcmp(field_str(stats, ST_F_STATUS), "DOWN ") == 0) {
3569 style = "going_up";
3570 }
3571 else if (strcmp(field_str(stats, ST_F_STATUS), "NOLB ") == 0) {
3572 style = "going_down";
3573 }
3574 else if (strcmp(field_str(stats, ST_F_STATUS), "NOLB") == 0) {
3575 style = "nolb";
3576 }
3577 else if (strcmp(field_str(stats, ST_F_STATUS), "no check") == 0) {
3578 style = "no_check";
3579 }
3580 else if (!stats[ST_F_CHKFAIL].type ||
3581 stats[ST_F_CHECK_HEALTH].u.u32 == stats[ST_F_CHECK_RISE].u.u32 + stats[ST_F_CHECK_FALL].u.u32 - 1) {
3582 /* no check or max health = UP */
3583 if (stats[ST_F_WEIGHT].u.u32)
3584 style = "up";
3585 else
3586 style = "draining";
3587 }
3588 else {
3589 style = "going_down";
3590 }
Willy Tarreaub5f66b82016-01-08 17:15:39 +01003591
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003592 if (memcmp(field_str(stats, ST_F_STATUS), "MAINT", 5) == 0)
Willy Tarreau60600742016-01-11 15:32:30 +01003593 chunk_appendf(out, "<tr class=\"maintain\">");
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003594 else
Willy Tarreau60600742016-01-11 15:32:30 +01003595 chunk_appendf(out,
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003596 "<tr class=\"%s_%s\">",
3597 (stats[ST_F_BCK].u.u32) ? "backup" : "active", style);
Willy Tarreaub5f66b82016-01-08 17:15:39 +01003598
Willy Tarreaub5f66b82016-01-08 17:15:39 +01003599
Willy Tarreau508a63f2016-01-11 15:28:40 +01003600 if (flags & ST_SHOWADMIN)
Willy Tarreau60600742016-01-11 15:32:30 +01003601 chunk_appendf(out,
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003602 "<td><input type=\"checkbox\" name=\"s\" value=\"%s\"></td>",
3603 field_str(stats, ST_F_SVNAME));
Willy Tarreaub5f66b82016-01-08 17:15:39 +01003604
Willy Tarreau60600742016-01-11 15:32:30 +01003605 chunk_appendf(out,
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003606 "<td class=ac><a name=\"%s/%s\"></a>%s"
3607 "<a class=lfsb href=\"#%s/%s\">%s</a>"
3608 "",
3609 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME),
3610 (flags & ST_SHLGNDS) ? "<u>" : "",
3611 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 +01003612
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003613 if (flags & ST_SHLGNDS) {
Willy Tarreau60600742016-01-11 15:32:30 +01003614 chunk_appendf(out, "<div class=tips>");
Willy Tarreaub5f66b82016-01-08 17:15:39 +01003615
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003616 if (isdigit(*field_str(stats, ST_F_ADDR)))
Willy Tarreau60600742016-01-11 15:32:30 +01003617 chunk_appendf(out, "IPv4: %s, ", field_str(stats, ST_F_ADDR));
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003618 else if (*field_str(stats, ST_F_ADDR) == '[')
Willy Tarreau60600742016-01-11 15:32:30 +01003619 chunk_appendf(out, "IPv6: %s, ", field_str(stats, ST_F_ADDR));
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003620 else if (*field_str(stats, ST_F_ADDR))
Willy Tarreau60600742016-01-11 15:32:30 +01003621 chunk_appendf(out, "%s, ", field_str(stats, ST_F_ADDR));
Willy Tarreaub5f66b82016-01-08 17:15:39 +01003622
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003623 /* id */
Willy Tarreau60600742016-01-11 15:32:30 +01003624 chunk_appendf(out, "id: %d", stats[ST_F_SID].u.u32);
Willy Tarreaub5f66b82016-01-08 17:15:39 +01003625
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003626 /* cookie */
3627 if (stats[ST_F_COOKIE].type) {
Willy Tarreau60600742016-01-11 15:32:30 +01003628 chunk_appendf(out, ", cookie: '");
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003629 chunk_initstr(&src, field_str(stats, ST_F_COOKIE));
Willy Tarreau60600742016-01-11 15:32:30 +01003630 chunk_htmlencode(out, &src);
3631 chunk_appendf(out, "'");
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003632 }
Willy Tarreaub0c9bc42009-10-04 15:56:38 +02003633
Willy Tarreau60600742016-01-11 15:32:30 +01003634 chunk_appendf(out, "</div>");
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003635 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003636
Willy Tarreau60600742016-01-11 15:32:30 +01003637 chunk_appendf(out,
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003638 /* queue : current, max, limit */
3639 "%s</td><td>%s</td><td>%s</td><td>%s</td>"
3640 /* sessions rate : current, max, limit */
3641 "<td>%s</td><td>%s</td><td></td>"
3642 "",
3643 (flags & ST_SHLGNDS) ? "</u>" : "",
3644 U2H(stats[ST_F_QCUR].u.u32), U2H(stats[ST_F_QMAX].u.u32), LIM2A(stats[ST_F_QLIMIT].u.u32, "-"),
3645 U2H(stats[ST_F_RATE].u.u32), U2H(stats[ST_F_RATE_MAX].u.u32));
Willy Tarreau4607fad2016-01-06 15:41:29 +01003646
Willy Tarreau60600742016-01-11 15:32:30 +01003647 chunk_appendf(out,
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003648 /* sessions: current, max, limit, total */
3649 "<td>%s</td><td>%s</td><td>%s</td>"
3650 "<td><u>%s<div class=tips><table class=det>"
3651 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
3652 "",
3653 U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SMAX].u.u32), LIM2A(stats[ST_F_SLIM].u.u32, "-"),
3654 U2H(stats[ST_F_STOT].u.u64),
3655 U2H(stats[ST_F_STOT].u.u64));
Willy Tarreau4607fad2016-01-06 15:41:29 +01003656
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003657 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
Willy Tarreauf8211df2016-01-11 14:09:38 +01003658 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003659 unsigned long long tot;
Willy Tarreaua6f5a732016-01-08 16:59:56 +01003660
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003661 tot = stats[ST_F_HRSP_OTHER].u.u64;
3662 tot += stats[ST_F_HRSP_1XX].u.u64;
3663 tot += stats[ST_F_HRSP_2XX].u.u64;
3664 tot += stats[ST_F_HRSP_3XX].u.u64;
3665 tot += stats[ST_F_HRSP_4XX].u.u64;
3666 tot += stats[ST_F_HRSP_5XX].u.u64;
3667
Willy Tarreau60600742016-01-11 15:32:30 +01003668 chunk_appendf(out,
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003669 "<tr><th>Cum. HTTP responses:</th><td>%s</td></tr>"
3670 "<tr><th>- HTTP 1xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
3671 "<tr><th>- HTTP 2xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
3672 "<tr><th>- HTTP 3xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
3673 "<tr><th>- HTTP 4xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
3674 "<tr><th>- HTTP 5xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
3675 "<tr><th>- other responses:</th><td>%s</td><td>(%d%%)</td></tr>"
3676 "",
3677 U2H(tot),
3678 U2H(stats[ST_F_HRSP_1XX].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_1XX].u.u64 / tot) : 0,
3679 U2H(stats[ST_F_HRSP_2XX].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_2XX].u.u64 / tot) : 0,
3680 U2H(stats[ST_F_HRSP_3XX].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_3XX].u.u64 / tot) : 0,
3681 U2H(stats[ST_F_HRSP_4XX].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_4XX].u.u64 / tot) : 0,
3682 U2H(stats[ST_F_HRSP_5XX].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_5XX].u.u64 / tot) : 0,
3683 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 +01003684 }
Willy Tarreaua6f5a732016-01-08 16:59:56 +01003685
Willy Tarreau60600742016-01-11 15:32:30 +01003686 chunk_appendf(out, "<tr><th colspan=3>Avg over last 1024 success. conn.</th></tr>");
3687 chunk_appendf(out, "<tr><th>- Queue time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_QTIME].u.u32));
3688 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 +01003689 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
Willy Tarreau60600742016-01-11 15:32:30 +01003690 chunk_appendf(out, "<tr><th>- Response time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_RTIME].u.u32));
3691 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 +02003692
Willy Tarreau60600742016-01-11 15:32:30 +01003693 chunk_appendf(out,
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003694 "</table></div></u></td>"
3695 /* sessions: lbtot, last */
3696 "<td>%s</td><td>%s</td>",
3697 U2H(stats[ST_F_LBTOT].u.u64),
3698 human_time(stats[ST_F_LASTSESS].u.s32, 1));
Willy Tarreau91861262007-10-17 17:06:05 +02003699
Willy Tarreau60600742016-01-11 15:32:30 +01003700 chunk_appendf(out,
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003701 /* bytes : in, out */
3702 "<td>%s</td><td>%s</td>"
3703 /* denied: req, resp */
3704 "<td></td><td>%s</td>"
3705 /* errors : request, connect */
3706 "<td></td><td>%s</td>"
3707 /* errors : response */
3708 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
3709 /* warnings: retries, redispatches */
3710 "<td>%lld</td><td>%lld</td>"
3711 "",
3712 U2H(stats[ST_F_BIN].u.u64), U2H(stats[ST_F_BOUT].u.u64),
3713 U2H(stats[ST_F_DRESP].u.u64),
3714 U2H(stats[ST_F_ECON].u.u64),
3715 U2H(stats[ST_F_ERESP].u.u64),
3716 (long long)stats[ST_F_CLI_ABRT].u.u64,
3717 (long long)stats[ST_F_SRV_ABRT].u.u64,
3718 (long long)stats[ST_F_WRETR].u.u64,
3719 (long long)stats[ST_F_WREDIS].u.u64);
3720
3721 /* status, last change */
Willy Tarreau60600742016-01-11 15:32:30 +01003722 chunk_appendf(out, "<td class=ac>");
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003723
3724 /* FIXME!!!!
3725 * LASTCHG should contain the last change for *this* server and must be computed
3726 * properly above, as was done below, ie: this server if maint, otherwise ref server
3727 * if tracking. Note that ref is either local or remote depending on tracking.
3728 */
3729
3730
3731 if (memcmp(field_str(stats, ST_F_STATUS), "MAINT", 5) == 0) {
Willy Tarreau60600742016-01-11 15:32:30 +01003732 chunk_appendf(out, "%s MAINT", human_time(stats[ST_F_LASTCHG].u.u32, 1));
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003733 }
3734 else if (memcmp(field_str(stats, ST_F_STATUS), "no check", 5) == 0) {
Willy Tarreau60600742016-01-11 15:32:30 +01003735 chunk_strcat(out, "<i>no check</i>");
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003736 }
3737 else {
Willy Tarreau60600742016-01-11 15:32:30 +01003738 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 +01003739 if (memcmp(field_str(stats, ST_F_STATUS), "DOWN", 4) == 0) {
3740 if (stats[ST_F_CHECK_HEALTH].u.u32)
Willy Tarreau60600742016-01-11 15:32:30 +01003741 chunk_strcat(out, " &uarr;");
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003742 }
3743 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 +01003744 chunk_strcat(out, " &darr;");
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003745 }
3746
3747 if (memcmp(field_str(stats, ST_F_STATUS), "DOWN", 4) == 0 &&
3748 stats[ST_F_AGENT_STATUS].type && !stats[ST_F_AGENT_HEALTH].u.u32) {
Willy Tarreau60600742016-01-11 15:32:30 +01003749 chunk_appendf(out,
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003750 "</td><td class=ac><u> %s",
3751 field_str(stats, ST_F_AGENT_STATUS));
3752
3753 if (stats[ST_F_AGENT_CODE].type)
Willy Tarreau60600742016-01-11 15:32:30 +01003754 chunk_appendf(out, "/%d", stats[ST_F_AGENT_CODE].u.u32);
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003755
David Carlier08d35de2016-06-27 14:12:59 +01003756 if (stats[ST_F_AGENT_DURATION].type)
Willy Tarreau60600742016-01-11 15:32:30 +01003757 chunk_appendf(out, " in %lums", (long)stats[ST_F_AGENT_DURATION].u.u64);
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003758
Willy Tarreau60600742016-01-11 15:32:30 +01003759 chunk_appendf(out, "<div class=tips>%s", field_str(stats, ST_F_AGENT_DESC));
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003760
3761 if (*field_str(stats, ST_F_LAST_AGT)) {
Willy Tarreau60600742016-01-11 15:32:30 +01003762 chunk_appendf(out, ": ");
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003763 chunk_initstr(&src, field_str(stats, ST_F_LAST_AGT));
Willy Tarreau60600742016-01-11 15:32:30 +01003764 chunk_htmlencode(out, &src);
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003765 }
Willy Tarreau60600742016-01-11 15:32:30 +01003766 chunk_appendf(out, "</div></u>");
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003767 }
3768 else if (stats[ST_F_CHECK_STATUS].type) {
Willy Tarreau60600742016-01-11 15:32:30 +01003769 chunk_appendf(out,
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003770 "</td><td class=ac><u> %s",
3771 field_str(stats, ST_F_CHECK_STATUS));
3772
3773 if (stats[ST_F_CHECK_CODE].type)
Willy Tarreau60600742016-01-11 15:32:30 +01003774 chunk_appendf(out, "/%d", stats[ST_F_CHECK_CODE].u.u32);
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003775
David Carlier08d35de2016-06-27 14:12:59 +01003776 if (stats[ST_F_CHECK_DURATION].type)
Willy Tarreau60600742016-01-11 15:32:30 +01003777 chunk_appendf(out, " in %lums", (long)stats[ST_F_CHECK_DURATION].u.u64);
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003778
Willy Tarreau60600742016-01-11 15:32:30 +01003779 chunk_appendf(out, "<div class=tips>%s", field_str(stats, ST_F_CHECK_DESC));
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003780
3781 if (*field_str(stats, ST_F_LAST_CHK)) {
Willy Tarreau60600742016-01-11 15:32:30 +01003782 chunk_appendf(out, ": ");
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003783 chunk_initstr(&src, field_str(stats, ST_F_LAST_CHK));
Willy Tarreau60600742016-01-11 15:32:30 +01003784 chunk_htmlencode(out, &src);
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003785 }
Willy Tarreau60600742016-01-11 15:32:30 +01003786 chunk_appendf(out, "</div></u>");
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003787 }
3788 else
Willy Tarreau60600742016-01-11 15:32:30 +01003789 chunk_appendf(out, "</td><td>");
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003790
Willy Tarreau60600742016-01-11 15:32:30 +01003791 chunk_appendf(out,
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003792 /* weight */
3793 "</td><td class=ac>%d</td>"
3794 /* act, bck */
3795 "<td class=ac>%s</td><td class=ac>%s</td>"
3796 "",
3797 stats[ST_F_WEIGHT].u.u32,
3798 stats[ST_F_BCK].u.u32 ? "-" : "Y",
3799 stats[ST_F_BCK].u.u32 ? "Y" : "-");
3800
3801 /* check failures: unique, fatal, down time */
3802 if (stats[ST_F_CHKFAIL].type) {
Willy Tarreau60600742016-01-11 15:32:30 +01003803 chunk_appendf(out, "<td><u>%lld", (long long)stats[ST_F_CHKFAIL].u.u64);
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003804
3805 if (stats[ST_F_HANAFAIL].type)
Willy Tarreau60600742016-01-11 15:32:30 +01003806 chunk_appendf(out, "/%lld", (long long)stats[ST_F_HANAFAIL].u.u64);
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003807
Willy Tarreau60600742016-01-11 15:32:30 +01003808 chunk_appendf(out,
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003809 "<div class=tips>Failed Health Checks%s</div></u></td>"
3810 "<td>%lld</td><td>%s</td>"
3811 "",
3812 stats[ST_F_HANAFAIL].type ? "/Health Analyses" : "",
3813 (long long)stats[ST_F_CHKDOWN].u.u64, human_time(stats[ST_F_DOWNTIME].u.u32, 1));
3814 }
3815 else if (strcmp(field_str(stats, ST_F_STATUS), "MAINT") != 0 && field_format(stats, ST_F_TRACKED) == FF_STR) {
3816 /* tracking a server (hence inherited maint would appear as "MAINT (via...)" */
Willy Tarreau60600742016-01-11 15:32:30 +01003817 chunk_appendf(out,
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003818 "<td class=ac colspan=3><a class=lfsb href=\"#%s\">via %s</a></td>",
3819 field_str(stats, ST_F_TRACKED), field_str(stats, ST_F_TRACKED));
3820 }
3821 else
Willy Tarreau60600742016-01-11 15:32:30 +01003822 chunk_appendf(out, "<td colspan=3></td>");
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003823
3824 /* throttle */
3825 if (stats[ST_F_THROTTLE].type)
Willy Tarreau60600742016-01-11 15:32:30 +01003826 chunk_appendf(out, "<td class=ac>%d %%</td></tr>\n", stats[ST_F_THROTTLE].u.u32);
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003827 else
Willy Tarreau60600742016-01-11 15:32:30 +01003828 chunk_appendf(out, "<td class=ac>-</td></tr>\n");
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003829 }
Willy Tarreaubbf84502016-01-08 17:25:50 +01003830 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_BE) {
Willy Tarreau60600742016-01-11 15:32:30 +01003831 chunk_appendf(out, "<tr class=\"backend\">");
Willy Tarreau508a63f2016-01-11 15:28:40 +01003832 if (flags & ST_SHOWADMIN) {
Willy Tarreaubbf84502016-01-08 17:25:50 +01003833 /* Column sub-heading for Enable or Disable server */
Willy Tarreau60600742016-01-11 15:32:30 +01003834 chunk_appendf(out, "<td></td>");
Willy Tarreaubbf84502016-01-08 17:25:50 +01003835 }
Willy Tarreau60600742016-01-11 15:32:30 +01003836 chunk_appendf(out,
Willy Tarreaubbf84502016-01-08 17:25:50 +01003837 "<td class=ac>"
3838 /* name */
3839 "%s<a name=\"%s/Backend\"></a>"
3840 "<a class=lfsb href=\"#%s/Backend\">Backend</a>"
3841 "",
3842 (flags & ST_SHLGNDS)?"<u>":"",
3843 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_PXNAME));
3844
3845 if (flags & ST_SHLGNDS) {
3846 /* balancing */
Willy Tarreau60600742016-01-11 15:32:30 +01003847 chunk_appendf(out, "<div class=tips>balancing: %s",
Willy Tarreauf1516d92016-01-11 14:48:36 +01003848 field_str(stats, ST_F_ALGO));
Willy Tarreaubbf84502016-01-08 17:25:50 +01003849
3850 /* cookie */
3851 if (stats[ST_F_COOKIE].type) {
Willy Tarreau60600742016-01-11 15:32:30 +01003852 chunk_appendf(out, ", cookie: '");
Willy Tarreaubbf84502016-01-08 17:25:50 +01003853 chunk_initstr(&src, field_str(stats, ST_F_COOKIE));
Willy Tarreau60600742016-01-11 15:32:30 +01003854 chunk_htmlencode(out, &src);
3855 chunk_appendf(out, "'");
Willy Tarreaubbf84502016-01-08 17:25:50 +01003856 }
Willy Tarreau60600742016-01-11 15:32:30 +01003857 chunk_appendf(out, "</div>");
Willy Tarreaubbf84502016-01-08 17:25:50 +01003858 }
3859
Willy Tarreau60600742016-01-11 15:32:30 +01003860 chunk_appendf(out,
Willy Tarreaubbf84502016-01-08 17:25:50 +01003861 "%s</td>"
3862 /* queue : current, max */
3863 "<td>%s</td><td>%s</td><td></td>"
3864 /* sessions rate : current, max, limit */
3865 "<td>%s</td><td>%s</td><td></td>"
3866 "",
3867 (flags & ST_SHLGNDS)?"</u>":"",
3868 U2H(stats[ST_F_QCUR].u.u32), U2H(stats[ST_F_QMAX].u.u32),
3869 U2H(stats[ST_F_RATE].u.u32), U2H(stats[ST_F_RATE_MAX].u.u32));
3870
Willy Tarreau60600742016-01-11 15:32:30 +01003871 chunk_appendf(out,
Willy Tarreaubbf84502016-01-08 17:25:50 +01003872 /* sessions: current, max, limit, total */
3873 "<td>%s</td><td>%s</td><td>%s</td>"
3874 "<td><u>%s<div class=tips><table class=det>"
3875 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
3876 "",
3877 U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SLIM].u.u32),
3878 U2H(stats[ST_F_STOT].u.u64),
3879 U2H(stats[ST_F_STOT].u.u64));
3880
3881 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
Willy Tarreauf8211df2016-01-11 14:09:38 +01003882 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
Willy Tarreau60600742016-01-11 15:32:30 +01003883 chunk_appendf(out,
Willy Tarreaubbf84502016-01-08 17:25:50 +01003884 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
3885 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
3886 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
3887 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
3888 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
3889 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
3890 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
3891 "<tr><th>- other responses:</th><td>%s</td></tr>"
3892 "<tr><th>Intercepted requests:</th><td>%s</td></tr>"
3893 "<tr><th colspan=3>Avg over last 1024 success. conn.</th></tr>"
3894 "",
3895 U2H(stats[ST_F_REQ_TOT].u.u64),
3896 U2H(stats[ST_F_HRSP_1XX].u.u64),
3897 U2H(stats[ST_F_HRSP_2XX].u.u64),
3898 U2H(stats[ST_F_COMP_RSP].u.u64),
3899 stats[ST_F_HRSP_2XX].u.u64 ?
3900 (int)(100 * stats[ST_F_COMP_RSP].u.u64 / stats[ST_F_HRSP_2XX].u.u64) : 0,
3901 U2H(stats[ST_F_HRSP_3XX].u.u64),
3902 U2H(stats[ST_F_HRSP_4XX].u.u64),
3903 U2H(stats[ST_F_HRSP_5XX].u.u64),
3904 U2H(stats[ST_F_HRSP_OTHER].u.u64),
Willy Tarreau5b9bdff2016-01-11 14:40:47 +01003905 U2H(stats[ST_F_INTERCEPTED].u.u64));
Willy Tarreaubbf84502016-01-08 17:25:50 +01003906 }
3907
Willy Tarreau60600742016-01-11 15:32:30 +01003908 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 +02003909 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 +01003910 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
Willy Tarreau60600742016-01-11 15:32:30 +01003911 chunk_appendf(out, "<tr><th>- Response time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_RTIME].u.u32));
3912 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 +01003913
Willy Tarreau60600742016-01-11 15:32:30 +01003914 chunk_appendf(out,
Willy Tarreaubbf84502016-01-08 17:25:50 +01003915 "</table></div></u></td>"
3916 /* sessions: lbtot, last */
3917 "<td>%s</td><td>%s</td>"
3918 /* bytes: in */
3919 "<td>%s</td>"
3920 "",
3921 U2H(stats[ST_F_LBTOT].u.u64),
3922 human_time(stats[ST_F_LASTSESS].u.s32, 1),
3923 U2H(stats[ST_F_BIN].u.u64));
3924
Willy Tarreau60600742016-01-11 15:32:30 +01003925 chunk_appendf(out,
Willy Tarreaubbf84502016-01-08 17:25:50 +01003926 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
3927 "<td>%s%s<div class=tips><table class=det>"
3928 "<tr><th>Response bytes in:</th><td>%s</td></tr>"
3929 "<tr><th>Compression in:</th><td>%s</td></tr>"
3930 "<tr><th>Compression out:</th><td>%s</td><td>(%d%%)</td></tr>"
3931 "<tr><th>Compression bypass:</th><td>%s</td></tr>"
3932 "<tr><th>Total bytes saved:</th><td>%s</td><td>(%d%%)</td></tr>"
3933 "</table></div>%s</td>",
3934 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "<u>":"",
3935 U2H(stats[ST_F_BOUT].u.u64),
3936 U2H(stats[ST_F_BOUT].u.u64),
3937 U2H(stats[ST_F_COMP_IN].u.u64),
3938 U2H(stats[ST_F_COMP_OUT].u.u64),
3939 stats[ST_F_COMP_IN].u.u64 ? (int)(stats[ST_F_COMP_OUT].u.u64 * 100 / stats[ST_F_COMP_IN].u.u64) : 0,
3940 U2H(stats[ST_F_COMP_BYP].u.u64),
Christopher Faulet0b64f622016-04-28 15:09:31 +02003941 U2H(stats[ST_F_COMP_IN].u.u64 - stats[ST_F_COMP_OUT].u.u64),
Willy Tarreaubbf84502016-01-08 17:25:50 +01003942 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,
3943 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "</u>":"");
3944
Willy Tarreau60600742016-01-11 15:32:30 +01003945 chunk_appendf(out,
Willy Tarreaubbf84502016-01-08 17:25:50 +01003946 /* denied: req, resp */
3947 "<td>%s</td><td>%s</td>"
3948 /* errors : request, connect */
3949 "<td></td><td>%s</td>"
3950 /* errors : response */
3951 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
3952 /* warnings: retries, redispatches */
3953 "<td>%lld</td><td>%lld</td>"
3954 /* backend status: reflect backend status (up/down): we display UP
3955 * if the backend has known working servers or if it has no server at
3956 * all (eg: for stats). Then we display the total weight, number of
3957 * active and backups. */
3958 "<td class=ac>%s %s</td><td class=ac>&nbsp;</td><td class=ac>%d</td>"
3959 "<td class=ac>%d</td><td class=ac>%d</td>"
3960 "",
3961 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
3962 U2H(stats[ST_F_ECON].u.u64),
3963 U2H(stats[ST_F_ERESP].u.u64),
3964 (long long)stats[ST_F_CLI_ABRT].u.u64,
3965 (long long)stats[ST_F_SRV_ABRT].u.u64,
3966 (long long)stats[ST_F_WRETR].u.u64, (long long)stats[ST_F_WREDIS].u.u64,
3967 human_time(stats[ST_F_LASTCHG].u.u32, 1),
3968 strcmp(field_str(stats, ST_F_STATUS), "DOWN") ? field_str(stats, ST_F_STATUS) : "<font color=\"red\"><b>DOWN</b></font>",
3969 stats[ST_F_WEIGHT].u.u32,
3970 stats[ST_F_ACT].u.u32, stats[ST_F_BCK].u.u32);
3971
Willy Tarreau60600742016-01-11 15:32:30 +01003972 chunk_appendf(out,
Willy Tarreaubbf84502016-01-08 17:25:50 +01003973 /* rest of backend: nothing, down transitions, total downtime, throttle */
3974 "<td class=ac>&nbsp;</td><td>%d</td>"
3975 "<td>%s</td>"
3976 "<td></td>"
3977 "</tr>",
3978 stats[ST_F_CHKDOWN].u.u32,
3979 stats[ST_F_DOWNTIME].type ? human_time(stats[ST_F_DOWNTIME].u.u32, 1) : "&nbsp;");
3980 }
Willy Tarreau362eaeb2016-01-08 17:23:28 +01003981 return 1;
3982}
3983
Willy Tarreau501f6022016-01-08 17:37:22 +01003984static int stats_dump_one_line(const struct field *stats, unsigned int flags, struct proxy *px, struct appctx *appctx)
3985{
Willy Tarreau508a63f2016-01-11 15:28:40 +01003986 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN))
3987 flags |= ST_SHOWADMIN;
Willy Tarreau501f6022016-01-08 17:37:22 +01003988
3989 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
Willy Tarreau60600742016-01-11 15:32:30 +01003990 return stats_dump_fields_html(&trash, stats, flags);
Willy Tarreau1e62df92016-01-11 18:57:53 +01003991 else if (appctx->ctx.stats.flags & STAT_FMT_TYPED)
3992 return stats_dump_fields_typed(&trash, stats);
Willy Tarreau501f6022016-01-08 17:37:22 +01003993 else
3994 return stats_dump_fields_csv(&trash, stats);
3995}
3996
Thierry Fournier23d2d642016-03-25 08:20:11 +01003997/* Fill <stats> with the frontend statistics. <stats> is
3998 * preallocated array of length <len>. The length of the array
3999 * must be at least ST_F_TOTAL_FIELDS. If this length is less then
4000 * this value, the function returns 0, otherwise, it returns 1.
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004001 */
Thierry Fournier23d2d642016-03-25 08:20:11 +01004002int stats_fill_fe_stats(struct proxy *px, struct field *stats, int len)
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004003{
Thierry Fournier23d2d642016-03-25 08:20:11 +01004004 if (len < ST_F_TOTAL_FIELDS)
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004005 return 0;
4006
Thierry Fournier23d2d642016-03-25 08:20:11 +01004007 memset(stats, 0, sizeof(*stats) * len);
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004008
4009 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
4010 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, "FRONTEND");
Willy Tarreauf8211df2016-01-11 14:09:38 +01004011 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004012 stats[ST_F_SCUR] = mkf_u32(0, px->feconn);
4013 stats[ST_F_SMAX] = mkf_u32(FN_MAX, px->fe_counters.conn_max);
4014 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, px->maxconn);
4015 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, px->fe_counters.cum_sess);
4016 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, px->fe_counters.bytes_in);
4017 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, px->fe_counters.bytes_out);
4018 stats[ST_F_DREQ] = mkf_u64(FN_COUNTER, px->fe_counters.denied_req);
4019 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, px->fe_counters.denied_resp);
4020 stats[ST_F_EREQ] = mkf_u64(FN_COUNTER, px->fe_counters.failed_req);
Willy Tarreau8a90b8e2016-10-21 18:15:32 +02004021 stats[ST_F_DCON] = mkf_u64(FN_COUNTER, px->fe_counters.denied_conn);
Willy Tarreaua5bc36b2016-10-21 18:16:27 +02004022 stats[ST_F_DSES] = mkf_u64(FN_COUNTER, px->fe_counters.denied_sess);
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004023 stats[ST_F_STATUS] = mkf_str(FO_STATUS, px->state == PR_STREADY ? "OPEN" : px->state == PR_STFULL ? "FULL" : "STOP");
4024 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
4025 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
4026 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, 0);
4027 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_FE);
4028 stats[ST_F_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_sess_per_sec));
4029 stats[ST_F_RATE_LIM] = mkf_u32(FO_CONFIG|FN_LIMIT, px->fe_sps_lim);
4030 stats[ST_F_RATE_MAX] = mkf_u32(FN_MAX, px->fe_counters.sps_max);
4031
4032 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
4033 if (px->mode == PR_MODE_HTTP) {
4034 stats[ST_F_HRSP_1XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[1]);
4035 stats[ST_F_HRSP_2XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[2]);
4036 stats[ST_F_HRSP_3XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[3]);
4037 stats[ST_F_HRSP_4XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[4]);
4038 stats[ST_F_HRSP_5XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[5]);
4039 stats[ST_F_HRSP_OTHER] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[0]);
Willy Tarreau5b9bdff2016-01-11 14:40:47 +01004040 stats[ST_F_INTERCEPTED] = mkf_u64(FN_COUNTER, px->fe_counters.intercepted_req);
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004041 }
4042
4043 /* requests : req_rate, req_rate_max, req_tot, */
4044 stats[ST_F_REQ_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_req_per_sec));
4045 stats[ST_F_REQ_RATE_MAX] = mkf_u32(FN_MAX, px->fe_counters.p.http.rps_max);
4046 stats[ST_F_REQ_TOT] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cum_req);
4047
4048 /* compression: in, out, bypassed, responses */
4049 stats[ST_F_COMP_IN] = mkf_u64(FN_COUNTER, px->fe_counters.comp_in);
4050 stats[ST_F_COMP_OUT] = mkf_u64(FN_COUNTER, px->fe_counters.comp_out);
4051 stats[ST_F_COMP_BYP] = mkf_u64(FN_COUNTER, px->fe_counters.comp_byp);
4052 stats[ST_F_COMP_RSP] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.comp_rsp);
4053
Willy Tarreauc73810f2016-01-11 13:52:04 +01004054 /* connections : conn_rate, conn_rate_max, conn_tot, conn_max */
4055 stats[ST_F_CONN_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_conn_per_sec));
4056 stats[ST_F_CONN_RATE_MAX] = mkf_u32(FN_MAX, px->fe_counters.cps_max);
4057 stats[ST_F_CONN_TOT] = mkf_u64(FN_COUNTER, px->fe_counters.cum_conn);
4058
Thierry Fournier23d2d642016-03-25 08:20:11 +01004059 return 1;
4060}
4061
4062/* Dumps a frontend's line to the trash for the current proxy <px> and uses
4063 * the state from stream interface <si>. The caller is responsible for clearing
4064 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
4065 */
4066static int stats_dump_fe_stats(struct stream_interface *si, struct proxy *px)
4067{
4068 struct appctx *appctx = __objt_appctx(si->end);
4069
4070 if (!(px->cap & PR_CAP_FE))
4071 return 0;
4072
4073 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_FE)))
4074 return 0;
4075
4076 if (!stats_fill_fe_stats(px, stats, ST_F_TOTAL_FIELDS))
4077 return 0;
4078
Willy Tarreau501f6022016-01-08 17:37:22 +01004079 return stats_dump_one_line(stats, 0, px, appctx);
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004080}
4081
Thierry Fournierc4456852016-03-25 08:20:49 +01004082/* Fill <stats> with the listener statistics. <stats> is
4083 * preallocated array of length <len>. The length of the array
4084 * must be at least ST_F_TOTAL_FIELDS. If this length is less
4085 * then this value, the function returns 0, otherwise, it
4086 * returns 1. <flags> can take the value ST_SHLGNDS.
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004087 */
Thierry Fournierc4456852016-03-25 08:20:49 +01004088int stats_fill_li_stats(struct proxy *px, struct listener *l, int flags,
4089 struct field *stats, int len)
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004090{
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004091 struct chunk *out = get_trash_chunk();
4092
Thierry Fournierc4456852016-03-25 08:20:49 +01004093 if (len < ST_F_TOTAL_FIELDS)
4094 return 0;
4095
Thierry Fournierac9d4672016-03-25 08:43:46 +01004096 if (!l->counters)
4097 return 0;
4098
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004099 chunk_reset(out);
Thierry Fournierc4456852016-03-25 08:20:49 +01004100 memset(stats, 0, sizeof(*stats) * len);
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004101
4102 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
4103 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, l->name);
Willy Tarreauf8211df2016-01-11 14:09:38 +01004104 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004105 stats[ST_F_SCUR] = mkf_u32(0, l->nbconn);
4106 stats[ST_F_SMAX] = mkf_u32(FN_MAX, l->counters->conn_max);
4107 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, l->maxconn);
4108 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, l->counters->cum_conn);
4109 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, l->counters->bytes_in);
4110 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, l->counters->bytes_out);
4111 stats[ST_F_DREQ] = mkf_u64(FN_COUNTER, l->counters->denied_req);
4112 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, l->counters->denied_resp);
4113 stats[ST_F_EREQ] = mkf_u64(FN_COUNTER, l->counters->failed_req);
Willy Tarreau8a90b8e2016-10-21 18:15:32 +02004114 stats[ST_F_DCON] = mkf_u64(FN_COUNTER, l->counters->denied_conn);
Willy Tarreaua5bc36b2016-10-21 18:16:27 +02004115 stats[ST_F_DSES] = mkf_u64(FN_COUNTER, l->counters->denied_sess);
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004116 stats[ST_F_STATUS] = mkf_str(FO_STATUS, (l->nbconn < l->maxconn) ? (l->state == LI_LIMITED) ? "WAITING" : "OPEN" : "FULL");
4117 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
4118 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
4119 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, l->luid);
4120 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_SO);
4121
4122 if (flags & ST_SHLGNDS) {
4123 char str[INET6_ADDRSTRLEN];
4124 int port;
4125
4126 port = get_host_port(&l->addr);
4127 switch (addr_to_str(&l->addr, str, sizeof(str))) {
4128 case AF_INET:
4129 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
4130 chunk_appendf(out, "%s:%d", str, port);
4131 break;
4132 case AF_INET6:
4133 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
4134 chunk_appendf(out, "[%s]:%d", str, port);
4135 break;
4136 case AF_UNIX:
4137 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, "unix");
4138 break;
4139 case -1:
4140 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
4141 chunk_strcat(out, strerror(errno));
4142 break;
4143 default: /* address family not supported */
4144 break;
4145 }
4146 }
4147
Thierry Fournierc4456852016-03-25 08:20:49 +01004148 return 1;
4149}
4150
4151/* Dumps a line for listener <l> and proxy <px> to the trash and uses the state
4152 * from stream interface <si>, and stats flags <flags>. The caller is responsible
4153 * for clearing the trash if needed. Returns non-zero if it emits anything, zero
4154 * otherwise.
4155 */
4156static int stats_dump_li_stats(struct stream_interface *si, struct proxy *px, struct listener *l, int flags)
4157{
4158 struct appctx *appctx = __objt_appctx(si->end);
4159
4160 if (!stats_fill_li_stats(px, l, flags, stats, ST_F_TOTAL_FIELDS))
4161 return 0;
4162
Willy Tarreau501f6022016-01-08 17:37:22 +01004163 return stats_dump_one_line(stats, flags, px, appctx);
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004164}
4165
4166enum srv_stats_state {
4167 SRV_STATS_STATE_DOWN = 0,
4168 SRV_STATS_STATE_DOWN_AGENT,
4169 SRV_STATS_STATE_GOING_UP,
4170 SRV_STATS_STATE_UP_GOING_DOWN,
4171 SRV_STATS_STATE_UP,
4172 SRV_STATS_STATE_NOLB_GOING_DOWN,
Simon Horman4d2eab62015-04-23 14:51:26 +09004173 SRV_STATS_STATE_NOLB,
4174 SRV_STATS_STATE_DRAIN_GOING_DOWN,
4175 SRV_STATS_STATE_DRAIN,
Simon Hormanb167b6b2015-04-23 14:51:29 +09004176 SRV_STATS_STATE_DRAIN_AGENT,
Simon Horman4d2eab62015-04-23 14:51:26 +09004177 SRV_STATS_STATE_NO_CHECK,
4178
4179 SRV_STATS_STATE_COUNT, /* Must be last */
4180};
4181
Willy Tarreau2b96cf12016-01-06 19:25:38 +01004182static const char *srv_hlt_st[SRV_STATS_STATE_COUNT] = {
4183 [SRV_STATS_STATE_DOWN] = "DOWN",
4184 [SRV_STATS_STATE_DOWN_AGENT] = "DOWN (agent)",
4185 [SRV_STATS_STATE_GOING_UP] = "DOWN %d/%d",
4186 [SRV_STATS_STATE_UP_GOING_DOWN] = "UP %d/%d",
4187 [SRV_STATS_STATE_UP] = "UP",
4188 [SRV_STATS_STATE_NOLB_GOING_DOWN] = "NOLB %d/%d",
4189 [SRV_STATS_STATE_NOLB] = "NOLB",
4190 [SRV_STATS_STATE_DRAIN_GOING_DOWN] = "DRAIN %d/%d",
4191 [SRV_STATS_STATE_DRAIN] = "DRAIN",
4192 [SRV_STATS_STATE_DRAIN_AGENT] = "DRAIN (agent)",
4193 [SRV_STATS_STATE_NO_CHECK] = "no check"
4194};
4195
Thierry Fournier61fe6c02016-03-25 08:21:21 +01004196/* Fill <stats> with the server statistics. <stats> is
4197 * preallocated array of length <len>. The length of the array
4198 * must be at least ST_F_TOTAL_FIELDS. If this length is less
4199 * then this value, the function returns 0, otherwise, it
4200 * returns 1. <flags> can take the value ST_SHLGNDS.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004201 */
Thierry Fournier61fe6c02016-03-25 08:21:21 +01004202int stats_fill_sv_stats(struct proxy *px, struct server *sv, int flags,
4203 struct field *stats, int len)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004204{
Willy Tarreau32091232014-05-16 13:52:00 +02004205 struct server *via, *ref;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004206 char str[INET6_ADDRSTRLEN];
Willy Tarreau2b96cf12016-01-06 19:25:38 +01004207 struct chunk *out = get_trash_chunk();
Willy Tarreauba2f2642016-01-07 09:54:40 +01004208 enum srv_stats_state state;
Willy Tarreau2b96cf12016-01-06 19:25:38 +01004209 char *fld_status;
Thierry Fournier61fe6c02016-03-25 08:21:21 +01004210
4211 if (len < ST_F_TOTAL_FIELDS)
4212 return 0;
4213
4214 memset(stats, 0, sizeof(*stats) * len);
4215
Willy Tarreau32091232014-05-16 13:52:00 +02004216 /* we have "via" which is the tracked server as described in the configuration,
4217 * and "ref" which is the checked server and the end of the chain.
4218 */
4219 via = sv->track ? sv->track : sv;
4220 ref = via;
4221 while (ref->track)
4222 ref = ref->track;
4223
Willy Tarreauba2f2642016-01-07 09:54:40 +01004224 if (sv->state == SRV_ST_RUNNING || sv->state == SRV_ST_STARTING) {
4225 if ((ref->check.state & CHK_ST_ENABLED) &&
4226 (ref->check.health < ref->check.rise + ref->check.fall - 1)) {
4227 state = SRV_STATS_STATE_UP_GOING_DOWN;
Willy Tarreauba2f2642016-01-07 09:54:40 +01004228 } else {
4229 state = SRV_STATS_STATE_UP;
Willy Tarreauba2f2642016-01-07 09:54:40 +01004230 }
4231
Willy Tarreauba2f2642016-01-07 09:54:40 +01004232 if (sv->admin & SRV_ADMF_DRAIN) {
4233 if (ref->agent.state & CHK_ST_ENABLED)
4234 state = SRV_STATS_STATE_DRAIN_AGENT;
4235 else if (state == SRV_STATS_STATE_UP_GOING_DOWN)
4236 state = SRV_STATS_STATE_DRAIN_GOING_DOWN;
4237 else
4238 state = SRV_STATS_STATE_DRAIN;
4239 }
4240
4241 if (state == SRV_STATS_STATE_UP && !(ref->check.state & CHK_ST_ENABLED)) {
4242 state = SRV_STATS_STATE_NO_CHECK;
Willy Tarreauba2f2642016-01-07 09:54:40 +01004243 }
4244 }
4245 else if (sv->state == SRV_ST_STOPPING) {
4246 if ((!(sv->check.state & CHK_ST_ENABLED) && !sv->track) ||
4247 (ref->check.health == ref->check.rise + ref->check.fall - 1)) {
4248 state = SRV_STATS_STATE_NOLB;
Willy Tarreauba2f2642016-01-07 09:54:40 +01004249 } else {
4250 state = SRV_STATS_STATE_NOLB_GOING_DOWN;
Willy Tarreauba2f2642016-01-07 09:54:40 +01004251 }
4252 }
4253 else { /* stopped */
4254 if ((ref->agent.state & CHK_ST_ENABLED) && !ref->agent.health) {
4255 state = SRV_STATS_STATE_DOWN_AGENT;
Willy Tarreauba2f2642016-01-07 09:54:40 +01004256 } else if ((ref->check.state & CHK_ST_ENABLED) && !ref->check.health) {
4257 state = SRV_STATS_STATE_DOWN; /* DOWN */
Willy Tarreauba2f2642016-01-07 09:54:40 +01004258 } else if ((ref->agent.state & CHK_ST_ENABLED) || (ref->check.state & CHK_ST_ENABLED)) {
4259 state = SRV_STATS_STATE_GOING_UP;
Willy Tarreauba2f2642016-01-07 09:54:40 +01004260 } else {
4261 state = SRV_STATS_STATE_DOWN; /* DOWN, unchecked */
Willy Tarreauba2f2642016-01-07 09:54:40 +01004262 }
4263 }
4264
Willy Tarreau2b96cf12016-01-06 19:25:38 +01004265 chunk_reset(out);
Willy Tarreau2b96cf12016-01-06 19:25:38 +01004266
4267 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
4268 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, sv->id);
Willy Tarreauf8211df2016-01-11 14:09:38 +01004269 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
Willy Tarreau2b96cf12016-01-06 19:25:38 +01004270 stats[ST_F_QCUR] = mkf_u32(0, sv->nbpend);
4271 stats[ST_F_QMAX] = mkf_u32(FN_MAX, sv->counters.nbpend_max);
4272 stats[ST_F_SCUR] = mkf_u32(0, sv->cur_sess);
4273 stats[ST_F_SMAX] = mkf_u32(FN_MAX, sv->counters.cur_sess_max);
4274
4275 if (sv->maxconn)
4276 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, sv->maxconn);
4277
4278 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, sv->counters.cum_sess);
4279 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, sv->counters.bytes_in);
4280 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, sv->counters.bytes_out);
4281 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, sv->counters.failed_secu);
4282 stats[ST_F_ECON] = mkf_u64(FN_COUNTER, sv->counters.failed_conns);
4283 stats[ST_F_ERESP] = mkf_u64(FN_COUNTER, sv->counters.failed_resp);
4284 stats[ST_F_WRETR] = mkf_u64(FN_COUNTER, sv->counters.retries);
4285 stats[ST_F_WREDIS] = mkf_u64(FN_COUNTER, sv->counters.redispatches);
4286
4287 /* status */
4288 fld_status = chunk_newstr(out);
4289 if (sv->admin & SRV_ADMF_IMAINT)
4290 chunk_appendf(out, "MAINT (via %s/%s)", via->proxy->id, via->id);
4291 else if (sv->admin & SRV_ADMF_MAINT)
4292 chunk_appendf(out, "MAINT");
4293 else
4294 chunk_appendf(out,
4295 srv_hlt_st[state],
4296 (ref->state != SRV_ST_STOPPED) ? (ref->check.health - ref->check.rise + 1) : (ref->check.health),
4297 (ref->state != SRV_ST_STOPPED) ? (ref->check.fall) : (ref->check.rise));
4298
4299 stats[ST_F_STATUS] = mkf_str(FO_STATUS, fld_status);
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004300 stats[ST_F_LASTCHG] = mkf_u32(FN_AGE, now.tv_sec - sv->last_change);
4301 stats[ST_F_WEIGHT] = mkf_u32(FN_AVG, (sv->eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv);
4302 stats[ST_F_ACT] = mkf_u32(FO_STATUS, (sv->flags & SRV_F_BACKUP) ? 0 : 1);
4303 stats[ST_F_BCK] = mkf_u32(FO_STATUS, (sv->flags & SRV_F_BACKUP) ? 1 : 0);
Willy Tarreau91861262007-10-17 17:06:05 +02004304
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004305 /* check failures: unique, fatal; last change, total downtime */
4306 if (sv->check.state & CHK_ST_ENABLED) {
4307 stats[ST_F_CHKFAIL] = mkf_u64(FN_COUNTER, sv->counters.failed_checks);
4308 stats[ST_F_CHKDOWN] = mkf_u64(FN_COUNTER, sv->counters.down_trans);
4309 stats[ST_F_DOWNTIME] = mkf_u32(FN_COUNTER, srv_downtime(sv));
4310 }
Willy Tarreau164d4a92016-01-06 19:48:21 +01004311
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004312 if (sv->maxqueue)
4313 stats[ST_F_QLIMIT] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->maxqueue);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004314
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004315 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
4316 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
4317 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, sv->puid);
Willy Tarreau91861262007-10-17 17:06:05 +02004318
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004319 if (sv->state == SRV_ST_STARTING && !server_is_draining(sv))
4320 stats[ST_F_THROTTLE] = mkf_u32(FN_AVG, server_throttle_rate(sv));
Willy Tarreauf5b1cc32014-06-17 12:20:59 +02004321
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004322 stats[ST_F_LBTOT] = mkf_u64(FN_COUNTER, sv->counters.cum_lbconn);
Willy Tarreau91861262007-10-17 17:06:05 +02004323
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004324 if (sv->track) {
4325 char *fld_track = chunk_newstr(out);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004326
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004327 chunk_appendf(out, "%s/%s", sv->track->proxy->id, sv->track->id);
4328 stats[ST_F_TRACKED] = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, fld_track);
4329 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004330
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004331 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_SV);
4332 stats[ST_F_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&sv->sess_per_sec));
4333 stats[ST_F_RATE_MAX] = mkf_u32(FN_MAX, sv->counters.sps_max);
Willy Tarreau164d4a92016-01-06 19:48:21 +01004334
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004335 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) {
4336 const char *fld_chksts;
Willy Tarreau164d4a92016-01-06 19:48:21 +01004337
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004338 fld_chksts = chunk_newstr(out);
4339 chunk_strcat(out, "* "); // for check in progress
4340 chunk_strcat(out, get_check_status_info(sv->check.status));
4341 if (!(sv->check.state & CHK_ST_INPROGRESS))
4342 fld_chksts += 2; // skip "* "
4343 stats[ST_F_CHECK_STATUS] = mkf_str(FN_OUTPUT, fld_chksts);
Willy Tarreau91861262007-10-17 17:06:05 +02004344
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004345 if (sv->check.status >= HCHK_STATUS_L57DATA)
4346 stats[ST_F_CHECK_CODE] = mkf_u32(FN_OUTPUT, sv->check.code);
Willy Tarreaucf2924b2014-05-23 12:15:15 +02004347
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004348 if (sv->check.status >= HCHK_STATUS_CHECKED)
4349 stats[ST_F_CHECK_DURATION] = mkf_u64(FN_DURATION, sv->check.duration);
Willy Tarreaucf2924b2014-05-23 12:15:15 +02004350
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004351 stats[ST_F_CHECK_DESC] = mkf_str(FN_OUTPUT, get_check_status_description(sv->check.status));
4352 stats[ST_F_LAST_CHK] = mkf_str(FN_OUTPUT, sv->check.desc);
4353 stats[ST_F_CHECK_RISE] = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.rise);
4354 stats[ST_F_CHECK_FALL] = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.fall);
4355 stats[ST_F_CHECK_HEALTH] = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.health);
4356 }
Willy Tarreaucf2924b2014-05-23 12:15:15 +02004357
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004358 if ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) {
4359 const char *fld_chksts;
Willy Tarreaudd7354b2016-01-08 13:47:26 +01004360
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004361 fld_chksts = chunk_newstr(out);
4362 chunk_strcat(out, "* "); // for check in progress
4363 chunk_strcat(out, get_check_status_info(sv->agent.status));
4364 if (!(sv->agent.state & CHK_ST_INPROGRESS))
4365 fld_chksts += 2; // skip "* "
4366 stats[ST_F_AGENT_STATUS] = mkf_str(FN_OUTPUT, fld_chksts);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004367
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004368 if (sv->agent.status >= HCHK_STATUS_L57DATA)
4369 stats[ST_F_AGENT_CODE] = mkf_u32(FN_OUTPUT, sv->agent.code);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004370
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004371 if (sv->agent.status >= HCHK_STATUS_CHECKED)
4372 stats[ST_F_AGENT_DURATION] = mkf_u64(FN_DURATION, sv->agent.duration);
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01004373
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004374 stats[ST_F_AGENT_DESC] = mkf_str(FN_OUTPUT, get_check_status_description(sv->agent.status));
4375 stats[ST_F_LAST_AGT] = mkf_str(FN_OUTPUT, sv->agent.desc);
4376 stats[ST_F_AGENT_RISE] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.rise);
4377 stats[ST_F_AGENT_FALL] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.fall);
4378 stats[ST_F_AGENT_HEALTH] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.health);
4379 }
Willy Tarreaudd7354b2016-01-08 13:47:26 +01004380
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004381 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
4382 if (px->mode == PR_MODE_HTTP) {
4383 stats[ST_F_HRSP_1XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[1]);
4384 stats[ST_F_HRSP_2XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[2]);
4385 stats[ST_F_HRSP_3XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[3]);
4386 stats[ST_F_HRSP_4XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[4]);
4387 stats[ST_F_HRSP_5XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[5]);
4388 stats[ST_F_HRSP_OTHER] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[0]);
4389 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004390
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004391 if (ref->observe)
4392 stats[ST_F_HANAFAIL] = mkf_u64(FN_COUNTER, sv->counters.failed_hana);
Willy Tarreau164d4a92016-01-06 19:48:21 +01004393
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004394 stats[ST_F_CLI_ABRT] = mkf_u64(FN_COUNTER, sv->counters.cli_aborts);
4395 stats[ST_F_SRV_ABRT] = mkf_u64(FN_COUNTER, sv->counters.srv_aborts);
4396 stats[ST_F_LASTSESS] = mkf_s32(FN_AGE, srv_lastsession(sv));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004397
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004398 stats[ST_F_QTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.q_time, TIME_STATS_SAMPLES));
4399 stats[ST_F_CTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.c_time, TIME_STATS_SAMPLES));
4400 stats[ST_F_RTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.d_time, TIME_STATS_SAMPLES));
4401 stats[ST_F_TTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.t_time, TIME_STATS_SAMPLES));
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004402
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004403 if (flags & ST_SHLGNDS) {
4404 switch (addr_to_str(&sv->addr, str, sizeof(str))) {
4405 case AF_INET:
4406 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
4407 chunk_appendf(out, "%s:%d", str, get_host_port(&sv->addr));
4408 break;
4409 case AF_INET6:
4410 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
4411 chunk_appendf(out, "[%s]:%d", str, get_host_port(&sv->addr));
4412 break;
4413 case AF_UNIX:
4414 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, "unix");
4415 break;
4416 case -1:
4417 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
4418 chunk_strcat(out, strerror(errno));
4419 break;
4420 default: /* address family not supported */
4421 break;
Willy Tarreauf4659942013-11-28 10:50:06 +01004422 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004423
Willy Tarreau362eaeb2016-01-08 17:23:28 +01004424 if (sv->cookie)
4425 stats[ST_F_COOKIE] = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, sv->cookie);
4426 }
4427
Thierry Fournier61fe6c02016-03-25 08:21:21 +01004428 return 1;
4429}
4430
4431/* Dumps a line for server <sv> and proxy <px> to the trash and uses the state
4432 * from stream interface <si>, stats flags <flags>, and server state <state>.
4433 * The caller is responsible for clearing the trash if needed. Returns non-zero
4434 * if it emits anything, zero otherwise.
4435 */
4436static int stats_dump_sv_stats(struct stream_interface *si, struct proxy *px, int flags, struct server *sv)
4437{
4438 struct appctx *appctx = __objt_appctx(si->end);
4439
4440 if (!stats_fill_sv_stats(px, sv, flags, stats, ST_F_TOTAL_FIELDS))
4441 return 0;
4442
Willy Tarreau501f6022016-01-08 17:37:22 +01004443 return stats_dump_one_line(stats, flags, px, appctx);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004444}
4445
Thierry Fournierd0a56c22016-03-25 08:21:51 +01004446/* Fill <stats> with the backend statistics. <stats> is
4447 * preallocated array of length <len>. The length of the array
4448 * must be at least ST_F_TOTAL_FIELDS. If this length is less
4449 * then this value, the function returns 0, otherwise, it
4450 * returns 1. <flags> can take the value ST_SHLGNDS.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004451 */
Thierry Fournierd0a56c22016-03-25 08:21:51 +01004452int stats_fill_be_stats(struct proxy *px, int flags, struct field *stats, int len)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004453{
Thierry Fournierd0a56c22016-03-25 08:21:51 +01004454 if (len < ST_F_TOTAL_FIELDS)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004455 return 0;
4456
Thierry Fournierd0a56c22016-03-25 08:21:51 +01004457 memset(stats, 0, sizeof(*stats) * len);
Willy Tarreauf6eecbe2016-01-06 16:14:50 +01004458
4459 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
4460 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, "BACKEND");
Willy Tarreauf8211df2016-01-11 14:09:38 +01004461 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
Willy Tarreauf6eecbe2016-01-06 16:14:50 +01004462 stats[ST_F_QCUR] = mkf_u32(0, px->nbpend);
4463 stats[ST_F_QMAX] = mkf_u32(FN_MAX, px->be_counters.nbpend_max);
4464 stats[ST_F_SCUR] = mkf_u32(FO_CONFIG|FN_LIMIT, px->beconn);
4465 stats[ST_F_SMAX] = mkf_u32(FN_MAX, px->be_counters.conn_max);
4466 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, px->fullconn);
4467 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, px->be_counters.cum_conn);
4468 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, px->be_counters.bytes_in);
4469 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, px->be_counters.bytes_out);
4470 stats[ST_F_DREQ] = mkf_u64(FN_COUNTER, px->be_counters.denied_req);
4471 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, px->be_counters.denied_resp);
4472 stats[ST_F_ECON] = mkf_u64(FN_COUNTER, px->be_counters.failed_conns);
4473 stats[ST_F_ERESP] = mkf_u64(FN_COUNTER, px->be_counters.failed_resp);
4474 stats[ST_F_WRETR] = mkf_u64(FN_COUNTER, px->be_counters.retries);
4475 stats[ST_F_WREDIS] = mkf_u64(FN_COUNTER, px->be_counters.redispatches);
4476 stats[ST_F_STATUS] = mkf_str(FO_STATUS, (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" : "DOWN");
4477 stats[ST_F_WEIGHT] = mkf_u32(FN_AVG, (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv);
4478 stats[ST_F_ACT] = mkf_u32(0, px->srv_act);
4479 stats[ST_F_BCK] = mkf_u32(0, px->srv_bck);
4480 stats[ST_F_CHKDOWN] = mkf_u64(FN_COUNTER, px->down_trans);
4481 stats[ST_F_LASTCHG] = mkf_u32(FN_AGE, now.tv_sec - px->last_change);
Willy Tarreau7344f472016-01-11 12:04:02 +01004482 if (px->srv)
4483 stats[ST_F_DOWNTIME] = mkf_u32(FN_COUNTER, be_downtime(px));
4484
Willy Tarreauf6eecbe2016-01-06 16:14:50 +01004485 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
4486 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
4487 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, 0);
4488 stats[ST_F_LBTOT] = mkf_u64(FN_COUNTER, px->be_counters.cum_lbconn);
4489 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_BE);
4490 stats[ST_F_RATE] = mkf_u32(0, read_freq_ctr(&px->be_sess_per_sec));
4491 stats[ST_F_RATE_MAX] = mkf_u32(0, px->be_counters.sps_max);
Willy Tarreauf1516d92016-01-11 14:48:36 +01004492
Willy Tarreaue4847c62016-01-08 15:43:54 +01004493 if (flags & ST_SHLGNDS) {
4494 if (px->cookie_name)
4495 stats[ST_F_COOKIE] = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, px->cookie_name);
Willy Tarreauf1516d92016-01-11 14:48:36 +01004496 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 +01004497 }
Willy Tarreauf6eecbe2016-01-06 16:14:50 +01004498
4499 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
4500 if (px->mode == PR_MODE_HTTP) {
4501 stats[ST_F_REQ_TOT] = mkf_u64(FN_COUNTER, px->be_counters.p.http.cum_req);
4502 stats[ST_F_HRSP_1XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[1]);
4503 stats[ST_F_HRSP_2XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[2]);
4504 stats[ST_F_HRSP_3XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[3]);
4505 stats[ST_F_HRSP_4XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[4]);
4506 stats[ST_F_HRSP_5XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[5]);
4507 stats[ST_F_HRSP_OTHER] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[0]);
Willy Tarreau5b9bdff2016-01-11 14:40:47 +01004508 stats[ST_F_INTERCEPTED] = mkf_u64(FN_COUNTER, px->be_counters.intercepted_req);
Willy Tarreauf6eecbe2016-01-06 16:14:50 +01004509 }
4510
4511 stats[ST_F_CLI_ABRT] = mkf_u64(FN_COUNTER, px->be_counters.cli_aborts);
4512 stats[ST_F_SRV_ABRT] = mkf_u64(FN_COUNTER, px->be_counters.srv_aborts);
4513
4514 /* compression: in, out, bypassed, responses */
4515 stats[ST_F_COMP_IN] = mkf_u64(FN_COUNTER, px->be_counters.comp_in);
4516 stats[ST_F_COMP_OUT] = mkf_u64(FN_COUNTER, px->be_counters.comp_out);
4517 stats[ST_F_COMP_BYP] = mkf_u64(FN_COUNTER, px->be_counters.comp_byp);
4518 stats[ST_F_COMP_RSP] = mkf_u64(FN_COUNTER, px->be_counters.p.http.comp_rsp);
4519 stats[ST_F_LASTSESS] = mkf_s32(FN_AGE, be_lastsession(px));
4520
4521 stats[ST_F_QTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.q_time, TIME_STATS_SAMPLES));
4522 stats[ST_F_CTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.c_time, TIME_STATS_SAMPLES));
4523 stats[ST_F_RTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.d_time, TIME_STATS_SAMPLES));
4524 stats[ST_F_TTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.t_time, TIME_STATS_SAMPLES));
4525
Thierry Fournierd0a56c22016-03-25 08:21:51 +01004526 return 1;
4527}
4528
4529/* Dumps a line for backend <px> to the trash for and uses the state from stream
4530 * interface <si> and stats flags <flags>. The caller is responsible for clearing
4531 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
4532 */
4533static int stats_dump_be_stats(struct stream_interface *si, struct proxy *px, int flags)
4534{
4535 struct appctx *appctx = __objt_appctx(si->end);
4536
4537 if (!(px->cap & PR_CAP_BE))
4538 return 0;
4539
4540 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_BE)))
4541 return 0;
4542
4543 if (!stats_fill_be_stats(px, flags, stats, ST_F_TOTAL_FIELDS))
4544 return 0;
4545
Willy Tarreau501f6022016-01-08 17:37:22 +01004546 return stats_dump_one_line(stats, flags, px, appctx);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004547}
4548
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004549/* Dumps the HTML table header for proxy <px> to the trash for and uses the state from
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004550 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004551 * for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004552 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004553static void stats_dump_html_px_hdr(struct stream_interface *si, struct proxy *px, struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004554{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004555 struct appctx *appctx = __objt_appctx(si->end);
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004556 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
4557
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004558 if (px->cap & PR_CAP_BE && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004559 /* A form to enable/disable this proxy servers */
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004560
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004561 /* 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 +02004562 scope_txt[0] = 0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004563 if (appctx->ctx.stats.scope_len) {
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004564 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau4e4292b2014-11-28 12:18:45 +01004565 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 +01004566 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004567 }
4568
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004569 chunk_appendf(&trash,
Jeff Buchbinder2dbbf4d2014-08-29 15:10:08 -05004570 "<form method=\"post\">");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004571 }
4572
4573 /* print a new table */
4574 chunk_appendf(&trash,
4575 "<table class=\"tbl\" width=\"100%%\">\n"
4576 "<tr class=\"titre\">"
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01004577 "<th class=\"pxname\" width=\"10%%\">");
4578
4579 chunk_appendf(&trash,
4580 "<a name=\"%s\"></a>%s"
4581 "<a class=px href=\"#%s\">%s</a>",
4582 px->id,
4583 (uri->flags & ST_SHLGNDS) ? "<u>":"",
4584 px->id, px->id);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004585
4586 if (uri->flags & ST_SHLGNDS) {
4587 /* cap, mode, id */
Willy Tarreau656a9ce2013-04-19 14:41:29 +02004588 chunk_appendf(&trash, "<div class=tips>cap: %s, mode: %s, id: %d",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004589 proxy_cap_str(px->cap), proxy_mode_str(px->mode),
4590 px->uuid);
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01004591 chunk_appendf(&trash, "</div>");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004592 }
4593
4594 chunk_appendf(&trash,
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01004595 "%s</th>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004596 "<th class=\"%s\" width=\"90%%\">%s</th>"
4597 "</tr>\n"
4598 "</table>\n"
4599 "<table class=\"tbl\" width=\"100%%\">\n"
4600 "<tr class=\"titre\">",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004601 (uri->flags & ST_SHLGNDS) ? "</u>":"",
4602 px->desc ? "desc" : "empty", px->desc ? px->desc : "");
4603
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004604 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004605 /* Column heading for Enable or Disable server */
4606 chunk_appendf(&trash, "<th rowspan=2 width=1></th>");
Willy Tarreau91861262007-10-17 17:06:05 +02004607 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004608
4609 chunk_appendf(&trash,
4610 "<th rowspan=2></th>"
4611 "<th colspan=3>Queue</th>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05004612 "<th colspan=3>Session rate</th><th colspan=6>Sessions</th>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004613 "<th colspan=2>Bytes</th><th colspan=2>Denied</th>"
4614 "<th colspan=3>Errors</th><th colspan=2>Warnings</th>"
4615 "<th colspan=9>Server</th>"
4616 "</tr>\n"
4617 "<tr class=\"titre\">"
4618 "<th>Cur</th><th>Max</th><th>Limit</th>"
4619 "<th>Cur</th><th>Max</th><th>Limit</th><th>Cur</th><th>Max</th>"
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05004620 "<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 +01004621 "<th>Req</th><th>Resp</th><th>Req</th><th>Conn</th>"
4622 "<th>Resp</th><th>Retr</th><th>Redis</th>"
4623 "<th>Status</th><th>LastChk</th><th>Wght</th><th>Act</th>"
4624 "<th>Bck</th><th>Chk</th><th>Dwn</th><th>Dwntme</th>"
4625 "<th>Thrtle</th>\n"
4626 "</tr>");
Willy Tarreau91861262007-10-17 17:06:05 +02004627}
4628
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004629/* Dumps the HTML table trailer for proxy <px> to the trash for and uses the state from
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004630 * stream interface <si>. The caller is responsible for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004631 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004632static void stats_dump_html_px_end(struct stream_interface *si, struct proxy *px)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004633{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004634 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004635 chunk_appendf(&trash, "</table>");
4636
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004637 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004638 /* close the form used to enable/disable this proxy servers */
4639 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004640 "Choose the action to perform on the checked servers : "
4641 "<select name=action>"
4642 "<option value=\"\"></option>"
Willy Tarreaued7df902014-05-22 18:04:49 +02004643 "<option value=\"ready\">Set state to READY</option>"
4644 "<option value=\"drain\">Set state to DRAIN</option>"
Marco Corte8c27bca2014-07-02 17:49:34 +02004645 "<option value=\"maint\">Set state to MAINT</option>"
Willy Tarreau248a60e2014-05-23 14:59:48 +02004646 "<option value=\"dhlth\">Health: disable checks</option>"
4647 "<option value=\"ehlth\">Health: enable checks</option>"
4648 "<option value=\"hrunn\">Health: force UP</option>"
4649 "<option value=\"hnolb\">Health: force NOLB</option>"
4650 "<option value=\"hdown\">Health: force DOWN</option>"
4651 "<option value=\"dagent\">Agent: disable checks</option>"
4652 "<option value=\"eagent\">Agent: enable checks</option>"
4653 "<option value=\"arunn\">Agent: force UP</option>"
4654 "<option value=\"adown\">Agent: force DOWN</option>"
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004655 "<option value=\"shutdown\">Kill Sessions</option>"
4656 "</select>"
4657 "<input type=\"hidden\" name=\"b\" value=\"#%d\">"
4658 "&nbsp;<input type=\"submit\" value=\"Apply\">"
4659 "</form>",
4660 px->uuid);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004661 }
4662
4663 chunk_appendf(&trash, "<p>\n");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004664}
Willy Tarreau91861262007-10-17 17:06:05 +02004665
4666/*
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004667 * Dumps statistics for a proxy. The output is sent to the stream interface's
4668 * input buffer. Returns 0 if it had to stop dumping data because of lack of
4669 * buffer space, or non-zero if everything completed. This function is used
4670 * both by the CLI and the HTTP entry points, and is able to dump the output
4671 * in HTML or CSV formats. If the later, <uri> must be NULL.
Willy Tarreau91861262007-10-17 17:06:05 +02004672 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004673static int stats_dump_proxy_to_buffer(struct stream_interface *si, struct proxy *px, struct uri_auth *uri)
Willy Tarreau91861262007-10-17 17:06:05 +02004674{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004675 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau87b09662015-04-03 00:22:06 +02004676 struct stream *s = si_strm(si);
Willy Tarreau2bb4a962014-11-28 11:11:05 +01004677 struct channel *rep = si_ic(si);
Willy Tarreau44267702011-10-28 15:35:33 +02004678 struct server *sv, *svs; /* server and server-state, server-state=server or server->track */
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004679 struct listener *l;
Willy Tarreau0deb85a2016-01-08 15:33:18 +01004680 unsigned int flags;
4681
4682 if (uri)
4683 flags = uri->flags;
4684 else if (strm_li(s)->bind_conf->level >= ACCESS_LVL_OPER)
4685 flags = ST_SHLGNDS | ST_SHNODE | ST_SHDESC;
4686 else
4687 flags = ST_SHNODE | ST_SHDESC;
Willy Tarreau91861262007-10-17 17:06:05 +02004688
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004689 chunk_reset(&trash);
Willy Tarreau91861262007-10-17 17:06:05 +02004690
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004691 switch (appctx->ctx.stats.px_st) {
Willy Tarreau295a8372011-03-10 11:25:07 +01004692 case STAT_PX_ST_INIT:
Willy Tarreau91861262007-10-17 17:06:05 +02004693 /* we are on a new proxy */
Willy Tarreau91861262007-10-17 17:06:05 +02004694 if (uri && uri->scope) {
4695 /* we have a limited scope, we have to check the proxy name */
4696 struct stat_scope *scope;
4697 int len;
4698
4699 len = strlen(px->id);
4700 scope = uri->scope;
4701
4702 while (scope) {
4703 /* match exact proxy name */
4704 if (scope->px_len == len && !memcmp(px->id, scope->px_id, len))
4705 break;
4706
4707 /* match '.' which means 'self' proxy */
Willy Tarreau1388a3a2007-10-18 16:38:37 +02004708 if (!strcmp(scope->px_id, ".") && px == s->be)
Willy Tarreau91861262007-10-17 17:06:05 +02004709 break;
4710 scope = scope->next;
4711 }
4712
4713 /* proxy name not found : don't dump anything */
4714 if (scope == NULL)
4715 return 1;
4716 }
4717
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004718 /* if the user has requested a limited output and the proxy
4719 * name does not match, skip it.
4720 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004721 if (appctx->ctx.stats.scope_len &&
Willy Tarreau4e4292b2014-11-28 12:18:45 +01004722 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 +02004723 return 1;
4724
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004725 if ((appctx->ctx.stats.flags & STAT_BOUND) &&
4726 (appctx->ctx.stats.iid != -1) &&
4727 (px->uuid != appctx->ctx.stats.iid))
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01004728 return 1;
4729
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004730 appctx->ctx.stats.px_st = STAT_PX_ST_TH;
Willy Tarreau91861262007-10-17 17:06:05 +02004731 /* fall through */
4732
Willy Tarreau295a8372011-03-10 11:25:07 +01004733 case STAT_PX_ST_TH:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004734 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004735 stats_dump_html_px_hdr(si, px, uri);
Willy Tarreaubc18da12015-03-13 14:00:47 +01004736 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004737 si_applet_cant_put(si);
Willy Tarreau55bb8452007-10-17 18:44:57 +02004738 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01004739 }
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004740 }
Willy Tarreau91861262007-10-17 17:06:05 +02004741
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004742 appctx->ctx.stats.px_st = STAT_PX_ST_FE;
Willy Tarreau91861262007-10-17 17:06:05 +02004743 /* fall through */
4744
Willy Tarreau295a8372011-03-10 11:25:07 +01004745 case STAT_PX_ST_FE:
Willy Tarreau91861262007-10-17 17:06:05 +02004746 /* print the frontend */
Willy Tarreaubc18da12015-03-13 14:00:47 +01004747 if (stats_dump_fe_stats(si, px)) {
4748 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004749 si_applet_cant_put(si);
Willy Tarreau91861262007-10-17 17:06:05 +02004750 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01004751 }
4752 }
Willy Tarreau91861262007-10-17 17:06:05 +02004753
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004754 appctx->ctx.stats.l = px->conf.listeners.n;
4755 appctx->ctx.stats.px_st = STAT_PX_ST_LI;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004756 /* fall through */
4757
Willy Tarreau295a8372011-03-10 11:25:07 +01004758 case STAT_PX_ST_LI:
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004759 /* stats.l has been initialized above */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004760 for (; appctx->ctx.stats.l != &px->conf.listeners; appctx->ctx.stats.l = l->by_fe.n) {
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01004761 if (buffer_almost_full(rep->buf)) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004762 si_applet_cant_put(si);
Willy Tarreau4e33d862009-10-11 23:35:10 +02004763 return 0;
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01004764 }
Willy Tarreau4e33d862009-10-11 23:35:10 +02004765
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004766 l = LIST_ELEM(appctx->ctx.stats.l, struct listener *, by_fe);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004767 if (!l->counters)
4768 continue;
4769
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004770 if (appctx->ctx.stats.flags & STAT_BOUND) {
4771 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SO)))
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004772 break;
4773
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004774 if (appctx->ctx.stats.sid != -1 && l->luid != appctx->ctx.stats.sid)
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004775 continue;
4776 }
4777
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004778 /* print the frontend */
Willy Tarreau0deb85a2016-01-08 15:33:18 +01004779 if (stats_dump_li_stats(si, px, l, flags)) {
Willy Tarreaubc18da12015-03-13 14:00:47 +01004780 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004781 si_applet_cant_put(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004782 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01004783 }
4784 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004785 }
4786
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004787 appctx->ctx.stats.sv = px->srv; /* may be NULL */
4788 appctx->ctx.stats.px_st = STAT_PX_ST_SV;
Willy Tarreau91861262007-10-17 17:06:05 +02004789 /* fall through */
4790
Willy Tarreau295a8372011-03-10 11:25:07 +01004791 case STAT_PX_ST_SV:
Willy Tarreau91861262007-10-17 17:06:05 +02004792 /* stats.sv has been initialized above */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004793 for (; appctx->ctx.stats.sv != NULL; appctx->ctx.stats.sv = sv->next) {
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01004794 if (buffer_almost_full(rep->buf)) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004795 si_applet_cant_put(si);
Willy Tarreau4e33d862009-10-11 23:35:10 +02004796 return 0;
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01004797 }
Willy Tarreau4e33d862009-10-11 23:35:10 +02004798
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004799 sv = appctx->ctx.stats.sv;
Willy Tarreau91861262007-10-17 17:06:05 +02004800
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004801 if (appctx->ctx.stats.flags & STAT_BOUND) {
4802 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SV)))
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01004803 break;
4804
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004805 if (appctx->ctx.stats.sid != -1 && sv->puid != appctx->ctx.stats.sid)
Krzysztof Piotr Oledzki2c6962c2008-03-02 02:42:14 +01004806 continue;
4807 }
4808
Willy Tarreau32091232014-05-16 13:52:00 +02004809 svs = sv;
4810 while (svs->track)
4811 svs = svs->track;
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01004812
Willy Tarreauba2f2642016-01-07 09:54:40 +01004813 /* do not report servers which are DOWN and not changing state */
4814 if ((appctx->ctx.stats.flags & STAT_HIDE_DOWN) &&
4815 ((sv->admin & SRV_ADMF_MAINT) || /* server is in maintenance */
4816 (sv->state == SRV_ST_STOPPED && /* server is down */
4817 (!((svs->agent.state | svs->check.state) & CHK_ST_ENABLED) ||
4818 ((svs->agent.state & CHK_ST_ENABLED) && !svs->agent.health) ||
4819 ((svs->check.state & CHK_ST_ENABLED) && !svs->check.health))))) {
Willy Tarreau91861262007-10-17 17:06:05 +02004820 continue;
4821 }
4822
Willy Tarreau0deb85a2016-01-08 15:33:18 +01004823 if (stats_dump_sv_stats(si, px, flags, sv)) {
Willy Tarreaubc18da12015-03-13 14:00:47 +01004824 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004825 si_applet_cant_put(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004826 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01004827 }
4828 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004829 } /* for sv */
Cyril Bonté474be412010-10-12 00:14:36 +02004830
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004831 appctx->ctx.stats.px_st = STAT_PX_ST_BE;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004832 /* fall through */
Cyril Bonté70be45d2010-10-12 00:14:35 +02004833
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004834 case STAT_PX_ST_BE:
4835 /* print the backend */
Willy Tarreau0deb85a2016-01-08 15:33:18 +01004836 if (stats_dump_be_stats(si, px, flags)) {
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 }
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01004842
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004843 appctx->ctx.stats.px_st = STAT_PX_ST_END;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004844 /* fall through */
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01004845
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004846 case STAT_PX_ST_END:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004847 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004848 stats_dump_html_px_end(si, px);
Willy Tarreaubc18da12015-03-13 14:00:47 +01004849 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02004850 si_applet_cant_put(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004851 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01004852 }
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004853 }
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01004854
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004855 appctx->ctx.stats.px_st = STAT_PX_ST_FIN;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004856 /* fall through */
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01004857
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004858 case STAT_PX_ST_FIN:
4859 return 1;
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01004860
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004861 default:
4862 /* unknown state, we should put an abort() here ! */
4863 return 1;
4864 }
4865}
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01004866
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004867/* Dumps the HTTP stats head block to the trash for and uses the per-uri
4868 * parameters <uri>. The caller is responsible for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004869 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004870static void stats_dump_html_head(struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004871{
4872 /* WARNING! This must fit in the first buffer !!! */
4873 chunk_appendf(&trash,
4874 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n"
4875 "\"http://www.w3.org/TR/html4/loose.dtd\">\n"
4876 "<html><head><title>Statistics Report for " PRODUCT_NAME "%s%s</title>\n"
4877 "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n"
4878 "<style type=\"text/css\"><!--\n"
4879 "body {"
4880 " font-family: arial, helvetica, sans-serif;"
4881 " font-size: 12px;"
4882 " font-weight: normal;"
4883 " color: black;"
4884 " background: white;"
4885 "}\n"
4886 "th,td {"
4887 " font-size: 10px;"
4888 "}\n"
4889 "h1 {"
4890 " font-size: x-large;"
4891 " margin-bottom: 0.5em;"
4892 "}\n"
4893 "h2 {"
4894 " font-family: helvetica, arial;"
4895 " font-size: x-large;"
4896 " font-weight: bold;"
4897 " font-style: italic;"
4898 " color: #6020a0;"
4899 " margin-top: 0em;"
4900 " margin-bottom: 0em;"
4901 "}\n"
4902 "h3 {"
4903 " font-family: helvetica, arial;"
4904 " font-size: 16px;"
4905 " font-weight: bold;"
4906 " color: #b00040;"
4907 " background: #e8e8d0;"
4908 " margin-top: 0em;"
4909 " margin-bottom: 0em;"
4910 "}\n"
4911 "li {"
4912 " margin-top: 0.25em;"
4913 " margin-right: 2em;"
4914 "}\n"
4915 ".hr {margin-top: 0.25em;"
4916 " border-color: black;"
4917 " border-bottom-style: solid;"
4918 "}\n"
4919 ".titre {background: #20D0D0;color: #000000; font-weight: bold; text-align: center;}\n"
4920 ".total {background: #20D0D0;color: #ffff80;}\n"
4921 ".frontend {background: #e8e8d0;}\n"
4922 ".socket {background: #d0d0d0;}\n"
4923 ".backend {background: #e8e8d0;}\n"
Simon Horman837bfa72015-04-23 14:51:27 +09004924 ".active_down {background: #ff9090;}\n"
4925 ".active_going_up {background: #ffd020;}\n"
4926 ".active_going_down {background: #ffffa0;}\n"
4927 ".active_up {background: #c0ffc0;}\n"
4928 ".active_nolb {background: #20a0ff;}\n"
4929 ".active_draining {background: #20a0FF;}\n"
4930 ".active_no_check {background: #e0e0e0;}\n"
4931 ".backup_down {background: #ff9090;}\n"
4932 ".backup_going_up {background: #ff80ff;}\n"
4933 ".backup_going_down {background: #c060ff;}\n"
4934 ".backup_up {background: #b0d0ff;}\n"
4935 ".backup_nolb {background: #90b0e0;}\n"
4936 ".backup_draining {background: #cc9900;}\n"
4937 ".backup_no_check {background: #e0e0e0;}\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004938 ".maintain {background: #c07820;}\n"
4939 ".rls {letter-spacing: 0.2em; margin-right: 1px;}\n" /* right letter spacing (used for grouping digits) */
4940 "\n"
4941 "a.px:link {color: #ffff40; text-decoration: none;}"
4942 "a.px:visited {color: #ffff40; text-decoration: none;}"
4943 "a.px:hover {color: #ffffff; text-decoration: none;}"
4944 "a.lfsb:link {color: #000000; text-decoration: none;}"
4945 "a.lfsb:visited {color: #000000; text-decoration: none;}"
4946 "a.lfsb:hover {color: #505050; text-decoration: none;}"
4947 "\n"
4948 "table.tbl { border-collapse: collapse; border-style: none;}\n"
4949 "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"
4950 "table.tbl td.ac { text-align: center;}\n"
4951 "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}\n"
4952 "table.tbl th.pxname { background: #b00040; color: #ffff40; font-weight: bold; border-style: solid solid none solid; padding: 2px 3px; white-space: nowrap;}\n"
4953 "table.tbl th.empty { border-style: none; empty-cells: hide; background: white;}\n"
4954 "table.tbl th.desc { background: white; border-style: solid solid none solid; text-align: left; padding: 2px 3px;}\n"
4955 "\n"
4956 "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n"
4957 "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n"
4958 "table.lgd td.noborder { border-style: none; padding: 2px; white-space: nowrap;}\n"
Willy Tarreau466c9b52012-12-23 02:25:03 +01004959 "table.det { border-collapse: collapse; border-style: none; }\n"
4960 "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 +01004961 "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 +01004962 "u {text-decoration:none; border-bottom: 1px dotted black;}\n"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02004963 "div.tips {\n"
Willy Tarreaue7dbfc62012-12-23 01:59:23 +01004964 " display:block;\n"
4965 " visibility:hidden;\n"
4966 " z-index:2147483647;\n"
4967 " position:absolute;\n"
4968 " padding:2px 4px 3px;\n"
4969 " background:#f0f060; color:#000000;\n"
4970 " border:1px solid #7040c0;\n"
4971 " white-space:nowrap;\n"
4972 " font-style:normal;font-size:11px;font-weight:normal;\n"
4973 " -moz-border-radius:3px;-webkit-border-radius:3px;border-radius:3px;\n"
4974 " -moz-box-shadow:gray 2px 2px 3px;-webkit-box-shadow:gray 2px 2px 3px;box-shadow:gray 2px 2px 3px;\n"
4975 "}\n"
Willy Tarreau656a9ce2013-04-19 14:41:29 +02004976 "u:hover div.tips {visibility:visible;}\n"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004977 "-->\n"
4978 "</style></head>\n",
4979 (uri->flags & ST_SHNODE) ? " on " : "",
4980 (uri->flags & ST_SHNODE) ? (uri->node ? uri->node : global.node) : ""
4981 );
4982}
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01004983
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004984/* Dumps the HTML stats information block to the trash for and uses the state from
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004985 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004986 * for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004987 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01004988static void stats_dump_html_info(struct stream_interface *si, struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004989{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004990 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004991 unsigned int up = (now.tv_sec - start_date.tv_sec);
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02004992 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
Krzysztof Piotr Oledzki15514c22010-01-04 16:03:09 +01004993
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004994 /* WARNING! this has to fit the first packet too.
4995 * We are around 3.5 kB, add adding entries will
4996 * become tricky if we want to support 4kB buffers !
4997 */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01004998 chunk_appendf(&trash,
4999 "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">"
5000 PRODUCT_NAME "%s</a></h1>\n"
5001 "<h2>Statistics Report for pid %d%s%s%s%s</h2>\n"
5002 "<hr width=\"100%%\" class=\"hr\">\n"
5003 "<h3>&gt; General process information</h3>\n"
5004 "<table border=0><tr><td align=\"left\" nowrap width=\"1%%\">\n"
5005 "<p><b>pid = </b> %d (process #%d, nbproc = %d)<br>\n"
5006 "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n"
5007 "<b>system limits:</b> memmax = %s%s; ulimit-n = %d<br>\n"
5008 "<b>maxsock = </b> %d; <b>maxconn = </b> %d; <b>maxpipes = </b> %d<br>\n"
5009 "current conns = %d; current pipes = %d/%d; conn rate = %d/sec<br>\n"
5010 "Running tasks: %d/%d; idle = %d %%<br>\n"
5011 "</td><td align=\"center\" nowrap>\n"
5012 "<table class=\"lgd\"><tr>\n"
Simon Horman837bfa72015-04-23 14:51:27 +09005013 "<td class=\"active_up\">&nbsp;</td><td class=\"noborder\">active UP </td>"
5014 "<td class=\"backup_up\">&nbsp;</td><td class=\"noborder\">backup UP </td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005015 "</tr><tr>\n"
Simon Horman837bfa72015-04-23 14:51:27 +09005016 "<td class=\"active_going_down\"></td><td class=\"noborder\">active UP, going down </td>"
5017 "<td class=\"backup_going_down\"></td><td class=\"noborder\">backup UP, going down </td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005018 "</tr><tr>\n"
Simon Horman837bfa72015-04-23 14:51:27 +09005019 "<td class=\"active_going_up\"></td><td class=\"noborder\">active DOWN, going up </td>"
5020 "<td class=\"backup_going_up\"></td><td class=\"noborder\">backup DOWN, going up </td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005021 "</tr><tr>\n"
Simon Horman837bfa72015-04-23 14:51:27 +09005022 "<td class=\"active_down\"></td><td class=\"noborder\">active or backup DOWN &nbsp;</td>"
5023 "<td class=\"active_no_check\"></td><td class=\"noborder\">not checked </td>"
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005024 "</tr><tr>\n"
5025 "<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 -07005026 "</tr><tr>\n"
Simon Horman837bfa72015-04-23 14:51:27 +09005027 "<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 +01005028 "</tr></table>\n"
Willy Tarreau6b7764a2013-12-04 00:43:21 +01005029 "Note: \"NOLB\"/\"DRAIN\" = UP with load-balancing disabled."
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005030 "</td>"
5031 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
5032 "<b>Display option:</b><ul style=\"margin-top: 0.25em;\">"
5033 "",
5034 (uri->flags & ST_HIDEVER) ? "" : (STATS_VERSION_STRING),
5035 pid, (uri->flags & ST_SHNODE) ? " on " : "",
5036 (uri->flags & ST_SHNODE) ? (uri->node ? uri->node : global.node) : "",
5037 (uri->flags & ST_SHDESC) ? ": " : "",
5038 (uri->flags & ST_SHDESC) ? (uri->desc ? uri->desc : global.desc) : "",
5039 pid, relative_pid, global.nbproc,
5040 up / 86400, (up % 86400) / 3600,
5041 (up % 3600) / 60, (up % 60),
5042 global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited",
5043 global.rlimit_memmax ? " MB" : "",
5044 global.rlimit_nofile,
5045 global.maxsock, global.maxconn, global.maxpipes,
5046 actconn, pipes_used, pipes_used+pipes_free, read_freq_ctr(&global.conn_per_sec),
5047 run_queue_cur, nb_tasks_cur, idle_pct
5048 );
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005049
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005050 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Willy Tarreau4e4292b2014-11-28 12:18:45 +01005051 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 +01005052 scope_txt[appctx->ctx.stats.scope_len] = '\0';
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02005053
5054 chunk_appendf(&trash,
Jeff Buchbinder2dbbf4d2014-08-29 15:10:08 -05005055 "<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 +01005056 (appctx->ctx.stats.scope_len > 0) ? scope_txt : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02005057 STAT_SCOPE_TXT_MAXLEN);
5058
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005059 /* 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 +02005060 scope_txt[0] = 0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005061 if (appctx->ctx.stats.scope_len) {
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02005062 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau4e4292b2014-11-28 12:18:45 +01005063 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 +01005064 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02005065 }
5066
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005067 if (appctx->ctx.stats.flags & STAT_HIDE_DOWN)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005068 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02005069 "<li><a href=\"%s%s%s%s\">Show all servers</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005070 uri->uri_prefix,
5071 "",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005072 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02005073 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005074 else
5075 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02005076 "<li><a href=\"%s%s%s%s\">Hide 'DOWN' servers</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005077 uri->uri_prefix,
5078 ";up",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005079 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02005080 scope_txt);
Willy Tarreau91861262007-10-17 17:06:05 +02005081
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005082 if (uri->refresh > 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005083 if (appctx->ctx.stats.flags & STAT_NO_REFRESH)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005084 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02005085 "<li><a href=\"%s%s%s%s\">Enable refresh</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005086 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005087 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02005088 "",
5089 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005090 else
5091 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02005092 "<li><a href=\"%s%s%s%s\">Disable refresh</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005093 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005094 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02005095 ";norefresh",
5096 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005097 }
Willy Tarreau55bb8452007-10-17 18:44:57 +02005098
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005099 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02005100 "<li><a href=\"%s%s%s%s\">Refresh now</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005101 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005102 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
5103 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02005104 scope_txt);
Elijah Epifanovacafc5f2007-10-25 20:15:38 +02005105
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005106 chunk_appendf(&trash,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02005107 "<li><a href=\"%s;csv%s%s\">CSV export</a><br>\n",
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005108 uri->uri_prefix,
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02005109 (uri->refresh > 0) ? ";norefresh" : "",
5110 scope_txt);
Willy Tarreau4bab24d2007-11-30 18:16:29 +01005111
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005112 chunk_appendf(&trash,
5113 "</ul></td>"
5114 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
5115 "<b>External resources:</b><ul style=\"margin-top: 0.25em;\">\n"
5116 "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>\n"
5117 "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>\n"
5118 "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>\n"
5119 "</ul>"
5120 "</td>"
5121 "</tr></table>\n"
5122 ""
5123 );
Willy Tarreau4bab24d2007-11-30 18:16:29 +01005124
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005125 if (appctx->ctx.stats.st_code) {
5126 switch (appctx->ctx.stats.st_code) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005127 case STAT_STATUS_DONE:
5128 chunk_appendf(&trash,
Simon Horman837bfa72015-04-23 14:51:27 +09005129 "<p><div class=active_up>"
Willy Tarreauba6be982013-04-19 12:16:55 +02005130 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005131 "Action processed successfully."
Willy Tarreauba6be982013-04-19 12:16:55 +02005132 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005133 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
5134 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02005135 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005136 break;
5137 case STAT_STATUS_NONE:
5138 chunk_appendf(&trash,
Simon Horman837bfa72015-04-23 14:51:27 +09005139 "<p><div class=active_going_down>"
Willy Tarreauba6be982013-04-19 12:16:55 +02005140 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005141 "Nothing has changed."
Willy Tarreauba6be982013-04-19 12:16:55 +02005142 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005143 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
5144 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02005145 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005146 break;
5147 case STAT_STATUS_PART:
5148 chunk_appendf(&trash,
Simon Horman837bfa72015-04-23 14:51:27 +09005149 "<p><div class=active_going_down>"
Willy Tarreauba6be982013-04-19 12:16:55 +02005150 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005151 "Action partially processed.<br>"
5152 "Some server names are probably unknown or ambiguous (duplicated names in the backend)."
Willy Tarreauba6be982013-04-19 12:16:55 +02005153 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005154 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
5155 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02005156 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005157 break;
5158 case STAT_STATUS_ERRP:
5159 chunk_appendf(&trash,
Simon Horman837bfa72015-04-23 14:51:27 +09005160 "<p><div class=active_down>"
Willy Tarreauba6be982013-04-19 12:16:55 +02005161 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005162 "Action not processed because of invalid parameters."
5163 "<ul>"
5164 "<li>The action is maybe unknown.</li>"
5165 "<li>The backend name is probably unknown or ambiguous (duplicated names).</li>"
5166 "<li>Some server names are probably unknown or ambiguous (duplicated names in the backend).</li>"
5167 "</ul>"
Willy Tarreauba6be982013-04-19 12:16:55 +02005168 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005169 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
5170 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02005171 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005172 break;
5173 case STAT_STATUS_EXCD:
5174 chunk_appendf(&trash,
Simon Horman837bfa72015-04-23 14:51:27 +09005175 "<p><div class=active_down>"
Willy Tarreauba6be982013-04-19 12:16:55 +02005176 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005177 "<b>Action not processed : the buffer couldn't store all the data.<br>"
5178 "You should retry with less servers at a time.</b>"
Willy Tarreauba6be982013-04-19 12:16:55 +02005179 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005180 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
5181 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02005182 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005183 break;
5184 case STAT_STATUS_DENY:
5185 chunk_appendf(&trash,
Simon Horman837bfa72015-04-23 14:51:27 +09005186 "<p><div class=active_down>"
Willy Tarreauba6be982013-04-19 12:16:55 +02005187 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005188 "<b>Action denied.</b>"
Willy Tarreauba6be982013-04-19 12:16:55 +02005189 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005190 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
5191 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02005192 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005193 break;
5194 default:
5195 chunk_appendf(&trash,
Simon Horman837bfa72015-04-23 14:51:27 +09005196 "<p><div class=active_no_check>"
Willy Tarreauba6be982013-04-19 12:16:55 +02005197 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005198 "Unexpected result."
Willy Tarreauba6be982013-04-19 12:16:55 +02005199 "</div>\n", uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005200 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
5201 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreauba6be982013-04-19 12:16:55 +02005202 scope_txt);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005203 }
5204 chunk_appendf(&trash, "<p>\n");
5205 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005206}
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01005207
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01005208/* Dumps the HTML stats trailer block to the trash. The caller is responsible
5209 * for clearing the trash if needed.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005210 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01005211static void stats_dump_html_end()
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005212{
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005213 chunk_appendf(&trash, "</body></html>\n");
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005214}
Willy Tarreau7f062c42009-03-05 18:43:00 +01005215
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01005216/* This function dumps statistics onto the stream interface's read buffer in
5217 * either CSV or HTML format. <uri> contains some HTML-specific parameters that
Willy Tarreau306f8302013-07-08 15:53:06 +02005218 * are ignored for CSV format (hence <uri> may be NULL there). It returns 0 if
5219 * 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 +02005220 * and the stream must be closed, or -1 in case of any error. This function is
Willy Tarreau306f8302013-07-08 15:53:06 +02005221 * used by both the CLI and the HTTP handlers.
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005222 */
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01005223static int stats_dump_stat_to_buffer(struct stream_interface *si, struct uri_auth *uri)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005224{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005225 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005226 struct channel *rep = si_ic(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005227 struct proxy *px;
Willy Tarreau7f062c42009-03-05 18:43:00 +01005228
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005229 chunk_reset(&trash);
Krzysztof Piotr Oledzki09605412009-09-23 22:09:24 +02005230
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005231 switch (appctx->st2) {
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005232 case STAT_ST_INIT:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005233 appctx->st2 = STAT_ST_HEAD; /* let's start producing data */
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005234 /* fall through */
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005235
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005236 case STAT_ST_HEAD:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005237 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01005238 stats_dump_html_head(uri);
Willy Tarreau1e62df92016-01-11 18:57:53 +01005239 else if (!(appctx->ctx.stats.flags & STAT_FMT_TYPED))
Willy Tarreau354898b2012-12-23 18:15:23 +01005240 stats_dump_csv_header();
Willy Tarreaud9b587f2010-02-26 10:05:55 +01005241
Willy Tarreaubc18da12015-03-13 14:00:47 +01005242 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02005243 si_applet_cant_put(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005244 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01005245 }
Willy Tarreauae526782010-03-04 20:34:23 +01005246
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005247 appctx->st2 = STAT_ST_INFO;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005248 /* fall through */
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01005249
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005250 case STAT_ST_INFO:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005251 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01005252 stats_dump_html_info(si, uri);
Willy Tarreaubc18da12015-03-13 14:00:47 +01005253 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02005254 si_applet_cant_put(si);
Willy Tarreau91861262007-10-17 17:06:05 +02005255 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01005256 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005257 }
Willy Tarreau91861262007-10-17 17:06:05 +02005258
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005259 appctx->ctx.stats.px = proxy;
5260 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
5261 appctx->st2 = STAT_ST_LIST;
Willy Tarreau91861262007-10-17 17:06:05 +02005262 /* fall through */
5263
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005264 case STAT_ST_LIST:
5265 /* dump proxies */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005266 while (appctx->ctx.stats.px) {
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01005267 if (buffer_almost_full(rep->buf)) {
Willy Tarreaufe127932015-04-21 19:23:39 +02005268 si_applet_cant_put(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005269 return 0;
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01005270 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005271
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005272 px = appctx->ctx.stats.px;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005273 /* skip the disabled proxies, global frontend and non-networked ones */
5274 if (px->state != PR_STSTOPPED && px->uuid > 0 && (px->cap & (PR_CAP_FE | PR_CAP_BE)))
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01005275 if (stats_dump_proxy_to_buffer(si, px, uri) == 0)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005276 return 0;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005277
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005278 appctx->ctx.stats.px = px->next;
5279 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005280 }
5281 /* here, we just have reached the last proxy */
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005282
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005283 appctx->st2 = STAT_ST_END;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005284 /* fall through */
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005285
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005286 case STAT_ST_END:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005287 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreaub5ba4ec2012-12-22 23:20:30 +01005288 stats_dump_html_end();
Willy Tarreaubc18da12015-03-13 14:00:47 +01005289 if (bi_putchk(rep, &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02005290 si_applet_cant_put(si);
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005291 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01005292 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005293 }
Willy Tarreau55058a72012-11-21 08:27:21 +01005294
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005295 appctx->st2 = STAT_ST_FIN;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005296 /* fall through */
Willy Tarreau0a6d2ef2009-03-29 14:46:01 +02005297
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005298 case STAT_ST_FIN:
5299 return 1;
Willy Tarreau55058a72012-11-21 08:27:21 +01005300
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005301 default:
5302 /* unknown state ! */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005303 appctx->st2 = STAT_ST_FIN;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005304 return -1;
5305 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005306}
Willy Tarreauae526782010-03-04 20:34:23 +01005307
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005308/* We reached the stats page through a POST request. The appctx is
5309 * expected to have already been allocated by the caller.
Willy Tarreau347a35d2013-11-22 17:51:09 +01005310 * Parse the posted data and enable/disable servers if necessary.
5311 * Returns 1 if request was parsed or zero if it needs more data.
5312 */
5313static int stats_process_http_post(struct stream_interface *si)
5314{
Willy Tarreau87b09662015-04-03 00:22:06 +02005315 struct stream *s = si_strm(si);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005316 struct appctx *appctx = objt_appctx(si->end);
Willy Tarreau347a35d2013-11-22 17:51:09 +01005317
5318 struct proxy *px = NULL;
5319 struct server *sv = NULL;
5320
5321 char key[LINESIZE];
5322 int action = ST_ADM_ACTION_NONE;
5323 int reprocess = 0;
5324
5325 int total_servers = 0;
5326 int altered_servers = 0;
5327
5328 char *first_param, *cur_param, *next_param, *end_params;
5329 char *st_cur_param = NULL;
5330 char *st_next_param = NULL;
5331
5332 struct chunk *temp;
5333 int reql;
5334
5335 temp = get_trash_chunk();
Willy Tarreaueee5b512015-04-03 23:46:31 +02005336 if (temp->size < s->txn->req.body_len) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01005337 /* too large request */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005338 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005339 goto out;
5340 }
5341
Willy Tarreaueee5b512015-04-03 23:46:31 +02005342 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 +01005343 if (reql <= 0) {
5344 /* we need more data */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005345 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005346 return 0;
5347 }
5348
5349 first_param = temp->str;
5350 end_params = temp->str + reql;
5351 cur_param = next_param = end_params;
5352 *end_params = '\0';
5353
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005354 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005355
5356 /*
5357 * Parse the parameters in reverse order to only store the last value.
5358 * From the html form, the backend and the action are at the end.
5359 */
5360 while (cur_param > first_param) {
5361 char *value;
5362 int poffset, plen;
5363
5364 cur_param--;
5365
5366 if ((*cur_param == '&') || (cur_param == first_param)) {
5367 reprocess_servers:
5368 /* Parse the key */
5369 poffset = (cur_param != first_param ? 1 : 0);
5370 plen = next_param - cur_param + (cur_param == first_param ? 1 : 0);
5371 if ((plen > 0) && (plen <= sizeof(key))) {
5372 strncpy(key, cur_param + poffset, plen);
5373 key[plen - 1] = '\0';
5374 } else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005375 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005376 goto out;
5377 }
5378
5379 /* Parse the value */
5380 value = key;
5381 while (*value != '\0' && *value != '=') {
5382 value++;
5383 }
5384 if (*value == '=') {
5385 /* Ok, a value is found, we can mark the end of the key */
5386 *value++ = '\0';
5387 }
5388 if (url_decode(key) < 0 || url_decode(value) < 0)
5389 break;
5390
5391 /* Now we can check the key to see what to do */
5392 if (!px && (strcmp(key, "b") == 0)) {
Willy Tarreau9e0bb102015-05-26 11:24:42 +02005393 if ((px = proxy_be_by_name(value)) == NULL) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01005394 /* the backend name is unknown or ambiguous (duplicate names) */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005395 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005396 goto out;
5397 }
5398 }
5399 else if (!action && (strcmp(key, "action") == 0)) {
Willy Tarreaued7df902014-05-22 18:04:49 +02005400 if (strcmp(value, "ready") == 0) {
5401 action = ST_ADM_ACTION_READY;
5402 }
5403 else if (strcmp(value, "drain") == 0) {
5404 action = ST_ADM_ACTION_DRAIN;
5405 }
5406 else if (strcmp(value, "maint") == 0) {
5407 action = ST_ADM_ACTION_MAINT;
5408 }
5409 else if (strcmp(value, "shutdown") == 0) {
5410 action = ST_ADM_ACTION_SHUTDOWN;
5411 }
Willy Tarreau248a60e2014-05-23 14:59:48 +02005412 else if (strcmp(value, "dhlth") == 0) {
5413 action = ST_ADM_ACTION_DHLTH;
5414 }
5415 else if (strcmp(value, "ehlth") == 0) {
5416 action = ST_ADM_ACTION_EHLTH;
5417 }
5418 else if (strcmp(value, "hrunn") == 0) {
5419 action = ST_ADM_ACTION_HRUNN;
5420 }
5421 else if (strcmp(value, "hnolb") == 0) {
5422 action = ST_ADM_ACTION_HNOLB;
5423 }
5424 else if (strcmp(value, "hdown") == 0) {
5425 action = ST_ADM_ACTION_HDOWN;
5426 }
5427 else if (strcmp(value, "dagent") == 0) {
5428 action = ST_ADM_ACTION_DAGENT;
5429 }
5430 else if (strcmp(value, "eagent") == 0) {
5431 action = ST_ADM_ACTION_EAGENT;
5432 }
5433 else if (strcmp(value, "arunn") == 0) {
5434 action = ST_ADM_ACTION_ARUNN;
5435 }
5436 else if (strcmp(value, "adown") == 0) {
5437 action = ST_ADM_ACTION_ADOWN;
5438 }
Willy Tarreaued7df902014-05-22 18:04:49 +02005439 /* else these are the old supported methods */
5440 else if (strcmp(value, "disable") == 0) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01005441 action = ST_ADM_ACTION_DISABLE;
5442 }
5443 else if (strcmp(value, "enable") == 0) {
5444 action = ST_ADM_ACTION_ENABLE;
5445 }
5446 else if (strcmp(value, "stop") == 0) {
5447 action = ST_ADM_ACTION_STOP;
5448 }
5449 else if (strcmp(value, "start") == 0) {
5450 action = ST_ADM_ACTION_START;
5451 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01005452 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005453 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005454 goto out;
5455 }
5456 }
5457 else if (strcmp(key, "s") == 0) {
5458 if (!(px && action)) {
5459 /*
5460 * Indicates that we'll need to reprocess the parameters
5461 * as soon as backend and action are known
5462 */
5463 if (!reprocess) {
5464 st_cur_param = cur_param;
5465 st_next_param = next_param;
5466 }
5467 reprocess = 1;
5468 }
5469 else if ((sv = findserver(px, value)) != NULL) {
5470 switch (action) {
5471 case ST_ADM_ACTION_DISABLE:
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02005472 if (!(sv->admin & SRV_ADMF_FMAINT)) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01005473 altered_servers++;
5474 total_servers++;
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02005475 srv_set_admin_flag(sv, SRV_ADMF_FMAINT);
Willy Tarreau347a35d2013-11-22 17:51:09 +01005476 }
5477 break;
5478 case ST_ADM_ACTION_ENABLE:
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02005479 if (sv->admin & SRV_ADMF_FMAINT) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01005480 altered_servers++;
5481 total_servers++;
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02005482 srv_clr_admin_flag(sv, SRV_ADMF_FMAINT);
Willy Tarreau347a35d2013-11-22 17:51:09 +01005483 }
5484 break;
5485 case ST_ADM_ACTION_STOP:
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02005486 if (!(sv->admin & SRV_ADMF_FDRAIN)) {
5487 srv_set_admin_flag(sv, SRV_ADMF_FDRAIN);
5488 altered_servers++;
5489 total_servers++;
5490 }
5491 break;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005492 case ST_ADM_ACTION_START:
Willy Tarreaufae3a7e2014-05-22 17:22:34 +02005493 if (sv->admin & SRV_ADMF_FDRAIN) {
5494 srv_clr_admin_flag(sv, SRV_ADMF_FDRAIN);
5495 altered_servers++;
5496 total_servers++;
5497 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01005498 break;
Willy Tarreau248a60e2014-05-23 14:59:48 +02005499 case ST_ADM_ACTION_DHLTH:
5500 if (sv->check.state & CHK_ST_CONFIGURED) {
5501 sv->check.state &= ~CHK_ST_ENABLED;
5502 altered_servers++;
5503 total_servers++;
5504 }
5505 break;
5506 case ST_ADM_ACTION_EHLTH:
5507 if (sv->check.state & CHK_ST_CONFIGURED) {
5508 sv->check.state |= CHK_ST_ENABLED;
5509 altered_servers++;
5510 total_servers++;
5511 }
5512 break;
5513 case ST_ADM_ACTION_HRUNN:
5514 if (!(sv->track)) {
5515 sv->check.health = sv->check.rise + sv->check.fall - 1;
5516 srv_set_running(sv, "changed from Web interface");
5517 altered_servers++;
5518 total_servers++;
5519 }
5520 break;
5521 case ST_ADM_ACTION_HNOLB:
5522 if (!(sv->track)) {
5523 sv->check.health = sv->check.rise + sv->check.fall - 1;
5524 srv_set_stopping(sv, "changed from Web interface");
5525 altered_servers++;
5526 total_servers++;
5527 }
5528 break;
5529 case ST_ADM_ACTION_HDOWN:
5530 if (!(sv->track)) {
5531 sv->check.health = 0;
5532 srv_set_stopped(sv, "changed from Web interface");
5533 altered_servers++;
5534 total_servers++;
5535 }
5536 break;
5537 case ST_ADM_ACTION_DAGENT:
5538 if (sv->agent.state & CHK_ST_CONFIGURED) {
5539 sv->agent.state &= ~CHK_ST_ENABLED;
5540 altered_servers++;
5541 total_servers++;
5542 }
5543 break;
5544 case ST_ADM_ACTION_EAGENT:
5545 if (sv->agent.state & CHK_ST_CONFIGURED) {
5546 sv->agent.state |= CHK_ST_ENABLED;
5547 altered_servers++;
5548 total_servers++;
5549 }
5550 break;
5551 case ST_ADM_ACTION_ARUNN:
5552 if (sv->agent.state & CHK_ST_ENABLED) {
5553 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
5554 srv_set_running(sv, "changed from Web interface");
5555 altered_servers++;
5556 total_servers++;
5557 }
5558 break;
5559 case ST_ADM_ACTION_ADOWN:
5560 if (sv->agent.state & CHK_ST_ENABLED) {
5561 sv->agent.health = 0;
5562 srv_set_stopped(sv, "changed from Web interface");
5563 altered_servers++;
5564 total_servers++;
5565 }
5566 break;
Willy Tarreaued7df902014-05-22 18:04:49 +02005567 case ST_ADM_ACTION_READY:
5568 srv_adm_set_ready(sv);
5569 altered_servers++;
5570 total_servers++;
5571 break;
5572 case ST_ADM_ACTION_DRAIN:
5573 srv_adm_set_drain(sv);
5574 altered_servers++;
5575 total_servers++;
5576 break;
5577 case ST_ADM_ACTION_MAINT:
5578 srv_adm_set_maint(sv);
5579 altered_servers++;
5580 total_servers++;
5581 break;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005582 case ST_ADM_ACTION_SHUTDOWN:
5583 if (px->state != PR_STSTOPPED) {
Willy Tarreau87b09662015-04-03 00:22:06 +02005584 struct stream *sess, *sess_bck;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005585
5586 list_for_each_entry_safe(sess, sess_bck, &sv->actconns, by_srv)
5587 if (sess->srv_conn == sv)
Willy Tarreaue7dff022015-04-03 01:14:29 +02005588 stream_shutdown(sess, SF_ERR_KILLED);
Willy Tarreau347a35d2013-11-22 17:51:09 +01005589
5590 altered_servers++;
5591 total_servers++;
5592 }
5593 break;
5594 }
5595 } else {
5596 /* the server name is unknown or ambiguous (duplicate names) */
5597 total_servers++;
5598 }
5599 }
5600 if (reprocess && px && action) {
5601 /* Now, we know the backend and the action chosen by the user.
5602 * We can safely restart from the first server parameter
5603 * to reprocess them
5604 */
5605 cur_param = st_cur_param;
5606 next_param = st_next_param;
5607 reprocess = 0;
5608 goto reprocess_servers;
5609 }
5610
5611 next_param = cur_param;
5612 }
5613 }
5614
5615 if (total_servers == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005616 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005617 }
5618 else if (altered_servers == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005619 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005620 }
5621 else if (altered_servers == total_servers) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005622 appctx->ctx.stats.st_code = STAT_STATUS_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005623 }
5624 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005625 appctx->ctx.stats.st_code = STAT_STATUS_PART;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005626 }
5627 out:
5628 return 1;
5629}
5630
5631
5632static int stats_send_http_headers(struct stream_interface *si)
5633{
Willy Tarreau87b09662015-04-03 00:22:06 +02005634 struct stream *s = si_strm(si);
Willy Tarreau347a35d2013-11-22 17:51:09 +01005635 struct uri_auth *uri = s->be->uri_auth;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005636 struct appctx *appctx = objt_appctx(si->end);
Willy Tarreau347a35d2013-11-22 17:51:09 +01005637
5638 chunk_printf(&trash,
Willy Tarreau8b8995f2014-04-24 22:51:54 +02005639 "HTTP/1.1 200 OK\r\n"
Willy Tarreau347a35d2013-11-22 17:51:09 +01005640 "Cache-Control: no-cache\r\n"
5641 "Connection: close\r\n"
5642 "Content-Type: %s\r\n",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005643 (appctx->ctx.stats.flags & STAT_FMT_HTML) ? "text/html" : "text/plain");
Willy Tarreau347a35d2013-11-22 17:51:09 +01005644
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005645 if (uri->refresh > 0 && !(appctx->ctx.stats.flags & STAT_NO_REFRESH))
Willy Tarreau347a35d2013-11-22 17:51:09 +01005646 chunk_appendf(&trash, "Refresh: %d\r\n",
5647 uri->refresh);
5648
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005649 /* we don't send the CRLF in chunked mode, it will be sent with the first chunk's size */
5650
5651 if (appctx->ctx.stats.flags & STAT_CHUNKED)
5652 chunk_appendf(&trash, "Transfer-Encoding: chunked\r\n");
5653 else
5654 chunk_appendf(&trash, "\r\n");
Willy Tarreau347a35d2013-11-22 17:51:09 +01005655
Willy Tarreaueee5b512015-04-03 23:46:31 +02005656 s->txn->status = 200;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005657 s->logs.tv_request = now;
5658
Willy Tarreaubc18da12015-03-13 14:00:47 +01005659 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02005660 si_applet_cant_put(si);
Willy Tarreau347a35d2013-11-22 17:51:09 +01005661 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01005662 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01005663
5664 return 1;
5665}
5666
5667static int stats_send_http_redirect(struct stream_interface *si)
5668{
5669 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
Willy Tarreau87b09662015-04-03 00:22:06 +02005670 struct stream *s = si_strm(si);
Willy Tarreau347a35d2013-11-22 17:51:09 +01005671 struct uri_auth *uri = s->be->uri_auth;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005672 struct appctx *appctx = objt_appctx(si->end);
Willy Tarreau347a35d2013-11-22 17:51:09 +01005673
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005674 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Willy Tarreau347a35d2013-11-22 17:51:09 +01005675 scope_txt[0] = 0;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005676 if (appctx->ctx.stats.scope_len) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01005677 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau4e4292b2014-11-28 12:18:45 +01005678 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 +01005679 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005680 }
5681
5682 /* We don't want to land on the posted stats page because a refresh will
5683 * repost the data. We don't want this to happen on accident so we redirect
5684 * the browse to the stats page with a GET.
5685 */
5686 chunk_printf(&trash,
5687 "HTTP/1.1 303 See Other\r\n"
5688 "Cache-Control: no-cache\r\n"
5689 "Content-Type: text/plain\r\n"
5690 "Connection: close\r\n"
5691 "Location: %s;st=%s%s%s%s\r\n"
Willy Tarreaufdfcc9d2016-01-26 13:57:29 +01005692 "Content-length: 0\r\n"
Willy Tarreau347a35d2013-11-22 17:51:09 +01005693 "\r\n",
5694 uri->uri_prefix,
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005695 ((appctx->ctx.stats.st_code > STAT_STATUS_INIT) &&
5696 (appctx->ctx.stats.st_code < STAT_STATUS_SIZE) &&
5697 stat_status_codes[appctx->ctx.stats.st_code]) ?
5698 stat_status_codes[appctx->ctx.stats.st_code] :
Willy Tarreau347a35d2013-11-22 17:51:09 +01005699 stat_status_codes[STAT_STATUS_UNKN],
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005700 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
5701 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
Willy Tarreau347a35d2013-11-22 17:51:09 +01005702 scope_txt);
5703
Willy Tarreaueee5b512015-04-03 23:46:31 +02005704 s->txn->status = 303;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005705 s->logs.tv_request = now;
5706
Willy Tarreaubc18da12015-03-13 14:00:47 +01005707 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02005708 si_applet_cant_put(si);
Willy Tarreau347a35d2013-11-22 17:51:09 +01005709 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01005710 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01005711
5712 return 1;
5713}
5714
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005715/* This I/O handler runs as an applet embedded in a stream interface. It is
5716 * used to send HTTP stats over a TCP socket. The mechanism is very simple.
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005717 * appctx->st0 contains the operation in progress (dump, done). The handler
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005718 * automatically unregisters itself once transfer is complete.
5719 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02005720static void http_stats_io_handler(struct appctx *appctx)
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005721{
Willy Tarreau00a37f02015-04-13 12:05:19 +02005722 struct stream_interface *si = appctx->owner;
Willy Tarreau87b09662015-04-03 00:22:06 +02005723 struct stream *s = si_strm(si);
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005724 struct channel *req = si_oc(si);
5725 struct channel *res = si_ic(si);
Willy Tarreau55058a72012-11-21 08:27:21 +01005726
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005727 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
5728 goto out;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01005729
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005730 /* check that the output is not closed */
5731 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005732 appctx->st0 = STAT_HTTP_DONE;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005733
Willy Tarreau347a35d2013-11-22 17:51:09 +01005734 /* all states are processed in sequence */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005735 if (appctx->st0 == STAT_HTTP_HEAD) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01005736 if (stats_send_http_headers(si)) {
Willy Tarreaueee5b512015-04-03 23:46:31 +02005737 if (s->txn->meth == HTTP_METH_HEAD)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005738 appctx->st0 = STAT_HTTP_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005739 else
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005740 appctx->st0 = STAT_HTTP_DUMP;
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005741 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01005742 }
5743
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005744 if (appctx->st0 == STAT_HTTP_DUMP) {
Willy Tarreau4e4292b2014-11-28 12:18:45 +01005745 unsigned int prev_len = si_ib(si)->i;
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005746 unsigned int data_len;
5747 unsigned int last_len;
Willy Tarreaucce36482014-04-24 20:26:41 +02005748 unsigned int last_fwd = 0;
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005749
5750 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
5751 /* One difficulty we're facing is that we must prevent
5752 * the input data from being automatically forwarded to
5753 * the output area. For this, we temporarily disable
5754 * forwarding on the channel.
5755 */
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005756 last_fwd = si_ic(si)->to_forward;
5757 si_ic(si)->to_forward = 0;
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005758 chunk_printf(&trash, "\r\n000000\r\n");
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005759 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02005760 si_applet_cant_put(si);
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005761 si_ic(si)->to_forward = last_fwd;
Willy Tarreau828824a2015-04-19 17:20:03 +02005762 goto out;
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005763 }
5764 }
5765
Willy Tarreau4e4292b2014-11-28 12:18:45 +01005766 data_len = si_ib(si)->i;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005767 if (stats_dump_stat_to_buffer(si, s->be->uri_auth))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005768 appctx->st0 = STAT_HTTP_DONE;
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005769
Willy Tarreau4e4292b2014-11-28 12:18:45 +01005770 last_len = si_ib(si)->i;
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005771
5772 /* Now we must either adjust or remove the chunk size. This is
5773 * not easy because the chunk size might wrap at the end of the
5774 * buffer, so we pretend we have nothing in the buffer, we write
5775 * the size, then restore the buffer's contents. Note that we can
5776 * only do that because no forwarding is scheduled on the stats
5777 * applet.
5778 */
5779 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
Willy Tarreau4e4292b2014-11-28 12:18:45 +01005780 si_ic(si)->total -= (last_len - prev_len);
5781 si_ib(si)->i -= (last_len - prev_len);
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005782
5783 if (last_len != data_len) {
5784 chunk_printf(&trash, "\r\n%06x\r\n", (last_len - data_len));
Willy Tarreaubc18da12015-03-13 14:00:47 +01005785 if (bi_putchk(si_ic(si), &trash) == -1)
Willy Tarreaufe127932015-04-21 19:23:39 +02005786 si_applet_cant_put(si);
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005787
Willy Tarreau4e4292b2014-11-28 12:18:45 +01005788 si_ic(si)->total += (last_len - data_len);
5789 si_ib(si)->i += (last_len - data_len);
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005790 }
5791 /* now re-enable forwarding */
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005792 channel_forward(si_ic(si), last_fwd);
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005793 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005794 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02005795
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005796 if (appctx->st0 == STAT_HTTP_POST) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01005797 if (stats_process_http_post(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005798 appctx->st0 = STAT_HTTP_LAST;
Willy Tarreau2bb4a962014-11-28 11:11:05 +01005799 else if (si_oc(si)->flags & CF_SHUTR)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005800 appctx->st0 = STAT_HTTP_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005801 }
5802
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005803 if (appctx->st0 == STAT_HTTP_LAST) {
Willy Tarreau347a35d2013-11-22 17:51:09 +01005804 if (stats_send_http_redirect(si))
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005805 appctx->st0 = STAT_HTTP_DONE;
Willy Tarreau347a35d2013-11-22 17:51:09 +01005806 }
5807
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005808 if (appctx->st0 == STAT_HTTP_DONE) {
5809 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
5810 chunk_printf(&trash, "\r\n0\r\n\r\n");
Willy Tarreaubc18da12015-03-13 14:00:47 +01005811 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02005812 si_applet_cant_put(si);
Willy Tarreau828824a2015-04-19 17:20:03 +02005813 goto out;
Willy Tarreaubc18da12015-03-13 14:00:47 +01005814 }
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005815 }
5816 /* eat the whole request */
Willy Tarreau4e4292b2014-11-28 12:18:45 +01005817 bo_skip(si_oc(si), si_ob(si)->o);
Willy Tarreauaf3cf702014-04-22 22:19:53 +02005818 res->flags |= CF_READ_NULL;
5819 si_shutr(si);
5820 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01005821
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005822 if ((res->flags & CF_SHUTR) && (si->state == SI_ST_EST))
5823 si_shutw(si);
Willy Tarreau91861262007-10-17 17:06:05 +02005824
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005825 if (appctx->st0 == STAT_HTTP_DONE) {
Willy Tarreau96d44912013-11-22 12:25:24 +01005826 if ((req->flags & CF_SHUTW) && (si->state == SI_ST_EST)) {
5827 si_shutr(si);
5828 res->flags |= CF_READ_NULL;
5829 }
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005830 }
Willy Tarreau828824a2015-04-19 17:20:03 +02005831 out:
Willy Tarreaud4da1962015-04-20 01:31:23 +02005832 /* just to make gcc happy */ ;
Willy Tarreau91861262007-10-17 17:06:05 +02005833}
5834
Willy Tarreaud9bdcd52012-12-22 20:31:10 +01005835
Willy Tarreau909d5172012-11-26 03:04:41 +01005836static inline const char *get_conn_ctrl_name(const struct connection *conn)
5837{
Willy Tarreau3c728722014-01-23 13:50:42 +01005838 if (!conn_ctrl_ready(conn))
Willy Tarreau909d5172012-11-26 03:04:41 +01005839 return "NONE";
5840 return conn->ctrl->name;
5841}
5842
5843static inline const char *get_conn_xprt_name(const struct connection *conn)
5844{
5845 static char ptr[17];
5846
Willy Tarreauaad69382014-01-23 14:21:42 +01005847 if (!conn_xprt_ready(conn))
Willy Tarreau909d5172012-11-26 03:04:41 +01005848 return "NONE";
5849
5850 if (conn->xprt == &raw_sock)
5851 return "RAW";
5852
5853#ifdef USE_OPENSSL
5854 if (conn->xprt == &ssl_sock)
5855 return "SSL";
5856#endif
5857 snprintf(ptr, sizeof(ptr), "%p", conn->xprt);
5858 return ptr;
5859}
5860
5861static inline const char *get_conn_data_name(const struct connection *conn)
5862{
5863 static char ptr[17];
5864
5865 if (!conn->data)
5866 return "NONE";
5867
5868 if (conn->data == &sess_conn_cb)
5869 return "SESS";
5870
5871 if (conn->data == &si_conn_cb)
5872 return "STRM";
5873
5874 if (conn->data == &check_conn_cb)
5875 return "CHCK";
5876
5877 snprintf(ptr, sizeof(ptr), "%p", conn->data);
5878 return ptr;
5879}
5880
Willy Tarreau87b09662015-04-03 00:22:06 +02005881/* This function dumps a complete stream state onto the stream interface's
5882 * read buffer. The stream has to be set in sess->target. It returns
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01005883 * 0 if the output buffer is full and it needs to be called again, otherwise
5884 * non-zero. It is designed to be called from stats_dump_sess_to_buffer() below.
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005885 */
Willy Tarreau87b09662015-04-03 00:22:06 +02005886static int stats_dump_full_sess_to_buffer(struct stream_interface *si, struct stream *sess)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005887{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005888 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005889 struct tm tm;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005890 extern const char *monthname[12];
5891 char pn[INET6_ADDRSTRLEN];
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005892 struct connection *conn;
Willy Tarreau284ddbf2013-12-01 20:45:00 +01005893 struct appctx *tmpctx;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005894
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005895 chunk_reset(&trash);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005896
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005897 if (appctx->ctx.sess.section > 0 && appctx->ctx.sess.uid != sess->uniq_id) {
Willy Tarreau87b09662015-04-03 00:22:06 +02005898 /* stream changed, no need to go any further */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005899 chunk_appendf(&trash, " *** session terminated while we were watching it ***\n");
Willy Tarreaubc18da12015-03-13 14:00:47 +01005900 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02005901 si_applet_cant_put(si);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005902 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01005903 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005904 appctx->ctx.sess.uid = 0;
5905 appctx->ctx.sess.section = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005906 return 1;
5907 }
5908
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005909 switch (appctx->ctx.sess.section) {
Willy Tarreau87b09662015-04-03 00:22:06 +02005910 case 0: /* main status of the stream */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01005911 appctx->ctx.sess.uid = sess->uniq_id;
5912 appctx->ctx.sess.section = 1;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005913 /* fall through */
5914
5915 case 1:
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005916 get_localtime(sess->logs.accept_date.tv_sec, &tm);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005917 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005918 "%p: [%02d/%s/%04d:%02d:%02d:%02d.%06d] id=%u proto=%s",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005919 sess,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01005920 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
5921 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(sess->logs.accept_date.tv_usec),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005922 sess->uniq_id,
Willy Tarreau666f5042015-06-17 19:49:52 +02005923 strm_li(sess) ? strm_li(sess)->proto->name : "?");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005924
Willy Tarreaud0d8da92015-04-04 02:10:38 +02005925 conn = objt_conn(strm_orig(sess));
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005926 switch (conn ? addr_to_str(&conn->addr.from, pn, sizeof(pn)) : AF_UNSPEC) {
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005927 case AF_INET:
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005928 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005929 chunk_appendf(&trash, " source=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005930 pn, get_host_port(&conn->addr.from));
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005931 break;
5932 case AF_UNIX:
Willy Tarreaud0d8da92015-04-04 02:10:38 +02005933 chunk_appendf(&trash, " source=unix:%d\n", strm_li(sess)->luid);
Emeric Brun837ca522010-10-22 16:19:01 +02005934 break;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005935 default:
5936 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005937 chunk_appendf(&trash, "\n");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005938 break;
5939 }
5940
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005941 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005942 " flags=0x%x, conn_retries=%d, srv_conn=%p, pend_pos=%p\n",
Willy Tarreauee28de02010-06-01 09:51:00 +02005943 sess->flags, sess->si[1].conn_retries, sess->srv_conn, sess->pend_pos);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005944
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005945 chunk_appendf(&trash,
Daniel Schultze90690c72012-03-23 10:53:36 -07005946 " frontend=%s (id=%u mode=%s), listener=%s (id=%u)",
Willy Tarreaud0d8da92015-04-04 02:10:38 +02005947 strm_fe(sess)->id, strm_fe(sess)->uuid, strm_fe(sess)->mode ? "http" : "tcp",
5948 strm_li(sess) ? strm_li(sess)->name ? strm_li(sess)->name : "?" : "?",
5949 strm_li(sess) ? strm_li(sess)->luid : 0);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01005950
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005951 if (conn)
5952 conn_get_to_addr(conn);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005953
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005954 switch (conn ? addr_to_str(&conn->addr.to, pn, sizeof(pn)) : AF_UNSPEC) {
Daniel Schultze90690c72012-03-23 10:53:36 -07005955 case AF_INET:
5956 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005957 chunk_appendf(&trash, " addr=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005958 pn, get_host_port(&conn->addr.to));
Daniel Schultze90690c72012-03-23 10:53:36 -07005959 break;
5960 case AF_UNIX:
Willy Tarreaud0d8da92015-04-04 02:10:38 +02005961 chunk_appendf(&trash, " addr=unix:%d\n", strm_li(sess)->luid);
Daniel Schultze90690c72012-03-23 10:53:36 -07005962 break;
5963 default:
5964 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005965 chunk_appendf(&trash, "\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07005966 break;
5967 }
5968
Willy Tarreau50943332011-09-02 17:33:05 +02005969 if (sess->be->cap & PR_CAP_BE)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005970 chunk_appendf(&trash,
Daniel Schultze90690c72012-03-23 10:53:36 -07005971 " backend=%s (id=%u mode=%s)",
Willy Tarreau50943332011-09-02 17:33:05 +02005972 sess->be->id,
Daniel Schultze90690c72012-03-23 10:53:36 -07005973 sess->be->uuid, sess->be->mode ? "http" : "tcp");
5974 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005975 chunk_appendf(&trash, " backend=<NONE> (id=-1 mode=-)");
Daniel Schultze90690c72012-03-23 10:53:36 -07005976
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005977 conn = objt_conn(sess->si[1].end);
5978 if (conn)
5979 conn_get_from_addr(conn);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02005980
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005981 switch (conn ? addr_to_str(&conn->addr.from, pn, sizeof(pn)) : AF_UNSPEC) {
Daniel Schultze90690c72012-03-23 10:53:36 -07005982 case AF_INET:
5983 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005984 chunk_appendf(&trash, " addr=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005985 pn, get_host_port(&conn->addr.from));
Daniel Schultze90690c72012-03-23 10:53:36 -07005986 break;
5987 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005988 chunk_appendf(&trash, " addr=unix\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07005989 break;
5990 default:
5991 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005992 chunk_appendf(&trash, "\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07005993 break;
5994 }
5995
5996 if (sess->be->cap & PR_CAP_BE)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005997 chunk_appendf(&trash,
Daniel Schultze90690c72012-03-23 10:53:36 -07005998 " server=%s (id=%u)",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005999 objt_server(sess->target) ? objt_server(sess->target)->id : "<none>",
6000 objt_server(sess->target) ? objt_server(sess->target)->puid : 0);
Willy Tarreau50943332011-09-02 17:33:05 +02006001 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006002 chunk_appendf(&trash, " server=<NONE> (id=-1)");
Daniel Schultze90690c72012-03-23 10:53:36 -07006003
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006004 if (conn)
6005 conn_get_to_addr(conn);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02006006
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006007 switch (conn ? addr_to_str(&conn->addr.to, pn, sizeof(pn)) : AF_UNSPEC) {
Daniel Schultze90690c72012-03-23 10:53:36 -07006008 case AF_INET:
6009 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006010 chunk_appendf(&trash, " addr=%s:%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006011 pn, get_host_port(&conn->addr.to));
Daniel Schultze90690c72012-03-23 10:53:36 -07006012 break;
6013 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006014 chunk_appendf(&trash, " addr=unix\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07006015 break;
6016 default:
6017 /* no more information to print right now */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006018 chunk_appendf(&trash, "\n");
Daniel Schultze90690c72012-03-23 10:53:36 -07006019 break;
6020 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006021
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006022 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01006023 " task=%p (state=0x%02x nice=%d calls=%d exp=%s%s",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006024 sess->task,
6025 sess->task->state,
6026 sess->task->nice, sess->task->calls,
6027 sess->task->expire ?
6028 tick_is_expired(sess->task->expire, now_ms) ? "<PAST>" :
6029 human_time(TICKS_TO_MS(sess->task->expire - now_ms),
6030 TICKS_TO_MS(1000)) : "<NEVER>",
6031 task_in_rq(sess->task) ? ", running" : "");
6032
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006033 chunk_appendf(&trash,
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01006034 " age=%s)\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006035 human_time(now.tv_sec - sess->logs.accept_date.tv_sec, 1));
6036
Willy Tarreaueee5b512015-04-03 23:46:31 +02006037 if (sess->txn)
6038 chunk_appendf(&trash,
Willy Tarreau98410192014-11-26 18:05:38 +01006039 " 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 +02006040 sess->txn, sess->txn->flags, sess->txn->meth, sess->txn->status,
6041 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 +01006042
6043 chunk_appendf(&trash,
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02006044 " si[0]=%p (state=%s flags=0x%02x endp0=%s:%p exp=%s, et=0x%03x)\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006045 &sess->si[0],
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01006046 si_state_str(sess->si[0].state),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006047 sess->si[0].flags,
Willy Tarreaucf644ed2013-09-29 17:19:56 +02006048 obj_type_name(sess->si[0].end),
6049 obj_base_ptr(sess->si[0].end),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006050 sess->si[0].exp ?
6051 tick_is_expired(sess->si[0].exp, now_ms) ? "<PAST>" :
6052 human_time(TICKS_TO_MS(sess->si[0].exp - now_ms),
6053 TICKS_TO_MS(1000)) : "<NEVER>",
6054 sess->si[0].err_type);
6055
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006056 chunk_appendf(&trash,
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02006057 " si[1]=%p (state=%s flags=0x%02x endp1=%s:%p exp=%s, et=0x%03x)\n",
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006058 &sess->si[1],
Willy Tarreau55e4ecd2012-12-08 17:48:47 +01006059 si_state_str(sess->si[1].state),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006060 sess->si[1].flags,
Willy Tarreaucf644ed2013-09-29 17:19:56 +02006061 obj_type_name(sess->si[1].end),
6062 obj_base_ptr(sess->si[1].end),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006063 sess->si[1].exp ?
6064 tick_is_expired(sess->si[1].exp, now_ms) ? "<PAST>" :
6065 human_time(TICKS_TO_MS(sess->si[1].exp - now_ms),
6066 TICKS_TO_MS(1000)) : "<NEVER>",
6067 sess->si[1].err_type);
6068
Willy Tarreau284ddbf2013-12-01 20:45:00 +01006069 if ((conn = objt_conn(sess->si[0].end)) != NULL) {
Willy Tarreaucf644ed2013-09-29 17:19:56 +02006070 chunk_appendf(&trash,
6071 " co0=%p ctrl=%s xprt=%s data=%s target=%s:%p\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006072 conn,
6073 get_conn_ctrl_name(conn),
6074 get_conn_xprt_name(conn),
6075 get_conn_data_name(conn),
6076 obj_type_name(conn->target),
6077 obj_base_ptr(conn->target));
Willy Tarreau909d5172012-11-26 03:04:41 +01006078
Willy Tarreaucf644ed2013-09-29 17:19:56 +02006079 chunk_appendf(&trash,
Willy Tarreau16f649c2014-01-25 19:10:48 +01006080 " flags=0x%08x fd=%d fd.state=%02x fd.cache=%d updt=%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006081 conn->flags,
6082 conn->t.sock.fd,
Willy Tarreau69a41fa2014-01-20 11:02:59 +01006083 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].state : 0,
Willy Tarreau15a4dec2014-01-20 11:09:39 +01006084 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].cache : 0,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006085 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].updated : 0);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02006086 }
Willy Tarreau284ddbf2013-12-01 20:45:00 +01006087 else if ((tmpctx = objt_appctx(sess->si[0].end)) != NULL) {
6088 chunk_appendf(&trash,
6089 " app0=%p st0=%d st1=%d st2=%d applet=%s\n",
6090 tmpctx,
6091 tmpctx->st0,
6092 tmpctx->st1,
6093 tmpctx->st2,
6094 tmpctx->applet->name);
6095 }
Willy Tarreaubc174aa2012-11-19 16:10:32 +01006096
Willy Tarreau284ddbf2013-12-01 20:45:00 +01006097 if ((conn = objt_conn(sess->si[1].end)) != NULL) {
Willy Tarreaucf644ed2013-09-29 17:19:56 +02006098 chunk_appendf(&trash,
6099 " co1=%p ctrl=%s xprt=%s data=%s target=%s:%p\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006100 conn,
6101 get_conn_ctrl_name(conn),
6102 get_conn_xprt_name(conn),
6103 get_conn_data_name(conn),
6104 obj_type_name(conn->target),
6105 obj_base_ptr(conn->target));
Willy Tarreau909d5172012-11-26 03:04:41 +01006106
Willy Tarreaucf644ed2013-09-29 17:19:56 +02006107 chunk_appendf(&trash,
Willy Tarreauceeafb52016-01-25 15:27:17 +01006108 " flags=0x%08x fd=%d fd.state=%02x fd.cache=%d updt=%d\n",
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006109 conn->flags,
6110 conn->t.sock.fd,
Willy Tarreau69a41fa2014-01-20 11:02:59 +01006111 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].state : 0,
Willy Tarreau15a4dec2014-01-20 11:09:39 +01006112 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].cache : 0,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006113 conn->t.sock.fd >= 0 ? fdtab[conn->t.sock.fd].updated : 0);
Willy Tarreaucf644ed2013-09-29 17:19:56 +02006114 }
Willy Tarreau284ddbf2013-12-01 20:45:00 +01006115 else if ((tmpctx = objt_appctx(sess->si[1].end)) != NULL) {
6116 chunk_appendf(&trash,
6117 " app1=%p st0=%d st1=%d st2=%d applet=%s\n",
6118 tmpctx,
6119 tmpctx->st0,
6120 tmpctx->st1,
6121 tmpctx->st2,
6122 tmpctx->applet->name);
6123 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006124
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006125 chunk_appendf(&trash,
Willy Tarreau909d5172012-11-26 03:04:41 +01006126 " req=%p (f=0x%06x an=0x%x pipe=%d tofwd=%d total=%lld)\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006127 " an_exp=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006128 &sess->req,
6129 sess->req.flags, sess->req.analysers,
6130 sess->req.pipe ? sess->req.pipe->data : 0,
6131 sess->req.to_forward, sess->req.total,
6132 sess->req.analyse_exp ?
6133 human_time(TICKS_TO_MS(sess->req.analyse_exp - now_ms),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006134 TICKS_TO_MS(1000)) : "<NEVER>");
6135
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006136 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006137 " rex=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006138 sess->req.rex ?
6139 human_time(TICKS_TO_MS(sess->req.rex - now_ms),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006140 TICKS_TO_MS(1000)) : "<NEVER>");
6141
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006142 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006143 " wex=%s\n"
Willy Tarreau909d5172012-11-26 03:04:41 +01006144 " buf=%p data=%p o=%d p=%d req.next=%d i=%d size=%d\n",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006145 sess->req.wex ?
6146 human_time(TICKS_TO_MS(sess->req.wex - now_ms),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006147 TICKS_TO_MS(1000)) : "<NEVER>",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006148 sess->req.buf,
6149 sess->req.buf->data, sess->req.buf->o,
6150 (int)(sess->req.buf->p - sess->req.buf->data),
Willy Tarreaueee5b512015-04-03 23:46:31 +02006151 sess->txn ? sess->txn->req.next : 0, sess->req.buf->i,
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006152 sess->req.buf->size);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006153
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006154 chunk_appendf(&trash,
Willy Tarreau909d5172012-11-26 03:04:41 +01006155 " res=%p (f=0x%06x an=0x%x pipe=%d tofwd=%d total=%lld)\n"
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006156 " an_exp=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006157 &sess->res,
6158 sess->res.flags, sess->res.analysers,
6159 sess->res.pipe ? sess->res.pipe->data : 0,
6160 sess->res.to_forward, sess->res.total,
6161 sess->res.analyse_exp ?
6162 human_time(TICKS_TO_MS(sess->res.analyse_exp - now_ms),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006163 TICKS_TO_MS(1000)) : "<NEVER>");
6164
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006165 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006166 " rex=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006167 sess->res.rex ?
6168 human_time(TICKS_TO_MS(sess->res.rex - now_ms),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006169 TICKS_TO_MS(1000)) : "<NEVER>");
6170
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006171 chunk_appendf(&trash,
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006172 " wex=%s\n"
Willy Tarreau909d5172012-11-26 03:04:41 +01006173 " buf=%p data=%p o=%d p=%d rsp.next=%d i=%d size=%d\n",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006174 sess->res.wex ?
6175 human_time(TICKS_TO_MS(sess->res.wex - now_ms),
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006176 TICKS_TO_MS(1000)) : "<NEVER>",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006177 sess->res.buf,
6178 sess->res.buf->data, sess->res.buf->o,
6179 (int)(sess->res.buf->p - sess->res.buf->data),
Willy Tarreaueee5b512015-04-03 23:46:31 +02006180 sess->txn ? sess->txn->rsp.next : 0, sess->res.buf->i,
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006181 sess->res.buf->size);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006182
Willy Tarreaubc18da12015-03-13 14:00:47 +01006183 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02006184 si_applet_cant_put(si);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006185 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01006186 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006187
6188 /* use other states to dump the contents */
6189 }
6190 /* end of dump */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006191 appctx->ctx.sess.uid = 0;
6192 appctx->ctx.sess.section = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006193 return 1;
6194}
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006195
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02006196#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
6197static int stats_tlskeys_list(struct stream_interface *si) {
6198 struct appctx *appctx = __objt_appctx(si->end);
6199
6200 switch (appctx->st2) {
6201 case STAT_ST_INIT:
6202 /* Display the column headers. If the message cannot be sent,
6203 * quit the fucntion with returning 0. The function is called
6204 * later and restart at the state "STAT_ST_INIT".
6205 */
6206 chunk_reset(&trash);
William Lallemand1d0b36a2016-05-20 17:40:26 +02006207
6208 if (appctx->st0 == STAT_CLI_O_TLSK_ENT)
6209 chunk_appendf(&trash, "# id secret\n");
6210 else
6211 chunk_appendf(&trash, "# id (file)\n");
6212
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02006213 if (bi_putchk(si_ic(si), &trash) == -1) {
6214 si_applet_cant_put(si);
6215 return 0;
6216 }
6217
William Lallemand72a8a182016-06-14 18:58:55 +02006218 appctx->ctx.tlskeys.dump_keys_index = 0;
William Lallemand1d0b36a2016-05-20 17:40:26 +02006219
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02006220 /* Now, we start the browsing of the references lists.
6221 * Note that the following call to LIST_ELEM return bad pointer. The only
Cyril Bontédb98eb32016-05-06 12:18:50 +02006222 * available field of this pointer is <list>. It is used with the function
6223 * tlskeys_list_get_next() for retruning the first available entry
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02006224 */
William Lallemandcf9e7882016-06-14 17:45:18 +02006225 if (appctx->ctx.tlskeys.ref == NULL) {
6226 appctx->ctx.tlskeys.ref = LIST_ELEM(&tlskeys_reference, struct tls_keys_ref *, list);
6227 appctx->ctx.tlskeys.ref = tlskeys_list_get_next(appctx->ctx.tlskeys.ref, &tlskeys_reference);
William Lallemand1d0b36a2016-05-20 17:40:26 +02006228 }
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02006229
6230 appctx->st2 = STAT_ST_LIST;
6231 /* fall through */
6232
6233 case STAT_ST_LIST:
William Lallemandcf9e7882016-06-14 17:45:18 +02006234 while (appctx->ctx.tlskeys.ref) {
William Lallemandcf9e7882016-06-14 17:45:18 +02006235 int head = appctx->ctx.tlskeys.ref->tls_ticket_enc_index;
William Lallemand1d0b36a2016-05-20 17:40:26 +02006236
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02006237 chunk_reset(&trash);
William Lallemand72a8a182016-06-14 18:58:55 +02006238 if (appctx->st0 == STAT_CLI_O_TLSK_ENT && appctx->ctx.tlskeys.dump_keys_index == 0)
William Lallemand1d0b36a2016-05-20 17:40:26 +02006239 chunk_appendf(&trash, "# ");
William Lallemand72a8a182016-06-14 18:58:55 +02006240 if (appctx->ctx.tlskeys.dump_keys_index == 0)
6241 chunk_appendf(&trash, "%d (%s)\n", appctx->ctx.tlskeys.ref->unique_id,
6242 appctx->ctx.tlskeys.ref->filename);
William Lallemand1d0b36a2016-05-20 17:40:26 +02006243 if (appctx->st0 == STAT_CLI_O_TLSK_ENT) {
William Lallemand72a8a182016-06-14 18:58:55 +02006244 while (appctx->ctx.tlskeys.dump_keys_index < TLS_TICKETS_NO) {
William Lallemand1d0b36a2016-05-20 17:40:26 +02006245 struct chunk *t2 = get_trash_chunk();
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02006246
William Lallemand1d0b36a2016-05-20 17:40:26 +02006247 chunk_reset(t2);
William Lallemand72a8a182016-06-14 18:58:55 +02006248 /* should never fail here because we dump only a key in the t2 buffer */
6249 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 +02006250 sizeof(struct tls_sess_key), t2->str, t2->size);
William Lallemand72a8a182016-06-14 18:58:55 +02006251 chunk_appendf(&trash, "%d.%d %s\n", appctx->ctx.tlskeys.ref->unique_id, appctx->ctx.tlskeys.dump_keys_index, t2->str);
6252
6253 if (bi_putchk(si_ic(si), &trash) == -1) {
6254 /* let's try again later from this stream. We add ourselves into
6255 * this stream's users so that it can remove us upon termination.
6256 */
6257 si_applet_cant_put(si);
William Lallemand1d0b36a2016-05-20 17:40:26 +02006258 return 0;
William Lallemand72a8a182016-06-14 18:58:55 +02006259 }
6260 appctx->ctx.tlskeys.dump_keys_index++;
William Lallemand1d0b36a2016-05-20 17:40:26 +02006261 }
William Lallemand72a8a182016-06-14 18:58:55 +02006262 appctx->ctx.tlskeys.dump_keys_index = 0;
William Lallemand1d0b36a2016-05-20 17:40:26 +02006263 }
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02006264 if (bi_putchk(si_ic(si), &trash) == -1) {
6265 /* let's try again later from this stream. We add ourselves into
6266 * this stream's users so that it can remove us upon termination.
6267 */
6268 si_applet_cant_put(si);
6269 return 0;
6270 }
6271
William Lallemandcf9e7882016-06-14 17:45:18 +02006272 if (appctx->ctx.tlskeys.dump_all == 0) /* don't display everything if not necessary */
William Lallemand1d0b36a2016-05-20 17:40:26 +02006273 break;
6274
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02006275 /* get next list entry and check the end of the list */
William Lallemandcf9e7882016-06-14 17:45:18 +02006276 appctx->ctx.tlskeys.ref = tlskeys_list_get_next(appctx->ctx.tlskeys.ref, &tlskeys_reference);
William Lallemand1d0b36a2016-05-20 17:40:26 +02006277
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02006278 }
6279
6280 appctx->st2 = STAT_ST_FIN;
6281 /* fall through */
6282
6283 default:
6284 appctx->st2 = STAT_ST_FIN;
6285 return 1;
6286 }
6287 return 0;
6288}
6289#endif
6290
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006291static int stats_pats_list(struct stream_interface *si)
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006292{
6293 struct appctx *appctx = __objt_appctx(si->end);
6294
6295 switch (appctx->st2) {
6296 case STAT_ST_INIT:
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01006297 /* Display the column headers. If the message cannot be sent,
6298 * quit the fucntion with returning 0. The function is called
6299 * later and restart at the state "STAT_ST_INIT".
6300 */
6301 chunk_reset(&trash);
Thierry FOURNIER65ce6132014-03-20 11:42:45 +01006302 chunk_appendf(&trash, "# id (file) description\n");
Willy Tarreaubc18da12015-03-13 14:00:47 +01006303 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02006304 si_applet_cant_put(si);
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01006305 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01006306 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006307
6308 /* Now, we start the browsing of the references lists.
6309 * Note that the following call to LIST_ELEM return bad pointer. The only
Cyril Bontédb98eb32016-05-06 12:18:50 +02006310 * available field of this pointer is <list>. It is used with the function
6311 * pat_list_get_next() for retruning the first available entry
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006312 */
6313 appctx->ctx.map.ref = LIST_ELEM(&pattern_reference, struct pat_ref *, list);
6314 appctx->ctx.map.ref = pat_list_get_next(appctx->ctx.map.ref, &pattern_reference,
6315 appctx->ctx.map.display_flags);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006316 appctx->st2 = STAT_ST_LIST;
6317 /* fall through */
6318
6319 case STAT_ST_LIST:
6320 while (appctx->ctx.map.ref) {
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006321 chunk_reset(&trash);
6322
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006323 /* Build messages. If the reference is used by another category than
6324 * the listed categorie, display the information in the massage.
6325 */
Thierry FOURNIERf7e04e92014-03-20 11:45:47 +01006326 chunk_appendf(&trash, "%d (%s) %s\n", appctx->ctx.map.ref->unique_id,
Thierry FOURNIER0d6ba512014-02-11 03:31:34 +01006327 appctx->ctx.map.ref->reference ? appctx->ctx.map.ref->reference : "",
6328 appctx->ctx.map.ref->display);
Thierry FOURNIERaf5a29d2014-03-11 14:29:22 +01006329
Willy Tarreau2bb4a962014-11-28 11:11:05 +01006330 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau87b09662015-04-03 00:22:06 +02006331 /* let's try again later from this stream. We add ourselves into
6332 * this stream's users so that it can remove us upon termination.
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006333 */
Willy Tarreaufe127932015-04-21 19:23:39 +02006334 si_applet_cant_put(si);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006335 return 0;
6336 }
6337
6338 /* get next list entry and check the end of the list */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006339 appctx->ctx.map.ref = pat_list_get_next(appctx->ctx.map.ref, &pattern_reference,
6340 appctx->ctx.map.display_flags);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006341 }
6342
6343 appctx->st2 = STAT_ST_FIN;
6344 /* fall through */
6345
6346 default:
6347 appctx->st2 = STAT_ST_FIN;
6348 return 1;
6349 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006350 return 0;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006351}
6352
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006353static int stats_map_lookup(struct stream_interface *si)
6354{
6355 struct appctx *appctx = __objt_appctx(si->end);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006356 struct sample sample;
6357 struct pattern *pat;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006358 int match_method;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006359
6360 switch (appctx->st2) {
6361 case STAT_ST_INIT:
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006362 /* Init to the first entry. The list cannot be change */
Thierry FOURNIERc5959fd2014-01-20 14:29:33 +01006363 appctx->ctx.map.expr = LIST_ELEM(&appctx->ctx.map.ref->pat, struct pattern_expr *, list);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006364 appctx->ctx.map.expr = pat_expr_get_next(appctx->ctx.map.expr, &appctx->ctx.map.ref->pat);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006365 appctx->st2 = STAT_ST_LIST;
6366 /* fall through */
6367
6368 case STAT_ST_LIST:
6369 /* for each lookup type */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006370 while (appctx->ctx.map.expr) {
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006371 /* initialise chunk to build new message */
6372 chunk_reset(&trash);
6373
6374 /* execute pattern matching */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02006375 sample.data.type = SMP_T_STR;
Andreas Seltenreich9727cf42016-03-03 19:32:25 +01006376 sample.flags = SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02006377 sample.data.u.str.len = appctx->ctx.map.chunk.len;
6378 sample.data.u.str.str = appctx->ctx.map.chunk.str;
Thierry FOURNIER5d344082014-01-27 14:19:53 +01006379 if (appctx->ctx.map.expr->pat_head->match &&
6380 sample_convert(&sample, appctx->ctx.map.expr->pat_head->expect_type))
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006381 pat = appctx->ctx.map.expr->pat_head->match(&sample, appctx->ctx.map.expr, 1);
6382 else
6383 pat = NULL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006384
6385 /* build return message: set type of match */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006386 for (match_method=0; match_method<PAT_MATCH_NUM; match_method++)
6387 if (appctx->ctx.map.expr->pat_head->match == pat_match_fcts[match_method])
6388 break;
6389 if (match_method >= PAT_MATCH_NUM)
6390 chunk_appendf(&trash, "type=unknown(%p)", appctx->ctx.map.expr->pat_head->match);
6391 else
6392 chunk_appendf(&trash, "type=%s", pat_match_names[match_method]);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006393
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02006394 /* case sensitive */
6395 if (appctx->ctx.map.expr->mflags & PAT_MF_IGNORE_CASE)
6396 chunk_appendf(&trash, ", case=insensitive");
6397 else
6398 chunk_appendf(&trash, ", case=sensitive");
6399
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006400 /* Display no match, and set default value */
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01006401 if (!pat) {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006402 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
6403 chunk_appendf(&trash, ", found=no");
6404 else
6405 chunk_appendf(&trash, ", match=no");
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006406 }
6407
6408 /* Display match and match info */
6409 else {
6410 /* display match */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006411 if (appctx->ctx.map.display_flags == PAT_REF_MAP)
6412 chunk_appendf(&trash, ", found=yes");
6413 else
6414 chunk_appendf(&trash, ", match=yes");
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006415
Thierry FOURNIER1794fdf2014-01-17 15:25:13 +01006416 /* display index mode */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02006417 if (pat->sflags & PAT_SF_TREE)
Thierry FOURNIERb9903842014-03-11 18:48:17 +01006418 chunk_appendf(&trash, ", idx=tree");
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006419 else
Thierry FOURNIERb9903842014-03-11 18:48:17 +01006420 chunk_appendf(&trash, ", idx=list");
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006421
Thierry FOURNIER86db66a2014-03-11 18:37:58 +01006422 /* display pattern */
6423 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
6424 if (pat->ref && pat->ref->pattern)
6425 chunk_appendf(&trash, ", key=\"%s\"", pat->ref->pattern);
6426 else
6427 chunk_appendf(&trash, ", key=unknown");
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006428 }
6429 else {
Thierry FOURNIER86db66a2014-03-11 18:37:58 +01006430 if (pat->ref && pat->ref->pattern)
6431 chunk_appendf(&trash, ", pattern=\"%s\"", pat->ref->pattern);
6432 else
6433 chunk_appendf(&trash, ", pattern=unknown");
6434 }
6435
6436 /* display return value */
6437 if (appctx->ctx.map.display_flags == PAT_REF_MAP) {
Thierry FOURNIER503bb092015-08-19 08:35:43 +02006438 if (pat->data && pat->ref && pat->ref->sample)
Thierry FOURNIER03d0e452015-07-23 18:33:41 +02006439 chunk_appendf(&trash, ", value=\"%s\", type=\"%s\"", pat->ref->sample,
Thierry FOURNIER7d4335c2015-08-19 09:05:25 +02006440 smp_to_type[pat->data->type]);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006441 else
Thierry FOURNIER86db66a2014-03-11 18:37:58 +01006442 chunk_appendf(&trash, ", value=none");
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006443 }
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006444 }
6445
Thierry FOURNIERb9903842014-03-11 18:48:17 +01006446 chunk_appendf(&trash, "\n");
6447
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006448 /* display response */
Willy Tarreau2bb4a962014-11-28 11:11:05 +01006449 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau87b09662015-04-03 00:22:06 +02006450 /* let's try again later from this stream. We add ourselves into
6451 * this stream's users so that it can remove us upon termination.
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006452 */
Willy Tarreaufe127932015-04-21 19:23:39 +02006453 si_applet_cant_put(si);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006454 return 0;
6455 }
6456
6457 /* get next entry */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006458 appctx->ctx.map.expr = pat_expr_get_next(appctx->ctx.map.expr,
6459 &appctx->ctx.map.ref->pat);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006460 }
6461
6462 appctx->st2 = STAT_ST_FIN;
6463 /* fall through */
6464
6465 default:
6466 appctx->st2 = STAT_ST_FIN;
6467 free(appctx->ctx.map.chunk.str);
6468 return 1;
6469 }
6470}
6471
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006472static int stats_pat_list(struct stream_interface *si)
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006473{
6474 struct appctx *appctx = __objt_appctx(si->end);
6475
6476 switch (appctx->st2) {
6477
6478 case STAT_ST_INIT:
6479 /* Init to the first entry. The list cannot be change */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006480 appctx->ctx.map.elt = LIST_NEXT(&appctx->ctx.map.ref->head,
6481 struct pat_ref_elt *, list);
6482 if (&appctx->ctx.map.elt->list == &appctx->ctx.map.ref->head)
6483 appctx->ctx.map.elt = NULL;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006484 appctx->st2 = STAT_ST_LIST;
6485 /* fall through */
6486
6487 case STAT_ST_LIST:
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006488 while (appctx->ctx.map.elt) {
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006489 chunk_reset(&trash);
6490
6491 /* build messages */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006492 if (appctx->ctx.map.elt->sample)
Thierry FOURNIER9356c682014-01-28 15:55:37 +01006493 chunk_appendf(&trash, "%p %s %s\n",
6494 appctx->ctx.map.elt, appctx->ctx.map.elt->pattern,
6495 appctx->ctx.map.elt->sample);
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006496 else
Thierry FOURNIER9356c682014-01-28 15:55:37 +01006497 chunk_appendf(&trash, "%p %s\n",
6498 appctx->ctx.map.elt, appctx->ctx.map.elt->pattern);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006499
Willy Tarreau2bb4a962014-11-28 11:11:05 +01006500 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau87b09662015-04-03 00:22:06 +02006501 /* let's try again later from this stream. We add ourselves into
6502 * this stream's users so that it can remove us upon termination.
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006503 */
Willy Tarreaufe127932015-04-21 19:23:39 +02006504 si_applet_cant_put(si);
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006505 return 0;
6506 }
6507
6508 /* get next list entry and check the end of the list */
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006509 appctx->ctx.map.elt = LIST_NEXT(&appctx->ctx.map.elt->list,
6510 struct pat_ref_elt *, list);
6511 if (&appctx->ctx.map.elt->list == &appctx->ctx.map.ref->head)
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +01006512 break;
6513 }
6514
6515 appctx->st2 = STAT_ST_FIN;
6516 /* fall through */
6517
6518 default:
6519 appctx->st2 = STAT_ST_FIN;
6520 return 1;
6521 }
6522}
6523
Willy Tarreau87b09662015-04-03 00:22:06 +02006524/* This function dumps all streams' states onto the stream interface's
Willy Tarreau306f8302013-07-08 15:53:06 +02006525 * read buffer. It returns 0 if the output buffer is full and it needs
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01006526 * to be called again, otherwise non-zero. It is designed to be called
6527 * from stats_dump_sess_to_buffer() below.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006528 */
Simon Horman9bd2c732011-06-15 15:18:44 +09006529static int stats_dump_sess_to_buffer(struct stream_interface *si)
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006530{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006531 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006532 struct connection *conn;
6533
Willy Tarreau2bb4a962014-11-28 11:11:05 +01006534 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW))) {
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006535 /* If we're forced to shut down, we might have to remove our
Willy Tarreau87b09662015-04-03 00:22:06 +02006536 * reference to the last stream being dumped.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006537 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006538 if (appctx->st2 == STAT_ST_LIST) {
6539 if (!LIST_ISEMPTY(&appctx->ctx.sess.bref.users)) {
6540 LIST_DEL(&appctx->ctx.sess.bref.users);
6541 LIST_INIT(&appctx->ctx.sess.bref.users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +01006542 }
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006543 }
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02006544 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006545 }
6546
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006547 chunk_reset(&trash);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006548
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006549 switch (appctx->st2) {
Willy Tarreau295a8372011-03-10 11:25:07 +01006550 case STAT_ST_INIT:
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006551 /* the function had not been called yet, let's prepare the
Willy Tarreau87b09662015-04-03 00:22:06 +02006552 * buffer for a response. We initialize the current stream
Willy Tarreaufd3828e2009-02-22 15:17:24 +01006553 * pointer to the first in the global list. When a target
Willy Tarreau87b09662015-04-03 00:22:06 +02006554 * stream is being destroyed, it is responsible for updating
Willy Tarreaufd3828e2009-02-22 15:17:24 +01006555 * this pointer. We know we have reached the end when this
Willy Tarreau87b09662015-04-03 00:22:06 +02006556 * pointer points back to the head of the streams list.
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006557 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006558 LIST_INIT(&appctx->ctx.sess.bref.users);
Willy Tarreau87b09662015-04-03 00:22:06 +02006559 appctx->ctx.sess.bref.ref = streams.n;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006560 appctx->st2 = STAT_ST_LIST;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006561 /* fall through */
6562
Willy Tarreau295a8372011-03-10 11:25:07 +01006563 case STAT_ST_LIST:
Willy Tarreau87b09662015-04-03 00:22:06 +02006564 /* first, let's detach the back-ref from a possible previous stream */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006565 if (!LIST_ISEMPTY(&appctx->ctx.sess.bref.users)) {
6566 LIST_DEL(&appctx->ctx.sess.bref.users);
6567 LIST_INIT(&appctx->ctx.sess.bref.users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +01006568 }
6569
6570 /* and start from where we stopped */
Willy Tarreau87b09662015-04-03 00:22:06 +02006571 while (appctx->ctx.sess.bref.ref != &streams) {
Cyril Bontéacd7d632010-11-01 19:26:02 +01006572 char pn[INET6_ADDRSTRLEN];
Willy Tarreau87b09662015-04-03 00:22:06 +02006573 struct stream *curr_sess;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006574
Willy Tarreau87b09662015-04-03 00:22:06 +02006575 curr_sess = LIST_ELEM(appctx->ctx.sess.bref.ref, struct stream *, list);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006576
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006577 if (appctx->ctx.sess.target) {
6578 if (appctx->ctx.sess.target != (void *)-1 && appctx->ctx.sess.target != curr_sess)
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006579 goto next_sess;
6580
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006581 LIST_ADDQ(&curr_sess->back_refs, &appctx->ctx.sess.bref.users);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006582 /* call the proper dump() function and return if we're missing space */
Willy Tarreau76153662012-11-26 01:16:39 +01006583 if (!stats_dump_full_sess_to_buffer(si, curr_sess))
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006584 return 0;
6585
Willy Tarreau87b09662015-04-03 00:22:06 +02006586 /* stream dump complete */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006587 LIST_DEL(&appctx->ctx.sess.bref.users);
6588 LIST_INIT(&appctx->ctx.sess.bref.users);
6589 if (appctx->ctx.sess.target != (void *)-1) {
6590 appctx->ctx.sess.target = NULL;
Willy Tarreau76153662012-11-26 01:16:39 +01006591 break;
6592 }
6593 else
6594 goto next_sess;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006595 }
6596
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006597 chunk_appendf(&trash,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006598 "%p: proto=%s",
6599 curr_sess,
Willy Tarreauf1e02122015-09-23 12:16:43 +02006600 strm_li(curr_sess) ? strm_li(curr_sess)->proto->name : "?");
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006601
Willy Tarreaud0d8da92015-04-04 02:10:38 +02006602 conn = objt_conn(strm_orig(curr_sess));
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006603 switch (conn ? addr_to_str(&conn->addr.from, pn, sizeof(pn)) : AF_UNSPEC) {
Willy Tarreau631f01c2011-09-05 00:36:48 +02006604 case AF_INET:
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006605 case AF_INET6:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006606 chunk_appendf(&trash,
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006607 " src=%s:%d fe=%s be=%s srv=%s",
6608 pn,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006609 get_host_port(&conn->addr.from),
Willy Tarreaud0d8da92015-04-04 02:10:38 +02006610 strm_fe(curr_sess)->id,
Willy Tarreau50943332011-09-02 17:33:05 +02006611 (curr_sess->be->cap & PR_CAP_BE) ? curr_sess->be->id : "<NONE>",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006612 objt_server(curr_sess->target) ? objt_server(curr_sess->target)->id : "<none>"
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006613 );
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006614 break;
6615 case AF_UNIX:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006616 chunk_appendf(&trash,
Emeric Brun837ca522010-10-22 16:19:01 +02006617 " src=unix:%d fe=%s be=%s srv=%s",
Willy Tarreaud0d8da92015-04-04 02:10:38 +02006618 strm_li(curr_sess)->luid,
6619 strm_fe(curr_sess)->id,
Willy Tarreau50943332011-09-02 17:33:05 +02006620 (curr_sess->be->cap & PR_CAP_BE) ? curr_sess->be->id : "<NONE>",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006621 objt_server(curr_sess->target) ? objt_server(curr_sess->target)->id : "<none>"
Emeric Brun837ca522010-10-22 16:19:01 +02006622 );
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006623 break;
6624 }
6625
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006626 chunk_appendf(&trash,
Willy Tarreau65671ab2009-10-04 14:24:59 +02006627 " ts=%02x age=%s calls=%d",
6628 curr_sess->task->state,
Willy Tarreau3884cba2009-03-28 17:54:35 +01006629 human_time(now.tv_sec - curr_sess->logs.tv_accept.tv_sec, 1),
6630 curr_sess->task->calls);
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006631
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006632 chunk_appendf(&trash,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01006633 " rq[f=%06xh,i=%d,an=%02xh,rx=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006634 curr_sess->req.flags,
6635 curr_sess->req.buf->i,
6636 curr_sess->req.analysers,
6637 curr_sess->req.rex ?
6638 human_time(TICKS_TO_MS(curr_sess->req.rex - now_ms),
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006639 TICKS_TO_MS(1000)) : "");
6640
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006641 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006642 ",wx=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006643 curr_sess->req.wex ?
6644 human_time(TICKS_TO_MS(curr_sess->req.wex - now_ms),
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006645 TICKS_TO_MS(1000)) : "");
6646
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006647 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006648 ",ax=%s]",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006649 curr_sess->req.analyse_exp ?
6650 human_time(TICKS_TO_MS(curr_sess->req.analyse_exp - now_ms),
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006651 TICKS_TO_MS(1000)) : "");
6652
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006653 chunk_appendf(&trash,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01006654 " rp[f=%06xh,i=%d,an=%02xh,rx=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006655 curr_sess->res.flags,
6656 curr_sess->res.buf->i,
6657 curr_sess->res.analysers,
6658 curr_sess->res.rex ?
6659 human_time(TICKS_TO_MS(curr_sess->res.rex - now_ms),
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006660 TICKS_TO_MS(1000)) : "");
6661
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006662 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006663 ",wx=%s",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006664 curr_sess->res.wex ?
6665 human_time(TICKS_TO_MS(curr_sess->res.wex - now_ms),
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006666 TICKS_TO_MS(1000)) : "");
6667
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006668 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006669 ",ax=%s]",
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01006670 curr_sess->res.analyse_exp ?
6671 human_time(TICKS_TO_MS(curr_sess->res.analyse_exp - now_ms),
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006672 TICKS_TO_MS(1000)) : "");
6673
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006674 conn = objt_conn(curr_sess->si[0].end);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006675 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006676 " s0=[%d,%1xh,fd=%d,ex=%s]",
6677 curr_sess->si[0].state,
6678 curr_sess->si[0].flags,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006679 conn ? conn->t.sock.fd : -1,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006680 curr_sess->si[0].exp ?
6681 human_time(TICKS_TO_MS(curr_sess->si[0].exp - now_ms),
6682 TICKS_TO_MS(1000)) : "");
6683
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006684 conn = objt_conn(curr_sess->si[1].end);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006685 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006686 " s1=[%d,%1xh,fd=%d,ex=%s]",
6687 curr_sess->si[1].state,
6688 curr_sess->si[1].flags,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006689 conn ? conn->t.sock.fd : -1,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006690 curr_sess->si[1].exp ?
6691 human_time(TICKS_TO_MS(curr_sess->si[1].exp - now_ms),
6692 TICKS_TO_MS(1000)) : "");
6693
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006694 chunk_appendf(&trash,
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006695 " exp=%s",
6696 curr_sess->task->expire ?
6697 human_time(TICKS_TO_MS(curr_sess->task->expire - now_ms),
6698 TICKS_TO_MS(1000)) : "");
Willy Tarreau4726f532009-03-07 17:25:21 +01006699 if (task_in_rq(curr_sess->task))
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006700 chunk_appendf(&trash, " run(nice=%d)", curr_sess->task->nice);
Willy Tarreauc6dcad62009-03-29 00:18:14 +01006701
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006702 chunk_appendf(&trash, "\n");
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006703
Willy Tarreau2bb4a962014-11-28 11:11:05 +01006704 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau87b09662015-04-03 00:22:06 +02006705 /* let's try again later from this stream. We add ourselves into
6706 * this stream's users so that it can remove us upon termination.
Willy Tarreaufd3828e2009-02-22 15:17:24 +01006707 */
Willy Tarreaufe127932015-04-21 19:23:39 +02006708 si_applet_cant_put(si);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006709 LIST_ADDQ(&curr_sess->back_refs, &appctx->ctx.sess.bref.users);
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02006710 return 0;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006711 }
6712
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006713 next_sess:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006714 appctx->ctx.sess.bref.ref = curr_sess->list.n;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006715 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006716
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006717 if (appctx->ctx.sess.target && appctx->ctx.sess.target != (void *)-1) {
Willy Tarreau87b09662015-04-03 00:22:06 +02006718 /* specified stream not found */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006719 if (appctx->ctx.sess.section > 0)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006720 chunk_appendf(&trash, " *** session terminated while we were watching it ***\n");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006721 else
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006722 chunk_appendf(&trash, "Session not found.\n");
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006723
Willy Tarreaubc18da12015-03-13 14:00:47 +01006724 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02006725 si_applet_cant_put(si);
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006726 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01006727 }
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006728
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006729 appctx->ctx.sess.target = NULL;
6730 appctx->ctx.sess.uid = 0;
Willy Tarreau66dc20a2010-03-05 17:53:32 +01006731 return 1;
6732 }
6733
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006734 appctx->st2 = STAT_ST_FIN;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006735 /* fall through */
6736
6737 default:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006738 appctx->st2 = STAT_ST_FIN;
Willy Tarreau7e72a8f2009-10-03 23:55:14 +02006739 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006740 }
Emeric Brun1e029aa2010-09-23 18:12:53 +02006741}
6742
Willy Tarreau5f9a8772012-11-26 02:22:40 +01006743/* This is called when the stream interface is closed. For instance, upon an
6744 * external abort, we won't call the i/o handler anymore so we may need to
Willy Tarreau87b09662015-04-03 00:22:06 +02006745 * remove back references to the stream currently being dumped.
Willy Tarreau5f9a8772012-11-26 02:22:40 +01006746 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02006747static void cli_release_handler(struct appctx *appctx)
Willy Tarreau5f9a8772012-11-26 02:22:40 +01006748{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006749 if (appctx->st0 == STAT_CLI_O_SESS && appctx->st2 == STAT_ST_LIST) {
6750 if (!LIST_ISEMPTY(&appctx->ctx.sess.bref.users))
6751 LIST_DEL(&appctx->ctx.sess.bref.users);
Willy Tarreau5f9a8772012-11-26 02:22:40 +01006752 }
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01006753 else if (appctx->st0 == STAT_CLI_PRINT_FREE) {
6754 free(appctx->ctx.cli.err);
Willy Tarreau6457d0f2015-10-07 20:00:24 +02006755 appctx->ctx.cli.err = NULL;
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +01006756 }
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006757 else if (appctx->st0 == STAT_CLI_O_MLOOK) {
6758 free(appctx->ctx.map.chunk.str);
Willy Tarreau6457d0f2015-10-07 20:00:24 +02006759 appctx->ctx.map.chunk.str = NULL;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +01006760 }
Willy Tarreau5f9a8772012-11-26 02:22:40 +01006761}
6762
Willy Tarreau20e99322013-04-13 09:22:25 +02006763/* This function is used to either dump tables states (when action is set
6764 * to STAT_CLI_O_TAB) or clear tables (when action is STAT_CLI_O_CLR).
Willy Tarreau20e99322013-04-13 09:22:25 +02006765 * It returns 0 if the output buffer is full and it needs to be called
6766 * again, otherwise non-zero.
Willy Tarreau69f58c82010-07-12 17:55:33 +02006767 */
Willy Tarreau20e99322013-04-13 09:22:25 +02006768static int stats_table_request(struct stream_interface *si, int action)
Willy Tarreau69f58c82010-07-12 17:55:33 +02006769{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006770 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau87b09662015-04-03 00:22:06 +02006771 struct stream *s = si_strm(si);
Willy Tarreau69f58c82010-07-12 17:55:33 +02006772 struct ebmb_node *eb;
6773 int dt;
Willy Tarreau44455022012-12-05 23:01:12 +01006774 int skip_entry;
Willy Tarreau20e99322013-04-13 09:22:25 +02006775 int show = action == STAT_CLI_O_TAB;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006776
6777 /*
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006778 * We have 3 possible states in appctx->st2 :
Willy Tarreau295a8372011-03-10 11:25:07 +01006779 * - STAT_ST_INIT : the first call
6780 * - STAT_ST_INFO : the proxy pointer points to the next table to
Willy Tarreau69f58c82010-07-12 17:55:33 +02006781 * dump, the entry pointer is NULL ;
Willy Tarreau295a8372011-03-10 11:25:07 +01006782 * - STAT_ST_LIST : the proxy pointer points to the current table
Willy Tarreau69f58c82010-07-12 17:55:33 +02006783 * and the entry pointer points to the next entry to be dumped,
6784 * and the refcount on the next entry is held ;
Willy Tarreau295a8372011-03-10 11:25:07 +01006785 * - STAT_ST_END : nothing left to dump, the buffer may contain some
Willy Tarreau69f58c82010-07-12 17:55:33 +02006786 * data though.
6787 */
6788
Willy Tarreau2bb4a962014-11-28 11:11:05 +01006789 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW))) {
Willy Tarreau69f58c82010-07-12 17:55:33 +02006790 /* in case of abort, remove any refcount we might have set on an entry */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006791 if (appctx->st2 == STAT_ST_LIST) {
6792 appctx->ctx.table.entry->ref_cnt--;
6793 stksess_kill_if_expired(&appctx->ctx.table.proxy->table, appctx->ctx.table.entry);
Willy Tarreauf6efda12010-08-03 20:34:06 +02006794 }
Willy Tarreau69f58c82010-07-12 17:55:33 +02006795 return 1;
6796 }
6797
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006798 chunk_reset(&trash);
Willy Tarreau69f58c82010-07-12 17:55:33 +02006799
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006800 while (appctx->st2 != STAT_ST_FIN) {
6801 switch (appctx->st2) {
Willy Tarreau295a8372011-03-10 11:25:07 +01006802 case STAT_ST_INIT:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006803 appctx->ctx.table.proxy = appctx->ctx.table.target;
6804 if (!appctx->ctx.table.proxy)
6805 appctx->ctx.table.proxy = proxy;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006806
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006807 appctx->ctx.table.entry = NULL;
6808 appctx->st2 = STAT_ST_INFO;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006809 break;
6810
Willy Tarreau295a8372011-03-10 11:25:07 +01006811 case STAT_ST_INFO:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006812 if (!appctx->ctx.table.proxy ||
6813 (appctx->ctx.table.target &&
6814 appctx->ctx.table.proxy != appctx->ctx.table.target)) {
6815 appctx->st2 = STAT_ST_END;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006816 break;
6817 }
6818
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006819 if (appctx->ctx.table.proxy->table.size) {
6820 if (show && !stats_dump_table_head_to_buffer(&trash, si, appctx->ctx.table.proxy,
6821 appctx->ctx.table.target))
Willy Tarreau69f58c82010-07-12 17:55:33 +02006822 return 0;
6823
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006824 if (appctx->ctx.table.target &&
Willy Tarreaud0d8da92015-04-04 02:10:38 +02006825 strm_li(s)->bind_conf->level >= ACCESS_LVL_OPER) {
Willy Tarreau69f58c82010-07-12 17:55:33 +02006826 /* dump entries only if table explicitly requested */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006827 eb = ebmb_first(&appctx->ctx.table.proxy->table.keys);
Willy Tarreau69f58c82010-07-12 17:55:33 +02006828 if (eb) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006829 appctx->ctx.table.entry = ebmb_entry(eb, struct stksess, key);
6830 appctx->ctx.table.entry->ref_cnt++;
6831 appctx->st2 = STAT_ST_LIST;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006832 break;
6833 }
6834 }
6835 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006836 appctx->ctx.table.proxy = appctx->ctx.table.proxy->next;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006837 break;
6838
Willy Tarreau295a8372011-03-10 11:25:07 +01006839 case STAT_ST_LIST:
Willy Tarreau44455022012-12-05 23:01:12 +01006840 skip_entry = 0;
Simon Hormanc88b8872011-06-15 15:18:49 +09006841
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006842 if (appctx->ctx.table.data_type >= 0) {
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02006843 /* we're filtering on some data contents */
6844 void *ptr;
6845 long long data;
6846
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006847 dt = appctx->ctx.table.data_type;
6848 ptr = stktable_data_ptr(&appctx->ctx.table.proxy->table,
6849 appctx->ctx.table.entry,
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02006850 dt);
6851
6852 data = 0;
6853 switch (stktable_data_types[dt].std_type) {
6854 case STD_T_SINT:
6855 data = stktable_data_cast(ptr, std_t_sint);
6856 break;
6857 case STD_T_UINT:
6858 data = stktable_data_cast(ptr, std_t_uint);
6859 break;
6860 case STD_T_ULL:
6861 data = stktable_data_cast(ptr, std_t_ull);
6862 break;
6863 case STD_T_FRQP:
6864 data = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006865 appctx->ctx.table.proxy->table.data_arg[dt].u);
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02006866 break;
6867 }
6868
6869 /* skip the entry if the data does not match the test and the value */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006870 if ((data < appctx->ctx.table.value &&
6871 (appctx->ctx.table.data_op == STD_OP_EQ ||
6872 appctx->ctx.table.data_op == STD_OP_GT ||
6873 appctx->ctx.table.data_op == STD_OP_GE)) ||
6874 (data == appctx->ctx.table.value &&
6875 (appctx->ctx.table.data_op == STD_OP_NE ||
6876 appctx->ctx.table.data_op == STD_OP_GT ||
6877 appctx->ctx.table.data_op == STD_OP_LT)) ||
6878 (data > appctx->ctx.table.value &&
6879 (appctx->ctx.table.data_op == STD_OP_EQ ||
6880 appctx->ctx.table.data_op == STD_OP_LT ||
6881 appctx->ctx.table.data_op == STD_OP_LE)))
Willy Tarreau44455022012-12-05 23:01:12 +01006882 skip_entry = 1;
Willy Tarreau4f3f01f2010-07-18 11:46:20 +02006883 }
6884
Simon Hormanc88b8872011-06-15 15:18:49 +09006885 if (show && !skip_entry &&
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006886 !stats_dump_table_entry_to_buffer(&trash, si, appctx->ctx.table.proxy,
6887 appctx->ctx.table.entry))
Simon Hormand9366582011-06-15 15:18:45 +09006888 return 0;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006889
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006890 appctx->ctx.table.entry->ref_cnt--;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006891
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006892 eb = ebmb_next(&appctx->ctx.table.entry->key);
Willy Tarreau69f58c82010-07-12 17:55:33 +02006893 if (eb) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006894 struct stksess *old = appctx->ctx.table.entry;
6895 appctx->ctx.table.entry = ebmb_entry(eb, struct stksess, key);
Willy Tarreau8fa52f42012-01-09 11:50:03 +01006896 if (show)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006897 stksess_kill_if_expired(&appctx->ctx.table.proxy->table, old);
6898 else if (!skip_entry && !appctx->ctx.table.entry->ref_cnt)
6899 stksess_kill(&appctx->ctx.table.proxy->table, old);
6900 appctx->ctx.table.entry->ref_cnt++;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006901 break;
6902 }
6903
Simon Hormanc88b8872011-06-15 15:18:49 +09006904
6905 if (show)
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006906 stksess_kill_if_expired(&appctx->ctx.table.proxy->table, appctx->ctx.table.entry);
6907 else if (!skip_entry && !appctx->ctx.table.entry->ref_cnt)
6908 stksess_kill(&appctx->ctx.table.proxy->table, appctx->ctx.table.entry);
Simon Hormanc88b8872011-06-15 15:18:49 +09006909
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006910 appctx->ctx.table.proxy = appctx->ctx.table.proxy->next;
6911 appctx->st2 = STAT_ST_INFO;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006912 break;
6913
Willy Tarreau295a8372011-03-10 11:25:07 +01006914 case STAT_ST_END:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01006915 appctx->st2 = STAT_ST_FIN;
Willy Tarreau69f58c82010-07-12 17:55:33 +02006916 break;
6917 }
6918 }
6919 return 1;
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01006920}
6921
Willy Tarreaud426a182010-03-05 14:58:26 +01006922/* print a line of text buffer (limited to 70 bytes) to <out>. The format is :
Willy Tarreau74808cb2009-03-04 15:53:18 +01006923 * <2 spaces> <offset=5 digits> <space or plus> <space> <70 chars max> <\n>
6924 * which is 60 chars per line. Non-printable chars \t, \n, \r and \e are
6925 * encoded in C format. Other non-printable chars are encoded "\xHH". Original
6926 * lines are respected within the limit of 70 output chars. Lines that are
6927 * continuation of a previous truncated line begin with "+" instead of " "
6928 * after the offset. The new pointer is returned.
6929 */
Willy Tarreaud426a182010-03-05 14:58:26 +01006930static int dump_text_line(struct chunk *out, const char *buf, int bsize, int len,
6931 int *line, int ptr)
Willy Tarreau74808cb2009-03-04 15:53:18 +01006932{
6933 int end;
6934 unsigned char c;
6935
6936 end = out->len + 80;
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02006937 if (end > out->size)
Willy Tarreau74808cb2009-03-04 15:53:18 +01006938 return ptr;
6939
Willy Tarreau77804732012-10-29 16:14:26 +01006940 chunk_appendf(out, " %05d%c ", ptr, (ptr == *line) ? ' ' : '+');
Willy Tarreau74808cb2009-03-04 15:53:18 +01006941
Willy Tarreaud426a182010-03-05 14:58:26 +01006942 while (ptr < len && ptr < bsize) {
6943 c = buf[ptr];
Willy Tarreau787bbd92009-03-12 08:18:33 +01006944 if (isprint(c) && isascii(c) && c != '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006945 if (out->len > end - 2)
6946 break;
6947 out->str[out->len++] = c;
Willy Tarreau787bbd92009-03-12 08:18:33 +01006948 } else if (c == '\t' || c == '\n' || c == '\r' || c == '\e' || c == '\\') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006949 if (out->len > end - 3)
6950 break;
6951 out->str[out->len++] = '\\';
6952 switch (c) {
6953 case '\t': c = 't'; break;
6954 case '\n': c = 'n'; break;
6955 case '\r': c = 'r'; break;
6956 case '\e': c = 'e'; break;
Willy Tarreau787bbd92009-03-12 08:18:33 +01006957 case '\\': c = '\\'; break;
Willy Tarreau74808cb2009-03-04 15:53:18 +01006958 }
6959 out->str[out->len++] = c;
6960 } else {
6961 if (out->len > end - 5)
6962 break;
6963 out->str[out->len++] = '\\';
6964 out->str[out->len++] = 'x';
6965 out->str[out->len++] = hextab[(c >> 4) & 0xF];
6966 out->str[out->len++] = hextab[c & 0xF];
6967 }
Willy Tarreaud426a182010-03-05 14:58:26 +01006968 if (buf[ptr++] == '\n') {
Willy Tarreau74808cb2009-03-04 15:53:18 +01006969 /* we had a line break, let's return now */
6970 out->str[out->len++] = '\n';
6971 *line = ptr;
6972 return ptr;
6973 }
6974 }
6975 /* we have an incomplete line, we return it as-is */
6976 out->str[out->len++] = '\n';
6977 return ptr;
6978}
6979
Baptiste Assmann3863f972015-05-17 00:33:24 +02006980/* This function dumps counters from all resolvers section and associated name servers.
6981 * It returns 0 if the output buffer is full and it needs
6982 * to be called again, otherwise non-zero.
6983 */
6984static int stats_dump_resolvers_to_buffer(struct stream_interface *si)
6985{
6986 struct appctx *appctx = __objt_appctx(si->end);
6987 struct dns_resolvers *presolvers;
6988 struct dns_nameserver *pnameserver;
6989
6990 chunk_reset(&trash);
6991
6992 switch (appctx->st2) {
6993 case STAT_ST_INIT:
6994 appctx->st2 = STAT_ST_LIST; /* let's start producing data */
6995 /* fall through */
6996
6997 case STAT_ST_LIST:
Andrew Hayworth68d05342015-10-02 20:33:01 +00006998 if (LIST_ISEMPTY(&dns_resolvers)) {
6999 chunk_appendf(&trash, "No resolvers found\n");
7000 }
7001 else {
7002 list_for_each_entry(presolvers, &dns_resolvers, list) {
7003 if (appctx->ctx.resolvers.ptr != NULL && appctx->ctx.resolvers.ptr != presolvers)
7004 continue;
7005
7006 chunk_appendf(&trash, "Resolvers section %s\n", presolvers->id);
7007 list_for_each_entry(pnameserver, &presolvers->nameserver_list, list) {
7008 chunk_appendf(&trash, " nameserver %s:\n", pnameserver->id);
7009 chunk_appendf(&trash, " sent: %ld\n", pnameserver->counters.sent);
7010 chunk_appendf(&trash, " valid: %ld\n", pnameserver->counters.valid);
7011 chunk_appendf(&trash, " update: %ld\n", pnameserver->counters.update);
7012 chunk_appendf(&trash, " cname: %ld\n", pnameserver->counters.cname);
7013 chunk_appendf(&trash, " cname_error: %ld\n", pnameserver->counters.cname_error);
7014 chunk_appendf(&trash, " any_err: %ld\n", pnameserver->counters.any_err);
7015 chunk_appendf(&trash, " nx: %ld\n", pnameserver->counters.nx);
7016 chunk_appendf(&trash, " timeout: %ld\n", pnameserver->counters.timeout);
7017 chunk_appendf(&trash, " refused: %ld\n", pnameserver->counters.refused);
7018 chunk_appendf(&trash, " other: %ld\n", pnameserver->counters.other);
7019 chunk_appendf(&trash, " invalid: %ld\n", pnameserver->counters.invalid);
7020 chunk_appendf(&trash, " too_big: %ld\n", pnameserver->counters.too_big);
7021 chunk_appendf(&trash, " truncated: %ld\n", pnameserver->counters.truncated);
7022 chunk_appendf(&trash, " outdated: %ld\n", pnameserver->counters.outdated);
7023 }
7024 }
Baptiste Assmann3863f972015-05-17 00:33:24 +02007025 }
7026
7027 /* display response */
7028 if (bi_putchk(si_ic(si), &trash) == -1) {
7029 /* let's try again later from this session. We add ourselves into
7030 * this session's users so that it can remove us upon termination.
7031 */
7032 si->flags |= SI_FL_WAIT_ROOM;
7033 return 0;
7034 }
7035
7036 appctx->st2 = STAT_ST_FIN;
7037 /* fall through */
7038
7039 default:
7040 appctx->st2 = STAT_ST_FIN;
7041 return 1;
7042 }
7043}
7044
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02007045/* This function dumps all captured errors onto the stream interface's
Willy Tarreau306f8302013-07-08 15:53:06 +02007046 * read buffer. It returns 0 if the output buffer is full and it needs
Willy Tarreau5ec29ff2011-02-13 15:27:22 +01007047 * to be called again, otherwise non-zero.
Willy Tarreau74808cb2009-03-04 15:53:18 +01007048 */
Simon Horman9bd2c732011-06-15 15:18:44 +09007049static int stats_dump_errors_to_buffer(struct stream_interface *si)
Willy Tarreau74808cb2009-03-04 15:53:18 +01007050{
Willy Tarreau7b4b4992013-12-01 09:15:12 +01007051 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau74808cb2009-03-04 15:53:18 +01007052 extern const char *monthname[12];
Willy Tarreau74808cb2009-03-04 15:53:18 +01007053
Willy Tarreau2bb4a962014-11-28 11:11:05 +01007054 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
Willy Tarreau61b34732009-10-03 23:49:35 +02007055 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01007056
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007057 chunk_reset(&trash);
Willy Tarreau74808cb2009-03-04 15:53:18 +01007058
Willy Tarreau7b4b4992013-12-01 09:15:12 +01007059 if (!appctx->ctx.errors.px) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01007060 /* the function had not been called yet, let's prepare the
7061 * buffer for a response.
7062 */
Willy Tarreau10479e42010-12-12 14:00:34 +01007063 struct tm tm;
7064
7065 get_localtime(date.tv_sec, &tm);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007066 chunk_appendf(&trash, "Total events captured on [%02d/%s/%04d:%02d:%02d:%02d.%03d] : %u\n",
Willy Tarreau10479e42010-12-12 14:00:34 +01007067 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
7068 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(date.tv_usec/1000),
7069 error_snapshot_id);
7070
Willy Tarreau2bb4a962014-11-28 11:11:05 +01007071 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau10479e42010-12-12 14:00:34 +01007072 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreaufe127932015-04-21 19:23:39 +02007073 si_applet_cant_put(si);
Willy Tarreau10479e42010-12-12 14:00:34 +01007074 return 0;
7075 }
7076
Willy Tarreau7b4b4992013-12-01 09:15:12 +01007077 appctx->ctx.errors.px = proxy;
7078 appctx->ctx.errors.buf = 0;
7079 appctx->ctx.errors.bol = 0;
7080 appctx->ctx.errors.ptr = -1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01007081 }
7082
7083 /* we have two inner loops here, one for the proxy, the other one for
7084 * the buffer.
7085 */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01007086 while (appctx->ctx.errors.px) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01007087 struct error_snapshot *es;
7088
Willy Tarreau7b4b4992013-12-01 09:15:12 +01007089 if (appctx->ctx.errors.buf == 0)
7090 es = &appctx->ctx.errors.px->invalid_req;
Willy Tarreau74808cb2009-03-04 15:53:18 +01007091 else
Willy Tarreau7b4b4992013-12-01 09:15:12 +01007092 es = &appctx->ctx.errors.px->invalid_rep;
Willy Tarreau74808cb2009-03-04 15:53:18 +01007093
7094 if (!es->when.tv_sec)
7095 goto next;
7096
Willy Tarreau7b4b4992013-12-01 09:15:12 +01007097 if (appctx->ctx.errors.iid >= 0 &&
7098 appctx->ctx.errors.px->uuid != appctx->ctx.errors.iid &&
7099 es->oe->uuid != appctx->ctx.errors.iid)
Willy Tarreau74808cb2009-03-04 15:53:18 +01007100 goto next;
7101
Willy Tarreau7b4b4992013-12-01 09:15:12 +01007102 if (appctx->ctx.errors.ptr < 0) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01007103 /* just print headers now */
7104
7105 char pn[INET6_ADDRSTRLEN];
7106 struct tm tm;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02007107 int port;
Willy Tarreau74808cb2009-03-04 15:53:18 +01007108
7109 get_localtime(es->when.tv_sec, &tm);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007110 chunk_appendf(&trash, " \n[%02d/%s/%04d:%02d:%02d:%02d.%03d]",
Willy Tarreau74808cb2009-03-04 15:53:18 +01007111 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
Willy Tarreau1772ece2009-04-03 14:49:12 +02007112 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(es->when.tv_usec/1000));
Willy Tarreau74808cb2009-03-04 15:53:18 +01007113
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02007114 switch (addr_to_str(&es->src, pn, sizeof(pn))) {
7115 case AF_INET:
7116 case AF_INET6:
7117 port = get_host_port(&es->src);
7118 break;
7119 default:
7120 port = 0;
7121 }
7122
Willy Tarreau7b4b4992013-12-01 09:15:12 +01007123 switch (appctx->ctx.errors.buf) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01007124 case 0:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007125 chunk_appendf(&trash,
Willy Tarreau74808cb2009-03-04 15:53:18 +01007126 " frontend %s (#%d): invalid request\n"
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02007127 " backend %s (#%d)",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01007128 appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02007129 (es->oe->cap & PR_CAP_BE) ? es->oe->id : "<NONE>",
7130 (es->oe->cap & PR_CAP_BE) ? es->oe->uuid : -1);
Willy Tarreau74808cb2009-03-04 15:53:18 +01007131 break;
7132 case 1:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007133 chunk_appendf(&trash,
Olivier Doucet08afdcb2014-09-08 11:23:00 +02007134 " backend %s (#%d): invalid response\n"
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02007135 " frontend %s (#%d)",
Willy Tarreau7b4b4992013-12-01 09:15:12 +01007136 appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02007137 es->oe->id, es->oe->uuid);
Willy Tarreau74808cb2009-03-04 15:53:18 +01007138 break;
7139 }
7140
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007141 chunk_appendf(&trash,
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02007142 ", server %s (#%d), event #%u\n"
7143 " src %s:%d, session #%d, session flags 0x%08x\n"
7144 " HTTP msg state %d, msg flags 0x%08x, tx flags 0x%08x\n"
7145 " HTTP chunk len %lld bytes, HTTP body len %lld bytes\n"
7146 " buffer flags 0x%08x, out %d bytes, total %lld bytes\n"
7147 " pending %d bytes, wrapping at %d, error at position %d:\n \n",
7148 es->srv ? es->srv->id : "<NONE>", es->srv ? es->srv->puid : -1,
7149 es->ev_id,
7150 pn, port, es->sid, es->s_flags,
7151 es->state, es->m_flags, es->t_flags,
7152 es->m_clen, es->m_blen,
7153 es->b_flags, es->b_out, es->b_tot,
7154 es->len, es->b_wrap, es->pos);
7155
Willy Tarreau2bb4a962014-11-28 11:11:05 +01007156 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01007157 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreaufe127932015-04-21 19:23:39 +02007158 si_applet_cant_put(si);
Willy Tarreau61b34732009-10-03 23:49:35 +02007159 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01007160 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01007161 appctx->ctx.errors.ptr = 0;
7162 appctx->ctx.errors.sid = es->sid;
Willy Tarreau74808cb2009-03-04 15:53:18 +01007163 }
7164
Willy Tarreau7b4b4992013-12-01 09:15:12 +01007165 if (appctx->ctx.errors.sid != es->sid) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01007166 /* the snapshot changed while we were dumping it */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007167 chunk_appendf(&trash,
Willy Tarreau74808cb2009-03-04 15:53:18 +01007168 " WARNING! update detected on this snapshot, dump interrupted. Please re-check!\n");
Willy Tarreaubc18da12015-03-13 14:00:47 +01007169 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaufe127932015-04-21 19:23:39 +02007170 si_applet_cant_put(si);
Willy Tarreau61b34732009-10-03 23:49:35 +02007171 return 0;
Willy Tarreaubc18da12015-03-13 14:00:47 +01007172 }
Willy Tarreau74808cb2009-03-04 15:53:18 +01007173 goto next;
7174 }
7175
7176 /* OK, ptr >= 0, so we have to dump the current line */
Willy Tarreauf3764b72016-03-31 13:45:10 +02007177 while (es->buf && appctx->ctx.errors.ptr < es->len && appctx->ctx.errors.ptr < global.tune.bufsize) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01007178 int newptr;
7179 int newline;
7180
Willy Tarreau7b4b4992013-12-01 09:15:12 +01007181 newline = appctx->ctx.errors.bol;
Willy Tarreauf3764b72016-03-31 13:45:10 +02007182 newptr = dump_text_line(&trash, es->buf, global.tune.bufsize, es->len, &newline, appctx->ctx.errors.ptr);
Willy Tarreau7b4b4992013-12-01 09:15:12 +01007183 if (newptr == appctx->ctx.errors.ptr)
Willy Tarreau61b34732009-10-03 23:49:35 +02007184 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01007185
Willy Tarreau2bb4a962014-11-28 11:11:05 +01007186 if (bi_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau74808cb2009-03-04 15:53:18 +01007187 /* Socket buffer full. Let's try again later from the same point */
Willy Tarreaufe127932015-04-21 19:23:39 +02007188 si_applet_cant_put(si);
Willy Tarreau61b34732009-10-03 23:49:35 +02007189 return 0;
Willy Tarreau74808cb2009-03-04 15:53:18 +01007190 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01007191 appctx->ctx.errors.ptr = newptr;
7192 appctx->ctx.errors.bol = newline;
Willy Tarreau74808cb2009-03-04 15:53:18 +01007193 };
7194 next:
Willy Tarreau7b4b4992013-12-01 09:15:12 +01007195 appctx->ctx.errors.bol = 0;
7196 appctx->ctx.errors.ptr = -1;
7197 appctx->ctx.errors.buf++;
7198 if (appctx->ctx.errors.buf > 1) {
7199 appctx->ctx.errors.buf = 0;
7200 appctx->ctx.errors.px = appctx->ctx.errors.px->next;
Willy Tarreau74808cb2009-03-04 15:53:18 +01007201 }
Willy Tarreauae795722016-02-16 11:27:28 +01007202 }
7203
7204 /* dump complete */
7205 return 1;
7206}
7207
7208/* This function dumps all environmnent variables to the buffer. It returns 0
7209 * if the output buffer is full and it needs to be called again, otherwise
7210 * non-zero. Dumps only one entry if st2 == STAT_ST_END.
7211 */
7212static int stats_dump_env_to_buffer(struct stream_interface *si)
7213{
7214 struct appctx *appctx = __objt_appctx(si->end);
7215
7216 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
7217 return 1;
7218
7219 chunk_reset(&trash);
7220
7221 /* we have two inner loops here, one for the proxy, the other one for
7222 * the buffer.
7223 */
7224 while (*appctx->ctx.env.var) {
7225 chunk_printf(&trash, "%s\n", *appctx->ctx.env.var);
7226
7227 if (bi_putchk(si_ic(si), &trash) == -1) {
7228 si_applet_cant_put(si);
7229 return 0;
7230 }
7231 if (appctx->st2 == STAT_ST_END)
7232 break;
7233 appctx->ctx.env.var++;
Willy Tarreau74808cb2009-03-04 15:53:18 +01007234 }
7235
7236 /* dump complete */
Willy Tarreau61b34732009-10-03 23:49:35 +02007237 return 1;
Willy Tarreau74808cb2009-03-04 15:53:18 +01007238}
7239
Willy Tarreaud5781202012-09-22 19:32:35 +02007240/* parse the "level" argument on the bind lines */
7241static int bind_parse_level(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
7242{
7243 if (!*args[cur_arg + 1]) {
7244 memprintf(err, "'%s' : missing level", args[cur_arg]);
7245 return ERR_ALERT | ERR_FATAL;
7246 }
7247
7248 if (!strcmp(args[cur_arg+1], "user"))
7249 conf->level = ACCESS_LVL_USER;
7250 else if (!strcmp(args[cur_arg+1], "operator"))
7251 conf->level = ACCESS_LVL_OPER;
7252 else if (!strcmp(args[cur_arg+1], "admin"))
7253 conf->level = ACCESS_LVL_ADMIN;
7254 else {
7255 memprintf(err, "'%s' only supports 'user', 'operator', and 'admin' (got '%s')",
7256 args[cur_arg], args[cur_arg+1]);
7257 return ERR_ALERT | ERR_FATAL;
7258 }
7259
7260 return 0;
7261}
7262
Willy Tarreau30576452015-04-13 13:50:30 +02007263struct applet http_stats_applet = {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007264 .obj_type = OBJ_TYPE_APPLET,
Willy Tarreaub24281b2011-02-13 13:16:36 +01007265 .name = "<STATS>", /* used for logging */
7266 .fct = http_stats_io_handler,
Aman Gupta9a13e842012-04-02 18:57:53 -07007267 .release = NULL,
Willy Tarreaub24281b2011-02-13 13:16:36 +01007268};
7269
Willy Tarreau30576452015-04-13 13:50:30 +02007270static struct applet cli_applet = {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007271 .obj_type = OBJ_TYPE_APPLET,
Willy Tarreaub24281b2011-02-13 13:16:36 +01007272 .name = "<CLI>", /* used for logging */
7273 .fct = cli_io_handler,
Willy Tarreau5f9a8772012-11-26 02:22:40 +01007274 .release = cli_release_handler,
Willy Tarreaub24281b2011-02-13 13:16:36 +01007275};
Willy Tarreau3dfe6cd2008-12-07 22:29:48 +01007276
Willy Tarreaudc13c112013-06-21 23:16:39 +02007277static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreau10522fd2008-07-09 20:12:41 +02007278 { CFG_GLOBAL, "stats", stats_parse_global },
7279 { 0, NULL, NULL },
7280}};
7281
Willy Tarreaud5781202012-09-22 19:32:35 +02007282static struct bind_kw_list bind_kws = { "STAT", { }, {
7283 { "level", bind_parse_level, 1 }, /* set the unix socket admin level */
7284 { NULL, NULL, 0 },
7285}};
7286
Willy Tarreau10522fd2008-07-09 20:12:41 +02007287__attribute__((constructor))
7288static void __dumpstats_module_init(void)
7289{
7290 cfg_register_keywords(&cfg_kws);
Willy Tarreaud5781202012-09-22 19:32:35 +02007291 bind_register_keywords(&bind_kws);
Willy Tarreau10522fd2008-07-09 20:12:41 +02007292}
7293
Willy Tarreau91861262007-10-17 17:06:05 +02007294/*
7295 * Local variables:
7296 * c-indent-level: 8
7297 * c-basic-offset: 8
7298 * End:
7299 */