blob: 4c51a868bdd6b32d7ebf0f3854afd59ea0c861d4 [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);
Christopher Faulet88a0db22019-09-24 16:35:10 +0200336 case FF_FLT: return chunk_appendf(out, "%f", f->u.flt);
William Lallemand74c24fb2016-11-21 17:18:36 +0100337 case FF_STR: return csv_enc_append(field_str(f, 0), 1, out) != NULL;
338 default: return chunk_appendf(out, "[INCORRECT_FIELD_TYPE_%08x]", f->type);
339 }
340}
341
342/* Emits a stats field prefixed with its type. No CSV encoding is prepared, the
343 * output is supposed to be used on its own line. Returns non-zero on success, 0
344 * if the buffer is full.
345 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200346int stats_emit_typed_data_field(struct buffer *out, const struct field *f)
William Lallemand74c24fb2016-11-21 17:18:36 +0100347{
348 switch (field_format(f, 0)) {
349 case FF_EMPTY: return 1;
350 case FF_S32: return chunk_appendf(out, "s32:%d", f->u.s32);
351 case FF_U32: return chunk_appendf(out, "u32:%u", f->u.u32);
352 case FF_S64: return chunk_appendf(out, "s64:%lld", (long long)f->u.s64);
353 case FF_U64: return chunk_appendf(out, "u64:%llu", (unsigned long long)f->u.u64);
Christopher Faulet88a0db22019-09-24 16:35:10 +0200354 case FF_FLT: return chunk_appendf(out, "flt:%f", f->u.flt);
William Lallemand74c24fb2016-11-21 17:18:36 +0100355 case FF_STR: return chunk_appendf(out, "str:%s", field_str(f, 0));
356 default: return chunk_appendf(out, "%08x:?", f->type);
357 }
358}
359
Simon Horman05ee2132017-01-04 09:37:25 +0100360/* Limit JSON integer values to the range [-(2**53)+1, (2**53)-1] as per
361 * the recommendation for interoperable integers in section 6 of RFC 7159.
362 */
363#define JSON_INT_MAX ((1ULL << 53) - 1)
364#define JSON_INT_MIN (0 - JSON_INT_MAX)
365
366/* Emits a stats field value and its type in JSON.
367 * Returns non-zero on success, 0 on error.
368 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200369int stats_emit_json_data_field(struct buffer *out, const struct field *f)
Simon Horman05ee2132017-01-04 09:37:25 +0100370{
371 int old_len;
372 char buf[20];
373 const char *type, *value = buf, *quote = "";
374
375 switch (field_format(f, 0)) {
376 case FF_EMPTY: return 1;
377 case FF_S32: type = "\"s32\"";
378 snprintf(buf, sizeof(buf), "%d", f->u.s32);
379 break;
380 case FF_U32: type = "\"u32\"";
381 snprintf(buf, sizeof(buf), "%u", f->u.u32);
382 break;
383 case FF_S64: type = "\"s64\"";
384 if (f->u.s64 < JSON_INT_MIN || f->u.s64 > JSON_INT_MAX)
385 return 0;
386 type = "\"s64\"";
387 snprintf(buf, sizeof(buf), "%lld", (long long)f->u.s64);
388 break;
389 case FF_U64: if (f->u.u64 > JSON_INT_MAX)
390 return 0;
391 type = "\"u64\"";
392 snprintf(buf, sizeof(buf), "%llu",
393 (unsigned long long) f->u.u64);
Christopher Faulet52c91bb2019-09-28 10:37:31 +0200394 break;
Christopher Faulet88a0db22019-09-24 16:35:10 +0200395 case FF_FLT: type = "\"flt\"";
396 snprintf(buf, sizeof(buf), "%f", f->u.flt);
Simon Horman05ee2132017-01-04 09:37:25 +0100397 break;
398 case FF_STR: type = "\"str\"";
399 value = field_str(f, 0);
400 quote = "\"";
401 break;
402 default: snprintf(buf, sizeof(buf), "%u", f->type);
403 type = buf;
404 value = "unknown";
405 quote = "\"";
406 break;
407 }
408
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200409 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100410 chunk_appendf(out, ",\"value\":{\"type\":%s,\"value\":%s%s%s}",
411 type, quote, value, quote);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200412 return !(old_len == out->data);
Simon Horman05ee2132017-01-04 09:37:25 +0100413}
414
William Lallemand74c24fb2016-11-21 17:18:36 +0100415/* Emits an encoding of the field type on 3 characters followed by a delimiter.
416 * Returns non-zero on success, 0 if the buffer is full.
417 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200418int stats_emit_field_tags(struct buffer *out, const struct field *f,
419 char delim)
William Lallemand74c24fb2016-11-21 17:18:36 +0100420{
421 char origin, nature, scope;
422
423 switch (field_origin(f, 0)) {
424 case FO_METRIC: origin = 'M'; break;
425 case FO_STATUS: origin = 'S'; break;
426 case FO_KEY: origin = 'K'; break;
427 case FO_CONFIG: origin = 'C'; break;
428 case FO_PRODUCT: origin = 'P'; break;
429 default: origin = '?'; break;
430 }
431
432 switch (field_nature(f, 0)) {
433 case FN_GAUGE: nature = 'G'; break;
434 case FN_LIMIT: nature = 'L'; break;
435 case FN_MIN: nature = 'm'; break;
436 case FN_MAX: nature = 'M'; break;
437 case FN_RATE: nature = 'R'; break;
438 case FN_COUNTER: nature = 'C'; break;
439 case FN_DURATION: nature = 'D'; break;
440 case FN_AGE: nature = 'A'; break;
441 case FN_TIME: nature = 'T'; break;
442 case FN_NAME: nature = 'N'; break;
443 case FN_OUTPUT: nature = 'O'; break;
444 case FN_AVG: nature = 'a'; break;
445 default: nature = '?'; break;
446 }
447
448 switch (field_scope(f, 0)) {
449 case FS_PROCESS: scope = 'P'; break;
450 case FS_SERVICE: scope = 'S'; break;
451 case FS_SYSTEM: scope = 's'; break;
452 case FS_CLUSTER: scope = 'C'; break;
453 default: scope = '?'; break;
454 }
455
456 return chunk_appendf(out, "%c%c%c%c", origin, nature, scope, delim);
457}
458
Simon Horman05ee2132017-01-04 09:37:25 +0100459/* Emits an encoding of the field type as JSON.
460 * Returns non-zero on success, 0 if the buffer is full.
461 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200462int stats_emit_json_field_tags(struct buffer *out, const struct field *f)
Simon Horman05ee2132017-01-04 09:37:25 +0100463{
464 const char *origin, *nature, *scope;
465 int old_len;
466
467 switch (field_origin(f, 0)) {
468 case FO_METRIC: origin = "Metric"; break;
469 case FO_STATUS: origin = "Status"; break;
470 case FO_KEY: origin = "Key"; break;
471 case FO_CONFIG: origin = "Config"; break;
472 case FO_PRODUCT: origin = "Product"; break;
473 default: origin = "Unknown"; break;
474 }
475
476 switch (field_nature(f, 0)) {
477 case FN_GAUGE: nature = "Gauge"; break;
478 case FN_LIMIT: nature = "Limit"; break;
479 case FN_MIN: nature = "Min"; break;
480 case FN_MAX: nature = "Max"; break;
481 case FN_RATE: nature = "Rate"; break;
482 case FN_COUNTER: nature = "Counter"; break;
483 case FN_DURATION: nature = "Duration"; break;
484 case FN_AGE: nature = "Age"; break;
485 case FN_TIME: nature = "Time"; break;
486 case FN_NAME: nature = "Name"; break;
487 case FN_OUTPUT: nature = "Output"; break;
488 case FN_AVG: nature = "Avg"; break;
489 default: nature = "Unknown"; break;
490 }
491
492 switch (field_scope(f, 0)) {
493 case FS_PROCESS: scope = "Process"; break;
494 case FS_SERVICE: scope = "Service"; break;
495 case FS_SYSTEM: scope = "System"; break;
496 case FS_CLUSTER: scope = "Cluster"; break;
497 default: scope = "Unknown"; break;
498 }
499
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200500 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100501 chunk_appendf(out, "\"tags\":{"
502 "\"origin\":\"%s\","
503 "\"nature\":\"%s\","
504 "\"scope\":\"%s\""
505 "}", origin, nature, scope);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200506 return !(old_len == out->data);
Simon Horman05ee2132017-01-04 09:37:25 +0100507}
William Lallemand74c24fb2016-11-21 17:18:36 +0100508
509/* Dump all fields from <stats> into <out> using CSV format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200510static int stats_dump_fields_csv(struct buffer *out,
Willy Tarreau43241ff2019-10-09 11:27:51 +0200511 const struct field *stats, unsigned int flags)
William Lallemand74c24fb2016-11-21 17:18:36 +0100512{
513 int field;
514
515 for (field = 0; field < ST_F_TOTAL_FIELDS; field++) {
516 if (!stats_emit_raw_data_field(out, &stats[field]))
517 return 0;
518 if (!chunk_strcat(out, ","))
519 return 0;
520 }
521 chunk_strcat(out, "\n");
522 return 1;
523}
524
525/* Dump all fields from <stats> into <out> using a typed "field:desc:type:value" format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200526static int stats_dump_fields_typed(struct buffer *out,
Willy Tarreau43241ff2019-10-09 11:27:51 +0200527 const struct field *stats, unsigned int flags)
William Lallemand74c24fb2016-11-21 17:18:36 +0100528{
529 int field;
530
531 for (field = 0; field < ST_F_TOTAL_FIELDS; field++) {
532 if (!stats[field].type)
533 continue;
534
535 chunk_appendf(out, "%c.%u.%u.%d.%s.%u:",
536 stats[ST_F_TYPE].u.u32 == STATS_TYPE_FE ? 'F' :
537 stats[ST_F_TYPE].u.u32 == STATS_TYPE_BE ? 'B' :
538 stats[ST_F_TYPE].u.u32 == STATS_TYPE_SO ? 'L' :
539 stats[ST_F_TYPE].u.u32 == STATS_TYPE_SV ? 'S' :
540 '?',
541 stats[ST_F_IID].u.u32, stats[ST_F_SID].u.u32,
542 field, stat_field_names[field], stats[ST_F_PID].u.u32);
543
544 if (!stats_emit_field_tags(out, &stats[field], ':'))
545 return 0;
546 if (!stats_emit_typed_data_field(out, &stats[field]))
547 return 0;
548 if (!chunk_strcat(out, "\n"))
549 return 0;
550 }
551 return 1;
552}
553
Simon Horman05ee2132017-01-04 09:37:25 +0100554/* Dump all fields from <stats> into <out> using the "show info json" format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200555static int stats_dump_json_info_fields(struct buffer *out,
Willy Tarreau43241ff2019-10-09 11:27:51 +0200556 const struct field *info, unsigned int flags)
Simon Horman05ee2132017-01-04 09:37:25 +0100557{
558 int field;
559 int started = 0;
560
561 if (!chunk_strcat(out, "["))
562 return 0;
563
564 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
565 int old_len;
566
567 if (!field_format(info, field))
568 continue;
569
570 if (started && !chunk_strcat(out, ","))
571 goto err;
572 started = 1;
573
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200574 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100575 chunk_appendf(out,
576 "{\"field\":{\"pos\":%d,\"name\":\"%s\"},"
577 "\"processNum\":%u,",
578 field, info_field_names[field],
579 info[INF_PROCESS_NUM].u.u32);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200580 if (old_len == out->data)
Simon Horman05ee2132017-01-04 09:37:25 +0100581 goto err;
582
583 if (!stats_emit_json_field_tags(out, &info[field]))
584 goto err;
585
586 if (!stats_emit_json_data_field(out, &info[field]))
587 goto err;
588
589 if (!chunk_strcat(out, "}"))
590 goto err;
591 }
592
593 if (!chunk_strcat(out, "]"))
594 goto err;
595 return 1;
596
597err:
598 chunk_reset(out);
599 chunk_appendf(out, "{\"errorStr\":\"output buffer too short\"}");
600 return 0;
601}
602
603/* Dump all fields from <stats> into <out> using a typed "field:desc:type:value" format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200604static int stats_dump_fields_json(struct buffer *out,
605 const struct field *stats,
Willy Tarreaub0ce3ad2019-10-09 11:19:29 +0200606 unsigned int flags)
Simon Horman05ee2132017-01-04 09:37:25 +0100607{
608 int field;
609 int started = 0;
610
Willy Tarreaub0ce3ad2019-10-09 11:19:29 +0200611 if ((flags & STAT_STARTED) && !chunk_strcat(out, ","))
Simon Horman05ee2132017-01-04 09:37:25 +0100612 return 0;
613 if (!chunk_strcat(out, "["))
614 return 0;
615
616 for (field = 0; field < ST_F_TOTAL_FIELDS; field++) {
617 const char *obj_type;
618 int old_len;
619
620 if (!stats[field].type)
621 continue;
622
623 if (started && !chunk_strcat(out, ","))
624 goto err;
625 started = 1;
626
627 switch (stats[ST_F_TYPE].u.u32) {
628 case STATS_TYPE_FE: obj_type = "Frontend"; break;
629 case STATS_TYPE_BE: obj_type = "Backend"; break;
630 case STATS_TYPE_SO: obj_type = "Listener"; break;
631 case STATS_TYPE_SV: obj_type = "Server"; break;
632 default: obj_type = "Unknown"; break;
633 }
634
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200635 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100636 chunk_appendf(out,
637 "{"
638 "\"objType\":\"%s\","
639 "\"proxyId\":%d,"
640 "\"id\":%d,"
641 "\"field\":{\"pos\":%d,\"name\":\"%s\"},"
642 "\"processNum\":%u,",
643 obj_type, stats[ST_F_IID].u.u32,
644 stats[ST_F_SID].u.u32, field,
645 stat_field_names[field], stats[ST_F_PID].u.u32);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200646 if (old_len == out->data)
Simon Horman05ee2132017-01-04 09:37:25 +0100647 goto err;
648
649 if (!stats_emit_json_field_tags(out, &stats[field]))
650 goto err;
651
652 if (!stats_emit_json_data_field(out, &stats[field]))
653 goto err;
654
655 if (!chunk_strcat(out, "}"))
656 goto err;
657 }
658
659 if (!chunk_strcat(out, "]"))
660 goto err;
661
662 return 1;
663
664err:
665 chunk_reset(out);
Willy Tarreaub0ce3ad2019-10-09 11:19:29 +0200666 if (flags & STAT_STARTED)
667 chunk_strcat(out, ",");
Simon Horman05ee2132017-01-04 09:37:25 +0100668 chunk_appendf(out, "{\"errorStr\":\"output buffer too short\"}");
669 return 0;
670}
671
William Lallemand74c24fb2016-11-21 17:18:36 +0100672/* Dump all fields from <stats> into <out> using the HTML format. A column is
Willy Tarreauab02b3f2019-10-09 11:11:46 +0200673 * reserved for the checkbox is STAT_ADMIN is set in <flags>. Some extra info
Willy Tarreau708c4162019-10-09 10:19:16 +0200674 * are provided if STAT_SHLGNDS is present in <flags>.
William Lallemand74c24fb2016-11-21 17:18:36 +0100675 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200676static int stats_dump_fields_html(struct buffer *out,
677 const struct field *stats,
678 unsigned int flags)
William Lallemand74c24fb2016-11-21 17:18:36 +0100679{
Willy Tarreau83061a82018-07-13 11:56:34 +0200680 struct buffer src;
William Lallemand74c24fb2016-11-21 17:18:36 +0100681
682 if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_FE) {
683 chunk_appendf(out,
684 /* name, queue */
685 "<tr class=\"frontend\">");
686
Willy Tarreauab02b3f2019-10-09 11:11:46 +0200687 if (flags & STAT_ADMIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100688 /* Column sub-heading for Enable or Disable server */
689 chunk_appendf(out, "<td></td>");
690 }
691
692 chunk_appendf(out,
693 "<td class=ac>"
694 "<a name=\"%s/Frontend\"></a>"
695 "<a class=lfsb href=\"#%s/Frontend\">Frontend</a></td>"
696 "<td colspan=3></td>"
697 "",
698 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_PXNAME));
699
700 chunk_appendf(out,
701 /* sessions rate : current */
702 "<td><u>%s<div class=tips><table class=det>"
703 "<tr><th>Current connection rate:</th><td>%s/s</td></tr>"
704 "<tr><th>Current session rate:</th><td>%s/s</td></tr>"
705 "",
706 U2H(stats[ST_F_RATE].u.u32),
707 U2H(stats[ST_F_CONN_RATE].u.u32),
708 U2H(stats[ST_F_RATE].u.u32));
709
710 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
711 chunk_appendf(out,
712 "<tr><th>Current request rate:</th><td>%s/s</td></tr>",
713 U2H(stats[ST_F_REQ_RATE].u.u32));
714
715 chunk_appendf(out,
716 "</table></div></u></td>"
717 /* sessions rate : max */
718 "<td><u>%s<div class=tips><table class=det>"
719 "<tr><th>Max connection rate:</th><td>%s/s</td></tr>"
720 "<tr><th>Max session rate:</th><td>%s/s</td></tr>"
721 "",
722 U2H(stats[ST_F_RATE_MAX].u.u32),
723 U2H(stats[ST_F_CONN_RATE_MAX].u.u32),
724 U2H(stats[ST_F_RATE_MAX].u.u32));
725
726 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
727 chunk_appendf(out,
728 "<tr><th>Max request rate:</th><td>%s/s</td></tr>",
729 U2H(stats[ST_F_REQ_RATE_MAX].u.u32));
730
731 chunk_appendf(out,
732 "</table></div></u></td>"
733 /* sessions rate : limit */
734 "<td>%s</td>",
735 LIM2A(stats[ST_F_RATE_LIM].u.u32, "-"));
736
737 chunk_appendf(out,
738 /* sessions: current, max, limit, total */
739 "<td>%s</td><td>%s</td><td>%s</td>"
740 "<td><u>%s<div class=tips><table class=det>"
741 "<tr><th>Cum. connections:</th><td>%s</td></tr>"
742 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
743 "",
744 U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SMAX].u.u32), U2H(stats[ST_F_SLIM].u.u32),
745 U2H(stats[ST_F_STOT].u.u64),
746 U2H(stats[ST_F_CONN_TOT].u.u64),
747 U2H(stats[ST_F_STOT].u.u64));
748
749 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
750 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
751 chunk_appendf(out,
752 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
753 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
754 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
755 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
756 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
757 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
758 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
759 "<tr><th>- other responses:</th><td>%s</td></tr>"
760 "<tr><th>Intercepted requests:</th><td>%s</td></tr>"
Willy Tarreaua1214a52018-12-14 14:00:25 +0100761 "<tr><th>Cache lookups:</th><td>%s</td></tr>"
762 "<tr><th>Cache hits:</th><td>%s</td><td>(%d%%)</td></tr>"
Willy Tarreau1b0f85e2018-05-28 15:12:40 +0200763 "<tr><th>Failed hdr rewrites:</th><td>%s</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +0100764 "",
765 U2H(stats[ST_F_REQ_TOT].u.u64),
766 U2H(stats[ST_F_HRSP_1XX].u.u64),
767 U2H(stats[ST_F_HRSP_2XX].u.u64),
768 U2H(stats[ST_F_COMP_RSP].u.u64),
769 stats[ST_F_HRSP_2XX].u.u64 ?
770 (int)(100 * stats[ST_F_COMP_RSP].u.u64 / stats[ST_F_HRSP_2XX].u.u64) : 0,
771 U2H(stats[ST_F_HRSP_3XX].u.u64),
772 U2H(stats[ST_F_HRSP_4XX].u.u64),
773 U2H(stats[ST_F_HRSP_5XX].u.u64),
774 U2H(stats[ST_F_HRSP_OTHER].u.u64),
Willy Tarreau1b0f85e2018-05-28 15:12:40 +0200775 U2H(stats[ST_F_INTERCEPTED].u.u64),
Willy Tarreaua1214a52018-12-14 14:00:25 +0100776 U2H(stats[ST_F_CACHE_LOOKUPS].u.u64),
777 U2H(stats[ST_F_CACHE_HITS].u.u64),
778 stats[ST_F_CACHE_LOOKUPS].u.u64 ?
779 (int)(100 * stats[ST_F_CACHE_HITS].u.u64 / stats[ST_F_CACHE_LOOKUPS].u.u64) : 0,
Willy Tarreau1b0f85e2018-05-28 15:12:40 +0200780 U2H(stats[ST_F_WREW].u.u64));
William Lallemand74c24fb2016-11-21 17:18:36 +0100781 }
782
783 chunk_appendf(out,
784 "</table></div></u></td>"
785 /* sessions: lbtot, lastsess */
786 "<td></td><td></td>"
787 /* bytes : in */
788 "<td>%s</td>"
789 "",
790 U2H(stats[ST_F_BIN].u.u64));
791
792 chunk_appendf(out,
793 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
794 "<td>%s%s<div class=tips><table class=det>"
795 "<tr><th>Response bytes in:</th><td>%s</td></tr>"
796 "<tr><th>Compression in:</th><td>%s</td></tr>"
797 "<tr><th>Compression out:</th><td>%s</td><td>(%d%%)</td></tr>"
798 "<tr><th>Compression bypass:</th><td>%s</td></tr>"
799 "<tr><th>Total bytes saved:</th><td>%s</td><td>(%d%%)</td></tr>"
800 "</table></div>%s</td>",
801 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "<u>":"",
802 U2H(stats[ST_F_BOUT].u.u64),
803 U2H(stats[ST_F_BOUT].u.u64),
804 U2H(stats[ST_F_COMP_IN].u.u64),
805 U2H(stats[ST_F_COMP_OUT].u.u64),
806 stats[ST_F_COMP_IN].u.u64 ? (int)(stats[ST_F_COMP_OUT].u.u64 * 100 / stats[ST_F_COMP_IN].u.u64) : 0,
807 U2H(stats[ST_F_COMP_BYP].u.u64),
808 U2H(stats[ST_F_COMP_IN].u.u64 - stats[ST_F_COMP_OUT].u.u64),
809 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,
810 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "</u>":"");
811
812 chunk_appendf(out,
813 /* denied: req, resp */
814 "<td>%s</td><td>%s</td>"
815 /* errors : request, connect, response */
816 "<td>%s</td><td></td><td></td>"
817 /* warnings: retries, redispatches */
818 "<td></td><td></td>"
819 /* server status : reflect frontend status */
820 "<td class=ac>%s</td>"
821 /* rest of server: nothing */
822 "<td class=ac colspan=8></td></tr>"
823 "",
824 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
825 U2H(stats[ST_F_EREQ].u.u64),
826 field_str(stats, ST_F_STATUS));
827 }
828 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_SO) {
829 chunk_appendf(out, "<tr class=socket>");
Willy Tarreauab02b3f2019-10-09 11:11:46 +0200830 if (flags & STAT_ADMIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100831 /* Column sub-heading for Enable or Disable server */
832 chunk_appendf(out, "<td></td>");
833 }
834
835 chunk_appendf(out,
836 /* frontend name, listener name */
837 "<td class=ac><a name=\"%s/+%s\"></a>%s"
838 "<a class=lfsb href=\"#%s/+%s\">%s</a>"
839 "",
840 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME),
Willy Tarreau708c4162019-10-09 10:19:16 +0200841 (flags & STAT_SHLGNDS)?"<u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +0100842 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME), field_str(stats, ST_F_SVNAME));
843
Willy Tarreau708c4162019-10-09 10:19:16 +0200844 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100845 chunk_appendf(out, "<div class=tips>");
846
847 if (isdigit(*field_str(stats, ST_F_ADDR)))
848 chunk_appendf(out, "IPv4: %s, ", field_str(stats, ST_F_ADDR));
849 else if (*field_str(stats, ST_F_ADDR) == '[')
850 chunk_appendf(out, "IPv6: %s, ", field_str(stats, ST_F_ADDR));
851 else if (*field_str(stats, ST_F_ADDR))
852 chunk_appendf(out, "%s, ", field_str(stats, ST_F_ADDR));
853
854 /* id */
855 chunk_appendf(out, "id: %d</div>", stats[ST_F_SID].u.u32);
856 }
857
858 chunk_appendf(out,
859 /* queue */
860 "%s</td><td colspan=3></td>"
861 /* sessions rate: current, max, limit */
862 "<td colspan=3>&nbsp;</td>"
863 /* sessions: current, max, limit, total, lbtot, lastsess */
864 "<td>%s</td><td>%s</td><td>%s</td>"
865 "<td>%s</td><td>&nbsp;</td><td>&nbsp;</td>"
866 /* bytes: in, out */
867 "<td>%s</td><td>%s</td>"
868 "",
Willy Tarreau708c4162019-10-09 10:19:16 +0200869 (flags & STAT_SHLGNDS)?"</u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +0100870 U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SMAX].u.u32), U2H(stats[ST_F_SLIM].u.u32),
871 U2H(stats[ST_F_STOT].u.u64), U2H(stats[ST_F_BIN].u.u64), U2H(stats[ST_F_BOUT].u.u64));
872
873 chunk_appendf(out,
874 /* denied: req, resp */
875 "<td>%s</td><td>%s</td>"
876 /* errors: request, connect, response */
877 "<td>%s</td><td></td><td></td>"
878 /* warnings: retries, redispatches */
879 "<td></td><td></td>"
880 /* server status: reflect listener status */
881 "<td class=ac>%s</td>"
882 /* rest of server: nothing */
883 "<td class=ac colspan=8></td></tr>"
884 "",
885 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
886 U2H(stats[ST_F_EREQ].u.u64),
887 field_str(stats, ST_F_STATUS));
888 }
889 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_SV) {
890 const char *style;
891
892 /* determine the style to use depending on the server's state,
893 * its health and weight. There isn't a 1-to-1 mapping between
894 * state and styles for the cases where the server is (still)
895 * up. The reason is that we don't want to report nolb and
896 * drain with the same color.
897 */
898
899 if (strcmp(field_str(stats, ST_F_STATUS), "DOWN") == 0 ||
900 strcmp(field_str(stats, ST_F_STATUS), "DOWN (agent)") == 0) {
901 style = "down";
902 }
903 else if (strcmp(field_str(stats, ST_F_STATUS), "DOWN ") == 0) {
904 style = "going_up";
905 }
906 else if (strcmp(field_str(stats, ST_F_STATUS), "NOLB ") == 0) {
907 style = "going_down";
908 }
909 else if (strcmp(field_str(stats, ST_F_STATUS), "NOLB") == 0) {
910 style = "nolb";
911 }
912 else if (strcmp(field_str(stats, ST_F_STATUS), "no check") == 0) {
913 style = "no_check";
914 }
915 else if (!stats[ST_F_CHKFAIL].type ||
916 stats[ST_F_CHECK_HEALTH].u.u32 == stats[ST_F_CHECK_RISE].u.u32 + stats[ST_F_CHECK_FALL].u.u32 - 1) {
917 /* no check or max health = UP */
918 if (stats[ST_F_WEIGHT].u.u32)
919 style = "up";
920 else
921 style = "draining";
922 }
923 else {
924 style = "going_down";
925 }
926
927 if (memcmp(field_str(stats, ST_F_STATUS), "MAINT", 5) == 0)
928 chunk_appendf(out, "<tr class=\"maintain\">");
929 else
930 chunk_appendf(out,
931 "<tr class=\"%s_%s\">",
932 (stats[ST_F_BCK].u.u32) ? "backup" : "active", style);
933
934
Willy Tarreauab02b3f2019-10-09 11:11:46 +0200935 if (flags & STAT_ADMIN)
William Lallemand74c24fb2016-11-21 17:18:36 +0100936 chunk_appendf(out,
David Harrigand3db35a2016-12-30 12:12:49 +0000937 "<td><input class='%s-checkbox' type=\"checkbox\" name=\"s\" value=\"%s\"></td>",
938 field_str(stats, ST_F_PXNAME),
William Lallemand74c24fb2016-11-21 17:18:36 +0100939 field_str(stats, ST_F_SVNAME));
940
941 chunk_appendf(out,
942 "<td class=ac><a name=\"%s/%s\"></a>%s"
943 "<a class=lfsb href=\"#%s/%s\">%s</a>"
944 "",
945 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME),
Willy Tarreau708c4162019-10-09 10:19:16 +0200946 (flags & STAT_SHLGNDS) ? "<u>" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +0100947 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME), field_str(stats, ST_F_SVNAME));
948
Willy Tarreau708c4162019-10-09 10:19:16 +0200949 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +0100950 chunk_appendf(out, "<div class=tips>");
951
952 if (isdigit(*field_str(stats, ST_F_ADDR)))
953 chunk_appendf(out, "IPv4: %s, ", field_str(stats, ST_F_ADDR));
954 else if (*field_str(stats, ST_F_ADDR) == '[')
955 chunk_appendf(out, "IPv6: %s, ", field_str(stats, ST_F_ADDR));
956 else if (*field_str(stats, ST_F_ADDR))
957 chunk_appendf(out, "%s, ", field_str(stats, ST_F_ADDR));
958
959 /* id */
960 chunk_appendf(out, "id: %d", stats[ST_F_SID].u.u32);
961
962 /* cookie */
963 if (stats[ST_F_COOKIE].type) {
964 chunk_appendf(out, ", cookie: '");
965 chunk_initstr(&src, field_str(stats, ST_F_COOKIE));
966 chunk_htmlencode(out, &src);
967 chunk_appendf(out, "'");
968 }
969
970 chunk_appendf(out, "</div>");
971 }
972
973 chunk_appendf(out,
974 /* queue : current, max, limit */
975 "%s</td><td>%s</td><td>%s</td><td>%s</td>"
976 /* sessions rate : current, max, limit */
977 "<td>%s</td><td>%s</td><td></td>"
978 "",
Willy Tarreau708c4162019-10-09 10:19:16 +0200979 (flags & STAT_SHLGNDS) ? "</u>" : "",
William Lallemand74c24fb2016-11-21 17:18:36 +0100980 U2H(stats[ST_F_QCUR].u.u32), U2H(stats[ST_F_QMAX].u.u32), LIM2A(stats[ST_F_QLIMIT].u.u32, "-"),
981 U2H(stats[ST_F_RATE].u.u32), U2H(stats[ST_F_RATE_MAX].u.u32));
982
983 chunk_appendf(out,
984 /* sessions: current, max, limit, total */
Willy Tarreauf21d17b2019-09-08 09:24:56 +0200985 "<td><u>%s<div class=tips>"
986 "<table class=det>"
987 "<tr><th>Current active connections:</th><td>%s</td></tr>"
988 "<tr><th>Current idle connections:</th><td>%s</td></tr>"
989 "<tr><th>Active connections limit:</th><td>%s</td></tr>"
990 "<tr><th>Idle connections limit:</th><td>%s</td></tr>"
991 "</table></div></u>"
992 "</td><td>%s</td><td>%s</td>"
William Lallemand74c24fb2016-11-21 17:18:36 +0100993 "<td><u>%s<div class=tips><table class=det>"
994 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
995 "",
Willy Tarreauf21d17b2019-09-08 09:24:56 +0200996 U2H(stats[ST_F_SCUR].u.u32),
997 U2H(stats[ST_F_SCUR].u.u32),
998 U2H(stats[ST_F_SRV_ICUR].u.u32),
999 LIM2A(stats[ST_F_SLIM].u.u32, "-"),
1000 stats[ST_F_SRV_ILIM].type ? U2H(stats[ST_F_SRV_ILIM].u.u32) : "-",
1001 U2H(stats[ST_F_SMAX].u.u32), LIM2A(stats[ST_F_SLIM].u.u32, "-"),
William Lallemand74c24fb2016-11-21 17:18:36 +01001002 U2H(stats[ST_F_STOT].u.u64),
1003 U2H(stats[ST_F_STOT].u.u64));
1004
1005 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
1006 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
1007 unsigned long long tot;
1008
1009 tot = stats[ST_F_HRSP_OTHER].u.u64;
1010 tot += stats[ST_F_HRSP_1XX].u.u64;
1011 tot += stats[ST_F_HRSP_2XX].u.u64;
1012 tot += stats[ST_F_HRSP_3XX].u.u64;
1013 tot += stats[ST_F_HRSP_4XX].u.u64;
1014 tot += stats[ST_F_HRSP_5XX].u.u64;
1015
1016 chunk_appendf(out,
Willy Tarreauf1573842018-12-14 11:35:36 +01001017 "<tr><th>New connections:</th><td>%s</td></tr>"
1018 "<tr><th>Reused connections:</th><td>%s</td><td>(%d%%)</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001019 "<tr><th>Cum. HTTP responses:</th><td>%s</td></tr>"
1020 "<tr><th>- HTTP 1xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1021 "<tr><th>- HTTP 2xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1022 "<tr><th>- HTTP 3xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1023 "<tr><th>- HTTP 4xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1024 "<tr><th>- HTTP 5xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1025 "<tr><th>- other responses:</th><td>%s</td><td>(%d%%)</td></tr>"
Willy Tarreau1b0f85e2018-05-28 15:12:40 +02001026 "<tr><th>Failed hdr rewrites:</th><td>%s</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001027 "",
Willy Tarreauf1573842018-12-14 11:35:36 +01001028 U2H(stats[ST_F_CONNECT].u.u64),
1029 U2H(stats[ST_F_REUSE].u.u64),
1030 (stats[ST_F_CONNECT].u.u64 + stats[ST_F_REUSE].u.u64) ?
1031 (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 +01001032 U2H(tot),
1033 U2H(stats[ST_F_HRSP_1XX].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_1XX].u.u64 / tot) : 0,
1034 U2H(stats[ST_F_HRSP_2XX].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_2XX].u.u64 / tot) : 0,
1035 U2H(stats[ST_F_HRSP_3XX].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_3XX].u.u64 / tot) : 0,
1036 U2H(stats[ST_F_HRSP_4XX].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_4XX].u.u64 / tot) : 0,
1037 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 +02001038 U2H(stats[ST_F_HRSP_OTHER].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_OTHER].u.u64 / tot) : 0,
1039 U2H(stats[ST_F_WREW].u.u64));
William Lallemand74c24fb2016-11-21 17:18:36 +01001040 }
1041
1042 chunk_appendf(out, "<tr><th colspan=3>Avg over last 1024 success. conn.</th></tr>");
1043 chunk_appendf(out, "<tr><th>- Queue time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_QTIME].u.u32));
1044 chunk_appendf(out, "<tr><th>- Connect time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_CTIME].u.u32));
1045 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
1046 chunk_appendf(out, "<tr><th>- Response time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_RTIME].u.u32));
1047 chunk_appendf(out, "<tr><th>- Total time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_TTIME].u.u32));
1048
1049 chunk_appendf(out,
1050 "</table></div></u></td>"
1051 /* sessions: lbtot, last */
1052 "<td>%s</td><td>%s</td>",
1053 U2H(stats[ST_F_LBTOT].u.u64),
1054 human_time(stats[ST_F_LASTSESS].u.s32, 1));
1055
1056 chunk_appendf(out,
1057 /* bytes : in, out */
1058 "<td>%s</td><td>%s</td>"
1059 /* denied: req, resp */
1060 "<td></td><td>%s</td>"
1061 /* errors : request, connect */
1062 "<td></td><td>%s</td>"
1063 /* errors : response */
1064 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
1065 /* warnings: retries, redispatches */
1066 "<td>%lld</td><td>%lld</td>"
1067 "",
1068 U2H(stats[ST_F_BIN].u.u64), U2H(stats[ST_F_BOUT].u.u64),
1069 U2H(stats[ST_F_DRESP].u.u64),
1070 U2H(stats[ST_F_ECON].u.u64),
1071 U2H(stats[ST_F_ERESP].u.u64),
1072 (long long)stats[ST_F_CLI_ABRT].u.u64,
1073 (long long)stats[ST_F_SRV_ABRT].u.u64,
1074 (long long)stats[ST_F_WRETR].u.u64,
1075 (long long)stats[ST_F_WREDIS].u.u64);
1076
1077 /* status, last change */
1078 chunk_appendf(out, "<td class=ac>");
1079
1080 /* FIXME!!!!
1081 * LASTCHG should contain the last change for *this* server and must be computed
1082 * properly above, as was done below, ie: this server if maint, otherwise ref server
1083 * if tracking. Note that ref is either local or remote depending on tracking.
1084 */
1085
1086
1087 if (memcmp(field_str(stats, ST_F_STATUS), "MAINT", 5) == 0) {
1088 chunk_appendf(out, "%s MAINT", human_time(stats[ST_F_LASTCHG].u.u32, 1));
1089 }
1090 else if (memcmp(field_str(stats, ST_F_STATUS), "no check", 5) == 0) {
1091 chunk_strcat(out, "<i>no check</i>");
1092 }
1093 else {
1094 chunk_appendf(out, "%s %s", human_time(stats[ST_F_LASTCHG].u.u32, 1), field_str(stats, ST_F_STATUS));
1095 if (memcmp(field_str(stats, ST_F_STATUS), "DOWN", 4) == 0) {
1096 if (stats[ST_F_CHECK_HEALTH].u.u32)
1097 chunk_strcat(out, " &uarr;");
1098 }
1099 else if (stats[ST_F_CHECK_HEALTH].u.u32 < stats[ST_F_CHECK_RISE].u.u32 + stats[ST_F_CHECK_FALL].u.u32 - 1)
1100 chunk_strcat(out, " &darr;");
1101 }
1102
1103 if (memcmp(field_str(stats, ST_F_STATUS), "DOWN", 4) == 0 &&
1104 stats[ST_F_AGENT_STATUS].type && !stats[ST_F_AGENT_HEALTH].u.u32) {
1105 chunk_appendf(out,
1106 "</td><td class=ac><u> %s",
1107 field_str(stats, ST_F_AGENT_STATUS));
1108
1109 if (stats[ST_F_AGENT_CODE].type)
1110 chunk_appendf(out, "/%d", stats[ST_F_AGENT_CODE].u.u32);
1111
1112 if (stats[ST_F_AGENT_DURATION].type)
1113 chunk_appendf(out, " in %lums", (long)stats[ST_F_AGENT_DURATION].u.u64);
1114
1115 chunk_appendf(out, "<div class=tips>%s", field_str(stats, ST_F_AGENT_DESC));
1116
1117 if (*field_str(stats, ST_F_LAST_AGT)) {
1118 chunk_appendf(out, ": ");
1119 chunk_initstr(&src, field_str(stats, ST_F_LAST_AGT));
1120 chunk_htmlencode(out, &src);
1121 }
1122 chunk_appendf(out, "</div></u>");
1123 }
1124 else if (stats[ST_F_CHECK_STATUS].type) {
1125 chunk_appendf(out,
1126 "</td><td class=ac><u> %s",
1127 field_str(stats, ST_F_CHECK_STATUS));
1128
1129 if (stats[ST_F_CHECK_CODE].type)
1130 chunk_appendf(out, "/%d", stats[ST_F_CHECK_CODE].u.u32);
1131
1132 if (stats[ST_F_CHECK_DURATION].type)
1133 chunk_appendf(out, " in %lums", (long)stats[ST_F_CHECK_DURATION].u.u64);
1134
1135 chunk_appendf(out, "<div class=tips>%s", field_str(stats, ST_F_CHECK_DESC));
1136
1137 if (*field_str(stats, ST_F_LAST_CHK)) {
1138 chunk_appendf(out, ": ");
1139 chunk_initstr(&src, field_str(stats, ST_F_LAST_CHK));
1140 chunk_htmlencode(out, &src);
1141 }
1142 chunk_appendf(out, "</div></u>");
1143 }
1144 else
1145 chunk_appendf(out, "</td><td>");
1146
1147 chunk_appendf(out,
1148 /* weight */
1149 "</td><td class=ac>%d</td>"
1150 /* act, bck */
1151 "<td class=ac>%s</td><td class=ac>%s</td>"
1152 "",
1153 stats[ST_F_WEIGHT].u.u32,
1154 stats[ST_F_BCK].u.u32 ? "-" : "Y",
1155 stats[ST_F_BCK].u.u32 ? "Y" : "-");
1156
1157 /* check failures: unique, fatal, down time */
1158 if (strcmp(field_str(stats, ST_F_STATUS), "MAINT (resolution)") == 0) {
1159 chunk_appendf(out, "<td class=ac colspan=3>resolution</td>");
1160 }
1161 else if (stats[ST_F_CHKFAIL].type) {
1162 chunk_appendf(out, "<td><u>%lld", (long long)stats[ST_F_CHKFAIL].u.u64);
1163
1164 if (stats[ST_F_HANAFAIL].type)
1165 chunk_appendf(out, "/%lld", (long long)stats[ST_F_HANAFAIL].u.u64);
1166
1167 chunk_appendf(out,
1168 "<div class=tips>Failed Health Checks%s</div></u></td>"
1169 "<td>%lld</td><td>%s</td>"
1170 "",
1171 stats[ST_F_HANAFAIL].type ? "/Health Analyses" : "",
1172 (long long)stats[ST_F_CHKDOWN].u.u64, human_time(stats[ST_F_DOWNTIME].u.u32, 1));
1173 }
1174 else if (strcmp(field_str(stats, ST_F_STATUS), "MAINT") != 0 && field_format(stats, ST_F_TRACKED) == FF_STR) {
1175 /* tracking a server (hence inherited maint would appear as "MAINT (via...)" */
1176 chunk_appendf(out,
1177 "<td class=ac colspan=3><a class=lfsb href=\"#%s\">via %s</a></td>",
1178 field_str(stats, ST_F_TRACKED), field_str(stats, ST_F_TRACKED));
1179 }
1180 else
1181 chunk_appendf(out, "<td colspan=3></td>");
1182
1183 /* throttle */
1184 if (stats[ST_F_THROTTLE].type)
1185 chunk_appendf(out, "<td class=ac>%d %%</td></tr>\n", stats[ST_F_THROTTLE].u.u32);
1186 else
1187 chunk_appendf(out, "<td class=ac>-</td></tr>\n");
1188 }
1189 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_BE) {
1190 chunk_appendf(out, "<tr class=\"backend\">");
Willy Tarreauab02b3f2019-10-09 11:11:46 +02001191 if (flags & STAT_ADMIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001192 /* Column sub-heading for Enable or Disable server */
1193 chunk_appendf(out, "<td></td>");
1194 }
1195 chunk_appendf(out,
1196 "<td class=ac>"
1197 /* name */
1198 "%s<a name=\"%s/Backend\"></a>"
1199 "<a class=lfsb href=\"#%s/Backend\">Backend</a>"
1200 "",
Willy Tarreau708c4162019-10-09 10:19:16 +02001201 (flags & STAT_SHLGNDS)?"<u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01001202 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_PXNAME));
1203
Willy Tarreau708c4162019-10-09 10:19:16 +02001204 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001205 /* balancing */
1206 chunk_appendf(out, "<div class=tips>balancing: %s",
1207 field_str(stats, ST_F_ALGO));
1208
1209 /* cookie */
1210 if (stats[ST_F_COOKIE].type) {
1211 chunk_appendf(out, ", cookie: '");
1212 chunk_initstr(&src, field_str(stats, ST_F_COOKIE));
1213 chunk_htmlencode(out, &src);
1214 chunk_appendf(out, "'");
1215 }
1216 chunk_appendf(out, "</div>");
1217 }
1218
1219 chunk_appendf(out,
1220 "%s</td>"
1221 /* queue : current, max */
1222 "<td>%s</td><td>%s</td><td></td>"
1223 /* sessions rate : current, max, limit */
1224 "<td>%s</td><td>%s</td><td></td>"
1225 "",
Willy Tarreau708c4162019-10-09 10:19:16 +02001226 (flags & STAT_SHLGNDS)?"</u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01001227 U2H(stats[ST_F_QCUR].u.u32), U2H(stats[ST_F_QMAX].u.u32),
1228 U2H(stats[ST_F_RATE].u.u32), U2H(stats[ST_F_RATE_MAX].u.u32));
1229
1230 chunk_appendf(out,
1231 /* sessions: current, max, limit, total */
1232 "<td>%s</td><td>%s</td><td>%s</td>"
1233 "<td><u>%s<div class=tips><table class=det>"
1234 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
1235 "",
Willy Tarreau8e0f1752016-12-12 15:07:29 +01001236 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 +01001237 U2H(stats[ST_F_STOT].u.u64),
1238 U2H(stats[ST_F_STOT].u.u64));
1239
1240 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
1241 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
1242 chunk_appendf(out,
Willy Tarreauf1573842018-12-14 11:35:36 +01001243 "<tr><th>New connections:</th><td>%s</td></tr>"
1244 "<tr><th>Reused connections:</th><td>%s</td><td>(%d%%)</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001245 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
1246 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
1247 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
1248 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
1249 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
1250 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
1251 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
1252 "<tr><th>- other responses:</th><td>%s</td></tr>"
Willy Tarreaua1214a52018-12-14 14:00:25 +01001253 "<tr><th>Cache lookups:</th><td>%s</td></tr>"
1254 "<tr><th>Cache hits:</th><td>%s</td><td>(%d%%)</td></tr>"
Willy Tarreau1b0f85e2018-05-28 15:12:40 +02001255 "<tr><th>Failed hdr rewrites:</th><td>%s</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001256 "<tr><th colspan=3>Avg over last 1024 success. conn.</th></tr>"
1257 "",
Willy Tarreauf1573842018-12-14 11:35:36 +01001258 U2H(stats[ST_F_CONNECT].u.u64),
1259 U2H(stats[ST_F_REUSE].u.u64),
1260 (stats[ST_F_CONNECT].u.u64 + stats[ST_F_REUSE].u.u64) ?
1261 (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 +01001262 U2H(stats[ST_F_REQ_TOT].u.u64),
1263 U2H(stats[ST_F_HRSP_1XX].u.u64),
1264 U2H(stats[ST_F_HRSP_2XX].u.u64),
1265 U2H(stats[ST_F_COMP_RSP].u.u64),
1266 stats[ST_F_HRSP_2XX].u.u64 ?
1267 (int)(100 * stats[ST_F_COMP_RSP].u.u64 / stats[ST_F_HRSP_2XX].u.u64) : 0,
1268 U2H(stats[ST_F_HRSP_3XX].u.u64),
1269 U2H(stats[ST_F_HRSP_4XX].u.u64),
1270 U2H(stats[ST_F_HRSP_5XX].u.u64),
Willy Tarreaufeead3a2018-12-14 13:48:44 +01001271 U2H(stats[ST_F_HRSP_OTHER].u.u64),
Willy Tarreaua1214a52018-12-14 14:00:25 +01001272 U2H(stats[ST_F_CACHE_LOOKUPS].u.u64),
1273 U2H(stats[ST_F_CACHE_HITS].u.u64),
1274 stats[ST_F_CACHE_LOOKUPS].u.u64 ?
1275 (int)(100 * stats[ST_F_CACHE_HITS].u.u64 / stats[ST_F_CACHE_LOOKUPS].u.u64) : 0,
Willy Tarreaufeead3a2018-12-14 13:48:44 +01001276 U2H(stats[ST_F_WREW].u.u64));
William Lallemand74c24fb2016-11-21 17:18:36 +01001277 }
1278
1279 chunk_appendf(out, "<tr><th>- Queue time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_QTIME].u.u32));
1280 chunk_appendf(out, "<tr><th>- Connect time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_CTIME].u.u32));
1281 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
1282 chunk_appendf(out, "<tr><th>- Response time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_RTIME].u.u32));
1283 chunk_appendf(out, "<tr><th>- Total time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_TTIME].u.u32));
1284
1285 chunk_appendf(out,
1286 "</table></div></u></td>"
1287 /* sessions: lbtot, last */
1288 "<td>%s</td><td>%s</td>"
1289 /* bytes: in */
1290 "<td>%s</td>"
1291 "",
1292 U2H(stats[ST_F_LBTOT].u.u64),
1293 human_time(stats[ST_F_LASTSESS].u.s32, 1),
1294 U2H(stats[ST_F_BIN].u.u64));
1295
1296 chunk_appendf(out,
1297 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
1298 "<td>%s%s<div class=tips><table class=det>"
1299 "<tr><th>Response bytes in:</th><td>%s</td></tr>"
1300 "<tr><th>Compression in:</th><td>%s</td></tr>"
1301 "<tr><th>Compression out:</th><td>%s</td><td>(%d%%)</td></tr>"
1302 "<tr><th>Compression bypass:</th><td>%s</td></tr>"
1303 "<tr><th>Total bytes saved:</th><td>%s</td><td>(%d%%)</td></tr>"
1304 "</table></div>%s</td>",
1305 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "<u>":"",
1306 U2H(stats[ST_F_BOUT].u.u64),
1307 U2H(stats[ST_F_BOUT].u.u64),
1308 U2H(stats[ST_F_COMP_IN].u.u64),
1309 U2H(stats[ST_F_COMP_OUT].u.u64),
1310 stats[ST_F_COMP_IN].u.u64 ? (int)(stats[ST_F_COMP_OUT].u.u64 * 100 / stats[ST_F_COMP_IN].u.u64) : 0,
1311 U2H(stats[ST_F_COMP_BYP].u.u64),
1312 U2H(stats[ST_F_COMP_IN].u.u64 - stats[ST_F_COMP_OUT].u.u64),
1313 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,
1314 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "</u>":"");
1315
1316 chunk_appendf(out,
1317 /* denied: req, resp */
1318 "<td>%s</td><td>%s</td>"
1319 /* errors : request, connect */
1320 "<td></td><td>%s</td>"
1321 /* errors : response */
1322 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
1323 /* warnings: retries, redispatches */
1324 "<td>%lld</td><td>%lld</td>"
1325 /* backend status: reflect backend status (up/down): we display UP
1326 * if the backend has known working servers or if it has no server at
1327 * all (eg: for stats). Then we display the total weight, number of
1328 * active and backups. */
1329 "<td class=ac>%s %s</td><td class=ac>&nbsp;</td><td class=ac>%d</td>"
1330 "<td class=ac>%d</td><td class=ac>%d</td>"
1331 "",
1332 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
1333 U2H(stats[ST_F_ECON].u.u64),
1334 U2H(stats[ST_F_ERESP].u.u64),
1335 (long long)stats[ST_F_CLI_ABRT].u.u64,
1336 (long long)stats[ST_F_SRV_ABRT].u.u64,
1337 (long long)stats[ST_F_WRETR].u.u64, (long long)stats[ST_F_WREDIS].u.u64,
1338 human_time(stats[ST_F_LASTCHG].u.u32, 1),
1339 strcmp(field_str(stats, ST_F_STATUS), "DOWN") ? field_str(stats, ST_F_STATUS) : "<font color=\"red\"><b>DOWN</b></font>",
1340 stats[ST_F_WEIGHT].u.u32,
1341 stats[ST_F_ACT].u.u32, stats[ST_F_BCK].u.u32);
1342
1343 chunk_appendf(out,
1344 /* rest of backend: nothing, down transitions, total downtime, throttle */
1345 "<td class=ac>&nbsp;</td><td>%d</td>"
1346 "<td>%s</td>"
1347 "<td></td>"
1348 "</tr>",
1349 stats[ST_F_CHKDOWN].u.u32,
1350 stats[ST_F_DOWNTIME].type ? human_time(stats[ST_F_DOWNTIME].u.u32, 1) : "&nbsp;");
1351 }
1352 return 1;
1353}
1354
Willy Tarreau43241ff2019-10-09 11:27:51 +02001355static int stats_dump_one_line(const struct field *stats, struct proxy *px, struct appctx *appctx)
William Lallemand74c24fb2016-11-21 17:18:36 +01001356{
Simon Horman05ee2132017-01-04 09:37:25 +01001357 int ret;
1358
William Lallemand74c24fb2016-11-21 17:18:36 +01001359 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
Willy Tarreau43241ff2019-10-09 11:27:51 +02001360 ret = stats_dump_fields_html(&trash, stats, appctx->ctx.stats.flags);
William Lallemand74c24fb2016-11-21 17:18:36 +01001361 else if (appctx->ctx.stats.flags & STAT_FMT_TYPED)
Willy Tarreau43241ff2019-10-09 11:27:51 +02001362 ret = stats_dump_fields_typed(&trash, stats, appctx->ctx.stats.flags);
Simon Horman05ee2132017-01-04 09:37:25 +01001363 else if (appctx->ctx.stats.flags & STAT_FMT_JSON)
Willy Tarreaub0ce3ad2019-10-09 11:19:29 +02001364 ret = stats_dump_fields_json(&trash, stats, appctx->ctx.stats.flags);
William Lallemand74c24fb2016-11-21 17:18:36 +01001365 else
Willy Tarreau43241ff2019-10-09 11:27:51 +02001366 ret = stats_dump_fields_csv(&trash, stats, appctx->ctx.stats.flags);
Simon Horman05ee2132017-01-04 09:37:25 +01001367
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
Willy Tarreau43241ff2019-10-09 11:27:51 +02001459 return stats_dump_one_line(stats, px, appctx);
William Lallemand74c24fb2016-11-21 17:18:36 +01001460}
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
Willy Tarreau708c4162019-10-09 10:19:16 +02001466 * returns 1. <flags> can take the value STAT_SHLGNDS.
William Lallemand74c24fb2016-11-21 17:18:36 +01001467 */
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
Willy Tarreau708c4162019-10-09 10:19:16 +02001503 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001504 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
Willy Tarreau43241ff2019-10-09 11:27:51 +02001533 * from stream interface <si>. The caller is responsible for clearing the trash
1534 * if needed. Returns non-zero if it emits anything, zero otherwise.
William Lallemand74c24fb2016-11-21 17:18:36 +01001535 */
Willy Tarreau43241ff2019-10-09 11:27:51 +02001536static int stats_dump_li_stats(struct stream_interface *si, struct proxy *px, struct listener *l)
William Lallemand74c24fb2016-11-21 17:18:36 +01001537{
1538 struct appctx *appctx = __objt_appctx(si->end);
1539
Willy Tarreau43241ff2019-10-09 11:27:51 +02001540 if (!stats_fill_li_stats(px, l, appctx->ctx.stats.flags, stats, ST_F_TOTAL_FIELDS))
William Lallemand74c24fb2016-11-21 17:18:36 +01001541 return 0;
1542
Willy Tarreau43241ff2019-10-09 11:27:51 +02001543 return stats_dump_one_line(stats, px, appctx);
William Lallemand74c24fb2016-11-21 17:18:36 +01001544}
1545
1546enum srv_stats_state {
1547 SRV_STATS_STATE_DOWN = 0,
1548 SRV_STATS_STATE_DOWN_AGENT,
1549 SRV_STATS_STATE_GOING_UP,
1550 SRV_STATS_STATE_UP_GOING_DOWN,
1551 SRV_STATS_STATE_UP,
1552 SRV_STATS_STATE_NOLB_GOING_DOWN,
1553 SRV_STATS_STATE_NOLB,
1554 SRV_STATS_STATE_DRAIN_GOING_DOWN,
1555 SRV_STATS_STATE_DRAIN,
1556 SRV_STATS_STATE_DRAIN_AGENT,
1557 SRV_STATS_STATE_NO_CHECK,
1558
1559 SRV_STATS_STATE_COUNT, /* Must be last */
1560};
1561
1562static const char *srv_hlt_st[SRV_STATS_STATE_COUNT] = {
1563 [SRV_STATS_STATE_DOWN] = "DOWN",
1564 [SRV_STATS_STATE_DOWN_AGENT] = "DOWN (agent)",
1565 [SRV_STATS_STATE_GOING_UP] = "DOWN %d/%d",
1566 [SRV_STATS_STATE_UP_GOING_DOWN] = "UP %d/%d",
1567 [SRV_STATS_STATE_UP] = "UP",
1568 [SRV_STATS_STATE_NOLB_GOING_DOWN] = "NOLB %d/%d",
1569 [SRV_STATS_STATE_NOLB] = "NOLB",
1570 [SRV_STATS_STATE_DRAIN_GOING_DOWN] = "DRAIN %d/%d",
1571 [SRV_STATS_STATE_DRAIN] = "DRAIN",
1572 [SRV_STATS_STATE_DRAIN_AGENT] = "DRAIN (agent)",
1573 [SRV_STATS_STATE_NO_CHECK] = "no check"
1574};
1575
1576/* Fill <stats> with the server statistics. <stats> is
1577 * preallocated array of length <len>. The length of the array
1578 * must be at least ST_F_TOTAL_FIELDS. If this length is less
1579 * then this value, the function returns 0, otherwise, it
Willy Tarreau708c4162019-10-09 10:19:16 +02001580 * returns 1. <flags> can take the value STAT_SHLGNDS.
William Lallemand74c24fb2016-11-21 17:18:36 +01001581 */
1582int stats_fill_sv_stats(struct proxy *px, struct server *sv, int flags,
1583 struct field *stats, int len)
1584{
1585 struct server *via, *ref;
1586 char str[INET6_ADDRSTRLEN];
Willy Tarreau83061a82018-07-13 11:56:34 +02001587 struct buffer *out = get_trash_chunk();
William Lallemand74c24fb2016-11-21 17:18:36 +01001588 enum srv_stats_state state;
1589 char *fld_status;
1590
1591 if (len < ST_F_TOTAL_FIELDS)
1592 return 0;
1593
1594 memset(stats, 0, sizeof(*stats) * len);
1595
1596 /* we have "via" which is the tracked server as described in the configuration,
1597 * and "ref" which is the checked server and the end of the chain.
1598 */
1599 via = sv->track ? sv->track : sv;
1600 ref = via;
1601 while (ref->track)
1602 ref = ref->track;
1603
Emeric Brun52a91d32017-08-31 14:41:55 +02001604 if (sv->cur_state == SRV_ST_RUNNING || sv->cur_state == SRV_ST_STARTING) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001605 if ((ref->check.state & CHK_ST_ENABLED) &&
1606 (ref->check.health < ref->check.rise + ref->check.fall - 1)) {
1607 state = SRV_STATS_STATE_UP_GOING_DOWN;
1608 } else {
1609 state = SRV_STATS_STATE_UP;
1610 }
1611
Emeric Brun52a91d32017-08-31 14:41:55 +02001612 if (sv->cur_admin & SRV_ADMF_DRAIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001613 if (ref->agent.state & CHK_ST_ENABLED)
1614 state = SRV_STATS_STATE_DRAIN_AGENT;
1615 else if (state == SRV_STATS_STATE_UP_GOING_DOWN)
1616 state = SRV_STATS_STATE_DRAIN_GOING_DOWN;
1617 else
1618 state = SRV_STATS_STATE_DRAIN;
1619 }
1620
1621 if (state == SRV_STATS_STATE_UP && !(ref->check.state & CHK_ST_ENABLED)) {
1622 state = SRV_STATS_STATE_NO_CHECK;
1623 }
1624 }
Emeric Brun52a91d32017-08-31 14:41:55 +02001625 else if (sv->cur_state == SRV_ST_STOPPING) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001626 if ((!(sv->check.state & CHK_ST_ENABLED) && !sv->track) ||
1627 (ref->check.health == ref->check.rise + ref->check.fall - 1)) {
1628 state = SRV_STATS_STATE_NOLB;
1629 } else {
1630 state = SRV_STATS_STATE_NOLB_GOING_DOWN;
1631 }
1632 }
1633 else { /* stopped */
1634 if ((ref->agent.state & CHK_ST_ENABLED) && !ref->agent.health) {
1635 state = SRV_STATS_STATE_DOWN_AGENT;
1636 } else if ((ref->check.state & CHK_ST_ENABLED) && !ref->check.health) {
1637 state = SRV_STATS_STATE_DOWN; /* DOWN */
1638 } else if ((ref->agent.state & CHK_ST_ENABLED) || (ref->check.state & CHK_ST_ENABLED)) {
1639 state = SRV_STATS_STATE_GOING_UP;
1640 } else {
1641 state = SRV_STATS_STATE_DOWN; /* DOWN, unchecked */
1642 }
1643 }
1644
1645 chunk_reset(out);
1646
1647 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1648 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, sv->id);
1649 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1650 stats[ST_F_QCUR] = mkf_u32(0, sv->nbpend);
1651 stats[ST_F_QMAX] = mkf_u32(FN_MAX, sv->counters.nbpend_max);
1652 stats[ST_F_SCUR] = mkf_u32(0, sv->cur_sess);
1653 stats[ST_F_SMAX] = mkf_u32(FN_MAX, sv->counters.cur_sess_max);
1654
1655 if (sv->maxconn)
1656 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, sv->maxconn);
1657
Willy Tarreauf21d17b2019-09-08 09:24:56 +02001658 stats[ST_F_SRV_ICUR] = mkf_u32(0, sv->curr_idle_conns);
1659 if (sv->max_idle_conns != -1)
1660 stats[ST_F_SRV_ILIM] = mkf_u32(FO_CONFIG|FN_LIMIT, sv->max_idle_conns);
1661
William Lallemand74c24fb2016-11-21 17:18:36 +01001662 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, sv->counters.cum_sess);
1663 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, sv->counters.bytes_in);
1664 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, sv->counters.bytes_out);
1665 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, sv->counters.failed_secu);
1666 stats[ST_F_ECON] = mkf_u64(FN_COUNTER, sv->counters.failed_conns);
1667 stats[ST_F_ERESP] = mkf_u64(FN_COUNTER, sv->counters.failed_resp);
1668 stats[ST_F_WRETR] = mkf_u64(FN_COUNTER, sv->counters.retries);
1669 stats[ST_F_WREDIS] = mkf_u64(FN_COUNTER, sv->counters.redispatches);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001670 stats[ST_F_WREW] = mkf_u64(FN_COUNTER, sv->counters.failed_rewrites);
Willy Tarreauf1573842018-12-14 11:35:36 +01001671 stats[ST_F_CONNECT] = mkf_u64(FN_COUNTER, sv->counters.connect);
1672 stats[ST_F_REUSE] = mkf_u64(FN_COUNTER, sv->counters.reuse);
William Lallemand74c24fb2016-11-21 17:18:36 +01001673
1674 /* status */
1675 fld_status = chunk_newstr(out);
Emeric Brun52a91d32017-08-31 14:41:55 +02001676 if (sv->cur_admin & SRV_ADMF_RMAINT)
William Lallemand74c24fb2016-11-21 17:18:36 +01001677 chunk_appendf(out, "MAINT (resolution)");
Emeric Brun52a91d32017-08-31 14:41:55 +02001678 else if (sv->cur_admin & SRV_ADMF_IMAINT)
William Lallemand74c24fb2016-11-21 17:18:36 +01001679 chunk_appendf(out, "MAINT (via %s/%s)", via->proxy->id, via->id);
Emeric Brun52a91d32017-08-31 14:41:55 +02001680 else if (sv->cur_admin & SRV_ADMF_MAINT)
William Lallemand74c24fb2016-11-21 17:18:36 +01001681 chunk_appendf(out, "MAINT");
1682 else
1683 chunk_appendf(out,
1684 srv_hlt_st[state],
Emeric Brun52a91d32017-08-31 14:41:55 +02001685 (ref->cur_state != SRV_ST_STOPPED) ? (ref->check.health - ref->check.rise + 1) : (ref->check.health),
1686 (ref->cur_state != SRV_ST_STOPPED) ? (ref->check.fall) : (ref->check.rise));
William Lallemand74c24fb2016-11-21 17:18:36 +01001687
1688 stats[ST_F_STATUS] = mkf_str(FO_STATUS, fld_status);
1689 stats[ST_F_LASTCHG] = mkf_u32(FN_AGE, now.tv_sec - sv->last_change);
Emeric Brun52a91d32017-08-31 14:41:55 +02001690 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 +01001691 stats[ST_F_ACT] = mkf_u32(FO_STATUS, (sv->flags & SRV_F_BACKUP) ? 0 : 1);
1692 stats[ST_F_BCK] = mkf_u32(FO_STATUS, (sv->flags & SRV_F_BACKUP) ? 1 : 0);
1693
1694 /* check failures: unique, fatal; last change, total downtime */
1695 if (sv->check.state & CHK_ST_ENABLED) {
1696 stats[ST_F_CHKFAIL] = mkf_u64(FN_COUNTER, sv->counters.failed_checks);
1697 stats[ST_F_CHKDOWN] = mkf_u64(FN_COUNTER, sv->counters.down_trans);
1698 stats[ST_F_DOWNTIME] = mkf_u32(FN_COUNTER, srv_downtime(sv));
1699 }
1700
1701 if (sv->maxqueue)
1702 stats[ST_F_QLIMIT] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->maxqueue);
1703
1704 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
1705 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1706 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, sv->puid);
1707
Emeric Brun52a91d32017-08-31 14:41:55 +02001708 if (sv->cur_state == SRV_ST_STARTING && !server_is_draining(sv))
William Lallemand74c24fb2016-11-21 17:18:36 +01001709 stats[ST_F_THROTTLE] = mkf_u32(FN_AVG, server_throttle_rate(sv));
1710
1711 stats[ST_F_LBTOT] = mkf_u64(FN_COUNTER, sv->counters.cum_lbconn);
1712
1713 if (sv->track) {
1714 char *fld_track = chunk_newstr(out);
1715
1716 chunk_appendf(out, "%s/%s", sv->track->proxy->id, sv->track->id);
1717 stats[ST_F_TRACKED] = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, fld_track);
1718 }
1719
1720 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_SV);
1721 stats[ST_F_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&sv->sess_per_sec));
1722 stats[ST_F_RATE_MAX] = mkf_u32(FN_MAX, sv->counters.sps_max);
1723
1724 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) {
1725 const char *fld_chksts;
1726
1727 fld_chksts = chunk_newstr(out);
1728 chunk_strcat(out, "* "); // for check in progress
1729 chunk_strcat(out, get_check_status_info(sv->check.status));
1730 if (!(sv->check.state & CHK_ST_INPROGRESS))
1731 fld_chksts += 2; // skip "* "
1732 stats[ST_F_CHECK_STATUS] = mkf_str(FN_OUTPUT, fld_chksts);
1733
1734 if (sv->check.status >= HCHK_STATUS_L57DATA)
1735 stats[ST_F_CHECK_CODE] = mkf_u32(FN_OUTPUT, sv->check.code);
1736
1737 if (sv->check.status >= HCHK_STATUS_CHECKED)
1738 stats[ST_F_CHECK_DURATION] = mkf_u64(FN_DURATION, sv->check.duration);
1739
1740 stats[ST_F_CHECK_DESC] = mkf_str(FN_OUTPUT, get_check_status_description(sv->check.status));
1741 stats[ST_F_LAST_CHK] = mkf_str(FN_OUTPUT, sv->check.desc);
1742 stats[ST_F_CHECK_RISE] = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.rise);
1743 stats[ST_F_CHECK_FALL] = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.fall);
1744 stats[ST_F_CHECK_HEALTH] = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.health);
1745 }
1746
1747 if ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) {
1748 const char *fld_chksts;
1749
1750 fld_chksts = chunk_newstr(out);
1751 chunk_strcat(out, "* "); // for check in progress
1752 chunk_strcat(out, get_check_status_info(sv->agent.status));
1753 if (!(sv->agent.state & CHK_ST_INPROGRESS))
1754 fld_chksts += 2; // skip "* "
1755 stats[ST_F_AGENT_STATUS] = mkf_str(FN_OUTPUT, fld_chksts);
1756
1757 if (sv->agent.status >= HCHK_STATUS_L57DATA)
1758 stats[ST_F_AGENT_CODE] = mkf_u32(FN_OUTPUT, sv->agent.code);
1759
1760 if (sv->agent.status >= HCHK_STATUS_CHECKED)
1761 stats[ST_F_AGENT_DURATION] = mkf_u64(FN_DURATION, sv->agent.duration);
1762
1763 stats[ST_F_AGENT_DESC] = mkf_str(FN_OUTPUT, get_check_status_description(sv->agent.status));
1764 stats[ST_F_LAST_AGT] = mkf_str(FN_OUTPUT, sv->agent.desc);
1765 stats[ST_F_AGENT_RISE] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.rise);
1766 stats[ST_F_AGENT_FALL] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.fall);
1767 stats[ST_F_AGENT_HEALTH] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.health);
1768 }
1769
1770 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
1771 if (px->mode == PR_MODE_HTTP) {
1772 stats[ST_F_HRSP_1XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[1]);
1773 stats[ST_F_HRSP_2XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[2]);
1774 stats[ST_F_HRSP_3XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[3]);
1775 stats[ST_F_HRSP_4XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[4]);
1776 stats[ST_F_HRSP_5XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[5]);
1777 stats[ST_F_HRSP_OTHER] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[0]);
1778 }
1779
1780 if (ref->observe)
1781 stats[ST_F_HANAFAIL] = mkf_u64(FN_COUNTER, sv->counters.failed_hana);
1782
1783 stats[ST_F_CLI_ABRT] = mkf_u64(FN_COUNTER, sv->counters.cli_aborts);
1784 stats[ST_F_SRV_ABRT] = mkf_u64(FN_COUNTER, sv->counters.srv_aborts);
1785 stats[ST_F_LASTSESS] = mkf_s32(FN_AGE, srv_lastsession(sv));
1786
1787 stats[ST_F_QTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.q_time, TIME_STATS_SAMPLES));
1788 stats[ST_F_CTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.c_time, TIME_STATS_SAMPLES));
1789 stats[ST_F_RTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.d_time, TIME_STATS_SAMPLES));
1790 stats[ST_F_TTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.t_time, TIME_STATS_SAMPLES));
1791
Willy Tarreau708c4162019-10-09 10:19:16 +02001792 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001793 switch (addr_to_str(&sv->addr, str, sizeof(str))) {
1794 case AF_INET:
1795 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
Willy Tarreau04276f32017-01-06 17:41:29 +01001796 chunk_appendf(out, "%s:%d", str, sv->svc_port);
William Lallemand74c24fb2016-11-21 17:18:36 +01001797 break;
1798 case AF_INET6:
1799 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
Willy Tarreau04276f32017-01-06 17:41:29 +01001800 chunk_appendf(out, "[%s]:%d", str, sv->svc_port);
William Lallemand74c24fb2016-11-21 17:18:36 +01001801 break;
1802 case AF_UNIX:
1803 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, "unix");
1804 break;
1805 case -1:
1806 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1807 chunk_strcat(out, strerror(errno));
1808 break;
1809 default: /* address family not supported */
1810 break;
1811 }
1812
1813 if (sv->cookie)
1814 stats[ST_F_COOKIE] = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, sv->cookie);
1815 }
1816
1817 return 1;
1818}
1819
1820/* Dumps a line for server <sv> and proxy <px> to the trash and uses the state
Willy Tarreau43241ff2019-10-09 11:27:51 +02001821 * from stream interface <si>, and server state <state>. The caller is
1822 * responsible for clearing the trash if needed. Returns non-zero if it emits
1823 * anything, zero otherwise.
William Lallemand74c24fb2016-11-21 17:18:36 +01001824 */
Willy Tarreau43241ff2019-10-09 11:27:51 +02001825static int stats_dump_sv_stats(struct stream_interface *si, struct proxy *px, struct server *sv)
William Lallemand74c24fb2016-11-21 17:18:36 +01001826{
1827 struct appctx *appctx = __objt_appctx(si->end);
1828
Willy Tarreau43241ff2019-10-09 11:27:51 +02001829 if (!stats_fill_sv_stats(px, sv, appctx->ctx.stats.flags, stats, ST_F_TOTAL_FIELDS))
William Lallemand74c24fb2016-11-21 17:18:36 +01001830 return 0;
1831
Willy Tarreau43241ff2019-10-09 11:27:51 +02001832 return stats_dump_one_line(stats, px, appctx);
William Lallemand74c24fb2016-11-21 17:18:36 +01001833}
1834
1835/* Fill <stats> with the backend statistics. <stats> is
1836 * preallocated array of length <len>. The length of the array
1837 * must be at least ST_F_TOTAL_FIELDS. If this length is less
1838 * then this value, the function returns 0, otherwise, it
Willy Tarreau708c4162019-10-09 10:19:16 +02001839 * returns 1. <flags> can take the value STAT_SHLGNDS.
William Lallemand74c24fb2016-11-21 17:18:36 +01001840 */
1841int stats_fill_be_stats(struct proxy *px, int flags, struct field *stats, int len)
1842{
1843 if (len < ST_F_TOTAL_FIELDS)
1844 return 0;
1845
1846 memset(stats, 0, sizeof(*stats) * len);
1847
1848 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1849 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, "BACKEND");
1850 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1851 stats[ST_F_QCUR] = mkf_u32(0, px->nbpend);
1852 stats[ST_F_QMAX] = mkf_u32(FN_MAX, px->be_counters.nbpend_max);
Thierry FOURNIER0ff98a42016-12-19 16:50:42 +01001853 stats[ST_F_SCUR] = mkf_u32(0, px->beconn);
William Lallemand74c24fb2016-11-21 17:18:36 +01001854 stats[ST_F_SMAX] = mkf_u32(FN_MAX, px->be_counters.conn_max);
1855 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, px->fullconn);
1856 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, px->be_counters.cum_conn);
1857 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, px->be_counters.bytes_in);
1858 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, px->be_counters.bytes_out);
1859 stats[ST_F_DREQ] = mkf_u64(FN_COUNTER, px->be_counters.denied_req);
1860 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, px->be_counters.denied_resp);
1861 stats[ST_F_ECON] = mkf_u64(FN_COUNTER, px->be_counters.failed_conns);
1862 stats[ST_F_ERESP] = mkf_u64(FN_COUNTER, px->be_counters.failed_resp);
1863 stats[ST_F_WRETR] = mkf_u64(FN_COUNTER, px->be_counters.retries);
1864 stats[ST_F_WREDIS] = mkf_u64(FN_COUNTER, px->be_counters.redispatches);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001865 stats[ST_F_WREW] = mkf_u64(FN_COUNTER, px->be_counters.failed_rewrites);
Willy Tarreauf1573842018-12-14 11:35:36 +01001866 stats[ST_F_CONNECT] = mkf_u64(FN_COUNTER, px->be_counters.connect);
1867 stats[ST_F_REUSE] = mkf_u64(FN_COUNTER, px->be_counters.reuse);
William Lallemand74c24fb2016-11-21 17:18:36 +01001868 stats[ST_F_STATUS] = mkf_str(FO_STATUS, (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" : "DOWN");
1869 stats[ST_F_WEIGHT] = mkf_u32(FN_AVG, (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv);
1870 stats[ST_F_ACT] = mkf_u32(0, px->srv_act);
1871 stats[ST_F_BCK] = mkf_u32(0, px->srv_bck);
1872 stats[ST_F_CHKDOWN] = mkf_u64(FN_COUNTER, px->down_trans);
1873 stats[ST_F_LASTCHG] = mkf_u32(FN_AGE, now.tv_sec - px->last_change);
1874 if (px->srv)
1875 stats[ST_F_DOWNTIME] = mkf_u32(FN_COUNTER, be_downtime(px));
1876
1877 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
1878 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1879 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, 0);
1880 stats[ST_F_LBTOT] = mkf_u64(FN_COUNTER, px->be_counters.cum_lbconn);
1881 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_BE);
1882 stats[ST_F_RATE] = mkf_u32(0, read_freq_ctr(&px->be_sess_per_sec));
1883 stats[ST_F_RATE_MAX] = mkf_u32(0, px->be_counters.sps_max);
1884
Willy Tarreau708c4162019-10-09 10:19:16 +02001885 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001886 if (px->cookie_name)
1887 stats[ST_F_COOKIE] = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, px->cookie_name);
1888 stats[ST_F_ALGO] = mkf_str(FO_CONFIG|FS_SERVICE, backend_lb_algo_str(px->lbprm.algo & BE_LB_ALGO));
1889 }
1890
1891 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
1892 if (px->mode == PR_MODE_HTTP) {
1893 stats[ST_F_REQ_TOT] = mkf_u64(FN_COUNTER, px->be_counters.p.http.cum_req);
1894 stats[ST_F_HRSP_1XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[1]);
1895 stats[ST_F_HRSP_2XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[2]);
1896 stats[ST_F_HRSP_3XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[3]);
1897 stats[ST_F_HRSP_4XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[4]);
1898 stats[ST_F_HRSP_5XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[5]);
1899 stats[ST_F_HRSP_OTHER] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[0]);
Willy Tarreaua1214a52018-12-14 14:00:25 +01001900 stats[ST_F_CACHE_LOOKUPS] = mkf_u64(FN_COUNTER, px->be_counters.p.http.cache_lookups);
1901 stats[ST_F_CACHE_HITS] = mkf_u64(FN_COUNTER, px->be_counters.p.http.cache_hits);
William Lallemand74c24fb2016-11-21 17:18:36 +01001902 }
1903
1904 stats[ST_F_CLI_ABRT] = mkf_u64(FN_COUNTER, px->be_counters.cli_aborts);
1905 stats[ST_F_SRV_ABRT] = mkf_u64(FN_COUNTER, px->be_counters.srv_aborts);
1906
1907 /* compression: in, out, bypassed, responses */
1908 stats[ST_F_COMP_IN] = mkf_u64(FN_COUNTER, px->be_counters.comp_in);
1909 stats[ST_F_COMP_OUT] = mkf_u64(FN_COUNTER, px->be_counters.comp_out);
1910 stats[ST_F_COMP_BYP] = mkf_u64(FN_COUNTER, px->be_counters.comp_byp);
1911 stats[ST_F_COMP_RSP] = mkf_u64(FN_COUNTER, px->be_counters.p.http.comp_rsp);
1912 stats[ST_F_LASTSESS] = mkf_s32(FN_AGE, be_lastsession(px));
1913
1914 stats[ST_F_QTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.q_time, TIME_STATS_SAMPLES));
1915 stats[ST_F_CTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.c_time, TIME_STATS_SAMPLES));
1916 stats[ST_F_RTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.d_time, TIME_STATS_SAMPLES));
1917 stats[ST_F_TTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.t_time, TIME_STATS_SAMPLES));
1918
1919 return 1;
1920}
1921
1922/* Dumps a line for backend <px> to the trash for and uses the state from stream
Willy Tarreau43241ff2019-10-09 11:27:51 +02001923 * interface <si>. The caller is responsible for clearing the trash if needed.
1924 * Returns non-zero if it emits anything, zero otherwise.
William Lallemand74c24fb2016-11-21 17:18:36 +01001925 */
Willy Tarreau43241ff2019-10-09 11:27:51 +02001926static int stats_dump_be_stats(struct stream_interface *si, struct proxy *px)
William Lallemand74c24fb2016-11-21 17:18:36 +01001927{
1928 struct appctx *appctx = __objt_appctx(si->end);
1929
1930 if (!(px->cap & PR_CAP_BE))
1931 return 0;
1932
1933 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_BE)))
1934 return 0;
1935
Willy Tarreau43241ff2019-10-09 11:27:51 +02001936 if (!stats_fill_be_stats(px, appctx->ctx.stats.flags, stats, ST_F_TOTAL_FIELDS))
William Lallemand74c24fb2016-11-21 17:18:36 +01001937 return 0;
1938
Willy Tarreau43241ff2019-10-09 11:27:51 +02001939 return stats_dump_one_line(stats, px, appctx);
William Lallemand74c24fb2016-11-21 17:18:36 +01001940}
1941
1942/* Dumps the HTML table header for proxy <px> to the trash for and uses the state from
1943 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
1944 * for clearing the trash if needed.
1945 */
Willy Tarreau676c29e2019-10-09 10:50:01 +02001946static void stats_dump_html_px_hdr(struct stream_interface *si, struct proxy *px)
William Lallemand74c24fb2016-11-21 17:18:36 +01001947{
1948 struct appctx *appctx = __objt_appctx(si->end);
1949 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
1950
1951 if (px->cap & PR_CAP_BE && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
1952 /* A form to enable/disable this proxy servers */
1953
1954 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
1955 scope_txt[0] = 0;
1956 if (appctx->ctx.stats.scope_len) {
Christopher Fauleted7a0662019-01-14 11:07:34 +01001957 const char *scope_ptr = stats_scope_ptr(appctx, si);
1958
William Lallemand74c24fb2016-11-21 17:18:36 +01001959 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Christopher Fauleted7a0662019-01-14 11:07:34 +01001960 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), scope_ptr, appctx->ctx.stats.scope_len);
William Lallemand74c24fb2016-11-21 17:18:36 +01001961 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
1962 }
1963
1964 chunk_appendf(&trash,
1965 "<form method=\"post\">");
1966 }
1967
1968 /* print a new table */
1969 chunk_appendf(&trash,
1970 "<table class=\"tbl\" width=\"100%%\">\n"
1971 "<tr class=\"titre\">"
1972 "<th class=\"pxname\" width=\"10%%\">");
1973
1974 chunk_appendf(&trash,
1975 "<a name=\"%s\"></a>%s"
1976 "<a class=px href=\"#%s\">%s</a>",
1977 px->id,
Willy Tarreau676c29e2019-10-09 10:50:01 +02001978 (appctx->ctx.stats.flags & STAT_SHLGNDS) ? "<u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01001979 px->id, px->id);
1980
Willy Tarreau676c29e2019-10-09 10:50:01 +02001981 if (appctx->ctx.stats.flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001982 /* cap, mode, id */
1983 chunk_appendf(&trash, "<div class=tips>cap: %s, mode: %s, id: %d",
1984 proxy_cap_str(px->cap), proxy_mode_str(px->mode),
1985 px->uuid);
1986 chunk_appendf(&trash, "</div>");
1987 }
1988
1989 chunk_appendf(&trash,
1990 "%s</th>"
1991 "<th class=\"%s\" width=\"90%%\">%s</th>"
1992 "</tr>\n"
1993 "</table>\n"
1994 "<table class=\"tbl\" width=\"100%%\">\n"
1995 "<tr class=\"titre\">",
Willy Tarreau676c29e2019-10-09 10:50:01 +02001996 (appctx->ctx.stats.flags & STAT_SHLGNDS) ? "</u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01001997 px->desc ? "desc" : "empty", px->desc ? px->desc : "");
1998
1999 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
2000 /* Column heading for Enable or Disable server */
David Harrigand3db35a2016-12-30 12:12:49 +00002001 chunk_appendf(&trash,
2002 "<th rowspan=2 width=1><input type=\"checkbox\" \
2003 onclick=\"for(c in document.getElementsByClassName('%s-checkbox')) \
2004 document.getElementsByClassName('%s-checkbox').item(c).checked = this.checked\"></th>",
2005 px->id,
2006 px->id);
William Lallemand74c24fb2016-11-21 17:18:36 +01002007 }
2008
2009 chunk_appendf(&trash,
2010 "<th rowspan=2></th>"
2011 "<th colspan=3>Queue</th>"
2012 "<th colspan=3>Session rate</th><th colspan=6>Sessions</th>"
2013 "<th colspan=2>Bytes</th><th colspan=2>Denied</th>"
2014 "<th colspan=3>Errors</th><th colspan=2>Warnings</th>"
2015 "<th colspan=9>Server</th>"
2016 "</tr>\n"
2017 "<tr class=\"titre\">"
2018 "<th>Cur</th><th>Max</th><th>Limit</th>"
2019 "<th>Cur</th><th>Max</th><th>Limit</th><th>Cur</th><th>Max</th>"
2020 "<th>Limit</th><th>Total</th><th>LbTot</th><th>Last</th><th>In</th><th>Out</th>"
2021 "<th>Req</th><th>Resp</th><th>Req</th><th>Conn</th>"
2022 "<th>Resp</th><th>Retr</th><th>Redis</th>"
2023 "<th>Status</th><th>LastChk</th><th>Wght</th><th>Act</th>"
2024 "<th>Bck</th><th>Chk</th><th>Dwn</th><th>Dwntme</th>"
2025 "<th>Thrtle</th>\n"
2026 "</tr>");
2027}
2028
2029/* Dumps the HTML table trailer for proxy <px> to the trash for and uses the state from
2030 * stream interface <si>. The caller is responsible for clearing the trash if needed.
2031 */
2032static void stats_dump_html_px_end(struct stream_interface *si, struct proxy *px)
2033{
2034 struct appctx *appctx = __objt_appctx(si->end);
2035 chunk_appendf(&trash, "</table>");
2036
2037 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
2038 /* close the form used to enable/disable this proxy servers */
2039 chunk_appendf(&trash,
2040 "Choose the action to perform on the checked servers : "
2041 "<select name=action>"
2042 "<option value=\"\"></option>"
2043 "<option value=\"ready\">Set state to READY</option>"
2044 "<option value=\"drain\">Set state to DRAIN</option>"
2045 "<option value=\"maint\">Set state to MAINT</option>"
2046 "<option value=\"dhlth\">Health: disable checks</option>"
2047 "<option value=\"ehlth\">Health: enable checks</option>"
2048 "<option value=\"hrunn\">Health: force UP</option>"
2049 "<option value=\"hnolb\">Health: force NOLB</option>"
2050 "<option value=\"hdown\">Health: force DOWN</option>"
2051 "<option value=\"dagent\">Agent: disable checks</option>"
2052 "<option value=\"eagent\">Agent: enable checks</option>"
2053 "<option value=\"arunn\">Agent: force UP</option>"
2054 "<option value=\"adown\">Agent: force DOWN</option>"
2055 "<option value=\"shutdown\">Kill Sessions</option>"
2056 "</select>"
2057 "<input type=\"hidden\" name=\"b\" value=\"#%d\">"
2058 "&nbsp;<input type=\"submit\" value=\"Apply\">"
2059 "</form>",
2060 px->uuid);
2061 }
2062
2063 chunk_appendf(&trash, "<p>\n");
2064}
2065
2066/*
2067 * Dumps statistics for a proxy. The output is sent to the stream interface's
2068 * input buffer. Returns 0 if it had to stop dumping data because of lack of
2069 * buffer space, or non-zero if everything completed. This function is used
2070 * both by the CLI and the HTTP entry points, and is able to dump the output
2071 * in HTML or CSV formats. If the later, <uri> must be NULL.
2072 */
Christopher Fauletef779222018-10-31 08:47:01 +01002073int stats_dump_proxy_to_buffer(struct stream_interface *si, struct htx *htx,
2074 struct proxy *px, struct uri_auth *uri)
William Lallemand74c24fb2016-11-21 17:18:36 +01002075{
2076 struct appctx *appctx = __objt_appctx(si->end);
2077 struct stream *s = si_strm(si);
2078 struct channel *rep = si_ic(si);
2079 struct server *sv, *svs; /* server and server-state, server-state=server or server->track */
2080 struct listener *l;
William Lallemand74c24fb2016-11-21 17:18:36 +01002081
2082 chunk_reset(&trash);
2083
2084 switch (appctx->ctx.stats.px_st) {
2085 case STAT_PX_ST_INIT:
2086 /* we are on a new proxy */
2087 if (uri && uri->scope) {
2088 /* we have a limited scope, we have to check the proxy name */
2089 struct stat_scope *scope;
2090 int len;
2091
2092 len = strlen(px->id);
2093 scope = uri->scope;
2094
2095 while (scope) {
2096 /* match exact proxy name */
2097 if (scope->px_len == len && !memcmp(px->id, scope->px_id, len))
2098 break;
2099
2100 /* match '.' which means 'self' proxy */
2101 if (!strcmp(scope->px_id, ".") && px == s->be)
2102 break;
2103 scope = scope->next;
2104 }
2105
2106 /* proxy name not found : don't dump anything */
2107 if (scope == NULL)
2108 return 1;
2109 }
2110
2111 /* if the user has requested a limited output and the proxy
2112 * name does not match, skip it.
2113 */
Christopher Fauleted7a0662019-01-14 11:07:34 +01002114 if (appctx->ctx.stats.scope_len) {
2115 const char *scope_ptr = stats_scope_ptr(appctx, si);
2116
2117 if (strnistr(px->id, strlen(px->id), scope_ptr, appctx->ctx.stats.scope_len) == NULL)
2118 return 1;
2119 }
William Lallemand74c24fb2016-11-21 17:18:36 +01002120
2121 if ((appctx->ctx.stats.flags & STAT_BOUND) &&
2122 (appctx->ctx.stats.iid != -1) &&
2123 (px->uuid != appctx->ctx.stats.iid))
2124 return 1;
2125
2126 appctx->ctx.stats.px_st = STAT_PX_ST_TH;
2127 /* fall through */
2128
2129 case STAT_PX_ST_TH:
2130 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
Willy Tarreau676c29e2019-10-09 10:50:01 +02002131 stats_dump_html_px_hdr(si, px);
Christopher Fauletef779222018-10-31 08:47:01 +01002132 if (!stats_putchk(rep, htx, &trash))
2133 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002134 }
2135
2136 appctx->ctx.stats.px_st = STAT_PX_ST_FE;
2137 /* fall through */
2138
2139 case STAT_PX_ST_FE:
2140 /* print the frontend */
2141 if (stats_dump_fe_stats(si, px)) {
Christopher Fauletef779222018-10-31 08:47:01 +01002142 if (!stats_putchk(rep, htx, &trash))
2143 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002144 }
2145
2146 appctx->ctx.stats.l = px->conf.listeners.n;
2147 appctx->ctx.stats.px_st = STAT_PX_ST_LI;
2148 /* fall through */
2149
2150 case STAT_PX_ST_LI:
2151 /* stats.l has been initialized above */
2152 for (; appctx->ctx.stats.l != &px->conf.listeners; appctx->ctx.stats.l = l->by_fe.n) {
Christopher Fauletef779222018-10-31 08:47:01 +01002153 if (htx) {
2154 if (htx_almost_full(htx))
2155 goto full;
2156 }
2157 else {
2158 if (buffer_almost_full(&rep->buf))
2159 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002160 }
2161
2162 l = LIST_ELEM(appctx->ctx.stats.l, struct listener *, by_fe);
2163 if (!l->counters)
2164 continue;
2165
2166 if (appctx->ctx.stats.flags & STAT_BOUND) {
2167 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SO)))
2168 break;
2169
2170 if (appctx->ctx.stats.sid != -1 && l->luid != appctx->ctx.stats.sid)
2171 continue;
2172 }
2173
2174 /* print the frontend */
Willy Tarreau43241ff2019-10-09 11:27:51 +02002175 if (stats_dump_li_stats(si, px, l)) {
Christopher Fauletef779222018-10-31 08:47:01 +01002176 if (!stats_putchk(rep, htx, &trash))
2177 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002178 }
2179 }
2180
2181 appctx->ctx.stats.sv = px->srv; /* may be NULL */
2182 appctx->ctx.stats.px_st = STAT_PX_ST_SV;
2183 /* fall through */
2184
2185 case STAT_PX_ST_SV:
2186 /* stats.sv has been initialized above */
2187 for (; appctx->ctx.stats.sv != NULL; appctx->ctx.stats.sv = sv->next) {
Christopher Fauletef779222018-10-31 08:47:01 +01002188 if (htx) {
2189 if (htx_almost_full(htx))
2190 goto full;
2191 }
2192 else {
2193 if (buffer_almost_full(&rep->buf))
2194 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002195 }
2196
2197 sv = appctx->ctx.stats.sv;
2198
2199 if (appctx->ctx.stats.flags & STAT_BOUND) {
2200 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SV)))
2201 break;
2202
2203 if (appctx->ctx.stats.sid != -1 && sv->puid != appctx->ctx.stats.sid)
2204 continue;
2205 }
2206
2207 svs = sv;
2208 while (svs->track)
2209 svs = svs->track;
2210
2211 /* do not report servers which are DOWN and not changing state */
2212 if ((appctx->ctx.stats.flags & STAT_HIDE_DOWN) &&
Emeric Brun52a91d32017-08-31 14:41:55 +02002213 ((sv->cur_admin & SRV_ADMF_MAINT) || /* server is in maintenance */
2214 (sv->cur_state == SRV_ST_STOPPED && /* server is down */
William Lallemand74c24fb2016-11-21 17:18:36 +01002215 (!((svs->agent.state | svs->check.state) & CHK_ST_ENABLED) ||
2216 ((svs->agent.state & CHK_ST_ENABLED) && !svs->agent.health) ||
2217 ((svs->check.state & CHK_ST_ENABLED) && !svs->check.health))))) {
2218 continue;
2219 }
2220
Willy Tarreau43241ff2019-10-09 11:27:51 +02002221 if (stats_dump_sv_stats(si, px, sv)) {
Christopher Fauletef779222018-10-31 08:47:01 +01002222 if (!stats_putchk(rep, htx, &trash))
2223 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002224 }
2225 } /* for sv */
2226
2227 appctx->ctx.stats.px_st = STAT_PX_ST_BE;
2228 /* fall through */
2229
2230 case STAT_PX_ST_BE:
2231 /* print the backend */
Willy Tarreau43241ff2019-10-09 11:27:51 +02002232 if (stats_dump_be_stats(si, px)) {
Christopher Fauletef779222018-10-31 08:47:01 +01002233 if (!stats_putchk(rep, htx, &trash))
2234 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002235 }
2236
2237 appctx->ctx.stats.px_st = STAT_PX_ST_END;
2238 /* fall through */
2239
2240 case STAT_PX_ST_END:
2241 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
2242 stats_dump_html_px_end(si, px);
Christopher Fauletef779222018-10-31 08:47:01 +01002243 if (!stats_putchk(rep, htx, &trash))
2244 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002245 }
2246
2247 appctx->ctx.stats.px_st = STAT_PX_ST_FIN;
2248 /* fall through */
2249
2250 case STAT_PX_ST_FIN:
2251 return 1;
2252
2253 default:
2254 /* unknown state, we should put an abort() here ! */
2255 return 1;
2256 }
Christopher Fauletef779222018-10-31 08:47:01 +01002257
2258 full:
2259 si_rx_room_blk(si);
2260 return 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01002261}
2262
2263/* Dumps the HTTP stats head block to the trash for and uses the per-uri
2264 * parameters <uri>. The caller is responsible for clearing the trash if needed.
2265 */
Willy Tarreau676c29e2019-10-09 10:50:01 +02002266static void stats_dump_html_head(struct appctx *appctx, struct uri_auth *uri)
William Lallemand74c24fb2016-11-21 17:18:36 +01002267{
2268 /* WARNING! This must fit in the first buffer !!! */
2269 chunk_appendf(&trash,
2270 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n"
2271 "\"http://www.w3.org/TR/html4/loose.dtd\">\n"
2272 "<html><head><title>Statistics Report for " PRODUCT_NAME "%s%s</title>\n"
2273 "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n"
2274 "<style type=\"text/css\"><!--\n"
2275 "body {"
2276 " font-family: arial, helvetica, sans-serif;"
2277 " font-size: 12px;"
2278 " font-weight: normal;"
2279 " color: black;"
2280 " background: white;"
2281 "}\n"
2282 "th,td {"
2283 " font-size: 10px;"
2284 "}\n"
2285 "h1 {"
2286 " font-size: x-large;"
2287 " margin-bottom: 0.5em;"
2288 "}\n"
2289 "h2 {"
2290 " font-family: helvetica, arial;"
2291 " font-size: x-large;"
2292 " font-weight: bold;"
2293 " font-style: italic;"
2294 " color: #6020a0;"
2295 " margin-top: 0em;"
2296 " margin-bottom: 0em;"
2297 "}\n"
2298 "h3 {"
2299 " font-family: helvetica, arial;"
2300 " font-size: 16px;"
2301 " font-weight: bold;"
2302 " color: #b00040;"
2303 " background: #e8e8d0;"
2304 " margin-top: 0em;"
2305 " margin-bottom: 0em;"
2306 "}\n"
2307 "li {"
2308 " margin-top: 0.25em;"
2309 " margin-right: 2em;"
2310 "}\n"
2311 ".hr {margin-top: 0.25em;"
2312 " border-color: black;"
2313 " border-bottom-style: solid;"
2314 "}\n"
2315 ".titre {background: #20D0D0;color: #000000; font-weight: bold; text-align: center;}\n"
2316 ".total {background: #20D0D0;color: #ffff80;}\n"
2317 ".frontend {background: #e8e8d0;}\n"
2318 ".socket {background: #d0d0d0;}\n"
2319 ".backend {background: #e8e8d0;}\n"
2320 ".active_down {background: #ff9090;}\n"
2321 ".active_going_up {background: #ffd020;}\n"
2322 ".active_going_down {background: #ffffa0;}\n"
2323 ".active_up {background: #c0ffc0;}\n"
2324 ".active_nolb {background: #20a0ff;}\n"
2325 ".active_draining {background: #20a0FF;}\n"
2326 ".active_no_check {background: #e0e0e0;}\n"
2327 ".backup_down {background: #ff9090;}\n"
2328 ".backup_going_up {background: #ff80ff;}\n"
2329 ".backup_going_down {background: #c060ff;}\n"
2330 ".backup_up {background: #b0d0ff;}\n"
2331 ".backup_nolb {background: #90b0e0;}\n"
2332 ".backup_draining {background: #cc9900;}\n"
2333 ".backup_no_check {background: #e0e0e0;}\n"
2334 ".maintain {background: #c07820;}\n"
2335 ".rls {letter-spacing: 0.2em; margin-right: 1px;}\n" /* right letter spacing (used for grouping digits) */
2336 "\n"
2337 "a.px:link {color: #ffff40; text-decoration: none;}"
2338 "a.px:visited {color: #ffff40; text-decoration: none;}"
2339 "a.px:hover {color: #ffffff; text-decoration: none;}"
2340 "a.lfsb:link {color: #000000; text-decoration: none;}"
2341 "a.lfsb:visited {color: #000000; text-decoration: none;}"
2342 "a.lfsb:hover {color: #505050; text-decoration: none;}"
2343 "\n"
2344 "table.tbl { border-collapse: collapse; border-style: none;}\n"
2345 "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"
2346 "table.tbl td.ac { text-align: center;}\n"
2347 "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}\n"
2348 "table.tbl th.pxname { background: #b00040; color: #ffff40; font-weight: bold; border-style: solid solid none solid; padding: 2px 3px; white-space: nowrap;}\n"
2349 "table.tbl th.empty { border-style: none; empty-cells: hide; background: white;}\n"
2350 "table.tbl th.desc { background: white; border-style: solid solid none solid; text-align: left; padding: 2px 3px;}\n"
2351 "\n"
2352 "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n"
2353 "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n"
2354 "table.lgd td.noborder { border-style: none; padding: 2px; white-space: nowrap;}\n"
2355 "table.det { border-collapse: collapse; border-style: none; }\n"
2356 "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"
2357 "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"
2358 "u {text-decoration:none; border-bottom: 1px dotted black;}\n"
2359 "div.tips {\n"
2360 " display:block;\n"
2361 " visibility:hidden;\n"
2362 " z-index:2147483647;\n"
2363 " position:absolute;\n"
2364 " padding:2px 4px 3px;\n"
2365 " background:#f0f060; color:#000000;\n"
2366 " border:1px solid #7040c0;\n"
2367 " white-space:nowrap;\n"
2368 " font-style:normal;font-size:11px;font-weight:normal;\n"
2369 " -moz-border-radius:3px;-webkit-border-radius:3px;border-radius:3px;\n"
2370 " -moz-box-shadow:gray 2px 2px 3px;-webkit-box-shadow:gray 2px 2px 3px;box-shadow:gray 2px 2px 3px;\n"
2371 "}\n"
2372 "u:hover div.tips {visibility:visible;}\n"
2373 "-->\n"
2374 "</style></head>\n",
Willy Tarreau676c29e2019-10-09 10:50:01 +02002375 (appctx->ctx.stats.flags & STAT_SHNODE) ? " on " : "",
2376 (appctx->ctx.stats.flags & STAT_SHNODE) ? (uri->node ? uri->node : global.node) : ""
William Lallemand74c24fb2016-11-21 17:18:36 +01002377 );
2378}
2379
2380/* Dumps the HTML stats information block to the trash for and uses the state from
2381 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
2382 * for clearing the trash if needed.
2383 */
2384static void stats_dump_html_info(struct stream_interface *si, struct uri_auth *uri)
2385{
2386 struct appctx *appctx = __objt_appctx(si->end);
2387 unsigned int up = (now.tv_sec - start_date.tv_sec);
2388 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
Christopher Fauleted7a0662019-01-14 11:07:34 +01002389 const char *scope_ptr = stats_scope_ptr(appctx, si);
Willy Tarreau1713c032019-05-23 12:23:55 +02002390 unsigned long long bps = (unsigned long long)read_freq_ctr(&global.out_32bps) * 32;
2391
2392 /* Turn the bytes per second to bits per second and take care of the
2393 * usual ethernet overhead in order to help figure how far we are from
2394 * interface saturation since it's the only case which usually matters.
2395 * For this we count the total size of an Ethernet frame on the wire
2396 * including preamble and IFG (1538) for the largest TCP segment it
2397 * transports (1448 with TCP timestamps). This is not valid for smaller
2398 * packets (under-estimated), but it gives a reasonably accurate
2399 * estimation of how far we are from uplink saturation.
2400 */
2401 bps = bps * 8 * 1538 / 1448;
William Lallemand74c24fb2016-11-21 17:18:36 +01002402
2403 /* WARNING! this has to fit the first packet too.
2404 * We are around 3.5 kB, add adding entries will
2405 * become tricky if we want to support 4kB buffers !
2406 */
2407 chunk_appendf(&trash,
2408 "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">"
2409 PRODUCT_NAME "%s</a></h1>\n"
2410 "<h2>Statistics Report for pid %d%s%s%s%s</h2>\n"
2411 "<hr width=\"100%%\" class=\"hr\">\n"
2412 "<h3>&gt; General process information</h3>\n"
2413 "<table border=0><tr><td align=\"left\" nowrap width=\"1%%\">\n"
Yves Lafon95317282018-02-26 11:10:37 +01002414 "<p><b>pid = </b> %d (process #%d, nbproc = %d, nbthread = %d)<br>\n"
William Lallemand74c24fb2016-11-21 17:18:36 +01002415 "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n"
2416 "<b>system limits:</b> memmax = %s%s; ulimit-n = %d<br>\n"
2417 "<b>maxsock = </b> %d; <b>maxconn = </b> %d; <b>maxpipes = </b> %d<br>\n"
Willy Tarreau1713c032019-05-23 12:23:55 +02002418 "current conns = %d; current pipes = %d/%d; conn rate = %d/sec; bit rate = %.3f %cbps<br>\n"
William Lallemand74c24fb2016-11-21 17:18:36 +01002419 "Running tasks: %d/%d; idle = %d %%<br>\n"
2420 "</td><td align=\"center\" nowrap>\n"
2421 "<table class=\"lgd\"><tr>\n"
2422 "<td class=\"active_up\">&nbsp;</td><td class=\"noborder\">active UP </td>"
2423 "<td class=\"backup_up\">&nbsp;</td><td class=\"noborder\">backup UP </td>"
2424 "</tr><tr>\n"
2425 "<td class=\"active_going_down\"></td><td class=\"noborder\">active UP, going down </td>"
2426 "<td class=\"backup_going_down\"></td><td class=\"noborder\">backup UP, going down </td>"
2427 "</tr><tr>\n"
2428 "<td class=\"active_going_up\"></td><td class=\"noborder\">active DOWN, going up </td>"
2429 "<td class=\"backup_going_up\"></td><td class=\"noborder\">backup DOWN, going up </td>"
2430 "</tr><tr>\n"
2431 "<td class=\"active_down\"></td><td class=\"noborder\">active or backup DOWN &nbsp;</td>"
2432 "<td class=\"active_no_check\"></td><td class=\"noborder\">not checked </td>"
2433 "</tr><tr>\n"
2434 "<td class=\"maintain\"></td><td class=\"noborder\" colspan=\"3\">active or backup DOWN for maintenance (MAINT) &nbsp;</td>"
2435 "</tr><tr>\n"
2436 "<td class=\"active_draining\"></td><td class=\"noborder\" colspan=\"3\">active or backup SOFT STOPPED for maintenance &nbsp;</td>"
2437 "</tr></table>\n"
2438 "Note: \"NOLB\"/\"DRAIN\" = UP with load-balancing disabled."
2439 "</td>"
2440 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
2441 "<b>Display option:</b><ul style=\"margin-top: 0.25em;\">"
2442 "",
Willy Tarreau676c29e2019-10-09 10:50:01 +02002443 (appctx->ctx.stats.flags & STAT_HIDEVER) ? "" : (stats_version_string),
2444 pid, (appctx->ctx.stats.flags & STAT_SHNODE) ? " on " : "",
2445 (appctx->ctx.stats.flags & STAT_SHNODE) ? (uri->node ? uri->node : global.node) : "",
2446 (appctx->ctx.stats.flags & STAT_SHDESC) ? ": " : "",
2447 (appctx->ctx.stats.flags & STAT_SHDESC) ? (uri->desc ? uri->desc : global.desc) : "",
Yves Lafon95317282018-02-26 11:10:37 +01002448 pid, relative_pid, global.nbproc, global.nbthread,
William Lallemand74c24fb2016-11-21 17:18:36 +01002449 up / 86400, (up % 86400) / 3600,
2450 (up % 3600) / 60, (up % 60),
2451 global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited",
2452 global.rlimit_memmax ? " MB" : "",
2453 global.rlimit_nofile,
2454 global.maxsock, global.maxconn, global.maxpipes,
2455 actconn, pipes_used, pipes_used+pipes_free, read_freq_ctr(&global.conn_per_sec),
Willy Tarreau1713c032019-05-23 12:23:55 +02002456 bps >= 1000000000UL ? (bps / 1000000000.0) : bps >= 1000000UL ? (bps / 1000000.0) : (bps / 1000.0),
2457 bps >= 1000000000UL ? 'G' : bps >= 1000000UL ? 'M' : 'k',
Willy Tarreau81036f22019-05-20 19:24:50 +02002458 tasks_run_queue_cur, nb_tasks_cur, ti->idle_pct
William Lallemand74c24fb2016-11-21 17:18:36 +01002459 );
2460
2461 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Christopher Fauleted7a0662019-01-14 11:07:34 +01002462 memcpy(scope_txt, scope_ptr, appctx->ctx.stats.scope_len);
William Lallemand74c24fb2016-11-21 17:18:36 +01002463 scope_txt[appctx->ctx.stats.scope_len] = '\0';
2464
2465 chunk_appendf(&trash,
2466 "<li><form method=\"GET\">Scope : <input value=\"%s\" name=\"" STAT_SCOPE_INPUT_NAME "\" size=\"8\" maxlength=\"%d\" tabindex=\"1\"/></form>\n",
2467 (appctx->ctx.stats.scope_len > 0) ? scope_txt : "",
2468 STAT_SCOPE_TXT_MAXLEN);
2469
2470 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
2471 scope_txt[0] = 0;
2472 if (appctx->ctx.stats.scope_len) {
2473 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Christopher Fauleted7a0662019-01-14 11:07:34 +01002474 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), scope_ptr, appctx->ctx.stats.scope_len);
William Lallemand74c24fb2016-11-21 17:18:36 +01002475 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
2476 }
2477
2478 if (appctx->ctx.stats.flags & STAT_HIDE_DOWN)
2479 chunk_appendf(&trash,
2480 "<li><a href=\"%s%s%s%s\">Show all servers</a><br>\n",
2481 uri->uri_prefix,
2482 "",
2483 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2484 scope_txt);
2485 else
2486 chunk_appendf(&trash,
2487 "<li><a href=\"%s%s%s%s\">Hide 'DOWN' servers</a><br>\n",
2488 uri->uri_prefix,
2489 ";up",
2490 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2491 scope_txt);
2492
2493 if (uri->refresh > 0) {
2494 if (appctx->ctx.stats.flags & STAT_NO_REFRESH)
2495 chunk_appendf(&trash,
2496 "<li><a href=\"%s%s%s%s\">Enable refresh</a><br>\n",
2497 uri->uri_prefix,
2498 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2499 "",
2500 scope_txt);
2501 else
2502 chunk_appendf(&trash,
2503 "<li><a href=\"%s%s%s%s\">Disable refresh</a><br>\n",
2504 uri->uri_prefix,
2505 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2506 ";norefresh",
2507 scope_txt);
2508 }
2509
2510 chunk_appendf(&trash,
2511 "<li><a href=\"%s%s%s%s\">Refresh now</a><br>\n",
2512 uri->uri_prefix,
2513 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2514 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2515 scope_txt);
2516
2517 chunk_appendf(&trash,
2518 "<li><a href=\"%s;csv%s%s\">CSV export</a><br>\n",
2519 uri->uri_prefix,
2520 (uri->refresh > 0) ? ";norefresh" : "",
2521 scope_txt);
2522
2523 chunk_appendf(&trash,
Christopher Faulet6338a082019-09-09 15:50:54 +02002524 "<li><a href=\"%s;json%s%s\">JSON export</a> (<a href=\"%s;json-schema\">schema</a>)<br>\n",
2525 uri->uri_prefix,
2526 (uri->refresh > 0) ? ";norefresh" : "",
2527 scope_txt, uri->uri_prefix);
2528
2529 chunk_appendf(&trash,
William Lallemand74c24fb2016-11-21 17:18:36 +01002530 "</ul></td>"
2531 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
2532 "<b>External resources:</b><ul style=\"margin-top: 0.25em;\">\n"
2533 "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>\n"
2534 "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>\n"
2535 "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>\n"
2536 "</ul>"
2537 "</td>"
2538 "</tr></table>\n"
2539 ""
2540 );
2541
2542 if (appctx->ctx.stats.st_code) {
2543 switch (appctx->ctx.stats.st_code) {
2544 case STAT_STATUS_DONE:
2545 chunk_appendf(&trash,
2546 "<p><div class=active_up>"
2547 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2548 "Action processed successfully."
2549 "</div>\n", uri->uri_prefix,
2550 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2551 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2552 scope_txt);
2553 break;
2554 case STAT_STATUS_NONE:
2555 chunk_appendf(&trash,
2556 "<p><div class=active_going_down>"
2557 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2558 "Nothing has changed."
2559 "</div>\n", uri->uri_prefix,
2560 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2561 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2562 scope_txt);
2563 break;
2564 case STAT_STATUS_PART:
2565 chunk_appendf(&trash,
2566 "<p><div class=active_going_down>"
2567 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2568 "Action partially processed.<br>"
2569 "Some server names are probably unknown or ambiguous (duplicated names in the backend)."
2570 "</div>\n", uri->uri_prefix,
2571 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2572 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2573 scope_txt);
2574 break;
2575 case STAT_STATUS_ERRP:
2576 chunk_appendf(&trash,
2577 "<p><div class=active_down>"
2578 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2579 "Action not processed because of invalid parameters."
2580 "<ul>"
2581 "<li>The action is maybe unknown.</li>"
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01002582 "<li>Invalid key parameter (empty or too long).</li>"
William Lallemand74c24fb2016-11-21 17:18:36 +01002583 "<li>The backend name is probably unknown or ambiguous (duplicated names).</li>"
2584 "<li>Some server names are probably unknown or ambiguous (duplicated names in the backend).</li>"
2585 "</ul>"
2586 "</div>\n", uri->uri_prefix,
2587 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2588 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2589 scope_txt);
2590 break;
2591 case STAT_STATUS_EXCD:
2592 chunk_appendf(&trash,
2593 "<p><div class=active_down>"
2594 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2595 "<b>Action not processed : the buffer couldn't store all the data.<br>"
2596 "You should retry with less servers at a time.</b>"
2597 "</div>\n", uri->uri_prefix,
2598 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2599 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2600 scope_txt);
2601 break;
2602 case STAT_STATUS_DENY:
2603 chunk_appendf(&trash,
2604 "<p><div class=active_down>"
2605 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2606 "<b>Action denied.</b>"
2607 "</div>\n", uri->uri_prefix,
2608 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2609 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2610 scope_txt);
2611 break;
Christopher Faulet3c2ecf72019-02-27 16:41:27 +01002612 case STAT_STATUS_IVAL:
2613 chunk_appendf(&trash,
2614 "<p><div class=active_down>"
2615 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2616 "<b>Invalid requests (unsupported method or chunked encoded request).</b>"
2617 "</div>\n", uri->uri_prefix,
2618 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2619 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2620 scope_txt);
2621 break;
William Lallemand74c24fb2016-11-21 17:18:36 +01002622 default:
2623 chunk_appendf(&trash,
2624 "<p><div class=active_no_check>"
2625 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2626 "Unexpected result."
2627 "</div>\n", uri->uri_prefix,
2628 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2629 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2630 scope_txt);
2631 }
2632 chunk_appendf(&trash, "<p>\n");
2633 }
2634}
2635
2636/* Dumps the HTML stats trailer block to the trash. The caller is responsible
2637 * for clearing the trash if needed.
2638 */
2639static void stats_dump_html_end()
2640{
2641 chunk_appendf(&trash, "</body></html>\n");
2642}
2643
Simon Horman05ee2132017-01-04 09:37:25 +01002644/* Dumps the stats JSON header to the trash buffer which. The caller is responsible
2645 * for clearing it if needed.
2646 */
2647static void stats_dump_json_header()
2648{
2649 chunk_strcat(&trash, "[");
2650}
2651
2652
2653/* Dumps the JSON stats trailer block to the trash. The caller is responsible
2654 * for clearing the trash if needed.
2655 */
2656static void stats_dump_json_end()
2657{
2658 chunk_strcat(&trash, "]");
2659}
2660
William Lallemand74c24fb2016-11-21 17:18:36 +01002661/* This function dumps statistics onto the stream interface's read buffer in
2662 * either CSV or HTML format. <uri> contains some HTML-specific parameters that
2663 * are ignored for CSV format (hence <uri> may be NULL there). It returns 0 if
2664 * it had to stop writing data and an I/O is needed, 1 if the dump is finished
2665 * and the stream must be closed, or -1 in case of any error. This function is
2666 * used by both the CLI and the HTTP handlers.
2667 */
Christopher Fauletef779222018-10-31 08:47:01 +01002668static int stats_dump_stat_to_buffer(struct stream_interface *si, struct htx *htx,
2669 struct uri_auth *uri)
William Lallemand74c24fb2016-11-21 17:18:36 +01002670{
2671 struct appctx *appctx = __objt_appctx(si->end);
2672 struct channel *rep = si_ic(si);
2673 struct proxy *px;
2674
2675 chunk_reset(&trash);
2676
2677 switch (appctx->st2) {
2678 case STAT_ST_INIT:
2679 appctx->st2 = STAT_ST_HEAD; /* let's start producing data */
2680 /* fall through */
2681
2682 case STAT_ST_HEAD:
2683 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
Willy Tarreau676c29e2019-10-09 10:50:01 +02002684 stats_dump_html_head(appctx, uri);
Christopher Faulet6338a082019-09-09 15:50:54 +02002685 else if (appctx->ctx.stats.flags & STAT_JSON_SCHM)
2686 stats_dump_json_schema(&trash);
Simon Horman05ee2132017-01-04 09:37:25 +01002687 else if (appctx->ctx.stats.flags & STAT_FMT_JSON)
Willy Tarreau9d7fb632017-04-11 07:53:04 +02002688 stats_dump_json_header();
William Lallemand74c24fb2016-11-21 17:18:36 +01002689 else if (!(appctx->ctx.stats.flags & STAT_FMT_TYPED))
2690 stats_dump_csv_header();
2691
Christopher Fauletef779222018-10-31 08:47:01 +01002692 if (!stats_putchk(rep, htx, &trash))
2693 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002694
Christopher Faulet6338a082019-09-09 15:50:54 +02002695 if (appctx->ctx.stats.flags & STAT_JSON_SCHM) {
2696 appctx->st2 = STAT_ST_FIN;
2697 return 1;
2698 }
William Lallemand74c24fb2016-11-21 17:18:36 +01002699 appctx->st2 = STAT_ST_INFO;
2700 /* fall through */
2701
2702 case STAT_ST_INFO:
2703 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
2704 stats_dump_html_info(si, uri);
Christopher Fauletef779222018-10-31 08:47:01 +01002705 if (!stats_putchk(rep, htx, &trash))
2706 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002707 }
2708
Olivier Houchardfbc74e82017-11-24 16:54:05 +01002709 appctx->ctx.stats.px = proxies_list;
William Lallemand74c24fb2016-11-21 17:18:36 +01002710 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
2711 appctx->st2 = STAT_ST_LIST;
2712 /* fall through */
2713
2714 case STAT_ST_LIST:
2715 /* dump proxies */
2716 while (appctx->ctx.stats.px) {
Christopher Fauletef779222018-10-31 08:47:01 +01002717 if (htx) {
2718 if (htx_almost_full(htx))
2719 goto full;
2720 }
2721 else {
2722 if (buffer_almost_full(&rep->buf))
2723 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002724 }
2725
2726 px = appctx->ctx.stats.px;
2727 /* skip the disabled proxies, global frontend and non-networked ones */
2728 if (px->state != PR_STSTOPPED && px->uuid > 0 && (px->cap & (PR_CAP_FE | PR_CAP_BE)))
Christopher Fauletef779222018-10-31 08:47:01 +01002729 if (stats_dump_proxy_to_buffer(si, htx, px, uri) == 0)
William Lallemand74c24fb2016-11-21 17:18:36 +01002730 return 0;
2731
2732 appctx->ctx.stats.px = px->next;
2733 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
2734 }
2735 /* here, we just have reached the last proxy */
2736
2737 appctx->st2 = STAT_ST_END;
2738 /* fall through */
2739
2740 case STAT_ST_END:
Simon Horman05ee2132017-01-04 09:37:25 +01002741 if (appctx->ctx.stats.flags & (STAT_FMT_HTML|STAT_FMT_JSON)) {
2742 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
2743 stats_dump_html_end();
2744 else
2745 stats_dump_json_end();
Christopher Fauletef779222018-10-31 08:47:01 +01002746 if (!stats_putchk(rep, htx, &trash))
2747 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002748 }
2749
2750 appctx->st2 = STAT_ST_FIN;
2751 /* fall through */
2752
2753 case STAT_ST_FIN:
2754 return 1;
2755
2756 default:
2757 /* unknown state ! */
2758 appctx->st2 = STAT_ST_FIN;
2759 return -1;
2760 }
Christopher Fauletef779222018-10-31 08:47:01 +01002761
2762 full:
2763 si_rx_room_blk(si);
2764 return 0;
2765
William Lallemand74c24fb2016-11-21 17:18:36 +01002766}
2767
2768/* We reached the stats page through a POST request. The appctx is
2769 * expected to have already been allocated by the caller.
2770 * Parse the posted data and enable/disable servers if necessary.
2771 * Returns 1 if request was parsed or zero if it needs more data.
2772 */
2773static int stats_process_http_post(struct stream_interface *si)
2774{
2775 struct stream *s = si_strm(si);
2776 struct appctx *appctx = objt_appctx(si->end);
2777
2778 struct proxy *px = NULL;
2779 struct server *sv = NULL;
2780
2781 char key[LINESIZE];
2782 int action = ST_ADM_ACTION_NONE;
2783 int reprocess = 0;
2784
2785 int total_servers = 0;
2786 int altered_servers = 0;
2787
2788 char *first_param, *cur_param, *next_param, *end_params;
2789 char *st_cur_param = NULL;
2790 char *st_next_param = NULL;
2791
Christopher Fauleta3618372018-12-13 21:59:56 +01002792 struct buffer *temp = get_trash_chunk();
William Lallemand74c24fb2016-11-21 17:18:36 +01002793
Christopher Fauletb7f88902019-07-15 21:56:43 +02002794 struct htx *htx = htxbuf(&s->req.buf);
2795 struct htx_blk *blk;
Christopher Fauleta3618372018-12-13 21:59:56 +01002796
Christopher Fauletb7f88902019-07-15 21:56:43 +02002797 /* we need more data */
2798 if (s->txn->req.msg_state < HTTP_MSG_DONE) {
2799 /* check if we can receive more */
2800 if (htx_free_data_space(htx) <= global.tune.maxrewrite) {
2801 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
2802 goto out;
Christopher Fauleta3618372018-12-13 21:59:56 +01002803 }
Christopher Fauletb7f88902019-07-15 21:56:43 +02002804 goto wait;
2805 }
Christopher Fauleta3618372018-12-13 21:59:56 +01002806
Christopher Fauletb7f88902019-07-15 21:56:43 +02002807 /* The request was fully received. Copy data */
2808 blk = htx_get_head_blk(htx);
2809 while (blk) {
2810 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Fauleta3618372018-12-13 21:59:56 +01002811
Christopher Fauletb7f88902019-07-15 21:56:43 +02002812 if (type == HTX_BLK_EOM || type == HTX_BLK_TLR || type == HTX_BLK_EOT)
2813 break;
2814 if (type == HTX_BLK_DATA) {
2815 struct ist v = htx_get_blk_value(htx, blk);
Christopher Fauleta3618372018-12-13 21:59:56 +01002816
Christopher Fauletb7f88902019-07-15 21:56:43 +02002817 if (!chunk_memcat(temp, v.ptr, v.len)) {
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01002818 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
2819 goto out;
2820 }
Christopher Fauleta3618372018-12-13 21:59:56 +01002821 }
Christopher Fauletb7f88902019-07-15 21:56:43 +02002822 blk = htx_get_next_blk(htx, blk);
William Lallemand74c24fb2016-11-21 17:18:36 +01002823 }
2824
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002825 first_param = temp->area;
Christopher Fauleta3618372018-12-13 21:59:56 +01002826 end_params = temp->area + temp->data;
William Lallemand74c24fb2016-11-21 17:18:36 +01002827 cur_param = next_param = end_params;
2828 *end_params = '\0';
2829
2830 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
2831
2832 /*
2833 * Parse the parameters in reverse order to only store the last value.
2834 * From the html form, the backend and the action are at the end.
2835 */
2836 while (cur_param > first_param) {
2837 char *value;
2838 int poffset, plen;
2839
2840 cur_param--;
2841
2842 if ((*cur_param == '&') || (cur_param == first_param)) {
2843 reprocess_servers:
2844 /* Parse the key */
2845 poffset = (cur_param != first_param ? 1 : 0);
2846 plen = next_param - cur_param + (cur_param == first_param ? 1 : 0);
2847 if ((plen > 0) && (plen <= sizeof(key))) {
2848 strncpy(key, cur_param + poffset, plen);
2849 key[plen - 1] = '\0';
2850 } else {
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01002851 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
William Lallemand74c24fb2016-11-21 17:18:36 +01002852 goto out;
2853 }
2854
2855 /* Parse the value */
2856 value = key;
2857 while (*value != '\0' && *value != '=') {
2858 value++;
2859 }
2860 if (*value == '=') {
2861 /* Ok, a value is found, we can mark the end of the key */
2862 *value++ = '\0';
2863 }
2864 if (url_decode(key) < 0 || url_decode(value) < 0)
2865 break;
2866
2867 /* Now we can check the key to see what to do */
2868 if (!px && (strcmp(key, "b") == 0)) {
2869 if ((px = proxy_be_by_name(value)) == NULL) {
2870 /* the backend name is unknown or ambiguous (duplicate names) */
2871 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
2872 goto out;
2873 }
2874 }
2875 else if (!action && (strcmp(key, "action") == 0)) {
2876 if (strcmp(value, "ready") == 0) {
2877 action = ST_ADM_ACTION_READY;
2878 }
2879 else if (strcmp(value, "drain") == 0) {
2880 action = ST_ADM_ACTION_DRAIN;
2881 }
2882 else if (strcmp(value, "maint") == 0) {
2883 action = ST_ADM_ACTION_MAINT;
2884 }
2885 else if (strcmp(value, "shutdown") == 0) {
2886 action = ST_ADM_ACTION_SHUTDOWN;
2887 }
2888 else if (strcmp(value, "dhlth") == 0) {
2889 action = ST_ADM_ACTION_DHLTH;
2890 }
2891 else if (strcmp(value, "ehlth") == 0) {
2892 action = ST_ADM_ACTION_EHLTH;
2893 }
2894 else if (strcmp(value, "hrunn") == 0) {
2895 action = ST_ADM_ACTION_HRUNN;
2896 }
2897 else if (strcmp(value, "hnolb") == 0) {
2898 action = ST_ADM_ACTION_HNOLB;
2899 }
2900 else if (strcmp(value, "hdown") == 0) {
2901 action = ST_ADM_ACTION_HDOWN;
2902 }
2903 else if (strcmp(value, "dagent") == 0) {
2904 action = ST_ADM_ACTION_DAGENT;
2905 }
2906 else if (strcmp(value, "eagent") == 0) {
2907 action = ST_ADM_ACTION_EAGENT;
2908 }
2909 else if (strcmp(value, "arunn") == 0) {
2910 action = ST_ADM_ACTION_ARUNN;
2911 }
2912 else if (strcmp(value, "adown") == 0) {
2913 action = ST_ADM_ACTION_ADOWN;
2914 }
2915 /* else these are the old supported methods */
2916 else if (strcmp(value, "disable") == 0) {
2917 action = ST_ADM_ACTION_DISABLE;
2918 }
2919 else if (strcmp(value, "enable") == 0) {
2920 action = ST_ADM_ACTION_ENABLE;
2921 }
2922 else if (strcmp(value, "stop") == 0) {
2923 action = ST_ADM_ACTION_STOP;
2924 }
2925 else if (strcmp(value, "start") == 0) {
2926 action = ST_ADM_ACTION_START;
2927 }
2928 else {
2929 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
2930 goto out;
2931 }
2932 }
2933 else if (strcmp(key, "s") == 0) {
2934 if (!(px && action)) {
2935 /*
2936 * Indicates that we'll need to reprocess the parameters
2937 * as soon as backend and action are known
2938 */
2939 if (!reprocess) {
2940 st_cur_param = cur_param;
2941 st_next_param = next_param;
2942 }
2943 reprocess = 1;
2944 }
2945 else if ((sv = findserver(px, value)) != NULL) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002946 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
William Lallemand74c24fb2016-11-21 17:18:36 +01002947 switch (action) {
2948 case ST_ADM_ACTION_DISABLE:
Emeric Brun52a91d32017-08-31 14:41:55 +02002949 if (!(sv->cur_admin & SRV_ADMF_FMAINT)) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002950 altered_servers++;
2951 total_servers++;
2952 srv_set_admin_flag(sv, SRV_ADMF_FMAINT, "'disable' on stats page");
2953 }
2954 break;
2955 case ST_ADM_ACTION_ENABLE:
Emeric Brun52a91d32017-08-31 14:41:55 +02002956 if (sv->cur_admin & SRV_ADMF_FMAINT) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002957 altered_servers++;
2958 total_servers++;
2959 srv_clr_admin_flag(sv, SRV_ADMF_FMAINT);
2960 }
2961 break;
2962 case ST_ADM_ACTION_STOP:
Emeric Brun52a91d32017-08-31 14:41:55 +02002963 if (!(sv->cur_admin & SRV_ADMF_FDRAIN)) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002964 srv_set_admin_flag(sv, SRV_ADMF_FDRAIN, "'stop' on stats page");
2965 altered_servers++;
2966 total_servers++;
2967 }
2968 break;
2969 case ST_ADM_ACTION_START:
Emeric Brun52a91d32017-08-31 14:41:55 +02002970 if (sv->cur_admin & SRV_ADMF_FDRAIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002971 srv_clr_admin_flag(sv, SRV_ADMF_FDRAIN);
2972 altered_servers++;
2973 total_servers++;
2974 }
2975 break;
2976 case ST_ADM_ACTION_DHLTH:
2977 if (sv->check.state & CHK_ST_CONFIGURED) {
2978 sv->check.state &= ~CHK_ST_ENABLED;
2979 altered_servers++;
2980 total_servers++;
2981 }
2982 break;
2983 case ST_ADM_ACTION_EHLTH:
2984 if (sv->check.state & CHK_ST_CONFIGURED) {
2985 sv->check.state |= CHK_ST_ENABLED;
2986 altered_servers++;
2987 total_servers++;
2988 }
2989 break;
2990 case ST_ADM_ACTION_HRUNN:
2991 if (!(sv->track)) {
2992 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02002993 srv_set_running(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01002994 altered_servers++;
2995 total_servers++;
2996 }
2997 break;
2998 case ST_ADM_ACTION_HNOLB:
2999 if (!(sv->track)) {
3000 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02003001 srv_set_stopping(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003002 altered_servers++;
3003 total_servers++;
3004 }
3005 break;
3006 case ST_ADM_ACTION_HDOWN:
3007 if (!(sv->track)) {
3008 sv->check.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02003009 srv_set_stopped(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003010 altered_servers++;
3011 total_servers++;
3012 }
3013 break;
3014 case ST_ADM_ACTION_DAGENT:
3015 if (sv->agent.state & CHK_ST_CONFIGURED) {
3016 sv->agent.state &= ~CHK_ST_ENABLED;
3017 altered_servers++;
3018 total_servers++;
3019 }
3020 break;
3021 case ST_ADM_ACTION_EAGENT:
3022 if (sv->agent.state & CHK_ST_CONFIGURED) {
3023 sv->agent.state |= CHK_ST_ENABLED;
3024 altered_servers++;
3025 total_servers++;
3026 }
3027 break;
3028 case ST_ADM_ACTION_ARUNN:
3029 if (sv->agent.state & CHK_ST_ENABLED) {
3030 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02003031 srv_set_running(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003032 altered_servers++;
3033 total_servers++;
3034 }
3035 break;
3036 case ST_ADM_ACTION_ADOWN:
3037 if (sv->agent.state & CHK_ST_ENABLED) {
3038 sv->agent.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02003039 srv_set_stopped(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003040 altered_servers++;
3041 total_servers++;
3042 }
3043 break;
3044 case ST_ADM_ACTION_READY:
3045 srv_adm_set_ready(sv);
3046 altered_servers++;
3047 total_servers++;
3048 break;
3049 case ST_ADM_ACTION_DRAIN:
3050 srv_adm_set_drain(sv);
3051 altered_servers++;
3052 total_servers++;
3053 break;
3054 case ST_ADM_ACTION_MAINT:
3055 srv_adm_set_maint(sv);
3056 altered_servers++;
3057 total_servers++;
3058 break;
3059 case ST_ADM_ACTION_SHUTDOWN:
3060 if (px->state != PR_STSTOPPED) {
3061 struct stream *sess, *sess_bck;
3062
3063 list_for_each_entry_safe(sess, sess_bck, &sv->actconns, by_srv)
3064 if (sess->srv_conn == sv)
3065 stream_shutdown(sess, SF_ERR_KILLED);
3066
3067 altered_servers++;
3068 total_servers++;
3069 }
3070 break;
3071 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003072 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Lallemand74c24fb2016-11-21 17:18:36 +01003073 } else {
3074 /* the server name is unknown or ambiguous (duplicate names) */
3075 total_servers++;
3076 }
3077 }
3078 if (reprocess && px && action) {
3079 /* Now, we know the backend and the action chosen by the user.
3080 * We can safely restart from the first server parameter
3081 * to reprocess them
3082 */
3083 cur_param = st_cur_param;
3084 next_param = st_next_param;
3085 reprocess = 0;
3086 goto reprocess_servers;
3087 }
3088
3089 next_param = cur_param;
3090 }
3091 }
3092
3093 if (total_servers == 0) {
3094 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
3095 }
3096 else if (altered_servers == 0) {
3097 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
3098 }
3099 else if (altered_servers == total_servers) {
3100 appctx->ctx.stats.st_code = STAT_STATUS_DONE;
3101 }
3102 else {
3103 appctx->ctx.stats.st_code = STAT_STATUS_PART;
3104 }
3105 out:
3106 return 1;
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01003107 wait:
3108 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
3109 return 0;
Christopher Fauletef779222018-10-31 08:47:01 +01003110}
3111
3112
Christopher Fauletb7f88902019-07-15 21:56:43 +02003113static int stats_send_http_headers(struct stream_interface *si, struct htx *htx)
Christopher Fauletef779222018-10-31 08:47:01 +01003114{
3115 struct stream *s = si_strm(si);
3116 struct uri_auth *uri = s->be->uri_auth;
3117 struct appctx *appctx = __objt_appctx(si->end);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003118 struct htx_sl *sl;
3119 unsigned int flags;
Christopher Fauletef779222018-10-31 08:47:01 +01003120
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003121 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);
3122 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist("200"), ist("OK"));
3123 if (!sl)
Christopher Fauletef779222018-10-31 08:47:01 +01003124 goto full;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003125 sl->info.res.status = 200;
Christopher Fauletef779222018-10-31 08:47:01 +01003126
Christopher Fauletb829f4c2019-03-29 16:13:55 +01003127 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")))
Christopher Fauletef779222018-10-31 08:47:01 +01003128 goto full;
3129 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
3130 if (!htx_add_header(htx, ist("Content-Type"), ist("text/html")))
3131 goto full;
3132 }
Christopher Faulet6338a082019-09-09 15:50:54 +02003133 else if (appctx->ctx.stats.flags & (STAT_FMT_JSON|STAT_JSON_SCHM)) {
3134 if (!htx_add_header(htx, ist("Content-Type"), ist("application/json")))
3135 goto full;
3136 }
Christopher Fauletef779222018-10-31 08:47:01 +01003137 else {
3138 if (!htx_add_header(htx, ist("Content-Type"), ist("text/plain")))
3139 goto full;
3140 }
3141
3142 if (uri->refresh > 0 && !(appctx->ctx.stats.flags & STAT_NO_REFRESH)) {
3143 const char *refresh = U2A(uri->refresh);
3144 if (!htx_add_header(htx, ist("Refresh"), ist2(refresh, strlen(refresh))))
3145 goto full;
3146 }
3147
3148 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
3149 if (!htx_add_header(htx, ist("Transfer-Encoding"), ist("chunked")))
3150 goto full;
3151 }
3152
3153 if (!htx_add_endof(htx, HTX_BLK_EOH))
3154 goto full;
3155
Christopher Faulet1e2d6362019-02-27 16:28:48 +01003156 channel_add_input(&s->res, htx->data);
Christopher Fauletef779222018-10-31 08:47:01 +01003157 return 1;
3158
3159 full:
3160 htx_reset(htx);
3161 si_rx_room_blk(si);
3162 return 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01003163}
3164
Christopher Fauletef779222018-10-31 08:47:01 +01003165
Christopher Fauletb7f88902019-07-15 21:56:43 +02003166static int stats_send_http_redirect(struct stream_interface *si, struct htx *htx)
Christopher Fauletef779222018-10-31 08:47:01 +01003167{
3168 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
3169 struct stream *s = si_strm(si);
3170 struct uri_auth *uri = s->be->uri_auth;
3171 struct appctx *appctx = __objt_appctx(si->end);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003172 struct htx_sl *sl;
3173 unsigned int flags;
Christopher Fauletef779222018-10-31 08:47:01 +01003174
3175 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
3176 scope_txt[0] = 0;
3177 if (appctx->ctx.stats.scope_len) {
Christopher Fauleted7a0662019-01-14 11:07:34 +01003178 const char *scope_ptr = stats_scope_ptr(appctx, si);
3179
Christopher Fauletef779222018-10-31 08:47:01 +01003180 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Christopher Fauleted7a0662019-01-14 11:07:34 +01003181 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), scope_ptr, appctx->ctx.stats.scope_len);
Christopher Fauletef779222018-10-31 08:47:01 +01003182 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
3183 }
3184
3185 /* We don't want to land on the posted stats page because a refresh will
3186 * repost the data. We don't want this to happen on accident so we redirect
3187 * the browse to the stats page with a GET.
3188 */
3189 chunk_printf(&trash, "%s;st=%s%s%s%s",
3190 uri->uri_prefix,
3191 ((appctx->ctx.stats.st_code > STAT_STATUS_INIT) &&
3192 (appctx->ctx.stats.st_code < STAT_STATUS_SIZE) &&
3193 stat_status_codes[appctx->ctx.stats.st_code]) ?
3194 stat_status_codes[appctx->ctx.stats.st_code] :
3195 stat_status_codes[STAT_STATUS_UNKN],
3196 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3197 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3198 scope_txt);
3199
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003200 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_CHNK);
3201 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist("303"), ist("See Other"));
3202 if (!sl)
Christopher Fauletef779222018-10-31 08:47:01 +01003203 goto full;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003204 sl->info.res.status = 303;
Christopher Fauletef779222018-10-31 08:47:01 +01003205
3206 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")) ||
Christopher Fauletef779222018-10-31 08:47:01 +01003207 !htx_add_header(htx, ist("Content-Type"), ist("text/plain")) ||
3208 !htx_add_header(htx, ist("Content-Length"), ist("0")) ||
3209 !htx_add_header(htx, ist("Location"), ist2(trash.area, trash.data)))
3210 goto full;
3211
3212 if (!htx_add_endof(htx, HTX_BLK_EOH))
3213 goto full;
3214
Christopher Faulet1e2d6362019-02-27 16:28:48 +01003215 channel_add_input(&s->res, htx->data);
Christopher Fauletef779222018-10-31 08:47:01 +01003216 return 1;
3217
3218full:
3219 htx_reset(htx);
3220 si_rx_room_blk(si);
3221 return 0;
3222}
William Lallemand74c24fb2016-11-21 17:18:36 +01003223
Simon Horman05ee2132017-01-04 09:37:25 +01003224
William Lallemand74c24fb2016-11-21 17:18:36 +01003225/* This I/O handler runs as an applet embedded in a stream interface. It is
3226 * used to send HTTP stats over a TCP socket. The mechanism is very simple.
3227 * appctx->st0 contains the operation in progress (dump, done). The handler
3228 * automatically unregisters itself once transfer is complete.
3229 */
Christopher Fauletb7f88902019-07-15 21:56:43 +02003230static void http_stats_io_handler(struct appctx *appctx)
Christopher Fauletef779222018-10-31 08:47:01 +01003231{
3232 struct stream_interface *si = appctx->owner;
3233 struct stream *s = si_strm(si);
3234 struct channel *req = si_oc(si);
3235 struct channel *res = si_ic(si);
3236 struct htx *req_htx, *res_htx;
3237
3238 res_htx = htx_from_buf(&res->buf);
3239
3240 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
3241 goto out;
3242
3243 /* Check if the input buffer is avalaible. */
3244 if (!b_size(&res->buf)) {
3245 si_rx_room_blk(si);
3246 goto out;
3247 }
3248
3249 /* check that the output is not closed */
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003250 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_SHUTR))
3251 appctx->st0 = STAT_HTTP_END;
Christopher Fauletef779222018-10-31 08:47:01 +01003252
3253 /* all states are processed in sequence */
3254 if (appctx->st0 == STAT_HTTP_HEAD) {
Christopher Fauletb7f88902019-07-15 21:56:43 +02003255 if (stats_send_http_headers(si, res_htx)) {
Christopher Fauletef779222018-10-31 08:47:01 +01003256 if (s->txn->meth == HTTP_METH_HEAD)
3257 appctx->st0 = STAT_HTTP_DONE;
3258 else
3259 appctx->st0 = STAT_HTTP_DUMP;
3260 }
3261 }
3262
3263 if (appctx->st0 == STAT_HTTP_DUMP) {
3264 if (stats_dump_stat_to_buffer(si, res_htx, s->be->uri_auth))
3265 appctx->st0 = STAT_HTTP_DONE;
3266 }
3267
3268 if (appctx->st0 == STAT_HTTP_POST) {
3269 if (stats_process_http_post(si))
3270 appctx->st0 = STAT_HTTP_LAST;
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003271 else if (req->flags & CF_SHUTR)
Christopher Fauletef779222018-10-31 08:47:01 +01003272 appctx->st0 = STAT_HTTP_DONE;
3273 }
3274
3275 if (appctx->st0 == STAT_HTTP_LAST) {
Christopher Fauletb7f88902019-07-15 21:56:43 +02003276 if (stats_send_http_redirect(si, res_htx))
Christopher Fauletef779222018-10-31 08:47:01 +01003277 appctx->st0 = STAT_HTTP_DONE;
3278 }
3279
3280 if (appctx->st0 == STAT_HTTP_DONE) {
Christopher Faulet54b5e212019-06-04 10:08:28 +02003281 /* Don't add TLR because mux-h1 will take care of it */
Christopher Fauletef779222018-10-31 08:47:01 +01003282 if (!htx_add_endof(res_htx, HTX_BLK_EOM)) {
3283 si_rx_room_blk(si);
3284 goto out;
3285 }
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003286 channel_add_input(&s->res, 1);
3287 appctx->st0 = STAT_HTTP_END;
Christopher Fauletef779222018-10-31 08:47:01 +01003288 }
3289
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003290 if (appctx->st0 == STAT_HTTP_END) {
3291 if (!(res->flags & CF_SHUTR)) {
Christopher Fauletef779222018-10-31 08:47:01 +01003292 res->flags |= CF_READ_NULL;
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003293 si_shutr(si);
3294 }
3295
3296 /* eat the whole request */
3297 if (co_data(req)) {
3298 req_htx = htx_from_buf(&req->buf);
3299 co_htx_skip(req, req_htx, co_data(req));
3300 htx_to_buf(req_htx, &req->buf);
Christopher Fauletef779222018-10-31 08:47:01 +01003301 }
3302 }
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003303
Christopher Fauletef779222018-10-31 08:47:01 +01003304 out:
3305 /* we have left the request in the buffer for the case where we
3306 * process a POST, and this automatically re-enables activity on
3307 * read. It's better to indicate that we want to stop reading when
3308 * we're sending, so that we know there's at most one direction
3309 * deciding to wake the applet up. It saves it from looping when
3310 * emitting large blocks into small TCP windows.
3311 */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003312 htx_to_buf(res_htx, &res->buf);
3313 if (!channel_is_empty(res))
Christopher Fauletef779222018-10-31 08:47:01 +01003314 si_stop_get(si);
Christopher Fauletef779222018-10-31 08:47:01 +01003315}
3316
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003317/* Dump all fields from <info> into <out> using the "show info" format (name: value) */
Willy Tarreau83061a82018-07-13 11:56:34 +02003318static int stats_dump_info_fields(struct buffer *out,
Willy Tarreau43241ff2019-10-09 11:27:51 +02003319 const struct field *info, unsigned int flags)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003320{
3321 int field;
3322
3323 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
3324 if (!field_format(info, field))
3325 continue;
3326
3327 if (!chunk_appendf(out, "%s: ", info_field_names[field]))
3328 return 0;
3329 if (!stats_emit_raw_data_field(out, &info[field]))
3330 return 0;
3331 if (!chunk_strcat(out, "\n"))
3332 return 0;
3333 }
3334 return 1;
3335}
3336
3337/* Dump all fields from <info> into <out> using the "show info typed" format */
Willy Tarreau83061a82018-07-13 11:56:34 +02003338static int stats_dump_typed_info_fields(struct buffer *out,
Willy Tarreau43241ff2019-10-09 11:27:51 +02003339 const struct field *info, unsigned int flags)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003340{
3341 int field;
3342
3343 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
3344 if (!field_format(info, field))
3345 continue;
3346
3347 if (!chunk_appendf(out, "%d.%s.%u:", field, info_field_names[field], info[INF_PROCESS_NUM].u.u32))
3348 return 0;
3349 if (!stats_emit_field_tags(out, &info[field], ':'))
3350 return 0;
3351 if (!stats_emit_typed_data_field(out, &info[field]))
3352 return 0;
3353 if (!chunk_strcat(out, "\n"))
3354 return 0;
3355 }
3356 return 1;
3357}
3358
3359/* Fill <info> with HAProxy global info. <info> is preallocated
3360 * array of length <len>. The length of the aray must be
3361 * INF_TOTAL_FIELDS. If this length is less then this value, the
3362 * function returns 0, otherwise, it returns 1.
3363 */
3364int stats_fill_info(struct field *info, int len)
3365{
3366 unsigned int up = (now.tv_sec - start_date.tv_sec);
Willy Tarreau83061a82018-07-13 11:56:34 +02003367 struct buffer *out = get_trash_chunk();
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003368
3369#ifdef USE_OPENSSL
3370 int ssl_sess_rate = read_freq_ctr(&global.ssl_per_sec);
3371 int ssl_key_rate = read_freq_ctr(&global.ssl_fe_keys_per_sec);
3372 int ssl_reuse = 0;
3373
3374 if (ssl_key_rate < ssl_sess_rate) {
3375 /* count the ssl reuse ratio and avoid overflows in both directions */
3376 ssl_reuse = 100 - (100 * ssl_key_rate + (ssl_sess_rate - 1) / 2) / ssl_sess_rate;
3377 }
3378#endif
3379
3380 if (len < INF_TOTAL_FIELDS)
3381 return 0;
3382
3383 chunk_reset(out);
3384 memset(info, 0, sizeof(*info) * len);
3385
3386 info[INF_NAME] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, PRODUCT_NAME);
Willy Tarreau909b9d82019-01-04 18:20:32 +01003387 info[INF_VERSION] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, haproxy_version);
3388 info[INF_RELEASE_DATE] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, haproxy_date);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003389
Yves Lafon95317282018-02-26 11:10:37 +01003390 info[INF_NBTHREAD] = mkf_u32(FO_CONFIG|FS_SERVICE, global.nbthread);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003391 info[INF_NBPROC] = mkf_u32(FO_CONFIG|FS_SERVICE, global.nbproc);
3392 info[INF_PROCESS_NUM] = mkf_u32(FO_KEY, relative_pid);
3393 info[INF_PID] = mkf_u32(FO_STATUS, pid);
3394
3395 info[INF_UPTIME] = mkf_str(FN_DURATION, chunk_newstr(out));
3396 chunk_appendf(out, "%ud %uh%02um%02us", up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60));
3397
3398 info[INF_UPTIME_SEC] = mkf_u32(FN_DURATION, up);
3399 info[INF_MEMMAX_MB] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_memmax);
3400 info[INF_POOL_ALLOC_MB] = mkf_u32(0, (unsigned)(pool_total_allocated() / 1048576L));
3401 info[INF_POOL_USED_MB] = mkf_u32(0, (unsigned)(pool_total_used() / 1048576L));
3402 info[INF_POOL_FAILED] = mkf_u32(FN_COUNTER, pool_total_failures());
3403 info[INF_ULIMIT_N] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_nofile);
3404 info[INF_MAXSOCK] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxsock);
3405 info[INF_MAXCONN] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxconn);
3406 info[INF_HARD_MAXCONN] = mkf_u32(FO_CONFIG|FN_LIMIT, global.hardmaxconn);
3407 info[INF_CURR_CONN] = mkf_u32(0, actconn);
3408 info[INF_CUM_CONN] = mkf_u32(FN_COUNTER, totalconn);
3409 info[INF_CUM_REQ] = mkf_u32(FN_COUNTER, global.req_count);
3410#ifdef USE_OPENSSL
3411 info[INF_MAX_SSL_CONNS] = mkf_u32(FN_MAX, global.maxsslconn);
3412 info[INF_CURR_SSL_CONNS] = mkf_u32(0, sslconns);
3413 info[INF_CUM_SSL_CONNS] = mkf_u32(FN_COUNTER, totalsslconns);
3414#endif
3415 info[INF_MAXPIPES] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxpipes);
3416 info[INF_PIPES_USED] = mkf_u32(0, pipes_used);
3417 info[INF_PIPES_FREE] = mkf_u32(0, pipes_free);
3418 info[INF_CONN_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.conn_per_sec));
3419 info[INF_CONN_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.cps_lim);
3420 info[INF_MAX_CONN_RATE] = mkf_u32(FN_MAX, global.cps_max);
3421 info[INF_SESS_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.sess_per_sec));
3422 info[INF_SESS_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.sps_lim);
3423 info[INF_MAX_SESS_RATE] = mkf_u32(FN_RATE, global.sps_max);
3424
3425#ifdef USE_OPENSSL
3426 info[INF_SSL_RATE] = mkf_u32(FN_RATE, ssl_sess_rate);
3427 info[INF_SSL_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.ssl_lim);
3428 info[INF_MAX_SSL_RATE] = mkf_u32(FN_MAX, global.ssl_max);
3429 info[INF_SSL_FRONTEND_KEY_RATE] = mkf_u32(0, ssl_key_rate);
3430 info[INF_SSL_FRONTEND_MAX_KEY_RATE] = mkf_u32(FN_MAX, global.ssl_fe_keys_max);
3431 info[INF_SSL_FRONTEND_SESSION_REUSE_PCT] = mkf_u32(0, ssl_reuse);
3432 info[INF_SSL_BACKEND_KEY_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.ssl_be_keys_per_sec));
3433 info[INF_SSL_BACKEND_MAX_KEY_RATE] = mkf_u32(FN_MAX, global.ssl_be_keys_max);
3434 info[INF_SSL_CACHE_LOOKUPS] = mkf_u32(FN_COUNTER, global.shctx_lookups);
3435 info[INF_SSL_CACHE_MISSES] = mkf_u32(FN_COUNTER, global.shctx_misses);
3436#endif
3437 info[INF_COMPRESS_BPS_IN] = mkf_u32(FN_RATE, read_freq_ctr(&global.comp_bps_in));
3438 info[INF_COMPRESS_BPS_OUT] = mkf_u32(FN_RATE, read_freq_ctr(&global.comp_bps_out));
3439 info[INF_COMPRESS_BPS_RATE_LIM] = mkf_u32(FO_CONFIG|FN_LIMIT, global.comp_rate_lim);
3440#ifdef USE_ZLIB
3441 info[INF_ZLIB_MEM_USAGE] = mkf_u32(0, zlib_used_memory);
3442 info[INF_MAX_ZLIB_MEM_USAGE] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxzlibmem);
3443#endif
3444 info[INF_TASKS] = mkf_u32(0, nb_tasks_cur);
Christopher Faulet34c5cc92016-12-06 09:15:30 +01003445 info[INF_RUN_QUEUE] = mkf_u32(0, tasks_run_queue_cur);
Willy Tarreau81036f22019-05-20 19:24:50 +02003446 info[INF_IDLE_PCT] = mkf_u32(FN_AVG, ti->idle_pct);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003447 info[INF_NODE] = mkf_str(FO_CONFIG|FN_OUTPUT|FS_SERVICE, global.node);
3448 if (global.desc)
3449 info[INF_DESCRIPTION] = mkf_str(FO_CONFIG|FN_OUTPUT|FS_SERVICE, global.desc);
Willy Tarreau00098ea2018-11-05 14:38:13 +01003450 info[INF_STOPPING] = mkf_u32(0, stopping);
3451 info[INF_JOBS] = mkf_u32(0, jobs);
William Lallemanda7199262018-11-16 16:57:20 +01003452 info[INF_UNSTOPPABLE_JOBS] = mkf_u32(0, unstoppable_jobs);
Willy Tarreau00098ea2018-11-05 14:38:13 +01003453 info[INF_LISTENERS] = mkf_u32(0, listeners);
Willy Tarreau199ad242018-11-05 16:31:22 +01003454 info[INF_ACTIVE_PEERS] = mkf_u32(0, active_peers);
Willy Tarreau2d372c22018-11-05 17:12:27 +01003455 info[INF_CONNECTED_PEERS] = mkf_u32(0, connected_peers);
Willy Tarreau13ef7732018-11-12 07:25:28 +01003456 info[INF_DROPPED_LOGS] = mkf_u32(0, dropped_logs);
Willy Tarreaubeb859a2018-11-22 18:07:59 +01003457 info[INF_BUSY_POLLING] = mkf_u32(0, !!(global.tune.options & GTUNE_BUSY_POLLING));
Baptiste Assmann333939c2019-01-21 08:34:50 +01003458 info[INF_FAILED_RESOLUTIONS] = mkf_u32(0, dns_failed_resolutions);
Willy Tarreau7cf0e452019-05-23 11:39:14 +02003459 info[INF_TOTAL_BYTES_OUT] = mkf_u64(0, global.out_bytes);
3460 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 +01003461
3462 return 1;
3463}
3464
3465/* This function dumps information onto the stream interface's read buffer.
3466 * It returns 0 as long as it does not complete, non-zero upon completion.
3467 * No state is used.
3468 */
3469static int stats_dump_info_to_buffer(struct stream_interface *si)
3470{
3471 struct appctx *appctx = __objt_appctx(si->end);
3472
3473 if (!stats_fill_info(info, INF_TOTAL_FIELDS))
3474 return 0;
3475
3476 chunk_reset(&trash);
3477
3478 if (appctx->ctx.stats.flags & STAT_FMT_TYPED)
Willy Tarreau43241ff2019-10-09 11:27:51 +02003479 stats_dump_typed_info_fields(&trash, info, appctx->ctx.stats.flags);
Simon Horman05ee2132017-01-04 09:37:25 +01003480 else if (appctx->ctx.stats.flags & STAT_FMT_JSON)
Willy Tarreau43241ff2019-10-09 11:27:51 +02003481 stats_dump_json_info_fields(&trash, info, appctx->ctx.stats.flags);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003482 else
Willy Tarreau43241ff2019-10-09 11:27:51 +02003483 stats_dump_info_fields(&trash, info, appctx->ctx.stats.flags);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003484
Willy Tarreau06d80a92017-10-19 14:32:15 +02003485 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01003486 si_rx_room_blk(si);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003487 return 0;
3488 }
3489
3490 return 1;
3491}
3492
Simon Horman6f6bb382017-01-04 09:37:26 +01003493/* This function dumps the schema onto the stream interface's read buffer.
3494 * It returns 0 as long as it does not complete, non-zero upon completion.
3495 * No state is used.
3496 *
3497 * Integer values bouned to the range [-(2**53)+1, (2**53)-1] as
3498 * per the recommendation for interoperable integers in section 6 of RFC 7159.
3499 */
Willy Tarreau83061a82018-07-13 11:56:34 +02003500static void stats_dump_json_schema(struct buffer *out)
Simon Horman6f6bb382017-01-04 09:37:26 +01003501{
3502
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003503 int old_len = out->data;
Simon Horman6f6bb382017-01-04 09:37:26 +01003504
3505 chunk_strcat(out,
3506 "{"
3507 "\"$schema\":\"http://json-schema.org/draft-04/schema#\","
3508 "\"oneOf\":["
3509 "{"
3510 "\"title\":\"Info\","
3511 "\"type\":\"array\","
3512 "\"items\":{"
3513 "\"properties\":{"
3514 "\"title\":\"InfoItem\","
3515 "\"type\":\"object\","
3516 "\"field\":{\"$ref\":\"#/definitions/field\"},"
3517 "\"processNum\":{\"$ref\":\"#/definitions/processNum\"},"
3518 "\"tags\":{\"$ref\":\"#/definitions/tags\"},"
3519 "\"value\":{\"$ref\":\"#/definitions/typedValue\"}"
3520 "},"
3521 "\"required\":[\"field\",\"processNum\",\"tags\","
3522 "\"value\"]"
3523 "}"
3524 "},"
3525 "{"
3526 "\"title\":\"Stat\","
3527 "\"type\":\"array\","
3528 "\"items\":{"
3529 "\"title\":\"InfoItem\","
3530 "\"type\":\"object\","
3531 "\"properties\":{"
3532 "\"objType\":{"
3533 "\"enum\":[\"Frontend\",\"Backend\",\"Listener\","
3534 "\"Server\",\"Unknown\"]"
3535 "},"
3536 "\"proxyId\":{"
3537 "\"type\":\"integer\","
3538 "\"minimum\":0"
3539 "},"
3540 "\"id\":{"
3541 "\"type\":\"integer\","
3542 "\"minimum\":0"
3543 "},"
3544 "\"field\":{\"$ref\":\"#/definitions/field\"},"
3545 "\"processNum\":{\"$ref\":\"#/definitions/processNum\"},"
3546 "\"tags\":{\"$ref\":\"#/definitions/tags\"},"
3547 "\"typedValue\":{\"$ref\":\"#/definitions/typedValue\"}"
3548 "},"
3549 "\"required\":[\"objType\",\"proxyId\",\"id\","
3550 "\"field\",\"processNum\",\"tags\","
3551 "\"value\"]"
3552 "}"
3553 "},"
3554 "{"
3555 "\"title\":\"Error\","
3556 "\"type\":\"object\","
3557 "\"properties\":{"
3558 "\"errorStr\":{"
3559 "\"type\":\"string\""
3560 "},"
3561 "\"required\":[\"errorStr\"]"
3562 "}"
3563 "}"
3564 "],"
3565 "\"definitions\":{"
3566 "\"field\":{"
3567 "\"type\":\"object\","
3568 "\"pos\":{"
3569 "\"type\":\"integer\","
3570 "\"minimum\":0"
3571 "},"
3572 "\"name\":{"
3573 "\"type\":\"string\""
3574 "},"
3575 "\"required\":[\"pos\",\"name\"]"
3576 "},"
3577 "\"processNum\":{"
3578 "\"type\":\"integer\","
3579 "\"minimum\":1"
3580 "},"
3581 "\"tags\":{"
3582 "\"type\":\"object\","
3583 "\"origin\":{"
3584 "\"type\":\"string\","
3585 "\"enum\":[\"Metric\",\"Status\",\"Key\","
3586 "\"Config\",\"Product\",\"Unknown\"]"
3587 "},"
3588 "\"nature\":{"
3589 "\"type\":\"string\","
3590 "\"enum\":[\"Gauge\",\"Limit\",\"Min\",\"Max\","
3591 "\"Rate\",\"Counter\",\"Duration\","
3592 "\"Age\",\"Time\",\"Name\",\"Output\","
3593 "\"Avg\", \"Unknown\"]"
3594 "},"
3595 "\"scope\":{"
3596 "\"type\":\"string\","
3597 "\"enum\":[\"Cluster\",\"Process\",\"Service\","
3598 "\"System\",\"Unknown\"]"
3599 "},"
3600 "\"required\":[\"origin\",\"nature\",\"scope\"]"
3601 "},"
3602 "\"typedValue\":{"
3603 "\"type\":\"object\","
3604 "\"oneOf\":["
3605 "{\"$ref\":\"#/definitions/typedValue/definitions/s32Value\"},"
3606 "{\"$ref\":\"#/definitions/typedValue/definitions/s64Value\"},"
3607 "{\"$ref\":\"#/definitions/typedValue/definitions/u32Value\"},"
3608 "{\"$ref\":\"#/definitions/typedValue/definitions/u64Value\"},"
3609 "{\"$ref\":\"#/definitions/typedValue/definitions/strValue\"}"
3610 "],"
3611 "\"definitions\":{"
3612 "\"s32Value\":{"
3613 "\"properties\":{"
3614 "\"type\":{"
3615 "\"type\":\"string\","
3616 "\"enum\":[\"s32\"]"
3617 "},"
3618 "\"value\":{"
3619 "\"type\":\"integer\","
3620 "\"minimum\":-2147483648,"
3621 "\"maximum\":2147483647"
3622 "}"
3623 "},"
3624 "\"required\":[\"type\",\"value\"]"
3625 "},"
3626 "\"s64Value\":{"
3627 "\"properties\":{"
3628 "\"type\":{"
3629 "\"type\":\"string\","
3630 "\"enum\":[\"s64\"]"
3631 "},"
3632 "\"value\":{"
3633 "\"type\":\"integer\","
3634 "\"minimum\":-9007199254740991,"
3635 "\"maximum\":9007199254740991"
3636 "}"
3637 "},"
3638 "\"required\":[\"type\",\"value\"]"
3639 "},"
3640 "\"u32Value\":{"
3641 "\"properties\":{"
3642 "\"type\":{"
3643 "\"type\":\"string\","
3644 "\"enum\":[\"u32\"]"
3645 "},"
3646 "\"value\":{"
3647 "\"type\":\"integer\","
3648 "\"minimum\":0,"
3649 "\"maximum\":4294967295"
3650 "}"
3651 "},"
3652 "\"required\":[\"type\",\"value\"]"
3653 "},"
3654 "\"u64Value\":{"
3655 "\"properties\":{"
3656 "\"type\":{"
3657 "\"type\":\"string\","
3658 "\"enum\":[\"u64\"]"
3659 "},"
3660 "\"value\":{"
3661 "\"type\":\"integer\","
3662 "\"minimum\":0,"
3663 "\"maximum\":9007199254740991"
3664 "}"
3665 "},"
3666 "\"required\":[\"type\",\"value\"]"
3667 "},"
3668 "\"strValue\":{"
3669 "\"properties\":{"
3670 "\"type\":{"
3671 "\"type\":\"string\","
3672 "\"enum\":[\"str\"]"
3673 "},"
3674 "\"value\":{\"type\":\"string\"}"
3675 "},"
3676 "\"required\":[\"type\",\"value\"]"
3677 "},"
3678 "\"unknownValue\":{"
3679 "\"properties\":{"
3680 "\"type\":{"
3681 "\"type\":\"integer\","
3682 "\"minimum\":0"
3683 "},"
3684 "\"value\":{"
3685 "\"type\":\"string\","
3686 "\"enum\":[\"unknown\"]"
3687 "}"
3688 "},"
3689 "\"required\":[\"type\",\"value\"]"
3690 "}"
3691 "}"
3692 "}"
3693 "}"
3694 "}");
3695
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003696 if (old_len == out->data) {
Simon Horman6f6bb382017-01-04 09:37:26 +01003697 chunk_reset(out);
3698 chunk_appendf(out,
3699 "{\"errorStr\":\"output buffer too short\"}");
3700 }
3701}
3702
3703/* This function dumps the schema onto the stream interface's read buffer.
3704 * It returns 0 as long as it does not complete, non-zero upon completion.
3705 * No state is used.
3706 */
3707static int stats_dump_json_schema_to_buffer(struct stream_interface *si)
3708{
3709 chunk_reset(&trash);
3710
3711 stats_dump_json_schema(&trash);
3712
Willy Tarreau06d80a92017-10-19 14:32:15 +02003713 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01003714 si_rx_room_blk(si);
Simon Horman6f6bb382017-01-04 09:37:26 +01003715 return 0;
3716 }
3717
3718 return 1;
3719}
3720
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02003721static int cli_parse_clear_counters(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau89d467c2016-11-23 11:02:40 +01003722{
3723 struct proxy *px;
3724 struct server *sv;
3725 struct listener *li;
3726 int clrall = 0;
3727
3728 if (strcmp(args[2], "all") == 0)
3729 clrall = 1;
3730
3731 /* check permissions */
3732 if (!cli_has_level(appctx, ACCESS_LVL_OPER) ||
3733 (clrall && !cli_has_level(appctx, ACCESS_LVL_ADMIN)))
3734 return 1;
3735
Olivier Houchardfbc74e82017-11-24 16:54:05 +01003736 for (px = proxies_list; px; px = px->next) {
Willy Tarreau89d467c2016-11-23 11:02:40 +01003737 if (clrall) {
3738 memset(&px->be_counters, 0, sizeof(px->be_counters));
3739 memset(&px->fe_counters, 0, sizeof(px->fe_counters));
3740 }
3741 else {
3742 px->be_counters.conn_max = 0;
3743 px->be_counters.p.http.rps_max = 0;
3744 px->be_counters.sps_max = 0;
3745 px->be_counters.cps_max = 0;
3746 px->be_counters.nbpend_max = 0;
3747
3748 px->fe_counters.conn_max = 0;
3749 px->fe_counters.p.http.rps_max = 0;
3750 px->fe_counters.sps_max = 0;
3751 px->fe_counters.cps_max = 0;
Willy Tarreau89d467c2016-11-23 11:02:40 +01003752 }
3753
3754 for (sv = px->srv; sv; sv = sv->next)
3755 if (clrall)
3756 memset(&sv->counters, 0, sizeof(sv->counters));
3757 else {
3758 sv->counters.cur_sess_max = 0;
3759 sv->counters.nbpend_max = 0;
3760 sv->counters.sps_max = 0;
3761 }
3762
3763 list_for_each_entry(li, &px->conf.listeners, by_fe)
3764 if (li->counters) {
3765 if (clrall)
3766 memset(li->counters, 0, sizeof(*li->counters));
3767 else
3768 li->counters->conn_max = 0;
3769 }
3770 }
3771
3772 global.cps_max = 0;
3773 global.sps_max = 0;
Olivier Houchard00bc3cb2017-10-17 19:23:25 +02003774 global.ssl_max = 0;
3775 global.ssl_fe_keys_max = 0;
3776 global.ssl_be_keys_max = 0;
Willy Tarreaud80cb4e2018-01-20 19:30:13 +01003777
3778 memset(activity, 0, sizeof(activity));
Willy Tarreau89d467c2016-11-23 11:02:40 +01003779 return 1;
3780}
3781
3782
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02003783static int cli_parse_show_info(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003784{
Willy Tarreau2f397382019-10-09 11:43:59 +02003785 int arg = 2;
3786
Willy Tarreaud25fc792016-12-16 12:33:47 +01003787 appctx->ctx.stats.scope_str = 0;
3788 appctx->ctx.stats.scope_len = 0;
3789 appctx->ctx.stats.flags = 0;
3790
Willy Tarreau2f397382019-10-09 11:43:59 +02003791 while (*args[arg]) {
3792 if (strcmp(args[arg], "typed") == 0)
3793 appctx->ctx.stats.flags = (appctx->ctx.stats.flags & ~STAT_FMT_MASK) | STAT_FMT_TYPED;
3794 else if (strcmp(args[arg], "json") == 0)
3795 appctx->ctx.stats.flags = (appctx->ctx.stats.flags & ~STAT_FMT_MASK) | STAT_FMT_JSON;
3796 else if (strcmp(args[arg], "desc") == 0)
3797 appctx->ctx.stats.flags |= STAT_SHOW_FDESC;
3798 arg++;
3799 }
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003800 return 0;
3801}
3802
3803
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02003804static int cli_parse_show_stat(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau2b812e22016-11-22 16:18:05 +01003805{
Willy Tarreau2f397382019-10-09 11:43:59 +02003806 int arg = 2;
3807
Willy Tarreaud25fc792016-12-16 12:33:47 +01003808 appctx->ctx.stats.scope_str = 0;
3809 appctx->ctx.stats.scope_len = 0;
Willy Tarreau578d6e42019-10-09 11:00:22 +02003810 appctx->ctx.stats.flags = STAT_SHNODE | STAT_SHDESC;
3811
3812 if ((strm_li(si_strm(appctx->owner))->bind_conf->level & ACCESS_LVL_MASK) >= ACCESS_LVL_OPER)
3813 appctx->ctx.stats.flags |= STAT_SHLGNDS;
Willy Tarreaud25fc792016-12-16 12:33:47 +01003814
Willy Tarreau2f397382019-10-09 11:43:59 +02003815 if (*args[arg] && *args[arg+1] && *args[arg+2]) {
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01003816 struct proxy *px;
3817
Willy Tarreau2f397382019-10-09 11:43:59 +02003818 px = proxy_find_by_name(args[arg], 0, 0);
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01003819 if (px)
3820 appctx->ctx.stats.iid = px->uuid;
3821 else
Willy Tarreau2f397382019-10-09 11:43:59 +02003822 appctx->ctx.stats.iid = atoi(args[arg]);
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01003823
Willy Tarreau9d008692019-08-09 11:21:01 +02003824 if (!appctx->ctx.stats.iid)
3825 return cli_err(appctx, "No such proxy.\n");
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01003826
Willy Tarreau2b812e22016-11-22 16:18:05 +01003827 appctx->ctx.stats.flags |= STAT_BOUND;
Willy Tarreau2f397382019-10-09 11:43:59 +02003828 appctx->ctx.stats.type = atoi(args[arg+1]);
3829 appctx->ctx.stats.sid = atoi(args[arg+2]);
3830 arg += 3;
3831 }
3832
3833 while (*args[arg]) {
3834 if (strcmp(args[arg], "typed") == 0)
3835 appctx->ctx.stats.flags = (appctx->ctx.stats.flags & ~STAT_FMT_MASK) | STAT_FMT_TYPED;
3836 else if (strcmp(args[arg], "json") == 0)
3837 appctx->ctx.stats.flags = (appctx->ctx.stats.flags & ~STAT_FMT_MASK) | STAT_FMT_JSON;
3838 else if (strcmp(args[arg], "desc") == 0)
3839 appctx->ctx.stats.flags |= STAT_SHOW_FDESC;
3840 arg++;
Willy Tarreau2b812e22016-11-22 16:18:05 +01003841 }
Willy Tarreau2b812e22016-11-22 16:18:05 +01003842
Willy Tarreau2b812e22016-11-22 16:18:05 +01003843 return 0;
3844}
3845
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003846static int cli_io_handler_dump_info(struct appctx *appctx)
3847{
3848 return stats_dump_info_to_buffer(appctx->owner);
3849}
3850
Willy Tarreau2b812e22016-11-22 16:18:05 +01003851/* This I/O handler runs as an applet embedded in a stream interface. It is
3852 * used to send raw stats over a socket.
3853 */
3854static int cli_io_handler_dump_stat(struct appctx *appctx)
3855{
Christopher Fauletef779222018-10-31 08:47:01 +01003856 return stats_dump_stat_to_buffer(appctx->owner, NULL, NULL);
Willy Tarreau2b812e22016-11-22 16:18:05 +01003857}
3858
Simon Horman6f6bb382017-01-04 09:37:26 +01003859static int cli_io_handler_dump_json_schema(struct appctx *appctx)
3860{
3861 return stats_dump_json_schema_to_buffer(appctx->owner);
3862}
3863
Willy Tarreau2b812e22016-11-22 16:18:05 +01003864/* register cli keywords */
3865static struct cli_kw_list cli_kws = {{ },{
Willy Tarreau89d467c2016-11-23 11:02:40 +01003866 { { "clear", "counters", NULL }, "clear counters : clear max statistics counters (add 'all' for all counters)", cli_parse_clear_counters, NULL, NULL },
Willy Tarreau61038362019-10-09 07:19:02 +02003867 { { "show", "info", NULL }, "show info : report information about the running process [json|typed]", cli_parse_show_info, cli_io_handler_dump_info, NULL },
3868 { { "show", "stat", NULL }, "show stat : report counters for each proxy and server [json|typed]", cli_parse_show_stat, cli_io_handler_dump_stat, NULL },
Simon Horman6f6bb382017-01-04 09:37:26 +01003869 { { "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 +01003870 {{},}
3871}};
3872
Willy Tarreau0108d902018-11-25 19:14:37 +01003873INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
3874
William Lallemand74c24fb2016-11-21 17:18:36 +01003875struct applet http_stats_applet = {
3876 .obj_type = OBJ_TYPE_APPLET,
3877 .name = "<STATS>", /* used for logging */
3878 .fct = http_stats_io_handler,
3879 .release = NULL,
3880};
3881
William Lallemand74c24fb2016-11-21 17:18:36 +01003882/*
3883 * Local variables:
3884 * c-indent-level: 8
3885 * c-basic-offset: 8
3886 * End:
3887 */