blob: f280e5a7178ed723ea37df00aa13836b8dbe10ae [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 Faulet88a0db22019-09-24 16:35:10 +0200394 case FF_FLT: type = "\"flt\"";
395 snprintf(buf, sizeof(buf), "%f", f->u.flt);
Simon Horman05ee2132017-01-04 09:37:25 +0100396 break;
397 case FF_STR: type = "\"str\"";
398 value = field_str(f, 0);
399 quote = "\"";
400 break;
401 default: snprintf(buf, sizeof(buf), "%u", f->type);
402 type = buf;
403 value = "unknown";
404 quote = "\"";
405 break;
406 }
407
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200408 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100409 chunk_appendf(out, ",\"value\":{\"type\":%s,\"value\":%s%s%s}",
410 type, quote, value, quote);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200411 return !(old_len == out->data);
Simon Horman05ee2132017-01-04 09:37:25 +0100412}
413
William Lallemand74c24fb2016-11-21 17:18:36 +0100414/* Emits an encoding of the field type on 3 characters followed by a delimiter.
415 * Returns non-zero on success, 0 if the buffer is full.
416 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200417int stats_emit_field_tags(struct buffer *out, const struct field *f,
418 char delim)
William Lallemand74c24fb2016-11-21 17:18:36 +0100419{
420 char origin, nature, scope;
421
422 switch (field_origin(f, 0)) {
423 case FO_METRIC: origin = 'M'; break;
424 case FO_STATUS: origin = 'S'; break;
425 case FO_KEY: origin = 'K'; break;
426 case FO_CONFIG: origin = 'C'; break;
427 case FO_PRODUCT: origin = 'P'; break;
428 default: origin = '?'; break;
429 }
430
431 switch (field_nature(f, 0)) {
432 case FN_GAUGE: nature = 'G'; break;
433 case FN_LIMIT: nature = 'L'; break;
434 case FN_MIN: nature = 'm'; break;
435 case FN_MAX: nature = 'M'; break;
436 case FN_RATE: nature = 'R'; break;
437 case FN_COUNTER: nature = 'C'; break;
438 case FN_DURATION: nature = 'D'; break;
439 case FN_AGE: nature = 'A'; break;
440 case FN_TIME: nature = 'T'; break;
441 case FN_NAME: nature = 'N'; break;
442 case FN_OUTPUT: nature = 'O'; break;
443 case FN_AVG: nature = 'a'; break;
444 default: nature = '?'; break;
445 }
446
447 switch (field_scope(f, 0)) {
448 case FS_PROCESS: scope = 'P'; break;
449 case FS_SERVICE: scope = 'S'; break;
450 case FS_SYSTEM: scope = 's'; break;
451 case FS_CLUSTER: scope = 'C'; break;
452 default: scope = '?'; break;
453 }
454
455 return chunk_appendf(out, "%c%c%c%c", origin, nature, scope, delim);
456}
457
Simon Horman05ee2132017-01-04 09:37:25 +0100458/* Emits an encoding of the field type as JSON.
459 * Returns non-zero on success, 0 if the buffer is full.
460 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200461int stats_emit_json_field_tags(struct buffer *out, const struct field *f)
Simon Horman05ee2132017-01-04 09:37:25 +0100462{
463 const char *origin, *nature, *scope;
464 int old_len;
465
466 switch (field_origin(f, 0)) {
467 case FO_METRIC: origin = "Metric"; break;
468 case FO_STATUS: origin = "Status"; break;
469 case FO_KEY: origin = "Key"; break;
470 case FO_CONFIG: origin = "Config"; break;
471 case FO_PRODUCT: origin = "Product"; break;
472 default: origin = "Unknown"; break;
473 }
474
475 switch (field_nature(f, 0)) {
476 case FN_GAUGE: nature = "Gauge"; break;
477 case FN_LIMIT: nature = "Limit"; break;
478 case FN_MIN: nature = "Min"; break;
479 case FN_MAX: nature = "Max"; break;
480 case FN_RATE: nature = "Rate"; break;
481 case FN_COUNTER: nature = "Counter"; break;
482 case FN_DURATION: nature = "Duration"; break;
483 case FN_AGE: nature = "Age"; break;
484 case FN_TIME: nature = "Time"; break;
485 case FN_NAME: nature = "Name"; break;
486 case FN_OUTPUT: nature = "Output"; break;
487 case FN_AVG: nature = "Avg"; break;
488 default: nature = "Unknown"; break;
489 }
490
491 switch (field_scope(f, 0)) {
492 case FS_PROCESS: scope = "Process"; break;
493 case FS_SERVICE: scope = "Service"; break;
494 case FS_SYSTEM: scope = "System"; break;
495 case FS_CLUSTER: scope = "Cluster"; break;
496 default: scope = "Unknown"; break;
497 }
498
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200499 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100500 chunk_appendf(out, "\"tags\":{"
501 "\"origin\":\"%s\","
502 "\"nature\":\"%s\","
503 "\"scope\":\"%s\""
504 "}", origin, nature, scope);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200505 return !(old_len == out->data);
Simon Horman05ee2132017-01-04 09:37:25 +0100506}
William Lallemand74c24fb2016-11-21 17:18:36 +0100507
508/* Dump all fields from <stats> into <out> using CSV format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200509static int stats_dump_fields_csv(struct buffer *out,
510 const struct field *stats)
William Lallemand74c24fb2016-11-21 17:18:36 +0100511{
512 int field;
513
514 for (field = 0; field < ST_F_TOTAL_FIELDS; field++) {
515 if (!stats_emit_raw_data_field(out, &stats[field]))
516 return 0;
517 if (!chunk_strcat(out, ","))
518 return 0;
519 }
520 chunk_strcat(out, "\n");
521 return 1;
522}
523
524/* Dump all fields from <stats> into <out> using a typed "field:desc:type:value" format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200525static int stats_dump_fields_typed(struct buffer *out,
526 const struct field *stats)
William Lallemand74c24fb2016-11-21 17:18:36 +0100527{
528 int field;
529
530 for (field = 0; field < ST_F_TOTAL_FIELDS; field++) {
531 if (!stats[field].type)
532 continue;
533
534 chunk_appendf(out, "%c.%u.%u.%d.%s.%u:",
535 stats[ST_F_TYPE].u.u32 == STATS_TYPE_FE ? 'F' :
536 stats[ST_F_TYPE].u.u32 == STATS_TYPE_BE ? 'B' :
537 stats[ST_F_TYPE].u.u32 == STATS_TYPE_SO ? 'L' :
538 stats[ST_F_TYPE].u.u32 == STATS_TYPE_SV ? 'S' :
539 '?',
540 stats[ST_F_IID].u.u32, stats[ST_F_SID].u.u32,
541 field, stat_field_names[field], stats[ST_F_PID].u.u32);
542
543 if (!stats_emit_field_tags(out, &stats[field], ':'))
544 return 0;
545 if (!stats_emit_typed_data_field(out, &stats[field]))
546 return 0;
547 if (!chunk_strcat(out, "\n"))
548 return 0;
549 }
550 return 1;
551}
552
Simon Horman05ee2132017-01-04 09:37:25 +0100553/* Dump all fields from <stats> into <out> using the "show info json" format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200554static int stats_dump_json_info_fields(struct buffer *out,
Simon Horman05ee2132017-01-04 09:37:25 +0100555 const struct field *info)
556{
557 int field;
558 int started = 0;
559
560 if (!chunk_strcat(out, "["))
561 return 0;
562
563 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
564 int old_len;
565
566 if (!field_format(info, field))
567 continue;
568
569 if (started && !chunk_strcat(out, ","))
570 goto err;
571 started = 1;
572
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200573 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100574 chunk_appendf(out,
575 "{\"field\":{\"pos\":%d,\"name\":\"%s\"},"
576 "\"processNum\":%u,",
577 field, info_field_names[field],
578 info[INF_PROCESS_NUM].u.u32);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200579 if (old_len == out->data)
Simon Horman05ee2132017-01-04 09:37:25 +0100580 goto err;
581
582 if (!stats_emit_json_field_tags(out, &info[field]))
583 goto err;
584
585 if (!stats_emit_json_data_field(out, &info[field]))
586 goto err;
587
588 if (!chunk_strcat(out, "}"))
589 goto err;
590 }
591
592 if (!chunk_strcat(out, "]"))
593 goto err;
594 return 1;
595
596err:
597 chunk_reset(out);
598 chunk_appendf(out, "{\"errorStr\":\"output buffer too short\"}");
599 return 0;
600}
601
602/* Dump all fields from <stats> into <out> using a typed "field:desc:type:value" format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200603static int stats_dump_fields_json(struct buffer *out,
604 const struct field *stats,
Simon Horman05ee2132017-01-04 09:37:25 +0100605 int first_stat)
606{
607 int field;
608 int started = 0;
609
610 if (!first_stat && !chunk_strcat(out, ","))
611 return 0;
612 if (!chunk_strcat(out, "["))
613 return 0;
614
615 for (field = 0; field < ST_F_TOTAL_FIELDS; field++) {
616 const char *obj_type;
617 int old_len;
618
619 if (!stats[field].type)
620 continue;
621
622 if (started && !chunk_strcat(out, ","))
623 goto err;
624 started = 1;
625
626 switch (stats[ST_F_TYPE].u.u32) {
627 case STATS_TYPE_FE: obj_type = "Frontend"; break;
628 case STATS_TYPE_BE: obj_type = "Backend"; break;
629 case STATS_TYPE_SO: obj_type = "Listener"; break;
630 case STATS_TYPE_SV: obj_type = "Server"; break;
631 default: obj_type = "Unknown"; break;
632 }
633
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200634 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100635 chunk_appendf(out,
636 "{"
637 "\"objType\":\"%s\","
638 "\"proxyId\":%d,"
639 "\"id\":%d,"
640 "\"field\":{\"pos\":%d,\"name\":\"%s\"},"
641 "\"processNum\":%u,",
642 obj_type, stats[ST_F_IID].u.u32,
643 stats[ST_F_SID].u.u32, field,
644 stat_field_names[field], stats[ST_F_PID].u.u32);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200645 if (old_len == out->data)
Simon Horman05ee2132017-01-04 09:37:25 +0100646 goto err;
647
648 if (!stats_emit_json_field_tags(out, &stats[field]))
649 goto err;
650
651 if (!stats_emit_json_data_field(out, &stats[field]))
652 goto err;
653
654 if (!chunk_strcat(out, "}"))
655 goto err;
656 }
657
658 if (!chunk_strcat(out, "]"))
659 goto err;
660
661 return 1;
662
663err:
664 chunk_reset(out);
665 if (!first_stat)
666 chunk_strcat(out, ",");
667 chunk_appendf(out, "{\"errorStr\":\"output buffer too short\"}");
668 return 0;
669}
670
William Lallemand74c24fb2016-11-21 17:18:36 +0100671/* Dump all fields from <stats> into <out> using the HTML format. A column is
672 * reserved for the checkbox is ST_SHOWADMIN is set in <flags>. Some extra info
673 * are provided if ST_SHLGNDS is present in <flags>.
674 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200675static int stats_dump_fields_html(struct buffer *out,
676 const struct field *stats,
677 unsigned int flags)
William Lallemand74c24fb2016-11-21 17:18:36 +0100678{
Willy Tarreau83061a82018-07-13 11:56:34 +0200679 struct buffer src;
William Lallemand74c24fb2016-11-21 17:18:36 +0100680
681 if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_FE) {
682 chunk_appendf(out,
683 /* name, queue */
684 "<tr class=\"frontend\">");
685
686 if (flags & ST_SHOWADMIN) {
687 /* Column sub-heading for Enable or Disable server */
688 chunk_appendf(out, "<td></td>");
689 }
690
691 chunk_appendf(out,
692 "<td class=ac>"
693 "<a name=\"%s/Frontend\"></a>"
694 "<a class=lfsb href=\"#%s/Frontend\">Frontend</a></td>"
695 "<td colspan=3></td>"
696 "",
697 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_PXNAME));
698
699 chunk_appendf(out,
700 /* sessions rate : current */
701 "<td><u>%s<div class=tips><table class=det>"
702 "<tr><th>Current connection rate:</th><td>%s/s</td></tr>"
703 "<tr><th>Current session rate:</th><td>%s/s</td></tr>"
704 "",
705 U2H(stats[ST_F_RATE].u.u32),
706 U2H(stats[ST_F_CONN_RATE].u.u32),
707 U2H(stats[ST_F_RATE].u.u32));
708
709 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
710 chunk_appendf(out,
711 "<tr><th>Current request rate:</th><td>%s/s</td></tr>",
712 U2H(stats[ST_F_REQ_RATE].u.u32));
713
714 chunk_appendf(out,
715 "</table></div></u></td>"
716 /* sessions rate : max */
717 "<td><u>%s<div class=tips><table class=det>"
718 "<tr><th>Max connection rate:</th><td>%s/s</td></tr>"
719 "<tr><th>Max session rate:</th><td>%s/s</td></tr>"
720 "",
721 U2H(stats[ST_F_RATE_MAX].u.u32),
722 U2H(stats[ST_F_CONN_RATE_MAX].u.u32),
723 U2H(stats[ST_F_RATE_MAX].u.u32));
724
725 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
726 chunk_appendf(out,
727 "<tr><th>Max request rate:</th><td>%s/s</td></tr>",
728 U2H(stats[ST_F_REQ_RATE_MAX].u.u32));
729
730 chunk_appendf(out,
731 "</table></div></u></td>"
732 /* sessions rate : limit */
733 "<td>%s</td>",
734 LIM2A(stats[ST_F_RATE_LIM].u.u32, "-"));
735
736 chunk_appendf(out,
737 /* sessions: current, max, limit, total */
738 "<td>%s</td><td>%s</td><td>%s</td>"
739 "<td><u>%s<div class=tips><table class=det>"
740 "<tr><th>Cum. connections:</th><td>%s</td></tr>"
741 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
742 "",
743 U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SMAX].u.u32), U2H(stats[ST_F_SLIM].u.u32),
744 U2H(stats[ST_F_STOT].u.u64),
745 U2H(stats[ST_F_CONN_TOT].u.u64),
746 U2H(stats[ST_F_STOT].u.u64));
747
748 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
749 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
750 chunk_appendf(out,
751 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
752 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
753 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
754 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
755 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
756 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
757 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
758 "<tr><th>- other responses:</th><td>%s</td></tr>"
759 "<tr><th>Intercepted requests:</th><td>%s</td></tr>"
Willy Tarreaua1214a52018-12-14 14:00:25 +0100760 "<tr><th>Cache lookups:</th><td>%s</td></tr>"
761 "<tr><th>Cache hits:</th><td>%s</td><td>(%d%%)</td></tr>"
Willy Tarreau1b0f85e2018-05-28 15:12:40 +0200762 "<tr><th>Failed hdr rewrites:</th><td>%s</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +0100763 "",
764 U2H(stats[ST_F_REQ_TOT].u.u64),
765 U2H(stats[ST_F_HRSP_1XX].u.u64),
766 U2H(stats[ST_F_HRSP_2XX].u.u64),
767 U2H(stats[ST_F_COMP_RSP].u.u64),
768 stats[ST_F_HRSP_2XX].u.u64 ?
769 (int)(100 * stats[ST_F_COMP_RSP].u.u64 / stats[ST_F_HRSP_2XX].u.u64) : 0,
770 U2H(stats[ST_F_HRSP_3XX].u.u64),
771 U2H(stats[ST_F_HRSP_4XX].u.u64),
772 U2H(stats[ST_F_HRSP_5XX].u.u64),
773 U2H(stats[ST_F_HRSP_OTHER].u.u64),
Willy Tarreau1b0f85e2018-05-28 15:12:40 +0200774 U2H(stats[ST_F_INTERCEPTED].u.u64),
Willy Tarreaua1214a52018-12-14 14:00:25 +0100775 U2H(stats[ST_F_CACHE_LOOKUPS].u.u64),
776 U2H(stats[ST_F_CACHE_HITS].u.u64),
777 stats[ST_F_CACHE_LOOKUPS].u.u64 ?
778 (int)(100 * stats[ST_F_CACHE_HITS].u.u64 / stats[ST_F_CACHE_LOOKUPS].u.u64) : 0,
Willy Tarreau1b0f85e2018-05-28 15:12:40 +0200779 U2H(stats[ST_F_WREW].u.u64));
William Lallemand74c24fb2016-11-21 17:18:36 +0100780 }
781
782 chunk_appendf(out,
783 "</table></div></u></td>"
784 /* sessions: lbtot, lastsess */
785 "<td></td><td></td>"
786 /* bytes : in */
787 "<td>%s</td>"
788 "",
789 U2H(stats[ST_F_BIN].u.u64));
790
791 chunk_appendf(out,
792 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
793 "<td>%s%s<div class=tips><table class=det>"
794 "<tr><th>Response bytes in:</th><td>%s</td></tr>"
795 "<tr><th>Compression in:</th><td>%s</td></tr>"
796 "<tr><th>Compression out:</th><td>%s</td><td>(%d%%)</td></tr>"
797 "<tr><th>Compression bypass:</th><td>%s</td></tr>"
798 "<tr><th>Total bytes saved:</th><td>%s</td><td>(%d%%)</td></tr>"
799 "</table></div>%s</td>",
800 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "<u>":"",
801 U2H(stats[ST_F_BOUT].u.u64),
802 U2H(stats[ST_F_BOUT].u.u64),
803 U2H(stats[ST_F_COMP_IN].u.u64),
804 U2H(stats[ST_F_COMP_OUT].u.u64),
805 stats[ST_F_COMP_IN].u.u64 ? (int)(stats[ST_F_COMP_OUT].u.u64 * 100 / stats[ST_F_COMP_IN].u.u64) : 0,
806 U2H(stats[ST_F_COMP_BYP].u.u64),
807 U2H(stats[ST_F_COMP_IN].u.u64 - stats[ST_F_COMP_OUT].u.u64),
808 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,
809 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "</u>":"");
810
811 chunk_appendf(out,
812 /* denied: req, resp */
813 "<td>%s</td><td>%s</td>"
814 /* errors : request, connect, response */
815 "<td>%s</td><td></td><td></td>"
816 /* warnings: retries, redispatches */
817 "<td></td><td></td>"
818 /* server status : reflect frontend status */
819 "<td class=ac>%s</td>"
820 /* rest of server: nothing */
821 "<td class=ac colspan=8></td></tr>"
822 "",
823 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
824 U2H(stats[ST_F_EREQ].u.u64),
825 field_str(stats, ST_F_STATUS));
826 }
827 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_SO) {
828 chunk_appendf(out, "<tr class=socket>");
829 if (flags & ST_SHOWADMIN) {
830 /* Column sub-heading for Enable or Disable server */
831 chunk_appendf(out, "<td></td>");
832 }
833
834 chunk_appendf(out,
835 /* frontend name, listener name */
836 "<td class=ac><a name=\"%s/+%s\"></a>%s"
837 "<a class=lfsb href=\"#%s/+%s\">%s</a>"
838 "",
839 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME),
840 (flags & ST_SHLGNDS)?"<u>":"",
841 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME), field_str(stats, ST_F_SVNAME));
842
843 if (flags & ST_SHLGNDS) {
844 chunk_appendf(out, "<div class=tips>");
845
846 if (isdigit(*field_str(stats, ST_F_ADDR)))
847 chunk_appendf(out, "IPv4: %s, ", field_str(stats, ST_F_ADDR));
848 else if (*field_str(stats, ST_F_ADDR) == '[')
849 chunk_appendf(out, "IPv6: %s, ", field_str(stats, ST_F_ADDR));
850 else if (*field_str(stats, ST_F_ADDR))
851 chunk_appendf(out, "%s, ", field_str(stats, ST_F_ADDR));
852
853 /* id */
854 chunk_appendf(out, "id: %d</div>", stats[ST_F_SID].u.u32);
855 }
856
857 chunk_appendf(out,
858 /* queue */
859 "%s</td><td colspan=3></td>"
860 /* sessions rate: current, max, limit */
861 "<td colspan=3>&nbsp;</td>"
862 /* sessions: current, max, limit, total, lbtot, lastsess */
863 "<td>%s</td><td>%s</td><td>%s</td>"
864 "<td>%s</td><td>&nbsp;</td><td>&nbsp;</td>"
865 /* bytes: in, out */
866 "<td>%s</td><td>%s</td>"
867 "",
868 (flags & ST_SHLGNDS)?"</u>":"",
869 U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SMAX].u.u32), U2H(stats[ST_F_SLIM].u.u32),
870 U2H(stats[ST_F_STOT].u.u64), U2H(stats[ST_F_BIN].u.u64), U2H(stats[ST_F_BOUT].u.u64));
871
872 chunk_appendf(out,
873 /* denied: req, resp */
874 "<td>%s</td><td>%s</td>"
875 /* errors: request, connect, response */
876 "<td>%s</td><td></td><td></td>"
877 /* warnings: retries, redispatches */
878 "<td></td><td></td>"
879 /* server status: reflect listener status */
880 "<td class=ac>%s</td>"
881 /* rest of server: nothing */
882 "<td class=ac colspan=8></td></tr>"
883 "",
884 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
885 U2H(stats[ST_F_EREQ].u.u64),
886 field_str(stats, ST_F_STATUS));
887 }
888 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_SV) {
889 const char *style;
890
891 /* determine the style to use depending on the server's state,
892 * its health and weight. There isn't a 1-to-1 mapping between
893 * state and styles for the cases where the server is (still)
894 * up. The reason is that we don't want to report nolb and
895 * drain with the same color.
896 */
897
898 if (strcmp(field_str(stats, ST_F_STATUS), "DOWN") == 0 ||
899 strcmp(field_str(stats, ST_F_STATUS), "DOWN (agent)") == 0) {
900 style = "down";
901 }
902 else if (strcmp(field_str(stats, ST_F_STATUS), "DOWN ") == 0) {
903 style = "going_up";
904 }
905 else if (strcmp(field_str(stats, ST_F_STATUS), "NOLB ") == 0) {
906 style = "going_down";
907 }
908 else if (strcmp(field_str(stats, ST_F_STATUS), "NOLB") == 0) {
909 style = "nolb";
910 }
911 else if (strcmp(field_str(stats, ST_F_STATUS), "no check") == 0) {
912 style = "no_check";
913 }
914 else if (!stats[ST_F_CHKFAIL].type ||
915 stats[ST_F_CHECK_HEALTH].u.u32 == stats[ST_F_CHECK_RISE].u.u32 + stats[ST_F_CHECK_FALL].u.u32 - 1) {
916 /* no check or max health = UP */
917 if (stats[ST_F_WEIGHT].u.u32)
918 style = "up";
919 else
920 style = "draining";
921 }
922 else {
923 style = "going_down";
924 }
925
926 if (memcmp(field_str(stats, ST_F_STATUS), "MAINT", 5) == 0)
927 chunk_appendf(out, "<tr class=\"maintain\">");
928 else
929 chunk_appendf(out,
930 "<tr class=\"%s_%s\">",
931 (stats[ST_F_BCK].u.u32) ? "backup" : "active", style);
932
933
934 if (flags & ST_SHOWADMIN)
935 chunk_appendf(out,
David Harrigand3db35a2016-12-30 12:12:49 +0000936 "<td><input class='%s-checkbox' type=\"checkbox\" name=\"s\" value=\"%s\"></td>",
937 field_str(stats, ST_F_PXNAME),
William Lallemand74c24fb2016-11-21 17:18:36 +0100938 field_str(stats, ST_F_SVNAME));
939
940 chunk_appendf(out,
941 "<td class=ac><a name=\"%s/%s\"></a>%s"
942 "<a class=lfsb href=\"#%s/%s\">%s</a>"
943 "",
944 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME),
945 (flags & ST_SHLGNDS) ? "<u>" : "",
946 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME), field_str(stats, ST_F_SVNAME));
947
948 if (flags & ST_SHLGNDS) {
949 chunk_appendf(out, "<div class=tips>");
950
951 if (isdigit(*field_str(stats, ST_F_ADDR)))
952 chunk_appendf(out, "IPv4: %s, ", field_str(stats, ST_F_ADDR));
953 else if (*field_str(stats, ST_F_ADDR) == '[')
954 chunk_appendf(out, "IPv6: %s, ", field_str(stats, ST_F_ADDR));
955 else if (*field_str(stats, ST_F_ADDR))
956 chunk_appendf(out, "%s, ", field_str(stats, ST_F_ADDR));
957
958 /* id */
959 chunk_appendf(out, "id: %d", stats[ST_F_SID].u.u32);
960
961 /* cookie */
962 if (stats[ST_F_COOKIE].type) {
963 chunk_appendf(out, ", cookie: '");
964 chunk_initstr(&src, field_str(stats, ST_F_COOKIE));
965 chunk_htmlencode(out, &src);
966 chunk_appendf(out, "'");
967 }
968
969 chunk_appendf(out, "</div>");
970 }
971
972 chunk_appendf(out,
973 /* queue : current, max, limit */
974 "%s</td><td>%s</td><td>%s</td><td>%s</td>"
975 /* sessions rate : current, max, limit */
976 "<td>%s</td><td>%s</td><td></td>"
977 "",
978 (flags & ST_SHLGNDS) ? "</u>" : "",
979 U2H(stats[ST_F_QCUR].u.u32), U2H(stats[ST_F_QMAX].u.u32), LIM2A(stats[ST_F_QLIMIT].u.u32, "-"),
980 U2H(stats[ST_F_RATE].u.u32), U2H(stats[ST_F_RATE_MAX].u.u32));
981
982 chunk_appendf(out,
983 /* sessions: current, max, limit, total */
Willy Tarreauf21d17b2019-09-08 09:24:56 +0200984 "<td><u>%s<div class=tips>"
985 "<table class=det>"
986 "<tr><th>Current active connections:</th><td>%s</td></tr>"
987 "<tr><th>Current idle connections:</th><td>%s</td></tr>"
988 "<tr><th>Active connections limit:</th><td>%s</td></tr>"
989 "<tr><th>Idle connections limit:</th><td>%s</td></tr>"
990 "</table></div></u>"
991 "</td><td>%s</td><td>%s</td>"
William Lallemand74c24fb2016-11-21 17:18:36 +0100992 "<td><u>%s<div class=tips><table class=det>"
993 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
994 "",
Willy Tarreauf21d17b2019-09-08 09:24:56 +0200995 U2H(stats[ST_F_SCUR].u.u32),
996 U2H(stats[ST_F_SCUR].u.u32),
997 U2H(stats[ST_F_SRV_ICUR].u.u32),
998 LIM2A(stats[ST_F_SLIM].u.u32, "-"),
999 stats[ST_F_SRV_ILIM].type ? U2H(stats[ST_F_SRV_ILIM].u.u32) : "-",
1000 U2H(stats[ST_F_SMAX].u.u32), LIM2A(stats[ST_F_SLIM].u.u32, "-"),
William Lallemand74c24fb2016-11-21 17:18:36 +01001001 U2H(stats[ST_F_STOT].u.u64),
1002 U2H(stats[ST_F_STOT].u.u64));
1003
1004 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
1005 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
1006 unsigned long long tot;
1007
1008 tot = stats[ST_F_HRSP_OTHER].u.u64;
1009 tot += stats[ST_F_HRSP_1XX].u.u64;
1010 tot += stats[ST_F_HRSP_2XX].u.u64;
1011 tot += stats[ST_F_HRSP_3XX].u.u64;
1012 tot += stats[ST_F_HRSP_4XX].u.u64;
1013 tot += stats[ST_F_HRSP_5XX].u.u64;
1014
1015 chunk_appendf(out,
Willy Tarreauf1573842018-12-14 11:35:36 +01001016 "<tr><th>New connections:</th><td>%s</td></tr>"
1017 "<tr><th>Reused connections:</th><td>%s</td><td>(%d%%)</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001018 "<tr><th>Cum. HTTP responses:</th><td>%s</td></tr>"
1019 "<tr><th>- HTTP 1xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1020 "<tr><th>- HTTP 2xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1021 "<tr><th>- HTTP 3xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1022 "<tr><th>- HTTP 4xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1023 "<tr><th>- HTTP 5xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1024 "<tr><th>- other responses:</th><td>%s</td><td>(%d%%)</td></tr>"
Willy Tarreau1b0f85e2018-05-28 15:12:40 +02001025 "<tr><th>Failed hdr rewrites:</th><td>%s</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001026 "",
Willy Tarreauf1573842018-12-14 11:35:36 +01001027 U2H(stats[ST_F_CONNECT].u.u64),
1028 U2H(stats[ST_F_REUSE].u.u64),
1029 (stats[ST_F_CONNECT].u.u64 + stats[ST_F_REUSE].u.u64) ?
1030 (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 +01001031 U2H(tot),
1032 U2H(stats[ST_F_HRSP_1XX].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_1XX].u.u64 / tot) : 0,
1033 U2H(stats[ST_F_HRSP_2XX].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_2XX].u.u64 / tot) : 0,
1034 U2H(stats[ST_F_HRSP_3XX].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_3XX].u.u64 / tot) : 0,
1035 U2H(stats[ST_F_HRSP_4XX].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_4XX].u.u64 / tot) : 0,
1036 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 +02001037 U2H(stats[ST_F_HRSP_OTHER].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_OTHER].u.u64 / tot) : 0,
1038 U2H(stats[ST_F_WREW].u.u64));
William Lallemand74c24fb2016-11-21 17:18:36 +01001039 }
1040
1041 chunk_appendf(out, "<tr><th colspan=3>Avg over last 1024 success. conn.</th></tr>");
1042 chunk_appendf(out, "<tr><th>- Queue time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_QTIME].u.u32));
1043 chunk_appendf(out, "<tr><th>- Connect time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_CTIME].u.u32));
1044 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
1045 chunk_appendf(out, "<tr><th>- Response time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_RTIME].u.u32));
1046 chunk_appendf(out, "<tr><th>- Total time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_TTIME].u.u32));
1047
1048 chunk_appendf(out,
1049 "</table></div></u></td>"
1050 /* sessions: lbtot, last */
1051 "<td>%s</td><td>%s</td>",
1052 U2H(stats[ST_F_LBTOT].u.u64),
1053 human_time(stats[ST_F_LASTSESS].u.s32, 1));
1054
1055 chunk_appendf(out,
1056 /* bytes : in, out */
1057 "<td>%s</td><td>%s</td>"
1058 /* denied: req, resp */
1059 "<td></td><td>%s</td>"
1060 /* errors : request, connect */
1061 "<td></td><td>%s</td>"
1062 /* errors : response */
1063 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
1064 /* warnings: retries, redispatches */
1065 "<td>%lld</td><td>%lld</td>"
1066 "",
1067 U2H(stats[ST_F_BIN].u.u64), U2H(stats[ST_F_BOUT].u.u64),
1068 U2H(stats[ST_F_DRESP].u.u64),
1069 U2H(stats[ST_F_ECON].u.u64),
1070 U2H(stats[ST_F_ERESP].u.u64),
1071 (long long)stats[ST_F_CLI_ABRT].u.u64,
1072 (long long)stats[ST_F_SRV_ABRT].u.u64,
1073 (long long)stats[ST_F_WRETR].u.u64,
1074 (long long)stats[ST_F_WREDIS].u.u64);
1075
1076 /* status, last change */
1077 chunk_appendf(out, "<td class=ac>");
1078
1079 /* FIXME!!!!
1080 * LASTCHG should contain the last change for *this* server and must be computed
1081 * properly above, as was done below, ie: this server if maint, otherwise ref server
1082 * if tracking. Note that ref is either local or remote depending on tracking.
1083 */
1084
1085
1086 if (memcmp(field_str(stats, ST_F_STATUS), "MAINT", 5) == 0) {
1087 chunk_appendf(out, "%s MAINT", human_time(stats[ST_F_LASTCHG].u.u32, 1));
1088 }
1089 else if (memcmp(field_str(stats, ST_F_STATUS), "no check", 5) == 0) {
1090 chunk_strcat(out, "<i>no check</i>");
1091 }
1092 else {
1093 chunk_appendf(out, "%s %s", human_time(stats[ST_F_LASTCHG].u.u32, 1), field_str(stats, ST_F_STATUS));
1094 if (memcmp(field_str(stats, ST_F_STATUS), "DOWN", 4) == 0) {
1095 if (stats[ST_F_CHECK_HEALTH].u.u32)
1096 chunk_strcat(out, " &uarr;");
1097 }
1098 else if (stats[ST_F_CHECK_HEALTH].u.u32 < stats[ST_F_CHECK_RISE].u.u32 + stats[ST_F_CHECK_FALL].u.u32 - 1)
1099 chunk_strcat(out, " &darr;");
1100 }
1101
1102 if (memcmp(field_str(stats, ST_F_STATUS), "DOWN", 4) == 0 &&
1103 stats[ST_F_AGENT_STATUS].type && !stats[ST_F_AGENT_HEALTH].u.u32) {
1104 chunk_appendf(out,
1105 "</td><td class=ac><u> %s",
1106 field_str(stats, ST_F_AGENT_STATUS));
1107
1108 if (stats[ST_F_AGENT_CODE].type)
1109 chunk_appendf(out, "/%d", stats[ST_F_AGENT_CODE].u.u32);
1110
1111 if (stats[ST_F_AGENT_DURATION].type)
1112 chunk_appendf(out, " in %lums", (long)stats[ST_F_AGENT_DURATION].u.u64);
1113
1114 chunk_appendf(out, "<div class=tips>%s", field_str(stats, ST_F_AGENT_DESC));
1115
1116 if (*field_str(stats, ST_F_LAST_AGT)) {
1117 chunk_appendf(out, ": ");
1118 chunk_initstr(&src, field_str(stats, ST_F_LAST_AGT));
1119 chunk_htmlencode(out, &src);
1120 }
1121 chunk_appendf(out, "</div></u>");
1122 }
1123 else if (stats[ST_F_CHECK_STATUS].type) {
1124 chunk_appendf(out,
1125 "</td><td class=ac><u> %s",
1126 field_str(stats, ST_F_CHECK_STATUS));
1127
1128 if (stats[ST_F_CHECK_CODE].type)
1129 chunk_appendf(out, "/%d", stats[ST_F_CHECK_CODE].u.u32);
1130
1131 if (stats[ST_F_CHECK_DURATION].type)
1132 chunk_appendf(out, " in %lums", (long)stats[ST_F_CHECK_DURATION].u.u64);
1133
1134 chunk_appendf(out, "<div class=tips>%s", field_str(stats, ST_F_CHECK_DESC));
1135
1136 if (*field_str(stats, ST_F_LAST_CHK)) {
1137 chunk_appendf(out, ": ");
1138 chunk_initstr(&src, field_str(stats, ST_F_LAST_CHK));
1139 chunk_htmlencode(out, &src);
1140 }
1141 chunk_appendf(out, "</div></u>");
1142 }
1143 else
1144 chunk_appendf(out, "</td><td>");
1145
1146 chunk_appendf(out,
1147 /* weight */
1148 "</td><td class=ac>%d</td>"
1149 /* act, bck */
1150 "<td class=ac>%s</td><td class=ac>%s</td>"
1151 "",
1152 stats[ST_F_WEIGHT].u.u32,
1153 stats[ST_F_BCK].u.u32 ? "-" : "Y",
1154 stats[ST_F_BCK].u.u32 ? "Y" : "-");
1155
1156 /* check failures: unique, fatal, down time */
1157 if (strcmp(field_str(stats, ST_F_STATUS), "MAINT (resolution)") == 0) {
1158 chunk_appendf(out, "<td class=ac colspan=3>resolution</td>");
1159 }
1160 else if (stats[ST_F_CHKFAIL].type) {
1161 chunk_appendf(out, "<td><u>%lld", (long long)stats[ST_F_CHKFAIL].u.u64);
1162
1163 if (stats[ST_F_HANAFAIL].type)
1164 chunk_appendf(out, "/%lld", (long long)stats[ST_F_HANAFAIL].u.u64);
1165
1166 chunk_appendf(out,
1167 "<div class=tips>Failed Health Checks%s</div></u></td>"
1168 "<td>%lld</td><td>%s</td>"
1169 "",
1170 stats[ST_F_HANAFAIL].type ? "/Health Analyses" : "",
1171 (long long)stats[ST_F_CHKDOWN].u.u64, human_time(stats[ST_F_DOWNTIME].u.u32, 1));
1172 }
1173 else if (strcmp(field_str(stats, ST_F_STATUS), "MAINT") != 0 && field_format(stats, ST_F_TRACKED) == FF_STR) {
1174 /* tracking a server (hence inherited maint would appear as "MAINT (via...)" */
1175 chunk_appendf(out,
1176 "<td class=ac colspan=3><a class=lfsb href=\"#%s\">via %s</a></td>",
1177 field_str(stats, ST_F_TRACKED), field_str(stats, ST_F_TRACKED));
1178 }
1179 else
1180 chunk_appendf(out, "<td colspan=3></td>");
1181
1182 /* throttle */
1183 if (stats[ST_F_THROTTLE].type)
1184 chunk_appendf(out, "<td class=ac>%d %%</td></tr>\n", stats[ST_F_THROTTLE].u.u32);
1185 else
1186 chunk_appendf(out, "<td class=ac>-</td></tr>\n");
1187 }
1188 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_BE) {
1189 chunk_appendf(out, "<tr class=\"backend\">");
1190 if (flags & ST_SHOWADMIN) {
1191 /* Column sub-heading for Enable or Disable server */
1192 chunk_appendf(out, "<td></td>");
1193 }
1194 chunk_appendf(out,
1195 "<td class=ac>"
1196 /* name */
1197 "%s<a name=\"%s/Backend\"></a>"
1198 "<a class=lfsb href=\"#%s/Backend\">Backend</a>"
1199 "",
1200 (flags & ST_SHLGNDS)?"<u>":"",
1201 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_PXNAME));
1202
1203 if (flags & ST_SHLGNDS) {
1204 /* balancing */
1205 chunk_appendf(out, "<div class=tips>balancing: %s",
1206 field_str(stats, ST_F_ALGO));
1207
1208 /* cookie */
1209 if (stats[ST_F_COOKIE].type) {
1210 chunk_appendf(out, ", cookie: '");
1211 chunk_initstr(&src, field_str(stats, ST_F_COOKIE));
1212 chunk_htmlencode(out, &src);
1213 chunk_appendf(out, "'");
1214 }
1215 chunk_appendf(out, "</div>");
1216 }
1217
1218 chunk_appendf(out,
1219 "%s</td>"
1220 /* queue : current, max */
1221 "<td>%s</td><td>%s</td><td></td>"
1222 /* sessions rate : current, max, limit */
1223 "<td>%s</td><td>%s</td><td></td>"
1224 "",
1225 (flags & ST_SHLGNDS)?"</u>":"",
1226 U2H(stats[ST_F_QCUR].u.u32), U2H(stats[ST_F_QMAX].u.u32),
1227 U2H(stats[ST_F_RATE].u.u32), U2H(stats[ST_F_RATE_MAX].u.u32));
1228
1229 chunk_appendf(out,
1230 /* sessions: current, max, limit, total */
1231 "<td>%s</td><td>%s</td><td>%s</td>"
1232 "<td><u>%s<div class=tips><table class=det>"
1233 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
1234 "",
Willy Tarreau8e0f1752016-12-12 15:07:29 +01001235 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 +01001236 U2H(stats[ST_F_STOT].u.u64),
1237 U2H(stats[ST_F_STOT].u.u64));
1238
1239 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
1240 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
1241 chunk_appendf(out,
Willy Tarreauf1573842018-12-14 11:35:36 +01001242 "<tr><th>New connections:</th><td>%s</td></tr>"
1243 "<tr><th>Reused connections:</th><td>%s</td><td>(%d%%)</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001244 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
1245 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
1246 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
1247 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
1248 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
1249 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
1250 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
1251 "<tr><th>- other responses:</th><td>%s</td></tr>"
Willy Tarreaua1214a52018-12-14 14:00:25 +01001252 "<tr><th>Cache lookups:</th><td>%s</td></tr>"
1253 "<tr><th>Cache hits:</th><td>%s</td><td>(%d%%)</td></tr>"
Willy Tarreau1b0f85e2018-05-28 15:12:40 +02001254 "<tr><th>Failed hdr rewrites:</th><td>%s</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001255 "<tr><th colspan=3>Avg over last 1024 success. conn.</th></tr>"
1256 "",
Willy Tarreauf1573842018-12-14 11:35:36 +01001257 U2H(stats[ST_F_CONNECT].u.u64),
1258 U2H(stats[ST_F_REUSE].u.u64),
1259 (stats[ST_F_CONNECT].u.u64 + stats[ST_F_REUSE].u.u64) ?
1260 (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 +01001261 U2H(stats[ST_F_REQ_TOT].u.u64),
1262 U2H(stats[ST_F_HRSP_1XX].u.u64),
1263 U2H(stats[ST_F_HRSP_2XX].u.u64),
1264 U2H(stats[ST_F_COMP_RSP].u.u64),
1265 stats[ST_F_HRSP_2XX].u.u64 ?
1266 (int)(100 * stats[ST_F_COMP_RSP].u.u64 / stats[ST_F_HRSP_2XX].u.u64) : 0,
1267 U2H(stats[ST_F_HRSP_3XX].u.u64),
1268 U2H(stats[ST_F_HRSP_4XX].u.u64),
1269 U2H(stats[ST_F_HRSP_5XX].u.u64),
Willy Tarreaufeead3a2018-12-14 13:48:44 +01001270 U2H(stats[ST_F_HRSP_OTHER].u.u64),
Willy Tarreaua1214a52018-12-14 14:00:25 +01001271 U2H(stats[ST_F_CACHE_LOOKUPS].u.u64),
1272 U2H(stats[ST_F_CACHE_HITS].u.u64),
1273 stats[ST_F_CACHE_LOOKUPS].u.u64 ?
1274 (int)(100 * stats[ST_F_CACHE_HITS].u.u64 / stats[ST_F_CACHE_LOOKUPS].u.u64) : 0,
Willy Tarreaufeead3a2018-12-14 13:48:44 +01001275 U2H(stats[ST_F_WREW].u.u64));
William Lallemand74c24fb2016-11-21 17:18:36 +01001276 }
1277
1278 chunk_appendf(out, "<tr><th>- Queue time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_QTIME].u.u32));
1279 chunk_appendf(out, "<tr><th>- Connect time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_CTIME].u.u32));
1280 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
1281 chunk_appendf(out, "<tr><th>- Response time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_RTIME].u.u32));
1282 chunk_appendf(out, "<tr><th>- Total time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_TTIME].u.u32));
1283
1284 chunk_appendf(out,
1285 "</table></div></u></td>"
1286 /* sessions: lbtot, last */
1287 "<td>%s</td><td>%s</td>"
1288 /* bytes: in */
1289 "<td>%s</td>"
1290 "",
1291 U2H(stats[ST_F_LBTOT].u.u64),
1292 human_time(stats[ST_F_LASTSESS].u.s32, 1),
1293 U2H(stats[ST_F_BIN].u.u64));
1294
1295 chunk_appendf(out,
1296 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
1297 "<td>%s%s<div class=tips><table class=det>"
1298 "<tr><th>Response bytes in:</th><td>%s</td></tr>"
1299 "<tr><th>Compression in:</th><td>%s</td></tr>"
1300 "<tr><th>Compression out:</th><td>%s</td><td>(%d%%)</td></tr>"
1301 "<tr><th>Compression bypass:</th><td>%s</td></tr>"
1302 "<tr><th>Total bytes saved:</th><td>%s</td><td>(%d%%)</td></tr>"
1303 "</table></div>%s</td>",
1304 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "<u>":"",
1305 U2H(stats[ST_F_BOUT].u.u64),
1306 U2H(stats[ST_F_BOUT].u.u64),
1307 U2H(stats[ST_F_COMP_IN].u.u64),
1308 U2H(stats[ST_F_COMP_OUT].u.u64),
1309 stats[ST_F_COMP_IN].u.u64 ? (int)(stats[ST_F_COMP_OUT].u.u64 * 100 / stats[ST_F_COMP_IN].u.u64) : 0,
1310 U2H(stats[ST_F_COMP_BYP].u.u64),
1311 U2H(stats[ST_F_COMP_IN].u.u64 - stats[ST_F_COMP_OUT].u.u64),
1312 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,
1313 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "</u>":"");
1314
1315 chunk_appendf(out,
1316 /* denied: req, resp */
1317 "<td>%s</td><td>%s</td>"
1318 /* errors : request, connect */
1319 "<td></td><td>%s</td>"
1320 /* errors : response */
1321 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
1322 /* warnings: retries, redispatches */
1323 "<td>%lld</td><td>%lld</td>"
1324 /* backend status: reflect backend status (up/down): we display UP
1325 * if the backend has known working servers or if it has no server at
1326 * all (eg: for stats). Then we display the total weight, number of
1327 * active and backups. */
1328 "<td class=ac>%s %s</td><td class=ac>&nbsp;</td><td class=ac>%d</td>"
1329 "<td class=ac>%d</td><td class=ac>%d</td>"
1330 "",
1331 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
1332 U2H(stats[ST_F_ECON].u.u64),
1333 U2H(stats[ST_F_ERESP].u.u64),
1334 (long long)stats[ST_F_CLI_ABRT].u.u64,
1335 (long long)stats[ST_F_SRV_ABRT].u.u64,
1336 (long long)stats[ST_F_WRETR].u.u64, (long long)stats[ST_F_WREDIS].u.u64,
1337 human_time(stats[ST_F_LASTCHG].u.u32, 1),
1338 strcmp(field_str(stats, ST_F_STATUS), "DOWN") ? field_str(stats, ST_F_STATUS) : "<font color=\"red\"><b>DOWN</b></font>",
1339 stats[ST_F_WEIGHT].u.u32,
1340 stats[ST_F_ACT].u.u32, stats[ST_F_BCK].u.u32);
1341
1342 chunk_appendf(out,
1343 /* rest of backend: nothing, down transitions, total downtime, throttle */
1344 "<td class=ac>&nbsp;</td><td>%d</td>"
1345 "<td>%s</td>"
1346 "<td></td>"
1347 "</tr>",
1348 stats[ST_F_CHKDOWN].u.u32,
1349 stats[ST_F_DOWNTIME].type ? human_time(stats[ST_F_DOWNTIME].u.u32, 1) : "&nbsp;");
1350 }
1351 return 1;
1352}
1353
1354int stats_dump_one_line(const struct field *stats, unsigned int flags, struct proxy *px, struct appctx *appctx)
1355{
Simon Horman05ee2132017-01-04 09:37:25 +01001356 int ret;
1357
William Lallemand74c24fb2016-11-21 17:18:36 +01001358 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN))
1359 flags |= ST_SHOWADMIN;
1360
1361 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
Simon Horman05ee2132017-01-04 09:37:25 +01001362 ret = stats_dump_fields_html(&trash, stats, flags);
William Lallemand74c24fb2016-11-21 17:18:36 +01001363 else if (appctx->ctx.stats.flags & STAT_FMT_TYPED)
Simon Horman05ee2132017-01-04 09:37:25 +01001364 ret = stats_dump_fields_typed(&trash, stats);
1365 else if (appctx->ctx.stats.flags & STAT_FMT_JSON)
1366 ret = stats_dump_fields_json(&trash, stats,
1367 !(appctx->ctx.stats.flags &
1368 STAT_STARTED));
William Lallemand74c24fb2016-11-21 17:18:36 +01001369 else
Simon Horman05ee2132017-01-04 09:37:25 +01001370 ret = stats_dump_fields_csv(&trash, stats);
1371
1372 if (ret)
1373 appctx->ctx.stats.flags |= STAT_STARTED;
1374
1375 return ret;
William Lallemand74c24fb2016-11-21 17:18:36 +01001376}
1377
1378/* Fill <stats> with the frontend statistics. <stats> is
1379 * preallocated array of length <len>. The length of the array
1380 * must be at least ST_F_TOTAL_FIELDS. If this length is less then
1381 * this value, the function returns 0, otherwise, it returns 1.
1382 */
1383int stats_fill_fe_stats(struct proxy *px, struct field *stats, int len)
1384{
1385 if (len < ST_F_TOTAL_FIELDS)
1386 return 0;
1387
1388 memset(stats, 0, sizeof(*stats) * len);
1389
1390 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1391 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, "FRONTEND");
1392 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1393 stats[ST_F_SCUR] = mkf_u32(0, px->feconn);
1394 stats[ST_F_SMAX] = mkf_u32(FN_MAX, px->fe_counters.conn_max);
1395 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, px->maxconn);
1396 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, px->fe_counters.cum_sess);
1397 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, px->fe_counters.bytes_in);
1398 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, px->fe_counters.bytes_out);
1399 stats[ST_F_DREQ] = mkf_u64(FN_COUNTER, px->fe_counters.denied_req);
1400 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, px->fe_counters.denied_resp);
1401 stats[ST_F_EREQ] = mkf_u64(FN_COUNTER, px->fe_counters.failed_req);
1402 stats[ST_F_DCON] = mkf_u64(FN_COUNTER, px->fe_counters.denied_conn);
1403 stats[ST_F_DSES] = mkf_u64(FN_COUNTER, px->fe_counters.denied_sess);
1404 stats[ST_F_STATUS] = mkf_str(FO_STATUS, px->state == PR_STREADY ? "OPEN" : px->state == PR_STFULL ? "FULL" : "STOP");
1405 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
1406 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1407 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, 0);
1408 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_FE);
1409 stats[ST_F_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_sess_per_sec));
1410 stats[ST_F_RATE_LIM] = mkf_u32(FO_CONFIG|FN_LIMIT, px->fe_sps_lim);
1411 stats[ST_F_RATE_MAX] = mkf_u32(FN_MAX, px->fe_counters.sps_max);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001412 stats[ST_F_WREW] = mkf_u64(FN_COUNTER, px->fe_counters.failed_rewrites);
William Lallemand74c24fb2016-11-21 17:18:36 +01001413
1414 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
1415 if (px->mode == PR_MODE_HTTP) {
1416 stats[ST_F_HRSP_1XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[1]);
1417 stats[ST_F_HRSP_2XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[2]);
1418 stats[ST_F_HRSP_3XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[3]);
1419 stats[ST_F_HRSP_4XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[4]);
1420 stats[ST_F_HRSP_5XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[5]);
1421 stats[ST_F_HRSP_OTHER] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[0]);
1422 stats[ST_F_INTERCEPTED] = mkf_u64(FN_COUNTER, px->fe_counters.intercepted_req);
Willy Tarreaua1214a52018-12-14 14:00:25 +01001423 stats[ST_F_CACHE_LOOKUPS] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cache_lookups);
1424 stats[ST_F_CACHE_HITS] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cache_hits);
William Lallemand74c24fb2016-11-21 17:18:36 +01001425 }
1426
1427 /* requests : req_rate, req_rate_max, req_tot, */
1428 stats[ST_F_REQ_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_req_per_sec));
1429 stats[ST_F_REQ_RATE_MAX] = mkf_u32(FN_MAX, px->fe_counters.p.http.rps_max);
1430 stats[ST_F_REQ_TOT] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cum_req);
1431
1432 /* compression: in, out, bypassed, responses */
1433 stats[ST_F_COMP_IN] = mkf_u64(FN_COUNTER, px->fe_counters.comp_in);
1434 stats[ST_F_COMP_OUT] = mkf_u64(FN_COUNTER, px->fe_counters.comp_out);
1435 stats[ST_F_COMP_BYP] = mkf_u64(FN_COUNTER, px->fe_counters.comp_byp);
1436 stats[ST_F_COMP_RSP] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.comp_rsp);
1437
1438 /* connections : conn_rate, conn_rate_max, conn_tot, conn_max */
1439 stats[ST_F_CONN_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_conn_per_sec));
1440 stats[ST_F_CONN_RATE_MAX] = mkf_u32(FN_MAX, px->fe_counters.cps_max);
1441 stats[ST_F_CONN_TOT] = mkf_u64(FN_COUNTER, px->fe_counters.cum_conn);
1442
1443 return 1;
1444}
1445
1446/* Dumps a frontend's line to the trash for the current proxy <px> and uses
1447 * the state from stream interface <si>. The caller is responsible for clearing
1448 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
1449 */
1450static int stats_dump_fe_stats(struct stream_interface *si, struct proxy *px)
1451{
1452 struct appctx *appctx = __objt_appctx(si->end);
1453
1454 if (!(px->cap & PR_CAP_FE))
1455 return 0;
1456
1457 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_FE)))
1458 return 0;
1459
1460 if (!stats_fill_fe_stats(px, stats, ST_F_TOTAL_FIELDS))
1461 return 0;
1462
1463 return stats_dump_one_line(stats, 0, px, appctx);
1464}
1465
1466/* Fill <stats> with the listener statistics. <stats> is
1467 * preallocated array of length <len>. The length of the array
1468 * must be at least ST_F_TOTAL_FIELDS. If this length is less
1469 * then this value, the function returns 0, otherwise, it
1470 * returns 1. <flags> can take the value ST_SHLGNDS.
1471 */
1472int stats_fill_li_stats(struct proxy *px, struct listener *l, int flags,
1473 struct field *stats, int len)
1474{
Willy Tarreau83061a82018-07-13 11:56:34 +02001475 struct buffer *out = get_trash_chunk();
William Lallemand74c24fb2016-11-21 17:18:36 +01001476
1477 if (len < ST_F_TOTAL_FIELDS)
1478 return 0;
1479
1480 if (!l->counters)
1481 return 0;
1482
1483 chunk_reset(out);
1484 memset(stats, 0, sizeof(*stats) * len);
1485
1486 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1487 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, l->name);
1488 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1489 stats[ST_F_SCUR] = mkf_u32(0, l->nbconn);
1490 stats[ST_F_SMAX] = mkf_u32(FN_MAX, l->counters->conn_max);
1491 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, l->maxconn);
1492 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, l->counters->cum_conn);
1493 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, l->counters->bytes_in);
1494 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, l->counters->bytes_out);
1495 stats[ST_F_DREQ] = mkf_u64(FN_COUNTER, l->counters->denied_req);
1496 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, l->counters->denied_resp);
1497 stats[ST_F_EREQ] = mkf_u64(FN_COUNTER, l->counters->failed_req);
1498 stats[ST_F_DCON] = mkf_u64(FN_COUNTER, l->counters->denied_conn);
1499 stats[ST_F_DSES] = mkf_u64(FN_COUNTER, l->counters->denied_sess);
Willy Tarreaua8cf66b2019-02-27 16:49:00 +01001500 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 +01001501 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
1502 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1503 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, l->luid);
1504 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_SO);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001505 stats[ST_F_WREW] = mkf_u64(FN_COUNTER, l->counters->failed_rewrites);
William Lallemand74c24fb2016-11-21 17:18:36 +01001506
1507 if (flags & ST_SHLGNDS) {
1508 char str[INET6_ADDRSTRLEN];
1509 int port;
1510
1511 port = get_host_port(&l->addr);
1512 switch (addr_to_str(&l->addr, str, sizeof(str))) {
1513 case AF_INET:
1514 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1515 chunk_appendf(out, "%s:%d", str, port);
1516 break;
1517 case AF_INET6:
1518 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1519 chunk_appendf(out, "[%s]:%d", str, port);
1520 break;
1521 case AF_UNIX:
1522 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, "unix");
1523 break;
1524 case -1:
1525 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1526 chunk_strcat(out, strerror(errno));
1527 break;
1528 default: /* address family not supported */
1529 break;
1530 }
1531 }
1532
1533 return 1;
1534}
1535
1536/* Dumps a line for listener <l> and proxy <px> to the trash and uses the state
1537 * from stream interface <si>, and stats flags <flags>. The caller is responsible
1538 * for clearing the trash if needed. Returns non-zero if it emits anything, zero
1539 * otherwise.
1540 */
1541static int stats_dump_li_stats(struct stream_interface *si, struct proxy *px, struct listener *l, int flags)
1542{
1543 struct appctx *appctx = __objt_appctx(si->end);
1544
1545 if (!stats_fill_li_stats(px, l, flags, stats, ST_F_TOTAL_FIELDS))
1546 return 0;
1547
1548 return stats_dump_one_line(stats, flags, px, appctx);
1549}
1550
1551enum srv_stats_state {
1552 SRV_STATS_STATE_DOWN = 0,
1553 SRV_STATS_STATE_DOWN_AGENT,
1554 SRV_STATS_STATE_GOING_UP,
1555 SRV_STATS_STATE_UP_GOING_DOWN,
1556 SRV_STATS_STATE_UP,
1557 SRV_STATS_STATE_NOLB_GOING_DOWN,
1558 SRV_STATS_STATE_NOLB,
1559 SRV_STATS_STATE_DRAIN_GOING_DOWN,
1560 SRV_STATS_STATE_DRAIN,
1561 SRV_STATS_STATE_DRAIN_AGENT,
1562 SRV_STATS_STATE_NO_CHECK,
1563
1564 SRV_STATS_STATE_COUNT, /* Must be last */
1565};
1566
1567static const char *srv_hlt_st[SRV_STATS_STATE_COUNT] = {
1568 [SRV_STATS_STATE_DOWN] = "DOWN",
1569 [SRV_STATS_STATE_DOWN_AGENT] = "DOWN (agent)",
1570 [SRV_STATS_STATE_GOING_UP] = "DOWN %d/%d",
1571 [SRV_STATS_STATE_UP_GOING_DOWN] = "UP %d/%d",
1572 [SRV_STATS_STATE_UP] = "UP",
1573 [SRV_STATS_STATE_NOLB_GOING_DOWN] = "NOLB %d/%d",
1574 [SRV_STATS_STATE_NOLB] = "NOLB",
1575 [SRV_STATS_STATE_DRAIN_GOING_DOWN] = "DRAIN %d/%d",
1576 [SRV_STATS_STATE_DRAIN] = "DRAIN",
1577 [SRV_STATS_STATE_DRAIN_AGENT] = "DRAIN (agent)",
1578 [SRV_STATS_STATE_NO_CHECK] = "no check"
1579};
1580
1581/* Fill <stats> with the server statistics. <stats> is
1582 * preallocated array of length <len>. The length of the array
1583 * must be at least ST_F_TOTAL_FIELDS. If this length is less
1584 * then this value, the function returns 0, otherwise, it
1585 * returns 1. <flags> can take the value ST_SHLGNDS.
1586 */
1587int stats_fill_sv_stats(struct proxy *px, struct server *sv, int flags,
1588 struct field *stats, int len)
1589{
1590 struct server *via, *ref;
1591 char str[INET6_ADDRSTRLEN];
Willy Tarreau83061a82018-07-13 11:56:34 +02001592 struct buffer *out = get_trash_chunk();
William Lallemand74c24fb2016-11-21 17:18:36 +01001593 enum srv_stats_state state;
1594 char *fld_status;
1595
1596 if (len < ST_F_TOTAL_FIELDS)
1597 return 0;
1598
1599 memset(stats, 0, sizeof(*stats) * len);
1600
1601 /* we have "via" which is the tracked server as described in the configuration,
1602 * and "ref" which is the checked server and the end of the chain.
1603 */
1604 via = sv->track ? sv->track : sv;
1605 ref = via;
1606 while (ref->track)
1607 ref = ref->track;
1608
Emeric Brun52a91d32017-08-31 14:41:55 +02001609 if (sv->cur_state == SRV_ST_RUNNING || sv->cur_state == SRV_ST_STARTING) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001610 if ((ref->check.state & CHK_ST_ENABLED) &&
1611 (ref->check.health < ref->check.rise + ref->check.fall - 1)) {
1612 state = SRV_STATS_STATE_UP_GOING_DOWN;
1613 } else {
1614 state = SRV_STATS_STATE_UP;
1615 }
1616
Emeric Brun52a91d32017-08-31 14:41:55 +02001617 if (sv->cur_admin & SRV_ADMF_DRAIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001618 if (ref->agent.state & CHK_ST_ENABLED)
1619 state = SRV_STATS_STATE_DRAIN_AGENT;
1620 else if (state == SRV_STATS_STATE_UP_GOING_DOWN)
1621 state = SRV_STATS_STATE_DRAIN_GOING_DOWN;
1622 else
1623 state = SRV_STATS_STATE_DRAIN;
1624 }
1625
1626 if (state == SRV_STATS_STATE_UP && !(ref->check.state & CHK_ST_ENABLED)) {
1627 state = SRV_STATS_STATE_NO_CHECK;
1628 }
1629 }
Emeric Brun52a91d32017-08-31 14:41:55 +02001630 else if (sv->cur_state == SRV_ST_STOPPING) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001631 if ((!(sv->check.state & CHK_ST_ENABLED) && !sv->track) ||
1632 (ref->check.health == ref->check.rise + ref->check.fall - 1)) {
1633 state = SRV_STATS_STATE_NOLB;
1634 } else {
1635 state = SRV_STATS_STATE_NOLB_GOING_DOWN;
1636 }
1637 }
1638 else { /* stopped */
1639 if ((ref->agent.state & CHK_ST_ENABLED) && !ref->agent.health) {
1640 state = SRV_STATS_STATE_DOWN_AGENT;
1641 } else if ((ref->check.state & CHK_ST_ENABLED) && !ref->check.health) {
1642 state = SRV_STATS_STATE_DOWN; /* DOWN */
1643 } else if ((ref->agent.state & CHK_ST_ENABLED) || (ref->check.state & CHK_ST_ENABLED)) {
1644 state = SRV_STATS_STATE_GOING_UP;
1645 } else {
1646 state = SRV_STATS_STATE_DOWN; /* DOWN, unchecked */
1647 }
1648 }
1649
1650 chunk_reset(out);
1651
1652 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1653 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, sv->id);
1654 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1655 stats[ST_F_QCUR] = mkf_u32(0, sv->nbpend);
1656 stats[ST_F_QMAX] = mkf_u32(FN_MAX, sv->counters.nbpend_max);
1657 stats[ST_F_SCUR] = mkf_u32(0, sv->cur_sess);
1658 stats[ST_F_SMAX] = mkf_u32(FN_MAX, sv->counters.cur_sess_max);
1659
1660 if (sv->maxconn)
1661 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, sv->maxconn);
1662
Willy Tarreauf21d17b2019-09-08 09:24:56 +02001663 stats[ST_F_SRV_ICUR] = mkf_u32(0, sv->curr_idle_conns);
1664 if (sv->max_idle_conns != -1)
1665 stats[ST_F_SRV_ILIM] = mkf_u32(FO_CONFIG|FN_LIMIT, sv->max_idle_conns);
1666
William Lallemand74c24fb2016-11-21 17:18:36 +01001667 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, sv->counters.cum_sess);
1668 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, sv->counters.bytes_in);
1669 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, sv->counters.bytes_out);
1670 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, sv->counters.failed_secu);
1671 stats[ST_F_ECON] = mkf_u64(FN_COUNTER, sv->counters.failed_conns);
1672 stats[ST_F_ERESP] = mkf_u64(FN_COUNTER, sv->counters.failed_resp);
1673 stats[ST_F_WRETR] = mkf_u64(FN_COUNTER, sv->counters.retries);
1674 stats[ST_F_WREDIS] = mkf_u64(FN_COUNTER, sv->counters.redispatches);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001675 stats[ST_F_WREW] = mkf_u64(FN_COUNTER, sv->counters.failed_rewrites);
Willy Tarreauf1573842018-12-14 11:35:36 +01001676 stats[ST_F_CONNECT] = mkf_u64(FN_COUNTER, sv->counters.connect);
1677 stats[ST_F_REUSE] = mkf_u64(FN_COUNTER, sv->counters.reuse);
William Lallemand74c24fb2016-11-21 17:18:36 +01001678
1679 /* status */
1680 fld_status = chunk_newstr(out);
Emeric Brun52a91d32017-08-31 14:41:55 +02001681 if (sv->cur_admin & SRV_ADMF_RMAINT)
William Lallemand74c24fb2016-11-21 17:18:36 +01001682 chunk_appendf(out, "MAINT (resolution)");
Emeric Brun52a91d32017-08-31 14:41:55 +02001683 else if (sv->cur_admin & SRV_ADMF_IMAINT)
William Lallemand74c24fb2016-11-21 17:18:36 +01001684 chunk_appendf(out, "MAINT (via %s/%s)", via->proxy->id, via->id);
Emeric Brun52a91d32017-08-31 14:41:55 +02001685 else if (sv->cur_admin & SRV_ADMF_MAINT)
William Lallemand74c24fb2016-11-21 17:18:36 +01001686 chunk_appendf(out, "MAINT");
1687 else
1688 chunk_appendf(out,
1689 srv_hlt_st[state],
Emeric Brun52a91d32017-08-31 14:41:55 +02001690 (ref->cur_state != SRV_ST_STOPPED) ? (ref->check.health - ref->check.rise + 1) : (ref->check.health),
1691 (ref->cur_state != SRV_ST_STOPPED) ? (ref->check.fall) : (ref->check.rise));
William Lallemand74c24fb2016-11-21 17:18:36 +01001692
1693 stats[ST_F_STATUS] = mkf_str(FO_STATUS, fld_status);
1694 stats[ST_F_LASTCHG] = mkf_u32(FN_AGE, now.tv_sec - sv->last_change);
Emeric Brun52a91d32017-08-31 14:41:55 +02001695 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 +01001696 stats[ST_F_ACT] = mkf_u32(FO_STATUS, (sv->flags & SRV_F_BACKUP) ? 0 : 1);
1697 stats[ST_F_BCK] = mkf_u32(FO_STATUS, (sv->flags & SRV_F_BACKUP) ? 1 : 0);
1698
1699 /* check failures: unique, fatal; last change, total downtime */
1700 if (sv->check.state & CHK_ST_ENABLED) {
1701 stats[ST_F_CHKFAIL] = mkf_u64(FN_COUNTER, sv->counters.failed_checks);
1702 stats[ST_F_CHKDOWN] = mkf_u64(FN_COUNTER, sv->counters.down_trans);
1703 stats[ST_F_DOWNTIME] = mkf_u32(FN_COUNTER, srv_downtime(sv));
1704 }
1705
1706 if (sv->maxqueue)
1707 stats[ST_F_QLIMIT] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->maxqueue);
1708
1709 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
1710 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1711 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, sv->puid);
1712
Emeric Brun52a91d32017-08-31 14:41:55 +02001713 if (sv->cur_state == SRV_ST_STARTING && !server_is_draining(sv))
William Lallemand74c24fb2016-11-21 17:18:36 +01001714 stats[ST_F_THROTTLE] = mkf_u32(FN_AVG, server_throttle_rate(sv));
1715
1716 stats[ST_F_LBTOT] = mkf_u64(FN_COUNTER, sv->counters.cum_lbconn);
1717
1718 if (sv->track) {
1719 char *fld_track = chunk_newstr(out);
1720
1721 chunk_appendf(out, "%s/%s", sv->track->proxy->id, sv->track->id);
1722 stats[ST_F_TRACKED] = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, fld_track);
1723 }
1724
1725 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_SV);
1726 stats[ST_F_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&sv->sess_per_sec));
1727 stats[ST_F_RATE_MAX] = mkf_u32(FN_MAX, sv->counters.sps_max);
1728
1729 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) {
1730 const char *fld_chksts;
1731
1732 fld_chksts = chunk_newstr(out);
1733 chunk_strcat(out, "* "); // for check in progress
1734 chunk_strcat(out, get_check_status_info(sv->check.status));
1735 if (!(sv->check.state & CHK_ST_INPROGRESS))
1736 fld_chksts += 2; // skip "* "
1737 stats[ST_F_CHECK_STATUS] = mkf_str(FN_OUTPUT, fld_chksts);
1738
1739 if (sv->check.status >= HCHK_STATUS_L57DATA)
1740 stats[ST_F_CHECK_CODE] = mkf_u32(FN_OUTPUT, sv->check.code);
1741
1742 if (sv->check.status >= HCHK_STATUS_CHECKED)
1743 stats[ST_F_CHECK_DURATION] = mkf_u64(FN_DURATION, sv->check.duration);
1744
1745 stats[ST_F_CHECK_DESC] = mkf_str(FN_OUTPUT, get_check_status_description(sv->check.status));
1746 stats[ST_F_LAST_CHK] = mkf_str(FN_OUTPUT, sv->check.desc);
1747 stats[ST_F_CHECK_RISE] = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.rise);
1748 stats[ST_F_CHECK_FALL] = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.fall);
1749 stats[ST_F_CHECK_HEALTH] = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.health);
1750 }
1751
1752 if ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) {
1753 const char *fld_chksts;
1754
1755 fld_chksts = chunk_newstr(out);
1756 chunk_strcat(out, "* "); // for check in progress
1757 chunk_strcat(out, get_check_status_info(sv->agent.status));
1758 if (!(sv->agent.state & CHK_ST_INPROGRESS))
1759 fld_chksts += 2; // skip "* "
1760 stats[ST_F_AGENT_STATUS] = mkf_str(FN_OUTPUT, fld_chksts);
1761
1762 if (sv->agent.status >= HCHK_STATUS_L57DATA)
1763 stats[ST_F_AGENT_CODE] = mkf_u32(FN_OUTPUT, sv->agent.code);
1764
1765 if (sv->agent.status >= HCHK_STATUS_CHECKED)
1766 stats[ST_F_AGENT_DURATION] = mkf_u64(FN_DURATION, sv->agent.duration);
1767
1768 stats[ST_F_AGENT_DESC] = mkf_str(FN_OUTPUT, get_check_status_description(sv->agent.status));
1769 stats[ST_F_LAST_AGT] = mkf_str(FN_OUTPUT, sv->agent.desc);
1770 stats[ST_F_AGENT_RISE] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.rise);
1771 stats[ST_F_AGENT_FALL] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.fall);
1772 stats[ST_F_AGENT_HEALTH] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.health);
1773 }
1774
1775 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
1776 if (px->mode == PR_MODE_HTTP) {
1777 stats[ST_F_HRSP_1XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[1]);
1778 stats[ST_F_HRSP_2XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[2]);
1779 stats[ST_F_HRSP_3XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[3]);
1780 stats[ST_F_HRSP_4XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[4]);
1781 stats[ST_F_HRSP_5XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[5]);
1782 stats[ST_F_HRSP_OTHER] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[0]);
1783 }
1784
1785 if (ref->observe)
1786 stats[ST_F_HANAFAIL] = mkf_u64(FN_COUNTER, sv->counters.failed_hana);
1787
1788 stats[ST_F_CLI_ABRT] = mkf_u64(FN_COUNTER, sv->counters.cli_aborts);
1789 stats[ST_F_SRV_ABRT] = mkf_u64(FN_COUNTER, sv->counters.srv_aborts);
1790 stats[ST_F_LASTSESS] = mkf_s32(FN_AGE, srv_lastsession(sv));
1791
1792 stats[ST_F_QTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.q_time, TIME_STATS_SAMPLES));
1793 stats[ST_F_CTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.c_time, TIME_STATS_SAMPLES));
1794 stats[ST_F_RTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.d_time, TIME_STATS_SAMPLES));
1795 stats[ST_F_TTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.t_time, TIME_STATS_SAMPLES));
1796
1797 if (flags & ST_SHLGNDS) {
1798 switch (addr_to_str(&sv->addr, str, sizeof(str))) {
1799 case AF_INET:
1800 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
Willy Tarreau04276f32017-01-06 17:41:29 +01001801 chunk_appendf(out, "%s:%d", str, sv->svc_port);
William Lallemand74c24fb2016-11-21 17:18:36 +01001802 break;
1803 case AF_INET6:
1804 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
Willy Tarreau04276f32017-01-06 17:41:29 +01001805 chunk_appendf(out, "[%s]:%d", str, sv->svc_port);
William Lallemand74c24fb2016-11-21 17:18:36 +01001806 break;
1807 case AF_UNIX:
1808 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, "unix");
1809 break;
1810 case -1:
1811 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1812 chunk_strcat(out, strerror(errno));
1813 break;
1814 default: /* address family not supported */
1815 break;
1816 }
1817
1818 if (sv->cookie)
1819 stats[ST_F_COOKIE] = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, sv->cookie);
1820 }
1821
1822 return 1;
1823}
1824
1825/* Dumps a line for server <sv> and proxy <px> to the trash and uses the state
1826 * from stream interface <si>, stats flags <flags>, and server state <state>.
1827 * The caller is responsible for clearing the trash if needed. Returns non-zero
1828 * if it emits anything, zero otherwise.
1829 */
1830static int stats_dump_sv_stats(struct stream_interface *si, struct proxy *px, int flags, struct server *sv)
1831{
1832 struct appctx *appctx = __objt_appctx(si->end);
1833
1834 if (!stats_fill_sv_stats(px, sv, flags, stats, ST_F_TOTAL_FIELDS))
1835 return 0;
1836
1837 return stats_dump_one_line(stats, flags, px, appctx);
1838}
1839
1840/* Fill <stats> with the backend statistics. <stats> is
1841 * preallocated array of length <len>. The length of the array
1842 * must be at least ST_F_TOTAL_FIELDS. If this length is less
1843 * then this value, the function returns 0, otherwise, it
1844 * returns 1. <flags> can take the value ST_SHLGNDS.
1845 */
1846int stats_fill_be_stats(struct proxy *px, int flags, struct field *stats, int len)
1847{
1848 if (len < ST_F_TOTAL_FIELDS)
1849 return 0;
1850
1851 memset(stats, 0, sizeof(*stats) * len);
1852
1853 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1854 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, "BACKEND");
1855 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1856 stats[ST_F_QCUR] = mkf_u32(0, px->nbpend);
1857 stats[ST_F_QMAX] = mkf_u32(FN_MAX, px->be_counters.nbpend_max);
Thierry FOURNIER0ff98a42016-12-19 16:50:42 +01001858 stats[ST_F_SCUR] = mkf_u32(0, px->beconn);
William Lallemand74c24fb2016-11-21 17:18:36 +01001859 stats[ST_F_SMAX] = mkf_u32(FN_MAX, px->be_counters.conn_max);
1860 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, px->fullconn);
1861 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, px->be_counters.cum_conn);
1862 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, px->be_counters.bytes_in);
1863 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, px->be_counters.bytes_out);
1864 stats[ST_F_DREQ] = mkf_u64(FN_COUNTER, px->be_counters.denied_req);
1865 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, px->be_counters.denied_resp);
1866 stats[ST_F_ECON] = mkf_u64(FN_COUNTER, px->be_counters.failed_conns);
1867 stats[ST_F_ERESP] = mkf_u64(FN_COUNTER, px->be_counters.failed_resp);
1868 stats[ST_F_WRETR] = mkf_u64(FN_COUNTER, px->be_counters.retries);
1869 stats[ST_F_WREDIS] = mkf_u64(FN_COUNTER, px->be_counters.redispatches);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001870 stats[ST_F_WREW] = mkf_u64(FN_COUNTER, px->be_counters.failed_rewrites);
Willy Tarreauf1573842018-12-14 11:35:36 +01001871 stats[ST_F_CONNECT] = mkf_u64(FN_COUNTER, px->be_counters.connect);
1872 stats[ST_F_REUSE] = mkf_u64(FN_COUNTER, px->be_counters.reuse);
William Lallemand74c24fb2016-11-21 17:18:36 +01001873 stats[ST_F_STATUS] = mkf_str(FO_STATUS, (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" : "DOWN");
1874 stats[ST_F_WEIGHT] = mkf_u32(FN_AVG, (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv);
1875 stats[ST_F_ACT] = mkf_u32(0, px->srv_act);
1876 stats[ST_F_BCK] = mkf_u32(0, px->srv_bck);
1877 stats[ST_F_CHKDOWN] = mkf_u64(FN_COUNTER, px->down_trans);
1878 stats[ST_F_LASTCHG] = mkf_u32(FN_AGE, now.tv_sec - px->last_change);
1879 if (px->srv)
1880 stats[ST_F_DOWNTIME] = mkf_u32(FN_COUNTER, be_downtime(px));
1881
1882 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
1883 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1884 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, 0);
1885 stats[ST_F_LBTOT] = mkf_u64(FN_COUNTER, px->be_counters.cum_lbconn);
1886 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_BE);
1887 stats[ST_F_RATE] = mkf_u32(0, read_freq_ctr(&px->be_sess_per_sec));
1888 stats[ST_F_RATE_MAX] = mkf_u32(0, px->be_counters.sps_max);
1889
1890 if (flags & ST_SHLGNDS) {
1891 if (px->cookie_name)
1892 stats[ST_F_COOKIE] = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, px->cookie_name);
1893 stats[ST_F_ALGO] = mkf_str(FO_CONFIG|FS_SERVICE, backend_lb_algo_str(px->lbprm.algo & BE_LB_ALGO));
1894 }
1895
1896 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
1897 if (px->mode == PR_MODE_HTTP) {
1898 stats[ST_F_REQ_TOT] = mkf_u64(FN_COUNTER, px->be_counters.p.http.cum_req);
1899 stats[ST_F_HRSP_1XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[1]);
1900 stats[ST_F_HRSP_2XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[2]);
1901 stats[ST_F_HRSP_3XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[3]);
1902 stats[ST_F_HRSP_4XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[4]);
1903 stats[ST_F_HRSP_5XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[5]);
1904 stats[ST_F_HRSP_OTHER] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[0]);
Willy Tarreaua1214a52018-12-14 14:00:25 +01001905 stats[ST_F_CACHE_LOOKUPS] = mkf_u64(FN_COUNTER, px->be_counters.p.http.cache_lookups);
1906 stats[ST_F_CACHE_HITS] = mkf_u64(FN_COUNTER, px->be_counters.p.http.cache_hits);
William Lallemand74c24fb2016-11-21 17:18:36 +01001907 }
1908
1909 stats[ST_F_CLI_ABRT] = mkf_u64(FN_COUNTER, px->be_counters.cli_aborts);
1910 stats[ST_F_SRV_ABRT] = mkf_u64(FN_COUNTER, px->be_counters.srv_aborts);
1911
1912 /* compression: in, out, bypassed, responses */
1913 stats[ST_F_COMP_IN] = mkf_u64(FN_COUNTER, px->be_counters.comp_in);
1914 stats[ST_F_COMP_OUT] = mkf_u64(FN_COUNTER, px->be_counters.comp_out);
1915 stats[ST_F_COMP_BYP] = mkf_u64(FN_COUNTER, px->be_counters.comp_byp);
1916 stats[ST_F_COMP_RSP] = mkf_u64(FN_COUNTER, px->be_counters.p.http.comp_rsp);
1917 stats[ST_F_LASTSESS] = mkf_s32(FN_AGE, be_lastsession(px));
1918
1919 stats[ST_F_QTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.q_time, TIME_STATS_SAMPLES));
1920 stats[ST_F_CTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.c_time, TIME_STATS_SAMPLES));
1921 stats[ST_F_RTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.d_time, TIME_STATS_SAMPLES));
1922 stats[ST_F_TTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.t_time, TIME_STATS_SAMPLES));
1923
1924 return 1;
1925}
1926
1927/* Dumps a line for backend <px> to the trash for and uses the state from stream
1928 * interface <si> and stats flags <flags>. The caller is responsible for clearing
1929 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
1930 */
1931static int stats_dump_be_stats(struct stream_interface *si, struct proxy *px, int flags)
1932{
1933 struct appctx *appctx = __objt_appctx(si->end);
1934
1935 if (!(px->cap & PR_CAP_BE))
1936 return 0;
1937
1938 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_BE)))
1939 return 0;
1940
1941 if (!stats_fill_be_stats(px, flags, stats, ST_F_TOTAL_FIELDS))
1942 return 0;
1943
1944 return stats_dump_one_line(stats, flags, px, appctx);
1945}
1946
1947/* Dumps the HTML table header for proxy <px> to the trash for and uses the state from
1948 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
1949 * for clearing the trash if needed.
1950 */
1951static void stats_dump_html_px_hdr(struct stream_interface *si, struct proxy *px, struct uri_auth *uri)
1952{
1953 struct appctx *appctx = __objt_appctx(si->end);
1954 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
1955
1956 if (px->cap & PR_CAP_BE && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
1957 /* A form to enable/disable this proxy servers */
1958
1959 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
1960 scope_txt[0] = 0;
1961 if (appctx->ctx.stats.scope_len) {
Christopher Fauleted7a0662019-01-14 11:07:34 +01001962 const char *scope_ptr = stats_scope_ptr(appctx, si);
1963
William Lallemand74c24fb2016-11-21 17:18:36 +01001964 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Christopher Fauleted7a0662019-01-14 11:07:34 +01001965 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), scope_ptr, appctx->ctx.stats.scope_len);
William Lallemand74c24fb2016-11-21 17:18:36 +01001966 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
1967 }
1968
1969 chunk_appendf(&trash,
1970 "<form method=\"post\">");
1971 }
1972
1973 /* print a new table */
1974 chunk_appendf(&trash,
1975 "<table class=\"tbl\" width=\"100%%\">\n"
1976 "<tr class=\"titre\">"
1977 "<th class=\"pxname\" width=\"10%%\">");
1978
1979 chunk_appendf(&trash,
1980 "<a name=\"%s\"></a>%s"
1981 "<a class=px href=\"#%s\">%s</a>",
1982 px->id,
1983 (uri->flags & ST_SHLGNDS) ? "<u>":"",
1984 px->id, px->id);
1985
1986 if (uri->flags & ST_SHLGNDS) {
1987 /* cap, mode, id */
1988 chunk_appendf(&trash, "<div class=tips>cap: %s, mode: %s, id: %d",
1989 proxy_cap_str(px->cap), proxy_mode_str(px->mode),
1990 px->uuid);
1991 chunk_appendf(&trash, "</div>");
1992 }
1993
1994 chunk_appendf(&trash,
1995 "%s</th>"
1996 "<th class=\"%s\" width=\"90%%\">%s</th>"
1997 "</tr>\n"
1998 "</table>\n"
1999 "<table class=\"tbl\" width=\"100%%\">\n"
2000 "<tr class=\"titre\">",
2001 (uri->flags & ST_SHLGNDS) ? "</u>":"",
2002 px->desc ? "desc" : "empty", px->desc ? px->desc : "");
2003
2004 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
2005 /* Column heading for Enable or Disable server */
David Harrigand3db35a2016-12-30 12:12:49 +00002006 chunk_appendf(&trash,
2007 "<th rowspan=2 width=1><input type=\"checkbox\" \
2008 onclick=\"for(c in document.getElementsByClassName('%s-checkbox')) \
2009 document.getElementsByClassName('%s-checkbox').item(c).checked = this.checked\"></th>",
2010 px->id,
2011 px->id);
William Lallemand74c24fb2016-11-21 17:18:36 +01002012 }
2013
2014 chunk_appendf(&trash,
2015 "<th rowspan=2></th>"
2016 "<th colspan=3>Queue</th>"
2017 "<th colspan=3>Session rate</th><th colspan=6>Sessions</th>"
2018 "<th colspan=2>Bytes</th><th colspan=2>Denied</th>"
2019 "<th colspan=3>Errors</th><th colspan=2>Warnings</th>"
2020 "<th colspan=9>Server</th>"
2021 "</tr>\n"
2022 "<tr class=\"titre\">"
2023 "<th>Cur</th><th>Max</th><th>Limit</th>"
2024 "<th>Cur</th><th>Max</th><th>Limit</th><th>Cur</th><th>Max</th>"
2025 "<th>Limit</th><th>Total</th><th>LbTot</th><th>Last</th><th>In</th><th>Out</th>"
2026 "<th>Req</th><th>Resp</th><th>Req</th><th>Conn</th>"
2027 "<th>Resp</th><th>Retr</th><th>Redis</th>"
2028 "<th>Status</th><th>LastChk</th><th>Wght</th><th>Act</th>"
2029 "<th>Bck</th><th>Chk</th><th>Dwn</th><th>Dwntme</th>"
2030 "<th>Thrtle</th>\n"
2031 "</tr>");
2032}
2033
2034/* Dumps the HTML table trailer for proxy <px> to the trash for and uses the state from
2035 * stream interface <si>. The caller is responsible for clearing the trash if needed.
2036 */
2037static void stats_dump_html_px_end(struct stream_interface *si, struct proxy *px)
2038{
2039 struct appctx *appctx = __objt_appctx(si->end);
2040 chunk_appendf(&trash, "</table>");
2041
2042 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
2043 /* close the form used to enable/disable this proxy servers */
2044 chunk_appendf(&trash,
2045 "Choose the action to perform on the checked servers : "
2046 "<select name=action>"
2047 "<option value=\"\"></option>"
2048 "<option value=\"ready\">Set state to READY</option>"
2049 "<option value=\"drain\">Set state to DRAIN</option>"
2050 "<option value=\"maint\">Set state to MAINT</option>"
2051 "<option value=\"dhlth\">Health: disable checks</option>"
2052 "<option value=\"ehlth\">Health: enable checks</option>"
2053 "<option value=\"hrunn\">Health: force UP</option>"
2054 "<option value=\"hnolb\">Health: force NOLB</option>"
2055 "<option value=\"hdown\">Health: force DOWN</option>"
2056 "<option value=\"dagent\">Agent: disable checks</option>"
2057 "<option value=\"eagent\">Agent: enable checks</option>"
2058 "<option value=\"arunn\">Agent: force UP</option>"
2059 "<option value=\"adown\">Agent: force DOWN</option>"
2060 "<option value=\"shutdown\">Kill Sessions</option>"
2061 "</select>"
2062 "<input type=\"hidden\" name=\"b\" value=\"#%d\">"
2063 "&nbsp;<input type=\"submit\" value=\"Apply\">"
2064 "</form>",
2065 px->uuid);
2066 }
2067
2068 chunk_appendf(&trash, "<p>\n");
2069}
2070
2071/*
2072 * Dumps statistics for a proxy. The output is sent to the stream interface's
2073 * input buffer. Returns 0 if it had to stop dumping data because of lack of
2074 * buffer space, or non-zero if everything completed. This function is used
2075 * both by the CLI and the HTTP entry points, and is able to dump the output
2076 * in HTML or CSV formats. If the later, <uri> must be NULL.
2077 */
Christopher Fauletef779222018-10-31 08:47:01 +01002078int stats_dump_proxy_to_buffer(struct stream_interface *si, struct htx *htx,
2079 struct proxy *px, struct uri_auth *uri)
William Lallemand74c24fb2016-11-21 17:18:36 +01002080{
2081 struct appctx *appctx = __objt_appctx(si->end);
2082 struct stream *s = si_strm(si);
2083 struct channel *rep = si_ic(si);
2084 struct server *sv, *svs; /* server and server-state, server-state=server or server->track */
2085 struct listener *l;
2086 unsigned int flags;
2087
2088 if (uri)
2089 flags = uri->flags;
William Lallemand07a62f72017-05-24 00:57:40 +02002090 else if ((strm_li(s)->bind_conf->level & ACCESS_LVL_MASK) >= ACCESS_LVL_OPER)
William Lallemand74c24fb2016-11-21 17:18:36 +01002091 flags = ST_SHLGNDS | ST_SHNODE | ST_SHDESC;
2092 else
2093 flags = ST_SHNODE | ST_SHDESC;
2094
2095 chunk_reset(&trash);
2096
2097 switch (appctx->ctx.stats.px_st) {
2098 case STAT_PX_ST_INIT:
2099 /* we are on a new proxy */
2100 if (uri && uri->scope) {
2101 /* we have a limited scope, we have to check the proxy name */
2102 struct stat_scope *scope;
2103 int len;
2104
2105 len = strlen(px->id);
2106 scope = uri->scope;
2107
2108 while (scope) {
2109 /* match exact proxy name */
2110 if (scope->px_len == len && !memcmp(px->id, scope->px_id, len))
2111 break;
2112
2113 /* match '.' which means 'self' proxy */
2114 if (!strcmp(scope->px_id, ".") && px == s->be)
2115 break;
2116 scope = scope->next;
2117 }
2118
2119 /* proxy name not found : don't dump anything */
2120 if (scope == NULL)
2121 return 1;
2122 }
2123
2124 /* if the user has requested a limited output and the proxy
2125 * name does not match, skip it.
2126 */
Christopher Fauleted7a0662019-01-14 11:07:34 +01002127 if (appctx->ctx.stats.scope_len) {
2128 const char *scope_ptr = stats_scope_ptr(appctx, si);
2129
2130 if (strnistr(px->id, strlen(px->id), scope_ptr, appctx->ctx.stats.scope_len) == NULL)
2131 return 1;
2132 }
William Lallemand74c24fb2016-11-21 17:18:36 +01002133
2134 if ((appctx->ctx.stats.flags & STAT_BOUND) &&
2135 (appctx->ctx.stats.iid != -1) &&
2136 (px->uuid != appctx->ctx.stats.iid))
2137 return 1;
2138
2139 appctx->ctx.stats.px_st = STAT_PX_ST_TH;
2140 /* fall through */
2141
2142 case STAT_PX_ST_TH:
2143 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
2144 stats_dump_html_px_hdr(si, px, uri);
Christopher Fauletef779222018-10-31 08:47:01 +01002145 if (!stats_putchk(rep, htx, &trash))
2146 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002147 }
2148
2149 appctx->ctx.stats.px_st = STAT_PX_ST_FE;
2150 /* fall through */
2151
2152 case STAT_PX_ST_FE:
2153 /* print the frontend */
2154 if (stats_dump_fe_stats(si, px)) {
Christopher Fauletef779222018-10-31 08:47:01 +01002155 if (!stats_putchk(rep, htx, &trash))
2156 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002157 }
2158
2159 appctx->ctx.stats.l = px->conf.listeners.n;
2160 appctx->ctx.stats.px_st = STAT_PX_ST_LI;
2161 /* fall through */
2162
2163 case STAT_PX_ST_LI:
2164 /* stats.l has been initialized above */
2165 for (; appctx->ctx.stats.l != &px->conf.listeners; appctx->ctx.stats.l = l->by_fe.n) {
Christopher Fauletef779222018-10-31 08:47:01 +01002166 if (htx) {
2167 if (htx_almost_full(htx))
2168 goto full;
2169 }
2170 else {
2171 if (buffer_almost_full(&rep->buf))
2172 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002173 }
2174
2175 l = LIST_ELEM(appctx->ctx.stats.l, struct listener *, by_fe);
2176 if (!l->counters)
2177 continue;
2178
2179 if (appctx->ctx.stats.flags & STAT_BOUND) {
2180 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SO)))
2181 break;
2182
2183 if (appctx->ctx.stats.sid != -1 && l->luid != appctx->ctx.stats.sid)
2184 continue;
2185 }
2186
2187 /* print the frontend */
2188 if (stats_dump_li_stats(si, px, l, flags)) {
Christopher Fauletef779222018-10-31 08:47:01 +01002189 if (!stats_putchk(rep, htx, &trash))
2190 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002191 }
2192 }
2193
2194 appctx->ctx.stats.sv = px->srv; /* may be NULL */
2195 appctx->ctx.stats.px_st = STAT_PX_ST_SV;
2196 /* fall through */
2197
2198 case STAT_PX_ST_SV:
2199 /* stats.sv has been initialized above */
2200 for (; appctx->ctx.stats.sv != NULL; appctx->ctx.stats.sv = sv->next) {
Christopher Fauletef779222018-10-31 08:47:01 +01002201 if (htx) {
2202 if (htx_almost_full(htx))
2203 goto full;
2204 }
2205 else {
2206 if (buffer_almost_full(&rep->buf))
2207 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002208 }
2209
2210 sv = appctx->ctx.stats.sv;
2211
2212 if (appctx->ctx.stats.flags & STAT_BOUND) {
2213 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SV)))
2214 break;
2215
2216 if (appctx->ctx.stats.sid != -1 && sv->puid != appctx->ctx.stats.sid)
2217 continue;
2218 }
2219
2220 svs = sv;
2221 while (svs->track)
2222 svs = svs->track;
2223
2224 /* do not report servers which are DOWN and not changing state */
2225 if ((appctx->ctx.stats.flags & STAT_HIDE_DOWN) &&
Emeric Brun52a91d32017-08-31 14:41:55 +02002226 ((sv->cur_admin & SRV_ADMF_MAINT) || /* server is in maintenance */
2227 (sv->cur_state == SRV_ST_STOPPED && /* server is down */
William Lallemand74c24fb2016-11-21 17:18:36 +01002228 (!((svs->agent.state | svs->check.state) & CHK_ST_ENABLED) ||
2229 ((svs->agent.state & CHK_ST_ENABLED) && !svs->agent.health) ||
2230 ((svs->check.state & CHK_ST_ENABLED) && !svs->check.health))))) {
2231 continue;
2232 }
2233
2234 if (stats_dump_sv_stats(si, px, flags, sv)) {
Christopher Fauletef779222018-10-31 08:47:01 +01002235 if (!stats_putchk(rep, htx, &trash))
2236 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002237 }
2238 } /* for sv */
2239
2240 appctx->ctx.stats.px_st = STAT_PX_ST_BE;
2241 /* fall through */
2242
2243 case STAT_PX_ST_BE:
2244 /* print the backend */
2245 if (stats_dump_be_stats(si, px, flags)) {
Christopher Fauletef779222018-10-31 08:47:01 +01002246 if (!stats_putchk(rep, htx, &trash))
2247 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002248 }
2249
2250 appctx->ctx.stats.px_st = STAT_PX_ST_END;
2251 /* fall through */
2252
2253 case STAT_PX_ST_END:
2254 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
2255 stats_dump_html_px_end(si, px);
Christopher Fauletef779222018-10-31 08:47:01 +01002256 if (!stats_putchk(rep, htx, &trash))
2257 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002258 }
2259
2260 appctx->ctx.stats.px_st = STAT_PX_ST_FIN;
2261 /* fall through */
2262
2263 case STAT_PX_ST_FIN:
2264 return 1;
2265
2266 default:
2267 /* unknown state, we should put an abort() here ! */
2268 return 1;
2269 }
Christopher Fauletef779222018-10-31 08:47:01 +01002270
2271 full:
2272 si_rx_room_blk(si);
2273 return 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01002274}
2275
2276/* Dumps the HTTP stats head block to the trash for and uses the per-uri
2277 * parameters <uri>. The caller is responsible for clearing the trash if needed.
2278 */
2279static void stats_dump_html_head(struct uri_auth *uri)
2280{
2281 /* WARNING! This must fit in the first buffer !!! */
2282 chunk_appendf(&trash,
2283 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n"
2284 "\"http://www.w3.org/TR/html4/loose.dtd\">\n"
2285 "<html><head><title>Statistics Report for " PRODUCT_NAME "%s%s</title>\n"
2286 "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n"
2287 "<style type=\"text/css\"><!--\n"
2288 "body {"
2289 " font-family: arial, helvetica, sans-serif;"
2290 " font-size: 12px;"
2291 " font-weight: normal;"
2292 " color: black;"
2293 " background: white;"
2294 "}\n"
2295 "th,td {"
2296 " font-size: 10px;"
2297 "}\n"
2298 "h1 {"
2299 " font-size: x-large;"
2300 " margin-bottom: 0.5em;"
2301 "}\n"
2302 "h2 {"
2303 " font-family: helvetica, arial;"
2304 " font-size: x-large;"
2305 " font-weight: bold;"
2306 " font-style: italic;"
2307 " color: #6020a0;"
2308 " margin-top: 0em;"
2309 " margin-bottom: 0em;"
2310 "}\n"
2311 "h3 {"
2312 " font-family: helvetica, arial;"
2313 " font-size: 16px;"
2314 " font-weight: bold;"
2315 " color: #b00040;"
2316 " background: #e8e8d0;"
2317 " margin-top: 0em;"
2318 " margin-bottom: 0em;"
2319 "}\n"
2320 "li {"
2321 " margin-top: 0.25em;"
2322 " margin-right: 2em;"
2323 "}\n"
2324 ".hr {margin-top: 0.25em;"
2325 " border-color: black;"
2326 " border-bottom-style: solid;"
2327 "}\n"
2328 ".titre {background: #20D0D0;color: #000000; font-weight: bold; text-align: center;}\n"
2329 ".total {background: #20D0D0;color: #ffff80;}\n"
2330 ".frontend {background: #e8e8d0;}\n"
2331 ".socket {background: #d0d0d0;}\n"
2332 ".backend {background: #e8e8d0;}\n"
2333 ".active_down {background: #ff9090;}\n"
2334 ".active_going_up {background: #ffd020;}\n"
2335 ".active_going_down {background: #ffffa0;}\n"
2336 ".active_up {background: #c0ffc0;}\n"
2337 ".active_nolb {background: #20a0ff;}\n"
2338 ".active_draining {background: #20a0FF;}\n"
2339 ".active_no_check {background: #e0e0e0;}\n"
2340 ".backup_down {background: #ff9090;}\n"
2341 ".backup_going_up {background: #ff80ff;}\n"
2342 ".backup_going_down {background: #c060ff;}\n"
2343 ".backup_up {background: #b0d0ff;}\n"
2344 ".backup_nolb {background: #90b0e0;}\n"
2345 ".backup_draining {background: #cc9900;}\n"
2346 ".backup_no_check {background: #e0e0e0;}\n"
2347 ".maintain {background: #c07820;}\n"
2348 ".rls {letter-spacing: 0.2em; margin-right: 1px;}\n" /* right letter spacing (used for grouping digits) */
2349 "\n"
2350 "a.px:link {color: #ffff40; text-decoration: none;}"
2351 "a.px:visited {color: #ffff40; text-decoration: none;}"
2352 "a.px:hover {color: #ffffff; text-decoration: none;}"
2353 "a.lfsb:link {color: #000000; text-decoration: none;}"
2354 "a.lfsb:visited {color: #000000; text-decoration: none;}"
2355 "a.lfsb:hover {color: #505050; text-decoration: none;}"
2356 "\n"
2357 "table.tbl { border-collapse: collapse; border-style: none;}\n"
2358 "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"
2359 "table.tbl td.ac { text-align: center;}\n"
2360 "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}\n"
2361 "table.tbl th.pxname { background: #b00040; color: #ffff40; font-weight: bold; border-style: solid solid none solid; padding: 2px 3px; white-space: nowrap;}\n"
2362 "table.tbl th.empty { border-style: none; empty-cells: hide; background: white;}\n"
2363 "table.tbl th.desc { background: white; border-style: solid solid none solid; text-align: left; padding: 2px 3px;}\n"
2364 "\n"
2365 "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n"
2366 "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n"
2367 "table.lgd td.noborder { border-style: none; padding: 2px; white-space: nowrap;}\n"
2368 "table.det { border-collapse: collapse; border-style: none; }\n"
2369 "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"
2370 "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"
2371 "u {text-decoration:none; border-bottom: 1px dotted black;}\n"
2372 "div.tips {\n"
2373 " display:block;\n"
2374 " visibility:hidden;\n"
2375 " z-index:2147483647;\n"
2376 " position:absolute;\n"
2377 " padding:2px 4px 3px;\n"
2378 " background:#f0f060; color:#000000;\n"
2379 " border:1px solid #7040c0;\n"
2380 " white-space:nowrap;\n"
2381 " font-style:normal;font-size:11px;font-weight:normal;\n"
2382 " -moz-border-radius:3px;-webkit-border-radius:3px;border-radius:3px;\n"
2383 " -moz-box-shadow:gray 2px 2px 3px;-webkit-box-shadow:gray 2px 2px 3px;box-shadow:gray 2px 2px 3px;\n"
2384 "}\n"
2385 "u:hover div.tips {visibility:visible;}\n"
2386 "-->\n"
2387 "</style></head>\n",
2388 (uri->flags & ST_SHNODE) ? " on " : "",
2389 (uri->flags & ST_SHNODE) ? (uri->node ? uri->node : global.node) : ""
2390 );
2391}
2392
2393/* Dumps the HTML stats information block to the trash for and uses the state from
2394 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
2395 * for clearing the trash if needed.
2396 */
2397static void stats_dump_html_info(struct stream_interface *si, struct uri_auth *uri)
2398{
2399 struct appctx *appctx = __objt_appctx(si->end);
2400 unsigned int up = (now.tv_sec - start_date.tv_sec);
2401 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
Christopher Fauleted7a0662019-01-14 11:07:34 +01002402 const char *scope_ptr = stats_scope_ptr(appctx, si);
Willy Tarreau1713c032019-05-23 12:23:55 +02002403 unsigned long long bps = (unsigned long long)read_freq_ctr(&global.out_32bps) * 32;
2404
2405 /* Turn the bytes per second to bits per second and take care of the
2406 * usual ethernet overhead in order to help figure how far we are from
2407 * interface saturation since it's the only case which usually matters.
2408 * For this we count the total size of an Ethernet frame on the wire
2409 * including preamble and IFG (1538) for the largest TCP segment it
2410 * transports (1448 with TCP timestamps). This is not valid for smaller
2411 * packets (under-estimated), but it gives a reasonably accurate
2412 * estimation of how far we are from uplink saturation.
2413 */
2414 bps = bps * 8 * 1538 / 1448;
William Lallemand74c24fb2016-11-21 17:18:36 +01002415
2416 /* WARNING! this has to fit the first packet too.
2417 * We are around 3.5 kB, add adding entries will
2418 * become tricky if we want to support 4kB buffers !
2419 */
2420 chunk_appendf(&trash,
2421 "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">"
2422 PRODUCT_NAME "%s</a></h1>\n"
2423 "<h2>Statistics Report for pid %d%s%s%s%s</h2>\n"
2424 "<hr width=\"100%%\" class=\"hr\">\n"
2425 "<h3>&gt; General process information</h3>\n"
2426 "<table border=0><tr><td align=\"left\" nowrap width=\"1%%\">\n"
Yves Lafon95317282018-02-26 11:10:37 +01002427 "<p><b>pid = </b> %d (process #%d, nbproc = %d, nbthread = %d)<br>\n"
William Lallemand74c24fb2016-11-21 17:18:36 +01002428 "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n"
2429 "<b>system limits:</b> memmax = %s%s; ulimit-n = %d<br>\n"
2430 "<b>maxsock = </b> %d; <b>maxconn = </b> %d; <b>maxpipes = </b> %d<br>\n"
Willy Tarreau1713c032019-05-23 12:23:55 +02002431 "current conns = %d; current pipes = %d/%d; conn rate = %d/sec; bit rate = %.3f %cbps<br>\n"
William Lallemand74c24fb2016-11-21 17:18:36 +01002432 "Running tasks: %d/%d; idle = %d %%<br>\n"
2433 "</td><td align=\"center\" nowrap>\n"
2434 "<table class=\"lgd\"><tr>\n"
2435 "<td class=\"active_up\">&nbsp;</td><td class=\"noborder\">active UP </td>"
2436 "<td class=\"backup_up\">&nbsp;</td><td class=\"noborder\">backup UP </td>"
2437 "</tr><tr>\n"
2438 "<td class=\"active_going_down\"></td><td class=\"noborder\">active UP, going down </td>"
2439 "<td class=\"backup_going_down\"></td><td class=\"noborder\">backup UP, going down </td>"
2440 "</tr><tr>\n"
2441 "<td class=\"active_going_up\"></td><td class=\"noborder\">active DOWN, going up </td>"
2442 "<td class=\"backup_going_up\"></td><td class=\"noborder\">backup DOWN, going up </td>"
2443 "</tr><tr>\n"
2444 "<td class=\"active_down\"></td><td class=\"noborder\">active or backup DOWN &nbsp;</td>"
2445 "<td class=\"active_no_check\"></td><td class=\"noborder\">not checked </td>"
2446 "</tr><tr>\n"
2447 "<td class=\"maintain\"></td><td class=\"noborder\" colspan=\"3\">active or backup DOWN for maintenance (MAINT) &nbsp;</td>"
2448 "</tr><tr>\n"
2449 "<td class=\"active_draining\"></td><td class=\"noborder\" colspan=\"3\">active or backup SOFT STOPPED for maintenance &nbsp;</td>"
2450 "</tr></table>\n"
2451 "Note: \"NOLB\"/\"DRAIN\" = UP with load-balancing disabled."
2452 "</td>"
2453 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
2454 "<b>Display option:</b><ul style=\"margin-top: 0.25em;\">"
2455 "",
Willy Tarreau909b9d82019-01-04 18:20:32 +01002456 (uri->flags & ST_HIDEVER) ? "" : (stats_version_string),
William Lallemand74c24fb2016-11-21 17:18:36 +01002457 pid, (uri->flags & ST_SHNODE) ? " on " : "",
2458 (uri->flags & ST_SHNODE) ? (uri->node ? uri->node : global.node) : "",
2459 (uri->flags & ST_SHDESC) ? ": " : "",
2460 (uri->flags & ST_SHDESC) ? (uri->desc ? uri->desc : global.desc) : "",
Yves Lafon95317282018-02-26 11:10:37 +01002461 pid, relative_pid, global.nbproc, global.nbthread,
William Lallemand74c24fb2016-11-21 17:18:36 +01002462 up / 86400, (up % 86400) / 3600,
2463 (up % 3600) / 60, (up % 60),
2464 global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited",
2465 global.rlimit_memmax ? " MB" : "",
2466 global.rlimit_nofile,
2467 global.maxsock, global.maxconn, global.maxpipes,
2468 actconn, pipes_used, pipes_used+pipes_free, read_freq_ctr(&global.conn_per_sec),
Willy Tarreau1713c032019-05-23 12:23:55 +02002469 bps >= 1000000000UL ? (bps / 1000000000.0) : bps >= 1000000UL ? (bps / 1000000.0) : (bps / 1000.0),
2470 bps >= 1000000000UL ? 'G' : bps >= 1000000UL ? 'M' : 'k',
Willy Tarreau81036f22019-05-20 19:24:50 +02002471 tasks_run_queue_cur, nb_tasks_cur, ti->idle_pct
William Lallemand74c24fb2016-11-21 17:18:36 +01002472 );
2473
2474 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Christopher Fauleted7a0662019-01-14 11:07:34 +01002475 memcpy(scope_txt, scope_ptr, appctx->ctx.stats.scope_len);
William Lallemand74c24fb2016-11-21 17:18:36 +01002476 scope_txt[appctx->ctx.stats.scope_len] = '\0';
2477
2478 chunk_appendf(&trash,
2479 "<li><form method=\"GET\">Scope : <input value=\"%s\" name=\"" STAT_SCOPE_INPUT_NAME "\" size=\"8\" maxlength=\"%d\" tabindex=\"1\"/></form>\n",
2480 (appctx->ctx.stats.scope_len > 0) ? scope_txt : "",
2481 STAT_SCOPE_TXT_MAXLEN);
2482
2483 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
2484 scope_txt[0] = 0;
2485 if (appctx->ctx.stats.scope_len) {
2486 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Christopher Fauleted7a0662019-01-14 11:07:34 +01002487 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), scope_ptr, appctx->ctx.stats.scope_len);
William Lallemand74c24fb2016-11-21 17:18:36 +01002488 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
2489 }
2490
2491 if (appctx->ctx.stats.flags & STAT_HIDE_DOWN)
2492 chunk_appendf(&trash,
2493 "<li><a href=\"%s%s%s%s\">Show all servers</a><br>\n",
2494 uri->uri_prefix,
2495 "",
2496 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2497 scope_txt);
2498 else
2499 chunk_appendf(&trash,
2500 "<li><a href=\"%s%s%s%s\">Hide 'DOWN' servers</a><br>\n",
2501 uri->uri_prefix,
2502 ";up",
2503 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2504 scope_txt);
2505
2506 if (uri->refresh > 0) {
2507 if (appctx->ctx.stats.flags & STAT_NO_REFRESH)
2508 chunk_appendf(&trash,
2509 "<li><a href=\"%s%s%s%s\">Enable refresh</a><br>\n",
2510 uri->uri_prefix,
2511 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2512 "",
2513 scope_txt);
2514 else
2515 chunk_appendf(&trash,
2516 "<li><a href=\"%s%s%s%s\">Disable refresh</a><br>\n",
2517 uri->uri_prefix,
2518 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2519 ";norefresh",
2520 scope_txt);
2521 }
2522
2523 chunk_appendf(&trash,
2524 "<li><a href=\"%s%s%s%s\">Refresh now</a><br>\n",
2525 uri->uri_prefix,
2526 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2527 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2528 scope_txt);
2529
2530 chunk_appendf(&trash,
2531 "<li><a href=\"%s;csv%s%s\">CSV export</a><br>\n",
2532 uri->uri_prefix,
2533 (uri->refresh > 0) ? ";norefresh" : "",
2534 scope_txt);
2535
2536 chunk_appendf(&trash,
Christopher Faulet6338a082019-09-09 15:50:54 +02002537 "<li><a href=\"%s;json%s%s\">JSON export</a> (<a href=\"%s;json-schema\">schema</a>)<br>\n",
2538 uri->uri_prefix,
2539 (uri->refresh > 0) ? ";norefresh" : "",
2540 scope_txt, uri->uri_prefix);
2541
2542 chunk_appendf(&trash,
William Lallemand74c24fb2016-11-21 17:18:36 +01002543 "</ul></td>"
2544 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
2545 "<b>External resources:</b><ul style=\"margin-top: 0.25em;\">\n"
2546 "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>\n"
2547 "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>\n"
2548 "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>\n"
2549 "</ul>"
2550 "</td>"
2551 "</tr></table>\n"
2552 ""
2553 );
2554
2555 if (appctx->ctx.stats.st_code) {
2556 switch (appctx->ctx.stats.st_code) {
2557 case STAT_STATUS_DONE:
2558 chunk_appendf(&trash,
2559 "<p><div class=active_up>"
2560 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2561 "Action processed successfully."
2562 "</div>\n", uri->uri_prefix,
2563 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2564 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2565 scope_txt);
2566 break;
2567 case STAT_STATUS_NONE:
2568 chunk_appendf(&trash,
2569 "<p><div class=active_going_down>"
2570 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2571 "Nothing has changed."
2572 "</div>\n", uri->uri_prefix,
2573 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2574 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2575 scope_txt);
2576 break;
2577 case STAT_STATUS_PART:
2578 chunk_appendf(&trash,
2579 "<p><div class=active_going_down>"
2580 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2581 "Action partially processed.<br>"
2582 "Some server names are probably unknown or ambiguous (duplicated names in the backend)."
2583 "</div>\n", uri->uri_prefix,
2584 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2585 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2586 scope_txt);
2587 break;
2588 case STAT_STATUS_ERRP:
2589 chunk_appendf(&trash,
2590 "<p><div class=active_down>"
2591 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2592 "Action not processed because of invalid parameters."
2593 "<ul>"
2594 "<li>The action is maybe unknown.</li>"
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01002595 "<li>Invalid key parameter (empty or too long).</li>"
William Lallemand74c24fb2016-11-21 17:18:36 +01002596 "<li>The backend name is probably unknown or ambiguous (duplicated names).</li>"
2597 "<li>Some server names are probably unknown or ambiguous (duplicated names in the backend).</li>"
2598 "</ul>"
2599 "</div>\n", uri->uri_prefix,
2600 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2601 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2602 scope_txt);
2603 break;
2604 case STAT_STATUS_EXCD:
2605 chunk_appendf(&trash,
2606 "<p><div class=active_down>"
2607 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2608 "<b>Action not processed : the buffer couldn't store all the data.<br>"
2609 "You should retry with less servers at a time.</b>"
2610 "</div>\n", uri->uri_prefix,
2611 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2612 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2613 scope_txt);
2614 break;
2615 case STAT_STATUS_DENY:
2616 chunk_appendf(&trash,
2617 "<p><div class=active_down>"
2618 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2619 "<b>Action denied.</b>"
2620 "</div>\n", uri->uri_prefix,
2621 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2622 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2623 scope_txt);
2624 break;
Christopher Faulet3c2ecf72019-02-27 16:41:27 +01002625 case STAT_STATUS_IVAL:
2626 chunk_appendf(&trash,
2627 "<p><div class=active_down>"
2628 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2629 "<b>Invalid requests (unsupported method or chunked encoded request).</b>"
2630 "</div>\n", uri->uri_prefix,
2631 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2632 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2633 scope_txt);
2634 break;
William Lallemand74c24fb2016-11-21 17:18:36 +01002635 default:
2636 chunk_appendf(&trash,
2637 "<p><div class=active_no_check>"
2638 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2639 "Unexpected result."
2640 "</div>\n", uri->uri_prefix,
2641 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2642 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2643 scope_txt);
2644 }
2645 chunk_appendf(&trash, "<p>\n");
2646 }
2647}
2648
2649/* Dumps the HTML stats trailer block to the trash. The caller is responsible
2650 * for clearing the trash if needed.
2651 */
2652static void stats_dump_html_end()
2653{
2654 chunk_appendf(&trash, "</body></html>\n");
2655}
2656
Simon Horman05ee2132017-01-04 09:37:25 +01002657/* Dumps the stats JSON header to the trash buffer which. The caller is responsible
2658 * for clearing it if needed.
2659 */
2660static void stats_dump_json_header()
2661{
2662 chunk_strcat(&trash, "[");
2663}
2664
2665
2666/* Dumps the JSON stats trailer block to the trash. The caller is responsible
2667 * for clearing the trash if needed.
2668 */
2669static void stats_dump_json_end()
2670{
2671 chunk_strcat(&trash, "]");
2672}
2673
William Lallemand74c24fb2016-11-21 17:18:36 +01002674/* This function dumps statistics onto the stream interface's read buffer in
2675 * either CSV or HTML format. <uri> contains some HTML-specific parameters that
2676 * are ignored for CSV format (hence <uri> may be NULL there). It returns 0 if
2677 * it had to stop writing data and an I/O is needed, 1 if the dump is finished
2678 * and the stream must be closed, or -1 in case of any error. This function is
2679 * used by both the CLI and the HTTP handlers.
2680 */
Christopher Fauletef779222018-10-31 08:47:01 +01002681static int stats_dump_stat_to_buffer(struct stream_interface *si, struct htx *htx,
2682 struct uri_auth *uri)
William Lallemand74c24fb2016-11-21 17:18:36 +01002683{
2684 struct appctx *appctx = __objt_appctx(si->end);
2685 struct channel *rep = si_ic(si);
2686 struct proxy *px;
2687
2688 chunk_reset(&trash);
2689
2690 switch (appctx->st2) {
2691 case STAT_ST_INIT:
2692 appctx->st2 = STAT_ST_HEAD; /* let's start producing data */
2693 /* fall through */
2694
2695 case STAT_ST_HEAD:
2696 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
2697 stats_dump_html_head(uri);
Christopher Faulet6338a082019-09-09 15:50:54 +02002698 else if (appctx->ctx.stats.flags & STAT_JSON_SCHM)
2699 stats_dump_json_schema(&trash);
Simon Horman05ee2132017-01-04 09:37:25 +01002700 else if (appctx->ctx.stats.flags & STAT_FMT_JSON)
Willy Tarreau9d7fb632017-04-11 07:53:04 +02002701 stats_dump_json_header();
William Lallemand74c24fb2016-11-21 17:18:36 +01002702 else if (!(appctx->ctx.stats.flags & STAT_FMT_TYPED))
2703 stats_dump_csv_header();
2704
Christopher Fauletef779222018-10-31 08:47:01 +01002705 if (!stats_putchk(rep, htx, &trash))
2706 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002707
Christopher Faulet6338a082019-09-09 15:50:54 +02002708 if (appctx->ctx.stats.flags & STAT_JSON_SCHM) {
2709 appctx->st2 = STAT_ST_FIN;
2710 return 1;
2711 }
William Lallemand74c24fb2016-11-21 17:18:36 +01002712 appctx->st2 = STAT_ST_INFO;
2713 /* fall through */
2714
2715 case STAT_ST_INFO:
2716 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
2717 stats_dump_html_info(si, uri);
Christopher Fauletef779222018-10-31 08:47:01 +01002718 if (!stats_putchk(rep, htx, &trash))
2719 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002720 }
2721
Olivier Houchardfbc74e82017-11-24 16:54:05 +01002722 appctx->ctx.stats.px = proxies_list;
William Lallemand74c24fb2016-11-21 17:18:36 +01002723 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
2724 appctx->st2 = STAT_ST_LIST;
2725 /* fall through */
2726
2727 case STAT_ST_LIST:
2728 /* dump proxies */
2729 while (appctx->ctx.stats.px) {
Christopher Fauletef779222018-10-31 08:47:01 +01002730 if (htx) {
2731 if (htx_almost_full(htx))
2732 goto full;
2733 }
2734 else {
2735 if (buffer_almost_full(&rep->buf))
2736 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002737 }
2738
2739 px = appctx->ctx.stats.px;
2740 /* skip the disabled proxies, global frontend and non-networked ones */
2741 if (px->state != PR_STSTOPPED && px->uuid > 0 && (px->cap & (PR_CAP_FE | PR_CAP_BE)))
Christopher Fauletef779222018-10-31 08:47:01 +01002742 if (stats_dump_proxy_to_buffer(si, htx, px, uri) == 0)
William Lallemand74c24fb2016-11-21 17:18:36 +01002743 return 0;
2744
2745 appctx->ctx.stats.px = px->next;
2746 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
2747 }
2748 /* here, we just have reached the last proxy */
2749
2750 appctx->st2 = STAT_ST_END;
2751 /* fall through */
2752
2753 case STAT_ST_END:
Simon Horman05ee2132017-01-04 09:37:25 +01002754 if (appctx->ctx.stats.flags & (STAT_FMT_HTML|STAT_FMT_JSON)) {
2755 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
2756 stats_dump_html_end();
2757 else
2758 stats_dump_json_end();
Christopher Fauletef779222018-10-31 08:47:01 +01002759 if (!stats_putchk(rep, htx, &trash))
2760 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002761 }
2762
2763 appctx->st2 = STAT_ST_FIN;
2764 /* fall through */
2765
2766 case STAT_ST_FIN:
2767 return 1;
2768
2769 default:
2770 /* unknown state ! */
2771 appctx->st2 = STAT_ST_FIN;
2772 return -1;
2773 }
Christopher Fauletef779222018-10-31 08:47:01 +01002774
2775 full:
2776 si_rx_room_blk(si);
2777 return 0;
2778
William Lallemand74c24fb2016-11-21 17:18:36 +01002779}
2780
2781/* We reached the stats page through a POST request. The appctx is
2782 * expected to have already been allocated by the caller.
2783 * Parse the posted data and enable/disable servers if necessary.
2784 * Returns 1 if request was parsed or zero if it needs more data.
2785 */
2786static int stats_process_http_post(struct stream_interface *si)
2787{
2788 struct stream *s = si_strm(si);
2789 struct appctx *appctx = objt_appctx(si->end);
2790
2791 struct proxy *px = NULL;
2792 struct server *sv = NULL;
2793
2794 char key[LINESIZE];
2795 int action = ST_ADM_ACTION_NONE;
2796 int reprocess = 0;
2797
2798 int total_servers = 0;
2799 int altered_servers = 0;
2800
2801 char *first_param, *cur_param, *next_param, *end_params;
2802 char *st_cur_param = NULL;
2803 char *st_next_param = NULL;
2804
Christopher Fauleta3618372018-12-13 21:59:56 +01002805 struct buffer *temp = get_trash_chunk();
William Lallemand74c24fb2016-11-21 17:18:36 +01002806
Christopher Fauletb7f88902019-07-15 21:56:43 +02002807 struct htx *htx = htxbuf(&s->req.buf);
2808 struct htx_blk *blk;
Christopher Fauleta3618372018-12-13 21:59:56 +01002809
Christopher Fauletb7f88902019-07-15 21:56:43 +02002810 /* we need more data */
2811 if (s->txn->req.msg_state < HTTP_MSG_DONE) {
2812 /* check if we can receive more */
2813 if (htx_free_data_space(htx) <= global.tune.maxrewrite) {
2814 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
2815 goto out;
Christopher Fauleta3618372018-12-13 21:59:56 +01002816 }
Christopher Fauletb7f88902019-07-15 21:56:43 +02002817 goto wait;
2818 }
Christopher Fauleta3618372018-12-13 21:59:56 +01002819
Christopher Fauletb7f88902019-07-15 21:56:43 +02002820 /* The request was fully received. Copy data */
2821 blk = htx_get_head_blk(htx);
2822 while (blk) {
2823 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Fauleta3618372018-12-13 21:59:56 +01002824
Christopher Fauletb7f88902019-07-15 21:56:43 +02002825 if (type == HTX_BLK_EOM || type == HTX_BLK_TLR || type == HTX_BLK_EOT)
2826 break;
2827 if (type == HTX_BLK_DATA) {
2828 struct ist v = htx_get_blk_value(htx, blk);
Christopher Fauleta3618372018-12-13 21:59:56 +01002829
Christopher Fauletb7f88902019-07-15 21:56:43 +02002830 if (!chunk_memcat(temp, v.ptr, v.len)) {
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01002831 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
2832 goto out;
2833 }
Christopher Fauleta3618372018-12-13 21:59:56 +01002834 }
Christopher Fauletb7f88902019-07-15 21:56:43 +02002835 blk = htx_get_next_blk(htx, blk);
William Lallemand74c24fb2016-11-21 17:18:36 +01002836 }
2837
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002838 first_param = temp->area;
Christopher Fauleta3618372018-12-13 21:59:56 +01002839 end_params = temp->area + temp->data;
William Lallemand74c24fb2016-11-21 17:18:36 +01002840 cur_param = next_param = end_params;
2841 *end_params = '\0';
2842
2843 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
2844
2845 /*
2846 * Parse the parameters in reverse order to only store the last value.
2847 * From the html form, the backend and the action are at the end.
2848 */
2849 while (cur_param > first_param) {
2850 char *value;
2851 int poffset, plen;
2852
2853 cur_param--;
2854
2855 if ((*cur_param == '&') || (cur_param == first_param)) {
2856 reprocess_servers:
2857 /* Parse the key */
2858 poffset = (cur_param != first_param ? 1 : 0);
2859 plen = next_param - cur_param + (cur_param == first_param ? 1 : 0);
2860 if ((plen > 0) && (plen <= sizeof(key))) {
2861 strncpy(key, cur_param + poffset, plen);
2862 key[plen - 1] = '\0';
2863 } else {
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01002864 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
William Lallemand74c24fb2016-11-21 17:18:36 +01002865 goto out;
2866 }
2867
2868 /* Parse the value */
2869 value = key;
2870 while (*value != '\0' && *value != '=') {
2871 value++;
2872 }
2873 if (*value == '=') {
2874 /* Ok, a value is found, we can mark the end of the key */
2875 *value++ = '\0';
2876 }
2877 if (url_decode(key) < 0 || url_decode(value) < 0)
2878 break;
2879
2880 /* Now we can check the key to see what to do */
2881 if (!px && (strcmp(key, "b") == 0)) {
2882 if ((px = proxy_be_by_name(value)) == NULL) {
2883 /* the backend name is unknown or ambiguous (duplicate names) */
2884 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
2885 goto out;
2886 }
2887 }
2888 else if (!action && (strcmp(key, "action") == 0)) {
2889 if (strcmp(value, "ready") == 0) {
2890 action = ST_ADM_ACTION_READY;
2891 }
2892 else if (strcmp(value, "drain") == 0) {
2893 action = ST_ADM_ACTION_DRAIN;
2894 }
2895 else if (strcmp(value, "maint") == 0) {
2896 action = ST_ADM_ACTION_MAINT;
2897 }
2898 else if (strcmp(value, "shutdown") == 0) {
2899 action = ST_ADM_ACTION_SHUTDOWN;
2900 }
2901 else if (strcmp(value, "dhlth") == 0) {
2902 action = ST_ADM_ACTION_DHLTH;
2903 }
2904 else if (strcmp(value, "ehlth") == 0) {
2905 action = ST_ADM_ACTION_EHLTH;
2906 }
2907 else if (strcmp(value, "hrunn") == 0) {
2908 action = ST_ADM_ACTION_HRUNN;
2909 }
2910 else if (strcmp(value, "hnolb") == 0) {
2911 action = ST_ADM_ACTION_HNOLB;
2912 }
2913 else if (strcmp(value, "hdown") == 0) {
2914 action = ST_ADM_ACTION_HDOWN;
2915 }
2916 else if (strcmp(value, "dagent") == 0) {
2917 action = ST_ADM_ACTION_DAGENT;
2918 }
2919 else if (strcmp(value, "eagent") == 0) {
2920 action = ST_ADM_ACTION_EAGENT;
2921 }
2922 else if (strcmp(value, "arunn") == 0) {
2923 action = ST_ADM_ACTION_ARUNN;
2924 }
2925 else if (strcmp(value, "adown") == 0) {
2926 action = ST_ADM_ACTION_ADOWN;
2927 }
2928 /* else these are the old supported methods */
2929 else if (strcmp(value, "disable") == 0) {
2930 action = ST_ADM_ACTION_DISABLE;
2931 }
2932 else if (strcmp(value, "enable") == 0) {
2933 action = ST_ADM_ACTION_ENABLE;
2934 }
2935 else if (strcmp(value, "stop") == 0) {
2936 action = ST_ADM_ACTION_STOP;
2937 }
2938 else if (strcmp(value, "start") == 0) {
2939 action = ST_ADM_ACTION_START;
2940 }
2941 else {
2942 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
2943 goto out;
2944 }
2945 }
2946 else if (strcmp(key, "s") == 0) {
2947 if (!(px && action)) {
2948 /*
2949 * Indicates that we'll need to reprocess the parameters
2950 * as soon as backend and action are known
2951 */
2952 if (!reprocess) {
2953 st_cur_param = cur_param;
2954 st_next_param = next_param;
2955 }
2956 reprocess = 1;
2957 }
2958 else if ((sv = findserver(px, value)) != NULL) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002959 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
William Lallemand74c24fb2016-11-21 17:18:36 +01002960 switch (action) {
2961 case ST_ADM_ACTION_DISABLE:
Emeric Brun52a91d32017-08-31 14:41:55 +02002962 if (!(sv->cur_admin & SRV_ADMF_FMAINT)) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002963 altered_servers++;
2964 total_servers++;
2965 srv_set_admin_flag(sv, SRV_ADMF_FMAINT, "'disable' on stats page");
2966 }
2967 break;
2968 case ST_ADM_ACTION_ENABLE:
Emeric Brun52a91d32017-08-31 14:41:55 +02002969 if (sv->cur_admin & SRV_ADMF_FMAINT) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002970 altered_servers++;
2971 total_servers++;
2972 srv_clr_admin_flag(sv, SRV_ADMF_FMAINT);
2973 }
2974 break;
2975 case ST_ADM_ACTION_STOP:
Emeric Brun52a91d32017-08-31 14:41:55 +02002976 if (!(sv->cur_admin & SRV_ADMF_FDRAIN)) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002977 srv_set_admin_flag(sv, SRV_ADMF_FDRAIN, "'stop' on stats page");
2978 altered_servers++;
2979 total_servers++;
2980 }
2981 break;
2982 case ST_ADM_ACTION_START:
Emeric Brun52a91d32017-08-31 14:41:55 +02002983 if (sv->cur_admin & SRV_ADMF_FDRAIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002984 srv_clr_admin_flag(sv, SRV_ADMF_FDRAIN);
2985 altered_servers++;
2986 total_servers++;
2987 }
2988 break;
2989 case ST_ADM_ACTION_DHLTH:
2990 if (sv->check.state & CHK_ST_CONFIGURED) {
2991 sv->check.state &= ~CHK_ST_ENABLED;
2992 altered_servers++;
2993 total_servers++;
2994 }
2995 break;
2996 case ST_ADM_ACTION_EHLTH:
2997 if (sv->check.state & CHK_ST_CONFIGURED) {
2998 sv->check.state |= CHK_ST_ENABLED;
2999 altered_servers++;
3000 total_servers++;
3001 }
3002 break;
3003 case ST_ADM_ACTION_HRUNN:
3004 if (!(sv->track)) {
3005 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02003006 srv_set_running(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003007 altered_servers++;
3008 total_servers++;
3009 }
3010 break;
3011 case ST_ADM_ACTION_HNOLB:
3012 if (!(sv->track)) {
3013 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02003014 srv_set_stopping(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003015 altered_servers++;
3016 total_servers++;
3017 }
3018 break;
3019 case ST_ADM_ACTION_HDOWN:
3020 if (!(sv->track)) {
3021 sv->check.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02003022 srv_set_stopped(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003023 altered_servers++;
3024 total_servers++;
3025 }
3026 break;
3027 case ST_ADM_ACTION_DAGENT:
3028 if (sv->agent.state & CHK_ST_CONFIGURED) {
3029 sv->agent.state &= ~CHK_ST_ENABLED;
3030 altered_servers++;
3031 total_servers++;
3032 }
3033 break;
3034 case ST_ADM_ACTION_EAGENT:
3035 if (sv->agent.state & CHK_ST_CONFIGURED) {
3036 sv->agent.state |= CHK_ST_ENABLED;
3037 altered_servers++;
3038 total_servers++;
3039 }
3040 break;
3041 case ST_ADM_ACTION_ARUNN:
3042 if (sv->agent.state & CHK_ST_ENABLED) {
3043 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02003044 srv_set_running(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003045 altered_servers++;
3046 total_servers++;
3047 }
3048 break;
3049 case ST_ADM_ACTION_ADOWN:
3050 if (sv->agent.state & CHK_ST_ENABLED) {
3051 sv->agent.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02003052 srv_set_stopped(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003053 altered_servers++;
3054 total_servers++;
3055 }
3056 break;
3057 case ST_ADM_ACTION_READY:
3058 srv_adm_set_ready(sv);
3059 altered_servers++;
3060 total_servers++;
3061 break;
3062 case ST_ADM_ACTION_DRAIN:
3063 srv_adm_set_drain(sv);
3064 altered_servers++;
3065 total_servers++;
3066 break;
3067 case ST_ADM_ACTION_MAINT:
3068 srv_adm_set_maint(sv);
3069 altered_servers++;
3070 total_servers++;
3071 break;
3072 case ST_ADM_ACTION_SHUTDOWN:
3073 if (px->state != PR_STSTOPPED) {
3074 struct stream *sess, *sess_bck;
3075
3076 list_for_each_entry_safe(sess, sess_bck, &sv->actconns, by_srv)
3077 if (sess->srv_conn == sv)
3078 stream_shutdown(sess, SF_ERR_KILLED);
3079
3080 altered_servers++;
3081 total_servers++;
3082 }
3083 break;
3084 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003085 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Lallemand74c24fb2016-11-21 17:18:36 +01003086 } else {
3087 /* the server name is unknown or ambiguous (duplicate names) */
3088 total_servers++;
3089 }
3090 }
3091 if (reprocess && px && action) {
3092 /* Now, we know the backend and the action chosen by the user.
3093 * We can safely restart from the first server parameter
3094 * to reprocess them
3095 */
3096 cur_param = st_cur_param;
3097 next_param = st_next_param;
3098 reprocess = 0;
3099 goto reprocess_servers;
3100 }
3101
3102 next_param = cur_param;
3103 }
3104 }
3105
3106 if (total_servers == 0) {
3107 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
3108 }
3109 else if (altered_servers == 0) {
3110 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
3111 }
3112 else if (altered_servers == total_servers) {
3113 appctx->ctx.stats.st_code = STAT_STATUS_DONE;
3114 }
3115 else {
3116 appctx->ctx.stats.st_code = STAT_STATUS_PART;
3117 }
3118 out:
3119 return 1;
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01003120 wait:
3121 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
3122 return 0;
Christopher Fauletef779222018-10-31 08:47:01 +01003123}
3124
3125
Christopher Fauletb7f88902019-07-15 21:56:43 +02003126static int stats_send_http_headers(struct stream_interface *si, struct htx *htx)
Christopher Fauletef779222018-10-31 08:47:01 +01003127{
3128 struct stream *s = si_strm(si);
3129 struct uri_auth *uri = s->be->uri_auth;
3130 struct appctx *appctx = __objt_appctx(si->end);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003131 struct htx_sl *sl;
3132 unsigned int flags;
Christopher Fauletef779222018-10-31 08:47:01 +01003133
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003134 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);
3135 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist("200"), ist("OK"));
3136 if (!sl)
Christopher Fauletef779222018-10-31 08:47:01 +01003137 goto full;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003138 sl->info.res.status = 200;
Christopher Fauletef779222018-10-31 08:47:01 +01003139
Christopher Fauletb829f4c2019-03-29 16:13:55 +01003140 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")))
Christopher Fauletef779222018-10-31 08:47:01 +01003141 goto full;
3142 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
3143 if (!htx_add_header(htx, ist("Content-Type"), ist("text/html")))
3144 goto full;
3145 }
Christopher Faulet6338a082019-09-09 15:50:54 +02003146 else if (appctx->ctx.stats.flags & (STAT_FMT_JSON|STAT_JSON_SCHM)) {
3147 if (!htx_add_header(htx, ist("Content-Type"), ist("application/json")))
3148 goto full;
3149 }
Christopher Fauletef779222018-10-31 08:47:01 +01003150 else {
3151 if (!htx_add_header(htx, ist("Content-Type"), ist("text/plain")))
3152 goto full;
3153 }
3154
3155 if (uri->refresh > 0 && !(appctx->ctx.stats.flags & STAT_NO_REFRESH)) {
3156 const char *refresh = U2A(uri->refresh);
3157 if (!htx_add_header(htx, ist("Refresh"), ist2(refresh, strlen(refresh))))
3158 goto full;
3159 }
3160
3161 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
3162 if (!htx_add_header(htx, ist("Transfer-Encoding"), ist("chunked")))
3163 goto full;
3164 }
3165
3166 if (!htx_add_endof(htx, HTX_BLK_EOH))
3167 goto full;
3168
Christopher Faulet1e2d6362019-02-27 16:28:48 +01003169 channel_add_input(&s->res, htx->data);
Christopher Fauletef779222018-10-31 08:47:01 +01003170 return 1;
3171
3172 full:
3173 htx_reset(htx);
3174 si_rx_room_blk(si);
3175 return 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01003176}
3177
Christopher Fauletef779222018-10-31 08:47:01 +01003178
Christopher Fauletb7f88902019-07-15 21:56:43 +02003179static int stats_send_http_redirect(struct stream_interface *si, struct htx *htx)
Christopher Fauletef779222018-10-31 08:47:01 +01003180{
3181 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
3182 struct stream *s = si_strm(si);
3183 struct uri_auth *uri = s->be->uri_auth;
3184 struct appctx *appctx = __objt_appctx(si->end);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003185 struct htx_sl *sl;
3186 unsigned int flags;
Christopher Fauletef779222018-10-31 08:47:01 +01003187
3188 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
3189 scope_txt[0] = 0;
3190 if (appctx->ctx.stats.scope_len) {
Christopher Fauleted7a0662019-01-14 11:07:34 +01003191 const char *scope_ptr = stats_scope_ptr(appctx, si);
3192
Christopher Fauletef779222018-10-31 08:47:01 +01003193 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Christopher Fauleted7a0662019-01-14 11:07:34 +01003194 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), scope_ptr, appctx->ctx.stats.scope_len);
Christopher Fauletef779222018-10-31 08:47:01 +01003195 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
3196 }
3197
3198 /* We don't want to land on the posted stats page because a refresh will
3199 * repost the data. We don't want this to happen on accident so we redirect
3200 * the browse to the stats page with a GET.
3201 */
3202 chunk_printf(&trash, "%s;st=%s%s%s%s",
3203 uri->uri_prefix,
3204 ((appctx->ctx.stats.st_code > STAT_STATUS_INIT) &&
3205 (appctx->ctx.stats.st_code < STAT_STATUS_SIZE) &&
3206 stat_status_codes[appctx->ctx.stats.st_code]) ?
3207 stat_status_codes[appctx->ctx.stats.st_code] :
3208 stat_status_codes[STAT_STATUS_UNKN],
3209 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3210 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3211 scope_txt);
3212
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003213 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_CHNK);
3214 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist("303"), ist("See Other"));
3215 if (!sl)
Christopher Fauletef779222018-10-31 08:47:01 +01003216 goto full;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003217 sl->info.res.status = 303;
Christopher Fauletef779222018-10-31 08:47:01 +01003218
3219 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")) ||
Christopher Fauletef779222018-10-31 08:47:01 +01003220 !htx_add_header(htx, ist("Content-Type"), ist("text/plain")) ||
3221 !htx_add_header(htx, ist("Content-Length"), ist("0")) ||
3222 !htx_add_header(htx, ist("Location"), ist2(trash.area, trash.data)))
3223 goto full;
3224
3225 if (!htx_add_endof(htx, HTX_BLK_EOH))
3226 goto full;
3227
Christopher Faulet1e2d6362019-02-27 16:28:48 +01003228 channel_add_input(&s->res, htx->data);
Christopher Fauletef779222018-10-31 08:47:01 +01003229 return 1;
3230
3231full:
3232 htx_reset(htx);
3233 si_rx_room_blk(si);
3234 return 0;
3235}
William Lallemand74c24fb2016-11-21 17:18:36 +01003236
Simon Horman05ee2132017-01-04 09:37:25 +01003237
William Lallemand74c24fb2016-11-21 17:18:36 +01003238/* This I/O handler runs as an applet embedded in a stream interface. It is
3239 * used to send HTTP stats over a TCP socket. The mechanism is very simple.
3240 * appctx->st0 contains the operation in progress (dump, done). The handler
3241 * automatically unregisters itself once transfer is complete.
3242 */
Christopher Fauletb7f88902019-07-15 21:56:43 +02003243static void http_stats_io_handler(struct appctx *appctx)
Christopher Fauletef779222018-10-31 08:47:01 +01003244{
3245 struct stream_interface *si = appctx->owner;
3246 struct stream *s = si_strm(si);
3247 struct channel *req = si_oc(si);
3248 struct channel *res = si_ic(si);
3249 struct htx *req_htx, *res_htx;
3250
3251 res_htx = htx_from_buf(&res->buf);
3252
3253 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
3254 goto out;
3255
3256 /* Check if the input buffer is avalaible. */
3257 if (!b_size(&res->buf)) {
3258 si_rx_room_blk(si);
3259 goto out;
3260 }
3261
3262 /* check that the output is not closed */
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003263 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_SHUTR))
3264 appctx->st0 = STAT_HTTP_END;
Christopher Fauletef779222018-10-31 08:47:01 +01003265
3266 /* all states are processed in sequence */
3267 if (appctx->st0 == STAT_HTTP_HEAD) {
Christopher Fauletb7f88902019-07-15 21:56:43 +02003268 if (stats_send_http_headers(si, res_htx)) {
Christopher Fauletef779222018-10-31 08:47:01 +01003269 if (s->txn->meth == HTTP_METH_HEAD)
3270 appctx->st0 = STAT_HTTP_DONE;
3271 else
3272 appctx->st0 = STAT_HTTP_DUMP;
3273 }
3274 }
3275
3276 if (appctx->st0 == STAT_HTTP_DUMP) {
3277 if (stats_dump_stat_to_buffer(si, res_htx, s->be->uri_auth))
3278 appctx->st0 = STAT_HTTP_DONE;
3279 }
3280
3281 if (appctx->st0 == STAT_HTTP_POST) {
3282 if (stats_process_http_post(si))
3283 appctx->st0 = STAT_HTTP_LAST;
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003284 else if (req->flags & CF_SHUTR)
Christopher Fauletef779222018-10-31 08:47:01 +01003285 appctx->st0 = STAT_HTTP_DONE;
3286 }
3287
3288 if (appctx->st0 == STAT_HTTP_LAST) {
Christopher Fauletb7f88902019-07-15 21:56:43 +02003289 if (stats_send_http_redirect(si, res_htx))
Christopher Fauletef779222018-10-31 08:47:01 +01003290 appctx->st0 = STAT_HTTP_DONE;
3291 }
3292
3293 if (appctx->st0 == STAT_HTTP_DONE) {
Christopher Faulet54b5e212019-06-04 10:08:28 +02003294 /* Don't add TLR because mux-h1 will take care of it */
Christopher Fauletef779222018-10-31 08:47:01 +01003295 if (!htx_add_endof(res_htx, HTX_BLK_EOM)) {
3296 si_rx_room_blk(si);
3297 goto out;
3298 }
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003299 channel_add_input(&s->res, 1);
3300 appctx->st0 = STAT_HTTP_END;
Christopher Fauletef779222018-10-31 08:47:01 +01003301 }
3302
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003303 if (appctx->st0 == STAT_HTTP_END) {
3304 if (!(res->flags & CF_SHUTR)) {
Christopher Fauletef779222018-10-31 08:47:01 +01003305 res->flags |= CF_READ_NULL;
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003306 si_shutr(si);
3307 }
3308
3309 /* eat the whole request */
3310 if (co_data(req)) {
3311 req_htx = htx_from_buf(&req->buf);
3312 co_htx_skip(req, req_htx, co_data(req));
3313 htx_to_buf(req_htx, &req->buf);
Christopher Fauletef779222018-10-31 08:47:01 +01003314 }
3315 }
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003316
Christopher Fauletef779222018-10-31 08:47:01 +01003317 out:
3318 /* we have left the request in the buffer for the case where we
3319 * process a POST, and this automatically re-enables activity on
3320 * read. It's better to indicate that we want to stop reading when
3321 * we're sending, so that we know there's at most one direction
3322 * deciding to wake the applet up. It saves it from looping when
3323 * emitting large blocks into small TCP windows.
3324 */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003325 htx_to_buf(res_htx, &res->buf);
3326 if (!channel_is_empty(res))
Christopher Fauletef779222018-10-31 08:47:01 +01003327 si_stop_get(si);
Christopher Fauletef779222018-10-31 08:47:01 +01003328}
3329
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003330/* Dump all fields from <info> into <out> using the "show info" format (name: value) */
Willy Tarreau83061a82018-07-13 11:56:34 +02003331static int stats_dump_info_fields(struct buffer *out,
3332 const struct field *info)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003333{
3334 int field;
3335
3336 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
3337 if (!field_format(info, field))
3338 continue;
3339
3340 if (!chunk_appendf(out, "%s: ", info_field_names[field]))
3341 return 0;
3342 if (!stats_emit_raw_data_field(out, &info[field]))
3343 return 0;
3344 if (!chunk_strcat(out, "\n"))
3345 return 0;
3346 }
3347 return 1;
3348}
3349
3350/* Dump all fields from <info> into <out> using the "show info typed" format */
Willy Tarreau83061a82018-07-13 11:56:34 +02003351static int stats_dump_typed_info_fields(struct buffer *out,
3352 const struct field *info)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003353{
3354 int field;
3355
3356 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
3357 if (!field_format(info, field))
3358 continue;
3359
3360 if (!chunk_appendf(out, "%d.%s.%u:", field, info_field_names[field], info[INF_PROCESS_NUM].u.u32))
3361 return 0;
3362 if (!stats_emit_field_tags(out, &info[field], ':'))
3363 return 0;
3364 if (!stats_emit_typed_data_field(out, &info[field]))
3365 return 0;
3366 if (!chunk_strcat(out, "\n"))
3367 return 0;
3368 }
3369 return 1;
3370}
3371
3372/* Fill <info> with HAProxy global info. <info> is preallocated
3373 * array of length <len>. The length of the aray must be
3374 * INF_TOTAL_FIELDS. If this length is less then this value, the
3375 * function returns 0, otherwise, it returns 1.
3376 */
3377int stats_fill_info(struct field *info, int len)
3378{
3379 unsigned int up = (now.tv_sec - start_date.tv_sec);
Willy Tarreau83061a82018-07-13 11:56:34 +02003380 struct buffer *out = get_trash_chunk();
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003381
3382#ifdef USE_OPENSSL
3383 int ssl_sess_rate = read_freq_ctr(&global.ssl_per_sec);
3384 int ssl_key_rate = read_freq_ctr(&global.ssl_fe_keys_per_sec);
3385 int ssl_reuse = 0;
3386
3387 if (ssl_key_rate < ssl_sess_rate) {
3388 /* count the ssl reuse ratio and avoid overflows in both directions */
3389 ssl_reuse = 100 - (100 * ssl_key_rate + (ssl_sess_rate - 1) / 2) / ssl_sess_rate;
3390 }
3391#endif
3392
3393 if (len < INF_TOTAL_FIELDS)
3394 return 0;
3395
3396 chunk_reset(out);
3397 memset(info, 0, sizeof(*info) * len);
3398
3399 info[INF_NAME] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, PRODUCT_NAME);
Willy Tarreau909b9d82019-01-04 18:20:32 +01003400 info[INF_VERSION] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, haproxy_version);
3401 info[INF_RELEASE_DATE] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, haproxy_date);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003402
Yves Lafon95317282018-02-26 11:10:37 +01003403 info[INF_NBTHREAD] = mkf_u32(FO_CONFIG|FS_SERVICE, global.nbthread);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003404 info[INF_NBPROC] = mkf_u32(FO_CONFIG|FS_SERVICE, global.nbproc);
3405 info[INF_PROCESS_NUM] = mkf_u32(FO_KEY, relative_pid);
3406 info[INF_PID] = mkf_u32(FO_STATUS, pid);
3407
3408 info[INF_UPTIME] = mkf_str(FN_DURATION, chunk_newstr(out));
3409 chunk_appendf(out, "%ud %uh%02um%02us", up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60));
3410
3411 info[INF_UPTIME_SEC] = mkf_u32(FN_DURATION, up);
3412 info[INF_MEMMAX_MB] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_memmax);
3413 info[INF_POOL_ALLOC_MB] = mkf_u32(0, (unsigned)(pool_total_allocated() / 1048576L));
3414 info[INF_POOL_USED_MB] = mkf_u32(0, (unsigned)(pool_total_used() / 1048576L));
3415 info[INF_POOL_FAILED] = mkf_u32(FN_COUNTER, pool_total_failures());
3416 info[INF_ULIMIT_N] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_nofile);
3417 info[INF_MAXSOCK] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxsock);
3418 info[INF_MAXCONN] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxconn);
3419 info[INF_HARD_MAXCONN] = mkf_u32(FO_CONFIG|FN_LIMIT, global.hardmaxconn);
3420 info[INF_CURR_CONN] = mkf_u32(0, actconn);
3421 info[INF_CUM_CONN] = mkf_u32(FN_COUNTER, totalconn);
3422 info[INF_CUM_REQ] = mkf_u32(FN_COUNTER, global.req_count);
3423#ifdef USE_OPENSSL
3424 info[INF_MAX_SSL_CONNS] = mkf_u32(FN_MAX, global.maxsslconn);
3425 info[INF_CURR_SSL_CONNS] = mkf_u32(0, sslconns);
3426 info[INF_CUM_SSL_CONNS] = mkf_u32(FN_COUNTER, totalsslconns);
3427#endif
3428 info[INF_MAXPIPES] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxpipes);
3429 info[INF_PIPES_USED] = mkf_u32(0, pipes_used);
3430 info[INF_PIPES_FREE] = mkf_u32(0, pipes_free);
3431 info[INF_CONN_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.conn_per_sec));
3432 info[INF_CONN_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.cps_lim);
3433 info[INF_MAX_CONN_RATE] = mkf_u32(FN_MAX, global.cps_max);
3434 info[INF_SESS_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.sess_per_sec));
3435 info[INF_SESS_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.sps_lim);
3436 info[INF_MAX_SESS_RATE] = mkf_u32(FN_RATE, global.sps_max);
3437
3438#ifdef USE_OPENSSL
3439 info[INF_SSL_RATE] = mkf_u32(FN_RATE, ssl_sess_rate);
3440 info[INF_SSL_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.ssl_lim);
3441 info[INF_MAX_SSL_RATE] = mkf_u32(FN_MAX, global.ssl_max);
3442 info[INF_SSL_FRONTEND_KEY_RATE] = mkf_u32(0, ssl_key_rate);
3443 info[INF_SSL_FRONTEND_MAX_KEY_RATE] = mkf_u32(FN_MAX, global.ssl_fe_keys_max);
3444 info[INF_SSL_FRONTEND_SESSION_REUSE_PCT] = mkf_u32(0, ssl_reuse);
3445 info[INF_SSL_BACKEND_KEY_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.ssl_be_keys_per_sec));
3446 info[INF_SSL_BACKEND_MAX_KEY_RATE] = mkf_u32(FN_MAX, global.ssl_be_keys_max);
3447 info[INF_SSL_CACHE_LOOKUPS] = mkf_u32(FN_COUNTER, global.shctx_lookups);
3448 info[INF_SSL_CACHE_MISSES] = mkf_u32(FN_COUNTER, global.shctx_misses);
3449#endif
3450 info[INF_COMPRESS_BPS_IN] = mkf_u32(FN_RATE, read_freq_ctr(&global.comp_bps_in));
3451 info[INF_COMPRESS_BPS_OUT] = mkf_u32(FN_RATE, read_freq_ctr(&global.comp_bps_out));
3452 info[INF_COMPRESS_BPS_RATE_LIM] = mkf_u32(FO_CONFIG|FN_LIMIT, global.comp_rate_lim);
3453#ifdef USE_ZLIB
3454 info[INF_ZLIB_MEM_USAGE] = mkf_u32(0, zlib_used_memory);
3455 info[INF_MAX_ZLIB_MEM_USAGE] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxzlibmem);
3456#endif
3457 info[INF_TASKS] = mkf_u32(0, nb_tasks_cur);
Christopher Faulet34c5cc92016-12-06 09:15:30 +01003458 info[INF_RUN_QUEUE] = mkf_u32(0, tasks_run_queue_cur);
Willy Tarreau81036f22019-05-20 19:24:50 +02003459 info[INF_IDLE_PCT] = mkf_u32(FN_AVG, ti->idle_pct);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003460 info[INF_NODE] = mkf_str(FO_CONFIG|FN_OUTPUT|FS_SERVICE, global.node);
3461 if (global.desc)
3462 info[INF_DESCRIPTION] = mkf_str(FO_CONFIG|FN_OUTPUT|FS_SERVICE, global.desc);
Willy Tarreau00098ea2018-11-05 14:38:13 +01003463 info[INF_STOPPING] = mkf_u32(0, stopping);
3464 info[INF_JOBS] = mkf_u32(0, jobs);
William Lallemanda7199262018-11-16 16:57:20 +01003465 info[INF_UNSTOPPABLE_JOBS] = mkf_u32(0, unstoppable_jobs);
Willy Tarreau00098ea2018-11-05 14:38:13 +01003466 info[INF_LISTENERS] = mkf_u32(0, listeners);
Willy Tarreau199ad242018-11-05 16:31:22 +01003467 info[INF_ACTIVE_PEERS] = mkf_u32(0, active_peers);
Willy Tarreau2d372c22018-11-05 17:12:27 +01003468 info[INF_CONNECTED_PEERS] = mkf_u32(0, connected_peers);
Willy Tarreau13ef7732018-11-12 07:25:28 +01003469 info[INF_DROPPED_LOGS] = mkf_u32(0, dropped_logs);
Willy Tarreaubeb859a2018-11-22 18:07:59 +01003470 info[INF_BUSY_POLLING] = mkf_u32(0, !!(global.tune.options & GTUNE_BUSY_POLLING));
Baptiste Assmann333939c2019-01-21 08:34:50 +01003471 info[INF_FAILED_RESOLUTIONS] = mkf_u32(0, dns_failed_resolutions);
Willy Tarreau7cf0e452019-05-23 11:39:14 +02003472 info[INF_TOTAL_BYTES_OUT] = mkf_u64(0, global.out_bytes);
3473 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 +01003474
3475 return 1;
3476}
3477
3478/* This function dumps information onto the stream interface's read buffer.
3479 * It returns 0 as long as it does not complete, non-zero upon completion.
3480 * No state is used.
3481 */
3482static int stats_dump_info_to_buffer(struct stream_interface *si)
3483{
3484 struct appctx *appctx = __objt_appctx(si->end);
3485
3486 if (!stats_fill_info(info, INF_TOTAL_FIELDS))
3487 return 0;
3488
3489 chunk_reset(&trash);
3490
3491 if (appctx->ctx.stats.flags & STAT_FMT_TYPED)
3492 stats_dump_typed_info_fields(&trash, info);
Simon Horman05ee2132017-01-04 09:37:25 +01003493 else if (appctx->ctx.stats.flags & STAT_FMT_JSON)
3494 stats_dump_json_info_fields(&trash, info);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003495 else
3496 stats_dump_info_fields(&trash, info);
3497
Willy Tarreau06d80a92017-10-19 14:32:15 +02003498 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01003499 si_rx_room_blk(si);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003500 return 0;
3501 }
3502
3503 return 1;
3504}
3505
Simon Horman6f6bb382017-01-04 09:37:26 +01003506/* This function dumps the schema onto the stream interface's read buffer.
3507 * It returns 0 as long as it does not complete, non-zero upon completion.
3508 * No state is used.
3509 *
3510 * Integer values bouned to the range [-(2**53)+1, (2**53)-1] as
3511 * per the recommendation for interoperable integers in section 6 of RFC 7159.
3512 */
Willy Tarreau83061a82018-07-13 11:56:34 +02003513static void stats_dump_json_schema(struct buffer *out)
Simon Horman6f6bb382017-01-04 09:37:26 +01003514{
3515
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003516 int old_len = out->data;
Simon Horman6f6bb382017-01-04 09:37:26 +01003517
3518 chunk_strcat(out,
3519 "{"
3520 "\"$schema\":\"http://json-schema.org/draft-04/schema#\","
3521 "\"oneOf\":["
3522 "{"
3523 "\"title\":\"Info\","
3524 "\"type\":\"array\","
3525 "\"items\":{"
3526 "\"properties\":{"
3527 "\"title\":\"InfoItem\","
3528 "\"type\":\"object\","
3529 "\"field\":{\"$ref\":\"#/definitions/field\"},"
3530 "\"processNum\":{\"$ref\":\"#/definitions/processNum\"},"
3531 "\"tags\":{\"$ref\":\"#/definitions/tags\"},"
3532 "\"value\":{\"$ref\":\"#/definitions/typedValue\"}"
3533 "},"
3534 "\"required\":[\"field\",\"processNum\",\"tags\","
3535 "\"value\"]"
3536 "}"
3537 "},"
3538 "{"
3539 "\"title\":\"Stat\","
3540 "\"type\":\"array\","
3541 "\"items\":{"
3542 "\"title\":\"InfoItem\","
3543 "\"type\":\"object\","
3544 "\"properties\":{"
3545 "\"objType\":{"
3546 "\"enum\":[\"Frontend\",\"Backend\",\"Listener\","
3547 "\"Server\",\"Unknown\"]"
3548 "},"
3549 "\"proxyId\":{"
3550 "\"type\":\"integer\","
3551 "\"minimum\":0"
3552 "},"
3553 "\"id\":{"
3554 "\"type\":\"integer\","
3555 "\"minimum\":0"
3556 "},"
3557 "\"field\":{\"$ref\":\"#/definitions/field\"},"
3558 "\"processNum\":{\"$ref\":\"#/definitions/processNum\"},"
3559 "\"tags\":{\"$ref\":\"#/definitions/tags\"},"
3560 "\"typedValue\":{\"$ref\":\"#/definitions/typedValue\"}"
3561 "},"
3562 "\"required\":[\"objType\",\"proxyId\",\"id\","
3563 "\"field\",\"processNum\",\"tags\","
3564 "\"value\"]"
3565 "}"
3566 "},"
3567 "{"
3568 "\"title\":\"Error\","
3569 "\"type\":\"object\","
3570 "\"properties\":{"
3571 "\"errorStr\":{"
3572 "\"type\":\"string\""
3573 "},"
3574 "\"required\":[\"errorStr\"]"
3575 "}"
3576 "}"
3577 "],"
3578 "\"definitions\":{"
3579 "\"field\":{"
3580 "\"type\":\"object\","
3581 "\"pos\":{"
3582 "\"type\":\"integer\","
3583 "\"minimum\":0"
3584 "},"
3585 "\"name\":{"
3586 "\"type\":\"string\""
3587 "},"
3588 "\"required\":[\"pos\",\"name\"]"
3589 "},"
3590 "\"processNum\":{"
3591 "\"type\":\"integer\","
3592 "\"minimum\":1"
3593 "},"
3594 "\"tags\":{"
3595 "\"type\":\"object\","
3596 "\"origin\":{"
3597 "\"type\":\"string\","
3598 "\"enum\":[\"Metric\",\"Status\",\"Key\","
3599 "\"Config\",\"Product\",\"Unknown\"]"
3600 "},"
3601 "\"nature\":{"
3602 "\"type\":\"string\","
3603 "\"enum\":[\"Gauge\",\"Limit\",\"Min\",\"Max\","
3604 "\"Rate\",\"Counter\",\"Duration\","
3605 "\"Age\",\"Time\",\"Name\",\"Output\","
3606 "\"Avg\", \"Unknown\"]"
3607 "},"
3608 "\"scope\":{"
3609 "\"type\":\"string\","
3610 "\"enum\":[\"Cluster\",\"Process\",\"Service\","
3611 "\"System\",\"Unknown\"]"
3612 "},"
3613 "\"required\":[\"origin\",\"nature\",\"scope\"]"
3614 "},"
3615 "\"typedValue\":{"
3616 "\"type\":\"object\","
3617 "\"oneOf\":["
3618 "{\"$ref\":\"#/definitions/typedValue/definitions/s32Value\"},"
3619 "{\"$ref\":\"#/definitions/typedValue/definitions/s64Value\"},"
3620 "{\"$ref\":\"#/definitions/typedValue/definitions/u32Value\"},"
3621 "{\"$ref\":\"#/definitions/typedValue/definitions/u64Value\"},"
3622 "{\"$ref\":\"#/definitions/typedValue/definitions/strValue\"}"
3623 "],"
3624 "\"definitions\":{"
3625 "\"s32Value\":{"
3626 "\"properties\":{"
3627 "\"type\":{"
3628 "\"type\":\"string\","
3629 "\"enum\":[\"s32\"]"
3630 "},"
3631 "\"value\":{"
3632 "\"type\":\"integer\","
3633 "\"minimum\":-2147483648,"
3634 "\"maximum\":2147483647"
3635 "}"
3636 "},"
3637 "\"required\":[\"type\",\"value\"]"
3638 "},"
3639 "\"s64Value\":{"
3640 "\"properties\":{"
3641 "\"type\":{"
3642 "\"type\":\"string\","
3643 "\"enum\":[\"s64\"]"
3644 "},"
3645 "\"value\":{"
3646 "\"type\":\"integer\","
3647 "\"minimum\":-9007199254740991,"
3648 "\"maximum\":9007199254740991"
3649 "}"
3650 "},"
3651 "\"required\":[\"type\",\"value\"]"
3652 "},"
3653 "\"u32Value\":{"
3654 "\"properties\":{"
3655 "\"type\":{"
3656 "\"type\":\"string\","
3657 "\"enum\":[\"u32\"]"
3658 "},"
3659 "\"value\":{"
3660 "\"type\":\"integer\","
3661 "\"minimum\":0,"
3662 "\"maximum\":4294967295"
3663 "}"
3664 "},"
3665 "\"required\":[\"type\",\"value\"]"
3666 "},"
3667 "\"u64Value\":{"
3668 "\"properties\":{"
3669 "\"type\":{"
3670 "\"type\":\"string\","
3671 "\"enum\":[\"u64\"]"
3672 "},"
3673 "\"value\":{"
3674 "\"type\":\"integer\","
3675 "\"minimum\":0,"
3676 "\"maximum\":9007199254740991"
3677 "}"
3678 "},"
3679 "\"required\":[\"type\",\"value\"]"
3680 "},"
3681 "\"strValue\":{"
3682 "\"properties\":{"
3683 "\"type\":{"
3684 "\"type\":\"string\","
3685 "\"enum\":[\"str\"]"
3686 "},"
3687 "\"value\":{\"type\":\"string\"}"
3688 "},"
3689 "\"required\":[\"type\",\"value\"]"
3690 "},"
3691 "\"unknownValue\":{"
3692 "\"properties\":{"
3693 "\"type\":{"
3694 "\"type\":\"integer\","
3695 "\"minimum\":0"
3696 "},"
3697 "\"value\":{"
3698 "\"type\":\"string\","
3699 "\"enum\":[\"unknown\"]"
3700 "}"
3701 "},"
3702 "\"required\":[\"type\",\"value\"]"
3703 "}"
3704 "}"
3705 "}"
3706 "}"
3707 "}");
3708
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003709 if (old_len == out->data) {
Simon Horman6f6bb382017-01-04 09:37:26 +01003710 chunk_reset(out);
3711 chunk_appendf(out,
3712 "{\"errorStr\":\"output buffer too short\"}");
3713 }
3714}
3715
3716/* This function dumps the schema onto the stream interface's read buffer.
3717 * It returns 0 as long as it does not complete, non-zero upon completion.
3718 * No state is used.
3719 */
3720static int stats_dump_json_schema_to_buffer(struct stream_interface *si)
3721{
3722 chunk_reset(&trash);
3723
3724 stats_dump_json_schema(&trash);
3725
Willy Tarreau06d80a92017-10-19 14:32:15 +02003726 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01003727 si_rx_room_blk(si);
Simon Horman6f6bb382017-01-04 09:37:26 +01003728 return 0;
3729 }
3730
3731 return 1;
3732}
3733
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02003734static int cli_parse_clear_counters(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau89d467c2016-11-23 11:02:40 +01003735{
3736 struct proxy *px;
3737 struct server *sv;
3738 struct listener *li;
3739 int clrall = 0;
3740
3741 if (strcmp(args[2], "all") == 0)
3742 clrall = 1;
3743
3744 /* check permissions */
3745 if (!cli_has_level(appctx, ACCESS_LVL_OPER) ||
3746 (clrall && !cli_has_level(appctx, ACCESS_LVL_ADMIN)))
3747 return 1;
3748
Olivier Houchardfbc74e82017-11-24 16:54:05 +01003749 for (px = proxies_list; px; px = px->next) {
Willy Tarreau89d467c2016-11-23 11:02:40 +01003750 if (clrall) {
3751 memset(&px->be_counters, 0, sizeof(px->be_counters));
3752 memset(&px->fe_counters, 0, sizeof(px->fe_counters));
3753 }
3754 else {
3755 px->be_counters.conn_max = 0;
3756 px->be_counters.p.http.rps_max = 0;
3757 px->be_counters.sps_max = 0;
3758 px->be_counters.cps_max = 0;
3759 px->be_counters.nbpend_max = 0;
3760
3761 px->fe_counters.conn_max = 0;
3762 px->fe_counters.p.http.rps_max = 0;
3763 px->fe_counters.sps_max = 0;
3764 px->fe_counters.cps_max = 0;
Willy Tarreau89d467c2016-11-23 11:02:40 +01003765 }
3766
3767 for (sv = px->srv; sv; sv = sv->next)
3768 if (clrall)
3769 memset(&sv->counters, 0, sizeof(sv->counters));
3770 else {
3771 sv->counters.cur_sess_max = 0;
3772 sv->counters.nbpend_max = 0;
3773 sv->counters.sps_max = 0;
3774 }
3775
3776 list_for_each_entry(li, &px->conf.listeners, by_fe)
3777 if (li->counters) {
3778 if (clrall)
3779 memset(li->counters, 0, sizeof(*li->counters));
3780 else
3781 li->counters->conn_max = 0;
3782 }
3783 }
3784
3785 global.cps_max = 0;
3786 global.sps_max = 0;
Olivier Houchard00bc3cb2017-10-17 19:23:25 +02003787 global.ssl_max = 0;
3788 global.ssl_fe_keys_max = 0;
3789 global.ssl_be_keys_max = 0;
Willy Tarreaud80cb4e2018-01-20 19:30:13 +01003790
3791 memset(activity, 0, sizeof(activity));
Willy Tarreau89d467c2016-11-23 11:02:40 +01003792 return 1;
3793}
3794
3795
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02003796static int cli_parse_show_info(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003797{
Willy Tarreaud25fc792016-12-16 12:33:47 +01003798 appctx->ctx.stats.scope_str = 0;
3799 appctx->ctx.stats.scope_len = 0;
3800 appctx->ctx.stats.flags = 0;
3801
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003802 if (strcmp(args[2], "typed") == 0)
3803 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
Simon Horman05ee2132017-01-04 09:37:25 +01003804 else if (strcmp(args[2], "json") == 0)
3805 appctx->ctx.stats.flags |= STAT_FMT_JSON;
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003806 return 0;
3807}
3808
3809
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02003810static int cli_parse_show_stat(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau2b812e22016-11-22 16:18:05 +01003811{
Willy Tarreaud25fc792016-12-16 12:33:47 +01003812 appctx->ctx.stats.scope_str = 0;
3813 appctx->ctx.stats.scope_len = 0;
3814 appctx->ctx.stats.flags = 0;
3815
Willy Tarreau2b812e22016-11-22 16:18:05 +01003816 if (*args[2] && *args[3] && *args[4]) {
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01003817 struct proxy *px;
3818
3819 px = proxy_find_by_name(args[2], 0, 0);
3820 if (px)
3821 appctx->ctx.stats.iid = px->uuid;
3822 else
3823 appctx->ctx.stats.iid = atoi(args[2]);
3824
Willy Tarreau9d008692019-08-09 11:21:01 +02003825 if (!appctx->ctx.stats.iid)
3826 return cli_err(appctx, "No such proxy.\n");
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01003827
Willy Tarreau2b812e22016-11-22 16:18:05 +01003828 appctx->ctx.stats.flags |= STAT_BOUND;
Willy Tarreau2b812e22016-11-22 16:18:05 +01003829 appctx->ctx.stats.type = atoi(args[3]);
3830 appctx->ctx.stats.sid = atoi(args[4]);
3831 if (strcmp(args[5], "typed") == 0)
3832 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
Simon Horman05ee2132017-01-04 09:37:25 +01003833 else if (strcmp(args[5], "json") == 0)
3834 appctx->ctx.stats.flags |= STAT_FMT_JSON;
Willy Tarreau2b812e22016-11-22 16:18:05 +01003835 }
3836 else if (strcmp(args[2], "typed") == 0)
3837 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
Simon Horman05ee2132017-01-04 09:37:25 +01003838 else if (strcmp(args[2], "json") == 0)
3839 appctx->ctx.stats.flags |= STAT_FMT_JSON;
Willy Tarreau2b812e22016-11-22 16:18:05 +01003840
Willy Tarreau2b812e22016-11-22 16:18:05 +01003841 return 0;
3842}
3843
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003844static int cli_io_handler_dump_info(struct appctx *appctx)
3845{
3846 return stats_dump_info_to_buffer(appctx->owner);
3847}
3848
Willy Tarreau2b812e22016-11-22 16:18:05 +01003849/* This I/O handler runs as an applet embedded in a stream interface. It is
3850 * used to send raw stats over a socket.
3851 */
3852static int cli_io_handler_dump_stat(struct appctx *appctx)
3853{
Christopher Fauletef779222018-10-31 08:47:01 +01003854 return stats_dump_stat_to_buffer(appctx->owner, NULL, NULL);
Willy Tarreau2b812e22016-11-22 16:18:05 +01003855}
3856
Simon Horman6f6bb382017-01-04 09:37:26 +01003857static int cli_io_handler_dump_json_schema(struct appctx *appctx)
3858{
3859 return stats_dump_json_schema_to_buffer(appctx->owner);
3860}
3861
Willy Tarreau2b812e22016-11-22 16:18:05 +01003862/* register cli keywords */
3863static struct cli_kw_list cli_kws = {{ },{
Willy Tarreau89d467c2016-11-23 11:02:40 +01003864 { { "clear", "counters", NULL }, "clear counters : clear max statistics counters (add 'all' for all counters)", cli_parse_clear_counters, NULL, NULL },
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003865 { { "show", "info", NULL }, "show info : report information about the running process", cli_parse_show_info, cli_io_handler_dump_info, NULL },
Willy Tarreau2b812e22016-11-22 16:18:05 +01003866 { { "show", "stat", NULL }, "show stat : report counters for each proxy and server", cli_parse_show_stat, cli_io_handler_dump_stat, NULL },
Simon Horman6f6bb382017-01-04 09:37:26 +01003867 { { "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 +01003868 {{},}
3869}};
3870
Willy Tarreau0108d902018-11-25 19:14:37 +01003871INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
3872
William Lallemand74c24fb2016-11-21 17:18:36 +01003873struct applet http_stats_applet = {
3874 .obj_type = OBJ_TYPE_APPLET,
3875 .name = "<STATS>", /* used for logging */
3876 .fct = http_stats_io_handler,
3877 .release = NULL,
3878};
3879
William Lallemand74c24fb2016-11-21 17:18:36 +01003880/*
3881 * Local variables:
3882 * c-indent-level: 8
3883 * c-basic-offset: 8
3884 * End:
3885 */