blob: 5819f459d4d1679e02226f3e5e67432fdf9dffe7 [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);
Christopher Faulet87d74c12019-09-24 16:35:10 +0200344 case FF_FLT: return chunk_appendf(out, "%f", f->u.flt);
William Lallemand74c24fb2016-11-21 17:18:36 +0100345 case FF_STR: return csv_enc_append(field_str(f, 0), 1, out) != NULL;
346 default: return chunk_appendf(out, "[INCORRECT_FIELD_TYPE_%08x]", f->type);
347 }
348}
349
350/* Emits a stats field prefixed with its type. No CSV encoding is prepared, the
351 * output is supposed to be used on its own line. Returns non-zero on success, 0
352 * if the buffer is full.
353 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200354int stats_emit_typed_data_field(struct buffer *out, const struct field *f)
William Lallemand74c24fb2016-11-21 17:18:36 +0100355{
356 switch (field_format(f, 0)) {
357 case FF_EMPTY: return 1;
358 case FF_S32: return chunk_appendf(out, "s32:%d", f->u.s32);
359 case FF_U32: return chunk_appendf(out, "u32:%u", f->u.u32);
360 case FF_S64: return chunk_appendf(out, "s64:%lld", (long long)f->u.s64);
361 case FF_U64: return chunk_appendf(out, "u64:%llu", (unsigned long long)f->u.u64);
Christopher Faulet87d74c12019-09-24 16:35:10 +0200362 case FF_FLT: return chunk_appendf(out, "flt:%f", f->u.flt);
William Lallemand74c24fb2016-11-21 17:18:36 +0100363 case FF_STR: return chunk_appendf(out, "str:%s", field_str(f, 0));
364 default: return chunk_appendf(out, "%08x:?", f->type);
365 }
366}
367
Simon Horman05ee2132017-01-04 09:37:25 +0100368/* Limit JSON integer values to the range [-(2**53)+1, (2**53)-1] as per
369 * the recommendation for interoperable integers in section 6 of RFC 7159.
370 */
371#define JSON_INT_MAX ((1ULL << 53) - 1)
372#define JSON_INT_MIN (0 - JSON_INT_MAX)
373
374/* Emits a stats field value and its type in JSON.
375 * Returns non-zero on success, 0 on error.
376 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200377int stats_emit_json_data_field(struct buffer *out, const struct field *f)
Simon Horman05ee2132017-01-04 09:37:25 +0100378{
379 int old_len;
380 char buf[20];
381 const char *type, *value = buf, *quote = "";
382
383 switch (field_format(f, 0)) {
384 case FF_EMPTY: return 1;
385 case FF_S32: type = "\"s32\"";
386 snprintf(buf, sizeof(buf), "%d", f->u.s32);
387 break;
388 case FF_U32: type = "\"u32\"";
389 snprintf(buf, sizeof(buf), "%u", f->u.u32);
390 break;
391 case FF_S64: type = "\"s64\"";
392 if (f->u.s64 < JSON_INT_MIN || f->u.s64 > JSON_INT_MAX)
393 return 0;
394 type = "\"s64\"";
395 snprintf(buf, sizeof(buf), "%lld", (long long)f->u.s64);
396 break;
397 case FF_U64: if (f->u.u64 > JSON_INT_MAX)
398 return 0;
399 type = "\"u64\"";
400 snprintf(buf, sizeof(buf), "%llu",
401 (unsigned long long) f->u.u64);
Christopher Faulet87d74c12019-09-24 16:35:10 +0200402 case FF_FLT: type = "\"flt\"";
403 snprintf(buf, sizeof(buf), "%f", f->u.flt);
Simon Horman05ee2132017-01-04 09:37:25 +0100404 break;
405 case FF_STR: type = "\"str\"";
406 value = field_str(f, 0);
407 quote = "\"";
408 break;
409 default: snprintf(buf, sizeof(buf), "%u", f->type);
410 type = buf;
411 value = "unknown";
412 quote = "\"";
413 break;
414 }
415
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200416 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100417 chunk_appendf(out, ",\"value\":{\"type\":%s,\"value\":%s%s%s}",
418 type, quote, value, quote);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200419 return !(old_len == out->data);
Simon Horman05ee2132017-01-04 09:37:25 +0100420}
421
William Lallemand74c24fb2016-11-21 17:18:36 +0100422/* Emits an encoding of the field type on 3 characters followed by a delimiter.
423 * Returns non-zero on success, 0 if the buffer is full.
424 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200425int stats_emit_field_tags(struct buffer *out, const struct field *f,
426 char delim)
William Lallemand74c24fb2016-11-21 17:18:36 +0100427{
428 char origin, nature, scope;
429
430 switch (field_origin(f, 0)) {
431 case FO_METRIC: origin = 'M'; break;
432 case FO_STATUS: origin = 'S'; break;
433 case FO_KEY: origin = 'K'; break;
434 case FO_CONFIG: origin = 'C'; break;
435 case FO_PRODUCT: origin = 'P'; break;
436 default: origin = '?'; break;
437 }
438
439 switch (field_nature(f, 0)) {
440 case FN_GAUGE: nature = 'G'; break;
441 case FN_LIMIT: nature = 'L'; break;
442 case FN_MIN: nature = 'm'; break;
443 case FN_MAX: nature = 'M'; break;
444 case FN_RATE: nature = 'R'; break;
445 case FN_COUNTER: nature = 'C'; break;
446 case FN_DURATION: nature = 'D'; break;
447 case FN_AGE: nature = 'A'; break;
448 case FN_TIME: nature = 'T'; break;
449 case FN_NAME: nature = 'N'; break;
450 case FN_OUTPUT: nature = 'O'; break;
451 case FN_AVG: nature = 'a'; break;
452 default: nature = '?'; break;
453 }
454
455 switch (field_scope(f, 0)) {
456 case FS_PROCESS: scope = 'P'; break;
457 case FS_SERVICE: scope = 'S'; break;
458 case FS_SYSTEM: scope = 's'; break;
459 case FS_CLUSTER: scope = 'C'; break;
460 default: scope = '?'; break;
461 }
462
463 return chunk_appendf(out, "%c%c%c%c", origin, nature, scope, delim);
464}
465
Simon Horman05ee2132017-01-04 09:37:25 +0100466/* Emits an encoding of the field type as JSON.
467 * Returns non-zero on success, 0 if the buffer is full.
468 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200469int stats_emit_json_field_tags(struct buffer *out, const struct field *f)
Simon Horman05ee2132017-01-04 09:37:25 +0100470{
471 const char *origin, *nature, *scope;
472 int old_len;
473
474 switch (field_origin(f, 0)) {
475 case FO_METRIC: origin = "Metric"; break;
476 case FO_STATUS: origin = "Status"; break;
477 case FO_KEY: origin = "Key"; break;
478 case FO_CONFIG: origin = "Config"; break;
479 case FO_PRODUCT: origin = "Product"; break;
480 default: origin = "Unknown"; break;
481 }
482
483 switch (field_nature(f, 0)) {
484 case FN_GAUGE: nature = "Gauge"; break;
485 case FN_LIMIT: nature = "Limit"; break;
486 case FN_MIN: nature = "Min"; break;
487 case FN_MAX: nature = "Max"; break;
488 case FN_RATE: nature = "Rate"; break;
489 case FN_COUNTER: nature = "Counter"; break;
490 case FN_DURATION: nature = "Duration"; break;
491 case FN_AGE: nature = "Age"; break;
492 case FN_TIME: nature = "Time"; break;
493 case FN_NAME: nature = "Name"; break;
494 case FN_OUTPUT: nature = "Output"; break;
495 case FN_AVG: nature = "Avg"; break;
496 default: nature = "Unknown"; break;
497 }
498
499 switch (field_scope(f, 0)) {
500 case FS_PROCESS: scope = "Process"; break;
501 case FS_SERVICE: scope = "Service"; break;
502 case FS_SYSTEM: scope = "System"; break;
503 case FS_CLUSTER: scope = "Cluster"; break;
504 default: scope = "Unknown"; break;
505 }
506
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200507 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100508 chunk_appendf(out, "\"tags\":{"
509 "\"origin\":\"%s\","
510 "\"nature\":\"%s\","
511 "\"scope\":\"%s\""
512 "}", origin, nature, scope);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200513 return !(old_len == out->data);
Simon Horman05ee2132017-01-04 09:37:25 +0100514}
William Lallemand74c24fb2016-11-21 17:18:36 +0100515
516/* Dump all fields from <stats> into <out> using CSV format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200517static int stats_dump_fields_csv(struct buffer *out,
518 const struct field *stats)
William Lallemand74c24fb2016-11-21 17:18:36 +0100519{
520 int field;
521
522 for (field = 0; field < ST_F_TOTAL_FIELDS; field++) {
523 if (!stats_emit_raw_data_field(out, &stats[field]))
524 return 0;
525 if (!chunk_strcat(out, ","))
526 return 0;
527 }
528 chunk_strcat(out, "\n");
529 return 1;
530}
531
532/* Dump all fields from <stats> into <out> using a typed "field:desc:type:value" format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200533static int stats_dump_fields_typed(struct buffer *out,
534 const struct field *stats)
William Lallemand74c24fb2016-11-21 17:18:36 +0100535{
536 int field;
537
538 for (field = 0; field < ST_F_TOTAL_FIELDS; field++) {
539 if (!stats[field].type)
540 continue;
541
542 chunk_appendf(out, "%c.%u.%u.%d.%s.%u:",
543 stats[ST_F_TYPE].u.u32 == STATS_TYPE_FE ? 'F' :
544 stats[ST_F_TYPE].u.u32 == STATS_TYPE_BE ? 'B' :
545 stats[ST_F_TYPE].u.u32 == STATS_TYPE_SO ? 'L' :
546 stats[ST_F_TYPE].u.u32 == STATS_TYPE_SV ? 'S' :
547 '?',
548 stats[ST_F_IID].u.u32, stats[ST_F_SID].u.u32,
549 field, stat_field_names[field], stats[ST_F_PID].u.u32);
550
551 if (!stats_emit_field_tags(out, &stats[field], ':'))
552 return 0;
553 if (!stats_emit_typed_data_field(out, &stats[field]))
554 return 0;
555 if (!chunk_strcat(out, "\n"))
556 return 0;
557 }
558 return 1;
559}
560
Simon Horman05ee2132017-01-04 09:37:25 +0100561/* Dump all fields from <stats> into <out> using the "show info json" format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200562static int stats_dump_json_info_fields(struct buffer *out,
Simon Horman05ee2132017-01-04 09:37:25 +0100563 const struct field *info)
564{
565 int field;
566 int started = 0;
567
568 if (!chunk_strcat(out, "["))
569 return 0;
570
571 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
572 int old_len;
573
574 if (!field_format(info, field))
575 continue;
576
577 if (started && !chunk_strcat(out, ","))
578 goto err;
579 started = 1;
580
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200581 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100582 chunk_appendf(out,
583 "{\"field\":{\"pos\":%d,\"name\":\"%s\"},"
584 "\"processNum\":%u,",
585 field, info_field_names[field],
586 info[INF_PROCESS_NUM].u.u32);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200587 if (old_len == out->data)
Simon Horman05ee2132017-01-04 09:37:25 +0100588 goto err;
589
590 if (!stats_emit_json_field_tags(out, &info[field]))
591 goto err;
592
593 if (!stats_emit_json_data_field(out, &info[field]))
594 goto err;
595
596 if (!chunk_strcat(out, "}"))
597 goto err;
598 }
599
600 if (!chunk_strcat(out, "]"))
601 goto err;
602 return 1;
603
604err:
605 chunk_reset(out);
606 chunk_appendf(out, "{\"errorStr\":\"output buffer too short\"}");
607 return 0;
608}
609
610/* Dump all fields from <stats> into <out> using a typed "field:desc:type:value" format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200611static int stats_dump_fields_json(struct buffer *out,
612 const struct field *stats,
Simon Horman05ee2132017-01-04 09:37:25 +0100613 int first_stat)
614{
615 int field;
616 int started = 0;
617
618 if (!first_stat && !chunk_strcat(out, ","))
619 return 0;
620 if (!chunk_strcat(out, "["))
621 return 0;
622
623 for (field = 0; field < ST_F_TOTAL_FIELDS; field++) {
624 const char *obj_type;
625 int old_len;
626
627 if (!stats[field].type)
628 continue;
629
630 if (started && !chunk_strcat(out, ","))
631 goto err;
632 started = 1;
633
634 switch (stats[ST_F_TYPE].u.u32) {
635 case STATS_TYPE_FE: obj_type = "Frontend"; break;
636 case STATS_TYPE_BE: obj_type = "Backend"; break;
637 case STATS_TYPE_SO: obj_type = "Listener"; break;
638 case STATS_TYPE_SV: obj_type = "Server"; break;
639 default: obj_type = "Unknown"; break;
640 }
641
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200642 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100643 chunk_appendf(out,
644 "{"
645 "\"objType\":\"%s\","
646 "\"proxyId\":%d,"
647 "\"id\":%d,"
648 "\"field\":{\"pos\":%d,\"name\":\"%s\"},"
649 "\"processNum\":%u,",
650 obj_type, stats[ST_F_IID].u.u32,
651 stats[ST_F_SID].u.u32, field,
652 stat_field_names[field], stats[ST_F_PID].u.u32);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200653 if (old_len == out->data)
Simon Horman05ee2132017-01-04 09:37:25 +0100654 goto err;
655
656 if (!stats_emit_json_field_tags(out, &stats[field]))
657 goto err;
658
659 if (!stats_emit_json_data_field(out, &stats[field]))
660 goto err;
661
662 if (!chunk_strcat(out, "}"))
663 goto err;
664 }
665
666 if (!chunk_strcat(out, "]"))
667 goto err;
668
669 return 1;
670
671err:
672 chunk_reset(out);
673 if (!first_stat)
674 chunk_strcat(out, ",");
675 chunk_appendf(out, "{\"errorStr\":\"output buffer too short\"}");
676 return 0;
677}
678
William Lallemand74c24fb2016-11-21 17:18:36 +0100679/* Dump all fields from <stats> into <out> using the HTML format. A column is
680 * reserved for the checkbox is ST_SHOWADMIN is set in <flags>. Some extra info
681 * are provided if ST_SHLGNDS is present in <flags>.
682 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200683static int stats_dump_fields_html(struct buffer *out,
684 const struct field *stats,
685 unsigned int flags)
William Lallemand74c24fb2016-11-21 17:18:36 +0100686{
Willy Tarreau83061a82018-07-13 11:56:34 +0200687 struct buffer src;
William Lallemand74c24fb2016-11-21 17:18:36 +0100688
689 if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_FE) {
690 chunk_appendf(out,
691 /* name, queue */
692 "<tr class=\"frontend\">");
693
694 if (flags & ST_SHOWADMIN) {
695 /* Column sub-heading for Enable or Disable server */
696 chunk_appendf(out, "<td></td>");
697 }
698
699 chunk_appendf(out,
700 "<td class=ac>"
701 "<a name=\"%s/Frontend\"></a>"
702 "<a class=lfsb href=\"#%s/Frontend\">Frontend</a></td>"
703 "<td colspan=3></td>"
704 "",
705 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_PXNAME));
706
707 chunk_appendf(out,
708 /* sessions rate : current */
709 "<td><u>%s<div class=tips><table class=det>"
710 "<tr><th>Current connection rate:</th><td>%s/s</td></tr>"
711 "<tr><th>Current session rate:</th><td>%s/s</td></tr>"
712 "",
713 U2H(stats[ST_F_RATE].u.u32),
714 U2H(stats[ST_F_CONN_RATE].u.u32),
715 U2H(stats[ST_F_RATE].u.u32));
716
717 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
718 chunk_appendf(out,
719 "<tr><th>Current request rate:</th><td>%s/s</td></tr>",
720 U2H(stats[ST_F_REQ_RATE].u.u32));
721
722 chunk_appendf(out,
723 "</table></div></u></td>"
724 /* sessions rate : max */
725 "<td><u>%s<div class=tips><table class=det>"
726 "<tr><th>Max connection rate:</th><td>%s/s</td></tr>"
727 "<tr><th>Max session rate:</th><td>%s/s</td></tr>"
728 "",
729 U2H(stats[ST_F_RATE_MAX].u.u32),
730 U2H(stats[ST_F_CONN_RATE_MAX].u.u32),
731 U2H(stats[ST_F_RATE_MAX].u.u32));
732
733 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
734 chunk_appendf(out,
735 "<tr><th>Max request rate:</th><td>%s/s</td></tr>",
736 U2H(stats[ST_F_REQ_RATE_MAX].u.u32));
737
738 chunk_appendf(out,
739 "</table></div></u></td>"
740 /* sessions rate : limit */
741 "<td>%s</td>",
742 LIM2A(stats[ST_F_RATE_LIM].u.u32, "-"));
743
744 chunk_appendf(out,
745 /* sessions: current, max, limit, total */
746 "<td>%s</td><td>%s</td><td>%s</td>"
747 "<td><u>%s<div class=tips><table class=det>"
748 "<tr><th>Cum. connections:</th><td>%s</td></tr>"
749 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
750 "",
751 U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SMAX].u.u32), U2H(stats[ST_F_SLIM].u.u32),
752 U2H(stats[ST_F_STOT].u.u64),
753 U2H(stats[ST_F_CONN_TOT].u.u64),
754 U2H(stats[ST_F_STOT].u.u64));
755
756 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
757 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
758 chunk_appendf(out,
759 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
760 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
761 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
762 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
763 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
764 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
765 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
766 "<tr><th>- other responses:</th><td>%s</td></tr>"
767 "<tr><th>Intercepted requests:</th><td>%s</td></tr>"
Willy Tarreaua1214a52018-12-14 14:00:25 +0100768 "<tr><th>Cache lookups:</th><td>%s</td></tr>"
769 "<tr><th>Cache hits:</th><td>%s</td><td>(%d%%)</td></tr>"
Willy Tarreau1b0f85e2018-05-28 15:12:40 +0200770 "<tr><th>Failed hdr rewrites:</th><td>%s</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +0100771 "",
772 U2H(stats[ST_F_REQ_TOT].u.u64),
773 U2H(stats[ST_F_HRSP_1XX].u.u64),
774 U2H(stats[ST_F_HRSP_2XX].u.u64),
775 U2H(stats[ST_F_COMP_RSP].u.u64),
776 stats[ST_F_HRSP_2XX].u.u64 ?
777 (int)(100 * stats[ST_F_COMP_RSP].u.u64 / stats[ST_F_HRSP_2XX].u.u64) : 0,
778 U2H(stats[ST_F_HRSP_3XX].u.u64),
779 U2H(stats[ST_F_HRSP_4XX].u.u64),
780 U2H(stats[ST_F_HRSP_5XX].u.u64),
781 U2H(stats[ST_F_HRSP_OTHER].u.u64),
Willy Tarreau1b0f85e2018-05-28 15:12:40 +0200782 U2H(stats[ST_F_INTERCEPTED].u.u64),
Willy Tarreaua1214a52018-12-14 14:00:25 +0100783 U2H(stats[ST_F_CACHE_LOOKUPS].u.u64),
784 U2H(stats[ST_F_CACHE_HITS].u.u64),
785 stats[ST_F_CACHE_LOOKUPS].u.u64 ?
786 (int)(100 * stats[ST_F_CACHE_HITS].u.u64 / stats[ST_F_CACHE_LOOKUPS].u.u64) : 0,
Willy Tarreau1b0f85e2018-05-28 15:12:40 +0200787 U2H(stats[ST_F_WREW].u.u64));
William Lallemand74c24fb2016-11-21 17:18:36 +0100788 }
789
790 chunk_appendf(out,
791 "</table></div></u></td>"
792 /* sessions: lbtot, lastsess */
793 "<td></td><td></td>"
794 /* bytes : in */
795 "<td>%s</td>"
796 "",
797 U2H(stats[ST_F_BIN].u.u64));
798
799 chunk_appendf(out,
800 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
801 "<td>%s%s<div class=tips><table class=det>"
802 "<tr><th>Response bytes in:</th><td>%s</td></tr>"
803 "<tr><th>Compression in:</th><td>%s</td></tr>"
804 "<tr><th>Compression out:</th><td>%s</td><td>(%d%%)</td></tr>"
805 "<tr><th>Compression bypass:</th><td>%s</td></tr>"
806 "<tr><th>Total bytes saved:</th><td>%s</td><td>(%d%%)</td></tr>"
807 "</table></div>%s</td>",
808 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "<u>":"",
809 U2H(stats[ST_F_BOUT].u.u64),
810 U2H(stats[ST_F_BOUT].u.u64),
811 U2H(stats[ST_F_COMP_IN].u.u64),
812 U2H(stats[ST_F_COMP_OUT].u.u64),
813 stats[ST_F_COMP_IN].u.u64 ? (int)(stats[ST_F_COMP_OUT].u.u64 * 100 / stats[ST_F_COMP_IN].u.u64) : 0,
814 U2H(stats[ST_F_COMP_BYP].u.u64),
815 U2H(stats[ST_F_COMP_IN].u.u64 - stats[ST_F_COMP_OUT].u.u64),
816 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,
817 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "</u>":"");
818
819 chunk_appendf(out,
820 /* denied: req, resp */
821 "<td>%s</td><td>%s</td>"
822 /* errors : request, connect, response */
823 "<td>%s</td><td></td><td></td>"
824 /* warnings: retries, redispatches */
825 "<td></td><td></td>"
826 /* server status : reflect frontend status */
827 "<td class=ac>%s</td>"
828 /* rest of server: nothing */
829 "<td class=ac colspan=8></td></tr>"
830 "",
831 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
832 U2H(stats[ST_F_EREQ].u.u64),
833 field_str(stats, ST_F_STATUS));
834 }
835 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_SO) {
836 chunk_appendf(out, "<tr class=socket>");
837 if (flags & ST_SHOWADMIN) {
838 /* Column sub-heading for Enable or Disable server */
839 chunk_appendf(out, "<td></td>");
840 }
841
842 chunk_appendf(out,
843 /* frontend name, listener name */
844 "<td class=ac><a name=\"%s/+%s\"></a>%s"
845 "<a class=lfsb href=\"#%s/+%s\">%s</a>"
846 "",
847 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME),
848 (flags & ST_SHLGNDS)?"<u>":"",
849 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME), field_str(stats, ST_F_SVNAME));
850
851 if (flags & ST_SHLGNDS) {
852 chunk_appendf(out, "<div class=tips>");
853
854 if (isdigit(*field_str(stats, ST_F_ADDR)))
855 chunk_appendf(out, "IPv4: %s, ", field_str(stats, ST_F_ADDR));
856 else if (*field_str(stats, ST_F_ADDR) == '[')
857 chunk_appendf(out, "IPv6: %s, ", field_str(stats, ST_F_ADDR));
858 else if (*field_str(stats, ST_F_ADDR))
859 chunk_appendf(out, "%s, ", field_str(stats, ST_F_ADDR));
860
861 /* id */
862 chunk_appendf(out, "id: %d</div>", stats[ST_F_SID].u.u32);
863 }
864
865 chunk_appendf(out,
866 /* queue */
867 "%s</td><td colspan=3></td>"
868 /* sessions rate: current, max, limit */
869 "<td colspan=3>&nbsp;</td>"
870 /* sessions: current, max, limit, total, lbtot, lastsess */
871 "<td>%s</td><td>%s</td><td>%s</td>"
872 "<td>%s</td><td>&nbsp;</td><td>&nbsp;</td>"
873 /* bytes: in, out */
874 "<td>%s</td><td>%s</td>"
875 "",
876 (flags & ST_SHLGNDS)?"</u>":"",
877 U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SMAX].u.u32), U2H(stats[ST_F_SLIM].u.u32),
878 U2H(stats[ST_F_STOT].u.u64), U2H(stats[ST_F_BIN].u.u64), U2H(stats[ST_F_BOUT].u.u64));
879
880 chunk_appendf(out,
881 /* denied: req, resp */
882 "<td>%s</td><td>%s</td>"
883 /* errors: request, connect, response */
884 "<td>%s</td><td></td><td></td>"
885 /* warnings: retries, redispatches */
886 "<td></td><td></td>"
887 /* server status: reflect listener status */
888 "<td class=ac>%s</td>"
889 /* rest of server: nothing */
890 "<td class=ac colspan=8></td></tr>"
891 "",
892 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
893 U2H(stats[ST_F_EREQ].u.u64),
894 field_str(stats, ST_F_STATUS));
895 }
896 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_SV) {
897 const char *style;
898
899 /* determine the style to use depending on the server's state,
900 * its health and weight. There isn't a 1-to-1 mapping between
901 * state and styles for the cases where the server is (still)
902 * up. The reason is that we don't want to report nolb and
903 * drain with the same color.
904 */
905
906 if (strcmp(field_str(stats, ST_F_STATUS), "DOWN") == 0 ||
907 strcmp(field_str(stats, ST_F_STATUS), "DOWN (agent)") == 0) {
908 style = "down";
909 }
910 else if (strcmp(field_str(stats, ST_F_STATUS), "DOWN ") == 0) {
911 style = "going_up";
912 }
913 else if (strcmp(field_str(stats, ST_F_STATUS), "NOLB ") == 0) {
914 style = "going_down";
915 }
916 else if (strcmp(field_str(stats, ST_F_STATUS), "NOLB") == 0) {
917 style = "nolb";
918 }
919 else if (strcmp(field_str(stats, ST_F_STATUS), "no check") == 0) {
920 style = "no_check";
921 }
922 else if (!stats[ST_F_CHKFAIL].type ||
923 stats[ST_F_CHECK_HEALTH].u.u32 == stats[ST_F_CHECK_RISE].u.u32 + stats[ST_F_CHECK_FALL].u.u32 - 1) {
924 /* no check or max health = UP */
925 if (stats[ST_F_WEIGHT].u.u32)
926 style = "up";
927 else
928 style = "draining";
929 }
930 else {
931 style = "going_down";
932 }
933
934 if (memcmp(field_str(stats, ST_F_STATUS), "MAINT", 5) == 0)
935 chunk_appendf(out, "<tr class=\"maintain\">");
936 else
937 chunk_appendf(out,
938 "<tr class=\"%s_%s\">",
939 (stats[ST_F_BCK].u.u32) ? "backup" : "active", style);
940
941
942 if (flags & ST_SHOWADMIN)
943 chunk_appendf(out,
David Harrigand3db35a2016-12-30 12:12:49 +0000944 "<td><input class='%s-checkbox' type=\"checkbox\" name=\"s\" value=\"%s\"></td>",
945 field_str(stats, ST_F_PXNAME),
William Lallemand74c24fb2016-11-21 17:18:36 +0100946 field_str(stats, ST_F_SVNAME));
947
948 chunk_appendf(out,
949 "<td class=ac><a name=\"%s/%s\"></a>%s"
950 "<a class=lfsb href=\"#%s/%s\">%s</a>"
951 "",
952 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME),
953 (flags & ST_SHLGNDS) ? "<u>" : "",
954 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME), field_str(stats, ST_F_SVNAME));
955
956 if (flags & ST_SHLGNDS) {
957 chunk_appendf(out, "<div class=tips>");
958
959 if (isdigit(*field_str(stats, ST_F_ADDR)))
960 chunk_appendf(out, "IPv4: %s, ", field_str(stats, ST_F_ADDR));
961 else if (*field_str(stats, ST_F_ADDR) == '[')
962 chunk_appendf(out, "IPv6: %s, ", field_str(stats, ST_F_ADDR));
963 else if (*field_str(stats, ST_F_ADDR))
964 chunk_appendf(out, "%s, ", field_str(stats, ST_F_ADDR));
965
966 /* id */
967 chunk_appendf(out, "id: %d", stats[ST_F_SID].u.u32);
968
969 /* cookie */
970 if (stats[ST_F_COOKIE].type) {
971 chunk_appendf(out, ", cookie: '");
972 chunk_initstr(&src, field_str(stats, ST_F_COOKIE));
973 chunk_htmlencode(out, &src);
974 chunk_appendf(out, "'");
975 }
976
977 chunk_appendf(out, "</div>");
978 }
979
980 chunk_appendf(out,
981 /* queue : current, max, limit */
982 "%s</td><td>%s</td><td>%s</td><td>%s</td>"
983 /* sessions rate : current, max, limit */
984 "<td>%s</td><td>%s</td><td></td>"
985 "",
986 (flags & ST_SHLGNDS) ? "</u>" : "",
987 U2H(stats[ST_F_QCUR].u.u32), U2H(stats[ST_F_QMAX].u.u32), LIM2A(stats[ST_F_QLIMIT].u.u32, "-"),
988 U2H(stats[ST_F_RATE].u.u32), U2H(stats[ST_F_RATE_MAX].u.u32));
989
990 chunk_appendf(out,
991 /* sessions: current, max, limit, total */
Willy Tarreau0ff699e2019-09-08 09:24:56 +0200992 "<td><u>%s<div class=tips>"
993 "<table class=det>"
994 "<tr><th>Current active connections:</th><td>%s</td></tr>"
995 "<tr><th>Current idle connections:</th><td>%s</td></tr>"
996 "<tr><th>Active connections limit:</th><td>%s</td></tr>"
997 "<tr><th>Idle connections limit:</th><td>%s</td></tr>"
998 "</table></div></u>"
999 "</td><td>%s</td><td>%s</td>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001000 "<td><u>%s<div class=tips><table class=det>"
1001 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
1002 "",
Willy Tarreau0ff699e2019-09-08 09:24:56 +02001003 U2H(stats[ST_F_SCUR].u.u32),
1004 U2H(stats[ST_F_SCUR].u.u32),
1005 U2H(stats[ST_F_SRV_ICUR].u.u32),
1006 LIM2A(stats[ST_F_SLIM].u.u32, "-"),
1007 stats[ST_F_SRV_ILIM].type ? U2H(stats[ST_F_SRV_ILIM].u.u32) : "-",
1008 U2H(stats[ST_F_SMAX].u.u32), LIM2A(stats[ST_F_SLIM].u.u32, "-"),
William Lallemand74c24fb2016-11-21 17:18:36 +01001009 U2H(stats[ST_F_STOT].u.u64),
1010 U2H(stats[ST_F_STOT].u.u64));
1011
1012 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
1013 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
1014 unsigned long long tot;
1015
1016 tot = stats[ST_F_HRSP_OTHER].u.u64;
1017 tot += stats[ST_F_HRSP_1XX].u.u64;
1018 tot += stats[ST_F_HRSP_2XX].u.u64;
1019 tot += stats[ST_F_HRSP_3XX].u.u64;
1020 tot += stats[ST_F_HRSP_4XX].u.u64;
1021 tot += stats[ST_F_HRSP_5XX].u.u64;
1022
1023 chunk_appendf(out,
Willy Tarreauf1573842018-12-14 11:35:36 +01001024 "<tr><th>New connections:</th><td>%s</td></tr>"
1025 "<tr><th>Reused connections:</th><td>%s</td><td>(%d%%)</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001026 "<tr><th>Cum. HTTP responses:</th><td>%s</td></tr>"
1027 "<tr><th>- HTTP 1xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1028 "<tr><th>- HTTP 2xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1029 "<tr><th>- HTTP 3xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1030 "<tr><th>- HTTP 4xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1031 "<tr><th>- HTTP 5xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1032 "<tr><th>- other responses:</th><td>%s</td><td>(%d%%)</td></tr>"
Willy Tarreau1b0f85e2018-05-28 15:12:40 +02001033 "<tr><th>Failed hdr rewrites:</th><td>%s</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001034 "",
Willy Tarreauf1573842018-12-14 11:35:36 +01001035 U2H(stats[ST_F_CONNECT].u.u64),
1036 U2H(stats[ST_F_REUSE].u.u64),
1037 (stats[ST_F_CONNECT].u.u64 + stats[ST_F_REUSE].u.u64) ?
1038 (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 +01001039 U2H(tot),
1040 U2H(stats[ST_F_HRSP_1XX].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_1XX].u.u64 / tot) : 0,
1041 U2H(stats[ST_F_HRSP_2XX].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_2XX].u.u64 / tot) : 0,
1042 U2H(stats[ST_F_HRSP_3XX].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_3XX].u.u64 / tot) : 0,
1043 U2H(stats[ST_F_HRSP_4XX].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_4XX].u.u64 / tot) : 0,
1044 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 +02001045 U2H(stats[ST_F_HRSP_OTHER].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_OTHER].u.u64 / tot) : 0,
1046 U2H(stats[ST_F_WREW].u.u64));
William Lallemand74c24fb2016-11-21 17:18:36 +01001047 }
1048
1049 chunk_appendf(out, "<tr><th colspan=3>Avg over last 1024 success. conn.</th></tr>");
1050 chunk_appendf(out, "<tr><th>- Queue time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_QTIME].u.u32));
1051 chunk_appendf(out, "<tr><th>- Connect time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_CTIME].u.u32));
1052 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
1053 chunk_appendf(out, "<tr><th>- Response time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_RTIME].u.u32));
1054 chunk_appendf(out, "<tr><th>- Total time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_TTIME].u.u32));
1055
1056 chunk_appendf(out,
1057 "</table></div></u></td>"
1058 /* sessions: lbtot, last */
1059 "<td>%s</td><td>%s</td>",
1060 U2H(stats[ST_F_LBTOT].u.u64),
1061 human_time(stats[ST_F_LASTSESS].u.s32, 1));
1062
1063 chunk_appendf(out,
1064 /* bytes : in, out */
1065 "<td>%s</td><td>%s</td>"
1066 /* denied: req, resp */
1067 "<td></td><td>%s</td>"
1068 /* errors : request, connect */
1069 "<td></td><td>%s</td>"
1070 /* errors : response */
1071 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
1072 /* warnings: retries, redispatches */
1073 "<td>%lld</td><td>%lld</td>"
1074 "",
1075 U2H(stats[ST_F_BIN].u.u64), U2H(stats[ST_F_BOUT].u.u64),
1076 U2H(stats[ST_F_DRESP].u.u64),
1077 U2H(stats[ST_F_ECON].u.u64),
1078 U2H(stats[ST_F_ERESP].u.u64),
1079 (long long)stats[ST_F_CLI_ABRT].u.u64,
1080 (long long)stats[ST_F_SRV_ABRT].u.u64,
1081 (long long)stats[ST_F_WRETR].u.u64,
1082 (long long)stats[ST_F_WREDIS].u.u64);
1083
1084 /* status, last change */
1085 chunk_appendf(out, "<td class=ac>");
1086
1087 /* FIXME!!!!
1088 * LASTCHG should contain the last change for *this* server and must be computed
1089 * properly above, as was done below, ie: this server if maint, otherwise ref server
1090 * if tracking. Note that ref is either local or remote depending on tracking.
1091 */
1092
1093
1094 if (memcmp(field_str(stats, ST_F_STATUS), "MAINT", 5) == 0) {
1095 chunk_appendf(out, "%s MAINT", human_time(stats[ST_F_LASTCHG].u.u32, 1));
1096 }
1097 else if (memcmp(field_str(stats, ST_F_STATUS), "no check", 5) == 0) {
1098 chunk_strcat(out, "<i>no check</i>");
1099 }
1100 else {
1101 chunk_appendf(out, "%s %s", human_time(stats[ST_F_LASTCHG].u.u32, 1), field_str(stats, ST_F_STATUS));
1102 if (memcmp(field_str(stats, ST_F_STATUS), "DOWN", 4) == 0) {
1103 if (stats[ST_F_CHECK_HEALTH].u.u32)
1104 chunk_strcat(out, " &uarr;");
1105 }
1106 else if (stats[ST_F_CHECK_HEALTH].u.u32 < stats[ST_F_CHECK_RISE].u.u32 + stats[ST_F_CHECK_FALL].u.u32 - 1)
1107 chunk_strcat(out, " &darr;");
1108 }
1109
1110 if (memcmp(field_str(stats, ST_F_STATUS), "DOWN", 4) == 0 &&
1111 stats[ST_F_AGENT_STATUS].type && !stats[ST_F_AGENT_HEALTH].u.u32) {
1112 chunk_appendf(out,
1113 "</td><td class=ac><u> %s",
1114 field_str(stats, ST_F_AGENT_STATUS));
1115
1116 if (stats[ST_F_AGENT_CODE].type)
1117 chunk_appendf(out, "/%d", stats[ST_F_AGENT_CODE].u.u32);
1118
1119 if (stats[ST_F_AGENT_DURATION].type)
1120 chunk_appendf(out, " in %lums", (long)stats[ST_F_AGENT_DURATION].u.u64);
1121
1122 chunk_appendf(out, "<div class=tips>%s", field_str(stats, ST_F_AGENT_DESC));
1123
1124 if (*field_str(stats, ST_F_LAST_AGT)) {
1125 chunk_appendf(out, ": ");
1126 chunk_initstr(&src, field_str(stats, ST_F_LAST_AGT));
1127 chunk_htmlencode(out, &src);
1128 }
1129 chunk_appendf(out, "</div></u>");
1130 }
1131 else if (stats[ST_F_CHECK_STATUS].type) {
1132 chunk_appendf(out,
1133 "</td><td class=ac><u> %s",
1134 field_str(stats, ST_F_CHECK_STATUS));
1135
1136 if (stats[ST_F_CHECK_CODE].type)
1137 chunk_appendf(out, "/%d", stats[ST_F_CHECK_CODE].u.u32);
1138
1139 if (stats[ST_F_CHECK_DURATION].type)
1140 chunk_appendf(out, " in %lums", (long)stats[ST_F_CHECK_DURATION].u.u64);
1141
1142 chunk_appendf(out, "<div class=tips>%s", field_str(stats, ST_F_CHECK_DESC));
1143
1144 if (*field_str(stats, ST_F_LAST_CHK)) {
1145 chunk_appendf(out, ": ");
1146 chunk_initstr(&src, field_str(stats, ST_F_LAST_CHK));
1147 chunk_htmlencode(out, &src);
1148 }
1149 chunk_appendf(out, "</div></u>");
1150 }
1151 else
1152 chunk_appendf(out, "</td><td>");
1153
1154 chunk_appendf(out,
1155 /* weight */
1156 "</td><td class=ac>%d</td>"
1157 /* act, bck */
1158 "<td class=ac>%s</td><td class=ac>%s</td>"
1159 "",
1160 stats[ST_F_WEIGHT].u.u32,
1161 stats[ST_F_BCK].u.u32 ? "-" : "Y",
1162 stats[ST_F_BCK].u.u32 ? "Y" : "-");
1163
1164 /* check failures: unique, fatal, down time */
1165 if (strcmp(field_str(stats, ST_F_STATUS), "MAINT (resolution)") == 0) {
1166 chunk_appendf(out, "<td class=ac colspan=3>resolution</td>");
1167 }
1168 else if (stats[ST_F_CHKFAIL].type) {
1169 chunk_appendf(out, "<td><u>%lld", (long long)stats[ST_F_CHKFAIL].u.u64);
1170
1171 if (stats[ST_F_HANAFAIL].type)
1172 chunk_appendf(out, "/%lld", (long long)stats[ST_F_HANAFAIL].u.u64);
1173
1174 chunk_appendf(out,
1175 "<div class=tips>Failed Health Checks%s</div></u></td>"
1176 "<td>%lld</td><td>%s</td>"
1177 "",
1178 stats[ST_F_HANAFAIL].type ? "/Health Analyses" : "",
1179 (long long)stats[ST_F_CHKDOWN].u.u64, human_time(stats[ST_F_DOWNTIME].u.u32, 1));
1180 }
1181 else if (strcmp(field_str(stats, ST_F_STATUS), "MAINT") != 0 && field_format(stats, ST_F_TRACKED) == FF_STR) {
1182 /* tracking a server (hence inherited maint would appear as "MAINT (via...)" */
1183 chunk_appendf(out,
1184 "<td class=ac colspan=3><a class=lfsb href=\"#%s\">via %s</a></td>",
1185 field_str(stats, ST_F_TRACKED), field_str(stats, ST_F_TRACKED));
1186 }
1187 else
1188 chunk_appendf(out, "<td colspan=3></td>");
1189
1190 /* throttle */
1191 if (stats[ST_F_THROTTLE].type)
1192 chunk_appendf(out, "<td class=ac>%d %%</td></tr>\n", stats[ST_F_THROTTLE].u.u32);
1193 else
1194 chunk_appendf(out, "<td class=ac>-</td></tr>\n");
1195 }
1196 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_BE) {
1197 chunk_appendf(out, "<tr class=\"backend\">");
1198 if (flags & ST_SHOWADMIN) {
1199 /* Column sub-heading for Enable or Disable server */
1200 chunk_appendf(out, "<td></td>");
1201 }
1202 chunk_appendf(out,
1203 "<td class=ac>"
1204 /* name */
1205 "%s<a name=\"%s/Backend\"></a>"
1206 "<a class=lfsb href=\"#%s/Backend\">Backend</a>"
1207 "",
1208 (flags & ST_SHLGNDS)?"<u>":"",
1209 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_PXNAME));
1210
1211 if (flags & ST_SHLGNDS) {
1212 /* balancing */
1213 chunk_appendf(out, "<div class=tips>balancing: %s",
1214 field_str(stats, ST_F_ALGO));
1215
1216 /* cookie */
1217 if (stats[ST_F_COOKIE].type) {
1218 chunk_appendf(out, ", cookie: '");
1219 chunk_initstr(&src, field_str(stats, ST_F_COOKIE));
1220 chunk_htmlencode(out, &src);
1221 chunk_appendf(out, "'");
1222 }
1223 chunk_appendf(out, "</div>");
1224 }
1225
1226 chunk_appendf(out,
1227 "%s</td>"
1228 /* queue : current, max */
1229 "<td>%s</td><td>%s</td><td></td>"
1230 /* sessions rate : current, max, limit */
1231 "<td>%s</td><td>%s</td><td></td>"
1232 "",
1233 (flags & ST_SHLGNDS)?"</u>":"",
1234 U2H(stats[ST_F_QCUR].u.u32), U2H(stats[ST_F_QMAX].u.u32),
1235 U2H(stats[ST_F_RATE].u.u32), U2H(stats[ST_F_RATE_MAX].u.u32));
1236
1237 chunk_appendf(out,
1238 /* sessions: current, max, limit, total */
1239 "<td>%s</td><td>%s</td><td>%s</td>"
1240 "<td><u>%s<div class=tips><table class=det>"
1241 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
1242 "",
Willy Tarreau8e0f1752016-12-12 15:07:29 +01001243 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 +01001244 U2H(stats[ST_F_STOT].u.u64),
1245 U2H(stats[ST_F_STOT].u.u64));
1246
1247 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
1248 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
1249 chunk_appendf(out,
Willy Tarreauf1573842018-12-14 11:35:36 +01001250 "<tr><th>New connections:</th><td>%s</td></tr>"
1251 "<tr><th>Reused connections:</th><td>%s</td><td>(%d%%)</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001252 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
1253 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
1254 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
1255 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
1256 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
1257 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
1258 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
1259 "<tr><th>- other responses:</th><td>%s</td></tr>"
Willy Tarreaua1214a52018-12-14 14:00:25 +01001260 "<tr><th>Cache lookups:</th><td>%s</td></tr>"
1261 "<tr><th>Cache hits:</th><td>%s</td><td>(%d%%)</td></tr>"
Willy Tarreau1b0f85e2018-05-28 15:12:40 +02001262 "<tr><th>Failed hdr rewrites:</th><td>%s</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001263 "<tr><th colspan=3>Avg over last 1024 success. conn.</th></tr>"
1264 "",
Willy Tarreauf1573842018-12-14 11:35:36 +01001265 U2H(stats[ST_F_CONNECT].u.u64),
1266 U2H(stats[ST_F_REUSE].u.u64),
1267 (stats[ST_F_CONNECT].u.u64 + stats[ST_F_REUSE].u.u64) ?
1268 (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 +01001269 U2H(stats[ST_F_REQ_TOT].u.u64),
1270 U2H(stats[ST_F_HRSP_1XX].u.u64),
1271 U2H(stats[ST_F_HRSP_2XX].u.u64),
1272 U2H(stats[ST_F_COMP_RSP].u.u64),
1273 stats[ST_F_HRSP_2XX].u.u64 ?
1274 (int)(100 * stats[ST_F_COMP_RSP].u.u64 / stats[ST_F_HRSP_2XX].u.u64) : 0,
1275 U2H(stats[ST_F_HRSP_3XX].u.u64),
1276 U2H(stats[ST_F_HRSP_4XX].u.u64),
1277 U2H(stats[ST_F_HRSP_5XX].u.u64),
Willy Tarreaufeead3a2018-12-14 13:48:44 +01001278 U2H(stats[ST_F_HRSP_OTHER].u.u64),
Willy Tarreaua1214a52018-12-14 14:00:25 +01001279 U2H(stats[ST_F_CACHE_LOOKUPS].u.u64),
1280 U2H(stats[ST_F_CACHE_HITS].u.u64),
1281 stats[ST_F_CACHE_LOOKUPS].u.u64 ?
1282 (int)(100 * stats[ST_F_CACHE_HITS].u.u64 / stats[ST_F_CACHE_LOOKUPS].u.u64) : 0,
Willy Tarreaufeead3a2018-12-14 13:48:44 +01001283 U2H(stats[ST_F_WREW].u.u64));
William Lallemand74c24fb2016-11-21 17:18:36 +01001284 }
1285
1286 chunk_appendf(out, "<tr><th>- Queue time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_QTIME].u.u32));
1287 chunk_appendf(out, "<tr><th>- Connect time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_CTIME].u.u32));
1288 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
1289 chunk_appendf(out, "<tr><th>- Response time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_RTIME].u.u32));
1290 chunk_appendf(out, "<tr><th>- Total time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_TTIME].u.u32));
1291
1292 chunk_appendf(out,
1293 "</table></div></u></td>"
1294 /* sessions: lbtot, last */
1295 "<td>%s</td><td>%s</td>"
1296 /* bytes: in */
1297 "<td>%s</td>"
1298 "",
1299 U2H(stats[ST_F_LBTOT].u.u64),
1300 human_time(stats[ST_F_LASTSESS].u.s32, 1),
1301 U2H(stats[ST_F_BIN].u.u64));
1302
1303 chunk_appendf(out,
1304 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
1305 "<td>%s%s<div class=tips><table class=det>"
1306 "<tr><th>Response bytes in:</th><td>%s</td></tr>"
1307 "<tr><th>Compression in:</th><td>%s</td></tr>"
1308 "<tr><th>Compression out:</th><td>%s</td><td>(%d%%)</td></tr>"
1309 "<tr><th>Compression bypass:</th><td>%s</td></tr>"
1310 "<tr><th>Total bytes saved:</th><td>%s</td><td>(%d%%)</td></tr>"
1311 "</table></div>%s</td>",
1312 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "<u>":"",
1313 U2H(stats[ST_F_BOUT].u.u64),
1314 U2H(stats[ST_F_BOUT].u.u64),
1315 U2H(stats[ST_F_COMP_IN].u.u64),
1316 U2H(stats[ST_F_COMP_OUT].u.u64),
1317 stats[ST_F_COMP_IN].u.u64 ? (int)(stats[ST_F_COMP_OUT].u.u64 * 100 / stats[ST_F_COMP_IN].u.u64) : 0,
1318 U2H(stats[ST_F_COMP_BYP].u.u64),
1319 U2H(stats[ST_F_COMP_IN].u.u64 - stats[ST_F_COMP_OUT].u.u64),
1320 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,
1321 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "</u>":"");
1322
1323 chunk_appendf(out,
1324 /* denied: req, resp */
1325 "<td>%s</td><td>%s</td>"
1326 /* errors : request, connect */
1327 "<td></td><td>%s</td>"
1328 /* errors : response */
1329 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
1330 /* warnings: retries, redispatches */
1331 "<td>%lld</td><td>%lld</td>"
1332 /* backend status: reflect backend status (up/down): we display UP
1333 * if the backend has known working servers or if it has no server at
1334 * all (eg: for stats). Then we display the total weight, number of
1335 * active and backups. */
1336 "<td class=ac>%s %s</td><td class=ac>&nbsp;</td><td class=ac>%d</td>"
1337 "<td class=ac>%d</td><td class=ac>%d</td>"
1338 "",
1339 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
1340 U2H(stats[ST_F_ECON].u.u64),
1341 U2H(stats[ST_F_ERESP].u.u64),
1342 (long long)stats[ST_F_CLI_ABRT].u.u64,
1343 (long long)stats[ST_F_SRV_ABRT].u.u64,
1344 (long long)stats[ST_F_WRETR].u.u64, (long long)stats[ST_F_WREDIS].u.u64,
1345 human_time(stats[ST_F_LASTCHG].u.u32, 1),
1346 strcmp(field_str(stats, ST_F_STATUS), "DOWN") ? field_str(stats, ST_F_STATUS) : "<font color=\"red\"><b>DOWN</b></font>",
1347 stats[ST_F_WEIGHT].u.u32,
1348 stats[ST_F_ACT].u.u32, stats[ST_F_BCK].u.u32);
1349
1350 chunk_appendf(out,
1351 /* rest of backend: nothing, down transitions, total downtime, throttle */
1352 "<td class=ac>&nbsp;</td><td>%d</td>"
1353 "<td>%s</td>"
1354 "<td></td>"
1355 "</tr>",
1356 stats[ST_F_CHKDOWN].u.u32,
1357 stats[ST_F_DOWNTIME].type ? human_time(stats[ST_F_DOWNTIME].u.u32, 1) : "&nbsp;");
1358 }
1359 return 1;
1360}
1361
1362int stats_dump_one_line(const struct field *stats, unsigned int flags, struct proxy *px, struct appctx *appctx)
1363{
Simon Horman05ee2132017-01-04 09:37:25 +01001364 int ret;
1365
William Lallemand74c24fb2016-11-21 17:18:36 +01001366 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN))
1367 flags |= ST_SHOWADMIN;
1368
1369 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
Simon Horman05ee2132017-01-04 09:37:25 +01001370 ret = stats_dump_fields_html(&trash, stats, flags);
William Lallemand74c24fb2016-11-21 17:18:36 +01001371 else if (appctx->ctx.stats.flags & STAT_FMT_TYPED)
Simon Horman05ee2132017-01-04 09:37:25 +01001372 ret = stats_dump_fields_typed(&trash, stats);
1373 else if (appctx->ctx.stats.flags & STAT_FMT_JSON)
1374 ret = stats_dump_fields_json(&trash, stats,
1375 !(appctx->ctx.stats.flags &
1376 STAT_STARTED));
William Lallemand74c24fb2016-11-21 17:18:36 +01001377 else
Simon Horman05ee2132017-01-04 09:37:25 +01001378 ret = stats_dump_fields_csv(&trash, stats);
1379
1380 if (ret)
1381 appctx->ctx.stats.flags |= STAT_STARTED;
1382
1383 return ret;
William Lallemand74c24fb2016-11-21 17:18:36 +01001384}
1385
1386/* Fill <stats> with the frontend statistics. <stats> is
1387 * preallocated array of length <len>. The length of the array
1388 * must be at least ST_F_TOTAL_FIELDS. If this length is less then
1389 * this value, the function returns 0, otherwise, it returns 1.
1390 */
1391int stats_fill_fe_stats(struct proxy *px, struct field *stats, int len)
1392{
1393 if (len < ST_F_TOTAL_FIELDS)
1394 return 0;
1395
1396 memset(stats, 0, sizeof(*stats) * len);
1397
1398 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1399 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, "FRONTEND");
1400 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1401 stats[ST_F_SCUR] = mkf_u32(0, px->feconn);
1402 stats[ST_F_SMAX] = mkf_u32(FN_MAX, px->fe_counters.conn_max);
1403 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, px->maxconn);
1404 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, px->fe_counters.cum_sess);
1405 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, px->fe_counters.bytes_in);
1406 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, px->fe_counters.bytes_out);
1407 stats[ST_F_DREQ] = mkf_u64(FN_COUNTER, px->fe_counters.denied_req);
1408 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, px->fe_counters.denied_resp);
1409 stats[ST_F_EREQ] = mkf_u64(FN_COUNTER, px->fe_counters.failed_req);
1410 stats[ST_F_DCON] = mkf_u64(FN_COUNTER, px->fe_counters.denied_conn);
1411 stats[ST_F_DSES] = mkf_u64(FN_COUNTER, px->fe_counters.denied_sess);
1412 stats[ST_F_STATUS] = mkf_str(FO_STATUS, px->state == PR_STREADY ? "OPEN" : px->state == PR_STFULL ? "FULL" : "STOP");
1413 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
1414 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1415 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, 0);
1416 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_FE);
1417 stats[ST_F_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_sess_per_sec));
1418 stats[ST_F_RATE_LIM] = mkf_u32(FO_CONFIG|FN_LIMIT, px->fe_sps_lim);
1419 stats[ST_F_RATE_MAX] = mkf_u32(FN_MAX, px->fe_counters.sps_max);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001420 stats[ST_F_WREW] = mkf_u64(FN_COUNTER, px->fe_counters.failed_rewrites);
William Lallemand74c24fb2016-11-21 17:18:36 +01001421
1422 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
1423 if (px->mode == PR_MODE_HTTP) {
1424 stats[ST_F_HRSP_1XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[1]);
1425 stats[ST_F_HRSP_2XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[2]);
1426 stats[ST_F_HRSP_3XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[3]);
1427 stats[ST_F_HRSP_4XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[4]);
1428 stats[ST_F_HRSP_5XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[5]);
1429 stats[ST_F_HRSP_OTHER] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[0]);
1430 stats[ST_F_INTERCEPTED] = mkf_u64(FN_COUNTER, px->fe_counters.intercepted_req);
Willy Tarreaua1214a52018-12-14 14:00:25 +01001431 stats[ST_F_CACHE_LOOKUPS] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cache_lookups);
1432 stats[ST_F_CACHE_HITS] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cache_hits);
William Lallemand74c24fb2016-11-21 17:18:36 +01001433 }
1434
1435 /* requests : req_rate, req_rate_max, req_tot, */
1436 stats[ST_F_REQ_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_req_per_sec));
1437 stats[ST_F_REQ_RATE_MAX] = mkf_u32(FN_MAX, px->fe_counters.p.http.rps_max);
1438 stats[ST_F_REQ_TOT] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cum_req);
1439
1440 /* compression: in, out, bypassed, responses */
1441 stats[ST_F_COMP_IN] = mkf_u64(FN_COUNTER, px->fe_counters.comp_in);
1442 stats[ST_F_COMP_OUT] = mkf_u64(FN_COUNTER, px->fe_counters.comp_out);
1443 stats[ST_F_COMP_BYP] = mkf_u64(FN_COUNTER, px->fe_counters.comp_byp);
1444 stats[ST_F_COMP_RSP] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.comp_rsp);
1445
1446 /* connections : conn_rate, conn_rate_max, conn_tot, conn_max */
1447 stats[ST_F_CONN_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_conn_per_sec));
1448 stats[ST_F_CONN_RATE_MAX] = mkf_u32(FN_MAX, px->fe_counters.cps_max);
1449 stats[ST_F_CONN_TOT] = mkf_u64(FN_COUNTER, px->fe_counters.cum_conn);
1450
1451 return 1;
1452}
1453
1454/* Dumps a frontend's line to the trash for the current proxy <px> and uses
1455 * the state from stream interface <si>. The caller is responsible for clearing
1456 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
1457 */
1458static int stats_dump_fe_stats(struct stream_interface *si, struct proxy *px)
1459{
1460 struct appctx *appctx = __objt_appctx(si->end);
1461
1462 if (!(px->cap & PR_CAP_FE))
1463 return 0;
1464
1465 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_FE)))
1466 return 0;
1467
1468 if (!stats_fill_fe_stats(px, stats, ST_F_TOTAL_FIELDS))
1469 return 0;
1470
1471 return stats_dump_one_line(stats, 0, px, appctx);
1472}
1473
1474/* Fill <stats> with the listener statistics. <stats> is
1475 * preallocated array of length <len>. The length of the array
1476 * must be at least ST_F_TOTAL_FIELDS. If this length is less
1477 * then this value, the function returns 0, otherwise, it
1478 * returns 1. <flags> can take the value ST_SHLGNDS.
1479 */
1480int stats_fill_li_stats(struct proxy *px, struct listener *l, int flags,
1481 struct field *stats, int len)
1482{
Willy Tarreau83061a82018-07-13 11:56:34 +02001483 struct buffer *out = get_trash_chunk();
William Lallemand74c24fb2016-11-21 17:18:36 +01001484
1485 if (len < ST_F_TOTAL_FIELDS)
1486 return 0;
1487
1488 if (!l->counters)
1489 return 0;
1490
1491 chunk_reset(out);
1492 memset(stats, 0, sizeof(*stats) * len);
1493
1494 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1495 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, l->name);
1496 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1497 stats[ST_F_SCUR] = mkf_u32(0, l->nbconn);
1498 stats[ST_F_SMAX] = mkf_u32(FN_MAX, l->counters->conn_max);
1499 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, l->maxconn);
1500 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, l->counters->cum_conn);
1501 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, l->counters->bytes_in);
1502 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, l->counters->bytes_out);
1503 stats[ST_F_DREQ] = mkf_u64(FN_COUNTER, l->counters->denied_req);
1504 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, l->counters->denied_resp);
1505 stats[ST_F_EREQ] = mkf_u64(FN_COUNTER, l->counters->failed_req);
1506 stats[ST_F_DCON] = mkf_u64(FN_COUNTER, l->counters->denied_conn);
1507 stats[ST_F_DSES] = mkf_u64(FN_COUNTER, l->counters->denied_sess);
Willy Tarreaua8cf66b2019-02-27 16:49:00 +01001508 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 +01001509 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
1510 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1511 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, l->luid);
1512 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_SO);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001513 stats[ST_F_WREW] = mkf_u64(FN_COUNTER, l->counters->failed_rewrites);
William Lallemand74c24fb2016-11-21 17:18:36 +01001514
1515 if (flags & ST_SHLGNDS) {
1516 char str[INET6_ADDRSTRLEN];
1517 int port;
1518
1519 port = get_host_port(&l->addr);
1520 switch (addr_to_str(&l->addr, str, sizeof(str))) {
1521 case AF_INET:
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_INET6:
1526 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1527 chunk_appendf(out, "[%s]:%d", str, port);
1528 break;
1529 case AF_UNIX:
1530 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, "unix");
1531 break;
1532 case -1:
1533 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1534 chunk_strcat(out, strerror(errno));
1535 break;
1536 default: /* address family not supported */
1537 break;
1538 }
1539 }
1540
1541 return 1;
1542}
1543
1544/* Dumps a line for listener <l> and proxy <px> to the trash and uses the state
1545 * from stream interface <si>, and stats flags <flags>. The caller is responsible
1546 * for clearing the trash if needed. Returns non-zero if it emits anything, zero
1547 * otherwise.
1548 */
1549static int stats_dump_li_stats(struct stream_interface *si, struct proxy *px, struct listener *l, int flags)
1550{
1551 struct appctx *appctx = __objt_appctx(si->end);
1552
1553 if (!stats_fill_li_stats(px, l, flags, stats, ST_F_TOTAL_FIELDS))
1554 return 0;
1555
1556 return stats_dump_one_line(stats, flags, px, appctx);
1557}
1558
1559enum srv_stats_state {
1560 SRV_STATS_STATE_DOWN = 0,
1561 SRV_STATS_STATE_DOWN_AGENT,
1562 SRV_STATS_STATE_GOING_UP,
1563 SRV_STATS_STATE_UP_GOING_DOWN,
1564 SRV_STATS_STATE_UP,
1565 SRV_STATS_STATE_NOLB_GOING_DOWN,
1566 SRV_STATS_STATE_NOLB,
1567 SRV_STATS_STATE_DRAIN_GOING_DOWN,
1568 SRV_STATS_STATE_DRAIN,
1569 SRV_STATS_STATE_DRAIN_AGENT,
1570 SRV_STATS_STATE_NO_CHECK,
1571
1572 SRV_STATS_STATE_COUNT, /* Must be last */
1573};
1574
1575static const char *srv_hlt_st[SRV_STATS_STATE_COUNT] = {
1576 [SRV_STATS_STATE_DOWN] = "DOWN",
1577 [SRV_STATS_STATE_DOWN_AGENT] = "DOWN (agent)",
1578 [SRV_STATS_STATE_GOING_UP] = "DOWN %d/%d",
1579 [SRV_STATS_STATE_UP_GOING_DOWN] = "UP %d/%d",
1580 [SRV_STATS_STATE_UP] = "UP",
1581 [SRV_STATS_STATE_NOLB_GOING_DOWN] = "NOLB %d/%d",
1582 [SRV_STATS_STATE_NOLB] = "NOLB",
1583 [SRV_STATS_STATE_DRAIN_GOING_DOWN] = "DRAIN %d/%d",
1584 [SRV_STATS_STATE_DRAIN] = "DRAIN",
1585 [SRV_STATS_STATE_DRAIN_AGENT] = "DRAIN (agent)",
1586 [SRV_STATS_STATE_NO_CHECK] = "no check"
1587};
1588
1589/* Fill <stats> with the server statistics. <stats> is
1590 * preallocated array of length <len>. The length of the array
1591 * must be at least ST_F_TOTAL_FIELDS. If this length is less
1592 * then this value, the function returns 0, otherwise, it
1593 * returns 1. <flags> can take the value ST_SHLGNDS.
1594 */
1595int stats_fill_sv_stats(struct proxy *px, struct server *sv, int flags,
1596 struct field *stats, int len)
1597{
1598 struct server *via, *ref;
1599 char str[INET6_ADDRSTRLEN];
Willy Tarreau83061a82018-07-13 11:56:34 +02001600 struct buffer *out = get_trash_chunk();
William Lallemand74c24fb2016-11-21 17:18:36 +01001601 enum srv_stats_state state;
1602 char *fld_status;
1603
1604 if (len < ST_F_TOTAL_FIELDS)
1605 return 0;
1606
1607 memset(stats, 0, sizeof(*stats) * len);
1608
1609 /* we have "via" which is the tracked server as described in the configuration,
1610 * and "ref" which is the checked server and the end of the chain.
1611 */
1612 via = sv->track ? sv->track : sv;
1613 ref = via;
1614 while (ref->track)
1615 ref = ref->track;
1616
Emeric Brun52a91d32017-08-31 14:41:55 +02001617 if (sv->cur_state == SRV_ST_RUNNING || sv->cur_state == SRV_ST_STARTING) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001618 if ((ref->check.state & CHK_ST_ENABLED) &&
1619 (ref->check.health < ref->check.rise + ref->check.fall - 1)) {
1620 state = SRV_STATS_STATE_UP_GOING_DOWN;
1621 } else {
1622 state = SRV_STATS_STATE_UP;
1623 }
1624
Emeric Brun52a91d32017-08-31 14:41:55 +02001625 if (sv->cur_admin & SRV_ADMF_DRAIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001626 if (ref->agent.state & CHK_ST_ENABLED)
1627 state = SRV_STATS_STATE_DRAIN_AGENT;
1628 else if (state == SRV_STATS_STATE_UP_GOING_DOWN)
1629 state = SRV_STATS_STATE_DRAIN_GOING_DOWN;
1630 else
1631 state = SRV_STATS_STATE_DRAIN;
1632 }
1633
1634 if (state == SRV_STATS_STATE_UP && !(ref->check.state & CHK_ST_ENABLED)) {
1635 state = SRV_STATS_STATE_NO_CHECK;
1636 }
1637 }
Emeric Brun52a91d32017-08-31 14:41:55 +02001638 else if (sv->cur_state == SRV_ST_STOPPING) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001639 if ((!(sv->check.state & CHK_ST_ENABLED) && !sv->track) ||
1640 (ref->check.health == ref->check.rise + ref->check.fall - 1)) {
1641 state = SRV_STATS_STATE_NOLB;
1642 } else {
1643 state = SRV_STATS_STATE_NOLB_GOING_DOWN;
1644 }
1645 }
1646 else { /* stopped */
1647 if ((ref->agent.state & CHK_ST_ENABLED) && !ref->agent.health) {
1648 state = SRV_STATS_STATE_DOWN_AGENT;
1649 } else if ((ref->check.state & CHK_ST_ENABLED) && !ref->check.health) {
1650 state = SRV_STATS_STATE_DOWN; /* DOWN */
1651 } else if ((ref->agent.state & CHK_ST_ENABLED) || (ref->check.state & CHK_ST_ENABLED)) {
1652 state = SRV_STATS_STATE_GOING_UP;
1653 } else {
1654 state = SRV_STATS_STATE_DOWN; /* DOWN, unchecked */
1655 }
1656 }
1657
1658 chunk_reset(out);
1659
1660 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1661 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, sv->id);
1662 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1663 stats[ST_F_QCUR] = mkf_u32(0, sv->nbpend);
1664 stats[ST_F_QMAX] = mkf_u32(FN_MAX, sv->counters.nbpend_max);
1665 stats[ST_F_SCUR] = mkf_u32(0, sv->cur_sess);
1666 stats[ST_F_SMAX] = mkf_u32(FN_MAX, sv->counters.cur_sess_max);
1667
1668 if (sv->maxconn)
1669 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, sv->maxconn);
1670
Willy Tarreau0ff699e2019-09-08 09:24:56 +02001671 stats[ST_F_SRV_ICUR] = mkf_u32(0, sv->curr_idle_conns);
1672 if (sv->max_idle_conns != -1)
1673 stats[ST_F_SRV_ILIM] = mkf_u32(FO_CONFIG|FN_LIMIT, sv->max_idle_conns);
1674
William Lallemand74c24fb2016-11-21 17:18:36 +01001675 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, sv->counters.cum_sess);
1676 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, sv->counters.bytes_in);
1677 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, sv->counters.bytes_out);
1678 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, sv->counters.failed_secu);
1679 stats[ST_F_ECON] = mkf_u64(FN_COUNTER, sv->counters.failed_conns);
1680 stats[ST_F_ERESP] = mkf_u64(FN_COUNTER, sv->counters.failed_resp);
1681 stats[ST_F_WRETR] = mkf_u64(FN_COUNTER, sv->counters.retries);
1682 stats[ST_F_WREDIS] = mkf_u64(FN_COUNTER, sv->counters.redispatches);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001683 stats[ST_F_WREW] = mkf_u64(FN_COUNTER, sv->counters.failed_rewrites);
Willy Tarreauf1573842018-12-14 11:35:36 +01001684 stats[ST_F_CONNECT] = mkf_u64(FN_COUNTER, sv->counters.connect);
1685 stats[ST_F_REUSE] = mkf_u64(FN_COUNTER, sv->counters.reuse);
William Lallemand74c24fb2016-11-21 17:18:36 +01001686
1687 /* status */
1688 fld_status = chunk_newstr(out);
Emeric Brun52a91d32017-08-31 14:41:55 +02001689 if (sv->cur_admin & SRV_ADMF_RMAINT)
William Lallemand74c24fb2016-11-21 17:18:36 +01001690 chunk_appendf(out, "MAINT (resolution)");
Emeric Brun52a91d32017-08-31 14:41:55 +02001691 else if (sv->cur_admin & SRV_ADMF_IMAINT)
William Lallemand74c24fb2016-11-21 17:18:36 +01001692 chunk_appendf(out, "MAINT (via %s/%s)", via->proxy->id, via->id);
Emeric Brun52a91d32017-08-31 14:41:55 +02001693 else if (sv->cur_admin & SRV_ADMF_MAINT)
William Lallemand74c24fb2016-11-21 17:18:36 +01001694 chunk_appendf(out, "MAINT");
1695 else
1696 chunk_appendf(out,
1697 srv_hlt_st[state],
Emeric Brun52a91d32017-08-31 14:41:55 +02001698 (ref->cur_state != SRV_ST_STOPPED) ? (ref->check.health - ref->check.rise + 1) : (ref->check.health),
1699 (ref->cur_state != SRV_ST_STOPPED) ? (ref->check.fall) : (ref->check.rise));
William Lallemand74c24fb2016-11-21 17:18:36 +01001700
1701 stats[ST_F_STATUS] = mkf_str(FO_STATUS, fld_status);
1702 stats[ST_F_LASTCHG] = mkf_u32(FN_AGE, now.tv_sec - sv->last_change);
Emeric Brun52a91d32017-08-31 14:41:55 +02001703 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 +01001704 stats[ST_F_ACT] = mkf_u32(FO_STATUS, (sv->flags & SRV_F_BACKUP) ? 0 : 1);
1705 stats[ST_F_BCK] = mkf_u32(FO_STATUS, (sv->flags & SRV_F_BACKUP) ? 1 : 0);
1706
1707 /* check failures: unique, fatal; last change, total downtime */
1708 if (sv->check.state & CHK_ST_ENABLED) {
1709 stats[ST_F_CHKFAIL] = mkf_u64(FN_COUNTER, sv->counters.failed_checks);
1710 stats[ST_F_CHKDOWN] = mkf_u64(FN_COUNTER, sv->counters.down_trans);
1711 stats[ST_F_DOWNTIME] = mkf_u32(FN_COUNTER, srv_downtime(sv));
1712 }
1713
1714 if (sv->maxqueue)
1715 stats[ST_F_QLIMIT] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->maxqueue);
1716
1717 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
1718 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1719 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, sv->puid);
1720
Emeric Brun52a91d32017-08-31 14:41:55 +02001721 if (sv->cur_state == SRV_ST_STARTING && !server_is_draining(sv))
William Lallemand74c24fb2016-11-21 17:18:36 +01001722 stats[ST_F_THROTTLE] = mkf_u32(FN_AVG, server_throttle_rate(sv));
1723
1724 stats[ST_F_LBTOT] = mkf_u64(FN_COUNTER, sv->counters.cum_lbconn);
1725
1726 if (sv->track) {
1727 char *fld_track = chunk_newstr(out);
1728
1729 chunk_appendf(out, "%s/%s", sv->track->proxy->id, sv->track->id);
1730 stats[ST_F_TRACKED] = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, fld_track);
1731 }
1732
1733 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_SV);
1734 stats[ST_F_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&sv->sess_per_sec));
1735 stats[ST_F_RATE_MAX] = mkf_u32(FN_MAX, sv->counters.sps_max);
1736
1737 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) {
1738 const char *fld_chksts;
1739
1740 fld_chksts = chunk_newstr(out);
1741 chunk_strcat(out, "* "); // for check in progress
1742 chunk_strcat(out, get_check_status_info(sv->check.status));
1743 if (!(sv->check.state & CHK_ST_INPROGRESS))
1744 fld_chksts += 2; // skip "* "
1745 stats[ST_F_CHECK_STATUS] = mkf_str(FN_OUTPUT, fld_chksts);
1746
1747 if (sv->check.status >= HCHK_STATUS_L57DATA)
1748 stats[ST_F_CHECK_CODE] = mkf_u32(FN_OUTPUT, sv->check.code);
1749
1750 if (sv->check.status >= HCHK_STATUS_CHECKED)
1751 stats[ST_F_CHECK_DURATION] = mkf_u64(FN_DURATION, sv->check.duration);
1752
1753 stats[ST_F_CHECK_DESC] = mkf_str(FN_OUTPUT, get_check_status_description(sv->check.status));
1754 stats[ST_F_LAST_CHK] = mkf_str(FN_OUTPUT, sv->check.desc);
1755 stats[ST_F_CHECK_RISE] = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.rise);
1756 stats[ST_F_CHECK_FALL] = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.fall);
1757 stats[ST_F_CHECK_HEALTH] = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.health);
1758 }
1759
1760 if ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) {
1761 const char *fld_chksts;
1762
1763 fld_chksts = chunk_newstr(out);
1764 chunk_strcat(out, "* "); // for check in progress
1765 chunk_strcat(out, get_check_status_info(sv->agent.status));
1766 if (!(sv->agent.state & CHK_ST_INPROGRESS))
1767 fld_chksts += 2; // skip "* "
1768 stats[ST_F_AGENT_STATUS] = mkf_str(FN_OUTPUT, fld_chksts);
1769
1770 if (sv->agent.status >= HCHK_STATUS_L57DATA)
1771 stats[ST_F_AGENT_CODE] = mkf_u32(FN_OUTPUT, sv->agent.code);
1772
1773 if (sv->agent.status >= HCHK_STATUS_CHECKED)
1774 stats[ST_F_AGENT_DURATION] = mkf_u64(FN_DURATION, sv->agent.duration);
1775
1776 stats[ST_F_AGENT_DESC] = mkf_str(FN_OUTPUT, get_check_status_description(sv->agent.status));
1777 stats[ST_F_LAST_AGT] = mkf_str(FN_OUTPUT, sv->agent.desc);
1778 stats[ST_F_AGENT_RISE] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.rise);
1779 stats[ST_F_AGENT_FALL] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.fall);
1780 stats[ST_F_AGENT_HEALTH] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.health);
1781 }
1782
1783 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
1784 if (px->mode == PR_MODE_HTTP) {
1785 stats[ST_F_HRSP_1XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[1]);
1786 stats[ST_F_HRSP_2XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[2]);
1787 stats[ST_F_HRSP_3XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[3]);
1788 stats[ST_F_HRSP_4XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[4]);
1789 stats[ST_F_HRSP_5XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[5]);
1790 stats[ST_F_HRSP_OTHER] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[0]);
1791 }
1792
1793 if (ref->observe)
1794 stats[ST_F_HANAFAIL] = mkf_u64(FN_COUNTER, sv->counters.failed_hana);
1795
1796 stats[ST_F_CLI_ABRT] = mkf_u64(FN_COUNTER, sv->counters.cli_aborts);
1797 stats[ST_F_SRV_ABRT] = mkf_u64(FN_COUNTER, sv->counters.srv_aborts);
1798 stats[ST_F_LASTSESS] = mkf_s32(FN_AGE, srv_lastsession(sv));
1799
1800 stats[ST_F_QTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.q_time, TIME_STATS_SAMPLES));
1801 stats[ST_F_CTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.c_time, TIME_STATS_SAMPLES));
1802 stats[ST_F_RTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.d_time, TIME_STATS_SAMPLES));
1803 stats[ST_F_TTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.t_time, TIME_STATS_SAMPLES));
1804
1805 if (flags & ST_SHLGNDS) {
1806 switch (addr_to_str(&sv->addr, str, sizeof(str))) {
1807 case AF_INET:
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_INET6:
1812 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
Willy Tarreau04276f32017-01-06 17:41:29 +01001813 chunk_appendf(out, "[%s]:%d", str, sv->svc_port);
William Lallemand74c24fb2016-11-21 17:18:36 +01001814 break;
1815 case AF_UNIX:
1816 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, "unix");
1817 break;
1818 case -1:
1819 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1820 chunk_strcat(out, strerror(errno));
1821 break;
1822 default: /* address family not supported */
1823 break;
1824 }
1825
1826 if (sv->cookie)
1827 stats[ST_F_COOKIE] = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, sv->cookie);
1828 }
1829
1830 return 1;
1831}
1832
1833/* Dumps a line for server <sv> and proxy <px> to the trash and uses the state
1834 * from stream interface <si>, stats flags <flags>, and server state <state>.
1835 * The caller is responsible for clearing the trash if needed. Returns non-zero
1836 * if it emits anything, zero otherwise.
1837 */
1838static int stats_dump_sv_stats(struct stream_interface *si, struct proxy *px, int flags, struct server *sv)
1839{
1840 struct appctx *appctx = __objt_appctx(si->end);
1841
1842 if (!stats_fill_sv_stats(px, sv, flags, stats, ST_F_TOTAL_FIELDS))
1843 return 0;
1844
1845 return stats_dump_one_line(stats, flags, px, appctx);
1846}
1847
1848/* Fill <stats> with the backend statistics. <stats> is
1849 * preallocated array of length <len>. The length of the array
1850 * must be at least ST_F_TOTAL_FIELDS. If this length is less
1851 * then this value, the function returns 0, otherwise, it
1852 * returns 1. <flags> can take the value ST_SHLGNDS.
1853 */
1854int stats_fill_be_stats(struct proxy *px, int flags, struct field *stats, int len)
1855{
1856 if (len < ST_F_TOTAL_FIELDS)
1857 return 0;
1858
1859 memset(stats, 0, sizeof(*stats) * len);
1860
1861 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1862 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, "BACKEND");
1863 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1864 stats[ST_F_QCUR] = mkf_u32(0, px->nbpend);
1865 stats[ST_F_QMAX] = mkf_u32(FN_MAX, px->be_counters.nbpend_max);
Thierry FOURNIER0ff98a42016-12-19 16:50:42 +01001866 stats[ST_F_SCUR] = mkf_u32(0, px->beconn);
William Lallemand74c24fb2016-11-21 17:18:36 +01001867 stats[ST_F_SMAX] = mkf_u32(FN_MAX, px->be_counters.conn_max);
1868 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, px->fullconn);
1869 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, px->be_counters.cum_conn);
1870 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, px->be_counters.bytes_in);
1871 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, px->be_counters.bytes_out);
1872 stats[ST_F_DREQ] = mkf_u64(FN_COUNTER, px->be_counters.denied_req);
1873 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, px->be_counters.denied_resp);
1874 stats[ST_F_ECON] = mkf_u64(FN_COUNTER, px->be_counters.failed_conns);
1875 stats[ST_F_ERESP] = mkf_u64(FN_COUNTER, px->be_counters.failed_resp);
1876 stats[ST_F_WRETR] = mkf_u64(FN_COUNTER, px->be_counters.retries);
1877 stats[ST_F_WREDIS] = mkf_u64(FN_COUNTER, px->be_counters.redispatches);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001878 stats[ST_F_WREW] = mkf_u64(FN_COUNTER, px->be_counters.failed_rewrites);
Willy Tarreauf1573842018-12-14 11:35:36 +01001879 stats[ST_F_CONNECT] = mkf_u64(FN_COUNTER, px->be_counters.connect);
1880 stats[ST_F_REUSE] = mkf_u64(FN_COUNTER, px->be_counters.reuse);
William Lallemand74c24fb2016-11-21 17:18:36 +01001881 stats[ST_F_STATUS] = mkf_str(FO_STATUS, (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" : "DOWN");
1882 stats[ST_F_WEIGHT] = mkf_u32(FN_AVG, (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv);
1883 stats[ST_F_ACT] = mkf_u32(0, px->srv_act);
1884 stats[ST_F_BCK] = mkf_u32(0, px->srv_bck);
1885 stats[ST_F_CHKDOWN] = mkf_u64(FN_COUNTER, px->down_trans);
1886 stats[ST_F_LASTCHG] = mkf_u32(FN_AGE, now.tv_sec - px->last_change);
1887 if (px->srv)
1888 stats[ST_F_DOWNTIME] = mkf_u32(FN_COUNTER, be_downtime(px));
1889
1890 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
1891 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1892 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, 0);
1893 stats[ST_F_LBTOT] = mkf_u64(FN_COUNTER, px->be_counters.cum_lbconn);
1894 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_BE);
1895 stats[ST_F_RATE] = mkf_u32(0, read_freq_ctr(&px->be_sess_per_sec));
1896 stats[ST_F_RATE_MAX] = mkf_u32(0, px->be_counters.sps_max);
1897
1898 if (flags & ST_SHLGNDS) {
1899 if (px->cookie_name)
1900 stats[ST_F_COOKIE] = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, px->cookie_name);
1901 stats[ST_F_ALGO] = mkf_str(FO_CONFIG|FS_SERVICE, backend_lb_algo_str(px->lbprm.algo & BE_LB_ALGO));
1902 }
1903
1904 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
1905 if (px->mode == PR_MODE_HTTP) {
1906 stats[ST_F_REQ_TOT] = mkf_u64(FN_COUNTER, px->be_counters.p.http.cum_req);
1907 stats[ST_F_HRSP_1XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[1]);
1908 stats[ST_F_HRSP_2XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[2]);
1909 stats[ST_F_HRSP_3XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[3]);
1910 stats[ST_F_HRSP_4XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[4]);
1911 stats[ST_F_HRSP_5XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[5]);
1912 stats[ST_F_HRSP_OTHER] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[0]);
Willy Tarreaua1214a52018-12-14 14:00:25 +01001913 stats[ST_F_CACHE_LOOKUPS] = mkf_u64(FN_COUNTER, px->be_counters.p.http.cache_lookups);
1914 stats[ST_F_CACHE_HITS] = mkf_u64(FN_COUNTER, px->be_counters.p.http.cache_hits);
William Lallemand74c24fb2016-11-21 17:18:36 +01001915 }
1916
1917 stats[ST_F_CLI_ABRT] = mkf_u64(FN_COUNTER, px->be_counters.cli_aborts);
1918 stats[ST_F_SRV_ABRT] = mkf_u64(FN_COUNTER, px->be_counters.srv_aborts);
1919
1920 /* compression: in, out, bypassed, responses */
1921 stats[ST_F_COMP_IN] = mkf_u64(FN_COUNTER, px->be_counters.comp_in);
1922 stats[ST_F_COMP_OUT] = mkf_u64(FN_COUNTER, px->be_counters.comp_out);
1923 stats[ST_F_COMP_BYP] = mkf_u64(FN_COUNTER, px->be_counters.comp_byp);
1924 stats[ST_F_COMP_RSP] = mkf_u64(FN_COUNTER, px->be_counters.p.http.comp_rsp);
1925 stats[ST_F_LASTSESS] = mkf_s32(FN_AGE, be_lastsession(px));
1926
1927 stats[ST_F_QTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.q_time, TIME_STATS_SAMPLES));
1928 stats[ST_F_CTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.c_time, TIME_STATS_SAMPLES));
1929 stats[ST_F_RTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.d_time, TIME_STATS_SAMPLES));
1930 stats[ST_F_TTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.t_time, TIME_STATS_SAMPLES));
1931
1932 return 1;
1933}
1934
1935/* Dumps a line for backend <px> to the trash for and uses the state from stream
1936 * interface <si> and stats flags <flags>. The caller is responsible for clearing
1937 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
1938 */
1939static int stats_dump_be_stats(struct stream_interface *si, struct proxy *px, int flags)
1940{
1941 struct appctx *appctx = __objt_appctx(si->end);
1942
1943 if (!(px->cap & PR_CAP_BE))
1944 return 0;
1945
1946 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_BE)))
1947 return 0;
1948
1949 if (!stats_fill_be_stats(px, flags, stats, ST_F_TOTAL_FIELDS))
1950 return 0;
1951
1952 return stats_dump_one_line(stats, flags, px, appctx);
1953}
1954
1955/* Dumps the HTML table header for proxy <px> to the trash for and uses the state from
1956 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
1957 * for clearing the trash if needed.
1958 */
1959static void stats_dump_html_px_hdr(struct stream_interface *si, struct proxy *px, struct uri_auth *uri)
1960{
1961 struct appctx *appctx = __objt_appctx(si->end);
1962 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
1963
1964 if (px->cap & PR_CAP_BE && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
1965 /* A form to enable/disable this proxy servers */
1966
1967 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
1968 scope_txt[0] = 0;
1969 if (appctx->ctx.stats.scope_len) {
Christopher Fauleted7a0662019-01-14 11:07:34 +01001970 const char *scope_ptr = stats_scope_ptr(appctx, si);
1971
William Lallemand74c24fb2016-11-21 17:18:36 +01001972 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Christopher Fauleted7a0662019-01-14 11:07:34 +01001973 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), scope_ptr, appctx->ctx.stats.scope_len);
William Lallemand74c24fb2016-11-21 17:18:36 +01001974 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
1975 }
1976
1977 chunk_appendf(&trash,
1978 "<form method=\"post\">");
1979 }
1980
1981 /* print a new table */
1982 chunk_appendf(&trash,
1983 "<table class=\"tbl\" width=\"100%%\">\n"
1984 "<tr class=\"titre\">"
1985 "<th class=\"pxname\" width=\"10%%\">");
1986
1987 chunk_appendf(&trash,
1988 "<a name=\"%s\"></a>%s"
1989 "<a class=px href=\"#%s\">%s</a>",
1990 px->id,
1991 (uri->flags & ST_SHLGNDS) ? "<u>":"",
1992 px->id, px->id);
1993
1994 if (uri->flags & ST_SHLGNDS) {
1995 /* cap, mode, id */
1996 chunk_appendf(&trash, "<div class=tips>cap: %s, mode: %s, id: %d",
1997 proxy_cap_str(px->cap), proxy_mode_str(px->mode),
1998 px->uuid);
1999 chunk_appendf(&trash, "</div>");
2000 }
2001
2002 chunk_appendf(&trash,
2003 "%s</th>"
2004 "<th class=\"%s\" width=\"90%%\">%s</th>"
2005 "</tr>\n"
2006 "</table>\n"
2007 "<table class=\"tbl\" width=\"100%%\">\n"
2008 "<tr class=\"titre\">",
2009 (uri->flags & ST_SHLGNDS) ? "</u>":"",
2010 px->desc ? "desc" : "empty", px->desc ? px->desc : "");
2011
2012 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
2013 /* Column heading for Enable or Disable server */
David Harrigand3db35a2016-12-30 12:12:49 +00002014 chunk_appendf(&trash,
2015 "<th rowspan=2 width=1><input type=\"checkbox\" \
2016 onclick=\"for(c in document.getElementsByClassName('%s-checkbox')) \
2017 document.getElementsByClassName('%s-checkbox').item(c).checked = this.checked\"></th>",
2018 px->id,
2019 px->id);
William Lallemand74c24fb2016-11-21 17:18:36 +01002020 }
2021
2022 chunk_appendf(&trash,
2023 "<th rowspan=2></th>"
2024 "<th colspan=3>Queue</th>"
2025 "<th colspan=3>Session rate</th><th colspan=6>Sessions</th>"
2026 "<th colspan=2>Bytes</th><th colspan=2>Denied</th>"
2027 "<th colspan=3>Errors</th><th colspan=2>Warnings</th>"
2028 "<th colspan=9>Server</th>"
2029 "</tr>\n"
2030 "<tr class=\"titre\">"
2031 "<th>Cur</th><th>Max</th><th>Limit</th>"
2032 "<th>Cur</th><th>Max</th><th>Limit</th><th>Cur</th><th>Max</th>"
2033 "<th>Limit</th><th>Total</th><th>LbTot</th><th>Last</th><th>In</th><th>Out</th>"
2034 "<th>Req</th><th>Resp</th><th>Req</th><th>Conn</th>"
2035 "<th>Resp</th><th>Retr</th><th>Redis</th>"
2036 "<th>Status</th><th>LastChk</th><th>Wght</th><th>Act</th>"
2037 "<th>Bck</th><th>Chk</th><th>Dwn</th><th>Dwntme</th>"
2038 "<th>Thrtle</th>\n"
2039 "</tr>");
2040}
2041
2042/* Dumps the HTML table trailer for proxy <px> to the trash for and uses the state from
2043 * stream interface <si>. The caller is responsible for clearing the trash if needed.
2044 */
2045static void stats_dump_html_px_end(struct stream_interface *si, struct proxy *px)
2046{
2047 struct appctx *appctx = __objt_appctx(si->end);
2048 chunk_appendf(&trash, "</table>");
2049
2050 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
2051 /* close the form used to enable/disable this proxy servers */
2052 chunk_appendf(&trash,
2053 "Choose the action to perform on the checked servers : "
2054 "<select name=action>"
2055 "<option value=\"\"></option>"
2056 "<option value=\"ready\">Set state to READY</option>"
2057 "<option value=\"drain\">Set state to DRAIN</option>"
2058 "<option value=\"maint\">Set state to MAINT</option>"
2059 "<option value=\"dhlth\">Health: disable checks</option>"
2060 "<option value=\"ehlth\">Health: enable checks</option>"
2061 "<option value=\"hrunn\">Health: force UP</option>"
2062 "<option value=\"hnolb\">Health: force NOLB</option>"
2063 "<option value=\"hdown\">Health: force DOWN</option>"
2064 "<option value=\"dagent\">Agent: disable checks</option>"
2065 "<option value=\"eagent\">Agent: enable checks</option>"
2066 "<option value=\"arunn\">Agent: force UP</option>"
2067 "<option value=\"adown\">Agent: force DOWN</option>"
2068 "<option value=\"shutdown\">Kill Sessions</option>"
2069 "</select>"
2070 "<input type=\"hidden\" name=\"b\" value=\"#%d\">"
2071 "&nbsp;<input type=\"submit\" value=\"Apply\">"
2072 "</form>",
2073 px->uuid);
2074 }
2075
2076 chunk_appendf(&trash, "<p>\n");
2077}
2078
2079/*
2080 * Dumps statistics for a proxy. The output is sent to the stream interface's
2081 * input buffer. Returns 0 if it had to stop dumping data because of lack of
2082 * buffer space, or non-zero if everything completed. This function is used
2083 * both by the CLI and the HTTP entry points, and is able to dump the output
2084 * in HTML or CSV formats. If the later, <uri> must be NULL.
2085 */
Christopher Fauletef779222018-10-31 08:47:01 +01002086int stats_dump_proxy_to_buffer(struct stream_interface *si, struct htx *htx,
2087 struct proxy *px, struct uri_auth *uri)
William Lallemand74c24fb2016-11-21 17:18:36 +01002088{
2089 struct appctx *appctx = __objt_appctx(si->end);
2090 struct stream *s = si_strm(si);
2091 struct channel *rep = si_ic(si);
2092 struct server *sv, *svs; /* server and server-state, server-state=server or server->track */
2093 struct listener *l;
2094 unsigned int flags;
2095
2096 if (uri)
2097 flags = uri->flags;
William Lallemand07a62f72017-05-24 00:57:40 +02002098 else if ((strm_li(s)->bind_conf->level & ACCESS_LVL_MASK) >= ACCESS_LVL_OPER)
William Lallemand74c24fb2016-11-21 17:18:36 +01002099 flags = ST_SHLGNDS | ST_SHNODE | ST_SHDESC;
2100 else
2101 flags = ST_SHNODE | ST_SHDESC;
2102
2103 chunk_reset(&trash);
2104
2105 switch (appctx->ctx.stats.px_st) {
2106 case STAT_PX_ST_INIT:
2107 /* we are on a new proxy */
2108 if (uri && uri->scope) {
2109 /* we have a limited scope, we have to check the proxy name */
2110 struct stat_scope *scope;
2111 int len;
2112
2113 len = strlen(px->id);
2114 scope = uri->scope;
2115
2116 while (scope) {
2117 /* match exact proxy name */
2118 if (scope->px_len == len && !memcmp(px->id, scope->px_id, len))
2119 break;
2120
2121 /* match '.' which means 'self' proxy */
2122 if (!strcmp(scope->px_id, ".") && px == s->be)
2123 break;
2124 scope = scope->next;
2125 }
2126
2127 /* proxy name not found : don't dump anything */
2128 if (scope == NULL)
2129 return 1;
2130 }
2131
2132 /* if the user has requested a limited output and the proxy
2133 * name does not match, skip it.
2134 */
Christopher Fauleted7a0662019-01-14 11:07:34 +01002135 if (appctx->ctx.stats.scope_len) {
2136 const char *scope_ptr = stats_scope_ptr(appctx, si);
2137
2138 if (strnistr(px->id, strlen(px->id), scope_ptr, appctx->ctx.stats.scope_len) == NULL)
2139 return 1;
2140 }
William Lallemand74c24fb2016-11-21 17:18:36 +01002141
2142 if ((appctx->ctx.stats.flags & STAT_BOUND) &&
2143 (appctx->ctx.stats.iid != -1) &&
2144 (px->uuid != appctx->ctx.stats.iid))
2145 return 1;
2146
2147 appctx->ctx.stats.px_st = STAT_PX_ST_TH;
2148 /* fall through */
2149
2150 case STAT_PX_ST_TH:
2151 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
2152 stats_dump_html_px_hdr(si, px, uri);
Christopher Fauletef779222018-10-31 08:47:01 +01002153 if (!stats_putchk(rep, htx, &trash))
2154 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002155 }
2156
2157 appctx->ctx.stats.px_st = STAT_PX_ST_FE;
2158 /* fall through */
2159
2160 case STAT_PX_ST_FE:
2161 /* print the frontend */
2162 if (stats_dump_fe_stats(si, px)) {
Christopher Fauletef779222018-10-31 08:47:01 +01002163 if (!stats_putchk(rep, htx, &trash))
2164 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002165 }
2166
2167 appctx->ctx.stats.l = px->conf.listeners.n;
2168 appctx->ctx.stats.px_st = STAT_PX_ST_LI;
2169 /* fall through */
2170
2171 case STAT_PX_ST_LI:
2172 /* stats.l has been initialized above */
2173 for (; appctx->ctx.stats.l != &px->conf.listeners; appctx->ctx.stats.l = l->by_fe.n) {
Christopher Fauletef779222018-10-31 08:47:01 +01002174 if (htx) {
2175 if (htx_almost_full(htx))
2176 goto full;
2177 }
2178 else {
2179 if (buffer_almost_full(&rep->buf))
2180 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002181 }
2182
2183 l = LIST_ELEM(appctx->ctx.stats.l, struct listener *, by_fe);
2184 if (!l->counters)
2185 continue;
2186
2187 if (appctx->ctx.stats.flags & STAT_BOUND) {
2188 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SO)))
2189 break;
2190
2191 if (appctx->ctx.stats.sid != -1 && l->luid != appctx->ctx.stats.sid)
2192 continue;
2193 }
2194
2195 /* print the frontend */
2196 if (stats_dump_li_stats(si, px, l, flags)) {
Christopher Fauletef779222018-10-31 08:47:01 +01002197 if (!stats_putchk(rep, htx, &trash))
2198 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002199 }
2200 }
2201
2202 appctx->ctx.stats.sv = px->srv; /* may be NULL */
2203 appctx->ctx.stats.px_st = STAT_PX_ST_SV;
2204 /* fall through */
2205
2206 case STAT_PX_ST_SV:
2207 /* stats.sv has been initialized above */
2208 for (; appctx->ctx.stats.sv != NULL; appctx->ctx.stats.sv = sv->next) {
Christopher Fauletef779222018-10-31 08:47:01 +01002209 if (htx) {
2210 if (htx_almost_full(htx))
2211 goto full;
2212 }
2213 else {
2214 if (buffer_almost_full(&rep->buf))
2215 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002216 }
2217
2218 sv = appctx->ctx.stats.sv;
2219
2220 if (appctx->ctx.stats.flags & STAT_BOUND) {
2221 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SV)))
2222 break;
2223
2224 if (appctx->ctx.stats.sid != -1 && sv->puid != appctx->ctx.stats.sid)
2225 continue;
2226 }
2227
2228 svs = sv;
2229 while (svs->track)
2230 svs = svs->track;
2231
2232 /* do not report servers which are DOWN and not changing state */
2233 if ((appctx->ctx.stats.flags & STAT_HIDE_DOWN) &&
Emeric Brun52a91d32017-08-31 14:41:55 +02002234 ((sv->cur_admin & SRV_ADMF_MAINT) || /* server is in maintenance */
2235 (sv->cur_state == SRV_ST_STOPPED && /* server is down */
William Lallemand74c24fb2016-11-21 17:18:36 +01002236 (!((svs->agent.state | svs->check.state) & CHK_ST_ENABLED) ||
2237 ((svs->agent.state & CHK_ST_ENABLED) && !svs->agent.health) ||
2238 ((svs->check.state & CHK_ST_ENABLED) && !svs->check.health))))) {
2239 continue;
2240 }
2241
2242 if (stats_dump_sv_stats(si, px, flags, sv)) {
Christopher Fauletef779222018-10-31 08:47:01 +01002243 if (!stats_putchk(rep, htx, &trash))
2244 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002245 }
2246 } /* for sv */
2247
2248 appctx->ctx.stats.px_st = STAT_PX_ST_BE;
2249 /* fall through */
2250
2251 case STAT_PX_ST_BE:
2252 /* print the backend */
2253 if (stats_dump_be_stats(si, px, flags)) {
Christopher Fauletef779222018-10-31 08:47:01 +01002254 if (!stats_putchk(rep, htx, &trash))
2255 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002256 }
2257
2258 appctx->ctx.stats.px_st = STAT_PX_ST_END;
2259 /* fall through */
2260
2261 case STAT_PX_ST_END:
2262 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
2263 stats_dump_html_px_end(si, px);
Christopher Fauletef779222018-10-31 08:47:01 +01002264 if (!stats_putchk(rep, htx, &trash))
2265 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002266 }
2267
2268 appctx->ctx.stats.px_st = STAT_PX_ST_FIN;
2269 /* fall through */
2270
2271 case STAT_PX_ST_FIN:
2272 return 1;
2273
2274 default:
2275 /* unknown state, we should put an abort() here ! */
2276 return 1;
2277 }
Christopher Fauletef779222018-10-31 08:47:01 +01002278
2279 full:
2280 si_rx_room_blk(si);
2281 return 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01002282}
2283
2284/* Dumps the HTTP stats head block to the trash for and uses the per-uri
2285 * parameters <uri>. The caller is responsible for clearing the trash if needed.
2286 */
2287static void stats_dump_html_head(struct uri_auth *uri)
2288{
2289 /* WARNING! This must fit in the first buffer !!! */
2290 chunk_appendf(&trash,
2291 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n"
2292 "\"http://www.w3.org/TR/html4/loose.dtd\">\n"
2293 "<html><head><title>Statistics Report for " PRODUCT_NAME "%s%s</title>\n"
2294 "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n"
2295 "<style type=\"text/css\"><!--\n"
2296 "body {"
2297 " font-family: arial, helvetica, sans-serif;"
2298 " font-size: 12px;"
2299 " font-weight: normal;"
2300 " color: black;"
2301 " background: white;"
2302 "}\n"
2303 "th,td {"
2304 " font-size: 10px;"
2305 "}\n"
2306 "h1 {"
2307 " font-size: x-large;"
2308 " margin-bottom: 0.5em;"
2309 "}\n"
2310 "h2 {"
2311 " font-family: helvetica, arial;"
2312 " font-size: x-large;"
2313 " font-weight: bold;"
2314 " font-style: italic;"
2315 " color: #6020a0;"
2316 " margin-top: 0em;"
2317 " margin-bottom: 0em;"
2318 "}\n"
2319 "h3 {"
2320 " font-family: helvetica, arial;"
2321 " font-size: 16px;"
2322 " font-weight: bold;"
2323 " color: #b00040;"
2324 " background: #e8e8d0;"
2325 " margin-top: 0em;"
2326 " margin-bottom: 0em;"
2327 "}\n"
2328 "li {"
2329 " margin-top: 0.25em;"
2330 " margin-right: 2em;"
2331 "}\n"
2332 ".hr {margin-top: 0.25em;"
2333 " border-color: black;"
2334 " border-bottom-style: solid;"
2335 "}\n"
2336 ".titre {background: #20D0D0;color: #000000; font-weight: bold; text-align: center;}\n"
2337 ".total {background: #20D0D0;color: #ffff80;}\n"
2338 ".frontend {background: #e8e8d0;}\n"
2339 ".socket {background: #d0d0d0;}\n"
2340 ".backend {background: #e8e8d0;}\n"
2341 ".active_down {background: #ff9090;}\n"
2342 ".active_going_up {background: #ffd020;}\n"
2343 ".active_going_down {background: #ffffa0;}\n"
2344 ".active_up {background: #c0ffc0;}\n"
2345 ".active_nolb {background: #20a0ff;}\n"
2346 ".active_draining {background: #20a0FF;}\n"
2347 ".active_no_check {background: #e0e0e0;}\n"
2348 ".backup_down {background: #ff9090;}\n"
2349 ".backup_going_up {background: #ff80ff;}\n"
2350 ".backup_going_down {background: #c060ff;}\n"
2351 ".backup_up {background: #b0d0ff;}\n"
2352 ".backup_nolb {background: #90b0e0;}\n"
2353 ".backup_draining {background: #cc9900;}\n"
2354 ".backup_no_check {background: #e0e0e0;}\n"
2355 ".maintain {background: #c07820;}\n"
2356 ".rls {letter-spacing: 0.2em; margin-right: 1px;}\n" /* right letter spacing (used for grouping digits) */
2357 "\n"
2358 "a.px:link {color: #ffff40; text-decoration: none;}"
2359 "a.px:visited {color: #ffff40; text-decoration: none;}"
2360 "a.px:hover {color: #ffffff; text-decoration: none;}"
2361 "a.lfsb:link {color: #000000; text-decoration: none;}"
2362 "a.lfsb:visited {color: #000000; text-decoration: none;}"
2363 "a.lfsb:hover {color: #505050; text-decoration: none;}"
2364 "\n"
2365 "table.tbl { border-collapse: collapse; border-style: none;}\n"
2366 "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"
2367 "table.tbl td.ac { text-align: center;}\n"
2368 "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}\n"
2369 "table.tbl th.pxname { background: #b00040; color: #ffff40; font-weight: bold; border-style: solid solid none solid; padding: 2px 3px; white-space: nowrap;}\n"
2370 "table.tbl th.empty { border-style: none; empty-cells: hide; background: white;}\n"
2371 "table.tbl th.desc { background: white; border-style: solid solid none solid; text-align: left; padding: 2px 3px;}\n"
2372 "\n"
2373 "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n"
2374 "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n"
2375 "table.lgd td.noborder { border-style: none; padding: 2px; white-space: nowrap;}\n"
2376 "table.det { border-collapse: collapse; border-style: none; }\n"
2377 "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"
2378 "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"
2379 "u {text-decoration:none; border-bottom: 1px dotted black;}\n"
2380 "div.tips {\n"
2381 " display:block;\n"
2382 " visibility:hidden;\n"
2383 " z-index:2147483647;\n"
2384 " position:absolute;\n"
2385 " padding:2px 4px 3px;\n"
2386 " background:#f0f060; color:#000000;\n"
2387 " border:1px solid #7040c0;\n"
2388 " white-space:nowrap;\n"
2389 " font-style:normal;font-size:11px;font-weight:normal;\n"
2390 " -moz-border-radius:3px;-webkit-border-radius:3px;border-radius:3px;\n"
2391 " -moz-box-shadow:gray 2px 2px 3px;-webkit-box-shadow:gray 2px 2px 3px;box-shadow:gray 2px 2px 3px;\n"
2392 "}\n"
2393 "u:hover div.tips {visibility:visible;}\n"
2394 "-->\n"
2395 "</style></head>\n",
2396 (uri->flags & ST_SHNODE) ? " on " : "",
2397 (uri->flags & ST_SHNODE) ? (uri->node ? uri->node : global.node) : ""
2398 );
2399}
2400
2401/* Dumps the HTML stats information block to the trash for and uses the state from
2402 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
2403 * for clearing the trash if needed.
2404 */
2405static void stats_dump_html_info(struct stream_interface *si, struct uri_auth *uri)
2406{
2407 struct appctx *appctx = __objt_appctx(si->end);
2408 unsigned int up = (now.tv_sec - start_date.tv_sec);
2409 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
Christopher Fauleted7a0662019-01-14 11:07:34 +01002410 const char *scope_ptr = stats_scope_ptr(appctx, si);
Willy Tarreau1713c032019-05-23 12:23:55 +02002411 unsigned long long bps = (unsigned long long)read_freq_ctr(&global.out_32bps) * 32;
2412
2413 /* Turn the bytes per second to bits per second and take care of the
2414 * usual ethernet overhead in order to help figure how far we are from
2415 * interface saturation since it's the only case which usually matters.
2416 * For this we count the total size of an Ethernet frame on the wire
2417 * including preamble and IFG (1538) for the largest TCP segment it
2418 * transports (1448 with TCP timestamps). This is not valid for smaller
2419 * packets (under-estimated), but it gives a reasonably accurate
2420 * estimation of how far we are from uplink saturation.
2421 */
2422 bps = bps * 8 * 1538 / 1448;
William Lallemand74c24fb2016-11-21 17:18:36 +01002423
2424 /* WARNING! this has to fit the first packet too.
2425 * We are around 3.5 kB, add adding entries will
2426 * become tricky if we want to support 4kB buffers !
2427 */
2428 chunk_appendf(&trash,
2429 "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">"
2430 PRODUCT_NAME "%s</a></h1>\n"
2431 "<h2>Statistics Report for pid %d%s%s%s%s</h2>\n"
2432 "<hr width=\"100%%\" class=\"hr\">\n"
2433 "<h3>&gt; General process information</h3>\n"
2434 "<table border=0><tr><td align=\"left\" nowrap width=\"1%%\">\n"
Yves Lafon95317282018-02-26 11:10:37 +01002435 "<p><b>pid = </b> %d (process #%d, nbproc = %d, nbthread = %d)<br>\n"
William Lallemand74c24fb2016-11-21 17:18:36 +01002436 "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n"
2437 "<b>system limits:</b> memmax = %s%s; ulimit-n = %d<br>\n"
2438 "<b>maxsock = </b> %d; <b>maxconn = </b> %d; <b>maxpipes = </b> %d<br>\n"
Willy Tarreau1713c032019-05-23 12:23:55 +02002439 "current conns = %d; current pipes = %d/%d; conn rate = %d/sec; bit rate = %.3f %cbps<br>\n"
William Lallemand74c24fb2016-11-21 17:18:36 +01002440 "Running tasks: %d/%d; idle = %d %%<br>\n"
2441 "</td><td align=\"center\" nowrap>\n"
2442 "<table class=\"lgd\"><tr>\n"
2443 "<td class=\"active_up\">&nbsp;</td><td class=\"noborder\">active UP </td>"
2444 "<td class=\"backup_up\">&nbsp;</td><td class=\"noborder\">backup UP </td>"
2445 "</tr><tr>\n"
2446 "<td class=\"active_going_down\"></td><td class=\"noborder\">active UP, going down </td>"
2447 "<td class=\"backup_going_down\"></td><td class=\"noborder\">backup UP, going down </td>"
2448 "</tr><tr>\n"
2449 "<td class=\"active_going_up\"></td><td class=\"noborder\">active DOWN, going up </td>"
2450 "<td class=\"backup_going_up\"></td><td class=\"noborder\">backup DOWN, going up </td>"
2451 "</tr><tr>\n"
2452 "<td class=\"active_down\"></td><td class=\"noborder\">active or backup DOWN &nbsp;</td>"
2453 "<td class=\"active_no_check\"></td><td class=\"noborder\">not checked </td>"
2454 "</tr><tr>\n"
2455 "<td class=\"maintain\"></td><td class=\"noborder\" colspan=\"3\">active or backup DOWN for maintenance (MAINT) &nbsp;</td>"
2456 "</tr><tr>\n"
2457 "<td class=\"active_draining\"></td><td class=\"noborder\" colspan=\"3\">active or backup SOFT STOPPED for maintenance &nbsp;</td>"
2458 "</tr></table>\n"
2459 "Note: \"NOLB\"/\"DRAIN\" = UP with load-balancing disabled."
2460 "</td>"
2461 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
2462 "<b>Display option:</b><ul style=\"margin-top: 0.25em;\">"
2463 "",
Willy Tarreau909b9d82019-01-04 18:20:32 +01002464 (uri->flags & ST_HIDEVER) ? "" : (stats_version_string),
William Lallemand74c24fb2016-11-21 17:18:36 +01002465 pid, (uri->flags & ST_SHNODE) ? " on " : "",
2466 (uri->flags & ST_SHNODE) ? (uri->node ? uri->node : global.node) : "",
2467 (uri->flags & ST_SHDESC) ? ": " : "",
2468 (uri->flags & ST_SHDESC) ? (uri->desc ? uri->desc : global.desc) : "",
Yves Lafon95317282018-02-26 11:10:37 +01002469 pid, relative_pid, global.nbproc, global.nbthread,
William Lallemand74c24fb2016-11-21 17:18:36 +01002470 up / 86400, (up % 86400) / 3600,
2471 (up % 3600) / 60, (up % 60),
2472 global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited",
2473 global.rlimit_memmax ? " MB" : "",
2474 global.rlimit_nofile,
2475 global.maxsock, global.maxconn, global.maxpipes,
2476 actconn, pipes_used, pipes_used+pipes_free, read_freq_ctr(&global.conn_per_sec),
Willy Tarreau1713c032019-05-23 12:23:55 +02002477 bps >= 1000000000UL ? (bps / 1000000000.0) : bps >= 1000000UL ? (bps / 1000000.0) : (bps / 1000.0),
2478 bps >= 1000000000UL ? 'G' : bps >= 1000000UL ? 'M' : 'k',
Willy Tarreau81036f22019-05-20 19:24:50 +02002479 tasks_run_queue_cur, nb_tasks_cur, ti->idle_pct
William Lallemand74c24fb2016-11-21 17:18:36 +01002480 );
2481
2482 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Christopher Fauleted7a0662019-01-14 11:07:34 +01002483 memcpy(scope_txt, scope_ptr, appctx->ctx.stats.scope_len);
William Lallemand74c24fb2016-11-21 17:18:36 +01002484 scope_txt[appctx->ctx.stats.scope_len] = '\0';
2485
2486 chunk_appendf(&trash,
2487 "<li><form method=\"GET\">Scope : <input value=\"%s\" name=\"" STAT_SCOPE_INPUT_NAME "\" size=\"8\" maxlength=\"%d\" tabindex=\"1\"/></form>\n",
2488 (appctx->ctx.stats.scope_len > 0) ? scope_txt : "",
2489 STAT_SCOPE_TXT_MAXLEN);
2490
2491 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
2492 scope_txt[0] = 0;
2493 if (appctx->ctx.stats.scope_len) {
2494 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Christopher Fauleted7a0662019-01-14 11:07:34 +01002495 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), scope_ptr, appctx->ctx.stats.scope_len);
William Lallemand74c24fb2016-11-21 17:18:36 +01002496 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
2497 }
2498
2499 if (appctx->ctx.stats.flags & STAT_HIDE_DOWN)
2500 chunk_appendf(&trash,
2501 "<li><a href=\"%s%s%s%s\">Show all servers</a><br>\n",
2502 uri->uri_prefix,
2503 "",
2504 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2505 scope_txt);
2506 else
2507 chunk_appendf(&trash,
2508 "<li><a href=\"%s%s%s%s\">Hide 'DOWN' servers</a><br>\n",
2509 uri->uri_prefix,
2510 ";up",
2511 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2512 scope_txt);
2513
2514 if (uri->refresh > 0) {
2515 if (appctx->ctx.stats.flags & STAT_NO_REFRESH)
2516 chunk_appendf(&trash,
2517 "<li><a href=\"%s%s%s%s\">Enable refresh</a><br>\n",
2518 uri->uri_prefix,
2519 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2520 "",
2521 scope_txt);
2522 else
2523 chunk_appendf(&trash,
2524 "<li><a href=\"%s%s%s%s\">Disable refresh</a><br>\n",
2525 uri->uri_prefix,
2526 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2527 ";norefresh",
2528 scope_txt);
2529 }
2530
2531 chunk_appendf(&trash,
2532 "<li><a href=\"%s%s%s%s\">Refresh now</a><br>\n",
2533 uri->uri_prefix,
2534 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2535 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2536 scope_txt);
2537
2538 chunk_appendf(&trash,
2539 "<li><a href=\"%s;csv%s%s\">CSV export</a><br>\n",
2540 uri->uri_prefix,
2541 (uri->refresh > 0) ? ";norefresh" : "",
2542 scope_txt);
2543
2544 chunk_appendf(&trash,
2545 "</ul></td>"
2546 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
2547 "<b>External resources:</b><ul style=\"margin-top: 0.25em;\">\n"
2548 "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>\n"
2549 "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>\n"
2550 "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>\n"
2551 "</ul>"
2552 "</td>"
2553 "</tr></table>\n"
2554 ""
2555 );
2556
2557 if (appctx->ctx.stats.st_code) {
2558 switch (appctx->ctx.stats.st_code) {
2559 case STAT_STATUS_DONE:
2560 chunk_appendf(&trash,
2561 "<p><div class=active_up>"
2562 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2563 "Action processed successfully."
2564 "</div>\n", uri->uri_prefix,
2565 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2566 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2567 scope_txt);
2568 break;
2569 case STAT_STATUS_NONE:
2570 chunk_appendf(&trash,
2571 "<p><div class=active_going_down>"
2572 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2573 "Nothing has changed."
2574 "</div>\n", uri->uri_prefix,
2575 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2576 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2577 scope_txt);
2578 break;
2579 case STAT_STATUS_PART:
2580 chunk_appendf(&trash,
2581 "<p><div class=active_going_down>"
2582 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2583 "Action partially processed.<br>"
2584 "Some server names are probably unknown or ambiguous (duplicated names in the backend)."
2585 "</div>\n", uri->uri_prefix,
2586 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2587 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2588 scope_txt);
2589 break;
2590 case STAT_STATUS_ERRP:
2591 chunk_appendf(&trash,
2592 "<p><div class=active_down>"
2593 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2594 "Action not processed because of invalid parameters."
2595 "<ul>"
2596 "<li>The action is maybe unknown.</li>"
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01002597 "<li>Invalid key parameter (empty or too long).</li>"
William Lallemand74c24fb2016-11-21 17:18:36 +01002598 "<li>The backend name is probably unknown or ambiguous (duplicated names).</li>"
2599 "<li>Some server names are probably unknown or ambiguous (duplicated names in the backend).</li>"
2600 "</ul>"
2601 "</div>\n", uri->uri_prefix,
2602 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2603 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2604 scope_txt);
2605 break;
2606 case STAT_STATUS_EXCD:
2607 chunk_appendf(&trash,
2608 "<p><div class=active_down>"
2609 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2610 "<b>Action not processed : the buffer couldn't store all the data.<br>"
2611 "You should retry with less servers at a time.</b>"
2612 "</div>\n", uri->uri_prefix,
2613 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2614 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2615 scope_txt);
2616 break;
2617 case STAT_STATUS_DENY:
2618 chunk_appendf(&trash,
2619 "<p><div class=active_down>"
2620 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2621 "<b>Action denied.</b>"
2622 "</div>\n", uri->uri_prefix,
2623 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2624 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2625 scope_txt);
2626 break;
Christopher Faulet3c2ecf72019-02-27 16:41:27 +01002627 case STAT_STATUS_IVAL:
2628 chunk_appendf(&trash,
2629 "<p><div class=active_down>"
2630 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2631 "<b>Invalid requests (unsupported method or chunked encoded request).</b>"
2632 "</div>\n", uri->uri_prefix,
2633 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2634 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2635 scope_txt);
2636 break;
William Lallemand74c24fb2016-11-21 17:18:36 +01002637 default:
2638 chunk_appendf(&trash,
2639 "<p><div class=active_no_check>"
2640 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2641 "Unexpected result."
2642 "</div>\n", uri->uri_prefix,
2643 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2644 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2645 scope_txt);
2646 }
2647 chunk_appendf(&trash, "<p>\n");
2648 }
2649}
2650
2651/* Dumps the HTML stats trailer block to the trash. The caller is responsible
2652 * for clearing the trash if needed.
2653 */
2654static void stats_dump_html_end()
2655{
2656 chunk_appendf(&trash, "</body></html>\n");
2657}
2658
Simon Horman05ee2132017-01-04 09:37:25 +01002659/* Dumps the stats JSON header to the trash buffer which. The caller is responsible
2660 * for clearing it if needed.
2661 */
2662static void stats_dump_json_header()
2663{
2664 chunk_strcat(&trash, "[");
2665}
2666
2667
2668/* Dumps the JSON stats trailer block to the trash. The caller is responsible
2669 * for clearing the trash if needed.
2670 */
2671static void stats_dump_json_end()
2672{
2673 chunk_strcat(&trash, "]");
2674}
2675
William Lallemand74c24fb2016-11-21 17:18:36 +01002676/* This function dumps statistics onto the stream interface's read buffer in
2677 * either CSV or HTML format. <uri> contains some HTML-specific parameters that
2678 * are ignored for CSV format (hence <uri> may be NULL there). It returns 0 if
2679 * it had to stop writing data and an I/O is needed, 1 if the dump is finished
2680 * and the stream must be closed, or -1 in case of any error. This function is
2681 * used by both the CLI and the HTTP handlers.
2682 */
Christopher Fauletef779222018-10-31 08:47:01 +01002683static int stats_dump_stat_to_buffer(struct stream_interface *si, struct htx *htx,
2684 struct uri_auth *uri)
William Lallemand74c24fb2016-11-21 17:18:36 +01002685{
2686 struct appctx *appctx = __objt_appctx(si->end);
2687 struct channel *rep = si_ic(si);
2688 struct proxy *px;
2689
2690 chunk_reset(&trash);
2691
2692 switch (appctx->st2) {
2693 case STAT_ST_INIT:
2694 appctx->st2 = STAT_ST_HEAD; /* let's start producing data */
2695 /* fall through */
2696
2697 case STAT_ST_HEAD:
2698 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
2699 stats_dump_html_head(uri);
Simon Horman05ee2132017-01-04 09:37:25 +01002700 else if (appctx->ctx.stats.flags & STAT_FMT_JSON)
Willy Tarreau9d7fb632017-04-11 07:53:04 +02002701 stats_dump_json_header();
William Lallemand74c24fb2016-11-21 17:18:36 +01002702 else if (!(appctx->ctx.stats.flags & STAT_FMT_TYPED))
2703 stats_dump_csv_header();
2704
Christopher Fauletef779222018-10-31 08:47:01 +01002705 if (!stats_putchk(rep, htx, &trash))
2706 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002707
2708 appctx->st2 = STAT_ST_INFO;
2709 /* fall through */
2710
2711 case STAT_ST_INFO:
2712 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
2713 stats_dump_html_info(si, uri);
Christopher Fauletef779222018-10-31 08:47:01 +01002714 if (!stats_putchk(rep, htx, &trash))
2715 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002716 }
2717
Olivier Houchardfbc74e82017-11-24 16:54:05 +01002718 appctx->ctx.stats.px = proxies_list;
William Lallemand74c24fb2016-11-21 17:18:36 +01002719 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
2720 appctx->st2 = STAT_ST_LIST;
2721 /* fall through */
2722
2723 case STAT_ST_LIST:
2724 /* dump proxies */
2725 while (appctx->ctx.stats.px) {
Christopher Fauletef779222018-10-31 08:47:01 +01002726 if (htx) {
2727 if (htx_almost_full(htx))
2728 goto full;
2729 }
2730 else {
2731 if (buffer_almost_full(&rep->buf))
2732 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002733 }
2734
2735 px = appctx->ctx.stats.px;
2736 /* skip the disabled proxies, global frontend and non-networked ones */
2737 if (px->state != PR_STSTOPPED && px->uuid > 0 && (px->cap & (PR_CAP_FE | PR_CAP_BE)))
Christopher Fauletef779222018-10-31 08:47:01 +01002738 if (stats_dump_proxy_to_buffer(si, htx, px, uri) == 0)
William Lallemand74c24fb2016-11-21 17:18:36 +01002739 return 0;
2740
2741 appctx->ctx.stats.px = px->next;
2742 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
2743 }
2744 /* here, we just have reached the last proxy */
2745
2746 appctx->st2 = STAT_ST_END;
2747 /* fall through */
2748
2749 case STAT_ST_END:
Simon Horman05ee2132017-01-04 09:37:25 +01002750 if (appctx->ctx.stats.flags & (STAT_FMT_HTML|STAT_FMT_JSON)) {
2751 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
2752 stats_dump_html_end();
2753 else
2754 stats_dump_json_end();
Christopher Fauletef779222018-10-31 08:47:01 +01002755 if (!stats_putchk(rep, htx, &trash))
2756 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002757 }
2758
2759 appctx->st2 = STAT_ST_FIN;
2760 /* fall through */
2761
2762 case STAT_ST_FIN:
2763 return 1;
2764
2765 default:
2766 /* unknown state ! */
2767 appctx->st2 = STAT_ST_FIN;
2768 return -1;
2769 }
Christopher Fauletef779222018-10-31 08:47:01 +01002770
2771 full:
2772 si_rx_room_blk(si);
2773 return 0;
2774
William Lallemand74c24fb2016-11-21 17:18:36 +01002775}
2776
2777/* We reached the stats page through a POST request. The appctx is
2778 * expected to have already been allocated by the caller.
2779 * Parse the posted data and enable/disable servers if necessary.
2780 * Returns 1 if request was parsed or zero if it needs more data.
2781 */
2782static int stats_process_http_post(struct stream_interface *si)
2783{
2784 struct stream *s = si_strm(si);
2785 struct appctx *appctx = objt_appctx(si->end);
2786
2787 struct proxy *px = NULL;
2788 struct server *sv = NULL;
2789
2790 char key[LINESIZE];
2791 int action = ST_ADM_ACTION_NONE;
2792 int reprocess = 0;
2793
2794 int total_servers = 0;
2795 int altered_servers = 0;
2796
2797 char *first_param, *cur_param, *next_param, *end_params;
2798 char *st_cur_param = NULL;
2799 char *st_next_param = NULL;
2800
Christopher Fauleta3618372018-12-13 21:59:56 +01002801 struct buffer *temp = get_trash_chunk();
William Lallemand74c24fb2016-11-21 17:18:36 +01002802
Christopher Fauleta3618372018-12-13 21:59:56 +01002803 if (IS_HTX_STRM(s)) {
2804 struct htx *htx = htxbuf(&s->req.buf);
2805 struct htx_blk *blk;
Christopher Fauleta3618372018-12-13 21:59:56 +01002806
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01002807 /* we need more data */
2808 if (s->txn->req.msg_state < HTTP_MSG_DONE) {
2809 /* check if we can receive more */
2810 if (htx_free_data_space(htx) <= global.tune.maxrewrite) {
2811 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
2812 goto out;
Christopher Fauleta3618372018-12-13 21:59:56 +01002813 }
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01002814 goto wait;
Christopher Fauleta3618372018-12-13 21:59:56 +01002815 }
William Lallemand74c24fb2016-11-21 17:18:36 +01002816
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01002817 /* The request was fully received. Copy data */
Christopher Faulet8fa60e42019-05-23 11:04:05 +02002818 blk = htx_get_head_blk(htx);
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01002819 while (blk) {
Christopher Fauleta3618372018-12-13 21:59:56 +01002820 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Fauleta3618372018-12-13 21:59:56 +01002821
Christopher Faulet54b5e212019-06-04 10:08:28 +02002822 if (type == HTX_BLK_EOM || type == HTX_BLK_TLR || type == HTX_BLK_EOT)
Christopher Fauleta3618372018-12-13 21:59:56 +01002823 break;
2824 if (type == HTX_BLK_DATA) {
2825 struct ist v = htx_get_blk_value(htx, blk);
2826
2827 if (!chunk_memcat(temp, v.ptr, v.len)) {
2828 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
2829 goto out;
2830 }
2831 }
Christopher Fauleted7a0662019-01-14 11:07:34 +01002832 blk = htx_get_next_blk(htx, blk);
Christopher Fauleta3618372018-12-13 21:59:56 +01002833 }
2834 }
2835 else {
2836 int reql;
2837
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01002838 /* we need more data */
2839 if (s->txn->req.msg_state < HTTP_MSG_DONE) {
2840 /* check if we can receive more */
2841 if (c_room(&s->req) <= global.tune.maxrewrite) {
2842 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
2843 goto out;
2844 }
2845 goto wait;
Christopher Fauleta3618372018-12-13 21:59:56 +01002846 }
Christopher Fauleta3618372018-12-13 21:59:56 +01002847 reql = co_getblk(si_oc(si), temp->area, s->txn->req.body_len,
2848 s->txn->req.eoh + 2);
2849 if (reql <= 0) {
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01002850 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
2851 goto out;
Christopher Fauleta3618372018-12-13 21:59:56 +01002852 }
2853 temp->data = reql;
William Lallemand74c24fb2016-11-21 17:18:36 +01002854 }
2855
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002856 first_param = temp->area;
Christopher Fauleta3618372018-12-13 21:59:56 +01002857 end_params = temp->area + temp->data;
William Lallemand74c24fb2016-11-21 17:18:36 +01002858 cur_param = next_param = end_params;
2859 *end_params = '\0';
2860
2861 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
2862
2863 /*
2864 * Parse the parameters in reverse order to only store the last value.
2865 * From the html form, the backend and the action are at the end.
2866 */
2867 while (cur_param > first_param) {
2868 char *value;
2869 int poffset, plen;
2870
2871 cur_param--;
2872
2873 if ((*cur_param == '&') || (cur_param == first_param)) {
2874 reprocess_servers:
2875 /* Parse the key */
2876 poffset = (cur_param != first_param ? 1 : 0);
2877 plen = next_param - cur_param + (cur_param == first_param ? 1 : 0);
2878 if ((plen > 0) && (plen <= sizeof(key))) {
2879 strncpy(key, cur_param + poffset, plen);
2880 key[plen - 1] = '\0';
2881 } else {
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01002882 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
William Lallemand74c24fb2016-11-21 17:18:36 +01002883 goto out;
2884 }
2885
2886 /* Parse the value */
2887 value = key;
2888 while (*value != '\0' && *value != '=') {
2889 value++;
2890 }
2891 if (*value == '=') {
2892 /* Ok, a value is found, we can mark the end of the key */
2893 *value++ = '\0';
2894 }
2895 if (url_decode(key) < 0 || url_decode(value) < 0)
2896 break;
2897
2898 /* Now we can check the key to see what to do */
2899 if (!px && (strcmp(key, "b") == 0)) {
2900 if ((px = proxy_be_by_name(value)) == NULL) {
2901 /* the backend name is unknown or ambiguous (duplicate names) */
2902 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
2903 goto out;
2904 }
2905 }
2906 else if (!action && (strcmp(key, "action") == 0)) {
2907 if (strcmp(value, "ready") == 0) {
2908 action = ST_ADM_ACTION_READY;
2909 }
2910 else if (strcmp(value, "drain") == 0) {
2911 action = ST_ADM_ACTION_DRAIN;
2912 }
2913 else if (strcmp(value, "maint") == 0) {
2914 action = ST_ADM_ACTION_MAINT;
2915 }
2916 else if (strcmp(value, "shutdown") == 0) {
2917 action = ST_ADM_ACTION_SHUTDOWN;
2918 }
2919 else if (strcmp(value, "dhlth") == 0) {
2920 action = ST_ADM_ACTION_DHLTH;
2921 }
2922 else if (strcmp(value, "ehlth") == 0) {
2923 action = ST_ADM_ACTION_EHLTH;
2924 }
2925 else if (strcmp(value, "hrunn") == 0) {
2926 action = ST_ADM_ACTION_HRUNN;
2927 }
2928 else if (strcmp(value, "hnolb") == 0) {
2929 action = ST_ADM_ACTION_HNOLB;
2930 }
2931 else if (strcmp(value, "hdown") == 0) {
2932 action = ST_ADM_ACTION_HDOWN;
2933 }
2934 else if (strcmp(value, "dagent") == 0) {
2935 action = ST_ADM_ACTION_DAGENT;
2936 }
2937 else if (strcmp(value, "eagent") == 0) {
2938 action = ST_ADM_ACTION_EAGENT;
2939 }
2940 else if (strcmp(value, "arunn") == 0) {
2941 action = ST_ADM_ACTION_ARUNN;
2942 }
2943 else if (strcmp(value, "adown") == 0) {
2944 action = ST_ADM_ACTION_ADOWN;
2945 }
2946 /* else these are the old supported methods */
2947 else if (strcmp(value, "disable") == 0) {
2948 action = ST_ADM_ACTION_DISABLE;
2949 }
2950 else if (strcmp(value, "enable") == 0) {
2951 action = ST_ADM_ACTION_ENABLE;
2952 }
2953 else if (strcmp(value, "stop") == 0) {
2954 action = ST_ADM_ACTION_STOP;
2955 }
2956 else if (strcmp(value, "start") == 0) {
2957 action = ST_ADM_ACTION_START;
2958 }
2959 else {
2960 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
2961 goto out;
2962 }
2963 }
2964 else if (strcmp(key, "s") == 0) {
2965 if (!(px && action)) {
2966 /*
2967 * Indicates that we'll need to reprocess the parameters
2968 * as soon as backend and action are known
2969 */
2970 if (!reprocess) {
2971 st_cur_param = cur_param;
2972 st_next_param = next_param;
2973 }
2974 reprocess = 1;
2975 }
2976 else if ((sv = findserver(px, value)) != NULL) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002977 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
William Lallemand74c24fb2016-11-21 17:18:36 +01002978 switch (action) {
2979 case ST_ADM_ACTION_DISABLE:
Emeric Brun52a91d32017-08-31 14:41:55 +02002980 if (!(sv->cur_admin & SRV_ADMF_FMAINT)) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002981 altered_servers++;
2982 total_servers++;
2983 srv_set_admin_flag(sv, SRV_ADMF_FMAINT, "'disable' on stats page");
2984 }
2985 break;
2986 case ST_ADM_ACTION_ENABLE:
Emeric Brun52a91d32017-08-31 14:41:55 +02002987 if (sv->cur_admin & SRV_ADMF_FMAINT) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002988 altered_servers++;
2989 total_servers++;
2990 srv_clr_admin_flag(sv, SRV_ADMF_FMAINT);
2991 }
2992 break;
2993 case ST_ADM_ACTION_STOP:
Emeric Brun52a91d32017-08-31 14:41:55 +02002994 if (!(sv->cur_admin & SRV_ADMF_FDRAIN)) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002995 srv_set_admin_flag(sv, SRV_ADMF_FDRAIN, "'stop' on stats page");
2996 altered_servers++;
2997 total_servers++;
2998 }
2999 break;
3000 case ST_ADM_ACTION_START:
Emeric Brun52a91d32017-08-31 14:41:55 +02003001 if (sv->cur_admin & SRV_ADMF_FDRAIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01003002 srv_clr_admin_flag(sv, SRV_ADMF_FDRAIN);
3003 altered_servers++;
3004 total_servers++;
3005 }
3006 break;
3007 case ST_ADM_ACTION_DHLTH:
3008 if (sv->check.state & CHK_ST_CONFIGURED) {
3009 sv->check.state &= ~CHK_ST_ENABLED;
3010 altered_servers++;
3011 total_servers++;
3012 }
3013 break;
3014 case ST_ADM_ACTION_EHLTH:
3015 if (sv->check.state & CHK_ST_CONFIGURED) {
3016 sv->check.state |= CHK_ST_ENABLED;
3017 altered_servers++;
3018 total_servers++;
3019 }
3020 break;
3021 case ST_ADM_ACTION_HRUNN:
3022 if (!(sv->track)) {
3023 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02003024 srv_set_running(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003025 altered_servers++;
3026 total_servers++;
3027 }
3028 break;
3029 case ST_ADM_ACTION_HNOLB:
3030 if (!(sv->track)) {
3031 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02003032 srv_set_stopping(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003033 altered_servers++;
3034 total_servers++;
3035 }
3036 break;
3037 case ST_ADM_ACTION_HDOWN:
3038 if (!(sv->track)) {
3039 sv->check.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02003040 srv_set_stopped(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003041 altered_servers++;
3042 total_servers++;
3043 }
3044 break;
3045 case ST_ADM_ACTION_DAGENT:
3046 if (sv->agent.state & CHK_ST_CONFIGURED) {
3047 sv->agent.state &= ~CHK_ST_ENABLED;
3048 altered_servers++;
3049 total_servers++;
3050 }
3051 break;
3052 case ST_ADM_ACTION_EAGENT:
3053 if (sv->agent.state & CHK_ST_CONFIGURED) {
3054 sv->agent.state |= CHK_ST_ENABLED;
3055 altered_servers++;
3056 total_servers++;
3057 }
3058 break;
3059 case ST_ADM_ACTION_ARUNN:
3060 if (sv->agent.state & CHK_ST_ENABLED) {
3061 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02003062 srv_set_running(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003063 altered_servers++;
3064 total_servers++;
3065 }
3066 break;
3067 case ST_ADM_ACTION_ADOWN:
3068 if (sv->agent.state & CHK_ST_ENABLED) {
3069 sv->agent.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02003070 srv_set_stopped(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003071 altered_servers++;
3072 total_servers++;
3073 }
3074 break;
3075 case ST_ADM_ACTION_READY:
3076 srv_adm_set_ready(sv);
3077 altered_servers++;
3078 total_servers++;
3079 break;
3080 case ST_ADM_ACTION_DRAIN:
3081 srv_adm_set_drain(sv);
3082 altered_servers++;
3083 total_servers++;
3084 break;
3085 case ST_ADM_ACTION_MAINT:
3086 srv_adm_set_maint(sv);
3087 altered_servers++;
3088 total_servers++;
3089 break;
3090 case ST_ADM_ACTION_SHUTDOWN:
3091 if (px->state != PR_STSTOPPED) {
3092 struct stream *sess, *sess_bck;
3093
3094 list_for_each_entry_safe(sess, sess_bck, &sv->actconns, by_srv)
3095 if (sess->srv_conn == sv)
3096 stream_shutdown(sess, SF_ERR_KILLED);
3097
3098 altered_servers++;
3099 total_servers++;
3100 }
3101 break;
3102 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003103 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Lallemand74c24fb2016-11-21 17:18:36 +01003104 } else {
3105 /* the server name is unknown or ambiguous (duplicate names) */
3106 total_servers++;
3107 }
3108 }
3109 if (reprocess && px && action) {
3110 /* Now, we know the backend and the action chosen by the user.
3111 * We can safely restart from the first server parameter
3112 * to reprocess them
3113 */
3114 cur_param = st_cur_param;
3115 next_param = st_next_param;
3116 reprocess = 0;
3117 goto reprocess_servers;
3118 }
3119
3120 next_param = cur_param;
3121 }
3122 }
3123
3124 if (total_servers == 0) {
3125 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
3126 }
3127 else if (altered_servers == 0) {
3128 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
3129 }
3130 else if (altered_servers == total_servers) {
3131 appctx->ctx.stats.st_code = STAT_STATUS_DONE;
3132 }
3133 else {
3134 appctx->ctx.stats.st_code = STAT_STATUS_PART;
3135 }
3136 out:
3137 return 1;
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01003138 wait:
3139 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
3140 return 0;
Christopher Fauletef779222018-10-31 08:47:01 +01003141}
3142
3143
3144static int stats_send_htx_headers(struct stream_interface *si, struct htx *htx)
3145{
3146 struct stream *s = si_strm(si);
3147 struct uri_auth *uri = s->be->uri_auth;
3148 struct appctx *appctx = __objt_appctx(si->end);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003149 struct htx_sl *sl;
3150 unsigned int flags;
Christopher Fauletef779222018-10-31 08:47:01 +01003151
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003152 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);
3153 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist("200"), ist("OK"));
3154 if (!sl)
Christopher Fauletef779222018-10-31 08:47:01 +01003155 goto full;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003156 sl->info.res.status = 200;
Christopher Fauletef779222018-10-31 08:47:01 +01003157
Christopher Fauletb829f4c2019-03-29 16:13:55 +01003158 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")))
Christopher Fauletef779222018-10-31 08:47:01 +01003159 goto full;
3160 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
3161 if (!htx_add_header(htx, ist("Content-Type"), ist("text/html")))
3162 goto full;
3163 }
3164 else {
3165 if (!htx_add_header(htx, ist("Content-Type"), ist("text/plain")))
3166 goto full;
3167 }
3168
3169 if (uri->refresh > 0 && !(appctx->ctx.stats.flags & STAT_NO_REFRESH)) {
3170 const char *refresh = U2A(uri->refresh);
3171 if (!htx_add_header(htx, ist("Refresh"), ist2(refresh, strlen(refresh))))
3172 goto full;
3173 }
3174
3175 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
3176 if (!htx_add_header(htx, ist("Transfer-Encoding"), ist("chunked")))
3177 goto full;
3178 }
3179
3180 if (!htx_add_endof(htx, HTX_BLK_EOH))
3181 goto full;
3182
Christopher Faulet1e2d6362019-02-27 16:28:48 +01003183 channel_add_input(&s->res, htx->data);
Christopher Fauletef779222018-10-31 08:47:01 +01003184 return 1;
3185
3186 full:
3187 htx_reset(htx);
3188 si_rx_room_blk(si);
3189 return 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01003190}
3191
Christopher Fauletef779222018-10-31 08:47:01 +01003192
3193static int stats_send_htx_redirect(struct stream_interface *si, struct htx *htx)
3194{
3195 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
3196 struct stream *s = si_strm(si);
3197 struct uri_auth *uri = s->be->uri_auth;
3198 struct appctx *appctx = __objt_appctx(si->end);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003199 struct htx_sl *sl;
3200 unsigned int flags;
Christopher Fauletef779222018-10-31 08:47:01 +01003201
3202 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
3203 scope_txt[0] = 0;
3204 if (appctx->ctx.stats.scope_len) {
Christopher Fauleted7a0662019-01-14 11:07:34 +01003205 const char *scope_ptr = stats_scope_ptr(appctx, si);
3206
Christopher Fauletef779222018-10-31 08:47:01 +01003207 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Christopher Fauleted7a0662019-01-14 11:07:34 +01003208 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), scope_ptr, appctx->ctx.stats.scope_len);
Christopher Fauletef779222018-10-31 08:47:01 +01003209 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
3210 }
3211
3212 /* We don't want to land on the posted stats page because a refresh will
3213 * repost the data. We don't want this to happen on accident so we redirect
3214 * the browse to the stats page with a GET.
3215 */
3216 chunk_printf(&trash, "%s;st=%s%s%s%s",
3217 uri->uri_prefix,
3218 ((appctx->ctx.stats.st_code > STAT_STATUS_INIT) &&
3219 (appctx->ctx.stats.st_code < STAT_STATUS_SIZE) &&
3220 stat_status_codes[appctx->ctx.stats.st_code]) ?
3221 stat_status_codes[appctx->ctx.stats.st_code] :
3222 stat_status_codes[STAT_STATUS_UNKN],
3223 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3224 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3225 scope_txt);
3226
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003227 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_CHNK);
3228 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist("303"), ist("See Other"));
3229 if (!sl)
Christopher Fauletef779222018-10-31 08:47:01 +01003230 goto full;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003231 sl->info.res.status = 303;
Christopher Fauletef779222018-10-31 08:47:01 +01003232
3233 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")) ||
Christopher Fauletef779222018-10-31 08:47:01 +01003234 !htx_add_header(htx, ist("Content-Type"), ist("text/plain")) ||
3235 !htx_add_header(htx, ist("Content-Length"), ist("0")) ||
3236 !htx_add_header(htx, ist("Location"), ist2(trash.area, trash.data)))
3237 goto full;
3238
3239 if (!htx_add_endof(htx, HTX_BLK_EOH))
3240 goto full;
3241
Christopher Faulet1e2d6362019-02-27 16:28:48 +01003242 channel_add_input(&s->res, htx->data);
Christopher Fauletef779222018-10-31 08:47:01 +01003243 return 1;
3244
3245full:
3246 htx_reset(htx);
3247 si_rx_room_blk(si);
3248 return 0;
3249}
William Lallemand74c24fb2016-11-21 17:18:36 +01003250
3251static int stats_send_http_headers(struct stream_interface *si)
3252{
3253 struct stream *s = si_strm(si);
3254 struct uri_auth *uri = s->be->uri_auth;
Willy Tarreau21ff2c42018-09-20 11:01:01 +02003255 struct appctx *appctx = __objt_appctx(si->end);
William Lallemand74c24fb2016-11-21 17:18:36 +01003256
3257 chunk_printf(&trash,
3258 "HTTP/1.1 200 OK\r\n"
3259 "Cache-Control: no-cache\r\n"
3260 "Connection: close\r\n"
3261 "Content-Type: %s\r\n",
3262 (appctx->ctx.stats.flags & STAT_FMT_HTML) ? "text/html" : "text/plain");
3263
3264 if (uri->refresh > 0 && !(appctx->ctx.stats.flags & STAT_NO_REFRESH))
3265 chunk_appendf(&trash, "Refresh: %d\r\n",
3266 uri->refresh);
3267
3268 /* we don't send the CRLF in chunked mode, it will be sent with the first chunk's size */
3269
3270 if (appctx->ctx.stats.flags & STAT_CHUNKED)
3271 chunk_appendf(&trash, "Transfer-Encoding: chunked\r\n");
3272 else
3273 chunk_appendf(&trash, "\r\n");
3274
Willy Tarreau06d80a92017-10-19 14:32:15 +02003275 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01003276 si_rx_room_blk(si);
William Lallemand74c24fb2016-11-21 17:18:36 +01003277 return 0;
3278 }
3279
3280 return 1;
3281}
3282
3283static int stats_send_http_redirect(struct stream_interface *si)
3284{
3285 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
3286 struct stream *s = si_strm(si);
3287 struct uri_auth *uri = s->be->uri_auth;
Willy Tarreau21ff2c42018-09-20 11:01:01 +02003288 struct appctx *appctx = __objt_appctx(si->end);
William Lallemand74c24fb2016-11-21 17:18:36 +01003289
3290 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
3291 scope_txt[0] = 0;
3292 if (appctx->ctx.stats.scope_len) {
3293 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau89faf5d2018-06-07 18:16:48 +02003294 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 +01003295 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
3296 }
3297
3298 /* We don't want to land on the posted stats page because a refresh will
3299 * repost the data. We don't want this to happen on accident so we redirect
3300 * the browse to the stats page with a GET.
3301 */
3302 chunk_printf(&trash,
3303 "HTTP/1.1 303 See Other\r\n"
3304 "Cache-Control: no-cache\r\n"
3305 "Content-Type: text/plain\r\n"
3306 "Connection: close\r\n"
3307 "Location: %s;st=%s%s%s%s\r\n"
3308 "Content-length: 0\r\n"
3309 "\r\n",
3310 uri->uri_prefix,
3311 ((appctx->ctx.stats.st_code > STAT_STATUS_INIT) &&
3312 (appctx->ctx.stats.st_code < STAT_STATUS_SIZE) &&
3313 stat_status_codes[appctx->ctx.stats.st_code]) ?
3314 stat_status_codes[appctx->ctx.stats.st_code] :
3315 stat_status_codes[STAT_STATUS_UNKN],
3316 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3317 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3318 scope_txt);
3319
Willy Tarreau06d80a92017-10-19 14:32:15 +02003320 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01003321 si_rx_room_blk(si);
William Lallemand74c24fb2016-11-21 17:18:36 +01003322 return 0;
3323 }
3324
3325 return 1;
3326}
3327
Simon Horman05ee2132017-01-04 09:37:25 +01003328
William Lallemand74c24fb2016-11-21 17:18:36 +01003329/* This I/O handler runs as an applet embedded in a stream interface. It is
3330 * used to send HTTP stats over a TCP socket. The mechanism is very simple.
3331 * appctx->st0 contains the operation in progress (dump, done). The handler
3332 * automatically unregisters itself once transfer is complete.
3333 */
Christopher Fauletef779222018-10-31 08:47:01 +01003334static void htx_stats_io_handler(struct appctx *appctx)
3335{
3336 struct stream_interface *si = appctx->owner;
3337 struct stream *s = si_strm(si);
3338 struct channel *req = si_oc(si);
3339 struct channel *res = si_ic(si);
3340 struct htx *req_htx, *res_htx;
3341
3342 res_htx = htx_from_buf(&res->buf);
3343
3344 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
3345 goto out;
3346
3347 /* Check if the input buffer is avalaible. */
3348 if (!b_size(&res->buf)) {
3349 si_rx_room_blk(si);
3350 goto out;
3351 }
3352
3353 /* check that the output is not closed */
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003354 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_SHUTR))
3355 appctx->st0 = STAT_HTTP_END;
Christopher Fauletef779222018-10-31 08:47:01 +01003356
3357 /* all states are processed in sequence */
3358 if (appctx->st0 == STAT_HTTP_HEAD) {
3359 if (stats_send_htx_headers(si, res_htx)) {
3360 if (s->txn->meth == HTTP_METH_HEAD)
3361 appctx->st0 = STAT_HTTP_DONE;
3362 else
3363 appctx->st0 = STAT_HTTP_DUMP;
3364 }
3365 }
3366
3367 if (appctx->st0 == STAT_HTTP_DUMP) {
3368 if (stats_dump_stat_to_buffer(si, res_htx, s->be->uri_auth))
3369 appctx->st0 = STAT_HTTP_DONE;
3370 }
3371
3372 if (appctx->st0 == STAT_HTTP_POST) {
3373 if (stats_process_http_post(si))
3374 appctx->st0 = STAT_HTTP_LAST;
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003375 else if (req->flags & CF_SHUTR)
Christopher Fauletef779222018-10-31 08:47:01 +01003376 appctx->st0 = STAT_HTTP_DONE;
3377 }
3378
3379 if (appctx->st0 == STAT_HTTP_LAST) {
3380 if (stats_send_htx_redirect(si, res_htx))
3381 appctx->st0 = STAT_HTTP_DONE;
3382 }
3383
3384 if (appctx->st0 == STAT_HTTP_DONE) {
Christopher Faulet54b5e212019-06-04 10:08:28 +02003385 /* Don't add TLR because mux-h1 will take care of it */
Christopher Fauletef779222018-10-31 08:47:01 +01003386 if (!htx_add_endof(res_htx, HTX_BLK_EOM)) {
3387 si_rx_room_blk(si);
3388 goto out;
3389 }
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003390 channel_add_input(&s->res, 1);
3391 appctx->st0 = STAT_HTTP_END;
Christopher Fauletef779222018-10-31 08:47:01 +01003392 }
3393
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003394 if (appctx->st0 == STAT_HTTP_END) {
3395 if (!(res->flags & CF_SHUTR)) {
Christopher Fauletef779222018-10-31 08:47:01 +01003396 res->flags |= CF_READ_NULL;
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003397 si_shutr(si);
3398 }
3399
3400 /* eat the whole request */
3401 if (co_data(req)) {
3402 req_htx = htx_from_buf(&req->buf);
3403 co_htx_skip(req, req_htx, co_data(req));
3404 htx_to_buf(req_htx, &req->buf);
Christopher Fauletef779222018-10-31 08:47:01 +01003405 }
3406 }
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003407
Christopher Fauletef779222018-10-31 08:47:01 +01003408 out:
3409 /* we have left the request in the buffer for the case where we
3410 * process a POST, and this automatically re-enables activity on
3411 * read. It's better to indicate that we want to stop reading when
3412 * we're sending, so that we know there's at most one direction
3413 * deciding to wake the applet up. It saves it from looping when
3414 * emitting large blocks into small TCP windows.
3415 */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003416 htx_to_buf(res_htx, &res->buf);
3417 if (!channel_is_empty(res))
Christopher Fauletef779222018-10-31 08:47:01 +01003418 si_stop_get(si);
Christopher Fauletef779222018-10-31 08:47:01 +01003419}
3420
3421
3422/* This I/O handler runs as an applet embedded in a stream interface. It is
3423 * used to send HTTP stats over a TCP socket. The mechanism is very simple.
3424 * appctx->st0 contains the operation in progress (dump, done). The handler
3425 * automatically unregisters itself once transfer is complete.
3426 */
William Lallemand74c24fb2016-11-21 17:18:36 +01003427static void http_stats_io_handler(struct appctx *appctx)
3428{
3429 struct stream_interface *si = appctx->owner;
3430 struct stream *s = si_strm(si);
3431 struct channel *req = si_oc(si);
3432 struct channel *res = si_ic(si);
3433
Christopher Fauletef779222018-10-31 08:47:01 +01003434 if (IS_HTX_STRM(s))
3435 return htx_stats_io_handler(appctx);
3436
William Lallemand74c24fb2016-11-21 17:18:36 +01003437 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
3438 goto out;
3439
Joseph Herlant29023ec2018-11-15 13:39:46 -08003440 /* Check if the input buffer is available. */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003441 if (res->buf.size == 0) {
Willy Tarreau4b962a42018-11-15 11:03:21 +01003442 /* already subscribed, we'll be called later once the buffer is
3443 * available.
3444 */
Christopher Fauleta73e59b2016-12-09 17:30:18 +01003445 goto out;
3446 }
3447
William Lallemand74c24fb2016-11-21 17:18:36 +01003448 /* check that the output is not closed */
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003449 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_SHUTR))
3450 appctx->st0 = STAT_HTTP_END;
William Lallemand74c24fb2016-11-21 17:18:36 +01003451
3452 /* all states are processed in sequence */
3453 if (appctx->st0 == STAT_HTTP_HEAD) {
3454 if (stats_send_http_headers(si)) {
3455 if (s->txn->meth == HTTP_METH_HEAD)
3456 appctx->st0 = STAT_HTTP_DONE;
3457 else
3458 appctx->st0 = STAT_HTTP_DUMP;
3459 }
3460 }
3461
3462 if (appctx->st0 == STAT_HTTP_DUMP) {
Willy Tarreau97f538b2018-06-15 19:41:31 +02003463 unsigned int prev_len = ci_data(si_ic(si));
William Lallemand74c24fb2016-11-21 17:18:36 +01003464 unsigned int data_len;
3465 unsigned int last_len;
3466 unsigned int last_fwd = 0;
3467
3468 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
3469 /* One difficulty we're facing is that we must prevent
3470 * the input data from being automatically forwarded to
3471 * the output area. For this, we temporarily disable
3472 * forwarding on the channel.
3473 */
3474 last_fwd = si_ic(si)->to_forward;
3475 si_ic(si)->to_forward = 0;
3476 chunk_printf(&trash, "\r\n000000\r\n");
Willy Tarreau06d80a92017-10-19 14:32:15 +02003477 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01003478 si_rx_room_blk(si);
William Lallemand74c24fb2016-11-21 17:18:36 +01003479 si_ic(si)->to_forward = last_fwd;
3480 goto out;
3481 }
3482 }
3483
Willy Tarreau97f538b2018-06-15 19:41:31 +02003484 data_len = ci_data(si_ic(si));
Christopher Fauletef779222018-10-31 08:47:01 +01003485 if (stats_dump_stat_to_buffer(si, NULL, s->be->uri_auth))
William Lallemand74c24fb2016-11-21 17:18:36 +01003486 appctx->st0 = STAT_HTTP_DONE;
3487
Willy Tarreau97f538b2018-06-15 19:41:31 +02003488 last_len = ci_data(si_ic(si));
William Lallemand74c24fb2016-11-21 17:18:36 +01003489
3490 /* Now we must either adjust or remove the chunk size. This is
3491 * not easy because the chunk size might wrap at the end of the
3492 * buffer, so we pretend we have nothing in the buffer, we write
3493 * the size, then restore the buffer's contents. Note that we can
3494 * only do that because no forwarding is scheduled on the stats
3495 * applet.
3496 */
3497 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
3498 si_ic(si)->total -= (last_len - prev_len);
Willy Tarreau97f538b2018-06-15 19:41:31 +02003499 b_sub(si_ib(si), (last_len - prev_len));
William Lallemand74c24fb2016-11-21 17:18:36 +01003500
3501 if (last_len != data_len) {
3502 chunk_printf(&trash, "\r\n%06x\r\n", (last_len - data_len));
Willy Tarreau06d80a92017-10-19 14:32:15 +02003503 if (ci_putchk(si_ic(si), &trash) == -1)
Willy Tarreaudb398432018-11-15 11:08:52 +01003504 si_rx_room_blk(si);
William Lallemand74c24fb2016-11-21 17:18:36 +01003505
3506 si_ic(si)->total += (last_len - data_len);
Willy Tarreau97f538b2018-06-15 19:41:31 +02003507 b_add(si_ib(si), last_len - data_len);
William Lallemand74c24fb2016-11-21 17:18:36 +01003508 }
3509 /* now re-enable forwarding */
3510 channel_forward(si_ic(si), last_fwd);
3511 }
3512 }
3513
3514 if (appctx->st0 == STAT_HTTP_POST) {
3515 if (stats_process_http_post(si))
3516 appctx->st0 = STAT_HTTP_LAST;
3517 else if (si_oc(si)->flags & CF_SHUTR)
3518 appctx->st0 = STAT_HTTP_DONE;
3519 }
3520
3521 if (appctx->st0 == STAT_HTTP_LAST) {
3522 if (stats_send_http_redirect(si))
3523 appctx->st0 = STAT_HTTP_DONE;
3524 }
3525
3526 if (appctx->st0 == STAT_HTTP_DONE) {
3527 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
3528 chunk_printf(&trash, "\r\n0\r\n\r\n");
Willy Tarreau06d80a92017-10-19 14:32:15 +02003529 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01003530 si_rx_room_blk(si);
William Lallemand74c24fb2016-11-21 17:18:36 +01003531 goto out;
3532 }
3533 }
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003534 appctx->st0 = STAT_HTTP_END;
William Lallemand74c24fb2016-11-21 17:18:36 +01003535 }
3536
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003537 if (appctx->st0 == STAT_HTTP_END) {
3538 if (!(res->flags & CF_SHUTR)) {
William Lallemand74c24fb2016-11-21 17:18:36 +01003539 res->flags |= CF_READ_NULL;
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003540 si_shutr(si);
William Lallemand74c24fb2016-11-21 17:18:36 +01003541 }
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003542
3543 /* eat the whole request */
3544 if (co_data(req))
3545 co_skip(si_oc(si), co_data(si_oc(si)));
William Lallemand74c24fb2016-11-21 17:18:36 +01003546 }
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003547
William Lallemand74c24fb2016-11-21 17:18:36 +01003548 out:
Willy Tarreau055ba4f2018-07-24 17:05:54 +02003549 /* we have left the request in the buffer for the case where we
3550 * process a POST, and this automatically re-enables activity on
3551 * read. It's better to indicate that we want to stop reading when
3552 * we're sending, so that we know there's at most one direction
3553 * deciding to wake the applet up. It saves it from looping when
3554 * emitting large blocks into small TCP windows.
3555 */
3556 if (!channel_is_empty(res))
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01003557 si_stop_get(si);
William Lallemand74c24fb2016-11-21 17:18:36 +01003558}
3559
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003560/* Dump all fields from <info> into <out> using the "show info" format (name: value) */
Willy Tarreau83061a82018-07-13 11:56:34 +02003561static int stats_dump_info_fields(struct buffer *out,
3562 const struct field *info)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003563{
3564 int field;
3565
3566 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
3567 if (!field_format(info, field))
3568 continue;
3569
3570 if (!chunk_appendf(out, "%s: ", info_field_names[field]))
3571 return 0;
3572 if (!stats_emit_raw_data_field(out, &info[field]))
3573 return 0;
3574 if (!chunk_strcat(out, "\n"))
3575 return 0;
3576 }
3577 return 1;
3578}
3579
3580/* Dump all fields from <info> into <out> using the "show info typed" format */
Willy Tarreau83061a82018-07-13 11:56:34 +02003581static int stats_dump_typed_info_fields(struct buffer *out,
3582 const struct field *info)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003583{
3584 int field;
3585
3586 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
3587 if (!field_format(info, field))
3588 continue;
3589
3590 if (!chunk_appendf(out, "%d.%s.%u:", field, info_field_names[field], info[INF_PROCESS_NUM].u.u32))
3591 return 0;
3592 if (!stats_emit_field_tags(out, &info[field], ':'))
3593 return 0;
3594 if (!stats_emit_typed_data_field(out, &info[field]))
3595 return 0;
3596 if (!chunk_strcat(out, "\n"))
3597 return 0;
3598 }
3599 return 1;
3600}
3601
3602/* Fill <info> with HAProxy global info. <info> is preallocated
3603 * array of length <len>. The length of the aray must be
3604 * INF_TOTAL_FIELDS. If this length is less then this value, the
3605 * function returns 0, otherwise, it returns 1.
3606 */
3607int stats_fill_info(struct field *info, int len)
3608{
3609 unsigned int up = (now.tv_sec - start_date.tv_sec);
Willy Tarreau83061a82018-07-13 11:56:34 +02003610 struct buffer *out = get_trash_chunk();
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003611
3612#ifdef USE_OPENSSL
3613 int ssl_sess_rate = read_freq_ctr(&global.ssl_per_sec);
3614 int ssl_key_rate = read_freq_ctr(&global.ssl_fe_keys_per_sec);
3615 int ssl_reuse = 0;
3616
3617 if (ssl_key_rate < ssl_sess_rate) {
3618 /* count the ssl reuse ratio and avoid overflows in both directions */
3619 ssl_reuse = 100 - (100 * ssl_key_rate + (ssl_sess_rate - 1) / 2) / ssl_sess_rate;
3620 }
3621#endif
3622
3623 if (len < INF_TOTAL_FIELDS)
3624 return 0;
3625
3626 chunk_reset(out);
3627 memset(info, 0, sizeof(*info) * len);
3628
3629 info[INF_NAME] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, PRODUCT_NAME);
Willy Tarreau909b9d82019-01-04 18:20:32 +01003630 info[INF_VERSION] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, haproxy_version);
3631 info[INF_RELEASE_DATE] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, haproxy_date);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003632
Yves Lafon95317282018-02-26 11:10:37 +01003633 info[INF_NBTHREAD] = mkf_u32(FO_CONFIG|FS_SERVICE, global.nbthread);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003634 info[INF_NBPROC] = mkf_u32(FO_CONFIG|FS_SERVICE, global.nbproc);
3635 info[INF_PROCESS_NUM] = mkf_u32(FO_KEY, relative_pid);
3636 info[INF_PID] = mkf_u32(FO_STATUS, pid);
3637
3638 info[INF_UPTIME] = mkf_str(FN_DURATION, chunk_newstr(out));
3639 chunk_appendf(out, "%ud %uh%02um%02us", up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60));
3640
3641 info[INF_UPTIME_SEC] = mkf_u32(FN_DURATION, up);
3642 info[INF_MEMMAX_MB] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_memmax);
3643 info[INF_POOL_ALLOC_MB] = mkf_u32(0, (unsigned)(pool_total_allocated() / 1048576L));
3644 info[INF_POOL_USED_MB] = mkf_u32(0, (unsigned)(pool_total_used() / 1048576L));
3645 info[INF_POOL_FAILED] = mkf_u32(FN_COUNTER, pool_total_failures());
3646 info[INF_ULIMIT_N] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_nofile);
3647 info[INF_MAXSOCK] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxsock);
3648 info[INF_MAXCONN] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxconn);
3649 info[INF_HARD_MAXCONN] = mkf_u32(FO_CONFIG|FN_LIMIT, global.hardmaxconn);
3650 info[INF_CURR_CONN] = mkf_u32(0, actconn);
3651 info[INF_CUM_CONN] = mkf_u32(FN_COUNTER, totalconn);
3652 info[INF_CUM_REQ] = mkf_u32(FN_COUNTER, global.req_count);
3653#ifdef USE_OPENSSL
3654 info[INF_MAX_SSL_CONNS] = mkf_u32(FN_MAX, global.maxsslconn);
3655 info[INF_CURR_SSL_CONNS] = mkf_u32(0, sslconns);
3656 info[INF_CUM_SSL_CONNS] = mkf_u32(FN_COUNTER, totalsslconns);
3657#endif
3658 info[INF_MAXPIPES] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxpipes);
3659 info[INF_PIPES_USED] = mkf_u32(0, pipes_used);
3660 info[INF_PIPES_FREE] = mkf_u32(0, pipes_free);
3661 info[INF_CONN_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.conn_per_sec));
3662 info[INF_CONN_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.cps_lim);
3663 info[INF_MAX_CONN_RATE] = mkf_u32(FN_MAX, global.cps_max);
3664 info[INF_SESS_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.sess_per_sec));
3665 info[INF_SESS_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.sps_lim);
3666 info[INF_MAX_SESS_RATE] = mkf_u32(FN_RATE, global.sps_max);
3667
3668#ifdef USE_OPENSSL
3669 info[INF_SSL_RATE] = mkf_u32(FN_RATE, ssl_sess_rate);
3670 info[INF_SSL_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.ssl_lim);
3671 info[INF_MAX_SSL_RATE] = mkf_u32(FN_MAX, global.ssl_max);
3672 info[INF_SSL_FRONTEND_KEY_RATE] = mkf_u32(0, ssl_key_rate);
3673 info[INF_SSL_FRONTEND_MAX_KEY_RATE] = mkf_u32(FN_MAX, global.ssl_fe_keys_max);
3674 info[INF_SSL_FRONTEND_SESSION_REUSE_PCT] = mkf_u32(0, ssl_reuse);
3675 info[INF_SSL_BACKEND_KEY_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.ssl_be_keys_per_sec));
3676 info[INF_SSL_BACKEND_MAX_KEY_RATE] = mkf_u32(FN_MAX, global.ssl_be_keys_max);
3677 info[INF_SSL_CACHE_LOOKUPS] = mkf_u32(FN_COUNTER, global.shctx_lookups);
3678 info[INF_SSL_CACHE_MISSES] = mkf_u32(FN_COUNTER, global.shctx_misses);
3679#endif
3680 info[INF_COMPRESS_BPS_IN] = mkf_u32(FN_RATE, read_freq_ctr(&global.comp_bps_in));
3681 info[INF_COMPRESS_BPS_OUT] = mkf_u32(FN_RATE, read_freq_ctr(&global.comp_bps_out));
3682 info[INF_COMPRESS_BPS_RATE_LIM] = mkf_u32(FO_CONFIG|FN_LIMIT, global.comp_rate_lim);
3683#ifdef USE_ZLIB
3684 info[INF_ZLIB_MEM_USAGE] = mkf_u32(0, zlib_used_memory);
3685 info[INF_MAX_ZLIB_MEM_USAGE] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxzlibmem);
3686#endif
3687 info[INF_TASKS] = mkf_u32(0, nb_tasks_cur);
Christopher Faulet34c5cc92016-12-06 09:15:30 +01003688 info[INF_RUN_QUEUE] = mkf_u32(0, tasks_run_queue_cur);
Willy Tarreau81036f22019-05-20 19:24:50 +02003689 info[INF_IDLE_PCT] = mkf_u32(FN_AVG, ti->idle_pct);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003690 info[INF_NODE] = mkf_str(FO_CONFIG|FN_OUTPUT|FS_SERVICE, global.node);
3691 if (global.desc)
3692 info[INF_DESCRIPTION] = mkf_str(FO_CONFIG|FN_OUTPUT|FS_SERVICE, global.desc);
Willy Tarreau00098ea2018-11-05 14:38:13 +01003693 info[INF_STOPPING] = mkf_u32(0, stopping);
3694 info[INF_JOBS] = mkf_u32(0, jobs);
William Lallemanda7199262018-11-16 16:57:20 +01003695 info[INF_UNSTOPPABLE_JOBS] = mkf_u32(0, unstoppable_jobs);
Willy Tarreau00098ea2018-11-05 14:38:13 +01003696 info[INF_LISTENERS] = mkf_u32(0, listeners);
Willy Tarreau199ad242018-11-05 16:31:22 +01003697 info[INF_ACTIVE_PEERS] = mkf_u32(0, active_peers);
Willy Tarreau2d372c22018-11-05 17:12:27 +01003698 info[INF_CONNECTED_PEERS] = mkf_u32(0, connected_peers);
Willy Tarreau13ef7732018-11-12 07:25:28 +01003699 info[INF_DROPPED_LOGS] = mkf_u32(0, dropped_logs);
Willy Tarreaubeb859a2018-11-22 18:07:59 +01003700 info[INF_BUSY_POLLING] = mkf_u32(0, !!(global.tune.options & GTUNE_BUSY_POLLING));
Baptiste Assmann333939c2019-01-21 08:34:50 +01003701 info[INF_FAILED_RESOLUTIONS] = mkf_u32(0, dns_failed_resolutions);
Willy Tarreau7cf0e452019-05-23 11:39:14 +02003702 info[INF_TOTAL_BYTES_OUT] = mkf_u64(0, global.out_bytes);
3703 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 +01003704
3705 return 1;
3706}
3707
3708/* This function dumps information onto the stream interface's read buffer.
3709 * It returns 0 as long as it does not complete, non-zero upon completion.
3710 * No state is used.
3711 */
3712static int stats_dump_info_to_buffer(struct stream_interface *si)
3713{
3714 struct appctx *appctx = __objt_appctx(si->end);
3715
3716 if (!stats_fill_info(info, INF_TOTAL_FIELDS))
3717 return 0;
3718
3719 chunk_reset(&trash);
3720
3721 if (appctx->ctx.stats.flags & STAT_FMT_TYPED)
3722 stats_dump_typed_info_fields(&trash, info);
Simon Horman05ee2132017-01-04 09:37:25 +01003723 else if (appctx->ctx.stats.flags & STAT_FMT_JSON)
3724 stats_dump_json_info_fields(&trash, info);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003725 else
3726 stats_dump_info_fields(&trash, info);
3727
Willy Tarreau06d80a92017-10-19 14:32:15 +02003728 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01003729 si_rx_room_blk(si);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003730 return 0;
3731 }
3732
3733 return 1;
3734}
3735
Simon Horman6f6bb382017-01-04 09:37:26 +01003736/* This function dumps the schema onto the stream interface's read buffer.
3737 * It returns 0 as long as it does not complete, non-zero upon completion.
3738 * No state is used.
3739 *
3740 * Integer values bouned to the range [-(2**53)+1, (2**53)-1] as
3741 * per the recommendation for interoperable integers in section 6 of RFC 7159.
3742 */
Willy Tarreau83061a82018-07-13 11:56:34 +02003743static void stats_dump_json_schema(struct buffer *out)
Simon Horman6f6bb382017-01-04 09:37:26 +01003744{
3745
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003746 int old_len = out->data;
Simon Horman6f6bb382017-01-04 09:37:26 +01003747
3748 chunk_strcat(out,
3749 "{"
3750 "\"$schema\":\"http://json-schema.org/draft-04/schema#\","
3751 "\"oneOf\":["
3752 "{"
3753 "\"title\":\"Info\","
3754 "\"type\":\"array\","
3755 "\"items\":{"
3756 "\"properties\":{"
3757 "\"title\":\"InfoItem\","
3758 "\"type\":\"object\","
3759 "\"field\":{\"$ref\":\"#/definitions/field\"},"
3760 "\"processNum\":{\"$ref\":\"#/definitions/processNum\"},"
3761 "\"tags\":{\"$ref\":\"#/definitions/tags\"},"
3762 "\"value\":{\"$ref\":\"#/definitions/typedValue\"}"
3763 "},"
3764 "\"required\":[\"field\",\"processNum\",\"tags\","
3765 "\"value\"]"
3766 "}"
3767 "},"
3768 "{"
3769 "\"title\":\"Stat\","
3770 "\"type\":\"array\","
3771 "\"items\":{"
3772 "\"title\":\"InfoItem\","
3773 "\"type\":\"object\","
3774 "\"properties\":{"
3775 "\"objType\":{"
3776 "\"enum\":[\"Frontend\",\"Backend\",\"Listener\","
3777 "\"Server\",\"Unknown\"]"
3778 "},"
3779 "\"proxyId\":{"
3780 "\"type\":\"integer\","
3781 "\"minimum\":0"
3782 "},"
3783 "\"id\":{"
3784 "\"type\":\"integer\","
3785 "\"minimum\":0"
3786 "},"
3787 "\"field\":{\"$ref\":\"#/definitions/field\"},"
3788 "\"processNum\":{\"$ref\":\"#/definitions/processNum\"},"
3789 "\"tags\":{\"$ref\":\"#/definitions/tags\"},"
3790 "\"typedValue\":{\"$ref\":\"#/definitions/typedValue\"}"
3791 "},"
3792 "\"required\":[\"objType\",\"proxyId\",\"id\","
3793 "\"field\",\"processNum\",\"tags\","
3794 "\"value\"]"
3795 "}"
3796 "},"
3797 "{"
3798 "\"title\":\"Error\","
3799 "\"type\":\"object\","
3800 "\"properties\":{"
3801 "\"errorStr\":{"
3802 "\"type\":\"string\""
3803 "},"
3804 "\"required\":[\"errorStr\"]"
3805 "}"
3806 "}"
3807 "],"
3808 "\"definitions\":{"
3809 "\"field\":{"
3810 "\"type\":\"object\","
3811 "\"pos\":{"
3812 "\"type\":\"integer\","
3813 "\"minimum\":0"
3814 "},"
3815 "\"name\":{"
3816 "\"type\":\"string\""
3817 "},"
3818 "\"required\":[\"pos\",\"name\"]"
3819 "},"
3820 "\"processNum\":{"
3821 "\"type\":\"integer\","
3822 "\"minimum\":1"
3823 "},"
3824 "\"tags\":{"
3825 "\"type\":\"object\","
3826 "\"origin\":{"
3827 "\"type\":\"string\","
3828 "\"enum\":[\"Metric\",\"Status\",\"Key\","
3829 "\"Config\",\"Product\",\"Unknown\"]"
3830 "},"
3831 "\"nature\":{"
3832 "\"type\":\"string\","
3833 "\"enum\":[\"Gauge\",\"Limit\",\"Min\",\"Max\","
3834 "\"Rate\",\"Counter\",\"Duration\","
3835 "\"Age\",\"Time\",\"Name\",\"Output\","
3836 "\"Avg\", \"Unknown\"]"
3837 "},"
3838 "\"scope\":{"
3839 "\"type\":\"string\","
3840 "\"enum\":[\"Cluster\",\"Process\",\"Service\","
3841 "\"System\",\"Unknown\"]"
3842 "},"
3843 "\"required\":[\"origin\",\"nature\",\"scope\"]"
3844 "},"
3845 "\"typedValue\":{"
3846 "\"type\":\"object\","
3847 "\"oneOf\":["
3848 "{\"$ref\":\"#/definitions/typedValue/definitions/s32Value\"},"
3849 "{\"$ref\":\"#/definitions/typedValue/definitions/s64Value\"},"
3850 "{\"$ref\":\"#/definitions/typedValue/definitions/u32Value\"},"
3851 "{\"$ref\":\"#/definitions/typedValue/definitions/u64Value\"},"
3852 "{\"$ref\":\"#/definitions/typedValue/definitions/strValue\"}"
3853 "],"
3854 "\"definitions\":{"
3855 "\"s32Value\":{"
3856 "\"properties\":{"
3857 "\"type\":{"
3858 "\"type\":\"string\","
3859 "\"enum\":[\"s32\"]"
3860 "},"
3861 "\"value\":{"
3862 "\"type\":\"integer\","
3863 "\"minimum\":-2147483648,"
3864 "\"maximum\":2147483647"
3865 "}"
3866 "},"
3867 "\"required\":[\"type\",\"value\"]"
3868 "},"
3869 "\"s64Value\":{"
3870 "\"properties\":{"
3871 "\"type\":{"
3872 "\"type\":\"string\","
3873 "\"enum\":[\"s64\"]"
3874 "},"
3875 "\"value\":{"
3876 "\"type\":\"integer\","
3877 "\"minimum\":-9007199254740991,"
3878 "\"maximum\":9007199254740991"
3879 "}"
3880 "},"
3881 "\"required\":[\"type\",\"value\"]"
3882 "},"
3883 "\"u32Value\":{"
3884 "\"properties\":{"
3885 "\"type\":{"
3886 "\"type\":\"string\","
3887 "\"enum\":[\"u32\"]"
3888 "},"
3889 "\"value\":{"
3890 "\"type\":\"integer\","
3891 "\"minimum\":0,"
3892 "\"maximum\":4294967295"
3893 "}"
3894 "},"
3895 "\"required\":[\"type\",\"value\"]"
3896 "},"
3897 "\"u64Value\":{"
3898 "\"properties\":{"
3899 "\"type\":{"
3900 "\"type\":\"string\","
3901 "\"enum\":[\"u64\"]"
3902 "},"
3903 "\"value\":{"
3904 "\"type\":\"integer\","
3905 "\"minimum\":0,"
3906 "\"maximum\":9007199254740991"
3907 "}"
3908 "},"
3909 "\"required\":[\"type\",\"value\"]"
3910 "},"
3911 "\"strValue\":{"
3912 "\"properties\":{"
3913 "\"type\":{"
3914 "\"type\":\"string\","
3915 "\"enum\":[\"str\"]"
3916 "},"
3917 "\"value\":{\"type\":\"string\"}"
3918 "},"
3919 "\"required\":[\"type\",\"value\"]"
3920 "},"
3921 "\"unknownValue\":{"
3922 "\"properties\":{"
3923 "\"type\":{"
3924 "\"type\":\"integer\","
3925 "\"minimum\":0"
3926 "},"
3927 "\"value\":{"
3928 "\"type\":\"string\","
3929 "\"enum\":[\"unknown\"]"
3930 "}"
3931 "},"
3932 "\"required\":[\"type\",\"value\"]"
3933 "}"
3934 "}"
3935 "}"
3936 "}"
3937 "}");
3938
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003939 if (old_len == out->data) {
Simon Horman6f6bb382017-01-04 09:37:26 +01003940 chunk_reset(out);
3941 chunk_appendf(out,
3942 "{\"errorStr\":\"output buffer too short\"}");
3943 }
3944}
3945
3946/* This function dumps the schema onto the stream interface's read buffer.
3947 * It returns 0 as long as it does not complete, non-zero upon completion.
3948 * No state is used.
3949 */
3950static int stats_dump_json_schema_to_buffer(struct stream_interface *si)
3951{
3952 chunk_reset(&trash);
3953
3954 stats_dump_json_schema(&trash);
3955
Willy Tarreau06d80a92017-10-19 14:32:15 +02003956 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01003957 si_rx_room_blk(si);
Simon Horman6f6bb382017-01-04 09:37:26 +01003958 return 0;
3959 }
3960
3961 return 1;
3962}
3963
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02003964static int cli_parse_clear_counters(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau89d467c2016-11-23 11:02:40 +01003965{
3966 struct proxy *px;
3967 struct server *sv;
3968 struct listener *li;
3969 int clrall = 0;
3970
3971 if (strcmp(args[2], "all") == 0)
3972 clrall = 1;
3973
3974 /* check permissions */
3975 if (!cli_has_level(appctx, ACCESS_LVL_OPER) ||
3976 (clrall && !cli_has_level(appctx, ACCESS_LVL_ADMIN)))
3977 return 1;
3978
Olivier Houchardfbc74e82017-11-24 16:54:05 +01003979 for (px = proxies_list; px; px = px->next) {
Willy Tarreau89d467c2016-11-23 11:02:40 +01003980 if (clrall) {
3981 memset(&px->be_counters, 0, sizeof(px->be_counters));
3982 memset(&px->fe_counters, 0, sizeof(px->fe_counters));
3983 }
3984 else {
3985 px->be_counters.conn_max = 0;
3986 px->be_counters.p.http.rps_max = 0;
3987 px->be_counters.sps_max = 0;
3988 px->be_counters.cps_max = 0;
3989 px->be_counters.nbpend_max = 0;
3990
3991 px->fe_counters.conn_max = 0;
3992 px->fe_counters.p.http.rps_max = 0;
3993 px->fe_counters.sps_max = 0;
3994 px->fe_counters.cps_max = 0;
Willy Tarreau89d467c2016-11-23 11:02:40 +01003995 }
3996
3997 for (sv = px->srv; sv; sv = sv->next)
3998 if (clrall)
3999 memset(&sv->counters, 0, sizeof(sv->counters));
4000 else {
4001 sv->counters.cur_sess_max = 0;
4002 sv->counters.nbpend_max = 0;
4003 sv->counters.sps_max = 0;
4004 }
4005
4006 list_for_each_entry(li, &px->conf.listeners, by_fe)
4007 if (li->counters) {
4008 if (clrall)
4009 memset(li->counters, 0, sizeof(*li->counters));
4010 else
4011 li->counters->conn_max = 0;
4012 }
4013 }
4014
4015 global.cps_max = 0;
4016 global.sps_max = 0;
Olivier Houchard00bc3cb2017-10-17 19:23:25 +02004017 global.ssl_max = 0;
4018 global.ssl_fe_keys_max = 0;
4019 global.ssl_be_keys_max = 0;
Willy Tarreaud80cb4e2018-01-20 19:30:13 +01004020
4021 memset(activity, 0, sizeof(activity));
Willy Tarreau89d467c2016-11-23 11:02:40 +01004022 return 1;
4023}
4024
4025
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004026static int cli_parse_show_info(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004027{
Willy Tarreaud25fc792016-12-16 12:33:47 +01004028 appctx->ctx.stats.scope_str = 0;
4029 appctx->ctx.stats.scope_len = 0;
4030 appctx->ctx.stats.flags = 0;
4031
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004032 if (strcmp(args[2], "typed") == 0)
4033 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
Simon Horman05ee2132017-01-04 09:37:25 +01004034 else if (strcmp(args[2], "json") == 0)
4035 appctx->ctx.stats.flags |= STAT_FMT_JSON;
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004036 return 0;
4037}
4038
4039
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004040static int cli_parse_show_stat(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau2b812e22016-11-22 16:18:05 +01004041{
Willy Tarreaud25fc792016-12-16 12:33:47 +01004042 appctx->ctx.stats.scope_str = 0;
4043 appctx->ctx.stats.scope_len = 0;
4044 appctx->ctx.stats.flags = 0;
4045
Willy Tarreau2b812e22016-11-22 16:18:05 +01004046 if (*args[2] && *args[3] && *args[4]) {
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01004047 struct proxy *px;
4048
4049 px = proxy_find_by_name(args[2], 0, 0);
4050 if (px)
4051 appctx->ctx.stats.iid = px->uuid;
4052 else
4053 appctx->ctx.stats.iid = atoi(args[2]);
4054
4055 if (!appctx->ctx.stats.iid) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004056 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01004057 appctx->ctx.cli.msg = "No such proxy.\n";
4058 appctx->st0 = CLI_ST_PRINT;
4059 return 1;
4060 }
4061
Willy Tarreau2b812e22016-11-22 16:18:05 +01004062 appctx->ctx.stats.flags |= STAT_BOUND;
Willy Tarreau2b812e22016-11-22 16:18:05 +01004063 appctx->ctx.stats.type = atoi(args[3]);
4064 appctx->ctx.stats.sid = atoi(args[4]);
4065 if (strcmp(args[5], "typed") == 0)
4066 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
Simon Horman05ee2132017-01-04 09:37:25 +01004067 else if (strcmp(args[5], "json") == 0)
4068 appctx->ctx.stats.flags |= STAT_FMT_JSON;
Willy Tarreau2b812e22016-11-22 16:18:05 +01004069 }
4070 else if (strcmp(args[2], "typed") == 0)
4071 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
Simon Horman05ee2132017-01-04 09:37:25 +01004072 else if (strcmp(args[2], "json") == 0)
4073 appctx->ctx.stats.flags |= STAT_FMT_JSON;
Willy Tarreau2b812e22016-11-22 16:18:05 +01004074
Willy Tarreau2b812e22016-11-22 16:18:05 +01004075 return 0;
4076}
4077
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004078static int cli_io_handler_dump_info(struct appctx *appctx)
4079{
4080 return stats_dump_info_to_buffer(appctx->owner);
4081}
4082
Willy Tarreau2b812e22016-11-22 16:18:05 +01004083/* This I/O handler runs as an applet embedded in a stream interface. It is
4084 * used to send raw stats over a socket.
4085 */
4086static int cli_io_handler_dump_stat(struct appctx *appctx)
4087{
Christopher Fauletef779222018-10-31 08:47:01 +01004088 return stats_dump_stat_to_buffer(appctx->owner, NULL, NULL);
Willy Tarreau2b812e22016-11-22 16:18:05 +01004089}
4090
Simon Horman6f6bb382017-01-04 09:37:26 +01004091static int cli_io_handler_dump_json_schema(struct appctx *appctx)
4092{
4093 return stats_dump_json_schema_to_buffer(appctx->owner);
4094}
4095
Willy Tarreau2b812e22016-11-22 16:18:05 +01004096/* register cli keywords */
4097static struct cli_kw_list cli_kws = {{ },{
Willy Tarreau89d467c2016-11-23 11:02:40 +01004098 { { "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 +01004099 { { "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 +01004100 { { "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 +01004101 { { "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 +01004102 {{},}
4103}};
4104
Willy Tarreau0108d902018-11-25 19:14:37 +01004105INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
4106
William Lallemand74c24fb2016-11-21 17:18:36 +01004107struct applet http_stats_applet = {
4108 .obj_type = OBJ_TYPE_APPLET,
4109 .name = "<STATS>", /* used for logging */
4110 .fct = http_stats_io_handler,
4111 .release = NULL,
4112};
4113
William Lallemand74c24fb2016-11-21 17:18:36 +01004114/*
4115 * Local variables:
4116 * c-indent-level: 8
4117 * c-basic-offset: 8
4118 * End:
4119 */