blob: f44f5eef7c40223beab7043df652e0392b79644b [file] [log] [blame]
William Lallemand74c24fb2016-11-21 17:18:36 +01001/*
2 * Functions dedicated to statistics output and the stats socket
3 *
4 * Copyright 2000-2012 Willy Tarreau <w@1wt.eu>
5 * Copyright 2007-2009 Krzysztof Piotr Oledzki <ole@ans.pl>
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
11 *
12 */
13
14#include <ctype.h>
15#include <errno.h>
16#include <fcntl.h>
17#include <stdio.h>
18#include <stdlib.h>
19#include <string.h>
20#include <pwd.h>
21#include <grp.h>
22
23#include <sys/socket.h>
24#include <sys/stat.h>
25#include <sys/types.h>
26
27#include <common/cfgparse.h>
28#include <common/compat.h>
29#include <common/config.h>
30#include <common/debug.h>
Willy Tarreau35b51c62018-09-10 15:38:55 +020031#include <common/http.h>
Willy Tarreaub96b77e2018-12-11 10:22:41 +010032#include <common/htx.h>
Willy Tarreau0108d902018-11-25 19:14:37 +010033#include <common/initcall.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010034#include <common/memory.h>
35#include <common/mini-clist.h>
36#include <common/standard.h>
37#include <common/ticks.h>
38#include <common/time.h>
39#include <common/uri_auth.h>
40#include <common/version.h>
41#include <common/base64.h>
42
43#include <types/applet.h>
Willy Tarreau2b812e22016-11-22 16:18:05 +010044#include <types/cli.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010045#include <types/global.h>
46#include <types/dns.h>
William Lallemand9ed62032016-11-21 17:49:11 +010047#include <types/stats.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010048
49#include <proto/backend.h>
50#include <proto/channel.h>
51#include <proto/checks.h>
Willy Tarreau2b812e22016-11-22 16:18:05 +010052#include <proto/cli.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010053#include <proto/compression.h>
Baptiste Assmann333939c2019-01-21 08:34:50 +010054#include <proto/dns.h>
William Lallemand9ed62032016-11-21 17:49:11 +010055#include <proto/stats.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010056#include <proto/fd.h>
57#include <proto/freq_ctr.h>
58#include <proto/frontend.h>
Christopher Fauleted7a0662019-01-14 11:07:34 +010059#include <proto/http_htx.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010060#include <proto/log.h>
61#include <proto/pattern.h>
62#include <proto/pipe.h>
63#include <proto/listener.h>
64#include <proto/map.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010065#include <proto/proxy.h>
66#include <proto/sample.h>
67#include <proto/session.h>
Willy Tarreauc125cef2019-05-10 09:58:43 +020068#include <proto/ssl_sock.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010069#include <proto/stream.h>
70#include <proto/server.h>
71#include <proto/raw_sock.h>
72#include <proto/stream_interface.h>
73#include <proto/task.h>
74
William Lallemand74c24fb2016-11-21 17:18:36 +010075
Christopher Faulet2b9b6782019-02-27 16:42:58 +010076/* status codes available for the stats admin page (strictly 4 chars length) */
77const char *stat_status_codes[STAT_STATUS_SIZE] = {
78 [STAT_STATUS_DENY] = "DENY",
79 [STAT_STATUS_DONE] = "DONE",
80 [STAT_STATUS_ERRP] = "ERRP",
81 [STAT_STATUS_EXCD] = "EXCD",
82 [STAT_STATUS_NONE] = "NONE",
83 [STAT_STATUS_PART] = "PART",
84 [STAT_STATUS_UNKN] = "UNKN",
85 [STAT_STATUS_IVAL] = "IVAL",
86};
87
Willy Tarreau0baac8c2016-11-22 16:36:53 +010088/* These are the field names for each INF_* field position. Please pay attention
89 * to always use the exact same name except that the strings for new names must
90 * be lower case or CamelCase while the enum entries must be upper case.
91 */
92const char *info_field_names[INF_TOTAL_FIELDS] = {
93 [INF_NAME] = "Name",
94 [INF_VERSION] = "Version",
95 [INF_RELEASE_DATE] = "Release_date",
Yves Lafon95317282018-02-26 11:10:37 +010096 [INF_NBTHREAD] = "Nbthread",
Willy Tarreau0baac8c2016-11-22 16:36:53 +010097 [INF_NBPROC] = "Nbproc",
98 [INF_PROCESS_NUM] = "Process_num",
99 [INF_PID] = "Pid",
100 [INF_UPTIME] = "Uptime",
101 [INF_UPTIME_SEC] = "Uptime_sec",
102 [INF_MEMMAX_MB] = "Memmax_MB",
103 [INF_POOL_ALLOC_MB] = "PoolAlloc_MB",
104 [INF_POOL_USED_MB] = "PoolUsed_MB",
105 [INF_POOL_FAILED] = "PoolFailed",
106 [INF_ULIMIT_N] = "Ulimit-n",
107 [INF_MAXSOCK] = "Maxsock",
108 [INF_MAXCONN] = "Maxconn",
109 [INF_HARD_MAXCONN] = "Hard_maxconn",
110 [INF_CURR_CONN] = "CurrConns",
111 [INF_CUM_CONN] = "CumConns",
112 [INF_CUM_REQ] = "CumReq",
113 [INF_MAX_SSL_CONNS] = "MaxSslConns",
114 [INF_CURR_SSL_CONNS] = "CurrSslConns",
115 [INF_CUM_SSL_CONNS] = "CumSslConns",
116 [INF_MAXPIPES] = "Maxpipes",
117 [INF_PIPES_USED] = "PipesUsed",
118 [INF_PIPES_FREE] = "PipesFree",
119 [INF_CONN_RATE] = "ConnRate",
120 [INF_CONN_RATE_LIMIT] = "ConnRateLimit",
121 [INF_MAX_CONN_RATE] = "MaxConnRate",
122 [INF_SESS_RATE] = "SessRate",
123 [INF_SESS_RATE_LIMIT] = "SessRateLimit",
124 [INF_MAX_SESS_RATE] = "MaxSessRate",
125 [INF_SSL_RATE] = "SslRate",
126 [INF_SSL_RATE_LIMIT] = "SslRateLimit",
127 [INF_MAX_SSL_RATE] = "MaxSslRate",
128 [INF_SSL_FRONTEND_KEY_RATE] = "SslFrontendKeyRate",
129 [INF_SSL_FRONTEND_MAX_KEY_RATE] = "SslFrontendMaxKeyRate",
130 [INF_SSL_FRONTEND_SESSION_REUSE_PCT] = "SslFrontendSessionReuse_pct",
131 [INF_SSL_BACKEND_KEY_RATE] = "SslBackendKeyRate",
132 [INF_SSL_BACKEND_MAX_KEY_RATE] = "SslBackendMaxKeyRate",
133 [INF_SSL_CACHE_LOOKUPS] = "SslCacheLookups",
134 [INF_SSL_CACHE_MISSES] = "SslCacheMisses",
135 [INF_COMPRESS_BPS_IN] = "CompressBpsIn",
136 [INF_COMPRESS_BPS_OUT] = "CompressBpsOut",
137 [INF_COMPRESS_BPS_RATE_LIM] = "CompressBpsRateLim",
138 [INF_ZLIB_MEM_USAGE] = "ZlibMemUsage",
139 [INF_MAX_ZLIB_MEM_USAGE] = "MaxZlibMemUsage",
140 [INF_TASKS] = "Tasks",
141 [INF_RUN_QUEUE] = "Run_queue",
142 [INF_IDLE_PCT] = "Idle_pct",
143 [INF_NODE] = "node",
144 [INF_DESCRIPTION] = "description",
Willy Tarreau00098ea2018-11-05 14:38:13 +0100145 [INF_STOPPING] = "Stopping",
146 [INF_JOBS] = "Jobs",
William Lallemanda7199262018-11-16 16:57:20 +0100147 [INF_UNSTOPPABLE_JOBS] = "Unstoppable Jobs",
Willy Tarreau00098ea2018-11-05 14:38:13 +0100148 [INF_LISTENERS] = "Listeners",
Willy Tarreau199ad242018-11-05 16:31:22 +0100149 [INF_ACTIVE_PEERS] = "ActivePeers",
Willy Tarreau2d372c22018-11-05 17:12:27 +0100150 [INF_CONNECTED_PEERS] = "ConnectedPeers",
Willy Tarreau13ef7732018-11-12 07:25:28 +0100151 [INF_DROPPED_LOGS] = "DroppedLogs",
Willy Tarreaubeb859a2018-11-22 18:07:59 +0100152 [INF_BUSY_POLLING] = "BusyPolling",
Baptiste Assmann333939c2019-01-21 08:34:50 +0100153 [INF_FAILED_RESOLUTIONS] = "FailedResolutions",
Willy Tarreau7cf0e452019-05-23 11:39:14 +0200154 [INF_TOTAL_BYTES_OUT] = "TotalBytesOut",
155 [INF_BYTES_OUT_RATE] = "BytesOutRate",
Willy Tarreau0baac8c2016-11-22 16:36:53 +0100156};
157
William Lallemand74c24fb2016-11-21 17:18:36 +0100158const char *stat_field_names[ST_F_TOTAL_FIELDS] = {
159 [ST_F_PXNAME] = "pxname",
160 [ST_F_SVNAME] = "svname",
161 [ST_F_QCUR] = "qcur",
162 [ST_F_QMAX] = "qmax",
163 [ST_F_SCUR] = "scur",
164 [ST_F_SMAX] = "smax",
165 [ST_F_SLIM] = "slim",
166 [ST_F_STOT] = "stot",
167 [ST_F_BIN] = "bin",
168 [ST_F_BOUT] = "bout",
169 [ST_F_DREQ] = "dreq",
170 [ST_F_DRESP] = "dresp",
171 [ST_F_EREQ] = "ereq",
172 [ST_F_ECON] = "econ",
173 [ST_F_ERESP] = "eresp",
174 [ST_F_WRETR] = "wretr",
175 [ST_F_WREDIS] = "wredis",
176 [ST_F_STATUS] = "status",
177 [ST_F_WEIGHT] = "weight",
178 [ST_F_ACT] = "act",
179 [ST_F_BCK] = "bck",
180 [ST_F_CHKFAIL] = "chkfail",
181 [ST_F_CHKDOWN] = "chkdown",
182 [ST_F_LASTCHG] = "lastchg",
183 [ST_F_DOWNTIME] = "downtime",
184 [ST_F_QLIMIT] = "qlimit",
185 [ST_F_PID] = "pid",
186 [ST_F_IID] = "iid",
187 [ST_F_SID] = "sid",
188 [ST_F_THROTTLE] = "throttle",
189 [ST_F_LBTOT] = "lbtot",
190 [ST_F_TRACKED] = "tracked",
191 [ST_F_TYPE] = "type",
192 [ST_F_RATE] = "rate",
193 [ST_F_RATE_LIM] = "rate_lim",
194 [ST_F_RATE_MAX] = "rate_max",
195 [ST_F_CHECK_STATUS] = "check_status",
196 [ST_F_CHECK_CODE] = "check_code",
197 [ST_F_CHECK_DURATION] = "check_duration",
198 [ST_F_HRSP_1XX] = "hrsp_1xx",
199 [ST_F_HRSP_2XX] = "hrsp_2xx",
200 [ST_F_HRSP_3XX] = "hrsp_3xx",
201 [ST_F_HRSP_4XX] = "hrsp_4xx",
202 [ST_F_HRSP_5XX] = "hrsp_5xx",
203 [ST_F_HRSP_OTHER] = "hrsp_other",
204 [ST_F_HANAFAIL] = "hanafail",
205 [ST_F_REQ_RATE] = "req_rate",
206 [ST_F_REQ_RATE_MAX] = "req_rate_max",
207 [ST_F_REQ_TOT] = "req_tot",
208 [ST_F_CLI_ABRT] = "cli_abrt",
209 [ST_F_SRV_ABRT] = "srv_abrt",
210 [ST_F_COMP_IN] = "comp_in",
211 [ST_F_COMP_OUT] = "comp_out",
212 [ST_F_COMP_BYP] = "comp_byp",
213 [ST_F_COMP_RSP] = "comp_rsp",
214 [ST_F_LASTSESS] = "lastsess",
215 [ST_F_LAST_CHK] = "last_chk",
216 [ST_F_LAST_AGT] = "last_agt",
217 [ST_F_QTIME] = "qtime",
218 [ST_F_CTIME] = "ctime",
219 [ST_F_RTIME] = "rtime",
220 [ST_F_TTIME] = "ttime",
221 [ST_F_AGENT_STATUS] = "agent_status",
222 [ST_F_AGENT_CODE] = "agent_code",
223 [ST_F_AGENT_DURATION] = "agent_duration",
224 [ST_F_CHECK_DESC] = "check_desc",
225 [ST_F_AGENT_DESC] = "agent_desc",
226 [ST_F_CHECK_RISE] = "check_rise",
227 [ST_F_CHECK_FALL] = "check_fall",
228 [ST_F_CHECK_HEALTH] = "check_health",
229 [ST_F_AGENT_RISE] = "agent_rise",
230 [ST_F_AGENT_FALL] = "agent_fall",
231 [ST_F_AGENT_HEALTH] = "agent_health",
232 [ST_F_ADDR] = "addr",
233 [ST_F_COOKIE] = "cookie",
234 [ST_F_MODE] = "mode",
235 [ST_F_ALGO] = "algo",
236 [ST_F_CONN_RATE] = "conn_rate",
237 [ST_F_CONN_RATE_MAX] = "conn_rate_max",
238 [ST_F_CONN_TOT] = "conn_tot",
239 [ST_F_INTERCEPTED] = "intercepted",
240 [ST_F_DCON] = "dcon",
241 [ST_F_DSES] = "dses",
Tim Duesterhus3fd19732018-05-27 20:35:08 +0200242 [ST_F_WREW] = "wrew",
Willy Tarreauf1573842018-12-14 11:35:36 +0100243 [ST_F_CONNECT] = "connect",
244 [ST_F_REUSE] = "reuse",
Willy Tarreaua1214a52018-12-14 14:00:25 +0100245 [ST_F_CACHE_LOOKUPS] = "cache_lookups",
246 [ST_F_CACHE_HITS] = "cache_hits",
Adis Nezirovica46b1422019-09-13 11:43:03 +0200247 [ST_F_SRV_ICUR] = "srv_icur",
248 [ST_F_SRV_ILIM] = "src_ilim"
William Lallemand74c24fb2016-11-21 17:18:36 +0100249};
250
Willy Tarreau0baac8c2016-11-22 16:36:53 +0100251/* one line of info */
Christopher Faulet1bc04c72017-10-29 20:14:08 +0100252static THREAD_LOCAL struct field info[INF_TOTAL_FIELDS];
William Lallemand74c24fb2016-11-21 17:18:36 +0100253/* one line of stats */
Christopher Faulet1bc04c72017-10-29 20:14:08 +0100254static THREAD_LOCAL struct field stats[ST_F_TOTAL_FIELDS];
William Lallemand74c24fb2016-11-21 17:18:36 +0100255
Christopher Faulet6338a082019-09-09 15:50:54 +0200256static void stats_dump_json_schema(struct buffer *out);
William Lallemand74c24fb2016-11-21 17:18:36 +0100257
Christopher Fauletef779222018-10-31 08:47:01 +0100258static int stats_putchk(struct channel *chn, struct htx *htx, struct buffer *chk)
259{
260 if (htx) {
Christopher Faulet69fc88c2019-01-07 14:27:53 +0100261 if (chk->data >= channel_htx_recv_max(chn, htx))
262 return 0;
Willy Tarreau0a7ef022019-05-28 10:30:11 +0200263 if (!htx_add_data_atonce(htx, ist2(chk->area, chk->data)))
Christopher Fauletef779222018-10-31 08:47:01 +0100264 return 0;
Christopher Faulet5adbeeb2019-01-02 14:34:39 +0100265 channel_add_input(chn, chk->data);
Christopher Fauletef779222018-10-31 08:47:01 +0100266 chk->data = 0;
Christopher Fauletef779222018-10-31 08:47:01 +0100267 }
268 else {
269 if (ci_putchk(chn, chk) == -1)
270 return 0;
271 }
272 return 1;
273}
Willy Tarreau0baac8c2016-11-22 16:36:53 +0100274
Christopher Fauleted7a0662019-01-14 11:07:34 +0100275static const char *stats_scope_ptr(struct appctx *appctx, struct stream_interface *si)
276{
Christopher Fauletb7f88902019-07-15 21:56:43 +0200277 struct channel *req = si_oc(si);
278 struct htx *htx = htxbuf(&req->buf);
279 struct htx_blk *blk;
280 struct ist uri;
Christopher Fauleted7a0662019-01-14 11:07:34 +0100281
Christopher Fauletb7f88902019-07-15 21:56:43 +0200282 blk = htx_get_head_blk(htx);
283 BUG_ON(htx_get_blk_type(blk) != HTX_BLK_REQ_SL);
284 ALREADY_CHECKED(blk);
285 uri = htx_sl_req_uri(htx_get_blk_ptr(htx, blk));
286 return uri.ptr + appctx->ctx.stats.scope_str;
Christopher Fauleted7a0662019-01-14 11:07:34 +0100287}
288
William Lallemand74c24fb2016-11-21 17:18:36 +0100289/*
290 * http_stats_io_handler()
291 * -> stats_dump_stat_to_buffer() // same as above, but used for CSV or HTML
292 * -> stats_dump_csv_header() // emits the CSV headers (same as above)
Simon Horman05ee2132017-01-04 09:37:25 +0100293 * -> stats_dump_json_header() // emits the JSON headers (same as above)
William Lallemand74c24fb2016-11-21 17:18:36 +0100294 * -> stats_dump_html_head() // emits the HTML headers
295 * -> stats_dump_html_info() // emits the equivalent of "show info" at the top
296 * -> stats_dump_proxy_to_buffer() // same as above, valid for CSV and HTML
297 * -> stats_dump_html_px_hdr()
298 * -> stats_dump_fe_stats()
299 * -> stats_dump_li_stats()
300 * -> stats_dump_sv_stats()
301 * -> stats_dump_be_stats()
302 * -> stats_dump_html_px_end()
303 * -> stats_dump_html_end() // emits HTML trailer
Simon Horman05ee2132017-01-04 09:37:25 +0100304 * -> stats_dump_json_end() // emits JSON trailer
William Lallemand74c24fb2016-11-21 17:18:36 +0100305 */
306
307
William Lallemand74c24fb2016-11-21 17:18:36 +0100308/* Dumps the stats CSV header to the trash buffer which. The caller is responsible
309 * for clearing it if needed.
310 * NOTE: Some tools happen to rely on the field position instead of its name,
311 * so please only append new fields at the end, never in the middle.
312 */
313static void stats_dump_csv_header()
314{
315 int field;
316
317 chunk_appendf(&trash, "# ");
318 for (field = 0; field < ST_F_TOTAL_FIELDS; field++)
319 chunk_appendf(&trash, "%s,", stat_field_names[field]);
320
321 chunk_appendf(&trash, "\n");
322}
323
324
325/* Emits a stats field without any surrounding element and properly encoded to
326 * resist CSV output. Returns non-zero on success, 0 if the buffer is full.
327 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200328int stats_emit_raw_data_field(struct buffer *out, const struct field *f)
William Lallemand74c24fb2016-11-21 17:18:36 +0100329{
330 switch (field_format(f, 0)) {
331 case FF_EMPTY: return 1;
332 case FF_S32: return chunk_appendf(out, "%d", f->u.s32);
333 case FF_U32: return chunk_appendf(out, "%u", f->u.u32);
334 case FF_S64: return chunk_appendf(out, "%lld", (long long)f->u.s64);
335 case FF_U64: return chunk_appendf(out, "%llu", (unsigned long long)f->u.u64);
336 case FF_STR: return csv_enc_append(field_str(f, 0), 1, out) != NULL;
337 default: return chunk_appendf(out, "[INCORRECT_FIELD_TYPE_%08x]", f->type);
338 }
339}
340
341/* Emits a stats field prefixed with its type. No CSV encoding is prepared, the
342 * output is supposed to be used on its own line. Returns non-zero on success, 0
343 * if the buffer is full.
344 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200345int stats_emit_typed_data_field(struct buffer *out, const struct field *f)
William Lallemand74c24fb2016-11-21 17:18:36 +0100346{
347 switch (field_format(f, 0)) {
348 case FF_EMPTY: return 1;
349 case FF_S32: return chunk_appendf(out, "s32:%d", f->u.s32);
350 case FF_U32: return chunk_appendf(out, "u32:%u", f->u.u32);
351 case FF_S64: return chunk_appendf(out, "s64:%lld", (long long)f->u.s64);
352 case FF_U64: return chunk_appendf(out, "u64:%llu", (unsigned long long)f->u.u64);
353 case FF_STR: return chunk_appendf(out, "str:%s", field_str(f, 0));
354 default: return chunk_appendf(out, "%08x:?", f->type);
355 }
356}
357
Simon Horman05ee2132017-01-04 09:37:25 +0100358/* Limit JSON integer values to the range [-(2**53)+1, (2**53)-1] as per
359 * the recommendation for interoperable integers in section 6 of RFC 7159.
360 */
361#define JSON_INT_MAX ((1ULL << 53) - 1)
362#define JSON_INT_MIN (0 - JSON_INT_MAX)
363
364/* Emits a stats field value and its type in JSON.
365 * Returns non-zero on success, 0 on error.
366 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200367int stats_emit_json_data_field(struct buffer *out, const struct field *f)
Simon Horman05ee2132017-01-04 09:37:25 +0100368{
369 int old_len;
370 char buf[20];
371 const char *type, *value = buf, *quote = "";
372
373 switch (field_format(f, 0)) {
374 case FF_EMPTY: return 1;
375 case FF_S32: type = "\"s32\"";
376 snprintf(buf, sizeof(buf), "%d", f->u.s32);
377 break;
378 case FF_U32: type = "\"u32\"";
379 snprintf(buf, sizeof(buf), "%u", f->u.u32);
380 break;
381 case FF_S64: type = "\"s64\"";
382 if (f->u.s64 < JSON_INT_MIN || f->u.s64 > JSON_INT_MAX)
383 return 0;
384 type = "\"s64\"";
385 snprintf(buf, sizeof(buf), "%lld", (long long)f->u.s64);
386 break;
387 case FF_U64: if (f->u.u64 > JSON_INT_MAX)
388 return 0;
389 type = "\"u64\"";
390 snprintf(buf, sizeof(buf), "%llu",
391 (unsigned long long) f->u.u64);
392 break;
393 case FF_STR: type = "\"str\"";
394 value = field_str(f, 0);
395 quote = "\"";
396 break;
397 default: snprintf(buf, sizeof(buf), "%u", f->type);
398 type = buf;
399 value = "unknown";
400 quote = "\"";
401 break;
402 }
403
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200404 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100405 chunk_appendf(out, ",\"value\":{\"type\":%s,\"value\":%s%s%s}",
406 type, quote, value, quote);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200407 return !(old_len == out->data);
Simon Horman05ee2132017-01-04 09:37:25 +0100408}
409
William Lallemand74c24fb2016-11-21 17:18:36 +0100410/* Emits an encoding of the field type on 3 characters followed by a delimiter.
411 * Returns non-zero on success, 0 if the buffer is full.
412 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200413int stats_emit_field_tags(struct buffer *out, const struct field *f,
414 char delim)
William Lallemand74c24fb2016-11-21 17:18:36 +0100415{
416 char origin, nature, scope;
417
418 switch (field_origin(f, 0)) {
419 case FO_METRIC: origin = 'M'; break;
420 case FO_STATUS: origin = 'S'; break;
421 case FO_KEY: origin = 'K'; break;
422 case FO_CONFIG: origin = 'C'; break;
423 case FO_PRODUCT: origin = 'P'; break;
424 default: origin = '?'; break;
425 }
426
427 switch (field_nature(f, 0)) {
428 case FN_GAUGE: nature = 'G'; break;
429 case FN_LIMIT: nature = 'L'; break;
430 case FN_MIN: nature = 'm'; break;
431 case FN_MAX: nature = 'M'; break;
432 case FN_RATE: nature = 'R'; break;
433 case FN_COUNTER: nature = 'C'; break;
434 case FN_DURATION: nature = 'D'; break;
435 case FN_AGE: nature = 'A'; break;
436 case FN_TIME: nature = 'T'; break;
437 case FN_NAME: nature = 'N'; break;
438 case FN_OUTPUT: nature = 'O'; break;
439 case FN_AVG: nature = 'a'; break;
440 default: nature = '?'; break;
441 }
442
443 switch (field_scope(f, 0)) {
444 case FS_PROCESS: scope = 'P'; break;
445 case FS_SERVICE: scope = 'S'; break;
446 case FS_SYSTEM: scope = 's'; break;
447 case FS_CLUSTER: scope = 'C'; break;
448 default: scope = '?'; break;
449 }
450
451 return chunk_appendf(out, "%c%c%c%c", origin, nature, scope, delim);
452}
453
Simon Horman05ee2132017-01-04 09:37:25 +0100454/* Emits an encoding of the field type as JSON.
455 * Returns non-zero on success, 0 if the buffer is full.
456 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200457int stats_emit_json_field_tags(struct buffer *out, const struct field *f)
Simon Horman05ee2132017-01-04 09:37:25 +0100458{
459 const char *origin, *nature, *scope;
460 int old_len;
461
462 switch (field_origin(f, 0)) {
463 case FO_METRIC: origin = "Metric"; break;
464 case FO_STATUS: origin = "Status"; break;
465 case FO_KEY: origin = "Key"; break;
466 case FO_CONFIG: origin = "Config"; break;
467 case FO_PRODUCT: origin = "Product"; break;
468 default: origin = "Unknown"; break;
469 }
470
471 switch (field_nature(f, 0)) {
472 case FN_GAUGE: nature = "Gauge"; break;
473 case FN_LIMIT: nature = "Limit"; break;
474 case FN_MIN: nature = "Min"; break;
475 case FN_MAX: nature = "Max"; break;
476 case FN_RATE: nature = "Rate"; break;
477 case FN_COUNTER: nature = "Counter"; break;
478 case FN_DURATION: nature = "Duration"; break;
479 case FN_AGE: nature = "Age"; break;
480 case FN_TIME: nature = "Time"; break;
481 case FN_NAME: nature = "Name"; break;
482 case FN_OUTPUT: nature = "Output"; break;
483 case FN_AVG: nature = "Avg"; break;
484 default: nature = "Unknown"; break;
485 }
486
487 switch (field_scope(f, 0)) {
488 case FS_PROCESS: scope = "Process"; break;
489 case FS_SERVICE: scope = "Service"; break;
490 case FS_SYSTEM: scope = "System"; break;
491 case FS_CLUSTER: scope = "Cluster"; break;
492 default: scope = "Unknown"; break;
493 }
494
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200495 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100496 chunk_appendf(out, "\"tags\":{"
497 "\"origin\":\"%s\","
498 "\"nature\":\"%s\","
499 "\"scope\":\"%s\""
500 "}", origin, nature, scope);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200501 return !(old_len == out->data);
Simon Horman05ee2132017-01-04 09:37:25 +0100502}
William Lallemand74c24fb2016-11-21 17:18:36 +0100503
504/* Dump all fields from <stats> into <out> using CSV format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200505static int stats_dump_fields_csv(struct buffer *out,
506 const struct field *stats)
William Lallemand74c24fb2016-11-21 17:18:36 +0100507{
508 int field;
509
510 for (field = 0; field < ST_F_TOTAL_FIELDS; field++) {
511 if (!stats_emit_raw_data_field(out, &stats[field]))
512 return 0;
513 if (!chunk_strcat(out, ","))
514 return 0;
515 }
516 chunk_strcat(out, "\n");
517 return 1;
518}
519
520/* Dump all fields from <stats> into <out> using a typed "field:desc:type:value" format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200521static int stats_dump_fields_typed(struct buffer *out,
522 const struct field *stats)
William Lallemand74c24fb2016-11-21 17:18:36 +0100523{
524 int field;
525
526 for (field = 0; field < ST_F_TOTAL_FIELDS; field++) {
527 if (!stats[field].type)
528 continue;
529
530 chunk_appendf(out, "%c.%u.%u.%d.%s.%u:",
531 stats[ST_F_TYPE].u.u32 == STATS_TYPE_FE ? 'F' :
532 stats[ST_F_TYPE].u.u32 == STATS_TYPE_BE ? 'B' :
533 stats[ST_F_TYPE].u.u32 == STATS_TYPE_SO ? 'L' :
534 stats[ST_F_TYPE].u.u32 == STATS_TYPE_SV ? 'S' :
535 '?',
536 stats[ST_F_IID].u.u32, stats[ST_F_SID].u.u32,
537 field, stat_field_names[field], stats[ST_F_PID].u.u32);
538
539 if (!stats_emit_field_tags(out, &stats[field], ':'))
540 return 0;
541 if (!stats_emit_typed_data_field(out, &stats[field]))
542 return 0;
543 if (!chunk_strcat(out, "\n"))
544 return 0;
545 }
546 return 1;
547}
548
Simon Horman05ee2132017-01-04 09:37:25 +0100549/* Dump all fields from <stats> into <out> using the "show info json" format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200550static int stats_dump_json_info_fields(struct buffer *out,
Simon Horman05ee2132017-01-04 09:37:25 +0100551 const struct field *info)
552{
553 int field;
554 int started = 0;
555
556 if (!chunk_strcat(out, "["))
557 return 0;
558
559 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
560 int old_len;
561
562 if (!field_format(info, field))
563 continue;
564
565 if (started && !chunk_strcat(out, ","))
566 goto err;
567 started = 1;
568
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200569 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100570 chunk_appendf(out,
571 "{\"field\":{\"pos\":%d,\"name\":\"%s\"},"
572 "\"processNum\":%u,",
573 field, info_field_names[field],
574 info[INF_PROCESS_NUM].u.u32);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200575 if (old_len == out->data)
Simon Horman05ee2132017-01-04 09:37:25 +0100576 goto err;
577
578 if (!stats_emit_json_field_tags(out, &info[field]))
579 goto err;
580
581 if (!stats_emit_json_data_field(out, &info[field]))
582 goto err;
583
584 if (!chunk_strcat(out, "}"))
585 goto err;
586 }
587
588 if (!chunk_strcat(out, "]"))
589 goto err;
590 return 1;
591
592err:
593 chunk_reset(out);
594 chunk_appendf(out, "{\"errorStr\":\"output buffer too short\"}");
595 return 0;
596}
597
598/* Dump all fields from <stats> into <out> using a typed "field:desc:type:value" format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200599static int stats_dump_fields_json(struct buffer *out,
600 const struct field *stats,
Simon Horman05ee2132017-01-04 09:37:25 +0100601 int first_stat)
602{
603 int field;
604 int started = 0;
605
606 if (!first_stat && !chunk_strcat(out, ","))
607 return 0;
608 if (!chunk_strcat(out, "["))
609 return 0;
610
611 for (field = 0; field < ST_F_TOTAL_FIELDS; field++) {
612 const char *obj_type;
613 int old_len;
614
615 if (!stats[field].type)
616 continue;
617
618 if (started && !chunk_strcat(out, ","))
619 goto err;
620 started = 1;
621
622 switch (stats[ST_F_TYPE].u.u32) {
623 case STATS_TYPE_FE: obj_type = "Frontend"; break;
624 case STATS_TYPE_BE: obj_type = "Backend"; break;
625 case STATS_TYPE_SO: obj_type = "Listener"; break;
626 case STATS_TYPE_SV: obj_type = "Server"; break;
627 default: obj_type = "Unknown"; break;
628 }
629
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200630 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100631 chunk_appendf(out,
632 "{"
633 "\"objType\":\"%s\","
634 "\"proxyId\":%d,"
635 "\"id\":%d,"
636 "\"field\":{\"pos\":%d,\"name\":\"%s\"},"
637 "\"processNum\":%u,",
638 obj_type, stats[ST_F_IID].u.u32,
639 stats[ST_F_SID].u.u32, field,
640 stat_field_names[field], stats[ST_F_PID].u.u32);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200641 if (old_len == out->data)
Simon Horman05ee2132017-01-04 09:37:25 +0100642 goto err;
643
644 if (!stats_emit_json_field_tags(out, &stats[field]))
645 goto err;
646
647 if (!stats_emit_json_data_field(out, &stats[field]))
648 goto err;
649
650 if (!chunk_strcat(out, "}"))
651 goto err;
652 }
653
654 if (!chunk_strcat(out, "]"))
655 goto err;
656
657 return 1;
658
659err:
660 chunk_reset(out);
661 if (!first_stat)
662 chunk_strcat(out, ",");
663 chunk_appendf(out, "{\"errorStr\":\"output buffer too short\"}");
664 return 0;
665}
666
William Lallemand74c24fb2016-11-21 17:18:36 +0100667/* Dump all fields from <stats> into <out> using the HTML format. A column is
668 * reserved for the checkbox is ST_SHOWADMIN is set in <flags>. Some extra info
669 * are provided if ST_SHLGNDS is present in <flags>.
670 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200671static int stats_dump_fields_html(struct buffer *out,
672 const struct field *stats,
673 unsigned int flags)
William Lallemand74c24fb2016-11-21 17:18:36 +0100674{
Willy Tarreau83061a82018-07-13 11:56:34 +0200675 struct buffer src;
William Lallemand74c24fb2016-11-21 17:18:36 +0100676
677 if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_FE) {
678 chunk_appendf(out,
679 /* name, queue */
680 "<tr class=\"frontend\">");
681
682 if (flags & ST_SHOWADMIN) {
683 /* Column sub-heading for Enable or Disable server */
684 chunk_appendf(out, "<td></td>");
685 }
686
687 chunk_appendf(out,
688 "<td class=ac>"
689 "<a name=\"%s/Frontend\"></a>"
690 "<a class=lfsb href=\"#%s/Frontend\">Frontend</a></td>"
691 "<td colspan=3></td>"
692 "",
693 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_PXNAME));
694
695 chunk_appendf(out,
696 /* sessions rate : current */
697 "<td><u>%s<div class=tips><table class=det>"
698 "<tr><th>Current connection rate:</th><td>%s/s</td></tr>"
699 "<tr><th>Current session rate:</th><td>%s/s</td></tr>"
700 "",
701 U2H(stats[ST_F_RATE].u.u32),
702 U2H(stats[ST_F_CONN_RATE].u.u32),
703 U2H(stats[ST_F_RATE].u.u32));
704
705 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
706 chunk_appendf(out,
707 "<tr><th>Current request rate:</th><td>%s/s</td></tr>",
708 U2H(stats[ST_F_REQ_RATE].u.u32));
709
710 chunk_appendf(out,
711 "</table></div></u></td>"
712 /* sessions rate : max */
713 "<td><u>%s<div class=tips><table class=det>"
714 "<tr><th>Max connection rate:</th><td>%s/s</td></tr>"
715 "<tr><th>Max session rate:</th><td>%s/s</td></tr>"
716 "",
717 U2H(stats[ST_F_RATE_MAX].u.u32),
718 U2H(stats[ST_F_CONN_RATE_MAX].u.u32),
719 U2H(stats[ST_F_RATE_MAX].u.u32));
720
721 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
722 chunk_appendf(out,
723 "<tr><th>Max request rate:</th><td>%s/s</td></tr>",
724 U2H(stats[ST_F_REQ_RATE_MAX].u.u32));
725
726 chunk_appendf(out,
727 "</table></div></u></td>"
728 /* sessions rate : limit */
729 "<td>%s</td>",
730 LIM2A(stats[ST_F_RATE_LIM].u.u32, "-"));
731
732 chunk_appendf(out,
733 /* sessions: current, max, limit, total */
734 "<td>%s</td><td>%s</td><td>%s</td>"
735 "<td><u>%s<div class=tips><table class=det>"
736 "<tr><th>Cum. connections:</th><td>%s</td></tr>"
737 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
738 "",
739 U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SMAX].u.u32), U2H(stats[ST_F_SLIM].u.u32),
740 U2H(stats[ST_F_STOT].u.u64),
741 U2H(stats[ST_F_CONN_TOT].u.u64),
742 U2H(stats[ST_F_STOT].u.u64));
743
744 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
745 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
746 chunk_appendf(out,
747 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
748 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
749 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
750 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
751 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
752 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
753 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
754 "<tr><th>- other responses:</th><td>%s</td></tr>"
755 "<tr><th>Intercepted requests:</th><td>%s</td></tr>"
Willy Tarreaua1214a52018-12-14 14:00:25 +0100756 "<tr><th>Cache lookups:</th><td>%s</td></tr>"
757 "<tr><th>Cache hits:</th><td>%s</td><td>(%d%%)</td></tr>"
Willy Tarreau1b0f85e2018-05-28 15:12:40 +0200758 "<tr><th>Failed hdr rewrites:</th><td>%s</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +0100759 "",
760 U2H(stats[ST_F_REQ_TOT].u.u64),
761 U2H(stats[ST_F_HRSP_1XX].u.u64),
762 U2H(stats[ST_F_HRSP_2XX].u.u64),
763 U2H(stats[ST_F_COMP_RSP].u.u64),
764 stats[ST_F_HRSP_2XX].u.u64 ?
765 (int)(100 * stats[ST_F_COMP_RSP].u.u64 / stats[ST_F_HRSP_2XX].u.u64) : 0,
766 U2H(stats[ST_F_HRSP_3XX].u.u64),
767 U2H(stats[ST_F_HRSP_4XX].u.u64),
768 U2H(stats[ST_F_HRSP_5XX].u.u64),
769 U2H(stats[ST_F_HRSP_OTHER].u.u64),
Willy Tarreau1b0f85e2018-05-28 15:12:40 +0200770 U2H(stats[ST_F_INTERCEPTED].u.u64),
Willy Tarreaua1214a52018-12-14 14:00:25 +0100771 U2H(stats[ST_F_CACHE_LOOKUPS].u.u64),
772 U2H(stats[ST_F_CACHE_HITS].u.u64),
773 stats[ST_F_CACHE_LOOKUPS].u.u64 ?
774 (int)(100 * stats[ST_F_CACHE_HITS].u.u64 / stats[ST_F_CACHE_LOOKUPS].u.u64) : 0,
Willy Tarreau1b0f85e2018-05-28 15:12:40 +0200775 U2H(stats[ST_F_WREW].u.u64));
William Lallemand74c24fb2016-11-21 17:18:36 +0100776 }
777
778 chunk_appendf(out,
779 "</table></div></u></td>"
780 /* sessions: lbtot, lastsess */
781 "<td></td><td></td>"
782 /* bytes : in */
783 "<td>%s</td>"
784 "",
785 U2H(stats[ST_F_BIN].u.u64));
786
787 chunk_appendf(out,
788 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
789 "<td>%s%s<div class=tips><table class=det>"
790 "<tr><th>Response bytes in:</th><td>%s</td></tr>"
791 "<tr><th>Compression in:</th><td>%s</td></tr>"
792 "<tr><th>Compression out:</th><td>%s</td><td>(%d%%)</td></tr>"
793 "<tr><th>Compression bypass:</th><td>%s</td></tr>"
794 "<tr><th>Total bytes saved:</th><td>%s</td><td>(%d%%)</td></tr>"
795 "</table></div>%s</td>",
796 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "<u>":"",
797 U2H(stats[ST_F_BOUT].u.u64),
798 U2H(stats[ST_F_BOUT].u.u64),
799 U2H(stats[ST_F_COMP_IN].u.u64),
800 U2H(stats[ST_F_COMP_OUT].u.u64),
801 stats[ST_F_COMP_IN].u.u64 ? (int)(stats[ST_F_COMP_OUT].u.u64 * 100 / stats[ST_F_COMP_IN].u.u64) : 0,
802 U2H(stats[ST_F_COMP_BYP].u.u64),
803 U2H(stats[ST_F_COMP_IN].u.u64 - stats[ST_F_COMP_OUT].u.u64),
804 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,
805 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "</u>":"");
806
807 chunk_appendf(out,
808 /* denied: req, resp */
809 "<td>%s</td><td>%s</td>"
810 /* errors : request, connect, response */
811 "<td>%s</td><td></td><td></td>"
812 /* warnings: retries, redispatches */
813 "<td></td><td></td>"
814 /* server status : reflect frontend status */
815 "<td class=ac>%s</td>"
816 /* rest of server: nothing */
817 "<td class=ac colspan=8></td></tr>"
818 "",
819 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
820 U2H(stats[ST_F_EREQ].u.u64),
821 field_str(stats, ST_F_STATUS));
822 }
823 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_SO) {
824 chunk_appendf(out, "<tr class=socket>");
825 if (flags & ST_SHOWADMIN) {
826 /* Column sub-heading for Enable or Disable server */
827 chunk_appendf(out, "<td></td>");
828 }
829
830 chunk_appendf(out,
831 /* frontend name, listener name */
832 "<td class=ac><a name=\"%s/+%s\"></a>%s"
833 "<a class=lfsb href=\"#%s/+%s\">%s</a>"
834 "",
835 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME),
836 (flags & ST_SHLGNDS)?"<u>":"",
837 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME), field_str(stats, ST_F_SVNAME));
838
839 if (flags & ST_SHLGNDS) {
840 chunk_appendf(out, "<div class=tips>");
841
842 if (isdigit(*field_str(stats, ST_F_ADDR)))
843 chunk_appendf(out, "IPv4: %s, ", field_str(stats, ST_F_ADDR));
844 else if (*field_str(stats, ST_F_ADDR) == '[')
845 chunk_appendf(out, "IPv6: %s, ", field_str(stats, ST_F_ADDR));
846 else if (*field_str(stats, ST_F_ADDR))
847 chunk_appendf(out, "%s, ", field_str(stats, ST_F_ADDR));
848
849 /* id */
850 chunk_appendf(out, "id: %d</div>", stats[ST_F_SID].u.u32);
851 }
852
853 chunk_appendf(out,
854 /* queue */
855 "%s</td><td colspan=3></td>"
856 /* sessions rate: current, max, limit */
857 "<td colspan=3>&nbsp;</td>"
858 /* sessions: current, max, limit, total, lbtot, lastsess */
859 "<td>%s</td><td>%s</td><td>%s</td>"
860 "<td>%s</td><td>&nbsp;</td><td>&nbsp;</td>"
861 /* bytes: in, out */
862 "<td>%s</td><td>%s</td>"
863 "",
864 (flags & ST_SHLGNDS)?"</u>":"",
865 U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SMAX].u.u32), U2H(stats[ST_F_SLIM].u.u32),
866 U2H(stats[ST_F_STOT].u.u64), U2H(stats[ST_F_BIN].u.u64), U2H(stats[ST_F_BOUT].u.u64));
867
868 chunk_appendf(out,
869 /* denied: req, resp */
870 "<td>%s</td><td>%s</td>"
871 /* errors: request, connect, response */
872 "<td>%s</td><td></td><td></td>"
873 /* warnings: retries, redispatches */
874 "<td></td><td></td>"
875 /* server status: reflect listener status */
876 "<td class=ac>%s</td>"
877 /* rest of server: nothing */
878 "<td class=ac colspan=8></td></tr>"
879 "",
880 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
881 U2H(stats[ST_F_EREQ].u.u64),
882 field_str(stats, ST_F_STATUS));
883 }
884 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_SV) {
885 const char *style;
886
887 /* determine the style to use depending on the server's state,
888 * its health and weight. There isn't a 1-to-1 mapping between
889 * state and styles for the cases where the server is (still)
890 * up. The reason is that we don't want to report nolb and
891 * drain with the same color.
892 */
893
894 if (strcmp(field_str(stats, ST_F_STATUS), "DOWN") == 0 ||
895 strcmp(field_str(stats, ST_F_STATUS), "DOWN (agent)") == 0) {
896 style = "down";
897 }
898 else if (strcmp(field_str(stats, ST_F_STATUS), "DOWN ") == 0) {
899 style = "going_up";
900 }
901 else if (strcmp(field_str(stats, ST_F_STATUS), "NOLB ") == 0) {
902 style = "going_down";
903 }
904 else if (strcmp(field_str(stats, ST_F_STATUS), "NOLB") == 0) {
905 style = "nolb";
906 }
907 else if (strcmp(field_str(stats, ST_F_STATUS), "no check") == 0) {
908 style = "no_check";
909 }
910 else if (!stats[ST_F_CHKFAIL].type ||
911 stats[ST_F_CHECK_HEALTH].u.u32 == stats[ST_F_CHECK_RISE].u.u32 + stats[ST_F_CHECK_FALL].u.u32 - 1) {
912 /* no check or max health = UP */
913 if (stats[ST_F_WEIGHT].u.u32)
914 style = "up";
915 else
916 style = "draining";
917 }
918 else {
919 style = "going_down";
920 }
921
922 if (memcmp(field_str(stats, ST_F_STATUS), "MAINT", 5) == 0)
923 chunk_appendf(out, "<tr class=\"maintain\">");
924 else
925 chunk_appendf(out,
926 "<tr class=\"%s_%s\">",
927 (stats[ST_F_BCK].u.u32) ? "backup" : "active", style);
928
929
930 if (flags & ST_SHOWADMIN)
931 chunk_appendf(out,
David Harrigand3db35a2016-12-30 12:12:49 +0000932 "<td><input class='%s-checkbox' type=\"checkbox\" name=\"s\" value=\"%s\"></td>",
933 field_str(stats, ST_F_PXNAME),
William Lallemand74c24fb2016-11-21 17:18:36 +0100934 field_str(stats, ST_F_SVNAME));
935
936 chunk_appendf(out,
937 "<td class=ac><a name=\"%s/%s\"></a>%s"
938 "<a class=lfsb href=\"#%s/%s\">%s</a>"
939 "",
940 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME),
941 (flags & ST_SHLGNDS) ? "<u>" : "",
942 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME), field_str(stats, ST_F_SVNAME));
943
944 if (flags & ST_SHLGNDS) {
945 chunk_appendf(out, "<div class=tips>");
946
947 if (isdigit(*field_str(stats, ST_F_ADDR)))
948 chunk_appendf(out, "IPv4: %s, ", field_str(stats, ST_F_ADDR));
949 else if (*field_str(stats, ST_F_ADDR) == '[')
950 chunk_appendf(out, "IPv6: %s, ", field_str(stats, ST_F_ADDR));
951 else if (*field_str(stats, ST_F_ADDR))
952 chunk_appendf(out, "%s, ", field_str(stats, ST_F_ADDR));
953
954 /* id */
955 chunk_appendf(out, "id: %d", stats[ST_F_SID].u.u32);
956
957 /* cookie */
958 if (stats[ST_F_COOKIE].type) {
959 chunk_appendf(out, ", cookie: '");
960 chunk_initstr(&src, field_str(stats, ST_F_COOKIE));
961 chunk_htmlencode(out, &src);
962 chunk_appendf(out, "'");
963 }
964
965 chunk_appendf(out, "</div>");
966 }
967
968 chunk_appendf(out,
969 /* queue : current, max, limit */
970 "%s</td><td>%s</td><td>%s</td><td>%s</td>"
971 /* sessions rate : current, max, limit */
972 "<td>%s</td><td>%s</td><td></td>"
973 "",
974 (flags & ST_SHLGNDS) ? "</u>" : "",
975 U2H(stats[ST_F_QCUR].u.u32), U2H(stats[ST_F_QMAX].u.u32), LIM2A(stats[ST_F_QLIMIT].u.u32, "-"),
976 U2H(stats[ST_F_RATE].u.u32), U2H(stats[ST_F_RATE_MAX].u.u32));
977
978 chunk_appendf(out,
979 /* sessions: current, max, limit, total */
Willy Tarreauf21d17b2019-09-08 09:24:56 +0200980 "<td><u>%s<div class=tips>"
981 "<table class=det>"
982 "<tr><th>Current active connections:</th><td>%s</td></tr>"
983 "<tr><th>Current idle connections:</th><td>%s</td></tr>"
984 "<tr><th>Active connections limit:</th><td>%s</td></tr>"
985 "<tr><th>Idle connections limit:</th><td>%s</td></tr>"
986 "</table></div></u>"
987 "</td><td>%s</td><td>%s</td>"
William Lallemand74c24fb2016-11-21 17:18:36 +0100988 "<td><u>%s<div class=tips><table class=det>"
989 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
990 "",
Willy Tarreauf21d17b2019-09-08 09:24:56 +0200991 U2H(stats[ST_F_SCUR].u.u32),
992 U2H(stats[ST_F_SCUR].u.u32),
993 U2H(stats[ST_F_SRV_ICUR].u.u32),
994 LIM2A(stats[ST_F_SLIM].u.u32, "-"),
995 stats[ST_F_SRV_ILIM].type ? U2H(stats[ST_F_SRV_ILIM].u.u32) : "-",
996 U2H(stats[ST_F_SMAX].u.u32), LIM2A(stats[ST_F_SLIM].u.u32, "-"),
William Lallemand74c24fb2016-11-21 17:18:36 +0100997 U2H(stats[ST_F_STOT].u.u64),
998 U2H(stats[ST_F_STOT].u.u64));
999
1000 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
1001 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
1002 unsigned long long tot;
1003
1004 tot = stats[ST_F_HRSP_OTHER].u.u64;
1005 tot += stats[ST_F_HRSP_1XX].u.u64;
1006 tot += stats[ST_F_HRSP_2XX].u.u64;
1007 tot += stats[ST_F_HRSP_3XX].u.u64;
1008 tot += stats[ST_F_HRSP_4XX].u.u64;
1009 tot += stats[ST_F_HRSP_5XX].u.u64;
1010
1011 chunk_appendf(out,
Willy Tarreauf1573842018-12-14 11:35:36 +01001012 "<tr><th>New connections:</th><td>%s</td></tr>"
1013 "<tr><th>Reused connections:</th><td>%s</td><td>(%d%%)</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001014 "<tr><th>Cum. HTTP responses:</th><td>%s</td></tr>"
1015 "<tr><th>- HTTP 1xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1016 "<tr><th>- HTTP 2xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1017 "<tr><th>- HTTP 3xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1018 "<tr><th>- HTTP 4xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1019 "<tr><th>- HTTP 5xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1020 "<tr><th>- other responses:</th><td>%s</td><td>(%d%%)</td></tr>"
Willy Tarreau1b0f85e2018-05-28 15:12:40 +02001021 "<tr><th>Failed hdr rewrites:</th><td>%s</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001022 "",
Willy Tarreauf1573842018-12-14 11:35:36 +01001023 U2H(stats[ST_F_CONNECT].u.u64),
1024 U2H(stats[ST_F_REUSE].u.u64),
1025 (stats[ST_F_CONNECT].u.u64 + stats[ST_F_REUSE].u.u64) ?
1026 (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 +01001027 U2H(tot),
1028 U2H(stats[ST_F_HRSP_1XX].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_1XX].u.u64 / tot) : 0,
1029 U2H(stats[ST_F_HRSP_2XX].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_2XX].u.u64 / tot) : 0,
1030 U2H(stats[ST_F_HRSP_3XX].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_3XX].u.u64 / tot) : 0,
1031 U2H(stats[ST_F_HRSP_4XX].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_4XX].u.u64 / tot) : 0,
1032 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 +02001033 U2H(stats[ST_F_HRSP_OTHER].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_OTHER].u.u64 / tot) : 0,
1034 U2H(stats[ST_F_WREW].u.u64));
William Lallemand74c24fb2016-11-21 17:18:36 +01001035 }
1036
1037 chunk_appendf(out, "<tr><th colspan=3>Avg over last 1024 success. conn.</th></tr>");
1038 chunk_appendf(out, "<tr><th>- Queue time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_QTIME].u.u32));
1039 chunk_appendf(out, "<tr><th>- Connect time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_CTIME].u.u32));
1040 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
1041 chunk_appendf(out, "<tr><th>- Response time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_RTIME].u.u32));
1042 chunk_appendf(out, "<tr><th>- Total time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_TTIME].u.u32));
1043
1044 chunk_appendf(out,
1045 "</table></div></u></td>"
1046 /* sessions: lbtot, last */
1047 "<td>%s</td><td>%s</td>",
1048 U2H(stats[ST_F_LBTOT].u.u64),
1049 human_time(stats[ST_F_LASTSESS].u.s32, 1));
1050
1051 chunk_appendf(out,
1052 /* bytes : in, out */
1053 "<td>%s</td><td>%s</td>"
1054 /* denied: req, resp */
1055 "<td></td><td>%s</td>"
1056 /* errors : request, connect */
1057 "<td></td><td>%s</td>"
1058 /* errors : response */
1059 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
1060 /* warnings: retries, redispatches */
1061 "<td>%lld</td><td>%lld</td>"
1062 "",
1063 U2H(stats[ST_F_BIN].u.u64), U2H(stats[ST_F_BOUT].u.u64),
1064 U2H(stats[ST_F_DRESP].u.u64),
1065 U2H(stats[ST_F_ECON].u.u64),
1066 U2H(stats[ST_F_ERESP].u.u64),
1067 (long long)stats[ST_F_CLI_ABRT].u.u64,
1068 (long long)stats[ST_F_SRV_ABRT].u.u64,
1069 (long long)stats[ST_F_WRETR].u.u64,
1070 (long long)stats[ST_F_WREDIS].u.u64);
1071
1072 /* status, last change */
1073 chunk_appendf(out, "<td class=ac>");
1074
1075 /* FIXME!!!!
1076 * LASTCHG should contain the last change for *this* server and must be computed
1077 * properly above, as was done below, ie: this server if maint, otherwise ref server
1078 * if tracking. Note that ref is either local or remote depending on tracking.
1079 */
1080
1081
1082 if (memcmp(field_str(stats, ST_F_STATUS), "MAINT", 5) == 0) {
1083 chunk_appendf(out, "%s MAINT", human_time(stats[ST_F_LASTCHG].u.u32, 1));
1084 }
1085 else if (memcmp(field_str(stats, ST_F_STATUS), "no check", 5) == 0) {
1086 chunk_strcat(out, "<i>no check</i>");
1087 }
1088 else {
1089 chunk_appendf(out, "%s %s", human_time(stats[ST_F_LASTCHG].u.u32, 1), field_str(stats, ST_F_STATUS));
1090 if (memcmp(field_str(stats, ST_F_STATUS), "DOWN", 4) == 0) {
1091 if (stats[ST_F_CHECK_HEALTH].u.u32)
1092 chunk_strcat(out, " &uarr;");
1093 }
1094 else if (stats[ST_F_CHECK_HEALTH].u.u32 < stats[ST_F_CHECK_RISE].u.u32 + stats[ST_F_CHECK_FALL].u.u32 - 1)
1095 chunk_strcat(out, " &darr;");
1096 }
1097
1098 if (memcmp(field_str(stats, ST_F_STATUS), "DOWN", 4) == 0 &&
1099 stats[ST_F_AGENT_STATUS].type && !stats[ST_F_AGENT_HEALTH].u.u32) {
1100 chunk_appendf(out,
1101 "</td><td class=ac><u> %s",
1102 field_str(stats, ST_F_AGENT_STATUS));
1103
1104 if (stats[ST_F_AGENT_CODE].type)
1105 chunk_appendf(out, "/%d", stats[ST_F_AGENT_CODE].u.u32);
1106
1107 if (stats[ST_F_AGENT_DURATION].type)
1108 chunk_appendf(out, " in %lums", (long)stats[ST_F_AGENT_DURATION].u.u64);
1109
1110 chunk_appendf(out, "<div class=tips>%s", field_str(stats, ST_F_AGENT_DESC));
1111
1112 if (*field_str(stats, ST_F_LAST_AGT)) {
1113 chunk_appendf(out, ": ");
1114 chunk_initstr(&src, field_str(stats, ST_F_LAST_AGT));
1115 chunk_htmlencode(out, &src);
1116 }
1117 chunk_appendf(out, "</div></u>");
1118 }
1119 else if (stats[ST_F_CHECK_STATUS].type) {
1120 chunk_appendf(out,
1121 "</td><td class=ac><u> %s",
1122 field_str(stats, ST_F_CHECK_STATUS));
1123
1124 if (stats[ST_F_CHECK_CODE].type)
1125 chunk_appendf(out, "/%d", stats[ST_F_CHECK_CODE].u.u32);
1126
1127 if (stats[ST_F_CHECK_DURATION].type)
1128 chunk_appendf(out, " in %lums", (long)stats[ST_F_CHECK_DURATION].u.u64);
1129
1130 chunk_appendf(out, "<div class=tips>%s", field_str(stats, ST_F_CHECK_DESC));
1131
1132 if (*field_str(stats, ST_F_LAST_CHK)) {
1133 chunk_appendf(out, ": ");
1134 chunk_initstr(&src, field_str(stats, ST_F_LAST_CHK));
1135 chunk_htmlencode(out, &src);
1136 }
1137 chunk_appendf(out, "</div></u>");
1138 }
1139 else
1140 chunk_appendf(out, "</td><td>");
1141
1142 chunk_appendf(out,
1143 /* weight */
1144 "</td><td class=ac>%d</td>"
1145 /* act, bck */
1146 "<td class=ac>%s</td><td class=ac>%s</td>"
1147 "",
1148 stats[ST_F_WEIGHT].u.u32,
1149 stats[ST_F_BCK].u.u32 ? "-" : "Y",
1150 stats[ST_F_BCK].u.u32 ? "Y" : "-");
1151
1152 /* check failures: unique, fatal, down time */
1153 if (strcmp(field_str(stats, ST_F_STATUS), "MAINT (resolution)") == 0) {
1154 chunk_appendf(out, "<td class=ac colspan=3>resolution</td>");
1155 }
1156 else if (stats[ST_F_CHKFAIL].type) {
1157 chunk_appendf(out, "<td><u>%lld", (long long)stats[ST_F_CHKFAIL].u.u64);
1158
1159 if (stats[ST_F_HANAFAIL].type)
1160 chunk_appendf(out, "/%lld", (long long)stats[ST_F_HANAFAIL].u.u64);
1161
1162 chunk_appendf(out,
1163 "<div class=tips>Failed Health Checks%s</div></u></td>"
1164 "<td>%lld</td><td>%s</td>"
1165 "",
1166 stats[ST_F_HANAFAIL].type ? "/Health Analyses" : "",
1167 (long long)stats[ST_F_CHKDOWN].u.u64, human_time(stats[ST_F_DOWNTIME].u.u32, 1));
1168 }
1169 else if (strcmp(field_str(stats, ST_F_STATUS), "MAINT") != 0 && field_format(stats, ST_F_TRACKED) == FF_STR) {
1170 /* tracking a server (hence inherited maint would appear as "MAINT (via...)" */
1171 chunk_appendf(out,
1172 "<td class=ac colspan=3><a class=lfsb href=\"#%s\">via %s</a></td>",
1173 field_str(stats, ST_F_TRACKED), field_str(stats, ST_F_TRACKED));
1174 }
1175 else
1176 chunk_appendf(out, "<td colspan=3></td>");
1177
1178 /* throttle */
1179 if (stats[ST_F_THROTTLE].type)
1180 chunk_appendf(out, "<td class=ac>%d %%</td></tr>\n", stats[ST_F_THROTTLE].u.u32);
1181 else
1182 chunk_appendf(out, "<td class=ac>-</td></tr>\n");
1183 }
1184 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_BE) {
1185 chunk_appendf(out, "<tr class=\"backend\">");
1186 if (flags & ST_SHOWADMIN) {
1187 /* Column sub-heading for Enable or Disable server */
1188 chunk_appendf(out, "<td></td>");
1189 }
1190 chunk_appendf(out,
1191 "<td class=ac>"
1192 /* name */
1193 "%s<a name=\"%s/Backend\"></a>"
1194 "<a class=lfsb href=\"#%s/Backend\">Backend</a>"
1195 "",
1196 (flags & ST_SHLGNDS)?"<u>":"",
1197 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_PXNAME));
1198
1199 if (flags & ST_SHLGNDS) {
1200 /* balancing */
1201 chunk_appendf(out, "<div class=tips>balancing: %s",
1202 field_str(stats, ST_F_ALGO));
1203
1204 /* cookie */
1205 if (stats[ST_F_COOKIE].type) {
1206 chunk_appendf(out, ", cookie: '");
1207 chunk_initstr(&src, field_str(stats, ST_F_COOKIE));
1208 chunk_htmlencode(out, &src);
1209 chunk_appendf(out, "'");
1210 }
1211 chunk_appendf(out, "</div>");
1212 }
1213
1214 chunk_appendf(out,
1215 "%s</td>"
1216 /* queue : current, max */
1217 "<td>%s</td><td>%s</td><td></td>"
1218 /* sessions rate : current, max, limit */
1219 "<td>%s</td><td>%s</td><td></td>"
1220 "",
1221 (flags & ST_SHLGNDS)?"</u>":"",
1222 U2H(stats[ST_F_QCUR].u.u32), U2H(stats[ST_F_QMAX].u.u32),
1223 U2H(stats[ST_F_RATE].u.u32), U2H(stats[ST_F_RATE_MAX].u.u32));
1224
1225 chunk_appendf(out,
1226 /* sessions: current, max, limit, total */
1227 "<td>%s</td><td>%s</td><td>%s</td>"
1228 "<td><u>%s<div class=tips><table class=det>"
1229 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
1230 "",
Willy Tarreau8e0f1752016-12-12 15:07:29 +01001231 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 +01001232 U2H(stats[ST_F_STOT].u.u64),
1233 U2H(stats[ST_F_STOT].u.u64));
1234
1235 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
1236 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
1237 chunk_appendf(out,
Willy Tarreauf1573842018-12-14 11:35:36 +01001238 "<tr><th>New connections:</th><td>%s</td></tr>"
1239 "<tr><th>Reused connections:</th><td>%s</td><td>(%d%%)</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001240 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
1241 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
1242 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
1243 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
1244 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
1245 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
1246 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
1247 "<tr><th>- other responses:</th><td>%s</td></tr>"
Willy Tarreaua1214a52018-12-14 14:00:25 +01001248 "<tr><th>Cache lookups:</th><td>%s</td></tr>"
1249 "<tr><th>Cache hits:</th><td>%s</td><td>(%d%%)</td></tr>"
Willy Tarreau1b0f85e2018-05-28 15:12:40 +02001250 "<tr><th>Failed hdr rewrites:</th><td>%s</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001251 "<tr><th colspan=3>Avg over last 1024 success. conn.</th></tr>"
1252 "",
Willy Tarreauf1573842018-12-14 11:35:36 +01001253 U2H(stats[ST_F_CONNECT].u.u64),
1254 U2H(stats[ST_F_REUSE].u.u64),
1255 (stats[ST_F_CONNECT].u.u64 + stats[ST_F_REUSE].u.u64) ?
1256 (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 +01001257 U2H(stats[ST_F_REQ_TOT].u.u64),
1258 U2H(stats[ST_F_HRSP_1XX].u.u64),
1259 U2H(stats[ST_F_HRSP_2XX].u.u64),
1260 U2H(stats[ST_F_COMP_RSP].u.u64),
1261 stats[ST_F_HRSP_2XX].u.u64 ?
1262 (int)(100 * stats[ST_F_COMP_RSP].u.u64 / stats[ST_F_HRSP_2XX].u.u64) : 0,
1263 U2H(stats[ST_F_HRSP_3XX].u.u64),
1264 U2H(stats[ST_F_HRSP_4XX].u.u64),
1265 U2H(stats[ST_F_HRSP_5XX].u.u64),
Willy Tarreaufeead3a2018-12-14 13:48:44 +01001266 U2H(stats[ST_F_HRSP_OTHER].u.u64),
Willy Tarreaua1214a52018-12-14 14:00:25 +01001267 U2H(stats[ST_F_CACHE_LOOKUPS].u.u64),
1268 U2H(stats[ST_F_CACHE_HITS].u.u64),
1269 stats[ST_F_CACHE_LOOKUPS].u.u64 ?
1270 (int)(100 * stats[ST_F_CACHE_HITS].u.u64 / stats[ST_F_CACHE_LOOKUPS].u.u64) : 0,
Willy Tarreaufeead3a2018-12-14 13:48:44 +01001271 U2H(stats[ST_F_WREW].u.u64));
William Lallemand74c24fb2016-11-21 17:18:36 +01001272 }
1273
1274 chunk_appendf(out, "<tr><th>- Queue time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_QTIME].u.u32));
1275 chunk_appendf(out, "<tr><th>- Connect time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_CTIME].u.u32));
1276 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
1277 chunk_appendf(out, "<tr><th>- Response time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_RTIME].u.u32));
1278 chunk_appendf(out, "<tr><th>- Total time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_TTIME].u.u32));
1279
1280 chunk_appendf(out,
1281 "</table></div></u></td>"
1282 /* sessions: lbtot, last */
1283 "<td>%s</td><td>%s</td>"
1284 /* bytes: in */
1285 "<td>%s</td>"
1286 "",
1287 U2H(stats[ST_F_LBTOT].u.u64),
1288 human_time(stats[ST_F_LASTSESS].u.s32, 1),
1289 U2H(stats[ST_F_BIN].u.u64));
1290
1291 chunk_appendf(out,
1292 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
1293 "<td>%s%s<div class=tips><table class=det>"
1294 "<tr><th>Response bytes in:</th><td>%s</td></tr>"
1295 "<tr><th>Compression in:</th><td>%s</td></tr>"
1296 "<tr><th>Compression out:</th><td>%s</td><td>(%d%%)</td></tr>"
1297 "<tr><th>Compression bypass:</th><td>%s</td></tr>"
1298 "<tr><th>Total bytes saved:</th><td>%s</td><td>(%d%%)</td></tr>"
1299 "</table></div>%s</td>",
1300 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "<u>":"",
1301 U2H(stats[ST_F_BOUT].u.u64),
1302 U2H(stats[ST_F_BOUT].u.u64),
1303 U2H(stats[ST_F_COMP_IN].u.u64),
1304 U2H(stats[ST_F_COMP_OUT].u.u64),
1305 stats[ST_F_COMP_IN].u.u64 ? (int)(stats[ST_F_COMP_OUT].u.u64 * 100 / stats[ST_F_COMP_IN].u.u64) : 0,
1306 U2H(stats[ST_F_COMP_BYP].u.u64),
1307 U2H(stats[ST_F_COMP_IN].u.u64 - stats[ST_F_COMP_OUT].u.u64),
1308 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,
1309 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "</u>":"");
1310
1311 chunk_appendf(out,
1312 /* denied: req, resp */
1313 "<td>%s</td><td>%s</td>"
1314 /* errors : request, connect */
1315 "<td></td><td>%s</td>"
1316 /* errors : response */
1317 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
1318 /* warnings: retries, redispatches */
1319 "<td>%lld</td><td>%lld</td>"
1320 /* backend status: reflect backend status (up/down): we display UP
1321 * if the backend has known working servers or if it has no server at
1322 * all (eg: for stats). Then we display the total weight, number of
1323 * active and backups. */
1324 "<td class=ac>%s %s</td><td class=ac>&nbsp;</td><td class=ac>%d</td>"
1325 "<td class=ac>%d</td><td class=ac>%d</td>"
1326 "",
1327 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
1328 U2H(stats[ST_F_ECON].u.u64),
1329 U2H(stats[ST_F_ERESP].u.u64),
1330 (long long)stats[ST_F_CLI_ABRT].u.u64,
1331 (long long)stats[ST_F_SRV_ABRT].u.u64,
1332 (long long)stats[ST_F_WRETR].u.u64, (long long)stats[ST_F_WREDIS].u.u64,
1333 human_time(stats[ST_F_LASTCHG].u.u32, 1),
1334 strcmp(field_str(stats, ST_F_STATUS), "DOWN") ? field_str(stats, ST_F_STATUS) : "<font color=\"red\"><b>DOWN</b></font>",
1335 stats[ST_F_WEIGHT].u.u32,
1336 stats[ST_F_ACT].u.u32, stats[ST_F_BCK].u.u32);
1337
1338 chunk_appendf(out,
1339 /* rest of backend: nothing, down transitions, total downtime, throttle */
1340 "<td class=ac>&nbsp;</td><td>%d</td>"
1341 "<td>%s</td>"
1342 "<td></td>"
1343 "</tr>",
1344 stats[ST_F_CHKDOWN].u.u32,
1345 stats[ST_F_DOWNTIME].type ? human_time(stats[ST_F_DOWNTIME].u.u32, 1) : "&nbsp;");
1346 }
1347 return 1;
1348}
1349
1350int stats_dump_one_line(const struct field *stats, unsigned int flags, struct proxy *px, struct appctx *appctx)
1351{
Simon Horman05ee2132017-01-04 09:37:25 +01001352 int ret;
1353
William Lallemand74c24fb2016-11-21 17:18:36 +01001354 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN))
1355 flags |= ST_SHOWADMIN;
1356
1357 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
Simon Horman05ee2132017-01-04 09:37:25 +01001358 ret = stats_dump_fields_html(&trash, stats, flags);
William Lallemand74c24fb2016-11-21 17:18:36 +01001359 else if (appctx->ctx.stats.flags & STAT_FMT_TYPED)
Simon Horman05ee2132017-01-04 09:37:25 +01001360 ret = stats_dump_fields_typed(&trash, stats);
1361 else if (appctx->ctx.stats.flags & STAT_FMT_JSON)
1362 ret = stats_dump_fields_json(&trash, stats,
1363 !(appctx->ctx.stats.flags &
1364 STAT_STARTED));
William Lallemand74c24fb2016-11-21 17:18:36 +01001365 else
Simon Horman05ee2132017-01-04 09:37:25 +01001366 ret = stats_dump_fields_csv(&trash, stats);
1367
1368 if (ret)
1369 appctx->ctx.stats.flags |= STAT_STARTED;
1370
1371 return ret;
William Lallemand74c24fb2016-11-21 17:18:36 +01001372}
1373
1374/* Fill <stats> with the frontend statistics. <stats> is
1375 * preallocated array of length <len>. The length of the array
1376 * must be at least ST_F_TOTAL_FIELDS. If this length is less then
1377 * this value, the function returns 0, otherwise, it returns 1.
1378 */
1379int stats_fill_fe_stats(struct proxy *px, struct field *stats, int len)
1380{
1381 if (len < ST_F_TOTAL_FIELDS)
1382 return 0;
1383
1384 memset(stats, 0, sizeof(*stats) * len);
1385
1386 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1387 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, "FRONTEND");
1388 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1389 stats[ST_F_SCUR] = mkf_u32(0, px->feconn);
1390 stats[ST_F_SMAX] = mkf_u32(FN_MAX, px->fe_counters.conn_max);
1391 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, px->maxconn);
1392 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, px->fe_counters.cum_sess);
1393 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, px->fe_counters.bytes_in);
1394 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, px->fe_counters.bytes_out);
1395 stats[ST_F_DREQ] = mkf_u64(FN_COUNTER, px->fe_counters.denied_req);
1396 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, px->fe_counters.denied_resp);
1397 stats[ST_F_EREQ] = mkf_u64(FN_COUNTER, px->fe_counters.failed_req);
1398 stats[ST_F_DCON] = mkf_u64(FN_COUNTER, px->fe_counters.denied_conn);
1399 stats[ST_F_DSES] = mkf_u64(FN_COUNTER, px->fe_counters.denied_sess);
1400 stats[ST_F_STATUS] = mkf_str(FO_STATUS, px->state == PR_STREADY ? "OPEN" : px->state == PR_STFULL ? "FULL" : "STOP");
1401 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
1402 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1403 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, 0);
1404 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_FE);
1405 stats[ST_F_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_sess_per_sec));
1406 stats[ST_F_RATE_LIM] = mkf_u32(FO_CONFIG|FN_LIMIT, px->fe_sps_lim);
1407 stats[ST_F_RATE_MAX] = mkf_u32(FN_MAX, px->fe_counters.sps_max);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001408 stats[ST_F_WREW] = mkf_u64(FN_COUNTER, px->fe_counters.failed_rewrites);
William Lallemand74c24fb2016-11-21 17:18:36 +01001409
1410 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
1411 if (px->mode == PR_MODE_HTTP) {
1412 stats[ST_F_HRSP_1XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[1]);
1413 stats[ST_F_HRSP_2XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[2]);
1414 stats[ST_F_HRSP_3XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[3]);
1415 stats[ST_F_HRSP_4XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[4]);
1416 stats[ST_F_HRSP_5XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[5]);
1417 stats[ST_F_HRSP_OTHER] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[0]);
1418 stats[ST_F_INTERCEPTED] = mkf_u64(FN_COUNTER, px->fe_counters.intercepted_req);
Willy Tarreaua1214a52018-12-14 14:00:25 +01001419 stats[ST_F_CACHE_LOOKUPS] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cache_lookups);
1420 stats[ST_F_CACHE_HITS] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cache_hits);
William Lallemand74c24fb2016-11-21 17:18:36 +01001421 }
1422
1423 /* requests : req_rate, req_rate_max, req_tot, */
1424 stats[ST_F_REQ_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_req_per_sec));
1425 stats[ST_F_REQ_RATE_MAX] = mkf_u32(FN_MAX, px->fe_counters.p.http.rps_max);
1426 stats[ST_F_REQ_TOT] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cum_req);
1427
1428 /* compression: in, out, bypassed, responses */
1429 stats[ST_F_COMP_IN] = mkf_u64(FN_COUNTER, px->fe_counters.comp_in);
1430 stats[ST_F_COMP_OUT] = mkf_u64(FN_COUNTER, px->fe_counters.comp_out);
1431 stats[ST_F_COMP_BYP] = mkf_u64(FN_COUNTER, px->fe_counters.comp_byp);
1432 stats[ST_F_COMP_RSP] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.comp_rsp);
1433
1434 /* connections : conn_rate, conn_rate_max, conn_tot, conn_max */
1435 stats[ST_F_CONN_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_conn_per_sec));
1436 stats[ST_F_CONN_RATE_MAX] = mkf_u32(FN_MAX, px->fe_counters.cps_max);
1437 stats[ST_F_CONN_TOT] = mkf_u64(FN_COUNTER, px->fe_counters.cum_conn);
1438
1439 return 1;
1440}
1441
1442/* Dumps a frontend's line to the trash for the current proxy <px> and uses
1443 * the state from stream interface <si>. The caller is responsible for clearing
1444 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
1445 */
1446static int stats_dump_fe_stats(struct stream_interface *si, struct proxy *px)
1447{
1448 struct appctx *appctx = __objt_appctx(si->end);
1449
1450 if (!(px->cap & PR_CAP_FE))
1451 return 0;
1452
1453 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_FE)))
1454 return 0;
1455
1456 if (!stats_fill_fe_stats(px, stats, ST_F_TOTAL_FIELDS))
1457 return 0;
1458
1459 return stats_dump_one_line(stats, 0, px, appctx);
1460}
1461
1462/* Fill <stats> with the listener statistics. <stats> is
1463 * preallocated array of length <len>. The length of the array
1464 * must be at least ST_F_TOTAL_FIELDS. If this length is less
1465 * then this value, the function returns 0, otherwise, it
1466 * returns 1. <flags> can take the value ST_SHLGNDS.
1467 */
1468int stats_fill_li_stats(struct proxy *px, struct listener *l, int flags,
1469 struct field *stats, int len)
1470{
Willy Tarreau83061a82018-07-13 11:56:34 +02001471 struct buffer *out = get_trash_chunk();
William Lallemand74c24fb2016-11-21 17:18:36 +01001472
1473 if (len < ST_F_TOTAL_FIELDS)
1474 return 0;
1475
1476 if (!l->counters)
1477 return 0;
1478
1479 chunk_reset(out);
1480 memset(stats, 0, sizeof(*stats) * len);
1481
1482 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1483 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, l->name);
1484 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1485 stats[ST_F_SCUR] = mkf_u32(0, l->nbconn);
1486 stats[ST_F_SMAX] = mkf_u32(FN_MAX, l->counters->conn_max);
1487 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, l->maxconn);
1488 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, l->counters->cum_conn);
1489 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, l->counters->bytes_in);
1490 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, l->counters->bytes_out);
1491 stats[ST_F_DREQ] = mkf_u64(FN_COUNTER, l->counters->denied_req);
1492 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, l->counters->denied_resp);
1493 stats[ST_F_EREQ] = mkf_u64(FN_COUNTER, l->counters->failed_req);
1494 stats[ST_F_DCON] = mkf_u64(FN_COUNTER, l->counters->denied_conn);
1495 stats[ST_F_DSES] = mkf_u64(FN_COUNTER, l->counters->denied_sess);
Willy Tarreaua8cf66b2019-02-27 16:49:00 +01001496 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 +01001497 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
1498 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1499 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, l->luid);
1500 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_SO);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001501 stats[ST_F_WREW] = mkf_u64(FN_COUNTER, l->counters->failed_rewrites);
William Lallemand74c24fb2016-11-21 17:18:36 +01001502
1503 if (flags & ST_SHLGNDS) {
1504 char str[INET6_ADDRSTRLEN];
1505 int port;
1506
1507 port = get_host_port(&l->addr);
1508 switch (addr_to_str(&l->addr, str, sizeof(str))) {
1509 case AF_INET:
1510 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1511 chunk_appendf(out, "%s:%d", str, port);
1512 break;
1513 case AF_INET6:
1514 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1515 chunk_appendf(out, "[%s]:%d", str, port);
1516 break;
1517 case AF_UNIX:
1518 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, "unix");
1519 break;
1520 case -1:
1521 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1522 chunk_strcat(out, strerror(errno));
1523 break;
1524 default: /* address family not supported */
1525 break;
1526 }
1527 }
1528
1529 return 1;
1530}
1531
1532/* Dumps a line for listener <l> and proxy <px> to the trash and uses the state
1533 * from stream interface <si>, and stats flags <flags>. The caller is responsible
1534 * for clearing the trash if needed. Returns non-zero if it emits anything, zero
1535 * otherwise.
1536 */
1537static int stats_dump_li_stats(struct stream_interface *si, struct proxy *px, struct listener *l, int flags)
1538{
1539 struct appctx *appctx = __objt_appctx(si->end);
1540
1541 if (!stats_fill_li_stats(px, l, flags, stats, ST_F_TOTAL_FIELDS))
1542 return 0;
1543
1544 return stats_dump_one_line(stats, flags, px, appctx);
1545}
1546
1547enum srv_stats_state {
1548 SRV_STATS_STATE_DOWN = 0,
1549 SRV_STATS_STATE_DOWN_AGENT,
1550 SRV_STATS_STATE_GOING_UP,
1551 SRV_STATS_STATE_UP_GOING_DOWN,
1552 SRV_STATS_STATE_UP,
1553 SRV_STATS_STATE_NOLB_GOING_DOWN,
1554 SRV_STATS_STATE_NOLB,
1555 SRV_STATS_STATE_DRAIN_GOING_DOWN,
1556 SRV_STATS_STATE_DRAIN,
1557 SRV_STATS_STATE_DRAIN_AGENT,
1558 SRV_STATS_STATE_NO_CHECK,
1559
1560 SRV_STATS_STATE_COUNT, /* Must be last */
1561};
1562
1563static const char *srv_hlt_st[SRV_STATS_STATE_COUNT] = {
1564 [SRV_STATS_STATE_DOWN] = "DOWN",
1565 [SRV_STATS_STATE_DOWN_AGENT] = "DOWN (agent)",
1566 [SRV_STATS_STATE_GOING_UP] = "DOWN %d/%d",
1567 [SRV_STATS_STATE_UP_GOING_DOWN] = "UP %d/%d",
1568 [SRV_STATS_STATE_UP] = "UP",
1569 [SRV_STATS_STATE_NOLB_GOING_DOWN] = "NOLB %d/%d",
1570 [SRV_STATS_STATE_NOLB] = "NOLB",
1571 [SRV_STATS_STATE_DRAIN_GOING_DOWN] = "DRAIN %d/%d",
1572 [SRV_STATS_STATE_DRAIN] = "DRAIN",
1573 [SRV_STATS_STATE_DRAIN_AGENT] = "DRAIN (agent)",
1574 [SRV_STATS_STATE_NO_CHECK] = "no check"
1575};
1576
1577/* Fill <stats> with the server statistics. <stats> is
1578 * preallocated array of length <len>. The length of the array
1579 * must be at least ST_F_TOTAL_FIELDS. If this length is less
1580 * then this value, the function returns 0, otherwise, it
1581 * returns 1. <flags> can take the value ST_SHLGNDS.
1582 */
1583int stats_fill_sv_stats(struct proxy *px, struct server *sv, int flags,
1584 struct field *stats, int len)
1585{
1586 struct server *via, *ref;
1587 char str[INET6_ADDRSTRLEN];
Willy Tarreau83061a82018-07-13 11:56:34 +02001588 struct buffer *out = get_trash_chunk();
William Lallemand74c24fb2016-11-21 17:18:36 +01001589 enum srv_stats_state state;
1590 char *fld_status;
1591
1592 if (len < ST_F_TOTAL_FIELDS)
1593 return 0;
1594
1595 memset(stats, 0, sizeof(*stats) * len);
1596
1597 /* we have "via" which is the tracked server as described in the configuration,
1598 * and "ref" which is the checked server and the end of the chain.
1599 */
1600 via = sv->track ? sv->track : sv;
1601 ref = via;
1602 while (ref->track)
1603 ref = ref->track;
1604
Emeric Brun52a91d32017-08-31 14:41:55 +02001605 if (sv->cur_state == SRV_ST_RUNNING || sv->cur_state == SRV_ST_STARTING) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001606 if ((ref->check.state & CHK_ST_ENABLED) &&
1607 (ref->check.health < ref->check.rise + ref->check.fall - 1)) {
1608 state = SRV_STATS_STATE_UP_GOING_DOWN;
1609 } else {
1610 state = SRV_STATS_STATE_UP;
1611 }
1612
Emeric Brun52a91d32017-08-31 14:41:55 +02001613 if (sv->cur_admin & SRV_ADMF_DRAIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001614 if (ref->agent.state & CHK_ST_ENABLED)
1615 state = SRV_STATS_STATE_DRAIN_AGENT;
1616 else if (state == SRV_STATS_STATE_UP_GOING_DOWN)
1617 state = SRV_STATS_STATE_DRAIN_GOING_DOWN;
1618 else
1619 state = SRV_STATS_STATE_DRAIN;
1620 }
1621
1622 if (state == SRV_STATS_STATE_UP && !(ref->check.state & CHK_ST_ENABLED)) {
1623 state = SRV_STATS_STATE_NO_CHECK;
1624 }
1625 }
Emeric Brun52a91d32017-08-31 14:41:55 +02001626 else if (sv->cur_state == SRV_ST_STOPPING) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001627 if ((!(sv->check.state & CHK_ST_ENABLED) && !sv->track) ||
1628 (ref->check.health == ref->check.rise + ref->check.fall - 1)) {
1629 state = SRV_STATS_STATE_NOLB;
1630 } else {
1631 state = SRV_STATS_STATE_NOLB_GOING_DOWN;
1632 }
1633 }
1634 else { /* stopped */
1635 if ((ref->agent.state & CHK_ST_ENABLED) && !ref->agent.health) {
1636 state = SRV_STATS_STATE_DOWN_AGENT;
1637 } else if ((ref->check.state & CHK_ST_ENABLED) && !ref->check.health) {
1638 state = SRV_STATS_STATE_DOWN; /* DOWN */
1639 } else if ((ref->agent.state & CHK_ST_ENABLED) || (ref->check.state & CHK_ST_ENABLED)) {
1640 state = SRV_STATS_STATE_GOING_UP;
1641 } else {
1642 state = SRV_STATS_STATE_DOWN; /* DOWN, unchecked */
1643 }
1644 }
1645
1646 chunk_reset(out);
1647
1648 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1649 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, sv->id);
1650 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1651 stats[ST_F_QCUR] = mkf_u32(0, sv->nbpend);
1652 stats[ST_F_QMAX] = mkf_u32(FN_MAX, sv->counters.nbpend_max);
1653 stats[ST_F_SCUR] = mkf_u32(0, sv->cur_sess);
1654 stats[ST_F_SMAX] = mkf_u32(FN_MAX, sv->counters.cur_sess_max);
1655
1656 if (sv->maxconn)
1657 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, sv->maxconn);
1658
Willy Tarreauf21d17b2019-09-08 09:24:56 +02001659 stats[ST_F_SRV_ICUR] = mkf_u32(0, sv->curr_idle_conns);
1660 if (sv->max_idle_conns != -1)
1661 stats[ST_F_SRV_ILIM] = mkf_u32(FO_CONFIG|FN_LIMIT, sv->max_idle_conns);
1662
William Lallemand74c24fb2016-11-21 17:18:36 +01001663 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, sv->counters.cum_sess);
1664 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, sv->counters.bytes_in);
1665 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, sv->counters.bytes_out);
1666 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, sv->counters.failed_secu);
1667 stats[ST_F_ECON] = mkf_u64(FN_COUNTER, sv->counters.failed_conns);
1668 stats[ST_F_ERESP] = mkf_u64(FN_COUNTER, sv->counters.failed_resp);
1669 stats[ST_F_WRETR] = mkf_u64(FN_COUNTER, sv->counters.retries);
1670 stats[ST_F_WREDIS] = mkf_u64(FN_COUNTER, sv->counters.redispatches);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001671 stats[ST_F_WREW] = mkf_u64(FN_COUNTER, sv->counters.failed_rewrites);
Willy Tarreauf1573842018-12-14 11:35:36 +01001672 stats[ST_F_CONNECT] = mkf_u64(FN_COUNTER, sv->counters.connect);
1673 stats[ST_F_REUSE] = mkf_u64(FN_COUNTER, sv->counters.reuse);
William Lallemand74c24fb2016-11-21 17:18:36 +01001674
1675 /* status */
1676 fld_status = chunk_newstr(out);
Emeric Brun52a91d32017-08-31 14:41:55 +02001677 if (sv->cur_admin & SRV_ADMF_RMAINT)
William Lallemand74c24fb2016-11-21 17:18:36 +01001678 chunk_appendf(out, "MAINT (resolution)");
Emeric Brun52a91d32017-08-31 14:41:55 +02001679 else if (sv->cur_admin & SRV_ADMF_IMAINT)
William Lallemand74c24fb2016-11-21 17:18:36 +01001680 chunk_appendf(out, "MAINT (via %s/%s)", via->proxy->id, via->id);
Emeric Brun52a91d32017-08-31 14:41:55 +02001681 else if (sv->cur_admin & SRV_ADMF_MAINT)
William Lallemand74c24fb2016-11-21 17:18:36 +01001682 chunk_appendf(out, "MAINT");
1683 else
1684 chunk_appendf(out,
1685 srv_hlt_st[state],
Emeric Brun52a91d32017-08-31 14:41:55 +02001686 (ref->cur_state != SRV_ST_STOPPED) ? (ref->check.health - ref->check.rise + 1) : (ref->check.health),
1687 (ref->cur_state != SRV_ST_STOPPED) ? (ref->check.fall) : (ref->check.rise));
William Lallemand74c24fb2016-11-21 17:18:36 +01001688
1689 stats[ST_F_STATUS] = mkf_str(FO_STATUS, fld_status);
1690 stats[ST_F_LASTCHG] = mkf_u32(FN_AGE, now.tv_sec - sv->last_change);
Emeric Brun52a91d32017-08-31 14:41:55 +02001691 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 +01001692 stats[ST_F_ACT] = mkf_u32(FO_STATUS, (sv->flags & SRV_F_BACKUP) ? 0 : 1);
1693 stats[ST_F_BCK] = mkf_u32(FO_STATUS, (sv->flags & SRV_F_BACKUP) ? 1 : 0);
1694
1695 /* check failures: unique, fatal; last change, total downtime */
1696 if (sv->check.state & CHK_ST_ENABLED) {
1697 stats[ST_F_CHKFAIL] = mkf_u64(FN_COUNTER, sv->counters.failed_checks);
1698 stats[ST_F_CHKDOWN] = mkf_u64(FN_COUNTER, sv->counters.down_trans);
1699 stats[ST_F_DOWNTIME] = mkf_u32(FN_COUNTER, srv_downtime(sv));
1700 }
1701
1702 if (sv->maxqueue)
1703 stats[ST_F_QLIMIT] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->maxqueue);
1704
1705 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
1706 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1707 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, sv->puid);
1708
Emeric Brun52a91d32017-08-31 14:41:55 +02001709 if (sv->cur_state == SRV_ST_STARTING && !server_is_draining(sv))
William Lallemand74c24fb2016-11-21 17:18:36 +01001710 stats[ST_F_THROTTLE] = mkf_u32(FN_AVG, server_throttle_rate(sv));
1711
1712 stats[ST_F_LBTOT] = mkf_u64(FN_COUNTER, sv->counters.cum_lbconn);
1713
1714 if (sv->track) {
1715 char *fld_track = chunk_newstr(out);
1716
1717 chunk_appendf(out, "%s/%s", sv->track->proxy->id, sv->track->id);
1718 stats[ST_F_TRACKED] = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, fld_track);
1719 }
1720
1721 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_SV);
1722 stats[ST_F_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&sv->sess_per_sec));
1723 stats[ST_F_RATE_MAX] = mkf_u32(FN_MAX, sv->counters.sps_max);
1724
1725 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) {
1726 const char *fld_chksts;
1727
1728 fld_chksts = chunk_newstr(out);
1729 chunk_strcat(out, "* "); // for check in progress
1730 chunk_strcat(out, get_check_status_info(sv->check.status));
1731 if (!(sv->check.state & CHK_ST_INPROGRESS))
1732 fld_chksts += 2; // skip "* "
1733 stats[ST_F_CHECK_STATUS] = mkf_str(FN_OUTPUT, fld_chksts);
1734
1735 if (sv->check.status >= HCHK_STATUS_L57DATA)
1736 stats[ST_F_CHECK_CODE] = mkf_u32(FN_OUTPUT, sv->check.code);
1737
1738 if (sv->check.status >= HCHK_STATUS_CHECKED)
1739 stats[ST_F_CHECK_DURATION] = mkf_u64(FN_DURATION, sv->check.duration);
1740
1741 stats[ST_F_CHECK_DESC] = mkf_str(FN_OUTPUT, get_check_status_description(sv->check.status));
1742 stats[ST_F_LAST_CHK] = mkf_str(FN_OUTPUT, sv->check.desc);
1743 stats[ST_F_CHECK_RISE] = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.rise);
1744 stats[ST_F_CHECK_FALL] = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.fall);
1745 stats[ST_F_CHECK_HEALTH] = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.health);
1746 }
1747
1748 if ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) {
1749 const char *fld_chksts;
1750
1751 fld_chksts = chunk_newstr(out);
1752 chunk_strcat(out, "* "); // for check in progress
1753 chunk_strcat(out, get_check_status_info(sv->agent.status));
1754 if (!(sv->agent.state & CHK_ST_INPROGRESS))
1755 fld_chksts += 2; // skip "* "
1756 stats[ST_F_AGENT_STATUS] = mkf_str(FN_OUTPUT, fld_chksts);
1757
1758 if (sv->agent.status >= HCHK_STATUS_L57DATA)
1759 stats[ST_F_AGENT_CODE] = mkf_u32(FN_OUTPUT, sv->agent.code);
1760
1761 if (sv->agent.status >= HCHK_STATUS_CHECKED)
1762 stats[ST_F_AGENT_DURATION] = mkf_u64(FN_DURATION, sv->agent.duration);
1763
1764 stats[ST_F_AGENT_DESC] = mkf_str(FN_OUTPUT, get_check_status_description(sv->agent.status));
1765 stats[ST_F_LAST_AGT] = mkf_str(FN_OUTPUT, sv->agent.desc);
1766 stats[ST_F_AGENT_RISE] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.rise);
1767 stats[ST_F_AGENT_FALL] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.fall);
1768 stats[ST_F_AGENT_HEALTH] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.health);
1769 }
1770
1771 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
1772 if (px->mode == PR_MODE_HTTP) {
1773 stats[ST_F_HRSP_1XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[1]);
1774 stats[ST_F_HRSP_2XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[2]);
1775 stats[ST_F_HRSP_3XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[3]);
1776 stats[ST_F_HRSP_4XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[4]);
1777 stats[ST_F_HRSP_5XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[5]);
1778 stats[ST_F_HRSP_OTHER] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[0]);
1779 }
1780
1781 if (ref->observe)
1782 stats[ST_F_HANAFAIL] = mkf_u64(FN_COUNTER, sv->counters.failed_hana);
1783
1784 stats[ST_F_CLI_ABRT] = mkf_u64(FN_COUNTER, sv->counters.cli_aborts);
1785 stats[ST_F_SRV_ABRT] = mkf_u64(FN_COUNTER, sv->counters.srv_aborts);
1786 stats[ST_F_LASTSESS] = mkf_s32(FN_AGE, srv_lastsession(sv));
1787
1788 stats[ST_F_QTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.q_time, TIME_STATS_SAMPLES));
1789 stats[ST_F_CTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.c_time, TIME_STATS_SAMPLES));
1790 stats[ST_F_RTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.d_time, TIME_STATS_SAMPLES));
1791 stats[ST_F_TTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.t_time, TIME_STATS_SAMPLES));
1792
1793 if (flags & ST_SHLGNDS) {
1794 switch (addr_to_str(&sv->addr, str, sizeof(str))) {
1795 case AF_INET:
1796 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
Willy Tarreau04276f32017-01-06 17:41:29 +01001797 chunk_appendf(out, "%s:%d", str, sv->svc_port);
William Lallemand74c24fb2016-11-21 17:18:36 +01001798 break;
1799 case AF_INET6:
1800 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
Willy Tarreau04276f32017-01-06 17:41:29 +01001801 chunk_appendf(out, "[%s]:%d", str, sv->svc_port);
William Lallemand74c24fb2016-11-21 17:18:36 +01001802 break;
1803 case AF_UNIX:
1804 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, "unix");
1805 break;
1806 case -1:
1807 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1808 chunk_strcat(out, strerror(errno));
1809 break;
1810 default: /* address family not supported */
1811 break;
1812 }
1813
1814 if (sv->cookie)
1815 stats[ST_F_COOKIE] = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, sv->cookie);
1816 }
1817
1818 return 1;
1819}
1820
1821/* Dumps a line for server <sv> and proxy <px> to the trash and uses the state
1822 * from stream interface <si>, stats flags <flags>, and server state <state>.
1823 * The caller is responsible for clearing the trash if needed. Returns non-zero
1824 * if it emits anything, zero otherwise.
1825 */
1826static int stats_dump_sv_stats(struct stream_interface *si, struct proxy *px, int flags, struct server *sv)
1827{
1828 struct appctx *appctx = __objt_appctx(si->end);
1829
1830 if (!stats_fill_sv_stats(px, sv, flags, stats, ST_F_TOTAL_FIELDS))
1831 return 0;
1832
1833 return stats_dump_one_line(stats, flags, px, appctx);
1834}
1835
1836/* Fill <stats> with the backend statistics. <stats> is
1837 * preallocated array of length <len>. The length of the array
1838 * must be at least ST_F_TOTAL_FIELDS. If this length is less
1839 * then this value, the function returns 0, otherwise, it
1840 * returns 1. <flags> can take the value ST_SHLGNDS.
1841 */
1842int stats_fill_be_stats(struct proxy *px, int flags, struct field *stats, int len)
1843{
1844 if (len < ST_F_TOTAL_FIELDS)
1845 return 0;
1846
1847 memset(stats, 0, sizeof(*stats) * len);
1848
1849 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1850 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, "BACKEND");
1851 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1852 stats[ST_F_QCUR] = mkf_u32(0, px->nbpend);
1853 stats[ST_F_QMAX] = mkf_u32(FN_MAX, px->be_counters.nbpend_max);
Thierry FOURNIER0ff98a42016-12-19 16:50:42 +01001854 stats[ST_F_SCUR] = mkf_u32(0, px->beconn);
William Lallemand74c24fb2016-11-21 17:18:36 +01001855 stats[ST_F_SMAX] = mkf_u32(FN_MAX, px->be_counters.conn_max);
1856 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, px->fullconn);
1857 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, px->be_counters.cum_conn);
1858 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, px->be_counters.bytes_in);
1859 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, px->be_counters.bytes_out);
1860 stats[ST_F_DREQ] = mkf_u64(FN_COUNTER, px->be_counters.denied_req);
1861 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, px->be_counters.denied_resp);
1862 stats[ST_F_ECON] = mkf_u64(FN_COUNTER, px->be_counters.failed_conns);
1863 stats[ST_F_ERESP] = mkf_u64(FN_COUNTER, px->be_counters.failed_resp);
1864 stats[ST_F_WRETR] = mkf_u64(FN_COUNTER, px->be_counters.retries);
1865 stats[ST_F_WREDIS] = mkf_u64(FN_COUNTER, px->be_counters.redispatches);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001866 stats[ST_F_WREW] = mkf_u64(FN_COUNTER, px->be_counters.failed_rewrites);
Willy Tarreauf1573842018-12-14 11:35:36 +01001867 stats[ST_F_CONNECT] = mkf_u64(FN_COUNTER, px->be_counters.connect);
1868 stats[ST_F_REUSE] = mkf_u64(FN_COUNTER, px->be_counters.reuse);
William Lallemand74c24fb2016-11-21 17:18:36 +01001869 stats[ST_F_STATUS] = mkf_str(FO_STATUS, (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" : "DOWN");
1870 stats[ST_F_WEIGHT] = mkf_u32(FN_AVG, (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv);
1871 stats[ST_F_ACT] = mkf_u32(0, px->srv_act);
1872 stats[ST_F_BCK] = mkf_u32(0, px->srv_bck);
1873 stats[ST_F_CHKDOWN] = mkf_u64(FN_COUNTER, px->down_trans);
1874 stats[ST_F_LASTCHG] = mkf_u32(FN_AGE, now.tv_sec - px->last_change);
1875 if (px->srv)
1876 stats[ST_F_DOWNTIME] = mkf_u32(FN_COUNTER, be_downtime(px));
1877
1878 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
1879 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1880 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, 0);
1881 stats[ST_F_LBTOT] = mkf_u64(FN_COUNTER, px->be_counters.cum_lbconn);
1882 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_BE);
1883 stats[ST_F_RATE] = mkf_u32(0, read_freq_ctr(&px->be_sess_per_sec));
1884 stats[ST_F_RATE_MAX] = mkf_u32(0, px->be_counters.sps_max);
1885
1886 if (flags & ST_SHLGNDS) {
1887 if (px->cookie_name)
1888 stats[ST_F_COOKIE] = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, px->cookie_name);
1889 stats[ST_F_ALGO] = mkf_str(FO_CONFIG|FS_SERVICE, backend_lb_algo_str(px->lbprm.algo & BE_LB_ALGO));
1890 }
1891
1892 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
1893 if (px->mode == PR_MODE_HTTP) {
1894 stats[ST_F_REQ_TOT] = mkf_u64(FN_COUNTER, px->be_counters.p.http.cum_req);
1895 stats[ST_F_HRSP_1XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[1]);
1896 stats[ST_F_HRSP_2XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[2]);
1897 stats[ST_F_HRSP_3XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[3]);
1898 stats[ST_F_HRSP_4XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[4]);
1899 stats[ST_F_HRSP_5XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[5]);
1900 stats[ST_F_HRSP_OTHER] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[0]);
Willy Tarreaua1214a52018-12-14 14:00:25 +01001901 stats[ST_F_CACHE_LOOKUPS] = mkf_u64(FN_COUNTER, px->be_counters.p.http.cache_lookups);
1902 stats[ST_F_CACHE_HITS] = mkf_u64(FN_COUNTER, px->be_counters.p.http.cache_hits);
William Lallemand74c24fb2016-11-21 17:18:36 +01001903 }
1904
1905 stats[ST_F_CLI_ABRT] = mkf_u64(FN_COUNTER, px->be_counters.cli_aborts);
1906 stats[ST_F_SRV_ABRT] = mkf_u64(FN_COUNTER, px->be_counters.srv_aborts);
1907
1908 /* compression: in, out, bypassed, responses */
1909 stats[ST_F_COMP_IN] = mkf_u64(FN_COUNTER, px->be_counters.comp_in);
1910 stats[ST_F_COMP_OUT] = mkf_u64(FN_COUNTER, px->be_counters.comp_out);
1911 stats[ST_F_COMP_BYP] = mkf_u64(FN_COUNTER, px->be_counters.comp_byp);
1912 stats[ST_F_COMP_RSP] = mkf_u64(FN_COUNTER, px->be_counters.p.http.comp_rsp);
1913 stats[ST_F_LASTSESS] = mkf_s32(FN_AGE, be_lastsession(px));
1914
1915 stats[ST_F_QTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.q_time, TIME_STATS_SAMPLES));
1916 stats[ST_F_CTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.c_time, TIME_STATS_SAMPLES));
1917 stats[ST_F_RTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.d_time, TIME_STATS_SAMPLES));
1918 stats[ST_F_TTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.t_time, TIME_STATS_SAMPLES));
1919
1920 return 1;
1921}
1922
1923/* Dumps a line for backend <px> to the trash for and uses the state from stream
1924 * interface <si> and stats flags <flags>. The caller is responsible for clearing
1925 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
1926 */
1927static int stats_dump_be_stats(struct stream_interface *si, struct proxy *px, int flags)
1928{
1929 struct appctx *appctx = __objt_appctx(si->end);
1930
1931 if (!(px->cap & PR_CAP_BE))
1932 return 0;
1933
1934 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_BE)))
1935 return 0;
1936
1937 if (!stats_fill_be_stats(px, flags, stats, ST_F_TOTAL_FIELDS))
1938 return 0;
1939
1940 return stats_dump_one_line(stats, flags, px, appctx);
1941}
1942
1943/* Dumps the HTML table header for proxy <px> to the trash for and uses the state from
1944 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
1945 * for clearing the trash if needed.
1946 */
1947static void stats_dump_html_px_hdr(struct stream_interface *si, struct proxy *px, struct uri_auth *uri)
1948{
1949 struct appctx *appctx = __objt_appctx(si->end);
1950 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
1951
1952 if (px->cap & PR_CAP_BE && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
1953 /* A form to enable/disable this proxy servers */
1954
1955 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
1956 scope_txt[0] = 0;
1957 if (appctx->ctx.stats.scope_len) {
Christopher Fauleted7a0662019-01-14 11:07:34 +01001958 const char *scope_ptr = stats_scope_ptr(appctx, si);
1959
William Lallemand74c24fb2016-11-21 17:18:36 +01001960 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Christopher Fauleted7a0662019-01-14 11:07:34 +01001961 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), scope_ptr, appctx->ctx.stats.scope_len);
William Lallemand74c24fb2016-11-21 17:18:36 +01001962 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
1963 }
1964
1965 chunk_appendf(&trash,
1966 "<form method=\"post\">");
1967 }
1968
1969 /* print a new table */
1970 chunk_appendf(&trash,
1971 "<table class=\"tbl\" width=\"100%%\">\n"
1972 "<tr class=\"titre\">"
1973 "<th class=\"pxname\" width=\"10%%\">");
1974
1975 chunk_appendf(&trash,
1976 "<a name=\"%s\"></a>%s"
1977 "<a class=px href=\"#%s\">%s</a>",
1978 px->id,
1979 (uri->flags & ST_SHLGNDS) ? "<u>":"",
1980 px->id, px->id);
1981
1982 if (uri->flags & ST_SHLGNDS) {
1983 /* cap, mode, id */
1984 chunk_appendf(&trash, "<div class=tips>cap: %s, mode: %s, id: %d",
1985 proxy_cap_str(px->cap), proxy_mode_str(px->mode),
1986 px->uuid);
1987 chunk_appendf(&trash, "</div>");
1988 }
1989
1990 chunk_appendf(&trash,
1991 "%s</th>"
1992 "<th class=\"%s\" width=\"90%%\">%s</th>"
1993 "</tr>\n"
1994 "</table>\n"
1995 "<table class=\"tbl\" width=\"100%%\">\n"
1996 "<tr class=\"titre\">",
1997 (uri->flags & ST_SHLGNDS) ? "</u>":"",
1998 px->desc ? "desc" : "empty", px->desc ? px->desc : "");
1999
2000 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
2001 /* Column heading for Enable or Disable server */
David Harrigand3db35a2016-12-30 12:12:49 +00002002 chunk_appendf(&trash,
2003 "<th rowspan=2 width=1><input type=\"checkbox\" \
2004 onclick=\"for(c in document.getElementsByClassName('%s-checkbox')) \
2005 document.getElementsByClassName('%s-checkbox').item(c).checked = this.checked\"></th>",
2006 px->id,
2007 px->id);
William Lallemand74c24fb2016-11-21 17:18:36 +01002008 }
2009
2010 chunk_appendf(&trash,
2011 "<th rowspan=2></th>"
2012 "<th colspan=3>Queue</th>"
2013 "<th colspan=3>Session rate</th><th colspan=6>Sessions</th>"
2014 "<th colspan=2>Bytes</th><th colspan=2>Denied</th>"
2015 "<th colspan=3>Errors</th><th colspan=2>Warnings</th>"
2016 "<th colspan=9>Server</th>"
2017 "</tr>\n"
2018 "<tr class=\"titre\">"
2019 "<th>Cur</th><th>Max</th><th>Limit</th>"
2020 "<th>Cur</th><th>Max</th><th>Limit</th><th>Cur</th><th>Max</th>"
2021 "<th>Limit</th><th>Total</th><th>LbTot</th><th>Last</th><th>In</th><th>Out</th>"
2022 "<th>Req</th><th>Resp</th><th>Req</th><th>Conn</th>"
2023 "<th>Resp</th><th>Retr</th><th>Redis</th>"
2024 "<th>Status</th><th>LastChk</th><th>Wght</th><th>Act</th>"
2025 "<th>Bck</th><th>Chk</th><th>Dwn</th><th>Dwntme</th>"
2026 "<th>Thrtle</th>\n"
2027 "</tr>");
2028}
2029
2030/* Dumps the HTML table trailer for proxy <px> to the trash for and uses the state from
2031 * stream interface <si>. The caller is responsible for clearing the trash if needed.
2032 */
2033static void stats_dump_html_px_end(struct stream_interface *si, struct proxy *px)
2034{
2035 struct appctx *appctx = __objt_appctx(si->end);
2036 chunk_appendf(&trash, "</table>");
2037
2038 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
2039 /* close the form used to enable/disable this proxy servers */
2040 chunk_appendf(&trash,
2041 "Choose the action to perform on the checked servers : "
2042 "<select name=action>"
2043 "<option value=\"\"></option>"
2044 "<option value=\"ready\">Set state to READY</option>"
2045 "<option value=\"drain\">Set state to DRAIN</option>"
2046 "<option value=\"maint\">Set state to MAINT</option>"
2047 "<option value=\"dhlth\">Health: disable checks</option>"
2048 "<option value=\"ehlth\">Health: enable checks</option>"
2049 "<option value=\"hrunn\">Health: force UP</option>"
2050 "<option value=\"hnolb\">Health: force NOLB</option>"
2051 "<option value=\"hdown\">Health: force DOWN</option>"
2052 "<option value=\"dagent\">Agent: disable checks</option>"
2053 "<option value=\"eagent\">Agent: enable checks</option>"
2054 "<option value=\"arunn\">Agent: force UP</option>"
2055 "<option value=\"adown\">Agent: force DOWN</option>"
2056 "<option value=\"shutdown\">Kill Sessions</option>"
2057 "</select>"
2058 "<input type=\"hidden\" name=\"b\" value=\"#%d\">"
2059 "&nbsp;<input type=\"submit\" value=\"Apply\">"
2060 "</form>",
2061 px->uuid);
2062 }
2063
2064 chunk_appendf(&trash, "<p>\n");
2065}
2066
2067/*
2068 * Dumps statistics for a proxy. The output is sent to the stream interface's
2069 * input buffer. Returns 0 if it had to stop dumping data because of lack of
2070 * buffer space, or non-zero if everything completed. This function is used
2071 * both by the CLI and the HTTP entry points, and is able to dump the output
2072 * in HTML or CSV formats. If the later, <uri> must be NULL.
2073 */
Christopher Fauletef779222018-10-31 08:47:01 +01002074int stats_dump_proxy_to_buffer(struct stream_interface *si, struct htx *htx,
2075 struct proxy *px, struct uri_auth *uri)
William Lallemand74c24fb2016-11-21 17:18:36 +01002076{
2077 struct appctx *appctx = __objt_appctx(si->end);
2078 struct stream *s = si_strm(si);
2079 struct channel *rep = si_ic(si);
2080 struct server *sv, *svs; /* server and server-state, server-state=server or server->track */
2081 struct listener *l;
2082 unsigned int flags;
2083
2084 if (uri)
2085 flags = uri->flags;
William Lallemand07a62f72017-05-24 00:57:40 +02002086 else if ((strm_li(s)->bind_conf->level & ACCESS_LVL_MASK) >= ACCESS_LVL_OPER)
William Lallemand74c24fb2016-11-21 17:18:36 +01002087 flags = ST_SHLGNDS | ST_SHNODE | ST_SHDESC;
2088 else
2089 flags = ST_SHNODE | ST_SHDESC;
2090
2091 chunk_reset(&trash);
2092
2093 switch (appctx->ctx.stats.px_st) {
2094 case STAT_PX_ST_INIT:
2095 /* we are on a new proxy */
2096 if (uri && uri->scope) {
2097 /* we have a limited scope, we have to check the proxy name */
2098 struct stat_scope *scope;
2099 int len;
2100
2101 len = strlen(px->id);
2102 scope = uri->scope;
2103
2104 while (scope) {
2105 /* match exact proxy name */
2106 if (scope->px_len == len && !memcmp(px->id, scope->px_id, len))
2107 break;
2108
2109 /* match '.' which means 'self' proxy */
2110 if (!strcmp(scope->px_id, ".") && px == s->be)
2111 break;
2112 scope = scope->next;
2113 }
2114
2115 /* proxy name not found : don't dump anything */
2116 if (scope == NULL)
2117 return 1;
2118 }
2119
2120 /* if the user has requested a limited output and the proxy
2121 * name does not match, skip it.
2122 */
Christopher Fauleted7a0662019-01-14 11:07:34 +01002123 if (appctx->ctx.stats.scope_len) {
2124 const char *scope_ptr = stats_scope_ptr(appctx, si);
2125
2126 if (strnistr(px->id, strlen(px->id), scope_ptr, appctx->ctx.stats.scope_len) == NULL)
2127 return 1;
2128 }
William Lallemand74c24fb2016-11-21 17:18:36 +01002129
2130 if ((appctx->ctx.stats.flags & STAT_BOUND) &&
2131 (appctx->ctx.stats.iid != -1) &&
2132 (px->uuid != appctx->ctx.stats.iid))
2133 return 1;
2134
2135 appctx->ctx.stats.px_st = STAT_PX_ST_TH;
2136 /* fall through */
2137
2138 case STAT_PX_ST_TH:
2139 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
2140 stats_dump_html_px_hdr(si, px, uri);
Christopher Fauletef779222018-10-31 08:47:01 +01002141 if (!stats_putchk(rep, htx, &trash))
2142 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002143 }
2144
2145 appctx->ctx.stats.px_st = STAT_PX_ST_FE;
2146 /* fall through */
2147
2148 case STAT_PX_ST_FE:
2149 /* print the frontend */
2150 if (stats_dump_fe_stats(si, px)) {
Christopher Fauletef779222018-10-31 08:47:01 +01002151 if (!stats_putchk(rep, htx, &trash))
2152 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002153 }
2154
2155 appctx->ctx.stats.l = px->conf.listeners.n;
2156 appctx->ctx.stats.px_st = STAT_PX_ST_LI;
2157 /* fall through */
2158
2159 case STAT_PX_ST_LI:
2160 /* stats.l has been initialized above */
2161 for (; appctx->ctx.stats.l != &px->conf.listeners; appctx->ctx.stats.l = l->by_fe.n) {
Christopher Fauletef779222018-10-31 08:47:01 +01002162 if (htx) {
2163 if (htx_almost_full(htx))
2164 goto full;
2165 }
2166 else {
2167 if (buffer_almost_full(&rep->buf))
2168 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002169 }
2170
2171 l = LIST_ELEM(appctx->ctx.stats.l, struct listener *, by_fe);
2172 if (!l->counters)
2173 continue;
2174
2175 if (appctx->ctx.stats.flags & STAT_BOUND) {
2176 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SO)))
2177 break;
2178
2179 if (appctx->ctx.stats.sid != -1 && l->luid != appctx->ctx.stats.sid)
2180 continue;
2181 }
2182
2183 /* print the frontend */
2184 if (stats_dump_li_stats(si, px, l, flags)) {
Christopher Fauletef779222018-10-31 08:47:01 +01002185 if (!stats_putchk(rep, htx, &trash))
2186 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002187 }
2188 }
2189
2190 appctx->ctx.stats.sv = px->srv; /* may be NULL */
2191 appctx->ctx.stats.px_st = STAT_PX_ST_SV;
2192 /* fall through */
2193
2194 case STAT_PX_ST_SV:
2195 /* stats.sv has been initialized above */
2196 for (; appctx->ctx.stats.sv != NULL; appctx->ctx.stats.sv = sv->next) {
Christopher Fauletef779222018-10-31 08:47:01 +01002197 if (htx) {
2198 if (htx_almost_full(htx))
2199 goto full;
2200 }
2201 else {
2202 if (buffer_almost_full(&rep->buf))
2203 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002204 }
2205
2206 sv = appctx->ctx.stats.sv;
2207
2208 if (appctx->ctx.stats.flags & STAT_BOUND) {
2209 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SV)))
2210 break;
2211
2212 if (appctx->ctx.stats.sid != -1 && sv->puid != appctx->ctx.stats.sid)
2213 continue;
2214 }
2215
2216 svs = sv;
2217 while (svs->track)
2218 svs = svs->track;
2219
2220 /* do not report servers which are DOWN and not changing state */
2221 if ((appctx->ctx.stats.flags & STAT_HIDE_DOWN) &&
Emeric Brun52a91d32017-08-31 14:41:55 +02002222 ((sv->cur_admin & SRV_ADMF_MAINT) || /* server is in maintenance */
2223 (sv->cur_state == SRV_ST_STOPPED && /* server is down */
William Lallemand74c24fb2016-11-21 17:18:36 +01002224 (!((svs->agent.state | svs->check.state) & CHK_ST_ENABLED) ||
2225 ((svs->agent.state & CHK_ST_ENABLED) && !svs->agent.health) ||
2226 ((svs->check.state & CHK_ST_ENABLED) && !svs->check.health))))) {
2227 continue;
2228 }
2229
2230 if (stats_dump_sv_stats(si, px, flags, sv)) {
Christopher Fauletef779222018-10-31 08:47:01 +01002231 if (!stats_putchk(rep, htx, &trash))
2232 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002233 }
2234 } /* for sv */
2235
2236 appctx->ctx.stats.px_st = STAT_PX_ST_BE;
2237 /* fall through */
2238
2239 case STAT_PX_ST_BE:
2240 /* print the backend */
2241 if (stats_dump_be_stats(si, px, flags)) {
Christopher Fauletef779222018-10-31 08:47:01 +01002242 if (!stats_putchk(rep, htx, &trash))
2243 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002244 }
2245
2246 appctx->ctx.stats.px_st = STAT_PX_ST_END;
2247 /* fall through */
2248
2249 case STAT_PX_ST_END:
2250 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
2251 stats_dump_html_px_end(si, px);
Christopher Fauletef779222018-10-31 08:47:01 +01002252 if (!stats_putchk(rep, htx, &trash))
2253 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002254 }
2255
2256 appctx->ctx.stats.px_st = STAT_PX_ST_FIN;
2257 /* fall through */
2258
2259 case STAT_PX_ST_FIN:
2260 return 1;
2261
2262 default:
2263 /* unknown state, we should put an abort() here ! */
2264 return 1;
2265 }
Christopher Fauletef779222018-10-31 08:47:01 +01002266
2267 full:
2268 si_rx_room_blk(si);
2269 return 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01002270}
2271
2272/* Dumps the HTTP stats head block to the trash for and uses the per-uri
2273 * parameters <uri>. The caller is responsible for clearing the trash if needed.
2274 */
2275static void stats_dump_html_head(struct uri_auth *uri)
2276{
2277 /* WARNING! This must fit in the first buffer !!! */
2278 chunk_appendf(&trash,
2279 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n"
2280 "\"http://www.w3.org/TR/html4/loose.dtd\">\n"
2281 "<html><head><title>Statistics Report for " PRODUCT_NAME "%s%s</title>\n"
2282 "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n"
2283 "<style type=\"text/css\"><!--\n"
2284 "body {"
2285 " font-family: arial, helvetica, sans-serif;"
2286 " font-size: 12px;"
2287 " font-weight: normal;"
2288 " color: black;"
2289 " background: white;"
2290 "}\n"
2291 "th,td {"
2292 " font-size: 10px;"
2293 "}\n"
2294 "h1 {"
2295 " font-size: x-large;"
2296 " margin-bottom: 0.5em;"
2297 "}\n"
2298 "h2 {"
2299 " font-family: helvetica, arial;"
2300 " font-size: x-large;"
2301 " font-weight: bold;"
2302 " font-style: italic;"
2303 " color: #6020a0;"
2304 " margin-top: 0em;"
2305 " margin-bottom: 0em;"
2306 "}\n"
2307 "h3 {"
2308 " font-family: helvetica, arial;"
2309 " font-size: 16px;"
2310 " font-weight: bold;"
2311 " color: #b00040;"
2312 " background: #e8e8d0;"
2313 " margin-top: 0em;"
2314 " margin-bottom: 0em;"
2315 "}\n"
2316 "li {"
2317 " margin-top: 0.25em;"
2318 " margin-right: 2em;"
2319 "}\n"
2320 ".hr {margin-top: 0.25em;"
2321 " border-color: black;"
2322 " border-bottom-style: solid;"
2323 "}\n"
2324 ".titre {background: #20D0D0;color: #000000; font-weight: bold; text-align: center;}\n"
2325 ".total {background: #20D0D0;color: #ffff80;}\n"
2326 ".frontend {background: #e8e8d0;}\n"
2327 ".socket {background: #d0d0d0;}\n"
2328 ".backend {background: #e8e8d0;}\n"
2329 ".active_down {background: #ff9090;}\n"
2330 ".active_going_up {background: #ffd020;}\n"
2331 ".active_going_down {background: #ffffa0;}\n"
2332 ".active_up {background: #c0ffc0;}\n"
2333 ".active_nolb {background: #20a0ff;}\n"
2334 ".active_draining {background: #20a0FF;}\n"
2335 ".active_no_check {background: #e0e0e0;}\n"
2336 ".backup_down {background: #ff9090;}\n"
2337 ".backup_going_up {background: #ff80ff;}\n"
2338 ".backup_going_down {background: #c060ff;}\n"
2339 ".backup_up {background: #b0d0ff;}\n"
2340 ".backup_nolb {background: #90b0e0;}\n"
2341 ".backup_draining {background: #cc9900;}\n"
2342 ".backup_no_check {background: #e0e0e0;}\n"
2343 ".maintain {background: #c07820;}\n"
2344 ".rls {letter-spacing: 0.2em; margin-right: 1px;}\n" /* right letter spacing (used for grouping digits) */
2345 "\n"
2346 "a.px:link {color: #ffff40; text-decoration: none;}"
2347 "a.px:visited {color: #ffff40; text-decoration: none;}"
2348 "a.px:hover {color: #ffffff; text-decoration: none;}"
2349 "a.lfsb:link {color: #000000; text-decoration: none;}"
2350 "a.lfsb:visited {color: #000000; text-decoration: none;}"
2351 "a.lfsb:hover {color: #505050; text-decoration: none;}"
2352 "\n"
2353 "table.tbl { border-collapse: collapse; border-style: none;}\n"
2354 "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"
2355 "table.tbl td.ac { text-align: center;}\n"
2356 "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}\n"
2357 "table.tbl th.pxname { background: #b00040; color: #ffff40; font-weight: bold; border-style: solid solid none solid; padding: 2px 3px; white-space: nowrap;}\n"
2358 "table.tbl th.empty { border-style: none; empty-cells: hide; background: white;}\n"
2359 "table.tbl th.desc { background: white; border-style: solid solid none solid; text-align: left; padding: 2px 3px;}\n"
2360 "\n"
2361 "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n"
2362 "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n"
2363 "table.lgd td.noborder { border-style: none; padding: 2px; white-space: nowrap;}\n"
2364 "table.det { border-collapse: collapse; border-style: none; }\n"
2365 "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"
2366 "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"
2367 "u {text-decoration:none; border-bottom: 1px dotted black;}\n"
2368 "div.tips {\n"
2369 " display:block;\n"
2370 " visibility:hidden;\n"
2371 " z-index:2147483647;\n"
2372 " position:absolute;\n"
2373 " padding:2px 4px 3px;\n"
2374 " background:#f0f060; color:#000000;\n"
2375 " border:1px solid #7040c0;\n"
2376 " white-space:nowrap;\n"
2377 " font-style:normal;font-size:11px;font-weight:normal;\n"
2378 " -moz-border-radius:3px;-webkit-border-radius:3px;border-radius:3px;\n"
2379 " -moz-box-shadow:gray 2px 2px 3px;-webkit-box-shadow:gray 2px 2px 3px;box-shadow:gray 2px 2px 3px;\n"
2380 "}\n"
2381 "u:hover div.tips {visibility:visible;}\n"
2382 "-->\n"
2383 "</style></head>\n",
2384 (uri->flags & ST_SHNODE) ? " on " : "",
2385 (uri->flags & ST_SHNODE) ? (uri->node ? uri->node : global.node) : ""
2386 );
2387}
2388
2389/* Dumps the HTML stats information block to the trash for and uses the state from
2390 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
2391 * for clearing the trash if needed.
2392 */
2393static void stats_dump_html_info(struct stream_interface *si, struct uri_auth *uri)
2394{
2395 struct appctx *appctx = __objt_appctx(si->end);
2396 unsigned int up = (now.tv_sec - start_date.tv_sec);
2397 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
Christopher Fauleted7a0662019-01-14 11:07:34 +01002398 const char *scope_ptr = stats_scope_ptr(appctx, si);
Willy Tarreau1713c032019-05-23 12:23:55 +02002399 unsigned long long bps = (unsigned long long)read_freq_ctr(&global.out_32bps) * 32;
2400
2401 /* Turn the bytes per second to bits per second and take care of the
2402 * usual ethernet overhead in order to help figure how far we are from
2403 * interface saturation since it's the only case which usually matters.
2404 * For this we count the total size of an Ethernet frame on the wire
2405 * including preamble and IFG (1538) for the largest TCP segment it
2406 * transports (1448 with TCP timestamps). This is not valid for smaller
2407 * packets (under-estimated), but it gives a reasonably accurate
2408 * estimation of how far we are from uplink saturation.
2409 */
2410 bps = bps * 8 * 1538 / 1448;
William Lallemand74c24fb2016-11-21 17:18:36 +01002411
2412 /* WARNING! this has to fit the first packet too.
2413 * We are around 3.5 kB, add adding entries will
2414 * become tricky if we want to support 4kB buffers !
2415 */
2416 chunk_appendf(&trash,
2417 "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">"
2418 PRODUCT_NAME "%s</a></h1>\n"
2419 "<h2>Statistics Report for pid %d%s%s%s%s</h2>\n"
2420 "<hr width=\"100%%\" class=\"hr\">\n"
2421 "<h3>&gt; General process information</h3>\n"
2422 "<table border=0><tr><td align=\"left\" nowrap width=\"1%%\">\n"
Yves Lafon95317282018-02-26 11:10:37 +01002423 "<p><b>pid = </b> %d (process #%d, nbproc = %d, nbthread = %d)<br>\n"
William Lallemand74c24fb2016-11-21 17:18:36 +01002424 "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n"
2425 "<b>system limits:</b> memmax = %s%s; ulimit-n = %d<br>\n"
2426 "<b>maxsock = </b> %d; <b>maxconn = </b> %d; <b>maxpipes = </b> %d<br>\n"
Willy Tarreau1713c032019-05-23 12:23:55 +02002427 "current conns = %d; current pipes = %d/%d; conn rate = %d/sec; bit rate = %.3f %cbps<br>\n"
William Lallemand74c24fb2016-11-21 17:18:36 +01002428 "Running tasks: %d/%d; idle = %d %%<br>\n"
2429 "</td><td align=\"center\" nowrap>\n"
2430 "<table class=\"lgd\"><tr>\n"
2431 "<td class=\"active_up\">&nbsp;</td><td class=\"noborder\">active UP </td>"
2432 "<td class=\"backup_up\">&nbsp;</td><td class=\"noborder\">backup UP </td>"
2433 "</tr><tr>\n"
2434 "<td class=\"active_going_down\"></td><td class=\"noborder\">active UP, going down </td>"
2435 "<td class=\"backup_going_down\"></td><td class=\"noborder\">backup UP, going down </td>"
2436 "</tr><tr>\n"
2437 "<td class=\"active_going_up\"></td><td class=\"noborder\">active DOWN, going up </td>"
2438 "<td class=\"backup_going_up\"></td><td class=\"noborder\">backup DOWN, going up </td>"
2439 "</tr><tr>\n"
2440 "<td class=\"active_down\"></td><td class=\"noborder\">active or backup DOWN &nbsp;</td>"
2441 "<td class=\"active_no_check\"></td><td class=\"noborder\">not checked </td>"
2442 "</tr><tr>\n"
2443 "<td class=\"maintain\"></td><td class=\"noborder\" colspan=\"3\">active or backup DOWN for maintenance (MAINT) &nbsp;</td>"
2444 "</tr><tr>\n"
2445 "<td class=\"active_draining\"></td><td class=\"noborder\" colspan=\"3\">active or backup SOFT STOPPED for maintenance &nbsp;</td>"
2446 "</tr></table>\n"
2447 "Note: \"NOLB\"/\"DRAIN\" = UP with load-balancing disabled."
2448 "</td>"
2449 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
2450 "<b>Display option:</b><ul style=\"margin-top: 0.25em;\">"
2451 "",
Willy Tarreau909b9d82019-01-04 18:20:32 +01002452 (uri->flags & ST_HIDEVER) ? "" : (stats_version_string),
William Lallemand74c24fb2016-11-21 17:18:36 +01002453 pid, (uri->flags & ST_SHNODE) ? " on " : "",
2454 (uri->flags & ST_SHNODE) ? (uri->node ? uri->node : global.node) : "",
2455 (uri->flags & ST_SHDESC) ? ": " : "",
2456 (uri->flags & ST_SHDESC) ? (uri->desc ? uri->desc : global.desc) : "",
Yves Lafon95317282018-02-26 11:10:37 +01002457 pid, relative_pid, global.nbproc, global.nbthread,
William Lallemand74c24fb2016-11-21 17:18:36 +01002458 up / 86400, (up % 86400) / 3600,
2459 (up % 3600) / 60, (up % 60),
2460 global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited",
2461 global.rlimit_memmax ? " MB" : "",
2462 global.rlimit_nofile,
2463 global.maxsock, global.maxconn, global.maxpipes,
2464 actconn, pipes_used, pipes_used+pipes_free, read_freq_ctr(&global.conn_per_sec),
Willy Tarreau1713c032019-05-23 12:23:55 +02002465 bps >= 1000000000UL ? (bps / 1000000000.0) : bps >= 1000000UL ? (bps / 1000000.0) : (bps / 1000.0),
2466 bps >= 1000000000UL ? 'G' : bps >= 1000000UL ? 'M' : 'k',
Willy Tarreau81036f22019-05-20 19:24:50 +02002467 tasks_run_queue_cur, nb_tasks_cur, ti->idle_pct
William Lallemand74c24fb2016-11-21 17:18:36 +01002468 );
2469
2470 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Christopher Fauleted7a0662019-01-14 11:07:34 +01002471 memcpy(scope_txt, scope_ptr, appctx->ctx.stats.scope_len);
William Lallemand74c24fb2016-11-21 17:18:36 +01002472 scope_txt[appctx->ctx.stats.scope_len] = '\0';
2473
2474 chunk_appendf(&trash,
2475 "<li><form method=\"GET\">Scope : <input value=\"%s\" name=\"" STAT_SCOPE_INPUT_NAME "\" size=\"8\" maxlength=\"%d\" tabindex=\"1\"/></form>\n",
2476 (appctx->ctx.stats.scope_len > 0) ? scope_txt : "",
2477 STAT_SCOPE_TXT_MAXLEN);
2478
2479 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
2480 scope_txt[0] = 0;
2481 if (appctx->ctx.stats.scope_len) {
2482 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Christopher Fauleted7a0662019-01-14 11:07:34 +01002483 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), scope_ptr, appctx->ctx.stats.scope_len);
William Lallemand74c24fb2016-11-21 17:18:36 +01002484 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
2485 }
2486
2487 if (appctx->ctx.stats.flags & STAT_HIDE_DOWN)
2488 chunk_appendf(&trash,
2489 "<li><a href=\"%s%s%s%s\">Show all servers</a><br>\n",
2490 uri->uri_prefix,
2491 "",
2492 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2493 scope_txt);
2494 else
2495 chunk_appendf(&trash,
2496 "<li><a href=\"%s%s%s%s\">Hide 'DOWN' servers</a><br>\n",
2497 uri->uri_prefix,
2498 ";up",
2499 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2500 scope_txt);
2501
2502 if (uri->refresh > 0) {
2503 if (appctx->ctx.stats.flags & STAT_NO_REFRESH)
2504 chunk_appendf(&trash,
2505 "<li><a href=\"%s%s%s%s\">Enable refresh</a><br>\n",
2506 uri->uri_prefix,
2507 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2508 "",
2509 scope_txt);
2510 else
2511 chunk_appendf(&trash,
2512 "<li><a href=\"%s%s%s%s\">Disable refresh</a><br>\n",
2513 uri->uri_prefix,
2514 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2515 ";norefresh",
2516 scope_txt);
2517 }
2518
2519 chunk_appendf(&trash,
2520 "<li><a href=\"%s%s%s%s\">Refresh now</a><br>\n",
2521 uri->uri_prefix,
2522 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2523 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2524 scope_txt);
2525
2526 chunk_appendf(&trash,
2527 "<li><a href=\"%s;csv%s%s\">CSV export</a><br>\n",
2528 uri->uri_prefix,
2529 (uri->refresh > 0) ? ";norefresh" : "",
2530 scope_txt);
2531
2532 chunk_appendf(&trash,
Christopher Faulet6338a082019-09-09 15:50:54 +02002533 "<li><a href=\"%s;json%s%s\">JSON export</a> (<a href=\"%s;json-schema\">schema</a>)<br>\n",
2534 uri->uri_prefix,
2535 (uri->refresh > 0) ? ";norefresh" : "",
2536 scope_txt, uri->uri_prefix);
2537
2538 chunk_appendf(&trash,
William Lallemand74c24fb2016-11-21 17:18:36 +01002539 "</ul></td>"
2540 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
2541 "<b>External resources:</b><ul style=\"margin-top: 0.25em;\">\n"
2542 "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>\n"
2543 "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>\n"
2544 "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>\n"
2545 "</ul>"
2546 "</td>"
2547 "</tr></table>\n"
2548 ""
2549 );
2550
2551 if (appctx->ctx.stats.st_code) {
2552 switch (appctx->ctx.stats.st_code) {
2553 case STAT_STATUS_DONE:
2554 chunk_appendf(&trash,
2555 "<p><div class=active_up>"
2556 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2557 "Action processed successfully."
2558 "</div>\n", uri->uri_prefix,
2559 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2560 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2561 scope_txt);
2562 break;
2563 case STAT_STATUS_NONE:
2564 chunk_appendf(&trash,
2565 "<p><div class=active_going_down>"
2566 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2567 "Nothing has changed."
2568 "</div>\n", uri->uri_prefix,
2569 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2570 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2571 scope_txt);
2572 break;
2573 case STAT_STATUS_PART:
2574 chunk_appendf(&trash,
2575 "<p><div class=active_going_down>"
2576 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2577 "Action partially processed.<br>"
2578 "Some server names are probably unknown or ambiguous (duplicated names in the backend)."
2579 "</div>\n", uri->uri_prefix,
2580 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2581 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2582 scope_txt);
2583 break;
2584 case STAT_STATUS_ERRP:
2585 chunk_appendf(&trash,
2586 "<p><div class=active_down>"
2587 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2588 "Action not processed because of invalid parameters."
2589 "<ul>"
2590 "<li>The action is maybe unknown.</li>"
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01002591 "<li>Invalid key parameter (empty or too long).</li>"
William Lallemand74c24fb2016-11-21 17:18:36 +01002592 "<li>The backend name is probably unknown or ambiguous (duplicated names).</li>"
2593 "<li>Some server names are probably unknown or ambiguous (duplicated names in the backend).</li>"
2594 "</ul>"
2595 "</div>\n", uri->uri_prefix,
2596 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2597 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2598 scope_txt);
2599 break;
2600 case STAT_STATUS_EXCD:
2601 chunk_appendf(&trash,
2602 "<p><div class=active_down>"
2603 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2604 "<b>Action not processed : the buffer couldn't store all the data.<br>"
2605 "You should retry with less servers at a time.</b>"
2606 "</div>\n", uri->uri_prefix,
2607 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2608 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2609 scope_txt);
2610 break;
2611 case STAT_STATUS_DENY:
2612 chunk_appendf(&trash,
2613 "<p><div class=active_down>"
2614 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2615 "<b>Action denied.</b>"
2616 "</div>\n", uri->uri_prefix,
2617 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2618 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2619 scope_txt);
2620 break;
Christopher Faulet3c2ecf72019-02-27 16:41:27 +01002621 case STAT_STATUS_IVAL:
2622 chunk_appendf(&trash,
2623 "<p><div class=active_down>"
2624 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2625 "<b>Invalid requests (unsupported method or chunked encoded request).</b>"
2626 "</div>\n", uri->uri_prefix,
2627 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2628 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2629 scope_txt);
2630 break;
William Lallemand74c24fb2016-11-21 17:18:36 +01002631 default:
2632 chunk_appendf(&trash,
2633 "<p><div class=active_no_check>"
2634 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2635 "Unexpected result."
2636 "</div>\n", uri->uri_prefix,
2637 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2638 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2639 scope_txt);
2640 }
2641 chunk_appendf(&trash, "<p>\n");
2642 }
2643}
2644
2645/* Dumps the HTML stats trailer block to the trash. The caller is responsible
2646 * for clearing the trash if needed.
2647 */
2648static void stats_dump_html_end()
2649{
2650 chunk_appendf(&trash, "</body></html>\n");
2651}
2652
Simon Horman05ee2132017-01-04 09:37:25 +01002653/* Dumps the stats JSON header to the trash buffer which. The caller is responsible
2654 * for clearing it if needed.
2655 */
2656static void stats_dump_json_header()
2657{
2658 chunk_strcat(&trash, "[");
2659}
2660
2661
2662/* Dumps the JSON stats trailer block to the trash. The caller is responsible
2663 * for clearing the trash if needed.
2664 */
2665static void stats_dump_json_end()
2666{
2667 chunk_strcat(&trash, "]");
2668}
2669
William Lallemand74c24fb2016-11-21 17:18:36 +01002670/* This function dumps statistics onto the stream interface's read buffer in
2671 * either CSV or HTML format. <uri> contains some HTML-specific parameters that
2672 * are ignored for CSV format (hence <uri> may be NULL there). It returns 0 if
2673 * it had to stop writing data and an I/O is needed, 1 if the dump is finished
2674 * and the stream must be closed, or -1 in case of any error. This function is
2675 * used by both the CLI and the HTTP handlers.
2676 */
Christopher Fauletef779222018-10-31 08:47:01 +01002677static int stats_dump_stat_to_buffer(struct stream_interface *si, struct htx *htx,
2678 struct uri_auth *uri)
William Lallemand74c24fb2016-11-21 17:18:36 +01002679{
2680 struct appctx *appctx = __objt_appctx(si->end);
2681 struct channel *rep = si_ic(si);
2682 struct proxy *px;
2683
2684 chunk_reset(&trash);
2685
2686 switch (appctx->st2) {
2687 case STAT_ST_INIT:
2688 appctx->st2 = STAT_ST_HEAD; /* let's start producing data */
2689 /* fall through */
2690
2691 case STAT_ST_HEAD:
2692 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
2693 stats_dump_html_head(uri);
Christopher Faulet6338a082019-09-09 15:50:54 +02002694 else if (appctx->ctx.stats.flags & STAT_JSON_SCHM)
2695 stats_dump_json_schema(&trash);
Simon Horman05ee2132017-01-04 09:37:25 +01002696 else if (appctx->ctx.stats.flags & STAT_FMT_JSON)
Willy Tarreau9d7fb632017-04-11 07:53:04 +02002697 stats_dump_json_header();
William Lallemand74c24fb2016-11-21 17:18:36 +01002698 else if (!(appctx->ctx.stats.flags & STAT_FMT_TYPED))
2699 stats_dump_csv_header();
2700
Christopher Fauletef779222018-10-31 08:47:01 +01002701 if (!stats_putchk(rep, htx, &trash))
2702 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002703
Christopher Faulet6338a082019-09-09 15:50:54 +02002704 if (appctx->ctx.stats.flags & STAT_JSON_SCHM) {
2705 appctx->st2 = STAT_ST_FIN;
2706 return 1;
2707 }
William Lallemand74c24fb2016-11-21 17:18:36 +01002708 appctx->st2 = STAT_ST_INFO;
2709 /* fall through */
2710
2711 case STAT_ST_INFO:
2712 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
2713 stats_dump_html_info(si, uri);
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
Olivier Houchardfbc74e82017-11-24 16:54:05 +01002718 appctx->ctx.stats.px = proxies_list;
William Lallemand74c24fb2016-11-21 17:18:36 +01002719 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
2720 appctx->st2 = STAT_ST_LIST;
2721 /* fall through */
2722
2723 case STAT_ST_LIST:
2724 /* dump proxies */
2725 while (appctx->ctx.stats.px) {
Christopher Fauletef779222018-10-31 08:47:01 +01002726 if (htx) {
2727 if (htx_almost_full(htx))
2728 goto full;
2729 }
2730 else {
2731 if (buffer_almost_full(&rep->buf))
2732 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002733 }
2734
2735 px = appctx->ctx.stats.px;
2736 /* skip the disabled proxies, global frontend and non-networked ones */
2737 if (px->state != PR_STSTOPPED && px->uuid > 0 && (px->cap & (PR_CAP_FE | PR_CAP_BE)))
Christopher Fauletef779222018-10-31 08:47:01 +01002738 if (stats_dump_proxy_to_buffer(si, htx, px, uri) == 0)
William Lallemand74c24fb2016-11-21 17:18:36 +01002739 return 0;
2740
2741 appctx->ctx.stats.px = px->next;
2742 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
2743 }
2744 /* here, we just have reached the last proxy */
2745
2746 appctx->st2 = STAT_ST_END;
2747 /* fall through */
2748
2749 case STAT_ST_END:
Simon Horman05ee2132017-01-04 09:37:25 +01002750 if (appctx->ctx.stats.flags & (STAT_FMT_HTML|STAT_FMT_JSON)) {
2751 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
2752 stats_dump_html_end();
2753 else
2754 stats_dump_json_end();
Christopher Fauletef779222018-10-31 08:47:01 +01002755 if (!stats_putchk(rep, htx, &trash))
2756 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002757 }
2758
2759 appctx->st2 = STAT_ST_FIN;
2760 /* fall through */
2761
2762 case STAT_ST_FIN:
2763 return 1;
2764
2765 default:
2766 /* unknown state ! */
2767 appctx->st2 = STAT_ST_FIN;
2768 return -1;
2769 }
Christopher Fauletef779222018-10-31 08:47:01 +01002770
2771 full:
2772 si_rx_room_blk(si);
2773 return 0;
2774
William Lallemand74c24fb2016-11-21 17:18:36 +01002775}
2776
2777/* We reached the stats page through a POST request. The appctx is
2778 * expected to have already been allocated by the caller.
2779 * Parse the posted data and enable/disable servers if necessary.
2780 * Returns 1 if request was parsed or zero if it needs more data.
2781 */
2782static int stats_process_http_post(struct stream_interface *si)
2783{
2784 struct stream *s = si_strm(si);
2785 struct appctx *appctx = objt_appctx(si->end);
2786
2787 struct proxy *px = NULL;
2788 struct server *sv = NULL;
2789
2790 char key[LINESIZE];
2791 int action = ST_ADM_ACTION_NONE;
2792 int reprocess = 0;
2793
2794 int total_servers = 0;
2795 int altered_servers = 0;
2796
2797 char *first_param, *cur_param, *next_param, *end_params;
2798 char *st_cur_param = NULL;
2799 char *st_next_param = NULL;
2800
Christopher Fauleta3618372018-12-13 21:59:56 +01002801 struct buffer *temp = get_trash_chunk();
William Lallemand74c24fb2016-11-21 17:18:36 +01002802
Christopher Fauletb7f88902019-07-15 21:56:43 +02002803 struct htx *htx = htxbuf(&s->req.buf);
2804 struct htx_blk *blk;
Christopher Fauleta3618372018-12-13 21:59:56 +01002805
Christopher Fauletb7f88902019-07-15 21:56:43 +02002806 /* we need more data */
2807 if (s->txn->req.msg_state < HTTP_MSG_DONE) {
2808 /* check if we can receive more */
2809 if (htx_free_data_space(htx) <= global.tune.maxrewrite) {
2810 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
2811 goto out;
Christopher Fauleta3618372018-12-13 21:59:56 +01002812 }
Christopher Fauletb7f88902019-07-15 21:56:43 +02002813 goto wait;
2814 }
Christopher Fauleta3618372018-12-13 21:59:56 +01002815
Christopher Fauletb7f88902019-07-15 21:56:43 +02002816 /* The request was fully received. Copy data */
2817 blk = htx_get_head_blk(htx);
2818 while (blk) {
2819 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Fauleta3618372018-12-13 21:59:56 +01002820
Christopher Fauletb7f88902019-07-15 21:56:43 +02002821 if (type == HTX_BLK_EOM || type == HTX_BLK_TLR || type == HTX_BLK_EOT)
2822 break;
2823 if (type == HTX_BLK_DATA) {
2824 struct ist v = htx_get_blk_value(htx, blk);
Christopher Fauleta3618372018-12-13 21:59:56 +01002825
Christopher Fauletb7f88902019-07-15 21:56:43 +02002826 if (!chunk_memcat(temp, v.ptr, v.len)) {
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01002827 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
2828 goto out;
2829 }
Christopher Fauleta3618372018-12-13 21:59:56 +01002830 }
Christopher Fauletb7f88902019-07-15 21:56:43 +02002831 blk = htx_get_next_blk(htx, blk);
William Lallemand74c24fb2016-11-21 17:18:36 +01002832 }
2833
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002834 first_param = temp->area;
Christopher Fauleta3618372018-12-13 21:59:56 +01002835 end_params = temp->area + temp->data;
William Lallemand74c24fb2016-11-21 17:18:36 +01002836 cur_param = next_param = end_params;
2837 *end_params = '\0';
2838
2839 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
2840
2841 /*
2842 * Parse the parameters in reverse order to only store the last value.
2843 * From the html form, the backend and the action are at the end.
2844 */
2845 while (cur_param > first_param) {
2846 char *value;
2847 int poffset, plen;
2848
2849 cur_param--;
2850
2851 if ((*cur_param == '&') || (cur_param == first_param)) {
2852 reprocess_servers:
2853 /* Parse the key */
2854 poffset = (cur_param != first_param ? 1 : 0);
2855 plen = next_param - cur_param + (cur_param == first_param ? 1 : 0);
2856 if ((plen > 0) && (plen <= sizeof(key))) {
2857 strncpy(key, cur_param + poffset, plen);
2858 key[plen - 1] = '\0';
2859 } else {
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01002860 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
William Lallemand74c24fb2016-11-21 17:18:36 +01002861 goto out;
2862 }
2863
2864 /* Parse the value */
2865 value = key;
2866 while (*value != '\0' && *value != '=') {
2867 value++;
2868 }
2869 if (*value == '=') {
2870 /* Ok, a value is found, we can mark the end of the key */
2871 *value++ = '\0';
2872 }
2873 if (url_decode(key) < 0 || url_decode(value) < 0)
2874 break;
2875
2876 /* Now we can check the key to see what to do */
2877 if (!px && (strcmp(key, "b") == 0)) {
2878 if ((px = proxy_be_by_name(value)) == NULL) {
2879 /* the backend name is unknown or ambiguous (duplicate names) */
2880 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
2881 goto out;
2882 }
2883 }
2884 else if (!action && (strcmp(key, "action") == 0)) {
2885 if (strcmp(value, "ready") == 0) {
2886 action = ST_ADM_ACTION_READY;
2887 }
2888 else if (strcmp(value, "drain") == 0) {
2889 action = ST_ADM_ACTION_DRAIN;
2890 }
2891 else if (strcmp(value, "maint") == 0) {
2892 action = ST_ADM_ACTION_MAINT;
2893 }
2894 else if (strcmp(value, "shutdown") == 0) {
2895 action = ST_ADM_ACTION_SHUTDOWN;
2896 }
2897 else if (strcmp(value, "dhlth") == 0) {
2898 action = ST_ADM_ACTION_DHLTH;
2899 }
2900 else if (strcmp(value, "ehlth") == 0) {
2901 action = ST_ADM_ACTION_EHLTH;
2902 }
2903 else if (strcmp(value, "hrunn") == 0) {
2904 action = ST_ADM_ACTION_HRUNN;
2905 }
2906 else if (strcmp(value, "hnolb") == 0) {
2907 action = ST_ADM_ACTION_HNOLB;
2908 }
2909 else if (strcmp(value, "hdown") == 0) {
2910 action = ST_ADM_ACTION_HDOWN;
2911 }
2912 else if (strcmp(value, "dagent") == 0) {
2913 action = ST_ADM_ACTION_DAGENT;
2914 }
2915 else if (strcmp(value, "eagent") == 0) {
2916 action = ST_ADM_ACTION_EAGENT;
2917 }
2918 else if (strcmp(value, "arunn") == 0) {
2919 action = ST_ADM_ACTION_ARUNN;
2920 }
2921 else if (strcmp(value, "adown") == 0) {
2922 action = ST_ADM_ACTION_ADOWN;
2923 }
2924 /* else these are the old supported methods */
2925 else if (strcmp(value, "disable") == 0) {
2926 action = ST_ADM_ACTION_DISABLE;
2927 }
2928 else if (strcmp(value, "enable") == 0) {
2929 action = ST_ADM_ACTION_ENABLE;
2930 }
2931 else if (strcmp(value, "stop") == 0) {
2932 action = ST_ADM_ACTION_STOP;
2933 }
2934 else if (strcmp(value, "start") == 0) {
2935 action = ST_ADM_ACTION_START;
2936 }
2937 else {
2938 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
2939 goto out;
2940 }
2941 }
2942 else if (strcmp(key, "s") == 0) {
2943 if (!(px && action)) {
2944 /*
2945 * Indicates that we'll need to reprocess the parameters
2946 * as soon as backend and action are known
2947 */
2948 if (!reprocess) {
2949 st_cur_param = cur_param;
2950 st_next_param = next_param;
2951 }
2952 reprocess = 1;
2953 }
2954 else if ((sv = findserver(px, value)) != NULL) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002955 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
William Lallemand74c24fb2016-11-21 17:18:36 +01002956 switch (action) {
2957 case ST_ADM_ACTION_DISABLE:
Emeric Brun52a91d32017-08-31 14:41:55 +02002958 if (!(sv->cur_admin & SRV_ADMF_FMAINT)) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002959 altered_servers++;
2960 total_servers++;
2961 srv_set_admin_flag(sv, SRV_ADMF_FMAINT, "'disable' on stats page");
2962 }
2963 break;
2964 case ST_ADM_ACTION_ENABLE:
Emeric Brun52a91d32017-08-31 14:41:55 +02002965 if (sv->cur_admin & SRV_ADMF_FMAINT) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002966 altered_servers++;
2967 total_servers++;
2968 srv_clr_admin_flag(sv, SRV_ADMF_FMAINT);
2969 }
2970 break;
2971 case ST_ADM_ACTION_STOP:
Emeric Brun52a91d32017-08-31 14:41:55 +02002972 if (!(sv->cur_admin & SRV_ADMF_FDRAIN)) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002973 srv_set_admin_flag(sv, SRV_ADMF_FDRAIN, "'stop' on stats page");
2974 altered_servers++;
2975 total_servers++;
2976 }
2977 break;
2978 case ST_ADM_ACTION_START:
Emeric Brun52a91d32017-08-31 14:41:55 +02002979 if (sv->cur_admin & SRV_ADMF_FDRAIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002980 srv_clr_admin_flag(sv, SRV_ADMF_FDRAIN);
2981 altered_servers++;
2982 total_servers++;
2983 }
2984 break;
2985 case ST_ADM_ACTION_DHLTH:
2986 if (sv->check.state & CHK_ST_CONFIGURED) {
2987 sv->check.state &= ~CHK_ST_ENABLED;
2988 altered_servers++;
2989 total_servers++;
2990 }
2991 break;
2992 case ST_ADM_ACTION_EHLTH:
2993 if (sv->check.state & CHK_ST_CONFIGURED) {
2994 sv->check.state |= CHK_ST_ENABLED;
2995 altered_servers++;
2996 total_servers++;
2997 }
2998 break;
2999 case ST_ADM_ACTION_HRUNN:
3000 if (!(sv->track)) {
3001 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02003002 srv_set_running(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003003 altered_servers++;
3004 total_servers++;
3005 }
3006 break;
3007 case ST_ADM_ACTION_HNOLB:
3008 if (!(sv->track)) {
3009 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02003010 srv_set_stopping(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003011 altered_servers++;
3012 total_servers++;
3013 }
3014 break;
3015 case ST_ADM_ACTION_HDOWN:
3016 if (!(sv->track)) {
3017 sv->check.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02003018 srv_set_stopped(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003019 altered_servers++;
3020 total_servers++;
3021 }
3022 break;
3023 case ST_ADM_ACTION_DAGENT:
3024 if (sv->agent.state & CHK_ST_CONFIGURED) {
3025 sv->agent.state &= ~CHK_ST_ENABLED;
3026 altered_servers++;
3027 total_servers++;
3028 }
3029 break;
3030 case ST_ADM_ACTION_EAGENT:
3031 if (sv->agent.state & CHK_ST_CONFIGURED) {
3032 sv->agent.state |= CHK_ST_ENABLED;
3033 altered_servers++;
3034 total_servers++;
3035 }
3036 break;
3037 case ST_ADM_ACTION_ARUNN:
3038 if (sv->agent.state & CHK_ST_ENABLED) {
3039 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02003040 srv_set_running(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003041 altered_servers++;
3042 total_servers++;
3043 }
3044 break;
3045 case ST_ADM_ACTION_ADOWN:
3046 if (sv->agent.state & CHK_ST_ENABLED) {
3047 sv->agent.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02003048 srv_set_stopped(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003049 altered_servers++;
3050 total_servers++;
3051 }
3052 break;
3053 case ST_ADM_ACTION_READY:
3054 srv_adm_set_ready(sv);
3055 altered_servers++;
3056 total_servers++;
3057 break;
3058 case ST_ADM_ACTION_DRAIN:
3059 srv_adm_set_drain(sv);
3060 altered_servers++;
3061 total_servers++;
3062 break;
3063 case ST_ADM_ACTION_MAINT:
3064 srv_adm_set_maint(sv);
3065 altered_servers++;
3066 total_servers++;
3067 break;
3068 case ST_ADM_ACTION_SHUTDOWN:
3069 if (px->state != PR_STSTOPPED) {
3070 struct stream *sess, *sess_bck;
3071
3072 list_for_each_entry_safe(sess, sess_bck, &sv->actconns, by_srv)
3073 if (sess->srv_conn == sv)
3074 stream_shutdown(sess, SF_ERR_KILLED);
3075
3076 altered_servers++;
3077 total_servers++;
3078 }
3079 break;
3080 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003081 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Lallemand74c24fb2016-11-21 17:18:36 +01003082 } else {
3083 /* the server name is unknown or ambiguous (duplicate names) */
3084 total_servers++;
3085 }
3086 }
3087 if (reprocess && px && action) {
3088 /* Now, we know the backend and the action chosen by the user.
3089 * We can safely restart from the first server parameter
3090 * to reprocess them
3091 */
3092 cur_param = st_cur_param;
3093 next_param = st_next_param;
3094 reprocess = 0;
3095 goto reprocess_servers;
3096 }
3097
3098 next_param = cur_param;
3099 }
3100 }
3101
3102 if (total_servers == 0) {
3103 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
3104 }
3105 else if (altered_servers == 0) {
3106 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
3107 }
3108 else if (altered_servers == total_servers) {
3109 appctx->ctx.stats.st_code = STAT_STATUS_DONE;
3110 }
3111 else {
3112 appctx->ctx.stats.st_code = STAT_STATUS_PART;
3113 }
3114 out:
3115 return 1;
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01003116 wait:
3117 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
3118 return 0;
Christopher Fauletef779222018-10-31 08:47:01 +01003119}
3120
3121
Christopher Fauletb7f88902019-07-15 21:56:43 +02003122static int stats_send_http_headers(struct stream_interface *si, struct htx *htx)
Christopher Fauletef779222018-10-31 08:47:01 +01003123{
3124 struct stream *s = si_strm(si);
3125 struct uri_auth *uri = s->be->uri_auth;
3126 struct appctx *appctx = __objt_appctx(si->end);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003127 struct htx_sl *sl;
3128 unsigned int flags;
Christopher Fauletef779222018-10-31 08:47:01 +01003129
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003130 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);
3131 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist("200"), ist("OK"));
3132 if (!sl)
Christopher Fauletef779222018-10-31 08:47:01 +01003133 goto full;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003134 sl->info.res.status = 200;
Christopher Fauletef779222018-10-31 08:47:01 +01003135
Christopher Fauletb829f4c2019-03-29 16:13:55 +01003136 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")))
Christopher Fauletef779222018-10-31 08:47:01 +01003137 goto full;
3138 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
3139 if (!htx_add_header(htx, ist("Content-Type"), ist("text/html")))
3140 goto full;
3141 }
Christopher Faulet6338a082019-09-09 15:50:54 +02003142 else if (appctx->ctx.stats.flags & (STAT_FMT_JSON|STAT_JSON_SCHM)) {
3143 if (!htx_add_header(htx, ist("Content-Type"), ist("application/json")))
3144 goto full;
3145 }
Christopher Fauletef779222018-10-31 08:47:01 +01003146 else {
3147 if (!htx_add_header(htx, ist("Content-Type"), ist("text/plain")))
3148 goto full;
3149 }
3150
3151 if (uri->refresh > 0 && !(appctx->ctx.stats.flags & STAT_NO_REFRESH)) {
3152 const char *refresh = U2A(uri->refresh);
3153 if (!htx_add_header(htx, ist("Refresh"), ist2(refresh, strlen(refresh))))
3154 goto full;
3155 }
3156
3157 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
3158 if (!htx_add_header(htx, ist("Transfer-Encoding"), ist("chunked")))
3159 goto full;
3160 }
3161
3162 if (!htx_add_endof(htx, HTX_BLK_EOH))
3163 goto full;
3164
Christopher Faulet1e2d6362019-02-27 16:28:48 +01003165 channel_add_input(&s->res, htx->data);
Christopher Fauletef779222018-10-31 08:47:01 +01003166 return 1;
3167
3168 full:
3169 htx_reset(htx);
3170 si_rx_room_blk(si);
3171 return 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01003172}
3173
Christopher Fauletef779222018-10-31 08:47:01 +01003174
Christopher Fauletb7f88902019-07-15 21:56:43 +02003175static int stats_send_http_redirect(struct stream_interface *si, struct htx *htx)
Christopher Fauletef779222018-10-31 08:47:01 +01003176{
3177 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
3178 struct stream *s = si_strm(si);
3179 struct uri_auth *uri = s->be->uri_auth;
3180 struct appctx *appctx = __objt_appctx(si->end);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003181 struct htx_sl *sl;
3182 unsigned int flags;
Christopher Fauletef779222018-10-31 08:47:01 +01003183
3184 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
3185 scope_txt[0] = 0;
3186 if (appctx->ctx.stats.scope_len) {
Christopher Fauleted7a0662019-01-14 11:07:34 +01003187 const char *scope_ptr = stats_scope_ptr(appctx, si);
3188
Christopher Fauletef779222018-10-31 08:47:01 +01003189 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Christopher Fauleted7a0662019-01-14 11:07:34 +01003190 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), scope_ptr, appctx->ctx.stats.scope_len);
Christopher Fauletef779222018-10-31 08:47:01 +01003191 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
3192 }
3193
3194 /* We don't want to land on the posted stats page because a refresh will
3195 * repost the data. We don't want this to happen on accident so we redirect
3196 * the browse to the stats page with a GET.
3197 */
3198 chunk_printf(&trash, "%s;st=%s%s%s%s",
3199 uri->uri_prefix,
3200 ((appctx->ctx.stats.st_code > STAT_STATUS_INIT) &&
3201 (appctx->ctx.stats.st_code < STAT_STATUS_SIZE) &&
3202 stat_status_codes[appctx->ctx.stats.st_code]) ?
3203 stat_status_codes[appctx->ctx.stats.st_code] :
3204 stat_status_codes[STAT_STATUS_UNKN],
3205 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3206 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3207 scope_txt);
3208
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003209 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_CHNK);
3210 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist("303"), ist("See Other"));
3211 if (!sl)
Christopher Fauletef779222018-10-31 08:47:01 +01003212 goto full;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003213 sl->info.res.status = 303;
Christopher Fauletef779222018-10-31 08:47:01 +01003214
3215 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")) ||
Christopher Fauletef779222018-10-31 08:47:01 +01003216 !htx_add_header(htx, ist("Content-Type"), ist("text/plain")) ||
3217 !htx_add_header(htx, ist("Content-Length"), ist("0")) ||
3218 !htx_add_header(htx, ist("Location"), ist2(trash.area, trash.data)))
3219 goto full;
3220
3221 if (!htx_add_endof(htx, HTX_BLK_EOH))
3222 goto full;
3223
Christopher Faulet1e2d6362019-02-27 16:28:48 +01003224 channel_add_input(&s->res, htx->data);
Christopher Fauletef779222018-10-31 08:47:01 +01003225 return 1;
3226
3227full:
3228 htx_reset(htx);
3229 si_rx_room_blk(si);
3230 return 0;
3231}
William Lallemand74c24fb2016-11-21 17:18:36 +01003232
Simon Horman05ee2132017-01-04 09:37:25 +01003233
William Lallemand74c24fb2016-11-21 17:18:36 +01003234/* This I/O handler runs as an applet embedded in a stream interface. It is
3235 * used to send HTTP stats over a TCP socket. The mechanism is very simple.
3236 * appctx->st0 contains the operation in progress (dump, done). The handler
3237 * automatically unregisters itself once transfer is complete.
3238 */
Christopher Fauletb7f88902019-07-15 21:56:43 +02003239static void http_stats_io_handler(struct appctx *appctx)
Christopher Fauletef779222018-10-31 08:47:01 +01003240{
3241 struct stream_interface *si = appctx->owner;
3242 struct stream *s = si_strm(si);
3243 struct channel *req = si_oc(si);
3244 struct channel *res = si_ic(si);
3245 struct htx *req_htx, *res_htx;
3246
3247 res_htx = htx_from_buf(&res->buf);
3248
3249 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
3250 goto out;
3251
3252 /* Check if the input buffer is avalaible. */
3253 if (!b_size(&res->buf)) {
3254 si_rx_room_blk(si);
3255 goto out;
3256 }
3257
3258 /* check that the output is not closed */
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003259 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_SHUTR))
3260 appctx->st0 = STAT_HTTP_END;
Christopher Fauletef779222018-10-31 08:47:01 +01003261
3262 /* all states are processed in sequence */
3263 if (appctx->st0 == STAT_HTTP_HEAD) {
Christopher Fauletb7f88902019-07-15 21:56:43 +02003264 if (stats_send_http_headers(si, res_htx)) {
Christopher Fauletef779222018-10-31 08:47:01 +01003265 if (s->txn->meth == HTTP_METH_HEAD)
3266 appctx->st0 = STAT_HTTP_DONE;
3267 else
3268 appctx->st0 = STAT_HTTP_DUMP;
3269 }
3270 }
3271
3272 if (appctx->st0 == STAT_HTTP_DUMP) {
3273 if (stats_dump_stat_to_buffer(si, res_htx, s->be->uri_auth))
3274 appctx->st0 = STAT_HTTP_DONE;
3275 }
3276
3277 if (appctx->st0 == STAT_HTTP_POST) {
3278 if (stats_process_http_post(si))
3279 appctx->st0 = STAT_HTTP_LAST;
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003280 else if (req->flags & CF_SHUTR)
Christopher Fauletef779222018-10-31 08:47:01 +01003281 appctx->st0 = STAT_HTTP_DONE;
3282 }
3283
3284 if (appctx->st0 == STAT_HTTP_LAST) {
Christopher Fauletb7f88902019-07-15 21:56:43 +02003285 if (stats_send_http_redirect(si, res_htx))
Christopher Fauletef779222018-10-31 08:47:01 +01003286 appctx->st0 = STAT_HTTP_DONE;
3287 }
3288
3289 if (appctx->st0 == STAT_HTTP_DONE) {
Christopher Faulet54b5e212019-06-04 10:08:28 +02003290 /* Don't add TLR because mux-h1 will take care of it */
Christopher Fauletef779222018-10-31 08:47:01 +01003291 if (!htx_add_endof(res_htx, HTX_BLK_EOM)) {
3292 si_rx_room_blk(si);
3293 goto out;
3294 }
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003295 channel_add_input(&s->res, 1);
3296 appctx->st0 = STAT_HTTP_END;
Christopher Fauletef779222018-10-31 08:47:01 +01003297 }
3298
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003299 if (appctx->st0 == STAT_HTTP_END) {
3300 if (!(res->flags & CF_SHUTR)) {
Christopher Fauletef779222018-10-31 08:47:01 +01003301 res->flags |= CF_READ_NULL;
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003302 si_shutr(si);
3303 }
3304
3305 /* eat the whole request */
3306 if (co_data(req)) {
3307 req_htx = htx_from_buf(&req->buf);
3308 co_htx_skip(req, req_htx, co_data(req));
3309 htx_to_buf(req_htx, &req->buf);
Christopher Fauletef779222018-10-31 08:47:01 +01003310 }
3311 }
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003312
Christopher Fauletef779222018-10-31 08:47:01 +01003313 out:
3314 /* we have left the request in the buffer for the case where we
3315 * process a POST, and this automatically re-enables activity on
3316 * read. It's better to indicate that we want to stop reading when
3317 * we're sending, so that we know there's at most one direction
3318 * deciding to wake the applet up. It saves it from looping when
3319 * emitting large blocks into small TCP windows.
3320 */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003321 htx_to_buf(res_htx, &res->buf);
3322 if (!channel_is_empty(res))
Christopher Fauletef779222018-10-31 08:47:01 +01003323 si_stop_get(si);
Christopher Fauletef779222018-10-31 08:47:01 +01003324}
3325
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003326/* Dump all fields from <info> into <out> using the "show info" format (name: value) */
Willy Tarreau83061a82018-07-13 11:56:34 +02003327static int stats_dump_info_fields(struct buffer *out,
3328 const struct field *info)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003329{
3330 int field;
3331
3332 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
3333 if (!field_format(info, field))
3334 continue;
3335
3336 if (!chunk_appendf(out, "%s: ", info_field_names[field]))
3337 return 0;
3338 if (!stats_emit_raw_data_field(out, &info[field]))
3339 return 0;
3340 if (!chunk_strcat(out, "\n"))
3341 return 0;
3342 }
3343 return 1;
3344}
3345
3346/* Dump all fields from <info> into <out> using the "show info typed" format */
Willy Tarreau83061a82018-07-13 11:56:34 +02003347static int stats_dump_typed_info_fields(struct buffer *out,
3348 const struct field *info)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003349{
3350 int field;
3351
3352 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
3353 if (!field_format(info, field))
3354 continue;
3355
3356 if (!chunk_appendf(out, "%d.%s.%u:", field, info_field_names[field], info[INF_PROCESS_NUM].u.u32))
3357 return 0;
3358 if (!stats_emit_field_tags(out, &info[field], ':'))
3359 return 0;
3360 if (!stats_emit_typed_data_field(out, &info[field]))
3361 return 0;
3362 if (!chunk_strcat(out, "\n"))
3363 return 0;
3364 }
3365 return 1;
3366}
3367
3368/* Fill <info> with HAProxy global info. <info> is preallocated
3369 * array of length <len>. The length of the aray must be
3370 * INF_TOTAL_FIELDS. If this length is less then this value, the
3371 * function returns 0, otherwise, it returns 1.
3372 */
3373int stats_fill_info(struct field *info, int len)
3374{
3375 unsigned int up = (now.tv_sec - start_date.tv_sec);
Willy Tarreau83061a82018-07-13 11:56:34 +02003376 struct buffer *out = get_trash_chunk();
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003377
3378#ifdef USE_OPENSSL
3379 int ssl_sess_rate = read_freq_ctr(&global.ssl_per_sec);
3380 int ssl_key_rate = read_freq_ctr(&global.ssl_fe_keys_per_sec);
3381 int ssl_reuse = 0;
3382
3383 if (ssl_key_rate < ssl_sess_rate) {
3384 /* count the ssl reuse ratio and avoid overflows in both directions */
3385 ssl_reuse = 100 - (100 * ssl_key_rate + (ssl_sess_rate - 1) / 2) / ssl_sess_rate;
3386 }
3387#endif
3388
3389 if (len < INF_TOTAL_FIELDS)
3390 return 0;
3391
3392 chunk_reset(out);
3393 memset(info, 0, sizeof(*info) * len);
3394
3395 info[INF_NAME] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, PRODUCT_NAME);
Willy Tarreau909b9d82019-01-04 18:20:32 +01003396 info[INF_VERSION] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, haproxy_version);
3397 info[INF_RELEASE_DATE] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, haproxy_date);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003398
Yves Lafon95317282018-02-26 11:10:37 +01003399 info[INF_NBTHREAD] = mkf_u32(FO_CONFIG|FS_SERVICE, global.nbthread);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003400 info[INF_NBPROC] = mkf_u32(FO_CONFIG|FS_SERVICE, global.nbproc);
3401 info[INF_PROCESS_NUM] = mkf_u32(FO_KEY, relative_pid);
3402 info[INF_PID] = mkf_u32(FO_STATUS, pid);
3403
3404 info[INF_UPTIME] = mkf_str(FN_DURATION, chunk_newstr(out));
3405 chunk_appendf(out, "%ud %uh%02um%02us", up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60));
3406
3407 info[INF_UPTIME_SEC] = mkf_u32(FN_DURATION, up);
3408 info[INF_MEMMAX_MB] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_memmax);
3409 info[INF_POOL_ALLOC_MB] = mkf_u32(0, (unsigned)(pool_total_allocated() / 1048576L));
3410 info[INF_POOL_USED_MB] = mkf_u32(0, (unsigned)(pool_total_used() / 1048576L));
3411 info[INF_POOL_FAILED] = mkf_u32(FN_COUNTER, pool_total_failures());
3412 info[INF_ULIMIT_N] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_nofile);
3413 info[INF_MAXSOCK] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxsock);
3414 info[INF_MAXCONN] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxconn);
3415 info[INF_HARD_MAXCONN] = mkf_u32(FO_CONFIG|FN_LIMIT, global.hardmaxconn);
3416 info[INF_CURR_CONN] = mkf_u32(0, actconn);
3417 info[INF_CUM_CONN] = mkf_u32(FN_COUNTER, totalconn);
3418 info[INF_CUM_REQ] = mkf_u32(FN_COUNTER, global.req_count);
3419#ifdef USE_OPENSSL
3420 info[INF_MAX_SSL_CONNS] = mkf_u32(FN_MAX, global.maxsslconn);
3421 info[INF_CURR_SSL_CONNS] = mkf_u32(0, sslconns);
3422 info[INF_CUM_SSL_CONNS] = mkf_u32(FN_COUNTER, totalsslconns);
3423#endif
3424 info[INF_MAXPIPES] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxpipes);
3425 info[INF_PIPES_USED] = mkf_u32(0, pipes_used);
3426 info[INF_PIPES_FREE] = mkf_u32(0, pipes_free);
3427 info[INF_CONN_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.conn_per_sec));
3428 info[INF_CONN_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.cps_lim);
3429 info[INF_MAX_CONN_RATE] = mkf_u32(FN_MAX, global.cps_max);
3430 info[INF_SESS_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.sess_per_sec));
3431 info[INF_SESS_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.sps_lim);
3432 info[INF_MAX_SESS_RATE] = mkf_u32(FN_RATE, global.sps_max);
3433
3434#ifdef USE_OPENSSL
3435 info[INF_SSL_RATE] = mkf_u32(FN_RATE, ssl_sess_rate);
3436 info[INF_SSL_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.ssl_lim);
3437 info[INF_MAX_SSL_RATE] = mkf_u32(FN_MAX, global.ssl_max);
3438 info[INF_SSL_FRONTEND_KEY_RATE] = mkf_u32(0, ssl_key_rate);
3439 info[INF_SSL_FRONTEND_MAX_KEY_RATE] = mkf_u32(FN_MAX, global.ssl_fe_keys_max);
3440 info[INF_SSL_FRONTEND_SESSION_REUSE_PCT] = mkf_u32(0, ssl_reuse);
3441 info[INF_SSL_BACKEND_KEY_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.ssl_be_keys_per_sec));
3442 info[INF_SSL_BACKEND_MAX_KEY_RATE] = mkf_u32(FN_MAX, global.ssl_be_keys_max);
3443 info[INF_SSL_CACHE_LOOKUPS] = mkf_u32(FN_COUNTER, global.shctx_lookups);
3444 info[INF_SSL_CACHE_MISSES] = mkf_u32(FN_COUNTER, global.shctx_misses);
3445#endif
3446 info[INF_COMPRESS_BPS_IN] = mkf_u32(FN_RATE, read_freq_ctr(&global.comp_bps_in));
3447 info[INF_COMPRESS_BPS_OUT] = mkf_u32(FN_RATE, read_freq_ctr(&global.comp_bps_out));
3448 info[INF_COMPRESS_BPS_RATE_LIM] = mkf_u32(FO_CONFIG|FN_LIMIT, global.comp_rate_lim);
3449#ifdef USE_ZLIB
3450 info[INF_ZLIB_MEM_USAGE] = mkf_u32(0, zlib_used_memory);
3451 info[INF_MAX_ZLIB_MEM_USAGE] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxzlibmem);
3452#endif
3453 info[INF_TASKS] = mkf_u32(0, nb_tasks_cur);
Christopher Faulet34c5cc92016-12-06 09:15:30 +01003454 info[INF_RUN_QUEUE] = mkf_u32(0, tasks_run_queue_cur);
Willy Tarreau81036f22019-05-20 19:24:50 +02003455 info[INF_IDLE_PCT] = mkf_u32(FN_AVG, ti->idle_pct);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003456 info[INF_NODE] = mkf_str(FO_CONFIG|FN_OUTPUT|FS_SERVICE, global.node);
3457 if (global.desc)
3458 info[INF_DESCRIPTION] = mkf_str(FO_CONFIG|FN_OUTPUT|FS_SERVICE, global.desc);
Willy Tarreau00098ea2018-11-05 14:38:13 +01003459 info[INF_STOPPING] = mkf_u32(0, stopping);
3460 info[INF_JOBS] = mkf_u32(0, jobs);
William Lallemanda7199262018-11-16 16:57:20 +01003461 info[INF_UNSTOPPABLE_JOBS] = mkf_u32(0, unstoppable_jobs);
Willy Tarreau00098ea2018-11-05 14:38:13 +01003462 info[INF_LISTENERS] = mkf_u32(0, listeners);
Willy Tarreau199ad242018-11-05 16:31:22 +01003463 info[INF_ACTIVE_PEERS] = mkf_u32(0, active_peers);
Willy Tarreau2d372c22018-11-05 17:12:27 +01003464 info[INF_CONNECTED_PEERS] = mkf_u32(0, connected_peers);
Willy Tarreau13ef7732018-11-12 07:25:28 +01003465 info[INF_DROPPED_LOGS] = mkf_u32(0, dropped_logs);
Willy Tarreaubeb859a2018-11-22 18:07:59 +01003466 info[INF_BUSY_POLLING] = mkf_u32(0, !!(global.tune.options & GTUNE_BUSY_POLLING));
Baptiste Assmann333939c2019-01-21 08:34:50 +01003467 info[INF_FAILED_RESOLUTIONS] = mkf_u32(0, dns_failed_resolutions);
Willy Tarreau7cf0e452019-05-23 11:39:14 +02003468 info[INF_TOTAL_BYTES_OUT] = mkf_u64(0, global.out_bytes);
3469 info[INF_BYTES_OUT_RATE] = mkf_u64(FN_RATE, (unsigned long long)read_freq_ctr(&global.out_32bps) * 32);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003470
3471 return 1;
3472}
3473
3474/* This function dumps information onto the stream interface's read buffer.
3475 * It returns 0 as long as it does not complete, non-zero upon completion.
3476 * No state is used.
3477 */
3478static int stats_dump_info_to_buffer(struct stream_interface *si)
3479{
3480 struct appctx *appctx = __objt_appctx(si->end);
3481
3482 if (!stats_fill_info(info, INF_TOTAL_FIELDS))
3483 return 0;
3484
3485 chunk_reset(&trash);
3486
3487 if (appctx->ctx.stats.flags & STAT_FMT_TYPED)
3488 stats_dump_typed_info_fields(&trash, info);
Simon Horman05ee2132017-01-04 09:37:25 +01003489 else if (appctx->ctx.stats.flags & STAT_FMT_JSON)
3490 stats_dump_json_info_fields(&trash, info);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003491 else
3492 stats_dump_info_fields(&trash, info);
3493
Willy Tarreau06d80a92017-10-19 14:32:15 +02003494 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01003495 si_rx_room_blk(si);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003496 return 0;
3497 }
3498
3499 return 1;
3500}
3501
Simon Horman6f6bb382017-01-04 09:37:26 +01003502/* This function dumps the schema onto the stream interface's read buffer.
3503 * It returns 0 as long as it does not complete, non-zero upon completion.
3504 * No state is used.
3505 *
3506 * Integer values bouned to the range [-(2**53)+1, (2**53)-1] as
3507 * per the recommendation for interoperable integers in section 6 of RFC 7159.
3508 */
Willy Tarreau83061a82018-07-13 11:56:34 +02003509static void stats_dump_json_schema(struct buffer *out)
Simon Horman6f6bb382017-01-04 09:37:26 +01003510{
3511
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003512 int old_len = out->data;
Simon Horman6f6bb382017-01-04 09:37:26 +01003513
3514 chunk_strcat(out,
3515 "{"
3516 "\"$schema\":\"http://json-schema.org/draft-04/schema#\","
3517 "\"oneOf\":["
3518 "{"
3519 "\"title\":\"Info\","
3520 "\"type\":\"array\","
3521 "\"items\":{"
3522 "\"properties\":{"
3523 "\"title\":\"InfoItem\","
3524 "\"type\":\"object\","
3525 "\"field\":{\"$ref\":\"#/definitions/field\"},"
3526 "\"processNum\":{\"$ref\":\"#/definitions/processNum\"},"
3527 "\"tags\":{\"$ref\":\"#/definitions/tags\"},"
3528 "\"value\":{\"$ref\":\"#/definitions/typedValue\"}"
3529 "},"
3530 "\"required\":[\"field\",\"processNum\",\"tags\","
3531 "\"value\"]"
3532 "}"
3533 "},"
3534 "{"
3535 "\"title\":\"Stat\","
3536 "\"type\":\"array\","
3537 "\"items\":{"
3538 "\"title\":\"InfoItem\","
3539 "\"type\":\"object\","
3540 "\"properties\":{"
3541 "\"objType\":{"
3542 "\"enum\":[\"Frontend\",\"Backend\",\"Listener\","
3543 "\"Server\",\"Unknown\"]"
3544 "},"
3545 "\"proxyId\":{"
3546 "\"type\":\"integer\","
3547 "\"minimum\":0"
3548 "},"
3549 "\"id\":{"
3550 "\"type\":\"integer\","
3551 "\"minimum\":0"
3552 "},"
3553 "\"field\":{\"$ref\":\"#/definitions/field\"},"
3554 "\"processNum\":{\"$ref\":\"#/definitions/processNum\"},"
3555 "\"tags\":{\"$ref\":\"#/definitions/tags\"},"
3556 "\"typedValue\":{\"$ref\":\"#/definitions/typedValue\"}"
3557 "},"
3558 "\"required\":[\"objType\",\"proxyId\",\"id\","
3559 "\"field\",\"processNum\",\"tags\","
3560 "\"value\"]"
3561 "}"
3562 "},"
3563 "{"
3564 "\"title\":\"Error\","
3565 "\"type\":\"object\","
3566 "\"properties\":{"
3567 "\"errorStr\":{"
3568 "\"type\":\"string\""
3569 "},"
3570 "\"required\":[\"errorStr\"]"
3571 "}"
3572 "}"
3573 "],"
3574 "\"definitions\":{"
3575 "\"field\":{"
3576 "\"type\":\"object\","
3577 "\"pos\":{"
3578 "\"type\":\"integer\","
3579 "\"minimum\":0"
3580 "},"
3581 "\"name\":{"
3582 "\"type\":\"string\""
3583 "},"
3584 "\"required\":[\"pos\",\"name\"]"
3585 "},"
3586 "\"processNum\":{"
3587 "\"type\":\"integer\","
3588 "\"minimum\":1"
3589 "},"
3590 "\"tags\":{"
3591 "\"type\":\"object\","
3592 "\"origin\":{"
3593 "\"type\":\"string\","
3594 "\"enum\":[\"Metric\",\"Status\",\"Key\","
3595 "\"Config\",\"Product\",\"Unknown\"]"
3596 "},"
3597 "\"nature\":{"
3598 "\"type\":\"string\","
3599 "\"enum\":[\"Gauge\",\"Limit\",\"Min\",\"Max\","
3600 "\"Rate\",\"Counter\",\"Duration\","
3601 "\"Age\",\"Time\",\"Name\",\"Output\","
3602 "\"Avg\", \"Unknown\"]"
3603 "},"
3604 "\"scope\":{"
3605 "\"type\":\"string\","
3606 "\"enum\":[\"Cluster\",\"Process\",\"Service\","
3607 "\"System\",\"Unknown\"]"
3608 "},"
3609 "\"required\":[\"origin\",\"nature\",\"scope\"]"
3610 "},"
3611 "\"typedValue\":{"
3612 "\"type\":\"object\","
3613 "\"oneOf\":["
3614 "{\"$ref\":\"#/definitions/typedValue/definitions/s32Value\"},"
3615 "{\"$ref\":\"#/definitions/typedValue/definitions/s64Value\"},"
3616 "{\"$ref\":\"#/definitions/typedValue/definitions/u32Value\"},"
3617 "{\"$ref\":\"#/definitions/typedValue/definitions/u64Value\"},"
3618 "{\"$ref\":\"#/definitions/typedValue/definitions/strValue\"}"
3619 "],"
3620 "\"definitions\":{"
3621 "\"s32Value\":{"
3622 "\"properties\":{"
3623 "\"type\":{"
3624 "\"type\":\"string\","
3625 "\"enum\":[\"s32\"]"
3626 "},"
3627 "\"value\":{"
3628 "\"type\":\"integer\","
3629 "\"minimum\":-2147483648,"
3630 "\"maximum\":2147483647"
3631 "}"
3632 "},"
3633 "\"required\":[\"type\",\"value\"]"
3634 "},"
3635 "\"s64Value\":{"
3636 "\"properties\":{"
3637 "\"type\":{"
3638 "\"type\":\"string\","
3639 "\"enum\":[\"s64\"]"
3640 "},"
3641 "\"value\":{"
3642 "\"type\":\"integer\","
3643 "\"minimum\":-9007199254740991,"
3644 "\"maximum\":9007199254740991"
3645 "}"
3646 "},"
3647 "\"required\":[\"type\",\"value\"]"
3648 "},"
3649 "\"u32Value\":{"
3650 "\"properties\":{"
3651 "\"type\":{"
3652 "\"type\":\"string\","
3653 "\"enum\":[\"u32\"]"
3654 "},"
3655 "\"value\":{"
3656 "\"type\":\"integer\","
3657 "\"minimum\":0,"
3658 "\"maximum\":4294967295"
3659 "}"
3660 "},"
3661 "\"required\":[\"type\",\"value\"]"
3662 "},"
3663 "\"u64Value\":{"
3664 "\"properties\":{"
3665 "\"type\":{"
3666 "\"type\":\"string\","
3667 "\"enum\":[\"u64\"]"
3668 "},"
3669 "\"value\":{"
3670 "\"type\":\"integer\","
3671 "\"minimum\":0,"
3672 "\"maximum\":9007199254740991"
3673 "}"
3674 "},"
3675 "\"required\":[\"type\",\"value\"]"
3676 "},"
3677 "\"strValue\":{"
3678 "\"properties\":{"
3679 "\"type\":{"
3680 "\"type\":\"string\","
3681 "\"enum\":[\"str\"]"
3682 "},"
3683 "\"value\":{\"type\":\"string\"}"
3684 "},"
3685 "\"required\":[\"type\",\"value\"]"
3686 "},"
3687 "\"unknownValue\":{"
3688 "\"properties\":{"
3689 "\"type\":{"
3690 "\"type\":\"integer\","
3691 "\"minimum\":0"
3692 "},"
3693 "\"value\":{"
3694 "\"type\":\"string\","
3695 "\"enum\":[\"unknown\"]"
3696 "}"
3697 "},"
3698 "\"required\":[\"type\",\"value\"]"
3699 "}"
3700 "}"
3701 "}"
3702 "}"
3703 "}");
3704
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003705 if (old_len == out->data) {
Simon Horman6f6bb382017-01-04 09:37:26 +01003706 chunk_reset(out);
3707 chunk_appendf(out,
3708 "{\"errorStr\":\"output buffer too short\"}");
3709 }
3710}
3711
3712/* This function dumps the schema onto the stream interface's read buffer.
3713 * It returns 0 as long as it does not complete, non-zero upon completion.
3714 * No state is used.
3715 */
3716static int stats_dump_json_schema_to_buffer(struct stream_interface *si)
3717{
3718 chunk_reset(&trash);
3719
3720 stats_dump_json_schema(&trash);
3721
Willy Tarreau06d80a92017-10-19 14:32:15 +02003722 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01003723 si_rx_room_blk(si);
Simon Horman6f6bb382017-01-04 09:37:26 +01003724 return 0;
3725 }
3726
3727 return 1;
3728}
3729
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02003730static int cli_parse_clear_counters(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau89d467c2016-11-23 11:02:40 +01003731{
3732 struct proxy *px;
3733 struct server *sv;
3734 struct listener *li;
3735 int clrall = 0;
3736
3737 if (strcmp(args[2], "all") == 0)
3738 clrall = 1;
3739
3740 /* check permissions */
3741 if (!cli_has_level(appctx, ACCESS_LVL_OPER) ||
3742 (clrall && !cli_has_level(appctx, ACCESS_LVL_ADMIN)))
3743 return 1;
3744
Olivier Houchardfbc74e82017-11-24 16:54:05 +01003745 for (px = proxies_list; px; px = px->next) {
Willy Tarreau89d467c2016-11-23 11:02:40 +01003746 if (clrall) {
3747 memset(&px->be_counters, 0, sizeof(px->be_counters));
3748 memset(&px->fe_counters, 0, sizeof(px->fe_counters));
3749 }
3750 else {
3751 px->be_counters.conn_max = 0;
3752 px->be_counters.p.http.rps_max = 0;
3753 px->be_counters.sps_max = 0;
3754 px->be_counters.cps_max = 0;
3755 px->be_counters.nbpend_max = 0;
3756
3757 px->fe_counters.conn_max = 0;
3758 px->fe_counters.p.http.rps_max = 0;
3759 px->fe_counters.sps_max = 0;
3760 px->fe_counters.cps_max = 0;
Willy Tarreau89d467c2016-11-23 11:02:40 +01003761 }
3762
3763 for (sv = px->srv; sv; sv = sv->next)
3764 if (clrall)
3765 memset(&sv->counters, 0, sizeof(sv->counters));
3766 else {
3767 sv->counters.cur_sess_max = 0;
3768 sv->counters.nbpend_max = 0;
3769 sv->counters.sps_max = 0;
3770 }
3771
3772 list_for_each_entry(li, &px->conf.listeners, by_fe)
3773 if (li->counters) {
3774 if (clrall)
3775 memset(li->counters, 0, sizeof(*li->counters));
3776 else
3777 li->counters->conn_max = 0;
3778 }
3779 }
3780
3781 global.cps_max = 0;
3782 global.sps_max = 0;
Olivier Houchard00bc3cb2017-10-17 19:23:25 +02003783 global.ssl_max = 0;
3784 global.ssl_fe_keys_max = 0;
3785 global.ssl_be_keys_max = 0;
Willy Tarreaud80cb4e2018-01-20 19:30:13 +01003786
3787 memset(activity, 0, sizeof(activity));
Willy Tarreau89d467c2016-11-23 11:02:40 +01003788 return 1;
3789}
3790
3791
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02003792static int cli_parse_show_info(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003793{
Willy Tarreaud25fc792016-12-16 12:33:47 +01003794 appctx->ctx.stats.scope_str = 0;
3795 appctx->ctx.stats.scope_len = 0;
3796 appctx->ctx.stats.flags = 0;
3797
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003798 if (strcmp(args[2], "typed") == 0)
3799 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
Simon Horman05ee2132017-01-04 09:37:25 +01003800 else if (strcmp(args[2], "json") == 0)
3801 appctx->ctx.stats.flags |= STAT_FMT_JSON;
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003802 return 0;
3803}
3804
3805
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02003806static int cli_parse_show_stat(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau2b812e22016-11-22 16:18:05 +01003807{
Willy Tarreaud25fc792016-12-16 12:33:47 +01003808 appctx->ctx.stats.scope_str = 0;
3809 appctx->ctx.stats.scope_len = 0;
3810 appctx->ctx.stats.flags = 0;
3811
Willy Tarreau2b812e22016-11-22 16:18:05 +01003812 if (*args[2] && *args[3] && *args[4]) {
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01003813 struct proxy *px;
3814
3815 px = proxy_find_by_name(args[2], 0, 0);
3816 if (px)
3817 appctx->ctx.stats.iid = px->uuid;
3818 else
3819 appctx->ctx.stats.iid = atoi(args[2]);
3820
Willy Tarreau9d008692019-08-09 11:21:01 +02003821 if (!appctx->ctx.stats.iid)
3822 return cli_err(appctx, "No such proxy.\n");
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01003823
Willy Tarreau2b812e22016-11-22 16:18:05 +01003824 appctx->ctx.stats.flags |= STAT_BOUND;
Willy Tarreau2b812e22016-11-22 16:18:05 +01003825 appctx->ctx.stats.type = atoi(args[3]);
3826 appctx->ctx.stats.sid = atoi(args[4]);
3827 if (strcmp(args[5], "typed") == 0)
3828 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
Simon Horman05ee2132017-01-04 09:37:25 +01003829 else if (strcmp(args[5], "json") == 0)
3830 appctx->ctx.stats.flags |= STAT_FMT_JSON;
Willy Tarreau2b812e22016-11-22 16:18:05 +01003831 }
3832 else if (strcmp(args[2], "typed") == 0)
3833 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
Simon Horman05ee2132017-01-04 09:37:25 +01003834 else if (strcmp(args[2], "json") == 0)
3835 appctx->ctx.stats.flags |= STAT_FMT_JSON;
Willy Tarreau2b812e22016-11-22 16:18:05 +01003836
Willy Tarreau2b812e22016-11-22 16:18:05 +01003837 return 0;
3838}
3839
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003840static int cli_io_handler_dump_info(struct appctx *appctx)
3841{
3842 return stats_dump_info_to_buffer(appctx->owner);
3843}
3844
Willy Tarreau2b812e22016-11-22 16:18:05 +01003845/* This I/O handler runs as an applet embedded in a stream interface. It is
3846 * used to send raw stats over a socket.
3847 */
3848static int cli_io_handler_dump_stat(struct appctx *appctx)
3849{
Christopher Fauletef779222018-10-31 08:47:01 +01003850 return stats_dump_stat_to_buffer(appctx->owner, NULL, NULL);
Willy Tarreau2b812e22016-11-22 16:18:05 +01003851}
3852
Simon Horman6f6bb382017-01-04 09:37:26 +01003853static int cli_io_handler_dump_json_schema(struct appctx *appctx)
3854{
3855 return stats_dump_json_schema_to_buffer(appctx->owner);
3856}
3857
Willy Tarreau2b812e22016-11-22 16:18:05 +01003858/* register cli keywords */
3859static struct cli_kw_list cli_kws = {{ },{
Willy Tarreau89d467c2016-11-23 11:02:40 +01003860 { { "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 +01003861 { { "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 +01003862 { { "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 +01003863 { { "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 +01003864 {{},}
3865}};
3866
Willy Tarreau0108d902018-11-25 19:14:37 +01003867INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
3868
William Lallemand74c24fb2016-11-21 17:18:36 +01003869struct applet http_stats_applet = {
3870 .obj_type = OBJ_TYPE_APPLET,
3871 .name = "<STATS>", /* used for logging */
3872 .fct = http_stats_io_handler,
3873 .release = NULL,
3874};
3875
William Lallemand74c24fb2016-11-21 17:18:36 +01003876/*
3877 * Local variables:
3878 * c-indent-level: 8
3879 * c-basic-offset: 8
3880 * End:
3881 */