blob: 14e5e7c3075184c254db369528079825cf9e120e [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,
511 const struct field *stats)
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,
527 const struct field *stats)
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,
Simon Horman05ee2132017-01-04 09:37:25 +0100556 const struct field *info)
557{
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,
Simon Horman05ee2132017-01-04 09:37:25 +0100606 int first_stat)
607{
608 int field;
609 int started = 0;
610
611 if (!first_stat && !chunk_strcat(out, ","))
612 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);
666 if (!first_stat)
667 chunk_strcat(out, ",");
668 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 Tarreau708c4162019-10-09 10:19:16 +0200673 * reserved for the checkbox is STAT_SHOWADMIN is set in <flags>. Some extra info
674 * 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 Tarreau708c4162019-10-09 10:19:16 +0200687 if (flags & STAT_SHOWADMIN) {
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 Tarreau708c4162019-10-09 10:19:16 +0200830 if (flags & STAT_SHOWADMIN) {
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 Tarreau708c4162019-10-09 10:19:16 +0200935 if (flags & STAT_SHOWADMIN)
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 Tarreau708c4162019-10-09 10:19:16 +02001191 if (flags & STAT_SHOWADMIN) {
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
1355int stats_dump_one_line(const struct field *stats, unsigned int flags, struct proxy *px, struct appctx *appctx)
1356{
Simon Horman05ee2132017-01-04 09:37:25 +01001357 int ret;
1358
William Lallemand74c24fb2016-11-21 17:18:36 +01001359 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN))
Willy Tarreau708c4162019-10-09 10:19:16 +02001360 flags |= STAT_SHOWADMIN;
William Lallemand74c24fb2016-11-21 17:18:36 +01001361
1362 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
Simon Horman05ee2132017-01-04 09:37:25 +01001363 ret = stats_dump_fields_html(&trash, stats, flags);
William Lallemand74c24fb2016-11-21 17:18:36 +01001364 else if (appctx->ctx.stats.flags & STAT_FMT_TYPED)
Simon Horman05ee2132017-01-04 09:37:25 +01001365 ret = stats_dump_fields_typed(&trash, stats);
1366 else if (appctx->ctx.stats.flags & STAT_FMT_JSON)
1367 ret = stats_dump_fields_json(&trash, stats,
1368 !(appctx->ctx.stats.flags &
1369 STAT_STARTED));
William Lallemand74c24fb2016-11-21 17:18:36 +01001370 else
Simon Horman05ee2132017-01-04 09:37:25 +01001371 ret = stats_dump_fields_csv(&trash, stats);
1372
1373 if (ret)
1374 appctx->ctx.stats.flags |= STAT_STARTED;
1375
1376 return ret;
William Lallemand74c24fb2016-11-21 17:18:36 +01001377}
1378
1379/* Fill <stats> with the frontend statistics. <stats> is
1380 * preallocated array of length <len>. The length of the array
1381 * must be at least ST_F_TOTAL_FIELDS. If this length is less then
1382 * this value, the function returns 0, otherwise, it returns 1.
1383 */
1384int stats_fill_fe_stats(struct proxy *px, struct field *stats, int len)
1385{
1386 if (len < ST_F_TOTAL_FIELDS)
1387 return 0;
1388
1389 memset(stats, 0, sizeof(*stats) * len);
1390
1391 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1392 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, "FRONTEND");
1393 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1394 stats[ST_F_SCUR] = mkf_u32(0, px->feconn);
1395 stats[ST_F_SMAX] = mkf_u32(FN_MAX, px->fe_counters.conn_max);
1396 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, px->maxconn);
1397 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, px->fe_counters.cum_sess);
1398 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, px->fe_counters.bytes_in);
1399 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, px->fe_counters.bytes_out);
1400 stats[ST_F_DREQ] = mkf_u64(FN_COUNTER, px->fe_counters.denied_req);
1401 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, px->fe_counters.denied_resp);
1402 stats[ST_F_EREQ] = mkf_u64(FN_COUNTER, px->fe_counters.failed_req);
1403 stats[ST_F_DCON] = mkf_u64(FN_COUNTER, px->fe_counters.denied_conn);
1404 stats[ST_F_DSES] = mkf_u64(FN_COUNTER, px->fe_counters.denied_sess);
1405 stats[ST_F_STATUS] = mkf_str(FO_STATUS, px->state == PR_STREADY ? "OPEN" : px->state == PR_STFULL ? "FULL" : "STOP");
1406 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
1407 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1408 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, 0);
1409 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_FE);
1410 stats[ST_F_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_sess_per_sec));
1411 stats[ST_F_RATE_LIM] = mkf_u32(FO_CONFIG|FN_LIMIT, px->fe_sps_lim);
1412 stats[ST_F_RATE_MAX] = mkf_u32(FN_MAX, px->fe_counters.sps_max);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001413 stats[ST_F_WREW] = mkf_u64(FN_COUNTER, px->fe_counters.failed_rewrites);
William Lallemand74c24fb2016-11-21 17:18:36 +01001414
1415 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
1416 if (px->mode == PR_MODE_HTTP) {
1417 stats[ST_F_HRSP_1XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[1]);
1418 stats[ST_F_HRSP_2XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[2]);
1419 stats[ST_F_HRSP_3XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[3]);
1420 stats[ST_F_HRSP_4XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[4]);
1421 stats[ST_F_HRSP_5XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[5]);
1422 stats[ST_F_HRSP_OTHER] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[0]);
1423 stats[ST_F_INTERCEPTED] = mkf_u64(FN_COUNTER, px->fe_counters.intercepted_req);
Willy Tarreaua1214a52018-12-14 14:00:25 +01001424 stats[ST_F_CACHE_LOOKUPS] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cache_lookups);
1425 stats[ST_F_CACHE_HITS] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cache_hits);
William Lallemand74c24fb2016-11-21 17:18:36 +01001426 }
1427
1428 /* requests : req_rate, req_rate_max, req_tot, */
1429 stats[ST_F_REQ_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_req_per_sec));
1430 stats[ST_F_REQ_RATE_MAX] = mkf_u32(FN_MAX, px->fe_counters.p.http.rps_max);
1431 stats[ST_F_REQ_TOT] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cum_req);
1432
1433 /* compression: in, out, bypassed, responses */
1434 stats[ST_F_COMP_IN] = mkf_u64(FN_COUNTER, px->fe_counters.comp_in);
1435 stats[ST_F_COMP_OUT] = mkf_u64(FN_COUNTER, px->fe_counters.comp_out);
1436 stats[ST_F_COMP_BYP] = mkf_u64(FN_COUNTER, px->fe_counters.comp_byp);
1437 stats[ST_F_COMP_RSP] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.comp_rsp);
1438
1439 /* connections : conn_rate, conn_rate_max, conn_tot, conn_max */
1440 stats[ST_F_CONN_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_conn_per_sec));
1441 stats[ST_F_CONN_RATE_MAX] = mkf_u32(FN_MAX, px->fe_counters.cps_max);
1442 stats[ST_F_CONN_TOT] = mkf_u64(FN_COUNTER, px->fe_counters.cum_conn);
1443
1444 return 1;
1445}
1446
1447/* Dumps a frontend's line to the trash for the current proxy <px> and uses
1448 * the state from stream interface <si>. The caller is responsible for clearing
1449 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
1450 */
1451static int stats_dump_fe_stats(struct stream_interface *si, struct proxy *px)
1452{
1453 struct appctx *appctx = __objt_appctx(si->end);
1454
1455 if (!(px->cap & PR_CAP_FE))
1456 return 0;
1457
1458 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_FE)))
1459 return 0;
1460
1461 if (!stats_fill_fe_stats(px, stats, ST_F_TOTAL_FIELDS))
1462 return 0;
1463
1464 return stats_dump_one_line(stats, 0, px, appctx);
1465}
1466
1467/* Fill <stats> with the listener statistics. <stats> is
1468 * preallocated array of length <len>. The length of the array
1469 * must be at least ST_F_TOTAL_FIELDS. If this length is less
1470 * then this value, the function returns 0, otherwise, it
Willy Tarreau708c4162019-10-09 10:19:16 +02001471 * returns 1. <flags> can take the value STAT_SHLGNDS.
William Lallemand74c24fb2016-11-21 17:18:36 +01001472 */
1473int stats_fill_li_stats(struct proxy *px, struct listener *l, int flags,
1474 struct field *stats, int len)
1475{
Willy Tarreau83061a82018-07-13 11:56:34 +02001476 struct buffer *out = get_trash_chunk();
William Lallemand74c24fb2016-11-21 17:18:36 +01001477
1478 if (len < ST_F_TOTAL_FIELDS)
1479 return 0;
1480
1481 if (!l->counters)
1482 return 0;
1483
1484 chunk_reset(out);
1485 memset(stats, 0, sizeof(*stats) * len);
1486
1487 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1488 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, l->name);
1489 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1490 stats[ST_F_SCUR] = mkf_u32(0, l->nbconn);
1491 stats[ST_F_SMAX] = mkf_u32(FN_MAX, l->counters->conn_max);
1492 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, l->maxconn);
1493 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, l->counters->cum_conn);
1494 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, l->counters->bytes_in);
1495 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, l->counters->bytes_out);
1496 stats[ST_F_DREQ] = mkf_u64(FN_COUNTER, l->counters->denied_req);
1497 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, l->counters->denied_resp);
1498 stats[ST_F_EREQ] = mkf_u64(FN_COUNTER, l->counters->failed_req);
1499 stats[ST_F_DCON] = mkf_u64(FN_COUNTER, l->counters->denied_conn);
1500 stats[ST_F_DSES] = mkf_u64(FN_COUNTER, l->counters->denied_sess);
Willy Tarreaua8cf66b2019-02-27 16:49:00 +01001501 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 +01001502 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
1503 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1504 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, l->luid);
1505 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_SO);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001506 stats[ST_F_WREW] = mkf_u64(FN_COUNTER, l->counters->failed_rewrites);
William Lallemand74c24fb2016-11-21 17:18:36 +01001507
Willy Tarreau708c4162019-10-09 10:19:16 +02001508 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001509 char str[INET6_ADDRSTRLEN];
1510 int port;
1511
1512 port = get_host_port(&l->addr);
1513 switch (addr_to_str(&l->addr, str, sizeof(str))) {
1514 case AF_INET:
1515 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1516 chunk_appendf(out, "%s:%d", str, port);
1517 break;
1518 case AF_INET6:
1519 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1520 chunk_appendf(out, "[%s]:%d", str, port);
1521 break;
1522 case AF_UNIX:
1523 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, "unix");
1524 break;
1525 case -1:
1526 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1527 chunk_strcat(out, strerror(errno));
1528 break;
1529 default: /* address family not supported */
1530 break;
1531 }
1532 }
1533
1534 return 1;
1535}
1536
1537/* Dumps a line for listener <l> and proxy <px> to the trash and uses the state
1538 * from stream interface <si>, and stats flags <flags>. The caller is responsible
1539 * for clearing the trash if needed. Returns non-zero if it emits anything, zero
1540 * otherwise.
1541 */
1542static int stats_dump_li_stats(struct stream_interface *si, struct proxy *px, struct listener *l, int flags)
1543{
1544 struct appctx *appctx = __objt_appctx(si->end);
1545
1546 if (!stats_fill_li_stats(px, l, flags, stats, ST_F_TOTAL_FIELDS))
1547 return 0;
1548
1549 return stats_dump_one_line(stats, flags, px, appctx);
1550}
1551
1552enum srv_stats_state {
1553 SRV_STATS_STATE_DOWN = 0,
1554 SRV_STATS_STATE_DOWN_AGENT,
1555 SRV_STATS_STATE_GOING_UP,
1556 SRV_STATS_STATE_UP_GOING_DOWN,
1557 SRV_STATS_STATE_UP,
1558 SRV_STATS_STATE_NOLB_GOING_DOWN,
1559 SRV_STATS_STATE_NOLB,
1560 SRV_STATS_STATE_DRAIN_GOING_DOWN,
1561 SRV_STATS_STATE_DRAIN,
1562 SRV_STATS_STATE_DRAIN_AGENT,
1563 SRV_STATS_STATE_NO_CHECK,
1564
1565 SRV_STATS_STATE_COUNT, /* Must be last */
1566};
1567
1568static const char *srv_hlt_st[SRV_STATS_STATE_COUNT] = {
1569 [SRV_STATS_STATE_DOWN] = "DOWN",
1570 [SRV_STATS_STATE_DOWN_AGENT] = "DOWN (agent)",
1571 [SRV_STATS_STATE_GOING_UP] = "DOWN %d/%d",
1572 [SRV_STATS_STATE_UP_GOING_DOWN] = "UP %d/%d",
1573 [SRV_STATS_STATE_UP] = "UP",
1574 [SRV_STATS_STATE_NOLB_GOING_DOWN] = "NOLB %d/%d",
1575 [SRV_STATS_STATE_NOLB] = "NOLB",
1576 [SRV_STATS_STATE_DRAIN_GOING_DOWN] = "DRAIN %d/%d",
1577 [SRV_STATS_STATE_DRAIN] = "DRAIN",
1578 [SRV_STATS_STATE_DRAIN_AGENT] = "DRAIN (agent)",
1579 [SRV_STATS_STATE_NO_CHECK] = "no check"
1580};
1581
1582/* Fill <stats> with the server statistics. <stats> is
1583 * preallocated array of length <len>. The length of the array
1584 * must be at least ST_F_TOTAL_FIELDS. If this length is less
1585 * then this value, the function returns 0, otherwise, it
Willy Tarreau708c4162019-10-09 10:19:16 +02001586 * returns 1. <flags> can take the value STAT_SHLGNDS.
William Lallemand74c24fb2016-11-21 17:18:36 +01001587 */
1588int stats_fill_sv_stats(struct proxy *px, struct server *sv, int flags,
1589 struct field *stats, int len)
1590{
1591 struct server *via, *ref;
1592 char str[INET6_ADDRSTRLEN];
Willy Tarreau83061a82018-07-13 11:56:34 +02001593 struct buffer *out = get_trash_chunk();
William Lallemand74c24fb2016-11-21 17:18:36 +01001594 enum srv_stats_state state;
1595 char *fld_status;
1596
1597 if (len < ST_F_TOTAL_FIELDS)
1598 return 0;
1599
1600 memset(stats, 0, sizeof(*stats) * len);
1601
1602 /* we have "via" which is the tracked server as described in the configuration,
1603 * and "ref" which is the checked server and the end of the chain.
1604 */
1605 via = sv->track ? sv->track : sv;
1606 ref = via;
1607 while (ref->track)
1608 ref = ref->track;
1609
Emeric Brun52a91d32017-08-31 14:41:55 +02001610 if (sv->cur_state == SRV_ST_RUNNING || sv->cur_state == SRV_ST_STARTING) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001611 if ((ref->check.state & CHK_ST_ENABLED) &&
1612 (ref->check.health < ref->check.rise + ref->check.fall - 1)) {
1613 state = SRV_STATS_STATE_UP_GOING_DOWN;
1614 } else {
1615 state = SRV_STATS_STATE_UP;
1616 }
1617
Emeric Brun52a91d32017-08-31 14:41:55 +02001618 if (sv->cur_admin & SRV_ADMF_DRAIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001619 if (ref->agent.state & CHK_ST_ENABLED)
1620 state = SRV_STATS_STATE_DRAIN_AGENT;
1621 else if (state == SRV_STATS_STATE_UP_GOING_DOWN)
1622 state = SRV_STATS_STATE_DRAIN_GOING_DOWN;
1623 else
1624 state = SRV_STATS_STATE_DRAIN;
1625 }
1626
1627 if (state == SRV_STATS_STATE_UP && !(ref->check.state & CHK_ST_ENABLED)) {
1628 state = SRV_STATS_STATE_NO_CHECK;
1629 }
1630 }
Emeric Brun52a91d32017-08-31 14:41:55 +02001631 else if (sv->cur_state == SRV_ST_STOPPING) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001632 if ((!(sv->check.state & CHK_ST_ENABLED) && !sv->track) ||
1633 (ref->check.health == ref->check.rise + ref->check.fall - 1)) {
1634 state = SRV_STATS_STATE_NOLB;
1635 } else {
1636 state = SRV_STATS_STATE_NOLB_GOING_DOWN;
1637 }
1638 }
1639 else { /* stopped */
1640 if ((ref->agent.state & CHK_ST_ENABLED) && !ref->agent.health) {
1641 state = SRV_STATS_STATE_DOWN_AGENT;
1642 } else if ((ref->check.state & CHK_ST_ENABLED) && !ref->check.health) {
1643 state = SRV_STATS_STATE_DOWN; /* DOWN */
1644 } else if ((ref->agent.state & CHK_ST_ENABLED) || (ref->check.state & CHK_ST_ENABLED)) {
1645 state = SRV_STATS_STATE_GOING_UP;
1646 } else {
1647 state = SRV_STATS_STATE_DOWN; /* DOWN, unchecked */
1648 }
1649 }
1650
1651 chunk_reset(out);
1652
1653 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1654 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, sv->id);
1655 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1656 stats[ST_F_QCUR] = mkf_u32(0, sv->nbpend);
1657 stats[ST_F_QMAX] = mkf_u32(FN_MAX, sv->counters.nbpend_max);
1658 stats[ST_F_SCUR] = mkf_u32(0, sv->cur_sess);
1659 stats[ST_F_SMAX] = mkf_u32(FN_MAX, sv->counters.cur_sess_max);
1660
1661 if (sv->maxconn)
1662 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, sv->maxconn);
1663
Willy Tarreauf21d17b2019-09-08 09:24:56 +02001664 stats[ST_F_SRV_ICUR] = mkf_u32(0, sv->curr_idle_conns);
1665 if (sv->max_idle_conns != -1)
1666 stats[ST_F_SRV_ILIM] = mkf_u32(FO_CONFIG|FN_LIMIT, sv->max_idle_conns);
1667
William Lallemand74c24fb2016-11-21 17:18:36 +01001668 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, sv->counters.cum_sess);
1669 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, sv->counters.bytes_in);
1670 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, sv->counters.bytes_out);
1671 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, sv->counters.failed_secu);
1672 stats[ST_F_ECON] = mkf_u64(FN_COUNTER, sv->counters.failed_conns);
1673 stats[ST_F_ERESP] = mkf_u64(FN_COUNTER, sv->counters.failed_resp);
1674 stats[ST_F_WRETR] = mkf_u64(FN_COUNTER, sv->counters.retries);
1675 stats[ST_F_WREDIS] = mkf_u64(FN_COUNTER, sv->counters.redispatches);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001676 stats[ST_F_WREW] = mkf_u64(FN_COUNTER, sv->counters.failed_rewrites);
Willy Tarreauf1573842018-12-14 11:35:36 +01001677 stats[ST_F_CONNECT] = mkf_u64(FN_COUNTER, sv->counters.connect);
1678 stats[ST_F_REUSE] = mkf_u64(FN_COUNTER, sv->counters.reuse);
William Lallemand74c24fb2016-11-21 17:18:36 +01001679
1680 /* status */
1681 fld_status = chunk_newstr(out);
Emeric Brun52a91d32017-08-31 14:41:55 +02001682 if (sv->cur_admin & SRV_ADMF_RMAINT)
William Lallemand74c24fb2016-11-21 17:18:36 +01001683 chunk_appendf(out, "MAINT (resolution)");
Emeric Brun52a91d32017-08-31 14:41:55 +02001684 else if (sv->cur_admin & SRV_ADMF_IMAINT)
William Lallemand74c24fb2016-11-21 17:18:36 +01001685 chunk_appendf(out, "MAINT (via %s/%s)", via->proxy->id, via->id);
Emeric Brun52a91d32017-08-31 14:41:55 +02001686 else if (sv->cur_admin & SRV_ADMF_MAINT)
William Lallemand74c24fb2016-11-21 17:18:36 +01001687 chunk_appendf(out, "MAINT");
1688 else
1689 chunk_appendf(out,
1690 srv_hlt_st[state],
Emeric Brun52a91d32017-08-31 14:41:55 +02001691 (ref->cur_state != SRV_ST_STOPPED) ? (ref->check.health - ref->check.rise + 1) : (ref->check.health),
1692 (ref->cur_state != SRV_ST_STOPPED) ? (ref->check.fall) : (ref->check.rise));
William Lallemand74c24fb2016-11-21 17:18:36 +01001693
1694 stats[ST_F_STATUS] = mkf_str(FO_STATUS, fld_status);
1695 stats[ST_F_LASTCHG] = mkf_u32(FN_AGE, now.tv_sec - sv->last_change);
Emeric Brun52a91d32017-08-31 14:41:55 +02001696 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 +01001697 stats[ST_F_ACT] = mkf_u32(FO_STATUS, (sv->flags & SRV_F_BACKUP) ? 0 : 1);
1698 stats[ST_F_BCK] = mkf_u32(FO_STATUS, (sv->flags & SRV_F_BACKUP) ? 1 : 0);
1699
1700 /* check failures: unique, fatal; last change, total downtime */
1701 if (sv->check.state & CHK_ST_ENABLED) {
1702 stats[ST_F_CHKFAIL] = mkf_u64(FN_COUNTER, sv->counters.failed_checks);
1703 stats[ST_F_CHKDOWN] = mkf_u64(FN_COUNTER, sv->counters.down_trans);
1704 stats[ST_F_DOWNTIME] = mkf_u32(FN_COUNTER, srv_downtime(sv));
1705 }
1706
1707 if (sv->maxqueue)
1708 stats[ST_F_QLIMIT] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->maxqueue);
1709
1710 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
1711 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1712 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, sv->puid);
1713
Emeric Brun52a91d32017-08-31 14:41:55 +02001714 if (sv->cur_state == SRV_ST_STARTING && !server_is_draining(sv))
William Lallemand74c24fb2016-11-21 17:18:36 +01001715 stats[ST_F_THROTTLE] = mkf_u32(FN_AVG, server_throttle_rate(sv));
1716
1717 stats[ST_F_LBTOT] = mkf_u64(FN_COUNTER, sv->counters.cum_lbconn);
1718
1719 if (sv->track) {
1720 char *fld_track = chunk_newstr(out);
1721
1722 chunk_appendf(out, "%s/%s", sv->track->proxy->id, sv->track->id);
1723 stats[ST_F_TRACKED] = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, fld_track);
1724 }
1725
1726 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_SV);
1727 stats[ST_F_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&sv->sess_per_sec));
1728 stats[ST_F_RATE_MAX] = mkf_u32(FN_MAX, sv->counters.sps_max);
1729
1730 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) {
1731 const char *fld_chksts;
1732
1733 fld_chksts = chunk_newstr(out);
1734 chunk_strcat(out, "* "); // for check in progress
1735 chunk_strcat(out, get_check_status_info(sv->check.status));
1736 if (!(sv->check.state & CHK_ST_INPROGRESS))
1737 fld_chksts += 2; // skip "* "
1738 stats[ST_F_CHECK_STATUS] = mkf_str(FN_OUTPUT, fld_chksts);
1739
1740 if (sv->check.status >= HCHK_STATUS_L57DATA)
1741 stats[ST_F_CHECK_CODE] = mkf_u32(FN_OUTPUT, sv->check.code);
1742
1743 if (sv->check.status >= HCHK_STATUS_CHECKED)
1744 stats[ST_F_CHECK_DURATION] = mkf_u64(FN_DURATION, sv->check.duration);
1745
1746 stats[ST_F_CHECK_DESC] = mkf_str(FN_OUTPUT, get_check_status_description(sv->check.status));
1747 stats[ST_F_LAST_CHK] = mkf_str(FN_OUTPUT, sv->check.desc);
1748 stats[ST_F_CHECK_RISE] = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.rise);
1749 stats[ST_F_CHECK_FALL] = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.fall);
1750 stats[ST_F_CHECK_HEALTH] = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.health);
1751 }
1752
1753 if ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) {
1754 const char *fld_chksts;
1755
1756 fld_chksts = chunk_newstr(out);
1757 chunk_strcat(out, "* "); // for check in progress
1758 chunk_strcat(out, get_check_status_info(sv->agent.status));
1759 if (!(sv->agent.state & CHK_ST_INPROGRESS))
1760 fld_chksts += 2; // skip "* "
1761 stats[ST_F_AGENT_STATUS] = mkf_str(FN_OUTPUT, fld_chksts);
1762
1763 if (sv->agent.status >= HCHK_STATUS_L57DATA)
1764 stats[ST_F_AGENT_CODE] = mkf_u32(FN_OUTPUT, sv->agent.code);
1765
1766 if (sv->agent.status >= HCHK_STATUS_CHECKED)
1767 stats[ST_F_AGENT_DURATION] = mkf_u64(FN_DURATION, sv->agent.duration);
1768
1769 stats[ST_F_AGENT_DESC] = mkf_str(FN_OUTPUT, get_check_status_description(sv->agent.status));
1770 stats[ST_F_LAST_AGT] = mkf_str(FN_OUTPUT, sv->agent.desc);
1771 stats[ST_F_AGENT_RISE] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.rise);
1772 stats[ST_F_AGENT_FALL] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.fall);
1773 stats[ST_F_AGENT_HEALTH] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.health);
1774 }
1775
1776 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
1777 if (px->mode == PR_MODE_HTTP) {
1778 stats[ST_F_HRSP_1XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[1]);
1779 stats[ST_F_HRSP_2XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[2]);
1780 stats[ST_F_HRSP_3XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[3]);
1781 stats[ST_F_HRSP_4XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[4]);
1782 stats[ST_F_HRSP_5XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[5]);
1783 stats[ST_F_HRSP_OTHER] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[0]);
1784 }
1785
1786 if (ref->observe)
1787 stats[ST_F_HANAFAIL] = mkf_u64(FN_COUNTER, sv->counters.failed_hana);
1788
1789 stats[ST_F_CLI_ABRT] = mkf_u64(FN_COUNTER, sv->counters.cli_aborts);
1790 stats[ST_F_SRV_ABRT] = mkf_u64(FN_COUNTER, sv->counters.srv_aborts);
1791 stats[ST_F_LASTSESS] = mkf_s32(FN_AGE, srv_lastsession(sv));
1792
1793 stats[ST_F_QTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.q_time, TIME_STATS_SAMPLES));
1794 stats[ST_F_CTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.c_time, TIME_STATS_SAMPLES));
1795 stats[ST_F_RTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.d_time, TIME_STATS_SAMPLES));
1796 stats[ST_F_TTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.t_time, TIME_STATS_SAMPLES));
1797
Willy Tarreau708c4162019-10-09 10:19:16 +02001798 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001799 switch (addr_to_str(&sv->addr, str, sizeof(str))) {
1800 case AF_INET:
1801 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
Willy Tarreau04276f32017-01-06 17:41:29 +01001802 chunk_appendf(out, "%s:%d", str, sv->svc_port);
William Lallemand74c24fb2016-11-21 17:18:36 +01001803 break;
1804 case AF_INET6:
1805 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
Willy Tarreau04276f32017-01-06 17:41:29 +01001806 chunk_appendf(out, "[%s]:%d", str, sv->svc_port);
William Lallemand74c24fb2016-11-21 17:18:36 +01001807 break;
1808 case AF_UNIX:
1809 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, "unix");
1810 break;
1811 case -1:
1812 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1813 chunk_strcat(out, strerror(errno));
1814 break;
1815 default: /* address family not supported */
1816 break;
1817 }
1818
1819 if (sv->cookie)
1820 stats[ST_F_COOKIE] = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, sv->cookie);
1821 }
1822
1823 return 1;
1824}
1825
1826/* Dumps a line for server <sv> and proxy <px> to the trash and uses the state
1827 * from stream interface <si>, stats flags <flags>, and server state <state>.
1828 * The caller is responsible for clearing the trash if needed. Returns non-zero
1829 * if it emits anything, zero otherwise.
1830 */
1831static int stats_dump_sv_stats(struct stream_interface *si, struct proxy *px, int flags, struct server *sv)
1832{
1833 struct appctx *appctx = __objt_appctx(si->end);
1834
1835 if (!stats_fill_sv_stats(px, sv, flags, stats, ST_F_TOTAL_FIELDS))
1836 return 0;
1837
1838 return stats_dump_one_line(stats, flags, px, appctx);
1839}
1840
1841/* Fill <stats> with the backend statistics. <stats> is
1842 * preallocated array of length <len>. The length of the array
1843 * must be at least ST_F_TOTAL_FIELDS. If this length is less
1844 * then this value, the function returns 0, otherwise, it
Willy Tarreau708c4162019-10-09 10:19:16 +02001845 * returns 1. <flags> can take the value STAT_SHLGNDS.
William Lallemand74c24fb2016-11-21 17:18:36 +01001846 */
1847int stats_fill_be_stats(struct proxy *px, int flags, struct field *stats, int len)
1848{
1849 if (len < ST_F_TOTAL_FIELDS)
1850 return 0;
1851
1852 memset(stats, 0, sizeof(*stats) * len);
1853
1854 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1855 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, "BACKEND");
1856 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1857 stats[ST_F_QCUR] = mkf_u32(0, px->nbpend);
1858 stats[ST_F_QMAX] = mkf_u32(FN_MAX, px->be_counters.nbpend_max);
Thierry FOURNIER0ff98a42016-12-19 16:50:42 +01001859 stats[ST_F_SCUR] = mkf_u32(0, px->beconn);
William Lallemand74c24fb2016-11-21 17:18:36 +01001860 stats[ST_F_SMAX] = mkf_u32(FN_MAX, px->be_counters.conn_max);
1861 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, px->fullconn);
1862 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, px->be_counters.cum_conn);
1863 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, px->be_counters.bytes_in);
1864 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, px->be_counters.bytes_out);
1865 stats[ST_F_DREQ] = mkf_u64(FN_COUNTER, px->be_counters.denied_req);
1866 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, px->be_counters.denied_resp);
1867 stats[ST_F_ECON] = mkf_u64(FN_COUNTER, px->be_counters.failed_conns);
1868 stats[ST_F_ERESP] = mkf_u64(FN_COUNTER, px->be_counters.failed_resp);
1869 stats[ST_F_WRETR] = mkf_u64(FN_COUNTER, px->be_counters.retries);
1870 stats[ST_F_WREDIS] = mkf_u64(FN_COUNTER, px->be_counters.redispatches);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001871 stats[ST_F_WREW] = mkf_u64(FN_COUNTER, px->be_counters.failed_rewrites);
Willy Tarreauf1573842018-12-14 11:35:36 +01001872 stats[ST_F_CONNECT] = mkf_u64(FN_COUNTER, px->be_counters.connect);
1873 stats[ST_F_REUSE] = mkf_u64(FN_COUNTER, px->be_counters.reuse);
William Lallemand74c24fb2016-11-21 17:18:36 +01001874 stats[ST_F_STATUS] = mkf_str(FO_STATUS, (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" : "DOWN");
1875 stats[ST_F_WEIGHT] = mkf_u32(FN_AVG, (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv);
1876 stats[ST_F_ACT] = mkf_u32(0, px->srv_act);
1877 stats[ST_F_BCK] = mkf_u32(0, px->srv_bck);
1878 stats[ST_F_CHKDOWN] = mkf_u64(FN_COUNTER, px->down_trans);
1879 stats[ST_F_LASTCHG] = mkf_u32(FN_AGE, now.tv_sec - px->last_change);
1880 if (px->srv)
1881 stats[ST_F_DOWNTIME] = mkf_u32(FN_COUNTER, be_downtime(px));
1882
1883 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
1884 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1885 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, 0);
1886 stats[ST_F_LBTOT] = mkf_u64(FN_COUNTER, px->be_counters.cum_lbconn);
1887 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_BE);
1888 stats[ST_F_RATE] = mkf_u32(0, read_freq_ctr(&px->be_sess_per_sec));
1889 stats[ST_F_RATE_MAX] = mkf_u32(0, px->be_counters.sps_max);
1890
Willy Tarreau708c4162019-10-09 10:19:16 +02001891 if (flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001892 if (px->cookie_name)
1893 stats[ST_F_COOKIE] = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, px->cookie_name);
1894 stats[ST_F_ALGO] = mkf_str(FO_CONFIG|FS_SERVICE, backend_lb_algo_str(px->lbprm.algo & BE_LB_ALGO));
1895 }
1896
1897 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
1898 if (px->mode == PR_MODE_HTTP) {
1899 stats[ST_F_REQ_TOT] = mkf_u64(FN_COUNTER, px->be_counters.p.http.cum_req);
1900 stats[ST_F_HRSP_1XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[1]);
1901 stats[ST_F_HRSP_2XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[2]);
1902 stats[ST_F_HRSP_3XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[3]);
1903 stats[ST_F_HRSP_4XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[4]);
1904 stats[ST_F_HRSP_5XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[5]);
1905 stats[ST_F_HRSP_OTHER] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[0]);
Willy Tarreaua1214a52018-12-14 14:00:25 +01001906 stats[ST_F_CACHE_LOOKUPS] = mkf_u64(FN_COUNTER, px->be_counters.p.http.cache_lookups);
1907 stats[ST_F_CACHE_HITS] = mkf_u64(FN_COUNTER, px->be_counters.p.http.cache_hits);
William Lallemand74c24fb2016-11-21 17:18:36 +01001908 }
1909
1910 stats[ST_F_CLI_ABRT] = mkf_u64(FN_COUNTER, px->be_counters.cli_aborts);
1911 stats[ST_F_SRV_ABRT] = mkf_u64(FN_COUNTER, px->be_counters.srv_aborts);
1912
1913 /* compression: in, out, bypassed, responses */
1914 stats[ST_F_COMP_IN] = mkf_u64(FN_COUNTER, px->be_counters.comp_in);
1915 stats[ST_F_COMP_OUT] = mkf_u64(FN_COUNTER, px->be_counters.comp_out);
1916 stats[ST_F_COMP_BYP] = mkf_u64(FN_COUNTER, px->be_counters.comp_byp);
1917 stats[ST_F_COMP_RSP] = mkf_u64(FN_COUNTER, px->be_counters.p.http.comp_rsp);
1918 stats[ST_F_LASTSESS] = mkf_s32(FN_AGE, be_lastsession(px));
1919
1920 stats[ST_F_QTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.q_time, TIME_STATS_SAMPLES));
1921 stats[ST_F_CTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.c_time, TIME_STATS_SAMPLES));
1922 stats[ST_F_RTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.d_time, TIME_STATS_SAMPLES));
1923 stats[ST_F_TTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.t_time, TIME_STATS_SAMPLES));
1924
1925 return 1;
1926}
1927
1928/* Dumps a line for backend <px> to the trash for and uses the state from stream
1929 * interface <si> and stats flags <flags>. The caller is responsible for clearing
1930 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
1931 */
1932static int stats_dump_be_stats(struct stream_interface *si, struct proxy *px, int flags)
1933{
1934 struct appctx *appctx = __objt_appctx(si->end);
1935
1936 if (!(px->cap & PR_CAP_BE))
1937 return 0;
1938
1939 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_BE)))
1940 return 0;
1941
1942 if (!stats_fill_be_stats(px, flags, stats, ST_F_TOTAL_FIELDS))
1943 return 0;
1944
1945 return stats_dump_one_line(stats, flags, px, appctx);
1946}
1947
1948/* Dumps the HTML table header for proxy <px> to the trash for and uses the state from
1949 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
1950 * for clearing the trash if needed.
1951 */
1952static void stats_dump_html_px_hdr(struct stream_interface *si, struct proxy *px, struct uri_auth *uri)
1953{
1954 struct appctx *appctx = __objt_appctx(si->end);
1955 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
1956
1957 if (px->cap & PR_CAP_BE && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
1958 /* A form to enable/disable this proxy servers */
1959
1960 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
1961 scope_txt[0] = 0;
1962 if (appctx->ctx.stats.scope_len) {
Christopher Fauleted7a0662019-01-14 11:07:34 +01001963 const char *scope_ptr = stats_scope_ptr(appctx, si);
1964
William Lallemand74c24fb2016-11-21 17:18:36 +01001965 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Christopher Fauleted7a0662019-01-14 11:07:34 +01001966 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), scope_ptr, appctx->ctx.stats.scope_len);
William Lallemand74c24fb2016-11-21 17:18:36 +01001967 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
1968 }
1969
1970 chunk_appendf(&trash,
1971 "<form method=\"post\">");
1972 }
1973
1974 /* print a new table */
1975 chunk_appendf(&trash,
1976 "<table class=\"tbl\" width=\"100%%\">\n"
1977 "<tr class=\"titre\">"
1978 "<th class=\"pxname\" width=\"10%%\">");
1979
1980 chunk_appendf(&trash,
1981 "<a name=\"%s\"></a>%s"
1982 "<a class=px href=\"#%s\">%s</a>",
1983 px->id,
Willy Tarreau708c4162019-10-09 10:19:16 +02001984 (uri->flags & STAT_SHLGNDS) ? "<u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01001985 px->id, px->id);
1986
Willy Tarreau708c4162019-10-09 10:19:16 +02001987 if (uri->flags & STAT_SHLGNDS) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001988 /* cap, mode, id */
1989 chunk_appendf(&trash, "<div class=tips>cap: %s, mode: %s, id: %d",
1990 proxy_cap_str(px->cap), proxy_mode_str(px->mode),
1991 px->uuid);
1992 chunk_appendf(&trash, "</div>");
1993 }
1994
1995 chunk_appendf(&trash,
1996 "%s</th>"
1997 "<th class=\"%s\" width=\"90%%\">%s</th>"
1998 "</tr>\n"
1999 "</table>\n"
2000 "<table class=\"tbl\" width=\"100%%\">\n"
2001 "<tr class=\"titre\">",
Willy Tarreau708c4162019-10-09 10:19:16 +02002002 (uri->flags & STAT_SHLGNDS) ? "</u>":"",
William Lallemand74c24fb2016-11-21 17:18:36 +01002003 px->desc ? "desc" : "empty", px->desc ? px->desc : "");
2004
2005 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
2006 /* Column heading for Enable or Disable server */
David Harrigand3db35a2016-12-30 12:12:49 +00002007 chunk_appendf(&trash,
2008 "<th rowspan=2 width=1><input type=\"checkbox\" \
2009 onclick=\"for(c in document.getElementsByClassName('%s-checkbox')) \
2010 document.getElementsByClassName('%s-checkbox').item(c).checked = this.checked\"></th>",
2011 px->id,
2012 px->id);
William Lallemand74c24fb2016-11-21 17:18:36 +01002013 }
2014
2015 chunk_appendf(&trash,
2016 "<th rowspan=2></th>"
2017 "<th colspan=3>Queue</th>"
2018 "<th colspan=3>Session rate</th><th colspan=6>Sessions</th>"
2019 "<th colspan=2>Bytes</th><th colspan=2>Denied</th>"
2020 "<th colspan=3>Errors</th><th colspan=2>Warnings</th>"
2021 "<th colspan=9>Server</th>"
2022 "</tr>\n"
2023 "<tr class=\"titre\">"
2024 "<th>Cur</th><th>Max</th><th>Limit</th>"
2025 "<th>Cur</th><th>Max</th><th>Limit</th><th>Cur</th><th>Max</th>"
2026 "<th>Limit</th><th>Total</th><th>LbTot</th><th>Last</th><th>In</th><th>Out</th>"
2027 "<th>Req</th><th>Resp</th><th>Req</th><th>Conn</th>"
2028 "<th>Resp</th><th>Retr</th><th>Redis</th>"
2029 "<th>Status</th><th>LastChk</th><th>Wght</th><th>Act</th>"
2030 "<th>Bck</th><th>Chk</th><th>Dwn</th><th>Dwntme</th>"
2031 "<th>Thrtle</th>\n"
2032 "</tr>");
2033}
2034
2035/* Dumps the HTML table trailer for proxy <px> to the trash for and uses the state from
2036 * stream interface <si>. The caller is responsible for clearing the trash if needed.
2037 */
2038static void stats_dump_html_px_end(struct stream_interface *si, struct proxy *px)
2039{
2040 struct appctx *appctx = __objt_appctx(si->end);
2041 chunk_appendf(&trash, "</table>");
2042
2043 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
2044 /* close the form used to enable/disable this proxy servers */
2045 chunk_appendf(&trash,
2046 "Choose the action to perform on the checked servers : "
2047 "<select name=action>"
2048 "<option value=\"\"></option>"
2049 "<option value=\"ready\">Set state to READY</option>"
2050 "<option value=\"drain\">Set state to DRAIN</option>"
2051 "<option value=\"maint\">Set state to MAINT</option>"
2052 "<option value=\"dhlth\">Health: disable checks</option>"
2053 "<option value=\"ehlth\">Health: enable checks</option>"
2054 "<option value=\"hrunn\">Health: force UP</option>"
2055 "<option value=\"hnolb\">Health: force NOLB</option>"
2056 "<option value=\"hdown\">Health: force DOWN</option>"
2057 "<option value=\"dagent\">Agent: disable checks</option>"
2058 "<option value=\"eagent\">Agent: enable checks</option>"
2059 "<option value=\"arunn\">Agent: force UP</option>"
2060 "<option value=\"adown\">Agent: force DOWN</option>"
2061 "<option value=\"shutdown\">Kill Sessions</option>"
2062 "</select>"
2063 "<input type=\"hidden\" name=\"b\" value=\"#%d\">"
2064 "&nbsp;<input type=\"submit\" value=\"Apply\">"
2065 "</form>",
2066 px->uuid);
2067 }
2068
2069 chunk_appendf(&trash, "<p>\n");
2070}
2071
2072/*
2073 * Dumps statistics for a proxy. The output is sent to the stream interface's
2074 * input buffer. Returns 0 if it had to stop dumping data because of lack of
2075 * buffer space, or non-zero if everything completed. This function is used
2076 * both by the CLI and the HTTP entry points, and is able to dump the output
2077 * in HTML or CSV formats. If the later, <uri> must be NULL.
2078 */
Christopher Fauletef779222018-10-31 08:47:01 +01002079int stats_dump_proxy_to_buffer(struct stream_interface *si, struct htx *htx,
2080 struct proxy *px, struct uri_auth *uri)
William Lallemand74c24fb2016-11-21 17:18:36 +01002081{
2082 struct appctx *appctx = __objt_appctx(si->end);
2083 struct stream *s = si_strm(si);
2084 struct channel *rep = si_ic(si);
2085 struct server *sv, *svs; /* server and server-state, server-state=server or server->track */
2086 struct listener *l;
2087 unsigned int flags;
2088
2089 if (uri)
2090 flags = uri->flags;
William Lallemand07a62f72017-05-24 00:57:40 +02002091 else if ((strm_li(s)->bind_conf->level & ACCESS_LVL_MASK) >= ACCESS_LVL_OPER)
Willy Tarreau708c4162019-10-09 10:19:16 +02002092 flags = STAT_SHLGNDS | STAT_SHNODE | STAT_SHDESC;
William Lallemand74c24fb2016-11-21 17:18:36 +01002093 else
Willy Tarreau708c4162019-10-09 10:19:16 +02002094 flags = STAT_SHNODE | STAT_SHDESC;
William Lallemand74c24fb2016-11-21 17:18:36 +01002095
2096 chunk_reset(&trash);
2097
2098 switch (appctx->ctx.stats.px_st) {
2099 case STAT_PX_ST_INIT:
2100 /* we are on a new proxy */
2101 if (uri && uri->scope) {
2102 /* we have a limited scope, we have to check the proxy name */
2103 struct stat_scope *scope;
2104 int len;
2105
2106 len = strlen(px->id);
2107 scope = uri->scope;
2108
2109 while (scope) {
2110 /* match exact proxy name */
2111 if (scope->px_len == len && !memcmp(px->id, scope->px_id, len))
2112 break;
2113
2114 /* match '.' which means 'self' proxy */
2115 if (!strcmp(scope->px_id, ".") && px == s->be)
2116 break;
2117 scope = scope->next;
2118 }
2119
2120 /* proxy name not found : don't dump anything */
2121 if (scope == NULL)
2122 return 1;
2123 }
2124
2125 /* if the user has requested a limited output and the proxy
2126 * name does not match, skip it.
2127 */
Christopher Fauleted7a0662019-01-14 11:07:34 +01002128 if (appctx->ctx.stats.scope_len) {
2129 const char *scope_ptr = stats_scope_ptr(appctx, si);
2130
2131 if (strnistr(px->id, strlen(px->id), scope_ptr, appctx->ctx.stats.scope_len) == NULL)
2132 return 1;
2133 }
William Lallemand74c24fb2016-11-21 17:18:36 +01002134
2135 if ((appctx->ctx.stats.flags & STAT_BOUND) &&
2136 (appctx->ctx.stats.iid != -1) &&
2137 (px->uuid != appctx->ctx.stats.iid))
2138 return 1;
2139
2140 appctx->ctx.stats.px_st = STAT_PX_ST_TH;
2141 /* fall through */
2142
2143 case STAT_PX_ST_TH:
2144 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
2145 stats_dump_html_px_hdr(si, px, uri);
Christopher Fauletef779222018-10-31 08:47:01 +01002146 if (!stats_putchk(rep, htx, &trash))
2147 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002148 }
2149
2150 appctx->ctx.stats.px_st = STAT_PX_ST_FE;
2151 /* fall through */
2152
2153 case STAT_PX_ST_FE:
2154 /* print the frontend */
2155 if (stats_dump_fe_stats(si, px)) {
Christopher Fauletef779222018-10-31 08:47:01 +01002156 if (!stats_putchk(rep, htx, &trash))
2157 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002158 }
2159
2160 appctx->ctx.stats.l = px->conf.listeners.n;
2161 appctx->ctx.stats.px_st = STAT_PX_ST_LI;
2162 /* fall through */
2163
2164 case STAT_PX_ST_LI:
2165 /* stats.l has been initialized above */
2166 for (; appctx->ctx.stats.l != &px->conf.listeners; appctx->ctx.stats.l = l->by_fe.n) {
Christopher Fauletef779222018-10-31 08:47:01 +01002167 if (htx) {
2168 if (htx_almost_full(htx))
2169 goto full;
2170 }
2171 else {
2172 if (buffer_almost_full(&rep->buf))
2173 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002174 }
2175
2176 l = LIST_ELEM(appctx->ctx.stats.l, struct listener *, by_fe);
2177 if (!l->counters)
2178 continue;
2179
2180 if (appctx->ctx.stats.flags & STAT_BOUND) {
2181 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SO)))
2182 break;
2183
2184 if (appctx->ctx.stats.sid != -1 && l->luid != appctx->ctx.stats.sid)
2185 continue;
2186 }
2187
2188 /* print the frontend */
2189 if (stats_dump_li_stats(si, px, l, flags)) {
Christopher Fauletef779222018-10-31 08:47:01 +01002190 if (!stats_putchk(rep, htx, &trash))
2191 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002192 }
2193 }
2194
2195 appctx->ctx.stats.sv = px->srv; /* may be NULL */
2196 appctx->ctx.stats.px_st = STAT_PX_ST_SV;
2197 /* fall through */
2198
2199 case STAT_PX_ST_SV:
2200 /* stats.sv has been initialized above */
2201 for (; appctx->ctx.stats.sv != NULL; appctx->ctx.stats.sv = sv->next) {
Christopher Fauletef779222018-10-31 08:47:01 +01002202 if (htx) {
2203 if (htx_almost_full(htx))
2204 goto full;
2205 }
2206 else {
2207 if (buffer_almost_full(&rep->buf))
2208 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002209 }
2210
2211 sv = appctx->ctx.stats.sv;
2212
2213 if (appctx->ctx.stats.flags & STAT_BOUND) {
2214 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SV)))
2215 break;
2216
2217 if (appctx->ctx.stats.sid != -1 && sv->puid != appctx->ctx.stats.sid)
2218 continue;
2219 }
2220
2221 svs = sv;
2222 while (svs->track)
2223 svs = svs->track;
2224
2225 /* do not report servers which are DOWN and not changing state */
2226 if ((appctx->ctx.stats.flags & STAT_HIDE_DOWN) &&
Emeric Brun52a91d32017-08-31 14:41:55 +02002227 ((sv->cur_admin & SRV_ADMF_MAINT) || /* server is in maintenance */
2228 (sv->cur_state == SRV_ST_STOPPED && /* server is down */
William Lallemand74c24fb2016-11-21 17:18:36 +01002229 (!((svs->agent.state | svs->check.state) & CHK_ST_ENABLED) ||
2230 ((svs->agent.state & CHK_ST_ENABLED) && !svs->agent.health) ||
2231 ((svs->check.state & CHK_ST_ENABLED) && !svs->check.health))))) {
2232 continue;
2233 }
2234
2235 if (stats_dump_sv_stats(si, px, flags, sv)) {
Christopher Fauletef779222018-10-31 08:47:01 +01002236 if (!stats_putchk(rep, htx, &trash))
2237 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002238 }
2239 } /* for sv */
2240
2241 appctx->ctx.stats.px_st = STAT_PX_ST_BE;
2242 /* fall through */
2243
2244 case STAT_PX_ST_BE:
2245 /* print the backend */
2246 if (stats_dump_be_stats(si, px, flags)) {
Christopher Fauletef779222018-10-31 08:47:01 +01002247 if (!stats_putchk(rep, htx, &trash))
2248 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002249 }
2250
2251 appctx->ctx.stats.px_st = STAT_PX_ST_END;
2252 /* fall through */
2253
2254 case STAT_PX_ST_END:
2255 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
2256 stats_dump_html_px_end(si, px);
Christopher Fauletef779222018-10-31 08:47:01 +01002257 if (!stats_putchk(rep, htx, &trash))
2258 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002259 }
2260
2261 appctx->ctx.stats.px_st = STAT_PX_ST_FIN;
2262 /* fall through */
2263
2264 case STAT_PX_ST_FIN:
2265 return 1;
2266
2267 default:
2268 /* unknown state, we should put an abort() here ! */
2269 return 1;
2270 }
Christopher Fauletef779222018-10-31 08:47:01 +01002271
2272 full:
2273 si_rx_room_blk(si);
2274 return 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01002275}
2276
2277/* Dumps the HTTP stats head block to the trash for and uses the per-uri
2278 * parameters <uri>. The caller is responsible for clearing the trash if needed.
2279 */
2280static void stats_dump_html_head(struct uri_auth *uri)
2281{
2282 /* WARNING! This must fit in the first buffer !!! */
2283 chunk_appendf(&trash,
2284 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n"
2285 "\"http://www.w3.org/TR/html4/loose.dtd\">\n"
2286 "<html><head><title>Statistics Report for " PRODUCT_NAME "%s%s</title>\n"
2287 "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n"
2288 "<style type=\"text/css\"><!--\n"
2289 "body {"
2290 " font-family: arial, helvetica, sans-serif;"
2291 " font-size: 12px;"
2292 " font-weight: normal;"
2293 " color: black;"
2294 " background: white;"
2295 "}\n"
2296 "th,td {"
2297 " font-size: 10px;"
2298 "}\n"
2299 "h1 {"
2300 " font-size: x-large;"
2301 " margin-bottom: 0.5em;"
2302 "}\n"
2303 "h2 {"
2304 " font-family: helvetica, arial;"
2305 " font-size: x-large;"
2306 " font-weight: bold;"
2307 " font-style: italic;"
2308 " color: #6020a0;"
2309 " margin-top: 0em;"
2310 " margin-bottom: 0em;"
2311 "}\n"
2312 "h3 {"
2313 " font-family: helvetica, arial;"
2314 " font-size: 16px;"
2315 " font-weight: bold;"
2316 " color: #b00040;"
2317 " background: #e8e8d0;"
2318 " margin-top: 0em;"
2319 " margin-bottom: 0em;"
2320 "}\n"
2321 "li {"
2322 " margin-top: 0.25em;"
2323 " margin-right: 2em;"
2324 "}\n"
2325 ".hr {margin-top: 0.25em;"
2326 " border-color: black;"
2327 " border-bottom-style: solid;"
2328 "}\n"
2329 ".titre {background: #20D0D0;color: #000000; font-weight: bold; text-align: center;}\n"
2330 ".total {background: #20D0D0;color: #ffff80;}\n"
2331 ".frontend {background: #e8e8d0;}\n"
2332 ".socket {background: #d0d0d0;}\n"
2333 ".backend {background: #e8e8d0;}\n"
2334 ".active_down {background: #ff9090;}\n"
2335 ".active_going_up {background: #ffd020;}\n"
2336 ".active_going_down {background: #ffffa0;}\n"
2337 ".active_up {background: #c0ffc0;}\n"
2338 ".active_nolb {background: #20a0ff;}\n"
2339 ".active_draining {background: #20a0FF;}\n"
2340 ".active_no_check {background: #e0e0e0;}\n"
2341 ".backup_down {background: #ff9090;}\n"
2342 ".backup_going_up {background: #ff80ff;}\n"
2343 ".backup_going_down {background: #c060ff;}\n"
2344 ".backup_up {background: #b0d0ff;}\n"
2345 ".backup_nolb {background: #90b0e0;}\n"
2346 ".backup_draining {background: #cc9900;}\n"
2347 ".backup_no_check {background: #e0e0e0;}\n"
2348 ".maintain {background: #c07820;}\n"
2349 ".rls {letter-spacing: 0.2em; margin-right: 1px;}\n" /* right letter spacing (used for grouping digits) */
2350 "\n"
2351 "a.px:link {color: #ffff40; text-decoration: none;}"
2352 "a.px:visited {color: #ffff40; text-decoration: none;}"
2353 "a.px:hover {color: #ffffff; text-decoration: none;}"
2354 "a.lfsb:link {color: #000000; text-decoration: none;}"
2355 "a.lfsb:visited {color: #000000; text-decoration: none;}"
2356 "a.lfsb:hover {color: #505050; text-decoration: none;}"
2357 "\n"
2358 "table.tbl { border-collapse: collapse; border-style: none;}\n"
2359 "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"
2360 "table.tbl td.ac { text-align: center;}\n"
2361 "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}\n"
2362 "table.tbl th.pxname { background: #b00040; color: #ffff40; font-weight: bold; border-style: solid solid none solid; padding: 2px 3px; white-space: nowrap;}\n"
2363 "table.tbl th.empty { border-style: none; empty-cells: hide; background: white;}\n"
2364 "table.tbl th.desc { background: white; border-style: solid solid none solid; text-align: left; padding: 2px 3px;}\n"
2365 "\n"
2366 "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n"
2367 "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n"
2368 "table.lgd td.noborder { border-style: none; padding: 2px; white-space: nowrap;}\n"
2369 "table.det { border-collapse: collapse; border-style: none; }\n"
2370 "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"
2371 "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"
2372 "u {text-decoration:none; border-bottom: 1px dotted black;}\n"
2373 "div.tips {\n"
2374 " display:block;\n"
2375 " visibility:hidden;\n"
2376 " z-index:2147483647;\n"
2377 " position:absolute;\n"
2378 " padding:2px 4px 3px;\n"
2379 " background:#f0f060; color:#000000;\n"
2380 " border:1px solid #7040c0;\n"
2381 " white-space:nowrap;\n"
2382 " font-style:normal;font-size:11px;font-weight:normal;\n"
2383 " -moz-border-radius:3px;-webkit-border-radius:3px;border-radius:3px;\n"
2384 " -moz-box-shadow:gray 2px 2px 3px;-webkit-box-shadow:gray 2px 2px 3px;box-shadow:gray 2px 2px 3px;\n"
2385 "}\n"
2386 "u:hover div.tips {visibility:visible;}\n"
2387 "-->\n"
2388 "</style></head>\n",
Willy Tarreau708c4162019-10-09 10:19:16 +02002389 (uri->flags & STAT_SHNODE) ? " on " : "",
2390 (uri->flags & STAT_SHNODE) ? (uri->node ? uri->node : global.node) : ""
William Lallemand74c24fb2016-11-21 17:18:36 +01002391 );
2392}
2393
2394/* Dumps the HTML stats information block to the trash for and uses the state from
2395 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
2396 * for clearing the trash if needed.
2397 */
2398static void stats_dump_html_info(struct stream_interface *si, struct uri_auth *uri)
2399{
2400 struct appctx *appctx = __objt_appctx(si->end);
2401 unsigned int up = (now.tv_sec - start_date.tv_sec);
2402 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
Christopher Fauleted7a0662019-01-14 11:07:34 +01002403 const char *scope_ptr = stats_scope_ptr(appctx, si);
Willy Tarreau1713c032019-05-23 12:23:55 +02002404 unsigned long long bps = (unsigned long long)read_freq_ctr(&global.out_32bps) * 32;
2405
2406 /* Turn the bytes per second to bits per second and take care of the
2407 * usual ethernet overhead in order to help figure how far we are from
2408 * interface saturation since it's the only case which usually matters.
2409 * For this we count the total size of an Ethernet frame on the wire
2410 * including preamble and IFG (1538) for the largest TCP segment it
2411 * transports (1448 with TCP timestamps). This is not valid for smaller
2412 * packets (under-estimated), but it gives a reasonably accurate
2413 * estimation of how far we are from uplink saturation.
2414 */
2415 bps = bps * 8 * 1538 / 1448;
William Lallemand74c24fb2016-11-21 17:18:36 +01002416
2417 /* WARNING! this has to fit the first packet too.
2418 * We are around 3.5 kB, add adding entries will
2419 * become tricky if we want to support 4kB buffers !
2420 */
2421 chunk_appendf(&trash,
2422 "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">"
2423 PRODUCT_NAME "%s</a></h1>\n"
2424 "<h2>Statistics Report for pid %d%s%s%s%s</h2>\n"
2425 "<hr width=\"100%%\" class=\"hr\">\n"
2426 "<h3>&gt; General process information</h3>\n"
2427 "<table border=0><tr><td align=\"left\" nowrap width=\"1%%\">\n"
Yves Lafon95317282018-02-26 11:10:37 +01002428 "<p><b>pid = </b> %d (process #%d, nbproc = %d, nbthread = %d)<br>\n"
William Lallemand74c24fb2016-11-21 17:18:36 +01002429 "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n"
2430 "<b>system limits:</b> memmax = %s%s; ulimit-n = %d<br>\n"
2431 "<b>maxsock = </b> %d; <b>maxconn = </b> %d; <b>maxpipes = </b> %d<br>\n"
Willy Tarreau1713c032019-05-23 12:23:55 +02002432 "current conns = %d; current pipes = %d/%d; conn rate = %d/sec; bit rate = %.3f %cbps<br>\n"
William Lallemand74c24fb2016-11-21 17:18:36 +01002433 "Running tasks: %d/%d; idle = %d %%<br>\n"
2434 "</td><td align=\"center\" nowrap>\n"
2435 "<table class=\"lgd\"><tr>\n"
2436 "<td class=\"active_up\">&nbsp;</td><td class=\"noborder\">active UP </td>"
2437 "<td class=\"backup_up\">&nbsp;</td><td class=\"noborder\">backup UP </td>"
2438 "</tr><tr>\n"
2439 "<td class=\"active_going_down\"></td><td class=\"noborder\">active UP, going down </td>"
2440 "<td class=\"backup_going_down\"></td><td class=\"noborder\">backup UP, going down </td>"
2441 "</tr><tr>\n"
2442 "<td class=\"active_going_up\"></td><td class=\"noborder\">active DOWN, going up </td>"
2443 "<td class=\"backup_going_up\"></td><td class=\"noborder\">backup DOWN, going up </td>"
2444 "</tr><tr>\n"
2445 "<td class=\"active_down\"></td><td class=\"noborder\">active or backup DOWN &nbsp;</td>"
2446 "<td class=\"active_no_check\"></td><td class=\"noborder\">not checked </td>"
2447 "</tr><tr>\n"
2448 "<td class=\"maintain\"></td><td class=\"noborder\" colspan=\"3\">active or backup DOWN for maintenance (MAINT) &nbsp;</td>"
2449 "</tr><tr>\n"
2450 "<td class=\"active_draining\"></td><td class=\"noborder\" colspan=\"3\">active or backup SOFT STOPPED for maintenance &nbsp;</td>"
2451 "</tr></table>\n"
2452 "Note: \"NOLB\"/\"DRAIN\" = UP with load-balancing disabled."
2453 "</td>"
2454 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
2455 "<b>Display option:</b><ul style=\"margin-top: 0.25em;\">"
2456 "",
Willy Tarreau708c4162019-10-09 10:19:16 +02002457 (uri->flags & STAT_HIDEVER) ? "" : (stats_version_string),
2458 pid, (uri->flags & STAT_SHNODE) ? " on " : "",
2459 (uri->flags & STAT_SHNODE) ? (uri->node ? uri->node : global.node) : "",
2460 (uri->flags & STAT_SHDESC) ? ": " : "",
2461 (uri->flags & STAT_SHDESC) ? (uri->desc ? uri->desc : global.desc) : "",
Yves Lafon95317282018-02-26 11:10:37 +01002462 pid, relative_pid, global.nbproc, global.nbthread,
William Lallemand74c24fb2016-11-21 17:18:36 +01002463 up / 86400, (up % 86400) / 3600,
2464 (up % 3600) / 60, (up % 60),
2465 global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited",
2466 global.rlimit_memmax ? " MB" : "",
2467 global.rlimit_nofile,
2468 global.maxsock, global.maxconn, global.maxpipes,
2469 actconn, pipes_used, pipes_used+pipes_free, read_freq_ctr(&global.conn_per_sec),
Willy Tarreau1713c032019-05-23 12:23:55 +02002470 bps >= 1000000000UL ? (bps / 1000000000.0) : bps >= 1000000UL ? (bps / 1000000.0) : (bps / 1000.0),
2471 bps >= 1000000000UL ? 'G' : bps >= 1000000UL ? 'M' : 'k',
Willy Tarreau81036f22019-05-20 19:24:50 +02002472 tasks_run_queue_cur, nb_tasks_cur, ti->idle_pct
William Lallemand74c24fb2016-11-21 17:18:36 +01002473 );
2474
2475 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Christopher Fauleted7a0662019-01-14 11:07:34 +01002476 memcpy(scope_txt, scope_ptr, appctx->ctx.stats.scope_len);
William Lallemand74c24fb2016-11-21 17:18:36 +01002477 scope_txt[appctx->ctx.stats.scope_len] = '\0';
2478
2479 chunk_appendf(&trash,
2480 "<li><form method=\"GET\">Scope : <input value=\"%s\" name=\"" STAT_SCOPE_INPUT_NAME "\" size=\"8\" maxlength=\"%d\" tabindex=\"1\"/></form>\n",
2481 (appctx->ctx.stats.scope_len > 0) ? scope_txt : "",
2482 STAT_SCOPE_TXT_MAXLEN);
2483
2484 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
2485 scope_txt[0] = 0;
2486 if (appctx->ctx.stats.scope_len) {
2487 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Christopher Fauleted7a0662019-01-14 11:07:34 +01002488 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), scope_ptr, appctx->ctx.stats.scope_len);
William Lallemand74c24fb2016-11-21 17:18:36 +01002489 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
2490 }
2491
2492 if (appctx->ctx.stats.flags & STAT_HIDE_DOWN)
2493 chunk_appendf(&trash,
2494 "<li><a href=\"%s%s%s%s\">Show all servers</a><br>\n",
2495 uri->uri_prefix,
2496 "",
2497 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2498 scope_txt);
2499 else
2500 chunk_appendf(&trash,
2501 "<li><a href=\"%s%s%s%s\">Hide 'DOWN' servers</a><br>\n",
2502 uri->uri_prefix,
2503 ";up",
2504 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2505 scope_txt);
2506
2507 if (uri->refresh > 0) {
2508 if (appctx->ctx.stats.flags & STAT_NO_REFRESH)
2509 chunk_appendf(&trash,
2510 "<li><a href=\"%s%s%s%s\">Enable refresh</a><br>\n",
2511 uri->uri_prefix,
2512 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2513 "",
2514 scope_txt);
2515 else
2516 chunk_appendf(&trash,
2517 "<li><a href=\"%s%s%s%s\">Disable refresh</a><br>\n",
2518 uri->uri_prefix,
2519 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2520 ";norefresh",
2521 scope_txt);
2522 }
2523
2524 chunk_appendf(&trash,
2525 "<li><a href=\"%s%s%s%s\">Refresh now</a><br>\n",
2526 uri->uri_prefix,
2527 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2528 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2529 scope_txt);
2530
2531 chunk_appendf(&trash,
2532 "<li><a href=\"%s;csv%s%s\">CSV export</a><br>\n",
2533 uri->uri_prefix,
2534 (uri->refresh > 0) ? ";norefresh" : "",
2535 scope_txt);
2536
2537 chunk_appendf(&trash,
Christopher Faulet6338a082019-09-09 15:50:54 +02002538 "<li><a href=\"%s;json%s%s\">JSON export</a> (<a href=\"%s;json-schema\">schema</a>)<br>\n",
2539 uri->uri_prefix,
2540 (uri->refresh > 0) ? ";norefresh" : "",
2541 scope_txt, uri->uri_prefix);
2542
2543 chunk_appendf(&trash,
William Lallemand74c24fb2016-11-21 17:18:36 +01002544 "</ul></td>"
2545 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
2546 "<b>External resources:</b><ul style=\"margin-top: 0.25em;\">\n"
2547 "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>\n"
2548 "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>\n"
2549 "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>\n"
2550 "</ul>"
2551 "</td>"
2552 "</tr></table>\n"
2553 ""
2554 );
2555
2556 if (appctx->ctx.stats.st_code) {
2557 switch (appctx->ctx.stats.st_code) {
2558 case STAT_STATUS_DONE:
2559 chunk_appendf(&trash,
2560 "<p><div class=active_up>"
2561 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2562 "Action processed successfully."
2563 "</div>\n", uri->uri_prefix,
2564 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2565 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2566 scope_txt);
2567 break;
2568 case STAT_STATUS_NONE:
2569 chunk_appendf(&trash,
2570 "<p><div class=active_going_down>"
2571 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2572 "Nothing has changed."
2573 "</div>\n", uri->uri_prefix,
2574 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2575 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2576 scope_txt);
2577 break;
2578 case STAT_STATUS_PART:
2579 chunk_appendf(&trash,
2580 "<p><div class=active_going_down>"
2581 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2582 "Action partially processed.<br>"
2583 "Some server names are probably unknown or ambiguous (duplicated names in the backend)."
2584 "</div>\n", uri->uri_prefix,
2585 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2586 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2587 scope_txt);
2588 break;
2589 case STAT_STATUS_ERRP:
2590 chunk_appendf(&trash,
2591 "<p><div class=active_down>"
2592 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2593 "Action not processed because of invalid parameters."
2594 "<ul>"
2595 "<li>The action is maybe unknown.</li>"
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01002596 "<li>Invalid key parameter (empty or too long).</li>"
William Lallemand74c24fb2016-11-21 17:18:36 +01002597 "<li>The backend name is probably unknown or ambiguous (duplicated names).</li>"
2598 "<li>Some server names are probably unknown or ambiguous (duplicated names in the backend).</li>"
2599 "</ul>"
2600 "</div>\n", uri->uri_prefix,
2601 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2602 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2603 scope_txt);
2604 break;
2605 case STAT_STATUS_EXCD:
2606 chunk_appendf(&trash,
2607 "<p><div class=active_down>"
2608 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2609 "<b>Action not processed : the buffer couldn't store all the data.<br>"
2610 "You should retry with less servers at a time.</b>"
2611 "</div>\n", uri->uri_prefix,
2612 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2613 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2614 scope_txt);
2615 break;
2616 case STAT_STATUS_DENY:
2617 chunk_appendf(&trash,
2618 "<p><div class=active_down>"
2619 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2620 "<b>Action denied.</b>"
2621 "</div>\n", uri->uri_prefix,
2622 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2623 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2624 scope_txt);
2625 break;
Christopher Faulet3c2ecf72019-02-27 16:41:27 +01002626 case STAT_STATUS_IVAL:
2627 chunk_appendf(&trash,
2628 "<p><div class=active_down>"
2629 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2630 "<b>Invalid requests (unsupported method or chunked encoded request).</b>"
2631 "</div>\n", uri->uri_prefix,
2632 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2633 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2634 scope_txt);
2635 break;
William Lallemand74c24fb2016-11-21 17:18:36 +01002636 default:
2637 chunk_appendf(&trash,
2638 "<p><div class=active_no_check>"
2639 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2640 "Unexpected result."
2641 "</div>\n", uri->uri_prefix,
2642 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2643 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2644 scope_txt);
2645 }
2646 chunk_appendf(&trash, "<p>\n");
2647 }
2648}
2649
2650/* Dumps the HTML stats trailer block to the trash. The caller is responsible
2651 * for clearing the trash if needed.
2652 */
2653static void stats_dump_html_end()
2654{
2655 chunk_appendf(&trash, "</body></html>\n");
2656}
2657
Simon Horman05ee2132017-01-04 09:37:25 +01002658/* Dumps the stats JSON header to the trash buffer which. The caller is responsible
2659 * for clearing it if needed.
2660 */
2661static void stats_dump_json_header()
2662{
2663 chunk_strcat(&trash, "[");
2664}
2665
2666
2667/* Dumps the JSON stats trailer block to the trash. The caller is responsible
2668 * for clearing the trash if needed.
2669 */
2670static void stats_dump_json_end()
2671{
2672 chunk_strcat(&trash, "]");
2673}
2674
William Lallemand74c24fb2016-11-21 17:18:36 +01002675/* This function dumps statistics onto the stream interface's read buffer in
2676 * either CSV or HTML format. <uri> contains some HTML-specific parameters that
2677 * are ignored for CSV format (hence <uri> may be NULL there). It returns 0 if
2678 * it had to stop writing data and an I/O is needed, 1 if the dump is finished
2679 * and the stream must be closed, or -1 in case of any error. This function is
2680 * used by both the CLI and the HTTP handlers.
2681 */
Christopher Fauletef779222018-10-31 08:47:01 +01002682static int stats_dump_stat_to_buffer(struct stream_interface *si, struct htx *htx,
2683 struct uri_auth *uri)
William Lallemand74c24fb2016-11-21 17:18:36 +01002684{
2685 struct appctx *appctx = __objt_appctx(si->end);
2686 struct channel *rep = si_ic(si);
2687 struct proxy *px;
2688
2689 chunk_reset(&trash);
2690
2691 switch (appctx->st2) {
2692 case STAT_ST_INIT:
2693 appctx->st2 = STAT_ST_HEAD; /* let's start producing data */
2694 /* fall through */
2695
2696 case STAT_ST_HEAD:
2697 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
2698 stats_dump_html_head(uri);
Christopher Faulet6338a082019-09-09 15:50:54 +02002699 else if (appctx->ctx.stats.flags & STAT_JSON_SCHM)
2700 stats_dump_json_schema(&trash);
Simon Horman05ee2132017-01-04 09:37:25 +01002701 else if (appctx->ctx.stats.flags & STAT_FMT_JSON)
Willy Tarreau9d7fb632017-04-11 07:53:04 +02002702 stats_dump_json_header();
William Lallemand74c24fb2016-11-21 17:18:36 +01002703 else if (!(appctx->ctx.stats.flags & STAT_FMT_TYPED))
2704 stats_dump_csv_header();
2705
Christopher Fauletef779222018-10-31 08:47:01 +01002706 if (!stats_putchk(rep, htx, &trash))
2707 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002708
Christopher Faulet6338a082019-09-09 15:50:54 +02002709 if (appctx->ctx.stats.flags & STAT_JSON_SCHM) {
2710 appctx->st2 = STAT_ST_FIN;
2711 return 1;
2712 }
William Lallemand74c24fb2016-11-21 17:18:36 +01002713 appctx->st2 = STAT_ST_INFO;
2714 /* fall through */
2715
2716 case STAT_ST_INFO:
2717 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
2718 stats_dump_html_info(si, uri);
Christopher Fauletef779222018-10-31 08:47:01 +01002719 if (!stats_putchk(rep, htx, &trash))
2720 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002721 }
2722
Olivier Houchardfbc74e82017-11-24 16:54:05 +01002723 appctx->ctx.stats.px = proxies_list;
William Lallemand74c24fb2016-11-21 17:18:36 +01002724 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
2725 appctx->st2 = STAT_ST_LIST;
2726 /* fall through */
2727
2728 case STAT_ST_LIST:
2729 /* dump proxies */
2730 while (appctx->ctx.stats.px) {
Christopher Fauletef779222018-10-31 08:47:01 +01002731 if (htx) {
2732 if (htx_almost_full(htx))
2733 goto full;
2734 }
2735 else {
2736 if (buffer_almost_full(&rep->buf))
2737 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002738 }
2739
2740 px = appctx->ctx.stats.px;
2741 /* skip the disabled proxies, global frontend and non-networked ones */
2742 if (px->state != PR_STSTOPPED && px->uuid > 0 && (px->cap & (PR_CAP_FE | PR_CAP_BE)))
Christopher Fauletef779222018-10-31 08:47:01 +01002743 if (stats_dump_proxy_to_buffer(si, htx, px, uri) == 0)
William Lallemand74c24fb2016-11-21 17:18:36 +01002744 return 0;
2745
2746 appctx->ctx.stats.px = px->next;
2747 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
2748 }
2749 /* here, we just have reached the last proxy */
2750
2751 appctx->st2 = STAT_ST_END;
2752 /* fall through */
2753
2754 case STAT_ST_END:
Simon Horman05ee2132017-01-04 09:37:25 +01002755 if (appctx->ctx.stats.flags & (STAT_FMT_HTML|STAT_FMT_JSON)) {
2756 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
2757 stats_dump_html_end();
2758 else
2759 stats_dump_json_end();
Christopher Fauletef779222018-10-31 08:47:01 +01002760 if (!stats_putchk(rep, htx, &trash))
2761 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002762 }
2763
2764 appctx->st2 = STAT_ST_FIN;
2765 /* fall through */
2766
2767 case STAT_ST_FIN:
2768 return 1;
2769
2770 default:
2771 /* unknown state ! */
2772 appctx->st2 = STAT_ST_FIN;
2773 return -1;
2774 }
Christopher Fauletef779222018-10-31 08:47:01 +01002775
2776 full:
2777 si_rx_room_blk(si);
2778 return 0;
2779
William Lallemand74c24fb2016-11-21 17:18:36 +01002780}
2781
2782/* We reached the stats page through a POST request. The appctx is
2783 * expected to have already been allocated by the caller.
2784 * Parse the posted data and enable/disable servers if necessary.
2785 * Returns 1 if request was parsed or zero if it needs more data.
2786 */
2787static int stats_process_http_post(struct stream_interface *si)
2788{
2789 struct stream *s = si_strm(si);
2790 struct appctx *appctx = objt_appctx(si->end);
2791
2792 struct proxy *px = NULL;
2793 struct server *sv = NULL;
2794
2795 char key[LINESIZE];
2796 int action = ST_ADM_ACTION_NONE;
2797 int reprocess = 0;
2798
2799 int total_servers = 0;
2800 int altered_servers = 0;
2801
2802 char *first_param, *cur_param, *next_param, *end_params;
2803 char *st_cur_param = NULL;
2804 char *st_next_param = NULL;
2805
Christopher Fauleta3618372018-12-13 21:59:56 +01002806 struct buffer *temp = get_trash_chunk();
William Lallemand74c24fb2016-11-21 17:18:36 +01002807
Christopher Fauletb7f88902019-07-15 21:56:43 +02002808 struct htx *htx = htxbuf(&s->req.buf);
2809 struct htx_blk *blk;
Christopher Fauleta3618372018-12-13 21:59:56 +01002810
Christopher Fauletb7f88902019-07-15 21:56:43 +02002811 /* we need more data */
2812 if (s->txn->req.msg_state < HTTP_MSG_DONE) {
2813 /* check if we can receive more */
2814 if (htx_free_data_space(htx) <= global.tune.maxrewrite) {
2815 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
2816 goto out;
Christopher Fauleta3618372018-12-13 21:59:56 +01002817 }
Christopher Fauletb7f88902019-07-15 21:56:43 +02002818 goto wait;
2819 }
Christopher Fauleta3618372018-12-13 21:59:56 +01002820
Christopher Fauletb7f88902019-07-15 21:56:43 +02002821 /* The request was fully received. Copy data */
2822 blk = htx_get_head_blk(htx);
2823 while (blk) {
2824 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Fauleta3618372018-12-13 21:59:56 +01002825
Christopher Fauletb7f88902019-07-15 21:56:43 +02002826 if (type == HTX_BLK_EOM || type == HTX_BLK_TLR || type == HTX_BLK_EOT)
2827 break;
2828 if (type == HTX_BLK_DATA) {
2829 struct ist v = htx_get_blk_value(htx, blk);
Christopher Fauleta3618372018-12-13 21:59:56 +01002830
Christopher Fauletb7f88902019-07-15 21:56:43 +02002831 if (!chunk_memcat(temp, v.ptr, v.len)) {
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01002832 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
2833 goto out;
2834 }
Christopher Fauleta3618372018-12-13 21:59:56 +01002835 }
Christopher Fauletb7f88902019-07-15 21:56:43 +02002836 blk = htx_get_next_blk(htx, blk);
William Lallemand74c24fb2016-11-21 17:18:36 +01002837 }
2838
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002839 first_param = temp->area;
Christopher Fauleta3618372018-12-13 21:59:56 +01002840 end_params = temp->area + temp->data;
William Lallemand74c24fb2016-11-21 17:18:36 +01002841 cur_param = next_param = end_params;
2842 *end_params = '\0';
2843
2844 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
2845
2846 /*
2847 * Parse the parameters in reverse order to only store the last value.
2848 * From the html form, the backend and the action are at the end.
2849 */
2850 while (cur_param > first_param) {
2851 char *value;
2852 int poffset, plen;
2853
2854 cur_param--;
2855
2856 if ((*cur_param == '&') || (cur_param == first_param)) {
2857 reprocess_servers:
2858 /* Parse the key */
2859 poffset = (cur_param != first_param ? 1 : 0);
2860 plen = next_param - cur_param + (cur_param == first_param ? 1 : 0);
2861 if ((plen > 0) && (plen <= sizeof(key))) {
2862 strncpy(key, cur_param + poffset, plen);
2863 key[plen - 1] = '\0';
2864 } else {
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01002865 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
William Lallemand74c24fb2016-11-21 17:18:36 +01002866 goto out;
2867 }
2868
2869 /* Parse the value */
2870 value = key;
2871 while (*value != '\0' && *value != '=') {
2872 value++;
2873 }
2874 if (*value == '=') {
2875 /* Ok, a value is found, we can mark the end of the key */
2876 *value++ = '\0';
2877 }
2878 if (url_decode(key) < 0 || url_decode(value) < 0)
2879 break;
2880
2881 /* Now we can check the key to see what to do */
2882 if (!px && (strcmp(key, "b") == 0)) {
2883 if ((px = proxy_be_by_name(value)) == NULL) {
2884 /* the backend name is unknown or ambiguous (duplicate names) */
2885 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
2886 goto out;
2887 }
2888 }
2889 else if (!action && (strcmp(key, "action") == 0)) {
2890 if (strcmp(value, "ready") == 0) {
2891 action = ST_ADM_ACTION_READY;
2892 }
2893 else if (strcmp(value, "drain") == 0) {
2894 action = ST_ADM_ACTION_DRAIN;
2895 }
2896 else if (strcmp(value, "maint") == 0) {
2897 action = ST_ADM_ACTION_MAINT;
2898 }
2899 else if (strcmp(value, "shutdown") == 0) {
2900 action = ST_ADM_ACTION_SHUTDOWN;
2901 }
2902 else if (strcmp(value, "dhlth") == 0) {
2903 action = ST_ADM_ACTION_DHLTH;
2904 }
2905 else if (strcmp(value, "ehlth") == 0) {
2906 action = ST_ADM_ACTION_EHLTH;
2907 }
2908 else if (strcmp(value, "hrunn") == 0) {
2909 action = ST_ADM_ACTION_HRUNN;
2910 }
2911 else if (strcmp(value, "hnolb") == 0) {
2912 action = ST_ADM_ACTION_HNOLB;
2913 }
2914 else if (strcmp(value, "hdown") == 0) {
2915 action = ST_ADM_ACTION_HDOWN;
2916 }
2917 else if (strcmp(value, "dagent") == 0) {
2918 action = ST_ADM_ACTION_DAGENT;
2919 }
2920 else if (strcmp(value, "eagent") == 0) {
2921 action = ST_ADM_ACTION_EAGENT;
2922 }
2923 else if (strcmp(value, "arunn") == 0) {
2924 action = ST_ADM_ACTION_ARUNN;
2925 }
2926 else if (strcmp(value, "adown") == 0) {
2927 action = ST_ADM_ACTION_ADOWN;
2928 }
2929 /* else these are the old supported methods */
2930 else if (strcmp(value, "disable") == 0) {
2931 action = ST_ADM_ACTION_DISABLE;
2932 }
2933 else if (strcmp(value, "enable") == 0) {
2934 action = ST_ADM_ACTION_ENABLE;
2935 }
2936 else if (strcmp(value, "stop") == 0) {
2937 action = ST_ADM_ACTION_STOP;
2938 }
2939 else if (strcmp(value, "start") == 0) {
2940 action = ST_ADM_ACTION_START;
2941 }
2942 else {
2943 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
2944 goto out;
2945 }
2946 }
2947 else if (strcmp(key, "s") == 0) {
2948 if (!(px && action)) {
2949 /*
2950 * Indicates that we'll need to reprocess the parameters
2951 * as soon as backend and action are known
2952 */
2953 if (!reprocess) {
2954 st_cur_param = cur_param;
2955 st_next_param = next_param;
2956 }
2957 reprocess = 1;
2958 }
2959 else if ((sv = findserver(px, value)) != NULL) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002960 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
William Lallemand74c24fb2016-11-21 17:18:36 +01002961 switch (action) {
2962 case ST_ADM_ACTION_DISABLE:
Emeric Brun52a91d32017-08-31 14:41:55 +02002963 if (!(sv->cur_admin & SRV_ADMF_FMAINT)) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002964 altered_servers++;
2965 total_servers++;
2966 srv_set_admin_flag(sv, SRV_ADMF_FMAINT, "'disable' on stats page");
2967 }
2968 break;
2969 case ST_ADM_ACTION_ENABLE:
Emeric Brun52a91d32017-08-31 14:41:55 +02002970 if (sv->cur_admin & SRV_ADMF_FMAINT) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002971 altered_servers++;
2972 total_servers++;
2973 srv_clr_admin_flag(sv, SRV_ADMF_FMAINT);
2974 }
2975 break;
2976 case ST_ADM_ACTION_STOP:
Emeric Brun52a91d32017-08-31 14:41:55 +02002977 if (!(sv->cur_admin & SRV_ADMF_FDRAIN)) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002978 srv_set_admin_flag(sv, SRV_ADMF_FDRAIN, "'stop' on stats page");
2979 altered_servers++;
2980 total_servers++;
2981 }
2982 break;
2983 case ST_ADM_ACTION_START:
Emeric Brun52a91d32017-08-31 14:41:55 +02002984 if (sv->cur_admin & SRV_ADMF_FDRAIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002985 srv_clr_admin_flag(sv, SRV_ADMF_FDRAIN);
2986 altered_servers++;
2987 total_servers++;
2988 }
2989 break;
2990 case ST_ADM_ACTION_DHLTH:
2991 if (sv->check.state & CHK_ST_CONFIGURED) {
2992 sv->check.state &= ~CHK_ST_ENABLED;
2993 altered_servers++;
2994 total_servers++;
2995 }
2996 break;
2997 case ST_ADM_ACTION_EHLTH:
2998 if (sv->check.state & CHK_ST_CONFIGURED) {
2999 sv->check.state |= CHK_ST_ENABLED;
3000 altered_servers++;
3001 total_servers++;
3002 }
3003 break;
3004 case ST_ADM_ACTION_HRUNN:
3005 if (!(sv->track)) {
3006 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02003007 srv_set_running(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003008 altered_servers++;
3009 total_servers++;
3010 }
3011 break;
3012 case ST_ADM_ACTION_HNOLB:
3013 if (!(sv->track)) {
3014 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02003015 srv_set_stopping(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003016 altered_servers++;
3017 total_servers++;
3018 }
3019 break;
3020 case ST_ADM_ACTION_HDOWN:
3021 if (!(sv->track)) {
3022 sv->check.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02003023 srv_set_stopped(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003024 altered_servers++;
3025 total_servers++;
3026 }
3027 break;
3028 case ST_ADM_ACTION_DAGENT:
3029 if (sv->agent.state & CHK_ST_CONFIGURED) {
3030 sv->agent.state &= ~CHK_ST_ENABLED;
3031 altered_servers++;
3032 total_servers++;
3033 }
3034 break;
3035 case ST_ADM_ACTION_EAGENT:
3036 if (sv->agent.state & CHK_ST_CONFIGURED) {
3037 sv->agent.state |= CHK_ST_ENABLED;
3038 altered_servers++;
3039 total_servers++;
3040 }
3041 break;
3042 case ST_ADM_ACTION_ARUNN:
3043 if (sv->agent.state & CHK_ST_ENABLED) {
3044 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02003045 srv_set_running(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003046 altered_servers++;
3047 total_servers++;
3048 }
3049 break;
3050 case ST_ADM_ACTION_ADOWN:
3051 if (sv->agent.state & CHK_ST_ENABLED) {
3052 sv->agent.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02003053 srv_set_stopped(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003054 altered_servers++;
3055 total_servers++;
3056 }
3057 break;
3058 case ST_ADM_ACTION_READY:
3059 srv_adm_set_ready(sv);
3060 altered_servers++;
3061 total_servers++;
3062 break;
3063 case ST_ADM_ACTION_DRAIN:
3064 srv_adm_set_drain(sv);
3065 altered_servers++;
3066 total_servers++;
3067 break;
3068 case ST_ADM_ACTION_MAINT:
3069 srv_adm_set_maint(sv);
3070 altered_servers++;
3071 total_servers++;
3072 break;
3073 case ST_ADM_ACTION_SHUTDOWN:
3074 if (px->state != PR_STSTOPPED) {
3075 struct stream *sess, *sess_bck;
3076
3077 list_for_each_entry_safe(sess, sess_bck, &sv->actconns, by_srv)
3078 if (sess->srv_conn == sv)
3079 stream_shutdown(sess, SF_ERR_KILLED);
3080
3081 altered_servers++;
3082 total_servers++;
3083 }
3084 break;
3085 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003086 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Lallemand74c24fb2016-11-21 17:18:36 +01003087 } else {
3088 /* the server name is unknown or ambiguous (duplicate names) */
3089 total_servers++;
3090 }
3091 }
3092 if (reprocess && px && action) {
3093 /* Now, we know the backend and the action chosen by the user.
3094 * We can safely restart from the first server parameter
3095 * to reprocess them
3096 */
3097 cur_param = st_cur_param;
3098 next_param = st_next_param;
3099 reprocess = 0;
3100 goto reprocess_servers;
3101 }
3102
3103 next_param = cur_param;
3104 }
3105 }
3106
3107 if (total_servers == 0) {
3108 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
3109 }
3110 else if (altered_servers == 0) {
3111 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
3112 }
3113 else if (altered_servers == total_servers) {
3114 appctx->ctx.stats.st_code = STAT_STATUS_DONE;
3115 }
3116 else {
3117 appctx->ctx.stats.st_code = STAT_STATUS_PART;
3118 }
3119 out:
3120 return 1;
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01003121 wait:
3122 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
3123 return 0;
Christopher Fauletef779222018-10-31 08:47:01 +01003124}
3125
3126
Christopher Fauletb7f88902019-07-15 21:56:43 +02003127static int stats_send_http_headers(struct stream_interface *si, struct htx *htx)
Christopher Fauletef779222018-10-31 08:47:01 +01003128{
3129 struct stream *s = si_strm(si);
3130 struct uri_auth *uri = s->be->uri_auth;
3131 struct appctx *appctx = __objt_appctx(si->end);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003132 struct htx_sl *sl;
3133 unsigned int flags;
Christopher Fauletef779222018-10-31 08:47:01 +01003134
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003135 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);
3136 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist("200"), ist("OK"));
3137 if (!sl)
Christopher Fauletef779222018-10-31 08:47:01 +01003138 goto full;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003139 sl->info.res.status = 200;
Christopher Fauletef779222018-10-31 08:47:01 +01003140
Christopher Fauletb829f4c2019-03-29 16:13:55 +01003141 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")))
Christopher Fauletef779222018-10-31 08:47:01 +01003142 goto full;
3143 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
3144 if (!htx_add_header(htx, ist("Content-Type"), ist("text/html")))
3145 goto full;
3146 }
Christopher Faulet6338a082019-09-09 15:50:54 +02003147 else if (appctx->ctx.stats.flags & (STAT_FMT_JSON|STAT_JSON_SCHM)) {
3148 if (!htx_add_header(htx, ist("Content-Type"), ist("application/json")))
3149 goto full;
3150 }
Christopher Fauletef779222018-10-31 08:47:01 +01003151 else {
3152 if (!htx_add_header(htx, ist("Content-Type"), ist("text/plain")))
3153 goto full;
3154 }
3155
3156 if (uri->refresh > 0 && !(appctx->ctx.stats.flags & STAT_NO_REFRESH)) {
3157 const char *refresh = U2A(uri->refresh);
3158 if (!htx_add_header(htx, ist("Refresh"), ist2(refresh, strlen(refresh))))
3159 goto full;
3160 }
3161
3162 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
3163 if (!htx_add_header(htx, ist("Transfer-Encoding"), ist("chunked")))
3164 goto full;
3165 }
3166
3167 if (!htx_add_endof(htx, HTX_BLK_EOH))
3168 goto full;
3169
Christopher Faulet1e2d6362019-02-27 16:28:48 +01003170 channel_add_input(&s->res, htx->data);
Christopher Fauletef779222018-10-31 08:47:01 +01003171 return 1;
3172
3173 full:
3174 htx_reset(htx);
3175 si_rx_room_blk(si);
3176 return 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01003177}
3178
Christopher Fauletef779222018-10-31 08:47:01 +01003179
Christopher Fauletb7f88902019-07-15 21:56:43 +02003180static int stats_send_http_redirect(struct stream_interface *si, struct htx *htx)
Christopher Fauletef779222018-10-31 08:47:01 +01003181{
3182 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
3183 struct stream *s = si_strm(si);
3184 struct uri_auth *uri = s->be->uri_auth;
3185 struct appctx *appctx = __objt_appctx(si->end);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003186 struct htx_sl *sl;
3187 unsigned int flags;
Christopher Fauletef779222018-10-31 08:47:01 +01003188
3189 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
3190 scope_txt[0] = 0;
3191 if (appctx->ctx.stats.scope_len) {
Christopher Fauleted7a0662019-01-14 11:07:34 +01003192 const char *scope_ptr = stats_scope_ptr(appctx, si);
3193
Christopher Fauletef779222018-10-31 08:47:01 +01003194 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Christopher Fauleted7a0662019-01-14 11:07:34 +01003195 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), scope_ptr, appctx->ctx.stats.scope_len);
Christopher Fauletef779222018-10-31 08:47:01 +01003196 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
3197 }
3198
3199 /* We don't want to land on the posted stats page because a refresh will
3200 * repost the data. We don't want this to happen on accident so we redirect
3201 * the browse to the stats page with a GET.
3202 */
3203 chunk_printf(&trash, "%s;st=%s%s%s%s",
3204 uri->uri_prefix,
3205 ((appctx->ctx.stats.st_code > STAT_STATUS_INIT) &&
3206 (appctx->ctx.stats.st_code < STAT_STATUS_SIZE) &&
3207 stat_status_codes[appctx->ctx.stats.st_code]) ?
3208 stat_status_codes[appctx->ctx.stats.st_code] :
3209 stat_status_codes[STAT_STATUS_UNKN],
3210 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3211 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3212 scope_txt);
3213
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003214 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_CHNK);
3215 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist("303"), ist("See Other"));
3216 if (!sl)
Christopher Fauletef779222018-10-31 08:47:01 +01003217 goto full;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003218 sl->info.res.status = 303;
Christopher Fauletef779222018-10-31 08:47:01 +01003219
3220 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")) ||
Christopher Fauletef779222018-10-31 08:47:01 +01003221 !htx_add_header(htx, ist("Content-Type"), ist("text/plain")) ||
3222 !htx_add_header(htx, ist("Content-Length"), ist("0")) ||
3223 !htx_add_header(htx, ist("Location"), ist2(trash.area, trash.data)))
3224 goto full;
3225
3226 if (!htx_add_endof(htx, HTX_BLK_EOH))
3227 goto full;
3228
Christopher Faulet1e2d6362019-02-27 16:28:48 +01003229 channel_add_input(&s->res, htx->data);
Christopher Fauletef779222018-10-31 08:47:01 +01003230 return 1;
3231
3232full:
3233 htx_reset(htx);
3234 si_rx_room_blk(si);
3235 return 0;
3236}
William Lallemand74c24fb2016-11-21 17:18:36 +01003237
Simon Horman05ee2132017-01-04 09:37:25 +01003238
William Lallemand74c24fb2016-11-21 17:18:36 +01003239/* This I/O handler runs as an applet embedded in a stream interface. It is
3240 * used to send HTTP stats over a TCP socket. The mechanism is very simple.
3241 * appctx->st0 contains the operation in progress (dump, done). The handler
3242 * automatically unregisters itself once transfer is complete.
3243 */
Christopher Fauletb7f88902019-07-15 21:56:43 +02003244static void http_stats_io_handler(struct appctx *appctx)
Christopher Fauletef779222018-10-31 08:47:01 +01003245{
3246 struct stream_interface *si = appctx->owner;
3247 struct stream *s = si_strm(si);
3248 struct channel *req = si_oc(si);
3249 struct channel *res = si_ic(si);
3250 struct htx *req_htx, *res_htx;
3251
3252 res_htx = htx_from_buf(&res->buf);
3253
3254 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
3255 goto out;
3256
3257 /* Check if the input buffer is avalaible. */
3258 if (!b_size(&res->buf)) {
3259 si_rx_room_blk(si);
3260 goto out;
3261 }
3262
3263 /* check that the output is not closed */
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003264 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_SHUTR))
3265 appctx->st0 = STAT_HTTP_END;
Christopher Fauletef779222018-10-31 08:47:01 +01003266
3267 /* all states are processed in sequence */
3268 if (appctx->st0 == STAT_HTTP_HEAD) {
Christopher Fauletb7f88902019-07-15 21:56:43 +02003269 if (stats_send_http_headers(si, res_htx)) {
Christopher Fauletef779222018-10-31 08:47:01 +01003270 if (s->txn->meth == HTTP_METH_HEAD)
3271 appctx->st0 = STAT_HTTP_DONE;
3272 else
3273 appctx->st0 = STAT_HTTP_DUMP;
3274 }
3275 }
3276
3277 if (appctx->st0 == STAT_HTTP_DUMP) {
3278 if (stats_dump_stat_to_buffer(si, res_htx, s->be->uri_auth))
3279 appctx->st0 = STAT_HTTP_DONE;
3280 }
3281
3282 if (appctx->st0 == STAT_HTTP_POST) {
3283 if (stats_process_http_post(si))
3284 appctx->st0 = STAT_HTTP_LAST;
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003285 else if (req->flags & CF_SHUTR)
Christopher Fauletef779222018-10-31 08:47:01 +01003286 appctx->st0 = STAT_HTTP_DONE;
3287 }
3288
3289 if (appctx->st0 == STAT_HTTP_LAST) {
Christopher Fauletb7f88902019-07-15 21:56:43 +02003290 if (stats_send_http_redirect(si, res_htx))
Christopher Fauletef779222018-10-31 08:47:01 +01003291 appctx->st0 = STAT_HTTP_DONE;
3292 }
3293
3294 if (appctx->st0 == STAT_HTTP_DONE) {
Christopher Faulet54b5e212019-06-04 10:08:28 +02003295 /* Don't add TLR because mux-h1 will take care of it */
Christopher Fauletef779222018-10-31 08:47:01 +01003296 if (!htx_add_endof(res_htx, HTX_BLK_EOM)) {
3297 si_rx_room_blk(si);
3298 goto out;
3299 }
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003300 channel_add_input(&s->res, 1);
3301 appctx->st0 = STAT_HTTP_END;
Christopher Fauletef779222018-10-31 08:47:01 +01003302 }
3303
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003304 if (appctx->st0 == STAT_HTTP_END) {
3305 if (!(res->flags & CF_SHUTR)) {
Christopher Fauletef779222018-10-31 08:47:01 +01003306 res->flags |= CF_READ_NULL;
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003307 si_shutr(si);
3308 }
3309
3310 /* eat the whole request */
3311 if (co_data(req)) {
3312 req_htx = htx_from_buf(&req->buf);
3313 co_htx_skip(req, req_htx, co_data(req));
3314 htx_to_buf(req_htx, &req->buf);
Christopher Fauletef779222018-10-31 08:47:01 +01003315 }
3316 }
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003317
Christopher Fauletef779222018-10-31 08:47:01 +01003318 out:
3319 /* we have left the request in the buffer for the case where we
3320 * process a POST, and this automatically re-enables activity on
3321 * read. It's better to indicate that we want to stop reading when
3322 * we're sending, so that we know there's at most one direction
3323 * deciding to wake the applet up. It saves it from looping when
3324 * emitting large blocks into small TCP windows.
3325 */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003326 htx_to_buf(res_htx, &res->buf);
3327 if (!channel_is_empty(res))
Christopher Fauletef779222018-10-31 08:47:01 +01003328 si_stop_get(si);
Christopher Fauletef779222018-10-31 08:47:01 +01003329}
3330
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003331/* Dump all fields from <info> into <out> using the "show info" format (name: value) */
Willy Tarreau83061a82018-07-13 11:56:34 +02003332static int stats_dump_info_fields(struct buffer *out,
3333 const struct field *info)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003334{
3335 int field;
3336
3337 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
3338 if (!field_format(info, field))
3339 continue;
3340
3341 if (!chunk_appendf(out, "%s: ", info_field_names[field]))
3342 return 0;
3343 if (!stats_emit_raw_data_field(out, &info[field]))
3344 return 0;
3345 if (!chunk_strcat(out, "\n"))
3346 return 0;
3347 }
3348 return 1;
3349}
3350
3351/* Dump all fields from <info> into <out> using the "show info typed" format */
Willy Tarreau83061a82018-07-13 11:56:34 +02003352static int stats_dump_typed_info_fields(struct buffer *out,
3353 const struct field *info)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003354{
3355 int field;
3356
3357 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
3358 if (!field_format(info, field))
3359 continue;
3360
3361 if (!chunk_appendf(out, "%d.%s.%u:", field, info_field_names[field], info[INF_PROCESS_NUM].u.u32))
3362 return 0;
3363 if (!stats_emit_field_tags(out, &info[field], ':'))
3364 return 0;
3365 if (!stats_emit_typed_data_field(out, &info[field]))
3366 return 0;
3367 if (!chunk_strcat(out, "\n"))
3368 return 0;
3369 }
3370 return 1;
3371}
3372
3373/* Fill <info> with HAProxy global info. <info> is preallocated
3374 * array of length <len>. The length of the aray must be
3375 * INF_TOTAL_FIELDS. If this length is less then this value, the
3376 * function returns 0, otherwise, it returns 1.
3377 */
3378int stats_fill_info(struct field *info, int len)
3379{
3380 unsigned int up = (now.tv_sec - start_date.tv_sec);
Willy Tarreau83061a82018-07-13 11:56:34 +02003381 struct buffer *out = get_trash_chunk();
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003382
3383#ifdef USE_OPENSSL
3384 int ssl_sess_rate = read_freq_ctr(&global.ssl_per_sec);
3385 int ssl_key_rate = read_freq_ctr(&global.ssl_fe_keys_per_sec);
3386 int ssl_reuse = 0;
3387
3388 if (ssl_key_rate < ssl_sess_rate) {
3389 /* count the ssl reuse ratio and avoid overflows in both directions */
3390 ssl_reuse = 100 - (100 * ssl_key_rate + (ssl_sess_rate - 1) / 2) / ssl_sess_rate;
3391 }
3392#endif
3393
3394 if (len < INF_TOTAL_FIELDS)
3395 return 0;
3396
3397 chunk_reset(out);
3398 memset(info, 0, sizeof(*info) * len);
3399
3400 info[INF_NAME] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, PRODUCT_NAME);
Willy Tarreau909b9d82019-01-04 18:20:32 +01003401 info[INF_VERSION] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, haproxy_version);
3402 info[INF_RELEASE_DATE] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, haproxy_date);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003403
Yves Lafon95317282018-02-26 11:10:37 +01003404 info[INF_NBTHREAD] = mkf_u32(FO_CONFIG|FS_SERVICE, global.nbthread);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003405 info[INF_NBPROC] = mkf_u32(FO_CONFIG|FS_SERVICE, global.nbproc);
3406 info[INF_PROCESS_NUM] = mkf_u32(FO_KEY, relative_pid);
3407 info[INF_PID] = mkf_u32(FO_STATUS, pid);
3408
3409 info[INF_UPTIME] = mkf_str(FN_DURATION, chunk_newstr(out));
3410 chunk_appendf(out, "%ud %uh%02um%02us", up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60));
3411
3412 info[INF_UPTIME_SEC] = mkf_u32(FN_DURATION, up);
3413 info[INF_MEMMAX_MB] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_memmax);
3414 info[INF_POOL_ALLOC_MB] = mkf_u32(0, (unsigned)(pool_total_allocated() / 1048576L));
3415 info[INF_POOL_USED_MB] = mkf_u32(0, (unsigned)(pool_total_used() / 1048576L));
3416 info[INF_POOL_FAILED] = mkf_u32(FN_COUNTER, pool_total_failures());
3417 info[INF_ULIMIT_N] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_nofile);
3418 info[INF_MAXSOCK] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxsock);
3419 info[INF_MAXCONN] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxconn);
3420 info[INF_HARD_MAXCONN] = mkf_u32(FO_CONFIG|FN_LIMIT, global.hardmaxconn);
3421 info[INF_CURR_CONN] = mkf_u32(0, actconn);
3422 info[INF_CUM_CONN] = mkf_u32(FN_COUNTER, totalconn);
3423 info[INF_CUM_REQ] = mkf_u32(FN_COUNTER, global.req_count);
3424#ifdef USE_OPENSSL
3425 info[INF_MAX_SSL_CONNS] = mkf_u32(FN_MAX, global.maxsslconn);
3426 info[INF_CURR_SSL_CONNS] = mkf_u32(0, sslconns);
3427 info[INF_CUM_SSL_CONNS] = mkf_u32(FN_COUNTER, totalsslconns);
3428#endif
3429 info[INF_MAXPIPES] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxpipes);
3430 info[INF_PIPES_USED] = mkf_u32(0, pipes_used);
3431 info[INF_PIPES_FREE] = mkf_u32(0, pipes_free);
3432 info[INF_CONN_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.conn_per_sec));
3433 info[INF_CONN_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.cps_lim);
3434 info[INF_MAX_CONN_RATE] = mkf_u32(FN_MAX, global.cps_max);
3435 info[INF_SESS_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.sess_per_sec));
3436 info[INF_SESS_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.sps_lim);
3437 info[INF_MAX_SESS_RATE] = mkf_u32(FN_RATE, global.sps_max);
3438
3439#ifdef USE_OPENSSL
3440 info[INF_SSL_RATE] = mkf_u32(FN_RATE, ssl_sess_rate);
3441 info[INF_SSL_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.ssl_lim);
3442 info[INF_MAX_SSL_RATE] = mkf_u32(FN_MAX, global.ssl_max);
3443 info[INF_SSL_FRONTEND_KEY_RATE] = mkf_u32(0, ssl_key_rate);
3444 info[INF_SSL_FRONTEND_MAX_KEY_RATE] = mkf_u32(FN_MAX, global.ssl_fe_keys_max);
3445 info[INF_SSL_FRONTEND_SESSION_REUSE_PCT] = mkf_u32(0, ssl_reuse);
3446 info[INF_SSL_BACKEND_KEY_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.ssl_be_keys_per_sec));
3447 info[INF_SSL_BACKEND_MAX_KEY_RATE] = mkf_u32(FN_MAX, global.ssl_be_keys_max);
3448 info[INF_SSL_CACHE_LOOKUPS] = mkf_u32(FN_COUNTER, global.shctx_lookups);
3449 info[INF_SSL_CACHE_MISSES] = mkf_u32(FN_COUNTER, global.shctx_misses);
3450#endif
3451 info[INF_COMPRESS_BPS_IN] = mkf_u32(FN_RATE, read_freq_ctr(&global.comp_bps_in));
3452 info[INF_COMPRESS_BPS_OUT] = mkf_u32(FN_RATE, read_freq_ctr(&global.comp_bps_out));
3453 info[INF_COMPRESS_BPS_RATE_LIM] = mkf_u32(FO_CONFIG|FN_LIMIT, global.comp_rate_lim);
3454#ifdef USE_ZLIB
3455 info[INF_ZLIB_MEM_USAGE] = mkf_u32(0, zlib_used_memory);
3456 info[INF_MAX_ZLIB_MEM_USAGE] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxzlibmem);
3457#endif
3458 info[INF_TASKS] = mkf_u32(0, nb_tasks_cur);
Christopher Faulet34c5cc92016-12-06 09:15:30 +01003459 info[INF_RUN_QUEUE] = mkf_u32(0, tasks_run_queue_cur);
Willy Tarreau81036f22019-05-20 19:24:50 +02003460 info[INF_IDLE_PCT] = mkf_u32(FN_AVG, ti->idle_pct);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003461 info[INF_NODE] = mkf_str(FO_CONFIG|FN_OUTPUT|FS_SERVICE, global.node);
3462 if (global.desc)
3463 info[INF_DESCRIPTION] = mkf_str(FO_CONFIG|FN_OUTPUT|FS_SERVICE, global.desc);
Willy Tarreau00098ea2018-11-05 14:38:13 +01003464 info[INF_STOPPING] = mkf_u32(0, stopping);
3465 info[INF_JOBS] = mkf_u32(0, jobs);
William Lallemanda7199262018-11-16 16:57:20 +01003466 info[INF_UNSTOPPABLE_JOBS] = mkf_u32(0, unstoppable_jobs);
Willy Tarreau00098ea2018-11-05 14:38:13 +01003467 info[INF_LISTENERS] = mkf_u32(0, listeners);
Willy Tarreau199ad242018-11-05 16:31:22 +01003468 info[INF_ACTIVE_PEERS] = mkf_u32(0, active_peers);
Willy Tarreau2d372c22018-11-05 17:12:27 +01003469 info[INF_CONNECTED_PEERS] = mkf_u32(0, connected_peers);
Willy Tarreau13ef7732018-11-12 07:25:28 +01003470 info[INF_DROPPED_LOGS] = mkf_u32(0, dropped_logs);
Willy Tarreaubeb859a2018-11-22 18:07:59 +01003471 info[INF_BUSY_POLLING] = mkf_u32(0, !!(global.tune.options & GTUNE_BUSY_POLLING));
Baptiste Assmann333939c2019-01-21 08:34:50 +01003472 info[INF_FAILED_RESOLUTIONS] = mkf_u32(0, dns_failed_resolutions);
Willy Tarreau7cf0e452019-05-23 11:39:14 +02003473 info[INF_TOTAL_BYTES_OUT] = mkf_u64(0, global.out_bytes);
3474 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 +01003475
3476 return 1;
3477}
3478
3479/* This function dumps information onto the stream interface's read buffer.
3480 * It returns 0 as long as it does not complete, non-zero upon completion.
3481 * No state is used.
3482 */
3483static int stats_dump_info_to_buffer(struct stream_interface *si)
3484{
3485 struct appctx *appctx = __objt_appctx(si->end);
3486
3487 if (!stats_fill_info(info, INF_TOTAL_FIELDS))
3488 return 0;
3489
3490 chunk_reset(&trash);
3491
3492 if (appctx->ctx.stats.flags & STAT_FMT_TYPED)
3493 stats_dump_typed_info_fields(&trash, info);
Simon Horman05ee2132017-01-04 09:37:25 +01003494 else if (appctx->ctx.stats.flags & STAT_FMT_JSON)
3495 stats_dump_json_info_fields(&trash, info);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003496 else
3497 stats_dump_info_fields(&trash, info);
3498
Willy Tarreau06d80a92017-10-19 14:32:15 +02003499 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01003500 si_rx_room_blk(si);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003501 return 0;
3502 }
3503
3504 return 1;
3505}
3506
Simon Horman6f6bb382017-01-04 09:37:26 +01003507/* This function dumps the schema onto the stream interface's read buffer.
3508 * It returns 0 as long as it does not complete, non-zero upon completion.
3509 * No state is used.
3510 *
3511 * Integer values bouned to the range [-(2**53)+1, (2**53)-1] as
3512 * per the recommendation for interoperable integers in section 6 of RFC 7159.
3513 */
Willy Tarreau83061a82018-07-13 11:56:34 +02003514static void stats_dump_json_schema(struct buffer *out)
Simon Horman6f6bb382017-01-04 09:37:26 +01003515{
3516
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003517 int old_len = out->data;
Simon Horman6f6bb382017-01-04 09:37:26 +01003518
3519 chunk_strcat(out,
3520 "{"
3521 "\"$schema\":\"http://json-schema.org/draft-04/schema#\","
3522 "\"oneOf\":["
3523 "{"
3524 "\"title\":\"Info\","
3525 "\"type\":\"array\","
3526 "\"items\":{"
3527 "\"properties\":{"
3528 "\"title\":\"InfoItem\","
3529 "\"type\":\"object\","
3530 "\"field\":{\"$ref\":\"#/definitions/field\"},"
3531 "\"processNum\":{\"$ref\":\"#/definitions/processNum\"},"
3532 "\"tags\":{\"$ref\":\"#/definitions/tags\"},"
3533 "\"value\":{\"$ref\":\"#/definitions/typedValue\"}"
3534 "},"
3535 "\"required\":[\"field\",\"processNum\",\"tags\","
3536 "\"value\"]"
3537 "}"
3538 "},"
3539 "{"
3540 "\"title\":\"Stat\","
3541 "\"type\":\"array\","
3542 "\"items\":{"
3543 "\"title\":\"InfoItem\","
3544 "\"type\":\"object\","
3545 "\"properties\":{"
3546 "\"objType\":{"
3547 "\"enum\":[\"Frontend\",\"Backend\",\"Listener\","
3548 "\"Server\",\"Unknown\"]"
3549 "},"
3550 "\"proxyId\":{"
3551 "\"type\":\"integer\","
3552 "\"minimum\":0"
3553 "},"
3554 "\"id\":{"
3555 "\"type\":\"integer\","
3556 "\"minimum\":0"
3557 "},"
3558 "\"field\":{\"$ref\":\"#/definitions/field\"},"
3559 "\"processNum\":{\"$ref\":\"#/definitions/processNum\"},"
3560 "\"tags\":{\"$ref\":\"#/definitions/tags\"},"
3561 "\"typedValue\":{\"$ref\":\"#/definitions/typedValue\"}"
3562 "},"
3563 "\"required\":[\"objType\",\"proxyId\",\"id\","
3564 "\"field\",\"processNum\",\"tags\","
3565 "\"value\"]"
3566 "}"
3567 "},"
3568 "{"
3569 "\"title\":\"Error\","
3570 "\"type\":\"object\","
3571 "\"properties\":{"
3572 "\"errorStr\":{"
3573 "\"type\":\"string\""
3574 "},"
3575 "\"required\":[\"errorStr\"]"
3576 "}"
3577 "}"
3578 "],"
3579 "\"definitions\":{"
3580 "\"field\":{"
3581 "\"type\":\"object\","
3582 "\"pos\":{"
3583 "\"type\":\"integer\","
3584 "\"minimum\":0"
3585 "},"
3586 "\"name\":{"
3587 "\"type\":\"string\""
3588 "},"
3589 "\"required\":[\"pos\",\"name\"]"
3590 "},"
3591 "\"processNum\":{"
3592 "\"type\":\"integer\","
3593 "\"minimum\":1"
3594 "},"
3595 "\"tags\":{"
3596 "\"type\":\"object\","
3597 "\"origin\":{"
3598 "\"type\":\"string\","
3599 "\"enum\":[\"Metric\",\"Status\",\"Key\","
3600 "\"Config\",\"Product\",\"Unknown\"]"
3601 "},"
3602 "\"nature\":{"
3603 "\"type\":\"string\","
3604 "\"enum\":[\"Gauge\",\"Limit\",\"Min\",\"Max\","
3605 "\"Rate\",\"Counter\",\"Duration\","
3606 "\"Age\",\"Time\",\"Name\",\"Output\","
3607 "\"Avg\", \"Unknown\"]"
3608 "},"
3609 "\"scope\":{"
3610 "\"type\":\"string\","
3611 "\"enum\":[\"Cluster\",\"Process\",\"Service\","
3612 "\"System\",\"Unknown\"]"
3613 "},"
3614 "\"required\":[\"origin\",\"nature\",\"scope\"]"
3615 "},"
3616 "\"typedValue\":{"
3617 "\"type\":\"object\","
3618 "\"oneOf\":["
3619 "{\"$ref\":\"#/definitions/typedValue/definitions/s32Value\"},"
3620 "{\"$ref\":\"#/definitions/typedValue/definitions/s64Value\"},"
3621 "{\"$ref\":\"#/definitions/typedValue/definitions/u32Value\"},"
3622 "{\"$ref\":\"#/definitions/typedValue/definitions/u64Value\"},"
3623 "{\"$ref\":\"#/definitions/typedValue/definitions/strValue\"}"
3624 "],"
3625 "\"definitions\":{"
3626 "\"s32Value\":{"
3627 "\"properties\":{"
3628 "\"type\":{"
3629 "\"type\":\"string\","
3630 "\"enum\":[\"s32\"]"
3631 "},"
3632 "\"value\":{"
3633 "\"type\":\"integer\","
3634 "\"minimum\":-2147483648,"
3635 "\"maximum\":2147483647"
3636 "}"
3637 "},"
3638 "\"required\":[\"type\",\"value\"]"
3639 "},"
3640 "\"s64Value\":{"
3641 "\"properties\":{"
3642 "\"type\":{"
3643 "\"type\":\"string\","
3644 "\"enum\":[\"s64\"]"
3645 "},"
3646 "\"value\":{"
3647 "\"type\":\"integer\","
3648 "\"minimum\":-9007199254740991,"
3649 "\"maximum\":9007199254740991"
3650 "}"
3651 "},"
3652 "\"required\":[\"type\",\"value\"]"
3653 "},"
3654 "\"u32Value\":{"
3655 "\"properties\":{"
3656 "\"type\":{"
3657 "\"type\":\"string\","
3658 "\"enum\":[\"u32\"]"
3659 "},"
3660 "\"value\":{"
3661 "\"type\":\"integer\","
3662 "\"minimum\":0,"
3663 "\"maximum\":4294967295"
3664 "}"
3665 "},"
3666 "\"required\":[\"type\",\"value\"]"
3667 "},"
3668 "\"u64Value\":{"
3669 "\"properties\":{"
3670 "\"type\":{"
3671 "\"type\":\"string\","
3672 "\"enum\":[\"u64\"]"
3673 "},"
3674 "\"value\":{"
3675 "\"type\":\"integer\","
3676 "\"minimum\":0,"
3677 "\"maximum\":9007199254740991"
3678 "}"
3679 "},"
3680 "\"required\":[\"type\",\"value\"]"
3681 "},"
3682 "\"strValue\":{"
3683 "\"properties\":{"
3684 "\"type\":{"
3685 "\"type\":\"string\","
3686 "\"enum\":[\"str\"]"
3687 "},"
3688 "\"value\":{\"type\":\"string\"}"
3689 "},"
3690 "\"required\":[\"type\",\"value\"]"
3691 "},"
3692 "\"unknownValue\":{"
3693 "\"properties\":{"
3694 "\"type\":{"
3695 "\"type\":\"integer\","
3696 "\"minimum\":0"
3697 "},"
3698 "\"value\":{"
3699 "\"type\":\"string\","
3700 "\"enum\":[\"unknown\"]"
3701 "}"
3702 "},"
3703 "\"required\":[\"type\",\"value\"]"
3704 "}"
3705 "}"
3706 "}"
3707 "}"
3708 "}");
3709
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003710 if (old_len == out->data) {
Simon Horman6f6bb382017-01-04 09:37:26 +01003711 chunk_reset(out);
3712 chunk_appendf(out,
3713 "{\"errorStr\":\"output buffer too short\"}");
3714 }
3715}
3716
3717/* This function dumps the schema onto the stream interface's read buffer.
3718 * It returns 0 as long as it does not complete, non-zero upon completion.
3719 * No state is used.
3720 */
3721static int stats_dump_json_schema_to_buffer(struct stream_interface *si)
3722{
3723 chunk_reset(&trash);
3724
3725 stats_dump_json_schema(&trash);
3726
Willy Tarreau06d80a92017-10-19 14:32:15 +02003727 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01003728 si_rx_room_blk(si);
Simon Horman6f6bb382017-01-04 09:37:26 +01003729 return 0;
3730 }
3731
3732 return 1;
3733}
3734
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02003735static int cli_parse_clear_counters(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau89d467c2016-11-23 11:02:40 +01003736{
3737 struct proxy *px;
3738 struct server *sv;
3739 struct listener *li;
3740 int clrall = 0;
3741
3742 if (strcmp(args[2], "all") == 0)
3743 clrall = 1;
3744
3745 /* check permissions */
3746 if (!cli_has_level(appctx, ACCESS_LVL_OPER) ||
3747 (clrall && !cli_has_level(appctx, ACCESS_LVL_ADMIN)))
3748 return 1;
3749
Olivier Houchardfbc74e82017-11-24 16:54:05 +01003750 for (px = proxies_list; px; px = px->next) {
Willy Tarreau89d467c2016-11-23 11:02:40 +01003751 if (clrall) {
3752 memset(&px->be_counters, 0, sizeof(px->be_counters));
3753 memset(&px->fe_counters, 0, sizeof(px->fe_counters));
3754 }
3755 else {
3756 px->be_counters.conn_max = 0;
3757 px->be_counters.p.http.rps_max = 0;
3758 px->be_counters.sps_max = 0;
3759 px->be_counters.cps_max = 0;
3760 px->be_counters.nbpend_max = 0;
3761
3762 px->fe_counters.conn_max = 0;
3763 px->fe_counters.p.http.rps_max = 0;
3764 px->fe_counters.sps_max = 0;
3765 px->fe_counters.cps_max = 0;
Willy Tarreau89d467c2016-11-23 11:02:40 +01003766 }
3767
3768 for (sv = px->srv; sv; sv = sv->next)
3769 if (clrall)
3770 memset(&sv->counters, 0, sizeof(sv->counters));
3771 else {
3772 sv->counters.cur_sess_max = 0;
3773 sv->counters.nbpend_max = 0;
3774 sv->counters.sps_max = 0;
3775 }
3776
3777 list_for_each_entry(li, &px->conf.listeners, by_fe)
3778 if (li->counters) {
3779 if (clrall)
3780 memset(li->counters, 0, sizeof(*li->counters));
3781 else
3782 li->counters->conn_max = 0;
3783 }
3784 }
3785
3786 global.cps_max = 0;
3787 global.sps_max = 0;
Olivier Houchard00bc3cb2017-10-17 19:23:25 +02003788 global.ssl_max = 0;
3789 global.ssl_fe_keys_max = 0;
3790 global.ssl_be_keys_max = 0;
Willy Tarreaud80cb4e2018-01-20 19:30:13 +01003791
3792 memset(activity, 0, sizeof(activity));
Willy Tarreau89d467c2016-11-23 11:02:40 +01003793 return 1;
3794}
3795
3796
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02003797static int cli_parse_show_info(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003798{
Willy Tarreaud25fc792016-12-16 12:33:47 +01003799 appctx->ctx.stats.scope_str = 0;
3800 appctx->ctx.stats.scope_len = 0;
3801 appctx->ctx.stats.flags = 0;
3802
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003803 if (strcmp(args[2], "typed") == 0)
3804 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
Simon Horman05ee2132017-01-04 09:37:25 +01003805 else if (strcmp(args[2], "json") == 0)
3806 appctx->ctx.stats.flags |= STAT_FMT_JSON;
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003807 return 0;
3808}
3809
3810
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02003811static int cli_parse_show_stat(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau2b812e22016-11-22 16:18:05 +01003812{
Willy Tarreaud25fc792016-12-16 12:33:47 +01003813 appctx->ctx.stats.scope_str = 0;
3814 appctx->ctx.stats.scope_len = 0;
3815 appctx->ctx.stats.flags = 0;
3816
Willy Tarreau2b812e22016-11-22 16:18:05 +01003817 if (*args[2] && *args[3] && *args[4]) {
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01003818 struct proxy *px;
3819
3820 px = proxy_find_by_name(args[2], 0, 0);
3821 if (px)
3822 appctx->ctx.stats.iid = px->uuid;
3823 else
3824 appctx->ctx.stats.iid = atoi(args[2]);
3825
Willy Tarreau9d008692019-08-09 11:21:01 +02003826 if (!appctx->ctx.stats.iid)
3827 return cli_err(appctx, "No such proxy.\n");
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01003828
Willy Tarreau2b812e22016-11-22 16:18:05 +01003829 appctx->ctx.stats.flags |= STAT_BOUND;
Willy Tarreau2b812e22016-11-22 16:18:05 +01003830 appctx->ctx.stats.type = atoi(args[3]);
3831 appctx->ctx.stats.sid = atoi(args[4]);
3832 if (strcmp(args[5], "typed") == 0)
3833 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
Simon Horman05ee2132017-01-04 09:37:25 +01003834 else if (strcmp(args[5], "json") == 0)
3835 appctx->ctx.stats.flags |= STAT_FMT_JSON;
Willy Tarreau2b812e22016-11-22 16:18:05 +01003836 }
3837 else if (strcmp(args[2], "typed") == 0)
3838 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
Simon Horman05ee2132017-01-04 09:37:25 +01003839 else if (strcmp(args[2], "json") == 0)
3840 appctx->ctx.stats.flags |= STAT_FMT_JSON;
Willy Tarreau2b812e22016-11-22 16:18:05 +01003841
Willy Tarreau2b812e22016-11-22 16:18:05 +01003842 return 0;
3843}
3844
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003845static int cli_io_handler_dump_info(struct appctx *appctx)
3846{
3847 return stats_dump_info_to_buffer(appctx->owner);
3848}
3849
Willy Tarreau2b812e22016-11-22 16:18:05 +01003850/* This I/O handler runs as an applet embedded in a stream interface. It is
3851 * used to send raw stats over a socket.
3852 */
3853static int cli_io_handler_dump_stat(struct appctx *appctx)
3854{
Christopher Fauletef779222018-10-31 08:47:01 +01003855 return stats_dump_stat_to_buffer(appctx->owner, NULL, NULL);
Willy Tarreau2b812e22016-11-22 16:18:05 +01003856}
3857
Simon Horman6f6bb382017-01-04 09:37:26 +01003858static int cli_io_handler_dump_json_schema(struct appctx *appctx)
3859{
3860 return stats_dump_json_schema_to_buffer(appctx->owner);
3861}
3862
Willy Tarreau2b812e22016-11-22 16:18:05 +01003863/* register cli keywords */
3864static struct cli_kw_list cli_kws = {{ },{
Willy Tarreau89d467c2016-11-23 11:02:40 +01003865 { { "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 +02003866 { { "show", "info", NULL }, "show info : report information about the running process [json|typed]", cli_parse_show_info, cli_io_handler_dump_info, NULL },
3867 { { "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 +01003868 { { "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 +01003869 {{},}
3870}};
3871
Willy Tarreau0108d902018-11-25 19:14:37 +01003872INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
3873
William Lallemand74c24fb2016-11-21 17:18:36 +01003874struct applet http_stats_applet = {
3875 .obj_type = OBJ_TYPE_APPLET,
3876 .name = "<STATS>", /* used for logging */
3877 .fct = http_stats_io_handler,
3878 .release = NULL,
3879};
3880
William Lallemand74c24fb2016-11-21 17:18:36 +01003881/*
3882 * Local variables:
3883 * c-indent-level: 8
3884 * c-basic-offset: 8
3885 * End:
3886 */