blob: c7cc4269591ab03a2efe3f0ac0e6ec4fd1ae6dbb [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>
Christopher Fauletef779222018-10-31 08:47:01 +010065#include <proto/proto_http.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010066#include <proto/proxy.h>
67#include <proto/sample.h>
68#include <proto/session.h>
Willy Tarreauc125cef2019-05-10 09:58:43 +020069#include <proto/ssl_sock.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010070#include <proto/stream.h>
71#include <proto/server.h>
72#include <proto/raw_sock.h>
73#include <proto/stream_interface.h>
74#include <proto/task.h>
75
William Lallemand74c24fb2016-11-21 17:18:36 +010076
Christopher Faulet2b9b6782019-02-27 16:42:58 +010077/* status codes available for the stats admin page (strictly 4 chars length) */
78const char *stat_status_codes[STAT_STATUS_SIZE] = {
79 [STAT_STATUS_DENY] = "DENY",
80 [STAT_STATUS_DONE] = "DONE",
81 [STAT_STATUS_ERRP] = "ERRP",
82 [STAT_STATUS_EXCD] = "EXCD",
83 [STAT_STATUS_NONE] = "NONE",
84 [STAT_STATUS_PART] = "PART",
85 [STAT_STATUS_UNKN] = "UNKN",
86 [STAT_STATUS_IVAL] = "IVAL",
87};
88
Willy Tarreau0baac8c2016-11-22 16:36:53 +010089/* These are the field names for each INF_* field position. Please pay attention
90 * to always use the exact same name except that the strings for new names must
91 * be lower case or CamelCase while the enum entries must be upper case.
92 */
93const char *info_field_names[INF_TOTAL_FIELDS] = {
94 [INF_NAME] = "Name",
95 [INF_VERSION] = "Version",
96 [INF_RELEASE_DATE] = "Release_date",
Yves Lafon95317282018-02-26 11:10:37 +010097 [INF_NBTHREAD] = "Nbthread",
Willy Tarreau0baac8c2016-11-22 16:36:53 +010098 [INF_NBPROC] = "Nbproc",
99 [INF_PROCESS_NUM] = "Process_num",
100 [INF_PID] = "Pid",
101 [INF_UPTIME] = "Uptime",
102 [INF_UPTIME_SEC] = "Uptime_sec",
103 [INF_MEMMAX_MB] = "Memmax_MB",
104 [INF_POOL_ALLOC_MB] = "PoolAlloc_MB",
105 [INF_POOL_USED_MB] = "PoolUsed_MB",
106 [INF_POOL_FAILED] = "PoolFailed",
107 [INF_ULIMIT_N] = "Ulimit-n",
108 [INF_MAXSOCK] = "Maxsock",
109 [INF_MAXCONN] = "Maxconn",
110 [INF_HARD_MAXCONN] = "Hard_maxconn",
111 [INF_CURR_CONN] = "CurrConns",
112 [INF_CUM_CONN] = "CumConns",
113 [INF_CUM_REQ] = "CumReq",
114 [INF_MAX_SSL_CONNS] = "MaxSslConns",
115 [INF_CURR_SSL_CONNS] = "CurrSslConns",
116 [INF_CUM_SSL_CONNS] = "CumSslConns",
117 [INF_MAXPIPES] = "Maxpipes",
118 [INF_PIPES_USED] = "PipesUsed",
119 [INF_PIPES_FREE] = "PipesFree",
120 [INF_CONN_RATE] = "ConnRate",
121 [INF_CONN_RATE_LIMIT] = "ConnRateLimit",
122 [INF_MAX_CONN_RATE] = "MaxConnRate",
123 [INF_SESS_RATE] = "SessRate",
124 [INF_SESS_RATE_LIMIT] = "SessRateLimit",
125 [INF_MAX_SESS_RATE] = "MaxSessRate",
126 [INF_SSL_RATE] = "SslRate",
127 [INF_SSL_RATE_LIMIT] = "SslRateLimit",
128 [INF_MAX_SSL_RATE] = "MaxSslRate",
129 [INF_SSL_FRONTEND_KEY_RATE] = "SslFrontendKeyRate",
130 [INF_SSL_FRONTEND_MAX_KEY_RATE] = "SslFrontendMaxKeyRate",
131 [INF_SSL_FRONTEND_SESSION_REUSE_PCT] = "SslFrontendSessionReuse_pct",
132 [INF_SSL_BACKEND_KEY_RATE] = "SslBackendKeyRate",
133 [INF_SSL_BACKEND_MAX_KEY_RATE] = "SslBackendMaxKeyRate",
134 [INF_SSL_CACHE_LOOKUPS] = "SslCacheLookups",
135 [INF_SSL_CACHE_MISSES] = "SslCacheMisses",
136 [INF_COMPRESS_BPS_IN] = "CompressBpsIn",
137 [INF_COMPRESS_BPS_OUT] = "CompressBpsOut",
138 [INF_COMPRESS_BPS_RATE_LIM] = "CompressBpsRateLim",
139 [INF_ZLIB_MEM_USAGE] = "ZlibMemUsage",
140 [INF_MAX_ZLIB_MEM_USAGE] = "MaxZlibMemUsage",
141 [INF_TASKS] = "Tasks",
142 [INF_RUN_QUEUE] = "Run_queue",
143 [INF_IDLE_PCT] = "Idle_pct",
144 [INF_NODE] = "node",
145 [INF_DESCRIPTION] = "description",
Willy Tarreau00098ea2018-11-05 14:38:13 +0100146 [INF_STOPPING] = "Stopping",
147 [INF_JOBS] = "Jobs",
William Lallemanda7199262018-11-16 16:57:20 +0100148 [INF_UNSTOPPABLE_JOBS] = "Unstoppable Jobs",
Willy Tarreau00098ea2018-11-05 14:38:13 +0100149 [INF_LISTENERS] = "Listeners",
Willy Tarreau199ad242018-11-05 16:31:22 +0100150 [INF_ACTIVE_PEERS] = "ActivePeers",
Willy Tarreau2d372c22018-11-05 17:12:27 +0100151 [INF_CONNECTED_PEERS] = "ConnectedPeers",
Willy Tarreau13ef7732018-11-12 07:25:28 +0100152 [INF_DROPPED_LOGS] = "DroppedLogs",
Willy Tarreaubeb859a2018-11-22 18:07:59 +0100153 [INF_BUSY_POLLING] = "BusyPolling",
Baptiste Assmann333939c2019-01-21 08:34:50 +0100154 [INF_FAILED_RESOLUTIONS] = "FailedResolutions",
Willy Tarreau7cf0e452019-05-23 11:39:14 +0200155 [INF_TOTAL_BYTES_OUT] = "TotalBytesOut",
156 [INF_BYTES_OUT_RATE] = "BytesOutRate",
Willy Tarreau0baac8c2016-11-22 16:36:53 +0100157};
158
William Lallemand74c24fb2016-11-21 17:18:36 +0100159const char *stat_field_names[ST_F_TOTAL_FIELDS] = {
160 [ST_F_PXNAME] = "pxname",
161 [ST_F_SVNAME] = "svname",
162 [ST_F_QCUR] = "qcur",
163 [ST_F_QMAX] = "qmax",
164 [ST_F_SCUR] = "scur",
165 [ST_F_SMAX] = "smax",
166 [ST_F_SLIM] = "slim",
167 [ST_F_STOT] = "stot",
168 [ST_F_BIN] = "bin",
169 [ST_F_BOUT] = "bout",
170 [ST_F_DREQ] = "dreq",
171 [ST_F_DRESP] = "dresp",
172 [ST_F_EREQ] = "ereq",
173 [ST_F_ECON] = "econ",
174 [ST_F_ERESP] = "eresp",
175 [ST_F_WRETR] = "wretr",
176 [ST_F_WREDIS] = "wredis",
177 [ST_F_STATUS] = "status",
178 [ST_F_WEIGHT] = "weight",
179 [ST_F_ACT] = "act",
180 [ST_F_BCK] = "bck",
181 [ST_F_CHKFAIL] = "chkfail",
182 [ST_F_CHKDOWN] = "chkdown",
183 [ST_F_LASTCHG] = "lastchg",
184 [ST_F_DOWNTIME] = "downtime",
185 [ST_F_QLIMIT] = "qlimit",
186 [ST_F_PID] = "pid",
187 [ST_F_IID] = "iid",
188 [ST_F_SID] = "sid",
189 [ST_F_THROTTLE] = "throttle",
190 [ST_F_LBTOT] = "lbtot",
191 [ST_F_TRACKED] = "tracked",
192 [ST_F_TYPE] = "type",
193 [ST_F_RATE] = "rate",
194 [ST_F_RATE_LIM] = "rate_lim",
195 [ST_F_RATE_MAX] = "rate_max",
196 [ST_F_CHECK_STATUS] = "check_status",
197 [ST_F_CHECK_CODE] = "check_code",
198 [ST_F_CHECK_DURATION] = "check_duration",
199 [ST_F_HRSP_1XX] = "hrsp_1xx",
200 [ST_F_HRSP_2XX] = "hrsp_2xx",
201 [ST_F_HRSP_3XX] = "hrsp_3xx",
202 [ST_F_HRSP_4XX] = "hrsp_4xx",
203 [ST_F_HRSP_5XX] = "hrsp_5xx",
204 [ST_F_HRSP_OTHER] = "hrsp_other",
205 [ST_F_HANAFAIL] = "hanafail",
206 [ST_F_REQ_RATE] = "req_rate",
207 [ST_F_REQ_RATE_MAX] = "req_rate_max",
208 [ST_F_REQ_TOT] = "req_tot",
209 [ST_F_CLI_ABRT] = "cli_abrt",
210 [ST_F_SRV_ABRT] = "srv_abrt",
211 [ST_F_COMP_IN] = "comp_in",
212 [ST_F_COMP_OUT] = "comp_out",
213 [ST_F_COMP_BYP] = "comp_byp",
214 [ST_F_COMP_RSP] = "comp_rsp",
215 [ST_F_LASTSESS] = "lastsess",
216 [ST_F_LAST_CHK] = "last_chk",
217 [ST_F_LAST_AGT] = "last_agt",
218 [ST_F_QTIME] = "qtime",
219 [ST_F_CTIME] = "ctime",
220 [ST_F_RTIME] = "rtime",
221 [ST_F_TTIME] = "ttime",
222 [ST_F_AGENT_STATUS] = "agent_status",
223 [ST_F_AGENT_CODE] = "agent_code",
224 [ST_F_AGENT_DURATION] = "agent_duration",
225 [ST_F_CHECK_DESC] = "check_desc",
226 [ST_F_AGENT_DESC] = "agent_desc",
227 [ST_F_CHECK_RISE] = "check_rise",
228 [ST_F_CHECK_FALL] = "check_fall",
229 [ST_F_CHECK_HEALTH] = "check_health",
230 [ST_F_AGENT_RISE] = "agent_rise",
231 [ST_F_AGENT_FALL] = "agent_fall",
232 [ST_F_AGENT_HEALTH] = "agent_health",
233 [ST_F_ADDR] = "addr",
234 [ST_F_COOKIE] = "cookie",
235 [ST_F_MODE] = "mode",
236 [ST_F_ALGO] = "algo",
237 [ST_F_CONN_RATE] = "conn_rate",
238 [ST_F_CONN_RATE_MAX] = "conn_rate_max",
239 [ST_F_CONN_TOT] = "conn_tot",
240 [ST_F_INTERCEPTED] = "intercepted",
241 [ST_F_DCON] = "dcon",
242 [ST_F_DSES] = "dses",
Tim Duesterhus3fd19732018-05-27 20:35:08 +0200243 [ST_F_WREW] = "wrew",
Willy Tarreauf1573842018-12-14 11:35:36 +0100244 [ST_F_CONNECT] = "connect",
245 [ST_F_REUSE] = "reuse",
Willy Tarreaua1214a52018-12-14 14:00:25 +0100246 [ST_F_CACHE_LOOKUPS] = "cache_lookups",
247 [ST_F_CACHE_HITS] = "cache_hits",
Adis Neziroviceeea5702019-09-13 11:43:03 +0200248 [ST_F_SRV_ICUR] = "srv_icur",
249 [ST_F_SRV_ILIM] = "src_ilim"
William Lallemand74c24fb2016-11-21 17:18:36 +0100250};
251
Willy Tarreau0baac8c2016-11-22 16:36:53 +0100252/* one line of info */
Christopher Faulet1bc04c72017-10-29 20:14:08 +0100253static THREAD_LOCAL struct field info[INF_TOTAL_FIELDS];
William Lallemand74c24fb2016-11-21 17:18:36 +0100254/* one line of stats */
Christopher Faulet1bc04c72017-10-29 20:14:08 +0100255static THREAD_LOCAL struct field stats[ST_F_TOTAL_FIELDS];
William Lallemand74c24fb2016-11-21 17:18:36 +0100256
257
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{
277 const char *p;
278
279 if (IS_HTX_STRM(si_strm(si))) {
280 struct channel *req = si_oc(si);
281 struct htx *htx = htxbuf(&req->buf);
Christopher Faulet8fa60e42019-05-23 11:04:05 +0200282 struct htx_blk *blk;
283 struct ist uri;
Christopher Fauleted7a0662019-01-14 11:07:34 +0100284
Christopher Faulet8fa60e42019-05-23 11:04:05 +0200285 blk = htx_get_head_blk(htx);
286 BUG_ON(htx_get_blk_type(blk) != HTX_BLK_REQ_SL);
287 ALREADY_CHECKED(blk);
288 uri = htx_sl_req_uri(htx_get_blk_ptr(htx, blk));
Christopher Fauleted7a0662019-01-14 11:07:34 +0100289 p = uri.ptr;
290 }
291 else
292 p = co_head(si_oc(si));
293
294 return p + appctx->ctx.stats.scope_str;
295}
296
William Lallemand74c24fb2016-11-21 17:18:36 +0100297/*
298 * http_stats_io_handler()
299 * -> stats_dump_stat_to_buffer() // same as above, but used for CSV or HTML
300 * -> stats_dump_csv_header() // emits the CSV headers (same as above)
Simon Horman05ee2132017-01-04 09:37:25 +0100301 * -> stats_dump_json_header() // emits the JSON headers (same as above)
William Lallemand74c24fb2016-11-21 17:18:36 +0100302 * -> stats_dump_html_head() // emits the HTML headers
303 * -> stats_dump_html_info() // emits the equivalent of "show info" at the top
304 * -> stats_dump_proxy_to_buffer() // same as above, valid for CSV and HTML
305 * -> stats_dump_html_px_hdr()
306 * -> stats_dump_fe_stats()
307 * -> stats_dump_li_stats()
308 * -> stats_dump_sv_stats()
309 * -> stats_dump_be_stats()
310 * -> stats_dump_html_px_end()
311 * -> stats_dump_html_end() // emits HTML trailer
Simon Horman05ee2132017-01-04 09:37:25 +0100312 * -> stats_dump_json_end() // emits JSON trailer
William Lallemand74c24fb2016-11-21 17:18:36 +0100313 */
314
315
William Lallemand74c24fb2016-11-21 17:18:36 +0100316/* Dumps the stats CSV header to the trash buffer which. The caller is responsible
317 * for clearing it if needed.
318 * NOTE: Some tools happen to rely on the field position instead of its name,
319 * so please only append new fields at the end, never in the middle.
320 */
321static void stats_dump_csv_header()
322{
323 int field;
324
325 chunk_appendf(&trash, "# ");
326 for (field = 0; field < ST_F_TOTAL_FIELDS; field++)
327 chunk_appendf(&trash, "%s,", stat_field_names[field]);
328
329 chunk_appendf(&trash, "\n");
330}
331
332
333/* Emits a stats field without any surrounding element and properly encoded to
334 * resist CSV output. Returns non-zero on success, 0 if the buffer is full.
335 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200336int stats_emit_raw_data_field(struct buffer *out, const struct field *f)
William Lallemand74c24fb2016-11-21 17:18:36 +0100337{
338 switch (field_format(f, 0)) {
339 case FF_EMPTY: return 1;
340 case FF_S32: return chunk_appendf(out, "%d", f->u.s32);
341 case FF_U32: return chunk_appendf(out, "%u", f->u.u32);
342 case FF_S64: return chunk_appendf(out, "%lld", (long long)f->u.s64);
343 case FF_U64: return chunk_appendf(out, "%llu", (unsigned long long)f->u.u64);
344 case FF_STR: return csv_enc_append(field_str(f, 0), 1, out) != NULL;
345 default: return chunk_appendf(out, "[INCORRECT_FIELD_TYPE_%08x]", f->type);
346 }
347}
348
349/* Emits a stats field prefixed with its type. No CSV encoding is prepared, the
350 * output is supposed to be used on its own line. Returns non-zero on success, 0
351 * if the buffer is full.
352 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200353int stats_emit_typed_data_field(struct buffer *out, const struct field *f)
William Lallemand74c24fb2016-11-21 17:18:36 +0100354{
355 switch (field_format(f, 0)) {
356 case FF_EMPTY: return 1;
357 case FF_S32: return chunk_appendf(out, "s32:%d", f->u.s32);
358 case FF_U32: return chunk_appendf(out, "u32:%u", f->u.u32);
359 case FF_S64: return chunk_appendf(out, "s64:%lld", (long long)f->u.s64);
360 case FF_U64: return chunk_appendf(out, "u64:%llu", (unsigned long long)f->u.u64);
361 case FF_STR: return chunk_appendf(out, "str:%s", field_str(f, 0));
362 default: return chunk_appendf(out, "%08x:?", f->type);
363 }
364}
365
Simon Horman05ee2132017-01-04 09:37:25 +0100366/* Limit JSON integer values to the range [-(2**53)+1, (2**53)-1] as per
367 * the recommendation for interoperable integers in section 6 of RFC 7159.
368 */
369#define JSON_INT_MAX ((1ULL << 53) - 1)
370#define JSON_INT_MIN (0 - JSON_INT_MAX)
371
372/* Emits a stats field value and its type in JSON.
373 * Returns non-zero on success, 0 on error.
374 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200375int stats_emit_json_data_field(struct buffer *out, const struct field *f)
Simon Horman05ee2132017-01-04 09:37:25 +0100376{
377 int old_len;
378 char buf[20];
379 const char *type, *value = buf, *quote = "";
380
381 switch (field_format(f, 0)) {
382 case FF_EMPTY: return 1;
383 case FF_S32: type = "\"s32\"";
384 snprintf(buf, sizeof(buf), "%d", f->u.s32);
385 break;
386 case FF_U32: type = "\"u32\"";
387 snprintf(buf, sizeof(buf), "%u", f->u.u32);
388 break;
389 case FF_S64: type = "\"s64\"";
390 if (f->u.s64 < JSON_INT_MIN || f->u.s64 > JSON_INT_MAX)
391 return 0;
392 type = "\"s64\"";
393 snprintf(buf, sizeof(buf), "%lld", (long long)f->u.s64);
394 break;
395 case FF_U64: if (f->u.u64 > JSON_INT_MAX)
396 return 0;
397 type = "\"u64\"";
398 snprintf(buf, sizeof(buf), "%llu",
399 (unsigned long long) f->u.u64);
400 break;
401 case FF_STR: type = "\"str\"";
402 value = field_str(f, 0);
403 quote = "\"";
404 break;
405 default: snprintf(buf, sizeof(buf), "%u", f->type);
406 type = buf;
407 value = "unknown";
408 quote = "\"";
409 break;
410 }
411
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200412 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100413 chunk_appendf(out, ",\"value\":{\"type\":%s,\"value\":%s%s%s}",
414 type, quote, value, quote);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200415 return !(old_len == out->data);
Simon Horman05ee2132017-01-04 09:37:25 +0100416}
417
William Lallemand74c24fb2016-11-21 17:18:36 +0100418/* Emits an encoding of the field type on 3 characters followed by a delimiter.
419 * Returns non-zero on success, 0 if the buffer is full.
420 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200421int stats_emit_field_tags(struct buffer *out, const struct field *f,
422 char delim)
William Lallemand74c24fb2016-11-21 17:18:36 +0100423{
424 char origin, nature, scope;
425
426 switch (field_origin(f, 0)) {
427 case FO_METRIC: origin = 'M'; break;
428 case FO_STATUS: origin = 'S'; break;
429 case FO_KEY: origin = 'K'; break;
430 case FO_CONFIG: origin = 'C'; break;
431 case FO_PRODUCT: origin = 'P'; break;
432 default: origin = '?'; break;
433 }
434
435 switch (field_nature(f, 0)) {
436 case FN_GAUGE: nature = 'G'; break;
437 case FN_LIMIT: nature = 'L'; break;
438 case FN_MIN: nature = 'm'; break;
439 case FN_MAX: nature = 'M'; break;
440 case FN_RATE: nature = 'R'; break;
441 case FN_COUNTER: nature = 'C'; break;
442 case FN_DURATION: nature = 'D'; break;
443 case FN_AGE: nature = 'A'; break;
444 case FN_TIME: nature = 'T'; break;
445 case FN_NAME: nature = 'N'; break;
446 case FN_OUTPUT: nature = 'O'; break;
447 case FN_AVG: nature = 'a'; break;
448 default: nature = '?'; break;
449 }
450
451 switch (field_scope(f, 0)) {
452 case FS_PROCESS: scope = 'P'; break;
453 case FS_SERVICE: scope = 'S'; break;
454 case FS_SYSTEM: scope = 's'; break;
455 case FS_CLUSTER: scope = 'C'; break;
456 default: scope = '?'; break;
457 }
458
459 return chunk_appendf(out, "%c%c%c%c", origin, nature, scope, delim);
460}
461
Simon Horman05ee2132017-01-04 09:37:25 +0100462/* Emits an encoding of the field type as JSON.
463 * Returns non-zero on success, 0 if the buffer is full.
464 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200465int stats_emit_json_field_tags(struct buffer *out, const struct field *f)
Simon Horman05ee2132017-01-04 09:37:25 +0100466{
467 const char *origin, *nature, *scope;
468 int old_len;
469
470 switch (field_origin(f, 0)) {
471 case FO_METRIC: origin = "Metric"; break;
472 case FO_STATUS: origin = "Status"; break;
473 case FO_KEY: origin = "Key"; break;
474 case FO_CONFIG: origin = "Config"; break;
475 case FO_PRODUCT: origin = "Product"; break;
476 default: origin = "Unknown"; break;
477 }
478
479 switch (field_nature(f, 0)) {
480 case FN_GAUGE: nature = "Gauge"; break;
481 case FN_LIMIT: nature = "Limit"; break;
482 case FN_MIN: nature = "Min"; break;
483 case FN_MAX: nature = "Max"; break;
484 case FN_RATE: nature = "Rate"; break;
485 case FN_COUNTER: nature = "Counter"; break;
486 case FN_DURATION: nature = "Duration"; break;
487 case FN_AGE: nature = "Age"; break;
488 case FN_TIME: nature = "Time"; break;
489 case FN_NAME: nature = "Name"; break;
490 case FN_OUTPUT: nature = "Output"; break;
491 case FN_AVG: nature = "Avg"; break;
492 default: nature = "Unknown"; break;
493 }
494
495 switch (field_scope(f, 0)) {
496 case FS_PROCESS: scope = "Process"; break;
497 case FS_SERVICE: scope = "Service"; break;
498 case FS_SYSTEM: scope = "System"; break;
499 case FS_CLUSTER: scope = "Cluster"; break;
500 default: scope = "Unknown"; break;
501 }
502
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200503 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100504 chunk_appendf(out, "\"tags\":{"
505 "\"origin\":\"%s\","
506 "\"nature\":\"%s\","
507 "\"scope\":\"%s\""
508 "}", origin, nature, scope);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200509 return !(old_len == out->data);
Simon Horman05ee2132017-01-04 09:37:25 +0100510}
William Lallemand74c24fb2016-11-21 17:18:36 +0100511
512/* Dump all fields from <stats> into <out> using CSV format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200513static int stats_dump_fields_csv(struct buffer *out,
514 const struct field *stats)
William Lallemand74c24fb2016-11-21 17:18:36 +0100515{
516 int field;
517
518 for (field = 0; field < ST_F_TOTAL_FIELDS; field++) {
519 if (!stats_emit_raw_data_field(out, &stats[field]))
520 return 0;
521 if (!chunk_strcat(out, ","))
522 return 0;
523 }
524 chunk_strcat(out, "\n");
525 return 1;
526}
527
528/* Dump all fields from <stats> into <out> using a typed "field:desc:type:value" format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200529static int stats_dump_fields_typed(struct buffer *out,
530 const struct field *stats)
William Lallemand74c24fb2016-11-21 17:18:36 +0100531{
532 int field;
533
534 for (field = 0; field < ST_F_TOTAL_FIELDS; field++) {
535 if (!stats[field].type)
536 continue;
537
538 chunk_appendf(out, "%c.%u.%u.%d.%s.%u:",
539 stats[ST_F_TYPE].u.u32 == STATS_TYPE_FE ? 'F' :
540 stats[ST_F_TYPE].u.u32 == STATS_TYPE_BE ? 'B' :
541 stats[ST_F_TYPE].u.u32 == STATS_TYPE_SO ? 'L' :
542 stats[ST_F_TYPE].u.u32 == STATS_TYPE_SV ? 'S' :
543 '?',
544 stats[ST_F_IID].u.u32, stats[ST_F_SID].u.u32,
545 field, stat_field_names[field], stats[ST_F_PID].u.u32);
546
547 if (!stats_emit_field_tags(out, &stats[field], ':'))
548 return 0;
549 if (!stats_emit_typed_data_field(out, &stats[field]))
550 return 0;
551 if (!chunk_strcat(out, "\n"))
552 return 0;
553 }
554 return 1;
555}
556
Simon Horman05ee2132017-01-04 09:37:25 +0100557/* Dump all fields from <stats> into <out> using the "show info json" format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200558static int stats_dump_json_info_fields(struct buffer *out,
Simon Horman05ee2132017-01-04 09:37:25 +0100559 const struct field *info)
560{
561 int field;
562 int started = 0;
563
564 if (!chunk_strcat(out, "["))
565 return 0;
566
567 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
568 int old_len;
569
570 if (!field_format(info, field))
571 continue;
572
573 if (started && !chunk_strcat(out, ","))
574 goto err;
575 started = 1;
576
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200577 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100578 chunk_appendf(out,
579 "{\"field\":{\"pos\":%d,\"name\":\"%s\"},"
580 "\"processNum\":%u,",
581 field, info_field_names[field],
582 info[INF_PROCESS_NUM].u.u32);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200583 if (old_len == out->data)
Simon Horman05ee2132017-01-04 09:37:25 +0100584 goto err;
585
586 if (!stats_emit_json_field_tags(out, &info[field]))
587 goto err;
588
589 if (!stats_emit_json_data_field(out, &info[field]))
590 goto err;
591
592 if (!chunk_strcat(out, "}"))
593 goto err;
594 }
595
596 if (!chunk_strcat(out, "]"))
597 goto err;
598 return 1;
599
600err:
601 chunk_reset(out);
602 chunk_appendf(out, "{\"errorStr\":\"output buffer too short\"}");
603 return 0;
604}
605
606/* Dump all fields from <stats> into <out> using a typed "field:desc:type:value" format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200607static int stats_dump_fields_json(struct buffer *out,
608 const struct field *stats,
Simon Horman05ee2132017-01-04 09:37:25 +0100609 int first_stat)
610{
611 int field;
612 int started = 0;
613
614 if (!first_stat && !chunk_strcat(out, ","))
615 return 0;
616 if (!chunk_strcat(out, "["))
617 return 0;
618
619 for (field = 0; field < ST_F_TOTAL_FIELDS; field++) {
620 const char *obj_type;
621 int old_len;
622
623 if (!stats[field].type)
624 continue;
625
626 if (started && !chunk_strcat(out, ","))
627 goto err;
628 started = 1;
629
630 switch (stats[ST_F_TYPE].u.u32) {
631 case STATS_TYPE_FE: obj_type = "Frontend"; break;
632 case STATS_TYPE_BE: obj_type = "Backend"; break;
633 case STATS_TYPE_SO: obj_type = "Listener"; break;
634 case STATS_TYPE_SV: obj_type = "Server"; break;
635 default: obj_type = "Unknown"; break;
636 }
637
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200638 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100639 chunk_appendf(out,
640 "{"
641 "\"objType\":\"%s\","
642 "\"proxyId\":%d,"
643 "\"id\":%d,"
644 "\"field\":{\"pos\":%d,\"name\":\"%s\"},"
645 "\"processNum\":%u,",
646 obj_type, stats[ST_F_IID].u.u32,
647 stats[ST_F_SID].u.u32, field,
648 stat_field_names[field], stats[ST_F_PID].u.u32);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200649 if (old_len == out->data)
Simon Horman05ee2132017-01-04 09:37:25 +0100650 goto err;
651
652 if (!stats_emit_json_field_tags(out, &stats[field]))
653 goto err;
654
655 if (!stats_emit_json_data_field(out, &stats[field]))
656 goto err;
657
658 if (!chunk_strcat(out, "}"))
659 goto err;
660 }
661
662 if (!chunk_strcat(out, "]"))
663 goto err;
664
665 return 1;
666
667err:
668 chunk_reset(out);
669 if (!first_stat)
670 chunk_strcat(out, ",");
671 chunk_appendf(out, "{\"errorStr\":\"output buffer too short\"}");
672 return 0;
673}
674
William Lallemand74c24fb2016-11-21 17:18:36 +0100675/* Dump all fields from <stats> into <out> using the HTML format. A column is
676 * reserved for the checkbox is ST_SHOWADMIN is set in <flags>. Some extra info
677 * are provided if ST_SHLGNDS is present in <flags>.
678 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200679static int stats_dump_fields_html(struct buffer *out,
680 const struct field *stats,
681 unsigned int flags)
William Lallemand74c24fb2016-11-21 17:18:36 +0100682{
Willy Tarreau83061a82018-07-13 11:56:34 +0200683 struct buffer src;
William Lallemand74c24fb2016-11-21 17:18:36 +0100684
685 if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_FE) {
686 chunk_appendf(out,
687 /* name, queue */
688 "<tr class=\"frontend\">");
689
690 if (flags & ST_SHOWADMIN) {
691 /* Column sub-heading for Enable or Disable server */
692 chunk_appendf(out, "<td></td>");
693 }
694
695 chunk_appendf(out,
696 "<td class=ac>"
697 "<a name=\"%s/Frontend\"></a>"
698 "<a class=lfsb href=\"#%s/Frontend\">Frontend</a></td>"
699 "<td colspan=3></td>"
700 "",
701 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_PXNAME));
702
703 chunk_appendf(out,
704 /* sessions rate : current */
705 "<td><u>%s<div class=tips><table class=det>"
706 "<tr><th>Current connection rate:</th><td>%s/s</td></tr>"
707 "<tr><th>Current session rate:</th><td>%s/s</td></tr>"
708 "",
709 U2H(stats[ST_F_RATE].u.u32),
710 U2H(stats[ST_F_CONN_RATE].u.u32),
711 U2H(stats[ST_F_RATE].u.u32));
712
713 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
714 chunk_appendf(out,
715 "<tr><th>Current request rate:</th><td>%s/s</td></tr>",
716 U2H(stats[ST_F_REQ_RATE].u.u32));
717
718 chunk_appendf(out,
719 "</table></div></u></td>"
720 /* sessions rate : max */
721 "<td><u>%s<div class=tips><table class=det>"
722 "<tr><th>Max connection rate:</th><td>%s/s</td></tr>"
723 "<tr><th>Max session rate:</th><td>%s/s</td></tr>"
724 "",
725 U2H(stats[ST_F_RATE_MAX].u.u32),
726 U2H(stats[ST_F_CONN_RATE_MAX].u.u32),
727 U2H(stats[ST_F_RATE_MAX].u.u32));
728
729 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
730 chunk_appendf(out,
731 "<tr><th>Max request rate:</th><td>%s/s</td></tr>",
732 U2H(stats[ST_F_REQ_RATE_MAX].u.u32));
733
734 chunk_appendf(out,
735 "</table></div></u></td>"
736 /* sessions rate : limit */
737 "<td>%s</td>",
738 LIM2A(stats[ST_F_RATE_LIM].u.u32, "-"));
739
740 chunk_appendf(out,
741 /* sessions: current, max, limit, total */
742 "<td>%s</td><td>%s</td><td>%s</td>"
743 "<td><u>%s<div class=tips><table class=det>"
744 "<tr><th>Cum. connections:</th><td>%s</td></tr>"
745 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
746 "",
747 U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SMAX].u.u32), U2H(stats[ST_F_SLIM].u.u32),
748 U2H(stats[ST_F_STOT].u.u64),
749 U2H(stats[ST_F_CONN_TOT].u.u64),
750 U2H(stats[ST_F_STOT].u.u64));
751
752 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
753 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
754 chunk_appendf(out,
755 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
756 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
757 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
758 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
759 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
760 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
761 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
762 "<tr><th>- other responses:</th><td>%s</td></tr>"
763 "<tr><th>Intercepted requests:</th><td>%s</td></tr>"
Willy Tarreaua1214a52018-12-14 14:00:25 +0100764 "<tr><th>Cache lookups:</th><td>%s</td></tr>"
765 "<tr><th>Cache hits:</th><td>%s</td><td>(%d%%)</td></tr>"
Willy Tarreau1b0f85e2018-05-28 15:12:40 +0200766 "<tr><th>Failed hdr rewrites:</th><td>%s</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +0100767 "",
768 U2H(stats[ST_F_REQ_TOT].u.u64),
769 U2H(stats[ST_F_HRSP_1XX].u.u64),
770 U2H(stats[ST_F_HRSP_2XX].u.u64),
771 U2H(stats[ST_F_COMP_RSP].u.u64),
772 stats[ST_F_HRSP_2XX].u.u64 ?
773 (int)(100 * stats[ST_F_COMP_RSP].u.u64 / stats[ST_F_HRSP_2XX].u.u64) : 0,
774 U2H(stats[ST_F_HRSP_3XX].u.u64),
775 U2H(stats[ST_F_HRSP_4XX].u.u64),
776 U2H(stats[ST_F_HRSP_5XX].u.u64),
777 U2H(stats[ST_F_HRSP_OTHER].u.u64),
Willy Tarreau1b0f85e2018-05-28 15:12:40 +0200778 U2H(stats[ST_F_INTERCEPTED].u.u64),
Willy Tarreaua1214a52018-12-14 14:00:25 +0100779 U2H(stats[ST_F_CACHE_LOOKUPS].u.u64),
780 U2H(stats[ST_F_CACHE_HITS].u.u64),
781 stats[ST_F_CACHE_LOOKUPS].u.u64 ?
782 (int)(100 * stats[ST_F_CACHE_HITS].u.u64 / stats[ST_F_CACHE_LOOKUPS].u.u64) : 0,
Willy Tarreau1b0f85e2018-05-28 15:12:40 +0200783 U2H(stats[ST_F_WREW].u.u64));
William Lallemand74c24fb2016-11-21 17:18:36 +0100784 }
785
786 chunk_appendf(out,
787 "</table></div></u></td>"
788 /* sessions: lbtot, lastsess */
789 "<td></td><td></td>"
790 /* bytes : in */
791 "<td>%s</td>"
792 "",
793 U2H(stats[ST_F_BIN].u.u64));
794
795 chunk_appendf(out,
796 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
797 "<td>%s%s<div class=tips><table class=det>"
798 "<tr><th>Response bytes in:</th><td>%s</td></tr>"
799 "<tr><th>Compression in:</th><td>%s</td></tr>"
800 "<tr><th>Compression out:</th><td>%s</td><td>(%d%%)</td></tr>"
801 "<tr><th>Compression bypass:</th><td>%s</td></tr>"
802 "<tr><th>Total bytes saved:</th><td>%s</td><td>(%d%%)</td></tr>"
803 "</table></div>%s</td>",
804 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "<u>":"",
805 U2H(stats[ST_F_BOUT].u.u64),
806 U2H(stats[ST_F_BOUT].u.u64),
807 U2H(stats[ST_F_COMP_IN].u.u64),
808 U2H(stats[ST_F_COMP_OUT].u.u64),
809 stats[ST_F_COMP_IN].u.u64 ? (int)(stats[ST_F_COMP_OUT].u.u64 * 100 / stats[ST_F_COMP_IN].u.u64) : 0,
810 U2H(stats[ST_F_COMP_BYP].u.u64),
811 U2H(stats[ST_F_COMP_IN].u.u64 - stats[ST_F_COMP_OUT].u.u64),
812 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,
813 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "</u>":"");
814
815 chunk_appendf(out,
816 /* denied: req, resp */
817 "<td>%s</td><td>%s</td>"
818 /* errors : request, connect, response */
819 "<td>%s</td><td></td><td></td>"
820 /* warnings: retries, redispatches */
821 "<td></td><td></td>"
822 /* server status : reflect frontend status */
823 "<td class=ac>%s</td>"
824 /* rest of server: nothing */
825 "<td class=ac colspan=8></td></tr>"
826 "",
827 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
828 U2H(stats[ST_F_EREQ].u.u64),
829 field_str(stats, ST_F_STATUS));
830 }
831 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_SO) {
832 chunk_appendf(out, "<tr class=socket>");
833 if (flags & ST_SHOWADMIN) {
834 /* Column sub-heading for Enable or Disable server */
835 chunk_appendf(out, "<td></td>");
836 }
837
838 chunk_appendf(out,
839 /* frontend name, listener name */
840 "<td class=ac><a name=\"%s/+%s\"></a>%s"
841 "<a class=lfsb href=\"#%s/+%s\">%s</a>"
842 "",
843 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME),
844 (flags & ST_SHLGNDS)?"<u>":"",
845 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME), field_str(stats, ST_F_SVNAME));
846
847 if (flags & ST_SHLGNDS) {
848 chunk_appendf(out, "<div class=tips>");
849
850 if (isdigit(*field_str(stats, ST_F_ADDR)))
851 chunk_appendf(out, "IPv4: %s, ", field_str(stats, ST_F_ADDR));
852 else if (*field_str(stats, ST_F_ADDR) == '[')
853 chunk_appendf(out, "IPv6: %s, ", field_str(stats, ST_F_ADDR));
854 else if (*field_str(stats, ST_F_ADDR))
855 chunk_appendf(out, "%s, ", field_str(stats, ST_F_ADDR));
856
857 /* id */
858 chunk_appendf(out, "id: %d</div>", stats[ST_F_SID].u.u32);
859 }
860
861 chunk_appendf(out,
862 /* queue */
863 "%s</td><td colspan=3></td>"
864 /* sessions rate: current, max, limit */
865 "<td colspan=3>&nbsp;</td>"
866 /* sessions: current, max, limit, total, lbtot, lastsess */
867 "<td>%s</td><td>%s</td><td>%s</td>"
868 "<td>%s</td><td>&nbsp;</td><td>&nbsp;</td>"
869 /* bytes: in, out */
870 "<td>%s</td><td>%s</td>"
871 "",
872 (flags & ST_SHLGNDS)?"</u>":"",
873 U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SMAX].u.u32), U2H(stats[ST_F_SLIM].u.u32),
874 U2H(stats[ST_F_STOT].u.u64), U2H(stats[ST_F_BIN].u.u64), U2H(stats[ST_F_BOUT].u.u64));
875
876 chunk_appendf(out,
877 /* denied: req, resp */
878 "<td>%s</td><td>%s</td>"
879 /* errors: request, connect, response */
880 "<td>%s</td><td></td><td></td>"
881 /* warnings: retries, redispatches */
882 "<td></td><td></td>"
883 /* server status: reflect listener status */
884 "<td class=ac>%s</td>"
885 /* rest of server: nothing */
886 "<td class=ac colspan=8></td></tr>"
887 "",
888 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
889 U2H(stats[ST_F_EREQ].u.u64),
890 field_str(stats, ST_F_STATUS));
891 }
892 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_SV) {
893 const char *style;
894
895 /* determine the style to use depending on the server's state,
896 * its health and weight. There isn't a 1-to-1 mapping between
897 * state and styles for the cases where the server is (still)
898 * up. The reason is that we don't want to report nolb and
899 * drain with the same color.
900 */
901
902 if (strcmp(field_str(stats, ST_F_STATUS), "DOWN") == 0 ||
903 strcmp(field_str(stats, ST_F_STATUS), "DOWN (agent)") == 0) {
904 style = "down";
905 }
906 else if (strcmp(field_str(stats, ST_F_STATUS), "DOWN ") == 0) {
907 style = "going_up";
908 }
909 else if (strcmp(field_str(stats, ST_F_STATUS), "NOLB ") == 0) {
910 style = "going_down";
911 }
912 else if (strcmp(field_str(stats, ST_F_STATUS), "NOLB") == 0) {
913 style = "nolb";
914 }
915 else if (strcmp(field_str(stats, ST_F_STATUS), "no check") == 0) {
916 style = "no_check";
917 }
918 else if (!stats[ST_F_CHKFAIL].type ||
919 stats[ST_F_CHECK_HEALTH].u.u32 == stats[ST_F_CHECK_RISE].u.u32 + stats[ST_F_CHECK_FALL].u.u32 - 1) {
920 /* no check or max health = UP */
921 if (stats[ST_F_WEIGHT].u.u32)
922 style = "up";
923 else
924 style = "draining";
925 }
926 else {
927 style = "going_down";
928 }
929
930 if (memcmp(field_str(stats, ST_F_STATUS), "MAINT", 5) == 0)
931 chunk_appendf(out, "<tr class=\"maintain\">");
932 else
933 chunk_appendf(out,
934 "<tr class=\"%s_%s\">",
935 (stats[ST_F_BCK].u.u32) ? "backup" : "active", style);
936
937
938 if (flags & ST_SHOWADMIN)
939 chunk_appendf(out,
David Harrigand3db35a2016-12-30 12:12:49 +0000940 "<td><input class='%s-checkbox' type=\"checkbox\" name=\"s\" value=\"%s\"></td>",
941 field_str(stats, ST_F_PXNAME),
William Lallemand74c24fb2016-11-21 17:18:36 +0100942 field_str(stats, ST_F_SVNAME));
943
944 chunk_appendf(out,
945 "<td class=ac><a name=\"%s/%s\"></a>%s"
946 "<a class=lfsb href=\"#%s/%s\">%s</a>"
947 "",
948 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME),
949 (flags & ST_SHLGNDS) ? "<u>" : "",
950 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME), field_str(stats, ST_F_SVNAME));
951
952 if (flags & ST_SHLGNDS) {
953 chunk_appendf(out, "<div class=tips>");
954
955 if (isdigit(*field_str(stats, ST_F_ADDR)))
956 chunk_appendf(out, "IPv4: %s, ", field_str(stats, ST_F_ADDR));
957 else if (*field_str(stats, ST_F_ADDR) == '[')
958 chunk_appendf(out, "IPv6: %s, ", field_str(stats, ST_F_ADDR));
959 else if (*field_str(stats, ST_F_ADDR))
960 chunk_appendf(out, "%s, ", field_str(stats, ST_F_ADDR));
961
962 /* id */
963 chunk_appendf(out, "id: %d", stats[ST_F_SID].u.u32);
964
965 /* cookie */
966 if (stats[ST_F_COOKIE].type) {
967 chunk_appendf(out, ", cookie: '");
968 chunk_initstr(&src, field_str(stats, ST_F_COOKIE));
969 chunk_htmlencode(out, &src);
970 chunk_appendf(out, "'");
971 }
972
973 chunk_appendf(out, "</div>");
974 }
975
976 chunk_appendf(out,
977 /* queue : current, max, limit */
978 "%s</td><td>%s</td><td>%s</td><td>%s</td>"
979 /* sessions rate : current, max, limit */
980 "<td>%s</td><td>%s</td><td></td>"
981 "",
982 (flags & ST_SHLGNDS) ? "</u>" : "",
983 U2H(stats[ST_F_QCUR].u.u32), U2H(stats[ST_F_QMAX].u.u32), LIM2A(stats[ST_F_QLIMIT].u.u32, "-"),
984 U2H(stats[ST_F_RATE].u.u32), U2H(stats[ST_F_RATE_MAX].u.u32));
985
986 chunk_appendf(out,
987 /* sessions: current, max, limit, total */
Willy Tarreau0ff699e2019-09-08 09:24:56 +0200988 "<td><u>%s<div class=tips>"
989 "<table class=det>"
990 "<tr><th>Current active connections:</th><td>%s</td></tr>"
991 "<tr><th>Current idle connections:</th><td>%s</td></tr>"
992 "<tr><th>Active connections limit:</th><td>%s</td></tr>"
993 "<tr><th>Idle connections limit:</th><td>%s</td></tr>"
994 "</table></div></u>"
995 "</td><td>%s</td><td>%s</td>"
William Lallemand74c24fb2016-11-21 17:18:36 +0100996 "<td><u>%s<div class=tips><table class=det>"
997 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
998 "",
Willy Tarreau0ff699e2019-09-08 09:24:56 +0200999 U2H(stats[ST_F_SCUR].u.u32),
1000 U2H(stats[ST_F_SCUR].u.u32),
1001 U2H(stats[ST_F_SRV_ICUR].u.u32),
1002 LIM2A(stats[ST_F_SLIM].u.u32, "-"),
1003 stats[ST_F_SRV_ILIM].type ? U2H(stats[ST_F_SRV_ILIM].u.u32) : "-",
1004 U2H(stats[ST_F_SMAX].u.u32), LIM2A(stats[ST_F_SLIM].u.u32, "-"),
William Lallemand74c24fb2016-11-21 17:18:36 +01001005 U2H(stats[ST_F_STOT].u.u64),
1006 U2H(stats[ST_F_STOT].u.u64));
1007
1008 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
1009 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
1010 unsigned long long tot;
1011
1012 tot = stats[ST_F_HRSP_OTHER].u.u64;
1013 tot += stats[ST_F_HRSP_1XX].u.u64;
1014 tot += stats[ST_F_HRSP_2XX].u.u64;
1015 tot += stats[ST_F_HRSP_3XX].u.u64;
1016 tot += stats[ST_F_HRSP_4XX].u.u64;
1017 tot += stats[ST_F_HRSP_5XX].u.u64;
1018
1019 chunk_appendf(out,
Willy Tarreauf1573842018-12-14 11:35:36 +01001020 "<tr><th>New connections:</th><td>%s</td></tr>"
1021 "<tr><th>Reused connections:</th><td>%s</td><td>(%d%%)</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001022 "<tr><th>Cum. HTTP responses:</th><td>%s</td></tr>"
1023 "<tr><th>- HTTP 1xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1024 "<tr><th>- HTTP 2xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1025 "<tr><th>- HTTP 3xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1026 "<tr><th>- HTTP 4xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1027 "<tr><th>- HTTP 5xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1028 "<tr><th>- other responses:</th><td>%s</td><td>(%d%%)</td></tr>"
Willy Tarreau1b0f85e2018-05-28 15:12:40 +02001029 "<tr><th>Failed hdr rewrites:</th><td>%s</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001030 "",
Willy Tarreauf1573842018-12-14 11:35:36 +01001031 U2H(stats[ST_F_CONNECT].u.u64),
1032 U2H(stats[ST_F_REUSE].u.u64),
1033 (stats[ST_F_CONNECT].u.u64 + stats[ST_F_REUSE].u.u64) ?
1034 (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 +01001035 U2H(tot),
1036 U2H(stats[ST_F_HRSP_1XX].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_1XX].u.u64 / tot) : 0,
1037 U2H(stats[ST_F_HRSP_2XX].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_2XX].u.u64 / tot) : 0,
1038 U2H(stats[ST_F_HRSP_3XX].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_3XX].u.u64 / tot) : 0,
1039 U2H(stats[ST_F_HRSP_4XX].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_4XX].u.u64 / tot) : 0,
1040 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 +02001041 U2H(stats[ST_F_HRSP_OTHER].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_OTHER].u.u64 / tot) : 0,
1042 U2H(stats[ST_F_WREW].u.u64));
William Lallemand74c24fb2016-11-21 17:18:36 +01001043 }
1044
1045 chunk_appendf(out, "<tr><th colspan=3>Avg over last 1024 success. conn.</th></tr>");
1046 chunk_appendf(out, "<tr><th>- Queue time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_QTIME].u.u32));
1047 chunk_appendf(out, "<tr><th>- Connect time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_CTIME].u.u32));
1048 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
1049 chunk_appendf(out, "<tr><th>- Response time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_RTIME].u.u32));
1050 chunk_appendf(out, "<tr><th>- Total time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_TTIME].u.u32));
1051
1052 chunk_appendf(out,
1053 "</table></div></u></td>"
1054 /* sessions: lbtot, last */
1055 "<td>%s</td><td>%s</td>",
1056 U2H(stats[ST_F_LBTOT].u.u64),
1057 human_time(stats[ST_F_LASTSESS].u.s32, 1));
1058
1059 chunk_appendf(out,
1060 /* bytes : in, out */
1061 "<td>%s</td><td>%s</td>"
1062 /* denied: req, resp */
1063 "<td></td><td>%s</td>"
1064 /* errors : request, connect */
1065 "<td></td><td>%s</td>"
1066 /* errors : response */
1067 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
1068 /* warnings: retries, redispatches */
1069 "<td>%lld</td><td>%lld</td>"
1070 "",
1071 U2H(stats[ST_F_BIN].u.u64), U2H(stats[ST_F_BOUT].u.u64),
1072 U2H(stats[ST_F_DRESP].u.u64),
1073 U2H(stats[ST_F_ECON].u.u64),
1074 U2H(stats[ST_F_ERESP].u.u64),
1075 (long long)stats[ST_F_CLI_ABRT].u.u64,
1076 (long long)stats[ST_F_SRV_ABRT].u.u64,
1077 (long long)stats[ST_F_WRETR].u.u64,
1078 (long long)stats[ST_F_WREDIS].u.u64);
1079
1080 /* status, last change */
1081 chunk_appendf(out, "<td class=ac>");
1082
1083 /* FIXME!!!!
1084 * LASTCHG should contain the last change for *this* server and must be computed
1085 * properly above, as was done below, ie: this server if maint, otherwise ref server
1086 * if tracking. Note that ref is either local or remote depending on tracking.
1087 */
1088
1089
1090 if (memcmp(field_str(stats, ST_F_STATUS), "MAINT", 5) == 0) {
1091 chunk_appendf(out, "%s MAINT", human_time(stats[ST_F_LASTCHG].u.u32, 1));
1092 }
1093 else if (memcmp(field_str(stats, ST_F_STATUS), "no check", 5) == 0) {
1094 chunk_strcat(out, "<i>no check</i>");
1095 }
1096 else {
1097 chunk_appendf(out, "%s %s", human_time(stats[ST_F_LASTCHG].u.u32, 1), field_str(stats, ST_F_STATUS));
1098 if (memcmp(field_str(stats, ST_F_STATUS), "DOWN", 4) == 0) {
1099 if (stats[ST_F_CHECK_HEALTH].u.u32)
1100 chunk_strcat(out, " &uarr;");
1101 }
1102 else if (stats[ST_F_CHECK_HEALTH].u.u32 < stats[ST_F_CHECK_RISE].u.u32 + stats[ST_F_CHECK_FALL].u.u32 - 1)
1103 chunk_strcat(out, " &darr;");
1104 }
1105
1106 if (memcmp(field_str(stats, ST_F_STATUS), "DOWN", 4) == 0 &&
1107 stats[ST_F_AGENT_STATUS].type && !stats[ST_F_AGENT_HEALTH].u.u32) {
1108 chunk_appendf(out,
1109 "</td><td class=ac><u> %s",
1110 field_str(stats, ST_F_AGENT_STATUS));
1111
1112 if (stats[ST_F_AGENT_CODE].type)
1113 chunk_appendf(out, "/%d", stats[ST_F_AGENT_CODE].u.u32);
1114
1115 if (stats[ST_F_AGENT_DURATION].type)
1116 chunk_appendf(out, " in %lums", (long)stats[ST_F_AGENT_DURATION].u.u64);
1117
1118 chunk_appendf(out, "<div class=tips>%s", field_str(stats, ST_F_AGENT_DESC));
1119
1120 if (*field_str(stats, ST_F_LAST_AGT)) {
1121 chunk_appendf(out, ": ");
1122 chunk_initstr(&src, field_str(stats, ST_F_LAST_AGT));
1123 chunk_htmlencode(out, &src);
1124 }
1125 chunk_appendf(out, "</div></u>");
1126 }
1127 else if (stats[ST_F_CHECK_STATUS].type) {
1128 chunk_appendf(out,
1129 "</td><td class=ac><u> %s",
1130 field_str(stats, ST_F_CHECK_STATUS));
1131
1132 if (stats[ST_F_CHECK_CODE].type)
1133 chunk_appendf(out, "/%d", stats[ST_F_CHECK_CODE].u.u32);
1134
1135 if (stats[ST_F_CHECK_DURATION].type)
1136 chunk_appendf(out, " in %lums", (long)stats[ST_F_CHECK_DURATION].u.u64);
1137
1138 chunk_appendf(out, "<div class=tips>%s", field_str(stats, ST_F_CHECK_DESC));
1139
1140 if (*field_str(stats, ST_F_LAST_CHK)) {
1141 chunk_appendf(out, ": ");
1142 chunk_initstr(&src, field_str(stats, ST_F_LAST_CHK));
1143 chunk_htmlencode(out, &src);
1144 }
1145 chunk_appendf(out, "</div></u>");
1146 }
1147 else
1148 chunk_appendf(out, "</td><td>");
1149
1150 chunk_appendf(out,
1151 /* weight */
1152 "</td><td class=ac>%d</td>"
1153 /* act, bck */
1154 "<td class=ac>%s</td><td class=ac>%s</td>"
1155 "",
1156 stats[ST_F_WEIGHT].u.u32,
1157 stats[ST_F_BCK].u.u32 ? "-" : "Y",
1158 stats[ST_F_BCK].u.u32 ? "Y" : "-");
1159
1160 /* check failures: unique, fatal, down time */
1161 if (strcmp(field_str(stats, ST_F_STATUS), "MAINT (resolution)") == 0) {
1162 chunk_appendf(out, "<td class=ac colspan=3>resolution</td>");
1163 }
1164 else if (stats[ST_F_CHKFAIL].type) {
1165 chunk_appendf(out, "<td><u>%lld", (long long)stats[ST_F_CHKFAIL].u.u64);
1166
1167 if (stats[ST_F_HANAFAIL].type)
1168 chunk_appendf(out, "/%lld", (long long)stats[ST_F_HANAFAIL].u.u64);
1169
1170 chunk_appendf(out,
1171 "<div class=tips>Failed Health Checks%s</div></u></td>"
1172 "<td>%lld</td><td>%s</td>"
1173 "",
1174 stats[ST_F_HANAFAIL].type ? "/Health Analyses" : "",
1175 (long long)stats[ST_F_CHKDOWN].u.u64, human_time(stats[ST_F_DOWNTIME].u.u32, 1));
1176 }
1177 else if (strcmp(field_str(stats, ST_F_STATUS), "MAINT") != 0 && field_format(stats, ST_F_TRACKED) == FF_STR) {
1178 /* tracking a server (hence inherited maint would appear as "MAINT (via...)" */
1179 chunk_appendf(out,
1180 "<td class=ac colspan=3><a class=lfsb href=\"#%s\">via %s</a></td>",
1181 field_str(stats, ST_F_TRACKED), field_str(stats, ST_F_TRACKED));
1182 }
1183 else
1184 chunk_appendf(out, "<td colspan=3></td>");
1185
1186 /* throttle */
1187 if (stats[ST_F_THROTTLE].type)
1188 chunk_appendf(out, "<td class=ac>%d %%</td></tr>\n", stats[ST_F_THROTTLE].u.u32);
1189 else
1190 chunk_appendf(out, "<td class=ac>-</td></tr>\n");
1191 }
1192 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_BE) {
1193 chunk_appendf(out, "<tr class=\"backend\">");
1194 if (flags & ST_SHOWADMIN) {
1195 /* Column sub-heading for Enable or Disable server */
1196 chunk_appendf(out, "<td></td>");
1197 }
1198 chunk_appendf(out,
1199 "<td class=ac>"
1200 /* name */
1201 "%s<a name=\"%s/Backend\"></a>"
1202 "<a class=lfsb href=\"#%s/Backend\">Backend</a>"
1203 "",
1204 (flags & ST_SHLGNDS)?"<u>":"",
1205 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_PXNAME));
1206
1207 if (flags & ST_SHLGNDS) {
1208 /* balancing */
1209 chunk_appendf(out, "<div class=tips>balancing: %s",
1210 field_str(stats, ST_F_ALGO));
1211
1212 /* cookie */
1213 if (stats[ST_F_COOKIE].type) {
1214 chunk_appendf(out, ", cookie: '");
1215 chunk_initstr(&src, field_str(stats, ST_F_COOKIE));
1216 chunk_htmlencode(out, &src);
1217 chunk_appendf(out, "'");
1218 }
1219 chunk_appendf(out, "</div>");
1220 }
1221
1222 chunk_appendf(out,
1223 "%s</td>"
1224 /* queue : current, max */
1225 "<td>%s</td><td>%s</td><td></td>"
1226 /* sessions rate : current, max, limit */
1227 "<td>%s</td><td>%s</td><td></td>"
1228 "",
1229 (flags & ST_SHLGNDS)?"</u>":"",
1230 U2H(stats[ST_F_QCUR].u.u32), U2H(stats[ST_F_QMAX].u.u32),
1231 U2H(stats[ST_F_RATE].u.u32), U2H(stats[ST_F_RATE_MAX].u.u32));
1232
1233 chunk_appendf(out,
1234 /* sessions: current, max, limit, total */
1235 "<td>%s</td><td>%s</td><td>%s</td>"
1236 "<td><u>%s<div class=tips><table class=det>"
1237 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
1238 "",
Willy Tarreau8e0f1752016-12-12 15:07:29 +01001239 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 +01001240 U2H(stats[ST_F_STOT].u.u64),
1241 U2H(stats[ST_F_STOT].u.u64));
1242
1243 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
1244 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
1245 chunk_appendf(out,
Willy Tarreauf1573842018-12-14 11:35:36 +01001246 "<tr><th>New connections:</th><td>%s</td></tr>"
1247 "<tr><th>Reused connections:</th><td>%s</td><td>(%d%%)</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001248 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
1249 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
1250 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
1251 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
1252 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
1253 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
1254 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
1255 "<tr><th>- other responses:</th><td>%s</td></tr>"
Willy Tarreaua1214a52018-12-14 14:00:25 +01001256 "<tr><th>Cache lookups:</th><td>%s</td></tr>"
1257 "<tr><th>Cache hits:</th><td>%s</td><td>(%d%%)</td></tr>"
Willy Tarreau1b0f85e2018-05-28 15:12:40 +02001258 "<tr><th>Failed hdr rewrites:</th><td>%s</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001259 "<tr><th colspan=3>Avg over last 1024 success. conn.</th></tr>"
1260 "",
Willy Tarreauf1573842018-12-14 11:35:36 +01001261 U2H(stats[ST_F_CONNECT].u.u64),
1262 U2H(stats[ST_F_REUSE].u.u64),
1263 (stats[ST_F_CONNECT].u.u64 + stats[ST_F_REUSE].u.u64) ?
1264 (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 +01001265 U2H(stats[ST_F_REQ_TOT].u.u64),
1266 U2H(stats[ST_F_HRSP_1XX].u.u64),
1267 U2H(stats[ST_F_HRSP_2XX].u.u64),
1268 U2H(stats[ST_F_COMP_RSP].u.u64),
1269 stats[ST_F_HRSP_2XX].u.u64 ?
1270 (int)(100 * stats[ST_F_COMP_RSP].u.u64 / stats[ST_F_HRSP_2XX].u.u64) : 0,
1271 U2H(stats[ST_F_HRSP_3XX].u.u64),
1272 U2H(stats[ST_F_HRSP_4XX].u.u64),
1273 U2H(stats[ST_F_HRSP_5XX].u.u64),
Willy Tarreaufeead3a2018-12-14 13:48:44 +01001274 U2H(stats[ST_F_HRSP_OTHER].u.u64),
Willy Tarreaua1214a52018-12-14 14:00:25 +01001275 U2H(stats[ST_F_CACHE_LOOKUPS].u.u64),
1276 U2H(stats[ST_F_CACHE_HITS].u.u64),
1277 stats[ST_F_CACHE_LOOKUPS].u.u64 ?
1278 (int)(100 * stats[ST_F_CACHE_HITS].u.u64 / stats[ST_F_CACHE_LOOKUPS].u.u64) : 0,
Willy Tarreaufeead3a2018-12-14 13:48:44 +01001279 U2H(stats[ST_F_WREW].u.u64));
William Lallemand74c24fb2016-11-21 17:18:36 +01001280 }
1281
1282 chunk_appendf(out, "<tr><th>- Queue time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_QTIME].u.u32));
1283 chunk_appendf(out, "<tr><th>- Connect time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_CTIME].u.u32));
1284 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
1285 chunk_appendf(out, "<tr><th>- Response time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_RTIME].u.u32));
1286 chunk_appendf(out, "<tr><th>- Total time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_TTIME].u.u32));
1287
1288 chunk_appendf(out,
1289 "</table></div></u></td>"
1290 /* sessions: lbtot, last */
1291 "<td>%s</td><td>%s</td>"
1292 /* bytes: in */
1293 "<td>%s</td>"
1294 "",
1295 U2H(stats[ST_F_LBTOT].u.u64),
1296 human_time(stats[ST_F_LASTSESS].u.s32, 1),
1297 U2H(stats[ST_F_BIN].u.u64));
1298
1299 chunk_appendf(out,
1300 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
1301 "<td>%s%s<div class=tips><table class=det>"
1302 "<tr><th>Response bytes in:</th><td>%s</td></tr>"
1303 "<tr><th>Compression in:</th><td>%s</td></tr>"
1304 "<tr><th>Compression out:</th><td>%s</td><td>(%d%%)</td></tr>"
1305 "<tr><th>Compression bypass:</th><td>%s</td></tr>"
1306 "<tr><th>Total bytes saved:</th><td>%s</td><td>(%d%%)</td></tr>"
1307 "</table></div>%s</td>",
1308 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "<u>":"",
1309 U2H(stats[ST_F_BOUT].u.u64),
1310 U2H(stats[ST_F_BOUT].u.u64),
1311 U2H(stats[ST_F_COMP_IN].u.u64),
1312 U2H(stats[ST_F_COMP_OUT].u.u64),
1313 stats[ST_F_COMP_IN].u.u64 ? (int)(stats[ST_F_COMP_OUT].u.u64 * 100 / stats[ST_F_COMP_IN].u.u64) : 0,
1314 U2H(stats[ST_F_COMP_BYP].u.u64),
1315 U2H(stats[ST_F_COMP_IN].u.u64 - stats[ST_F_COMP_OUT].u.u64),
1316 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,
1317 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "</u>":"");
1318
1319 chunk_appendf(out,
1320 /* denied: req, resp */
1321 "<td>%s</td><td>%s</td>"
1322 /* errors : request, connect */
1323 "<td></td><td>%s</td>"
1324 /* errors : response */
1325 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
1326 /* warnings: retries, redispatches */
1327 "<td>%lld</td><td>%lld</td>"
1328 /* backend status: reflect backend status (up/down): we display UP
1329 * if the backend has known working servers or if it has no server at
1330 * all (eg: for stats). Then we display the total weight, number of
1331 * active and backups. */
1332 "<td class=ac>%s %s</td><td class=ac>&nbsp;</td><td class=ac>%d</td>"
1333 "<td class=ac>%d</td><td class=ac>%d</td>"
1334 "",
1335 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
1336 U2H(stats[ST_F_ECON].u.u64),
1337 U2H(stats[ST_F_ERESP].u.u64),
1338 (long long)stats[ST_F_CLI_ABRT].u.u64,
1339 (long long)stats[ST_F_SRV_ABRT].u.u64,
1340 (long long)stats[ST_F_WRETR].u.u64, (long long)stats[ST_F_WREDIS].u.u64,
1341 human_time(stats[ST_F_LASTCHG].u.u32, 1),
1342 strcmp(field_str(stats, ST_F_STATUS), "DOWN") ? field_str(stats, ST_F_STATUS) : "<font color=\"red\"><b>DOWN</b></font>",
1343 stats[ST_F_WEIGHT].u.u32,
1344 stats[ST_F_ACT].u.u32, stats[ST_F_BCK].u.u32);
1345
1346 chunk_appendf(out,
1347 /* rest of backend: nothing, down transitions, total downtime, throttle */
1348 "<td class=ac>&nbsp;</td><td>%d</td>"
1349 "<td>%s</td>"
1350 "<td></td>"
1351 "</tr>",
1352 stats[ST_F_CHKDOWN].u.u32,
1353 stats[ST_F_DOWNTIME].type ? human_time(stats[ST_F_DOWNTIME].u.u32, 1) : "&nbsp;");
1354 }
1355 return 1;
1356}
1357
1358int stats_dump_one_line(const struct field *stats, unsigned int flags, struct proxy *px, struct appctx *appctx)
1359{
Simon Horman05ee2132017-01-04 09:37:25 +01001360 int ret;
1361
William Lallemand74c24fb2016-11-21 17:18:36 +01001362 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN))
1363 flags |= ST_SHOWADMIN;
1364
1365 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
Simon Horman05ee2132017-01-04 09:37:25 +01001366 ret = stats_dump_fields_html(&trash, stats, flags);
William Lallemand74c24fb2016-11-21 17:18:36 +01001367 else if (appctx->ctx.stats.flags & STAT_FMT_TYPED)
Simon Horman05ee2132017-01-04 09:37:25 +01001368 ret = stats_dump_fields_typed(&trash, stats);
1369 else if (appctx->ctx.stats.flags & STAT_FMT_JSON)
1370 ret = stats_dump_fields_json(&trash, stats,
1371 !(appctx->ctx.stats.flags &
1372 STAT_STARTED));
William Lallemand74c24fb2016-11-21 17:18:36 +01001373 else
Simon Horman05ee2132017-01-04 09:37:25 +01001374 ret = stats_dump_fields_csv(&trash, stats);
1375
1376 if (ret)
1377 appctx->ctx.stats.flags |= STAT_STARTED;
1378
1379 return ret;
William Lallemand74c24fb2016-11-21 17:18:36 +01001380}
1381
1382/* Fill <stats> with the frontend statistics. <stats> is
1383 * preallocated array of length <len>. The length of the array
1384 * must be at least ST_F_TOTAL_FIELDS. If this length is less then
1385 * this value, the function returns 0, otherwise, it returns 1.
1386 */
1387int stats_fill_fe_stats(struct proxy *px, struct field *stats, int len)
1388{
1389 if (len < ST_F_TOTAL_FIELDS)
1390 return 0;
1391
1392 memset(stats, 0, sizeof(*stats) * len);
1393
1394 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1395 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, "FRONTEND");
1396 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1397 stats[ST_F_SCUR] = mkf_u32(0, px->feconn);
1398 stats[ST_F_SMAX] = mkf_u32(FN_MAX, px->fe_counters.conn_max);
1399 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, px->maxconn);
1400 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, px->fe_counters.cum_sess);
1401 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, px->fe_counters.bytes_in);
1402 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, px->fe_counters.bytes_out);
1403 stats[ST_F_DREQ] = mkf_u64(FN_COUNTER, px->fe_counters.denied_req);
1404 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, px->fe_counters.denied_resp);
1405 stats[ST_F_EREQ] = mkf_u64(FN_COUNTER, px->fe_counters.failed_req);
1406 stats[ST_F_DCON] = mkf_u64(FN_COUNTER, px->fe_counters.denied_conn);
1407 stats[ST_F_DSES] = mkf_u64(FN_COUNTER, px->fe_counters.denied_sess);
1408 stats[ST_F_STATUS] = mkf_str(FO_STATUS, px->state == PR_STREADY ? "OPEN" : px->state == PR_STFULL ? "FULL" : "STOP");
1409 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
1410 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1411 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, 0);
1412 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_FE);
1413 stats[ST_F_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_sess_per_sec));
1414 stats[ST_F_RATE_LIM] = mkf_u32(FO_CONFIG|FN_LIMIT, px->fe_sps_lim);
1415 stats[ST_F_RATE_MAX] = mkf_u32(FN_MAX, px->fe_counters.sps_max);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001416 stats[ST_F_WREW] = mkf_u64(FN_COUNTER, px->fe_counters.failed_rewrites);
William Lallemand74c24fb2016-11-21 17:18:36 +01001417
1418 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
1419 if (px->mode == PR_MODE_HTTP) {
1420 stats[ST_F_HRSP_1XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[1]);
1421 stats[ST_F_HRSP_2XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[2]);
1422 stats[ST_F_HRSP_3XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[3]);
1423 stats[ST_F_HRSP_4XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[4]);
1424 stats[ST_F_HRSP_5XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[5]);
1425 stats[ST_F_HRSP_OTHER] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[0]);
1426 stats[ST_F_INTERCEPTED] = mkf_u64(FN_COUNTER, px->fe_counters.intercepted_req);
Willy Tarreaua1214a52018-12-14 14:00:25 +01001427 stats[ST_F_CACHE_LOOKUPS] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cache_lookups);
1428 stats[ST_F_CACHE_HITS] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cache_hits);
William Lallemand74c24fb2016-11-21 17:18:36 +01001429 }
1430
1431 /* requests : req_rate, req_rate_max, req_tot, */
1432 stats[ST_F_REQ_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_req_per_sec));
1433 stats[ST_F_REQ_RATE_MAX] = mkf_u32(FN_MAX, px->fe_counters.p.http.rps_max);
1434 stats[ST_F_REQ_TOT] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cum_req);
1435
1436 /* compression: in, out, bypassed, responses */
1437 stats[ST_F_COMP_IN] = mkf_u64(FN_COUNTER, px->fe_counters.comp_in);
1438 stats[ST_F_COMP_OUT] = mkf_u64(FN_COUNTER, px->fe_counters.comp_out);
1439 stats[ST_F_COMP_BYP] = mkf_u64(FN_COUNTER, px->fe_counters.comp_byp);
1440 stats[ST_F_COMP_RSP] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.comp_rsp);
1441
1442 /* connections : conn_rate, conn_rate_max, conn_tot, conn_max */
1443 stats[ST_F_CONN_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_conn_per_sec));
1444 stats[ST_F_CONN_RATE_MAX] = mkf_u32(FN_MAX, px->fe_counters.cps_max);
1445 stats[ST_F_CONN_TOT] = mkf_u64(FN_COUNTER, px->fe_counters.cum_conn);
1446
1447 return 1;
1448}
1449
1450/* Dumps a frontend's line to the trash for the current proxy <px> and uses
1451 * the state from stream interface <si>. The caller is responsible for clearing
1452 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
1453 */
1454static int stats_dump_fe_stats(struct stream_interface *si, struct proxy *px)
1455{
1456 struct appctx *appctx = __objt_appctx(si->end);
1457
1458 if (!(px->cap & PR_CAP_FE))
1459 return 0;
1460
1461 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_FE)))
1462 return 0;
1463
1464 if (!stats_fill_fe_stats(px, stats, ST_F_TOTAL_FIELDS))
1465 return 0;
1466
1467 return stats_dump_one_line(stats, 0, px, appctx);
1468}
1469
1470/* Fill <stats> with the listener statistics. <stats> is
1471 * preallocated array of length <len>. The length of the array
1472 * must be at least ST_F_TOTAL_FIELDS. If this length is less
1473 * then this value, the function returns 0, otherwise, it
1474 * returns 1. <flags> can take the value ST_SHLGNDS.
1475 */
1476int stats_fill_li_stats(struct proxy *px, struct listener *l, int flags,
1477 struct field *stats, int len)
1478{
Willy Tarreau83061a82018-07-13 11:56:34 +02001479 struct buffer *out = get_trash_chunk();
William Lallemand74c24fb2016-11-21 17:18:36 +01001480
1481 if (len < ST_F_TOTAL_FIELDS)
1482 return 0;
1483
1484 if (!l->counters)
1485 return 0;
1486
1487 chunk_reset(out);
1488 memset(stats, 0, sizeof(*stats) * len);
1489
1490 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1491 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, l->name);
1492 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1493 stats[ST_F_SCUR] = mkf_u32(0, l->nbconn);
1494 stats[ST_F_SMAX] = mkf_u32(FN_MAX, l->counters->conn_max);
1495 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, l->maxconn);
1496 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, l->counters->cum_conn);
1497 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, l->counters->bytes_in);
1498 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, l->counters->bytes_out);
1499 stats[ST_F_DREQ] = mkf_u64(FN_COUNTER, l->counters->denied_req);
1500 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, l->counters->denied_resp);
1501 stats[ST_F_EREQ] = mkf_u64(FN_COUNTER, l->counters->failed_req);
1502 stats[ST_F_DCON] = mkf_u64(FN_COUNTER, l->counters->denied_conn);
1503 stats[ST_F_DSES] = mkf_u64(FN_COUNTER, l->counters->denied_sess);
Willy Tarreaua8cf66b2019-02-27 16:49:00 +01001504 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 +01001505 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
1506 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1507 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, l->luid);
1508 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_SO);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001509 stats[ST_F_WREW] = mkf_u64(FN_COUNTER, l->counters->failed_rewrites);
William Lallemand74c24fb2016-11-21 17:18:36 +01001510
1511 if (flags & ST_SHLGNDS) {
1512 char str[INET6_ADDRSTRLEN];
1513 int port;
1514
1515 port = get_host_port(&l->addr);
1516 switch (addr_to_str(&l->addr, str, sizeof(str))) {
1517 case AF_INET:
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_INET6:
1522 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1523 chunk_appendf(out, "[%s]:%d", str, port);
1524 break;
1525 case AF_UNIX:
1526 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, "unix");
1527 break;
1528 case -1:
1529 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1530 chunk_strcat(out, strerror(errno));
1531 break;
1532 default: /* address family not supported */
1533 break;
1534 }
1535 }
1536
1537 return 1;
1538}
1539
1540/* Dumps a line for listener <l> and proxy <px> to the trash and uses the state
1541 * from stream interface <si>, and stats flags <flags>. The caller is responsible
1542 * for clearing the trash if needed. Returns non-zero if it emits anything, zero
1543 * otherwise.
1544 */
1545static int stats_dump_li_stats(struct stream_interface *si, struct proxy *px, struct listener *l, int flags)
1546{
1547 struct appctx *appctx = __objt_appctx(si->end);
1548
1549 if (!stats_fill_li_stats(px, l, flags, stats, ST_F_TOTAL_FIELDS))
1550 return 0;
1551
1552 return stats_dump_one_line(stats, flags, px, appctx);
1553}
1554
1555enum srv_stats_state {
1556 SRV_STATS_STATE_DOWN = 0,
1557 SRV_STATS_STATE_DOWN_AGENT,
1558 SRV_STATS_STATE_GOING_UP,
1559 SRV_STATS_STATE_UP_GOING_DOWN,
1560 SRV_STATS_STATE_UP,
1561 SRV_STATS_STATE_NOLB_GOING_DOWN,
1562 SRV_STATS_STATE_NOLB,
1563 SRV_STATS_STATE_DRAIN_GOING_DOWN,
1564 SRV_STATS_STATE_DRAIN,
1565 SRV_STATS_STATE_DRAIN_AGENT,
1566 SRV_STATS_STATE_NO_CHECK,
1567
1568 SRV_STATS_STATE_COUNT, /* Must be last */
1569};
1570
1571static const char *srv_hlt_st[SRV_STATS_STATE_COUNT] = {
1572 [SRV_STATS_STATE_DOWN] = "DOWN",
1573 [SRV_STATS_STATE_DOWN_AGENT] = "DOWN (agent)",
1574 [SRV_STATS_STATE_GOING_UP] = "DOWN %d/%d",
1575 [SRV_STATS_STATE_UP_GOING_DOWN] = "UP %d/%d",
1576 [SRV_STATS_STATE_UP] = "UP",
1577 [SRV_STATS_STATE_NOLB_GOING_DOWN] = "NOLB %d/%d",
1578 [SRV_STATS_STATE_NOLB] = "NOLB",
1579 [SRV_STATS_STATE_DRAIN_GOING_DOWN] = "DRAIN %d/%d",
1580 [SRV_STATS_STATE_DRAIN] = "DRAIN",
1581 [SRV_STATS_STATE_DRAIN_AGENT] = "DRAIN (agent)",
1582 [SRV_STATS_STATE_NO_CHECK] = "no check"
1583};
1584
1585/* Fill <stats> with the server statistics. <stats> is
1586 * preallocated array of length <len>. The length of the array
1587 * must be at least ST_F_TOTAL_FIELDS. If this length is less
1588 * then this value, the function returns 0, otherwise, it
1589 * returns 1. <flags> can take the value ST_SHLGNDS.
1590 */
1591int stats_fill_sv_stats(struct proxy *px, struct server *sv, int flags,
1592 struct field *stats, int len)
1593{
1594 struct server *via, *ref;
1595 char str[INET6_ADDRSTRLEN];
Willy Tarreau83061a82018-07-13 11:56:34 +02001596 struct buffer *out = get_trash_chunk();
William Lallemand74c24fb2016-11-21 17:18:36 +01001597 enum srv_stats_state state;
1598 char *fld_status;
1599
1600 if (len < ST_F_TOTAL_FIELDS)
1601 return 0;
1602
1603 memset(stats, 0, sizeof(*stats) * len);
1604
1605 /* we have "via" which is the tracked server as described in the configuration,
1606 * and "ref" which is the checked server and the end of the chain.
1607 */
1608 via = sv->track ? sv->track : sv;
1609 ref = via;
1610 while (ref->track)
1611 ref = ref->track;
1612
Emeric Brun52a91d32017-08-31 14:41:55 +02001613 if (sv->cur_state == SRV_ST_RUNNING || sv->cur_state == SRV_ST_STARTING) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001614 if ((ref->check.state & CHK_ST_ENABLED) &&
1615 (ref->check.health < ref->check.rise + ref->check.fall - 1)) {
1616 state = SRV_STATS_STATE_UP_GOING_DOWN;
1617 } else {
1618 state = SRV_STATS_STATE_UP;
1619 }
1620
Emeric Brun52a91d32017-08-31 14:41:55 +02001621 if (sv->cur_admin & SRV_ADMF_DRAIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001622 if (ref->agent.state & CHK_ST_ENABLED)
1623 state = SRV_STATS_STATE_DRAIN_AGENT;
1624 else if (state == SRV_STATS_STATE_UP_GOING_DOWN)
1625 state = SRV_STATS_STATE_DRAIN_GOING_DOWN;
1626 else
1627 state = SRV_STATS_STATE_DRAIN;
1628 }
1629
1630 if (state == SRV_STATS_STATE_UP && !(ref->check.state & CHK_ST_ENABLED)) {
1631 state = SRV_STATS_STATE_NO_CHECK;
1632 }
1633 }
Emeric Brun52a91d32017-08-31 14:41:55 +02001634 else if (sv->cur_state == SRV_ST_STOPPING) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001635 if ((!(sv->check.state & CHK_ST_ENABLED) && !sv->track) ||
1636 (ref->check.health == ref->check.rise + ref->check.fall - 1)) {
1637 state = SRV_STATS_STATE_NOLB;
1638 } else {
1639 state = SRV_STATS_STATE_NOLB_GOING_DOWN;
1640 }
1641 }
1642 else { /* stopped */
1643 if ((ref->agent.state & CHK_ST_ENABLED) && !ref->agent.health) {
1644 state = SRV_STATS_STATE_DOWN_AGENT;
1645 } else if ((ref->check.state & CHK_ST_ENABLED) && !ref->check.health) {
1646 state = SRV_STATS_STATE_DOWN; /* DOWN */
1647 } else if ((ref->agent.state & CHK_ST_ENABLED) || (ref->check.state & CHK_ST_ENABLED)) {
1648 state = SRV_STATS_STATE_GOING_UP;
1649 } else {
1650 state = SRV_STATS_STATE_DOWN; /* DOWN, unchecked */
1651 }
1652 }
1653
1654 chunk_reset(out);
1655
1656 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1657 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, sv->id);
1658 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1659 stats[ST_F_QCUR] = mkf_u32(0, sv->nbpend);
1660 stats[ST_F_QMAX] = mkf_u32(FN_MAX, sv->counters.nbpend_max);
1661 stats[ST_F_SCUR] = mkf_u32(0, sv->cur_sess);
1662 stats[ST_F_SMAX] = mkf_u32(FN_MAX, sv->counters.cur_sess_max);
1663
1664 if (sv->maxconn)
1665 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, sv->maxconn);
1666
Willy Tarreau0ff699e2019-09-08 09:24:56 +02001667 stats[ST_F_SRV_ICUR] = mkf_u32(0, sv->curr_idle_conns);
1668 if (sv->max_idle_conns != -1)
1669 stats[ST_F_SRV_ILIM] = mkf_u32(FO_CONFIG|FN_LIMIT, sv->max_idle_conns);
1670
William Lallemand74c24fb2016-11-21 17:18:36 +01001671 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, sv->counters.cum_sess);
1672 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, sv->counters.bytes_in);
1673 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, sv->counters.bytes_out);
1674 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, sv->counters.failed_secu);
1675 stats[ST_F_ECON] = mkf_u64(FN_COUNTER, sv->counters.failed_conns);
1676 stats[ST_F_ERESP] = mkf_u64(FN_COUNTER, sv->counters.failed_resp);
1677 stats[ST_F_WRETR] = mkf_u64(FN_COUNTER, sv->counters.retries);
1678 stats[ST_F_WREDIS] = mkf_u64(FN_COUNTER, sv->counters.redispatches);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001679 stats[ST_F_WREW] = mkf_u64(FN_COUNTER, sv->counters.failed_rewrites);
Willy Tarreauf1573842018-12-14 11:35:36 +01001680 stats[ST_F_CONNECT] = mkf_u64(FN_COUNTER, sv->counters.connect);
1681 stats[ST_F_REUSE] = mkf_u64(FN_COUNTER, sv->counters.reuse);
William Lallemand74c24fb2016-11-21 17:18:36 +01001682
1683 /* status */
1684 fld_status = chunk_newstr(out);
Emeric Brun52a91d32017-08-31 14:41:55 +02001685 if (sv->cur_admin & SRV_ADMF_RMAINT)
William Lallemand74c24fb2016-11-21 17:18:36 +01001686 chunk_appendf(out, "MAINT (resolution)");
Emeric Brun52a91d32017-08-31 14:41:55 +02001687 else if (sv->cur_admin & SRV_ADMF_IMAINT)
William Lallemand74c24fb2016-11-21 17:18:36 +01001688 chunk_appendf(out, "MAINT (via %s/%s)", via->proxy->id, via->id);
Emeric Brun52a91d32017-08-31 14:41:55 +02001689 else if (sv->cur_admin & SRV_ADMF_MAINT)
William Lallemand74c24fb2016-11-21 17:18:36 +01001690 chunk_appendf(out, "MAINT");
1691 else
1692 chunk_appendf(out,
1693 srv_hlt_st[state],
Emeric Brun52a91d32017-08-31 14:41:55 +02001694 (ref->cur_state != SRV_ST_STOPPED) ? (ref->check.health - ref->check.rise + 1) : (ref->check.health),
1695 (ref->cur_state != SRV_ST_STOPPED) ? (ref->check.fall) : (ref->check.rise));
William Lallemand74c24fb2016-11-21 17:18:36 +01001696
1697 stats[ST_F_STATUS] = mkf_str(FO_STATUS, fld_status);
1698 stats[ST_F_LASTCHG] = mkf_u32(FN_AGE, now.tv_sec - sv->last_change);
Emeric Brun52a91d32017-08-31 14:41:55 +02001699 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 +01001700 stats[ST_F_ACT] = mkf_u32(FO_STATUS, (sv->flags & SRV_F_BACKUP) ? 0 : 1);
1701 stats[ST_F_BCK] = mkf_u32(FO_STATUS, (sv->flags & SRV_F_BACKUP) ? 1 : 0);
1702
1703 /* check failures: unique, fatal; last change, total downtime */
1704 if (sv->check.state & CHK_ST_ENABLED) {
1705 stats[ST_F_CHKFAIL] = mkf_u64(FN_COUNTER, sv->counters.failed_checks);
1706 stats[ST_F_CHKDOWN] = mkf_u64(FN_COUNTER, sv->counters.down_trans);
1707 stats[ST_F_DOWNTIME] = mkf_u32(FN_COUNTER, srv_downtime(sv));
1708 }
1709
1710 if (sv->maxqueue)
1711 stats[ST_F_QLIMIT] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->maxqueue);
1712
1713 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
1714 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1715 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, sv->puid);
1716
Emeric Brun52a91d32017-08-31 14:41:55 +02001717 if (sv->cur_state == SRV_ST_STARTING && !server_is_draining(sv))
William Lallemand74c24fb2016-11-21 17:18:36 +01001718 stats[ST_F_THROTTLE] = mkf_u32(FN_AVG, server_throttle_rate(sv));
1719
1720 stats[ST_F_LBTOT] = mkf_u64(FN_COUNTER, sv->counters.cum_lbconn);
1721
1722 if (sv->track) {
1723 char *fld_track = chunk_newstr(out);
1724
1725 chunk_appendf(out, "%s/%s", sv->track->proxy->id, sv->track->id);
1726 stats[ST_F_TRACKED] = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, fld_track);
1727 }
1728
1729 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_SV);
1730 stats[ST_F_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&sv->sess_per_sec));
1731 stats[ST_F_RATE_MAX] = mkf_u32(FN_MAX, sv->counters.sps_max);
1732
1733 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) {
1734 const char *fld_chksts;
1735
1736 fld_chksts = chunk_newstr(out);
1737 chunk_strcat(out, "* "); // for check in progress
1738 chunk_strcat(out, get_check_status_info(sv->check.status));
1739 if (!(sv->check.state & CHK_ST_INPROGRESS))
1740 fld_chksts += 2; // skip "* "
1741 stats[ST_F_CHECK_STATUS] = mkf_str(FN_OUTPUT, fld_chksts);
1742
1743 if (sv->check.status >= HCHK_STATUS_L57DATA)
1744 stats[ST_F_CHECK_CODE] = mkf_u32(FN_OUTPUT, sv->check.code);
1745
1746 if (sv->check.status >= HCHK_STATUS_CHECKED)
1747 stats[ST_F_CHECK_DURATION] = mkf_u64(FN_DURATION, sv->check.duration);
1748
1749 stats[ST_F_CHECK_DESC] = mkf_str(FN_OUTPUT, get_check_status_description(sv->check.status));
1750 stats[ST_F_LAST_CHK] = mkf_str(FN_OUTPUT, sv->check.desc);
1751 stats[ST_F_CHECK_RISE] = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.rise);
1752 stats[ST_F_CHECK_FALL] = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.fall);
1753 stats[ST_F_CHECK_HEALTH] = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.health);
1754 }
1755
1756 if ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) {
1757 const char *fld_chksts;
1758
1759 fld_chksts = chunk_newstr(out);
1760 chunk_strcat(out, "* "); // for check in progress
1761 chunk_strcat(out, get_check_status_info(sv->agent.status));
1762 if (!(sv->agent.state & CHK_ST_INPROGRESS))
1763 fld_chksts += 2; // skip "* "
1764 stats[ST_F_AGENT_STATUS] = mkf_str(FN_OUTPUT, fld_chksts);
1765
1766 if (sv->agent.status >= HCHK_STATUS_L57DATA)
1767 stats[ST_F_AGENT_CODE] = mkf_u32(FN_OUTPUT, sv->agent.code);
1768
1769 if (sv->agent.status >= HCHK_STATUS_CHECKED)
1770 stats[ST_F_AGENT_DURATION] = mkf_u64(FN_DURATION, sv->agent.duration);
1771
1772 stats[ST_F_AGENT_DESC] = mkf_str(FN_OUTPUT, get_check_status_description(sv->agent.status));
1773 stats[ST_F_LAST_AGT] = mkf_str(FN_OUTPUT, sv->agent.desc);
1774 stats[ST_F_AGENT_RISE] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.rise);
1775 stats[ST_F_AGENT_FALL] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.fall);
1776 stats[ST_F_AGENT_HEALTH] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.health);
1777 }
1778
1779 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
1780 if (px->mode == PR_MODE_HTTP) {
1781 stats[ST_F_HRSP_1XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[1]);
1782 stats[ST_F_HRSP_2XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[2]);
1783 stats[ST_F_HRSP_3XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[3]);
1784 stats[ST_F_HRSP_4XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[4]);
1785 stats[ST_F_HRSP_5XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[5]);
1786 stats[ST_F_HRSP_OTHER] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[0]);
1787 }
1788
1789 if (ref->observe)
1790 stats[ST_F_HANAFAIL] = mkf_u64(FN_COUNTER, sv->counters.failed_hana);
1791
1792 stats[ST_F_CLI_ABRT] = mkf_u64(FN_COUNTER, sv->counters.cli_aborts);
1793 stats[ST_F_SRV_ABRT] = mkf_u64(FN_COUNTER, sv->counters.srv_aborts);
1794 stats[ST_F_LASTSESS] = mkf_s32(FN_AGE, srv_lastsession(sv));
1795
1796 stats[ST_F_QTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.q_time, TIME_STATS_SAMPLES));
1797 stats[ST_F_CTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.c_time, TIME_STATS_SAMPLES));
1798 stats[ST_F_RTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.d_time, TIME_STATS_SAMPLES));
1799 stats[ST_F_TTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.t_time, TIME_STATS_SAMPLES));
1800
1801 if (flags & ST_SHLGNDS) {
1802 switch (addr_to_str(&sv->addr, str, sizeof(str))) {
1803 case AF_INET:
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_INET6:
1808 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
Willy Tarreau04276f32017-01-06 17:41:29 +01001809 chunk_appendf(out, "[%s]:%d", str, sv->svc_port);
William Lallemand74c24fb2016-11-21 17:18:36 +01001810 break;
1811 case AF_UNIX:
1812 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, "unix");
1813 break;
1814 case -1:
1815 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1816 chunk_strcat(out, strerror(errno));
1817 break;
1818 default: /* address family not supported */
1819 break;
1820 }
1821
1822 if (sv->cookie)
1823 stats[ST_F_COOKIE] = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, sv->cookie);
1824 }
1825
1826 return 1;
1827}
1828
1829/* Dumps a line for server <sv> and proxy <px> to the trash and uses the state
1830 * from stream interface <si>, stats flags <flags>, and server state <state>.
1831 * The caller is responsible for clearing the trash if needed. Returns non-zero
1832 * if it emits anything, zero otherwise.
1833 */
1834static int stats_dump_sv_stats(struct stream_interface *si, struct proxy *px, int flags, struct server *sv)
1835{
1836 struct appctx *appctx = __objt_appctx(si->end);
1837
1838 if (!stats_fill_sv_stats(px, sv, flags, stats, ST_F_TOTAL_FIELDS))
1839 return 0;
1840
1841 return stats_dump_one_line(stats, flags, px, appctx);
1842}
1843
1844/* Fill <stats> with the backend statistics. <stats> is
1845 * preallocated array of length <len>. The length of the array
1846 * must be at least ST_F_TOTAL_FIELDS. If this length is less
1847 * then this value, the function returns 0, otherwise, it
1848 * returns 1. <flags> can take the value ST_SHLGNDS.
1849 */
1850int stats_fill_be_stats(struct proxy *px, int flags, struct field *stats, int len)
1851{
1852 if (len < ST_F_TOTAL_FIELDS)
1853 return 0;
1854
1855 memset(stats, 0, sizeof(*stats) * len);
1856
1857 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1858 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, "BACKEND");
1859 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1860 stats[ST_F_QCUR] = mkf_u32(0, px->nbpend);
1861 stats[ST_F_QMAX] = mkf_u32(FN_MAX, px->be_counters.nbpend_max);
Thierry FOURNIER0ff98a42016-12-19 16:50:42 +01001862 stats[ST_F_SCUR] = mkf_u32(0, px->beconn);
William Lallemand74c24fb2016-11-21 17:18:36 +01001863 stats[ST_F_SMAX] = mkf_u32(FN_MAX, px->be_counters.conn_max);
1864 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, px->fullconn);
1865 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, px->be_counters.cum_conn);
1866 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, px->be_counters.bytes_in);
1867 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, px->be_counters.bytes_out);
1868 stats[ST_F_DREQ] = mkf_u64(FN_COUNTER, px->be_counters.denied_req);
1869 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, px->be_counters.denied_resp);
1870 stats[ST_F_ECON] = mkf_u64(FN_COUNTER, px->be_counters.failed_conns);
1871 stats[ST_F_ERESP] = mkf_u64(FN_COUNTER, px->be_counters.failed_resp);
1872 stats[ST_F_WRETR] = mkf_u64(FN_COUNTER, px->be_counters.retries);
1873 stats[ST_F_WREDIS] = mkf_u64(FN_COUNTER, px->be_counters.redispatches);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001874 stats[ST_F_WREW] = mkf_u64(FN_COUNTER, px->be_counters.failed_rewrites);
Willy Tarreauf1573842018-12-14 11:35:36 +01001875 stats[ST_F_CONNECT] = mkf_u64(FN_COUNTER, px->be_counters.connect);
1876 stats[ST_F_REUSE] = mkf_u64(FN_COUNTER, px->be_counters.reuse);
William Lallemand74c24fb2016-11-21 17:18:36 +01001877 stats[ST_F_STATUS] = mkf_str(FO_STATUS, (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" : "DOWN");
1878 stats[ST_F_WEIGHT] = mkf_u32(FN_AVG, (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv);
1879 stats[ST_F_ACT] = mkf_u32(0, px->srv_act);
1880 stats[ST_F_BCK] = mkf_u32(0, px->srv_bck);
1881 stats[ST_F_CHKDOWN] = mkf_u64(FN_COUNTER, px->down_trans);
1882 stats[ST_F_LASTCHG] = mkf_u32(FN_AGE, now.tv_sec - px->last_change);
1883 if (px->srv)
1884 stats[ST_F_DOWNTIME] = mkf_u32(FN_COUNTER, be_downtime(px));
1885
1886 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
1887 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1888 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, 0);
1889 stats[ST_F_LBTOT] = mkf_u64(FN_COUNTER, px->be_counters.cum_lbconn);
1890 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_BE);
1891 stats[ST_F_RATE] = mkf_u32(0, read_freq_ctr(&px->be_sess_per_sec));
1892 stats[ST_F_RATE_MAX] = mkf_u32(0, px->be_counters.sps_max);
1893
1894 if (flags & ST_SHLGNDS) {
1895 if (px->cookie_name)
1896 stats[ST_F_COOKIE] = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, px->cookie_name);
1897 stats[ST_F_ALGO] = mkf_str(FO_CONFIG|FS_SERVICE, backend_lb_algo_str(px->lbprm.algo & BE_LB_ALGO));
1898 }
1899
1900 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
1901 if (px->mode == PR_MODE_HTTP) {
1902 stats[ST_F_REQ_TOT] = mkf_u64(FN_COUNTER, px->be_counters.p.http.cum_req);
1903 stats[ST_F_HRSP_1XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[1]);
1904 stats[ST_F_HRSP_2XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[2]);
1905 stats[ST_F_HRSP_3XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[3]);
1906 stats[ST_F_HRSP_4XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[4]);
1907 stats[ST_F_HRSP_5XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[5]);
1908 stats[ST_F_HRSP_OTHER] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[0]);
Willy Tarreaua1214a52018-12-14 14:00:25 +01001909 stats[ST_F_CACHE_LOOKUPS] = mkf_u64(FN_COUNTER, px->be_counters.p.http.cache_lookups);
1910 stats[ST_F_CACHE_HITS] = mkf_u64(FN_COUNTER, px->be_counters.p.http.cache_hits);
William Lallemand74c24fb2016-11-21 17:18:36 +01001911 }
1912
1913 stats[ST_F_CLI_ABRT] = mkf_u64(FN_COUNTER, px->be_counters.cli_aborts);
1914 stats[ST_F_SRV_ABRT] = mkf_u64(FN_COUNTER, px->be_counters.srv_aborts);
1915
1916 /* compression: in, out, bypassed, responses */
1917 stats[ST_F_COMP_IN] = mkf_u64(FN_COUNTER, px->be_counters.comp_in);
1918 stats[ST_F_COMP_OUT] = mkf_u64(FN_COUNTER, px->be_counters.comp_out);
1919 stats[ST_F_COMP_BYP] = mkf_u64(FN_COUNTER, px->be_counters.comp_byp);
1920 stats[ST_F_COMP_RSP] = mkf_u64(FN_COUNTER, px->be_counters.p.http.comp_rsp);
1921 stats[ST_F_LASTSESS] = mkf_s32(FN_AGE, be_lastsession(px));
1922
1923 stats[ST_F_QTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.q_time, TIME_STATS_SAMPLES));
1924 stats[ST_F_CTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.c_time, TIME_STATS_SAMPLES));
1925 stats[ST_F_RTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.d_time, TIME_STATS_SAMPLES));
1926 stats[ST_F_TTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.t_time, TIME_STATS_SAMPLES));
1927
1928 return 1;
1929}
1930
1931/* Dumps a line for backend <px> to the trash for and uses the state from stream
1932 * interface <si> and stats flags <flags>. The caller is responsible for clearing
1933 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
1934 */
1935static int stats_dump_be_stats(struct stream_interface *si, struct proxy *px, int flags)
1936{
1937 struct appctx *appctx = __objt_appctx(si->end);
1938
1939 if (!(px->cap & PR_CAP_BE))
1940 return 0;
1941
1942 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_BE)))
1943 return 0;
1944
1945 if (!stats_fill_be_stats(px, flags, stats, ST_F_TOTAL_FIELDS))
1946 return 0;
1947
1948 return stats_dump_one_line(stats, flags, px, appctx);
1949}
1950
1951/* Dumps the HTML table header for proxy <px> to the trash for and uses the state from
1952 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
1953 * for clearing the trash if needed.
1954 */
1955static void stats_dump_html_px_hdr(struct stream_interface *si, struct proxy *px, struct uri_auth *uri)
1956{
1957 struct appctx *appctx = __objt_appctx(si->end);
1958 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
1959
1960 if (px->cap & PR_CAP_BE && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
1961 /* A form to enable/disable this proxy servers */
1962
1963 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
1964 scope_txt[0] = 0;
1965 if (appctx->ctx.stats.scope_len) {
Christopher Fauleted7a0662019-01-14 11:07:34 +01001966 const char *scope_ptr = stats_scope_ptr(appctx, si);
1967
William Lallemand74c24fb2016-11-21 17:18:36 +01001968 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Christopher Fauleted7a0662019-01-14 11:07:34 +01001969 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), scope_ptr, appctx->ctx.stats.scope_len);
William Lallemand74c24fb2016-11-21 17:18:36 +01001970 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
1971 }
1972
1973 chunk_appendf(&trash,
1974 "<form method=\"post\">");
1975 }
1976
1977 /* print a new table */
1978 chunk_appendf(&trash,
1979 "<table class=\"tbl\" width=\"100%%\">\n"
1980 "<tr class=\"titre\">"
1981 "<th class=\"pxname\" width=\"10%%\">");
1982
1983 chunk_appendf(&trash,
1984 "<a name=\"%s\"></a>%s"
1985 "<a class=px href=\"#%s\">%s</a>",
1986 px->id,
1987 (uri->flags & ST_SHLGNDS) ? "<u>":"",
1988 px->id, px->id);
1989
1990 if (uri->flags & ST_SHLGNDS) {
1991 /* cap, mode, id */
1992 chunk_appendf(&trash, "<div class=tips>cap: %s, mode: %s, id: %d",
1993 proxy_cap_str(px->cap), proxy_mode_str(px->mode),
1994 px->uuid);
1995 chunk_appendf(&trash, "</div>");
1996 }
1997
1998 chunk_appendf(&trash,
1999 "%s</th>"
2000 "<th class=\"%s\" width=\"90%%\">%s</th>"
2001 "</tr>\n"
2002 "</table>\n"
2003 "<table class=\"tbl\" width=\"100%%\">\n"
2004 "<tr class=\"titre\">",
2005 (uri->flags & ST_SHLGNDS) ? "</u>":"",
2006 px->desc ? "desc" : "empty", px->desc ? px->desc : "");
2007
2008 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
2009 /* Column heading for Enable or Disable server */
David Harrigand3db35a2016-12-30 12:12:49 +00002010 chunk_appendf(&trash,
2011 "<th rowspan=2 width=1><input type=\"checkbox\" \
2012 onclick=\"for(c in document.getElementsByClassName('%s-checkbox')) \
2013 document.getElementsByClassName('%s-checkbox').item(c).checked = this.checked\"></th>",
2014 px->id,
2015 px->id);
William Lallemand74c24fb2016-11-21 17:18:36 +01002016 }
2017
2018 chunk_appendf(&trash,
2019 "<th rowspan=2></th>"
2020 "<th colspan=3>Queue</th>"
2021 "<th colspan=3>Session rate</th><th colspan=6>Sessions</th>"
2022 "<th colspan=2>Bytes</th><th colspan=2>Denied</th>"
2023 "<th colspan=3>Errors</th><th colspan=2>Warnings</th>"
2024 "<th colspan=9>Server</th>"
2025 "</tr>\n"
2026 "<tr class=\"titre\">"
2027 "<th>Cur</th><th>Max</th><th>Limit</th>"
2028 "<th>Cur</th><th>Max</th><th>Limit</th><th>Cur</th><th>Max</th>"
2029 "<th>Limit</th><th>Total</th><th>LbTot</th><th>Last</th><th>In</th><th>Out</th>"
2030 "<th>Req</th><th>Resp</th><th>Req</th><th>Conn</th>"
2031 "<th>Resp</th><th>Retr</th><th>Redis</th>"
2032 "<th>Status</th><th>LastChk</th><th>Wght</th><th>Act</th>"
2033 "<th>Bck</th><th>Chk</th><th>Dwn</th><th>Dwntme</th>"
2034 "<th>Thrtle</th>\n"
2035 "</tr>");
2036}
2037
2038/* Dumps the HTML table trailer for proxy <px> to the trash for and uses the state from
2039 * stream interface <si>. The caller is responsible for clearing the trash if needed.
2040 */
2041static void stats_dump_html_px_end(struct stream_interface *si, struct proxy *px)
2042{
2043 struct appctx *appctx = __objt_appctx(si->end);
2044 chunk_appendf(&trash, "</table>");
2045
2046 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
2047 /* close the form used to enable/disable this proxy servers */
2048 chunk_appendf(&trash,
2049 "Choose the action to perform on the checked servers : "
2050 "<select name=action>"
2051 "<option value=\"\"></option>"
2052 "<option value=\"ready\">Set state to READY</option>"
2053 "<option value=\"drain\">Set state to DRAIN</option>"
2054 "<option value=\"maint\">Set state to MAINT</option>"
2055 "<option value=\"dhlth\">Health: disable checks</option>"
2056 "<option value=\"ehlth\">Health: enable checks</option>"
2057 "<option value=\"hrunn\">Health: force UP</option>"
2058 "<option value=\"hnolb\">Health: force NOLB</option>"
2059 "<option value=\"hdown\">Health: force DOWN</option>"
2060 "<option value=\"dagent\">Agent: disable checks</option>"
2061 "<option value=\"eagent\">Agent: enable checks</option>"
2062 "<option value=\"arunn\">Agent: force UP</option>"
2063 "<option value=\"adown\">Agent: force DOWN</option>"
2064 "<option value=\"shutdown\">Kill Sessions</option>"
2065 "</select>"
2066 "<input type=\"hidden\" name=\"b\" value=\"#%d\">"
2067 "&nbsp;<input type=\"submit\" value=\"Apply\">"
2068 "</form>",
2069 px->uuid);
2070 }
2071
2072 chunk_appendf(&trash, "<p>\n");
2073}
2074
2075/*
2076 * Dumps statistics for a proxy. The output is sent to the stream interface's
2077 * input buffer. Returns 0 if it had to stop dumping data because of lack of
2078 * buffer space, or non-zero if everything completed. This function is used
2079 * both by the CLI and the HTTP entry points, and is able to dump the output
2080 * in HTML or CSV formats. If the later, <uri> must be NULL.
2081 */
Christopher Fauletef779222018-10-31 08:47:01 +01002082int stats_dump_proxy_to_buffer(struct stream_interface *si, struct htx *htx,
2083 struct proxy *px, struct uri_auth *uri)
William Lallemand74c24fb2016-11-21 17:18:36 +01002084{
2085 struct appctx *appctx = __objt_appctx(si->end);
2086 struct stream *s = si_strm(si);
2087 struct channel *rep = si_ic(si);
2088 struct server *sv, *svs; /* server and server-state, server-state=server or server->track */
2089 struct listener *l;
2090 unsigned int flags;
2091
2092 if (uri)
2093 flags = uri->flags;
William Lallemand07a62f72017-05-24 00:57:40 +02002094 else if ((strm_li(s)->bind_conf->level & ACCESS_LVL_MASK) >= ACCESS_LVL_OPER)
William Lallemand74c24fb2016-11-21 17:18:36 +01002095 flags = ST_SHLGNDS | ST_SHNODE | ST_SHDESC;
2096 else
2097 flags = ST_SHNODE | ST_SHDESC;
2098
2099 chunk_reset(&trash);
2100
2101 switch (appctx->ctx.stats.px_st) {
2102 case STAT_PX_ST_INIT:
2103 /* we are on a new proxy */
2104 if (uri && uri->scope) {
2105 /* we have a limited scope, we have to check the proxy name */
2106 struct stat_scope *scope;
2107 int len;
2108
2109 len = strlen(px->id);
2110 scope = uri->scope;
2111
2112 while (scope) {
2113 /* match exact proxy name */
2114 if (scope->px_len == len && !memcmp(px->id, scope->px_id, len))
2115 break;
2116
2117 /* match '.' which means 'self' proxy */
2118 if (!strcmp(scope->px_id, ".") && px == s->be)
2119 break;
2120 scope = scope->next;
2121 }
2122
2123 /* proxy name not found : don't dump anything */
2124 if (scope == NULL)
2125 return 1;
2126 }
2127
2128 /* if the user has requested a limited output and the proxy
2129 * name does not match, skip it.
2130 */
Christopher Fauleted7a0662019-01-14 11:07:34 +01002131 if (appctx->ctx.stats.scope_len) {
2132 const char *scope_ptr = stats_scope_ptr(appctx, si);
2133
2134 if (strnistr(px->id, strlen(px->id), scope_ptr, appctx->ctx.stats.scope_len) == NULL)
2135 return 1;
2136 }
William Lallemand74c24fb2016-11-21 17:18:36 +01002137
2138 if ((appctx->ctx.stats.flags & STAT_BOUND) &&
2139 (appctx->ctx.stats.iid != -1) &&
2140 (px->uuid != appctx->ctx.stats.iid))
2141 return 1;
2142
2143 appctx->ctx.stats.px_st = STAT_PX_ST_TH;
2144 /* fall through */
2145
2146 case STAT_PX_ST_TH:
2147 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
2148 stats_dump_html_px_hdr(si, px, uri);
Christopher Fauletef779222018-10-31 08:47:01 +01002149 if (!stats_putchk(rep, htx, &trash))
2150 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002151 }
2152
2153 appctx->ctx.stats.px_st = STAT_PX_ST_FE;
2154 /* fall through */
2155
2156 case STAT_PX_ST_FE:
2157 /* print the frontend */
2158 if (stats_dump_fe_stats(si, px)) {
Christopher Fauletef779222018-10-31 08:47:01 +01002159 if (!stats_putchk(rep, htx, &trash))
2160 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002161 }
2162
2163 appctx->ctx.stats.l = px->conf.listeners.n;
2164 appctx->ctx.stats.px_st = STAT_PX_ST_LI;
2165 /* fall through */
2166
2167 case STAT_PX_ST_LI:
2168 /* stats.l has been initialized above */
2169 for (; appctx->ctx.stats.l != &px->conf.listeners; appctx->ctx.stats.l = l->by_fe.n) {
Christopher Fauletef779222018-10-31 08:47:01 +01002170 if (htx) {
2171 if (htx_almost_full(htx))
2172 goto full;
2173 }
2174 else {
2175 if (buffer_almost_full(&rep->buf))
2176 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002177 }
2178
2179 l = LIST_ELEM(appctx->ctx.stats.l, struct listener *, by_fe);
2180 if (!l->counters)
2181 continue;
2182
2183 if (appctx->ctx.stats.flags & STAT_BOUND) {
2184 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SO)))
2185 break;
2186
2187 if (appctx->ctx.stats.sid != -1 && l->luid != appctx->ctx.stats.sid)
2188 continue;
2189 }
2190
2191 /* print the frontend */
2192 if (stats_dump_li_stats(si, px, l, flags)) {
Christopher Fauletef779222018-10-31 08:47:01 +01002193 if (!stats_putchk(rep, htx, &trash))
2194 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002195 }
2196 }
2197
2198 appctx->ctx.stats.sv = px->srv; /* may be NULL */
2199 appctx->ctx.stats.px_st = STAT_PX_ST_SV;
2200 /* fall through */
2201
2202 case STAT_PX_ST_SV:
2203 /* stats.sv has been initialized above */
2204 for (; appctx->ctx.stats.sv != NULL; appctx->ctx.stats.sv = sv->next) {
Christopher Fauletef779222018-10-31 08:47:01 +01002205 if (htx) {
2206 if (htx_almost_full(htx))
2207 goto full;
2208 }
2209 else {
2210 if (buffer_almost_full(&rep->buf))
2211 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002212 }
2213
2214 sv = appctx->ctx.stats.sv;
2215
2216 if (appctx->ctx.stats.flags & STAT_BOUND) {
2217 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SV)))
2218 break;
2219
2220 if (appctx->ctx.stats.sid != -1 && sv->puid != appctx->ctx.stats.sid)
2221 continue;
2222 }
2223
2224 svs = sv;
2225 while (svs->track)
2226 svs = svs->track;
2227
2228 /* do not report servers which are DOWN and not changing state */
2229 if ((appctx->ctx.stats.flags & STAT_HIDE_DOWN) &&
Emeric Brun52a91d32017-08-31 14:41:55 +02002230 ((sv->cur_admin & SRV_ADMF_MAINT) || /* server is in maintenance */
2231 (sv->cur_state == SRV_ST_STOPPED && /* server is down */
William Lallemand74c24fb2016-11-21 17:18:36 +01002232 (!((svs->agent.state | svs->check.state) & CHK_ST_ENABLED) ||
2233 ((svs->agent.state & CHK_ST_ENABLED) && !svs->agent.health) ||
2234 ((svs->check.state & CHK_ST_ENABLED) && !svs->check.health))))) {
2235 continue;
2236 }
2237
2238 if (stats_dump_sv_stats(si, px, flags, sv)) {
Christopher Fauletef779222018-10-31 08:47:01 +01002239 if (!stats_putchk(rep, htx, &trash))
2240 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002241 }
2242 } /* for sv */
2243
2244 appctx->ctx.stats.px_st = STAT_PX_ST_BE;
2245 /* fall through */
2246
2247 case STAT_PX_ST_BE:
2248 /* print the backend */
2249 if (stats_dump_be_stats(si, px, flags)) {
Christopher Fauletef779222018-10-31 08:47:01 +01002250 if (!stats_putchk(rep, htx, &trash))
2251 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002252 }
2253
2254 appctx->ctx.stats.px_st = STAT_PX_ST_END;
2255 /* fall through */
2256
2257 case STAT_PX_ST_END:
2258 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
2259 stats_dump_html_px_end(si, px);
Christopher Fauletef779222018-10-31 08:47:01 +01002260 if (!stats_putchk(rep, htx, &trash))
2261 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002262 }
2263
2264 appctx->ctx.stats.px_st = STAT_PX_ST_FIN;
2265 /* fall through */
2266
2267 case STAT_PX_ST_FIN:
2268 return 1;
2269
2270 default:
2271 /* unknown state, we should put an abort() here ! */
2272 return 1;
2273 }
Christopher Fauletef779222018-10-31 08:47:01 +01002274
2275 full:
2276 si_rx_room_blk(si);
2277 return 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01002278}
2279
2280/* Dumps the HTTP stats head block to the trash for and uses the per-uri
2281 * parameters <uri>. The caller is responsible for clearing the trash if needed.
2282 */
2283static void stats_dump_html_head(struct uri_auth *uri)
2284{
2285 /* WARNING! This must fit in the first buffer !!! */
2286 chunk_appendf(&trash,
2287 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n"
2288 "\"http://www.w3.org/TR/html4/loose.dtd\">\n"
2289 "<html><head><title>Statistics Report for " PRODUCT_NAME "%s%s</title>\n"
2290 "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n"
2291 "<style type=\"text/css\"><!--\n"
2292 "body {"
2293 " font-family: arial, helvetica, sans-serif;"
2294 " font-size: 12px;"
2295 " font-weight: normal;"
2296 " color: black;"
2297 " background: white;"
2298 "}\n"
2299 "th,td {"
2300 " font-size: 10px;"
2301 "}\n"
2302 "h1 {"
2303 " font-size: x-large;"
2304 " margin-bottom: 0.5em;"
2305 "}\n"
2306 "h2 {"
2307 " font-family: helvetica, arial;"
2308 " font-size: x-large;"
2309 " font-weight: bold;"
2310 " font-style: italic;"
2311 " color: #6020a0;"
2312 " margin-top: 0em;"
2313 " margin-bottom: 0em;"
2314 "}\n"
2315 "h3 {"
2316 " font-family: helvetica, arial;"
2317 " font-size: 16px;"
2318 " font-weight: bold;"
2319 " color: #b00040;"
2320 " background: #e8e8d0;"
2321 " margin-top: 0em;"
2322 " margin-bottom: 0em;"
2323 "}\n"
2324 "li {"
2325 " margin-top: 0.25em;"
2326 " margin-right: 2em;"
2327 "}\n"
2328 ".hr {margin-top: 0.25em;"
2329 " border-color: black;"
2330 " border-bottom-style: solid;"
2331 "}\n"
2332 ".titre {background: #20D0D0;color: #000000; font-weight: bold; text-align: center;}\n"
2333 ".total {background: #20D0D0;color: #ffff80;}\n"
2334 ".frontend {background: #e8e8d0;}\n"
2335 ".socket {background: #d0d0d0;}\n"
2336 ".backend {background: #e8e8d0;}\n"
2337 ".active_down {background: #ff9090;}\n"
2338 ".active_going_up {background: #ffd020;}\n"
2339 ".active_going_down {background: #ffffa0;}\n"
2340 ".active_up {background: #c0ffc0;}\n"
2341 ".active_nolb {background: #20a0ff;}\n"
2342 ".active_draining {background: #20a0FF;}\n"
2343 ".active_no_check {background: #e0e0e0;}\n"
2344 ".backup_down {background: #ff9090;}\n"
2345 ".backup_going_up {background: #ff80ff;}\n"
2346 ".backup_going_down {background: #c060ff;}\n"
2347 ".backup_up {background: #b0d0ff;}\n"
2348 ".backup_nolb {background: #90b0e0;}\n"
2349 ".backup_draining {background: #cc9900;}\n"
2350 ".backup_no_check {background: #e0e0e0;}\n"
2351 ".maintain {background: #c07820;}\n"
2352 ".rls {letter-spacing: 0.2em; margin-right: 1px;}\n" /* right letter spacing (used for grouping digits) */
2353 "\n"
2354 "a.px:link {color: #ffff40; text-decoration: none;}"
2355 "a.px:visited {color: #ffff40; text-decoration: none;}"
2356 "a.px:hover {color: #ffffff; text-decoration: none;}"
2357 "a.lfsb:link {color: #000000; text-decoration: none;}"
2358 "a.lfsb:visited {color: #000000; text-decoration: none;}"
2359 "a.lfsb:hover {color: #505050; text-decoration: none;}"
2360 "\n"
2361 "table.tbl { border-collapse: collapse; border-style: none;}\n"
2362 "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"
2363 "table.tbl td.ac { text-align: center;}\n"
2364 "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}\n"
2365 "table.tbl th.pxname { background: #b00040; color: #ffff40; font-weight: bold; border-style: solid solid none solid; padding: 2px 3px; white-space: nowrap;}\n"
2366 "table.tbl th.empty { border-style: none; empty-cells: hide; background: white;}\n"
2367 "table.tbl th.desc { background: white; border-style: solid solid none solid; text-align: left; padding: 2px 3px;}\n"
2368 "\n"
2369 "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n"
2370 "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n"
2371 "table.lgd td.noborder { border-style: none; padding: 2px; white-space: nowrap;}\n"
2372 "table.det { border-collapse: collapse; border-style: none; }\n"
2373 "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"
2374 "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"
2375 "u {text-decoration:none; border-bottom: 1px dotted black;}\n"
2376 "div.tips {\n"
2377 " display:block;\n"
2378 " visibility:hidden;\n"
2379 " z-index:2147483647;\n"
2380 " position:absolute;\n"
2381 " padding:2px 4px 3px;\n"
2382 " background:#f0f060; color:#000000;\n"
2383 " border:1px solid #7040c0;\n"
2384 " white-space:nowrap;\n"
2385 " font-style:normal;font-size:11px;font-weight:normal;\n"
2386 " -moz-border-radius:3px;-webkit-border-radius:3px;border-radius:3px;\n"
2387 " -moz-box-shadow:gray 2px 2px 3px;-webkit-box-shadow:gray 2px 2px 3px;box-shadow:gray 2px 2px 3px;\n"
2388 "}\n"
2389 "u:hover div.tips {visibility:visible;}\n"
2390 "-->\n"
2391 "</style></head>\n",
2392 (uri->flags & ST_SHNODE) ? " on " : "",
2393 (uri->flags & ST_SHNODE) ? (uri->node ? uri->node : global.node) : ""
2394 );
2395}
2396
2397/* Dumps the HTML stats information block to the trash for and uses the state from
2398 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
2399 * for clearing the trash if needed.
2400 */
2401static void stats_dump_html_info(struct stream_interface *si, struct uri_auth *uri)
2402{
2403 struct appctx *appctx = __objt_appctx(si->end);
2404 unsigned int up = (now.tv_sec - start_date.tv_sec);
2405 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
Christopher Fauleted7a0662019-01-14 11:07:34 +01002406 const char *scope_ptr = stats_scope_ptr(appctx, si);
Willy Tarreau1713c032019-05-23 12:23:55 +02002407 unsigned long long bps = (unsigned long long)read_freq_ctr(&global.out_32bps) * 32;
2408
2409 /* Turn the bytes per second to bits per second and take care of the
2410 * usual ethernet overhead in order to help figure how far we are from
2411 * interface saturation since it's the only case which usually matters.
2412 * For this we count the total size of an Ethernet frame on the wire
2413 * including preamble and IFG (1538) for the largest TCP segment it
2414 * transports (1448 with TCP timestamps). This is not valid for smaller
2415 * packets (under-estimated), but it gives a reasonably accurate
2416 * estimation of how far we are from uplink saturation.
2417 */
2418 bps = bps * 8 * 1538 / 1448;
William Lallemand74c24fb2016-11-21 17:18:36 +01002419
2420 /* WARNING! this has to fit the first packet too.
2421 * We are around 3.5 kB, add adding entries will
2422 * become tricky if we want to support 4kB buffers !
2423 */
2424 chunk_appendf(&trash,
2425 "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">"
2426 PRODUCT_NAME "%s</a></h1>\n"
2427 "<h2>Statistics Report for pid %d%s%s%s%s</h2>\n"
2428 "<hr width=\"100%%\" class=\"hr\">\n"
2429 "<h3>&gt; General process information</h3>\n"
2430 "<table border=0><tr><td align=\"left\" nowrap width=\"1%%\">\n"
Yves Lafon95317282018-02-26 11:10:37 +01002431 "<p><b>pid = </b> %d (process #%d, nbproc = %d, nbthread = %d)<br>\n"
William Lallemand74c24fb2016-11-21 17:18:36 +01002432 "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n"
2433 "<b>system limits:</b> memmax = %s%s; ulimit-n = %d<br>\n"
2434 "<b>maxsock = </b> %d; <b>maxconn = </b> %d; <b>maxpipes = </b> %d<br>\n"
Willy Tarreau1713c032019-05-23 12:23:55 +02002435 "current conns = %d; current pipes = %d/%d; conn rate = %d/sec; bit rate = %.3f %cbps<br>\n"
William Lallemand74c24fb2016-11-21 17:18:36 +01002436 "Running tasks: %d/%d; idle = %d %%<br>\n"
2437 "</td><td align=\"center\" nowrap>\n"
2438 "<table class=\"lgd\"><tr>\n"
2439 "<td class=\"active_up\">&nbsp;</td><td class=\"noborder\">active UP </td>"
2440 "<td class=\"backup_up\">&nbsp;</td><td class=\"noborder\">backup UP </td>"
2441 "</tr><tr>\n"
2442 "<td class=\"active_going_down\"></td><td class=\"noborder\">active UP, going down </td>"
2443 "<td class=\"backup_going_down\"></td><td class=\"noborder\">backup UP, going down </td>"
2444 "</tr><tr>\n"
2445 "<td class=\"active_going_up\"></td><td class=\"noborder\">active DOWN, going up </td>"
2446 "<td class=\"backup_going_up\"></td><td class=\"noborder\">backup DOWN, going up </td>"
2447 "</tr><tr>\n"
2448 "<td class=\"active_down\"></td><td class=\"noborder\">active or backup DOWN &nbsp;</td>"
2449 "<td class=\"active_no_check\"></td><td class=\"noborder\">not checked </td>"
2450 "</tr><tr>\n"
2451 "<td class=\"maintain\"></td><td class=\"noborder\" colspan=\"3\">active or backup DOWN for maintenance (MAINT) &nbsp;</td>"
2452 "</tr><tr>\n"
2453 "<td class=\"active_draining\"></td><td class=\"noborder\" colspan=\"3\">active or backup SOFT STOPPED for maintenance &nbsp;</td>"
2454 "</tr></table>\n"
2455 "Note: \"NOLB\"/\"DRAIN\" = UP with load-balancing disabled."
2456 "</td>"
2457 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
2458 "<b>Display option:</b><ul style=\"margin-top: 0.25em;\">"
2459 "",
Willy Tarreau909b9d82019-01-04 18:20:32 +01002460 (uri->flags & ST_HIDEVER) ? "" : (stats_version_string),
William Lallemand74c24fb2016-11-21 17:18:36 +01002461 pid, (uri->flags & ST_SHNODE) ? " on " : "",
2462 (uri->flags & ST_SHNODE) ? (uri->node ? uri->node : global.node) : "",
2463 (uri->flags & ST_SHDESC) ? ": " : "",
2464 (uri->flags & ST_SHDESC) ? (uri->desc ? uri->desc : global.desc) : "",
Yves Lafon95317282018-02-26 11:10:37 +01002465 pid, relative_pid, global.nbproc, global.nbthread,
William Lallemand74c24fb2016-11-21 17:18:36 +01002466 up / 86400, (up % 86400) / 3600,
2467 (up % 3600) / 60, (up % 60),
2468 global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited",
2469 global.rlimit_memmax ? " MB" : "",
2470 global.rlimit_nofile,
2471 global.maxsock, global.maxconn, global.maxpipes,
2472 actconn, pipes_used, pipes_used+pipes_free, read_freq_ctr(&global.conn_per_sec),
Willy Tarreau1713c032019-05-23 12:23:55 +02002473 bps >= 1000000000UL ? (bps / 1000000000.0) : bps >= 1000000UL ? (bps / 1000000.0) : (bps / 1000.0),
2474 bps >= 1000000000UL ? 'G' : bps >= 1000000UL ? 'M' : 'k',
Willy Tarreau81036f22019-05-20 19:24:50 +02002475 tasks_run_queue_cur, nb_tasks_cur, ti->idle_pct
William Lallemand74c24fb2016-11-21 17:18:36 +01002476 );
2477
2478 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Christopher Fauleted7a0662019-01-14 11:07:34 +01002479 memcpy(scope_txt, scope_ptr, appctx->ctx.stats.scope_len);
William Lallemand74c24fb2016-11-21 17:18:36 +01002480 scope_txt[appctx->ctx.stats.scope_len] = '\0';
2481
2482 chunk_appendf(&trash,
2483 "<li><form method=\"GET\">Scope : <input value=\"%s\" name=\"" STAT_SCOPE_INPUT_NAME "\" size=\"8\" maxlength=\"%d\" tabindex=\"1\"/></form>\n",
2484 (appctx->ctx.stats.scope_len > 0) ? scope_txt : "",
2485 STAT_SCOPE_TXT_MAXLEN);
2486
2487 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
2488 scope_txt[0] = 0;
2489 if (appctx->ctx.stats.scope_len) {
2490 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Christopher Fauleted7a0662019-01-14 11:07:34 +01002491 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), scope_ptr, appctx->ctx.stats.scope_len);
William Lallemand74c24fb2016-11-21 17:18:36 +01002492 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
2493 }
2494
2495 if (appctx->ctx.stats.flags & STAT_HIDE_DOWN)
2496 chunk_appendf(&trash,
2497 "<li><a href=\"%s%s%s%s\">Show all servers</a><br>\n",
2498 uri->uri_prefix,
2499 "",
2500 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2501 scope_txt);
2502 else
2503 chunk_appendf(&trash,
2504 "<li><a href=\"%s%s%s%s\">Hide 'DOWN' servers</a><br>\n",
2505 uri->uri_prefix,
2506 ";up",
2507 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2508 scope_txt);
2509
2510 if (uri->refresh > 0) {
2511 if (appctx->ctx.stats.flags & STAT_NO_REFRESH)
2512 chunk_appendf(&trash,
2513 "<li><a href=\"%s%s%s%s\">Enable refresh</a><br>\n",
2514 uri->uri_prefix,
2515 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2516 "",
2517 scope_txt);
2518 else
2519 chunk_appendf(&trash,
2520 "<li><a href=\"%s%s%s%s\">Disable refresh</a><br>\n",
2521 uri->uri_prefix,
2522 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2523 ";norefresh",
2524 scope_txt);
2525 }
2526
2527 chunk_appendf(&trash,
2528 "<li><a href=\"%s%s%s%s\">Refresh now</a><br>\n",
2529 uri->uri_prefix,
2530 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2531 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2532 scope_txt);
2533
2534 chunk_appendf(&trash,
2535 "<li><a href=\"%s;csv%s%s\">CSV export</a><br>\n",
2536 uri->uri_prefix,
2537 (uri->refresh > 0) ? ";norefresh" : "",
2538 scope_txt);
2539
2540 chunk_appendf(&trash,
2541 "</ul></td>"
2542 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
2543 "<b>External resources:</b><ul style=\"margin-top: 0.25em;\">\n"
2544 "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>\n"
2545 "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>\n"
2546 "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>\n"
2547 "</ul>"
2548 "</td>"
2549 "</tr></table>\n"
2550 ""
2551 );
2552
2553 if (appctx->ctx.stats.st_code) {
2554 switch (appctx->ctx.stats.st_code) {
2555 case STAT_STATUS_DONE:
2556 chunk_appendf(&trash,
2557 "<p><div class=active_up>"
2558 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2559 "Action processed successfully."
2560 "</div>\n", uri->uri_prefix,
2561 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2562 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2563 scope_txt);
2564 break;
2565 case STAT_STATUS_NONE:
2566 chunk_appendf(&trash,
2567 "<p><div class=active_going_down>"
2568 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2569 "Nothing has changed."
2570 "</div>\n", uri->uri_prefix,
2571 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2572 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2573 scope_txt);
2574 break;
2575 case STAT_STATUS_PART:
2576 chunk_appendf(&trash,
2577 "<p><div class=active_going_down>"
2578 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2579 "Action partially processed.<br>"
2580 "Some server names are probably unknown or ambiguous (duplicated names in the backend)."
2581 "</div>\n", uri->uri_prefix,
2582 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2583 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2584 scope_txt);
2585 break;
2586 case STAT_STATUS_ERRP:
2587 chunk_appendf(&trash,
2588 "<p><div class=active_down>"
2589 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2590 "Action not processed because of invalid parameters."
2591 "<ul>"
2592 "<li>The action is maybe unknown.</li>"
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01002593 "<li>Invalid key parameter (empty or too long).</li>"
William Lallemand74c24fb2016-11-21 17:18:36 +01002594 "<li>The backend name is probably unknown or ambiguous (duplicated names).</li>"
2595 "<li>Some server names are probably unknown or ambiguous (duplicated names in the backend).</li>"
2596 "</ul>"
2597 "</div>\n", uri->uri_prefix,
2598 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2599 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2600 scope_txt);
2601 break;
2602 case STAT_STATUS_EXCD:
2603 chunk_appendf(&trash,
2604 "<p><div class=active_down>"
2605 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2606 "<b>Action not processed : the buffer couldn't store all the data.<br>"
2607 "You should retry with less servers at a time.</b>"
2608 "</div>\n", uri->uri_prefix,
2609 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2610 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2611 scope_txt);
2612 break;
2613 case STAT_STATUS_DENY:
2614 chunk_appendf(&trash,
2615 "<p><div class=active_down>"
2616 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2617 "<b>Action denied.</b>"
2618 "</div>\n", uri->uri_prefix,
2619 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2620 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2621 scope_txt);
2622 break;
Christopher Faulet3c2ecf72019-02-27 16:41:27 +01002623 case STAT_STATUS_IVAL:
2624 chunk_appendf(&trash,
2625 "<p><div class=active_down>"
2626 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2627 "<b>Invalid requests (unsupported method or chunked encoded request).</b>"
2628 "</div>\n", uri->uri_prefix,
2629 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2630 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2631 scope_txt);
2632 break;
William Lallemand74c24fb2016-11-21 17:18:36 +01002633 default:
2634 chunk_appendf(&trash,
2635 "<p><div class=active_no_check>"
2636 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2637 "Unexpected result."
2638 "</div>\n", uri->uri_prefix,
2639 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2640 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2641 scope_txt);
2642 }
2643 chunk_appendf(&trash, "<p>\n");
2644 }
2645}
2646
2647/* Dumps the HTML stats trailer block to the trash. The caller is responsible
2648 * for clearing the trash if needed.
2649 */
2650static void stats_dump_html_end()
2651{
2652 chunk_appendf(&trash, "</body></html>\n");
2653}
2654
Simon Horman05ee2132017-01-04 09:37:25 +01002655/* Dumps the stats JSON header to the trash buffer which. The caller is responsible
2656 * for clearing it if needed.
2657 */
2658static void stats_dump_json_header()
2659{
2660 chunk_strcat(&trash, "[");
2661}
2662
2663
2664/* Dumps the JSON stats trailer block to the trash. The caller is responsible
2665 * for clearing the trash if needed.
2666 */
2667static void stats_dump_json_end()
2668{
2669 chunk_strcat(&trash, "]");
2670}
2671
William Lallemand74c24fb2016-11-21 17:18:36 +01002672/* This function dumps statistics onto the stream interface's read buffer in
2673 * either CSV or HTML format. <uri> contains some HTML-specific parameters that
2674 * are ignored for CSV format (hence <uri> may be NULL there). It returns 0 if
2675 * it had to stop writing data and an I/O is needed, 1 if the dump is finished
2676 * and the stream must be closed, or -1 in case of any error. This function is
2677 * used by both the CLI and the HTTP handlers.
2678 */
Christopher Fauletef779222018-10-31 08:47:01 +01002679static int stats_dump_stat_to_buffer(struct stream_interface *si, struct htx *htx,
2680 struct uri_auth *uri)
William Lallemand74c24fb2016-11-21 17:18:36 +01002681{
2682 struct appctx *appctx = __objt_appctx(si->end);
2683 struct channel *rep = si_ic(si);
2684 struct proxy *px;
2685
2686 chunk_reset(&trash);
2687
2688 switch (appctx->st2) {
2689 case STAT_ST_INIT:
2690 appctx->st2 = STAT_ST_HEAD; /* let's start producing data */
2691 /* fall through */
2692
2693 case STAT_ST_HEAD:
2694 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
2695 stats_dump_html_head(uri);
Simon Horman05ee2132017-01-04 09:37:25 +01002696 else if (appctx->ctx.stats.flags & STAT_FMT_JSON)
Willy Tarreau9d7fb632017-04-11 07:53:04 +02002697 stats_dump_json_header();
William Lallemand74c24fb2016-11-21 17:18:36 +01002698 else if (!(appctx->ctx.stats.flags & STAT_FMT_TYPED))
2699 stats_dump_csv_header();
2700
Christopher Fauletef779222018-10-31 08:47:01 +01002701 if (!stats_putchk(rep, htx, &trash))
2702 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002703
2704 appctx->st2 = STAT_ST_INFO;
2705 /* fall through */
2706
2707 case STAT_ST_INFO:
2708 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
2709 stats_dump_html_info(si, uri);
Christopher Fauletef779222018-10-31 08:47:01 +01002710 if (!stats_putchk(rep, htx, &trash))
2711 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002712 }
2713
Olivier Houchardfbc74e82017-11-24 16:54:05 +01002714 appctx->ctx.stats.px = proxies_list;
William Lallemand74c24fb2016-11-21 17:18:36 +01002715 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
2716 appctx->st2 = STAT_ST_LIST;
2717 /* fall through */
2718
2719 case STAT_ST_LIST:
2720 /* dump proxies */
2721 while (appctx->ctx.stats.px) {
Christopher Fauletef779222018-10-31 08:47:01 +01002722 if (htx) {
2723 if (htx_almost_full(htx))
2724 goto full;
2725 }
2726 else {
2727 if (buffer_almost_full(&rep->buf))
2728 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002729 }
2730
2731 px = appctx->ctx.stats.px;
2732 /* skip the disabled proxies, global frontend and non-networked ones */
2733 if (px->state != PR_STSTOPPED && px->uuid > 0 && (px->cap & (PR_CAP_FE | PR_CAP_BE)))
Christopher Fauletef779222018-10-31 08:47:01 +01002734 if (stats_dump_proxy_to_buffer(si, htx, px, uri) == 0)
William Lallemand74c24fb2016-11-21 17:18:36 +01002735 return 0;
2736
2737 appctx->ctx.stats.px = px->next;
2738 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
2739 }
2740 /* here, we just have reached the last proxy */
2741
2742 appctx->st2 = STAT_ST_END;
2743 /* fall through */
2744
2745 case STAT_ST_END:
Simon Horman05ee2132017-01-04 09:37:25 +01002746 if (appctx->ctx.stats.flags & (STAT_FMT_HTML|STAT_FMT_JSON)) {
2747 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
2748 stats_dump_html_end();
2749 else
2750 stats_dump_json_end();
Christopher Fauletef779222018-10-31 08:47:01 +01002751 if (!stats_putchk(rep, htx, &trash))
2752 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002753 }
2754
2755 appctx->st2 = STAT_ST_FIN;
2756 /* fall through */
2757
2758 case STAT_ST_FIN:
2759 return 1;
2760
2761 default:
2762 /* unknown state ! */
2763 appctx->st2 = STAT_ST_FIN;
2764 return -1;
2765 }
Christopher Fauletef779222018-10-31 08:47:01 +01002766
2767 full:
2768 si_rx_room_blk(si);
2769 return 0;
2770
William Lallemand74c24fb2016-11-21 17:18:36 +01002771}
2772
2773/* We reached the stats page through a POST request. The appctx is
2774 * expected to have already been allocated by the caller.
2775 * Parse the posted data and enable/disable servers if necessary.
2776 * Returns 1 if request was parsed or zero if it needs more data.
2777 */
2778static int stats_process_http_post(struct stream_interface *si)
2779{
2780 struct stream *s = si_strm(si);
2781 struct appctx *appctx = objt_appctx(si->end);
2782
2783 struct proxy *px = NULL;
2784 struct server *sv = NULL;
2785
2786 char key[LINESIZE];
2787 int action = ST_ADM_ACTION_NONE;
2788 int reprocess = 0;
2789
2790 int total_servers = 0;
2791 int altered_servers = 0;
2792
2793 char *first_param, *cur_param, *next_param, *end_params;
2794 char *st_cur_param = NULL;
2795 char *st_next_param = NULL;
2796
Christopher Fauleta3618372018-12-13 21:59:56 +01002797 struct buffer *temp = get_trash_chunk();
William Lallemand74c24fb2016-11-21 17:18:36 +01002798
Christopher Fauleta3618372018-12-13 21:59:56 +01002799 if (IS_HTX_STRM(s)) {
2800 struct htx *htx = htxbuf(&s->req.buf);
2801 struct htx_blk *blk;
Christopher Fauleta3618372018-12-13 21:59:56 +01002802
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01002803 /* we need more data */
2804 if (s->txn->req.msg_state < HTTP_MSG_DONE) {
2805 /* check if we can receive more */
2806 if (htx_free_data_space(htx) <= global.tune.maxrewrite) {
2807 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
2808 goto out;
Christopher Fauleta3618372018-12-13 21:59:56 +01002809 }
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01002810 goto wait;
Christopher Fauleta3618372018-12-13 21:59:56 +01002811 }
William Lallemand74c24fb2016-11-21 17:18:36 +01002812
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01002813 /* The request was fully received. Copy data */
Christopher Faulet8fa60e42019-05-23 11:04:05 +02002814 blk = htx_get_head_blk(htx);
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01002815 while (blk) {
Christopher Fauleta3618372018-12-13 21:59:56 +01002816 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Fauleta3618372018-12-13 21:59:56 +01002817
Christopher Faulet54b5e212019-06-04 10:08:28 +02002818 if (type == HTX_BLK_EOM || type == HTX_BLK_TLR || type == HTX_BLK_EOT)
Christopher Fauleta3618372018-12-13 21:59:56 +01002819 break;
2820 if (type == HTX_BLK_DATA) {
2821 struct ist v = htx_get_blk_value(htx, blk);
2822
2823 if (!chunk_memcat(temp, v.ptr, v.len)) {
2824 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
2825 goto out;
2826 }
2827 }
Christopher Fauleted7a0662019-01-14 11:07:34 +01002828 blk = htx_get_next_blk(htx, blk);
Christopher Fauleta3618372018-12-13 21:59:56 +01002829 }
2830 }
2831 else {
2832 int reql;
2833
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01002834 /* we need more data */
2835 if (s->txn->req.msg_state < HTTP_MSG_DONE) {
2836 /* check if we can receive more */
2837 if (c_room(&s->req) <= global.tune.maxrewrite) {
2838 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
2839 goto out;
2840 }
2841 goto wait;
Christopher Fauleta3618372018-12-13 21:59:56 +01002842 }
Christopher Fauleta3618372018-12-13 21:59:56 +01002843 reql = co_getblk(si_oc(si), temp->area, s->txn->req.body_len,
2844 s->txn->req.eoh + 2);
2845 if (reql <= 0) {
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01002846 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
2847 goto out;
Christopher Fauleta3618372018-12-13 21:59:56 +01002848 }
2849 temp->data = reql;
William Lallemand74c24fb2016-11-21 17:18:36 +01002850 }
2851
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002852 first_param = temp->area;
Christopher Fauleta3618372018-12-13 21:59:56 +01002853 end_params = temp->area + temp->data;
William Lallemand74c24fb2016-11-21 17:18:36 +01002854 cur_param = next_param = end_params;
2855 *end_params = '\0';
2856
2857 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
2858
2859 /*
2860 * Parse the parameters in reverse order to only store the last value.
2861 * From the html form, the backend and the action are at the end.
2862 */
2863 while (cur_param > first_param) {
2864 char *value;
2865 int poffset, plen;
2866
2867 cur_param--;
2868
2869 if ((*cur_param == '&') || (cur_param == first_param)) {
2870 reprocess_servers:
2871 /* Parse the key */
2872 poffset = (cur_param != first_param ? 1 : 0);
2873 plen = next_param - cur_param + (cur_param == first_param ? 1 : 0);
2874 if ((plen > 0) && (plen <= sizeof(key))) {
2875 strncpy(key, cur_param + poffset, plen);
2876 key[plen - 1] = '\0';
2877 } else {
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01002878 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
William Lallemand74c24fb2016-11-21 17:18:36 +01002879 goto out;
2880 }
2881
2882 /* Parse the value */
2883 value = key;
2884 while (*value != '\0' && *value != '=') {
2885 value++;
2886 }
2887 if (*value == '=') {
2888 /* Ok, a value is found, we can mark the end of the key */
2889 *value++ = '\0';
2890 }
2891 if (url_decode(key) < 0 || url_decode(value) < 0)
2892 break;
2893
2894 /* Now we can check the key to see what to do */
2895 if (!px && (strcmp(key, "b") == 0)) {
2896 if ((px = proxy_be_by_name(value)) == NULL) {
2897 /* the backend name is unknown or ambiguous (duplicate names) */
2898 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
2899 goto out;
2900 }
2901 }
2902 else if (!action && (strcmp(key, "action") == 0)) {
2903 if (strcmp(value, "ready") == 0) {
2904 action = ST_ADM_ACTION_READY;
2905 }
2906 else if (strcmp(value, "drain") == 0) {
2907 action = ST_ADM_ACTION_DRAIN;
2908 }
2909 else if (strcmp(value, "maint") == 0) {
2910 action = ST_ADM_ACTION_MAINT;
2911 }
2912 else if (strcmp(value, "shutdown") == 0) {
2913 action = ST_ADM_ACTION_SHUTDOWN;
2914 }
2915 else if (strcmp(value, "dhlth") == 0) {
2916 action = ST_ADM_ACTION_DHLTH;
2917 }
2918 else if (strcmp(value, "ehlth") == 0) {
2919 action = ST_ADM_ACTION_EHLTH;
2920 }
2921 else if (strcmp(value, "hrunn") == 0) {
2922 action = ST_ADM_ACTION_HRUNN;
2923 }
2924 else if (strcmp(value, "hnolb") == 0) {
2925 action = ST_ADM_ACTION_HNOLB;
2926 }
2927 else if (strcmp(value, "hdown") == 0) {
2928 action = ST_ADM_ACTION_HDOWN;
2929 }
2930 else if (strcmp(value, "dagent") == 0) {
2931 action = ST_ADM_ACTION_DAGENT;
2932 }
2933 else if (strcmp(value, "eagent") == 0) {
2934 action = ST_ADM_ACTION_EAGENT;
2935 }
2936 else if (strcmp(value, "arunn") == 0) {
2937 action = ST_ADM_ACTION_ARUNN;
2938 }
2939 else if (strcmp(value, "adown") == 0) {
2940 action = ST_ADM_ACTION_ADOWN;
2941 }
2942 /* else these are the old supported methods */
2943 else if (strcmp(value, "disable") == 0) {
2944 action = ST_ADM_ACTION_DISABLE;
2945 }
2946 else if (strcmp(value, "enable") == 0) {
2947 action = ST_ADM_ACTION_ENABLE;
2948 }
2949 else if (strcmp(value, "stop") == 0) {
2950 action = ST_ADM_ACTION_STOP;
2951 }
2952 else if (strcmp(value, "start") == 0) {
2953 action = ST_ADM_ACTION_START;
2954 }
2955 else {
2956 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
2957 goto out;
2958 }
2959 }
2960 else if (strcmp(key, "s") == 0) {
2961 if (!(px && action)) {
2962 /*
2963 * Indicates that we'll need to reprocess the parameters
2964 * as soon as backend and action are known
2965 */
2966 if (!reprocess) {
2967 st_cur_param = cur_param;
2968 st_next_param = next_param;
2969 }
2970 reprocess = 1;
2971 }
2972 else if ((sv = findserver(px, value)) != NULL) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002973 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
William Lallemand74c24fb2016-11-21 17:18:36 +01002974 switch (action) {
2975 case ST_ADM_ACTION_DISABLE:
Emeric Brun52a91d32017-08-31 14:41:55 +02002976 if (!(sv->cur_admin & SRV_ADMF_FMAINT)) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002977 altered_servers++;
2978 total_servers++;
2979 srv_set_admin_flag(sv, SRV_ADMF_FMAINT, "'disable' on stats page");
2980 }
2981 break;
2982 case ST_ADM_ACTION_ENABLE:
Emeric Brun52a91d32017-08-31 14:41:55 +02002983 if (sv->cur_admin & SRV_ADMF_FMAINT) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002984 altered_servers++;
2985 total_servers++;
2986 srv_clr_admin_flag(sv, SRV_ADMF_FMAINT);
2987 }
2988 break;
2989 case ST_ADM_ACTION_STOP:
Emeric Brun52a91d32017-08-31 14:41:55 +02002990 if (!(sv->cur_admin & SRV_ADMF_FDRAIN)) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002991 srv_set_admin_flag(sv, SRV_ADMF_FDRAIN, "'stop' on stats page");
2992 altered_servers++;
2993 total_servers++;
2994 }
2995 break;
2996 case ST_ADM_ACTION_START:
Emeric Brun52a91d32017-08-31 14:41:55 +02002997 if (sv->cur_admin & SRV_ADMF_FDRAIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002998 srv_clr_admin_flag(sv, SRV_ADMF_FDRAIN);
2999 altered_servers++;
3000 total_servers++;
3001 }
3002 break;
3003 case ST_ADM_ACTION_DHLTH:
3004 if (sv->check.state & CHK_ST_CONFIGURED) {
3005 sv->check.state &= ~CHK_ST_ENABLED;
3006 altered_servers++;
3007 total_servers++;
3008 }
3009 break;
3010 case ST_ADM_ACTION_EHLTH:
3011 if (sv->check.state & CHK_ST_CONFIGURED) {
3012 sv->check.state |= CHK_ST_ENABLED;
3013 altered_servers++;
3014 total_servers++;
3015 }
3016 break;
3017 case ST_ADM_ACTION_HRUNN:
3018 if (!(sv->track)) {
3019 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02003020 srv_set_running(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003021 altered_servers++;
3022 total_servers++;
3023 }
3024 break;
3025 case ST_ADM_ACTION_HNOLB:
3026 if (!(sv->track)) {
3027 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02003028 srv_set_stopping(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003029 altered_servers++;
3030 total_servers++;
3031 }
3032 break;
3033 case ST_ADM_ACTION_HDOWN:
3034 if (!(sv->track)) {
3035 sv->check.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02003036 srv_set_stopped(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003037 altered_servers++;
3038 total_servers++;
3039 }
3040 break;
3041 case ST_ADM_ACTION_DAGENT:
3042 if (sv->agent.state & CHK_ST_CONFIGURED) {
3043 sv->agent.state &= ~CHK_ST_ENABLED;
3044 altered_servers++;
3045 total_servers++;
3046 }
3047 break;
3048 case ST_ADM_ACTION_EAGENT:
3049 if (sv->agent.state & CHK_ST_CONFIGURED) {
3050 sv->agent.state |= CHK_ST_ENABLED;
3051 altered_servers++;
3052 total_servers++;
3053 }
3054 break;
3055 case ST_ADM_ACTION_ARUNN:
3056 if (sv->agent.state & CHK_ST_ENABLED) {
3057 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02003058 srv_set_running(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003059 altered_servers++;
3060 total_servers++;
3061 }
3062 break;
3063 case ST_ADM_ACTION_ADOWN:
3064 if (sv->agent.state & CHK_ST_ENABLED) {
3065 sv->agent.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02003066 srv_set_stopped(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003067 altered_servers++;
3068 total_servers++;
3069 }
3070 break;
3071 case ST_ADM_ACTION_READY:
3072 srv_adm_set_ready(sv);
3073 altered_servers++;
3074 total_servers++;
3075 break;
3076 case ST_ADM_ACTION_DRAIN:
3077 srv_adm_set_drain(sv);
3078 altered_servers++;
3079 total_servers++;
3080 break;
3081 case ST_ADM_ACTION_MAINT:
3082 srv_adm_set_maint(sv);
3083 altered_servers++;
3084 total_servers++;
3085 break;
3086 case ST_ADM_ACTION_SHUTDOWN:
3087 if (px->state != PR_STSTOPPED) {
3088 struct stream *sess, *sess_bck;
3089
3090 list_for_each_entry_safe(sess, sess_bck, &sv->actconns, by_srv)
3091 if (sess->srv_conn == sv)
3092 stream_shutdown(sess, SF_ERR_KILLED);
3093
3094 altered_servers++;
3095 total_servers++;
3096 }
3097 break;
3098 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003099 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Lallemand74c24fb2016-11-21 17:18:36 +01003100 } else {
3101 /* the server name is unknown or ambiguous (duplicate names) */
3102 total_servers++;
3103 }
3104 }
3105 if (reprocess && px && action) {
3106 /* Now, we know the backend and the action chosen by the user.
3107 * We can safely restart from the first server parameter
3108 * to reprocess them
3109 */
3110 cur_param = st_cur_param;
3111 next_param = st_next_param;
3112 reprocess = 0;
3113 goto reprocess_servers;
3114 }
3115
3116 next_param = cur_param;
3117 }
3118 }
3119
3120 if (total_servers == 0) {
3121 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
3122 }
3123 else if (altered_servers == 0) {
3124 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
3125 }
3126 else if (altered_servers == total_servers) {
3127 appctx->ctx.stats.st_code = STAT_STATUS_DONE;
3128 }
3129 else {
3130 appctx->ctx.stats.st_code = STAT_STATUS_PART;
3131 }
3132 out:
3133 return 1;
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01003134 wait:
3135 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
3136 return 0;
Christopher Fauletef779222018-10-31 08:47:01 +01003137}
3138
3139
3140static int stats_send_htx_headers(struct stream_interface *si, struct htx *htx)
3141{
3142 struct stream *s = si_strm(si);
3143 struct uri_auth *uri = s->be->uri_auth;
3144 struct appctx *appctx = __objt_appctx(si->end);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003145 struct htx_sl *sl;
3146 unsigned int flags;
Christopher Fauletef779222018-10-31 08:47:01 +01003147
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003148 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);
3149 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist("200"), ist("OK"));
3150 if (!sl)
Christopher Fauletef779222018-10-31 08:47:01 +01003151 goto full;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003152 sl->info.res.status = 200;
Christopher Fauletef779222018-10-31 08:47:01 +01003153
Christopher Fauletb829f4c2019-03-29 16:13:55 +01003154 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")))
Christopher Fauletef779222018-10-31 08:47:01 +01003155 goto full;
3156 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
3157 if (!htx_add_header(htx, ist("Content-Type"), ist("text/html")))
3158 goto full;
3159 }
3160 else {
3161 if (!htx_add_header(htx, ist("Content-Type"), ist("text/plain")))
3162 goto full;
3163 }
3164
3165 if (uri->refresh > 0 && !(appctx->ctx.stats.flags & STAT_NO_REFRESH)) {
3166 const char *refresh = U2A(uri->refresh);
3167 if (!htx_add_header(htx, ist("Refresh"), ist2(refresh, strlen(refresh))))
3168 goto full;
3169 }
3170
3171 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
3172 if (!htx_add_header(htx, ist("Transfer-Encoding"), ist("chunked")))
3173 goto full;
3174 }
3175
3176 if (!htx_add_endof(htx, HTX_BLK_EOH))
3177 goto full;
3178
Christopher Faulet1e2d6362019-02-27 16:28:48 +01003179 channel_add_input(&s->res, htx->data);
Christopher Fauletef779222018-10-31 08:47:01 +01003180 return 1;
3181
3182 full:
3183 htx_reset(htx);
3184 si_rx_room_blk(si);
3185 return 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01003186}
3187
Christopher Fauletef779222018-10-31 08:47:01 +01003188
3189static int stats_send_htx_redirect(struct stream_interface *si, struct htx *htx)
3190{
3191 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
3192 struct stream *s = si_strm(si);
3193 struct uri_auth *uri = s->be->uri_auth;
3194 struct appctx *appctx = __objt_appctx(si->end);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003195 struct htx_sl *sl;
3196 unsigned int flags;
Christopher Fauletef779222018-10-31 08:47:01 +01003197
3198 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
3199 scope_txt[0] = 0;
3200 if (appctx->ctx.stats.scope_len) {
Christopher Fauleted7a0662019-01-14 11:07:34 +01003201 const char *scope_ptr = stats_scope_ptr(appctx, si);
3202
Christopher Fauletef779222018-10-31 08:47:01 +01003203 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Christopher Fauleted7a0662019-01-14 11:07:34 +01003204 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), scope_ptr, appctx->ctx.stats.scope_len);
Christopher Fauletef779222018-10-31 08:47:01 +01003205 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
3206 }
3207
3208 /* We don't want to land on the posted stats page because a refresh will
3209 * repost the data. We don't want this to happen on accident so we redirect
3210 * the browse to the stats page with a GET.
3211 */
3212 chunk_printf(&trash, "%s;st=%s%s%s%s",
3213 uri->uri_prefix,
3214 ((appctx->ctx.stats.st_code > STAT_STATUS_INIT) &&
3215 (appctx->ctx.stats.st_code < STAT_STATUS_SIZE) &&
3216 stat_status_codes[appctx->ctx.stats.st_code]) ?
3217 stat_status_codes[appctx->ctx.stats.st_code] :
3218 stat_status_codes[STAT_STATUS_UNKN],
3219 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3220 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3221 scope_txt);
3222
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003223 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_CHNK);
3224 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist("303"), ist("See Other"));
3225 if (!sl)
Christopher Fauletef779222018-10-31 08:47:01 +01003226 goto full;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003227 sl->info.res.status = 303;
Christopher Fauletef779222018-10-31 08:47:01 +01003228
3229 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")) ||
Christopher Fauletef779222018-10-31 08:47:01 +01003230 !htx_add_header(htx, ist("Content-Type"), ist("text/plain")) ||
3231 !htx_add_header(htx, ist("Content-Length"), ist("0")) ||
3232 !htx_add_header(htx, ist("Location"), ist2(trash.area, trash.data)))
3233 goto full;
3234
3235 if (!htx_add_endof(htx, HTX_BLK_EOH))
3236 goto full;
3237
Christopher Faulet1e2d6362019-02-27 16:28:48 +01003238 channel_add_input(&s->res, htx->data);
Christopher Fauletef779222018-10-31 08:47:01 +01003239 return 1;
3240
3241full:
3242 htx_reset(htx);
3243 si_rx_room_blk(si);
3244 return 0;
3245}
William Lallemand74c24fb2016-11-21 17:18:36 +01003246
3247static int stats_send_http_headers(struct stream_interface *si)
3248{
3249 struct stream *s = si_strm(si);
3250 struct uri_auth *uri = s->be->uri_auth;
Willy Tarreau21ff2c42018-09-20 11:01:01 +02003251 struct appctx *appctx = __objt_appctx(si->end);
William Lallemand74c24fb2016-11-21 17:18:36 +01003252
3253 chunk_printf(&trash,
3254 "HTTP/1.1 200 OK\r\n"
3255 "Cache-Control: no-cache\r\n"
3256 "Connection: close\r\n"
3257 "Content-Type: %s\r\n",
3258 (appctx->ctx.stats.flags & STAT_FMT_HTML) ? "text/html" : "text/plain");
3259
3260 if (uri->refresh > 0 && !(appctx->ctx.stats.flags & STAT_NO_REFRESH))
3261 chunk_appendf(&trash, "Refresh: %d\r\n",
3262 uri->refresh);
3263
3264 /* we don't send the CRLF in chunked mode, it will be sent with the first chunk's size */
3265
3266 if (appctx->ctx.stats.flags & STAT_CHUNKED)
3267 chunk_appendf(&trash, "Transfer-Encoding: chunked\r\n");
3268 else
3269 chunk_appendf(&trash, "\r\n");
3270
Willy Tarreau06d80a92017-10-19 14:32:15 +02003271 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01003272 si_rx_room_blk(si);
William Lallemand74c24fb2016-11-21 17:18:36 +01003273 return 0;
3274 }
3275
3276 return 1;
3277}
3278
3279static int stats_send_http_redirect(struct stream_interface *si)
3280{
3281 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
3282 struct stream *s = si_strm(si);
3283 struct uri_auth *uri = s->be->uri_auth;
Willy Tarreau21ff2c42018-09-20 11:01:01 +02003284 struct appctx *appctx = __objt_appctx(si->end);
William Lallemand74c24fb2016-11-21 17:18:36 +01003285
3286 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
3287 scope_txt[0] = 0;
3288 if (appctx->ctx.stats.scope_len) {
3289 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau89faf5d2018-06-07 18:16:48 +02003290 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), co_head(si_oc(si)) + appctx->ctx.stats.scope_str, appctx->ctx.stats.scope_len);
William Lallemand74c24fb2016-11-21 17:18:36 +01003291 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
3292 }
3293
3294 /* We don't want to land on the posted stats page because a refresh will
3295 * repost the data. We don't want this to happen on accident so we redirect
3296 * the browse to the stats page with a GET.
3297 */
3298 chunk_printf(&trash,
3299 "HTTP/1.1 303 See Other\r\n"
3300 "Cache-Control: no-cache\r\n"
3301 "Content-Type: text/plain\r\n"
3302 "Connection: close\r\n"
3303 "Location: %s;st=%s%s%s%s\r\n"
3304 "Content-length: 0\r\n"
3305 "\r\n",
3306 uri->uri_prefix,
3307 ((appctx->ctx.stats.st_code > STAT_STATUS_INIT) &&
3308 (appctx->ctx.stats.st_code < STAT_STATUS_SIZE) &&
3309 stat_status_codes[appctx->ctx.stats.st_code]) ?
3310 stat_status_codes[appctx->ctx.stats.st_code] :
3311 stat_status_codes[STAT_STATUS_UNKN],
3312 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3313 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3314 scope_txt);
3315
Willy Tarreau06d80a92017-10-19 14:32:15 +02003316 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01003317 si_rx_room_blk(si);
William Lallemand74c24fb2016-11-21 17:18:36 +01003318 return 0;
3319 }
3320
3321 return 1;
3322}
3323
Simon Horman05ee2132017-01-04 09:37:25 +01003324
William Lallemand74c24fb2016-11-21 17:18:36 +01003325/* This I/O handler runs as an applet embedded in a stream interface. It is
3326 * used to send HTTP stats over a TCP socket. The mechanism is very simple.
3327 * appctx->st0 contains the operation in progress (dump, done). The handler
3328 * automatically unregisters itself once transfer is complete.
3329 */
Christopher Fauletef779222018-10-31 08:47:01 +01003330static void htx_stats_io_handler(struct appctx *appctx)
3331{
3332 struct stream_interface *si = appctx->owner;
3333 struct stream *s = si_strm(si);
3334 struct channel *req = si_oc(si);
3335 struct channel *res = si_ic(si);
3336 struct htx *req_htx, *res_htx;
3337
3338 res_htx = htx_from_buf(&res->buf);
3339
3340 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
3341 goto out;
3342
3343 /* Check if the input buffer is avalaible. */
3344 if (!b_size(&res->buf)) {
3345 si_rx_room_blk(si);
3346 goto out;
3347 }
3348
3349 /* check that the output is not closed */
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003350 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_SHUTR))
3351 appctx->st0 = STAT_HTTP_END;
Christopher Fauletef779222018-10-31 08:47:01 +01003352
3353 /* all states are processed in sequence */
3354 if (appctx->st0 == STAT_HTTP_HEAD) {
3355 if (stats_send_htx_headers(si, res_htx)) {
3356 if (s->txn->meth == HTTP_METH_HEAD)
3357 appctx->st0 = STAT_HTTP_DONE;
3358 else
3359 appctx->st0 = STAT_HTTP_DUMP;
3360 }
3361 }
3362
3363 if (appctx->st0 == STAT_HTTP_DUMP) {
3364 if (stats_dump_stat_to_buffer(si, res_htx, s->be->uri_auth))
3365 appctx->st0 = STAT_HTTP_DONE;
3366 }
3367
3368 if (appctx->st0 == STAT_HTTP_POST) {
3369 if (stats_process_http_post(si))
3370 appctx->st0 = STAT_HTTP_LAST;
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003371 else if (req->flags & CF_SHUTR)
Christopher Fauletef779222018-10-31 08:47:01 +01003372 appctx->st0 = STAT_HTTP_DONE;
3373 }
3374
3375 if (appctx->st0 == STAT_HTTP_LAST) {
3376 if (stats_send_htx_redirect(si, res_htx))
3377 appctx->st0 = STAT_HTTP_DONE;
3378 }
3379
3380 if (appctx->st0 == STAT_HTTP_DONE) {
Christopher Faulet54b5e212019-06-04 10:08:28 +02003381 /* Don't add TLR because mux-h1 will take care of it */
Christopher Fauletef779222018-10-31 08:47:01 +01003382 if (!htx_add_endof(res_htx, HTX_BLK_EOM)) {
3383 si_rx_room_blk(si);
3384 goto out;
3385 }
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003386 channel_add_input(&s->res, 1);
3387 appctx->st0 = STAT_HTTP_END;
Christopher Fauletef779222018-10-31 08:47:01 +01003388 }
3389
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003390 if (appctx->st0 == STAT_HTTP_END) {
3391 if (!(res->flags & CF_SHUTR)) {
Christopher Fauletef779222018-10-31 08:47:01 +01003392 res->flags |= CF_READ_NULL;
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003393 si_shutr(si);
3394 }
3395
3396 /* eat the whole request */
3397 if (co_data(req)) {
3398 req_htx = htx_from_buf(&req->buf);
3399 co_htx_skip(req, req_htx, co_data(req));
3400 htx_to_buf(req_htx, &req->buf);
Christopher Fauletef779222018-10-31 08:47:01 +01003401 }
3402 }
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003403
Christopher Fauletef779222018-10-31 08:47:01 +01003404 out:
3405 /* we have left the request in the buffer for the case where we
3406 * process a POST, and this automatically re-enables activity on
3407 * read. It's better to indicate that we want to stop reading when
3408 * we're sending, so that we know there's at most one direction
3409 * deciding to wake the applet up. It saves it from looping when
3410 * emitting large blocks into small TCP windows.
3411 */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003412 htx_to_buf(res_htx, &res->buf);
3413 if (!channel_is_empty(res))
Christopher Fauletef779222018-10-31 08:47:01 +01003414 si_stop_get(si);
Christopher Fauletef779222018-10-31 08:47:01 +01003415}
3416
3417
3418/* This I/O handler runs as an applet embedded in a stream interface. It is
3419 * used to send HTTP stats over a TCP socket. The mechanism is very simple.
3420 * appctx->st0 contains the operation in progress (dump, done). The handler
3421 * automatically unregisters itself once transfer is complete.
3422 */
William Lallemand74c24fb2016-11-21 17:18:36 +01003423static void http_stats_io_handler(struct appctx *appctx)
3424{
3425 struct stream_interface *si = appctx->owner;
3426 struct stream *s = si_strm(si);
3427 struct channel *req = si_oc(si);
3428 struct channel *res = si_ic(si);
3429
Christopher Fauletef779222018-10-31 08:47:01 +01003430 if (IS_HTX_STRM(s))
3431 return htx_stats_io_handler(appctx);
3432
William Lallemand74c24fb2016-11-21 17:18:36 +01003433 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
3434 goto out;
3435
Joseph Herlant29023ec2018-11-15 13:39:46 -08003436 /* Check if the input buffer is available. */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003437 if (res->buf.size == 0) {
Willy Tarreau4b962a42018-11-15 11:03:21 +01003438 /* already subscribed, we'll be called later once the buffer is
3439 * available.
3440 */
Christopher Fauleta73e59b2016-12-09 17:30:18 +01003441 goto out;
3442 }
3443
William Lallemand74c24fb2016-11-21 17:18:36 +01003444 /* check that the output is not closed */
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003445 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_SHUTR))
3446 appctx->st0 = STAT_HTTP_END;
William Lallemand74c24fb2016-11-21 17:18:36 +01003447
3448 /* all states are processed in sequence */
3449 if (appctx->st0 == STAT_HTTP_HEAD) {
3450 if (stats_send_http_headers(si)) {
3451 if (s->txn->meth == HTTP_METH_HEAD)
3452 appctx->st0 = STAT_HTTP_DONE;
3453 else
3454 appctx->st0 = STAT_HTTP_DUMP;
3455 }
3456 }
3457
3458 if (appctx->st0 == STAT_HTTP_DUMP) {
Willy Tarreau97f538b2018-06-15 19:41:31 +02003459 unsigned int prev_len = ci_data(si_ic(si));
William Lallemand74c24fb2016-11-21 17:18:36 +01003460 unsigned int data_len;
3461 unsigned int last_len;
3462 unsigned int last_fwd = 0;
3463
3464 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
3465 /* One difficulty we're facing is that we must prevent
3466 * the input data from being automatically forwarded to
3467 * the output area. For this, we temporarily disable
3468 * forwarding on the channel.
3469 */
3470 last_fwd = si_ic(si)->to_forward;
3471 si_ic(si)->to_forward = 0;
3472 chunk_printf(&trash, "\r\n000000\r\n");
Willy Tarreau06d80a92017-10-19 14:32:15 +02003473 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01003474 si_rx_room_blk(si);
William Lallemand74c24fb2016-11-21 17:18:36 +01003475 si_ic(si)->to_forward = last_fwd;
3476 goto out;
3477 }
3478 }
3479
Willy Tarreau97f538b2018-06-15 19:41:31 +02003480 data_len = ci_data(si_ic(si));
Christopher Fauletef779222018-10-31 08:47:01 +01003481 if (stats_dump_stat_to_buffer(si, NULL, s->be->uri_auth))
William Lallemand74c24fb2016-11-21 17:18:36 +01003482 appctx->st0 = STAT_HTTP_DONE;
3483
Willy Tarreau97f538b2018-06-15 19:41:31 +02003484 last_len = ci_data(si_ic(si));
William Lallemand74c24fb2016-11-21 17:18:36 +01003485
3486 /* Now we must either adjust or remove the chunk size. This is
3487 * not easy because the chunk size might wrap at the end of the
3488 * buffer, so we pretend we have nothing in the buffer, we write
3489 * the size, then restore the buffer's contents. Note that we can
3490 * only do that because no forwarding is scheduled on the stats
3491 * applet.
3492 */
3493 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
3494 si_ic(si)->total -= (last_len - prev_len);
Willy Tarreau97f538b2018-06-15 19:41:31 +02003495 b_sub(si_ib(si), (last_len - prev_len));
William Lallemand74c24fb2016-11-21 17:18:36 +01003496
3497 if (last_len != data_len) {
3498 chunk_printf(&trash, "\r\n%06x\r\n", (last_len - data_len));
Willy Tarreau06d80a92017-10-19 14:32:15 +02003499 if (ci_putchk(si_ic(si), &trash) == -1)
Willy Tarreaudb398432018-11-15 11:08:52 +01003500 si_rx_room_blk(si);
William Lallemand74c24fb2016-11-21 17:18:36 +01003501
3502 si_ic(si)->total += (last_len - data_len);
Willy Tarreau97f538b2018-06-15 19:41:31 +02003503 b_add(si_ib(si), last_len - data_len);
William Lallemand74c24fb2016-11-21 17:18:36 +01003504 }
3505 /* now re-enable forwarding */
3506 channel_forward(si_ic(si), last_fwd);
3507 }
3508 }
3509
3510 if (appctx->st0 == STAT_HTTP_POST) {
3511 if (stats_process_http_post(si))
3512 appctx->st0 = STAT_HTTP_LAST;
3513 else if (si_oc(si)->flags & CF_SHUTR)
3514 appctx->st0 = STAT_HTTP_DONE;
3515 }
3516
3517 if (appctx->st0 == STAT_HTTP_LAST) {
3518 if (stats_send_http_redirect(si))
3519 appctx->st0 = STAT_HTTP_DONE;
3520 }
3521
3522 if (appctx->st0 == STAT_HTTP_DONE) {
3523 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
3524 chunk_printf(&trash, "\r\n0\r\n\r\n");
Willy Tarreau06d80a92017-10-19 14:32:15 +02003525 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01003526 si_rx_room_blk(si);
William Lallemand74c24fb2016-11-21 17:18:36 +01003527 goto out;
3528 }
3529 }
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003530 appctx->st0 = STAT_HTTP_END;
William Lallemand74c24fb2016-11-21 17:18:36 +01003531 }
3532
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003533 if (appctx->st0 == STAT_HTTP_END) {
3534 if (!(res->flags & CF_SHUTR)) {
William Lallemand74c24fb2016-11-21 17:18:36 +01003535 res->flags |= CF_READ_NULL;
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003536 si_shutr(si);
William Lallemand74c24fb2016-11-21 17:18:36 +01003537 }
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003538
3539 /* eat the whole request */
3540 if (co_data(req))
3541 co_skip(si_oc(si), co_data(si_oc(si)));
William Lallemand74c24fb2016-11-21 17:18:36 +01003542 }
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003543
William Lallemand74c24fb2016-11-21 17:18:36 +01003544 out:
Willy Tarreau055ba4f2018-07-24 17:05:54 +02003545 /* we have left the request in the buffer for the case where we
3546 * process a POST, and this automatically re-enables activity on
3547 * read. It's better to indicate that we want to stop reading when
3548 * we're sending, so that we know there's at most one direction
3549 * deciding to wake the applet up. It saves it from looping when
3550 * emitting large blocks into small TCP windows.
3551 */
3552 if (!channel_is_empty(res))
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01003553 si_stop_get(si);
William Lallemand74c24fb2016-11-21 17:18:36 +01003554}
3555
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003556/* Dump all fields from <info> into <out> using the "show info" format (name: value) */
Willy Tarreau83061a82018-07-13 11:56:34 +02003557static int stats_dump_info_fields(struct buffer *out,
3558 const struct field *info)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003559{
3560 int field;
3561
3562 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
3563 if (!field_format(info, field))
3564 continue;
3565
3566 if (!chunk_appendf(out, "%s: ", info_field_names[field]))
3567 return 0;
3568 if (!stats_emit_raw_data_field(out, &info[field]))
3569 return 0;
3570 if (!chunk_strcat(out, "\n"))
3571 return 0;
3572 }
3573 return 1;
3574}
3575
3576/* Dump all fields from <info> into <out> using the "show info typed" format */
Willy Tarreau83061a82018-07-13 11:56:34 +02003577static int stats_dump_typed_info_fields(struct buffer *out,
3578 const struct field *info)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003579{
3580 int field;
3581
3582 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
3583 if (!field_format(info, field))
3584 continue;
3585
3586 if (!chunk_appendf(out, "%d.%s.%u:", field, info_field_names[field], info[INF_PROCESS_NUM].u.u32))
3587 return 0;
3588 if (!stats_emit_field_tags(out, &info[field], ':'))
3589 return 0;
3590 if (!stats_emit_typed_data_field(out, &info[field]))
3591 return 0;
3592 if (!chunk_strcat(out, "\n"))
3593 return 0;
3594 }
3595 return 1;
3596}
3597
3598/* Fill <info> with HAProxy global info. <info> is preallocated
3599 * array of length <len>. The length of the aray must be
3600 * INF_TOTAL_FIELDS. If this length is less then this value, the
3601 * function returns 0, otherwise, it returns 1.
3602 */
3603int stats_fill_info(struct field *info, int len)
3604{
3605 unsigned int up = (now.tv_sec - start_date.tv_sec);
Willy Tarreau83061a82018-07-13 11:56:34 +02003606 struct buffer *out = get_trash_chunk();
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003607
3608#ifdef USE_OPENSSL
3609 int ssl_sess_rate = read_freq_ctr(&global.ssl_per_sec);
3610 int ssl_key_rate = read_freq_ctr(&global.ssl_fe_keys_per_sec);
3611 int ssl_reuse = 0;
3612
3613 if (ssl_key_rate < ssl_sess_rate) {
3614 /* count the ssl reuse ratio and avoid overflows in both directions */
3615 ssl_reuse = 100 - (100 * ssl_key_rate + (ssl_sess_rate - 1) / 2) / ssl_sess_rate;
3616 }
3617#endif
3618
3619 if (len < INF_TOTAL_FIELDS)
3620 return 0;
3621
3622 chunk_reset(out);
3623 memset(info, 0, sizeof(*info) * len);
3624
3625 info[INF_NAME] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, PRODUCT_NAME);
Willy Tarreau909b9d82019-01-04 18:20:32 +01003626 info[INF_VERSION] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, haproxy_version);
3627 info[INF_RELEASE_DATE] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, haproxy_date);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003628
Yves Lafon95317282018-02-26 11:10:37 +01003629 info[INF_NBTHREAD] = mkf_u32(FO_CONFIG|FS_SERVICE, global.nbthread);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003630 info[INF_NBPROC] = mkf_u32(FO_CONFIG|FS_SERVICE, global.nbproc);
3631 info[INF_PROCESS_NUM] = mkf_u32(FO_KEY, relative_pid);
3632 info[INF_PID] = mkf_u32(FO_STATUS, pid);
3633
3634 info[INF_UPTIME] = mkf_str(FN_DURATION, chunk_newstr(out));
3635 chunk_appendf(out, "%ud %uh%02um%02us", up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60));
3636
3637 info[INF_UPTIME_SEC] = mkf_u32(FN_DURATION, up);
3638 info[INF_MEMMAX_MB] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_memmax);
3639 info[INF_POOL_ALLOC_MB] = mkf_u32(0, (unsigned)(pool_total_allocated() / 1048576L));
3640 info[INF_POOL_USED_MB] = mkf_u32(0, (unsigned)(pool_total_used() / 1048576L));
3641 info[INF_POOL_FAILED] = mkf_u32(FN_COUNTER, pool_total_failures());
3642 info[INF_ULIMIT_N] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_nofile);
3643 info[INF_MAXSOCK] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxsock);
3644 info[INF_MAXCONN] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxconn);
3645 info[INF_HARD_MAXCONN] = mkf_u32(FO_CONFIG|FN_LIMIT, global.hardmaxconn);
3646 info[INF_CURR_CONN] = mkf_u32(0, actconn);
3647 info[INF_CUM_CONN] = mkf_u32(FN_COUNTER, totalconn);
3648 info[INF_CUM_REQ] = mkf_u32(FN_COUNTER, global.req_count);
3649#ifdef USE_OPENSSL
3650 info[INF_MAX_SSL_CONNS] = mkf_u32(FN_MAX, global.maxsslconn);
3651 info[INF_CURR_SSL_CONNS] = mkf_u32(0, sslconns);
3652 info[INF_CUM_SSL_CONNS] = mkf_u32(FN_COUNTER, totalsslconns);
3653#endif
3654 info[INF_MAXPIPES] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxpipes);
3655 info[INF_PIPES_USED] = mkf_u32(0, pipes_used);
3656 info[INF_PIPES_FREE] = mkf_u32(0, pipes_free);
3657 info[INF_CONN_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.conn_per_sec));
3658 info[INF_CONN_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.cps_lim);
3659 info[INF_MAX_CONN_RATE] = mkf_u32(FN_MAX, global.cps_max);
3660 info[INF_SESS_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.sess_per_sec));
3661 info[INF_SESS_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.sps_lim);
3662 info[INF_MAX_SESS_RATE] = mkf_u32(FN_RATE, global.sps_max);
3663
3664#ifdef USE_OPENSSL
3665 info[INF_SSL_RATE] = mkf_u32(FN_RATE, ssl_sess_rate);
3666 info[INF_SSL_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.ssl_lim);
3667 info[INF_MAX_SSL_RATE] = mkf_u32(FN_MAX, global.ssl_max);
3668 info[INF_SSL_FRONTEND_KEY_RATE] = mkf_u32(0, ssl_key_rate);
3669 info[INF_SSL_FRONTEND_MAX_KEY_RATE] = mkf_u32(FN_MAX, global.ssl_fe_keys_max);
3670 info[INF_SSL_FRONTEND_SESSION_REUSE_PCT] = mkf_u32(0, ssl_reuse);
3671 info[INF_SSL_BACKEND_KEY_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.ssl_be_keys_per_sec));
3672 info[INF_SSL_BACKEND_MAX_KEY_RATE] = mkf_u32(FN_MAX, global.ssl_be_keys_max);
3673 info[INF_SSL_CACHE_LOOKUPS] = mkf_u32(FN_COUNTER, global.shctx_lookups);
3674 info[INF_SSL_CACHE_MISSES] = mkf_u32(FN_COUNTER, global.shctx_misses);
3675#endif
3676 info[INF_COMPRESS_BPS_IN] = mkf_u32(FN_RATE, read_freq_ctr(&global.comp_bps_in));
3677 info[INF_COMPRESS_BPS_OUT] = mkf_u32(FN_RATE, read_freq_ctr(&global.comp_bps_out));
3678 info[INF_COMPRESS_BPS_RATE_LIM] = mkf_u32(FO_CONFIG|FN_LIMIT, global.comp_rate_lim);
3679#ifdef USE_ZLIB
3680 info[INF_ZLIB_MEM_USAGE] = mkf_u32(0, zlib_used_memory);
3681 info[INF_MAX_ZLIB_MEM_USAGE] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxzlibmem);
3682#endif
3683 info[INF_TASKS] = mkf_u32(0, nb_tasks_cur);
Christopher Faulet34c5cc92016-12-06 09:15:30 +01003684 info[INF_RUN_QUEUE] = mkf_u32(0, tasks_run_queue_cur);
Willy Tarreau81036f22019-05-20 19:24:50 +02003685 info[INF_IDLE_PCT] = mkf_u32(FN_AVG, ti->idle_pct);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003686 info[INF_NODE] = mkf_str(FO_CONFIG|FN_OUTPUT|FS_SERVICE, global.node);
3687 if (global.desc)
3688 info[INF_DESCRIPTION] = mkf_str(FO_CONFIG|FN_OUTPUT|FS_SERVICE, global.desc);
Willy Tarreau00098ea2018-11-05 14:38:13 +01003689 info[INF_STOPPING] = mkf_u32(0, stopping);
3690 info[INF_JOBS] = mkf_u32(0, jobs);
William Lallemanda7199262018-11-16 16:57:20 +01003691 info[INF_UNSTOPPABLE_JOBS] = mkf_u32(0, unstoppable_jobs);
Willy Tarreau00098ea2018-11-05 14:38:13 +01003692 info[INF_LISTENERS] = mkf_u32(0, listeners);
Willy Tarreau199ad242018-11-05 16:31:22 +01003693 info[INF_ACTIVE_PEERS] = mkf_u32(0, active_peers);
Willy Tarreau2d372c22018-11-05 17:12:27 +01003694 info[INF_CONNECTED_PEERS] = mkf_u32(0, connected_peers);
Willy Tarreau13ef7732018-11-12 07:25:28 +01003695 info[INF_DROPPED_LOGS] = mkf_u32(0, dropped_logs);
Willy Tarreaubeb859a2018-11-22 18:07:59 +01003696 info[INF_BUSY_POLLING] = mkf_u32(0, !!(global.tune.options & GTUNE_BUSY_POLLING));
Baptiste Assmann333939c2019-01-21 08:34:50 +01003697 info[INF_FAILED_RESOLUTIONS] = mkf_u32(0, dns_failed_resolutions);
Willy Tarreau7cf0e452019-05-23 11:39:14 +02003698 info[INF_TOTAL_BYTES_OUT] = mkf_u64(0, global.out_bytes);
3699 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 +01003700
3701 return 1;
3702}
3703
3704/* This function dumps information onto the stream interface's read buffer.
3705 * It returns 0 as long as it does not complete, non-zero upon completion.
3706 * No state is used.
3707 */
3708static int stats_dump_info_to_buffer(struct stream_interface *si)
3709{
3710 struct appctx *appctx = __objt_appctx(si->end);
3711
3712 if (!stats_fill_info(info, INF_TOTAL_FIELDS))
3713 return 0;
3714
3715 chunk_reset(&trash);
3716
3717 if (appctx->ctx.stats.flags & STAT_FMT_TYPED)
3718 stats_dump_typed_info_fields(&trash, info);
Simon Horman05ee2132017-01-04 09:37:25 +01003719 else if (appctx->ctx.stats.flags & STAT_FMT_JSON)
3720 stats_dump_json_info_fields(&trash, info);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003721 else
3722 stats_dump_info_fields(&trash, info);
3723
Willy Tarreau06d80a92017-10-19 14:32:15 +02003724 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01003725 si_rx_room_blk(si);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003726 return 0;
3727 }
3728
3729 return 1;
3730}
3731
Simon Horman6f6bb382017-01-04 09:37:26 +01003732/* This function dumps the schema onto the stream interface's read buffer.
3733 * It returns 0 as long as it does not complete, non-zero upon completion.
3734 * No state is used.
3735 *
3736 * Integer values bouned to the range [-(2**53)+1, (2**53)-1] as
3737 * per the recommendation for interoperable integers in section 6 of RFC 7159.
3738 */
Willy Tarreau83061a82018-07-13 11:56:34 +02003739static void stats_dump_json_schema(struct buffer *out)
Simon Horman6f6bb382017-01-04 09:37:26 +01003740{
3741
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003742 int old_len = out->data;
Simon Horman6f6bb382017-01-04 09:37:26 +01003743
3744 chunk_strcat(out,
3745 "{"
3746 "\"$schema\":\"http://json-schema.org/draft-04/schema#\","
3747 "\"oneOf\":["
3748 "{"
3749 "\"title\":\"Info\","
3750 "\"type\":\"array\","
3751 "\"items\":{"
3752 "\"properties\":{"
3753 "\"title\":\"InfoItem\","
3754 "\"type\":\"object\","
3755 "\"field\":{\"$ref\":\"#/definitions/field\"},"
3756 "\"processNum\":{\"$ref\":\"#/definitions/processNum\"},"
3757 "\"tags\":{\"$ref\":\"#/definitions/tags\"},"
3758 "\"value\":{\"$ref\":\"#/definitions/typedValue\"}"
3759 "},"
3760 "\"required\":[\"field\",\"processNum\",\"tags\","
3761 "\"value\"]"
3762 "}"
3763 "},"
3764 "{"
3765 "\"title\":\"Stat\","
3766 "\"type\":\"array\","
3767 "\"items\":{"
3768 "\"title\":\"InfoItem\","
3769 "\"type\":\"object\","
3770 "\"properties\":{"
3771 "\"objType\":{"
3772 "\"enum\":[\"Frontend\",\"Backend\",\"Listener\","
3773 "\"Server\",\"Unknown\"]"
3774 "},"
3775 "\"proxyId\":{"
3776 "\"type\":\"integer\","
3777 "\"minimum\":0"
3778 "},"
3779 "\"id\":{"
3780 "\"type\":\"integer\","
3781 "\"minimum\":0"
3782 "},"
3783 "\"field\":{\"$ref\":\"#/definitions/field\"},"
3784 "\"processNum\":{\"$ref\":\"#/definitions/processNum\"},"
3785 "\"tags\":{\"$ref\":\"#/definitions/tags\"},"
3786 "\"typedValue\":{\"$ref\":\"#/definitions/typedValue\"}"
3787 "},"
3788 "\"required\":[\"objType\",\"proxyId\",\"id\","
3789 "\"field\",\"processNum\",\"tags\","
3790 "\"value\"]"
3791 "}"
3792 "},"
3793 "{"
3794 "\"title\":\"Error\","
3795 "\"type\":\"object\","
3796 "\"properties\":{"
3797 "\"errorStr\":{"
3798 "\"type\":\"string\""
3799 "},"
3800 "\"required\":[\"errorStr\"]"
3801 "}"
3802 "}"
3803 "],"
3804 "\"definitions\":{"
3805 "\"field\":{"
3806 "\"type\":\"object\","
3807 "\"pos\":{"
3808 "\"type\":\"integer\","
3809 "\"minimum\":0"
3810 "},"
3811 "\"name\":{"
3812 "\"type\":\"string\""
3813 "},"
3814 "\"required\":[\"pos\",\"name\"]"
3815 "},"
3816 "\"processNum\":{"
3817 "\"type\":\"integer\","
3818 "\"minimum\":1"
3819 "},"
3820 "\"tags\":{"
3821 "\"type\":\"object\","
3822 "\"origin\":{"
3823 "\"type\":\"string\","
3824 "\"enum\":[\"Metric\",\"Status\",\"Key\","
3825 "\"Config\",\"Product\",\"Unknown\"]"
3826 "},"
3827 "\"nature\":{"
3828 "\"type\":\"string\","
3829 "\"enum\":[\"Gauge\",\"Limit\",\"Min\",\"Max\","
3830 "\"Rate\",\"Counter\",\"Duration\","
3831 "\"Age\",\"Time\",\"Name\",\"Output\","
3832 "\"Avg\", \"Unknown\"]"
3833 "},"
3834 "\"scope\":{"
3835 "\"type\":\"string\","
3836 "\"enum\":[\"Cluster\",\"Process\",\"Service\","
3837 "\"System\",\"Unknown\"]"
3838 "},"
3839 "\"required\":[\"origin\",\"nature\",\"scope\"]"
3840 "},"
3841 "\"typedValue\":{"
3842 "\"type\":\"object\","
3843 "\"oneOf\":["
3844 "{\"$ref\":\"#/definitions/typedValue/definitions/s32Value\"},"
3845 "{\"$ref\":\"#/definitions/typedValue/definitions/s64Value\"},"
3846 "{\"$ref\":\"#/definitions/typedValue/definitions/u32Value\"},"
3847 "{\"$ref\":\"#/definitions/typedValue/definitions/u64Value\"},"
3848 "{\"$ref\":\"#/definitions/typedValue/definitions/strValue\"}"
3849 "],"
3850 "\"definitions\":{"
3851 "\"s32Value\":{"
3852 "\"properties\":{"
3853 "\"type\":{"
3854 "\"type\":\"string\","
3855 "\"enum\":[\"s32\"]"
3856 "},"
3857 "\"value\":{"
3858 "\"type\":\"integer\","
3859 "\"minimum\":-2147483648,"
3860 "\"maximum\":2147483647"
3861 "}"
3862 "},"
3863 "\"required\":[\"type\",\"value\"]"
3864 "},"
3865 "\"s64Value\":{"
3866 "\"properties\":{"
3867 "\"type\":{"
3868 "\"type\":\"string\","
3869 "\"enum\":[\"s64\"]"
3870 "},"
3871 "\"value\":{"
3872 "\"type\":\"integer\","
3873 "\"minimum\":-9007199254740991,"
3874 "\"maximum\":9007199254740991"
3875 "}"
3876 "},"
3877 "\"required\":[\"type\",\"value\"]"
3878 "},"
3879 "\"u32Value\":{"
3880 "\"properties\":{"
3881 "\"type\":{"
3882 "\"type\":\"string\","
3883 "\"enum\":[\"u32\"]"
3884 "},"
3885 "\"value\":{"
3886 "\"type\":\"integer\","
3887 "\"minimum\":0,"
3888 "\"maximum\":4294967295"
3889 "}"
3890 "},"
3891 "\"required\":[\"type\",\"value\"]"
3892 "},"
3893 "\"u64Value\":{"
3894 "\"properties\":{"
3895 "\"type\":{"
3896 "\"type\":\"string\","
3897 "\"enum\":[\"u64\"]"
3898 "},"
3899 "\"value\":{"
3900 "\"type\":\"integer\","
3901 "\"minimum\":0,"
3902 "\"maximum\":9007199254740991"
3903 "}"
3904 "},"
3905 "\"required\":[\"type\",\"value\"]"
3906 "},"
3907 "\"strValue\":{"
3908 "\"properties\":{"
3909 "\"type\":{"
3910 "\"type\":\"string\","
3911 "\"enum\":[\"str\"]"
3912 "},"
3913 "\"value\":{\"type\":\"string\"}"
3914 "},"
3915 "\"required\":[\"type\",\"value\"]"
3916 "},"
3917 "\"unknownValue\":{"
3918 "\"properties\":{"
3919 "\"type\":{"
3920 "\"type\":\"integer\","
3921 "\"minimum\":0"
3922 "},"
3923 "\"value\":{"
3924 "\"type\":\"string\","
3925 "\"enum\":[\"unknown\"]"
3926 "}"
3927 "},"
3928 "\"required\":[\"type\",\"value\"]"
3929 "}"
3930 "}"
3931 "}"
3932 "}"
3933 "}");
3934
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003935 if (old_len == out->data) {
Simon Horman6f6bb382017-01-04 09:37:26 +01003936 chunk_reset(out);
3937 chunk_appendf(out,
3938 "{\"errorStr\":\"output buffer too short\"}");
3939 }
3940}
3941
3942/* This function dumps the schema onto the stream interface's read buffer.
3943 * It returns 0 as long as it does not complete, non-zero upon completion.
3944 * No state is used.
3945 */
3946static int stats_dump_json_schema_to_buffer(struct stream_interface *si)
3947{
3948 chunk_reset(&trash);
3949
3950 stats_dump_json_schema(&trash);
3951
Willy Tarreau06d80a92017-10-19 14:32:15 +02003952 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01003953 si_rx_room_blk(si);
Simon Horman6f6bb382017-01-04 09:37:26 +01003954 return 0;
3955 }
3956
3957 return 1;
3958}
3959
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02003960static int cli_parse_clear_counters(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau89d467c2016-11-23 11:02:40 +01003961{
3962 struct proxy *px;
3963 struct server *sv;
3964 struct listener *li;
3965 int clrall = 0;
3966
3967 if (strcmp(args[2], "all") == 0)
3968 clrall = 1;
3969
3970 /* check permissions */
3971 if (!cli_has_level(appctx, ACCESS_LVL_OPER) ||
3972 (clrall && !cli_has_level(appctx, ACCESS_LVL_ADMIN)))
3973 return 1;
3974
Olivier Houchardfbc74e82017-11-24 16:54:05 +01003975 for (px = proxies_list; px; px = px->next) {
Willy Tarreau89d467c2016-11-23 11:02:40 +01003976 if (clrall) {
3977 memset(&px->be_counters, 0, sizeof(px->be_counters));
3978 memset(&px->fe_counters, 0, sizeof(px->fe_counters));
3979 }
3980 else {
3981 px->be_counters.conn_max = 0;
3982 px->be_counters.p.http.rps_max = 0;
3983 px->be_counters.sps_max = 0;
3984 px->be_counters.cps_max = 0;
3985 px->be_counters.nbpend_max = 0;
3986
3987 px->fe_counters.conn_max = 0;
3988 px->fe_counters.p.http.rps_max = 0;
3989 px->fe_counters.sps_max = 0;
3990 px->fe_counters.cps_max = 0;
Willy Tarreau89d467c2016-11-23 11:02:40 +01003991 }
3992
3993 for (sv = px->srv; sv; sv = sv->next)
3994 if (clrall)
3995 memset(&sv->counters, 0, sizeof(sv->counters));
3996 else {
3997 sv->counters.cur_sess_max = 0;
3998 sv->counters.nbpend_max = 0;
3999 sv->counters.sps_max = 0;
4000 }
4001
4002 list_for_each_entry(li, &px->conf.listeners, by_fe)
4003 if (li->counters) {
4004 if (clrall)
4005 memset(li->counters, 0, sizeof(*li->counters));
4006 else
4007 li->counters->conn_max = 0;
4008 }
4009 }
4010
4011 global.cps_max = 0;
4012 global.sps_max = 0;
Olivier Houchard00bc3cb2017-10-17 19:23:25 +02004013 global.ssl_max = 0;
4014 global.ssl_fe_keys_max = 0;
4015 global.ssl_be_keys_max = 0;
Willy Tarreaud80cb4e2018-01-20 19:30:13 +01004016
4017 memset(activity, 0, sizeof(activity));
Willy Tarreau89d467c2016-11-23 11:02:40 +01004018 return 1;
4019}
4020
4021
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004022static int cli_parse_show_info(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004023{
Willy Tarreaud25fc792016-12-16 12:33:47 +01004024 appctx->ctx.stats.scope_str = 0;
4025 appctx->ctx.stats.scope_len = 0;
4026 appctx->ctx.stats.flags = 0;
4027
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004028 if (strcmp(args[2], "typed") == 0)
4029 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
Simon Horman05ee2132017-01-04 09:37:25 +01004030 else if (strcmp(args[2], "json") == 0)
4031 appctx->ctx.stats.flags |= STAT_FMT_JSON;
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004032 return 0;
4033}
4034
4035
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004036static int cli_parse_show_stat(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau2b812e22016-11-22 16:18:05 +01004037{
Willy Tarreaud25fc792016-12-16 12:33:47 +01004038 appctx->ctx.stats.scope_str = 0;
4039 appctx->ctx.stats.scope_len = 0;
4040 appctx->ctx.stats.flags = 0;
4041
Willy Tarreau2b812e22016-11-22 16:18:05 +01004042 if (*args[2] && *args[3] && *args[4]) {
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01004043 struct proxy *px;
4044
4045 px = proxy_find_by_name(args[2], 0, 0);
4046 if (px)
4047 appctx->ctx.stats.iid = px->uuid;
4048 else
4049 appctx->ctx.stats.iid = atoi(args[2]);
4050
4051 if (!appctx->ctx.stats.iid) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004052 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01004053 appctx->ctx.cli.msg = "No such proxy.\n";
4054 appctx->st0 = CLI_ST_PRINT;
4055 return 1;
4056 }
4057
Willy Tarreau2b812e22016-11-22 16:18:05 +01004058 appctx->ctx.stats.flags |= STAT_BOUND;
Willy Tarreau2b812e22016-11-22 16:18:05 +01004059 appctx->ctx.stats.type = atoi(args[3]);
4060 appctx->ctx.stats.sid = atoi(args[4]);
4061 if (strcmp(args[5], "typed") == 0)
4062 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
Simon Horman05ee2132017-01-04 09:37:25 +01004063 else if (strcmp(args[5], "json") == 0)
4064 appctx->ctx.stats.flags |= STAT_FMT_JSON;
Willy Tarreau2b812e22016-11-22 16:18:05 +01004065 }
4066 else if (strcmp(args[2], "typed") == 0)
4067 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
Simon Horman05ee2132017-01-04 09:37:25 +01004068 else if (strcmp(args[2], "json") == 0)
4069 appctx->ctx.stats.flags |= STAT_FMT_JSON;
Willy Tarreau2b812e22016-11-22 16:18:05 +01004070
Willy Tarreau2b812e22016-11-22 16:18:05 +01004071 return 0;
4072}
4073
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004074static int cli_io_handler_dump_info(struct appctx *appctx)
4075{
4076 return stats_dump_info_to_buffer(appctx->owner);
4077}
4078
Willy Tarreau2b812e22016-11-22 16:18:05 +01004079/* This I/O handler runs as an applet embedded in a stream interface. It is
4080 * used to send raw stats over a socket.
4081 */
4082static int cli_io_handler_dump_stat(struct appctx *appctx)
4083{
Christopher Fauletef779222018-10-31 08:47:01 +01004084 return stats_dump_stat_to_buffer(appctx->owner, NULL, NULL);
Willy Tarreau2b812e22016-11-22 16:18:05 +01004085}
4086
Simon Horman6f6bb382017-01-04 09:37:26 +01004087static int cli_io_handler_dump_json_schema(struct appctx *appctx)
4088{
4089 return stats_dump_json_schema_to_buffer(appctx->owner);
4090}
4091
Willy Tarreau2b812e22016-11-22 16:18:05 +01004092/* register cli keywords */
4093static struct cli_kw_list cli_kws = {{ },{
Willy Tarreau89d467c2016-11-23 11:02:40 +01004094 { { "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 +01004095 { { "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 +01004096 { { "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 +01004097 { { "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 +01004098 {{},}
4099}};
4100
Willy Tarreau0108d902018-11-25 19:14:37 +01004101INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
4102
William Lallemand74c24fb2016-11-21 17:18:36 +01004103struct applet http_stats_applet = {
4104 .obj_type = OBJ_TYPE_APPLET,
4105 .name = "<STATS>", /* used for logging */
4106 .fct = http_stats_io_handler,
4107 .release = NULL,
4108};
4109
William Lallemand74c24fb2016-11-21 17:18:36 +01004110/*
4111 * Local variables:
4112 * c-indent-level: 8
4113 * c-basic-offset: 8
4114 * End:
4115 */