blob: 545350d730f5a80f1a6462b393fab27e6dfef285 [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",
Willy Tarreau0baac8c2016-11-22 16:36:53 +0100157};
158
William Lallemand74c24fb2016-11-21 17:18:36 +0100159const char *stat_field_names[ST_F_TOTAL_FIELDS] = {
160 [ST_F_PXNAME] = "pxname",
161 [ST_F_SVNAME] = "svname",
162 [ST_F_QCUR] = "qcur",
163 [ST_F_QMAX] = "qmax",
164 [ST_F_SCUR] = "scur",
165 [ST_F_SMAX] = "smax",
166 [ST_F_SLIM] = "slim",
167 [ST_F_STOT] = "stot",
168 [ST_F_BIN] = "bin",
169 [ST_F_BOUT] = "bout",
170 [ST_F_DREQ] = "dreq",
171 [ST_F_DRESP] = "dresp",
172 [ST_F_EREQ] = "ereq",
173 [ST_F_ECON] = "econ",
174 [ST_F_ERESP] = "eresp",
175 [ST_F_WRETR] = "wretr",
176 [ST_F_WREDIS] = "wredis",
177 [ST_F_STATUS] = "status",
178 [ST_F_WEIGHT] = "weight",
179 [ST_F_ACT] = "act",
180 [ST_F_BCK] = "bck",
181 [ST_F_CHKFAIL] = "chkfail",
182 [ST_F_CHKDOWN] = "chkdown",
183 [ST_F_LASTCHG] = "lastchg",
184 [ST_F_DOWNTIME] = "downtime",
185 [ST_F_QLIMIT] = "qlimit",
186 [ST_F_PID] = "pid",
187 [ST_F_IID] = "iid",
188 [ST_F_SID] = "sid",
189 [ST_F_THROTTLE] = "throttle",
190 [ST_F_LBTOT] = "lbtot",
191 [ST_F_TRACKED] = "tracked",
192 [ST_F_TYPE] = "type",
193 [ST_F_RATE] = "rate",
194 [ST_F_RATE_LIM] = "rate_lim",
195 [ST_F_RATE_MAX] = "rate_max",
196 [ST_F_CHECK_STATUS] = "check_status",
197 [ST_F_CHECK_CODE] = "check_code",
198 [ST_F_CHECK_DURATION] = "check_duration",
199 [ST_F_HRSP_1XX] = "hrsp_1xx",
200 [ST_F_HRSP_2XX] = "hrsp_2xx",
201 [ST_F_HRSP_3XX] = "hrsp_3xx",
202 [ST_F_HRSP_4XX] = "hrsp_4xx",
203 [ST_F_HRSP_5XX] = "hrsp_5xx",
204 [ST_F_HRSP_OTHER] = "hrsp_other",
205 [ST_F_HANAFAIL] = "hanafail",
206 [ST_F_REQ_RATE] = "req_rate",
207 [ST_F_REQ_RATE_MAX] = "req_rate_max",
208 [ST_F_REQ_TOT] = "req_tot",
209 [ST_F_CLI_ABRT] = "cli_abrt",
210 [ST_F_SRV_ABRT] = "srv_abrt",
211 [ST_F_COMP_IN] = "comp_in",
212 [ST_F_COMP_OUT] = "comp_out",
213 [ST_F_COMP_BYP] = "comp_byp",
214 [ST_F_COMP_RSP] = "comp_rsp",
215 [ST_F_LASTSESS] = "lastsess",
216 [ST_F_LAST_CHK] = "last_chk",
217 [ST_F_LAST_AGT] = "last_agt",
218 [ST_F_QTIME] = "qtime",
219 [ST_F_CTIME] = "ctime",
220 [ST_F_RTIME] = "rtime",
221 [ST_F_TTIME] = "ttime",
222 [ST_F_AGENT_STATUS] = "agent_status",
223 [ST_F_AGENT_CODE] = "agent_code",
224 [ST_F_AGENT_DURATION] = "agent_duration",
225 [ST_F_CHECK_DESC] = "check_desc",
226 [ST_F_AGENT_DESC] = "agent_desc",
227 [ST_F_CHECK_RISE] = "check_rise",
228 [ST_F_CHECK_FALL] = "check_fall",
229 [ST_F_CHECK_HEALTH] = "check_health",
230 [ST_F_AGENT_RISE] = "agent_rise",
231 [ST_F_AGENT_FALL] = "agent_fall",
232 [ST_F_AGENT_HEALTH] = "agent_health",
233 [ST_F_ADDR] = "addr",
234 [ST_F_COOKIE] = "cookie",
235 [ST_F_MODE] = "mode",
236 [ST_F_ALGO] = "algo",
237 [ST_F_CONN_RATE] = "conn_rate",
238 [ST_F_CONN_RATE_MAX] = "conn_rate_max",
239 [ST_F_CONN_TOT] = "conn_tot",
240 [ST_F_INTERCEPTED] = "intercepted",
241 [ST_F_DCON] = "dcon",
242 [ST_F_DSES] = "dses",
Tim Duesterhus3fd19732018-05-27 20:35:08 +0200243 [ST_F_WREW] = "wrew",
Willy Tarreauf1573842018-12-14 11:35:36 +0100244 [ST_F_CONNECT] = "connect",
245 [ST_F_REUSE] = "reuse",
Willy Tarreaua1214a52018-12-14 14:00:25 +0100246 [ST_F_CACHE_LOOKUPS] = "cache_lookups",
247 [ST_F_CACHE_HITS] = "cache_hits",
William Lallemand74c24fb2016-11-21 17:18:36 +0100248};
249
Willy Tarreau0baac8c2016-11-22 16:36:53 +0100250/* one line of info */
Christopher Faulet1bc04c72017-10-29 20:14:08 +0100251static THREAD_LOCAL struct field info[INF_TOTAL_FIELDS];
William Lallemand74c24fb2016-11-21 17:18:36 +0100252/* one line of stats */
Christopher Faulet1bc04c72017-10-29 20:14:08 +0100253static THREAD_LOCAL struct field stats[ST_F_TOTAL_FIELDS];
William Lallemand74c24fb2016-11-21 17:18:36 +0100254
255
Christopher Fauletef779222018-10-31 08:47:01 +0100256static int stats_putchk(struct channel *chn, struct htx *htx, struct buffer *chk)
257{
258 if (htx) {
Christopher Faulet69fc88c2019-01-07 14:27:53 +0100259 if (chk->data >= channel_htx_recv_max(chn, htx))
260 return 0;
Willy Tarreau0a7ef022019-05-28 10:30:11 +0200261 if (!htx_add_data_atonce(htx, ist2(chk->area, chk->data)))
Christopher Fauletef779222018-10-31 08:47:01 +0100262 return 0;
Christopher Faulet5adbeeb2019-01-02 14:34:39 +0100263 channel_add_input(chn, chk->data);
Christopher Fauletef779222018-10-31 08:47:01 +0100264 chk->data = 0;
Christopher Fauletef779222018-10-31 08:47:01 +0100265 }
266 else {
267 if (ci_putchk(chn, chk) == -1)
268 return 0;
269 }
270 return 1;
271}
Willy Tarreau0baac8c2016-11-22 16:36:53 +0100272
Christopher Fauleted7a0662019-01-14 11:07:34 +0100273static const char *stats_scope_ptr(struct appctx *appctx, struct stream_interface *si)
274{
275 const char *p;
276
277 if (IS_HTX_STRM(si_strm(si))) {
278 struct channel *req = si_oc(si);
279 struct htx *htx = htxbuf(&req->buf);
Christopher Faulet8fa60e42019-05-23 11:04:05 +0200280 struct htx_blk *blk;
281 struct ist uri;
Christopher Fauleted7a0662019-01-14 11:07:34 +0100282
Christopher Faulet8fa60e42019-05-23 11:04:05 +0200283 blk = htx_get_head_blk(htx);
284 BUG_ON(htx_get_blk_type(blk) != HTX_BLK_REQ_SL);
285 ALREADY_CHECKED(blk);
286 uri = htx_sl_req_uri(htx_get_blk_ptr(htx, blk));
Christopher Fauleted7a0662019-01-14 11:07:34 +0100287 p = uri.ptr;
288 }
289 else
290 p = co_head(si_oc(si));
291
292 return p + appctx->ctx.stats.scope_str;
293}
294
William Lallemand74c24fb2016-11-21 17:18:36 +0100295/*
296 * http_stats_io_handler()
297 * -> stats_dump_stat_to_buffer() // same as above, but used for CSV or HTML
298 * -> stats_dump_csv_header() // emits the CSV headers (same as above)
Simon Horman05ee2132017-01-04 09:37:25 +0100299 * -> stats_dump_json_header() // emits the JSON headers (same as above)
William Lallemand74c24fb2016-11-21 17:18:36 +0100300 * -> stats_dump_html_head() // emits the HTML headers
301 * -> stats_dump_html_info() // emits the equivalent of "show info" at the top
302 * -> stats_dump_proxy_to_buffer() // same as above, valid for CSV and HTML
303 * -> stats_dump_html_px_hdr()
304 * -> stats_dump_fe_stats()
305 * -> stats_dump_li_stats()
306 * -> stats_dump_sv_stats()
307 * -> stats_dump_be_stats()
308 * -> stats_dump_html_px_end()
309 * -> stats_dump_html_end() // emits HTML trailer
Simon Horman05ee2132017-01-04 09:37:25 +0100310 * -> stats_dump_json_end() // emits JSON trailer
William Lallemand74c24fb2016-11-21 17:18:36 +0100311 */
312
313
William Lallemand74c24fb2016-11-21 17:18:36 +0100314/* Dumps the stats CSV header to the trash buffer which. The caller is responsible
315 * for clearing it if needed.
316 * NOTE: Some tools happen to rely on the field position instead of its name,
317 * so please only append new fields at the end, never in the middle.
318 */
319static void stats_dump_csv_header()
320{
321 int field;
322
323 chunk_appendf(&trash, "# ");
324 for (field = 0; field < ST_F_TOTAL_FIELDS; field++)
325 chunk_appendf(&trash, "%s,", stat_field_names[field]);
326
327 chunk_appendf(&trash, "\n");
328}
329
330
331/* Emits a stats field without any surrounding element and properly encoded to
332 * resist CSV output. Returns non-zero on success, 0 if the buffer is full.
333 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200334int stats_emit_raw_data_field(struct buffer *out, const struct field *f)
William Lallemand74c24fb2016-11-21 17:18:36 +0100335{
336 switch (field_format(f, 0)) {
337 case FF_EMPTY: return 1;
338 case FF_S32: return chunk_appendf(out, "%d", f->u.s32);
339 case FF_U32: return chunk_appendf(out, "%u", f->u.u32);
340 case FF_S64: return chunk_appendf(out, "%lld", (long long)f->u.s64);
341 case FF_U64: return chunk_appendf(out, "%llu", (unsigned long long)f->u.u64);
342 case FF_STR: return csv_enc_append(field_str(f, 0), 1, out) != NULL;
343 default: return chunk_appendf(out, "[INCORRECT_FIELD_TYPE_%08x]", f->type);
344 }
345}
346
347/* Emits a stats field prefixed with its type. No CSV encoding is prepared, the
348 * output is supposed to be used on its own line. Returns non-zero on success, 0
349 * if the buffer is full.
350 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200351int stats_emit_typed_data_field(struct buffer *out, const struct field *f)
William Lallemand74c24fb2016-11-21 17:18:36 +0100352{
353 switch (field_format(f, 0)) {
354 case FF_EMPTY: return 1;
355 case FF_S32: return chunk_appendf(out, "s32:%d", f->u.s32);
356 case FF_U32: return chunk_appendf(out, "u32:%u", f->u.u32);
357 case FF_S64: return chunk_appendf(out, "s64:%lld", (long long)f->u.s64);
358 case FF_U64: return chunk_appendf(out, "u64:%llu", (unsigned long long)f->u.u64);
359 case FF_STR: return chunk_appendf(out, "str:%s", field_str(f, 0));
360 default: return chunk_appendf(out, "%08x:?", f->type);
361 }
362}
363
Simon Horman05ee2132017-01-04 09:37:25 +0100364/* Limit JSON integer values to the range [-(2**53)+1, (2**53)-1] as per
365 * the recommendation for interoperable integers in section 6 of RFC 7159.
366 */
367#define JSON_INT_MAX ((1ULL << 53) - 1)
368#define JSON_INT_MIN (0 - JSON_INT_MAX)
369
370/* Emits a stats field value and its type in JSON.
371 * Returns non-zero on success, 0 on error.
372 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200373int stats_emit_json_data_field(struct buffer *out, const struct field *f)
Simon Horman05ee2132017-01-04 09:37:25 +0100374{
375 int old_len;
376 char buf[20];
377 const char *type, *value = buf, *quote = "";
378
379 switch (field_format(f, 0)) {
380 case FF_EMPTY: return 1;
381 case FF_S32: type = "\"s32\"";
382 snprintf(buf, sizeof(buf), "%d", f->u.s32);
383 break;
384 case FF_U32: type = "\"u32\"";
385 snprintf(buf, sizeof(buf), "%u", f->u.u32);
386 break;
387 case FF_S64: type = "\"s64\"";
388 if (f->u.s64 < JSON_INT_MIN || f->u.s64 > JSON_INT_MAX)
389 return 0;
390 type = "\"s64\"";
391 snprintf(buf, sizeof(buf), "%lld", (long long)f->u.s64);
392 break;
393 case FF_U64: if (f->u.u64 > JSON_INT_MAX)
394 return 0;
395 type = "\"u64\"";
396 snprintf(buf, sizeof(buf), "%llu",
397 (unsigned long long) f->u.u64);
398 break;
399 case FF_STR: type = "\"str\"";
400 value = field_str(f, 0);
401 quote = "\"";
402 break;
403 default: snprintf(buf, sizeof(buf), "%u", f->type);
404 type = buf;
405 value = "unknown";
406 quote = "\"";
407 break;
408 }
409
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200410 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100411 chunk_appendf(out, ",\"value\":{\"type\":%s,\"value\":%s%s%s}",
412 type, quote, value, quote);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200413 return !(old_len == out->data);
Simon Horman05ee2132017-01-04 09:37:25 +0100414}
415
William Lallemand74c24fb2016-11-21 17:18:36 +0100416/* Emits an encoding of the field type on 3 characters followed by a delimiter.
417 * Returns non-zero on success, 0 if the buffer is full.
418 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200419int stats_emit_field_tags(struct buffer *out, const struct field *f,
420 char delim)
William Lallemand74c24fb2016-11-21 17:18:36 +0100421{
422 char origin, nature, scope;
423
424 switch (field_origin(f, 0)) {
425 case FO_METRIC: origin = 'M'; break;
426 case FO_STATUS: origin = 'S'; break;
427 case FO_KEY: origin = 'K'; break;
428 case FO_CONFIG: origin = 'C'; break;
429 case FO_PRODUCT: origin = 'P'; break;
430 default: origin = '?'; break;
431 }
432
433 switch (field_nature(f, 0)) {
434 case FN_GAUGE: nature = 'G'; break;
435 case FN_LIMIT: nature = 'L'; break;
436 case FN_MIN: nature = 'm'; break;
437 case FN_MAX: nature = 'M'; break;
438 case FN_RATE: nature = 'R'; break;
439 case FN_COUNTER: nature = 'C'; break;
440 case FN_DURATION: nature = 'D'; break;
441 case FN_AGE: nature = 'A'; break;
442 case FN_TIME: nature = 'T'; break;
443 case FN_NAME: nature = 'N'; break;
444 case FN_OUTPUT: nature = 'O'; break;
445 case FN_AVG: nature = 'a'; break;
446 default: nature = '?'; break;
447 }
448
449 switch (field_scope(f, 0)) {
450 case FS_PROCESS: scope = 'P'; break;
451 case FS_SERVICE: scope = 'S'; break;
452 case FS_SYSTEM: scope = 's'; break;
453 case FS_CLUSTER: scope = 'C'; break;
454 default: scope = '?'; break;
455 }
456
457 return chunk_appendf(out, "%c%c%c%c", origin, nature, scope, delim);
458}
459
Simon Horman05ee2132017-01-04 09:37:25 +0100460/* Emits an encoding of the field type as JSON.
461 * Returns non-zero on success, 0 if the buffer is full.
462 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200463int stats_emit_json_field_tags(struct buffer *out, const struct field *f)
Simon Horman05ee2132017-01-04 09:37:25 +0100464{
465 const char *origin, *nature, *scope;
466 int old_len;
467
468 switch (field_origin(f, 0)) {
469 case FO_METRIC: origin = "Metric"; break;
470 case FO_STATUS: origin = "Status"; break;
471 case FO_KEY: origin = "Key"; break;
472 case FO_CONFIG: origin = "Config"; break;
473 case FO_PRODUCT: origin = "Product"; break;
474 default: origin = "Unknown"; break;
475 }
476
477 switch (field_nature(f, 0)) {
478 case FN_GAUGE: nature = "Gauge"; break;
479 case FN_LIMIT: nature = "Limit"; break;
480 case FN_MIN: nature = "Min"; break;
481 case FN_MAX: nature = "Max"; break;
482 case FN_RATE: nature = "Rate"; break;
483 case FN_COUNTER: nature = "Counter"; break;
484 case FN_DURATION: nature = "Duration"; break;
485 case FN_AGE: nature = "Age"; break;
486 case FN_TIME: nature = "Time"; break;
487 case FN_NAME: nature = "Name"; break;
488 case FN_OUTPUT: nature = "Output"; break;
489 case FN_AVG: nature = "Avg"; break;
490 default: nature = "Unknown"; break;
491 }
492
493 switch (field_scope(f, 0)) {
494 case FS_PROCESS: scope = "Process"; break;
495 case FS_SERVICE: scope = "Service"; break;
496 case FS_SYSTEM: scope = "System"; break;
497 case FS_CLUSTER: scope = "Cluster"; break;
498 default: scope = "Unknown"; break;
499 }
500
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200501 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100502 chunk_appendf(out, "\"tags\":{"
503 "\"origin\":\"%s\","
504 "\"nature\":\"%s\","
505 "\"scope\":\"%s\""
506 "}", origin, nature, scope);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200507 return !(old_len == out->data);
Simon Horman05ee2132017-01-04 09:37:25 +0100508}
William Lallemand74c24fb2016-11-21 17:18:36 +0100509
510/* Dump all fields from <stats> into <out> using CSV format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200511static int stats_dump_fields_csv(struct buffer *out,
512 const struct field *stats)
William Lallemand74c24fb2016-11-21 17:18:36 +0100513{
514 int field;
515
516 for (field = 0; field < ST_F_TOTAL_FIELDS; field++) {
517 if (!stats_emit_raw_data_field(out, &stats[field]))
518 return 0;
519 if (!chunk_strcat(out, ","))
520 return 0;
521 }
522 chunk_strcat(out, "\n");
523 return 1;
524}
525
526/* Dump all fields from <stats> into <out> using a typed "field:desc:type:value" format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200527static int stats_dump_fields_typed(struct buffer *out,
528 const struct field *stats)
William Lallemand74c24fb2016-11-21 17:18:36 +0100529{
530 int field;
531
532 for (field = 0; field < ST_F_TOTAL_FIELDS; field++) {
533 if (!stats[field].type)
534 continue;
535
536 chunk_appendf(out, "%c.%u.%u.%d.%s.%u:",
537 stats[ST_F_TYPE].u.u32 == STATS_TYPE_FE ? 'F' :
538 stats[ST_F_TYPE].u.u32 == STATS_TYPE_BE ? 'B' :
539 stats[ST_F_TYPE].u.u32 == STATS_TYPE_SO ? 'L' :
540 stats[ST_F_TYPE].u.u32 == STATS_TYPE_SV ? 'S' :
541 '?',
542 stats[ST_F_IID].u.u32, stats[ST_F_SID].u.u32,
543 field, stat_field_names[field], stats[ST_F_PID].u.u32);
544
545 if (!stats_emit_field_tags(out, &stats[field], ':'))
546 return 0;
547 if (!stats_emit_typed_data_field(out, &stats[field]))
548 return 0;
549 if (!chunk_strcat(out, "\n"))
550 return 0;
551 }
552 return 1;
553}
554
Simon Horman05ee2132017-01-04 09:37:25 +0100555/* Dump all fields from <stats> into <out> using the "show info json" format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200556static int stats_dump_json_info_fields(struct buffer *out,
Simon Horman05ee2132017-01-04 09:37:25 +0100557 const struct field *info)
558{
559 int field;
560 int started = 0;
561
562 if (!chunk_strcat(out, "["))
563 return 0;
564
565 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
566 int old_len;
567
568 if (!field_format(info, field))
569 continue;
570
571 if (started && !chunk_strcat(out, ","))
572 goto err;
573 started = 1;
574
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200575 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100576 chunk_appendf(out,
577 "{\"field\":{\"pos\":%d,\"name\":\"%s\"},"
578 "\"processNum\":%u,",
579 field, info_field_names[field],
580 info[INF_PROCESS_NUM].u.u32);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200581 if (old_len == out->data)
Simon Horman05ee2132017-01-04 09:37:25 +0100582 goto err;
583
584 if (!stats_emit_json_field_tags(out, &info[field]))
585 goto err;
586
587 if (!stats_emit_json_data_field(out, &info[field]))
588 goto err;
589
590 if (!chunk_strcat(out, "}"))
591 goto err;
592 }
593
594 if (!chunk_strcat(out, "]"))
595 goto err;
596 return 1;
597
598err:
599 chunk_reset(out);
600 chunk_appendf(out, "{\"errorStr\":\"output buffer too short\"}");
601 return 0;
602}
603
604/* Dump all fields from <stats> into <out> using a typed "field:desc:type:value" format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200605static int stats_dump_fields_json(struct buffer *out,
606 const struct field *stats,
Simon Horman05ee2132017-01-04 09:37:25 +0100607 int first_stat)
608{
609 int field;
610 int started = 0;
611
612 if (!first_stat && !chunk_strcat(out, ","))
613 return 0;
614 if (!chunk_strcat(out, "["))
615 return 0;
616
617 for (field = 0; field < ST_F_TOTAL_FIELDS; field++) {
618 const char *obj_type;
619 int old_len;
620
621 if (!stats[field].type)
622 continue;
623
624 if (started && !chunk_strcat(out, ","))
625 goto err;
626 started = 1;
627
628 switch (stats[ST_F_TYPE].u.u32) {
629 case STATS_TYPE_FE: obj_type = "Frontend"; break;
630 case STATS_TYPE_BE: obj_type = "Backend"; break;
631 case STATS_TYPE_SO: obj_type = "Listener"; break;
632 case STATS_TYPE_SV: obj_type = "Server"; break;
633 default: obj_type = "Unknown"; break;
634 }
635
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200636 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100637 chunk_appendf(out,
638 "{"
639 "\"objType\":\"%s\","
640 "\"proxyId\":%d,"
641 "\"id\":%d,"
642 "\"field\":{\"pos\":%d,\"name\":\"%s\"},"
643 "\"processNum\":%u,",
644 obj_type, stats[ST_F_IID].u.u32,
645 stats[ST_F_SID].u.u32, field,
646 stat_field_names[field], stats[ST_F_PID].u.u32);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200647 if (old_len == out->data)
Simon Horman05ee2132017-01-04 09:37:25 +0100648 goto err;
649
650 if (!stats_emit_json_field_tags(out, &stats[field]))
651 goto err;
652
653 if (!stats_emit_json_data_field(out, &stats[field]))
654 goto err;
655
656 if (!chunk_strcat(out, "}"))
657 goto err;
658 }
659
660 if (!chunk_strcat(out, "]"))
661 goto err;
662
663 return 1;
664
665err:
666 chunk_reset(out);
667 if (!first_stat)
668 chunk_strcat(out, ",");
669 chunk_appendf(out, "{\"errorStr\":\"output buffer too short\"}");
670 return 0;
671}
672
William Lallemand74c24fb2016-11-21 17:18:36 +0100673/* Dump all fields from <stats> into <out> using the HTML format. A column is
674 * reserved for the checkbox is ST_SHOWADMIN is set in <flags>. Some extra info
675 * are provided if ST_SHLGNDS is present in <flags>.
676 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200677static int stats_dump_fields_html(struct buffer *out,
678 const struct field *stats,
679 unsigned int flags)
William Lallemand74c24fb2016-11-21 17:18:36 +0100680{
Willy Tarreau83061a82018-07-13 11:56:34 +0200681 struct buffer src;
William Lallemand74c24fb2016-11-21 17:18:36 +0100682
683 if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_FE) {
684 chunk_appendf(out,
685 /* name, queue */
686 "<tr class=\"frontend\">");
687
688 if (flags & ST_SHOWADMIN) {
689 /* Column sub-heading for Enable or Disable server */
690 chunk_appendf(out, "<td></td>");
691 }
692
693 chunk_appendf(out,
694 "<td class=ac>"
695 "<a name=\"%s/Frontend\"></a>"
696 "<a class=lfsb href=\"#%s/Frontend\">Frontend</a></td>"
697 "<td colspan=3></td>"
698 "",
699 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_PXNAME));
700
701 chunk_appendf(out,
702 /* sessions rate : current */
703 "<td><u>%s<div class=tips><table class=det>"
704 "<tr><th>Current connection rate:</th><td>%s/s</td></tr>"
705 "<tr><th>Current session rate:</th><td>%s/s</td></tr>"
706 "",
707 U2H(stats[ST_F_RATE].u.u32),
708 U2H(stats[ST_F_CONN_RATE].u.u32),
709 U2H(stats[ST_F_RATE].u.u32));
710
711 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
712 chunk_appendf(out,
713 "<tr><th>Current request rate:</th><td>%s/s</td></tr>",
714 U2H(stats[ST_F_REQ_RATE].u.u32));
715
716 chunk_appendf(out,
717 "</table></div></u></td>"
718 /* sessions rate : max */
719 "<td><u>%s<div class=tips><table class=det>"
720 "<tr><th>Max connection rate:</th><td>%s/s</td></tr>"
721 "<tr><th>Max session rate:</th><td>%s/s</td></tr>"
722 "",
723 U2H(stats[ST_F_RATE_MAX].u.u32),
724 U2H(stats[ST_F_CONN_RATE_MAX].u.u32),
725 U2H(stats[ST_F_RATE_MAX].u.u32));
726
727 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
728 chunk_appendf(out,
729 "<tr><th>Max request rate:</th><td>%s/s</td></tr>",
730 U2H(stats[ST_F_REQ_RATE_MAX].u.u32));
731
732 chunk_appendf(out,
733 "</table></div></u></td>"
734 /* sessions rate : limit */
735 "<td>%s</td>",
736 LIM2A(stats[ST_F_RATE_LIM].u.u32, "-"));
737
738 chunk_appendf(out,
739 /* sessions: current, max, limit, total */
740 "<td>%s</td><td>%s</td><td>%s</td>"
741 "<td><u>%s<div class=tips><table class=det>"
742 "<tr><th>Cum. connections:</th><td>%s</td></tr>"
743 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
744 "",
745 U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SMAX].u.u32), U2H(stats[ST_F_SLIM].u.u32),
746 U2H(stats[ST_F_STOT].u.u64),
747 U2H(stats[ST_F_CONN_TOT].u.u64),
748 U2H(stats[ST_F_STOT].u.u64));
749
750 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
751 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
752 chunk_appendf(out,
753 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
754 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
755 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
756 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
757 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
758 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
759 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
760 "<tr><th>- other responses:</th><td>%s</td></tr>"
761 "<tr><th>Intercepted requests:</th><td>%s</td></tr>"
Willy Tarreaua1214a52018-12-14 14:00:25 +0100762 "<tr><th>Cache lookups:</th><td>%s</td></tr>"
763 "<tr><th>Cache hits:</th><td>%s</td><td>(%d%%)</td></tr>"
Willy Tarreau1b0f85e2018-05-28 15:12:40 +0200764 "<tr><th>Failed hdr rewrites:</th><td>%s</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +0100765 "",
766 U2H(stats[ST_F_REQ_TOT].u.u64),
767 U2H(stats[ST_F_HRSP_1XX].u.u64),
768 U2H(stats[ST_F_HRSP_2XX].u.u64),
769 U2H(stats[ST_F_COMP_RSP].u.u64),
770 stats[ST_F_HRSP_2XX].u.u64 ?
771 (int)(100 * stats[ST_F_COMP_RSP].u.u64 / stats[ST_F_HRSP_2XX].u.u64) : 0,
772 U2H(stats[ST_F_HRSP_3XX].u.u64),
773 U2H(stats[ST_F_HRSP_4XX].u.u64),
774 U2H(stats[ST_F_HRSP_5XX].u.u64),
775 U2H(stats[ST_F_HRSP_OTHER].u.u64),
Willy Tarreau1b0f85e2018-05-28 15:12:40 +0200776 U2H(stats[ST_F_INTERCEPTED].u.u64),
Willy Tarreaua1214a52018-12-14 14:00:25 +0100777 U2H(stats[ST_F_CACHE_LOOKUPS].u.u64),
778 U2H(stats[ST_F_CACHE_HITS].u.u64),
779 stats[ST_F_CACHE_LOOKUPS].u.u64 ?
780 (int)(100 * stats[ST_F_CACHE_HITS].u.u64 / stats[ST_F_CACHE_LOOKUPS].u.u64) : 0,
Willy Tarreau1b0f85e2018-05-28 15:12:40 +0200781 U2H(stats[ST_F_WREW].u.u64));
William Lallemand74c24fb2016-11-21 17:18:36 +0100782 }
783
784 chunk_appendf(out,
785 "</table></div></u></td>"
786 /* sessions: lbtot, lastsess */
787 "<td></td><td></td>"
788 /* bytes : in */
789 "<td>%s</td>"
790 "",
791 U2H(stats[ST_F_BIN].u.u64));
792
793 chunk_appendf(out,
794 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
795 "<td>%s%s<div class=tips><table class=det>"
796 "<tr><th>Response bytes in:</th><td>%s</td></tr>"
797 "<tr><th>Compression in:</th><td>%s</td></tr>"
798 "<tr><th>Compression out:</th><td>%s</td><td>(%d%%)</td></tr>"
799 "<tr><th>Compression bypass:</th><td>%s</td></tr>"
800 "<tr><th>Total bytes saved:</th><td>%s</td><td>(%d%%)</td></tr>"
801 "</table></div>%s</td>",
802 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "<u>":"",
803 U2H(stats[ST_F_BOUT].u.u64),
804 U2H(stats[ST_F_BOUT].u.u64),
805 U2H(stats[ST_F_COMP_IN].u.u64),
806 U2H(stats[ST_F_COMP_OUT].u.u64),
807 stats[ST_F_COMP_IN].u.u64 ? (int)(stats[ST_F_COMP_OUT].u.u64 * 100 / stats[ST_F_COMP_IN].u.u64) : 0,
808 U2H(stats[ST_F_COMP_BYP].u.u64),
809 U2H(stats[ST_F_COMP_IN].u.u64 - stats[ST_F_COMP_OUT].u.u64),
810 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,
811 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "</u>":"");
812
813 chunk_appendf(out,
814 /* denied: req, resp */
815 "<td>%s</td><td>%s</td>"
816 /* errors : request, connect, response */
817 "<td>%s</td><td></td><td></td>"
818 /* warnings: retries, redispatches */
819 "<td></td><td></td>"
820 /* server status : reflect frontend status */
821 "<td class=ac>%s</td>"
822 /* rest of server: nothing */
823 "<td class=ac colspan=8></td></tr>"
824 "",
825 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
826 U2H(stats[ST_F_EREQ].u.u64),
827 field_str(stats, ST_F_STATUS));
828 }
829 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_SO) {
830 chunk_appendf(out, "<tr class=socket>");
831 if (flags & ST_SHOWADMIN) {
832 /* Column sub-heading for Enable or Disable server */
833 chunk_appendf(out, "<td></td>");
834 }
835
836 chunk_appendf(out,
837 /* frontend name, listener name */
838 "<td class=ac><a name=\"%s/+%s\"></a>%s"
839 "<a class=lfsb href=\"#%s/+%s\">%s</a>"
840 "",
841 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME),
842 (flags & ST_SHLGNDS)?"<u>":"",
843 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME), field_str(stats, ST_F_SVNAME));
844
845 if (flags & ST_SHLGNDS) {
846 chunk_appendf(out, "<div class=tips>");
847
848 if (isdigit(*field_str(stats, ST_F_ADDR)))
849 chunk_appendf(out, "IPv4: %s, ", field_str(stats, ST_F_ADDR));
850 else if (*field_str(stats, ST_F_ADDR) == '[')
851 chunk_appendf(out, "IPv6: %s, ", field_str(stats, ST_F_ADDR));
852 else if (*field_str(stats, ST_F_ADDR))
853 chunk_appendf(out, "%s, ", field_str(stats, ST_F_ADDR));
854
855 /* id */
856 chunk_appendf(out, "id: %d</div>", stats[ST_F_SID].u.u32);
857 }
858
859 chunk_appendf(out,
860 /* queue */
861 "%s</td><td colspan=3></td>"
862 /* sessions rate: current, max, limit */
863 "<td colspan=3>&nbsp;</td>"
864 /* sessions: current, max, limit, total, lbtot, lastsess */
865 "<td>%s</td><td>%s</td><td>%s</td>"
866 "<td>%s</td><td>&nbsp;</td><td>&nbsp;</td>"
867 /* bytes: in, out */
868 "<td>%s</td><td>%s</td>"
869 "",
870 (flags & ST_SHLGNDS)?"</u>":"",
871 U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SMAX].u.u32), U2H(stats[ST_F_SLIM].u.u32),
872 U2H(stats[ST_F_STOT].u.u64), U2H(stats[ST_F_BIN].u.u64), U2H(stats[ST_F_BOUT].u.u64));
873
874 chunk_appendf(out,
875 /* denied: req, resp */
876 "<td>%s</td><td>%s</td>"
877 /* errors: request, connect, response */
878 "<td>%s</td><td></td><td></td>"
879 /* warnings: retries, redispatches */
880 "<td></td><td></td>"
881 /* server status: reflect listener status */
882 "<td class=ac>%s</td>"
883 /* rest of server: nothing */
884 "<td class=ac colspan=8></td></tr>"
885 "",
886 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
887 U2H(stats[ST_F_EREQ].u.u64),
888 field_str(stats, ST_F_STATUS));
889 }
890 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_SV) {
891 const char *style;
892
893 /* determine the style to use depending on the server's state,
894 * its health and weight. There isn't a 1-to-1 mapping between
895 * state and styles for the cases where the server is (still)
896 * up. The reason is that we don't want to report nolb and
897 * drain with the same color.
898 */
899
900 if (strcmp(field_str(stats, ST_F_STATUS), "DOWN") == 0 ||
901 strcmp(field_str(stats, ST_F_STATUS), "DOWN (agent)") == 0) {
902 style = "down";
903 }
904 else if (strcmp(field_str(stats, ST_F_STATUS), "DOWN ") == 0) {
905 style = "going_up";
906 }
907 else if (strcmp(field_str(stats, ST_F_STATUS), "NOLB ") == 0) {
908 style = "going_down";
909 }
910 else if (strcmp(field_str(stats, ST_F_STATUS), "NOLB") == 0) {
911 style = "nolb";
912 }
913 else if (strcmp(field_str(stats, ST_F_STATUS), "no check") == 0) {
914 style = "no_check";
915 }
916 else if (!stats[ST_F_CHKFAIL].type ||
917 stats[ST_F_CHECK_HEALTH].u.u32 == stats[ST_F_CHECK_RISE].u.u32 + stats[ST_F_CHECK_FALL].u.u32 - 1) {
918 /* no check or max health = UP */
919 if (stats[ST_F_WEIGHT].u.u32)
920 style = "up";
921 else
922 style = "draining";
923 }
924 else {
925 style = "going_down";
926 }
927
928 if (memcmp(field_str(stats, ST_F_STATUS), "MAINT", 5) == 0)
929 chunk_appendf(out, "<tr class=\"maintain\">");
930 else
931 chunk_appendf(out,
932 "<tr class=\"%s_%s\">",
933 (stats[ST_F_BCK].u.u32) ? "backup" : "active", style);
934
935
936 if (flags & ST_SHOWADMIN)
937 chunk_appendf(out,
David Harrigand3db35a2016-12-30 12:12:49 +0000938 "<td><input class='%s-checkbox' type=\"checkbox\" name=\"s\" value=\"%s\"></td>",
939 field_str(stats, ST_F_PXNAME),
William Lallemand74c24fb2016-11-21 17:18:36 +0100940 field_str(stats, ST_F_SVNAME));
941
942 chunk_appendf(out,
943 "<td class=ac><a name=\"%s/%s\"></a>%s"
944 "<a class=lfsb href=\"#%s/%s\">%s</a>"
945 "",
946 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME),
947 (flags & ST_SHLGNDS) ? "<u>" : "",
948 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME), field_str(stats, ST_F_SVNAME));
949
950 if (flags & ST_SHLGNDS) {
951 chunk_appendf(out, "<div class=tips>");
952
953 if (isdigit(*field_str(stats, ST_F_ADDR)))
954 chunk_appendf(out, "IPv4: %s, ", field_str(stats, ST_F_ADDR));
955 else if (*field_str(stats, ST_F_ADDR) == '[')
956 chunk_appendf(out, "IPv6: %s, ", field_str(stats, ST_F_ADDR));
957 else if (*field_str(stats, ST_F_ADDR))
958 chunk_appendf(out, "%s, ", field_str(stats, ST_F_ADDR));
959
960 /* id */
961 chunk_appendf(out, "id: %d", stats[ST_F_SID].u.u32);
962
963 /* cookie */
964 if (stats[ST_F_COOKIE].type) {
965 chunk_appendf(out, ", cookie: '");
966 chunk_initstr(&src, field_str(stats, ST_F_COOKIE));
967 chunk_htmlencode(out, &src);
968 chunk_appendf(out, "'");
969 }
970
971 chunk_appendf(out, "</div>");
972 }
973
974 chunk_appendf(out,
975 /* queue : current, max, limit */
976 "%s</td><td>%s</td><td>%s</td><td>%s</td>"
977 /* sessions rate : current, max, limit */
978 "<td>%s</td><td>%s</td><td></td>"
979 "",
980 (flags & ST_SHLGNDS) ? "</u>" : "",
981 U2H(stats[ST_F_QCUR].u.u32), U2H(stats[ST_F_QMAX].u.u32), LIM2A(stats[ST_F_QLIMIT].u.u32, "-"),
982 U2H(stats[ST_F_RATE].u.u32), U2H(stats[ST_F_RATE_MAX].u.u32));
983
984 chunk_appendf(out,
985 /* sessions: current, max, limit, total */
Willy Tarreau0ff699e2019-09-08 09:24:56 +0200986 "<td><u>%s<div class=tips>"
987 "<table class=det>"
988 "<tr><th>Current active connections:</th><td>%s</td></tr>"
989 "<tr><th>Current idle connections:</th><td>%s</td></tr>"
990 "<tr><th>Active connections limit:</th><td>%s</td></tr>"
991 "<tr><th>Idle connections limit:</th><td>%s</td></tr>"
992 "</table></div></u>"
993 "</td><td>%s</td><td>%s</td>"
William Lallemand74c24fb2016-11-21 17:18:36 +0100994 "<td><u>%s<div class=tips><table class=det>"
995 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
996 "",
Willy Tarreau0ff699e2019-09-08 09:24:56 +0200997 U2H(stats[ST_F_SCUR].u.u32),
998 U2H(stats[ST_F_SCUR].u.u32),
999 U2H(stats[ST_F_SRV_ICUR].u.u32),
1000 LIM2A(stats[ST_F_SLIM].u.u32, "-"),
1001 stats[ST_F_SRV_ILIM].type ? U2H(stats[ST_F_SRV_ILIM].u.u32) : "-",
1002 U2H(stats[ST_F_SMAX].u.u32), LIM2A(stats[ST_F_SLIM].u.u32, "-"),
William Lallemand74c24fb2016-11-21 17:18:36 +01001003 U2H(stats[ST_F_STOT].u.u64),
1004 U2H(stats[ST_F_STOT].u.u64));
1005
1006 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
1007 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
1008 unsigned long long tot;
1009
1010 tot = stats[ST_F_HRSP_OTHER].u.u64;
1011 tot += stats[ST_F_HRSP_1XX].u.u64;
1012 tot += stats[ST_F_HRSP_2XX].u.u64;
1013 tot += stats[ST_F_HRSP_3XX].u.u64;
1014 tot += stats[ST_F_HRSP_4XX].u.u64;
1015 tot += stats[ST_F_HRSP_5XX].u.u64;
1016
1017 chunk_appendf(out,
Willy Tarreauf1573842018-12-14 11:35:36 +01001018 "<tr><th>New connections:</th><td>%s</td></tr>"
1019 "<tr><th>Reused connections:</th><td>%s</td><td>(%d%%)</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001020 "<tr><th>Cum. HTTP responses:</th><td>%s</td></tr>"
1021 "<tr><th>- HTTP 1xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1022 "<tr><th>- HTTP 2xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1023 "<tr><th>- HTTP 3xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1024 "<tr><th>- HTTP 4xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1025 "<tr><th>- HTTP 5xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1026 "<tr><th>- other responses:</th><td>%s</td><td>(%d%%)</td></tr>"
Willy Tarreau1b0f85e2018-05-28 15:12:40 +02001027 "<tr><th>Failed hdr rewrites:</th><td>%s</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001028 "",
Willy Tarreauf1573842018-12-14 11:35:36 +01001029 U2H(stats[ST_F_CONNECT].u.u64),
1030 U2H(stats[ST_F_REUSE].u.u64),
1031 (stats[ST_F_CONNECT].u.u64 + stats[ST_F_REUSE].u.u64) ?
1032 (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 +01001033 U2H(tot),
1034 U2H(stats[ST_F_HRSP_1XX].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_1XX].u.u64 / tot) : 0,
1035 U2H(stats[ST_F_HRSP_2XX].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_2XX].u.u64 / tot) : 0,
1036 U2H(stats[ST_F_HRSP_3XX].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_3XX].u.u64 / tot) : 0,
1037 U2H(stats[ST_F_HRSP_4XX].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_4XX].u.u64 / tot) : 0,
1038 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 +02001039 U2H(stats[ST_F_HRSP_OTHER].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_OTHER].u.u64 / tot) : 0,
1040 U2H(stats[ST_F_WREW].u.u64));
William Lallemand74c24fb2016-11-21 17:18:36 +01001041 }
1042
1043 chunk_appendf(out, "<tr><th colspan=3>Avg over last 1024 success. conn.</th></tr>");
1044 chunk_appendf(out, "<tr><th>- Queue time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_QTIME].u.u32));
1045 chunk_appendf(out, "<tr><th>- Connect time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_CTIME].u.u32));
1046 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
1047 chunk_appendf(out, "<tr><th>- Response time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_RTIME].u.u32));
1048 chunk_appendf(out, "<tr><th>- Total time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_TTIME].u.u32));
1049
1050 chunk_appendf(out,
1051 "</table></div></u></td>"
1052 /* sessions: lbtot, last */
1053 "<td>%s</td><td>%s</td>",
1054 U2H(stats[ST_F_LBTOT].u.u64),
1055 human_time(stats[ST_F_LASTSESS].u.s32, 1));
1056
1057 chunk_appendf(out,
1058 /* bytes : in, out */
1059 "<td>%s</td><td>%s</td>"
1060 /* denied: req, resp */
1061 "<td></td><td>%s</td>"
1062 /* errors : request, connect */
1063 "<td></td><td>%s</td>"
1064 /* errors : response */
1065 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
1066 /* warnings: retries, redispatches */
1067 "<td>%lld</td><td>%lld</td>"
1068 "",
1069 U2H(stats[ST_F_BIN].u.u64), U2H(stats[ST_F_BOUT].u.u64),
1070 U2H(stats[ST_F_DRESP].u.u64),
1071 U2H(stats[ST_F_ECON].u.u64),
1072 U2H(stats[ST_F_ERESP].u.u64),
1073 (long long)stats[ST_F_CLI_ABRT].u.u64,
1074 (long long)stats[ST_F_SRV_ABRT].u.u64,
1075 (long long)stats[ST_F_WRETR].u.u64,
1076 (long long)stats[ST_F_WREDIS].u.u64);
1077
1078 /* status, last change */
1079 chunk_appendf(out, "<td class=ac>");
1080
1081 /* FIXME!!!!
1082 * LASTCHG should contain the last change for *this* server and must be computed
1083 * properly above, as was done below, ie: this server if maint, otherwise ref server
1084 * if tracking. Note that ref is either local or remote depending on tracking.
1085 */
1086
1087
1088 if (memcmp(field_str(stats, ST_F_STATUS), "MAINT", 5) == 0) {
1089 chunk_appendf(out, "%s MAINT", human_time(stats[ST_F_LASTCHG].u.u32, 1));
1090 }
1091 else if (memcmp(field_str(stats, ST_F_STATUS), "no check", 5) == 0) {
1092 chunk_strcat(out, "<i>no check</i>");
1093 }
1094 else {
1095 chunk_appendf(out, "%s %s", human_time(stats[ST_F_LASTCHG].u.u32, 1), field_str(stats, ST_F_STATUS));
1096 if (memcmp(field_str(stats, ST_F_STATUS), "DOWN", 4) == 0) {
1097 if (stats[ST_F_CHECK_HEALTH].u.u32)
1098 chunk_strcat(out, " &uarr;");
1099 }
1100 else if (stats[ST_F_CHECK_HEALTH].u.u32 < stats[ST_F_CHECK_RISE].u.u32 + stats[ST_F_CHECK_FALL].u.u32 - 1)
1101 chunk_strcat(out, " &darr;");
1102 }
1103
1104 if (memcmp(field_str(stats, ST_F_STATUS), "DOWN", 4) == 0 &&
1105 stats[ST_F_AGENT_STATUS].type && !stats[ST_F_AGENT_HEALTH].u.u32) {
1106 chunk_appendf(out,
1107 "</td><td class=ac><u> %s",
1108 field_str(stats, ST_F_AGENT_STATUS));
1109
1110 if (stats[ST_F_AGENT_CODE].type)
1111 chunk_appendf(out, "/%d", stats[ST_F_AGENT_CODE].u.u32);
1112
1113 if (stats[ST_F_AGENT_DURATION].type)
1114 chunk_appendf(out, " in %lums", (long)stats[ST_F_AGENT_DURATION].u.u64);
1115
1116 chunk_appendf(out, "<div class=tips>%s", field_str(stats, ST_F_AGENT_DESC));
1117
1118 if (*field_str(stats, ST_F_LAST_AGT)) {
1119 chunk_appendf(out, ": ");
1120 chunk_initstr(&src, field_str(stats, ST_F_LAST_AGT));
1121 chunk_htmlencode(out, &src);
1122 }
1123 chunk_appendf(out, "</div></u>");
1124 }
1125 else if (stats[ST_F_CHECK_STATUS].type) {
1126 chunk_appendf(out,
1127 "</td><td class=ac><u> %s",
1128 field_str(stats, ST_F_CHECK_STATUS));
1129
1130 if (stats[ST_F_CHECK_CODE].type)
1131 chunk_appendf(out, "/%d", stats[ST_F_CHECK_CODE].u.u32);
1132
1133 if (stats[ST_F_CHECK_DURATION].type)
1134 chunk_appendf(out, " in %lums", (long)stats[ST_F_CHECK_DURATION].u.u64);
1135
1136 chunk_appendf(out, "<div class=tips>%s", field_str(stats, ST_F_CHECK_DESC));
1137
1138 if (*field_str(stats, ST_F_LAST_CHK)) {
1139 chunk_appendf(out, ": ");
1140 chunk_initstr(&src, field_str(stats, ST_F_LAST_CHK));
1141 chunk_htmlencode(out, &src);
1142 }
1143 chunk_appendf(out, "</div></u>");
1144 }
1145 else
1146 chunk_appendf(out, "</td><td>");
1147
1148 chunk_appendf(out,
1149 /* weight */
1150 "</td><td class=ac>%d</td>"
1151 /* act, bck */
1152 "<td class=ac>%s</td><td class=ac>%s</td>"
1153 "",
1154 stats[ST_F_WEIGHT].u.u32,
1155 stats[ST_F_BCK].u.u32 ? "-" : "Y",
1156 stats[ST_F_BCK].u.u32 ? "Y" : "-");
1157
1158 /* check failures: unique, fatal, down time */
1159 if (strcmp(field_str(stats, ST_F_STATUS), "MAINT (resolution)") == 0) {
1160 chunk_appendf(out, "<td class=ac colspan=3>resolution</td>");
1161 }
1162 else if (stats[ST_F_CHKFAIL].type) {
1163 chunk_appendf(out, "<td><u>%lld", (long long)stats[ST_F_CHKFAIL].u.u64);
1164
1165 if (stats[ST_F_HANAFAIL].type)
1166 chunk_appendf(out, "/%lld", (long long)stats[ST_F_HANAFAIL].u.u64);
1167
1168 chunk_appendf(out,
1169 "<div class=tips>Failed Health Checks%s</div></u></td>"
1170 "<td>%lld</td><td>%s</td>"
1171 "",
1172 stats[ST_F_HANAFAIL].type ? "/Health Analyses" : "",
1173 (long long)stats[ST_F_CHKDOWN].u.u64, human_time(stats[ST_F_DOWNTIME].u.u32, 1));
1174 }
1175 else if (strcmp(field_str(stats, ST_F_STATUS), "MAINT") != 0 && field_format(stats, ST_F_TRACKED) == FF_STR) {
1176 /* tracking a server (hence inherited maint would appear as "MAINT (via...)" */
1177 chunk_appendf(out,
1178 "<td class=ac colspan=3><a class=lfsb href=\"#%s\">via %s</a></td>",
1179 field_str(stats, ST_F_TRACKED), field_str(stats, ST_F_TRACKED));
1180 }
1181 else
1182 chunk_appendf(out, "<td colspan=3></td>");
1183
1184 /* throttle */
1185 if (stats[ST_F_THROTTLE].type)
1186 chunk_appendf(out, "<td class=ac>%d %%</td></tr>\n", stats[ST_F_THROTTLE].u.u32);
1187 else
1188 chunk_appendf(out, "<td class=ac>-</td></tr>\n");
1189 }
1190 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_BE) {
1191 chunk_appendf(out, "<tr class=\"backend\">");
1192 if (flags & ST_SHOWADMIN) {
1193 /* Column sub-heading for Enable or Disable server */
1194 chunk_appendf(out, "<td></td>");
1195 }
1196 chunk_appendf(out,
1197 "<td class=ac>"
1198 /* name */
1199 "%s<a name=\"%s/Backend\"></a>"
1200 "<a class=lfsb href=\"#%s/Backend\">Backend</a>"
1201 "",
1202 (flags & ST_SHLGNDS)?"<u>":"",
1203 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_PXNAME));
1204
1205 if (flags & ST_SHLGNDS) {
1206 /* balancing */
1207 chunk_appendf(out, "<div class=tips>balancing: %s",
1208 field_str(stats, ST_F_ALGO));
1209
1210 /* cookie */
1211 if (stats[ST_F_COOKIE].type) {
1212 chunk_appendf(out, ", cookie: '");
1213 chunk_initstr(&src, field_str(stats, ST_F_COOKIE));
1214 chunk_htmlencode(out, &src);
1215 chunk_appendf(out, "'");
1216 }
1217 chunk_appendf(out, "</div>");
1218 }
1219
1220 chunk_appendf(out,
1221 "%s</td>"
1222 /* queue : current, max */
1223 "<td>%s</td><td>%s</td><td></td>"
1224 /* sessions rate : current, max, limit */
1225 "<td>%s</td><td>%s</td><td></td>"
1226 "",
1227 (flags & ST_SHLGNDS)?"</u>":"",
1228 U2H(stats[ST_F_QCUR].u.u32), U2H(stats[ST_F_QMAX].u.u32),
1229 U2H(stats[ST_F_RATE].u.u32), U2H(stats[ST_F_RATE_MAX].u.u32));
1230
1231 chunk_appendf(out,
1232 /* sessions: current, max, limit, total */
1233 "<td>%s</td><td>%s</td><td>%s</td>"
1234 "<td><u>%s<div class=tips><table class=det>"
1235 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
1236 "",
Willy Tarreau8e0f1752016-12-12 15:07:29 +01001237 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 +01001238 U2H(stats[ST_F_STOT].u.u64),
1239 U2H(stats[ST_F_STOT].u.u64));
1240
1241 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
1242 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
1243 chunk_appendf(out,
Willy Tarreauf1573842018-12-14 11:35:36 +01001244 "<tr><th>New connections:</th><td>%s</td></tr>"
1245 "<tr><th>Reused connections:</th><td>%s</td><td>(%d%%)</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001246 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
1247 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
1248 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
1249 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
1250 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
1251 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
1252 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
1253 "<tr><th>- other responses:</th><td>%s</td></tr>"
Willy Tarreaua1214a52018-12-14 14:00:25 +01001254 "<tr><th>Cache lookups:</th><td>%s</td></tr>"
1255 "<tr><th>Cache hits:</th><td>%s</td><td>(%d%%)</td></tr>"
Willy Tarreau1b0f85e2018-05-28 15:12:40 +02001256 "<tr><th>Failed hdr rewrites:</th><td>%s</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001257 "<tr><th colspan=3>Avg over last 1024 success. conn.</th></tr>"
1258 "",
Willy Tarreauf1573842018-12-14 11:35:36 +01001259 U2H(stats[ST_F_CONNECT].u.u64),
1260 U2H(stats[ST_F_REUSE].u.u64),
1261 (stats[ST_F_CONNECT].u.u64 + stats[ST_F_REUSE].u.u64) ?
1262 (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 +01001263 U2H(stats[ST_F_REQ_TOT].u.u64),
1264 U2H(stats[ST_F_HRSP_1XX].u.u64),
1265 U2H(stats[ST_F_HRSP_2XX].u.u64),
1266 U2H(stats[ST_F_COMP_RSP].u.u64),
1267 stats[ST_F_HRSP_2XX].u.u64 ?
1268 (int)(100 * stats[ST_F_COMP_RSP].u.u64 / stats[ST_F_HRSP_2XX].u.u64) : 0,
1269 U2H(stats[ST_F_HRSP_3XX].u.u64),
1270 U2H(stats[ST_F_HRSP_4XX].u.u64),
1271 U2H(stats[ST_F_HRSP_5XX].u.u64),
Willy Tarreaufeead3a2018-12-14 13:48:44 +01001272 U2H(stats[ST_F_HRSP_OTHER].u.u64),
Willy Tarreaua1214a52018-12-14 14:00:25 +01001273 U2H(stats[ST_F_CACHE_LOOKUPS].u.u64),
1274 U2H(stats[ST_F_CACHE_HITS].u.u64),
1275 stats[ST_F_CACHE_LOOKUPS].u.u64 ?
1276 (int)(100 * stats[ST_F_CACHE_HITS].u.u64 / stats[ST_F_CACHE_LOOKUPS].u.u64) : 0,
Willy Tarreaufeead3a2018-12-14 13:48:44 +01001277 U2H(stats[ST_F_WREW].u.u64));
William Lallemand74c24fb2016-11-21 17:18:36 +01001278 }
1279
1280 chunk_appendf(out, "<tr><th>- Queue time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_QTIME].u.u32));
1281 chunk_appendf(out, "<tr><th>- Connect time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_CTIME].u.u32));
1282 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
1283 chunk_appendf(out, "<tr><th>- Response time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_RTIME].u.u32));
1284 chunk_appendf(out, "<tr><th>- Total time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_TTIME].u.u32));
1285
1286 chunk_appendf(out,
1287 "</table></div></u></td>"
1288 /* sessions: lbtot, last */
1289 "<td>%s</td><td>%s</td>"
1290 /* bytes: in */
1291 "<td>%s</td>"
1292 "",
1293 U2H(stats[ST_F_LBTOT].u.u64),
1294 human_time(stats[ST_F_LASTSESS].u.s32, 1),
1295 U2H(stats[ST_F_BIN].u.u64));
1296
1297 chunk_appendf(out,
1298 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
1299 "<td>%s%s<div class=tips><table class=det>"
1300 "<tr><th>Response bytes in:</th><td>%s</td></tr>"
1301 "<tr><th>Compression in:</th><td>%s</td></tr>"
1302 "<tr><th>Compression out:</th><td>%s</td><td>(%d%%)</td></tr>"
1303 "<tr><th>Compression bypass:</th><td>%s</td></tr>"
1304 "<tr><th>Total bytes saved:</th><td>%s</td><td>(%d%%)</td></tr>"
1305 "</table></div>%s</td>",
1306 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "<u>":"",
1307 U2H(stats[ST_F_BOUT].u.u64),
1308 U2H(stats[ST_F_BOUT].u.u64),
1309 U2H(stats[ST_F_COMP_IN].u.u64),
1310 U2H(stats[ST_F_COMP_OUT].u.u64),
1311 stats[ST_F_COMP_IN].u.u64 ? (int)(stats[ST_F_COMP_OUT].u.u64 * 100 / stats[ST_F_COMP_IN].u.u64) : 0,
1312 U2H(stats[ST_F_COMP_BYP].u.u64),
1313 U2H(stats[ST_F_COMP_IN].u.u64 - stats[ST_F_COMP_OUT].u.u64),
1314 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,
1315 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "</u>":"");
1316
1317 chunk_appendf(out,
1318 /* denied: req, resp */
1319 "<td>%s</td><td>%s</td>"
1320 /* errors : request, connect */
1321 "<td></td><td>%s</td>"
1322 /* errors : response */
1323 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
1324 /* warnings: retries, redispatches */
1325 "<td>%lld</td><td>%lld</td>"
1326 /* backend status: reflect backend status (up/down): we display UP
1327 * if the backend has known working servers or if it has no server at
1328 * all (eg: for stats). Then we display the total weight, number of
1329 * active and backups. */
1330 "<td class=ac>%s %s</td><td class=ac>&nbsp;</td><td class=ac>%d</td>"
1331 "<td class=ac>%d</td><td class=ac>%d</td>"
1332 "",
1333 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
1334 U2H(stats[ST_F_ECON].u.u64),
1335 U2H(stats[ST_F_ERESP].u.u64),
1336 (long long)stats[ST_F_CLI_ABRT].u.u64,
1337 (long long)stats[ST_F_SRV_ABRT].u.u64,
1338 (long long)stats[ST_F_WRETR].u.u64, (long long)stats[ST_F_WREDIS].u.u64,
1339 human_time(stats[ST_F_LASTCHG].u.u32, 1),
1340 strcmp(field_str(stats, ST_F_STATUS), "DOWN") ? field_str(stats, ST_F_STATUS) : "<font color=\"red\"><b>DOWN</b></font>",
1341 stats[ST_F_WEIGHT].u.u32,
1342 stats[ST_F_ACT].u.u32, stats[ST_F_BCK].u.u32);
1343
1344 chunk_appendf(out,
1345 /* rest of backend: nothing, down transitions, total downtime, throttle */
1346 "<td class=ac>&nbsp;</td><td>%d</td>"
1347 "<td>%s</td>"
1348 "<td></td>"
1349 "</tr>",
1350 stats[ST_F_CHKDOWN].u.u32,
1351 stats[ST_F_DOWNTIME].type ? human_time(stats[ST_F_DOWNTIME].u.u32, 1) : "&nbsp;");
1352 }
1353 return 1;
1354}
1355
1356int stats_dump_one_line(const struct field *stats, unsigned int flags, struct proxy *px, struct appctx *appctx)
1357{
Simon Horman05ee2132017-01-04 09:37:25 +01001358 int ret;
1359
William Lallemand74c24fb2016-11-21 17:18:36 +01001360 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN))
1361 flags |= ST_SHOWADMIN;
1362
1363 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
Simon Horman05ee2132017-01-04 09:37:25 +01001364 ret = stats_dump_fields_html(&trash, stats, flags);
William Lallemand74c24fb2016-11-21 17:18:36 +01001365 else if (appctx->ctx.stats.flags & STAT_FMT_TYPED)
Simon Horman05ee2132017-01-04 09:37:25 +01001366 ret = stats_dump_fields_typed(&trash, stats);
1367 else if (appctx->ctx.stats.flags & STAT_FMT_JSON)
1368 ret = stats_dump_fields_json(&trash, stats,
1369 !(appctx->ctx.stats.flags &
1370 STAT_STARTED));
William Lallemand74c24fb2016-11-21 17:18:36 +01001371 else
Simon Horman05ee2132017-01-04 09:37:25 +01001372 ret = stats_dump_fields_csv(&trash, stats);
1373
1374 if (ret)
1375 appctx->ctx.stats.flags |= STAT_STARTED;
1376
1377 return ret;
William Lallemand74c24fb2016-11-21 17:18:36 +01001378}
1379
1380/* Fill <stats> with the frontend statistics. <stats> is
1381 * preallocated array of length <len>. The length of the array
1382 * must be at least ST_F_TOTAL_FIELDS. If this length is less then
1383 * this value, the function returns 0, otherwise, it returns 1.
1384 */
1385int stats_fill_fe_stats(struct proxy *px, struct field *stats, int len)
1386{
1387 if (len < ST_F_TOTAL_FIELDS)
1388 return 0;
1389
1390 memset(stats, 0, sizeof(*stats) * len);
1391
1392 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1393 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, "FRONTEND");
1394 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1395 stats[ST_F_SCUR] = mkf_u32(0, px->feconn);
1396 stats[ST_F_SMAX] = mkf_u32(FN_MAX, px->fe_counters.conn_max);
1397 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, px->maxconn);
1398 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, px->fe_counters.cum_sess);
1399 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, px->fe_counters.bytes_in);
1400 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, px->fe_counters.bytes_out);
1401 stats[ST_F_DREQ] = mkf_u64(FN_COUNTER, px->fe_counters.denied_req);
1402 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, px->fe_counters.denied_resp);
1403 stats[ST_F_EREQ] = mkf_u64(FN_COUNTER, px->fe_counters.failed_req);
1404 stats[ST_F_DCON] = mkf_u64(FN_COUNTER, px->fe_counters.denied_conn);
1405 stats[ST_F_DSES] = mkf_u64(FN_COUNTER, px->fe_counters.denied_sess);
1406 stats[ST_F_STATUS] = mkf_str(FO_STATUS, px->state == PR_STREADY ? "OPEN" : px->state == PR_STFULL ? "FULL" : "STOP");
1407 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
1408 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1409 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, 0);
1410 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_FE);
1411 stats[ST_F_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_sess_per_sec));
1412 stats[ST_F_RATE_LIM] = mkf_u32(FO_CONFIG|FN_LIMIT, px->fe_sps_lim);
1413 stats[ST_F_RATE_MAX] = mkf_u32(FN_MAX, px->fe_counters.sps_max);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001414 stats[ST_F_WREW] = mkf_u64(FN_COUNTER, px->fe_counters.failed_rewrites);
William Lallemand74c24fb2016-11-21 17:18:36 +01001415
1416 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
1417 if (px->mode == PR_MODE_HTTP) {
1418 stats[ST_F_HRSP_1XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[1]);
1419 stats[ST_F_HRSP_2XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[2]);
1420 stats[ST_F_HRSP_3XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[3]);
1421 stats[ST_F_HRSP_4XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[4]);
1422 stats[ST_F_HRSP_5XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[5]);
1423 stats[ST_F_HRSP_OTHER] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[0]);
1424 stats[ST_F_INTERCEPTED] = mkf_u64(FN_COUNTER, px->fe_counters.intercepted_req);
Willy Tarreaua1214a52018-12-14 14:00:25 +01001425 stats[ST_F_CACHE_LOOKUPS] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cache_lookups);
1426 stats[ST_F_CACHE_HITS] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cache_hits);
William Lallemand74c24fb2016-11-21 17:18:36 +01001427 }
1428
1429 /* requests : req_rate, req_rate_max, req_tot, */
1430 stats[ST_F_REQ_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_req_per_sec));
1431 stats[ST_F_REQ_RATE_MAX] = mkf_u32(FN_MAX, px->fe_counters.p.http.rps_max);
1432 stats[ST_F_REQ_TOT] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cum_req);
1433
1434 /* compression: in, out, bypassed, responses */
1435 stats[ST_F_COMP_IN] = mkf_u64(FN_COUNTER, px->fe_counters.comp_in);
1436 stats[ST_F_COMP_OUT] = mkf_u64(FN_COUNTER, px->fe_counters.comp_out);
1437 stats[ST_F_COMP_BYP] = mkf_u64(FN_COUNTER, px->fe_counters.comp_byp);
1438 stats[ST_F_COMP_RSP] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.comp_rsp);
1439
1440 /* connections : conn_rate, conn_rate_max, conn_tot, conn_max */
1441 stats[ST_F_CONN_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_conn_per_sec));
1442 stats[ST_F_CONN_RATE_MAX] = mkf_u32(FN_MAX, px->fe_counters.cps_max);
1443 stats[ST_F_CONN_TOT] = mkf_u64(FN_COUNTER, px->fe_counters.cum_conn);
1444
1445 return 1;
1446}
1447
1448/* Dumps a frontend's line to the trash for the current proxy <px> and uses
1449 * the state from stream interface <si>. The caller is responsible for clearing
1450 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
1451 */
1452static int stats_dump_fe_stats(struct stream_interface *si, struct proxy *px)
1453{
1454 struct appctx *appctx = __objt_appctx(si->end);
1455
1456 if (!(px->cap & PR_CAP_FE))
1457 return 0;
1458
1459 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_FE)))
1460 return 0;
1461
1462 if (!stats_fill_fe_stats(px, stats, ST_F_TOTAL_FIELDS))
1463 return 0;
1464
1465 return stats_dump_one_line(stats, 0, px, appctx);
1466}
1467
1468/* Fill <stats> with the listener statistics. <stats> is
1469 * preallocated array of length <len>. The length of the array
1470 * must be at least ST_F_TOTAL_FIELDS. If this length is less
1471 * then this value, the function returns 0, otherwise, it
1472 * returns 1. <flags> can take the value ST_SHLGNDS.
1473 */
1474int stats_fill_li_stats(struct proxy *px, struct listener *l, int flags,
1475 struct field *stats, int len)
1476{
Willy Tarreau83061a82018-07-13 11:56:34 +02001477 struct buffer *out = get_trash_chunk();
William Lallemand74c24fb2016-11-21 17:18:36 +01001478
1479 if (len < ST_F_TOTAL_FIELDS)
1480 return 0;
1481
1482 if (!l->counters)
1483 return 0;
1484
1485 chunk_reset(out);
1486 memset(stats, 0, sizeof(*stats) * len);
1487
1488 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1489 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, l->name);
1490 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1491 stats[ST_F_SCUR] = mkf_u32(0, l->nbconn);
1492 stats[ST_F_SMAX] = mkf_u32(FN_MAX, l->counters->conn_max);
1493 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, l->maxconn);
1494 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, l->counters->cum_conn);
1495 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, l->counters->bytes_in);
1496 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, l->counters->bytes_out);
1497 stats[ST_F_DREQ] = mkf_u64(FN_COUNTER, l->counters->denied_req);
1498 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, l->counters->denied_resp);
1499 stats[ST_F_EREQ] = mkf_u64(FN_COUNTER, l->counters->failed_req);
1500 stats[ST_F_DCON] = mkf_u64(FN_COUNTER, l->counters->denied_conn);
1501 stats[ST_F_DSES] = mkf_u64(FN_COUNTER, l->counters->denied_sess);
Willy Tarreaua8cf66b2019-02-27 16:49:00 +01001502 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 +01001503 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
1504 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1505 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, l->luid);
1506 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_SO);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001507 stats[ST_F_WREW] = mkf_u64(FN_COUNTER, l->counters->failed_rewrites);
William Lallemand74c24fb2016-11-21 17:18:36 +01001508
1509 if (flags & ST_SHLGNDS) {
1510 char str[INET6_ADDRSTRLEN];
1511 int port;
1512
1513 port = get_host_port(&l->addr);
1514 switch (addr_to_str(&l->addr, str, sizeof(str))) {
1515 case AF_INET:
1516 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1517 chunk_appendf(out, "%s:%d", str, port);
1518 break;
1519 case AF_INET6:
1520 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1521 chunk_appendf(out, "[%s]:%d", str, port);
1522 break;
1523 case AF_UNIX:
1524 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, "unix");
1525 break;
1526 case -1:
1527 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1528 chunk_strcat(out, strerror(errno));
1529 break;
1530 default: /* address family not supported */
1531 break;
1532 }
1533 }
1534
1535 return 1;
1536}
1537
1538/* Dumps a line for listener <l> and proxy <px> to the trash and uses the state
1539 * from stream interface <si>, and stats flags <flags>. The caller is responsible
1540 * for clearing the trash if needed. Returns non-zero if it emits anything, zero
1541 * otherwise.
1542 */
1543static int stats_dump_li_stats(struct stream_interface *si, struct proxy *px, struct listener *l, int flags)
1544{
1545 struct appctx *appctx = __objt_appctx(si->end);
1546
1547 if (!stats_fill_li_stats(px, l, flags, stats, ST_F_TOTAL_FIELDS))
1548 return 0;
1549
1550 return stats_dump_one_line(stats, flags, px, appctx);
1551}
1552
1553enum srv_stats_state {
1554 SRV_STATS_STATE_DOWN = 0,
1555 SRV_STATS_STATE_DOWN_AGENT,
1556 SRV_STATS_STATE_GOING_UP,
1557 SRV_STATS_STATE_UP_GOING_DOWN,
1558 SRV_STATS_STATE_UP,
1559 SRV_STATS_STATE_NOLB_GOING_DOWN,
1560 SRV_STATS_STATE_NOLB,
1561 SRV_STATS_STATE_DRAIN_GOING_DOWN,
1562 SRV_STATS_STATE_DRAIN,
1563 SRV_STATS_STATE_DRAIN_AGENT,
1564 SRV_STATS_STATE_NO_CHECK,
1565
1566 SRV_STATS_STATE_COUNT, /* Must be last */
1567};
1568
1569static const char *srv_hlt_st[SRV_STATS_STATE_COUNT] = {
1570 [SRV_STATS_STATE_DOWN] = "DOWN",
1571 [SRV_STATS_STATE_DOWN_AGENT] = "DOWN (agent)",
1572 [SRV_STATS_STATE_GOING_UP] = "DOWN %d/%d",
1573 [SRV_STATS_STATE_UP_GOING_DOWN] = "UP %d/%d",
1574 [SRV_STATS_STATE_UP] = "UP",
1575 [SRV_STATS_STATE_NOLB_GOING_DOWN] = "NOLB %d/%d",
1576 [SRV_STATS_STATE_NOLB] = "NOLB",
1577 [SRV_STATS_STATE_DRAIN_GOING_DOWN] = "DRAIN %d/%d",
1578 [SRV_STATS_STATE_DRAIN] = "DRAIN",
1579 [SRV_STATS_STATE_DRAIN_AGENT] = "DRAIN (agent)",
1580 [SRV_STATS_STATE_NO_CHECK] = "no check"
1581};
1582
1583/* Fill <stats> with the server statistics. <stats> is
1584 * preallocated array of length <len>. The length of the array
1585 * must be at least ST_F_TOTAL_FIELDS. If this length is less
1586 * then this value, the function returns 0, otherwise, it
1587 * returns 1. <flags> can take the value ST_SHLGNDS.
1588 */
1589int stats_fill_sv_stats(struct proxy *px, struct server *sv, int flags,
1590 struct field *stats, int len)
1591{
1592 struct server *via, *ref;
1593 char str[INET6_ADDRSTRLEN];
Willy Tarreau83061a82018-07-13 11:56:34 +02001594 struct buffer *out = get_trash_chunk();
William Lallemand74c24fb2016-11-21 17:18:36 +01001595 enum srv_stats_state state;
1596 char *fld_status;
1597
1598 if (len < ST_F_TOTAL_FIELDS)
1599 return 0;
1600
1601 memset(stats, 0, sizeof(*stats) * len);
1602
1603 /* we have "via" which is the tracked server as described in the configuration,
1604 * and "ref" which is the checked server and the end of the chain.
1605 */
1606 via = sv->track ? sv->track : sv;
1607 ref = via;
1608 while (ref->track)
1609 ref = ref->track;
1610
Emeric Brun52a91d32017-08-31 14:41:55 +02001611 if (sv->cur_state == SRV_ST_RUNNING || sv->cur_state == SRV_ST_STARTING) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001612 if ((ref->check.state & CHK_ST_ENABLED) &&
1613 (ref->check.health < ref->check.rise + ref->check.fall - 1)) {
1614 state = SRV_STATS_STATE_UP_GOING_DOWN;
1615 } else {
1616 state = SRV_STATS_STATE_UP;
1617 }
1618
Emeric Brun52a91d32017-08-31 14:41:55 +02001619 if (sv->cur_admin & SRV_ADMF_DRAIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001620 if (ref->agent.state & CHK_ST_ENABLED)
1621 state = SRV_STATS_STATE_DRAIN_AGENT;
1622 else if (state == SRV_STATS_STATE_UP_GOING_DOWN)
1623 state = SRV_STATS_STATE_DRAIN_GOING_DOWN;
1624 else
1625 state = SRV_STATS_STATE_DRAIN;
1626 }
1627
1628 if (state == SRV_STATS_STATE_UP && !(ref->check.state & CHK_ST_ENABLED)) {
1629 state = SRV_STATS_STATE_NO_CHECK;
1630 }
1631 }
Emeric Brun52a91d32017-08-31 14:41:55 +02001632 else if (sv->cur_state == SRV_ST_STOPPING) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001633 if ((!(sv->check.state & CHK_ST_ENABLED) && !sv->track) ||
1634 (ref->check.health == ref->check.rise + ref->check.fall - 1)) {
1635 state = SRV_STATS_STATE_NOLB;
1636 } else {
1637 state = SRV_STATS_STATE_NOLB_GOING_DOWN;
1638 }
1639 }
1640 else { /* stopped */
1641 if ((ref->agent.state & CHK_ST_ENABLED) && !ref->agent.health) {
1642 state = SRV_STATS_STATE_DOWN_AGENT;
1643 } else if ((ref->check.state & CHK_ST_ENABLED) && !ref->check.health) {
1644 state = SRV_STATS_STATE_DOWN; /* DOWN */
1645 } else if ((ref->agent.state & CHK_ST_ENABLED) || (ref->check.state & CHK_ST_ENABLED)) {
1646 state = SRV_STATS_STATE_GOING_UP;
1647 } else {
1648 state = SRV_STATS_STATE_DOWN; /* DOWN, unchecked */
1649 }
1650 }
1651
1652 chunk_reset(out);
1653
1654 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1655 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, sv->id);
1656 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1657 stats[ST_F_QCUR] = mkf_u32(0, sv->nbpend);
1658 stats[ST_F_QMAX] = mkf_u32(FN_MAX, sv->counters.nbpend_max);
1659 stats[ST_F_SCUR] = mkf_u32(0, sv->cur_sess);
1660 stats[ST_F_SMAX] = mkf_u32(FN_MAX, sv->counters.cur_sess_max);
1661
1662 if (sv->maxconn)
1663 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, sv->maxconn);
1664
Willy Tarreau0ff699e2019-09-08 09:24:56 +02001665 stats[ST_F_SRV_ICUR] = mkf_u32(0, sv->curr_idle_conns);
1666 if (sv->max_idle_conns != -1)
1667 stats[ST_F_SRV_ILIM] = mkf_u32(FO_CONFIG|FN_LIMIT, sv->max_idle_conns);
1668
William Lallemand74c24fb2016-11-21 17:18:36 +01001669 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, sv->counters.cum_sess);
1670 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, sv->counters.bytes_in);
1671 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, sv->counters.bytes_out);
1672 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, sv->counters.failed_secu);
1673 stats[ST_F_ECON] = mkf_u64(FN_COUNTER, sv->counters.failed_conns);
1674 stats[ST_F_ERESP] = mkf_u64(FN_COUNTER, sv->counters.failed_resp);
1675 stats[ST_F_WRETR] = mkf_u64(FN_COUNTER, sv->counters.retries);
1676 stats[ST_F_WREDIS] = mkf_u64(FN_COUNTER, sv->counters.redispatches);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001677 stats[ST_F_WREW] = mkf_u64(FN_COUNTER, sv->counters.failed_rewrites);
Willy Tarreauf1573842018-12-14 11:35:36 +01001678 stats[ST_F_CONNECT] = mkf_u64(FN_COUNTER, sv->counters.connect);
1679 stats[ST_F_REUSE] = mkf_u64(FN_COUNTER, sv->counters.reuse);
William Lallemand74c24fb2016-11-21 17:18:36 +01001680
1681 /* status */
1682 fld_status = chunk_newstr(out);
Emeric Brun52a91d32017-08-31 14:41:55 +02001683 if (sv->cur_admin & SRV_ADMF_RMAINT)
William Lallemand74c24fb2016-11-21 17:18:36 +01001684 chunk_appendf(out, "MAINT (resolution)");
Emeric Brun52a91d32017-08-31 14:41:55 +02001685 else if (sv->cur_admin & SRV_ADMF_IMAINT)
William Lallemand74c24fb2016-11-21 17:18:36 +01001686 chunk_appendf(out, "MAINT (via %s/%s)", via->proxy->id, via->id);
Emeric Brun52a91d32017-08-31 14:41:55 +02001687 else if (sv->cur_admin & SRV_ADMF_MAINT)
William Lallemand74c24fb2016-11-21 17:18:36 +01001688 chunk_appendf(out, "MAINT");
1689 else
1690 chunk_appendf(out,
1691 srv_hlt_st[state],
Emeric Brun52a91d32017-08-31 14:41:55 +02001692 (ref->cur_state != SRV_ST_STOPPED) ? (ref->check.health - ref->check.rise + 1) : (ref->check.health),
1693 (ref->cur_state != SRV_ST_STOPPED) ? (ref->check.fall) : (ref->check.rise));
William Lallemand74c24fb2016-11-21 17:18:36 +01001694
1695 stats[ST_F_STATUS] = mkf_str(FO_STATUS, fld_status);
1696 stats[ST_F_LASTCHG] = mkf_u32(FN_AGE, now.tv_sec - sv->last_change);
Emeric Brun52a91d32017-08-31 14:41:55 +02001697 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 +01001698 stats[ST_F_ACT] = mkf_u32(FO_STATUS, (sv->flags & SRV_F_BACKUP) ? 0 : 1);
1699 stats[ST_F_BCK] = mkf_u32(FO_STATUS, (sv->flags & SRV_F_BACKUP) ? 1 : 0);
1700
1701 /* check failures: unique, fatal; last change, total downtime */
1702 if (sv->check.state & CHK_ST_ENABLED) {
1703 stats[ST_F_CHKFAIL] = mkf_u64(FN_COUNTER, sv->counters.failed_checks);
1704 stats[ST_F_CHKDOWN] = mkf_u64(FN_COUNTER, sv->counters.down_trans);
1705 stats[ST_F_DOWNTIME] = mkf_u32(FN_COUNTER, srv_downtime(sv));
1706 }
1707
1708 if (sv->maxqueue)
1709 stats[ST_F_QLIMIT] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->maxqueue);
1710
1711 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
1712 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1713 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, sv->puid);
1714
Emeric Brun52a91d32017-08-31 14:41:55 +02001715 if (sv->cur_state == SRV_ST_STARTING && !server_is_draining(sv))
William Lallemand74c24fb2016-11-21 17:18:36 +01001716 stats[ST_F_THROTTLE] = mkf_u32(FN_AVG, server_throttle_rate(sv));
1717
1718 stats[ST_F_LBTOT] = mkf_u64(FN_COUNTER, sv->counters.cum_lbconn);
1719
1720 if (sv->track) {
1721 char *fld_track = chunk_newstr(out);
1722
1723 chunk_appendf(out, "%s/%s", sv->track->proxy->id, sv->track->id);
1724 stats[ST_F_TRACKED] = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, fld_track);
1725 }
1726
1727 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_SV);
1728 stats[ST_F_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&sv->sess_per_sec));
1729 stats[ST_F_RATE_MAX] = mkf_u32(FN_MAX, sv->counters.sps_max);
1730
1731 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) {
1732 const char *fld_chksts;
1733
1734 fld_chksts = chunk_newstr(out);
1735 chunk_strcat(out, "* "); // for check in progress
1736 chunk_strcat(out, get_check_status_info(sv->check.status));
1737 if (!(sv->check.state & CHK_ST_INPROGRESS))
1738 fld_chksts += 2; // skip "* "
1739 stats[ST_F_CHECK_STATUS] = mkf_str(FN_OUTPUT, fld_chksts);
1740
1741 if (sv->check.status >= HCHK_STATUS_L57DATA)
1742 stats[ST_F_CHECK_CODE] = mkf_u32(FN_OUTPUT, sv->check.code);
1743
1744 if (sv->check.status >= HCHK_STATUS_CHECKED)
1745 stats[ST_F_CHECK_DURATION] = mkf_u64(FN_DURATION, sv->check.duration);
1746
1747 stats[ST_F_CHECK_DESC] = mkf_str(FN_OUTPUT, get_check_status_description(sv->check.status));
1748 stats[ST_F_LAST_CHK] = mkf_str(FN_OUTPUT, sv->check.desc);
1749 stats[ST_F_CHECK_RISE] = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.rise);
1750 stats[ST_F_CHECK_FALL] = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.fall);
1751 stats[ST_F_CHECK_HEALTH] = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.health);
1752 }
1753
1754 if ((sv->agent.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->agent.status));
1760 if (!(sv->agent.state & CHK_ST_INPROGRESS))
1761 fld_chksts += 2; // skip "* "
1762 stats[ST_F_AGENT_STATUS] = mkf_str(FN_OUTPUT, fld_chksts);
1763
1764 if (sv->agent.status >= HCHK_STATUS_L57DATA)
1765 stats[ST_F_AGENT_CODE] = mkf_u32(FN_OUTPUT, sv->agent.code);
1766
1767 if (sv->agent.status >= HCHK_STATUS_CHECKED)
1768 stats[ST_F_AGENT_DURATION] = mkf_u64(FN_DURATION, sv->agent.duration);
1769
1770 stats[ST_F_AGENT_DESC] = mkf_str(FN_OUTPUT, get_check_status_description(sv->agent.status));
1771 stats[ST_F_LAST_AGT] = mkf_str(FN_OUTPUT, sv->agent.desc);
1772 stats[ST_F_AGENT_RISE] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.rise);
1773 stats[ST_F_AGENT_FALL] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.fall);
1774 stats[ST_F_AGENT_HEALTH] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.health);
1775 }
1776
1777 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
1778 if (px->mode == PR_MODE_HTTP) {
1779 stats[ST_F_HRSP_1XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[1]);
1780 stats[ST_F_HRSP_2XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[2]);
1781 stats[ST_F_HRSP_3XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[3]);
1782 stats[ST_F_HRSP_4XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[4]);
1783 stats[ST_F_HRSP_5XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[5]);
1784 stats[ST_F_HRSP_OTHER] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[0]);
1785 }
1786
1787 if (ref->observe)
1788 stats[ST_F_HANAFAIL] = mkf_u64(FN_COUNTER, sv->counters.failed_hana);
1789
1790 stats[ST_F_CLI_ABRT] = mkf_u64(FN_COUNTER, sv->counters.cli_aborts);
1791 stats[ST_F_SRV_ABRT] = mkf_u64(FN_COUNTER, sv->counters.srv_aborts);
1792 stats[ST_F_LASTSESS] = mkf_s32(FN_AGE, srv_lastsession(sv));
1793
1794 stats[ST_F_QTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.q_time, TIME_STATS_SAMPLES));
1795 stats[ST_F_CTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.c_time, TIME_STATS_SAMPLES));
1796 stats[ST_F_RTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.d_time, TIME_STATS_SAMPLES));
1797 stats[ST_F_TTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.t_time, TIME_STATS_SAMPLES));
1798
1799 if (flags & ST_SHLGNDS) {
1800 switch (addr_to_str(&sv->addr, str, sizeof(str))) {
1801 case AF_INET:
1802 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
Willy Tarreau04276f32017-01-06 17:41:29 +01001803 chunk_appendf(out, "%s:%d", str, sv->svc_port);
William Lallemand74c24fb2016-11-21 17:18:36 +01001804 break;
1805 case AF_INET6:
1806 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
Willy Tarreau04276f32017-01-06 17:41:29 +01001807 chunk_appendf(out, "[%s]:%d", str, sv->svc_port);
William Lallemand74c24fb2016-11-21 17:18:36 +01001808 break;
1809 case AF_UNIX:
1810 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, "unix");
1811 break;
1812 case -1:
1813 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1814 chunk_strcat(out, strerror(errno));
1815 break;
1816 default: /* address family not supported */
1817 break;
1818 }
1819
1820 if (sv->cookie)
1821 stats[ST_F_COOKIE] = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, sv->cookie);
1822 }
1823
1824 return 1;
1825}
1826
1827/* Dumps a line for server <sv> and proxy <px> to the trash and uses the state
1828 * from stream interface <si>, stats flags <flags>, and server state <state>.
1829 * The caller is responsible for clearing the trash if needed. Returns non-zero
1830 * if it emits anything, zero otherwise.
1831 */
1832static int stats_dump_sv_stats(struct stream_interface *si, struct proxy *px, int flags, struct server *sv)
1833{
1834 struct appctx *appctx = __objt_appctx(si->end);
1835
1836 if (!stats_fill_sv_stats(px, sv, flags, stats, ST_F_TOTAL_FIELDS))
1837 return 0;
1838
1839 return stats_dump_one_line(stats, flags, px, appctx);
1840}
1841
1842/* Fill <stats> with the backend statistics. <stats> is
1843 * preallocated array of length <len>. The length of the array
1844 * must be at least ST_F_TOTAL_FIELDS. If this length is less
1845 * then this value, the function returns 0, otherwise, it
1846 * returns 1. <flags> can take the value ST_SHLGNDS.
1847 */
1848int stats_fill_be_stats(struct proxy *px, int flags, struct field *stats, int len)
1849{
1850 if (len < ST_F_TOTAL_FIELDS)
1851 return 0;
1852
1853 memset(stats, 0, sizeof(*stats) * len);
1854
1855 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1856 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, "BACKEND");
1857 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1858 stats[ST_F_QCUR] = mkf_u32(0, px->nbpend);
1859 stats[ST_F_QMAX] = mkf_u32(FN_MAX, px->be_counters.nbpend_max);
Thierry FOURNIER0ff98a42016-12-19 16:50:42 +01001860 stats[ST_F_SCUR] = mkf_u32(0, px->beconn);
William Lallemand74c24fb2016-11-21 17:18:36 +01001861 stats[ST_F_SMAX] = mkf_u32(FN_MAX, px->be_counters.conn_max);
1862 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, px->fullconn);
1863 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, px->be_counters.cum_conn);
1864 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, px->be_counters.bytes_in);
1865 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, px->be_counters.bytes_out);
1866 stats[ST_F_DREQ] = mkf_u64(FN_COUNTER, px->be_counters.denied_req);
1867 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, px->be_counters.denied_resp);
1868 stats[ST_F_ECON] = mkf_u64(FN_COUNTER, px->be_counters.failed_conns);
1869 stats[ST_F_ERESP] = mkf_u64(FN_COUNTER, px->be_counters.failed_resp);
1870 stats[ST_F_WRETR] = mkf_u64(FN_COUNTER, px->be_counters.retries);
1871 stats[ST_F_WREDIS] = mkf_u64(FN_COUNTER, px->be_counters.redispatches);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001872 stats[ST_F_WREW] = mkf_u64(FN_COUNTER, px->be_counters.failed_rewrites);
Willy Tarreauf1573842018-12-14 11:35:36 +01001873 stats[ST_F_CONNECT] = mkf_u64(FN_COUNTER, px->be_counters.connect);
1874 stats[ST_F_REUSE] = mkf_u64(FN_COUNTER, px->be_counters.reuse);
William Lallemand74c24fb2016-11-21 17:18:36 +01001875 stats[ST_F_STATUS] = mkf_str(FO_STATUS, (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" : "DOWN");
1876 stats[ST_F_WEIGHT] = mkf_u32(FN_AVG, (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv);
1877 stats[ST_F_ACT] = mkf_u32(0, px->srv_act);
1878 stats[ST_F_BCK] = mkf_u32(0, px->srv_bck);
1879 stats[ST_F_CHKDOWN] = mkf_u64(FN_COUNTER, px->down_trans);
1880 stats[ST_F_LASTCHG] = mkf_u32(FN_AGE, now.tv_sec - px->last_change);
1881 if (px->srv)
1882 stats[ST_F_DOWNTIME] = mkf_u32(FN_COUNTER, be_downtime(px));
1883
1884 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
1885 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1886 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, 0);
1887 stats[ST_F_LBTOT] = mkf_u64(FN_COUNTER, px->be_counters.cum_lbconn);
1888 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_BE);
1889 stats[ST_F_RATE] = mkf_u32(0, read_freq_ctr(&px->be_sess_per_sec));
1890 stats[ST_F_RATE_MAX] = mkf_u32(0, px->be_counters.sps_max);
1891
1892 if (flags & ST_SHLGNDS) {
1893 if (px->cookie_name)
1894 stats[ST_F_COOKIE] = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, px->cookie_name);
1895 stats[ST_F_ALGO] = mkf_str(FO_CONFIG|FS_SERVICE, backend_lb_algo_str(px->lbprm.algo & BE_LB_ALGO));
1896 }
1897
1898 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
1899 if (px->mode == PR_MODE_HTTP) {
1900 stats[ST_F_REQ_TOT] = mkf_u64(FN_COUNTER, px->be_counters.p.http.cum_req);
1901 stats[ST_F_HRSP_1XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[1]);
1902 stats[ST_F_HRSP_2XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[2]);
1903 stats[ST_F_HRSP_3XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[3]);
1904 stats[ST_F_HRSP_4XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[4]);
1905 stats[ST_F_HRSP_5XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[5]);
1906 stats[ST_F_HRSP_OTHER] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[0]);
Willy Tarreaua1214a52018-12-14 14:00:25 +01001907 stats[ST_F_CACHE_LOOKUPS] = mkf_u64(FN_COUNTER, px->be_counters.p.http.cache_lookups);
1908 stats[ST_F_CACHE_HITS] = mkf_u64(FN_COUNTER, px->be_counters.p.http.cache_hits);
William Lallemand74c24fb2016-11-21 17:18:36 +01001909 }
1910
1911 stats[ST_F_CLI_ABRT] = mkf_u64(FN_COUNTER, px->be_counters.cli_aborts);
1912 stats[ST_F_SRV_ABRT] = mkf_u64(FN_COUNTER, px->be_counters.srv_aborts);
1913
1914 /* compression: in, out, bypassed, responses */
1915 stats[ST_F_COMP_IN] = mkf_u64(FN_COUNTER, px->be_counters.comp_in);
1916 stats[ST_F_COMP_OUT] = mkf_u64(FN_COUNTER, px->be_counters.comp_out);
1917 stats[ST_F_COMP_BYP] = mkf_u64(FN_COUNTER, px->be_counters.comp_byp);
1918 stats[ST_F_COMP_RSP] = mkf_u64(FN_COUNTER, px->be_counters.p.http.comp_rsp);
1919 stats[ST_F_LASTSESS] = mkf_s32(FN_AGE, be_lastsession(px));
1920
1921 stats[ST_F_QTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.q_time, TIME_STATS_SAMPLES));
1922 stats[ST_F_CTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.c_time, TIME_STATS_SAMPLES));
1923 stats[ST_F_RTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.d_time, TIME_STATS_SAMPLES));
1924 stats[ST_F_TTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.t_time, TIME_STATS_SAMPLES));
1925
1926 return 1;
1927}
1928
1929/* Dumps a line for backend <px> to the trash for and uses the state from stream
1930 * interface <si> and stats flags <flags>. The caller is responsible for clearing
1931 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
1932 */
1933static int stats_dump_be_stats(struct stream_interface *si, struct proxy *px, int flags)
1934{
1935 struct appctx *appctx = __objt_appctx(si->end);
1936
1937 if (!(px->cap & PR_CAP_BE))
1938 return 0;
1939
1940 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_BE)))
1941 return 0;
1942
1943 if (!stats_fill_be_stats(px, flags, stats, ST_F_TOTAL_FIELDS))
1944 return 0;
1945
1946 return stats_dump_one_line(stats, flags, px, appctx);
1947}
1948
1949/* Dumps the HTML table header for proxy <px> to the trash for and uses the state from
1950 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
1951 * for clearing the trash if needed.
1952 */
1953static void stats_dump_html_px_hdr(struct stream_interface *si, struct proxy *px, struct uri_auth *uri)
1954{
1955 struct appctx *appctx = __objt_appctx(si->end);
1956 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
1957
1958 if (px->cap & PR_CAP_BE && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
1959 /* A form to enable/disable this proxy servers */
1960
1961 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
1962 scope_txt[0] = 0;
1963 if (appctx->ctx.stats.scope_len) {
Christopher Fauleted7a0662019-01-14 11:07:34 +01001964 const char *scope_ptr = stats_scope_ptr(appctx, si);
1965
William Lallemand74c24fb2016-11-21 17:18:36 +01001966 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Christopher Fauleted7a0662019-01-14 11:07:34 +01001967 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), scope_ptr, appctx->ctx.stats.scope_len);
William Lallemand74c24fb2016-11-21 17:18:36 +01001968 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
1969 }
1970
1971 chunk_appendf(&trash,
1972 "<form method=\"post\">");
1973 }
1974
1975 /* print a new table */
1976 chunk_appendf(&trash,
1977 "<table class=\"tbl\" width=\"100%%\">\n"
1978 "<tr class=\"titre\">"
1979 "<th class=\"pxname\" width=\"10%%\">");
1980
1981 chunk_appendf(&trash,
1982 "<a name=\"%s\"></a>%s"
1983 "<a class=px href=\"#%s\">%s</a>",
1984 px->id,
1985 (uri->flags & ST_SHLGNDS) ? "<u>":"",
1986 px->id, px->id);
1987
1988 if (uri->flags & ST_SHLGNDS) {
1989 /* cap, mode, id */
1990 chunk_appendf(&trash, "<div class=tips>cap: %s, mode: %s, id: %d",
1991 proxy_cap_str(px->cap), proxy_mode_str(px->mode),
1992 px->uuid);
1993 chunk_appendf(&trash, "</div>");
1994 }
1995
1996 chunk_appendf(&trash,
1997 "%s</th>"
1998 "<th class=\"%s\" width=\"90%%\">%s</th>"
1999 "</tr>\n"
2000 "</table>\n"
2001 "<table class=\"tbl\" width=\"100%%\">\n"
2002 "<tr class=\"titre\">",
2003 (uri->flags & ST_SHLGNDS) ? "</u>":"",
2004 px->desc ? "desc" : "empty", px->desc ? px->desc : "");
2005
2006 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
2007 /* Column heading for Enable or Disable server */
David Harrigand3db35a2016-12-30 12:12:49 +00002008 chunk_appendf(&trash,
2009 "<th rowspan=2 width=1><input type=\"checkbox\" \
2010 onclick=\"for(c in document.getElementsByClassName('%s-checkbox')) \
2011 document.getElementsByClassName('%s-checkbox').item(c).checked = this.checked\"></th>",
2012 px->id,
2013 px->id);
William Lallemand74c24fb2016-11-21 17:18:36 +01002014 }
2015
2016 chunk_appendf(&trash,
2017 "<th rowspan=2></th>"
2018 "<th colspan=3>Queue</th>"
2019 "<th colspan=3>Session rate</th><th colspan=6>Sessions</th>"
2020 "<th colspan=2>Bytes</th><th colspan=2>Denied</th>"
2021 "<th colspan=3>Errors</th><th colspan=2>Warnings</th>"
2022 "<th colspan=9>Server</th>"
2023 "</tr>\n"
2024 "<tr class=\"titre\">"
2025 "<th>Cur</th><th>Max</th><th>Limit</th>"
2026 "<th>Cur</th><th>Max</th><th>Limit</th><th>Cur</th><th>Max</th>"
2027 "<th>Limit</th><th>Total</th><th>LbTot</th><th>Last</th><th>In</th><th>Out</th>"
2028 "<th>Req</th><th>Resp</th><th>Req</th><th>Conn</th>"
2029 "<th>Resp</th><th>Retr</th><th>Redis</th>"
2030 "<th>Status</th><th>LastChk</th><th>Wght</th><th>Act</th>"
2031 "<th>Bck</th><th>Chk</th><th>Dwn</th><th>Dwntme</th>"
2032 "<th>Thrtle</th>\n"
2033 "</tr>");
2034}
2035
2036/* Dumps the HTML table trailer for proxy <px> to the trash for and uses the state from
2037 * stream interface <si>. The caller is responsible for clearing the trash if needed.
2038 */
2039static void stats_dump_html_px_end(struct stream_interface *si, struct proxy *px)
2040{
2041 struct appctx *appctx = __objt_appctx(si->end);
2042 chunk_appendf(&trash, "</table>");
2043
2044 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
2045 /* close the form used to enable/disable this proxy servers */
2046 chunk_appendf(&trash,
2047 "Choose the action to perform on the checked servers : "
2048 "<select name=action>"
2049 "<option value=\"\"></option>"
2050 "<option value=\"ready\">Set state to READY</option>"
2051 "<option value=\"drain\">Set state to DRAIN</option>"
2052 "<option value=\"maint\">Set state to MAINT</option>"
2053 "<option value=\"dhlth\">Health: disable checks</option>"
2054 "<option value=\"ehlth\">Health: enable checks</option>"
2055 "<option value=\"hrunn\">Health: force UP</option>"
2056 "<option value=\"hnolb\">Health: force NOLB</option>"
2057 "<option value=\"hdown\">Health: force DOWN</option>"
2058 "<option value=\"dagent\">Agent: disable checks</option>"
2059 "<option value=\"eagent\">Agent: enable checks</option>"
2060 "<option value=\"arunn\">Agent: force UP</option>"
2061 "<option value=\"adown\">Agent: force DOWN</option>"
2062 "<option value=\"shutdown\">Kill Sessions</option>"
2063 "</select>"
2064 "<input type=\"hidden\" name=\"b\" value=\"#%d\">"
2065 "&nbsp;<input type=\"submit\" value=\"Apply\">"
2066 "</form>",
2067 px->uuid);
2068 }
2069
2070 chunk_appendf(&trash, "<p>\n");
2071}
2072
2073/*
2074 * Dumps statistics for a proxy. The output is sent to the stream interface's
2075 * input buffer. Returns 0 if it had to stop dumping data because of lack of
2076 * buffer space, or non-zero if everything completed. This function is used
2077 * both by the CLI and the HTTP entry points, and is able to dump the output
2078 * in HTML or CSV formats. If the later, <uri> must be NULL.
2079 */
Christopher Fauletef779222018-10-31 08:47:01 +01002080int stats_dump_proxy_to_buffer(struct stream_interface *si, struct htx *htx,
2081 struct proxy *px, struct uri_auth *uri)
William Lallemand74c24fb2016-11-21 17:18:36 +01002082{
2083 struct appctx *appctx = __objt_appctx(si->end);
2084 struct stream *s = si_strm(si);
2085 struct channel *rep = si_ic(si);
2086 struct server *sv, *svs; /* server and server-state, server-state=server or server->track */
2087 struct listener *l;
2088 unsigned int flags;
2089
2090 if (uri)
2091 flags = uri->flags;
William Lallemand07a62f72017-05-24 00:57:40 +02002092 else if ((strm_li(s)->bind_conf->level & ACCESS_LVL_MASK) >= ACCESS_LVL_OPER)
William Lallemand74c24fb2016-11-21 17:18:36 +01002093 flags = ST_SHLGNDS | ST_SHNODE | ST_SHDESC;
2094 else
2095 flags = ST_SHNODE | ST_SHDESC;
2096
2097 chunk_reset(&trash);
2098
2099 switch (appctx->ctx.stats.px_st) {
2100 case STAT_PX_ST_INIT:
2101 /* we are on a new proxy */
2102 if (uri && uri->scope) {
2103 /* we have a limited scope, we have to check the proxy name */
2104 struct stat_scope *scope;
2105 int len;
2106
2107 len = strlen(px->id);
2108 scope = uri->scope;
2109
2110 while (scope) {
2111 /* match exact proxy name */
2112 if (scope->px_len == len && !memcmp(px->id, scope->px_id, len))
2113 break;
2114
2115 /* match '.' which means 'self' proxy */
2116 if (!strcmp(scope->px_id, ".") && px == s->be)
2117 break;
2118 scope = scope->next;
2119 }
2120
2121 /* proxy name not found : don't dump anything */
2122 if (scope == NULL)
2123 return 1;
2124 }
2125
2126 /* if the user has requested a limited output and the proxy
2127 * name does not match, skip it.
2128 */
Christopher Fauleted7a0662019-01-14 11:07:34 +01002129 if (appctx->ctx.stats.scope_len) {
2130 const char *scope_ptr = stats_scope_ptr(appctx, si);
2131
2132 if (strnistr(px->id, strlen(px->id), scope_ptr, appctx->ctx.stats.scope_len) == NULL)
2133 return 1;
2134 }
William Lallemand74c24fb2016-11-21 17:18:36 +01002135
2136 if ((appctx->ctx.stats.flags & STAT_BOUND) &&
2137 (appctx->ctx.stats.iid != -1) &&
2138 (px->uuid != appctx->ctx.stats.iid))
2139 return 1;
2140
2141 appctx->ctx.stats.px_st = STAT_PX_ST_TH;
2142 /* fall through */
2143
2144 case STAT_PX_ST_TH:
2145 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
2146 stats_dump_html_px_hdr(si, px, uri);
Christopher Fauletef779222018-10-31 08:47:01 +01002147 if (!stats_putchk(rep, htx, &trash))
2148 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002149 }
2150
2151 appctx->ctx.stats.px_st = STAT_PX_ST_FE;
2152 /* fall through */
2153
2154 case STAT_PX_ST_FE:
2155 /* print the frontend */
2156 if (stats_dump_fe_stats(si, px)) {
Christopher Fauletef779222018-10-31 08:47:01 +01002157 if (!stats_putchk(rep, htx, &trash))
2158 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002159 }
2160
2161 appctx->ctx.stats.l = px->conf.listeners.n;
2162 appctx->ctx.stats.px_st = STAT_PX_ST_LI;
2163 /* fall through */
2164
2165 case STAT_PX_ST_LI:
2166 /* stats.l has been initialized above */
2167 for (; appctx->ctx.stats.l != &px->conf.listeners; appctx->ctx.stats.l = l->by_fe.n) {
Christopher Fauletef779222018-10-31 08:47:01 +01002168 if (htx) {
2169 if (htx_almost_full(htx))
2170 goto full;
2171 }
2172 else {
2173 if (buffer_almost_full(&rep->buf))
2174 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002175 }
2176
2177 l = LIST_ELEM(appctx->ctx.stats.l, struct listener *, by_fe);
2178 if (!l->counters)
2179 continue;
2180
2181 if (appctx->ctx.stats.flags & STAT_BOUND) {
2182 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SO)))
2183 break;
2184
2185 if (appctx->ctx.stats.sid != -1 && l->luid != appctx->ctx.stats.sid)
2186 continue;
2187 }
2188
2189 /* print the frontend */
2190 if (stats_dump_li_stats(si, px, l, flags)) {
Christopher Fauletef779222018-10-31 08:47:01 +01002191 if (!stats_putchk(rep, htx, &trash))
2192 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002193 }
2194 }
2195
2196 appctx->ctx.stats.sv = px->srv; /* may be NULL */
2197 appctx->ctx.stats.px_st = STAT_PX_ST_SV;
2198 /* fall through */
2199
2200 case STAT_PX_ST_SV:
2201 /* stats.sv has been initialized above */
2202 for (; appctx->ctx.stats.sv != NULL; appctx->ctx.stats.sv = sv->next) {
Christopher Fauletef779222018-10-31 08:47:01 +01002203 if (htx) {
2204 if (htx_almost_full(htx))
2205 goto full;
2206 }
2207 else {
2208 if (buffer_almost_full(&rep->buf))
2209 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002210 }
2211
2212 sv = appctx->ctx.stats.sv;
2213
2214 if (appctx->ctx.stats.flags & STAT_BOUND) {
2215 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SV)))
2216 break;
2217
2218 if (appctx->ctx.stats.sid != -1 && sv->puid != appctx->ctx.stats.sid)
2219 continue;
2220 }
2221
2222 svs = sv;
2223 while (svs->track)
2224 svs = svs->track;
2225
2226 /* do not report servers which are DOWN and not changing state */
2227 if ((appctx->ctx.stats.flags & STAT_HIDE_DOWN) &&
Emeric Brun52a91d32017-08-31 14:41:55 +02002228 ((sv->cur_admin & SRV_ADMF_MAINT) || /* server is in maintenance */
2229 (sv->cur_state == SRV_ST_STOPPED && /* server is down */
William Lallemand74c24fb2016-11-21 17:18:36 +01002230 (!((svs->agent.state | svs->check.state) & CHK_ST_ENABLED) ||
2231 ((svs->agent.state & CHK_ST_ENABLED) && !svs->agent.health) ||
2232 ((svs->check.state & CHK_ST_ENABLED) && !svs->check.health))))) {
2233 continue;
2234 }
2235
2236 if (stats_dump_sv_stats(si, px, flags, sv)) {
Christopher Fauletef779222018-10-31 08:47:01 +01002237 if (!stats_putchk(rep, htx, &trash))
2238 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002239 }
2240 } /* for sv */
2241
2242 appctx->ctx.stats.px_st = STAT_PX_ST_BE;
2243 /* fall through */
2244
2245 case STAT_PX_ST_BE:
2246 /* print the backend */
2247 if (stats_dump_be_stats(si, px, flags)) {
Christopher Fauletef779222018-10-31 08:47:01 +01002248 if (!stats_putchk(rep, htx, &trash))
2249 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002250 }
2251
2252 appctx->ctx.stats.px_st = STAT_PX_ST_END;
2253 /* fall through */
2254
2255 case STAT_PX_ST_END:
2256 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
2257 stats_dump_html_px_end(si, px);
Christopher Fauletef779222018-10-31 08:47:01 +01002258 if (!stats_putchk(rep, htx, &trash))
2259 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002260 }
2261
2262 appctx->ctx.stats.px_st = STAT_PX_ST_FIN;
2263 /* fall through */
2264
2265 case STAT_PX_ST_FIN:
2266 return 1;
2267
2268 default:
2269 /* unknown state, we should put an abort() here ! */
2270 return 1;
2271 }
Christopher Fauletef779222018-10-31 08:47:01 +01002272
2273 full:
2274 si_rx_room_blk(si);
2275 return 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01002276}
2277
2278/* Dumps the HTTP stats head block to the trash for and uses the per-uri
2279 * parameters <uri>. The caller is responsible for clearing the trash if needed.
2280 */
2281static void stats_dump_html_head(struct uri_auth *uri)
2282{
2283 /* WARNING! This must fit in the first buffer !!! */
2284 chunk_appendf(&trash,
2285 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n"
2286 "\"http://www.w3.org/TR/html4/loose.dtd\">\n"
2287 "<html><head><title>Statistics Report for " PRODUCT_NAME "%s%s</title>\n"
2288 "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n"
2289 "<style type=\"text/css\"><!--\n"
2290 "body {"
2291 " font-family: arial, helvetica, sans-serif;"
2292 " font-size: 12px;"
2293 " font-weight: normal;"
2294 " color: black;"
2295 " background: white;"
2296 "}\n"
2297 "th,td {"
2298 " font-size: 10px;"
2299 "}\n"
2300 "h1 {"
2301 " font-size: x-large;"
2302 " margin-bottom: 0.5em;"
2303 "}\n"
2304 "h2 {"
2305 " font-family: helvetica, arial;"
2306 " font-size: x-large;"
2307 " font-weight: bold;"
2308 " font-style: italic;"
2309 " color: #6020a0;"
2310 " margin-top: 0em;"
2311 " margin-bottom: 0em;"
2312 "}\n"
2313 "h3 {"
2314 " font-family: helvetica, arial;"
2315 " font-size: 16px;"
2316 " font-weight: bold;"
2317 " color: #b00040;"
2318 " background: #e8e8d0;"
2319 " margin-top: 0em;"
2320 " margin-bottom: 0em;"
2321 "}\n"
2322 "li {"
2323 " margin-top: 0.25em;"
2324 " margin-right: 2em;"
2325 "}\n"
2326 ".hr {margin-top: 0.25em;"
2327 " border-color: black;"
2328 " border-bottom-style: solid;"
2329 "}\n"
2330 ".titre {background: #20D0D0;color: #000000; font-weight: bold; text-align: center;}\n"
2331 ".total {background: #20D0D0;color: #ffff80;}\n"
2332 ".frontend {background: #e8e8d0;}\n"
2333 ".socket {background: #d0d0d0;}\n"
2334 ".backend {background: #e8e8d0;}\n"
2335 ".active_down {background: #ff9090;}\n"
2336 ".active_going_up {background: #ffd020;}\n"
2337 ".active_going_down {background: #ffffa0;}\n"
2338 ".active_up {background: #c0ffc0;}\n"
2339 ".active_nolb {background: #20a0ff;}\n"
2340 ".active_draining {background: #20a0FF;}\n"
2341 ".active_no_check {background: #e0e0e0;}\n"
2342 ".backup_down {background: #ff9090;}\n"
2343 ".backup_going_up {background: #ff80ff;}\n"
2344 ".backup_going_down {background: #c060ff;}\n"
2345 ".backup_up {background: #b0d0ff;}\n"
2346 ".backup_nolb {background: #90b0e0;}\n"
2347 ".backup_draining {background: #cc9900;}\n"
2348 ".backup_no_check {background: #e0e0e0;}\n"
2349 ".maintain {background: #c07820;}\n"
2350 ".rls {letter-spacing: 0.2em; margin-right: 1px;}\n" /* right letter spacing (used for grouping digits) */
2351 "\n"
2352 "a.px:link {color: #ffff40; text-decoration: none;}"
2353 "a.px:visited {color: #ffff40; text-decoration: none;}"
2354 "a.px:hover {color: #ffffff; text-decoration: none;}"
2355 "a.lfsb:link {color: #000000; text-decoration: none;}"
2356 "a.lfsb:visited {color: #000000; text-decoration: none;}"
2357 "a.lfsb:hover {color: #505050; text-decoration: none;}"
2358 "\n"
2359 "table.tbl { border-collapse: collapse; border-style: none;}\n"
2360 "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"
2361 "table.tbl td.ac { text-align: center;}\n"
2362 "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}\n"
2363 "table.tbl th.pxname { background: #b00040; color: #ffff40; font-weight: bold; border-style: solid solid none solid; padding: 2px 3px; white-space: nowrap;}\n"
2364 "table.tbl th.empty { border-style: none; empty-cells: hide; background: white;}\n"
2365 "table.tbl th.desc { background: white; border-style: solid solid none solid; text-align: left; padding: 2px 3px;}\n"
2366 "\n"
2367 "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n"
2368 "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n"
2369 "table.lgd td.noborder { border-style: none; padding: 2px; white-space: nowrap;}\n"
2370 "table.det { border-collapse: collapse; border-style: none; }\n"
2371 "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"
2372 "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"
2373 "u {text-decoration:none; border-bottom: 1px dotted black;}\n"
2374 "div.tips {\n"
2375 " display:block;\n"
2376 " visibility:hidden;\n"
2377 " z-index:2147483647;\n"
2378 " position:absolute;\n"
2379 " padding:2px 4px 3px;\n"
2380 " background:#f0f060; color:#000000;\n"
2381 " border:1px solid #7040c0;\n"
2382 " white-space:nowrap;\n"
2383 " font-style:normal;font-size:11px;font-weight:normal;\n"
2384 " -moz-border-radius:3px;-webkit-border-radius:3px;border-radius:3px;\n"
2385 " -moz-box-shadow:gray 2px 2px 3px;-webkit-box-shadow:gray 2px 2px 3px;box-shadow:gray 2px 2px 3px;\n"
2386 "}\n"
2387 "u:hover div.tips {visibility:visible;}\n"
2388 "-->\n"
2389 "</style></head>\n",
2390 (uri->flags & ST_SHNODE) ? " on " : "",
2391 (uri->flags & ST_SHNODE) ? (uri->node ? uri->node : global.node) : ""
2392 );
2393}
2394
2395/* Dumps the HTML stats information block to the trash for and uses the state from
2396 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
2397 * for clearing the trash if needed.
2398 */
2399static void stats_dump_html_info(struct stream_interface *si, struct uri_auth *uri)
2400{
2401 struct appctx *appctx = __objt_appctx(si->end);
2402 unsigned int up = (now.tv_sec - start_date.tv_sec);
2403 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
Christopher Fauleted7a0662019-01-14 11:07:34 +01002404 const char *scope_ptr = stats_scope_ptr(appctx, si);
Willy Tarreau1713c032019-05-23 12:23:55 +02002405 unsigned long long bps = (unsigned long long)read_freq_ctr(&global.out_32bps) * 32;
2406
2407 /* Turn the bytes per second to bits per second and take care of the
2408 * usual ethernet overhead in order to help figure how far we are from
2409 * interface saturation since it's the only case which usually matters.
2410 * For this we count the total size of an Ethernet frame on the wire
2411 * including preamble and IFG (1538) for the largest TCP segment it
2412 * transports (1448 with TCP timestamps). This is not valid for smaller
2413 * packets (under-estimated), but it gives a reasonably accurate
2414 * estimation of how far we are from uplink saturation.
2415 */
2416 bps = bps * 8 * 1538 / 1448;
William Lallemand74c24fb2016-11-21 17:18:36 +01002417
2418 /* WARNING! this has to fit the first packet too.
2419 * We are around 3.5 kB, add adding entries will
2420 * become tricky if we want to support 4kB buffers !
2421 */
2422 chunk_appendf(&trash,
2423 "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">"
2424 PRODUCT_NAME "%s</a></h1>\n"
2425 "<h2>Statistics Report for pid %d%s%s%s%s</h2>\n"
2426 "<hr width=\"100%%\" class=\"hr\">\n"
2427 "<h3>&gt; General process information</h3>\n"
2428 "<table border=0><tr><td align=\"left\" nowrap width=\"1%%\">\n"
Yves Lafon95317282018-02-26 11:10:37 +01002429 "<p><b>pid = </b> %d (process #%d, nbproc = %d, nbthread = %d)<br>\n"
William Lallemand74c24fb2016-11-21 17:18:36 +01002430 "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n"
2431 "<b>system limits:</b> memmax = %s%s; ulimit-n = %d<br>\n"
2432 "<b>maxsock = </b> %d; <b>maxconn = </b> %d; <b>maxpipes = </b> %d<br>\n"
Willy Tarreau1713c032019-05-23 12:23:55 +02002433 "current conns = %d; current pipes = %d/%d; conn rate = %d/sec; bit rate = %.3f %cbps<br>\n"
William Lallemand74c24fb2016-11-21 17:18:36 +01002434 "Running tasks: %d/%d; idle = %d %%<br>\n"
2435 "</td><td align=\"center\" nowrap>\n"
2436 "<table class=\"lgd\"><tr>\n"
2437 "<td class=\"active_up\">&nbsp;</td><td class=\"noborder\">active UP </td>"
2438 "<td class=\"backup_up\">&nbsp;</td><td class=\"noborder\">backup UP </td>"
2439 "</tr><tr>\n"
2440 "<td class=\"active_going_down\"></td><td class=\"noborder\">active UP, going down </td>"
2441 "<td class=\"backup_going_down\"></td><td class=\"noborder\">backup UP, going down </td>"
2442 "</tr><tr>\n"
2443 "<td class=\"active_going_up\"></td><td class=\"noborder\">active DOWN, going up </td>"
2444 "<td class=\"backup_going_up\"></td><td class=\"noborder\">backup DOWN, going up </td>"
2445 "</tr><tr>\n"
2446 "<td class=\"active_down\"></td><td class=\"noborder\">active or backup DOWN &nbsp;</td>"
2447 "<td class=\"active_no_check\"></td><td class=\"noborder\">not checked </td>"
2448 "</tr><tr>\n"
2449 "<td class=\"maintain\"></td><td class=\"noborder\" colspan=\"3\">active or backup DOWN for maintenance (MAINT) &nbsp;</td>"
2450 "</tr><tr>\n"
2451 "<td class=\"active_draining\"></td><td class=\"noborder\" colspan=\"3\">active or backup SOFT STOPPED for maintenance &nbsp;</td>"
2452 "</tr></table>\n"
2453 "Note: \"NOLB\"/\"DRAIN\" = UP with load-balancing disabled."
2454 "</td>"
2455 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
2456 "<b>Display option:</b><ul style=\"margin-top: 0.25em;\">"
2457 "",
Willy Tarreau909b9d82019-01-04 18:20:32 +01002458 (uri->flags & ST_HIDEVER) ? "" : (stats_version_string),
William Lallemand74c24fb2016-11-21 17:18:36 +01002459 pid, (uri->flags & ST_SHNODE) ? " on " : "",
2460 (uri->flags & ST_SHNODE) ? (uri->node ? uri->node : global.node) : "",
2461 (uri->flags & ST_SHDESC) ? ": " : "",
2462 (uri->flags & ST_SHDESC) ? (uri->desc ? uri->desc : global.desc) : "",
Yves Lafon95317282018-02-26 11:10:37 +01002463 pid, relative_pid, global.nbproc, global.nbthread,
William Lallemand74c24fb2016-11-21 17:18:36 +01002464 up / 86400, (up % 86400) / 3600,
2465 (up % 3600) / 60, (up % 60),
2466 global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited",
2467 global.rlimit_memmax ? " MB" : "",
2468 global.rlimit_nofile,
2469 global.maxsock, global.maxconn, global.maxpipes,
2470 actconn, pipes_used, pipes_used+pipes_free, read_freq_ctr(&global.conn_per_sec),
Willy Tarreau1713c032019-05-23 12:23:55 +02002471 bps >= 1000000000UL ? (bps / 1000000000.0) : bps >= 1000000UL ? (bps / 1000000.0) : (bps / 1000.0),
2472 bps >= 1000000000UL ? 'G' : bps >= 1000000UL ? 'M' : 'k',
Willy Tarreau81036f22019-05-20 19:24:50 +02002473 tasks_run_queue_cur, nb_tasks_cur, ti->idle_pct
William Lallemand74c24fb2016-11-21 17:18:36 +01002474 );
2475
2476 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Christopher Fauleted7a0662019-01-14 11:07:34 +01002477 memcpy(scope_txt, scope_ptr, appctx->ctx.stats.scope_len);
William Lallemand74c24fb2016-11-21 17:18:36 +01002478 scope_txt[appctx->ctx.stats.scope_len] = '\0';
2479
2480 chunk_appendf(&trash,
2481 "<li><form method=\"GET\">Scope : <input value=\"%s\" name=\"" STAT_SCOPE_INPUT_NAME "\" size=\"8\" maxlength=\"%d\" tabindex=\"1\"/></form>\n",
2482 (appctx->ctx.stats.scope_len > 0) ? scope_txt : "",
2483 STAT_SCOPE_TXT_MAXLEN);
2484
2485 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
2486 scope_txt[0] = 0;
2487 if (appctx->ctx.stats.scope_len) {
2488 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Christopher Fauleted7a0662019-01-14 11:07:34 +01002489 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), scope_ptr, appctx->ctx.stats.scope_len);
William Lallemand74c24fb2016-11-21 17:18:36 +01002490 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
2491 }
2492
2493 if (appctx->ctx.stats.flags & STAT_HIDE_DOWN)
2494 chunk_appendf(&trash,
2495 "<li><a href=\"%s%s%s%s\">Show all servers</a><br>\n",
2496 uri->uri_prefix,
2497 "",
2498 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2499 scope_txt);
2500 else
2501 chunk_appendf(&trash,
2502 "<li><a href=\"%s%s%s%s\">Hide 'DOWN' servers</a><br>\n",
2503 uri->uri_prefix,
2504 ";up",
2505 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2506 scope_txt);
2507
2508 if (uri->refresh > 0) {
2509 if (appctx->ctx.stats.flags & STAT_NO_REFRESH)
2510 chunk_appendf(&trash,
2511 "<li><a href=\"%s%s%s%s\">Enable refresh</a><br>\n",
2512 uri->uri_prefix,
2513 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2514 "",
2515 scope_txt);
2516 else
2517 chunk_appendf(&trash,
2518 "<li><a href=\"%s%s%s%s\">Disable refresh</a><br>\n",
2519 uri->uri_prefix,
2520 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2521 ";norefresh",
2522 scope_txt);
2523 }
2524
2525 chunk_appendf(&trash,
2526 "<li><a href=\"%s%s%s%s\">Refresh now</a><br>\n",
2527 uri->uri_prefix,
2528 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2529 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2530 scope_txt);
2531
2532 chunk_appendf(&trash,
2533 "<li><a href=\"%s;csv%s%s\">CSV export</a><br>\n",
2534 uri->uri_prefix,
2535 (uri->refresh > 0) ? ";norefresh" : "",
2536 scope_txt);
2537
2538 chunk_appendf(&trash,
2539 "</ul></td>"
2540 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
2541 "<b>External resources:</b><ul style=\"margin-top: 0.25em;\">\n"
2542 "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>\n"
2543 "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>\n"
2544 "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>\n"
2545 "</ul>"
2546 "</td>"
2547 "</tr></table>\n"
2548 ""
2549 );
2550
2551 if (appctx->ctx.stats.st_code) {
2552 switch (appctx->ctx.stats.st_code) {
2553 case STAT_STATUS_DONE:
2554 chunk_appendf(&trash,
2555 "<p><div class=active_up>"
2556 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2557 "Action processed successfully."
2558 "</div>\n", uri->uri_prefix,
2559 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2560 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2561 scope_txt);
2562 break;
2563 case STAT_STATUS_NONE:
2564 chunk_appendf(&trash,
2565 "<p><div class=active_going_down>"
2566 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2567 "Nothing has changed."
2568 "</div>\n", uri->uri_prefix,
2569 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2570 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2571 scope_txt);
2572 break;
2573 case STAT_STATUS_PART:
2574 chunk_appendf(&trash,
2575 "<p><div class=active_going_down>"
2576 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2577 "Action partially processed.<br>"
2578 "Some server names are probably unknown or ambiguous (duplicated names in the backend)."
2579 "</div>\n", uri->uri_prefix,
2580 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2581 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2582 scope_txt);
2583 break;
2584 case STAT_STATUS_ERRP:
2585 chunk_appendf(&trash,
2586 "<p><div class=active_down>"
2587 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2588 "Action not processed because of invalid parameters."
2589 "<ul>"
2590 "<li>The action is maybe unknown.</li>"
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01002591 "<li>Invalid key parameter (empty or too long).</li>"
William Lallemand74c24fb2016-11-21 17:18:36 +01002592 "<li>The backend name is probably unknown or ambiguous (duplicated names).</li>"
2593 "<li>Some server names are probably unknown or ambiguous (duplicated names in the backend).</li>"
2594 "</ul>"
2595 "</div>\n", uri->uri_prefix,
2596 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2597 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2598 scope_txt);
2599 break;
2600 case STAT_STATUS_EXCD:
2601 chunk_appendf(&trash,
2602 "<p><div class=active_down>"
2603 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2604 "<b>Action not processed : the buffer couldn't store all the data.<br>"
2605 "You should retry with less servers at a time.</b>"
2606 "</div>\n", uri->uri_prefix,
2607 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2608 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2609 scope_txt);
2610 break;
2611 case STAT_STATUS_DENY:
2612 chunk_appendf(&trash,
2613 "<p><div class=active_down>"
2614 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2615 "<b>Action denied.</b>"
2616 "</div>\n", uri->uri_prefix,
2617 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2618 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2619 scope_txt);
2620 break;
Christopher Faulet3c2ecf72019-02-27 16:41:27 +01002621 case STAT_STATUS_IVAL:
2622 chunk_appendf(&trash,
2623 "<p><div class=active_down>"
2624 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2625 "<b>Invalid requests (unsupported method or chunked encoded request).</b>"
2626 "</div>\n", uri->uri_prefix,
2627 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2628 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2629 scope_txt);
2630 break;
William Lallemand74c24fb2016-11-21 17:18:36 +01002631 default:
2632 chunk_appendf(&trash,
2633 "<p><div class=active_no_check>"
2634 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2635 "Unexpected result."
2636 "</div>\n", uri->uri_prefix,
2637 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2638 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2639 scope_txt);
2640 }
2641 chunk_appendf(&trash, "<p>\n");
2642 }
2643}
2644
2645/* Dumps the HTML stats trailer block to the trash. The caller is responsible
2646 * for clearing the trash if needed.
2647 */
2648static void stats_dump_html_end()
2649{
2650 chunk_appendf(&trash, "</body></html>\n");
2651}
2652
Simon Horman05ee2132017-01-04 09:37:25 +01002653/* Dumps the stats JSON header to the trash buffer which. The caller is responsible
2654 * for clearing it if needed.
2655 */
2656static void stats_dump_json_header()
2657{
2658 chunk_strcat(&trash, "[");
2659}
2660
2661
2662/* Dumps the JSON stats trailer block to the trash. The caller is responsible
2663 * for clearing the trash if needed.
2664 */
2665static void stats_dump_json_end()
2666{
2667 chunk_strcat(&trash, "]");
2668}
2669
William Lallemand74c24fb2016-11-21 17:18:36 +01002670/* This function dumps statistics onto the stream interface's read buffer in
2671 * either CSV or HTML format. <uri> contains some HTML-specific parameters that
2672 * are ignored for CSV format (hence <uri> may be NULL there). It returns 0 if
2673 * it had to stop writing data and an I/O is needed, 1 if the dump is finished
2674 * and the stream must be closed, or -1 in case of any error. This function is
2675 * used by both the CLI and the HTTP handlers.
2676 */
Christopher Fauletef779222018-10-31 08:47:01 +01002677static int stats_dump_stat_to_buffer(struct stream_interface *si, struct htx *htx,
2678 struct uri_auth *uri)
William Lallemand74c24fb2016-11-21 17:18:36 +01002679{
2680 struct appctx *appctx = __objt_appctx(si->end);
2681 struct channel *rep = si_ic(si);
2682 struct proxy *px;
2683
2684 chunk_reset(&trash);
2685
2686 switch (appctx->st2) {
2687 case STAT_ST_INIT:
2688 appctx->st2 = STAT_ST_HEAD; /* let's start producing data */
2689 /* fall through */
2690
2691 case STAT_ST_HEAD:
2692 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
2693 stats_dump_html_head(uri);
Simon Horman05ee2132017-01-04 09:37:25 +01002694 else if (appctx->ctx.stats.flags & STAT_FMT_JSON)
Willy Tarreau9d7fb632017-04-11 07:53:04 +02002695 stats_dump_json_header();
William Lallemand74c24fb2016-11-21 17:18:36 +01002696 else if (!(appctx->ctx.stats.flags & STAT_FMT_TYPED))
2697 stats_dump_csv_header();
2698
Christopher Fauletef779222018-10-31 08:47:01 +01002699 if (!stats_putchk(rep, htx, &trash))
2700 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002701
2702 appctx->st2 = STAT_ST_INFO;
2703 /* fall through */
2704
2705 case STAT_ST_INFO:
2706 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
2707 stats_dump_html_info(si, uri);
Christopher Fauletef779222018-10-31 08:47:01 +01002708 if (!stats_putchk(rep, htx, &trash))
2709 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002710 }
2711
Olivier Houchardfbc74e82017-11-24 16:54:05 +01002712 appctx->ctx.stats.px = proxies_list;
William Lallemand74c24fb2016-11-21 17:18:36 +01002713 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
2714 appctx->st2 = STAT_ST_LIST;
2715 /* fall through */
2716
2717 case STAT_ST_LIST:
2718 /* dump proxies */
2719 while (appctx->ctx.stats.px) {
Christopher Fauletef779222018-10-31 08:47:01 +01002720 if (htx) {
2721 if (htx_almost_full(htx))
2722 goto full;
2723 }
2724 else {
2725 if (buffer_almost_full(&rep->buf))
2726 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002727 }
2728
2729 px = appctx->ctx.stats.px;
2730 /* skip the disabled proxies, global frontend and non-networked ones */
2731 if (px->state != PR_STSTOPPED && px->uuid > 0 && (px->cap & (PR_CAP_FE | PR_CAP_BE)))
Christopher Fauletef779222018-10-31 08:47:01 +01002732 if (stats_dump_proxy_to_buffer(si, htx, px, uri) == 0)
William Lallemand74c24fb2016-11-21 17:18:36 +01002733 return 0;
2734
2735 appctx->ctx.stats.px = px->next;
2736 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
2737 }
2738 /* here, we just have reached the last proxy */
2739
2740 appctx->st2 = STAT_ST_END;
2741 /* fall through */
2742
2743 case STAT_ST_END:
Simon Horman05ee2132017-01-04 09:37:25 +01002744 if (appctx->ctx.stats.flags & (STAT_FMT_HTML|STAT_FMT_JSON)) {
2745 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
2746 stats_dump_html_end();
2747 else
2748 stats_dump_json_end();
Christopher Fauletef779222018-10-31 08:47:01 +01002749 if (!stats_putchk(rep, htx, &trash))
2750 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002751 }
2752
2753 appctx->st2 = STAT_ST_FIN;
2754 /* fall through */
2755
2756 case STAT_ST_FIN:
2757 return 1;
2758
2759 default:
2760 /* unknown state ! */
2761 appctx->st2 = STAT_ST_FIN;
2762 return -1;
2763 }
Christopher Fauletef779222018-10-31 08:47:01 +01002764
2765 full:
2766 si_rx_room_blk(si);
2767 return 0;
2768
William Lallemand74c24fb2016-11-21 17:18:36 +01002769}
2770
2771/* We reached the stats page through a POST request. The appctx is
2772 * expected to have already been allocated by the caller.
2773 * Parse the posted data and enable/disable servers if necessary.
2774 * Returns 1 if request was parsed or zero if it needs more data.
2775 */
2776static int stats_process_http_post(struct stream_interface *si)
2777{
2778 struct stream *s = si_strm(si);
2779 struct appctx *appctx = objt_appctx(si->end);
2780
2781 struct proxy *px = NULL;
2782 struct server *sv = NULL;
2783
2784 char key[LINESIZE];
2785 int action = ST_ADM_ACTION_NONE;
2786 int reprocess = 0;
2787
2788 int total_servers = 0;
2789 int altered_servers = 0;
2790
2791 char *first_param, *cur_param, *next_param, *end_params;
2792 char *st_cur_param = NULL;
2793 char *st_next_param = NULL;
2794
Christopher Fauleta3618372018-12-13 21:59:56 +01002795 struct buffer *temp = get_trash_chunk();
William Lallemand74c24fb2016-11-21 17:18:36 +01002796
Christopher Fauleta3618372018-12-13 21:59:56 +01002797 if (IS_HTX_STRM(s)) {
2798 struct htx *htx = htxbuf(&s->req.buf);
2799 struct htx_blk *blk;
Christopher Fauleta3618372018-12-13 21:59:56 +01002800
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01002801 /* we need more data */
2802 if (s->txn->req.msg_state < HTTP_MSG_DONE) {
2803 /* check if we can receive more */
2804 if (htx_free_data_space(htx) <= global.tune.maxrewrite) {
2805 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
2806 goto out;
Christopher Fauleta3618372018-12-13 21:59:56 +01002807 }
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01002808 goto wait;
Christopher Fauleta3618372018-12-13 21:59:56 +01002809 }
William Lallemand74c24fb2016-11-21 17:18:36 +01002810
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01002811 /* The request was fully received. Copy data */
Christopher Faulet8fa60e42019-05-23 11:04:05 +02002812 blk = htx_get_head_blk(htx);
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01002813 while (blk) {
Christopher Fauleta3618372018-12-13 21:59:56 +01002814 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Fauleta3618372018-12-13 21:59:56 +01002815
Christopher Faulet54b5e212019-06-04 10:08:28 +02002816 if (type == HTX_BLK_EOM || type == HTX_BLK_TLR || type == HTX_BLK_EOT)
Christopher Fauleta3618372018-12-13 21:59:56 +01002817 break;
2818 if (type == HTX_BLK_DATA) {
2819 struct ist v = htx_get_blk_value(htx, blk);
2820
2821 if (!chunk_memcat(temp, v.ptr, v.len)) {
2822 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
2823 goto out;
2824 }
2825 }
Christopher Fauleted7a0662019-01-14 11:07:34 +01002826 blk = htx_get_next_blk(htx, blk);
Christopher Fauleta3618372018-12-13 21:59:56 +01002827 }
2828 }
2829 else {
2830 int reql;
2831
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01002832 /* we need more data */
2833 if (s->txn->req.msg_state < HTTP_MSG_DONE) {
2834 /* check if we can receive more */
2835 if (c_room(&s->req) <= global.tune.maxrewrite) {
2836 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
2837 goto out;
2838 }
2839 goto wait;
Christopher Fauleta3618372018-12-13 21:59:56 +01002840 }
Christopher Fauleta3618372018-12-13 21:59:56 +01002841 reql = co_getblk(si_oc(si), temp->area, s->txn->req.body_len,
2842 s->txn->req.eoh + 2);
2843 if (reql <= 0) {
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01002844 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
2845 goto out;
Christopher Fauleta3618372018-12-13 21:59:56 +01002846 }
2847 temp->data = reql;
William Lallemand74c24fb2016-11-21 17:18:36 +01002848 }
2849
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002850 first_param = temp->area;
Christopher Fauleta3618372018-12-13 21:59:56 +01002851 end_params = temp->area + temp->data;
William Lallemand74c24fb2016-11-21 17:18:36 +01002852 cur_param = next_param = end_params;
2853 *end_params = '\0';
2854
2855 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
2856
2857 /*
2858 * Parse the parameters in reverse order to only store the last value.
2859 * From the html form, the backend and the action are at the end.
2860 */
2861 while (cur_param > first_param) {
2862 char *value;
2863 int poffset, plen;
2864
2865 cur_param--;
2866
2867 if ((*cur_param == '&') || (cur_param == first_param)) {
2868 reprocess_servers:
2869 /* Parse the key */
2870 poffset = (cur_param != first_param ? 1 : 0);
2871 plen = next_param - cur_param + (cur_param == first_param ? 1 : 0);
2872 if ((plen > 0) && (plen <= sizeof(key))) {
2873 strncpy(key, cur_param + poffset, plen);
2874 key[plen - 1] = '\0';
2875 } else {
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01002876 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
William Lallemand74c24fb2016-11-21 17:18:36 +01002877 goto out;
2878 }
2879
2880 /* Parse the value */
2881 value = key;
2882 while (*value != '\0' && *value != '=') {
2883 value++;
2884 }
2885 if (*value == '=') {
2886 /* Ok, a value is found, we can mark the end of the key */
2887 *value++ = '\0';
2888 }
2889 if (url_decode(key) < 0 || url_decode(value) < 0)
2890 break;
2891
2892 /* Now we can check the key to see what to do */
2893 if (!px && (strcmp(key, "b") == 0)) {
2894 if ((px = proxy_be_by_name(value)) == NULL) {
2895 /* the backend name is unknown or ambiguous (duplicate names) */
2896 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
2897 goto out;
2898 }
2899 }
2900 else if (!action && (strcmp(key, "action") == 0)) {
2901 if (strcmp(value, "ready") == 0) {
2902 action = ST_ADM_ACTION_READY;
2903 }
2904 else if (strcmp(value, "drain") == 0) {
2905 action = ST_ADM_ACTION_DRAIN;
2906 }
2907 else if (strcmp(value, "maint") == 0) {
2908 action = ST_ADM_ACTION_MAINT;
2909 }
2910 else if (strcmp(value, "shutdown") == 0) {
2911 action = ST_ADM_ACTION_SHUTDOWN;
2912 }
2913 else if (strcmp(value, "dhlth") == 0) {
2914 action = ST_ADM_ACTION_DHLTH;
2915 }
2916 else if (strcmp(value, "ehlth") == 0) {
2917 action = ST_ADM_ACTION_EHLTH;
2918 }
2919 else if (strcmp(value, "hrunn") == 0) {
2920 action = ST_ADM_ACTION_HRUNN;
2921 }
2922 else if (strcmp(value, "hnolb") == 0) {
2923 action = ST_ADM_ACTION_HNOLB;
2924 }
2925 else if (strcmp(value, "hdown") == 0) {
2926 action = ST_ADM_ACTION_HDOWN;
2927 }
2928 else if (strcmp(value, "dagent") == 0) {
2929 action = ST_ADM_ACTION_DAGENT;
2930 }
2931 else if (strcmp(value, "eagent") == 0) {
2932 action = ST_ADM_ACTION_EAGENT;
2933 }
2934 else if (strcmp(value, "arunn") == 0) {
2935 action = ST_ADM_ACTION_ARUNN;
2936 }
2937 else if (strcmp(value, "adown") == 0) {
2938 action = ST_ADM_ACTION_ADOWN;
2939 }
2940 /* else these are the old supported methods */
2941 else if (strcmp(value, "disable") == 0) {
2942 action = ST_ADM_ACTION_DISABLE;
2943 }
2944 else if (strcmp(value, "enable") == 0) {
2945 action = ST_ADM_ACTION_ENABLE;
2946 }
2947 else if (strcmp(value, "stop") == 0) {
2948 action = ST_ADM_ACTION_STOP;
2949 }
2950 else if (strcmp(value, "start") == 0) {
2951 action = ST_ADM_ACTION_START;
2952 }
2953 else {
2954 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
2955 goto out;
2956 }
2957 }
2958 else if (strcmp(key, "s") == 0) {
2959 if (!(px && action)) {
2960 /*
2961 * Indicates that we'll need to reprocess the parameters
2962 * as soon as backend and action are known
2963 */
2964 if (!reprocess) {
2965 st_cur_param = cur_param;
2966 st_next_param = next_param;
2967 }
2968 reprocess = 1;
2969 }
2970 else if ((sv = findserver(px, value)) != NULL) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002971 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
William Lallemand74c24fb2016-11-21 17:18:36 +01002972 switch (action) {
2973 case ST_ADM_ACTION_DISABLE:
Emeric Brun52a91d32017-08-31 14:41:55 +02002974 if (!(sv->cur_admin & SRV_ADMF_FMAINT)) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002975 altered_servers++;
2976 total_servers++;
2977 srv_set_admin_flag(sv, SRV_ADMF_FMAINT, "'disable' on stats page");
2978 }
2979 break;
2980 case ST_ADM_ACTION_ENABLE:
Emeric Brun52a91d32017-08-31 14:41:55 +02002981 if (sv->cur_admin & SRV_ADMF_FMAINT) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002982 altered_servers++;
2983 total_servers++;
2984 srv_clr_admin_flag(sv, SRV_ADMF_FMAINT);
2985 }
2986 break;
2987 case ST_ADM_ACTION_STOP:
Emeric Brun52a91d32017-08-31 14:41:55 +02002988 if (!(sv->cur_admin & SRV_ADMF_FDRAIN)) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002989 srv_set_admin_flag(sv, SRV_ADMF_FDRAIN, "'stop' on stats page");
2990 altered_servers++;
2991 total_servers++;
2992 }
2993 break;
2994 case ST_ADM_ACTION_START:
Emeric Brun52a91d32017-08-31 14:41:55 +02002995 if (sv->cur_admin & SRV_ADMF_FDRAIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002996 srv_clr_admin_flag(sv, SRV_ADMF_FDRAIN);
2997 altered_servers++;
2998 total_servers++;
2999 }
3000 break;
3001 case ST_ADM_ACTION_DHLTH:
3002 if (sv->check.state & CHK_ST_CONFIGURED) {
3003 sv->check.state &= ~CHK_ST_ENABLED;
3004 altered_servers++;
3005 total_servers++;
3006 }
3007 break;
3008 case ST_ADM_ACTION_EHLTH:
3009 if (sv->check.state & CHK_ST_CONFIGURED) {
3010 sv->check.state |= CHK_ST_ENABLED;
3011 altered_servers++;
3012 total_servers++;
3013 }
3014 break;
3015 case ST_ADM_ACTION_HRUNN:
3016 if (!(sv->track)) {
3017 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02003018 srv_set_running(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003019 altered_servers++;
3020 total_servers++;
3021 }
3022 break;
3023 case ST_ADM_ACTION_HNOLB:
3024 if (!(sv->track)) {
3025 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02003026 srv_set_stopping(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003027 altered_servers++;
3028 total_servers++;
3029 }
3030 break;
3031 case ST_ADM_ACTION_HDOWN:
3032 if (!(sv->track)) {
3033 sv->check.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02003034 srv_set_stopped(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003035 altered_servers++;
3036 total_servers++;
3037 }
3038 break;
3039 case ST_ADM_ACTION_DAGENT:
3040 if (sv->agent.state & CHK_ST_CONFIGURED) {
3041 sv->agent.state &= ~CHK_ST_ENABLED;
3042 altered_servers++;
3043 total_servers++;
3044 }
3045 break;
3046 case ST_ADM_ACTION_EAGENT:
3047 if (sv->agent.state & CHK_ST_CONFIGURED) {
3048 sv->agent.state |= CHK_ST_ENABLED;
3049 altered_servers++;
3050 total_servers++;
3051 }
3052 break;
3053 case ST_ADM_ACTION_ARUNN:
3054 if (sv->agent.state & CHK_ST_ENABLED) {
3055 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02003056 srv_set_running(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003057 altered_servers++;
3058 total_servers++;
3059 }
3060 break;
3061 case ST_ADM_ACTION_ADOWN:
3062 if (sv->agent.state & CHK_ST_ENABLED) {
3063 sv->agent.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02003064 srv_set_stopped(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003065 altered_servers++;
3066 total_servers++;
3067 }
3068 break;
3069 case ST_ADM_ACTION_READY:
3070 srv_adm_set_ready(sv);
3071 altered_servers++;
3072 total_servers++;
3073 break;
3074 case ST_ADM_ACTION_DRAIN:
3075 srv_adm_set_drain(sv);
3076 altered_servers++;
3077 total_servers++;
3078 break;
3079 case ST_ADM_ACTION_MAINT:
3080 srv_adm_set_maint(sv);
3081 altered_servers++;
3082 total_servers++;
3083 break;
3084 case ST_ADM_ACTION_SHUTDOWN:
3085 if (px->state != PR_STSTOPPED) {
3086 struct stream *sess, *sess_bck;
3087
3088 list_for_each_entry_safe(sess, sess_bck, &sv->actconns, by_srv)
3089 if (sess->srv_conn == sv)
3090 stream_shutdown(sess, SF_ERR_KILLED);
3091
3092 altered_servers++;
3093 total_servers++;
3094 }
3095 break;
3096 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003097 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Lallemand74c24fb2016-11-21 17:18:36 +01003098 } else {
3099 /* the server name is unknown or ambiguous (duplicate names) */
3100 total_servers++;
3101 }
3102 }
3103 if (reprocess && px && action) {
3104 /* Now, we know the backend and the action chosen by the user.
3105 * We can safely restart from the first server parameter
3106 * to reprocess them
3107 */
3108 cur_param = st_cur_param;
3109 next_param = st_next_param;
3110 reprocess = 0;
3111 goto reprocess_servers;
3112 }
3113
3114 next_param = cur_param;
3115 }
3116 }
3117
3118 if (total_servers == 0) {
3119 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
3120 }
3121 else if (altered_servers == 0) {
3122 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
3123 }
3124 else if (altered_servers == total_servers) {
3125 appctx->ctx.stats.st_code = STAT_STATUS_DONE;
3126 }
3127 else {
3128 appctx->ctx.stats.st_code = STAT_STATUS_PART;
3129 }
3130 out:
3131 return 1;
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01003132 wait:
3133 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
3134 return 0;
Christopher Fauletef779222018-10-31 08:47:01 +01003135}
3136
3137
3138static int stats_send_htx_headers(struct stream_interface *si, struct htx *htx)
3139{
3140 struct stream *s = si_strm(si);
3141 struct uri_auth *uri = s->be->uri_auth;
3142 struct appctx *appctx = __objt_appctx(si->end);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003143 struct htx_sl *sl;
3144 unsigned int flags;
Christopher Fauletef779222018-10-31 08:47:01 +01003145
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003146 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);
3147 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist("200"), ist("OK"));
3148 if (!sl)
Christopher Fauletef779222018-10-31 08:47:01 +01003149 goto full;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003150 sl->info.res.status = 200;
Christopher Fauletef779222018-10-31 08:47:01 +01003151
Christopher Fauletb829f4c2019-03-29 16:13:55 +01003152 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")))
Christopher Fauletef779222018-10-31 08:47:01 +01003153 goto full;
3154 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
3155 if (!htx_add_header(htx, ist("Content-Type"), ist("text/html")))
3156 goto full;
3157 }
3158 else {
3159 if (!htx_add_header(htx, ist("Content-Type"), ist("text/plain")))
3160 goto full;
3161 }
3162
3163 if (uri->refresh > 0 && !(appctx->ctx.stats.flags & STAT_NO_REFRESH)) {
3164 const char *refresh = U2A(uri->refresh);
3165 if (!htx_add_header(htx, ist("Refresh"), ist2(refresh, strlen(refresh))))
3166 goto full;
3167 }
3168
3169 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
3170 if (!htx_add_header(htx, ist("Transfer-Encoding"), ist("chunked")))
3171 goto full;
3172 }
3173
3174 if (!htx_add_endof(htx, HTX_BLK_EOH))
3175 goto full;
3176
Christopher Faulet1e2d6362019-02-27 16:28:48 +01003177 channel_add_input(&s->res, htx->data);
Christopher Fauletef779222018-10-31 08:47:01 +01003178 return 1;
3179
3180 full:
3181 htx_reset(htx);
3182 si_rx_room_blk(si);
3183 return 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01003184}
3185
Christopher Fauletef779222018-10-31 08:47:01 +01003186
3187static int stats_send_htx_redirect(struct stream_interface *si, struct htx *htx)
3188{
3189 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
3190 struct stream *s = si_strm(si);
3191 struct uri_auth *uri = s->be->uri_auth;
3192 struct appctx *appctx = __objt_appctx(si->end);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003193 struct htx_sl *sl;
3194 unsigned int flags;
Christopher Fauletef779222018-10-31 08:47:01 +01003195
3196 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
3197 scope_txt[0] = 0;
3198 if (appctx->ctx.stats.scope_len) {
Christopher Fauleted7a0662019-01-14 11:07:34 +01003199 const char *scope_ptr = stats_scope_ptr(appctx, si);
3200
Christopher Fauletef779222018-10-31 08:47:01 +01003201 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Christopher Fauleted7a0662019-01-14 11:07:34 +01003202 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), scope_ptr, appctx->ctx.stats.scope_len);
Christopher Fauletef779222018-10-31 08:47:01 +01003203 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
3204 }
3205
3206 /* We don't want to land on the posted stats page because a refresh will
3207 * repost the data. We don't want this to happen on accident so we redirect
3208 * the browse to the stats page with a GET.
3209 */
3210 chunk_printf(&trash, "%s;st=%s%s%s%s",
3211 uri->uri_prefix,
3212 ((appctx->ctx.stats.st_code > STAT_STATUS_INIT) &&
3213 (appctx->ctx.stats.st_code < STAT_STATUS_SIZE) &&
3214 stat_status_codes[appctx->ctx.stats.st_code]) ?
3215 stat_status_codes[appctx->ctx.stats.st_code] :
3216 stat_status_codes[STAT_STATUS_UNKN],
3217 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3218 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3219 scope_txt);
3220
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003221 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_CHNK);
3222 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist("303"), ist("See Other"));
3223 if (!sl)
Christopher Fauletef779222018-10-31 08:47:01 +01003224 goto full;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003225 sl->info.res.status = 303;
Christopher Fauletef779222018-10-31 08:47:01 +01003226
3227 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")) ||
Christopher Fauletef779222018-10-31 08:47:01 +01003228 !htx_add_header(htx, ist("Content-Type"), ist("text/plain")) ||
3229 !htx_add_header(htx, ist("Content-Length"), ist("0")) ||
3230 !htx_add_header(htx, ist("Location"), ist2(trash.area, trash.data)))
3231 goto full;
3232
3233 if (!htx_add_endof(htx, HTX_BLK_EOH))
3234 goto full;
3235
Christopher Faulet1e2d6362019-02-27 16:28:48 +01003236 channel_add_input(&s->res, htx->data);
Christopher Fauletef779222018-10-31 08:47:01 +01003237 return 1;
3238
3239full:
3240 htx_reset(htx);
3241 si_rx_room_blk(si);
3242 return 0;
3243}
William Lallemand74c24fb2016-11-21 17:18:36 +01003244
3245static int stats_send_http_headers(struct stream_interface *si)
3246{
3247 struct stream *s = si_strm(si);
3248 struct uri_auth *uri = s->be->uri_auth;
Willy Tarreau21ff2c42018-09-20 11:01:01 +02003249 struct appctx *appctx = __objt_appctx(si->end);
William Lallemand74c24fb2016-11-21 17:18:36 +01003250
3251 chunk_printf(&trash,
3252 "HTTP/1.1 200 OK\r\n"
3253 "Cache-Control: no-cache\r\n"
3254 "Connection: close\r\n"
3255 "Content-Type: %s\r\n",
3256 (appctx->ctx.stats.flags & STAT_FMT_HTML) ? "text/html" : "text/plain");
3257
3258 if (uri->refresh > 0 && !(appctx->ctx.stats.flags & STAT_NO_REFRESH))
3259 chunk_appendf(&trash, "Refresh: %d\r\n",
3260 uri->refresh);
3261
3262 /* we don't send the CRLF in chunked mode, it will be sent with the first chunk's size */
3263
3264 if (appctx->ctx.stats.flags & STAT_CHUNKED)
3265 chunk_appendf(&trash, "Transfer-Encoding: chunked\r\n");
3266 else
3267 chunk_appendf(&trash, "\r\n");
3268
Willy Tarreau06d80a92017-10-19 14:32:15 +02003269 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01003270 si_rx_room_blk(si);
William Lallemand74c24fb2016-11-21 17:18:36 +01003271 return 0;
3272 }
3273
3274 return 1;
3275}
3276
3277static int stats_send_http_redirect(struct stream_interface *si)
3278{
3279 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
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 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
3285 scope_txt[0] = 0;
3286 if (appctx->ctx.stats.scope_len) {
3287 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau89faf5d2018-06-07 18:16:48 +02003288 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 +01003289 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
3290 }
3291
3292 /* We don't want to land on the posted stats page because a refresh will
3293 * repost the data. We don't want this to happen on accident so we redirect
3294 * the browse to the stats page with a GET.
3295 */
3296 chunk_printf(&trash,
3297 "HTTP/1.1 303 See Other\r\n"
3298 "Cache-Control: no-cache\r\n"
3299 "Content-Type: text/plain\r\n"
3300 "Connection: close\r\n"
3301 "Location: %s;st=%s%s%s%s\r\n"
3302 "Content-length: 0\r\n"
3303 "\r\n",
3304 uri->uri_prefix,
3305 ((appctx->ctx.stats.st_code > STAT_STATUS_INIT) &&
3306 (appctx->ctx.stats.st_code < STAT_STATUS_SIZE) &&
3307 stat_status_codes[appctx->ctx.stats.st_code]) ?
3308 stat_status_codes[appctx->ctx.stats.st_code] :
3309 stat_status_codes[STAT_STATUS_UNKN],
3310 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3311 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3312 scope_txt);
3313
Willy Tarreau06d80a92017-10-19 14:32:15 +02003314 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01003315 si_rx_room_blk(si);
William Lallemand74c24fb2016-11-21 17:18:36 +01003316 return 0;
3317 }
3318
3319 return 1;
3320}
3321
Simon Horman05ee2132017-01-04 09:37:25 +01003322
William Lallemand74c24fb2016-11-21 17:18:36 +01003323/* This I/O handler runs as an applet embedded in a stream interface. It is
3324 * used to send HTTP stats over a TCP socket. The mechanism is very simple.
3325 * appctx->st0 contains the operation in progress (dump, done). The handler
3326 * automatically unregisters itself once transfer is complete.
3327 */
Christopher Fauletef779222018-10-31 08:47:01 +01003328static void htx_stats_io_handler(struct appctx *appctx)
3329{
3330 struct stream_interface *si = appctx->owner;
3331 struct stream *s = si_strm(si);
3332 struct channel *req = si_oc(si);
3333 struct channel *res = si_ic(si);
3334 struct htx *req_htx, *res_htx;
3335
3336 res_htx = htx_from_buf(&res->buf);
3337
3338 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
3339 goto out;
3340
3341 /* Check if the input buffer is avalaible. */
3342 if (!b_size(&res->buf)) {
3343 si_rx_room_blk(si);
3344 goto out;
3345 }
3346
3347 /* check that the output is not closed */
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003348 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_SHUTR))
3349 appctx->st0 = STAT_HTTP_END;
Christopher Fauletef779222018-10-31 08:47:01 +01003350
3351 /* all states are processed in sequence */
3352 if (appctx->st0 == STAT_HTTP_HEAD) {
3353 if (stats_send_htx_headers(si, res_htx)) {
3354 if (s->txn->meth == HTTP_METH_HEAD)
3355 appctx->st0 = STAT_HTTP_DONE;
3356 else
3357 appctx->st0 = STAT_HTTP_DUMP;
3358 }
3359 }
3360
3361 if (appctx->st0 == STAT_HTTP_DUMP) {
3362 if (stats_dump_stat_to_buffer(si, res_htx, s->be->uri_auth))
3363 appctx->st0 = STAT_HTTP_DONE;
3364 }
3365
3366 if (appctx->st0 == STAT_HTTP_POST) {
3367 if (stats_process_http_post(si))
3368 appctx->st0 = STAT_HTTP_LAST;
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003369 else if (req->flags & CF_SHUTR)
Christopher Fauletef779222018-10-31 08:47:01 +01003370 appctx->st0 = STAT_HTTP_DONE;
3371 }
3372
3373 if (appctx->st0 == STAT_HTTP_LAST) {
3374 if (stats_send_htx_redirect(si, res_htx))
3375 appctx->st0 = STAT_HTTP_DONE;
3376 }
3377
3378 if (appctx->st0 == STAT_HTTP_DONE) {
Christopher Faulet54b5e212019-06-04 10:08:28 +02003379 /* Don't add TLR because mux-h1 will take care of it */
Christopher Fauletef779222018-10-31 08:47:01 +01003380 if (!htx_add_endof(res_htx, HTX_BLK_EOM)) {
3381 si_rx_room_blk(si);
3382 goto out;
3383 }
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003384 channel_add_input(&s->res, 1);
3385 appctx->st0 = STAT_HTTP_END;
Christopher Fauletef779222018-10-31 08:47:01 +01003386 }
3387
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003388 if (appctx->st0 == STAT_HTTP_END) {
3389 if (!(res->flags & CF_SHUTR)) {
Christopher Fauletef779222018-10-31 08:47:01 +01003390 res->flags |= CF_READ_NULL;
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003391 si_shutr(si);
3392 }
3393
3394 /* eat the whole request */
3395 if (co_data(req)) {
3396 req_htx = htx_from_buf(&req->buf);
3397 co_htx_skip(req, req_htx, co_data(req));
3398 htx_to_buf(req_htx, &req->buf);
Christopher Fauletef779222018-10-31 08:47:01 +01003399 }
3400 }
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003401
Christopher Fauletef779222018-10-31 08:47:01 +01003402 out:
3403 /* we have left the request in the buffer for the case where we
3404 * process a POST, and this automatically re-enables activity on
3405 * read. It's better to indicate that we want to stop reading when
3406 * we're sending, so that we know there's at most one direction
3407 * deciding to wake the applet up. It saves it from looping when
3408 * emitting large blocks into small TCP windows.
3409 */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003410 htx_to_buf(res_htx, &res->buf);
3411 if (!channel_is_empty(res))
Christopher Fauletef779222018-10-31 08:47:01 +01003412 si_stop_get(si);
Christopher Fauletef779222018-10-31 08:47:01 +01003413}
3414
3415
3416/* This I/O handler runs as an applet embedded in a stream interface. It is
3417 * used to send HTTP stats over a TCP socket. The mechanism is very simple.
3418 * appctx->st0 contains the operation in progress (dump, done). The handler
3419 * automatically unregisters itself once transfer is complete.
3420 */
William Lallemand74c24fb2016-11-21 17:18:36 +01003421static void http_stats_io_handler(struct appctx *appctx)
3422{
3423 struct stream_interface *si = appctx->owner;
3424 struct stream *s = si_strm(si);
3425 struct channel *req = si_oc(si);
3426 struct channel *res = si_ic(si);
3427
Christopher Fauletef779222018-10-31 08:47:01 +01003428 if (IS_HTX_STRM(s))
3429 return htx_stats_io_handler(appctx);
3430
William Lallemand74c24fb2016-11-21 17:18:36 +01003431 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
3432 goto out;
3433
Joseph Herlant29023ec2018-11-15 13:39:46 -08003434 /* Check if the input buffer is available. */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003435 if (res->buf.size == 0) {
Willy Tarreau4b962a42018-11-15 11:03:21 +01003436 /* already subscribed, we'll be called later once the buffer is
3437 * available.
3438 */
Christopher Fauleta73e59b2016-12-09 17:30:18 +01003439 goto out;
3440 }
3441
William Lallemand74c24fb2016-11-21 17:18:36 +01003442 /* check that the output is not closed */
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003443 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_SHUTR))
3444 appctx->st0 = STAT_HTTP_END;
William Lallemand74c24fb2016-11-21 17:18:36 +01003445
3446 /* all states are processed in sequence */
3447 if (appctx->st0 == STAT_HTTP_HEAD) {
3448 if (stats_send_http_headers(si)) {
3449 if (s->txn->meth == HTTP_METH_HEAD)
3450 appctx->st0 = STAT_HTTP_DONE;
3451 else
3452 appctx->st0 = STAT_HTTP_DUMP;
3453 }
3454 }
3455
3456 if (appctx->st0 == STAT_HTTP_DUMP) {
Willy Tarreau97f538b2018-06-15 19:41:31 +02003457 unsigned int prev_len = ci_data(si_ic(si));
William Lallemand74c24fb2016-11-21 17:18:36 +01003458 unsigned int data_len;
3459 unsigned int last_len;
3460 unsigned int last_fwd = 0;
3461
3462 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
3463 /* One difficulty we're facing is that we must prevent
3464 * the input data from being automatically forwarded to
3465 * the output area. For this, we temporarily disable
3466 * forwarding on the channel.
3467 */
3468 last_fwd = si_ic(si)->to_forward;
3469 si_ic(si)->to_forward = 0;
3470 chunk_printf(&trash, "\r\n000000\r\n");
Willy Tarreau06d80a92017-10-19 14:32:15 +02003471 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01003472 si_rx_room_blk(si);
William Lallemand74c24fb2016-11-21 17:18:36 +01003473 si_ic(si)->to_forward = last_fwd;
3474 goto out;
3475 }
3476 }
3477
Willy Tarreau97f538b2018-06-15 19:41:31 +02003478 data_len = ci_data(si_ic(si));
Christopher Fauletef779222018-10-31 08:47:01 +01003479 if (stats_dump_stat_to_buffer(si, NULL, s->be->uri_auth))
William Lallemand74c24fb2016-11-21 17:18:36 +01003480 appctx->st0 = STAT_HTTP_DONE;
3481
Willy Tarreau97f538b2018-06-15 19:41:31 +02003482 last_len = ci_data(si_ic(si));
William Lallemand74c24fb2016-11-21 17:18:36 +01003483
3484 /* Now we must either adjust or remove the chunk size. This is
3485 * not easy because the chunk size might wrap at the end of the
3486 * buffer, so we pretend we have nothing in the buffer, we write
3487 * the size, then restore the buffer's contents. Note that we can
3488 * only do that because no forwarding is scheduled on the stats
3489 * applet.
3490 */
3491 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
3492 si_ic(si)->total -= (last_len - prev_len);
Willy Tarreau97f538b2018-06-15 19:41:31 +02003493 b_sub(si_ib(si), (last_len - prev_len));
William Lallemand74c24fb2016-11-21 17:18:36 +01003494
3495 if (last_len != data_len) {
3496 chunk_printf(&trash, "\r\n%06x\r\n", (last_len - data_len));
Willy Tarreau06d80a92017-10-19 14:32:15 +02003497 if (ci_putchk(si_ic(si), &trash) == -1)
Willy Tarreaudb398432018-11-15 11:08:52 +01003498 si_rx_room_blk(si);
William Lallemand74c24fb2016-11-21 17:18:36 +01003499
3500 si_ic(si)->total += (last_len - data_len);
Willy Tarreau97f538b2018-06-15 19:41:31 +02003501 b_add(si_ib(si), last_len - data_len);
William Lallemand74c24fb2016-11-21 17:18:36 +01003502 }
3503 /* now re-enable forwarding */
3504 channel_forward(si_ic(si), last_fwd);
3505 }
3506 }
3507
3508 if (appctx->st0 == STAT_HTTP_POST) {
3509 if (stats_process_http_post(si))
3510 appctx->st0 = STAT_HTTP_LAST;
3511 else if (si_oc(si)->flags & CF_SHUTR)
3512 appctx->st0 = STAT_HTTP_DONE;
3513 }
3514
3515 if (appctx->st0 == STAT_HTTP_LAST) {
3516 if (stats_send_http_redirect(si))
3517 appctx->st0 = STAT_HTTP_DONE;
3518 }
3519
3520 if (appctx->st0 == STAT_HTTP_DONE) {
3521 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
3522 chunk_printf(&trash, "\r\n0\r\n\r\n");
Willy Tarreau06d80a92017-10-19 14:32:15 +02003523 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01003524 si_rx_room_blk(si);
William Lallemand74c24fb2016-11-21 17:18:36 +01003525 goto out;
3526 }
3527 }
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003528 appctx->st0 = STAT_HTTP_END;
William Lallemand74c24fb2016-11-21 17:18:36 +01003529 }
3530
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003531 if (appctx->st0 == STAT_HTTP_END) {
3532 if (!(res->flags & CF_SHUTR)) {
William Lallemand74c24fb2016-11-21 17:18:36 +01003533 res->flags |= CF_READ_NULL;
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003534 si_shutr(si);
William Lallemand74c24fb2016-11-21 17:18:36 +01003535 }
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003536
3537 /* eat the whole request */
3538 if (co_data(req))
3539 co_skip(si_oc(si), co_data(si_oc(si)));
William Lallemand74c24fb2016-11-21 17:18:36 +01003540 }
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003541
William Lallemand74c24fb2016-11-21 17:18:36 +01003542 out:
Willy Tarreau055ba4f2018-07-24 17:05:54 +02003543 /* we have left the request in the buffer for the case where we
3544 * process a POST, and this automatically re-enables activity on
3545 * read. It's better to indicate that we want to stop reading when
3546 * we're sending, so that we know there's at most one direction
3547 * deciding to wake the applet up. It saves it from looping when
3548 * emitting large blocks into small TCP windows.
3549 */
3550 if (!channel_is_empty(res))
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01003551 si_stop_get(si);
William Lallemand74c24fb2016-11-21 17:18:36 +01003552}
3553
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003554/* Dump all fields from <info> into <out> using the "show info" format (name: value) */
Willy Tarreau83061a82018-07-13 11:56:34 +02003555static int stats_dump_info_fields(struct buffer *out,
3556 const struct field *info)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003557{
3558 int field;
3559
3560 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
3561 if (!field_format(info, field))
3562 continue;
3563
3564 if (!chunk_appendf(out, "%s: ", info_field_names[field]))
3565 return 0;
3566 if (!stats_emit_raw_data_field(out, &info[field]))
3567 return 0;
3568 if (!chunk_strcat(out, "\n"))
3569 return 0;
3570 }
3571 return 1;
3572}
3573
3574/* Dump all fields from <info> into <out> using the "show info typed" format */
Willy Tarreau83061a82018-07-13 11:56:34 +02003575static int stats_dump_typed_info_fields(struct buffer *out,
3576 const struct field *info)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003577{
3578 int field;
3579
3580 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
3581 if (!field_format(info, field))
3582 continue;
3583
3584 if (!chunk_appendf(out, "%d.%s.%u:", field, info_field_names[field], info[INF_PROCESS_NUM].u.u32))
3585 return 0;
3586 if (!stats_emit_field_tags(out, &info[field], ':'))
3587 return 0;
3588 if (!stats_emit_typed_data_field(out, &info[field]))
3589 return 0;
3590 if (!chunk_strcat(out, "\n"))
3591 return 0;
3592 }
3593 return 1;
3594}
3595
3596/* Fill <info> with HAProxy global info. <info> is preallocated
3597 * array of length <len>. The length of the aray must be
3598 * INF_TOTAL_FIELDS. If this length is less then this value, the
3599 * function returns 0, otherwise, it returns 1.
3600 */
3601int stats_fill_info(struct field *info, int len)
3602{
3603 unsigned int up = (now.tv_sec - start_date.tv_sec);
Willy Tarreau83061a82018-07-13 11:56:34 +02003604 struct buffer *out = get_trash_chunk();
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003605
3606#ifdef USE_OPENSSL
3607 int ssl_sess_rate = read_freq_ctr(&global.ssl_per_sec);
3608 int ssl_key_rate = read_freq_ctr(&global.ssl_fe_keys_per_sec);
3609 int ssl_reuse = 0;
3610
3611 if (ssl_key_rate < ssl_sess_rate) {
3612 /* count the ssl reuse ratio and avoid overflows in both directions */
3613 ssl_reuse = 100 - (100 * ssl_key_rate + (ssl_sess_rate - 1) / 2) / ssl_sess_rate;
3614 }
3615#endif
3616
3617 if (len < INF_TOTAL_FIELDS)
3618 return 0;
3619
3620 chunk_reset(out);
3621 memset(info, 0, sizeof(*info) * len);
3622
3623 info[INF_NAME] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, PRODUCT_NAME);
Willy Tarreau909b9d82019-01-04 18:20:32 +01003624 info[INF_VERSION] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, haproxy_version);
3625 info[INF_RELEASE_DATE] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, haproxy_date);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003626
Yves Lafon95317282018-02-26 11:10:37 +01003627 info[INF_NBTHREAD] = mkf_u32(FO_CONFIG|FS_SERVICE, global.nbthread);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003628 info[INF_NBPROC] = mkf_u32(FO_CONFIG|FS_SERVICE, global.nbproc);
3629 info[INF_PROCESS_NUM] = mkf_u32(FO_KEY, relative_pid);
3630 info[INF_PID] = mkf_u32(FO_STATUS, pid);
3631
3632 info[INF_UPTIME] = mkf_str(FN_DURATION, chunk_newstr(out));
3633 chunk_appendf(out, "%ud %uh%02um%02us", up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60));
3634
3635 info[INF_UPTIME_SEC] = mkf_u32(FN_DURATION, up);
3636 info[INF_MEMMAX_MB] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_memmax);
3637 info[INF_POOL_ALLOC_MB] = mkf_u32(0, (unsigned)(pool_total_allocated() / 1048576L));
3638 info[INF_POOL_USED_MB] = mkf_u32(0, (unsigned)(pool_total_used() / 1048576L));
3639 info[INF_POOL_FAILED] = mkf_u32(FN_COUNTER, pool_total_failures());
3640 info[INF_ULIMIT_N] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_nofile);
3641 info[INF_MAXSOCK] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxsock);
3642 info[INF_MAXCONN] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxconn);
3643 info[INF_HARD_MAXCONN] = mkf_u32(FO_CONFIG|FN_LIMIT, global.hardmaxconn);
3644 info[INF_CURR_CONN] = mkf_u32(0, actconn);
3645 info[INF_CUM_CONN] = mkf_u32(FN_COUNTER, totalconn);
3646 info[INF_CUM_REQ] = mkf_u32(FN_COUNTER, global.req_count);
3647#ifdef USE_OPENSSL
3648 info[INF_MAX_SSL_CONNS] = mkf_u32(FN_MAX, global.maxsslconn);
3649 info[INF_CURR_SSL_CONNS] = mkf_u32(0, sslconns);
3650 info[INF_CUM_SSL_CONNS] = mkf_u32(FN_COUNTER, totalsslconns);
3651#endif
3652 info[INF_MAXPIPES] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxpipes);
3653 info[INF_PIPES_USED] = mkf_u32(0, pipes_used);
3654 info[INF_PIPES_FREE] = mkf_u32(0, pipes_free);
3655 info[INF_CONN_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.conn_per_sec));
3656 info[INF_CONN_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.cps_lim);
3657 info[INF_MAX_CONN_RATE] = mkf_u32(FN_MAX, global.cps_max);
3658 info[INF_SESS_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.sess_per_sec));
3659 info[INF_SESS_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.sps_lim);
3660 info[INF_MAX_SESS_RATE] = mkf_u32(FN_RATE, global.sps_max);
3661
3662#ifdef USE_OPENSSL
3663 info[INF_SSL_RATE] = mkf_u32(FN_RATE, ssl_sess_rate);
3664 info[INF_SSL_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.ssl_lim);
3665 info[INF_MAX_SSL_RATE] = mkf_u32(FN_MAX, global.ssl_max);
3666 info[INF_SSL_FRONTEND_KEY_RATE] = mkf_u32(0, ssl_key_rate);
3667 info[INF_SSL_FRONTEND_MAX_KEY_RATE] = mkf_u32(FN_MAX, global.ssl_fe_keys_max);
3668 info[INF_SSL_FRONTEND_SESSION_REUSE_PCT] = mkf_u32(0, ssl_reuse);
3669 info[INF_SSL_BACKEND_KEY_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.ssl_be_keys_per_sec));
3670 info[INF_SSL_BACKEND_MAX_KEY_RATE] = mkf_u32(FN_MAX, global.ssl_be_keys_max);
3671 info[INF_SSL_CACHE_LOOKUPS] = mkf_u32(FN_COUNTER, global.shctx_lookups);
3672 info[INF_SSL_CACHE_MISSES] = mkf_u32(FN_COUNTER, global.shctx_misses);
3673#endif
3674 info[INF_COMPRESS_BPS_IN] = mkf_u32(FN_RATE, read_freq_ctr(&global.comp_bps_in));
3675 info[INF_COMPRESS_BPS_OUT] = mkf_u32(FN_RATE, read_freq_ctr(&global.comp_bps_out));
3676 info[INF_COMPRESS_BPS_RATE_LIM] = mkf_u32(FO_CONFIG|FN_LIMIT, global.comp_rate_lim);
3677#ifdef USE_ZLIB
3678 info[INF_ZLIB_MEM_USAGE] = mkf_u32(0, zlib_used_memory);
3679 info[INF_MAX_ZLIB_MEM_USAGE] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxzlibmem);
3680#endif
3681 info[INF_TASKS] = mkf_u32(0, nb_tasks_cur);
Christopher Faulet34c5cc92016-12-06 09:15:30 +01003682 info[INF_RUN_QUEUE] = mkf_u32(0, tasks_run_queue_cur);
Willy Tarreau81036f22019-05-20 19:24:50 +02003683 info[INF_IDLE_PCT] = mkf_u32(FN_AVG, ti->idle_pct);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003684 info[INF_NODE] = mkf_str(FO_CONFIG|FN_OUTPUT|FS_SERVICE, global.node);
3685 if (global.desc)
3686 info[INF_DESCRIPTION] = mkf_str(FO_CONFIG|FN_OUTPUT|FS_SERVICE, global.desc);
Willy Tarreau00098ea2018-11-05 14:38:13 +01003687 info[INF_STOPPING] = mkf_u32(0, stopping);
3688 info[INF_JOBS] = mkf_u32(0, jobs);
William Lallemanda7199262018-11-16 16:57:20 +01003689 info[INF_UNSTOPPABLE_JOBS] = mkf_u32(0, unstoppable_jobs);
Willy Tarreau00098ea2018-11-05 14:38:13 +01003690 info[INF_LISTENERS] = mkf_u32(0, listeners);
Willy Tarreau199ad242018-11-05 16:31:22 +01003691 info[INF_ACTIVE_PEERS] = mkf_u32(0, active_peers);
Willy Tarreau2d372c22018-11-05 17:12:27 +01003692 info[INF_CONNECTED_PEERS] = mkf_u32(0, connected_peers);
Willy Tarreau13ef7732018-11-12 07:25:28 +01003693 info[INF_DROPPED_LOGS] = mkf_u32(0, dropped_logs);
Willy Tarreaubeb859a2018-11-22 18:07:59 +01003694 info[INF_BUSY_POLLING] = mkf_u32(0, !!(global.tune.options & GTUNE_BUSY_POLLING));
Baptiste Assmann333939c2019-01-21 08:34:50 +01003695 info[INF_FAILED_RESOLUTIONS] = mkf_u32(0, dns_failed_resolutions);
Willy Tarreau7cf0e452019-05-23 11:39:14 +02003696 info[INF_TOTAL_BYTES_OUT] = mkf_u64(0, global.out_bytes);
3697 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 +01003698
3699 return 1;
3700}
3701
3702/* This function dumps information onto the stream interface's read buffer.
3703 * It returns 0 as long as it does not complete, non-zero upon completion.
3704 * No state is used.
3705 */
3706static int stats_dump_info_to_buffer(struct stream_interface *si)
3707{
3708 struct appctx *appctx = __objt_appctx(si->end);
3709
3710 if (!stats_fill_info(info, INF_TOTAL_FIELDS))
3711 return 0;
3712
3713 chunk_reset(&trash);
3714
3715 if (appctx->ctx.stats.flags & STAT_FMT_TYPED)
3716 stats_dump_typed_info_fields(&trash, info);
Simon Horman05ee2132017-01-04 09:37:25 +01003717 else if (appctx->ctx.stats.flags & STAT_FMT_JSON)
3718 stats_dump_json_info_fields(&trash, info);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003719 else
3720 stats_dump_info_fields(&trash, info);
3721
Willy Tarreau06d80a92017-10-19 14:32:15 +02003722 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01003723 si_rx_room_blk(si);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003724 return 0;
3725 }
3726
3727 return 1;
3728}
3729
Simon Horman6f6bb382017-01-04 09:37:26 +01003730/* This function dumps the schema onto the stream interface's read buffer.
3731 * It returns 0 as long as it does not complete, non-zero upon completion.
3732 * No state is used.
3733 *
3734 * Integer values bouned to the range [-(2**53)+1, (2**53)-1] as
3735 * per the recommendation for interoperable integers in section 6 of RFC 7159.
3736 */
Willy Tarreau83061a82018-07-13 11:56:34 +02003737static void stats_dump_json_schema(struct buffer *out)
Simon Horman6f6bb382017-01-04 09:37:26 +01003738{
3739
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003740 int old_len = out->data;
Simon Horman6f6bb382017-01-04 09:37:26 +01003741
3742 chunk_strcat(out,
3743 "{"
3744 "\"$schema\":\"http://json-schema.org/draft-04/schema#\","
3745 "\"oneOf\":["
3746 "{"
3747 "\"title\":\"Info\","
3748 "\"type\":\"array\","
3749 "\"items\":{"
3750 "\"properties\":{"
3751 "\"title\":\"InfoItem\","
3752 "\"type\":\"object\","
3753 "\"field\":{\"$ref\":\"#/definitions/field\"},"
3754 "\"processNum\":{\"$ref\":\"#/definitions/processNum\"},"
3755 "\"tags\":{\"$ref\":\"#/definitions/tags\"},"
3756 "\"value\":{\"$ref\":\"#/definitions/typedValue\"}"
3757 "},"
3758 "\"required\":[\"field\",\"processNum\",\"tags\","
3759 "\"value\"]"
3760 "}"
3761 "},"
3762 "{"
3763 "\"title\":\"Stat\","
3764 "\"type\":\"array\","
3765 "\"items\":{"
3766 "\"title\":\"InfoItem\","
3767 "\"type\":\"object\","
3768 "\"properties\":{"
3769 "\"objType\":{"
3770 "\"enum\":[\"Frontend\",\"Backend\",\"Listener\","
3771 "\"Server\",\"Unknown\"]"
3772 "},"
3773 "\"proxyId\":{"
3774 "\"type\":\"integer\","
3775 "\"minimum\":0"
3776 "},"
3777 "\"id\":{"
3778 "\"type\":\"integer\","
3779 "\"minimum\":0"
3780 "},"
3781 "\"field\":{\"$ref\":\"#/definitions/field\"},"
3782 "\"processNum\":{\"$ref\":\"#/definitions/processNum\"},"
3783 "\"tags\":{\"$ref\":\"#/definitions/tags\"},"
3784 "\"typedValue\":{\"$ref\":\"#/definitions/typedValue\"}"
3785 "},"
3786 "\"required\":[\"objType\",\"proxyId\",\"id\","
3787 "\"field\",\"processNum\",\"tags\","
3788 "\"value\"]"
3789 "}"
3790 "},"
3791 "{"
3792 "\"title\":\"Error\","
3793 "\"type\":\"object\","
3794 "\"properties\":{"
3795 "\"errorStr\":{"
3796 "\"type\":\"string\""
3797 "},"
3798 "\"required\":[\"errorStr\"]"
3799 "}"
3800 "}"
3801 "],"
3802 "\"definitions\":{"
3803 "\"field\":{"
3804 "\"type\":\"object\","
3805 "\"pos\":{"
3806 "\"type\":\"integer\","
3807 "\"minimum\":0"
3808 "},"
3809 "\"name\":{"
3810 "\"type\":\"string\""
3811 "},"
3812 "\"required\":[\"pos\",\"name\"]"
3813 "},"
3814 "\"processNum\":{"
3815 "\"type\":\"integer\","
3816 "\"minimum\":1"
3817 "},"
3818 "\"tags\":{"
3819 "\"type\":\"object\","
3820 "\"origin\":{"
3821 "\"type\":\"string\","
3822 "\"enum\":[\"Metric\",\"Status\",\"Key\","
3823 "\"Config\",\"Product\",\"Unknown\"]"
3824 "},"
3825 "\"nature\":{"
3826 "\"type\":\"string\","
3827 "\"enum\":[\"Gauge\",\"Limit\",\"Min\",\"Max\","
3828 "\"Rate\",\"Counter\",\"Duration\","
3829 "\"Age\",\"Time\",\"Name\",\"Output\","
3830 "\"Avg\", \"Unknown\"]"
3831 "},"
3832 "\"scope\":{"
3833 "\"type\":\"string\","
3834 "\"enum\":[\"Cluster\",\"Process\",\"Service\","
3835 "\"System\",\"Unknown\"]"
3836 "},"
3837 "\"required\":[\"origin\",\"nature\",\"scope\"]"
3838 "},"
3839 "\"typedValue\":{"
3840 "\"type\":\"object\","
3841 "\"oneOf\":["
3842 "{\"$ref\":\"#/definitions/typedValue/definitions/s32Value\"},"
3843 "{\"$ref\":\"#/definitions/typedValue/definitions/s64Value\"},"
3844 "{\"$ref\":\"#/definitions/typedValue/definitions/u32Value\"},"
3845 "{\"$ref\":\"#/definitions/typedValue/definitions/u64Value\"},"
3846 "{\"$ref\":\"#/definitions/typedValue/definitions/strValue\"}"
3847 "],"
3848 "\"definitions\":{"
3849 "\"s32Value\":{"
3850 "\"properties\":{"
3851 "\"type\":{"
3852 "\"type\":\"string\","
3853 "\"enum\":[\"s32\"]"
3854 "},"
3855 "\"value\":{"
3856 "\"type\":\"integer\","
3857 "\"minimum\":-2147483648,"
3858 "\"maximum\":2147483647"
3859 "}"
3860 "},"
3861 "\"required\":[\"type\",\"value\"]"
3862 "},"
3863 "\"s64Value\":{"
3864 "\"properties\":{"
3865 "\"type\":{"
3866 "\"type\":\"string\","
3867 "\"enum\":[\"s64\"]"
3868 "},"
3869 "\"value\":{"
3870 "\"type\":\"integer\","
3871 "\"minimum\":-9007199254740991,"
3872 "\"maximum\":9007199254740991"
3873 "}"
3874 "},"
3875 "\"required\":[\"type\",\"value\"]"
3876 "},"
3877 "\"u32Value\":{"
3878 "\"properties\":{"
3879 "\"type\":{"
3880 "\"type\":\"string\","
3881 "\"enum\":[\"u32\"]"
3882 "},"
3883 "\"value\":{"
3884 "\"type\":\"integer\","
3885 "\"minimum\":0,"
3886 "\"maximum\":4294967295"
3887 "}"
3888 "},"
3889 "\"required\":[\"type\",\"value\"]"
3890 "},"
3891 "\"u64Value\":{"
3892 "\"properties\":{"
3893 "\"type\":{"
3894 "\"type\":\"string\","
3895 "\"enum\":[\"u64\"]"
3896 "},"
3897 "\"value\":{"
3898 "\"type\":\"integer\","
3899 "\"minimum\":0,"
3900 "\"maximum\":9007199254740991"
3901 "}"
3902 "},"
3903 "\"required\":[\"type\",\"value\"]"
3904 "},"
3905 "\"strValue\":{"
3906 "\"properties\":{"
3907 "\"type\":{"
3908 "\"type\":\"string\","
3909 "\"enum\":[\"str\"]"
3910 "},"
3911 "\"value\":{\"type\":\"string\"}"
3912 "},"
3913 "\"required\":[\"type\",\"value\"]"
3914 "},"
3915 "\"unknownValue\":{"
3916 "\"properties\":{"
3917 "\"type\":{"
3918 "\"type\":\"integer\","
3919 "\"minimum\":0"
3920 "},"
3921 "\"value\":{"
3922 "\"type\":\"string\","
3923 "\"enum\":[\"unknown\"]"
3924 "}"
3925 "},"
3926 "\"required\":[\"type\",\"value\"]"
3927 "}"
3928 "}"
3929 "}"
3930 "}"
3931 "}");
3932
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003933 if (old_len == out->data) {
Simon Horman6f6bb382017-01-04 09:37:26 +01003934 chunk_reset(out);
3935 chunk_appendf(out,
3936 "{\"errorStr\":\"output buffer too short\"}");
3937 }
3938}
3939
3940/* This function dumps the schema onto the stream interface's read buffer.
3941 * It returns 0 as long as it does not complete, non-zero upon completion.
3942 * No state is used.
3943 */
3944static int stats_dump_json_schema_to_buffer(struct stream_interface *si)
3945{
3946 chunk_reset(&trash);
3947
3948 stats_dump_json_schema(&trash);
3949
Willy Tarreau06d80a92017-10-19 14:32:15 +02003950 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01003951 si_rx_room_blk(si);
Simon Horman6f6bb382017-01-04 09:37:26 +01003952 return 0;
3953 }
3954
3955 return 1;
3956}
3957
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02003958static int cli_parse_clear_counters(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau89d467c2016-11-23 11:02:40 +01003959{
3960 struct proxy *px;
3961 struct server *sv;
3962 struct listener *li;
3963 int clrall = 0;
3964
3965 if (strcmp(args[2], "all") == 0)
3966 clrall = 1;
3967
3968 /* check permissions */
3969 if (!cli_has_level(appctx, ACCESS_LVL_OPER) ||
3970 (clrall && !cli_has_level(appctx, ACCESS_LVL_ADMIN)))
3971 return 1;
3972
Olivier Houchardfbc74e82017-11-24 16:54:05 +01003973 for (px = proxies_list; px; px = px->next) {
Willy Tarreau89d467c2016-11-23 11:02:40 +01003974 if (clrall) {
3975 memset(&px->be_counters, 0, sizeof(px->be_counters));
3976 memset(&px->fe_counters, 0, sizeof(px->fe_counters));
3977 }
3978 else {
3979 px->be_counters.conn_max = 0;
3980 px->be_counters.p.http.rps_max = 0;
3981 px->be_counters.sps_max = 0;
3982 px->be_counters.cps_max = 0;
3983 px->be_counters.nbpend_max = 0;
3984
3985 px->fe_counters.conn_max = 0;
3986 px->fe_counters.p.http.rps_max = 0;
3987 px->fe_counters.sps_max = 0;
3988 px->fe_counters.cps_max = 0;
Willy Tarreau89d467c2016-11-23 11:02:40 +01003989 }
3990
3991 for (sv = px->srv; sv; sv = sv->next)
3992 if (clrall)
3993 memset(&sv->counters, 0, sizeof(sv->counters));
3994 else {
3995 sv->counters.cur_sess_max = 0;
3996 sv->counters.nbpend_max = 0;
3997 sv->counters.sps_max = 0;
3998 }
3999
4000 list_for_each_entry(li, &px->conf.listeners, by_fe)
4001 if (li->counters) {
4002 if (clrall)
4003 memset(li->counters, 0, sizeof(*li->counters));
4004 else
4005 li->counters->conn_max = 0;
4006 }
4007 }
4008
4009 global.cps_max = 0;
4010 global.sps_max = 0;
Olivier Houchard00bc3cb2017-10-17 19:23:25 +02004011 global.ssl_max = 0;
4012 global.ssl_fe_keys_max = 0;
4013 global.ssl_be_keys_max = 0;
Willy Tarreaud80cb4e2018-01-20 19:30:13 +01004014
4015 memset(activity, 0, sizeof(activity));
Willy Tarreau89d467c2016-11-23 11:02:40 +01004016 return 1;
4017}
4018
4019
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004020static int cli_parse_show_info(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004021{
Willy Tarreaud25fc792016-12-16 12:33:47 +01004022 appctx->ctx.stats.scope_str = 0;
4023 appctx->ctx.stats.scope_len = 0;
4024 appctx->ctx.stats.flags = 0;
4025
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004026 if (strcmp(args[2], "typed") == 0)
4027 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
Simon Horman05ee2132017-01-04 09:37:25 +01004028 else if (strcmp(args[2], "json") == 0)
4029 appctx->ctx.stats.flags |= STAT_FMT_JSON;
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004030 return 0;
4031}
4032
4033
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004034static int cli_parse_show_stat(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau2b812e22016-11-22 16:18:05 +01004035{
Willy Tarreaud25fc792016-12-16 12:33:47 +01004036 appctx->ctx.stats.scope_str = 0;
4037 appctx->ctx.stats.scope_len = 0;
4038 appctx->ctx.stats.flags = 0;
4039
Willy Tarreau2b812e22016-11-22 16:18:05 +01004040 if (*args[2] && *args[3] && *args[4]) {
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01004041 struct proxy *px;
4042
4043 px = proxy_find_by_name(args[2], 0, 0);
4044 if (px)
4045 appctx->ctx.stats.iid = px->uuid;
4046 else
4047 appctx->ctx.stats.iid = atoi(args[2]);
4048
4049 if (!appctx->ctx.stats.iid) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004050 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01004051 appctx->ctx.cli.msg = "No such proxy.\n";
4052 appctx->st0 = CLI_ST_PRINT;
4053 return 1;
4054 }
4055
Willy Tarreau2b812e22016-11-22 16:18:05 +01004056 appctx->ctx.stats.flags |= STAT_BOUND;
Willy Tarreau2b812e22016-11-22 16:18:05 +01004057 appctx->ctx.stats.type = atoi(args[3]);
4058 appctx->ctx.stats.sid = atoi(args[4]);
4059 if (strcmp(args[5], "typed") == 0)
4060 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
Simon Horman05ee2132017-01-04 09:37:25 +01004061 else if (strcmp(args[5], "json") == 0)
4062 appctx->ctx.stats.flags |= STAT_FMT_JSON;
Willy Tarreau2b812e22016-11-22 16:18:05 +01004063 }
4064 else if (strcmp(args[2], "typed") == 0)
4065 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
Simon Horman05ee2132017-01-04 09:37:25 +01004066 else if (strcmp(args[2], "json") == 0)
4067 appctx->ctx.stats.flags |= STAT_FMT_JSON;
Willy Tarreau2b812e22016-11-22 16:18:05 +01004068
Willy Tarreau2b812e22016-11-22 16:18:05 +01004069 return 0;
4070}
4071
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004072static int cli_io_handler_dump_info(struct appctx *appctx)
4073{
4074 return stats_dump_info_to_buffer(appctx->owner);
4075}
4076
Willy Tarreau2b812e22016-11-22 16:18:05 +01004077/* This I/O handler runs as an applet embedded in a stream interface. It is
4078 * used to send raw stats over a socket.
4079 */
4080static int cli_io_handler_dump_stat(struct appctx *appctx)
4081{
Christopher Fauletef779222018-10-31 08:47:01 +01004082 return stats_dump_stat_to_buffer(appctx->owner, NULL, NULL);
Willy Tarreau2b812e22016-11-22 16:18:05 +01004083}
4084
Simon Horman6f6bb382017-01-04 09:37:26 +01004085static int cli_io_handler_dump_json_schema(struct appctx *appctx)
4086{
4087 return stats_dump_json_schema_to_buffer(appctx->owner);
4088}
4089
Willy Tarreau2b812e22016-11-22 16:18:05 +01004090/* register cli keywords */
4091static struct cli_kw_list cli_kws = {{ },{
Willy Tarreau89d467c2016-11-23 11:02:40 +01004092 { { "clear", "counters", NULL }, "clear counters : clear max statistics counters (add 'all' for all counters)", cli_parse_clear_counters, NULL, NULL },
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004093 { { "show", "info", NULL }, "show info : report information about the running process", cli_parse_show_info, cli_io_handler_dump_info, NULL },
Willy Tarreau2b812e22016-11-22 16:18:05 +01004094 { { "show", "stat", NULL }, "show stat : report counters for each proxy and server", cli_parse_show_stat, cli_io_handler_dump_stat, NULL },
Simon Horman6f6bb382017-01-04 09:37:26 +01004095 { { "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 +01004096 {{},}
4097}};
4098
Willy Tarreau0108d902018-11-25 19:14:37 +01004099INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
4100
William Lallemand74c24fb2016-11-21 17:18:36 +01004101struct applet http_stats_applet = {
4102 .obj_type = OBJ_TYPE_APPLET,
4103 .name = "<STATS>", /* used for logging */
4104 .fct = http_stats_io_handler,
4105 .release = NULL,
4106};
4107
William Lallemand74c24fb2016-11-21 17:18:36 +01004108/*
4109 * Local variables:
4110 * c-indent-level: 8
4111 * c-basic-offset: 8
4112 * End:
4113 */