blob: 91b7f3db81db5358ab9e29481a3f9e1318a039bf [file] [log] [blame]
William Lallemand74c24fb2016-11-21 17:18:36 +01001/*
2 * Functions dedicated to statistics output and the stats socket
3 *
4 * Copyright 2000-2012 Willy Tarreau <w@1wt.eu>
5 * Copyright 2007-2009 Krzysztof Piotr Oledzki <ole@ans.pl>
6 *
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>
20#include <pwd.h>
21#include <grp.h>
22
23#include <sys/socket.h>
24#include <sys/stat.h>
25#include <sys/types.h>
26
27#include <common/cfgparse.h>
28#include <common/compat.h>
29#include <common/config.h>
30#include <common/debug.h>
Willy Tarreau35b51c62018-09-10 15:38:55 +020031#include <common/http.h>
Willy Tarreaub96b77e2018-12-11 10:22:41 +010032#include <common/htx.h>
Willy Tarreau0108d902018-11-25 19:14:37 +010033#include <common/initcall.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010034#include <common/memory.h>
35#include <common/mini-clist.h>
36#include <common/standard.h>
37#include <common/ticks.h>
38#include <common/time.h>
39#include <common/uri_auth.h>
40#include <common/version.h>
41#include <common/base64.h>
42
43#include <types/applet.h>
Willy Tarreau2b812e22016-11-22 16:18:05 +010044#include <types/cli.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010045#include <types/global.h>
46#include <types/dns.h>
William Lallemand9ed62032016-11-21 17:49:11 +010047#include <types/stats.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010048
49#include <proto/backend.h>
50#include <proto/channel.h>
51#include <proto/checks.h>
Willy Tarreau2b812e22016-11-22 16:18:05 +010052#include <proto/cli.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010053#include <proto/compression.h>
Baptiste Assmann333939c2019-01-21 08:34:50 +010054#include <proto/dns.h>
William Lallemand9ed62032016-11-21 17:49:11 +010055#include <proto/stats.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010056#include <proto/fd.h>
57#include <proto/freq_ctr.h>
58#include <proto/frontend.h>
Christopher Fauleted7a0662019-01-14 11:07:34 +010059#include <proto/http_htx.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010060#include <proto/log.h>
61#include <proto/pattern.h>
62#include <proto/pipe.h>
63#include <proto/listener.h>
64#include <proto/map.h>
Christopher Fauletef779222018-10-31 08:47:01 +010065#include <proto/proto_http.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010066#include <proto/proxy.h>
67#include <proto/sample.h>
68#include <proto/session.h>
Willy Tarreauc125cef2019-05-10 09:58:43 +020069#include <proto/ssl_sock.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010070#include <proto/stream.h>
71#include <proto/server.h>
72#include <proto/raw_sock.h>
73#include <proto/stream_interface.h>
74#include <proto/task.h>
75
William Lallemand74c24fb2016-11-21 17:18:36 +010076
Christopher Faulet2b9b6782019-02-27 16:42:58 +010077/* status codes available for the stats admin page (strictly 4 chars length) */
78const char *stat_status_codes[STAT_STATUS_SIZE] = {
79 [STAT_STATUS_DENY] = "DENY",
80 [STAT_STATUS_DONE] = "DONE",
81 [STAT_STATUS_ERRP] = "ERRP",
82 [STAT_STATUS_EXCD] = "EXCD",
83 [STAT_STATUS_NONE] = "NONE",
84 [STAT_STATUS_PART] = "PART",
85 [STAT_STATUS_UNKN] = "UNKN",
86 [STAT_STATUS_IVAL] = "IVAL",
87};
88
Willy Tarreau0baac8c2016-11-22 16:36:53 +010089/* These are the field names for each INF_* field position. Please pay attention
90 * to always use the exact same name except that the strings for new names must
91 * be lower case or CamelCase while the enum entries must be upper case.
92 */
93const char *info_field_names[INF_TOTAL_FIELDS] = {
94 [INF_NAME] = "Name",
95 [INF_VERSION] = "Version",
96 [INF_RELEASE_DATE] = "Release_date",
Yves Lafon95317282018-02-26 11:10:37 +010097 [INF_NBTHREAD] = "Nbthread",
Willy Tarreau0baac8c2016-11-22 16:36:53 +010098 [INF_NBPROC] = "Nbproc",
99 [INF_PROCESS_NUM] = "Process_num",
100 [INF_PID] = "Pid",
101 [INF_UPTIME] = "Uptime",
102 [INF_UPTIME_SEC] = "Uptime_sec",
103 [INF_MEMMAX_MB] = "Memmax_MB",
104 [INF_POOL_ALLOC_MB] = "PoolAlloc_MB",
105 [INF_POOL_USED_MB] = "PoolUsed_MB",
106 [INF_POOL_FAILED] = "PoolFailed",
107 [INF_ULIMIT_N] = "Ulimit-n",
108 [INF_MAXSOCK] = "Maxsock",
109 [INF_MAXCONN] = "Maxconn",
110 [INF_HARD_MAXCONN] = "Hard_maxconn",
111 [INF_CURR_CONN] = "CurrConns",
112 [INF_CUM_CONN] = "CumConns",
113 [INF_CUM_REQ] = "CumReq",
114 [INF_MAX_SSL_CONNS] = "MaxSslConns",
115 [INF_CURR_SSL_CONNS] = "CurrSslConns",
116 [INF_CUM_SSL_CONNS] = "CumSslConns",
117 [INF_MAXPIPES] = "Maxpipes",
118 [INF_PIPES_USED] = "PipesUsed",
119 [INF_PIPES_FREE] = "PipesFree",
120 [INF_CONN_RATE] = "ConnRate",
121 [INF_CONN_RATE_LIMIT] = "ConnRateLimit",
122 [INF_MAX_CONN_RATE] = "MaxConnRate",
123 [INF_SESS_RATE] = "SessRate",
124 [INF_SESS_RATE_LIMIT] = "SessRateLimit",
125 [INF_MAX_SESS_RATE] = "MaxSessRate",
126 [INF_SSL_RATE] = "SslRate",
127 [INF_SSL_RATE_LIMIT] = "SslRateLimit",
128 [INF_MAX_SSL_RATE] = "MaxSslRate",
129 [INF_SSL_FRONTEND_KEY_RATE] = "SslFrontendKeyRate",
130 [INF_SSL_FRONTEND_MAX_KEY_RATE] = "SslFrontendMaxKeyRate",
131 [INF_SSL_FRONTEND_SESSION_REUSE_PCT] = "SslFrontendSessionReuse_pct",
132 [INF_SSL_BACKEND_KEY_RATE] = "SslBackendKeyRate",
133 [INF_SSL_BACKEND_MAX_KEY_RATE] = "SslBackendMaxKeyRate",
134 [INF_SSL_CACHE_LOOKUPS] = "SslCacheLookups",
135 [INF_SSL_CACHE_MISSES] = "SslCacheMisses",
136 [INF_COMPRESS_BPS_IN] = "CompressBpsIn",
137 [INF_COMPRESS_BPS_OUT] = "CompressBpsOut",
138 [INF_COMPRESS_BPS_RATE_LIM] = "CompressBpsRateLim",
139 [INF_ZLIB_MEM_USAGE] = "ZlibMemUsage",
140 [INF_MAX_ZLIB_MEM_USAGE] = "MaxZlibMemUsage",
141 [INF_TASKS] = "Tasks",
142 [INF_RUN_QUEUE] = "Run_queue",
143 [INF_IDLE_PCT] = "Idle_pct",
144 [INF_NODE] = "node",
145 [INF_DESCRIPTION] = "description",
Willy Tarreau00098ea2018-11-05 14:38:13 +0100146 [INF_STOPPING] = "Stopping",
147 [INF_JOBS] = "Jobs",
William Lallemanda7199262018-11-16 16:57:20 +0100148 [INF_UNSTOPPABLE_JOBS] = "Unstoppable Jobs",
Willy Tarreau00098ea2018-11-05 14:38:13 +0100149 [INF_LISTENERS] = "Listeners",
Willy Tarreau199ad242018-11-05 16:31:22 +0100150 [INF_ACTIVE_PEERS] = "ActivePeers",
Willy Tarreau2d372c22018-11-05 17:12:27 +0100151 [INF_CONNECTED_PEERS] = "ConnectedPeers",
Willy Tarreau13ef7732018-11-12 07:25:28 +0100152 [INF_DROPPED_LOGS] = "DroppedLogs",
Willy Tarreaubeb859a2018-11-22 18:07:59 +0100153 [INF_BUSY_POLLING] = "BusyPolling",
Baptiste Assmann333939c2019-01-21 08:34:50 +0100154 [INF_FAILED_RESOLUTIONS] = "FailedResolutions",
Willy Tarreau7cf0e452019-05-23 11:39:14 +0200155 [INF_TOTAL_BYTES_OUT] = "TotalBytesOut",
156 [INF_BYTES_OUT_RATE] = "BytesOutRate",
Adis Nezirovic3ad7b3d2021-01-15 13:12:33 +0100157 [INF_BUILD_INFO] = "Build info",
Willy Tarreau0baac8c2016-11-22 16:36:53 +0100158};
159
William Lallemand74c24fb2016-11-21 17:18:36 +0100160const char *stat_field_names[ST_F_TOTAL_FIELDS] = {
161 [ST_F_PXNAME] = "pxname",
162 [ST_F_SVNAME] = "svname",
163 [ST_F_QCUR] = "qcur",
164 [ST_F_QMAX] = "qmax",
165 [ST_F_SCUR] = "scur",
166 [ST_F_SMAX] = "smax",
167 [ST_F_SLIM] = "slim",
168 [ST_F_STOT] = "stot",
169 [ST_F_BIN] = "bin",
170 [ST_F_BOUT] = "bout",
171 [ST_F_DREQ] = "dreq",
172 [ST_F_DRESP] = "dresp",
173 [ST_F_EREQ] = "ereq",
174 [ST_F_ECON] = "econ",
175 [ST_F_ERESP] = "eresp",
176 [ST_F_WRETR] = "wretr",
177 [ST_F_WREDIS] = "wredis",
178 [ST_F_STATUS] = "status",
179 [ST_F_WEIGHT] = "weight",
180 [ST_F_ACT] = "act",
181 [ST_F_BCK] = "bck",
182 [ST_F_CHKFAIL] = "chkfail",
183 [ST_F_CHKDOWN] = "chkdown",
184 [ST_F_LASTCHG] = "lastchg",
185 [ST_F_DOWNTIME] = "downtime",
186 [ST_F_QLIMIT] = "qlimit",
187 [ST_F_PID] = "pid",
188 [ST_F_IID] = "iid",
189 [ST_F_SID] = "sid",
190 [ST_F_THROTTLE] = "throttle",
191 [ST_F_LBTOT] = "lbtot",
192 [ST_F_TRACKED] = "tracked",
193 [ST_F_TYPE] = "type",
194 [ST_F_RATE] = "rate",
195 [ST_F_RATE_LIM] = "rate_lim",
196 [ST_F_RATE_MAX] = "rate_max",
197 [ST_F_CHECK_STATUS] = "check_status",
198 [ST_F_CHECK_CODE] = "check_code",
199 [ST_F_CHECK_DURATION] = "check_duration",
200 [ST_F_HRSP_1XX] = "hrsp_1xx",
201 [ST_F_HRSP_2XX] = "hrsp_2xx",
202 [ST_F_HRSP_3XX] = "hrsp_3xx",
203 [ST_F_HRSP_4XX] = "hrsp_4xx",
204 [ST_F_HRSP_5XX] = "hrsp_5xx",
205 [ST_F_HRSP_OTHER] = "hrsp_other",
206 [ST_F_HANAFAIL] = "hanafail",
207 [ST_F_REQ_RATE] = "req_rate",
208 [ST_F_REQ_RATE_MAX] = "req_rate_max",
209 [ST_F_REQ_TOT] = "req_tot",
210 [ST_F_CLI_ABRT] = "cli_abrt",
211 [ST_F_SRV_ABRT] = "srv_abrt",
212 [ST_F_COMP_IN] = "comp_in",
213 [ST_F_COMP_OUT] = "comp_out",
214 [ST_F_COMP_BYP] = "comp_byp",
215 [ST_F_COMP_RSP] = "comp_rsp",
216 [ST_F_LASTSESS] = "lastsess",
217 [ST_F_LAST_CHK] = "last_chk",
218 [ST_F_LAST_AGT] = "last_agt",
219 [ST_F_QTIME] = "qtime",
220 [ST_F_CTIME] = "ctime",
221 [ST_F_RTIME] = "rtime",
222 [ST_F_TTIME] = "ttime",
223 [ST_F_AGENT_STATUS] = "agent_status",
224 [ST_F_AGENT_CODE] = "agent_code",
225 [ST_F_AGENT_DURATION] = "agent_duration",
226 [ST_F_CHECK_DESC] = "check_desc",
227 [ST_F_AGENT_DESC] = "agent_desc",
228 [ST_F_CHECK_RISE] = "check_rise",
229 [ST_F_CHECK_FALL] = "check_fall",
230 [ST_F_CHECK_HEALTH] = "check_health",
231 [ST_F_AGENT_RISE] = "agent_rise",
232 [ST_F_AGENT_FALL] = "agent_fall",
233 [ST_F_AGENT_HEALTH] = "agent_health",
234 [ST_F_ADDR] = "addr",
235 [ST_F_COOKIE] = "cookie",
236 [ST_F_MODE] = "mode",
237 [ST_F_ALGO] = "algo",
238 [ST_F_CONN_RATE] = "conn_rate",
239 [ST_F_CONN_RATE_MAX] = "conn_rate_max",
240 [ST_F_CONN_TOT] = "conn_tot",
241 [ST_F_INTERCEPTED] = "intercepted",
242 [ST_F_DCON] = "dcon",
243 [ST_F_DSES] = "dses",
Tim Duesterhus3fd19732018-05-27 20:35:08 +0200244 [ST_F_WREW] = "wrew",
Willy Tarreauf1573842018-12-14 11:35:36 +0100245 [ST_F_CONNECT] = "connect",
246 [ST_F_REUSE] = "reuse",
Willy Tarreaua1214a52018-12-14 14:00:25 +0100247 [ST_F_CACHE_LOOKUPS] = "cache_lookups",
248 [ST_F_CACHE_HITS] = "cache_hits",
Adis Neziroviceeea5702019-09-13 11:43:03 +0200249 [ST_F_SRV_ICUR] = "srv_icur",
Christopher Fauletb3fdd522019-11-08 14:59:51 +0100250 [ST_F_SRV_ILIM] = "src_ilim",
251 [ST_F_QT_MAX] = "qtime_max",
252 [ST_F_CT_MAX] = "ctime_max",
253 [ST_F_RT_MAX] = "rtime_max",
254 [ST_F_TT_MAX] = "ttime_max",
William Lallemand74c24fb2016-11-21 17:18:36 +0100255};
256
Willy Tarreau0baac8c2016-11-22 16:36:53 +0100257/* one line of info */
Christopher Faulet1bc04c72017-10-29 20:14:08 +0100258static THREAD_LOCAL struct field info[INF_TOTAL_FIELDS];
William Lallemand74c24fb2016-11-21 17:18:36 +0100259/* one line of stats */
Christopher Faulet1bc04c72017-10-29 20:14:08 +0100260static THREAD_LOCAL struct field stats[ST_F_TOTAL_FIELDS];
William Lallemand74c24fb2016-11-21 17:18:36 +0100261
262
Christopher Fauletef779222018-10-31 08:47:01 +0100263static int stats_putchk(struct channel *chn, struct htx *htx, struct buffer *chk)
264{
265 if (htx) {
Christopher Faulet69fc88c2019-01-07 14:27:53 +0100266 if (chk->data >= channel_htx_recv_max(chn, htx))
267 return 0;
Willy Tarreau0a7ef022019-05-28 10:30:11 +0200268 if (!htx_add_data_atonce(htx, ist2(chk->area, chk->data)))
Christopher Fauletef779222018-10-31 08:47:01 +0100269 return 0;
Christopher Faulet5adbeeb2019-01-02 14:34:39 +0100270 channel_add_input(chn, chk->data);
Christopher Fauletef779222018-10-31 08:47:01 +0100271 chk->data = 0;
Christopher Fauletef779222018-10-31 08:47:01 +0100272 }
273 else {
274 if (ci_putchk(chn, chk) == -1)
275 return 0;
276 }
277 return 1;
278}
Willy Tarreau0baac8c2016-11-22 16:36:53 +0100279
Christopher Fauleted7a0662019-01-14 11:07:34 +0100280static const char *stats_scope_ptr(struct appctx *appctx, struct stream_interface *si)
281{
282 const char *p;
283
284 if (IS_HTX_STRM(si_strm(si))) {
285 struct channel *req = si_oc(si);
286 struct htx *htx = htxbuf(&req->buf);
Christopher Faulet8fa60e42019-05-23 11:04:05 +0200287 struct htx_blk *blk;
288 struct ist uri;
Christopher Fauleted7a0662019-01-14 11:07:34 +0100289
Christopher Faulet8fa60e42019-05-23 11:04:05 +0200290 blk = htx_get_head_blk(htx);
Christopher Faulet43a686d2019-11-18 15:50:25 +0100291 BUG_ON(!blk || htx_get_blk_type(blk) != HTX_BLK_REQ_SL);
Christopher Faulet8fa60e42019-05-23 11:04:05 +0200292 ALREADY_CHECKED(blk);
293 uri = htx_sl_req_uri(htx_get_blk_ptr(htx, blk));
Christopher Fauleted7a0662019-01-14 11:07:34 +0100294 p = uri.ptr;
295 }
296 else
297 p = co_head(si_oc(si));
298
299 return p + appctx->ctx.stats.scope_str;
300}
301
William Lallemand74c24fb2016-11-21 17:18:36 +0100302/*
303 * http_stats_io_handler()
304 * -> stats_dump_stat_to_buffer() // same as above, but used for CSV or HTML
305 * -> stats_dump_csv_header() // emits the CSV headers (same as above)
Simon Horman05ee2132017-01-04 09:37:25 +0100306 * -> stats_dump_json_header() // emits the JSON headers (same as above)
William Lallemand74c24fb2016-11-21 17:18:36 +0100307 * -> stats_dump_html_head() // emits the HTML headers
308 * -> stats_dump_html_info() // emits the equivalent of "show info" at the top
309 * -> stats_dump_proxy_to_buffer() // same as above, valid for CSV and HTML
310 * -> stats_dump_html_px_hdr()
311 * -> stats_dump_fe_stats()
312 * -> stats_dump_li_stats()
313 * -> stats_dump_sv_stats()
314 * -> stats_dump_be_stats()
315 * -> stats_dump_html_px_end()
316 * -> stats_dump_html_end() // emits HTML trailer
Simon Horman05ee2132017-01-04 09:37:25 +0100317 * -> stats_dump_json_end() // emits JSON trailer
William Lallemand74c24fb2016-11-21 17:18:36 +0100318 */
319
320
William Lallemand74c24fb2016-11-21 17:18:36 +0100321/* Dumps the stats CSV header to the trash buffer which. The caller is responsible
322 * for clearing it if needed.
323 * NOTE: Some tools happen to rely on the field position instead of its name,
324 * so please only append new fields at the end, never in the middle.
325 */
326static void stats_dump_csv_header()
327{
328 int field;
329
330 chunk_appendf(&trash, "# ");
331 for (field = 0; field < ST_F_TOTAL_FIELDS; field++)
332 chunk_appendf(&trash, "%s,", stat_field_names[field]);
333
334 chunk_appendf(&trash, "\n");
335}
336
337
338/* Emits a stats field without any surrounding element and properly encoded to
339 * resist CSV output. Returns non-zero on success, 0 if the buffer is full.
340 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200341int stats_emit_raw_data_field(struct buffer *out, const struct field *f)
William Lallemand74c24fb2016-11-21 17:18:36 +0100342{
343 switch (field_format(f, 0)) {
344 case FF_EMPTY: return 1;
345 case FF_S32: return chunk_appendf(out, "%d", f->u.s32);
346 case FF_U32: return chunk_appendf(out, "%u", f->u.u32);
347 case FF_S64: return chunk_appendf(out, "%lld", (long long)f->u.s64);
348 case FF_U64: return chunk_appendf(out, "%llu", (unsigned long long)f->u.u64);
Christopher Faulet87d74c12019-09-24 16:35:10 +0200349 case FF_FLT: return chunk_appendf(out, "%f", f->u.flt);
William Lallemand74c24fb2016-11-21 17:18:36 +0100350 case FF_STR: return csv_enc_append(field_str(f, 0), 1, out) != NULL;
351 default: return chunk_appendf(out, "[INCORRECT_FIELD_TYPE_%08x]", f->type);
352 }
353}
354
355/* Emits a stats field prefixed with its type. No CSV encoding is prepared, the
356 * output is supposed to be used on its own line. Returns non-zero on success, 0
357 * if the buffer is full.
358 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200359int stats_emit_typed_data_field(struct buffer *out, const struct field *f)
William Lallemand74c24fb2016-11-21 17:18:36 +0100360{
361 switch (field_format(f, 0)) {
362 case FF_EMPTY: return 1;
363 case FF_S32: return chunk_appendf(out, "s32:%d", f->u.s32);
364 case FF_U32: return chunk_appendf(out, "u32:%u", f->u.u32);
365 case FF_S64: return chunk_appendf(out, "s64:%lld", (long long)f->u.s64);
366 case FF_U64: return chunk_appendf(out, "u64:%llu", (unsigned long long)f->u.u64);
Christopher Faulet87d74c12019-09-24 16:35:10 +0200367 case FF_FLT: return chunk_appendf(out, "flt:%f", f->u.flt);
William Lallemand74c24fb2016-11-21 17:18:36 +0100368 case FF_STR: return chunk_appendf(out, "str:%s", field_str(f, 0));
369 default: return chunk_appendf(out, "%08x:?", f->type);
370 }
371}
372
Simon Horman05ee2132017-01-04 09:37:25 +0100373/* Limit JSON integer values to the range [-(2**53)+1, (2**53)-1] as per
374 * the recommendation for interoperable integers in section 6 of RFC 7159.
375 */
376#define JSON_INT_MAX ((1ULL << 53) - 1)
377#define JSON_INT_MIN (0 - JSON_INT_MAX)
378
379/* Emits a stats field value and its type in JSON.
380 * Returns non-zero on success, 0 on error.
381 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200382int stats_emit_json_data_field(struct buffer *out, const struct field *f)
Simon Horman05ee2132017-01-04 09:37:25 +0100383{
384 int old_len;
385 char buf[20];
386 const char *type, *value = buf, *quote = "";
387
388 switch (field_format(f, 0)) {
389 case FF_EMPTY: return 1;
390 case FF_S32: type = "\"s32\"";
391 snprintf(buf, sizeof(buf), "%d", f->u.s32);
392 break;
393 case FF_U32: type = "\"u32\"";
394 snprintf(buf, sizeof(buf), "%u", f->u.u32);
395 break;
396 case FF_S64: type = "\"s64\"";
397 if (f->u.s64 < JSON_INT_MIN || f->u.s64 > JSON_INT_MAX)
398 return 0;
399 type = "\"s64\"";
400 snprintf(buf, sizeof(buf), "%lld", (long long)f->u.s64);
401 break;
402 case FF_U64: if (f->u.u64 > JSON_INT_MAX)
403 return 0;
404 type = "\"u64\"";
405 snprintf(buf, sizeof(buf), "%llu",
406 (unsigned long long) f->u.u64);
Christopher Fauletc0104fc2019-09-28 10:37:31 +0200407 break;
Christopher Faulet87d74c12019-09-24 16:35:10 +0200408 case FF_FLT: type = "\"flt\"";
409 snprintf(buf, sizeof(buf), "%f", f->u.flt);
Simon Horman05ee2132017-01-04 09:37:25 +0100410 break;
411 case FF_STR: type = "\"str\"";
412 value = field_str(f, 0);
413 quote = "\"";
414 break;
415 default: snprintf(buf, sizeof(buf), "%u", f->type);
416 type = buf;
417 value = "unknown";
418 quote = "\"";
419 break;
420 }
421
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200422 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100423 chunk_appendf(out, ",\"value\":{\"type\":%s,\"value\":%s%s%s}",
424 type, quote, value, quote);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200425 return !(old_len == out->data);
Simon Horman05ee2132017-01-04 09:37:25 +0100426}
427
William Lallemand74c24fb2016-11-21 17:18:36 +0100428/* Emits an encoding of the field type on 3 characters followed by a delimiter.
429 * Returns non-zero on success, 0 if the buffer is full.
430 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200431int stats_emit_field_tags(struct buffer *out, const struct field *f,
432 char delim)
William Lallemand74c24fb2016-11-21 17:18:36 +0100433{
434 char origin, nature, scope;
435
436 switch (field_origin(f, 0)) {
437 case FO_METRIC: origin = 'M'; break;
438 case FO_STATUS: origin = 'S'; break;
439 case FO_KEY: origin = 'K'; break;
440 case FO_CONFIG: origin = 'C'; break;
441 case FO_PRODUCT: origin = 'P'; break;
442 default: origin = '?'; break;
443 }
444
445 switch (field_nature(f, 0)) {
446 case FN_GAUGE: nature = 'G'; break;
447 case FN_LIMIT: nature = 'L'; break;
448 case FN_MIN: nature = 'm'; break;
449 case FN_MAX: nature = 'M'; break;
450 case FN_RATE: nature = 'R'; break;
451 case FN_COUNTER: nature = 'C'; break;
452 case FN_DURATION: nature = 'D'; break;
453 case FN_AGE: nature = 'A'; break;
454 case FN_TIME: nature = 'T'; break;
455 case FN_NAME: nature = 'N'; break;
456 case FN_OUTPUT: nature = 'O'; break;
457 case FN_AVG: nature = 'a'; break;
458 default: nature = '?'; break;
459 }
460
461 switch (field_scope(f, 0)) {
462 case FS_PROCESS: scope = 'P'; break;
463 case FS_SERVICE: scope = 'S'; break;
464 case FS_SYSTEM: scope = 's'; break;
465 case FS_CLUSTER: scope = 'C'; break;
466 default: scope = '?'; break;
467 }
468
469 return chunk_appendf(out, "%c%c%c%c", origin, nature, scope, delim);
470}
471
Simon Horman05ee2132017-01-04 09:37:25 +0100472/* Emits an encoding of the field type as JSON.
473 * Returns non-zero on success, 0 if the buffer is full.
474 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200475int stats_emit_json_field_tags(struct buffer *out, const struct field *f)
Simon Horman05ee2132017-01-04 09:37:25 +0100476{
477 const char *origin, *nature, *scope;
478 int old_len;
479
480 switch (field_origin(f, 0)) {
481 case FO_METRIC: origin = "Metric"; break;
482 case FO_STATUS: origin = "Status"; break;
483 case FO_KEY: origin = "Key"; break;
484 case FO_CONFIG: origin = "Config"; break;
485 case FO_PRODUCT: origin = "Product"; break;
486 default: origin = "Unknown"; break;
487 }
488
489 switch (field_nature(f, 0)) {
490 case FN_GAUGE: nature = "Gauge"; break;
491 case FN_LIMIT: nature = "Limit"; break;
492 case FN_MIN: nature = "Min"; break;
493 case FN_MAX: nature = "Max"; break;
494 case FN_RATE: nature = "Rate"; break;
495 case FN_COUNTER: nature = "Counter"; break;
496 case FN_DURATION: nature = "Duration"; break;
497 case FN_AGE: nature = "Age"; break;
498 case FN_TIME: nature = "Time"; break;
499 case FN_NAME: nature = "Name"; break;
500 case FN_OUTPUT: nature = "Output"; break;
501 case FN_AVG: nature = "Avg"; break;
502 default: nature = "Unknown"; break;
503 }
504
505 switch (field_scope(f, 0)) {
506 case FS_PROCESS: scope = "Process"; break;
507 case FS_SERVICE: scope = "Service"; break;
508 case FS_SYSTEM: scope = "System"; break;
509 case FS_CLUSTER: scope = "Cluster"; break;
510 default: scope = "Unknown"; break;
511 }
512
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200513 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100514 chunk_appendf(out, "\"tags\":{"
515 "\"origin\":\"%s\","
516 "\"nature\":\"%s\","
517 "\"scope\":\"%s\""
518 "}", origin, nature, scope);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200519 return !(old_len == out->data);
Simon Horman05ee2132017-01-04 09:37:25 +0100520}
William Lallemand74c24fb2016-11-21 17:18:36 +0100521
522/* Dump all fields from <stats> into <out> using CSV format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200523static int stats_dump_fields_csv(struct buffer *out,
524 const struct field *stats)
William Lallemand74c24fb2016-11-21 17:18:36 +0100525{
526 int field;
527
528 for (field = 0; field < ST_F_TOTAL_FIELDS; field++) {
529 if (!stats_emit_raw_data_field(out, &stats[field]))
530 return 0;
531 if (!chunk_strcat(out, ","))
532 return 0;
533 }
534 chunk_strcat(out, "\n");
535 return 1;
536}
537
538/* Dump all fields from <stats> into <out> using a typed "field:desc:type:value" format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200539static int stats_dump_fields_typed(struct buffer *out,
540 const struct field *stats)
William Lallemand74c24fb2016-11-21 17:18:36 +0100541{
542 int field;
543
544 for (field = 0; field < ST_F_TOTAL_FIELDS; field++) {
545 if (!stats[field].type)
546 continue;
547
548 chunk_appendf(out, "%c.%u.%u.%d.%s.%u:",
549 stats[ST_F_TYPE].u.u32 == STATS_TYPE_FE ? 'F' :
550 stats[ST_F_TYPE].u.u32 == STATS_TYPE_BE ? 'B' :
551 stats[ST_F_TYPE].u.u32 == STATS_TYPE_SO ? 'L' :
552 stats[ST_F_TYPE].u.u32 == STATS_TYPE_SV ? 'S' :
553 '?',
554 stats[ST_F_IID].u.u32, stats[ST_F_SID].u.u32,
555 field, stat_field_names[field], stats[ST_F_PID].u.u32);
556
557 if (!stats_emit_field_tags(out, &stats[field], ':'))
558 return 0;
559 if (!stats_emit_typed_data_field(out, &stats[field]))
560 return 0;
561 if (!chunk_strcat(out, "\n"))
562 return 0;
563 }
564 return 1;
565}
566
Simon Horman05ee2132017-01-04 09:37:25 +0100567/* Dump all fields from <stats> into <out> using the "show info json" format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200568static int stats_dump_json_info_fields(struct buffer *out,
Simon Horman05ee2132017-01-04 09:37:25 +0100569 const struct field *info)
570{
571 int field;
572 int started = 0;
573
574 if (!chunk_strcat(out, "["))
575 return 0;
576
577 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
578 int old_len;
579
580 if (!field_format(info, field))
581 continue;
582
583 if (started && !chunk_strcat(out, ","))
584 goto err;
585 started = 1;
586
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200587 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100588 chunk_appendf(out,
589 "{\"field\":{\"pos\":%d,\"name\":\"%s\"},"
590 "\"processNum\":%u,",
591 field, info_field_names[field],
592 info[INF_PROCESS_NUM].u.u32);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200593 if (old_len == out->data)
Simon Horman05ee2132017-01-04 09:37:25 +0100594 goto err;
595
596 if (!stats_emit_json_field_tags(out, &info[field]))
597 goto err;
598
599 if (!stats_emit_json_data_field(out, &info[field]))
600 goto err;
601
602 if (!chunk_strcat(out, "}"))
603 goto err;
604 }
605
606 if (!chunk_strcat(out, "]"))
607 goto err;
608 return 1;
609
610err:
611 chunk_reset(out);
612 chunk_appendf(out, "{\"errorStr\":\"output buffer too short\"}");
613 return 0;
614}
615
616/* Dump all fields from <stats> into <out> using a typed "field:desc:type:value" format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200617static int stats_dump_fields_json(struct buffer *out,
618 const struct field *stats,
Simon Horman05ee2132017-01-04 09:37:25 +0100619 int first_stat)
620{
621 int field;
622 int started = 0;
623
624 if (!first_stat && !chunk_strcat(out, ","))
625 return 0;
626 if (!chunk_strcat(out, "["))
627 return 0;
628
629 for (field = 0; field < ST_F_TOTAL_FIELDS; field++) {
630 const char *obj_type;
631 int old_len;
632
633 if (!stats[field].type)
634 continue;
635
636 if (started && !chunk_strcat(out, ","))
637 goto err;
638 started = 1;
639
640 switch (stats[ST_F_TYPE].u.u32) {
641 case STATS_TYPE_FE: obj_type = "Frontend"; break;
642 case STATS_TYPE_BE: obj_type = "Backend"; break;
643 case STATS_TYPE_SO: obj_type = "Listener"; break;
644 case STATS_TYPE_SV: obj_type = "Server"; break;
645 default: obj_type = "Unknown"; break;
646 }
647
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200648 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100649 chunk_appendf(out,
650 "{"
651 "\"objType\":\"%s\","
652 "\"proxyId\":%d,"
653 "\"id\":%d,"
654 "\"field\":{\"pos\":%d,\"name\":\"%s\"},"
655 "\"processNum\":%u,",
656 obj_type, stats[ST_F_IID].u.u32,
657 stats[ST_F_SID].u.u32, field,
658 stat_field_names[field], stats[ST_F_PID].u.u32);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200659 if (old_len == out->data)
Simon Horman05ee2132017-01-04 09:37:25 +0100660 goto err;
661
662 if (!stats_emit_json_field_tags(out, &stats[field]))
663 goto err;
664
665 if (!stats_emit_json_data_field(out, &stats[field]))
666 goto err;
667
668 if (!chunk_strcat(out, "}"))
669 goto err;
670 }
671
672 if (!chunk_strcat(out, "]"))
673 goto err;
674
675 return 1;
676
677err:
678 chunk_reset(out);
679 if (!first_stat)
680 chunk_strcat(out, ",");
681 chunk_appendf(out, "{\"errorStr\":\"output buffer too short\"}");
682 return 0;
683}
684
William Lallemand74c24fb2016-11-21 17:18:36 +0100685/* Dump all fields from <stats> into <out> using the HTML format. A column is
686 * reserved for the checkbox is ST_SHOWADMIN is set in <flags>. Some extra info
687 * are provided if ST_SHLGNDS is present in <flags>.
688 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200689static int stats_dump_fields_html(struct buffer *out,
690 const struct field *stats,
691 unsigned int flags)
William Lallemand74c24fb2016-11-21 17:18:36 +0100692{
Willy Tarreau83061a82018-07-13 11:56:34 +0200693 struct buffer src;
William Lallemand74c24fb2016-11-21 17:18:36 +0100694
695 if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_FE) {
696 chunk_appendf(out,
697 /* name, queue */
698 "<tr class=\"frontend\">");
699
700 if (flags & ST_SHOWADMIN) {
701 /* Column sub-heading for Enable or Disable server */
702 chunk_appendf(out, "<td></td>");
703 }
704
705 chunk_appendf(out,
706 "<td class=ac>"
707 "<a name=\"%s/Frontend\"></a>"
708 "<a class=lfsb href=\"#%s/Frontend\">Frontend</a></td>"
709 "<td colspan=3></td>"
710 "",
711 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_PXNAME));
712
713 chunk_appendf(out,
714 /* sessions rate : current */
715 "<td><u>%s<div class=tips><table class=det>"
716 "<tr><th>Current connection rate:</th><td>%s/s</td></tr>"
717 "<tr><th>Current session rate:</th><td>%s/s</td></tr>"
718 "",
719 U2H(stats[ST_F_RATE].u.u32),
720 U2H(stats[ST_F_CONN_RATE].u.u32),
721 U2H(stats[ST_F_RATE].u.u32));
722
723 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
724 chunk_appendf(out,
725 "<tr><th>Current request rate:</th><td>%s/s</td></tr>",
726 U2H(stats[ST_F_REQ_RATE].u.u32));
727
728 chunk_appendf(out,
729 "</table></div></u></td>"
730 /* sessions rate : max */
731 "<td><u>%s<div class=tips><table class=det>"
732 "<tr><th>Max connection rate:</th><td>%s/s</td></tr>"
733 "<tr><th>Max session rate:</th><td>%s/s</td></tr>"
734 "",
735 U2H(stats[ST_F_RATE_MAX].u.u32),
736 U2H(stats[ST_F_CONN_RATE_MAX].u.u32),
737 U2H(stats[ST_F_RATE_MAX].u.u32));
738
739 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
740 chunk_appendf(out,
741 "<tr><th>Max request rate:</th><td>%s/s</td></tr>",
742 U2H(stats[ST_F_REQ_RATE_MAX].u.u32));
743
744 chunk_appendf(out,
745 "</table></div></u></td>"
746 /* sessions rate : limit */
747 "<td>%s</td>",
748 LIM2A(stats[ST_F_RATE_LIM].u.u32, "-"));
749
750 chunk_appendf(out,
751 /* sessions: current, max, limit, total */
752 "<td>%s</td><td>%s</td><td>%s</td>"
753 "<td><u>%s<div class=tips><table class=det>"
754 "<tr><th>Cum. connections:</th><td>%s</td></tr>"
755 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
756 "",
757 U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SMAX].u.u32), U2H(stats[ST_F_SLIM].u.u32),
758 U2H(stats[ST_F_STOT].u.u64),
759 U2H(stats[ST_F_CONN_TOT].u.u64),
760 U2H(stats[ST_F_STOT].u.u64));
761
762 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
763 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
764 chunk_appendf(out,
765 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
766 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
767 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
768 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
769 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
770 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
771 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
772 "<tr><th>- other responses:</th><td>%s</td></tr>"
773 "<tr><th>Intercepted requests:</th><td>%s</td></tr>"
Willy Tarreaua1214a52018-12-14 14:00:25 +0100774 "<tr><th>Cache lookups:</th><td>%s</td></tr>"
775 "<tr><th>Cache hits:</th><td>%s</td><td>(%d%%)</td></tr>"
Willy Tarreau1b0f85e2018-05-28 15:12:40 +0200776 "<tr><th>Failed hdr rewrites:</th><td>%s</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +0100777 "",
778 U2H(stats[ST_F_REQ_TOT].u.u64),
779 U2H(stats[ST_F_HRSP_1XX].u.u64),
780 U2H(stats[ST_F_HRSP_2XX].u.u64),
781 U2H(stats[ST_F_COMP_RSP].u.u64),
782 stats[ST_F_HRSP_2XX].u.u64 ?
783 (int)(100 * stats[ST_F_COMP_RSP].u.u64 / stats[ST_F_HRSP_2XX].u.u64) : 0,
784 U2H(stats[ST_F_HRSP_3XX].u.u64),
785 U2H(stats[ST_F_HRSP_4XX].u.u64),
786 U2H(stats[ST_F_HRSP_5XX].u.u64),
787 U2H(stats[ST_F_HRSP_OTHER].u.u64),
Willy Tarreau1b0f85e2018-05-28 15:12:40 +0200788 U2H(stats[ST_F_INTERCEPTED].u.u64),
Willy Tarreaua1214a52018-12-14 14:00:25 +0100789 U2H(stats[ST_F_CACHE_LOOKUPS].u.u64),
790 U2H(stats[ST_F_CACHE_HITS].u.u64),
791 stats[ST_F_CACHE_LOOKUPS].u.u64 ?
792 (int)(100 * stats[ST_F_CACHE_HITS].u.u64 / stats[ST_F_CACHE_LOOKUPS].u.u64) : 0,
Willy Tarreau1b0f85e2018-05-28 15:12:40 +0200793 U2H(stats[ST_F_WREW].u.u64));
William Lallemand74c24fb2016-11-21 17:18:36 +0100794 }
795
796 chunk_appendf(out,
797 "</table></div></u></td>"
798 /* sessions: lbtot, lastsess */
799 "<td></td><td></td>"
800 /* bytes : in */
801 "<td>%s</td>"
802 "",
803 U2H(stats[ST_F_BIN].u.u64));
804
805 chunk_appendf(out,
806 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
807 "<td>%s%s<div class=tips><table class=det>"
808 "<tr><th>Response bytes in:</th><td>%s</td></tr>"
809 "<tr><th>Compression in:</th><td>%s</td></tr>"
810 "<tr><th>Compression out:</th><td>%s</td><td>(%d%%)</td></tr>"
811 "<tr><th>Compression bypass:</th><td>%s</td></tr>"
812 "<tr><th>Total bytes saved:</th><td>%s</td><td>(%d%%)</td></tr>"
813 "</table></div>%s</td>",
814 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "<u>":"",
815 U2H(stats[ST_F_BOUT].u.u64),
816 U2H(stats[ST_F_BOUT].u.u64),
817 U2H(stats[ST_F_COMP_IN].u.u64),
818 U2H(stats[ST_F_COMP_OUT].u.u64),
819 stats[ST_F_COMP_IN].u.u64 ? (int)(stats[ST_F_COMP_OUT].u.u64 * 100 / stats[ST_F_COMP_IN].u.u64) : 0,
820 U2H(stats[ST_F_COMP_BYP].u.u64),
821 U2H(stats[ST_F_COMP_IN].u.u64 - stats[ST_F_COMP_OUT].u.u64),
822 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,
823 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "</u>":"");
824
825 chunk_appendf(out,
826 /* denied: req, resp */
827 "<td>%s</td><td>%s</td>"
828 /* errors : request, connect, response */
829 "<td>%s</td><td></td><td></td>"
830 /* warnings: retries, redispatches */
831 "<td></td><td></td>"
832 /* server status : reflect frontend status */
833 "<td class=ac>%s</td>"
834 /* rest of server: nothing */
835 "<td class=ac colspan=8></td></tr>"
836 "",
837 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
838 U2H(stats[ST_F_EREQ].u.u64),
839 field_str(stats, ST_F_STATUS));
840 }
841 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_SO) {
842 chunk_appendf(out, "<tr class=socket>");
843 if (flags & ST_SHOWADMIN) {
844 /* Column sub-heading for Enable or Disable server */
845 chunk_appendf(out, "<td></td>");
846 }
847
848 chunk_appendf(out,
849 /* frontend name, listener name */
850 "<td class=ac><a name=\"%s/+%s\"></a>%s"
851 "<a class=lfsb href=\"#%s/+%s\">%s</a>"
852 "",
853 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME),
854 (flags & ST_SHLGNDS)?"<u>":"",
855 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME), field_str(stats, ST_F_SVNAME));
856
857 if (flags & ST_SHLGNDS) {
858 chunk_appendf(out, "<div class=tips>");
859
860 if (isdigit(*field_str(stats, ST_F_ADDR)))
861 chunk_appendf(out, "IPv4: %s, ", field_str(stats, ST_F_ADDR));
862 else if (*field_str(stats, ST_F_ADDR) == '[')
863 chunk_appendf(out, "IPv6: %s, ", field_str(stats, ST_F_ADDR));
864 else if (*field_str(stats, ST_F_ADDR))
865 chunk_appendf(out, "%s, ", field_str(stats, ST_F_ADDR));
866
867 /* id */
868 chunk_appendf(out, "id: %d</div>", stats[ST_F_SID].u.u32);
869 }
870
871 chunk_appendf(out,
872 /* queue */
873 "%s</td><td colspan=3></td>"
874 /* sessions rate: current, max, limit */
875 "<td colspan=3>&nbsp;</td>"
876 /* sessions: current, max, limit, total, lbtot, lastsess */
877 "<td>%s</td><td>%s</td><td>%s</td>"
878 "<td>%s</td><td>&nbsp;</td><td>&nbsp;</td>"
879 /* bytes: in, out */
880 "<td>%s</td><td>%s</td>"
881 "",
882 (flags & ST_SHLGNDS)?"</u>":"",
883 U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SMAX].u.u32), U2H(stats[ST_F_SLIM].u.u32),
884 U2H(stats[ST_F_STOT].u.u64), U2H(stats[ST_F_BIN].u.u64), U2H(stats[ST_F_BOUT].u.u64));
885
886 chunk_appendf(out,
887 /* denied: req, resp */
888 "<td>%s</td><td>%s</td>"
889 /* errors: request, connect, response */
890 "<td>%s</td><td></td><td></td>"
891 /* warnings: retries, redispatches */
892 "<td></td><td></td>"
893 /* server status: reflect listener status */
894 "<td class=ac>%s</td>"
895 /* rest of server: nothing */
896 "<td class=ac colspan=8></td></tr>"
897 "",
898 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
899 U2H(stats[ST_F_EREQ].u.u64),
900 field_str(stats, ST_F_STATUS));
901 }
902 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_SV) {
903 const char *style;
904
905 /* determine the style to use depending on the server's state,
906 * its health and weight. There isn't a 1-to-1 mapping between
907 * state and styles for the cases where the server is (still)
908 * up. The reason is that we don't want to report nolb and
909 * drain with the same color.
910 */
911
912 if (strcmp(field_str(stats, ST_F_STATUS), "DOWN") == 0 ||
913 strcmp(field_str(stats, ST_F_STATUS), "DOWN (agent)") == 0) {
914 style = "down";
915 }
Florian Apolloner3106a6b2021-03-30 13:28:35 +0200916 else if (strncmp(field_str(stats, ST_F_STATUS), "DOWN ", strlen("DOWN ")) == 0) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100917 style = "going_up";
918 }
Daniel Corbett90e2d312020-03-28 12:35:50 -0400919 else if (strcmp(field_str(stats, ST_F_STATUS), "DRAIN") == 0) {
920 style = "draining";
921 }
Florian Apolloner3106a6b2021-03-30 13:28:35 +0200922 else if (strncmp(field_str(stats, ST_F_STATUS), "NOLB ", strlen("NOLB ")) == 0) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100923 style = "going_down";
924 }
925 else if (strcmp(field_str(stats, ST_F_STATUS), "NOLB") == 0) {
926 style = "nolb";
927 }
928 else if (strcmp(field_str(stats, ST_F_STATUS), "no check") == 0) {
929 style = "no_check";
930 }
931 else if (!stats[ST_F_CHKFAIL].type ||
932 stats[ST_F_CHECK_HEALTH].u.u32 == stats[ST_F_CHECK_RISE].u.u32 + stats[ST_F_CHECK_FALL].u.u32 - 1) {
933 /* no check or max health = UP */
934 if (stats[ST_F_WEIGHT].u.u32)
935 style = "up";
936 else
937 style = "draining";
938 }
939 else {
940 style = "going_down";
941 }
942
Willy Tarreauf3109612020-08-11 10:26:36 +0200943 if (strncmp(field_str(stats, ST_F_STATUS), "MAINT", 5) == 0)
William Lallemand74c24fb2016-11-21 17:18:36 +0100944 chunk_appendf(out, "<tr class=\"maintain\">");
945 else
946 chunk_appendf(out,
947 "<tr class=\"%s_%s\">",
948 (stats[ST_F_BCK].u.u32) ? "backup" : "active", style);
949
950
951 if (flags & ST_SHOWADMIN)
952 chunk_appendf(out,
David Harrigand3db35a2016-12-30 12:12:49 +0000953 "<td><input class='%s-checkbox' type=\"checkbox\" name=\"s\" value=\"%s\"></td>",
954 field_str(stats, ST_F_PXNAME),
William Lallemand74c24fb2016-11-21 17:18:36 +0100955 field_str(stats, ST_F_SVNAME));
956
957 chunk_appendf(out,
958 "<td class=ac><a name=\"%s/%s\"></a>%s"
959 "<a class=lfsb href=\"#%s/%s\">%s</a>"
960 "",
961 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME),
962 (flags & ST_SHLGNDS) ? "<u>" : "",
963 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME), field_str(stats, ST_F_SVNAME));
964
965 if (flags & ST_SHLGNDS) {
966 chunk_appendf(out, "<div class=tips>");
967
968 if (isdigit(*field_str(stats, ST_F_ADDR)))
969 chunk_appendf(out, "IPv4: %s, ", field_str(stats, ST_F_ADDR));
970 else if (*field_str(stats, ST_F_ADDR) == '[')
971 chunk_appendf(out, "IPv6: %s, ", field_str(stats, ST_F_ADDR));
972 else if (*field_str(stats, ST_F_ADDR))
973 chunk_appendf(out, "%s, ", field_str(stats, ST_F_ADDR));
974
975 /* id */
976 chunk_appendf(out, "id: %d", stats[ST_F_SID].u.u32);
977
978 /* cookie */
979 if (stats[ST_F_COOKIE].type) {
980 chunk_appendf(out, ", cookie: '");
981 chunk_initstr(&src, field_str(stats, ST_F_COOKIE));
982 chunk_htmlencode(out, &src);
983 chunk_appendf(out, "'");
984 }
985
986 chunk_appendf(out, "</div>");
987 }
988
989 chunk_appendf(out,
990 /* queue : current, max, limit */
991 "%s</td><td>%s</td><td>%s</td><td>%s</td>"
992 /* sessions rate : current, max, limit */
993 "<td>%s</td><td>%s</td><td></td>"
994 "",
995 (flags & ST_SHLGNDS) ? "</u>" : "",
996 U2H(stats[ST_F_QCUR].u.u32), U2H(stats[ST_F_QMAX].u.u32), LIM2A(stats[ST_F_QLIMIT].u.u32, "-"),
997 U2H(stats[ST_F_RATE].u.u32), U2H(stats[ST_F_RATE_MAX].u.u32));
998
999 chunk_appendf(out,
1000 /* sessions: current, max, limit, total */
Willy Tarreau0ff699e2019-09-08 09:24:56 +02001001 "<td><u>%s<div class=tips>"
1002 "<table class=det>"
1003 "<tr><th>Current active connections:</th><td>%s</td></tr>"
1004 "<tr><th>Current idle connections:</th><td>%s</td></tr>"
1005 "<tr><th>Active connections limit:</th><td>%s</td></tr>"
1006 "<tr><th>Idle connections limit:</th><td>%s</td></tr>"
1007 "</table></div></u>"
1008 "</td><td>%s</td><td>%s</td>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001009 "<td><u>%s<div class=tips><table class=det>"
1010 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
1011 "",
Willy Tarreau0ff699e2019-09-08 09:24:56 +02001012 U2H(stats[ST_F_SCUR].u.u32),
1013 U2H(stats[ST_F_SCUR].u.u32),
1014 U2H(stats[ST_F_SRV_ICUR].u.u32),
1015 LIM2A(stats[ST_F_SLIM].u.u32, "-"),
1016 stats[ST_F_SRV_ILIM].type ? U2H(stats[ST_F_SRV_ILIM].u.u32) : "-",
1017 U2H(stats[ST_F_SMAX].u.u32), LIM2A(stats[ST_F_SLIM].u.u32, "-"),
William Lallemand74c24fb2016-11-21 17:18:36 +01001018 U2H(stats[ST_F_STOT].u.u64),
1019 U2H(stats[ST_F_STOT].u.u64));
1020
1021 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
1022 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
1023 unsigned long long tot;
1024
1025 tot = stats[ST_F_HRSP_OTHER].u.u64;
1026 tot += stats[ST_F_HRSP_1XX].u.u64;
1027 tot += stats[ST_F_HRSP_2XX].u.u64;
1028 tot += stats[ST_F_HRSP_3XX].u.u64;
1029 tot += stats[ST_F_HRSP_4XX].u.u64;
1030 tot += stats[ST_F_HRSP_5XX].u.u64;
1031
1032 chunk_appendf(out,
Willy Tarreauf1573842018-12-14 11:35:36 +01001033 "<tr><th>New connections:</th><td>%s</td></tr>"
1034 "<tr><th>Reused connections:</th><td>%s</td><td>(%d%%)</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001035 "<tr><th>Cum. HTTP responses:</th><td>%s</td></tr>"
1036 "<tr><th>- HTTP 1xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1037 "<tr><th>- HTTP 2xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1038 "<tr><th>- HTTP 3xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1039 "<tr><th>- HTTP 4xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1040 "<tr><th>- HTTP 5xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1041 "<tr><th>- other responses:</th><td>%s</td><td>(%d%%)</td></tr>"
Willy Tarreau1b0f85e2018-05-28 15:12:40 +02001042 "<tr><th>Failed hdr rewrites:</th><td>%s</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001043 "",
Willy Tarreauf1573842018-12-14 11:35:36 +01001044 U2H(stats[ST_F_CONNECT].u.u64),
1045 U2H(stats[ST_F_REUSE].u.u64),
1046 (stats[ST_F_CONNECT].u.u64 + stats[ST_F_REUSE].u.u64) ?
1047 (int)(100 * stats[ST_F_REUSE].u.u64 / (stats[ST_F_CONNECT].u.u64 + stats[ST_F_REUSE].u.u64)) : 0,
William Lallemand74c24fb2016-11-21 17:18:36 +01001048 U2H(tot),
1049 U2H(stats[ST_F_HRSP_1XX].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_1XX].u.u64 / tot) : 0,
1050 U2H(stats[ST_F_HRSP_2XX].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_2XX].u.u64 / tot) : 0,
1051 U2H(stats[ST_F_HRSP_3XX].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_3XX].u.u64 / tot) : 0,
1052 U2H(stats[ST_F_HRSP_4XX].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_4XX].u.u64 / tot) : 0,
1053 U2H(stats[ST_F_HRSP_5XX].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_5XX].u.u64 / tot) : 0,
Willy Tarreau1b0f85e2018-05-28 15:12:40 +02001054 U2H(stats[ST_F_HRSP_OTHER].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_OTHER].u.u64 / tot) : 0,
1055 U2H(stats[ST_F_WREW].u.u64));
William Lallemand74c24fb2016-11-21 17:18:36 +01001056 }
1057
Christopher Fauletb3fdd522019-11-08 14:59:51 +01001058 chunk_appendf(out, "<tr><th colspan=3>Max / Avg over last 1024 success. conn.</th></tr>");
1059 chunk_appendf(out, "<tr><th>- Queue time:</th><td>%s / %s</td><td>ms</td></tr>",
1060 U2H(stats[ST_F_QT_MAX].u.u32), U2H(stats[ST_F_QTIME].u.u32));
1061 chunk_appendf(out, "<tr><th>- Connect time:</th><td>%s / %s</td><td>ms</td></tr>",
1062 U2H(stats[ST_F_CT_MAX].u.u32), U2H(stats[ST_F_CTIME].u.u32));
William Lallemand74c24fb2016-11-21 17:18:36 +01001063 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
Christopher Fauletb3fdd522019-11-08 14:59:51 +01001064 chunk_appendf(out, "<tr><th>- Responses time:</th><td>%s / %s</td><td>ms</td></tr>",
1065 U2H(stats[ST_F_RT_MAX].u.u32), U2H(stats[ST_F_RTIME].u.u32));
1066 chunk_appendf(out, "<tr><th>- Total time:</th><td>%s / %s</td><td>ms</td></tr>",
1067 U2H(stats[ST_F_TT_MAX].u.u32), U2H(stats[ST_F_TTIME].u.u32));
William Lallemand74c24fb2016-11-21 17:18:36 +01001068
1069 chunk_appendf(out,
1070 "</table></div></u></td>"
1071 /* sessions: lbtot, last */
1072 "<td>%s</td><td>%s</td>",
1073 U2H(stats[ST_F_LBTOT].u.u64),
1074 human_time(stats[ST_F_LASTSESS].u.s32, 1));
1075
1076 chunk_appendf(out,
1077 /* bytes : in, out */
1078 "<td>%s</td><td>%s</td>"
1079 /* denied: req, resp */
1080 "<td></td><td>%s</td>"
1081 /* errors : request, connect */
1082 "<td></td><td>%s</td>"
1083 /* errors : response */
1084 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
1085 /* warnings: retries, redispatches */
1086 "<td>%lld</td><td>%lld</td>"
1087 "",
1088 U2H(stats[ST_F_BIN].u.u64), U2H(stats[ST_F_BOUT].u.u64),
1089 U2H(stats[ST_F_DRESP].u.u64),
1090 U2H(stats[ST_F_ECON].u.u64),
1091 U2H(stats[ST_F_ERESP].u.u64),
1092 (long long)stats[ST_F_CLI_ABRT].u.u64,
1093 (long long)stats[ST_F_SRV_ABRT].u.u64,
1094 (long long)stats[ST_F_WRETR].u.u64,
1095 (long long)stats[ST_F_WREDIS].u.u64);
1096
1097 /* status, last change */
1098 chunk_appendf(out, "<td class=ac>");
1099
1100 /* FIXME!!!!
1101 * LASTCHG should contain the last change for *this* server and must be computed
1102 * properly above, as was done below, ie: this server if maint, otherwise ref server
1103 * if tracking. Note that ref is either local or remote depending on tracking.
1104 */
1105
1106
Willy Tarreauf3109612020-08-11 10:26:36 +02001107 if (strncmp(field_str(stats, ST_F_STATUS), "MAINT", 5) == 0) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001108 chunk_appendf(out, "%s MAINT", human_time(stats[ST_F_LASTCHG].u.u32, 1));
1109 }
Willy Tarreauf3109612020-08-11 10:26:36 +02001110 else if (strcmp(field_str(stats, ST_F_STATUS), "no check") == 0) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001111 chunk_strcat(out, "<i>no check</i>");
1112 }
1113 else {
1114 chunk_appendf(out, "%s %s", human_time(stats[ST_F_LASTCHG].u.u32, 1), field_str(stats, ST_F_STATUS));
Willy Tarreauf3109612020-08-11 10:26:36 +02001115 if (strncmp(field_str(stats, ST_F_STATUS), "DOWN", 4) == 0) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001116 if (stats[ST_F_CHECK_HEALTH].u.u32)
1117 chunk_strcat(out, " &uarr;");
1118 }
1119 else if (stats[ST_F_CHECK_HEALTH].u.u32 < stats[ST_F_CHECK_RISE].u.u32 + stats[ST_F_CHECK_FALL].u.u32 - 1)
1120 chunk_strcat(out, " &darr;");
1121 }
1122
Willy Tarreauf3109612020-08-11 10:26:36 +02001123 if (strncmp(field_str(stats, ST_F_STATUS), "DOWN", 4) == 0 &&
William Lallemand74c24fb2016-11-21 17:18:36 +01001124 stats[ST_F_AGENT_STATUS].type && !stats[ST_F_AGENT_HEALTH].u.u32) {
1125 chunk_appendf(out,
1126 "</td><td class=ac><u> %s",
1127 field_str(stats, ST_F_AGENT_STATUS));
1128
1129 if (stats[ST_F_AGENT_CODE].type)
1130 chunk_appendf(out, "/%d", stats[ST_F_AGENT_CODE].u.u32);
1131
1132 if (stats[ST_F_AGENT_DURATION].type)
1133 chunk_appendf(out, " in %lums", (long)stats[ST_F_AGENT_DURATION].u.u64);
1134
1135 chunk_appendf(out, "<div class=tips>%s", field_str(stats, ST_F_AGENT_DESC));
1136
1137 if (*field_str(stats, ST_F_LAST_AGT)) {
1138 chunk_appendf(out, ": ");
1139 chunk_initstr(&src, field_str(stats, ST_F_LAST_AGT));
1140 chunk_htmlencode(out, &src);
1141 }
1142 chunk_appendf(out, "</div></u>");
1143 }
1144 else if (stats[ST_F_CHECK_STATUS].type) {
1145 chunk_appendf(out,
1146 "</td><td class=ac><u> %s",
1147 field_str(stats, ST_F_CHECK_STATUS));
1148
1149 if (stats[ST_F_CHECK_CODE].type)
1150 chunk_appendf(out, "/%d", stats[ST_F_CHECK_CODE].u.u32);
1151
1152 if (stats[ST_F_CHECK_DURATION].type)
1153 chunk_appendf(out, " in %lums", (long)stats[ST_F_CHECK_DURATION].u.u64);
1154
1155 chunk_appendf(out, "<div class=tips>%s", field_str(stats, ST_F_CHECK_DESC));
1156
1157 if (*field_str(stats, ST_F_LAST_CHK)) {
1158 chunk_appendf(out, ": ");
1159 chunk_initstr(&src, field_str(stats, ST_F_LAST_CHK));
1160 chunk_htmlencode(out, &src);
1161 }
1162 chunk_appendf(out, "</div></u>");
1163 }
1164 else
1165 chunk_appendf(out, "</td><td>");
1166
1167 chunk_appendf(out,
1168 /* weight */
1169 "</td><td class=ac>%d</td>"
1170 /* act, bck */
1171 "<td class=ac>%s</td><td class=ac>%s</td>"
1172 "",
1173 stats[ST_F_WEIGHT].u.u32,
1174 stats[ST_F_BCK].u.u32 ? "-" : "Y",
1175 stats[ST_F_BCK].u.u32 ? "Y" : "-");
1176
1177 /* check failures: unique, fatal, down time */
1178 if (strcmp(field_str(stats, ST_F_STATUS), "MAINT (resolution)") == 0) {
1179 chunk_appendf(out, "<td class=ac colspan=3>resolution</td>");
1180 }
1181 else if (stats[ST_F_CHKFAIL].type) {
1182 chunk_appendf(out, "<td><u>%lld", (long long)stats[ST_F_CHKFAIL].u.u64);
1183
1184 if (stats[ST_F_HANAFAIL].type)
1185 chunk_appendf(out, "/%lld", (long long)stats[ST_F_HANAFAIL].u.u64);
1186
1187 chunk_appendf(out,
1188 "<div class=tips>Failed Health Checks%s</div></u></td>"
1189 "<td>%lld</td><td>%s</td>"
1190 "",
1191 stats[ST_F_HANAFAIL].type ? "/Health Analyses" : "",
1192 (long long)stats[ST_F_CHKDOWN].u.u64, human_time(stats[ST_F_DOWNTIME].u.u32, 1));
1193 }
1194 else if (strcmp(field_str(stats, ST_F_STATUS), "MAINT") != 0 && field_format(stats, ST_F_TRACKED) == FF_STR) {
1195 /* tracking a server (hence inherited maint would appear as "MAINT (via...)" */
1196 chunk_appendf(out,
1197 "<td class=ac colspan=3><a class=lfsb href=\"#%s\">via %s</a></td>",
1198 field_str(stats, ST_F_TRACKED), field_str(stats, ST_F_TRACKED));
1199 }
1200 else
1201 chunk_appendf(out, "<td colspan=3></td>");
1202
1203 /* throttle */
1204 if (stats[ST_F_THROTTLE].type)
1205 chunk_appendf(out, "<td class=ac>%d %%</td></tr>\n", stats[ST_F_THROTTLE].u.u32);
1206 else
1207 chunk_appendf(out, "<td class=ac>-</td></tr>\n");
1208 }
1209 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_BE) {
1210 chunk_appendf(out, "<tr class=\"backend\">");
1211 if (flags & ST_SHOWADMIN) {
1212 /* Column sub-heading for Enable or Disable server */
1213 chunk_appendf(out, "<td></td>");
1214 }
1215 chunk_appendf(out,
1216 "<td class=ac>"
1217 /* name */
1218 "%s<a name=\"%s/Backend\"></a>"
1219 "<a class=lfsb href=\"#%s/Backend\">Backend</a>"
1220 "",
1221 (flags & ST_SHLGNDS)?"<u>":"",
1222 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_PXNAME));
1223
1224 if (flags & ST_SHLGNDS) {
1225 /* balancing */
1226 chunk_appendf(out, "<div class=tips>balancing: %s",
1227 field_str(stats, ST_F_ALGO));
1228
1229 /* cookie */
1230 if (stats[ST_F_COOKIE].type) {
1231 chunk_appendf(out, ", cookie: '");
1232 chunk_initstr(&src, field_str(stats, ST_F_COOKIE));
1233 chunk_htmlencode(out, &src);
1234 chunk_appendf(out, "'");
1235 }
1236 chunk_appendf(out, "</div>");
1237 }
1238
1239 chunk_appendf(out,
1240 "%s</td>"
1241 /* queue : current, max */
1242 "<td>%s</td><td>%s</td><td></td>"
1243 /* sessions rate : current, max, limit */
1244 "<td>%s</td><td>%s</td><td></td>"
1245 "",
1246 (flags & ST_SHLGNDS)?"</u>":"",
1247 U2H(stats[ST_F_QCUR].u.u32), U2H(stats[ST_F_QMAX].u.u32),
1248 U2H(stats[ST_F_RATE].u.u32), U2H(stats[ST_F_RATE_MAX].u.u32));
1249
1250 chunk_appendf(out,
1251 /* sessions: current, max, limit, total */
1252 "<td>%s</td><td>%s</td><td>%s</td>"
1253 "<td><u>%s<div class=tips><table class=det>"
1254 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
1255 "",
Willy Tarreau8e0f1752016-12-12 15:07:29 +01001256 U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SMAX].u.u32), U2H(stats[ST_F_SLIM].u.u32),
William Lallemand74c24fb2016-11-21 17:18:36 +01001257 U2H(stats[ST_F_STOT].u.u64),
1258 U2H(stats[ST_F_STOT].u.u64));
1259
1260 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
1261 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
1262 chunk_appendf(out,
Willy Tarreauf1573842018-12-14 11:35:36 +01001263 "<tr><th>New connections:</th><td>%s</td></tr>"
1264 "<tr><th>Reused connections:</th><td>%s</td><td>(%d%%)</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001265 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
1266 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
1267 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
1268 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
1269 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
1270 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
1271 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
1272 "<tr><th>- other responses:</th><td>%s</td></tr>"
Willy Tarreaua1214a52018-12-14 14:00:25 +01001273 "<tr><th>Cache lookups:</th><td>%s</td></tr>"
1274 "<tr><th>Cache hits:</th><td>%s</td><td>(%d%%)</td></tr>"
Willy Tarreau1b0f85e2018-05-28 15:12:40 +02001275 "<tr><th>Failed hdr rewrites:</th><td>%s</td></tr>"
Christopher Fauletb3fdd522019-11-08 14:59:51 +01001276 "",
Willy Tarreauf1573842018-12-14 11:35:36 +01001277 U2H(stats[ST_F_CONNECT].u.u64),
1278 U2H(stats[ST_F_REUSE].u.u64),
1279 (stats[ST_F_CONNECT].u.u64 + stats[ST_F_REUSE].u.u64) ?
1280 (int)(100 * stats[ST_F_REUSE].u.u64 / (stats[ST_F_CONNECT].u.u64 + stats[ST_F_REUSE].u.u64)) : 0,
William Lallemand74c24fb2016-11-21 17:18:36 +01001281 U2H(stats[ST_F_REQ_TOT].u.u64),
1282 U2H(stats[ST_F_HRSP_1XX].u.u64),
1283 U2H(stats[ST_F_HRSP_2XX].u.u64),
1284 U2H(stats[ST_F_COMP_RSP].u.u64),
1285 stats[ST_F_HRSP_2XX].u.u64 ?
1286 (int)(100 * stats[ST_F_COMP_RSP].u.u64 / stats[ST_F_HRSP_2XX].u.u64) : 0,
1287 U2H(stats[ST_F_HRSP_3XX].u.u64),
1288 U2H(stats[ST_F_HRSP_4XX].u.u64),
1289 U2H(stats[ST_F_HRSP_5XX].u.u64),
Willy Tarreaufeead3a2018-12-14 13:48:44 +01001290 U2H(stats[ST_F_HRSP_OTHER].u.u64),
Willy Tarreaua1214a52018-12-14 14:00:25 +01001291 U2H(stats[ST_F_CACHE_LOOKUPS].u.u64),
1292 U2H(stats[ST_F_CACHE_HITS].u.u64),
1293 stats[ST_F_CACHE_LOOKUPS].u.u64 ?
1294 (int)(100 * stats[ST_F_CACHE_HITS].u.u64 / stats[ST_F_CACHE_LOOKUPS].u.u64) : 0,
Willy Tarreaufeead3a2018-12-14 13:48:44 +01001295 U2H(stats[ST_F_WREW].u.u64));
William Lallemand74c24fb2016-11-21 17:18:36 +01001296 }
1297
Christopher Fauletb3fdd522019-11-08 14:59:51 +01001298 chunk_appendf(out, "<tr><th colspan=3>Max / Avg over last 1024 success. conn.</th></tr>");
1299 chunk_appendf(out, "<tr><th>- Queue time:</th><td>%s / %s</td><td>ms</td></tr>",
1300 U2H(stats[ST_F_QT_MAX].u.u32), U2H(stats[ST_F_QTIME].u.u32));
1301 chunk_appendf(out, "<tr><th>- Connect time:</th><td>%s / %s</td><td>ms</td></tr>",
1302 U2H(stats[ST_F_CT_MAX].u.u32), U2H(stats[ST_F_CTIME].u.u32));
William Lallemand74c24fb2016-11-21 17:18:36 +01001303 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
Christopher Fauletb3fdd522019-11-08 14:59:51 +01001304 chunk_appendf(out, "<tr><th>- Responses time:</th><td>%s / %s</td><td>ms</td></tr>",
1305 U2H(stats[ST_F_RT_MAX].u.u32), U2H(stats[ST_F_RTIME].u.u32));
1306 chunk_appendf(out, "<tr><th>- Total time:</th><td>%s / %s</td><td>ms</td></tr>",
1307 U2H(stats[ST_F_TT_MAX].u.u32), U2H(stats[ST_F_TTIME].u.u32));
William Lallemand74c24fb2016-11-21 17:18:36 +01001308
1309 chunk_appendf(out,
1310 "</table></div></u></td>"
1311 /* sessions: lbtot, last */
1312 "<td>%s</td><td>%s</td>"
1313 /* bytes: in */
1314 "<td>%s</td>"
1315 "",
1316 U2H(stats[ST_F_LBTOT].u.u64),
1317 human_time(stats[ST_F_LASTSESS].u.s32, 1),
1318 U2H(stats[ST_F_BIN].u.u64));
1319
1320 chunk_appendf(out,
1321 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
1322 "<td>%s%s<div class=tips><table class=det>"
1323 "<tr><th>Response bytes in:</th><td>%s</td></tr>"
1324 "<tr><th>Compression in:</th><td>%s</td></tr>"
1325 "<tr><th>Compression out:</th><td>%s</td><td>(%d%%)</td></tr>"
1326 "<tr><th>Compression bypass:</th><td>%s</td></tr>"
1327 "<tr><th>Total bytes saved:</th><td>%s</td><td>(%d%%)</td></tr>"
1328 "</table></div>%s</td>",
1329 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "<u>":"",
1330 U2H(stats[ST_F_BOUT].u.u64),
1331 U2H(stats[ST_F_BOUT].u.u64),
1332 U2H(stats[ST_F_COMP_IN].u.u64),
1333 U2H(stats[ST_F_COMP_OUT].u.u64),
1334 stats[ST_F_COMP_IN].u.u64 ? (int)(stats[ST_F_COMP_OUT].u.u64 * 100 / stats[ST_F_COMP_IN].u.u64) : 0,
1335 U2H(stats[ST_F_COMP_BYP].u.u64),
1336 U2H(stats[ST_F_COMP_IN].u.u64 - stats[ST_F_COMP_OUT].u.u64),
1337 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,
1338 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "</u>":"");
1339
1340 chunk_appendf(out,
1341 /* denied: req, resp */
1342 "<td>%s</td><td>%s</td>"
1343 /* errors : request, connect */
1344 "<td></td><td>%s</td>"
1345 /* errors : response */
1346 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
1347 /* warnings: retries, redispatches */
1348 "<td>%lld</td><td>%lld</td>"
1349 /* backend status: reflect backend status (up/down): we display UP
1350 * if the backend has known working servers or if it has no server at
1351 * all (eg: for stats). Then we display the total weight, number of
1352 * active and backups. */
1353 "<td class=ac>%s %s</td><td class=ac>&nbsp;</td><td class=ac>%d</td>"
1354 "<td class=ac>%d</td><td class=ac>%d</td>"
1355 "",
1356 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
1357 U2H(stats[ST_F_ECON].u.u64),
1358 U2H(stats[ST_F_ERESP].u.u64),
1359 (long long)stats[ST_F_CLI_ABRT].u.u64,
1360 (long long)stats[ST_F_SRV_ABRT].u.u64,
1361 (long long)stats[ST_F_WRETR].u.u64, (long long)stats[ST_F_WREDIS].u.u64,
1362 human_time(stats[ST_F_LASTCHG].u.u32, 1),
1363 strcmp(field_str(stats, ST_F_STATUS), "DOWN") ? field_str(stats, ST_F_STATUS) : "<font color=\"red\"><b>DOWN</b></font>",
1364 stats[ST_F_WEIGHT].u.u32,
1365 stats[ST_F_ACT].u.u32, stats[ST_F_BCK].u.u32);
1366
1367 chunk_appendf(out,
1368 /* rest of backend: nothing, down transitions, total downtime, throttle */
1369 "<td class=ac>&nbsp;</td><td>%d</td>"
1370 "<td>%s</td>"
1371 "<td></td>"
1372 "</tr>",
1373 stats[ST_F_CHKDOWN].u.u32,
1374 stats[ST_F_DOWNTIME].type ? human_time(stats[ST_F_DOWNTIME].u.u32, 1) : "&nbsp;");
1375 }
1376 return 1;
1377}
1378
1379int stats_dump_one_line(const struct field *stats, unsigned int flags, struct proxy *px, struct appctx *appctx)
1380{
Simon Horman05ee2132017-01-04 09:37:25 +01001381 int ret;
1382
William Lallemand74c24fb2016-11-21 17:18:36 +01001383 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN))
1384 flags |= ST_SHOWADMIN;
1385
1386 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
Simon Horman05ee2132017-01-04 09:37:25 +01001387 ret = stats_dump_fields_html(&trash, stats, flags);
William Lallemand74c24fb2016-11-21 17:18:36 +01001388 else if (appctx->ctx.stats.flags & STAT_FMT_TYPED)
Simon Horman05ee2132017-01-04 09:37:25 +01001389 ret = stats_dump_fields_typed(&trash, stats);
1390 else if (appctx->ctx.stats.flags & STAT_FMT_JSON)
1391 ret = stats_dump_fields_json(&trash, stats,
1392 !(appctx->ctx.stats.flags &
1393 STAT_STARTED));
William Lallemand74c24fb2016-11-21 17:18:36 +01001394 else
Simon Horman05ee2132017-01-04 09:37:25 +01001395 ret = stats_dump_fields_csv(&trash, stats);
1396
1397 if (ret)
1398 appctx->ctx.stats.flags |= STAT_STARTED;
1399
1400 return ret;
William Lallemand74c24fb2016-11-21 17:18:36 +01001401}
1402
1403/* Fill <stats> with the frontend statistics. <stats> is
1404 * preallocated array of length <len>. The length of the array
1405 * must be at least ST_F_TOTAL_FIELDS. If this length is less then
1406 * this value, the function returns 0, otherwise, it returns 1.
1407 */
1408int stats_fill_fe_stats(struct proxy *px, struct field *stats, int len)
1409{
1410 if (len < ST_F_TOTAL_FIELDS)
1411 return 0;
1412
1413 memset(stats, 0, sizeof(*stats) * len);
1414
1415 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1416 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, "FRONTEND");
1417 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1418 stats[ST_F_SCUR] = mkf_u32(0, px->feconn);
1419 stats[ST_F_SMAX] = mkf_u32(FN_MAX, px->fe_counters.conn_max);
1420 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, px->maxconn);
1421 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, px->fe_counters.cum_sess);
1422 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, px->fe_counters.bytes_in);
1423 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, px->fe_counters.bytes_out);
1424 stats[ST_F_DREQ] = mkf_u64(FN_COUNTER, px->fe_counters.denied_req);
1425 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, px->fe_counters.denied_resp);
1426 stats[ST_F_EREQ] = mkf_u64(FN_COUNTER, px->fe_counters.failed_req);
1427 stats[ST_F_DCON] = mkf_u64(FN_COUNTER, px->fe_counters.denied_conn);
1428 stats[ST_F_DSES] = mkf_u64(FN_COUNTER, px->fe_counters.denied_sess);
1429 stats[ST_F_STATUS] = mkf_str(FO_STATUS, px->state == PR_STREADY ? "OPEN" : px->state == PR_STFULL ? "FULL" : "STOP");
1430 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
1431 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1432 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, 0);
1433 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_FE);
1434 stats[ST_F_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_sess_per_sec));
1435 stats[ST_F_RATE_LIM] = mkf_u32(FO_CONFIG|FN_LIMIT, px->fe_sps_lim);
1436 stats[ST_F_RATE_MAX] = mkf_u32(FN_MAX, px->fe_counters.sps_max);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001437 stats[ST_F_WREW] = mkf_u64(FN_COUNTER, px->fe_counters.failed_rewrites);
William Lallemand74c24fb2016-11-21 17:18:36 +01001438
1439 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
1440 if (px->mode == PR_MODE_HTTP) {
1441 stats[ST_F_HRSP_1XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[1]);
1442 stats[ST_F_HRSP_2XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[2]);
1443 stats[ST_F_HRSP_3XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[3]);
1444 stats[ST_F_HRSP_4XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[4]);
1445 stats[ST_F_HRSP_5XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[5]);
1446 stats[ST_F_HRSP_OTHER] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[0]);
1447 stats[ST_F_INTERCEPTED] = mkf_u64(FN_COUNTER, px->fe_counters.intercepted_req);
Willy Tarreaua1214a52018-12-14 14:00:25 +01001448 stats[ST_F_CACHE_LOOKUPS] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cache_lookups);
1449 stats[ST_F_CACHE_HITS] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cache_hits);
William Lallemand74c24fb2016-11-21 17:18:36 +01001450 }
1451
1452 /* requests : req_rate, req_rate_max, req_tot, */
1453 stats[ST_F_REQ_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_req_per_sec));
1454 stats[ST_F_REQ_RATE_MAX] = mkf_u32(FN_MAX, px->fe_counters.p.http.rps_max);
1455 stats[ST_F_REQ_TOT] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cum_req);
1456
1457 /* compression: in, out, bypassed, responses */
1458 stats[ST_F_COMP_IN] = mkf_u64(FN_COUNTER, px->fe_counters.comp_in);
1459 stats[ST_F_COMP_OUT] = mkf_u64(FN_COUNTER, px->fe_counters.comp_out);
1460 stats[ST_F_COMP_BYP] = mkf_u64(FN_COUNTER, px->fe_counters.comp_byp);
1461 stats[ST_F_COMP_RSP] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.comp_rsp);
1462
1463 /* connections : conn_rate, conn_rate_max, conn_tot, conn_max */
1464 stats[ST_F_CONN_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_conn_per_sec));
1465 stats[ST_F_CONN_RATE_MAX] = mkf_u32(FN_MAX, px->fe_counters.cps_max);
1466 stats[ST_F_CONN_TOT] = mkf_u64(FN_COUNTER, px->fe_counters.cum_conn);
1467
1468 return 1;
1469}
1470
1471/* Dumps a frontend's line to the trash for the current proxy <px> and uses
1472 * the state from stream interface <si>. The caller is responsible for clearing
1473 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
1474 */
1475static int stats_dump_fe_stats(struct stream_interface *si, struct proxy *px)
1476{
1477 struct appctx *appctx = __objt_appctx(si->end);
1478
1479 if (!(px->cap & PR_CAP_FE))
1480 return 0;
1481
1482 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_FE)))
1483 return 0;
1484
1485 if (!stats_fill_fe_stats(px, stats, ST_F_TOTAL_FIELDS))
1486 return 0;
1487
1488 return stats_dump_one_line(stats, 0, px, appctx);
1489}
1490
1491/* Fill <stats> with the listener statistics. <stats> is
1492 * preallocated array of length <len>. The length of the array
1493 * must be at least ST_F_TOTAL_FIELDS. If this length is less
1494 * then this value, the function returns 0, otherwise, it
1495 * returns 1. <flags> can take the value ST_SHLGNDS.
1496 */
1497int stats_fill_li_stats(struct proxy *px, struct listener *l, int flags,
1498 struct field *stats, int len)
1499{
Willy Tarreau83061a82018-07-13 11:56:34 +02001500 struct buffer *out = get_trash_chunk();
William Lallemand74c24fb2016-11-21 17:18:36 +01001501
1502 if (len < ST_F_TOTAL_FIELDS)
1503 return 0;
1504
1505 if (!l->counters)
1506 return 0;
1507
1508 chunk_reset(out);
1509 memset(stats, 0, sizeof(*stats) * len);
1510
1511 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1512 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, l->name);
1513 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1514 stats[ST_F_SCUR] = mkf_u32(0, l->nbconn);
1515 stats[ST_F_SMAX] = mkf_u32(FN_MAX, l->counters->conn_max);
1516 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, l->maxconn);
1517 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, l->counters->cum_conn);
1518 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, l->counters->bytes_in);
1519 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, l->counters->bytes_out);
1520 stats[ST_F_DREQ] = mkf_u64(FN_COUNTER, l->counters->denied_req);
1521 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, l->counters->denied_resp);
1522 stats[ST_F_EREQ] = mkf_u64(FN_COUNTER, l->counters->failed_req);
1523 stats[ST_F_DCON] = mkf_u64(FN_COUNTER, l->counters->denied_conn);
1524 stats[ST_F_DSES] = mkf_u64(FN_COUNTER, l->counters->denied_sess);
Willy Tarreaua8cf66b2019-02-27 16:49:00 +01001525 stats[ST_F_STATUS] = mkf_str(FO_STATUS, (!l->maxconn || l->nbconn < l->maxconn) ? (l->state == LI_LIMITED) ? "WAITING" : "OPEN" : "FULL");
William Lallemand74c24fb2016-11-21 17:18:36 +01001526 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
1527 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1528 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, l->luid);
1529 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_SO);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001530 stats[ST_F_WREW] = mkf_u64(FN_COUNTER, l->counters->failed_rewrites);
William Lallemand74c24fb2016-11-21 17:18:36 +01001531
1532 if (flags & ST_SHLGNDS) {
1533 char str[INET6_ADDRSTRLEN];
1534 int port;
1535
1536 port = get_host_port(&l->addr);
1537 switch (addr_to_str(&l->addr, str, sizeof(str))) {
1538 case AF_INET:
1539 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1540 chunk_appendf(out, "%s:%d", str, port);
1541 break;
1542 case AF_INET6:
1543 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1544 chunk_appendf(out, "[%s]:%d", str, port);
1545 break;
1546 case AF_UNIX:
1547 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, "unix");
1548 break;
1549 case -1:
1550 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1551 chunk_strcat(out, strerror(errno));
1552 break;
1553 default: /* address family not supported */
1554 break;
1555 }
1556 }
1557
1558 return 1;
1559}
1560
1561/* Dumps a line for listener <l> and proxy <px> to the trash and uses the state
1562 * from stream interface <si>, and stats flags <flags>. The caller is responsible
1563 * for clearing the trash if needed. Returns non-zero if it emits anything, zero
1564 * otherwise.
1565 */
1566static int stats_dump_li_stats(struct stream_interface *si, struct proxy *px, struct listener *l, int flags)
1567{
1568 struct appctx *appctx = __objt_appctx(si->end);
1569
1570 if (!stats_fill_li_stats(px, l, flags, stats, ST_F_TOTAL_FIELDS))
1571 return 0;
1572
1573 return stats_dump_one_line(stats, flags, px, appctx);
1574}
1575
1576enum srv_stats_state {
1577 SRV_STATS_STATE_DOWN = 0,
1578 SRV_STATS_STATE_DOWN_AGENT,
1579 SRV_STATS_STATE_GOING_UP,
1580 SRV_STATS_STATE_UP_GOING_DOWN,
1581 SRV_STATS_STATE_UP,
1582 SRV_STATS_STATE_NOLB_GOING_DOWN,
1583 SRV_STATS_STATE_NOLB,
1584 SRV_STATS_STATE_DRAIN_GOING_DOWN,
1585 SRV_STATS_STATE_DRAIN,
1586 SRV_STATS_STATE_DRAIN_AGENT,
1587 SRV_STATS_STATE_NO_CHECK,
1588
1589 SRV_STATS_STATE_COUNT, /* Must be last */
1590};
1591
1592static const char *srv_hlt_st[SRV_STATS_STATE_COUNT] = {
1593 [SRV_STATS_STATE_DOWN] = "DOWN",
1594 [SRV_STATS_STATE_DOWN_AGENT] = "DOWN (agent)",
1595 [SRV_STATS_STATE_GOING_UP] = "DOWN %d/%d",
1596 [SRV_STATS_STATE_UP_GOING_DOWN] = "UP %d/%d",
1597 [SRV_STATS_STATE_UP] = "UP",
1598 [SRV_STATS_STATE_NOLB_GOING_DOWN] = "NOLB %d/%d",
1599 [SRV_STATS_STATE_NOLB] = "NOLB",
1600 [SRV_STATS_STATE_DRAIN_GOING_DOWN] = "DRAIN %d/%d",
1601 [SRV_STATS_STATE_DRAIN] = "DRAIN",
1602 [SRV_STATS_STATE_DRAIN_AGENT] = "DRAIN (agent)",
1603 [SRV_STATS_STATE_NO_CHECK] = "no check"
1604};
1605
1606/* Fill <stats> with the server statistics. <stats> is
1607 * preallocated array of length <len>. The length of the array
1608 * must be at least ST_F_TOTAL_FIELDS. If this length is less
1609 * then this value, the function returns 0, otherwise, it
1610 * returns 1. <flags> can take the value ST_SHLGNDS.
1611 */
1612int stats_fill_sv_stats(struct proxy *px, struct server *sv, int flags,
1613 struct field *stats, int len)
1614{
1615 struct server *via, *ref;
1616 char str[INET6_ADDRSTRLEN];
Willy Tarreau83061a82018-07-13 11:56:34 +02001617 struct buffer *out = get_trash_chunk();
William Lallemand74c24fb2016-11-21 17:18:36 +01001618 enum srv_stats_state state;
1619 char *fld_status;
1620
1621 if (len < ST_F_TOTAL_FIELDS)
1622 return 0;
1623
1624 memset(stats, 0, sizeof(*stats) * len);
1625
1626 /* we have "via" which is the tracked server as described in the configuration,
1627 * and "ref" which is the checked server and the end of the chain.
1628 */
1629 via = sv->track ? sv->track : sv;
1630 ref = via;
1631 while (ref->track)
1632 ref = ref->track;
1633
Emeric Brun52a91d32017-08-31 14:41:55 +02001634 if (sv->cur_state == SRV_ST_RUNNING || sv->cur_state == SRV_ST_STARTING) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001635 if ((ref->check.state & CHK_ST_ENABLED) &&
1636 (ref->check.health < ref->check.rise + ref->check.fall - 1)) {
1637 state = SRV_STATS_STATE_UP_GOING_DOWN;
1638 } else {
1639 state = SRV_STATS_STATE_UP;
1640 }
1641
Emeric Brun52a91d32017-08-31 14:41:55 +02001642 if (sv->cur_admin & SRV_ADMF_DRAIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001643 if (ref->agent.state & CHK_ST_ENABLED)
1644 state = SRV_STATS_STATE_DRAIN_AGENT;
1645 else if (state == SRV_STATS_STATE_UP_GOING_DOWN)
1646 state = SRV_STATS_STATE_DRAIN_GOING_DOWN;
1647 else
1648 state = SRV_STATS_STATE_DRAIN;
1649 }
1650
1651 if (state == SRV_STATS_STATE_UP && !(ref->check.state & CHK_ST_ENABLED)) {
1652 state = SRV_STATS_STATE_NO_CHECK;
1653 }
1654 }
Emeric Brun52a91d32017-08-31 14:41:55 +02001655 else if (sv->cur_state == SRV_ST_STOPPING) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001656 if ((!(sv->check.state & CHK_ST_ENABLED) && !sv->track) ||
1657 (ref->check.health == ref->check.rise + ref->check.fall - 1)) {
1658 state = SRV_STATS_STATE_NOLB;
1659 } else {
1660 state = SRV_STATS_STATE_NOLB_GOING_DOWN;
1661 }
1662 }
1663 else { /* stopped */
1664 if ((ref->agent.state & CHK_ST_ENABLED) && !ref->agent.health) {
1665 state = SRV_STATS_STATE_DOWN_AGENT;
1666 } else if ((ref->check.state & CHK_ST_ENABLED) && !ref->check.health) {
1667 state = SRV_STATS_STATE_DOWN; /* DOWN */
1668 } else if ((ref->agent.state & CHK_ST_ENABLED) || (ref->check.state & CHK_ST_ENABLED)) {
1669 state = SRV_STATS_STATE_GOING_UP;
1670 } else {
1671 state = SRV_STATS_STATE_DOWN; /* DOWN, unchecked */
1672 }
1673 }
1674
1675 chunk_reset(out);
1676
1677 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1678 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, sv->id);
1679 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1680 stats[ST_F_QCUR] = mkf_u32(0, sv->nbpend);
1681 stats[ST_F_QMAX] = mkf_u32(FN_MAX, sv->counters.nbpend_max);
1682 stats[ST_F_SCUR] = mkf_u32(0, sv->cur_sess);
1683 stats[ST_F_SMAX] = mkf_u32(FN_MAX, sv->counters.cur_sess_max);
1684
1685 if (sv->maxconn)
1686 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, sv->maxconn);
1687
Willy Tarreau0ff699e2019-09-08 09:24:56 +02001688 stats[ST_F_SRV_ICUR] = mkf_u32(0, sv->curr_idle_conns);
1689 if (sv->max_idle_conns != -1)
1690 stats[ST_F_SRV_ILIM] = mkf_u32(FO_CONFIG|FN_LIMIT, sv->max_idle_conns);
1691
William Lallemand74c24fb2016-11-21 17:18:36 +01001692 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, sv->counters.cum_sess);
1693 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, sv->counters.bytes_in);
1694 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, sv->counters.bytes_out);
1695 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, sv->counters.failed_secu);
1696 stats[ST_F_ECON] = mkf_u64(FN_COUNTER, sv->counters.failed_conns);
1697 stats[ST_F_ERESP] = mkf_u64(FN_COUNTER, sv->counters.failed_resp);
1698 stats[ST_F_WRETR] = mkf_u64(FN_COUNTER, sv->counters.retries);
1699 stats[ST_F_WREDIS] = mkf_u64(FN_COUNTER, sv->counters.redispatches);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001700 stats[ST_F_WREW] = mkf_u64(FN_COUNTER, sv->counters.failed_rewrites);
Willy Tarreauf1573842018-12-14 11:35:36 +01001701 stats[ST_F_CONNECT] = mkf_u64(FN_COUNTER, sv->counters.connect);
1702 stats[ST_F_REUSE] = mkf_u64(FN_COUNTER, sv->counters.reuse);
William Lallemand74c24fb2016-11-21 17:18:36 +01001703
1704 /* status */
1705 fld_status = chunk_newstr(out);
Emeric Brun52a91d32017-08-31 14:41:55 +02001706 if (sv->cur_admin & SRV_ADMF_RMAINT)
William Lallemand74c24fb2016-11-21 17:18:36 +01001707 chunk_appendf(out, "MAINT (resolution)");
Emeric Brun52a91d32017-08-31 14:41:55 +02001708 else if (sv->cur_admin & SRV_ADMF_IMAINT)
William Lallemand74c24fb2016-11-21 17:18:36 +01001709 chunk_appendf(out, "MAINT (via %s/%s)", via->proxy->id, via->id);
Emeric Brun52a91d32017-08-31 14:41:55 +02001710 else if (sv->cur_admin & SRV_ADMF_MAINT)
William Lallemand74c24fb2016-11-21 17:18:36 +01001711 chunk_appendf(out, "MAINT");
1712 else
1713 chunk_appendf(out,
1714 srv_hlt_st[state],
Emeric Brun52a91d32017-08-31 14:41:55 +02001715 (ref->cur_state != SRV_ST_STOPPED) ? (ref->check.health - ref->check.rise + 1) : (ref->check.health),
1716 (ref->cur_state != SRV_ST_STOPPED) ? (ref->check.fall) : (ref->check.rise));
William Lallemand74c24fb2016-11-21 17:18:36 +01001717
1718 stats[ST_F_STATUS] = mkf_str(FO_STATUS, fld_status);
1719 stats[ST_F_LASTCHG] = mkf_u32(FN_AGE, now.tv_sec - sv->last_change);
Emeric Brun52a91d32017-08-31 14:41:55 +02001720 stats[ST_F_WEIGHT] = mkf_u32(FN_AVG, (sv->cur_eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv);
William Lallemand74c24fb2016-11-21 17:18:36 +01001721 stats[ST_F_ACT] = mkf_u32(FO_STATUS, (sv->flags & SRV_F_BACKUP) ? 0 : 1);
1722 stats[ST_F_BCK] = mkf_u32(FO_STATUS, (sv->flags & SRV_F_BACKUP) ? 1 : 0);
1723
1724 /* check failures: unique, fatal; last change, total downtime */
1725 if (sv->check.state & CHK_ST_ENABLED) {
1726 stats[ST_F_CHKFAIL] = mkf_u64(FN_COUNTER, sv->counters.failed_checks);
1727 stats[ST_F_CHKDOWN] = mkf_u64(FN_COUNTER, sv->counters.down_trans);
1728 stats[ST_F_DOWNTIME] = mkf_u32(FN_COUNTER, srv_downtime(sv));
1729 }
1730
1731 if (sv->maxqueue)
1732 stats[ST_F_QLIMIT] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->maxqueue);
1733
1734 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
1735 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1736 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, sv->puid);
1737
Emeric Brun52a91d32017-08-31 14:41:55 +02001738 if (sv->cur_state == SRV_ST_STARTING && !server_is_draining(sv))
William Lallemand74c24fb2016-11-21 17:18:36 +01001739 stats[ST_F_THROTTLE] = mkf_u32(FN_AVG, server_throttle_rate(sv));
1740
1741 stats[ST_F_LBTOT] = mkf_u64(FN_COUNTER, sv->counters.cum_lbconn);
1742
1743 if (sv->track) {
1744 char *fld_track = chunk_newstr(out);
1745
1746 chunk_appendf(out, "%s/%s", sv->track->proxy->id, sv->track->id);
1747 stats[ST_F_TRACKED] = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, fld_track);
1748 }
1749
1750 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_SV);
1751 stats[ST_F_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&sv->sess_per_sec));
1752 stats[ST_F_RATE_MAX] = mkf_u32(FN_MAX, sv->counters.sps_max);
1753
1754 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) {
1755 const char *fld_chksts;
1756
1757 fld_chksts = chunk_newstr(out);
1758 chunk_strcat(out, "* "); // for check in progress
1759 chunk_strcat(out, get_check_status_info(sv->check.status));
1760 if (!(sv->check.state & CHK_ST_INPROGRESS))
1761 fld_chksts += 2; // skip "* "
1762 stats[ST_F_CHECK_STATUS] = mkf_str(FN_OUTPUT, fld_chksts);
1763
1764 if (sv->check.status >= HCHK_STATUS_L57DATA)
1765 stats[ST_F_CHECK_CODE] = mkf_u32(FN_OUTPUT, sv->check.code);
1766
1767 if (sv->check.status >= HCHK_STATUS_CHECKED)
1768 stats[ST_F_CHECK_DURATION] = mkf_u64(FN_DURATION, sv->check.duration);
1769
1770 stats[ST_F_CHECK_DESC] = mkf_str(FN_OUTPUT, get_check_status_description(sv->check.status));
1771 stats[ST_F_LAST_CHK] = mkf_str(FN_OUTPUT, sv->check.desc);
1772 stats[ST_F_CHECK_RISE] = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.rise);
1773 stats[ST_F_CHECK_FALL] = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.fall);
1774 stats[ST_F_CHECK_HEALTH] = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.health);
1775 }
1776
1777 if ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) {
1778 const char *fld_chksts;
1779
1780 fld_chksts = chunk_newstr(out);
1781 chunk_strcat(out, "* "); // for check in progress
1782 chunk_strcat(out, get_check_status_info(sv->agent.status));
1783 if (!(sv->agent.state & CHK_ST_INPROGRESS))
1784 fld_chksts += 2; // skip "* "
1785 stats[ST_F_AGENT_STATUS] = mkf_str(FN_OUTPUT, fld_chksts);
1786
1787 if (sv->agent.status >= HCHK_STATUS_L57DATA)
1788 stats[ST_F_AGENT_CODE] = mkf_u32(FN_OUTPUT, sv->agent.code);
1789
1790 if (sv->agent.status >= HCHK_STATUS_CHECKED)
1791 stats[ST_F_AGENT_DURATION] = mkf_u64(FN_DURATION, sv->agent.duration);
1792
1793 stats[ST_F_AGENT_DESC] = mkf_str(FN_OUTPUT, get_check_status_description(sv->agent.status));
1794 stats[ST_F_LAST_AGT] = mkf_str(FN_OUTPUT, sv->agent.desc);
1795 stats[ST_F_AGENT_RISE] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.rise);
1796 stats[ST_F_AGENT_FALL] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.fall);
1797 stats[ST_F_AGENT_HEALTH] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.health);
1798 }
1799
1800 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
1801 if (px->mode == PR_MODE_HTTP) {
1802 stats[ST_F_HRSP_1XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[1]);
1803 stats[ST_F_HRSP_2XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[2]);
1804 stats[ST_F_HRSP_3XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[3]);
1805 stats[ST_F_HRSP_4XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[4]);
1806 stats[ST_F_HRSP_5XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[5]);
1807 stats[ST_F_HRSP_OTHER] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[0]);
1808 }
1809
1810 if (ref->observe)
1811 stats[ST_F_HANAFAIL] = mkf_u64(FN_COUNTER, sv->counters.failed_hana);
1812
1813 stats[ST_F_CLI_ABRT] = mkf_u64(FN_COUNTER, sv->counters.cli_aborts);
1814 stats[ST_F_SRV_ABRT] = mkf_u64(FN_COUNTER, sv->counters.srv_aborts);
1815 stats[ST_F_LASTSESS] = mkf_s32(FN_AGE, srv_lastsession(sv));
1816
1817 stats[ST_F_QTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.q_time, TIME_STATS_SAMPLES));
1818 stats[ST_F_CTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.c_time, TIME_STATS_SAMPLES));
1819 stats[ST_F_RTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.d_time, TIME_STATS_SAMPLES));
1820 stats[ST_F_TTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.t_time, TIME_STATS_SAMPLES));
1821
Christopher Fauletb3fdd522019-11-08 14:59:51 +01001822 stats[ST_F_QT_MAX] = mkf_u32(FN_MAX, sv->counters.qtime_max);
1823 stats[ST_F_CT_MAX] = mkf_u32(FN_MAX, sv->counters.ctime_max);
1824 stats[ST_F_RT_MAX] = mkf_u32(FN_MAX, sv->counters.dtime_max);
1825 stats[ST_F_TT_MAX] = mkf_u32(FN_MAX, sv->counters.ttime_max);
1826
William Lallemand74c24fb2016-11-21 17:18:36 +01001827 if (flags & ST_SHLGNDS) {
1828 switch (addr_to_str(&sv->addr, str, sizeof(str))) {
1829 case AF_INET:
1830 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
Willy Tarreau04276f32017-01-06 17:41:29 +01001831 chunk_appendf(out, "%s:%d", str, sv->svc_port);
William Lallemand74c24fb2016-11-21 17:18:36 +01001832 break;
1833 case AF_INET6:
1834 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
Willy Tarreau04276f32017-01-06 17:41:29 +01001835 chunk_appendf(out, "[%s]:%d", str, sv->svc_port);
William Lallemand74c24fb2016-11-21 17:18:36 +01001836 break;
1837 case AF_UNIX:
1838 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, "unix");
1839 break;
1840 case -1:
1841 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1842 chunk_strcat(out, strerror(errno));
1843 break;
1844 default: /* address family not supported */
1845 break;
1846 }
1847
1848 if (sv->cookie)
1849 stats[ST_F_COOKIE] = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, sv->cookie);
1850 }
1851
1852 return 1;
1853}
1854
1855/* Dumps a line for server <sv> and proxy <px> to the trash and uses the state
1856 * from stream interface <si>, stats flags <flags>, and server state <state>.
1857 * The caller is responsible for clearing the trash if needed. Returns non-zero
1858 * if it emits anything, zero otherwise.
1859 */
1860static int stats_dump_sv_stats(struct stream_interface *si, struct proxy *px, int flags, struct server *sv)
1861{
1862 struct appctx *appctx = __objt_appctx(si->end);
1863
1864 if (!stats_fill_sv_stats(px, sv, flags, stats, ST_F_TOTAL_FIELDS))
1865 return 0;
1866
1867 return stats_dump_one_line(stats, flags, px, appctx);
1868}
1869
1870/* Fill <stats> with the backend statistics. <stats> is
1871 * preallocated array of length <len>. The length of the array
1872 * must be at least ST_F_TOTAL_FIELDS. If this length is less
1873 * then this value, the function returns 0, otherwise, it
1874 * returns 1. <flags> can take the value ST_SHLGNDS.
1875 */
1876int stats_fill_be_stats(struct proxy *px, int flags, struct field *stats, int len)
1877{
1878 if (len < ST_F_TOTAL_FIELDS)
1879 return 0;
1880
1881 memset(stats, 0, sizeof(*stats) * len);
1882
1883 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1884 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, "BACKEND");
1885 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1886 stats[ST_F_QCUR] = mkf_u32(0, px->nbpend);
1887 stats[ST_F_QMAX] = mkf_u32(FN_MAX, px->be_counters.nbpend_max);
Thierry FOURNIER0ff98a42016-12-19 16:50:42 +01001888 stats[ST_F_SCUR] = mkf_u32(0, px->beconn);
William Lallemand74c24fb2016-11-21 17:18:36 +01001889 stats[ST_F_SMAX] = mkf_u32(FN_MAX, px->be_counters.conn_max);
1890 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, px->fullconn);
1891 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, px->be_counters.cum_conn);
1892 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, px->be_counters.bytes_in);
1893 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, px->be_counters.bytes_out);
1894 stats[ST_F_DREQ] = mkf_u64(FN_COUNTER, px->be_counters.denied_req);
1895 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, px->be_counters.denied_resp);
1896 stats[ST_F_ECON] = mkf_u64(FN_COUNTER, px->be_counters.failed_conns);
1897 stats[ST_F_ERESP] = mkf_u64(FN_COUNTER, px->be_counters.failed_resp);
1898 stats[ST_F_WRETR] = mkf_u64(FN_COUNTER, px->be_counters.retries);
1899 stats[ST_F_WREDIS] = mkf_u64(FN_COUNTER, px->be_counters.redispatches);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001900 stats[ST_F_WREW] = mkf_u64(FN_COUNTER, px->be_counters.failed_rewrites);
Willy Tarreauf1573842018-12-14 11:35:36 +01001901 stats[ST_F_CONNECT] = mkf_u64(FN_COUNTER, px->be_counters.connect);
1902 stats[ST_F_REUSE] = mkf_u64(FN_COUNTER, px->be_counters.reuse);
William Lallemand74c24fb2016-11-21 17:18:36 +01001903 stats[ST_F_STATUS] = mkf_str(FO_STATUS, (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" : "DOWN");
1904 stats[ST_F_WEIGHT] = mkf_u32(FN_AVG, (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv);
1905 stats[ST_F_ACT] = mkf_u32(0, px->srv_act);
1906 stats[ST_F_BCK] = mkf_u32(0, px->srv_bck);
1907 stats[ST_F_CHKDOWN] = mkf_u64(FN_COUNTER, px->down_trans);
1908 stats[ST_F_LASTCHG] = mkf_u32(FN_AGE, now.tv_sec - px->last_change);
1909 if (px->srv)
1910 stats[ST_F_DOWNTIME] = mkf_u32(FN_COUNTER, be_downtime(px));
1911
1912 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
1913 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1914 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, 0);
1915 stats[ST_F_LBTOT] = mkf_u64(FN_COUNTER, px->be_counters.cum_lbconn);
1916 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_BE);
1917 stats[ST_F_RATE] = mkf_u32(0, read_freq_ctr(&px->be_sess_per_sec));
1918 stats[ST_F_RATE_MAX] = mkf_u32(0, px->be_counters.sps_max);
1919
1920 if (flags & ST_SHLGNDS) {
1921 if (px->cookie_name)
1922 stats[ST_F_COOKIE] = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, px->cookie_name);
1923 stats[ST_F_ALGO] = mkf_str(FO_CONFIG|FS_SERVICE, backend_lb_algo_str(px->lbprm.algo & BE_LB_ALGO));
1924 }
1925
1926 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
1927 if (px->mode == PR_MODE_HTTP) {
1928 stats[ST_F_REQ_TOT] = mkf_u64(FN_COUNTER, px->be_counters.p.http.cum_req);
1929 stats[ST_F_HRSP_1XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[1]);
1930 stats[ST_F_HRSP_2XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[2]);
1931 stats[ST_F_HRSP_3XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[3]);
1932 stats[ST_F_HRSP_4XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[4]);
1933 stats[ST_F_HRSP_5XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[5]);
1934 stats[ST_F_HRSP_OTHER] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[0]);
Willy Tarreaua1214a52018-12-14 14:00:25 +01001935 stats[ST_F_CACHE_LOOKUPS] = mkf_u64(FN_COUNTER, px->be_counters.p.http.cache_lookups);
1936 stats[ST_F_CACHE_HITS] = mkf_u64(FN_COUNTER, px->be_counters.p.http.cache_hits);
William Lallemand74c24fb2016-11-21 17:18:36 +01001937 }
1938
1939 stats[ST_F_CLI_ABRT] = mkf_u64(FN_COUNTER, px->be_counters.cli_aborts);
1940 stats[ST_F_SRV_ABRT] = mkf_u64(FN_COUNTER, px->be_counters.srv_aborts);
1941
1942 /* compression: in, out, bypassed, responses */
1943 stats[ST_F_COMP_IN] = mkf_u64(FN_COUNTER, px->be_counters.comp_in);
1944 stats[ST_F_COMP_OUT] = mkf_u64(FN_COUNTER, px->be_counters.comp_out);
1945 stats[ST_F_COMP_BYP] = mkf_u64(FN_COUNTER, px->be_counters.comp_byp);
1946 stats[ST_F_COMP_RSP] = mkf_u64(FN_COUNTER, px->be_counters.p.http.comp_rsp);
1947 stats[ST_F_LASTSESS] = mkf_s32(FN_AGE, be_lastsession(px));
1948
1949 stats[ST_F_QTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.q_time, TIME_STATS_SAMPLES));
1950 stats[ST_F_CTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.c_time, TIME_STATS_SAMPLES));
1951 stats[ST_F_RTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.d_time, TIME_STATS_SAMPLES));
1952 stats[ST_F_TTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.t_time, TIME_STATS_SAMPLES));
1953
Christopher Fauletb3fdd522019-11-08 14:59:51 +01001954 stats[ST_F_QT_MAX] = mkf_u32(FN_MAX, px->be_counters.qtime_max);
1955 stats[ST_F_CT_MAX] = mkf_u32(FN_MAX, px->be_counters.ctime_max);
1956 stats[ST_F_RT_MAX] = mkf_u32(FN_MAX, px->be_counters.dtime_max);
1957 stats[ST_F_TT_MAX] = mkf_u32(FN_MAX, px->be_counters.ttime_max);
1958
William Lallemand74c24fb2016-11-21 17:18:36 +01001959 return 1;
1960}
1961
1962/* Dumps a line for backend <px> to the trash for and uses the state from stream
1963 * interface <si> and stats flags <flags>. The caller is responsible for clearing
1964 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
1965 */
1966static int stats_dump_be_stats(struct stream_interface *si, struct proxy *px, int flags)
1967{
1968 struct appctx *appctx = __objt_appctx(si->end);
1969
1970 if (!(px->cap & PR_CAP_BE))
1971 return 0;
1972
1973 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_BE)))
1974 return 0;
1975
1976 if (!stats_fill_be_stats(px, flags, stats, ST_F_TOTAL_FIELDS))
1977 return 0;
1978
1979 return stats_dump_one_line(stats, flags, px, appctx);
1980}
1981
1982/* Dumps the HTML table header for proxy <px> to the trash for and uses the state from
1983 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
1984 * for clearing the trash if needed.
1985 */
1986static void stats_dump_html_px_hdr(struct stream_interface *si, struct proxy *px, struct uri_auth *uri)
1987{
1988 struct appctx *appctx = __objt_appctx(si->end);
1989 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
1990
1991 if (px->cap & PR_CAP_BE && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
1992 /* A form to enable/disable this proxy servers */
1993
1994 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
1995 scope_txt[0] = 0;
1996 if (appctx->ctx.stats.scope_len) {
Christopher Fauleted7a0662019-01-14 11:07:34 +01001997 const char *scope_ptr = stats_scope_ptr(appctx, si);
1998
William Lallemand74c24fb2016-11-21 17:18:36 +01001999 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Christopher Fauleted7a0662019-01-14 11:07:34 +01002000 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), scope_ptr, appctx->ctx.stats.scope_len);
William Lallemand74c24fb2016-11-21 17:18:36 +01002001 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
2002 }
2003
2004 chunk_appendf(&trash,
2005 "<form method=\"post\">");
2006 }
2007
2008 /* print a new table */
2009 chunk_appendf(&trash,
2010 "<table class=\"tbl\" width=\"100%%\">\n"
2011 "<tr class=\"titre\">"
2012 "<th class=\"pxname\" width=\"10%%\">");
2013
2014 chunk_appendf(&trash,
2015 "<a name=\"%s\"></a>%s"
2016 "<a class=px href=\"#%s\">%s</a>",
2017 px->id,
2018 (uri->flags & ST_SHLGNDS) ? "<u>":"",
2019 px->id, px->id);
2020
2021 if (uri->flags & ST_SHLGNDS) {
2022 /* cap, mode, id */
2023 chunk_appendf(&trash, "<div class=tips>cap: %s, mode: %s, id: %d",
2024 proxy_cap_str(px->cap), proxy_mode_str(px->mode),
2025 px->uuid);
2026 chunk_appendf(&trash, "</div>");
2027 }
2028
2029 chunk_appendf(&trash,
2030 "%s</th>"
2031 "<th class=\"%s\" width=\"90%%\">%s</th>"
2032 "</tr>\n"
2033 "</table>\n"
2034 "<table class=\"tbl\" width=\"100%%\">\n"
2035 "<tr class=\"titre\">",
2036 (uri->flags & ST_SHLGNDS) ? "</u>":"",
2037 px->desc ? "desc" : "empty", px->desc ? px->desc : "");
2038
2039 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
2040 /* Column heading for Enable or Disable server */
David Harrigand3db35a2016-12-30 12:12:49 +00002041 chunk_appendf(&trash,
2042 "<th rowspan=2 width=1><input type=\"checkbox\" \
2043 onclick=\"for(c in document.getElementsByClassName('%s-checkbox')) \
2044 document.getElementsByClassName('%s-checkbox').item(c).checked = this.checked\"></th>",
2045 px->id,
2046 px->id);
William Lallemand74c24fb2016-11-21 17:18:36 +01002047 }
2048
2049 chunk_appendf(&trash,
2050 "<th rowspan=2></th>"
2051 "<th colspan=3>Queue</th>"
2052 "<th colspan=3>Session rate</th><th colspan=6>Sessions</th>"
2053 "<th colspan=2>Bytes</th><th colspan=2>Denied</th>"
2054 "<th colspan=3>Errors</th><th colspan=2>Warnings</th>"
2055 "<th colspan=9>Server</th>"
2056 "</tr>\n"
2057 "<tr class=\"titre\">"
2058 "<th>Cur</th><th>Max</th><th>Limit</th>"
2059 "<th>Cur</th><th>Max</th><th>Limit</th><th>Cur</th><th>Max</th>"
2060 "<th>Limit</th><th>Total</th><th>LbTot</th><th>Last</th><th>In</th><th>Out</th>"
2061 "<th>Req</th><th>Resp</th><th>Req</th><th>Conn</th>"
2062 "<th>Resp</th><th>Retr</th><th>Redis</th>"
2063 "<th>Status</th><th>LastChk</th><th>Wght</th><th>Act</th>"
2064 "<th>Bck</th><th>Chk</th><th>Dwn</th><th>Dwntme</th>"
2065 "<th>Thrtle</th>\n"
2066 "</tr>");
2067}
2068
2069/* Dumps the HTML table trailer for proxy <px> to the trash for and uses the state from
2070 * stream interface <si>. The caller is responsible for clearing the trash if needed.
2071 */
2072static void stats_dump_html_px_end(struct stream_interface *si, struct proxy *px)
2073{
2074 struct appctx *appctx = __objt_appctx(si->end);
2075 chunk_appendf(&trash, "</table>");
2076
2077 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
2078 /* close the form used to enable/disable this proxy servers */
2079 chunk_appendf(&trash,
2080 "Choose the action to perform on the checked servers : "
2081 "<select name=action>"
2082 "<option value=\"\"></option>"
2083 "<option value=\"ready\">Set state to READY</option>"
2084 "<option value=\"drain\">Set state to DRAIN</option>"
2085 "<option value=\"maint\">Set state to MAINT</option>"
2086 "<option value=\"dhlth\">Health: disable checks</option>"
2087 "<option value=\"ehlth\">Health: enable checks</option>"
2088 "<option value=\"hrunn\">Health: force UP</option>"
2089 "<option value=\"hnolb\">Health: force NOLB</option>"
2090 "<option value=\"hdown\">Health: force DOWN</option>"
2091 "<option value=\"dagent\">Agent: disable checks</option>"
2092 "<option value=\"eagent\">Agent: enable checks</option>"
2093 "<option value=\"arunn\">Agent: force UP</option>"
2094 "<option value=\"adown\">Agent: force DOWN</option>"
2095 "<option value=\"shutdown\">Kill Sessions</option>"
2096 "</select>"
2097 "<input type=\"hidden\" name=\"b\" value=\"#%d\">"
2098 "&nbsp;<input type=\"submit\" value=\"Apply\">"
2099 "</form>",
2100 px->uuid);
2101 }
2102
2103 chunk_appendf(&trash, "<p>\n");
2104}
2105
2106/*
2107 * Dumps statistics for a proxy. The output is sent to the stream interface's
2108 * input buffer. Returns 0 if it had to stop dumping data because of lack of
2109 * buffer space, or non-zero if everything completed. This function is used
2110 * both by the CLI and the HTTP entry points, and is able to dump the output
2111 * in HTML or CSV formats. If the later, <uri> must be NULL.
2112 */
Christopher Fauletef779222018-10-31 08:47:01 +01002113int stats_dump_proxy_to_buffer(struct stream_interface *si, struct htx *htx,
2114 struct proxy *px, struct uri_auth *uri)
William Lallemand74c24fb2016-11-21 17:18:36 +01002115{
2116 struct appctx *appctx = __objt_appctx(si->end);
2117 struct stream *s = si_strm(si);
2118 struct channel *rep = si_ic(si);
2119 struct server *sv, *svs; /* server and server-state, server-state=server or server->track */
2120 struct listener *l;
2121 unsigned int flags;
2122
2123 if (uri)
2124 flags = uri->flags;
William Lallemand07a62f72017-05-24 00:57:40 +02002125 else if ((strm_li(s)->bind_conf->level & ACCESS_LVL_MASK) >= ACCESS_LVL_OPER)
William Lallemand74c24fb2016-11-21 17:18:36 +01002126 flags = ST_SHLGNDS | ST_SHNODE | ST_SHDESC;
2127 else
2128 flags = ST_SHNODE | ST_SHDESC;
2129
2130 chunk_reset(&trash);
2131
2132 switch (appctx->ctx.stats.px_st) {
2133 case STAT_PX_ST_INIT:
2134 /* we are on a new proxy */
2135 if (uri && uri->scope) {
2136 /* we have a limited scope, we have to check the proxy name */
2137 struct stat_scope *scope;
2138 int len;
2139
2140 len = strlen(px->id);
2141 scope = uri->scope;
2142
2143 while (scope) {
2144 /* match exact proxy name */
2145 if (scope->px_len == len && !memcmp(px->id, scope->px_id, len))
2146 break;
2147
2148 /* match '.' which means 'self' proxy */
2149 if (!strcmp(scope->px_id, ".") && px == s->be)
2150 break;
2151 scope = scope->next;
2152 }
2153
2154 /* proxy name not found : don't dump anything */
2155 if (scope == NULL)
2156 return 1;
2157 }
2158
2159 /* if the user has requested a limited output and the proxy
2160 * name does not match, skip it.
2161 */
Christopher Fauleted7a0662019-01-14 11:07:34 +01002162 if (appctx->ctx.stats.scope_len) {
2163 const char *scope_ptr = stats_scope_ptr(appctx, si);
2164
2165 if (strnistr(px->id, strlen(px->id), scope_ptr, appctx->ctx.stats.scope_len) == NULL)
2166 return 1;
2167 }
William Lallemand74c24fb2016-11-21 17:18:36 +01002168
2169 if ((appctx->ctx.stats.flags & STAT_BOUND) &&
2170 (appctx->ctx.stats.iid != -1) &&
2171 (px->uuid != appctx->ctx.stats.iid))
2172 return 1;
2173
2174 appctx->ctx.stats.px_st = STAT_PX_ST_TH;
2175 /* fall through */
2176
2177 case STAT_PX_ST_TH:
2178 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
2179 stats_dump_html_px_hdr(si, px, uri);
Christopher Fauletef779222018-10-31 08:47:01 +01002180 if (!stats_putchk(rep, htx, &trash))
2181 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002182 }
2183
2184 appctx->ctx.stats.px_st = STAT_PX_ST_FE;
2185 /* fall through */
2186
2187 case STAT_PX_ST_FE:
2188 /* print the frontend */
2189 if (stats_dump_fe_stats(si, px)) {
Christopher Fauletef779222018-10-31 08:47:01 +01002190 if (!stats_putchk(rep, htx, &trash))
2191 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002192 }
2193
2194 appctx->ctx.stats.l = px->conf.listeners.n;
2195 appctx->ctx.stats.px_st = STAT_PX_ST_LI;
2196 /* fall through */
2197
2198 case STAT_PX_ST_LI:
2199 /* stats.l has been initialized above */
2200 for (; appctx->ctx.stats.l != &px->conf.listeners; appctx->ctx.stats.l = l->by_fe.n) {
Christopher Fauletef779222018-10-31 08:47:01 +01002201 if (htx) {
2202 if (htx_almost_full(htx))
2203 goto full;
2204 }
2205 else {
2206 if (buffer_almost_full(&rep->buf))
2207 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002208 }
2209
2210 l = LIST_ELEM(appctx->ctx.stats.l, struct listener *, by_fe);
2211 if (!l->counters)
2212 continue;
2213
2214 if (appctx->ctx.stats.flags & STAT_BOUND) {
2215 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SO)))
2216 break;
2217
2218 if (appctx->ctx.stats.sid != -1 && l->luid != appctx->ctx.stats.sid)
2219 continue;
2220 }
2221
2222 /* print the frontend */
2223 if (stats_dump_li_stats(si, px, l, flags)) {
Christopher Fauletef779222018-10-31 08:47:01 +01002224 if (!stats_putchk(rep, htx, &trash))
2225 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002226 }
2227 }
2228
2229 appctx->ctx.stats.sv = px->srv; /* may be NULL */
2230 appctx->ctx.stats.px_st = STAT_PX_ST_SV;
2231 /* fall through */
2232
2233 case STAT_PX_ST_SV:
2234 /* stats.sv has been initialized above */
2235 for (; appctx->ctx.stats.sv != NULL; appctx->ctx.stats.sv = sv->next) {
Christopher Fauletef779222018-10-31 08:47:01 +01002236 if (htx) {
2237 if (htx_almost_full(htx))
2238 goto full;
2239 }
2240 else {
2241 if (buffer_almost_full(&rep->buf))
2242 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002243 }
2244
2245 sv = appctx->ctx.stats.sv;
2246
2247 if (appctx->ctx.stats.flags & STAT_BOUND) {
2248 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SV)))
2249 break;
2250
2251 if (appctx->ctx.stats.sid != -1 && sv->puid != appctx->ctx.stats.sid)
2252 continue;
2253 }
2254
2255 svs = sv;
2256 while (svs->track)
2257 svs = svs->track;
2258
2259 /* do not report servers which are DOWN and not changing state */
2260 if ((appctx->ctx.stats.flags & STAT_HIDE_DOWN) &&
Emeric Brun52a91d32017-08-31 14:41:55 +02002261 ((sv->cur_admin & SRV_ADMF_MAINT) || /* server is in maintenance */
2262 (sv->cur_state == SRV_ST_STOPPED && /* server is down */
William Lallemand74c24fb2016-11-21 17:18:36 +01002263 (!((svs->agent.state | svs->check.state) & CHK_ST_ENABLED) ||
2264 ((svs->agent.state & CHK_ST_ENABLED) && !svs->agent.health) ||
2265 ((svs->check.state & CHK_ST_ENABLED) && !svs->check.health))))) {
2266 continue;
2267 }
2268
2269 if (stats_dump_sv_stats(si, px, flags, sv)) {
Christopher Fauletef779222018-10-31 08:47:01 +01002270 if (!stats_putchk(rep, htx, &trash))
2271 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002272 }
2273 } /* for sv */
2274
2275 appctx->ctx.stats.px_st = STAT_PX_ST_BE;
2276 /* fall through */
2277
2278 case STAT_PX_ST_BE:
2279 /* print the backend */
2280 if (stats_dump_be_stats(si, px, flags)) {
Christopher Fauletef779222018-10-31 08:47:01 +01002281 if (!stats_putchk(rep, htx, &trash))
2282 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002283 }
2284
2285 appctx->ctx.stats.px_st = STAT_PX_ST_END;
2286 /* fall through */
2287
2288 case STAT_PX_ST_END:
2289 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
2290 stats_dump_html_px_end(si, px);
Christopher Fauletef779222018-10-31 08:47:01 +01002291 if (!stats_putchk(rep, htx, &trash))
2292 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002293 }
2294
2295 appctx->ctx.stats.px_st = STAT_PX_ST_FIN;
2296 /* fall through */
2297
2298 case STAT_PX_ST_FIN:
2299 return 1;
2300
2301 default:
2302 /* unknown state, we should put an abort() here ! */
2303 return 1;
2304 }
Christopher Fauletef779222018-10-31 08:47:01 +01002305
2306 full:
2307 si_rx_room_blk(si);
2308 return 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01002309}
2310
2311/* Dumps the HTTP stats head block to the trash for and uses the per-uri
2312 * parameters <uri>. The caller is responsible for clearing the trash if needed.
2313 */
2314static void stats_dump_html_head(struct uri_auth *uri)
2315{
2316 /* WARNING! This must fit in the first buffer !!! */
2317 chunk_appendf(&trash,
2318 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n"
2319 "\"http://www.w3.org/TR/html4/loose.dtd\">\n"
2320 "<html><head><title>Statistics Report for " PRODUCT_NAME "%s%s</title>\n"
2321 "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n"
2322 "<style type=\"text/css\"><!--\n"
2323 "body {"
2324 " font-family: arial, helvetica, sans-serif;"
2325 " font-size: 12px;"
2326 " font-weight: normal;"
2327 " color: black;"
2328 " background: white;"
2329 "}\n"
2330 "th,td {"
2331 " font-size: 10px;"
2332 "}\n"
2333 "h1 {"
2334 " font-size: x-large;"
2335 " margin-bottom: 0.5em;"
2336 "}\n"
2337 "h2 {"
2338 " font-family: helvetica, arial;"
2339 " font-size: x-large;"
2340 " font-weight: bold;"
2341 " font-style: italic;"
2342 " color: #6020a0;"
2343 " margin-top: 0em;"
2344 " margin-bottom: 0em;"
2345 "}\n"
2346 "h3 {"
2347 " font-family: helvetica, arial;"
2348 " font-size: 16px;"
2349 " font-weight: bold;"
2350 " color: #b00040;"
2351 " background: #e8e8d0;"
2352 " margin-top: 0em;"
2353 " margin-bottom: 0em;"
2354 "}\n"
2355 "li {"
2356 " margin-top: 0.25em;"
2357 " margin-right: 2em;"
2358 "}\n"
2359 ".hr {margin-top: 0.25em;"
2360 " border-color: black;"
2361 " border-bottom-style: solid;"
2362 "}\n"
2363 ".titre {background: #20D0D0;color: #000000; font-weight: bold; text-align: center;}\n"
2364 ".total {background: #20D0D0;color: #ffff80;}\n"
2365 ".frontend {background: #e8e8d0;}\n"
2366 ".socket {background: #d0d0d0;}\n"
2367 ".backend {background: #e8e8d0;}\n"
2368 ".active_down {background: #ff9090;}\n"
2369 ".active_going_up {background: #ffd020;}\n"
2370 ".active_going_down {background: #ffffa0;}\n"
2371 ".active_up {background: #c0ffc0;}\n"
2372 ".active_nolb {background: #20a0ff;}\n"
2373 ".active_draining {background: #20a0FF;}\n"
2374 ".active_no_check {background: #e0e0e0;}\n"
2375 ".backup_down {background: #ff9090;}\n"
2376 ".backup_going_up {background: #ff80ff;}\n"
2377 ".backup_going_down {background: #c060ff;}\n"
2378 ".backup_up {background: #b0d0ff;}\n"
2379 ".backup_nolb {background: #90b0e0;}\n"
2380 ".backup_draining {background: #cc9900;}\n"
2381 ".backup_no_check {background: #e0e0e0;}\n"
2382 ".maintain {background: #c07820;}\n"
2383 ".rls {letter-spacing: 0.2em; margin-right: 1px;}\n" /* right letter spacing (used for grouping digits) */
2384 "\n"
2385 "a.px:link {color: #ffff40; text-decoration: none;}"
2386 "a.px:visited {color: #ffff40; text-decoration: none;}"
2387 "a.px:hover {color: #ffffff; text-decoration: none;}"
2388 "a.lfsb:link {color: #000000; text-decoration: none;}"
2389 "a.lfsb:visited {color: #000000; text-decoration: none;}"
2390 "a.lfsb:hover {color: #505050; text-decoration: none;}"
2391 "\n"
2392 "table.tbl { border-collapse: collapse; border-style: none;}\n"
2393 "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"
2394 "table.tbl td.ac { text-align: center;}\n"
2395 "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}\n"
2396 "table.tbl th.pxname { background: #b00040; color: #ffff40; font-weight: bold; border-style: solid solid none solid; padding: 2px 3px; white-space: nowrap;}\n"
2397 "table.tbl th.empty { border-style: none; empty-cells: hide; background: white;}\n"
2398 "table.tbl th.desc { background: white; border-style: solid solid none solid; text-align: left; padding: 2px 3px;}\n"
2399 "\n"
2400 "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n"
2401 "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n"
2402 "table.lgd td.noborder { border-style: none; padding: 2px; white-space: nowrap;}\n"
2403 "table.det { border-collapse: collapse; border-style: none; }\n"
2404 "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"
2405 "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"
2406 "u {text-decoration:none; border-bottom: 1px dotted black;}\n"
2407 "div.tips {\n"
2408 " display:block;\n"
2409 " visibility:hidden;\n"
2410 " z-index:2147483647;\n"
2411 " position:absolute;\n"
2412 " padding:2px 4px 3px;\n"
2413 " background:#f0f060; color:#000000;\n"
2414 " border:1px solid #7040c0;\n"
2415 " white-space:nowrap;\n"
2416 " font-style:normal;font-size:11px;font-weight:normal;\n"
2417 " -moz-border-radius:3px;-webkit-border-radius:3px;border-radius:3px;\n"
2418 " -moz-box-shadow:gray 2px 2px 3px;-webkit-box-shadow:gray 2px 2px 3px;box-shadow:gray 2px 2px 3px;\n"
2419 "}\n"
2420 "u:hover div.tips {visibility:visible;}\n"
2421 "-->\n"
2422 "</style></head>\n",
2423 (uri->flags & ST_SHNODE) ? " on " : "",
2424 (uri->flags & ST_SHNODE) ? (uri->node ? uri->node : global.node) : ""
2425 );
2426}
2427
2428/* Dumps the HTML stats information block to the trash for and uses the state from
2429 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
2430 * for clearing the trash if needed.
2431 */
2432static void stats_dump_html_info(struct stream_interface *si, struct uri_auth *uri)
2433{
2434 struct appctx *appctx = __objt_appctx(si->end);
2435 unsigned int up = (now.tv_sec - start_date.tv_sec);
2436 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
Christopher Fauleted7a0662019-01-14 11:07:34 +01002437 const char *scope_ptr = stats_scope_ptr(appctx, si);
Willy Tarreau1713c032019-05-23 12:23:55 +02002438 unsigned long long bps = (unsigned long long)read_freq_ctr(&global.out_32bps) * 32;
2439
2440 /* Turn the bytes per second to bits per second and take care of the
2441 * usual ethernet overhead in order to help figure how far we are from
2442 * interface saturation since it's the only case which usually matters.
2443 * For this we count the total size of an Ethernet frame on the wire
2444 * including preamble and IFG (1538) for the largest TCP segment it
2445 * transports (1448 with TCP timestamps). This is not valid for smaller
2446 * packets (under-estimated), but it gives a reasonably accurate
2447 * estimation of how far we are from uplink saturation.
2448 */
2449 bps = bps * 8 * 1538 / 1448;
William Lallemand74c24fb2016-11-21 17:18:36 +01002450
2451 /* WARNING! this has to fit the first packet too.
2452 * We are around 3.5 kB, add adding entries will
2453 * become tricky if we want to support 4kB buffers !
2454 */
2455 chunk_appendf(&trash,
2456 "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">"
2457 PRODUCT_NAME "%s</a></h1>\n"
2458 "<h2>Statistics Report for pid %d%s%s%s%s</h2>\n"
2459 "<hr width=\"100%%\" class=\"hr\">\n"
2460 "<h3>&gt; General process information</h3>\n"
2461 "<table border=0><tr><td align=\"left\" nowrap width=\"1%%\">\n"
Yves Lafon95317282018-02-26 11:10:37 +01002462 "<p><b>pid = </b> %d (process #%d, nbproc = %d, nbthread = %d)<br>\n"
William Lallemand74c24fb2016-11-21 17:18:36 +01002463 "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n"
2464 "<b>system limits:</b> memmax = %s%s; ulimit-n = %d<br>\n"
2465 "<b>maxsock = </b> %d; <b>maxconn = </b> %d; <b>maxpipes = </b> %d<br>\n"
Willy Tarreau1713c032019-05-23 12:23:55 +02002466 "current conns = %d; current pipes = %d/%d; conn rate = %d/sec; bit rate = %.3f %cbps<br>\n"
William Lallemand74c24fb2016-11-21 17:18:36 +01002467 "Running tasks: %d/%d; idle = %d %%<br>\n"
2468 "</td><td align=\"center\" nowrap>\n"
2469 "<table class=\"lgd\"><tr>\n"
2470 "<td class=\"active_up\">&nbsp;</td><td class=\"noborder\">active UP </td>"
2471 "<td class=\"backup_up\">&nbsp;</td><td class=\"noborder\">backup UP </td>"
2472 "</tr><tr>\n"
2473 "<td class=\"active_going_down\"></td><td class=\"noborder\">active UP, going down </td>"
2474 "<td class=\"backup_going_down\"></td><td class=\"noborder\">backup UP, going down </td>"
2475 "</tr><tr>\n"
2476 "<td class=\"active_going_up\"></td><td class=\"noborder\">active DOWN, going up </td>"
2477 "<td class=\"backup_going_up\"></td><td class=\"noborder\">backup DOWN, going up </td>"
2478 "</tr><tr>\n"
2479 "<td class=\"active_down\"></td><td class=\"noborder\">active or backup DOWN &nbsp;</td>"
2480 "<td class=\"active_no_check\"></td><td class=\"noborder\">not checked </td>"
2481 "</tr><tr>\n"
2482 "<td class=\"maintain\"></td><td class=\"noborder\" colspan=\"3\">active or backup DOWN for maintenance (MAINT) &nbsp;</td>"
2483 "</tr><tr>\n"
2484 "<td class=\"active_draining\"></td><td class=\"noborder\" colspan=\"3\">active or backup SOFT STOPPED for maintenance &nbsp;</td>"
2485 "</tr></table>\n"
2486 "Note: \"NOLB\"/\"DRAIN\" = UP with load-balancing disabled."
2487 "</td>"
2488 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
2489 "<b>Display option:</b><ul style=\"margin-top: 0.25em;\">"
2490 "",
Willy Tarreau909b9d82019-01-04 18:20:32 +01002491 (uri->flags & ST_HIDEVER) ? "" : (stats_version_string),
William Lallemand74c24fb2016-11-21 17:18:36 +01002492 pid, (uri->flags & ST_SHNODE) ? " on " : "",
2493 (uri->flags & ST_SHNODE) ? (uri->node ? uri->node : global.node) : "",
2494 (uri->flags & ST_SHDESC) ? ": " : "",
2495 (uri->flags & ST_SHDESC) ? (uri->desc ? uri->desc : global.desc) : "",
Yves Lafon95317282018-02-26 11:10:37 +01002496 pid, relative_pid, global.nbproc, global.nbthread,
William Lallemand74c24fb2016-11-21 17:18:36 +01002497 up / 86400, (up % 86400) / 3600,
2498 (up % 3600) / 60, (up % 60),
2499 global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited",
2500 global.rlimit_memmax ? " MB" : "",
2501 global.rlimit_nofile,
2502 global.maxsock, global.maxconn, global.maxpipes,
2503 actconn, pipes_used, pipes_used+pipes_free, read_freq_ctr(&global.conn_per_sec),
Willy Tarreau1713c032019-05-23 12:23:55 +02002504 bps >= 1000000000UL ? (bps / 1000000000.0) : bps >= 1000000UL ? (bps / 1000000.0) : (bps / 1000.0),
2505 bps >= 1000000000UL ? 'G' : bps >= 1000000UL ? 'M' : 'k',
Willy Tarreau81036f22019-05-20 19:24:50 +02002506 tasks_run_queue_cur, nb_tasks_cur, ti->idle_pct
William Lallemand74c24fb2016-11-21 17:18:36 +01002507 );
2508
2509 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Christopher Fauleted7a0662019-01-14 11:07:34 +01002510 memcpy(scope_txt, scope_ptr, appctx->ctx.stats.scope_len);
William Lallemand74c24fb2016-11-21 17:18:36 +01002511 scope_txt[appctx->ctx.stats.scope_len] = '\0';
2512
2513 chunk_appendf(&trash,
2514 "<li><form method=\"GET\">Scope : <input value=\"%s\" name=\"" STAT_SCOPE_INPUT_NAME "\" size=\"8\" maxlength=\"%d\" tabindex=\"1\"/></form>\n",
2515 (appctx->ctx.stats.scope_len > 0) ? scope_txt : "",
2516 STAT_SCOPE_TXT_MAXLEN);
2517
2518 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
2519 scope_txt[0] = 0;
2520 if (appctx->ctx.stats.scope_len) {
2521 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Christopher Fauleted7a0662019-01-14 11:07:34 +01002522 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), scope_ptr, appctx->ctx.stats.scope_len);
William Lallemand74c24fb2016-11-21 17:18:36 +01002523 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
2524 }
2525
2526 if (appctx->ctx.stats.flags & STAT_HIDE_DOWN)
2527 chunk_appendf(&trash,
2528 "<li><a href=\"%s%s%s%s\">Show all servers</a><br>\n",
2529 uri->uri_prefix,
2530 "",
2531 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2532 scope_txt);
2533 else
2534 chunk_appendf(&trash,
2535 "<li><a href=\"%s%s%s%s\">Hide 'DOWN' servers</a><br>\n",
2536 uri->uri_prefix,
2537 ";up",
2538 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2539 scope_txt);
2540
2541 if (uri->refresh > 0) {
2542 if (appctx->ctx.stats.flags & STAT_NO_REFRESH)
2543 chunk_appendf(&trash,
2544 "<li><a href=\"%s%s%s%s\">Enable refresh</a><br>\n",
2545 uri->uri_prefix,
2546 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2547 "",
2548 scope_txt);
2549 else
2550 chunk_appendf(&trash,
2551 "<li><a href=\"%s%s%s%s\">Disable refresh</a><br>\n",
2552 uri->uri_prefix,
2553 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2554 ";norefresh",
2555 scope_txt);
2556 }
2557
2558 chunk_appendf(&trash,
2559 "<li><a href=\"%s%s%s%s\">Refresh now</a><br>\n",
2560 uri->uri_prefix,
2561 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2562 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2563 scope_txt);
2564
2565 chunk_appendf(&trash,
2566 "<li><a href=\"%s;csv%s%s\">CSV export</a><br>\n",
2567 uri->uri_prefix,
2568 (uri->refresh > 0) ? ";norefresh" : "",
2569 scope_txt);
2570
2571 chunk_appendf(&trash,
2572 "</ul></td>"
2573 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
2574 "<b>External resources:</b><ul style=\"margin-top: 0.25em;\">\n"
2575 "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>\n"
2576 "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>\n"
2577 "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>\n"
2578 "</ul>"
2579 "</td>"
2580 "</tr></table>\n"
2581 ""
2582 );
2583
2584 if (appctx->ctx.stats.st_code) {
2585 switch (appctx->ctx.stats.st_code) {
2586 case STAT_STATUS_DONE:
2587 chunk_appendf(&trash,
2588 "<p><div class=active_up>"
2589 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2590 "Action processed successfully."
2591 "</div>\n", uri->uri_prefix,
2592 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2593 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2594 scope_txt);
2595 break;
2596 case STAT_STATUS_NONE:
2597 chunk_appendf(&trash,
2598 "<p><div class=active_going_down>"
2599 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2600 "Nothing has changed."
2601 "</div>\n", uri->uri_prefix,
2602 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2603 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2604 scope_txt);
2605 break;
2606 case STAT_STATUS_PART:
2607 chunk_appendf(&trash,
2608 "<p><div class=active_going_down>"
2609 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2610 "Action partially processed.<br>"
2611 "Some server names are probably unknown or ambiguous (duplicated names in the backend)."
2612 "</div>\n", uri->uri_prefix,
2613 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2614 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2615 scope_txt);
2616 break;
2617 case STAT_STATUS_ERRP:
2618 chunk_appendf(&trash,
2619 "<p><div class=active_down>"
2620 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2621 "Action not processed because of invalid parameters."
2622 "<ul>"
2623 "<li>The action is maybe unknown.</li>"
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01002624 "<li>Invalid key parameter (empty or too long).</li>"
William Lallemand74c24fb2016-11-21 17:18:36 +01002625 "<li>The backend name is probably unknown or ambiguous (duplicated names).</li>"
2626 "<li>Some server names are probably unknown or ambiguous (duplicated names in the backend).</li>"
2627 "</ul>"
2628 "</div>\n", uri->uri_prefix,
2629 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2630 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2631 scope_txt);
2632 break;
2633 case STAT_STATUS_EXCD:
2634 chunk_appendf(&trash,
2635 "<p><div class=active_down>"
2636 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2637 "<b>Action not processed : the buffer couldn't store all the data.<br>"
2638 "You should retry with less servers at a time.</b>"
2639 "</div>\n", uri->uri_prefix,
2640 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2641 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2642 scope_txt);
2643 break;
2644 case STAT_STATUS_DENY:
2645 chunk_appendf(&trash,
2646 "<p><div class=active_down>"
2647 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2648 "<b>Action denied.</b>"
2649 "</div>\n", uri->uri_prefix,
2650 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2651 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2652 scope_txt);
2653 break;
Christopher Faulet3c2ecf72019-02-27 16:41:27 +01002654 case STAT_STATUS_IVAL:
2655 chunk_appendf(&trash,
2656 "<p><div class=active_down>"
2657 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2658 "<b>Invalid requests (unsupported method or chunked encoded request).</b>"
2659 "</div>\n", uri->uri_prefix,
2660 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2661 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2662 scope_txt);
2663 break;
William Lallemand74c24fb2016-11-21 17:18:36 +01002664 default:
2665 chunk_appendf(&trash,
2666 "<p><div class=active_no_check>"
2667 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2668 "Unexpected result."
2669 "</div>\n", uri->uri_prefix,
2670 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2671 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2672 scope_txt);
2673 }
2674 chunk_appendf(&trash, "<p>\n");
2675 }
2676}
2677
2678/* Dumps the HTML stats trailer block to the trash. The caller is responsible
2679 * for clearing the trash if needed.
2680 */
2681static void stats_dump_html_end()
2682{
2683 chunk_appendf(&trash, "</body></html>\n");
2684}
2685
Simon Horman05ee2132017-01-04 09:37:25 +01002686/* Dumps the stats JSON header to the trash buffer which. The caller is responsible
2687 * for clearing it if needed.
2688 */
2689static void stats_dump_json_header()
2690{
2691 chunk_strcat(&trash, "[");
2692}
2693
2694
2695/* Dumps the JSON stats trailer block to the trash. The caller is responsible
2696 * for clearing the trash if needed.
2697 */
2698static void stats_dump_json_end()
2699{
2700 chunk_strcat(&trash, "]");
2701}
2702
William Lallemand74c24fb2016-11-21 17:18:36 +01002703/* This function dumps statistics onto the stream interface's read buffer in
2704 * either CSV or HTML format. <uri> contains some HTML-specific parameters that
2705 * are ignored for CSV format (hence <uri> may be NULL there). It returns 0 if
2706 * it had to stop writing data and an I/O is needed, 1 if the dump is finished
2707 * and the stream must be closed, or -1 in case of any error. This function is
2708 * used by both the CLI and the HTTP handlers.
2709 */
Christopher Fauletef779222018-10-31 08:47:01 +01002710static int stats_dump_stat_to_buffer(struct stream_interface *si, struct htx *htx,
2711 struct uri_auth *uri)
William Lallemand74c24fb2016-11-21 17:18:36 +01002712{
2713 struct appctx *appctx = __objt_appctx(si->end);
2714 struct channel *rep = si_ic(si);
2715 struct proxy *px;
2716
2717 chunk_reset(&trash);
2718
2719 switch (appctx->st2) {
2720 case STAT_ST_INIT:
2721 appctx->st2 = STAT_ST_HEAD; /* let's start producing data */
2722 /* fall through */
2723
2724 case STAT_ST_HEAD:
2725 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
2726 stats_dump_html_head(uri);
Simon Horman05ee2132017-01-04 09:37:25 +01002727 else if (appctx->ctx.stats.flags & STAT_FMT_JSON)
Willy Tarreau9d7fb632017-04-11 07:53:04 +02002728 stats_dump_json_header();
William Lallemand74c24fb2016-11-21 17:18:36 +01002729 else if (!(appctx->ctx.stats.flags & STAT_FMT_TYPED))
2730 stats_dump_csv_header();
2731
Christopher Fauletef779222018-10-31 08:47:01 +01002732 if (!stats_putchk(rep, htx, &trash))
2733 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002734
2735 appctx->st2 = STAT_ST_INFO;
2736 /* fall through */
2737
2738 case STAT_ST_INFO:
2739 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
2740 stats_dump_html_info(si, uri);
Christopher Fauletef779222018-10-31 08:47:01 +01002741 if (!stats_putchk(rep, htx, &trash))
2742 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002743 }
2744
Olivier Houchardfbc74e82017-11-24 16:54:05 +01002745 appctx->ctx.stats.px = proxies_list;
William Lallemand74c24fb2016-11-21 17:18:36 +01002746 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
2747 appctx->st2 = STAT_ST_LIST;
2748 /* fall through */
2749
2750 case STAT_ST_LIST:
2751 /* dump proxies */
2752 while (appctx->ctx.stats.px) {
Christopher Fauletef779222018-10-31 08:47:01 +01002753 if (htx) {
2754 if (htx_almost_full(htx))
2755 goto full;
2756 }
2757 else {
2758 if (buffer_almost_full(&rep->buf))
2759 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002760 }
2761
2762 px = appctx->ctx.stats.px;
2763 /* skip the disabled proxies, global frontend and non-networked ones */
2764 if (px->state != PR_STSTOPPED && px->uuid > 0 && (px->cap & (PR_CAP_FE | PR_CAP_BE)))
Christopher Fauletef779222018-10-31 08:47:01 +01002765 if (stats_dump_proxy_to_buffer(si, htx, px, uri) == 0)
William Lallemand74c24fb2016-11-21 17:18:36 +01002766 return 0;
2767
2768 appctx->ctx.stats.px = px->next;
2769 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
2770 }
2771 /* here, we just have reached the last proxy */
2772
2773 appctx->st2 = STAT_ST_END;
2774 /* fall through */
2775
2776 case STAT_ST_END:
Simon Horman05ee2132017-01-04 09:37:25 +01002777 if (appctx->ctx.stats.flags & (STAT_FMT_HTML|STAT_FMT_JSON)) {
2778 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
2779 stats_dump_html_end();
2780 else
2781 stats_dump_json_end();
Christopher Fauletef779222018-10-31 08:47:01 +01002782 if (!stats_putchk(rep, htx, &trash))
2783 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002784 }
2785
2786 appctx->st2 = STAT_ST_FIN;
2787 /* fall through */
2788
2789 case STAT_ST_FIN:
2790 return 1;
2791
2792 default:
2793 /* unknown state ! */
2794 appctx->st2 = STAT_ST_FIN;
2795 return -1;
2796 }
Christopher Fauletef779222018-10-31 08:47:01 +01002797
2798 full:
2799 si_rx_room_blk(si);
2800 return 0;
2801
William Lallemand74c24fb2016-11-21 17:18:36 +01002802}
2803
2804/* We reached the stats page through a POST request. The appctx is
2805 * expected to have already been allocated by the caller.
2806 * Parse the posted data and enable/disable servers if necessary.
2807 * Returns 1 if request was parsed or zero if it needs more data.
2808 */
2809static int stats_process_http_post(struct stream_interface *si)
2810{
2811 struct stream *s = si_strm(si);
2812 struct appctx *appctx = objt_appctx(si->end);
2813
2814 struct proxy *px = NULL;
2815 struct server *sv = NULL;
2816
2817 char key[LINESIZE];
2818 int action = ST_ADM_ACTION_NONE;
2819 int reprocess = 0;
2820
2821 int total_servers = 0;
2822 int altered_servers = 0;
2823
2824 char *first_param, *cur_param, *next_param, *end_params;
2825 char *st_cur_param = NULL;
2826 char *st_next_param = NULL;
2827
Christopher Fauleta3618372018-12-13 21:59:56 +01002828 struct buffer *temp = get_trash_chunk();
William Lallemand74c24fb2016-11-21 17:18:36 +01002829
Christopher Fauleta3618372018-12-13 21:59:56 +01002830 if (IS_HTX_STRM(s)) {
2831 struct htx *htx = htxbuf(&s->req.buf);
2832 struct htx_blk *blk;
Christopher Fauleta3618372018-12-13 21:59:56 +01002833
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01002834 /* we need more data */
2835 if (s->txn->req.msg_state < HTTP_MSG_DONE) {
2836 /* check if we can receive more */
2837 if (htx_free_data_space(htx) <= global.tune.maxrewrite) {
2838 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
2839 goto out;
Christopher Fauleta3618372018-12-13 21:59:56 +01002840 }
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01002841 goto wait;
Christopher Fauleta3618372018-12-13 21:59:56 +01002842 }
William Lallemand74c24fb2016-11-21 17:18:36 +01002843
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01002844 /* The request was fully received. Copy data */
Christopher Faulet8fa60e42019-05-23 11:04:05 +02002845 blk = htx_get_head_blk(htx);
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01002846 while (blk) {
Christopher Fauleta3618372018-12-13 21:59:56 +01002847 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Fauleta3618372018-12-13 21:59:56 +01002848
Christopher Faulet54b5e212019-06-04 10:08:28 +02002849 if (type == HTX_BLK_EOM || type == HTX_BLK_TLR || type == HTX_BLK_EOT)
Christopher Fauleta3618372018-12-13 21:59:56 +01002850 break;
2851 if (type == HTX_BLK_DATA) {
2852 struct ist v = htx_get_blk_value(htx, blk);
2853
2854 if (!chunk_memcat(temp, v.ptr, v.len)) {
2855 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
2856 goto out;
2857 }
2858 }
Christopher Fauleted7a0662019-01-14 11:07:34 +01002859 blk = htx_get_next_blk(htx, blk);
Christopher Fauleta3618372018-12-13 21:59:56 +01002860 }
2861 }
2862 else {
2863 int reql;
2864
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01002865 /* we need more data */
2866 if (s->txn->req.msg_state < HTTP_MSG_DONE) {
2867 /* check if we can receive more */
2868 if (c_room(&s->req) <= global.tune.maxrewrite) {
2869 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
2870 goto out;
2871 }
2872 goto wait;
Christopher Fauleta3618372018-12-13 21:59:56 +01002873 }
Christopher Fauleta3618372018-12-13 21:59:56 +01002874 reql = co_getblk(si_oc(si), temp->area, s->txn->req.body_len,
2875 s->txn->req.eoh + 2);
2876 if (reql <= 0) {
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01002877 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
2878 goto out;
Christopher Fauleta3618372018-12-13 21:59:56 +01002879 }
2880 temp->data = reql;
William Lallemand74c24fb2016-11-21 17:18:36 +01002881 }
2882
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002883 first_param = temp->area;
Christopher Fauleta3618372018-12-13 21:59:56 +01002884 end_params = temp->area + temp->data;
William Lallemand74c24fb2016-11-21 17:18:36 +01002885 cur_param = next_param = end_params;
2886 *end_params = '\0';
2887
2888 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
2889
2890 /*
2891 * Parse the parameters in reverse order to only store the last value.
2892 * From the html form, the backend and the action are at the end.
2893 */
2894 while (cur_param > first_param) {
2895 char *value;
2896 int poffset, plen;
2897
2898 cur_param--;
2899
2900 if ((*cur_param == '&') || (cur_param == first_param)) {
2901 reprocess_servers:
2902 /* Parse the key */
2903 poffset = (cur_param != first_param ? 1 : 0);
2904 plen = next_param - cur_param + (cur_param == first_param ? 1 : 0);
2905 if ((plen > 0) && (plen <= sizeof(key))) {
2906 strncpy(key, cur_param + poffset, plen);
2907 key[plen - 1] = '\0';
2908 } else {
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01002909 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
William Lallemand74c24fb2016-11-21 17:18:36 +01002910 goto out;
2911 }
2912
2913 /* Parse the value */
2914 value = key;
2915 while (*value != '\0' && *value != '=') {
2916 value++;
2917 }
2918 if (*value == '=') {
2919 /* Ok, a value is found, we can mark the end of the key */
2920 *value++ = '\0';
2921 }
Willy Tarreau7e913cb2020-04-23 17:54:47 +02002922 if (url_decode(key, 1) < 0 || url_decode(value, 1) < 0)
William Lallemand74c24fb2016-11-21 17:18:36 +01002923 break;
2924
2925 /* Now we can check the key to see what to do */
2926 if (!px && (strcmp(key, "b") == 0)) {
2927 if ((px = proxy_be_by_name(value)) == NULL) {
2928 /* the backend name is unknown or ambiguous (duplicate names) */
2929 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
2930 goto out;
2931 }
2932 }
2933 else if (!action && (strcmp(key, "action") == 0)) {
2934 if (strcmp(value, "ready") == 0) {
2935 action = ST_ADM_ACTION_READY;
2936 }
2937 else if (strcmp(value, "drain") == 0) {
2938 action = ST_ADM_ACTION_DRAIN;
2939 }
2940 else if (strcmp(value, "maint") == 0) {
2941 action = ST_ADM_ACTION_MAINT;
2942 }
2943 else if (strcmp(value, "shutdown") == 0) {
2944 action = ST_ADM_ACTION_SHUTDOWN;
2945 }
2946 else if (strcmp(value, "dhlth") == 0) {
2947 action = ST_ADM_ACTION_DHLTH;
2948 }
2949 else if (strcmp(value, "ehlth") == 0) {
2950 action = ST_ADM_ACTION_EHLTH;
2951 }
2952 else if (strcmp(value, "hrunn") == 0) {
2953 action = ST_ADM_ACTION_HRUNN;
2954 }
2955 else if (strcmp(value, "hnolb") == 0) {
2956 action = ST_ADM_ACTION_HNOLB;
2957 }
2958 else if (strcmp(value, "hdown") == 0) {
2959 action = ST_ADM_ACTION_HDOWN;
2960 }
2961 else if (strcmp(value, "dagent") == 0) {
2962 action = ST_ADM_ACTION_DAGENT;
2963 }
2964 else if (strcmp(value, "eagent") == 0) {
2965 action = ST_ADM_ACTION_EAGENT;
2966 }
2967 else if (strcmp(value, "arunn") == 0) {
2968 action = ST_ADM_ACTION_ARUNN;
2969 }
2970 else if (strcmp(value, "adown") == 0) {
2971 action = ST_ADM_ACTION_ADOWN;
2972 }
2973 /* else these are the old supported methods */
2974 else if (strcmp(value, "disable") == 0) {
2975 action = ST_ADM_ACTION_DISABLE;
2976 }
2977 else if (strcmp(value, "enable") == 0) {
2978 action = ST_ADM_ACTION_ENABLE;
2979 }
2980 else if (strcmp(value, "stop") == 0) {
2981 action = ST_ADM_ACTION_STOP;
2982 }
2983 else if (strcmp(value, "start") == 0) {
2984 action = ST_ADM_ACTION_START;
2985 }
2986 else {
2987 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
2988 goto out;
2989 }
2990 }
2991 else if (strcmp(key, "s") == 0) {
2992 if (!(px && action)) {
2993 /*
2994 * Indicates that we'll need to reprocess the parameters
2995 * as soon as backend and action are known
2996 */
2997 if (!reprocess) {
2998 st_cur_param = cur_param;
2999 st_next_param = next_param;
3000 }
3001 reprocess = 1;
3002 }
3003 else if ((sv = findserver(px, value)) != NULL) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003004 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
William Lallemand74c24fb2016-11-21 17:18:36 +01003005 switch (action) {
3006 case ST_ADM_ACTION_DISABLE:
Emeric Brun52a91d32017-08-31 14:41:55 +02003007 if (!(sv->cur_admin & SRV_ADMF_FMAINT)) {
William Lallemand74c24fb2016-11-21 17:18:36 +01003008 altered_servers++;
3009 total_servers++;
3010 srv_set_admin_flag(sv, SRV_ADMF_FMAINT, "'disable' on stats page");
3011 }
3012 break;
3013 case ST_ADM_ACTION_ENABLE:
Emeric Brun52a91d32017-08-31 14:41:55 +02003014 if (sv->cur_admin & SRV_ADMF_FMAINT) {
William Lallemand74c24fb2016-11-21 17:18:36 +01003015 altered_servers++;
3016 total_servers++;
3017 srv_clr_admin_flag(sv, SRV_ADMF_FMAINT);
3018 }
3019 break;
3020 case ST_ADM_ACTION_STOP:
Emeric Brun52a91d32017-08-31 14:41:55 +02003021 if (!(sv->cur_admin & SRV_ADMF_FDRAIN)) {
William Lallemand74c24fb2016-11-21 17:18:36 +01003022 srv_set_admin_flag(sv, SRV_ADMF_FDRAIN, "'stop' on stats page");
3023 altered_servers++;
3024 total_servers++;
3025 }
3026 break;
3027 case ST_ADM_ACTION_START:
Emeric Brun52a91d32017-08-31 14:41:55 +02003028 if (sv->cur_admin & SRV_ADMF_FDRAIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01003029 srv_clr_admin_flag(sv, SRV_ADMF_FDRAIN);
3030 altered_servers++;
3031 total_servers++;
3032 }
3033 break;
3034 case ST_ADM_ACTION_DHLTH:
3035 if (sv->check.state & CHK_ST_CONFIGURED) {
3036 sv->check.state &= ~CHK_ST_ENABLED;
3037 altered_servers++;
3038 total_servers++;
3039 }
3040 break;
3041 case ST_ADM_ACTION_EHLTH:
3042 if (sv->check.state & CHK_ST_CONFIGURED) {
3043 sv->check.state |= CHK_ST_ENABLED;
3044 altered_servers++;
3045 total_servers++;
3046 }
3047 break;
3048 case ST_ADM_ACTION_HRUNN:
3049 if (!(sv->track)) {
3050 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02003051 srv_set_running(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003052 altered_servers++;
3053 total_servers++;
3054 }
3055 break;
3056 case ST_ADM_ACTION_HNOLB:
3057 if (!(sv->track)) {
3058 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02003059 srv_set_stopping(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003060 altered_servers++;
3061 total_servers++;
3062 }
3063 break;
3064 case ST_ADM_ACTION_HDOWN:
3065 if (!(sv->track)) {
3066 sv->check.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02003067 srv_set_stopped(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003068 altered_servers++;
3069 total_servers++;
3070 }
3071 break;
3072 case ST_ADM_ACTION_DAGENT:
3073 if (sv->agent.state & CHK_ST_CONFIGURED) {
3074 sv->agent.state &= ~CHK_ST_ENABLED;
3075 altered_servers++;
3076 total_servers++;
3077 }
3078 break;
3079 case ST_ADM_ACTION_EAGENT:
3080 if (sv->agent.state & CHK_ST_CONFIGURED) {
3081 sv->agent.state |= CHK_ST_ENABLED;
3082 altered_servers++;
3083 total_servers++;
3084 }
3085 break;
3086 case ST_ADM_ACTION_ARUNN:
3087 if (sv->agent.state & CHK_ST_ENABLED) {
3088 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02003089 srv_set_running(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003090 altered_servers++;
3091 total_servers++;
3092 }
3093 break;
3094 case ST_ADM_ACTION_ADOWN:
3095 if (sv->agent.state & CHK_ST_ENABLED) {
3096 sv->agent.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02003097 srv_set_stopped(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003098 altered_servers++;
3099 total_servers++;
3100 }
3101 break;
3102 case ST_ADM_ACTION_READY:
3103 srv_adm_set_ready(sv);
3104 altered_servers++;
3105 total_servers++;
3106 break;
3107 case ST_ADM_ACTION_DRAIN:
3108 srv_adm_set_drain(sv);
3109 altered_servers++;
3110 total_servers++;
3111 break;
3112 case ST_ADM_ACTION_MAINT:
3113 srv_adm_set_maint(sv);
3114 altered_servers++;
3115 total_servers++;
3116 break;
3117 case ST_ADM_ACTION_SHUTDOWN:
3118 if (px->state != PR_STSTOPPED) {
3119 struct stream *sess, *sess_bck;
3120
3121 list_for_each_entry_safe(sess, sess_bck, &sv->actconns, by_srv)
3122 if (sess->srv_conn == sv)
3123 stream_shutdown(sess, SF_ERR_KILLED);
3124
3125 altered_servers++;
3126 total_servers++;
3127 }
3128 break;
3129 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003130 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Lallemand74c24fb2016-11-21 17:18:36 +01003131 } else {
3132 /* the server name is unknown or ambiguous (duplicate names) */
3133 total_servers++;
3134 }
3135 }
3136 if (reprocess && px && action) {
3137 /* Now, we know the backend and the action chosen by the user.
3138 * We can safely restart from the first server parameter
3139 * to reprocess them
3140 */
3141 cur_param = st_cur_param;
3142 next_param = st_next_param;
3143 reprocess = 0;
3144 goto reprocess_servers;
3145 }
3146
3147 next_param = cur_param;
3148 }
3149 }
3150
3151 if (total_servers == 0) {
3152 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
3153 }
3154 else if (altered_servers == 0) {
3155 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
3156 }
3157 else if (altered_servers == total_servers) {
3158 appctx->ctx.stats.st_code = STAT_STATUS_DONE;
3159 }
3160 else {
3161 appctx->ctx.stats.st_code = STAT_STATUS_PART;
3162 }
3163 out:
3164 return 1;
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01003165 wait:
3166 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
3167 return 0;
Christopher Fauletef779222018-10-31 08:47:01 +01003168}
3169
3170
3171static int stats_send_htx_headers(struct stream_interface *si, struct htx *htx)
3172{
3173 struct stream *s = si_strm(si);
3174 struct uri_auth *uri = s->be->uri_auth;
3175 struct appctx *appctx = __objt_appctx(si->end);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003176 struct htx_sl *sl;
3177 unsigned int flags;
Christopher Fauletef779222018-10-31 08:47:01 +01003178
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003179 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_ENC|HTX_SL_F_XFER_LEN|HTX_SL_F_CHNK);
3180 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist("200"), ist("OK"));
3181 if (!sl)
Christopher Fauletef779222018-10-31 08:47:01 +01003182 goto full;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003183 sl->info.res.status = 200;
Christopher Fauletef779222018-10-31 08:47:01 +01003184
Christopher Fauletb829f4c2019-03-29 16:13:55 +01003185 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")))
Christopher Fauletef779222018-10-31 08:47:01 +01003186 goto full;
3187 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
3188 if (!htx_add_header(htx, ist("Content-Type"), ist("text/html")))
3189 goto full;
3190 }
3191 else {
3192 if (!htx_add_header(htx, ist("Content-Type"), ist("text/plain")))
3193 goto full;
3194 }
3195
3196 if (uri->refresh > 0 && !(appctx->ctx.stats.flags & STAT_NO_REFRESH)) {
3197 const char *refresh = U2A(uri->refresh);
3198 if (!htx_add_header(htx, ist("Refresh"), ist2(refresh, strlen(refresh))))
3199 goto full;
3200 }
3201
3202 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
3203 if (!htx_add_header(htx, ist("Transfer-Encoding"), ist("chunked")))
3204 goto full;
3205 }
3206
3207 if (!htx_add_endof(htx, HTX_BLK_EOH))
3208 goto full;
3209
Christopher Faulet1e2d6362019-02-27 16:28:48 +01003210 channel_add_input(&s->res, htx->data);
Christopher Fauletef779222018-10-31 08:47:01 +01003211 return 1;
3212
3213 full:
3214 htx_reset(htx);
3215 si_rx_room_blk(si);
3216 return 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01003217}
3218
Christopher Fauletef779222018-10-31 08:47:01 +01003219
3220static int stats_send_htx_redirect(struct stream_interface *si, struct htx *htx)
3221{
3222 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
3223 struct stream *s = si_strm(si);
3224 struct uri_auth *uri = s->be->uri_auth;
3225 struct appctx *appctx = __objt_appctx(si->end);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003226 struct htx_sl *sl;
3227 unsigned int flags;
Christopher Fauletef779222018-10-31 08:47:01 +01003228
3229 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
3230 scope_txt[0] = 0;
3231 if (appctx->ctx.stats.scope_len) {
Christopher Fauleted7a0662019-01-14 11:07:34 +01003232 const char *scope_ptr = stats_scope_ptr(appctx, si);
3233
Christopher Fauletef779222018-10-31 08:47:01 +01003234 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Christopher Fauleted7a0662019-01-14 11:07:34 +01003235 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), scope_ptr, appctx->ctx.stats.scope_len);
Christopher Fauletef779222018-10-31 08:47:01 +01003236 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
3237 }
3238
3239 /* We don't want to land on the posted stats page because a refresh will
3240 * repost the data. We don't want this to happen on accident so we redirect
3241 * the browse to the stats page with a GET.
3242 */
3243 chunk_printf(&trash, "%s;st=%s%s%s%s",
3244 uri->uri_prefix,
3245 ((appctx->ctx.stats.st_code > STAT_STATUS_INIT) &&
3246 (appctx->ctx.stats.st_code < STAT_STATUS_SIZE) &&
3247 stat_status_codes[appctx->ctx.stats.st_code]) ?
3248 stat_status_codes[appctx->ctx.stats.st_code] :
3249 stat_status_codes[STAT_STATUS_UNKN],
3250 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3251 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3252 scope_txt);
3253
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003254 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_CHNK);
3255 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist("303"), ist("See Other"));
3256 if (!sl)
Christopher Fauletef779222018-10-31 08:47:01 +01003257 goto full;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003258 sl->info.res.status = 303;
Christopher Fauletef779222018-10-31 08:47:01 +01003259
3260 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")) ||
Christopher Fauletef779222018-10-31 08:47:01 +01003261 !htx_add_header(htx, ist("Content-Type"), ist("text/plain")) ||
3262 !htx_add_header(htx, ist("Content-Length"), ist("0")) ||
3263 !htx_add_header(htx, ist("Location"), ist2(trash.area, trash.data)))
3264 goto full;
3265
3266 if (!htx_add_endof(htx, HTX_BLK_EOH))
3267 goto full;
3268
Christopher Faulet1e2d6362019-02-27 16:28:48 +01003269 channel_add_input(&s->res, htx->data);
Christopher Fauletef779222018-10-31 08:47:01 +01003270 return 1;
3271
3272full:
3273 htx_reset(htx);
3274 si_rx_room_blk(si);
3275 return 0;
3276}
William Lallemand74c24fb2016-11-21 17:18:36 +01003277
3278static int stats_send_http_headers(struct stream_interface *si)
3279{
3280 struct stream *s = si_strm(si);
3281 struct uri_auth *uri = s->be->uri_auth;
Willy Tarreau21ff2c42018-09-20 11:01:01 +02003282 struct appctx *appctx = __objt_appctx(si->end);
William Lallemand74c24fb2016-11-21 17:18:36 +01003283
3284 chunk_printf(&trash,
3285 "HTTP/1.1 200 OK\r\n"
3286 "Cache-Control: no-cache\r\n"
3287 "Connection: close\r\n"
3288 "Content-Type: %s\r\n",
3289 (appctx->ctx.stats.flags & STAT_FMT_HTML) ? "text/html" : "text/plain");
3290
3291 if (uri->refresh > 0 && !(appctx->ctx.stats.flags & STAT_NO_REFRESH))
3292 chunk_appendf(&trash, "Refresh: %d\r\n",
3293 uri->refresh);
3294
3295 /* we don't send the CRLF in chunked mode, it will be sent with the first chunk's size */
3296
3297 if (appctx->ctx.stats.flags & STAT_CHUNKED)
3298 chunk_appendf(&trash, "Transfer-Encoding: chunked\r\n");
3299 else
3300 chunk_appendf(&trash, "\r\n");
3301
Willy Tarreau06d80a92017-10-19 14:32:15 +02003302 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01003303 si_rx_room_blk(si);
William Lallemand74c24fb2016-11-21 17:18:36 +01003304 return 0;
3305 }
3306
3307 return 1;
3308}
3309
3310static int stats_send_http_redirect(struct stream_interface *si)
3311{
3312 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
3313 struct stream *s = si_strm(si);
3314 struct uri_auth *uri = s->be->uri_auth;
Willy Tarreau21ff2c42018-09-20 11:01:01 +02003315 struct appctx *appctx = __objt_appctx(si->end);
William Lallemand74c24fb2016-11-21 17:18:36 +01003316
3317 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
3318 scope_txt[0] = 0;
3319 if (appctx->ctx.stats.scope_len) {
3320 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau89faf5d2018-06-07 18:16:48 +02003321 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), co_head(si_oc(si)) + appctx->ctx.stats.scope_str, appctx->ctx.stats.scope_len);
William Lallemand74c24fb2016-11-21 17:18:36 +01003322 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
3323 }
3324
3325 /* We don't want to land on the posted stats page because a refresh will
3326 * repost the data. We don't want this to happen on accident so we redirect
3327 * the browse to the stats page with a GET.
3328 */
3329 chunk_printf(&trash,
3330 "HTTP/1.1 303 See Other\r\n"
3331 "Cache-Control: no-cache\r\n"
3332 "Content-Type: text/plain\r\n"
3333 "Connection: close\r\n"
3334 "Location: %s;st=%s%s%s%s\r\n"
3335 "Content-length: 0\r\n"
3336 "\r\n",
3337 uri->uri_prefix,
3338 ((appctx->ctx.stats.st_code > STAT_STATUS_INIT) &&
3339 (appctx->ctx.stats.st_code < STAT_STATUS_SIZE) &&
3340 stat_status_codes[appctx->ctx.stats.st_code]) ?
3341 stat_status_codes[appctx->ctx.stats.st_code] :
3342 stat_status_codes[STAT_STATUS_UNKN],
3343 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3344 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3345 scope_txt);
3346
Willy Tarreau06d80a92017-10-19 14:32:15 +02003347 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01003348 si_rx_room_blk(si);
William Lallemand74c24fb2016-11-21 17:18:36 +01003349 return 0;
3350 }
3351
3352 return 1;
3353}
3354
Simon Horman05ee2132017-01-04 09:37:25 +01003355
William Lallemand74c24fb2016-11-21 17:18:36 +01003356/* This I/O handler runs as an applet embedded in a stream interface. It is
3357 * used to send HTTP stats over a TCP socket. The mechanism is very simple.
3358 * appctx->st0 contains the operation in progress (dump, done). The handler
3359 * automatically unregisters itself once transfer is complete.
3360 */
Christopher Fauletef779222018-10-31 08:47:01 +01003361static void htx_stats_io_handler(struct appctx *appctx)
3362{
3363 struct stream_interface *si = appctx->owner;
3364 struct stream *s = si_strm(si);
3365 struct channel *req = si_oc(si);
3366 struct channel *res = si_ic(si);
3367 struct htx *req_htx, *res_htx;
3368
3369 res_htx = htx_from_buf(&res->buf);
3370
3371 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
3372 goto out;
3373
3374 /* Check if the input buffer is avalaible. */
3375 if (!b_size(&res->buf)) {
3376 si_rx_room_blk(si);
3377 goto out;
3378 }
3379
3380 /* check that the output is not closed */
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003381 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_SHUTR))
3382 appctx->st0 = STAT_HTTP_END;
Christopher Fauletef779222018-10-31 08:47:01 +01003383
3384 /* all states are processed in sequence */
3385 if (appctx->st0 == STAT_HTTP_HEAD) {
3386 if (stats_send_htx_headers(si, res_htx)) {
3387 if (s->txn->meth == HTTP_METH_HEAD)
3388 appctx->st0 = STAT_HTTP_DONE;
3389 else
3390 appctx->st0 = STAT_HTTP_DUMP;
3391 }
3392 }
3393
3394 if (appctx->st0 == STAT_HTTP_DUMP) {
3395 if (stats_dump_stat_to_buffer(si, res_htx, s->be->uri_auth))
3396 appctx->st0 = STAT_HTTP_DONE;
3397 }
3398
3399 if (appctx->st0 == STAT_HTTP_POST) {
3400 if (stats_process_http_post(si))
3401 appctx->st0 = STAT_HTTP_LAST;
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003402 else if (req->flags & CF_SHUTR)
Christopher Fauletef779222018-10-31 08:47:01 +01003403 appctx->st0 = STAT_HTTP_DONE;
3404 }
3405
3406 if (appctx->st0 == STAT_HTTP_LAST) {
3407 if (stats_send_htx_redirect(si, res_htx))
3408 appctx->st0 = STAT_HTTP_DONE;
3409 }
3410
3411 if (appctx->st0 == STAT_HTTP_DONE) {
Christopher Faulet54b5e212019-06-04 10:08:28 +02003412 /* Don't add TLR because mux-h1 will take care of it */
Christopher Fauletef779222018-10-31 08:47:01 +01003413 if (!htx_add_endof(res_htx, HTX_BLK_EOM)) {
3414 si_rx_room_blk(si);
3415 goto out;
3416 }
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003417 channel_add_input(&s->res, 1);
3418 appctx->st0 = STAT_HTTP_END;
Christopher Fauletef779222018-10-31 08:47:01 +01003419 }
3420
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003421 if (appctx->st0 == STAT_HTTP_END) {
3422 if (!(res->flags & CF_SHUTR)) {
Christopher Fauletef779222018-10-31 08:47:01 +01003423 res->flags |= CF_READ_NULL;
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003424 si_shutr(si);
3425 }
3426
3427 /* eat the whole request */
3428 if (co_data(req)) {
3429 req_htx = htx_from_buf(&req->buf);
3430 co_htx_skip(req, req_htx, co_data(req));
3431 htx_to_buf(req_htx, &req->buf);
Christopher Fauletef779222018-10-31 08:47:01 +01003432 }
3433 }
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003434
Christopher Fauletef779222018-10-31 08:47:01 +01003435 out:
3436 /* we have left the request in the buffer for the case where we
3437 * process a POST, and this automatically re-enables activity on
3438 * read. It's better to indicate that we want to stop reading when
3439 * we're sending, so that we know there's at most one direction
3440 * deciding to wake the applet up. It saves it from looping when
3441 * emitting large blocks into small TCP windows.
3442 */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003443 htx_to_buf(res_htx, &res->buf);
3444 if (!channel_is_empty(res))
Christopher Fauletef779222018-10-31 08:47:01 +01003445 si_stop_get(si);
Christopher Fauletef779222018-10-31 08:47:01 +01003446}
3447
3448
3449/* This I/O handler runs as an applet embedded in a stream interface. It is
3450 * used to send HTTP stats over a TCP socket. The mechanism is very simple.
3451 * appctx->st0 contains the operation in progress (dump, done). The handler
3452 * automatically unregisters itself once transfer is complete.
3453 */
William Lallemand74c24fb2016-11-21 17:18:36 +01003454static void http_stats_io_handler(struct appctx *appctx)
3455{
3456 struct stream_interface *si = appctx->owner;
3457 struct stream *s = si_strm(si);
3458 struct channel *req = si_oc(si);
3459 struct channel *res = si_ic(si);
3460
Christopher Fauletef779222018-10-31 08:47:01 +01003461 if (IS_HTX_STRM(s))
3462 return htx_stats_io_handler(appctx);
3463
William Lallemand74c24fb2016-11-21 17:18:36 +01003464 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
3465 goto out;
3466
Joseph Herlant29023ec2018-11-15 13:39:46 -08003467 /* Check if the input buffer is available. */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003468 if (res->buf.size == 0) {
Willy Tarreau4b962a42018-11-15 11:03:21 +01003469 /* already subscribed, we'll be called later once the buffer is
3470 * available.
3471 */
Christopher Fauleta73e59b2016-12-09 17:30:18 +01003472 goto out;
3473 }
3474
William Lallemand74c24fb2016-11-21 17:18:36 +01003475 /* check that the output is not closed */
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003476 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_SHUTR))
3477 appctx->st0 = STAT_HTTP_END;
William Lallemand74c24fb2016-11-21 17:18:36 +01003478
3479 /* all states are processed in sequence */
3480 if (appctx->st0 == STAT_HTTP_HEAD) {
3481 if (stats_send_http_headers(si)) {
3482 if (s->txn->meth == HTTP_METH_HEAD)
3483 appctx->st0 = STAT_HTTP_DONE;
3484 else
3485 appctx->st0 = STAT_HTTP_DUMP;
3486 }
3487 }
3488
3489 if (appctx->st0 == STAT_HTTP_DUMP) {
Willy Tarreau97f538b2018-06-15 19:41:31 +02003490 unsigned int prev_len = ci_data(si_ic(si));
William Lallemand74c24fb2016-11-21 17:18:36 +01003491 unsigned int data_len;
3492 unsigned int last_len;
3493 unsigned int last_fwd = 0;
3494
3495 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
3496 /* One difficulty we're facing is that we must prevent
3497 * the input data from being automatically forwarded to
3498 * the output area. For this, we temporarily disable
3499 * forwarding on the channel.
3500 */
3501 last_fwd = si_ic(si)->to_forward;
3502 si_ic(si)->to_forward = 0;
3503 chunk_printf(&trash, "\r\n000000\r\n");
Willy Tarreau06d80a92017-10-19 14:32:15 +02003504 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01003505 si_rx_room_blk(si);
William Lallemand74c24fb2016-11-21 17:18:36 +01003506 si_ic(si)->to_forward = last_fwd;
3507 goto out;
3508 }
3509 }
3510
Willy Tarreau97f538b2018-06-15 19:41:31 +02003511 data_len = ci_data(si_ic(si));
Christopher Fauletef779222018-10-31 08:47:01 +01003512 if (stats_dump_stat_to_buffer(si, NULL, s->be->uri_auth))
William Lallemand74c24fb2016-11-21 17:18:36 +01003513 appctx->st0 = STAT_HTTP_DONE;
3514
Willy Tarreau97f538b2018-06-15 19:41:31 +02003515 last_len = ci_data(si_ic(si));
William Lallemand74c24fb2016-11-21 17:18:36 +01003516
3517 /* Now we must either adjust or remove the chunk size. This is
3518 * not easy because the chunk size might wrap at the end of the
3519 * buffer, so we pretend we have nothing in the buffer, we write
3520 * the size, then restore the buffer's contents. Note that we can
3521 * only do that because no forwarding is scheduled on the stats
3522 * applet.
3523 */
3524 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
3525 si_ic(si)->total -= (last_len - prev_len);
Willy Tarreau97f538b2018-06-15 19:41:31 +02003526 b_sub(si_ib(si), (last_len - prev_len));
William Lallemand74c24fb2016-11-21 17:18:36 +01003527
3528 if (last_len != data_len) {
3529 chunk_printf(&trash, "\r\n%06x\r\n", (last_len - data_len));
Willy Tarreau06d80a92017-10-19 14:32:15 +02003530 if (ci_putchk(si_ic(si), &trash) == -1)
Willy Tarreaudb398432018-11-15 11:08:52 +01003531 si_rx_room_blk(si);
William Lallemand74c24fb2016-11-21 17:18:36 +01003532
3533 si_ic(si)->total += (last_len - data_len);
Willy Tarreau97f538b2018-06-15 19:41:31 +02003534 b_add(si_ib(si), last_len - data_len);
William Lallemand74c24fb2016-11-21 17:18:36 +01003535 }
3536 /* now re-enable forwarding */
3537 channel_forward(si_ic(si), last_fwd);
3538 }
3539 }
3540
3541 if (appctx->st0 == STAT_HTTP_POST) {
3542 if (stats_process_http_post(si))
3543 appctx->st0 = STAT_HTTP_LAST;
3544 else if (si_oc(si)->flags & CF_SHUTR)
3545 appctx->st0 = STAT_HTTP_DONE;
3546 }
3547
3548 if (appctx->st0 == STAT_HTTP_LAST) {
3549 if (stats_send_http_redirect(si))
3550 appctx->st0 = STAT_HTTP_DONE;
3551 }
3552
3553 if (appctx->st0 == STAT_HTTP_DONE) {
3554 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
3555 chunk_printf(&trash, "\r\n0\r\n\r\n");
Willy Tarreau06d80a92017-10-19 14:32:15 +02003556 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01003557 si_rx_room_blk(si);
William Lallemand74c24fb2016-11-21 17:18:36 +01003558 goto out;
3559 }
3560 }
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003561 appctx->st0 = STAT_HTTP_END;
William Lallemand74c24fb2016-11-21 17:18:36 +01003562 }
3563
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003564 if (appctx->st0 == STAT_HTTP_END) {
3565 if (!(res->flags & CF_SHUTR)) {
William Lallemand74c24fb2016-11-21 17:18:36 +01003566 res->flags |= CF_READ_NULL;
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003567 si_shutr(si);
William Lallemand74c24fb2016-11-21 17:18:36 +01003568 }
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003569
3570 /* eat the whole request */
3571 if (co_data(req))
3572 co_skip(si_oc(si), co_data(si_oc(si)));
William Lallemand74c24fb2016-11-21 17:18:36 +01003573 }
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003574
William Lallemand74c24fb2016-11-21 17:18:36 +01003575 out:
Willy Tarreau055ba4f2018-07-24 17:05:54 +02003576 /* we have left the request in the buffer for the case where we
3577 * process a POST, and this automatically re-enables activity on
3578 * read. It's better to indicate that we want to stop reading when
3579 * we're sending, so that we know there's at most one direction
3580 * deciding to wake the applet up. It saves it from looping when
3581 * emitting large blocks into small TCP windows.
3582 */
3583 if (!channel_is_empty(res))
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01003584 si_stop_get(si);
William Lallemand74c24fb2016-11-21 17:18:36 +01003585}
3586
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003587/* Dump all fields from <info> into <out> using the "show info" format (name: value) */
Willy Tarreau83061a82018-07-13 11:56:34 +02003588static int stats_dump_info_fields(struct buffer *out,
3589 const struct field *info)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003590{
3591 int field;
3592
3593 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
3594 if (!field_format(info, field))
3595 continue;
3596
3597 if (!chunk_appendf(out, "%s: ", info_field_names[field]))
3598 return 0;
3599 if (!stats_emit_raw_data_field(out, &info[field]))
3600 return 0;
3601 if (!chunk_strcat(out, "\n"))
3602 return 0;
3603 }
3604 return 1;
3605}
3606
3607/* Dump all fields from <info> into <out> using the "show info typed" format */
Willy Tarreau83061a82018-07-13 11:56:34 +02003608static int stats_dump_typed_info_fields(struct buffer *out,
3609 const struct field *info)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003610{
3611 int field;
3612
3613 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
3614 if (!field_format(info, field))
3615 continue;
3616
3617 if (!chunk_appendf(out, "%d.%s.%u:", field, info_field_names[field], info[INF_PROCESS_NUM].u.u32))
3618 return 0;
3619 if (!stats_emit_field_tags(out, &info[field], ':'))
3620 return 0;
3621 if (!stats_emit_typed_data_field(out, &info[field]))
3622 return 0;
3623 if (!chunk_strcat(out, "\n"))
3624 return 0;
3625 }
3626 return 1;
3627}
3628
3629/* Fill <info> with HAProxy global info. <info> is preallocated
3630 * array of length <len>. The length of the aray must be
3631 * INF_TOTAL_FIELDS. If this length is less then this value, the
3632 * function returns 0, otherwise, it returns 1.
3633 */
3634int stats_fill_info(struct field *info, int len)
3635{
3636 unsigned int up = (now.tv_sec - start_date.tv_sec);
Willy Tarreau83061a82018-07-13 11:56:34 +02003637 struct buffer *out = get_trash_chunk();
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003638
3639#ifdef USE_OPENSSL
3640 int ssl_sess_rate = read_freq_ctr(&global.ssl_per_sec);
3641 int ssl_key_rate = read_freq_ctr(&global.ssl_fe_keys_per_sec);
3642 int ssl_reuse = 0;
3643
3644 if (ssl_key_rate < ssl_sess_rate) {
3645 /* count the ssl reuse ratio and avoid overflows in both directions */
3646 ssl_reuse = 100 - (100 * ssl_key_rate + (ssl_sess_rate - 1) / 2) / ssl_sess_rate;
3647 }
3648#endif
3649
3650 if (len < INF_TOTAL_FIELDS)
3651 return 0;
3652
3653 chunk_reset(out);
3654 memset(info, 0, sizeof(*info) * len);
3655
3656 info[INF_NAME] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, PRODUCT_NAME);
Willy Tarreau909b9d82019-01-04 18:20:32 +01003657 info[INF_VERSION] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, haproxy_version);
Adis Nezirovic3ad7b3d2021-01-15 13:12:33 +01003658 info[INF_BUILD_INFO] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, haproxy_version);
Willy Tarreau909b9d82019-01-04 18:20:32 +01003659 info[INF_RELEASE_DATE] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, haproxy_date);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003660
Yves Lafon95317282018-02-26 11:10:37 +01003661 info[INF_NBTHREAD] = mkf_u32(FO_CONFIG|FS_SERVICE, global.nbthread);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003662 info[INF_NBPROC] = mkf_u32(FO_CONFIG|FS_SERVICE, global.nbproc);
3663 info[INF_PROCESS_NUM] = mkf_u32(FO_KEY, relative_pid);
3664 info[INF_PID] = mkf_u32(FO_STATUS, pid);
3665
3666 info[INF_UPTIME] = mkf_str(FN_DURATION, chunk_newstr(out));
3667 chunk_appendf(out, "%ud %uh%02um%02us", up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60));
3668
3669 info[INF_UPTIME_SEC] = mkf_u32(FN_DURATION, up);
3670 info[INF_MEMMAX_MB] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_memmax);
3671 info[INF_POOL_ALLOC_MB] = mkf_u32(0, (unsigned)(pool_total_allocated() / 1048576L));
3672 info[INF_POOL_USED_MB] = mkf_u32(0, (unsigned)(pool_total_used() / 1048576L));
3673 info[INF_POOL_FAILED] = mkf_u32(FN_COUNTER, pool_total_failures());
3674 info[INF_ULIMIT_N] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_nofile);
3675 info[INF_MAXSOCK] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxsock);
3676 info[INF_MAXCONN] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxconn);
3677 info[INF_HARD_MAXCONN] = mkf_u32(FO_CONFIG|FN_LIMIT, global.hardmaxconn);
3678 info[INF_CURR_CONN] = mkf_u32(0, actconn);
3679 info[INF_CUM_CONN] = mkf_u32(FN_COUNTER, totalconn);
3680 info[INF_CUM_REQ] = mkf_u32(FN_COUNTER, global.req_count);
3681#ifdef USE_OPENSSL
3682 info[INF_MAX_SSL_CONNS] = mkf_u32(FN_MAX, global.maxsslconn);
3683 info[INF_CURR_SSL_CONNS] = mkf_u32(0, sslconns);
3684 info[INF_CUM_SSL_CONNS] = mkf_u32(FN_COUNTER, totalsslconns);
3685#endif
3686 info[INF_MAXPIPES] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxpipes);
3687 info[INF_PIPES_USED] = mkf_u32(0, pipes_used);
3688 info[INF_PIPES_FREE] = mkf_u32(0, pipes_free);
3689 info[INF_CONN_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.conn_per_sec));
3690 info[INF_CONN_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.cps_lim);
3691 info[INF_MAX_CONN_RATE] = mkf_u32(FN_MAX, global.cps_max);
3692 info[INF_SESS_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.sess_per_sec));
3693 info[INF_SESS_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.sps_lim);
3694 info[INF_MAX_SESS_RATE] = mkf_u32(FN_RATE, global.sps_max);
3695
3696#ifdef USE_OPENSSL
3697 info[INF_SSL_RATE] = mkf_u32(FN_RATE, ssl_sess_rate);
3698 info[INF_SSL_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.ssl_lim);
3699 info[INF_MAX_SSL_RATE] = mkf_u32(FN_MAX, global.ssl_max);
3700 info[INF_SSL_FRONTEND_KEY_RATE] = mkf_u32(0, ssl_key_rate);
3701 info[INF_SSL_FRONTEND_MAX_KEY_RATE] = mkf_u32(FN_MAX, global.ssl_fe_keys_max);
3702 info[INF_SSL_FRONTEND_SESSION_REUSE_PCT] = mkf_u32(0, ssl_reuse);
3703 info[INF_SSL_BACKEND_KEY_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.ssl_be_keys_per_sec));
3704 info[INF_SSL_BACKEND_MAX_KEY_RATE] = mkf_u32(FN_MAX, global.ssl_be_keys_max);
3705 info[INF_SSL_CACHE_LOOKUPS] = mkf_u32(FN_COUNTER, global.shctx_lookups);
3706 info[INF_SSL_CACHE_MISSES] = mkf_u32(FN_COUNTER, global.shctx_misses);
3707#endif
3708 info[INF_COMPRESS_BPS_IN] = mkf_u32(FN_RATE, read_freq_ctr(&global.comp_bps_in));
3709 info[INF_COMPRESS_BPS_OUT] = mkf_u32(FN_RATE, read_freq_ctr(&global.comp_bps_out));
3710 info[INF_COMPRESS_BPS_RATE_LIM] = mkf_u32(FO_CONFIG|FN_LIMIT, global.comp_rate_lim);
3711#ifdef USE_ZLIB
3712 info[INF_ZLIB_MEM_USAGE] = mkf_u32(0, zlib_used_memory);
3713 info[INF_MAX_ZLIB_MEM_USAGE] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxzlibmem);
3714#endif
3715 info[INF_TASKS] = mkf_u32(0, nb_tasks_cur);
Christopher Faulet34c5cc92016-12-06 09:15:30 +01003716 info[INF_RUN_QUEUE] = mkf_u32(0, tasks_run_queue_cur);
Willy Tarreau81036f22019-05-20 19:24:50 +02003717 info[INF_IDLE_PCT] = mkf_u32(FN_AVG, ti->idle_pct);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003718 info[INF_NODE] = mkf_str(FO_CONFIG|FN_OUTPUT|FS_SERVICE, global.node);
3719 if (global.desc)
3720 info[INF_DESCRIPTION] = mkf_str(FO_CONFIG|FN_OUTPUT|FS_SERVICE, global.desc);
Willy Tarreau00098ea2018-11-05 14:38:13 +01003721 info[INF_STOPPING] = mkf_u32(0, stopping);
3722 info[INF_JOBS] = mkf_u32(0, jobs);
William Lallemanda7199262018-11-16 16:57:20 +01003723 info[INF_UNSTOPPABLE_JOBS] = mkf_u32(0, unstoppable_jobs);
Willy Tarreau00098ea2018-11-05 14:38:13 +01003724 info[INF_LISTENERS] = mkf_u32(0, listeners);
Willy Tarreau199ad242018-11-05 16:31:22 +01003725 info[INF_ACTIVE_PEERS] = mkf_u32(0, active_peers);
Willy Tarreau2d372c22018-11-05 17:12:27 +01003726 info[INF_CONNECTED_PEERS] = mkf_u32(0, connected_peers);
Willy Tarreau13ef7732018-11-12 07:25:28 +01003727 info[INF_DROPPED_LOGS] = mkf_u32(0, dropped_logs);
Willy Tarreaubeb859a2018-11-22 18:07:59 +01003728 info[INF_BUSY_POLLING] = mkf_u32(0, !!(global.tune.options & GTUNE_BUSY_POLLING));
Baptiste Assmann333939c2019-01-21 08:34:50 +01003729 info[INF_FAILED_RESOLUTIONS] = mkf_u32(0, dns_failed_resolutions);
Willy Tarreau7cf0e452019-05-23 11:39:14 +02003730 info[INF_TOTAL_BYTES_OUT] = mkf_u64(0, global.out_bytes);
3731 info[INF_BYTES_OUT_RATE] = mkf_u64(FN_RATE, (unsigned long long)read_freq_ctr(&global.out_32bps) * 32);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003732
3733 return 1;
3734}
3735
3736/* This function dumps information onto the stream interface's read buffer.
3737 * It returns 0 as long as it does not complete, non-zero upon completion.
3738 * No state is used.
3739 */
3740static int stats_dump_info_to_buffer(struct stream_interface *si)
3741{
3742 struct appctx *appctx = __objt_appctx(si->end);
3743
3744 if (!stats_fill_info(info, INF_TOTAL_FIELDS))
3745 return 0;
3746
3747 chunk_reset(&trash);
3748
3749 if (appctx->ctx.stats.flags & STAT_FMT_TYPED)
3750 stats_dump_typed_info_fields(&trash, info);
Simon Horman05ee2132017-01-04 09:37:25 +01003751 else if (appctx->ctx.stats.flags & STAT_FMT_JSON)
3752 stats_dump_json_info_fields(&trash, info);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003753 else
3754 stats_dump_info_fields(&trash, info);
3755
Willy Tarreau06d80a92017-10-19 14:32:15 +02003756 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01003757 si_rx_room_blk(si);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003758 return 0;
3759 }
3760
3761 return 1;
3762}
3763
Simon Horman6f6bb382017-01-04 09:37:26 +01003764/* This function dumps the schema onto the stream interface's read buffer.
3765 * It returns 0 as long as it does not complete, non-zero upon completion.
3766 * No state is used.
3767 *
3768 * Integer values bouned to the range [-(2**53)+1, (2**53)-1] as
3769 * per the recommendation for interoperable integers in section 6 of RFC 7159.
3770 */
Willy Tarreau83061a82018-07-13 11:56:34 +02003771static void stats_dump_json_schema(struct buffer *out)
Simon Horman6f6bb382017-01-04 09:37:26 +01003772{
3773
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003774 int old_len = out->data;
Simon Horman6f6bb382017-01-04 09:37:26 +01003775
3776 chunk_strcat(out,
3777 "{"
3778 "\"$schema\":\"http://json-schema.org/draft-04/schema#\","
3779 "\"oneOf\":["
3780 "{"
3781 "\"title\":\"Info\","
3782 "\"type\":\"array\","
3783 "\"items\":{"
Amaury Denoyellec80f9762020-10-02 18:31:59 +02003784 "\"title\":\"InfoItem\","
3785 "\"type\":\"object\","
Simon Horman6f6bb382017-01-04 09:37:26 +01003786 "\"properties\":{"
Simon Horman6f6bb382017-01-04 09:37:26 +01003787 "\"field\":{\"$ref\":\"#/definitions/field\"},"
3788 "\"processNum\":{\"$ref\":\"#/definitions/processNum\"},"
3789 "\"tags\":{\"$ref\":\"#/definitions/tags\"},"
3790 "\"value\":{\"$ref\":\"#/definitions/typedValue\"}"
3791 "},"
3792 "\"required\":[\"field\",\"processNum\",\"tags\","
3793 "\"value\"]"
3794 "}"
3795 "},"
3796 "{"
3797 "\"title\":\"Stat\","
3798 "\"type\":\"array\","
3799 "\"items\":{"
3800 "\"title\":\"InfoItem\","
3801 "\"type\":\"object\","
3802 "\"properties\":{"
3803 "\"objType\":{"
3804 "\"enum\":[\"Frontend\",\"Backend\",\"Listener\","
3805 "\"Server\",\"Unknown\"]"
3806 "},"
3807 "\"proxyId\":{"
3808 "\"type\":\"integer\","
3809 "\"minimum\":0"
3810 "},"
3811 "\"id\":{"
3812 "\"type\":\"integer\","
3813 "\"minimum\":0"
3814 "},"
3815 "\"field\":{\"$ref\":\"#/definitions/field\"},"
3816 "\"processNum\":{\"$ref\":\"#/definitions/processNum\"},"
3817 "\"tags\":{\"$ref\":\"#/definitions/tags\"},"
3818 "\"typedValue\":{\"$ref\":\"#/definitions/typedValue\"}"
3819 "},"
3820 "\"required\":[\"objType\",\"proxyId\",\"id\","
3821 "\"field\",\"processNum\",\"tags\","
3822 "\"value\"]"
3823 "}"
3824 "},"
3825 "{"
3826 "\"title\":\"Error\","
3827 "\"type\":\"object\","
3828 "\"properties\":{"
3829 "\"errorStr\":{"
3830 "\"type\":\"string\""
Amaury Denoyellec80f9762020-10-02 18:31:59 +02003831 "}"
3832 "},"
3833 "\"required\":[\"errorStr\"]"
Simon Horman6f6bb382017-01-04 09:37:26 +01003834 "}"
3835 "],"
3836 "\"definitions\":{"
3837 "\"field\":{"
3838 "\"type\":\"object\","
3839 "\"pos\":{"
3840 "\"type\":\"integer\","
3841 "\"minimum\":0"
3842 "},"
3843 "\"name\":{"
3844 "\"type\":\"string\""
3845 "},"
3846 "\"required\":[\"pos\",\"name\"]"
3847 "},"
3848 "\"processNum\":{"
3849 "\"type\":\"integer\","
3850 "\"minimum\":1"
3851 "},"
3852 "\"tags\":{"
3853 "\"type\":\"object\","
3854 "\"origin\":{"
3855 "\"type\":\"string\","
3856 "\"enum\":[\"Metric\",\"Status\",\"Key\","
3857 "\"Config\",\"Product\",\"Unknown\"]"
3858 "},"
3859 "\"nature\":{"
3860 "\"type\":\"string\","
3861 "\"enum\":[\"Gauge\",\"Limit\",\"Min\",\"Max\","
3862 "\"Rate\",\"Counter\",\"Duration\","
3863 "\"Age\",\"Time\",\"Name\",\"Output\","
3864 "\"Avg\", \"Unknown\"]"
3865 "},"
3866 "\"scope\":{"
3867 "\"type\":\"string\","
3868 "\"enum\":[\"Cluster\",\"Process\",\"Service\","
3869 "\"System\",\"Unknown\"]"
3870 "},"
3871 "\"required\":[\"origin\",\"nature\",\"scope\"]"
3872 "},"
3873 "\"typedValue\":{"
3874 "\"type\":\"object\","
3875 "\"oneOf\":["
3876 "{\"$ref\":\"#/definitions/typedValue/definitions/s32Value\"},"
3877 "{\"$ref\":\"#/definitions/typedValue/definitions/s64Value\"},"
3878 "{\"$ref\":\"#/definitions/typedValue/definitions/u32Value\"},"
3879 "{\"$ref\":\"#/definitions/typedValue/definitions/u64Value\"},"
3880 "{\"$ref\":\"#/definitions/typedValue/definitions/strValue\"}"
3881 "],"
3882 "\"definitions\":{"
3883 "\"s32Value\":{"
3884 "\"properties\":{"
3885 "\"type\":{"
3886 "\"type\":\"string\","
3887 "\"enum\":[\"s32\"]"
3888 "},"
3889 "\"value\":{"
3890 "\"type\":\"integer\","
3891 "\"minimum\":-2147483648,"
3892 "\"maximum\":2147483647"
3893 "}"
3894 "},"
3895 "\"required\":[\"type\",\"value\"]"
3896 "},"
3897 "\"s64Value\":{"
3898 "\"properties\":{"
3899 "\"type\":{"
3900 "\"type\":\"string\","
3901 "\"enum\":[\"s64\"]"
3902 "},"
3903 "\"value\":{"
3904 "\"type\":\"integer\","
3905 "\"minimum\":-9007199254740991,"
3906 "\"maximum\":9007199254740991"
3907 "}"
3908 "},"
3909 "\"required\":[\"type\",\"value\"]"
3910 "},"
3911 "\"u32Value\":{"
3912 "\"properties\":{"
3913 "\"type\":{"
3914 "\"type\":\"string\","
3915 "\"enum\":[\"u32\"]"
3916 "},"
3917 "\"value\":{"
3918 "\"type\":\"integer\","
3919 "\"minimum\":0,"
3920 "\"maximum\":4294967295"
3921 "}"
3922 "},"
3923 "\"required\":[\"type\",\"value\"]"
3924 "},"
3925 "\"u64Value\":{"
3926 "\"properties\":{"
3927 "\"type\":{"
3928 "\"type\":\"string\","
3929 "\"enum\":[\"u64\"]"
3930 "},"
3931 "\"value\":{"
3932 "\"type\":\"integer\","
3933 "\"minimum\":0,"
3934 "\"maximum\":9007199254740991"
3935 "}"
3936 "},"
3937 "\"required\":[\"type\",\"value\"]"
3938 "},"
3939 "\"strValue\":{"
3940 "\"properties\":{"
3941 "\"type\":{"
3942 "\"type\":\"string\","
3943 "\"enum\":[\"str\"]"
3944 "},"
3945 "\"value\":{\"type\":\"string\"}"
3946 "},"
3947 "\"required\":[\"type\",\"value\"]"
3948 "},"
3949 "\"unknownValue\":{"
3950 "\"properties\":{"
3951 "\"type\":{"
3952 "\"type\":\"integer\","
3953 "\"minimum\":0"
3954 "},"
3955 "\"value\":{"
3956 "\"type\":\"string\","
3957 "\"enum\":[\"unknown\"]"
3958 "}"
3959 "},"
3960 "\"required\":[\"type\",\"value\"]"
3961 "}"
3962 "}"
3963 "}"
3964 "}"
3965 "}");
3966
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003967 if (old_len == out->data) {
Simon Horman6f6bb382017-01-04 09:37:26 +01003968 chunk_reset(out);
3969 chunk_appendf(out,
3970 "{\"errorStr\":\"output buffer too short\"}");
3971 }
3972}
3973
3974/* This function dumps the schema onto the stream interface's read buffer.
3975 * It returns 0 as long as it does not complete, non-zero upon completion.
3976 * No state is used.
3977 */
3978static int stats_dump_json_schema_to_buffer(struct stream_interface *si)
3979{
3980 chunk_reset(&trash);
3981
3982 stats_dump_json_schema(&trash);
3983
Willy Tarreau06d80a92017-10-19 14:32:15 +02003984 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01003985 si_rx_room_blk(si);
Simon Horman6f6bb382017-01-04 09:37:26 +01003986 return 0;
3987 }
3988
3989 return 1;
3990}
3991
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02003992static int cli_parse_clear_counters(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau89d467c2016-11-23 11:02:40 +01003993{
3994 struct proxy *px;
3995 struct server *sv;
3996 struct listener *li;
3997 int clrall = 0;
3998
3999 if (strcmp(args[2], "all") == 0)
4000 clrall = 1;
4001
4002 /* check permissions */
4003 if (!cli_has_level(appctx, ACCESS_LVL_OPER) ||
4004 (clrall && !cli_has_level(appctx, ACCESS_LVL_ADMIN)))
4005 return 1;
4006
Olivier Houchardfbc74e82017-11-24 16:54:05 +01004007 for (px = proxies_list; px; px = px->next) {
Willy Tarreau89d467c2016-11-23 11:02:40 +01004008 if (clrall) {
4009 memset(&px->be_counters, 0, sizeof(px->be_counters));
4010 memset(&px->fe_counters, 0, sizeof(px->fe_counters));
4011 }
4012 else {
4013 px->be_counters.conn_max = 0;
4014 px->be_counters.p.http.rps_max = 0;
4015 px->be_counters.sps_max = 0;
4016 px->be_counters.cps_max = 0;
4017 px->be_counters.nbpend_max = 0;
Christopher Fauletdf73efe2019-11-08 14:53:15 +01004018 px->be_counters.qtime_max = 0;
4019 px->be_counters.ctime_max = 0;
4020 px->be_counters.dtime_max = 0;
4021 px->be_counters.ttime_max = 0;
Willy Tarreau89d467c2016-11-23 11:02:40 +01004022
4023 px->fe_counters.conn_max = 0;
4024 px->fe_counters.p.http.rps_max = 0;
4025 px->fe_counters.sps_max = 0;
4026 px->fe_counters.cps_max = 0;
Willy Tarreau89d467c2016-11-23 11:02:40 +01004027 }
4028
4029 for (sv = px->srv; sv; sv = sv->next)
4030 if (clrall)
4031 memset(&sv->counters, 0, sizeof(sv->counters));
4032 else {
4033 sv->counters.cur_sess_max = 0;
4034 sv->counters.nbpend_max = 0;
4035 sv->counters.sps_max = 0;
Christopher Fauletdf73efe2019-11-08 14:53:15 +01004036 sv->counters.qtime_max = 0;
4037 sv->counters.ctime_max = 0;
4038 sv->counters.dtime_max = 0;
4039 sv->counters.ttime_max = 0;
Willy Tarreau89d467c2016-11-23 11:02:40 +01004040 }
4041
4042 list_for_each_entry(li, &px->conf.listeners, by_fe)
4043 if (li->counters) {
4044 if (clrall)
4045 memset(li->counters, 0, sizeof(*li->counters));
4046 else
4047 li->counters->conn_max = 0;
4048 }
4049 }
4050
4051 global.cps_max = 0;
4052 global.sps_max = 0;
Olivier Houchard00bc3cb2017-10-17 19:23:25 +02004053 global.ssl_max = 0;
4054 global.ssl_fe_keys_max = 0;
4055 global.ssl_be_keys_max = 0;
Willy Tarreaud80cb4e2018-01-20 19:30:13 +01004056
4057 memset(activity, 0, sizeof(activity));
Willy Tarreau89d467c2016-11-23 11:02:40 +01004058 return 1;
4059}
4060
4061
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004062static int cli_parse_show_info(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004063{
Willy Tarreaud25fc792016-12-16 12:33:47 +01004064 appctx->ctx.stats.scope_str = 0;
4065 appctx->ctx.stats.scope_len = 0;
4066 appctx->ctx.stats.flags = 0;
4067
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004068 if (strcmp(args[2], "typed") == 0)
4069 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
Simon Horman05ee2132017-01-04 09:37:25 +01004070 else if (strcmp(args[2], "json") == 0)
4071 appctx->ctx.stats.flags |= STAT_FMT_JSON;
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004072 return 0;
4073}
4074
4075
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004076static int cli_parse_show_stat(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau2b812e22016-11-22 16:18:05 +01004077{
Willy Tarreaud25fc792016-12-16 12:33:47 +01004078 appctx->ctx.stats.scope_str = 0;
4079 appctx->ctx.stats.scope_len = 0;
4080 appctx->ctx.stats.flags = 0;
4081
Willy Tarreau2b812e22016-11-22 16:18:05 +01004082 if (*args[2] && *args[3] && *args[4]) {
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01004083 struct proxy *px;
4084
4085 px = proxy_find_by_name(args[2], 0, 0);
4086 if (px)
4087 appctx->ctx.stats.iid = px->uuid;
4088 else
4089 appctx->ctx.stats.iid = atoi(args[2]);
4090
4091 if (!appctx->ctx.stats.iid) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004092 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01004093 appctx->ctx.cli.msg = "No such proxy.\n";
4094 appctx->st0 = CLI_ST_PRINT;
4095 return 1;
4096 }
4097
Willy Tarreau2b812e22016-11-22 16:18:05 +01004098 appctx->ctx.stats.flags |= STAT_BOUND;
Willy Tarreau2b812e22016-11-22 16:18:05 +01004099 appctx->ctx.stats.type = atoi(args[3]);
4100 appctx->ctx.stats.sid = atoi(args[4]);
4101 if (strcmp(args[5], "typed") == 0)
4102 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
Simon Horman05ee2132017-01-04 09:37:25 +01004103 else if (strcmp(args[5], "json") == 0)
4104 appctx->ctx.stats.flags |= STAT_FMT_JSON;
Willy Tarreau2b812e22016-11-22 16:18:05 +01004105 }
4106 else if (strcmp(args[2], "typed") == 0)
4107 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
Simon Horman05ee2132017-01-04 09:37:25 +01004108 else if (strcmp(args[2], "json") == 0)
4109 appctx->ctx.stats.flags |= STAT_FMT_JSON;
Willy Tarreau2b812e22016-11-22 16:18:05 +01004110
Willy Tarreau2b812e22016-11-22 16:18:05 +01004111 return 0;
4112}
4113
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004114static int cli_io_handler_dump_info(struct appctx *appctx)
4115{
4116 return stats_dump_info_to_buffer(appctx->owner);
4117}
4118
Willy Tarreau2b812e22016-11-22 16:18:05 +01004119/* This I/O handler runs as an applet embedded in a stream interface. It is
4120 * used to send raw stats over a socket.
4121 */
4122static int cli_io_handler_dump_stat(struct appctx *appctx)
4123{
Christopher Fauletef779222018-10-31 08:47:01 +01004124 return stats_dump_stat_to_buffer(appctx->owner, NULL, NULL);
Willy Tarreau2b812e22016-11-22 16:18:05 +01004125}
4126
Simon Horman6f6bb382017-01-04 09:37:26 +01004127static int cli_io_handler_dump_json_schema(struct appctx *appctx)
4128{
4129 return stats_dump_json_schema_to_buffer(appctx->owner);
4130}
4131
Willy Tarreau2b812e22016-11-22 16:18:05 +01004132/* register cli keywords */
4133static struct cli_kw_list cli_kws = {{ },{
Willy Tarreau89d467c2016-11-23 11:02:40 +01004134 { { "clear", "counters", NULL }, "clear counters : clear max statistics counters (add 'all' for all counters)", cli_parse_clear_counters, NULL, NULL },
Willy Tarreaue9038432019-10-09 07:19:02 +02004135 { { "show", "info", NULL }, "show info : report information about the running process [json|typed]", cli_parse_show_info, cli_io_handler_dump_info, NULL },
4136 { { "show", "stat", NULL }, "show stat : report counters for each proxy and server [json|typed]", cli_parse_show_stat, cli_io_handler_dump_stat, NULL },
Simon Horman6f6bb382017-01-04 09:37:26 +01004137 { { "show", "schema", "json", NULL }, "show schema json : report schema used for stats", NULL, cli_io_handler_dump_json_schema, NULL },
Willy Tarreau2b812e22016-11-22 16:18:05 +01004138 {{},}
4139}};
4140
Willy Tarreau0108d902018-11-25 19:14:37 +01004141INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
4142
William Lallemand74c24fb2016-11-21 17:18:36 +01004143struct applet http_stats_applet = {
4144 .obj_type = OBJ_TYPE_APPLET,
4145 .name = "<STATS>", /* used for logging */
4146 .fct = http_stats_io_handler,
4147 .release = NULL,
4148};
4149
William Lallemand74c24fb2016-11-21 17:18:36 +01004150/*
4151 * Local variables:
4152 * c-indent-level: 8
4153 * c-basic-offset: 8
4154 * End:
4155 */