blob: 965fe4299dad0a706716a8002f15a413d82649f4 [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>
William Lallemand9ed62032016-11-21 17:49:11 +010054#include <proto/stats.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010055#include <proto/fd.h>
56#include <proto/freq_ctr.h>
57#include <proto/frontend.h>
Christopher Fauleted7a0662019-01-14 11:07:34 +010058#include <proto/http_htx.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010059#include <proto/log.h>
60#include <proto/pattern.h>
61#include <proto/pipe.h>
62#include <proto/listener.h>
63#include <proto/map.h>
Christopher Fauletef779222018-10-31 08:47:01 +010064#include <proto/proto_http.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010065#include <proto/proxy.h>
66#include <proto/sample.h>
67#include <proto/session.h>
68#include <proto/stream.h>
69#include <proto/server.h>
70#include <proto/raw_sock.h>
71#include <proto/stream_interface.h>
72#include <proto/task.h>
73
74#ifdef USE_OPENSSL
75#include <proto/ssl_sock.h>
76#include <types/ssl_sock.h>
77#endif
78
79
Christopher Faulet2b9b6782019-02-27 16:42:58 +010080/* status codes available for the stats admin page (strictly 4 chars length) */
81const char *stat_status_codes[STAT_STATUS_SIZE] = {
82 [STAT_STATUS_DENY] = "DENY",
83 [STAT_STATUS_DONE] = "DONE",
84 [STAT_STATUS_ERRP] = "ERRP",
85 [STAT_STATUS_EXCD] = "EXCD",
86 [STAT_STATUS_NONE] = "NONE",
87 [STAT_STATUS_PART] = "PART",
88 [STAT_STATUS_UNKN] = "UNKN",
89 [STAT_STATUS_IVAL] = "IVAL",
90};
91
Willy Tarreau0baac8c2016-11-22 16:36:53 +010092/* These are the field names for each INF_* field position. Please pay attention
93 * to always use the exact same name except that the strings for new names must
94 * be lower case or CamelCase while the enum entries must be upper case.
95 */
96const char *info_field_names[INF_TOTAL_FIELDS] = {
97 [INF_NAME] = "Name",
98 [INF_VERSION] = "Version",
99 [INF_RELEASE_DATE] = "Release_date",
Yves Lafon95317282018-02-26 11:10:37 +0100100 [INF_NBTHREAD] = "Nbthread",
Willy Tarreau0baac8c2016-11-22 16:36:53 +0100101 [INF_NBPROC] = "Nbproc",
102 [INF_PROCESS_NUM] = "Process_num",
103 [INF_PID] = "Pid",
104 [INF_UPTIME] = "Uptime",
105 [INF_UPTIME_SEC] = "Uptime_sec",
106 [INF_MEMMAX_MB] = "Memmax_MB",
107 [INF_POOL_ALLOC_MB] = "PoolAlloc_MB",
108 [INF_POOL_USED_MB] = "PoolUsed_MB",
109 [INF_POOL_FAILED] = "PoolFailed",
110 [INF_ULIMIT_N] = "Ulimit-n",
111 [INF_MAXSOCK] = "Maxsock",
112 [INF_MAXCONN] = "Maxconn",
113 [INF_HARD_MAXCONN] = "Hard_maxconn",
114 [INF_CURR_CONN] = "CurrConns",
115 [INF_CUM_CONN] = "CumConns",
116 [INF_CUM_REQ] = "CumReq",
117 [INF_MAX_SSL_CONNS] = "MaxSslConns",
118 [INF_CURR_SSL_CONNS] = "CurrSslConns",
119 [INF_CUM_SSL_CONNS] = "CumSslConns",
120 [INF_MAXPIPES] = "Maxpipes",
121 [INF_PIPES_USED] = "PipesUsed",
122 [INF_PIPES_FREE] = "PipesFree",
123 [INF_CONN_RATE] = "ConnRate",
124 [INF_CONN_RATE_LIMIT] = "ConnRateLimit",
125 [INF_MAX_CONN_RATE] = "MaxConnRate",
126 [INF_SESS_RATE] = "SessRate",
127 [INF_SESS_RATE_LIMIT] = "SessRateLimit",
128 [INF_MAX_SESS_RATE] = "MaxSessRate",
129 [INF_SSL_RATE] = "SslRate",
130 [INF_SSL_RATE_LIMIT] = "SslRateLimit",
131 [INF_MAX_SSL_RATE] = "MaxSslRate",
132 [INF_SSL_FRONTEND_KEY_RATE] = "SslFrontendKeyRate",
133 [INF_SSL_FRONTEND_MAX_KEY_RATE] = "SslFrontendMaxKeyRate",
134 [INF_SSL_FRONTEND_SESSION_REUSE_PCT] = "SslFrontendSessionReuse_pct",
135 [INF_SSL_BACKEND_KEY_RATE] = "SslBackendKeyRate",
136 [INF_SSL_BACKEND_MAX_KEY_RATE] = "SslBackendMaxKeyRate",
137 [INF_SSL_CACHE_LOOKUPS] = "SslCacheLookups",
138 [INF_SSL_CACHE_MISSES] = "SslCacheMisses",
139 [INF_COMPRESS_BPS_IN] = "CompressBpsIn",
140 [INF_COMPRESS_BPS_OUT] = "CompressBpsOut",
141 [INF_COMPRESS_BPS_RATE_LIM] = "CompressBpsRateLim",
142 [INF_ZLIB_MEM_USAGE] = "ZlibMemUsage",
143 [INF_MAX_ZLIB_MEM_USAGE] = "MaxZlibMemUsage",
144 [INF_TASKS] = "Tasks",
145 [INF_RUN_QUEUE] = "Run_queue",
146 [INF_IDLE_PCT] = "Idle_pct",
147 [INF_NODE] = "node",
148 [INF_DESCRIPTION] = "description",
Willy Tarreau00098ea2018-11-05 14:38:13 +0100149 [INF_STOPPING] = "Stopping",
150 [INF_JOBS] = "Jobs",
William Lallemanda7199262018-11-16 16:57:20 +0100151 [INF_UNSTOPPABLE_JOBS] = "Unstoppable Jobs",
Willy Tarreau00098ea2018-11-05 14:38:13 +0100152 [INF_LISTENERS] = "Listeners",
Willy Tarreau199ad242018-11-05 16:31:22 +0100153 [INF_ACTIVE_PEERS] = "ActivePeers",
Willy Tarreau2d372c22018-11-05 17:12:27 +0100154 [INF_CONNECTED_PEERS] = "ConnectedPeers",
Willy Tarreau13ef7732018-11-12 07:25:28 +0100155 [INF_DROPPED_LOGS] = "DroppedLogs",
Willy Tarreaubeb859a2018-11-22 18:07:59 +0100156 [INF_BUSY_POLLING] = "BusyPolling",
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;
Christopher Fauletef779222018-10-31 08:47:01 +0100261 if (!htx_add_data(htx, ist2(chk->area, chk->data)))
262 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);
280 struct ist uri = htx_sl_req_uri(http_find_stline(htx));
281
282 p = uri.ptr;
283 }
284 else
285 p = co_head(si_oc(si));
286
287 return p + appctx->ctx.stats.scope_str;
288}
289
William Lallemand74c24fb2016-11-21 17:18:36 +0100290/*
291 * http_stats_io_handler()
292 * -> stats_dump_stat_to_buffer() // same as above, but used for CSV or HTML
293 * -> stats_dump_csv_header() // emits the CSV headers (same as above)
Simon Horman05ee2132017-01-04 09:37:25 +0100294 * -> stats_dump_json_header() // emits the JSON headers (same as above)
William Lallemand74c24fb2016-11-21 17:18:36 +0100295 * -> stats_dump_html_head() // emits the HTML headers
296 * -> stats_dump_html_info() // emits the equivalent of "show info" at the top
297 * -> stats_dump_proxy_to_buffer() // same as above, valid for CSV and HTML
298 * -> stats_dump_html_px_hdr()
299 * -> stats_dump_fe_stats()
300 * -> stats_dump_li_stats()
301 * -> stats_dump_sv_stats()
302 * -> stats_dump_be_stats()
303 * -> stats_dump_html_px_end()
304 * -> stats_dump_html_end() // emits HTML trailer
Simon Horman05ee2132017-01-04 09:37:25 +0100305 * -> stats_dump_json_end() // emits JSON trailer
William Lallemand74c24fb2016-11-21 17:18:36 +0100306 */
307
308
William Lallemand74c24fb2016-11-21 17:18:36 +0100309/* Dumps the stats CSV header to the trash buffer which. The caller is responsible
310 * for clearing it if needed.
311 * NOTE: Some tools happen to rely on the field position instead of its name,
312 * so please only append new fields at the end, never in the middle.
313 */
314static void stats_dump_csv_header()
315{
316 int field;
317
318 chunk_appendf(&trash, "# ");
319 for (field = 0; field < ST_F_TOTAL_FIELDS; field++)
320 chunk_appendf(&trash, "%s,", stat_field_names[field]);
321
322 chunk_appendf(&trash, "\n");
323}
324
325
326/* Emits a stats field without any surrounding element and properly encoded to
327 * resist CSV output. Returns non-zero on success, 0 if the buffer is full.
328 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200329int stats_emit_raw_data_field(struct buffer *out, const struct field *f)
William Lallemand74c24fb2016-11-21 17:18:36 +0100330{
331 switch (field_format(f, 0)) {
332 case FF_EMPTY: return 1;
333 case FF_S32: return chunk_appendf(out, "%d", f->u.s32);
334 case FF_U32: return chunk_appendf(out, "%u", f->u.u32);
335 case FF_S64: return chunk_appendf(out, "%lld", (long long)f->u.s64);
336 case FF_U64: return chunk_appendf(out, "%llu", (unsigned long long)f->u.u64);
337 case FF_STR: return csv_enc_append(field_str(f, 0), 1, out) != NULL;
338 default: return chunk_appendf(out, "[INCORRECT_FIELD_TYPE_%08x]", f->type);
339 }
340}
341
342/* Emits a stats field prefixed with its type. No CSV encoding is prepared, the
343 * output is supposed to be used on its own line. Returns non-zero on success, 0
344 * if the buffer is full.
345 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200346int stats_emit_typed_data_field(struct buffer *out, const struct field *f)
William Lallemand74c24fb2016-11-21 17:18:36 +0100347{
348 switch (field_format(f, 0)) {
349 case FF_EMPTY: return 1;
350 case FF_S32: return chunk_appendf(out, "s32:%d", f->u.s32);
351 case FF_U32: return chunk_appendf(out, "u32:%u", f->u.u32);
352 case FF_S64: return chunk_appendf(out, "s64:%lld", (long long)f->u.s64);
353 case FF_U64: return chunk_appendf(out, "u64:%llu", (unsigned long long)f->u.u64);
354 case FF_STR: return chunk_appendf(out, "str:%s", field_str(f, 0));
355 default: return chunk_appendf(out, "%08x:?", f->type);
356 }
357}
358
Simon Horman05ee2132017-01-04 09:37:25 +0100359/* Limit JSON integer values to the range [-(2**53)+1, (2**53)-1] as per
360 * the recommendation for interoperable integers in section 6 of RFC 7159.
361 */
362#define JSON_INT_MAX ((1ULL << 53) - 1)
363#define JSON_INT_MIN (0 - JSON_INT_MAX)
364
365/* Emits a stats field value and its type in JSON.
366 * Returns non-zero on success, 0 on error.
367 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200368int stats_emit_json_data_field(struct buffer *out, const struct field *f)
Simon Horman05ee2132017-01-04 09:37:25 +0100369{
370 int old_len;
371 char buf[20];
372 const char *type, *value = buf, *quote = "";
373
374 switch (field_format(f, 0)) {
375 case FF_EMPTY: return 1;
376 case FF_S32: type = "\"s32\"";
377 snprintf(buf, sizeof(buf), "%d", f->u.s32);
378 break;
379 case FF_U32: type = "\"u32\"";
380 snprintf(buf, sizeof(buf), "%u", f->u.u32);
381 break;
382 case FF_S64: type = "\"s64\"";
383 if (f->u.s64 < JSON_INT_MIN || f->u.s64 > JSON_INT_MAX)
384 return 0;
385 type = "\"s64\"";
386 snprintf(buf, sizeof(buf), "%lld", (long long)f->u.s64);
387 break;
388 case FF_U64: if (f->u.u64 > JSON_INT_MAX)
389 return 0;
390 type = "\"u64\"";
391 snprintf(buf, sizeof(buf), "%llu",
392 (unsigned long long) f->u.u64);
393 break;
394 case FF_STR: type = "\"str\"";
395 value = field_str(f, 0);
396 quote = "\"";
397 break;
398 default: snprintf(buf, sizeof(buf), "%u", f->type);
399 type = buf;
400 value = "unknown";
401 quote = "\"";
402 break;
403 }
404
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200405 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100406 chunk_appendf(out, ",\"value\":{\"type\":%s,\"value\":%s%s%s}",
407 type, quote, value, quote);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200408 return !(old_len == out->data);
Simon Horman05ee2132017-01-04 09:37:25 +0100409}
410
William Lallemand74c24fb2016-11-21 17:18:36 +0100411/* Emits an encoding of the field type on 3 characters followed by a delimiter.
412 * Returns non-zero on success, 0 if the buffer is full.
413 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200414int stats_emit_field_tags(struct buffer *out, const struct field *f,
415 char delim)
William Lallemand74c24fb2016-11-21 17:18:36 +0100416{
417 char origin, nature, scope;
418
419 switch (field_origin(f, 0)) {
420 case FO_METRIC: origin = 'M'; break;
421 case FO_STATUS: origin = 'S'; break;
422 case FO_KEY: origin = 'K'; break;
423 case FO_CONFIG: origin = 'C'; break;
424 case FO_PRODUCT: origin = 'P'; break;
425 default: origin = '?'; break;
426 }
427
428 switch (field_nature(f, 0)) {
429 case FN_GAUGE: nature = 'G'; break;
430 case FN_LIMIT: nature = 'L'; break;
431 case FN_MIN: nature = 'm'; break;
432 case FN_MAX: nature = 'M'; break;
433 case FN_RATE: nature = 'R'; break;
434 case FN_COUNTER: nature = 'C'; break;
435 case FN_DURATION: nature = 'D'; break;
436 case FN_AGE: nature = 'A'; break;
437 case FN_TIME: nature = 'T'; break;
438 case FN_NAME: nature = 'N'; break;
439 case FN_OUTPUT: nature = 'O'; break;
440 case FN_AVG: nature = 'a'; break;
441 default: nature = '?'; break;
442 }
443
444 switch (field_scope(f, 0)) {
445 case FS_PROCESS: scope = 'P'; break;
446 case FS_SERVICE: scope = 'S'; break;
447 case FS_SYSTEM: scope = 's'; break;
448 case FS_CLUSTER: scope = 'C'; break;
449 default: scope = '?'; break;
450 }
451
452 return chunk_appendf(out, "%c%c%c%c", origin, nature, scope, delim);
453}
454
Simon Horman05ee2132017-01-04 09:37:25 +0100455/* Emits an encoding of the field type as JSON.
456 * Returns non-zero on success, 0 if the buffer is full.
457 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200458int stats_emit_json_field_tags(struct buffer *out, const struct field *f)
Simon Horman05ee2132017-01-04 09:37:25 +0100459{
460 const char *origin, *nature, *scope;
461 int old_len;
462
463 switch (field_origin(f, 0)) {
464 case FO_METRIC: origin = "Metric"; break;
465 case FO_STATUS: origin = "Status"; break;
466 case FO_KEY: origin = "Key"; break;
467 case FO_CONFIG: origin = "Config"; break;
468 case FO_PRODUCT: origin = "Product"; break;
469 default: origin = "Unknown"; break;
470 }
471
472 switch (field_nature(f, 0)) {
473 case FN_GAUGE: nature = "Gauge"; break;
474 case FN_LIMIT: nature = "Limit"; break;
475 case FN_MIN: nature = "Min"; break;
476 case FN_MAX: nature = "Max"; break;
477 case FN_RATE: nature = "Rate"; break;
478 case FN_COUNTER: nature = "Counter"; break;
479 case FN_DURATION: nature = "Duration"; break;
480 case FN_AGE: nature = "Age"; break;
481 case FN_TIME: nature = "Time"; break;
482 case FN_NAME: nature = "Name"; break;
483 case FN_OUTPUT: nature = "Output"; break;
484 case FN_AVG: nature = "Avg"; break;
485 default: nature = "Unknown"; break;
486 }
487
488 switch (field_scope(f, 0)) {
489 case FS_PROCESS: scope = "Process"; break;
490 case FS_SERVICE: scope = "Service"; break;
491 case FS_SYSTEM: scope = "System"; break;
492 case FS_CLUSTER: scope = "Cluster"; break;
493 default: scope = "Unknown"; break;
494 }
495
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200496 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100497 chunk_appendf(out, "\"tags\":{"
498 "\"origin\":\"%s\","
499 "\"nature\":\"%s\","
500 "\"scope\":\"%s\""
501 "}", origin, nature, scope);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200502 return !(old_len == out->data);
Simon Horman05ee2132017-01-04 09:37:25 +0100503}
William Lallemand74c24fb2016-11-21 17:18:36 +0100504
505/* Dump all fields from <stats> into <out> using CSV format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200506static int stats_dump_fields_csv(struct buffer *out,
507 const struct field *stats)
William Lallemand74c24fb2016-11-21 17:18:36 +0100508{
509 int field;
510
511 for (field = 0; field < ST_F_TOTAL_FIELDS; field++) {
512 if (!stats_emit_raw_data_field(out, &stats[field]))
513 return 0;
514 if (!chunk_strcat(out, ","))
515 return 0;
516 }
517 chunk_strcat(out, "\n");
518 return 1;
519}
520
521/* Dump all fields from <stats> into <out> using a typed "field:desc:type:value" format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200522static int stats_dump_fields_typed(struct buffer *out,
523 const struct field *stats)
William Lallemand74c24fb2016-11-21 17:18:36 +0100524{
525 int field;
526
527 for (field = 0; field < ST_F_TOTAL_FIELDS; field++) {
528 if (!stats[field].type)
529 continue;
530
531 chunk_appendf(out, "%c.%u.%u.%d.%s.%u:",
532 stats[ST_F_TYPE].u.u32 == STATS_TYPE_FE ? 'F' :
533 stats[ST_F_TYPE].u.u32 == STATS_TYPE_BE ? 'B' :
534 stats[ST_F_TYPE].u.u32 == STATS_TYPE_SO ? 'L' :
535 stats[ST_F_TYPE].u.u32 == STATS_TYPE_SV ? 'S' :
536 '?',
537 stats[ST_F_IID].u.u32, stats[ST_F_SID].u.u32,
538 field, stat_field_names[field], stats[ST_F_PID].u.u32);
539
540 if (!stats_emit_field_tags(out, &stats[field], ':'))
541 return 0;
542 if (!stats_emit_typed_data_field(out, &stats[field]))
543 return 0;
544 if (!chunk_strcat(out, "\n"))
545 return 0;
546 }
547 return 1;
548}
549
Simon Horman05ee2132017-01-04 09:37:25 +0100550/* Dump all fields from <stats> into <out> using the "show info json" format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200551static int stats_dump_json_info_fields(struct buffer *out,
Simon Horman05ee2132017-01-04 09:37:25 +0100552 const struct field *info)
553{
554 int field;
555 int started = 0;
556
557 if (!chunk_strcat(out, "["))
558 return 0;
559
560 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
561 int old_len;
562
563 if (!field_format(info, field))
564 continue;
565
566 if (started && !chunk_strcat(out, ","))
567 goto err;
568 started = 1;
569
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200570 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100571 chunk_appendf(out,
572 "{\"field\":{\"pos\":%d,\"name\":\"%s\"},"
573 "\"processNum\":%u,",
574 field, info_field_names[field],
575 info[INF_PROCESS_NUM].u.u32);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200576 if (old_len == out->data)
Simon Horman05ee2132017-01-04 09:37:25 +0100577 goto err;
578
579 if (!stats_emit_json_field_tags(out, &info[field]))
580 goto err;
581
582 if (!stats_emit_json_data_field(out, &info[field]))
583 goto err;
584
585 if (!chunk_strcat(out, "}"))
586 goto err;
587 }
588
589 if (!chunk_strcat(out, "]"))
590 goto err;
591 return 1;
592
593err:
594 chunk_reset(out);
595 chunk_appendf(out, "{\"errorStr\":\"output buffer too short\"}");
596 return 0;
597}
598
599/* Dump all fields from <stats> into <out> using a typed "field:desc:type:value" format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200600static int stats_dump_fields_json(struct buffer *out,
601 const struct field *stats,
Simon Horman05ee2132017-01-04 09:37:25 +0100602 int first_stat)
603{
604 int field;
605 int started = 0;
606
607 if (!first_stat && !chunk_strcat(out, ","))
608 return 0;
609 if (!chunk_strcat(out, "["))
610 return 0;
611
612 for (field = 0; field < ST_F_TOTAL_FIELDS; field++) {
613 const char *obj_type;
614 int old_len;
615
616 if (!stats[field].type)
617 continue;
618
619 if (started && !chunk_strcat(out, ","))
620 goto err;
621 started = 1;
622
623 switch (stats[ST_F_TYPE].u.u32) {
624 case STATS_TYPE_FE: obj_type = "Frontend"; break;
625 case STATS_TYPE_BE: obj_type = "Backend"; break;
626 case STATS_TYPE_SO: obj_type = "Listener"; break;
627 case STATS_TYPE_SV: obj_type = "Server"; break;
628 default: obj_type = "Unknown"; break;
629 }
630
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200631 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100632 chunk_appendf(out,
633 "{"
634 "\"objType\":\"%s\","
635 "\"proxyId\":%d,"
636 "\"id\":%d,"
637 "\"field\":{\"pos\":%d,\"name\":\"%s\"},"
638 "\"processNum\":%u,",
639 obj_type, stats[ST_F_IID].u.u32,
640 stats[ST_F_SID].u.u32, field,
641 stat_field_names[field], stats[ST_F_PID].u.u32);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200642 if (old_len == out->data)
Simon Horman05ee2132017-01-04 09:37:25 +0100643 goto err;
644
645 if (!stats_emit_json_field_tags(out, &stats[field]))
646 goto err;
647
648 if (!stats_emit_json_data_field(out, &stats[field]))
649 goto err;
650
651 if (!chunk_strcat(out, "}"))
652 goto err;
653 }
654
655 if (!chunk_strcat(out, "]"))
656 goto err;
657
658 return 1;
659
660err:
661 chunk_reset(out);
662 if (!first_stat)
663 chunk_strcat(out, ",");
664 chunk_appendf(out, "{\"errorStr\":\"output buffer too short\"}");
665 return 0;
666}
667
William Lallemand74c24fb2016-11-21 17:18:36 +0100668/* Dump all fields from <stats> into <out> using the HTML format. A column is
669 * reserved for the checkbox is ST_SHOWADMIN is set in <flags>. Some extra info
670 * are provided if ST_SHLGNDS is present in <flags>.
671 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200672static int stats_dump_fields_html(struct buffer *out,
673 const struct field *stats,
674 unsigned int flags)
William Lallemand74c24fb2016-11-21 17:18:36 +0100675{
Willy Tarreau83061a82018-07-13 11:56:34 +0200676 struct buffer src;
William Lallemand74c24fb2016-11-21 17:18:36 +0100677
678 if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_FE) {
679 chunk_appendf(out,
680 /* name, queue */
681 "<tr class=\"frontend\">");
682
683 if (flags & ST_SHOWADMIN) {
684 /* Column sub-heading for Enable or Disable server */
685 chunk_appendf(out, "<td></td>");
686 }
687
688 chunk_appendf(out,
689 "<td class=ac>"
690 "<a name=\"%s/Frontend\"></a>"
691 "<a class=lfsb href=\"#%s/Frontend\">Frontend</a></td>"
692 "<td colspan=3></td>"
693 "",
694 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_PXNAME));
695
696 chunk_appendf(out,
697 /* sessions rate : current */
698 "<td><u>%s<div class=tips><table class=det>"
699 "<tr><th>Current connection rate:</th><td>%s/s</td></tr>"
700 "<tr><th>Current session rate:</th><td>%s/s</td></tr>"
701 "",
702 U2H(stats[ST_F_RATE].u.u32),
703 U2H(stats[ST_F_CONN_RATE].u.u32),
704 U2H(stats[ST_F_RATE].u.u32));
705
706 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
707 chunk_appendf(out,
708 "<tr><th>Current request rate:</th><td>%s/s</td></tr>",
709 U2H(stats[ST_F_REQ_RATE].u.u32));
710
711 chunk_appendf(out,
712 "</table></div></u></td>"
713 /* sessions rate : max */
714 "<td><u>%s<div class=tips><table class=det>"
715 "<tr><th>Max connection rate:</th><td>%s/s</td></tr>"
716 "<tr><th>Max session rate:</th><td>%s/s</td></tr>"
717 "",
718 U2H(stats[ST_F_RATE_MAX].u.u32),
719 U2H(stats[ST_F_CONN_RATE_MAX].u.u32),
720 U2H(stats[ST_F_RATE_MAX].u.u32));
721
722 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
723 chunk_appendf(out,
724 "<tr><th>Max request rate:</th><td>%s/s</td></tr>",
725 U2H(stats[ST_F_REQ_RATE_MAX].u.u32));
726
727 chunk_appendf(out,
728 "</table></div></u></td>"
729 /* sessions rate : limit */
730 "<td>%s</td>",
731 LIM2A(stats[ST_F_RATE_LIM].u.u32, "-"));
732
733 chunk_appendf(out,
734 /* sessions: current, max, limit, total */
735 "<td>%s</td><td>%s</td><td>%s</td>"
736 "<td><u>%s<div class=tips><table class=det>"
737 "<tr><th>Cum. connections:</th><td>%s</td></tr>"
738 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
739 "",
740 U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SMAX].u.u32), U2H(stats[ST_F_SLIM].u.u32),
741 U2H(stats[ST_F_STOT].u.u64),
742 U2H(stats[ST_F_CONN_TOT].u.u64),
743 U2H(stats[ST_F_STOT].u.u64));
744
745 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
746 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
747 chunk_appendf(out,
748 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
749 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
750 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
751 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
752 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
753 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
754 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
755 "<tr><th>- other responses:</th><td>%s</td></tr>"
756 "<tr><th>Intercepted requests:</th><td>%s</td></tr>"
Willy Tarreaua1214a52018-12-14 14:00:25 +0100757 "<tr><th>Cache lookups:</th><td>%s</td></tr>"
758 "<tr><th>Cache hits:</th><td>%s</td><td>(%d%%)</td></tr>"
Willy Tarreau1b0f85e2018-05-28 15:12:40 +0200759 "<tr><th>Failed hdr rewrites:</th><td>%s</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +0100760 "",
761 U2H(stats[ST_F_REQ_TOT].u.u64),
762 U2H(stats[ST_F_HRSP_1XX].u.u64),
763 U2H(stats[ST_F_HRSP_2XX].u.u64),
764 U2H(stats[ST_F_COMP_RSP].u.u64),
765 stats[ST_F_HRSP_2XX].u.u64 ?
766 (int)(100 * stats[ST_F_COMP_RSP].u.u64 / stats[ST_F_HRSP_2XX].u.u64) : 0,
767 U2H(stats[ST_F_HRSP_3XX].u.u64),
768 U2H(stats[ST_F_HRSP_4XX].u.u64),
769 U2H(stats[ST_F_HRSP_5XX].u.u64),
770 U2H(stats[ST_F_HRSP_OTHER].u.u64),
Willy Tarreau1b0f85e2018-05-28 15:12:40 +0200771 U2H(stats[ST_F_INTERCEPTED].u.u64),
Willy Tarreaua1214a52018-12-14 14:00:25 +0100772 U2H(stats[ST_F_CACHE_LOOKUPS].u.u64),
773 U2H(stats[ST_F_CACHE_HITS].u.u64),
774 stats[ST_F_CACHE_LOOKUPS].u.u64 ?
775 (int)(100 * stats[ST_F_CACHE_HITS].u.u64 / stats[ST_F_CACHE_LOOKUPS].u.u64) : 0,
Willy Tarreau1b0f85e2018-05-28 15:12:40 +0200776 U2H(stats[ST_F_WREW].u.u64));
William Lallemand74c24fb2016-11-21 17:18:36 +0100777 }
778
779 chunk_appendf(out,
780 "</table></div></u></td>"
781 /* sessions: lbtot, lastsess */
782 "<td></td><td></td>"
783 /* bytes : in */
784 "<td>%s</td>"
785 "",
786 U2H(stats[ST_F_BIN].u.u64));
787
788 chunk_appendf(out,
789 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
790 "<td>%s%s<div class=tips><table class=det>"
791 "<tr><th>Response bytes in:</th><td>%s</td></tr>"
792 "<tr><th>Compression in:</th><td>%s</td></tr>"
793 "<tr><th>Compression out:</th><td>%s</td><td>(%d%%)</td></tr>"
794 "<tr><th>Compression bypass:</th><td>%s</td></tr>"
795 "<tr><th>Total bytes saved:</th><td>%s</td><td>(%d%%)</td></tr>"
796 "</table></div>%s</td>",
797 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "<u>":"",
798 U2H(stats[ST_F_BOUT].u.u64),
799 U2H(stats[ST_F_BOUT].u.u64),
800 U2H(stats[ST_F_COMP_IN].u.u64),
801 U2H(stats[ST_F_COMP_OUT].u.u64),
802 stats[ST_F_COMP_IN].u.u64 ? (int)(stats[ST_F_COMP_OUT].u.u64 * 100 / stats[ST_F_COMP_IN].u.u64) : 0,
803 U2H(stats[ST_F_COMP_BYP].u.u64),
804 U2H(stats[ST_F_COMP_IN].u.u64 - stats[ST_F_COMP_OUT].u.u64),
805 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,
806 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "</u>":"");
807
808 chunk_appendf(out,
809 /* denied: req, resp */
810 "<td>%s</td><td>%s</td>"
811 /* errors : request, connect, response */
812 "<td>%s</td><td></td><td></td>"
813 /* warnings: retries, redispatches */
814 "<td></td><td></td>"
815 /* server status : reflect frontend status */
816 "<td class=ac>%s</td>"
817 /* rest of server: nothing */
818 "<td class=ac colspan=8></td></tr>"
819 "",
820 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
821 U2H(stats[ST_F_EREQ].u.u64),
822 field_str(stats, ST_F_STATUS));
823 }
824 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_SO) {
825 chunk_appendf(out, "<tr class=socket>");
826 if (flags & ST_SHOWADMIN) {
827 /* Column sub-heading for Enable or Disable server */
828 chunk_appendf(out, "<td></td>");
829 }
830
831 chunk_appendf(out,
832 /* frontend name, listener name */
833 "<td class=ac><a name=\"%s/+%s\"></a>%s"
834 "<a class=lfsb href=\"#%s/+%s\">%s</a>"
835 "",
836 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME),
837 (flags & ST_SHLGNDS)?"<u>":"",
838 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME), field_str(stats, ST_F_SVNAME));
839
840 if (flags & ST_SHLGNDS) {
841 chunk_appendf(out, "<div class=tips>");
842
843 if (isdigit(*field_str(stats, ST_F_ADDR)))
844 chunk_appendf(out, "IPv4: %s, ", field_str(stats, ST_F_ADDR));
845 else if (*field_str(stats, ST_F_ADDR) == '[')
846 chunk_appendf(out, "IPv6: %s, ", field_str(stats, ST_F_ADDR));
847 else if (*field_str(stats, ST_F_ADDR))
848 chunk_appendf(out, "%s, ", field_str(stats, ST_F_ADDR));
849
850 /* id */
851 chunk_appendf(out, "id: %d</div>", stats[ST_F_SID].u.u32);
852 }
853
854 chunk_appendf(out,
855 /* queue */
856 "%s</td><td colspan=3></td>"
857 /* sessions rate: current, max, limit */
858 "<td colspan=3>&nbsp;</td>"
859 /* sessions: current, max, limit, total, lbtot, lastsess */
860 "<td>%s</td><td>%s</td><td>%s</td>"
861 "<td>%s</td><td>&nbsp;</td><td>&nbsp;</td>"
862 /* bytes: in, out */
863 "<td>%s</td><td>%s</td>"
864 "",
865 (flags & ST_SHLGNDS)?"</u>":"",
866 U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SMAX].u.u32), U2H(stats[ST_F_SLIM].u.u32),
867 U2H(stats[ST_F_STOT].u.u64), U2H(stats[ST_F_BIN].u.u64), U2H(stats[ST_F_BOUT].u.u64));
868
869 chunk_appendf(out,
870 /* denied: req, resp */
871 "<td>%s</td><td>%s</td>"
872 /* errors: request, connect, response */
873 "<td>%s</td><td></td><td></td>"
874 /* warnings: retries, redispatches */
875 "<td></td><td></td>"
876 /* server status: reflect listener status */
877 "<td class=ac>%s</td>"
878 /* rest of server: nothing */
879 "<td class=ac colspan=8></td></tr>"
880 "",
881 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
882 U2H(stats[ST_F_EREQ].u.u64),
883 field_str(stats, ST_F_STATUS));
884 }
885 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_SV) {
886 const char *style;
887
888 /* determine the style to use depending on the server's state,
889 * its health and weight. There isn't a 1-to-1 mapping between
890 * state and styles for the cases where the server is (still)
891 * up. The reason is that we don't want to report nolb and
892 * drain with the same color.
893 */
894
895 if (strcmp(field_str(stats, ST_F_STATUS), "DOWN") == 0 ||
896 strcmp(field_str(stats, ST_F_STATUS), "DOWN (agent)") == 0) {
897 style = "down";
898 }
899 else if (strcmp(field_str(stats, ST_F_STATUS), "DOWN ") == 0) {
900 style = "going_up";
901 }
902 else if (strcmp(field_str(stats, ST_F_STATUS), "NOLB ") == 0) {
903 style = "going_down";
904 }
905 else if (strcmp(field_str(stats, ST_F_STATUS), "NOLB") == 0) {
906 style = "nolb";
907 }
908 else if (strcmp(field_str(stats, ST_F_STATUS), "no check") == 0) {
909 style = "no_check";
910 }
911 else if (!stats[ST_F_CHKFAIL].type ||
912 stats[ST_F_CHECK_HEALTH].u.u32 == stats[ST_F_CHECK_RISE].u.u32 + stats[ST_F_CHECK_FALL].u.u32 - 1) {
913 /* no check or max health = UP */
914 if (stats[ST_F_WEIGHT].u.u32)
915 style = "up";
916 else
917 style = "draining";
918 }
919 else {
920 style = "going_down";
921 }
922
923 if (memcmp(field_str(stats, ST_F_STATUS), "MAINT", 5) == 0)
924 chunk_appendf(out, "<tr class=\"maintain\">");
925 else
926 chunk_appendf(out,
927 "<tr class=\"%s_%s\">",
928 (stats[ST_F_BCK].u.u32) ? "backup" : "active", style);
929
930
931 if (flags & ST_SHOWADMIN)
932 chunk_appendf(out,
David Harrigand3db35a2016-12-30 12:12:49 +0000933 "<td><input class='%s-checkbox' type=\"checkbox\" name=\"s\" value=\"%s\"></td>",
934 field_str(stats, ST_F_PXNAME),
William Lallemand74c24fb2016-11-21 17:18:36 +0100935 field_str(stats, ST_F_SVNAME));
936
937 chunk_appendf(out,
938 "<td class=ac><a name=\"%s/%s\"></a>%s"
939 "<a class=lfsb href=\"#%s/%s\">%s</a>"
940 "",
941 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME),
942 (flags & ST_SHLGNDS) ? "<u>" : "",
943 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME), field_str(stats, ST_F_SVNAME));
944
945 if (flags & ST_SHLGNDS) {
946 chunk_appendf(out, "<div class=tips>");
947
948 if (isdigit(*field_str(stats, ST_F_ADDR)))
949 chunk_appendf(out, "IPv4: %s, ", field_str(stats, ST_F_ADDR));
950 else if (*field_str(stats, ST_F_ADDR) == '[')
951 chunk_appendf(out, "IPv6: %s, ", field_str(stats, ST_F_ADDR));
952 else if (*field_str(stats, ST_F_ADDR))
953 chunk_appendf(out, "%s, ", field_str(stats, ST_F_ADDR));
954
955 /* id */
956 chunk_appendf(out, "id: %d", stats[ST_F_SID].u.u32);
957
958 /* cookie */
959 if (stats[ST_F_COOKIE].type) {
960 chunk_appendf(out, ", cookie: '");
961 chunk_initstr(&src, field_str(stats, ST_F_COOKIE));
962 chunk_htmlencode(out, &src);
963 chunk_appendf(out, "'");
964 }
965
966 chunk_appendf(out, "</div>");
967 }
968
969 chunk_appendf(out,
970 /* queue : current, max, limit */
971 "%s</td><td>%s</td><td>%s</td><td>%s</td>"
972 /* sessions rate : current, max, limit */
973 "<td>%s</td><td>%s</td><td></td>"
974 "",
975 (flags & ST_SHLGNDS) ? "</u>" : "",
976 U2H(stats[ST_F_QCUR].u.u32), U2H(stats[ST_F_QMAX].u.u32), LIM2A(stats[ST_F_QLIMIT].u.u32, "-"),
977 U2H(stats[ST_F_RATE].u.u32), U2H(stats[ST_F_RATE_MAX].u.u32));
978
979 chunk_appendf(out,
980 /* sessions: current, max, limit, total */
981 "<td>%s</td><td>%s</td><td>%s</td>"
982 "<td><u>%s<div class=tips><table class=det>"
983 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
984 "",
985 U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SMAX].u.u32), LIM2A(stats[ST_F_SLIM].u.u32, "-"),
986 U2H(stats[ST_F_STOT].u.u64),
987 U2H(stats[ST_F_STOT].u.u64));
988
989 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
990 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
991 unsigned long long tot;
992
993 tot = stats[ST_F_HRSP_OTHER].u.u64;
994 tot += stats[ST_F_HRSP_1XX].u.u64;
995 tot += stats[ST_F_HRSP_2XX].u.u64;
996 tot += stats[ST_F_HRSP_3XX].u.u64;
997 tot += stats[ST_F_HRSP_4XX].u.u64;
998 tot += stats[ST_F_HRSP_5XX].u.u64;
999
1000 chunk_appendf(out,
Willy Tarreauf1573842018-12-14 11:35:36 +01001001 "<tr><th>New connections:</th><td>%s</td></tr>"
1002 "<tr><th>Reused connections:</th><td>%s</td><td>(%d%%)</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001003 "<tr><th>Cum. HTTP responses:</th><td>%s</td></tr>"
1004 "<tr><th>- HTTP 1xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1005 "<tr><th>- HTTP 2xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1006 "<tr><th>- HTTP 3xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1007 "<tr><th>- HTTP 4xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1008 "<tr><th>- HTTP 5xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1009 "<tr><th>- other responses:</th><td>%s</td><td>(%d%%)</td></tr>"
Willy Tarreau1b0f85e2018-05-28 15:12:40 +02001010 "<tr><th>Failed hdr rewrites:</th><td>%s</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001011 "",
Willy Tarreauf1573842018-12-14 11:35:36 +01001012 U2H(stats[ST_F_CONNECT].u.u64),
1013 U2H(stats[ST_F_REUSE].u.u64),
1014 (stats[ST_F_CONNECT].u.u64 + stats[ST_F_REUSE].u.u64) ?
1015 (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 +01001016 U2H(tot),
1017 U2H(stats[ST_F_HRSP_1XX].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_1XX].u.u64 / tot) : 0,
1018 U2H(stats[ST_F_HRSP_2XX].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_2XX].u.u64 / tot) : 0,
1019 U2H(stats[ST_F_HRSP_3XX].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_3XX].u.u64 / tot) : 0,
1020 U2H(stats[ST_F_HRSP_4XX].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_4XX].u.u64 / tot) : 0,
1021 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 +02001022 U2H(stats[ST_F_HRSP_OTHER].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_OTHER].u.u64 / tot) : 0,
1023 U2H(stats[ST_F_WREW].u.u64));
William Lallemand74c24fb2016-11-21 17:18:36 +01001024 }
1025
1026 chunk_appendf(out, "<tr><th colspan=3>Avg over last 1024 success. conn.</th></tr>");
1027 chunk_appendf(out, "<tr><th>- Queue time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_QTIME].u.u32));
1028 chunk_appendf(out, "<tr><th>- Connect time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_CTIME].u.u32));
1029 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
1030 chunk_appendf(out, "<tr><th>- Response time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_RTIME].u.u32));
1031 chunk_appendf(out, "<tr><th>- Total time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_TTIME].u.u32));
1032
1033 chunk_appendf(out,
1034 "</table></div></u></td>"
1035 /* sessions: lbtot, last */
1036 "<td>%s</td><td>%s</td>",
1037 U2H(stats[ST_F_LBTOT].u.u64),
1038 human_time(stats[ST_F_LASTSESS].u.s32, 1));
1039
1040 chunk_appendf(out,
1041 /* bytes : in, out */
1042 "<td>%s</td><td>%s</td>"
1043 /* denied: req, resp */
1044 "<td></td><td>%s</td>"
1045 /* errors : request, connect */
1046 "<td></td><td>%s</td>"
1047 /* errors : response */
1048 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
1049 /* warnings: retries, redispatches */
1050 "<td>%lld</td><td>%lld</td>"
1051 "",
1052 U2H(stats[ST_F_BIN].u.u64), U2H(stats[ST_F_BOUT].u.u64),
1053 U2H(stats[ST_F_DRESP].u.u64),
1054 U2H(stats[ST_F_ECON].u.u64),
1055 U2H(stats[ST_F_ERESP].u.u64),
1056 (long long)stats[ST_F_CLI_ABRT].u.u64,
1057 (long long)stats[ST_F_SRV_ABRT].u.u64,
1058 (long long)stats[ST_F_WRETR].u.u64,
1059 (long long)stats[ST_F_WREDIS].u.u64);
1060
1061 /* status, last change */
1062 chunk_appendf(out, "<td class=ac>");
1063
1064 /* FIXME!!!!
1065 * LASTCHG should contain the last change for *this* server and must be computed
1066 * properly above, as was done below, ie: this server if maint, otherwise ref server
1067 * if tracking. Note that ref is either local or remote depending on tracking.
1068 */
1069
1070
1071 if (memcmp(field_str(stats, ST_F_STATUS), "MAINT", 5) == 0) {
1072 chunk_appendf(out, "%s MAINT", human_time(stats[ST_F_LASTCHG].u.u32, 1));
1073 }
1074 else if (memcmp(field_str(stats, ST_F_STATUS), "no check", 5) == 0) {
1075 chunk_strcat(out, "<i>no check</i>");
1076 }
1077 else {
1078 chunk_appendf(out, "%s %s", human_time(stats[ST_F_LASTCHG].u.u32, 1), field_str(stats, ST_F_STATUS));
1079 if (memcmp(field_str(stats, ST_F_STATUS), "DOWN", 4) == 0) {
1080 if (stats[ST_F_CHECK_HEALTH].u.u32)
1081 chunk_strcat(out, " &uarr;");
1082 }
1083 else if (stats[ST_F_CHECK_HEALTH].u.u32 < stats[ST_F_CHECK_RISE].u.u32 + stats[ST_F_CHECK_FALL].u.u32 - 1)
1084 chunk_strcat(out, " &darr;");
1085 }
1086
1087 if (memcmp(field_str(stats, ST_F_STATUS), "DOWN", 4) == 0 &&
1088 stats[ST_F_AGENT_STATUS].type && !stats[ST_F_AGENT_HEALTH].u.u32) {
1089 chunk_appendf(out,
1090 "</td><td class=ac><u> %s",
1091 field_str(stats, ST_F_AGENT_STATUS));
1092
1093 if (stats[ST_F_AGENT_CODE].type)
1094 chunk_appendf(out, "/%d", stats[ST_F_AGENT_CODE].u.u32);
1095
1096 if (stats[ST_F_AGENT_DURATION].type)
1097 chunk_appendf(out, " in %lums", (long)stats[ST_F_AGENT_DURATION].u.u64);
1098
1099 chunk_appendf(out, "<div class=tips>%s", field_str(stats, ST_F_AGENT_DESC));
1100
1101 if (*field_str(stats, ST_F_LAST_AGT)) {
1102 chunk_appendf(out, ": ");
1103 chunk_initstr(&src, field_str(stats, ST_F_LAST_AGT));
1104 chunk_htmlencode(out, &src);
1105 }
1106 chunk_appendf(out, "</div></u>");
1107 }
1108 else if (stats[ST_F_CHECK_STATUS].type) {
1109 chunk_appendf(out,
1110 "</td><td class=ac><u> %s",
1111 field_str(stats, ST_F_CHECK_STATUS));
1112
1113 if (stats[ST_F_CHECK_CODE].type)
1114 chunk_appendf(out, "/%d", stats[ST_F_CHECK_CODE].u.u32);
1115
1116 if (stats[ST_F_CHECK_DURATION].type)
1117 chunk_appendf(out, " in %lums", (long)stats[ST_F_CHECK_DURATION].u.u64);
1118
1119 chunk_appendf(out, "<div class=tips>%s", field_str(stats, ST_F_CHECK_DESC));
1120
1121 if (*field_str(stats, ST_F_LAST_CHK)) {
1122 chunk_appendf(out, ": ");
1123 chunk_initstr(&src, field_str(stats, ST_F_LAST_CHK));
1124 chunk_htmlencode(out, &src);
1125 }
1126 chunk_appendf(out, "</div></u>");
1127 }
1128 else
1129 chunk_appendf(out, "</td><td>");
1130
1131 chunk_appendf(out,
1132 /* weight */
1133 "</td><td class=ac>%d</td>"
1134 /* act, bck */
1135 "<td class=ac>%s</td><td class=ac>%s</td>"
1136 "",
1137 stats[ST_F_WEIGHT].u.u32,
1138 stats[ST_F_BCK].u.u32 ? "-" : "Y",
1139 stats[ST_F_BCK].u.u32 ? "Y" : "-");
1140
1141 /* check failures: unique, fatal, down time */
1142 if (strcmp(field_str(stats, ST_F_STATUS), "MAINT (resolution)") == 0) {
1143 chunk_appendf(out, "<td class=ac colspan=3>resolution</td>");
1144 }
1145 else if (stats[ST_F_CHKFAIL].type) {
1146 chunk_appendf(out, "<td><u>%lld", (long long)stats[ST_F_CHKFAIL].u.u64);
1147
1148 if (stats[ST_F_HANAFAIL].type)
1149 chunk_appendf(out, "/%lld", (long long)stats[ST_F_HANAFAIL].u.u64);
1150
1151 chunk_appendf(out,
1152 "<div class=tips>Failed Health Checks%s</div></u></td>"
1153 "<td>%lld</td><td>%s</td>"
1154 "",
1155 stats[ST_F_HANAFAIL].type ? "/Health Analyses" : "",
1156 (long long)stats[ST_F_CHKDOWN].u.u64, human_time(stats[ST_F_DOWNTIME].u.u32, 1));
1157 }
1158 else if (strcmp(field_str(stats, ST_F_STATUS), "MAINT") != 0 && field_format(stats, ST_F_TRACKED) == FF_STR) {
1159 /* tracking a server (hence inherited maint would appear as "MAINT (via...)" */
1160 chunk_appendf(out,
1161 "<td class=ac colspan=3><a class=lfsb href=\"#%s\">via %s</a></td>",
1162 field_str(stats, ST_F_TRACKED), field_str(stats, ST_F_TRACKED));
1163 }
1164 else
1165 chunk_appendf(out, "<td colspan=3></td>");
1166
1167 /* throttle */
1168 if (stats[ST_F_THROTTLE].type)
1169 chunk_appendf(out, "<td class=ac>%d %%</td></tr>\n", stats[ST_F_THROTTLE].u.u32);
1170 else
1171 chunk_appendf(out, "<td class=ac>-</td></tr>\n");
1172 }
1173 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_BE) {
1174 chunk_appendf(out, "<tr class=\"backend\">");
1175 if (flags & ST_SHOWADMIN) {
1176 /* Column sub-heading for Enable or Disable server */
1177 chunk_appendf(out, "<td></td>");
1178 }
1179 chunk_appendf(out,
1180 "<td class=ac>"
1181 /* name */
1182 "%s<a name=\"%s/Backend\"></a>"
1183 "<a class=lfsb href=\"#%s/Backend\">Backend</a>"
1184 "",
1185 (flags & ST_SHLGNDS)?"<u>":"",
1186 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_PXNAME));
1187
1188 if (flags & ST_SHLGNDS) {
1189 /* balancing */
1190 chunk_appendf(out, "<div class=tips>balancing: %s",
1191 field_str(stats, ST_F_ALGO));
1192
1193 /* cookie */
1194 if (stats[ST_F_COOKIE].type) {
1195 chunk_appendf(out, ", cookie: '");
1196 chunk_initstr(&src, field_str(stats, ST_F_COOKIE));
1197 chunk_htmlencode(out, &src);
1198 chunk_appendf(out, "'");
1199 }
1200 chunk_appendf(out, "</div>");
1201 }
1202
1203 chunk_appendf(out,
1204 "%s</td>"
1205 /* queue : current, max */
1206 "<td>%s</td><td>%s</td><td></td>"
1207 /* sessions rate : current, max, limit */
1208 "<td>%s</td><td>%s</td><td></td>"
1209 "",
1210 (flags & ST_SHLGNDS)?"</u>":"",
1211 U2H(stats[ST_F_QCUR].u.u32), U2H(stats[ST_F_QMAX].u.u32),
1212 U2H(stats[ST_F_RATE].u.u32), U2H(stats[ST_F_RATE_MAX].u.u32));
1213
1214 chunk_appendf(out,
1215 /* sessions: current, max, limit, total */
1216 "<td>%s</td><td>%s</td><td>%s</td>"
1217 "<td><u>%s<div class=tips><table class=det>"
1218 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
1219 "",
Willy Tarreau8e0f1752016-12-12 15:07:29 +01001220 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 +01001221 U2H(stats[ST_F_STOT].u.u64),
1222 U2H(stats[ST_F_STOT].u.u64));
1223
1224 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
1225 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
1226 chunk_appendf(out,
Willy Tarreauf1573842018-12-14 11:35:36 +01001227 "<tr><th>New connections:</th><td>%s</td></tr>"
1228 "<tr><th>Reused connections:</th><td>%s</td><td>(%d%%)</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001229 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
1230 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
1231 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
1232 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
1233 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
1234 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
1235 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
1236 "<tr><th>- other responses:</th><td>%s</td></tr>"
Willy Tarreaua1214a52018-12-14 14:00:25 +01001237 "<tr><th>Cache lookups:</th><td>%s</td></tr>"
1238 "<tr><th>Cache hits:</th><td>%s</td><td>(%d%%)</td></tr>"
Willy Tarreau1b0f85e2018-05-28 15:12:40 +02001239 "<tr><th>Failed hdr rewrites:</th><td>%s</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001240 "<tr><th colspan=3>Avg over last 1024 success. conn.</th></tr>"
1241 "",
Willy Tarreauf1573842018-12-14 11:35:36 +01001242 U2H(stats[ST_F_CONNECT].u.u64),
1243 U2H(stats[ST_F_REUSE].u.u64),
1244 (stats[ST_F_CONNECT].u.u64 + stats[ST_F_REUSE].u.u64) ?
1245 (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 +01001246 U2H(stats[ST_F_REQ_TOT].u.u64),
1247 U2H(stats[ST_F_HRSP_1XX].u.u64),
1248 U2H(stats[ST_F_HRSP_2XX].u.u64),
1249 U2H(stats[ST_F_COMP_RSP].u.u64),
1250 stats[ST_F_HRSP_2XX].u.u64 ?
1251 (int)(100 * stats[ST_F_COMP_RSP].u.u64 / stats[ST_F_HRSP_2XX].u.u64) : 0,
1252 U2H(stats[ST_F_HRSP_3XX].u.u64),
1253 U2H(stats[ST_F_HRSP_4XX].u.u64),
1254 U2H(stats[ST_F_HRSP_5XX].u.u64),
Willy Tarreaufeead3a2018-12-14 13:48:44 +01001255 U2H(stats[ST_F_HRSP_OTHER].u.u64),
Willy Tarreaua1214a52018-12-14 14:00:25 +01001256 U2H(stats[ST_F_CACHE_LOOKUPS].u.u64),
1257 U2H(stats[ST_F_CACHE_HITS].u.u64),
1258 stats[ST_F_CACHE_LOOKUPS].u.u64 ?
1259 (int)(100 * stats[ST_F_CACHE_HITS].u.u64 / stats[ST_F_CACHE_LOOKUPS].u.u64) : 0,
Willy Tarreaufeead3a2018-12-14 13:48:44 +01001260 U2H(stats[ST_F_WREW].u.u64));
William Lallemand74c24fb2016-11-21 17:18:36 +01001261 }
1262
1263 chunk_appendf(out, "<tr><th>- Queue time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_QTIME].u.u32));
1264 chunk_appendf(out, "<tr><th>- Connect time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_CTIME].u.u32));
1265 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
1266 chunk_appendf(out, "<tr><th>- Response time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_RTIME].u.u32));
1267 chunk_appendf(out, "<tr><th>- Total time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_TTIME].u.u32));
1268
1269 chunk_appendf(out,
1270 "</table></div></u></td>"
1271 /* sessions: lbtot, last */
1272 "<td>%s</td><td>%s</td>"
1273 /* bytes: in */
1274 "<td>%s</td>"
1275 "",
1276 U2H(stats[ST_F_LBTOT].u.u64),
1277 human_time(stats[ST_F_LASTSESS].u.s32, 1),
1278 U2H(stats[ST_F_BIN].u.u64));
1279
1280 chunk_appendf(out,
1281 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
1282 "<td>%s%s<div class=tips><table class=det>"
1283 "<tr><th>Response bytes in:</th><td>%s</td></tr>"
1284 "<tr><th>Compression in:</th><td>%s</td></tr>"
1285 "<tr><th>Compression out:</th><td>%s</td><td>(%d%%)</td></tr>"
1286 "<tr><th>Compression bypass:</th><td>%s</td></tr>"
1287 "<tr><th>Total bytes saved:</th><td>%s</td><td>(%d%%)</td></tr>"
1288 "</table></div>%s</td>",
1289 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "<u>":"",
1290 U2H(stats[ST_F_BOUT].u.u64),
1291 U2H(stats[ST_F_BOUT].u.u64),
1292 U2H(stats[ST_F_COMP_IN].u.u64),
1293 U2H(stats[ST_F_COMP_OUT].u.u64),
1294 stats[ST_F_COMP_IN].u.u64 ? (int)(stats[ST_F_COMP_OUT].u.u64 * 100 / stats[ST_F_COMP_IN].u.u64) : 0,
1295 U2H(stats[ST_F_COMP_BYP].u.u64),
1296 U2H(stats[ST_F_COMP_IN].u.u64 - stats[ST_F_COMP_OUT].u.u64),
1297 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,
1298 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "</u>":"");
1299
1300 chunk_appendf(out,
1301 /* denied: req, resp */
1302 "<td>%s</td><td>%s</td>"
1303 /* errors : request, connect */
1304 "<td></td><td>%s</td>"
1305 /* errors : response */
1306 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
1307 /* warnings: retries, redispatches */
1308 "<td>%lld</td><td>%lld</td>"
1309 /* backend status: reflect backend status (up/down): we display UP
1310 * if the backend has known working servers or if it has no server at
1311 * all (eg: for stats). Then we display the total weight, number of
1312 * active and backups. */
1313 "<td class=ac>%s %s</td><td class=ac>&nbsp;</td><td class=ac>%d</td>"
1314 "<td class=ac>%d</td><td class=ac>%d</td>"
1315 "",
1316 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
1317 U2H(stats[ST_F_ECON].u.u64),
1318 U2H(stats[ST_F_ERESP].u.u64),
1319 (long long)stats[ST_F_CLI_ABRT].u.u64,
1320 (long long)stats[ST_F_SRV_ABRT].u.u64,
1321 (long long)stats[ST_F_WRETR].u.u64, (long long)stats[ST_F_WREDIS].u.u64,
1322 human_time(stats[ST_F_LASTCHG].u.u32, 1),
1323 strcmp(field_str(stats, ST_F_STATUS), "DOWN") ? field_str(stats, ST_F_STATUS) : "<font color=\"red\"><b>DOWN</b></font>",
1324 stats[ST_F_WEIGHT].u.u32,
1325 stats[ST_F_ACT].u.u32, stats[ST_F_BCK].u.u32);
1326
1327 chunk_appendf(out,
1328 /* rest of backend: nothing, down transitions, total downtime, throttle */
1329 "<td class=ac>&nbsp;</td><td>%d</td>"
1330 "<td>%s</td>"
1331 "<td></td>"
1332 "</tr>",
1333 stats[ST_F_CHKDOWN].u.u32,
1334 stats[ST_F_DOWNTIME].type ? human_time(stats[ST_F_DOWNTIME].u.u32, 1) : "&nbsp;");
1335 }
1336 return 1;
1337}
1338
1339int stats_dump_one_line(const struct field *stats, unsigned int flags, struct proxy *px, struct appctx *appctx)
1340{
Simon Horman05ee2132017-01-04 09:37:25 +01001341 int ret;
1342
William Lallemand74c24fb2016-11-21 17:18:36 +01001343 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN))
1344 flags |= ST_SHOWADMIN;
1345
1346 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
Simon Horman05ee2132017-01-04 09:37:25 +01001347 ret = stats_dump_fields_html(&trash, stats, flags);
William Lallemand74c24fb2016-11-21 17:18:36 +01001348 else if (appctx->ctx.stats.flags & STAT_FMT_TYPED)
Simon Horman05ee2132017-01-04 09:37:25 +01001349 ret = stats_dump_fields_typed(&trash, stats);
1350 else if (appctx->ctx.stats.flags & STAT_FMT_JSON)
1351 ret = stats_dump_fields_json(&trash, stats,
1352 !(appctx->ctx.stats.flags &
1353 STAT_STARTED));
William Lallemand74c24fb2016-11-21 17:18:36 +01001354 else
Simon Horman05ee2132017-01-04 09:37:25 +01001355 ret = stats_dump_fields_csv(&trash, stats);
1356
1357 if (ret)
1358 appctx->ctx.stats.flags |= STAT_STARTED;
1359
1360 return ret;
William Lallemand74c24fb2016-11-21 17:18:36 +01001361}
1362
1363/* Fill <stats> with the frontend statistics. <stats> is
1364 * preallocated array of length <len>. The length of the array
1365 * must be at least ST_F_TOTAL_FIELDS. If this length is less then
1366 * this value, the function returns 0, otherwise, it returns 1.
1367 */
1368int stats_fill_fe_stats(struct proxy *px, struct field *stats, int len)
1369{
1370 if (len < ST_F_TOTAL_FIELDS)
1371 return 0;
1372
1373 memset(stats, 0, sizeof(*stats) * len);
1374
1375 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1376 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, "FRONTEND");
1377 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1378 stats[ST_F_SCUR] = mkf_u32(0, px->feconn);
1379 stats[ST_F_SMAX] = mkf_u32(FN_MAX, px->fe_counters.conn_max);
1380 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, px->maxconn);
1381 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, px->fe_counters.cum_sess);
1382 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, px->fe_counters.bytes_in);
1383 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, px->fe_counters.bytes_out);
1384 stats[ST_F_DREQ] = mkf_u64(FN_COUNTER, px->fe_counters.denied_req);
1385 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, px->fe_counters.denied_resp);
1386 stats[ST_F_EREQ] = mkf_u64(FN_COUNTER, px->fe_counters.failed_req);
1387 stats[ST_F_DCON] = mkf_u64(FN_COUNTER, px->fe_counters.denied_conn);
1388 stats[ST_F_DSES] = mkf_u64(FN_COUNTER, px->fe_counters.denied_sess);
1389 stats[ST_F_STATUS] = mkf_str(FO_STATUS, px->state == PR_STREADY ? "OPEN" : px->state == PR_STFULL ? "FULL" : "STOP");
1390 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
1391 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1392 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, 0);
1393 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_FE);
1394 stats[ST_F_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_sess_per_sec));
1395 stats[ST_F_RATE_LIM] = mkf_u32(FO_CONFIG|FN_LIMIT, px->fe_sps_lim);
1396 stats[ST_F_RATE_MAX] = mkf_u32(FN_MAX, px->fe_counters.sps_max);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001397 stats[ST_F_WREW] = mkf_u64(FN_COUNTER, px->fe_counters.failed_rewrites);
William Lallemand74c24fb2016-11-21 17:18:36 +01001398
1399 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
1400 if (px->mode == PR_MODE_HTTP) {
1401 stats[ST_F_HRSP_1XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[1]);
1402 stats[ST_F_HRSP_2XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[2]);
1403 stats[ST_F_HRSP_3XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[3]);
1404 stats[ST_F_HRSP_4XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[4]);
1405 stats[ST_F_HRSP_5XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[5]);
1406 stats[ST_F_HRSP_OTHER] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[0]);
1407 stats[ST_F_INTERCEPTED] = mkf_u64(FN_COUNTER, px->fe_counters.intercepted_req);
Willy Tarreaua1214a52018-12-14 14:00:25 +01001408 stats[ST_F_CACHE_LOOKUPS] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cache_lookups);
1409 stats[ST_F_CACHE_HITS] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cache_hits);
William Lallemand74c24fb2016-11-21 17:18:36 +01001410 }
1411
1412 /* requests : req_rate, req_rate_max, req_tot, */
1413 stats[ST_F_REQ_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_req_per_sec));
1414 stats[ST_F_REQ_RATE_MAX] = mkf_u32(FN_MAX, px->fe_counters.p.http.rps_max);
1415 stats[ST_F_REQ_TOT] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cum_req);
1416
1417 /* compression: in, out, bypassed, responses */
1418 stats[ST_F_COMP_IN] = mkf_u64(FN_COUNTER, px->fe_counters.comp_in);
1419 stats[ST_F_COMP_OUT] = mkf_u64(FN_COUNTER, px->fe_counters.comp_out);
1420 stats[ST_F_COMP_BYP] = mkf_u64(FN_COUNTER, px->fe_counters.comp_byp);
1421 stats[ST_F_COMP_RSP] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.comp_rsp);
1422
1423 /* connections : conn_rate, conn_rate_max, conn_tot, conn_max */
1424 stats[ST_F_CONN_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_conn_per_sec));
1425 stats[ST_F_CONN_RATE_MAX] = mkf_u32(FN_MAX, px->fe_counters.cps_max);
1426 stats[ST_F_CONN_TOT] = mkf_u64(FN_COUNTER, px->fe_counters.cum_conn);
1427
1428 return 1;
1429}
1430
1431/* Dumps a frontend's line to the trash for the current proxy <px> and uses
1432 * the state from stream interface <si>. The caller is responsible for clearing
1433 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
1434 */
1435static int stats_dump_fe_stats(struct stream_interface *si, struct proxy *px)
1436{
1437 struct appctx *appctx = __objt_appctx(si->end);
1438
1439 if (!(px->cap & PR_CAP_FE))
1440 return 0;
1441
1442 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_FE)))
1443 return 0;
1444
1445 if (!stats_fill_fe_stats(px, stats, ST_F_TOTAL_FIELDS))
1446 return 0;
1447
1448 return stats_dump_one_line(stats, 0, px, appctx);
1449}
1450
1451/* Fill <stats> with the listener statistics. <stats> is
1452 * preallocated array of length <len>. The length of the array
1453 * must be at least ST_F_TOTAL_FIELDS. If this length is less
1454 * then this value, the function returns 0, otherwise, it
1455 * returns 1. <flags> can take the value ST_SHLGNDS.
1456 */
1457int stats_fill_li_stats(struct proxy *px, struct listener *l, int flags,
1458 struct field *stats, int len)
1459{
Willy Tarreau83061a82018-07-13 11:56:34 +02001460 struct buffer *out = get_trash_chunk();
William Lallemand74c24fb2016-11-21 17:18:36 +01001461
1462 if (len < ST_F_TOTAL_FIELDS)
1463 return 0;
1464
1465 if (!l->counters)
1466 return 0;
1467
1468 chunk_reset(out);
1469 memset(stats, 0, sizeof(*stats) * len);
1470
1471 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1472 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, l->name);
1473 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1474 stats[ST_F_SCUR] = mkf_u32(0, l->nbconn);
1475 stats[ST_F_SMAX] = mkf_u32(FN_MAX, l->counters->conn_max);
1476 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, l->maxconn);
1477 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, l->counters->cum_conn);
1478 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, l->counters->bytes_in);
1479 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, l->counters->bytes_out);
1480 stats[ST_F_DREQ] = mkf_u64(FN_COUNTER, l->counters->denied_req);
1481 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, l->counters->denied_resp);
1482 stats[ST_F_EREQ] = mkf_u64(FN_COUNTER, l->counters->failed_req);
1483 stats[ST_F_DCON] = mkf_u64(FN_COUNTER, l->counters->denied_conn);
1484 stats[ST_F_DSES] = mkf_u64(FN_COUNTER, l->counters->denied_sess);
Willy Tarreaua8cf66b2019-02-27 16:49:00 +01001485 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 +01001486 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
1487 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1488 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, l->luid);
1489 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_SO);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001490 stats[ST_F_WREW] = mkf_u64(FN_COUNTER, l->counters->failed_rewrites);
William Lallemand74c24fb2016-11-21 17:18:36 +01001491
1492 if (flags & ST_SHLGNDS) {
1493 char str[INET6_ADDRSTRLEN];
1494 int port;
1495
1496 port = get_host_port(&l->addr);
1497 switch (addr_to_str(&l->addr, str, sizeof(str))) {
1498 case AF_INET:
1499 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1500 chunk_appendf(out, "%s:%d", str, port);
1501 break;
1502 case AF_INET6:
1503 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1504 chunk_appendf(out, "[%s]:%d", str, port);
1505 break;
1506 case AF_UNIX:
1507 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, "unix");
1508 break;
1509 case -1:
1510 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1511 chunk_strcat(out, strerror(errno));
1512 break;
1513 default: /* address family not supported */
1514 break;
1515 }
1516 }
1517
1518 return 1;
1519}
1520
1521/* Dumps a line for listener <l> and proxy <px> to the trash and uses the state
1522 * from stream interface <si>, and stats flags <flags>. The caller is responsible
1523 * for clearing the trash if needed. Returns non-zero if it emits anything, zero
1524 * otherwise.
1525 */
1526static int stats_dump_li_stats(struct stream_interface *si, struct proxy *px, struct listener *l, int flags)
1527{
1528 struct appctx *appctx = __objt_appctx(si->end);
1529
1530 if (!stats_fill_li_stats(px, l, flags, stats, ST_F_TOTAL_FIELDS))
1531 return 0;
1532
1533 return stats_dump_one_line(stats, flags, px, appctx);
1534}
1535
1536enum srv_stats_state {
1537 SRV_STATS_STATE_DOWN = 0,
1538 SRV_STATS_STATE_DOWN_AGENT,
1539 SRV_STATS_STATE_GOING_UP,
1540 SRV_STATS_STATE_UP_GOING_DOWN,
1541 SRV_STATS_STATE_UP,
1542 SRV_STATS_STATE_NOLB_GOING_DOWN,
1543 SRV_STATS_STATE_NOLB,
1544 SRV_STATS_STATE_DRAIN_GOING_DOWN,
1545 SRV_STATS_STATE_DRAIN,
1546 SRV_STATS_STATE_DRAIN_AGENT,
1547 SRV_STATS_STATE_NO_CHECK,
1548
1549 SRV_STATS_STATE_COUNT, /* Must be last */
1550};
1551
1552static const char *srv_hlt_st[SRV_STATS_STATE_COUNT] = {
1553 [SRV_STATS_STATE_DOWN] = "DOWN",
1554 [SRV_STATS_STATE_DOWN_AGENT] = "DOWN (agent)",
1555 [SRV_STATS_STATE_GOING_UP] = "DOWN %d/%d",
1556 [SRV_STATS_STATE_UP_GOING_DOWN] = "UP %d/%d",
1557 [SRV_STATS_STATE_UP] = "UP",
1558 [SRV_STATS_STATE_NOLB_GOING_DOWN] = "NOLB %d/%d",
1559 [SRV_STATS_STATE_NOLB] = "NOLB",
1560 [SRV_STATS_STATE_DRAIN_GOING_DOWN] = "DRAIN %d/%d",
1561 [SRV_STATS_STATE_DRAIN] = "DRAIN",
1562 [SRV_STATS_STATE_DRAIN_AGENT] = "DRAIN (agent)",
1563 [SRV_STATS_STATE_NO_CHECK] = "no check"
1564};
1565
1566/* Fill <stats> with the server statistics. <stats> is
1567 * preallocated array of length <len>. The length of the array
1568 * must be at least ST_F_TOTAL_FIELDS. If this length is less
1569 * then this value, the function returns 0, otherwise, it
1570 * returns 1. <flags> can take the value ST_SHLGNDS.
1571 */
1572int stats_fill_sv_stats(struct proxy *px, struct server *sv, int flags,
1573 struct field *stats, int len)
1574{
1575 struct server *via, *ref;
1576 char str[INET6_ADDRSTRLEN];
Willy Tarreau83061a82018-07-13 11:56:34 +02001577 struct buffer *out = get_trash_chunk();
William Lallemand74c24fb2016-11-21 17:18:36 +01001578 enum srv_stats_state state;
1579 char *fld_status;
1580
1581 if (len < ST_F_TOTAL_FIELDS)
1582 return 0;
1583
1584 memset(stats, 0, sizeof(*stats) * len);
1585
1586 /* we have "via" which is the tracked server as described in the configuration,
1587 * and "ref" which is the checked server and the end of the chain.
1588 */
1589 via = sv->track ? sv->track : sv;
1590 ref = via;
1591 while (ref->track)
1592 ref = ref->track;
1593
Emeric Brun52a91d32017-08-31 14:41:55 +02001594 if (sv->cur_state == SRV_ST_RUNNING || sv->cur_state == SRV_ST_STARTING) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001595 if ((ref->check.state & CHK_ST_ENABLED) &&
1596 (ref->check.health < ref->check.rise + ref->check.fall - 1)) {
1597 state = SRV_STATS_STATE_UP_GOING_DOWN;
1598 } else {
1599 state = SRV_STATS_STATE_UP;
1600 }
1601
Emeric Brun52a91d32017-08-31 14:41:55 +02001602 if (sv->cur_admin & SRV_ADMF_DRAIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001603 if (ref->agent.state & CHK_ST_ENABLED)
1604 state = SRV_STATS_STATE_DRAIN_AGENT;
1605 else if (state == SRV_STATS_STATE_UP_GOING_DOWN)
1606 state = SRV_STATS_STATE_DRAIN_GOING_DOWN;
1607 else
1608 state = SRV_STATS_STATE_DRAIN;
1609 }
1610
1611 if (state == SRV_STATS_STATE_UP && !(ref->check.state & CHK_ST_ENABLED)) {
1612 state = SRV_STATS_STATE_NO_CHECK;
1613 }
1614 }
Emeric Brun52a91d32017-08-31 14:41:55 +02001615 else if (sv->cur_state == SRV_ST_STOPPING) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001616 if ((!(sv->check.state & CHK_ST_ENABLED) && !sv->track) ||
1617 (ref->check.health == ref->check.rise + ref->check.fall - 1)) {
1618 state = SRV_STATS_STATE_NOLB;
1619 } else {
1620 state = SRV_STATS_STATE_NOLB_GOING_DOWN;
1621 }
1622 }
1623 else { /* stopped */
1624 if ((ref->agent.state & CHK_ST_ENABLED) && !ref->agent.health) {
1625 state = SRV_STATS_STATE_DOWN_AGENT;
1626 } else if ((ref->check.state & CHK_ST_ENABLED) && !ref->check.health) {
1627 state = SRV_STATS_STATE_DOWN; /* DOWN */
1628 } else if ((ref->agent.state & CHK_ST_ENABLED) || (ref->check.state & CHK_ST_ENABLED)) {
1629 state = SRV_STATS_STATE_GOING_UP;
1630 } else {
1631 state = SRV_STATS_STATE_DOWN; /* DOWN, unchecked */
1632 }
1633 }
1634
1635 chunk_reset(out);
1636
1637 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1638 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, sv->id);
1639 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1640 stats[ST_F_QCUR] = mkf_u32(0, sv->nbpend);
1641 stats[ST_F_QMAX] = mkf_u32(FN_MAX, sv->counters.nbpend_max);
1642 stats[ST_F_SCUR] = mkf_u32(0, sv->cur_sess);
1643 stats[ST_F_SMAX] = mkf_u32(FN_MAX, sv->counters.cur_sess_max);
1644
1645 if (sv->maxconn)
1646 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, sv->maxconn);
1647
1648 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, sv->counters.cum_sess);
1649 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, sv->counters.bytes_in);
1650 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, sv->counters.bytes_out);
1651 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, sv->counters.failed_secu);
1652 stats[ST_F_ECON] = mkf_u64(FN_COUNTER, sv->counters.failed_conns);
1653 stats[ST_F_ERESP] = mkf_u64(FN_COUNTER, sv->counters.failed_resp);
1654 stats[ST_F_WRETR] = mkf_u64(FN_COUNTER, sv->counters.retries);
1655 stats[ST_F_WREDIS] = mkf_u64(FN_COUNTER, sv->counters.redispatches);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001656 stats[ST_F_WREW] = mkf_u64(FN_COUNTER, sv->counters.failed_rewrites);
Willy Tarreauf1573842018-12-14 11:35:36 +01001657 stats[ST_F_CONNECT] = mkf_u64(FN_COUNTER, sv->counters.connect);
1658 stats[ST_F_REUSE] = mkf_u64(FN_COUNTER, sv->counters.reuse);
William Lallemand74c24fb2016-11-21 17:18:36 +01001659
1660 /* status */
1661 fld_status = chunk_newstr(out);
Emeric Brun52a91d32017-08-31 14:41:55 +02001662 if (sv->cur_admin & SRV_ADMF_RMAINT)
William Lallemand74c24fb2016-11-21 17:18:36 +01001663 chunk_appendf(out, "MAINT (resolution)");
Emeric Brun52a91d32017-08-31 14:41:55 +02001664 else if (sv->cur_admin & SRV_ADMF_IMAINT)
William Lallemand74c24fb2016-11-21 17:18:36 +01001665 chunk_appendf(out, "MAINT (via %s/%s)", via->proxy->id, via->id);
Emeric Brun52a91d32017-08-31 14:41:55 +02001666 else if (sv->cur_admin & SRV_ADMF_MAINT)
William Lallemand74c24fb2016-11-21 17:18:36 +01001667 chunk_appendf(out, "MAINT");
1668 else
1669 chunk_appendf(out,
1670 srv_hlt_st[state],
Emeric Brun52a91d32017-08-31 14:41:55 +02001671 (ref->cur_state != SRV_ST_STOPPED) ? (ref->check.health - ref->check.rise + 1) : (ref->check.health),
1672 (ref->cur_state != SRV_ST_STOPPED) ? (ref->check.fall) : (ref->check.rise));
William Lallemand74c24fb2016-11-21 17:18:36 +01001673
1674 stats[ST_F_STATUS] = mkf_str(FO_STATUS, fld_status);
1675 stats[ST_F_LASTCHG] = mkf_u32(FN_AGE, now.tv_sec - sv->last_change);
Emeric Brun52a91d32017-08-31 14:41:55 +02001676 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 +01001677 stats[ST_F_ACT] = mkf_u32(FO_STATUS, (sv->flags & SRV_F_BACKUP) ? 0 : 1);
1678 stats[ST_F_BCK] = mkf_u32(FO_STATUS, (sv->flags & SRV_F_BACKUP) ? 1 : 0);
1679
1680 /* check failures: unique, fatal; last change, total downtime */
1681 if (sv->check.state & CHK_ST_ENABLED) {
1682 stats[ST_F_CHKFAIL] = mkf_u64(FN_COUNTER, sv->counters.failed_checks);
1683 stats[ST_F_CHKDOWN] = mkf_u64(FN_COUNTER, sv->counters.down_trans);
1684 stats[ST_F_DOWNTIME] = mkf_u32(FN_COUNTER, srv_downtime(sv));
1685 }
1686
1687 if (sv->maxqueue)
1688 stats[ST_F_QLIMIT] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->maxqueue);
1689
1690 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
1691 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1692 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, sv->puid);
1693
Emeric Brun52a91d32017-08-31 14:41:55 +02001694 if (sv->cur_state == SRV_ST_STARTING && !server_is_draining(sv))
William Lallemand74c24fb2016-11-21 17:18:36 +01001695 stats[ST_F_THROTTLE] = mkf_u32(FN_AVG, server_throttle_rate(sv));
1696
1697 stats[ST_F_LBTOT] = mkf_u64(FN_COUNTER, sv->counters.cum_lbconn);
1698
1699 if (sv->track) {
1700 char *fld_track = chunk_newstr(out);
1701
1702 chunk_appendf(out, "%s/%s", sv->track->proxy->id, sv->track->id);
1703 stats[ST_F_TRACKED] = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, fld_track);
1704 }
1705
1706 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_SV);
1707 stats[ST_F_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&sv->sess_per_sec));
1708 stats[ST_F_RATE_MAX] = mkf_u32(FN_MAX, sv->counters.sps_max);
1709
1710 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) {
1711 const char *fld_chksts;
1712
1713 fld_chksts = chunk_newstr(out);
1714 chunk_strcat(out, "* "); // for check in progress
1715 chunk_strcat(out, get_check_status_info(sv->check.status));
1716 if (!(sv->check.state & CHK_ST_INPROGRESS))
1717 fld_chksts += 2; // skip "* "
1718 stats[ST_F_CHECK_STATUS] = mkf_str(FN_OUTPUT, fld_chksts);
1719
1720 if (sv->check.status >= HCHK_STATUS_L57DATA)
1721 stats[ST_F_CHECK_CODE] = mkf_u32(FN_OUTPUT, sv->check.code);
1722
1723 if (sv->check.status >= HCHK_STATUS_CHECKED)
1724 stats[ST_F_CHECK_DURATION] = mkf_u64(FN_DURATION, sv->check.duration);
1725
1726 stats[ST_F_CHECK_DESC] = mkf_str(FN_OUTPUT, get_check_status_description(sv->check.status));
1727 stats[ST_F_LAST_CHK] = mkf_str(FN_OUTPUT, sv->check.desc);
1728 stats[ST_F_CHECK_RISE] = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.rise);
1729 stats[ST_F_CHECK_FALL] = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.fall);
1730 stats[ST_F_CHECK_HEALTH] = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.health);
1731 }
1732
1733 if ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) {
1734 const char *fld_chksts;
1735
1736 fld_chksts = chunk_newstr(out);
1737 chunk_strcat(out, "* "); // for check in progress
1738 chunk_strcat(out, get_check_status_info(sv->agent.status));
1739 if (!(sv->agent.state & CHK_ST_INPROGRESS))
1740 fld_chksts += 2; // skip "* "
1741 stats[ST_F_AGENT_STATUS] = mkf_str(FN_OUTPUT, fld_chksts);
1742
1743 if (sv->agent.status >= HCHK_STATUS_L57DATA)
1744 stats[ST_F_AGENT_CODE] = mkf_u32(FN_OUTPUT, sv->agent.code);
1745
1746 if (sv->agent.status >= HCHK_STATUS_CHECKED)
1747 stats[ST_F_AGENT_DURATION] = mkf_u64(FN_DURATION, sv->agent.duration);
1748
1749 stats[ST_F_AGENT_DESC] = mkf_str(FN_OUTPUT, get_check_status_description(sv->agent.status));
1750 stats[ST_F_LAST_AGT] = mkf_str(FN_OUTPUT, sv->agent.desc);
1751 stats[ST_F_AGENT_RISE] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.rise);
1752 stats[ST_F_AGENT_FALL] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.fall);
1753 stats[ST_F_AGENT_HEALTH] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.health);
1754 }
1755
1756 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
1757 if (px->mode == PR_MODE_HTTP) {
1758 stats[ST_F_HRSP_1XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[1]);
1759 stats[ST_F_HRSP_2XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[2]);
1760 stats[ST_F_HRSP_3XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[3]);
1761 stats[ST_F_HRSP_4XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[4]);
1762 stats[ST_F_HRSP_5XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[5]);
1763 stats[ST_F_HRSP_OTHER] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[0]);
1764 }
1765
1766 if (ref->observe)
1767 stats[ST_F_HANAFAIL] = mkf_u64(FN_COUNTER, sv->counters.failed_hana);
1768
1769 stats[ST_F_CLI_ABRT] = mkf_u64(FN_COUNTER, sv->counters.cli_aborts);
1770 stats[ST_F_SRV_ABRT] = mkf_u64(FN_COUNTER, sv->counters.srv_aborts);
1771 stats[ST_F_LASTSESS] = mkf_s32(FN_AGE, srv_lastsession(sv));
1772
1773 stats[ST_F_QTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.q_time, TIME_STATS_SAMPLES));
1774 stats[ST_F_CTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.c_time, TIME_STATS_SAMPLES));
1775 stats[ST_F_RTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.d_time, TIME_STATS_SAMPLES));
1776 stats[ST_F_TTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.t_time, TIME_STATS_SAMPLES));
1777
1778 if (flags & ST_SHLGNDS) {
1779 switch (addr_to_str(&sv->addr, str, sizeof(str))) {
1780 case AF_INET:
1781 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
Willy Tarreau04276f32017-01-06 17:41:29 +01001782 chunk_appendf(out, "%s:%d", str, sv->svc_port);
William Lallemand74c24fb2016-11-21 17:18:36 +01001783 break;
1784 case AF_INET6:
1785 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
Willy Tarreau04276f32017-01-06 17:41:29 +01001786 chunk_appendf(out, "[%s]:%d", str, sv->svc_port);
William Lallemand74c24fb2016-11-21 17:18:36 +01001787 break;
1788 case AF_UNIX:
1789 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, "unix");
1790 break;
1791 case -1:
1792 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1793 chunk_strcat(out, strerror(errno));
1794 break;
1795 default: /* address family not supported */
1796 break;
1797 }
1798
1799 if (sv->cookie)
1800 stats[ST_F_COOKIE] = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, sv->cookie);
1801 }
1802
1803 return 1;
1804}
1805
1806/* Dumps a line for server <sv> and proxy <px> to the trash and uses the state
1807 * from stream interface <si>, stats flags <flags>, and server state <state>.
1808 * The caller is responsible for clearing the trash if needed. Returns non-zero
1809 * if it emits anything, zero otherwise.
1810 */
1811static int stats_dump_sv_stats(struct stream_interface *si, struct proxy *px, int flags, struct server *sv)
1812{
1813 struct appctx *appctx = __objt_appctx(si->end);
1814
1815 if (!stats_fill_sv_stats(px, sv, flags, stats, ST_F_TOTAL_FIELDS))
1816 return 0;
1817
1818 return stats_dump_one_line(stats, flags, px, appctx);
1819}
1820
1821/* Fill <stats> with the backend statistics. <stats> is
1822 * preallocated array of length <len>. The length of the array
1823 * must be at least ST_F_TOTAL_FIELDS. If this length is less
1824 * then this value, the function returns 0, otherwise, it
1825 * returns 1. <flags> can take the value ST_SHLGNDS.
1826 */
1827int stats_fill_be_stats(struct proxy *px, int flags, struct field *stats, int len)
1828{
1829 if (len < ST_F_TOTAL_FIELDS)
1830 return 0;
1831
1832 memset(stats, 0, sizeof(*stats) * len);
1833
1834 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1835 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, "BACKEND");
1836 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1837 stats[ST_F_QCUR] = mkf_u32(0, px->nbpend);
1838 stats[ST_F_QMAX] = mkf_u32(FN_MAX, px->be_counters.nbpend_max);
Thierry FOURNIER0ff98a42016-12-19 16:50:42 +01001839 stats[ST_F_SCUR] = mkf_u32(0, px->beconn);
William Lallemand74c24fb2016-11-21 17:18:36 +01001840 stats[ST_F_SMAX] = mkf_u32(FN_MAX, px->be_counters.conn_max);
1841 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, px->fullconn);
1842 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, px->be_counters.cum_conn);
1843 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, px->be_counters.bytes_in);
1844 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, px->be_counters.bytes_out);
1845 stats[ST_F_DREQ] = mkf_u64(FN_COUNTER, px->be_counters.denied_req);
1846 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, px->be_counters.denied_resp);
1847 stats[ST_F_ECON] = mkf_u64(FN_COUNTER, px->be_counters.failed_conns);
1848 stats[ST_F_ERESP] = mkf_u64(FN_COUNTER, px->be_counters.failed_resp);
1849 stats[ST_F_WRETR] = mkf_u64(FN_COUNTER, px->be_counters.retries);
1850 stats[ST_F_WREDIS] = mkf_u64(FN_COUNTER, px->be_counters.redispatches);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001851 stats[ST_F_WREW] = mkf_u64(FN_COUNTER, px->be_counters.failed_rewrites);
Willy Tarreauf1573842018-12-14 11:35:36 +01001852 stats[ST_F_CONNECT] = mkf_u64(FN_COUNTER, px->be_counters.connect);
1853 stats[ST_F_REUSE] = mkf_u64(FN_COUNTER, px->be_counters.reuse);
William Lallemand74c24fb2016-11-21 17:18:36 +01001854 stats[ST_F_STATUS] = mkf_str(FO_STATUS, (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" : "DOWN");
1855 stats[ST_F_WEIGHT] = mkf_u32(FN_AVG, (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv);
1856 stats[ST_F_ACT] = mkf_u32(0, px->srv_act);
1857 stats[ST_F_BCK] = mkf_u32(0, px->srv_bck);
1858 stats[ST_F_CHKDOWN] = mkf_u64(FN_COUNTER, px->down_trans);
1859 stats[ST_F_LASTCHG] = mkf_u32(FN_AGE, now.tv_sec - px->last_change);
1860 if (px->srv)
1861 stats[ST_F_DOWNTIME] = mkf_u32(FN_COUNTER, be_downtime(px));
1862
1863 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
1864 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1865 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, 0);
1866 stats[ST_F_LBTOT] = mkf_u64(FN_COUNTER, px->be_counters.cum_lbconn);
1867 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_BE);
1868 stats[ST_F_RATE] = mkf_u32(0, read_freq_ctr(&px->be_sess_per_sec));
1869 stats[ST_F_RATE_MAX] = mkf_u32(0, px->be_counters.sps_max);
1870
1871 if (flags & ST_SHLGNDS) {
1872 if (px->cookie_name)
1873 stats[ST_F_COOKIE] = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, px->cookie_name);
1874 stats[ST_F_ALGO] = mkf_str(FO_CONFIG|FS_SERVICE, backend_lb_algo_str(px->lbprm.algo & BE_LB_ALGO));
1875 }
1876
1877 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
1878 if (px->mode == PR_MODE_HTTP) {
1879 stats[ST_F_REQ_TOT] = mkf_u64(FN_COUNTER, px->be_counters.p.http.cum_req);
1880 stats[ST_F_HRSP_1XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[1]);
1881 stats[ST_F_HRSP_2XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[2]);
1882 stats[ST_F_HRSP_3XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[3]);
1883 stats[ST_F_HRSP_4XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[4]);
1884 stats[ST_F_HRSP_5XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[5]);
1885 stats[ST_F_HRSP_OTHER] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[0]);
Willy Tarreaua1214a52018-12-14 14:00:25 +01001886 stats[ST_F_CACHE_LOOKUPS] = mkf_u64(FN_COUNTER, px->be_counters.p.http.cache_lookups);
1887 stats[ST_F_CACHE_HITS] = mkf_u64(FN_COUNTER, px->be_counters.p.http.cache_hits);
William Lallemand74c24fb2016-11-21 17:18:36 +01001888 }
1889
1890 stats[ST_F_CLI_ABRT] = mkf_u64(FN_COUNTER, px->be_counters.cli_aborts);
1891 stats[ST_F_SRV_ABRT] = mkf_u64(FN_COUNTER, px->be_counters.srv_aborts);
1892
1893 /* compression: in, out, bypassed, responses */
1894 stats[ST_F_COMP_IN] = mkf_u64(FN_COUNTER, px->be_counters.comp_in);
1895 stats[ST_F_COMP_OUT] = mkf_u64(FN_COUNTER, px->be_counters.comp_out);
1896 stats[ST_F_COMP_BYP] = mkf_u64(FN_COUNTER, px->be_counters.comp_byp);
1897 stats[ST_F_COMP_RSP] = mkf_u64(FN_COUNTER, px->be_counters.p.http.comp_rsp);
1898 stats[ST_F_LASTSESS] = mkf_s32(FN_AGE, be_lastsession(px));
1899
1900 stats[ST_F_QTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.q_time, TIME_STATS_SAMPLES));
1901 stats[ST_F_CTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.c_time, TIME_STATS_SAMPLES));
1902 stats[ST_F_RTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.d_time, TIME_STATS_SAMPLES));
1903 stats[ST_F_TTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.t_time, TIME_STATS_SAMPLES));
1904
1905 return 1;
1906}
1907
1908/* Dumps a line for backend <px> to the trash for and uses the state from stream
1909 * interface <si> and stats flags <flags>. The caller is responsible for clearing
1910 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
1911 */
1912static int stats_dump_be_stats(struct stream_interface *si, struct proxy *px, int flags)
1913{
1914 struct appctx *appctx = __objt_appctx(si->end);
1915
1916 if (!(px->cap & PR_CAP_BE))
1917 return 0;
1918
1919 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_BE)))
1920 return 0;
1921
1922 if (!stats_fill_be_stats(px, flags, stats, ST_F_TOTAL_FIELDS))
1923 return 0;
1924
1925 return stats_dump_one_line(stats, flags, px, appctx);
1926}
1927
1928/* Dumps the HTML table header for proxy <px> to the trash for and uses the state from
1929 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
1930 * for clearing the trash if needed.
1931 */
1932static void stats_dump_html_px_hdr(struct stream_interface *si, struct proxy *px, struct uri_auth *uri)
1933{
1934 struct appctx *appctx = __objt_appctx(si->end);
1935 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
1936
1937 if (px->cap & PR_CAP_BE && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
1938 /* A form to enable/disable this proxy servers */
1939
1940 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
1941 scope_txt[0] = 0;
1942 if (appctx->ctx.stats.scope_len) {
Christopher Fauleted7a0662019-01-14 11:07:34 +01001943 const char *scope_ptr = stats_scope_ptr(appctx, si);
1944
William Lallemand74c24fb2016-11-21 17:18:36 +01001945 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Christopher Fauleted7a0662019-01-14 11:07:34 +01001946 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), scope_ptr, appctx->ctx.stats.scope_len);
William Lallemand74c24fb2016-11-21 17:18:36 +01001947 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
1948 }
1949
1950 chunk_appendf(&trash,
1951 "<form method=\"post\">");
1952 }
1953
1954 /* print a new table */
1955 chunk_appendf(&trash,
1956 "<table class=\"tbl\" width=\"100%%\">\n"
1957 "<tr class=\"titre\">"
1958 "<th class=\"pxname\" width=\"10%%\">");
1959
1960 chunk_appendf(&trash,
1961 "<a name=\"%s\"></a>%s"
1962 "<a class=px href=\"#%s\">%s</a>",
1963 px->id,
1964 (uri->flags & ST_SHLGNDS) ? "<u>":"",
1965 px->id, px->id);
1966
1967 if (uri->flags & ST_SHLGNDS) {
1968 /* cap, mode, id */
1969 chunk_appendf(&trash, "<div class=tips>cap: %s, mode: %s, id: %d",
1970 proxy_cap_str(px->cap), proxy_mode_str(px->mode),
1971 px->uuid);
1972 chunk_appendf(&trash, "</div>");
1973 }
1974
1975 chunk_appendf(&trash,
1976 "%s</th>"
1977 "<th class=\"%s\" width=\"90%%\">%s</th>"
1978 "</tr>\n"
1979 "</table>\n"
1980 "<table class=\"tbl\" width=\"100%%\">\n"
1981 "<tr class=\"titre\">",
1982 (uri->flags & ST_SHLGNDS) ? "</u>":"",
1983 px->desc ? "desc" : "empty", px->desc ? px->desc : "");
1984
1985 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
1986 /* Column heading for Enable or Disable server */
David Harrigand3db35a2016-12-30 12:12:49 +00001987 chunk_appendf(&trash,
1988 "<th rowspan=2 width=1><input type=\"checkbox\" \
1989 onclick=\"for(c in document.getElementsByClassName('%s-checkbox')) \
1990 document.getElementsByClassName('%s-checkbox').item(c).checked = this.checked\"></th>",
1991 px->id,
1992 px->id);
William Lallemand74c24fb2016-11-21 17:18:36 +01001993 }
1994
1995 chunk_appendf(&trash,
1996 "<th rowspan=2></th>"
1997 "<th colspan=3>Queue</th>"
1998 "<th colspan=3>Session rate</th><th colspan=6>Sessions</th>"
1999 "<th colspan=2>Bytes</th><th colspan=2>Denied</th>"
2000 "<th colspan=3>Errors</th><th colspan=2>Warnings</th>"
2001 "<th colspan=9>Server</th>"
2002 "</tr>\n"
2003 "<tr class=\"titre\">"
2004 "<th>Cur</th><th>Max</th><th>Limit</th>"
2005 "<th>Cur</th><th>Max</th><th>Limit</th><th>Cur</th><th>Max</th>"
2006 "<th>Limit</th><th>Total</th><th>LbTot</th><th>Last</th><th>In</th><th>Out</th>"
2007 "<th>Req</th><th>Resp</th><th>Req</th><th>Conn</th>"
2008 "<th>Resp</th><th>Retr</th><th>Redis</th>"
2009 "<th>Status</th><th>LastChk</th><th>Wght</th><th>Act</th>"
2010 "<th>Bck</th><th>Chk</th><th>Dwn</th><th>Dwntme</th>"
2011 "<th>Thrtle</th>\n"
2012 "</tr>");
2013}
2014
2015/* Dumps the HTML table trailer for proxy <px> to the trash for and uses the state from
2016 * stream interface <si>. The caller is responsible for clearing the trash if needed.
2017 */
2018static void stats_dump_html_px_end(struct stream_interface *si, struct proxy *px)
2019{
2020 struct appctx *appctx = __objt_appctx(si->end);
2021 chunk_appendf(&trash, "</table>");
2022
2023 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
2024 /* close the form used to enable/disable this proxy servers */
2025 chunk_appendf(&trash,
2026 "Choose the action to perform on the checked servers : "
2027 "<select name=action>"
2028 "<option value=\"\"></option>"
2029 "<option value=\"ready\">Set state to READY</option>"
2030 "<option value=\"drain\">Set state to DRAIN</option>"
2031 "<option value=\"maint\">Set state to MAINT</option>"
2032 "<option value=\"dhlth\">Health: disable checks</option>"
2033 "<option value=\"ehlth\">Health: enable checks</option>"
2034 "<option value=\"hrunn\">Health: force UP</option>"
2035 "<option value=\"hnolb\">Health: force NOLB</option>"
2036 "<option value=\"hdown\">Health: force DOWN</option>"
2037 "<option value=\"dagent\">Agent: disable checks</option>"
2038 "<option value=\"eagent\">Agent: enable checks</option>"
2039 "<option value=\"arunn\">Agent: force UP</option>"
2040 "<option value=\"adown\">Agent: force DOWN</option>"
2041 "<option value=\"shutdown\">Kill Sessions</option>"
2042 "</select>"
2043 "<input type=\"hidden\" name=\"b\" value=\"#%d\">"
2044 "&nbsp;<input type=\"submit\" value=\"Apply\">"
2045 "</form>",
2046 px->uuid);
2047 }
2048
2049 chunk_appendf(&trash, "<p>\n");
2050}
2051
2052/*
2053 * Dumps statistics for a proxy. The output is sent to the stream interface's
2054 * input buffer. Returns 0 if it had to stop dumping data because of lack of
2055 * buffer space, or non-zero if everything completed. This function is used
2056 * both by the CLI and the HTTP entry points, and is able to dump the output
2057 * in HTML or CSV formats. If the later, <uri> must be NULL.
2058 */
Christopher Fauletef779222018-10-31 08:47:01 +01002059int stats_dump_proxy_to_buffer(struct stream_interface *si, struct htx *htx,
2060 struct proxy *px, struct uri_auth *uri)
William Lallemand74c24fb2016-11-21 17:18:36 +01002061{
2062 struct appctx *appctx = __objt_appctx(si->end);
2063 struct stream *s = si_strm(si);
2064 struct channel *rep = si_ic(si);
2065 struct server *sv, *svs; /* server and server-state, server-state=server or server->track */
2066 struct listener *l;
2067 unsigned int flags;
2068
2069 if (uri)
2070 flags = uri->flags;
William Lallemand07a62f72017-05-24 00:57:40 +02002071 else if ((strm_li(s)->bind_conf->level & ACCESS_LVL_MASK) >= ACCESS_LVL_OPER)
William Lallemand74c24fb2016-11-21 17:18:36 +01002072 flags = ST_SHLGNDS | ST_SHNODE | ST_SHDESC;
2073 else
2074 flags = ST_SHNODE | ST_SHDESC;
2075
2076 chunk_reset(&trash);
2077
2078 switch (appctx->ctx.stats.px_st) {
2079 case STAT_PX_ST_INIT:
2080 /* we are on a new proxy */
2081 if (uri && uri->scope) {
2082 /* we have a limited scope, we have to check the proxy name */
2083 struct stat_scope *scope;
2084 int len;
2085
2086 len = strlen(px->id);
2087 scope = uri->scope;
2088
2089 while (scope) {
2090 /* match exact proxy name */
2091 if (scope->px_len == len && !memcmp(px->id, scope->px_id, len))
2092 break;
2093
2094 /* match '.' which means 'self' proxy */
2095 if (!strcmp(scope->px_id, ".") && px == s->be)
2096 break;
2097 scope = scope->next;
2098 }
2099
2100 /* proxy name not found : don't dump anything */
2101 if (scope == NULL)
2102 return 1;
2103 }
2104
2105 /* if the user has requested a limited output and the proxy
2106 * name does not match, skip it.
2107 */
Christopher Fauleted7a0662019-01-14 11:07:34 +01002108 if (appctx->ctx.stats.scope_len) {
2109 const char *scope_ptr = stats_scope_ptr(appctx, si);
2110
2111 if (strnistr(px->id, strlen(px->id), scope_ptr, appctx->ctx.stats.scope_len) == NULL)
2112 return 1;
2113 }
William Lallemand74c24fb2016-11-21 17:18:36 +01002114
2115 if ((appctx->ctx.stats.flags & STAT_BOUND) &&
2116 (appctx->ctx.stats.iid != -1) &&
2117 (px->uuid != appctx->ctx.stats.iid))
2118 return 1;
2119
2120 appctx->ctx.stats.px_st = STAT_PX_ST_TH;
2121 /* fall through */
2122
2123 case STAT_PX_ST_TH:
2124 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
2125 stats_dump_html_px_hdr(si, px, uri);
Christopher Fauletef779222018-10-31 08:47:01 +01002126 if (!stats_putchk(rep, htx, &trash))
2127 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002128 }
2129
2130 appctx->ctx.stats.px_st = STAT_PX_ST_FE;
2131 /* fall through */
2132
2133 case STAT_PX_ST_FE:
2134 /* print the frontend */
2135 if (stats_dump_fe_stats(si, px)) {
Christopher Fauletef779222018-10-31 08:47:01 +01002136 if (!stats_putchk(rep, htx, &trash))
2137 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002138 }
2139
2140 appctx->ctx.stats.l = px->conf.listeners.n;
2141 appctx->ctx.stats.px_st = STAT_PX_ST_LI;
2142 /* fall through */
2143
2144 case STAT_PX_ST_LI:
2145 /* stats.l has been initialized above */
2146 for (; appctx->ctx.stats.l != &px->conf.listeners; appctx->ctx.stats.l = l->by_fe.n) {
Christopher Fauletef779222018-10-31 08:47:01 +01002147 if (htx) {
2148 if (htx_almost_full(htx))
2149 goto full;
2150 }
2151 else {
2152 if (buffer_almost_full(&rep->buf))
2153 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002154 }
2155
2156 l = LIST_ELEM(appctx->ctx.stats.l, struct listener *, by_fe);
2157 if (!l->counters)
2158 continue;
2159
2160 if (appctx->ctx.stats.flags & STAT_BOUND) {
2161 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SO)))
2162 break;
2163
2164 if (appctx->ctx.stats.sid != -1 && l->luid != appctx->ctx.stats.sid)
2165 continue;
2166 }
2167
2168 /* print the frontend */
2169 if (stats_dump_li_stats(si, px, l, flags)) {
Christopher Fauletef779222018-10-31 08:47:01 +01002170 if (!stats_putchk(rep, htx, &trash))
2171 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002172 }
2173 }
2174
2175 appctx->ctx.stats.sv = px->srv; /* may be NULL */
2176 appctx->ctx.stats.px_st = STAT_PX_ST_SV;
2177 /* fall through */
2178
2179 case STAT_PX_ST_SV:
2180 /* stats.sv has been initialized above */
2181 for (; appctx->ctx.stats.sv != NULL; appctx->ctx.stats.sv = sv->next) {
Christopher Fauletef779222018-10-31 08:47:01 +01002182 if (htx) {
2183 if (htx_almost_full(htx))
2184 goto full;
2185 }
2186 else {
2187 if (buffer_almost_full(&rep->buf))
2188 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002189 }
2190
2191 sv = appctx->ctx.stats.sv;
2192
2193 if (appctx->ctx.stats.flags & STAT_BOUND) {
2194 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SV)))
2195 break;
2196
2197 if (appctx->ctx.stats.sid != -1 && sv->puid != appctx->ctx.stats.sid)
2198 continue;
2199 }
2200
2201 svs = sv;
2202 while (svs->track)
2203 svs = svs->track;
2204
2205 /* do not report servers which are DOWN and not changing state */
2206 if ((appctx->ctx.stats.flags & STAT_HIDE_DOWN) &&
Emeric Brun52a91d32017-08-31 14:41:55 +02002207 ((sv->cur_admin & SRV_ADMF_MAINT) || /* server is in maintenance */
2208 (sv->cur_state == SRV_ST_STOPPED && /* server is down */
William Lallemand74c24fb2016-11-21 17:18:36 +01002209 (!((svs->agent.state | svs->check.state) & CHK_ST_ENABLED) ||
2210 ((svs->agent.state & CHK_ST_ENABLED) && !svs->agent.health) ||
2211 ((svs->check.state & CHK_ST_ENABLED) && !svs->check.health))))) {
2212 continue;
2213 }
2214
2215 if (stats_dump_sv_stats(si, px, flags, sv)) {
Christopher Fauletef779222018-10-31 08:47:01 +01002216 if (!stats_putchk(rep, htx, &trash))
2217 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002218 }
2219 } /* for sv */
2220
2221 appctx->ctx.stats.px_st = STAT_PX_ST_BE;
2222 /* fall through */
2223
2224 case STAT_PX_ST_BE:
2225 /* print the backend */
2226 if (stats_dump_be_stats(si, px, flags)) {
Christopher Fauletef779222018-10-31 08:47:01 +01002227 if (!stats_putchk(rep, htx, &trash))
2228 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002229 }
2230
2231 appctx->ctx.stats.px_st = STAT_PX_ST_END;
2232 /* fall through */
2233
2234 case STAT_PX_ST_END:
2235 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
2236 stats_dump_html_px_end(si, px);
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
2241 appctx->ctx.stats.px_st = STAT_PX_ST_FIN;
2242 /* fall through */
2243
2244 case STAT_PX_ST_FIN:
2245 return 1;
2246
2247 default:
2248 /* unknown state, we should put an abort() here ! */
2249 return 1;
2250 }
Christopher Fauletef779222018-10-31 08:47:01 +01002251
2252 full:
2253 si_rx_room_blk(si);
2254 return 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01002255}
2256
2257/* Dumps the HTTP stats head block to the trash for and uses the per-uri
2258 * parameters <uri>. The caller is responsible for clearing the trash if needed.
2259 */
2260static void stats_dump_html_head(struct uri_auth *uri)
2261{
2262 /* WARNING! This must fit in the first buffer !!! */
2263 chunk_appendf(&trash,
2264 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n"
2265 "\"http://www.w3.org/TR/html4/loose.dtd\">\n"
2266 "<html><head><title>Statistics Report for " PRODUCT_NAME "%s%s</title>\n"
2267 "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n"
2268 "<style type=\"text/css\"><!--\n"
2269 "body {"
2270 " font-family: arial, helvetica, sans-serif;"
2271 " font-size: 12px;"
2272 " font-weight: normal;"
2273 " color: black;"
2274 " background: white;"
2275 "}\n"
2276 "th,td {"
2277 " font-size: 10px;"
2278 "}\n"
2279 "h1 {"
2280 " font-size: x-large;"
2281 " margin-bottom: 0.5em;"
2282 "}\n"
2283 "h2 {"
2284 " font-family: helvetica, arial;"
2285 " font-size: x-large;"
2286 " font-weight: bold;"
2287 " font-style: italic;"
2288 " color: #6020a0;"
2289 " margin-top: 0em;"
2290 " margin-bottom: 0em;"
2291 "}\n"
2292 "h3 {"
2293 " font-family: helvetica, arial;"
2294 " font-size: 16px;"
2295 " font-weight: bold;"
2296 " color: #b00040;"
2297 " background: #e8e8d0;"
2298 " margin-top: 0em;"
2299 " margin-bottom: 0em;"
2300 "}\n"
2301 "li {"
2302 " margin-top: 0.25em;"
2303 " margin-right: 2em;"
2304 "}\n"
2305 ".hr {margin-top: 0.25em;"
2306 " border-color: black;"
2307 " border-bottom-style: solid;"
2308 "}\n"
2309 ".titre {background: #20D0D0;color: #000000; font-weight: bold; text-align: center;}\n"
2310 ".total {background: #20D0D0;color: #ffff80;}\n"
2311 ".frontend {background: #e8e8d0;}\n"
2312 ".socket {background: #d0d0d0;}\n"
2313 ".backend {background: #e8e8d0;}\n"
2314 ".active_down {background: #ff9090;}\n"
2315 ".active_going_up {background: #ffd020;}\n"
2316 ".active_going_down {background: #ffffa0;}\n"
2317 ".active_up {background: #c0ffc0;}\n"
2318 ".active_nolb {background: #20a0ff;}\n"
2319 ".active_draining {background: #20a0FF;}\n"
2320 ".active_no_check {background: #e0e0e0;}\n"
2321 ".backup_down {background: #ff9090;}\n"
2322 ".backup_going_up {background: #ff80ff;}\n"
2323 ".backup_going_down {background: #c060ff;}\n"
2324 ".backup_up {background: #b0d0ff;}\n"
2325 ".backup_nolb {background: #90b0e0;}\n"
2326 ".backup_draining {background: #cc9900;}\n"
2327 ".backup_no_check {background: #e0e0e0;}\n"
2328 ".maintain {background: #c07820;}\n"
2329 ".rls {letter-spacing: 0.2em; margin-right: 1px;}\n" /* right letter spacing (used for grouping digits) */
2330 "\n"
2331 "a.px:link {color: #ffff40; text-decoration: none;}"
2332 "a.px:visited {color: #ffff40; text-decoration: none;}"
2333 "a.px:hover {color: #ffffff; text-decoration: none;}"
2334 "a.lfsb:link {color: #000000; text-decoration: none;}"
2335 "a.lfsb:visited {color: #000000; text-decoration: none;}"
2336 "a.lfsb:hover {color: #505050; text-decoration: none;}"
2337 "\n"
2338 "table.tbl { border-collapse: collapse; border-style: none;}\n"
2339 "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"
2340 "table.tbl td.ac { text-align: center;}\n"
2341 "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}\n"
2342 "table.tbl th.pxname { background: #b00040; color: #ffff40; font-weight: bold; border-style: solid solid none solid; padding: 2px 3px; white-space: nowrap;}\n"
2343 "table.tbl th.empty { border-style: none; empty-cells: hide; background: white;}\n"
2344 "table.tbl th.desc { background: white; border-style: solid solid none solid; text-align: left; padding: 2px 3px;}\n"
2345 "\n"
2346 "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n"
2347 "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n"
2348 "table.lgd td.noborder { border-style: none; padding: 2px; white-space: nowrap;}\n"
2349 "table.det { border-collapse: collapse; border-style: none; }\n"
2350 "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"
2351 "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"
2352 "u {text-decoration:none; border-bottom: 1px dotted black;}\n"
2353 "div.tips {\n"
2354 " display:block;\n"
2355 " visibility:hidden;\n"
2356 " z-index:2147483647;\n"
2357 " position:absolute;\n"
2358 " padding:2px 4px 3px;\n"
2359 " background:#f0f060; color:#000000;\n"
2360 " border:1px solid #7040c0;\n"
2361 " white-space:nowrap;\n"
2362 " font-style:normal;font-size:11px;font-weight:normal;\n"
2363 " -moz-border-radius:3px;-webkit-border-radius:3px;border-radius:3px;\n"
2364 " -moz-box-shadow:gray 2px 2px 3px;-webkit-box-shadow:gray 2px 2px 3px;box-shadow:gray 2px 2px 3px;\n"
2365 "}\n"
2366 "u:hover div.tips {visibility:visible;}\n"
2367 "-->\n"
2368 "</style></head>\n",
2369 (uri->flags & ST_SHNODE) ? " on " : "",
2370 (uri->flags & ST_SHNODE) ? (uri->node ? uri->node : global.node) : ""
2371 );
2372}
2373
2374/* Dumps the HTML stats information block to the trash for and uses the state from
2375 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
2376 * for clearing the trash if needed.
2377 */
2378static void stats_dump_html_info(struct stream_interface *si, struct uri_auth *uri)
2379{
2380 struct appctx *appctx = __objt_appctx(si->end);
2381 unsigned int up = (now.tv_sec - start_date.tv_sec);
2382 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
Christopher Fauleted7a0662019-01-14 11:07:34 +01002383 const char *scope_ptr = stats_scope_ptr(appctx, si);
William Lallemand74c24fb2016-11-21 17:18:36 +01002384
2385 /* WARNING! this has to fit the first packet too.
2386 * We are around 3.5 kB, add adding entries will
2387 * become tricky if we want to support 4kB buffers !
2388 */
2389 chunk_appendf(&trash,
2390 "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">"
2391 PRODUCT_NAME "%s</a></h1>\n"
2392 "<h2>Statistics Report for pid %d%s%s%s%s</h2>\n"
2393 "<hr width=\"100%%\" class=\"hr\">\n"
2394 "<h3>&gt; General process information</h3>\n"
2395 "<table border=0><tr><td align=\"left\" nowrap width=\"1%%\">\n"
Yves Lafon95317282018-02-26 11:10:37 +01002396 "<p><b>pid = </b> %d (process #%d, nbproc = %d, nbthread = %d)<br>\n"
William Lallemand74c24fb2016-11-21 17:18:36 +01002397 "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n"
2398 "<b>system limits:</b> memmax = %s%s; ulimit-n = %d<br>\n"
2399 "<b>maxsock = </b> %d; <b>maxconn = </b> %d; <b>maxpipes = </b> %d<br>\n"
2400 "current conns = %d; current pipes = %d/%d; conn rate = %d/sec<br>\n"
2401 "Running tasks: %d/%d; idle = %d %%<br>\n"
2402 "</td><td align=\"center\" nowrap>\n"
2403 "<table class=\"lgd\"><tr>\n"
2404 "<td class=\"active_up\">&nbsp;</td><td class=\"noborder\">active UP </td>"
2405 "<td class=\"backup_up\">&nbsp;</td><td class=\"noborder\">backup UP </td>"
2406 "</tr><tr>\n"
2407 "<td class=\"active_going_down\"></td><td class=\"noborder\">active UP, going down </td>"
2408 "<td class=\"backup_going_down\"></td><td class=\"noborder\">backup UP, going down </td>"
2409 "</tr><tr>\n"
2410 "<td class=\"active_going_up\"></td><td class=\"noborder\">active DOWN, going up </td>"
2411 "<td class=\"backup_going_up\"></td><td class=\"noborder\">backup DOWN, going up </td>"
2412 "</tr><tr>\n"
2413 "<td class=\"active_down\"></td><td class=\"noborder\">active or backup DOWN &nbsp;</td>"
2414 "<td class=\"active_no_check\"></td><td class=\"noborder\">not checked </td>"
2415 "</tr><tr>\n"
2416 "<td class=\"maintain\"></td><td class=\"noborder\" colspan=\"3\">active or backup DOWN for maintenance (MAINT) &nbsp;</td>"
2417 "</tr><tr>\n"
2418 "<td class=\"active_draining\"></td><td class=\"noborder\" colspan=\"3\">active or backup SOFT STOPPED for maintenance &nbsp;</td>"
2419 "</tr></table>\n"
2420 "Note: \"NOLB\"/\"DRAIN\" = UP with load-balancing disabled."
2421 "</td>"
2422 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
2423 "<b>Display option:</b><ul style=\"margin-top: 0.25em;\">"
2424 "",
Willy Tarreau909b9d82019-01-04 18:20:32 +01002425 (uri->flags & ST_HIDEVER) ? "" : (stats_version_string),
William Lallemand74c24fb2016-11-21 17:18:36 +01002426 pid, (uri->flags & ST_SHNODE) ? " on " : "",
2427 (uri->flags & ST_SHNODE) ? (uri->node ? uri->node : global.node) : "",
2428 (uri->flags & ST_SHDESC) ? ": " : "",
2429 (uri->flags & ST_SHDESC) ? (uri->desc ? uri->desc : global.desc) : "",
Yves Lafon95317282018-02-26 11:10:37 +01002430 pid, relative_pid, global.nbproc, global.nbthread,
William Lallemand74c24fb2016-11-21 17:18:36 +01002431 up / 86400, (up % 86400) / 3600,
2432 (up % 3600) / 60, (up % 60),
2433 global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited",
2434 global.rlimit_memmax ? " MB" : "",
2435 global.rlimit_nofile,
2436 global.maxsock, global.maxconn, global.maxpipes,
2437 actconn, pipes_used, pipes_used+pipes_free, read_freq_ctr(&global.conn_per_sec),
Christopher Faulet34c5cc92016-12-06 09:15:30 +01002438 tasks_run_queue_cur, nb_tasks_cur, idle_pct
William Lallemand74c24fb2016-11-21 17:18:36 +01002439 );
2440
2441 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Christopher Fauleted7a0662019-01-14 11:07:34 +01002442 memcpy(scope_txt, scope_ptr, appctx->ctx.stats.scope_len);
William Lallemand74c24fb2016-11-21 17:18:36 +01002443 scope_txt[appctx->ctx.stats.scope_len] = '\0';
2444
2445 chunk_appendf(&trash,
2446 "<li><form method=\"GET\">Scope : <input value=\"%s\" name=\"" STAT_SCOPE_INPUT_NAME "\" size=\"8\" maxlength=\"%d\" tabindex=\"1\"/></form>\n",
2447 (appctx->ctx.stats.scope_len > 0) ? scope_txt : "",
2448 STAT_SCOPE_TXT_MAXLEN);
2449
2450 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
2451 scope_txt[0] = 0;
2452 if (appctx->ctx.stats.scope_len) {
2453 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Christopher Fauleted7a0662019-01-14 11:07:34 +01002454 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), scope_ptr, appctx->ctx.stats.scope_len);
William Lallemand74c24fb2016-11-21 17:18:36 +01002455 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
2456 }
2457
2458 if (appctx->ctx.stats.flags & STAT_HIDE_DOWN)
2459 chunk_appendf(&trash,
2460 "<li><a href=\"%s%s%s%s\">Show all servers</a><br>\n",
2461 uri->uri_prefix,
2462 "",
2463 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2464 scope_txt);
2465 else
2466 chunk_appendf(&trash,
2467 "<li><a href=\"%s%s%s%s\">Hide 'DOWN' servers</a><br>\n",
2468 uri->uri_prefix,
2469 ";up",
2470 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2471 scope_txt);
2472
2473 if (uri->refresh > 0) {
2474 if (appctx->ctx.stats.flags & STAT_NO_REFRESH)
2475 chunk_appendf(&trash,
2476 "<li><a href=\"%s%s%s%s\">Enable refresh</a><br>\n",
2477 uri->uri_prefix,
2478 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2479 "",
2480 scope_txt);
2481 else
2482 chunk_appendf(&trash,
2483 "<li><a href=\"%s%s%s%s\">Disable refresh</a><br>\n",
2484 uri->uri_prefix,
2485 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2486 ";norefresh",
2487 scope_txt);
2488 }
2489
2490 chunk_appendf(&trash,
2491 "<li><a href=\"%s%s%s%s\">Refresh now</a><br>\n",
2492 uri->uri_prefix,
2493 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2494 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2495 scope_txt);
2496
2497 chunk_appendf(&trash,
2498 "<li><a href=\"%s;csv%s%s\">CSV export</a><br>\n",
2499 uri->uri_prefix,
2500 (uri->refresh > 0) ? ";norefresh" : "",
2501 scope_txt);
2502
2503 chunk_appendf(&trash,
2504 "</ul></td>"
2505 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
2506 "<b>External resources:</b><ul style=\"margin-top: 0.25em;\">\n"
2507 "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>\n"
2508 "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>\n"
2509 "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>\n"
2510 "</ul>"
2511 "</td>"
2512 "</tr></table>\n"
2513 ""
2514 );
2515
2516 if (appctx->ctx.stats.st_code) {
2517 switch (appctx->ctx.stats.st_code) {
2518 case STAT_STATUS_DONE:
2519 chunk_appendf(&trash,
2520 "<p><div class=active_up>"
2521 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2522 "Action processed successfully."
2523 "</div>\n", uri->uri_prefix,
2524 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2525 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2526 scope_txt);
2527 break;
2528 case STAT_STATUS_NONE:
2529 chunk_appendf(&trash,
2530 "<p><div class=active_going_down>"
2531 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2532 "Nothing has changed."
2533 "</div>\n", uri->uri_prefix,
2534 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2535 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2536 scope_txt);
2537 break;
2538 case STAT_STATUS_PART:
2539 chunk_appendf(&trash,
2540 "<p><div class=active_going_down>"
2541 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2542 "Action partially processed.<br>"
2543 "Some server names are probably unknown or ambiguous (duplicated names in the backend)."
2544 "</div>\n", uri->uri_prefix,
2545 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2546 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2547 scope_txt);
2548 break;
2549 case STAT_STATUS_ERRP:
2550 chunk_appendf(&trash,
2551 "<p><div class=active_down>"
2552 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2553 "Action not processed because of invalid parameters."
2554 "<ul>"
2555 "<li>The action is maybe unknown.</li>"
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01002556 "<li>Invalid key parameter (empty or too long).</li>"
William Lallemand74c24fb2016-11-21 17:18:36 +01002557 "<li>The backend name is probably unknown or ambiguous (duplicated names).</li>"
2558 "<li>Some server names are probably unknown or ambiguous (duplicated names in the backend).</li>"
2559 "</ul>"
2560 "</div>\n", uri->uri_prefix,
2561 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2562 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2563 scope_txt);
2564 break;
2565 case STAT_STATUS_EXCD:
2566 chunk_appendf(&trash,
2567 "<p><div class=active_down>"
2568 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2569 "<b>Action not processed : the buffer couldn't store all the data.<br>"
2570 "You should retry with less servers at a time.</b>"
2571 "</div>\n", uri->uri_prefix,
2572 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2573 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2574 scope_txt);
2575 break;
2576 case STAT_STATUS_DENY:
2577 chunk_appendf(&trash,
2578 "<p><div class=active_down>"
2579 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2580 "<b>Action denied.</b>"
2581 "</div>\n", uri->uri_prefix,
2582 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2583 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2584 scope_txt);
2585 break;
Christopher Faulet3c2ecf72019-02-27 16:41:27 +01002586 case STAT_STATUS_IVAL:
2587 chunk_appendf(&trash,
2588 "<p><div class=active_down>"
2589 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2590 "<b>Invalid requests (unsupported method or chunked encoded request).</b>"
2591 "</div>\n", uri->uri_prefix,
2592 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2593 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2594 scope_txt);
2595 break;
William Lallemand74c24fb2016-11-21 17:18:36 +01002596 default:
2597 chunk_appendf(&trash,
2598 "<p><div class=active_no_check>"
2599 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2600 "Unexpected result."
2601 "</div>\n", uri->uri_prefix,
2602 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2603 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2604 scope_txt);
2605 }
2606 chunk_appendf(&trash, "<p>\n");
2607 }
2608}
2609
2610/* Dumps the HTML stats trailer block to the trash. The caller is responsible
2611 * for clearing the trash if needed.
2612 */
2613static void stats_dump_html_end()
2614{
2615 chunk_appendf(&trash, "</body></html>\n");
2616}
2617
Simon Horman05ee2132017-01-04 09:37:25 +01002618/* Dumps the stats JSON header to the trash buffer which. The caller is responsible
2619 * for clearing it if needed.
2620 */
2621static void stats_dump_json_header()
2622{
2623 chunk_strcat(&trash, "[");
2624}
2625
2626
2627/* Dumps the JSON stats trailer block to the trash. The caller is responsible
2628 * for clearing the trash if needed.
2629 */
2630static void stats_dump_json_end()
2631{
2632 chunk_strcat(&trash, "]");
2633}
2634
William Lallemand74c24fb2016-11-21 17:18:36 +01002635/* This function dumps statistics onto the stream interface's read buffer in
2636 * either CSV or HTML format. <uri> contains some HTML-specific parameters that
2637 * are ignored for CSV format (hence <uri> may be NULL there). It returns 0 if
2638 * it had to stop writing data and an I/O is needed, 1 if the dump is finished
2639 * and the stream must be closed, or -1 in case of any error. This function is
2640 * used by both the CLI and the HTTP handlers.
2641 */
Christopher Fauletef779222018-10-31 08:47:01 +01002642static int stats_dump_stat_to_buffer(struct stream_interface *si, struct htx *htx,
2643 struct uri_auth *uri)
William Lallemand74c24fb2016-11-21 17:18:36 +01002644{
2645 struct appctx *appctx = __objt_appctx(si->end);
2646 struct channel *rep = si_ic(si);
2647 struct proxy *px;
2648
2649 chunk_reset(&trash);
2650
2651 switch (appctx->st2) {
2652 case STAT_ST_INIT:
2653 appctx->st2 = STAT_ST_HEAD; /* let's start producing data */
2654 /* fall through */
2655
2656 case STAT_ST_HEAD:
2657 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
2658 stats_dump_html_head(uri);
Simon Horman05ee2132017-01-04 09:37:25 +01002659 else if (appctx->ctx.stats.flags & STAT_FMT_JSON)
Willy Tarreau9d7fb632017-04-11 07:53:04 +02002660 stats_dump_json_header();
William Lallemand74c24fb2016-11-21 17:18:36 +01002661 else if (!(appctx->ctx.stats.flags & STAT_FMT_TYPED))
2662 stats_dump_csv_header();
2663
Christopher Fauletef779222018-10-31 08:47:01 +01002664 if (!stats_putchk(rep, htx, &trash))
2665 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002666
2667 appctx->st2 = STAT_ST_INFO;
2668 /* fall through */
2669
2670 case STAT_ST_INFO:
2671 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
2672 stats_dump_html_info(si, uri);
Christopher Fauletef779222018-10-31 08:47:01 +01002673 if (!stats_putchk(rep, htx, &trash))
2674 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002675 }
2676
Olivier Houchardfbc74e82017-11-24 16:54:05 +01002677 appctx->ctx.stats.px = proxies_list;
William Lallemand74c24fb2016-11-21 17:18:36 +01002678 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
2679 appctx->st2 = STAT_ST_LIST;
2680 /* fall through */
2681
2682 case STAT_ST_LIST:
2683 /* dump proxies */
2684 while (appctx->ctx.stats.px) {
Christopher Fauletef779222018-10-31 08:47:01 +01002685 if (htx) {
2686 if (htx_almost_full(htx))
2687 goto full;
2688 }
2689 else {
2690 if (buffer_almost_full(&rep->buf))
2691 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002692 }
2693
2694 px = appctx->ctx.stats.px;
2695 /* skip the disabled proxies, global frontend and non-networked ones */
2696 if (px->state != PR_STSTOPPED && px->uuid > 0 && (px->cap & (PR_CAP_FE | PR_CAP_BE)))
Christopher Fauletef779222018-10-31 08:47:01 +01002697 if (stats_dump_proxy_to_buffer(si, htx, px, uri) == 0)
William Lallemand74c24fb2016-11-21 17:18:36 +01002698 return 0;
2699
2700 appctx->ctx.stats.px = px->next;
2701 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
2702 }
2703 /* here, we just have reached the last proxy */
2704
2705 appctx->st2 = STAT_ST_END;
2706 /* fall through */
2707
2708 case STAT_ST_END:
Simon Horman05ee2132017-01-04 09:37:25 +01002709 if (appctx->ctx.stats.flags & (STAT_FMT_HTML|STAT_FMT_JSON)) {
2710 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
2711 stats_dump_html_end();
2712 else
2713 stats_dump_json_end();
Christopher Fauletef779222018-10-31 08:47:01 +01002714 if (!stats_putchk(rep, htx, &trash))
2715 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002716 }
2717
2718 appctx->st2 = STAT_ST_FIN;
2719 /* fall through */
2720
2721 case STAT_ST_FIN:
2722 return 1;
2723
2724 default:
2725 /* unknown state ! */
2726 appctx->st2 = STAT_ST_FIN;
2727 return -1;
2728 }
Christopher Fauletef779222018-10-31 08:47:01 +01002729
2730 full:
2731 si_rx_room_blk(si);
2732 return 0;
2733
William Lallemand74c24fb2016-11-21 17:18:36 +01002734}
2735
2736/* We reached the stats page through a POST request. The appctx is
2737 * expected to have already been allocated by the caller.
2738 * Parse the posted data and enable/disable servers if necessary.
2739 * Returns 1 if request was parsed or zero if it needs more data.
2740 */
2741static int stats_process_http_post(struct stream_interface *si)
2742{
2743 struct stream *s = si_strm(si);
2744 struct appctx *appctx = objt_appctx(si->end);
2745
2746 struct proxy *px = NULL;
2747 struct server *sv = NULL;
2748
2749 char key[LINESIZE];
2750 int action = ST_ADM_ACTION_NONE;
2751 int reprocess = 0;
2752
2753 int total_servers = 0;
2754 int altered_servers = 0;
2755
2756 char *first_param, *cur_param, *next_param, *end_params;
2757 char *st_cur_param = NULL;
2758 char *st_next_param = NULL;
2759
Christopher Fauleta3618372018-12-13 21:59:56 +01002760 struct buffer *temp = get_trash_chunk();
William Lallemand74c24fb2016-11-21 17:18:36 +01002761
Christopher Fauleta3618372018-12-13 21:59:56 +01002762 if (IS_HTX_STRM(s)) {
2763 struct htx *htx = htxbuf(&s->req.buf);
2764 struct htx_blk *blk;
Christopher Fauleta3618372018-12-13 21:59:56 +01002765
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01002766 /* we need more data */
2767 if (s->txn->req.msg_state < HTTP_MSG_DONE) {
2768 /* check if we can receive more */
2769 if (htx_free_data_space(htx) <= global.tune.maxrewrite) {
2770 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
2771 goto out;
Christopher Fauleta3618372018-12-13 21:59:56 +01002772 }
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01002773 goto wait;
Christopher Fauleta3618372018-12-13 21:59:56 +01002774 }
William Lallemand74c24fb2016-11-21 17:18:36 +01002775
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01002776 /* The request was fully received. Copy data */
2777 blk = htx_get_head_blk(htx);
2778 while (blk) {
Christopher Fauleta3618372018-12-13 21:59:56 +01002779 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Fauleta3618372018-12-13 21:59:56 +01002780
2781 if (type == HTX_BLK_EOM || type == HTX_BLK_EOD)
2782 break;
2783 if (type == HTX_BLK_DATA) {
2784 struct ist v = htx_get_blk_value(htx, blk);
2785
2786 if (!chunk_memcat(temp, v.ptr, v.len)) {
2787 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
2788 goto out;
2789 }
2790 }
Christopher Fauleted7a0662019-01-14 11:07:34 +01002791 blk = htx_get_next_blk(htx, blk);
Christopher Fauleta3618372018-12-13 21:59:56 +01002792 }
2793 }
2794 else {
2795 int reql;
2796
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01002797 /* we need more data */
2798 if (s->txn->req.msg_state < HTTP_MSG_DONE) {
2799 /* check if we can receive more */
2800 if (c_room(&s->req) <= global.tune.maxrewrite) {
2801 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
2802 goto out;
2803 }
2804 goto wait;
Christopher Fauleta3618372018-12-13 21:59:56 +01002805 }
Christopher Fauleta3618372018-12-13 21:59:56 +01002806 reql = co_getblk(si_oc(si), temp->area, s->txn->req.body_len,
2807 s->txn->req.eoh + 2);
2808 if (reql <= 0) {
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01002809 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
2810 goto out;
Christopher Fauleta3618372018-12-13 21:59:56 +01002811 }
2812 temp->data = reql;
William Lallemand74c24fb2016-11-21 17:18:36 +01002813 }
2814
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002815 first_param = temp->area;
Christopher Fauleta3618372018-12-13 21:59:56 +01002816 end_params = temp->area + temp->data;
William Lallemand74c24fb2016-11-21 17:18:36 +01002817 cur_param = next_param = end_params;
2818 *end_params = '\0';
2819
2820 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
2821
2822 /*
2823 * Parse the parameters in reverse order to only store the last value.
2824 * From the html form, the backend and the action are at the end.
2825 */
2826 while (cur_param > first_param) {
2827 char *value;
2828 int poffset, plen;
2829
2830 cur_param--;
2831
2832 if ((*cur_param == '&') || (cur_param == first_param)) {
2833 reprocess_servers:
2834 /* Parse the key */
2835 poffset = (cur_param != first_param ? 1 : 0);
2836 plen = next_param - cur_param + (cur_param == first_param ? 1 : 0);
2837 if ((plen > 0) && (plen <= sizeof(key))) {
2838 strncpy(key, cur_param + poffset, plen);
2839 key[plen - 1] = '\0';
2840 } else {
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01002841 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
William Lallemand74c24fb2016-11-21 17:18:36 +01002842 goto out;
2843 }
2844
2845 /* Parse the value */
2846 value = key;
2847 while (*value != '\0' && *value != '=') {
2848 value++;
2849 }
2850 if (*value == '=') {
2851 /* Ok, a value is found, we can mark the end of the key */
2852 *value++ = '\0';
2853 }
2854 if (url_decode(key) < 0 || url_decode(value) < 0)
2855 break;
2856
2857 /* Now we can check the key to see what to do */
2858 if (!px && (strcmp(key, "b") == 0)) {
2859 if ((px = proxy_be_by_name(value)) == NULL) {
2860 /* the backend name is unknown or ambiguous (duplicate names) */
2861 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
2862 goto out;
2863 }
2864 }
2865 else if (!action && (strcmp(key, "action") == 0)) {
2866 if (strcmp(value, "ready") == 0) {
2867 action = ST_ADM_ACTION_READY;
2868 }
2869 else if (strcmp(value, "drain") == 0) {
2870 action = ST_ADM_ACTION_DRAIN;
2871 }
2872 else if (strcmp(value, "maint") == 0) {
2873 action = ST_ADM_ACTION_MAINT;
2874 }
2875 else if (strcmp(value, "shutdown") == 0) {
2876 action = ST_ADM_ACTION_SHUTDOWN;
2877 }
2878 else if (strcmp(value, "dhlth") == 0) {
2879 action = ST_ADM_ACTION_DHLTH;
2880 }
2881 else if (strcmp(value, "ehlth") == 0) {
2882 action = ST_ADM_ACTION_EHLTH;
2883 }
2884 else if (strcmp(value, "hrunn") == 0) {
2885 action = ST_ADM_ACTION_HRUNN;
2886 }
2887 else if (strcmp(value, "hnolb") == 0) {
2888 action = ST_ADM_ACTION_HNOLB;
2889 }
2890 else if (strcmp(value, "hdown") == 0) {
2891 action = ST_ADM_ACTION_HDOWN;
2892 }
2893 else if (strcmp(value, "dagent") == 0) {
2894 action = ST_ADM_ACTION_DAGENT;
2895 }
2896 else if (strcmp(value, "eagent") == 0) {
2897 action = ST_ADM_ACTION_EAGENT;
2898 }
2899 else if (strcmp(value, "arunn") == 0) {
2900 action = ST_ADM_ACTION_ARUNN;
2901 }
2902 else if (strcmp(value, "adown") == 0) {
2903 action = ST_ADM_ACTION_ADOWN;
2904 }
2905 /* else these are the old supported methods */
2906 else if (strcmp(value, "disable") == 0) {
2907 action = ST_ADM_ACTION_DISABLE;
2908 }
2909 else if (strcmp(value, "enable") == 0) {
2910 action = ST_ADM_ACTION_ENABLE;
2911 }
2912 else if (strcmp(value, "stop") == 0) {
2913 action = ST_ADM_ACTION_STOP;
2914 }
2915 else if (strcmp(value, "start") == 0) {
2916 action = ST_ADM_ACTION_START;
2917 }
2918 else {
2919 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
2920 goto out;
2921 }
2922 }
2923 else if (strcmp(key, "s") == 0) {
2924 if (!(px && action)) {
2925 /*
2926 * Indicates that we'll need to reprocess the parameters
2927 * as soon as backend and action are known
2928 */
2929 if (!reprocess) {
2930 st_cur_param = cur_param;
2931 st_next_param = next_param;
2932 }
2933 reprocess = 1;
2934 }
2935 else if ((sv = findserver(px, value)) != NULL) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002936 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
William Lallemand74c24fb2016-11-21 17:18:36 +01002937 switch (action) {
2938 case ST_ADM_ACTION_DISABLE:
Emeric Brun52a91d32017-08-31 14:41:55 +02002939 if (!(sv->cur_admin & SRV_ADMF_FMAINT)) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002940 altered_servers++;
2941 total_servers++;
2942 srv_set_admin_flag(sv, SRV_ADMF_FMAINT, "'disable' on stats page");
2943 }
2944 break;
2945 case ST_ADM_ACTION_ENABLE:
Emeric Brun52a91d32017-08-31 14:41:55 +02002946 if (sv->cur_admin & SRV_ADMF_FMAINT) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002947 altered_servers++;
2948 total_servers++;
2949 srv_clr_admin_flag(sv, SRV_ADMF_FMAINT);
2950 }
2951 break;
2952 case ST_ADM_ACTION_STOP:
Emeric Brun52a91d32017-08-31 14:41:55 +02002953 if (!(sv->cur_admin & SRV_ADMF_FDRAIN)) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002954 srv_set_admin_flag(sv, SRV_ADMF_FDRAIN, "'stop' on stats page");
2955 altered_servers++;
2956 total_servers++;
2957 }
2958 break;
2959 case ST_ADM_ACTION_START:
Emeric Brun52a91d32017-08-31 14:41:55 +02002960 if (sv->cur_admin & SRV_ADMF_FDRAIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002961 srv_clr_admin_flag(sv, SRV_ADMF_FDRAIN);
2962 altered_servers++;
2963 total_servers++;
2964 }
2965 break;
2966 case ST_ADM_ACTION_DHLTH:
2967 if (sv->check.state & CHK_ST_CONFIGURED) {
2968 sv->check.state &= ~CHK_ST_ENABLED;
2969 altered_servers++;
2970 total_servers++;
2971 }
2972 break;
2973 case ST_ADM_ACTION_EHLTH:
2974 if (sv->check.state & CHK_ST_CONFIGURED) {
2975 sv->check.state |= CHK_ST_ENABLED;
2976 altered_servers++;
2977 total_servers++;
2978 }
2979 break;
2980 case ST_ADM_ACTION_HRUNN:
2981 if (!(sv->track)) {
2982 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02002983 srv_set_running(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01002984 altered_servers++;
2985 total_servers++;
2986 }
2987 break;
2988 case ST_ADM_ACTION_HNOLB:
2989 if (!(sv->track)) {
2990 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02002991 srv_set_stopping(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01002992 altered_servers++;
2993 total_servers++;
2994 }
2995 break;
2996 case ST_ADM_ACTION_HDOWN:
2997 if (!(sv->track)) {
2998 sv->check.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02002999 srv_set_stopped(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003000 altered_servers++;
3001 total_servers++;
3002 }
3003 break;
3004 case ST_ADM_ACTION_DAGENT:
3005 if (sv->agent.state & CHK_ST_CONFIGURED) {
3006 sv->agent.state &= ~CHK_ST_ENABLED;
3007 altered_servers++;
3008 total_servers++;
3009 }
3010 break;
3011 case ST_ADM_ACTION_EAGENT:
3012 if (sv->agent.state & CHK_ST_CONFIGURED) {
3013 sv->agent.state |= CHK_ST_ENABLED;
3014 altered_servers++;
3015 total_servers++;
3016 }
3017 break;
3018 case ST_ADM_ACTION_ARUNN:
3019 if (sv->agent.state & CHK_ST_ENABLED) {
3020 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02003021 srv_set_running(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003022 altered_servers++;
3023 total_servers++;
3024 }
3025 break;
3026 case ST_ADM_ACTION_ADOWN:
3027 if (sv->agent.state & CHK_ST_ENABLED) {
3028 sv->agent.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02003029 srv_set_stopped(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003030 altered_servers++;
3031 total_servers++;
3032 }
3033 break;
3034 case ST_ADM_ACTION_READY:
3035 srv_adm_set_ready(sv);
3036 altered_servers++;
3037 total_servers++;
3038 break;
3039 case ST_ADM_ACTION_DRAIN:
3040 srv_adm_set_drain(sv);
3041 altered_servers++;
3042 total_servers++;
3043 break;
3044 case ST_ADM_ACTION_MAINT:
3045 srv_adm_set_maint(sv);
3046 altered_servers++;
3047 total_servers++;
3048 break;
3049 case ST_ADM_ACTION_SHUTDOWN:
3050 if (px->state != PR_STSTOPPED) {
3051 struct stream *sess, *sess_bck;
3052
3053 list_for_each_entry_safe(sess, sess_bck, &sv->actconns, by_srv)
3054 if (sess->srv_conn == sv)
3055 stream_shutdown(sess, SF_ERR_KILLED);
3056
3057 altered_servers++;
3058 total_servers++;
3059 }
3060 break;
3061 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003062 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Lallemand74c24fb2016-11-21 17:18:36 +01003063 } else {
3064 /* the server name is unknown or ambiguous (duplicate names) */
3065 total_servers++;
3066 }
3067 }
3068 if (reprocess && px && action) {
3069 /* Now, we know the backend and the action chosen by the user.
3070 * We can safely restart from the first server parameter
3071 * to reprocess them
3072 */
3073 cur_param = st_cur_param;
3074 next_param = st_next_param;
3075 reprocess = 0;
3076 goto reprocess_servers;
3077 }
3078
3079 next_param = cur_param;
3080 }
3081 }
3082
3083 if (total_servers == 0) {
3084 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
3085 }
3086 else if (altered_servers == 0) {
3087 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
3088 }
3089 else if (altered_servers == total_servers) {
3090 appctx->ctx.stats.st_code = STAT_STATUS_DONE;
3091 }
3092 else {
3093 appctx->ctx.stats.st_code = STAT_STATUS_PART;
3094 }
3095 out:
3096 return 1;
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01003097 wait:
3098 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
3099 return 0;
Christopher Fauletef779222018-10-31 08:47:01 +01003100}
3101
3102
3103static int stats_send_htx_headers(struct stream_interface *si, struct htx *htx)
3104{
3105 struct stream *s = si_strm(si);
3106 struct uri_auth *uri = s->be->uri_auth;
3107 struct appctx *appctx = __objt_appctx(si->end);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003108 struct htx_sl *sl;
3109 unsigned int flags;
Christopher Fauletef779222018-10-31 08:47:01 +01003110
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003111 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);
3112 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist("200"), ist("OK"));
3113 if (!sl)
Christopher Fauletef779222018-10-31 08:47:01 +01003114 goto full;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003115 sl->info.res.status = 200;
Christopher Fauletef779222018-10-31 08:47:01 +01003116
3117 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")) ||
3118 !htx_add_header(htx, ist("Connection"), ist("close")))
3119 goto full;
3120 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
3121 if (!htx_add_header(htx, ist("Content-Type"), ist("text/html")))
3122 goto full;
3123 }
3124 else {
3125 if (!htx_add_header(htx, ist("Content-Type"), ist("text/plain")))
3126 goto full;
3127 }
3128
3129 if (uri->refresh > 0 && !(appctx->ctx.stats.flags & STAT_NO_REFRESH)) {
3130 const char *refresh = U2A(uri->refresh);
3131 if (!htx_add_header(htx, ist("Refresh"), ist2(refresh, strlen(refresh))))
3132 goto full;
3133 }
3134
3135 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
3136 if (!htx_add_header(htx, ist("Transfer-Encoding"), ist("chunked")))
3137 goto full;
3138 }
3139
3140 if (!htx_add_endof(htx, HTX_BLK_EOH))
3141 goto full;
3142
Christopher Faulet1e2d6362019-02-27 16:28:48 +01003143 channel_add_input(&s->res, htx->data);
Christopher Fauletef779222018-10-31 08:47:01 +01003144 return 1;
3145
3146 full:
3147 htx_reset(htx);
3148 si_rx_room_blk(si);
3149 return 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01003150}
3151
Christopher Fauletef779222018-10-31 08:47:01 +01003152
3153static int stats_send_htx_redirect(struct stream_interface *si, struct htx *htx)
3154{
3155 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
3156 struct stream *s = si_strm(si);
3157 struct uri_auth *uri = s->be->uri_auth;
3158 struct appctx *appctx = __objt_appctx(si->end);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003159 struct htx_sl *sl;
3160 unsigned int flags;
Christopher Fauletef779222018-10-31 08:47:01 +01003161
3162 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
3163 scope_txt[0] = 0;
3164 if (appctx->ctx.stats.scope_len) {
Christopher Fauleted7a0662019-01-14 11:07:34 +01003165 const char *scope_ptr = stats_scope_ptr(appctx, si);
3166
Christopher Fauletef779222018-10-31 08:47:01 +01003167 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Christopher Fauleted7a0662019-01-14 11:07:34 +01003168 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), scope_ptr, appctx->ctx.stats.scope_len);
Christopher Fauletef779222018-10-31 08:47:01 +01003169 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
3170 }
3171
3172 /* We don't want to land on the posted stats page because a refresh will
3173 * repost the data. We don't want this to happen on accident so we redirect
3174 * the browse to the stats page with a GET.
3175 */
3176 chunk_printf(&trash, "%s;st=%s%s%s%s",
3177 uri->uri_prefix,
3178 ((appctx->ctx.stats.st_code > STAT_STATUS_INIT) &&
3179 (appctx->ctx.stats.st_code < STAT_STATUS_SIZE) &&
3180 stat_status_codes[appctx->ctx.stats.st_code]) ?
3181 stat_status_codes[appctx->ctx.stats.st_code] :
3182 stat_status_codes[STAT_STATUS_UNKN],
3183 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3184 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3185 scope_txt);
3186
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003187 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_CHNK);
3188 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist("303"), ist("See Other"));
3189 if (!sl)
Christopher Fauletef779222018-10-31 08:47:01 +01003190 goto full;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003191 sl->info.res.status = 303;
Christopher Fauletef779222018-10-31 08:47:01 +01003192
3193 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")) ||
3194 !htx_add_header(htx, ist("Connection"), ist("close")) ||
3195 !htx_add_header(htx, ist("Content-Type"), ist("text/plain")) ||
3196 !htx_add_header(htx, ist("Content-Length"), ist("0")) ||
3197 !htx_add_header(htx, ist("Location"), ist2(trash.area, trash.data)))
3198 goto full;
3199
3200 if (!htx_add_endof(htx, HTX_BLK_EOH))
3201 goto full;
3202
Christopher Faulet1e2d6362019-02-27 16:28:48 +01003203 channel_add_input(&s->res, htx->data);
Christopher Fauletef779222018-10-31 08:47:01 +01003204 return 1;
3205
3206full:
3207 htx_reset(htx);
3208 si_rx_room_blk(si);
3209 return 0;
3210}
William Lallemand74c24fb2016-11-21 17:18:36 +01003211
3212static int stats_send_http_headers(struct stream_interface *si)
3213{
3214 struct stream *s = si_strm(si);
3215 struct uri_auth *uri = s->be->uri_auth;
Willy Tarreau21ff2c42018-09-20 11:01:01 +02003216 struct appctx *appctx = __objt_appctx(si->end);
William Lallemand74c24fb2016-11-21 17:18:36 +01003217
3218 chunk_printf(&trash,
3219 "HTTP/1.1 200 OK\r\n"
3220 "Cache-Control: no-cache\r\n"
3221 "Connection: close\r\n"
3222 "Content-Type: %s\r\n",
3223 (appctx->ctx.stats.flags & STAT_FMT_HTML) ? "text/html" : "text/plain");
3224
3225 if (uri->refresh > 0 && !(appctx->ctx.stats.flags & STAT_NO_REFRESH))
3226 chunk_appendf(&trash, "Refresh: %d\r\n",
3227 uri->refresh);
3228
3229 /* we don't send the CRLF in chunked mode, it will be sent with the first chunk's size */
3230
3231 if (appctx->ctx.stats.flags & STAT_CHUNKED)
3232 chunk_appendf(&trash, "Transfer-Encoding: chunked\r\n");
3233 else
3234 chunk_appendf(&trash, "\r\n");
3235
Willy Tarreau06d80a92017-10-19 14:32:15 +02003236 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01003237 si_rx_room_blk(si);
William Lallemand74c24fb2016-11-21 17:18:36 +01003238 return 0;
3239 }
3240
3241 return 1;
3242}
3243
3244static int stats_send_http_redirect(struct stream_interface *si)
3245{
3246 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
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 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
3252 scope_txt[0] = 0;
3253 if (appctx->ctx.stats.scope_len) {
3254 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau89faf5d2018-06-07 18:16:48 +02003255 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 +01003256 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
3257 }
3258
3259 /* We don't want to land on the posted stats page because a refresh will
3260 * repost the data. We don't want this to happen on accident so we redirect
3261 * the browse to the stats page with a GET.
3262 */
3263 chunk_printf(&trash,
3264 "HTTP/1.1 303 See Other\r\n"
3265 "Cache-Control: no-cache\r\n"
3266 "Content-Type: text/plain\r\n"
3267 "Connection: close\r\n"
3268 "Location: %s;st=%s%s%s%s\r\n"
3269 "Content-length: 0\r\n"
3270 "\r\n",
3271 uri->uri_prefix,
3272 ((appctx->ctx.stats.st_code > STAT_STATUS_INIT) &&
3273 (appctx->ctx.stats.st_code < STAT_STATUS_SIZE) &&
3274 stat_status_codes[appctx->ctx.stats.st_code]) ?
3275 stat_status_codes[appctx->ctx.stats.st_code] :
3276 stat_status_codes[STAT_STATUS_UNKN],
3277 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3278 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3279 scope_txt);
3280
Willy Tarreau06d80a92017-10-19 14:32:15 +02003281 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01003282 si_rx_room_blk(si);
William Lallemand74c24fb2016-11-21 17:18:36 +01003283 return 0;
3284 }
3285
3286 return 1;
3287}
3288
Simon Horman05ee2132017-01-04 09:37:25 +01003289
William Lallemand74c24fb2016-11-21 17:18:36 +01003290/* This I/O handler runs as an applet embedded in a stream interface. It is
3291 * used to send HTTP stats over a TCP socket. The mechanism is very simple.
3292 * appctx->st0 contains the operation in progress (dump, done). The handler
3293 * automatically unregisters itself once transfer is complete.
3294 */
Christopher Fauletef779222018-10-31 08:47:01 +01003295static void htx_stats_io_handler(struct appctx *appctx)
3296{
3297 struct stream_interface *si = appctx->owner;
3298 struct stream *s = si_strm(si);
3299 struct channel *req = si_oc(si);
3300 struct channel *res = si_ic(si);
3301 struct htx *req_htx, *res_htx;
3302
3303 res_htx = htx_from_buf(&res->buf);
3304
3305 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
3306 goto out;
3307
3308 /* Check if the input buffer is avalaible. */
3309 if (!b_size(&res->buf)) {
3310 si_rx_room_blk(si);
3311 goto out;
3312 }
3313
3314 /* check that the output is not closed */
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003315 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_SHUTR))
3316 appctx->st0 = STAT_HTTP_END;
Christopher Fauletef779222018-10-31 08:47:01 +01003317
3318 /* all states are processed in sequence */
3319 if (appctx->st0 == STAT_HTTP_HEAD) {
3320 if (stats_send_htx_headers(si, res_htx)) {
3321 if (s->txn->meth == HTTP_METH_HEAD)
3322 appctx->st0 = STAT_HTTP_DONE;
3323 else
3324 appctx->st0 = STAT_HTTP_DUMP;
3325 }
3326 }
3327
3328 if (appctx->st0 == STAT_HTTP_DUMP) {
3329 if (stats_dump_stat_to_buffer(si, res_htx, s->be->uri_auth))
3330 appctx->st0 = STAT_HTTP_DONE;
3331 }
3332
3333 if (appctx->st0 == STAT_HTTP_POST) {
3334 if (stats_process_http_post(si))
3335 appctx->st0 = STAT_HTTP_LAST;
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003336 else if (req->flags & CF_SHUTR)
Christopher Fauletef779222018-10-31 08:47:01 +01003337 appctx->st0 = STAT_HTTP_DONE;
3338 }
3339
3340 if (appctx->st0 == STAT_HTTP_LAST) {
3341 if (stats_send_htx_redirect(si, res_htx))
3342 appctx->st0 = STAT_HTTP_DONE;
3343 }
3344
3345 if (appctx->st0 == STAT_HTTP_DONE) {
Christopher Fauletb1b08212018-11-21 13:51:07 +01003346 /* Don't add EOD and TLR because mux-h1 will take care of it */
Christopher Fauletef779222018-10-31 08:47:01 +01003347 if (!htx_add_endof(res_htx, HTX_BLK_EOM)) {
3348 si_rx_room_blk(si);
3349 goto out;
3350 }
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003351 channel_add_input(&s->res, 1);
3352 appctx->st0 = STAT_HTTP_END;
Christopher Fauletef779222018-10-31 08:47:01 +01003353 }
3354
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003355 if (appctx->st0 == STAT_HTTP_END) {
3356 if (!(res->flags & CF_SHUTR)) {
Christopher Fauletef779222018-10-31 08:47:01 +01003357 res->flags |= CF_READ_NULL;
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003358 si_shutr(si);
3359 }
3360
3361 /* eat the whole request */
3362 if (co_data(req)) {
3363 req_htx = htx_from_buf(&req->buf);
3364 co_htx_skip(req, req_htx, co_data(req));
3365 htx_to_buf(req_htx, &req->buf);
Christopher Fauletef779222018-10-31 08:47:01 +01003366 }
3367 }
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003368
Christopher Fauletef779222018-10-31 08:47:01 +01003369 out:
3370 /* we have left the request in the buffer for the case where we
3371 * process a POST, and this automatically re-enables activity on
3372 * read. It's better to indicate that we want to stop reading when
3373 * we're sending, so that we know there's at most one direction
3374 * deciding to wake the applet up. It saves it from looping when
3375 * emitting large blocks into small TCP windows.
3376 */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003377 htx_to_buf(res_htx, &res->buf);
3378 if (!channel_is_empty(res))
Christopher Fauletef779222018-10-31 08:47:01 +01003379 si_stop_get(si);
Christopher Fauletef779222018-10-31 08:47:01 +01003380}
3381
3382
3383/* This I/O handler runs as an applet embedded in a stream interface. It is
3384 * used to send HTTP stats over a TCP socket. The mechanism is very simple.
3385 * appctx->st0 contains the operation in progress (dump, done). The handler
3386 * automatically unregisters itself once transfer is complete.
3387 */
William Lallemand74c24fb2016-11-21 17:18:36 +01003388static void http_stats_io_handler(struct appctx *appctx)
3389{
3390 struct stream_interface *si = appctx->owner;
3391 struct stream *s = si_strm(si);
3392 struct channel *req = si_oc(si);
3393 struct channel *res = si_ic(si);
3394
Christopher Fauletef779222018-10-31 08:47:01 +01003395 if (IS_HTX_STRM(s))
3396 return htx_stats_io_handler(appctx);
3397
William Lallemand74c24fb2016-11-21 17:18:36 +01003398 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
3399 goto out;
3400
Joseph Herlant29023ec2018-11-15 13:39:46 -08003401 /* Check if the input buffer is available. */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003402 if (res->buf.size == 0) {
Willy Tarreau4b962a42018-11-15 11:03:21 +01003403 /* already subscribed, we'll be called later once the buffer is
3404 * available.
3405 */
Christopher Fauleta73e59b2016-12-09 17:30:18 +01003406 goto out;
3407 }
3408
William Lallemand74c24fb2016-11-21 17:18:36 +01003409 /* check that the output is not closed */
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003410 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_SHUTR))
3411 appctx->st0 = STAT_HTTP_END;
William Lallemand74c24fb2016-11-21 17:18:36 +01003412
3413 /* all states are processed in sequence */
3414 if (appctx->st0 == STAT_HTTP_HEAD) {
3415 if (stats_send_http_headers(si)) {
3416 if (s->txn->meth == HTTP_METH_HEAD)
3417 appctx->st0 = STAT_HTTP_DONE;
3418 else
3419 appctx->st0 = STAT_HTTP_DUMP;
3420 }
3421 }
3422
3423 if (appctx->st0 == STAT_HTTP_DUMP) {
Willy Tarreau97f538b2018-06-15 19:41:31 +02003424 unsigned int prev_len = ci_data(si_ic(si));
William Lallemand74c24fb2016-11-21 17:18:36 +01003425 unsigned int data_len;
3426 unsigned int last_len;
3427 unsigned int last_fwd = 0;
3428
3429 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
3430 /* One difficulty we're facing is that we must prevent
3431 * the input data from being automatically forwarded to
3432 * the output area. For this, we temporarily disable
3433 * forwarding on the channel.
3434 */
3435 last_fwd = si_ic(si)->to_forward;
3436 si_ic(si)->to_forward = 0;
3437 chunk_printf(&trash, "\r\n000000\r\n");
Willy Tarreau06d80a92017-10-19 14:32:15 +02003438 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01003439 si_rx_room_blk(si);
William Lallemand74c24fb2016-11-21 17:18:36 +01003440 si_ic(si)->to_forward = last_fwd;
3441 goto out;
3442 }
3443 }
3444
Willy Tarreau97f538b2018-06-15 19:41:31 +02003445 data_len = ci_data(si_ic(si));
Christopher Fauletef779222018-10-31 08:47:01 +01003446 if (stats_dump_stat_to_buffer(si, NULL, s->be->uri_auth))
William Lallemand74c24fb2016-11-21 17:18:36 +01003447 appctx->st0 = STAT_HTTP_DONE;
3448
Willy Tarreau97f538b2018-06-15 19:41:31 +02003449 last_len = ci_data(si_ic(si));
William Lallemand74c24fb2016-11-21 17:18:36 +01003450
3451 /* Now we must either adjust or remove the chunk size. This is
3452 * not easy because the chunk size might wrap at the end of the
3453 * buffer, so we pretend we have nothing in the buffer, we write
3454 * the size, then restore the buffer's contents. Note that we can
3455 * only do that because no forwarding is scheduled on the stats
3456 * applet.
3457 */
3458 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
3459 si_ic(si)->total -= (last_len - prev_len);
Willy Tarreau97f538b2018-06-15 19:41:31 +02003460 b_sub(si_ib(si), (last_len - prev_len));
William Lallemand74c24fb2016-11-21 17:18:36 +01003461
3462 if (last_len != data_len) {
3463 chunk_printf(&trash, "\r\n%06x\r\n", (last_len - data_len));
Willy Tarreau06d80a92017-10-19 14:32:15 +02003464 if (ci_putchk(si_ic(si), &trash) == -1)
Willy Tarreaudb398432018-11-15 11:08:52 +01003465 si_rx_room_blk(si);
William Lallemand74c24fb2016-11-21 17:18:36 +01003466
3467 si_ic(si)->total += (last_len - data_len);
Willy Tarreau97f538b2018-06-15 19:41:31 +02003468 b_add(si_ib(si), last_len - data_len);
William Lallemand74c24fb2016-11-21 17:18:36 +01003469 }
3470 /* now re-enable forwarding */
3471 channel_forward(si_ic(si), last_fwd);
3472 }
3473 }
3474
3475 if (appctx->st0 == STAT_HTTP_POST) {
3476 if (stats_process_http_post(si))
3477 appctx->st0 = STAT_HTTP_LAST;
3478 else if (si_oc(si)->flags & CF_SHUTR)
3479 appctx->st0 = STAT_HTTP_DONE;
3480 }
3481
3482 if (appctx->st0 == STAT_HTTP_LAST) {
3483 if (stats_send_http_redirect(si))
3484 appctx->st0 = STAT_HTTP_DONE;
3485 }
3486
3487 if (appctx->st0 == STAT_HTTP_DONE) {
3488 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
3489 chunk_printf(&trash, "\r\n0\r\n\r\n");
Willy Tarreau06d80a92017-10-19 14:32:15 +02003490 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01003491 si_rx_room_blk(si);
William Lallemand74c24fb2016-11-21 17:18:36 +01003492 goto out;
3493 }
3494 }
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003495 appctx->st0 = STAT_HTTP_END;
William Lallemand74c24fb2016-11-21 17:18:36 +01003496 }
3497
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003498 if (appctx->st0 == STAT_HTTP_END) {
3499 if (!(res->flags & CF_SHUTR)) {
William Lallemand74c24fb2016-11-21 17:18:36 +01003500 res->flags |= CF_READ_NULL;
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003501 si_shutr(si);
William Lallemand74c24fb2016-11-21 17:18:36 +01003502 }
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003503
3504 /* eat the whole request */
3505 if (co_data(req))
3506 co_skip(si_oc(si), co_data(si_oc(si)));
William Lallemand74c24fb2016-11-21 17:18:36 +01003507 }
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003508
William Lallemand74c24fb2016-11-21 17:18:36 +01003509 out:
Willy Tarreau055ba4f2018-07-24 17:05:54 +02003510 /* we have left the request in the buffer for the case where we
3511 * process a POST, and this automatically re-enables activity on
3512 * read. It's better to indicate that we want to stop reading when
3513 * we're sending, so that we know there's at most one direction
3514 * deciding to wake the applet up. It saves it from looping when
3515 * emitting large blocks into small TCP windows.
3516 */
3517 if (!channel_is_empty(res))
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01003518 si_stop_get(si);
William Lallemand74c24fb2016-11-21 17:18:36 +01003519}
3520
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003521/* Dump all fields from <info> into <out> using the "show info" format (name: value) */
Willy Tarreau83061a82018-07-13 11:56:34 +02003522static int stats_dump_info_fields(struct buffer *out,
3523 const struct field *info)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003524{
3525 int field;
3526
3527 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
3528 if (!field_format(info, field))
3529 continue;
3530
3531 if (!chunk_appendf(out, "%s: ", info_field_names[field]))
3532 return 0;
3533 if (!stats_emit_raw_data_field(out, &info[field]))
3534 return 0;
3535 if (!chunk_strcat(out, "\n"))
3536 return 0;
3537 }
3538 return 1;
3539}
3540
3541/* Dump all fields from <info> into <out> using the "show info typed" format */
Willy Tarreau83061a82018-07-13 11:56:34 +02003542static int stats_dump_typed_info_fields(struct buffer *out,
3543 const struct field *info)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003544{
3545 int field;
3546
3547 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
3548 if (!field_format(info, field))
3549 continue;
3550
3551 if (!chunk_appendf(out, "%d.%s.%u:", field, info_field_names[field], info[INF_PROCESS_NUM].u.u32))
3552 return 0;
3553 if (!stats_emit_field_tags(out, &info[field], ':'))
3554 return 0;
3555 if (!stats_emit_typed_data_field(out, &info[field]))
3556 return 0;
3557 if (!chunk_strcat(out, "\n"))
3558 return 0;
3559 }
3560 return 1;
3561}
3562
3563/* Fill <info> with HAProxy global info. <info> is preallocated
3564 * array of length <len>. The length of the aray must be
3565 * INF_TOTAL_FIELDS. If this length is less then this value, the
3566 * function returns 0, otherwise, it returns 1.
3567 */
3568int stats_fill_info(struct field *info, int len)
3569{
3570 unsigned int up = (now.tv_sec - start_date.tv_sec);
Willy Tarreau83061a82018-07-13 11:56:34 +02003571 struct buffer *out = get_trash_chunk();
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003572
3573#ifdef USE_OPENSSL
3574 int ssl_sess_rate = read_freq_ctr(&global.ssl_per_sec);
3575 int ssl_key_rate = read_freq_ctr(&global.ssl_fe_keys_per_sec);
3576 int ssl_reuse = 0;
3577
3578 if (ssl_key_rate < ssl_sess_rate) {
3579 /* count the ssl reuse ratio and avoid overflows in both directions */
3580 ssl_reuse = 100 - (100 * ssl_key_rate + (ssl_sess_rate - 1) / 2) / ssl_sess_rate;
3581 }
3582#endif
3583
3584 if (len < INF_TOTAL_FIELDS)
3585 return 0;
3586
3587 chunk_reset(out);
3588 memset(info, 0, sizeof(*info) * len);
3589
3590 info[INF_NAME] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, PRODUCT_NAME);
Willy Tarreau909b9d82019-01-04 18:20:32 +01003591 info[INF_VERSION] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, haproxy_version);
3592 info[INF_RELEASE_DATE] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, haproxy_date);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003593
Yves Lafon95317282018-02-26 11:10:37 +01003594 info[INF_NBTHREAD] = mkf_u32(FO_CONFIG|FS_SERVICE, global.nbthread);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003595 info[INF_NBPROC] = mkf_u32(FO_CONFIG|FS_SERVICE, global.nbproc);
3596 info[INF_PROCESS_NUM] = mkf_u32(FO_KEY, relative_pid);
3597 info[INF_PID] = mkf_u32(FO_STATUS, pid);
3598
3599 info[INF_UPTIME] = mkf_str(FN_DURATION, chunk_newstr(out));
3600 chunk_appendf(out, "%ud %uh%02um%02us", up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60));
3601
3602 info[INF_UPTIME_SEC] = mkf_u32(FN_DURATION, up);
3603 info[INF_MEMMAX_MB] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_memmax);
3604 info[INF_POOL_ALLOC_MB] = mkf_u32(0, (unsigned)(pool_total_allocated() / 1048576L));
3605 info[INF_POOL_USED_MB] = mkf_u32(0, (unsigned)(pool_total_used() / 1048576L));
3606 info[INF_POOL_FAILED] = mkf_u32(FN_COUNTER, pool_total_failures());
3607 info[INF_ULIMIT_N] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_nofile);
3608 info[INF_MAXSOCK] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxsock);
3609 info[INF_MAXCONN] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxconn);
3610 info[INF_HARD_MAXCONN] = mkf_u32(FO_CONFIG|FN_LIMIT, global.hardmaxconn);
3611 info[INF_CURR_CONN] = mkf_u32(0, actconn);
3612 info[INF_CUM_CONN] = mkf_u32(FN_COUNTER, totalconn);
3613 info[INF_CUM_REQ] = mkf_u32(FN_COUNTER, global.req_count);
3614#ifdef USE_OPENSSL
3615 info[INF_MAX_SSL_CONNS] = mkf_u32(FN_MAX, global.maxsslconn);
3616 info[INF_CURR_SSL_CONNS] = mkf_u32(0, sslconns);
3617 info[INF_CUM_SSL_CONNS] = mkf_u32(FN_COUNTER, totalsslconns);
3618#endif
3619 info[INF_MAXPIPES] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxpipes);
3620 info[INF_PIPES_USED] = mkf_u32(0, pipes_used);
3621 info[INF_PIPES_FREE] = mkf_u32(0, pipes_free);
3622 info[INF_CONN_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.conn_per_sec));
3623 info[INF_CONN_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.cps_lim);
3624 info[INF_MAX_CONN_RATE] = mkf_u32(FN_MAX, global.cps_max);
3625 info[INF_SESS_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.sess_per_sec));
3626 info[INF_SESS_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.sps_lim);
3627 info[INF_MAX_SESS_RATE] = mkf_u32(FN_RATE, global.sps_max);
3628
3629#ifdef USE_OPENSSL
3630 info[INF_SSL_RATE] = mkf_u32(FN_RATE, ssl_sess_rate);
3631 info[INF_SSL_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.ssl_lim);
3632 info[INF_MAX_SSL_RATE] = mkf_u32(FN_MAX, global.ssl_max);
3633 info[INF_SSL_FRONTEND_KEY_RATE] = mkf_u32(0, ssl_key_rate);
3634 info[INF_SSL_FRONTEND_MAX_KEY_RATE] = mkf_u32(FN_MAX, global.ssl_fe_keys_max);
3635 info[INF_SSL_FRONTEND_SESSION_REUSE_PCT] = mkf_u32(0, ssl_reuse);
3636 info[INF_SSL_BACKEND_KEY_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.ssl_be_keys_per_sec));
3637 info[INF_SSL_BACKEND_MAX_KEY_RATE] = mkf_u32(FN_MAX, global.ssl_be_keys_max);
3638 info[INF_SSL_CACHE_LOOKUPS] = mkf_u32(FN_COUNTER, global.shctx_lookups);
3639 info[INF_SSL_CACHE_MISSES] = mkf_u32(FN_COUNTER, global.shctx_misses);
3640#endif
3641 info[INF_COMPRESS_BPS_IN] = mkf_u32(FN_RATE, read_freq_ctr(&global.comp_bps_in));
3642 info[INF_COMPRESS_BPS_OUT] = mkf_u32(FN_RATE, read_freq_ctr(&global.comp_bps_out));
3643 info[INF_COMPRESS_BPS_RATE_LIM] = mkf_u32(FO_CONFIG|FN_LIMIT, global.comp_rate_lim);
3644#ifdef USE_ZLIB
3645 info[INF_ZLIB_MEM_USAGE] = mkf_u32(0, zlib_used_memory);
3646 info[INF_MAX_ZLIB_MEM_USAGE] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxzlibmem);
3647#endif
3648 info[INF_TASKS] = mkf_u32(0, nb_tasks_cur);
Christopher Faulet34c5cc92016-12-06 09:15:30 +01003649 info[INF_RUN_QUEUE] = mkf_u32(0, tasks_run_queue_cur);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003650 info[INF_IDLE_PCT] = mkf_u32(FN_AVG, idle_pct);
3651 info[INF_NODE] = mkf_str(FO_CONFIG|FN_OUTPUT|FS_SERVICE, global.node);
3652 if (global.desc)
3653 info[INF_DESCRIPTION] = mkf_str(FO_CONFIG|FN_OUTPUT|FS_SERVICE, global.desc);
Willy Tarreau00098ea2018-11-05 14:38:13 +01003654 info[INF_STOPPING] = mkf_u32(0, stopping);
3655 info[INF_JOBS] = mkf_u32(0, jobs);
William Lallemanda7199262018-11-16 16:57:20 +01003656 info[INF_UNSTOPPABLE_JOBS] = mkf_u32(0, unstoppable_jobs);
Willy Tarreau00098ea2018-11-05 14:38:13 +01003657 info[INF_LISTENERS] = mkf_u32(0, listeners);
Willy Tarreau199ad242018-11-05 16:31:22 +01003658 info[INF_ACTIVE_PEERS] = mkf_u32(0, active_peers);
Willy Tarreau2d372c22018-11-05 17:12:27 +01003659 info[INF_CONNECTED_PEERS] = mkf_u32(0, connected_peers);
Willy Tarreau13ef7732018-11-12 07:25:28 +01003660 info[INF_DROPPED_LOGS] = mkf_u32(0, dropped_logs);
Willy Tarreaubeb859a2018-11-22 18:07:59 +01003661 info[INF_BUSY_POLLING] = mkf_u32(0, !!(global.tune.options & GTUNE_BUSY_POLLING));
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003662
3663 return 1;
3664}
3665
3666/* This function dumps information onto the stream interface's read buffer.
3667 * It returns 0 as long as it does not complete, non-zero upon completion.
3668 * No state is used.
3669 */
3670static int stats_dump_info_to_buffer(struct stream_interface *si)
3671{
3672 struct appctx *appctx = __objt_appctx(si->end);
3673
3674 if (!stats_fill_info(info, INF_TOTAL_FIELDS))
3675 return 0;
3676
3677 chunk_reset(&trash);
3678
3679 if (appctx->ctx.stats.flags & STAT_FMT_TYPED)
3680 stats_dump_typed_info_fields(&trash, info);
Simon Horman05ee2132017-01-04 09:37:25 +01003681 else if (appctx->ctx.stats.flags & STAT_FMT_JSON)
3682 stats_dump_json_info_fields(&trash, info);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003683 else
3684 stats_dump_info_fields(&trash, info);
3685
Willy Tarreau06d80a92017-10-19 14:32:15 +02003686 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01003687 si_rx_room_blk(si);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003688 return 0;
3689 }
3690
3691 return 1;
3692}
3693
Simon Horman6f6bb382017-01-04 09:37:26 +01003694/* This function dumps the schema onto the stream interface's read buffer.
3695 * It returns 0 as long as it does not complete, non-zero upon completion.
3696 * No state is used.
3697 *
3698 * Integer values bouned to the range [-(2**53)+1, (2**53)-1] as
3699 * per the recommendation for interoperable integers in section 6 of RFC 7159.
3700 */
Willy Tarreau83061a82018-07-13 11:56:34 +02003701static void stats_dump_json_schema(struct buffer *out)
Simon Horman6f6bb382017-01-04 09:37:26 +01003702{
3703
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003704 int old_len = out->data;
Simon Horman6f6bb382017-01-04 09:37:26 +01003705
3706 chunk_strcat(out,
3707 "{"
3708 "\"$schema\":\"http://json-schema.org/draft-04/schema#\","
3709 "\"oneOf\":["
3710 "{"
3711 "\"title\":\"Info\","
3712 "\"type\":\"array\","
3713 "\"items\":{"
3714 "\"properties\":{"
3715 "\"title\":\"InfoItem\","
3716 "\"type\":\"object\","
3717 "\"field\":{\"$ref\":\"#/definitions/field\"},"
3718 "\"processNum\":{\"$ref\":\"#/definitions/processNum\"},"
3719 "\"tags\":{\"$ref\":\"#/definitions/tags\"},"
3720 "\"value\":{\"$ref\":\"#/definitions/typedValue\"}"
3721 "},"
3722 "\"required\":[\"field\",\"processNum\",\"tags\","
3723 "\"value\"]"
3724 "}"
3725 "},"
3726 "{"
3727 "\"title\":\"Stat\","
3728 "\"type\":\"array\","
3729 "\"items\":{"
3730 "\"title\":\"InfoItem\","
3731 "\"type\":\"object\","
3732 "\"properties\":{"
3733 "\"objType\":{"
3734 "\"enum\":[\"Frontend\",\"Backend\",\"Listener\","
3735 "\"Server\",\"Unknown\"]"
3736 "},"
3737 "\"proxyId\":{"
3738 "\"type\":\"integer\","
3739 "\"minimum\":0"
3740 "},"
3741 "\"id\":{"
3742 "\"type\":\"integer\","
3743 "\"minimum\":0"
3744 "},"
3745 "\"field\":{\"$ref\":\"#/definitions/field\"},"
3746 "\"processNum\":{\"$ref\":\"#/definitions/processNum\"},"
3747 "\"tags\":{\"$ref\":\"#/definitions/tags\"},"
3748 "\"typedValue\":{\"$ref\":\"#/definitions/typedValue\"}"
3749 "},"
3750 "\"required\":[\"objType\",\"proxyId\",\"id\","
3751 "\"field\",\"processNum\",\"tags\","
3752 "\"value\"]"
3753 "}"
3754 "},"
3755 "{"
3756 "\"title\":\"Error\","
3757 "\"type\":\"object\","
3758 "\"properties\":{"
3759 "\"errorStr\":{"
3760 "\"type\":\"string\""
3761 "},"
3762 "\"required\":[\"errorStr\"]"
3763 "}"
3764 "}"
3765 "],"
3766 "\"definitions\":{"
3767 "\"field\":{"
3768 "\"type\":\"object\","
3769 "\"pos\":{"
3770 "\"type\":\"integer\","
3771 "\"minimum\":0"
3772 "},"
3773 "\"name\":{"
3774 "\"type\":\"string\""
3775 "},"
3776 "\"required\":[\"pos\",\"name\"]"
3777 "},"
3778 "\"processNum\":{"
3779 "\"type\":\"integer\","
3780 "\"minimum\":1"
3781 "},"
3782 "\"tags\":{"
3783 "\"type\":\"object\","
3784 "\"origin\":{"
3785 "\"type\":\"string\","
3786 "\"enum\":[\"Metric\",\"Status\",\"Key\","
3787 "\"Config\",\"Product\",\"Unknown\"]"
3788 "},"
3789 "\"nature\":{"
3790 "\"type\":\"string\","
3791 "\"enum\":[\"Gauge\",\"Limit\",\"Min\",\"Max\","
3792 "\"Rate\",\"Counter\",\"Duration\","
3793 "\"Age\",\"Time\",\"Name\",\"Output\","
3794 "\"Avg\", \"Unknown\"]"
3795 "},"
3796 "\"scope\":{"
3797 "\"type\":\"string\","
3798 "\"enum\":[\"Cluster\",\"Process\",\"Service\","
3799 "\"System\",\"Unknown\"]"
3800 "},"
3801 "\"required\":[\"origin\",\"nature\",\"scope\"]"
3802 "},"
3803 "\"typedValue\":{"
3804 "\"type\":\"object\","
3805 "\"oneOf\":["
3806 "{\"$ref\":\"#/definitions/typedValue/definitions/s32Value\"},"
3807 "{\"$ref\":\"#/definitions/typedValue/definitions/s64Value\"},"
3808 "{\"$ref\":\"#/definitions/typedValue/definitions/u32Value\"},"
3809 "{\"$ref\":\"#/definitions/typedValue/definitions/u64Value\"},"
3810 "{\"$ref\":\"#/definitions/typedValue/definitions/strValue\"}"
3811 "],"
3812 "\"definitions\":{"
3813 "\"s32Value\":{"
3814 "\"properties\":{"
3815 "\"type\":{"
3816 "\"type\":\"string\","
3817 "\"enum\":[\"s32\"]"
3818 "},"
3819 "\"value\":{"
3820 "\"type\":\"integer\","
3821 "\"minimum\":-2147483648,"
3822 "\"maximum\":2147483647"
3823 "}"
3824 "},"
3825 "\"required\":[\"type\",\"value\"]"
3826 "},"
3827 "\"s64Value\":{"
3828 "\"properties\":{"
3829 "\"type\":{"
3830 "\"type\":\"string\","
3831 "\"enum\":[\"s64\"]"
3832 "},"
3833 "\"value\":{"
3834 "\"type\":\"integer\","
3835 "\"minimum\":-9007199254740991,"
3836 "\"maximum\":9007199254740991"
3837 "}"
3838 "},"
3839 "\"required\":[\"type\",\"value\"]"
3840 "},"
3841 "\"u32Value\":{"
3842 "\"properties\":{"
3843 "\"type\":{"
3844 "\"type\":\"string\","
3845 "\"enum\":[\"u32\"]"
3846 "},"
3847 "\"value\":{"
3848 "\"type\":\"integer\","
3849 "\"minimum\":0,"
3850 "\"maximum\":4294967295"
3851 "}"
3852 "},"
3853 "\"required\":[\"type\",\"value\"]"
3854 "},"
3855 "\"u64Value\":{"
3856 "\"properties\":{"
3857 "\"type\":{"
3858 "\"type\":\"string\","
3859 "\"enum\":[\"u64\"]"
3860 "},"
3861 "\"value\":{"
3862 "\"type\":\"integer\","
3863 "\"minimum\":0,"
3864 "\"maximum\":9007199254740991"
3865 "}"
3866 "},"
3867 "\"required\":[\"type\",\"value\"]"
3868 "},"
3869 "\"strValue\":{"
3870 "\"properties\":{"
3871 "\"type\":{"
3872 "\"type\":\"string\","
3873 "\"enum\":[\"str\"]"
3874 "},"
3875 "\"value\":{\"type\":\"string\"}"
3876 "},"
3877 "\"required\":[\"type\",\"value\"]"
3878 "},"
3879 "\"unknownValue\":{"
3880 "\"properties\":{"
3881 "\"type\":{"
3882 "\"type\":\"integer\","
3883 "\"minimum\":0"
3884 "},"
3885 "\"value\":{"
3886 "\"type\":\"string\","
3887 "\"enum\":[\"unknown\"]"
3888 "}"
3889 "},"
3890 "\"required\":[\"type\",\"value\"]"
3891 "}"
3892 "}"
3893 "}"
3894 "}"
3895 "}");
3896
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003897 if (old_len == out->data) {
Simon Horman6f6bb382017-01-04 09:37:26 +01003898 chunk_reset(out);
3899 chunk_appendf(out,
3900 "{\"errorStr\":\"output buffer too short\"}");
3901 }
3902}
3903
3904/* This function dumps the schema onto the stream interface's read buffer.
3905 * It returns 0 as long as it does not complete, non-zero upon completion.
3906 * No state is used.
3907 */
3908static int stats_dump_json_schema_to_buffer(struct stream_interface *si)
3909{
3910 chunk_reset(&trash);
3911
3912 stats_dump_json_schema(&trash);
3913
Willy Tarreau06d80a92017-10-19 14:32:15 +02003914 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01003915 si_rx_room_blk(si);
Simon Horman6f6bb382017-01-04 09:37:26 +01003916 return 0;
3917 }
3918
3919 return 1;
3920}
3921
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02003922static int cli_parse_clear_counters(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau89d467c2016-11-23 11:02:40 +01003923{
3924 struct proxy *px;
3925 struct server *sv;
3926 struct listener *li;
3927 int clrall = 0;
3928
3929 if (strcmp(args[2], "all") == 0)
3930 clrall = 1;
3931
3932 /* check permissions */
3933 if (!cli_has_level(appctx, ACCESS_LVL_OPER) ||
3934 (clrall && !cli_has_level(appctx, ACCESS_LVL_ADMIN)))
3935 return 1;
3936
Olivier Houchardfbc74e82017-11-24 16:54:05 +01003937 for (px = proxies_list; px; px = px->next) {
Willy Tarreau89d467c2016-11-23 11:02:40 +01003938 if (clrall) {
3939 memset(&px->be_counters, 0, sizeof(px->be_counters));
3940 memset(&px->fe_counters, 0, sizeof(px->fe_counters));
3941 }
3942 else {
3943 px->be_counters.conn_max = 0;
3944 px->be_counters.p.http.rps_max = 0;
3945 px->be_counters.sps_max = 0;
3946 px->be_counters.cps_max = 0;
3947 px->be_counters.nbpend_max = 0;
3948
3949 px->fe_counters.conn_max = 0;
3950 px->fe_counters.p.http.rps_max = 0;
3951 px->fe_counters.sps_max = 0;
3952 px->fe_counters.cps_max = 0;
Willy Tarreau89d467c2016-11-23 11:02:40 +01003953 }
3954
3955 for (sv = px->srv; sv; sv = sv->next)
3956 if (clrall)
3957 memset(&sv->counters, 0, sizeof(sv->counters));
3958 else {
3959 sv->counters.cur_sess_max = 0;
3960 sv->counters.nbpend_max = 0;
3961 sv->counters.sps_max = 0;
3962 }
3963
3964 list_for_each_entry(li, &px->conf.listeners, by_fe)
3965 if (li->counters) {
3966 if (clrall)
3967 memset(li->counters, 0, sizeof(*li->counters));
3968 else
3969 li->counters->conn_max = 0;
3970 }
3971 }
3972
3973 global.cps_max = 0;
3974 global.sps_max = 0;
Olivier Houchard00bc3cb2017-10-17 19:23:25 +02003975 global.ssl_max = 0;
3976 global.ssl_fe_keys_max = 0;
3977 global.ssl_be_keys_max = 0;
Willy Tarreaud80cb4e2018-01-20 19:30:13 +01003978
3979 memset(activity, 0, sizeof(activity));
Willy Tarreau89d467c2016-11-23 11:02:40 +01003980 return 1;
3981}
3982
3983
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02003984static int cli_parse_show_info(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003985{
Willy Tarreaud25fc792016-12-16 12:33:47 +01003986 appctx->ctx.stats.scope_str = 0;
3987 appctx->ctx.stats.scope_len = 0;
3988 appctx->ctx.stats.flags = 0;
3989
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003990 if (strcmp(args[2], "typed") == 0)
3991 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
Simon Horman05ee2132017-01-04 09:37:25 +01003992 else if (strcmp(args[2], "json") == 0)
3993 appctx->ctx.stats.flags |= STAT_FMT_JSON;
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003994 return 0;
3995}
3996
3997
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02003998static int cli_parse_show_stat(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau2b812e22016-11-22 16:18:05 +01003999{
Willy Tarreaud25fc792016-12-16 12:33:47 +01004000 appctx->ctx.stats.scope_str = 0;
4001 appctx->ctx.stats.scope_len = 0;
4002 appctx->ctx.stats.flags = 0;
4003
Willy Tarreau2b812e22016-11-22 16:18:05 +01004004 if (*args[2] && *args[3] && *args[4]) {
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01004005 struct proxy *px;
4006
4007 px = proxy_find_by_name(args[2], 0, 0);
4008 if (px)
4009 appctx->ctx.stats.iid = px->uuid;
4010 else
4011 appctx->ctx.stats.iid = atoi(args[2]);
4012
4013 if (!appctx->ctx.stats.iid) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004014 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01004015 appctx->ctx.cli.msg = "No such proxy.\n";
4016 appctx->st0 = CLI_ST_PRINT;
4017 return 1;
4018 }
4019
Willy Tarreau2b812e22016-11-22 16:18:05 +01004020 appctx->ctx.stats.flags |= STAT_BOUND;
Willy Tarreau2b812e22016-11-22 16:18:05 +01004021 appctx->ctx.stats.type = atoi(args[3]);
4022 appctx->ctx.stats.sid = atoi(args[4]);
4023 if (strcmp(args[5], "typed") == 0)
4024 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
Simon Horman05ee2132017-01-04 09:37:25 +01004025 else if (strcmp(args[5], "json") == 0)
4026 appctx->ctx.stats.flags |= STAT_FMT_JSON;
Willy Tarreau2b812e22016-11-22 16:18:05 +01004027 }
4028 else if (strcmp(args[2], "typed") == 0)
4029 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
Simon Horman05ee2132017-01-04 09:37:25 +01004030 else if (strcmp(args[2], "json") == 0)
4031 appctx->ctx.stats.flags |= STAT_FMT_JSON;
Willy Tarreau2b812e22016-11-22 16:18:05 +01004032
Willy Tarreau2b812e22016-11-22 16:18:05 +01004033 return 0;
4034}
4035
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004036static int cli_io_handler_dump_info(struct appctx *appctx)
4037{
4038 return stats_dump_info_to_buffer(appctx->owner);
4039}
4040
Willy Tarreau2b812e22016-11-22 16:18:05 +01004041/* This I/O handler runs as an applet embedded in a stream interface. It is
4042 * used to send raw stats over a socket.
4043 */
4044static int cli_io_handler_dump_stat(struct appctx *appctx)
4045{
Christopher Fauletef779222018-10-31 08:47:01 +01004046 return stats_dump_stat_to_buffer(appctx->owner, NULL, NULL);
Willy Tarreau2b812e22016-11-22 16:18:05 +01004047}
4048
Simon Horman6f6bb382017-01-04 09:37:26 +01004049static int cli_io_handler_dump_json_schema(struct appctx *appctx)
4050{
4051 return stats_dump_json_schema_to_buffer(appctx->owner);
4052}
4053
Willy Tarreau2b812e22016-11-22 16:18:05 +01004054/* register cli keywords */
4055static struct cli_kw_list cli_kws = {{ },{
Willy Tarreau89d467c2016-11-23 11:02:40 +01004056 { { "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 +01004057 { { "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 +01004058 { { "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 +01004059 { { "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 +01004060 {{},}
4061}};
4062
Willy Tarreau0108d902018-11-25 19:14:37 +01004063INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
4064
William Lallemand74c24fb2016-11-21 17:18:36 +01004065struct applet http_stats_applet = {
4066 .obj_type = OBJ_TYPE_APPLET,
4067 .name = "<STATS>", /* used for logging */
4068 .fct = http_stats_io_handler,
4069 .release = NULL,
4070};
4071
William Lallemand74c24fb2016-11-21 17:18:36 +01004072/*
4073 * Local variables:
4074 * c-indent-level: 8
4075 * c-basic-offset: 8
4076 * End:
4077 */