blob: 0a0ed1d6384a0a523635d7840c3aeae124572d51 [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 Fauletc0104fc2019-09-28 10:37:31 +0200402 break;
Christopher Faulet87d74c12019-09-24 16:35:10 +0200403 case FF_FLT: type = "\"flt\"";
404 snprintf(buf, sizeof(buf), "%f", f->u.flt);
Simon Horman05ee2132017-01-04 09:37:25 +0100405 break;
406 case FF_STR: type = "\"str\"";
407 value = field_str(f, 0);
408 quote = "\"";
409 break;
410 default: snprintf(buf, sizeof(buf), "%u", f->type);
411 type = buf;
412 value = "unknown";
413 quote = "\"";
414 break;
415 }
416
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200417 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100418 chunk_appendf(out, ",\"value\":{\"type\":%s,\"value\":%s%s%s}",
419 type, quote, value, quote);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200420 return !(old_len == out->data);
Simon Horman05ee2132017-01-04 09:37:25 +0100421}
422
William Lallemand74c24fb2016-11-21 17:18:36 +0100423/* Emits an encoding of the field type on 3 characters followed by a delimiter.
424 * Returns non-zero on success, 0 if the buffer is full.
425 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200426int stats_emit_field_tags(struct buffer *out, const struct field *f,
427 char delim)
William Lallemand74c24fb2016-11-21 17:18:36 +0100428{
429 char origin, nature, scope;
430
431 switch (field_origin(f, 0)) {
432 case FO_METRIC: origin = 'M'; break;
433 case FO_STATUS: origin = 'S'; break;
434 case FO_KEY: origin = 'K'; break;
435 case FO_CONFIG: origin = 'C'; break;
436 case FO_PRODUCT: origin = 'P'; break;
437 default: origin = '?'; break;
438 }
439
440 switch (field_nature(f, 0)) {
441 case FN_GAUGE: nature = 'G'; break;
442 case FN_LIMIT: nature = 'L'; break;
443 case FN_MIN: nature = 'm'; break;
444 case FN_MAX: nature = 'M'; break;
445 case FN_RATE: nature = 'R'; break;
446 case FN_COUNTER: nature = 'C'; break;
447 case FN_DURATION: nature = 'D'; break;
448 case FN_AGE: nature = 'A'; break;
449 case FN_TIME: nature = 'T'; break;
450 case FN_NAME: nature = 'N'; break;
451 case FN_OUTPUT: nature = 'O'; break;
452 case FN_AVG: nature = 'a'; break;
453 default: nature = '?'; break;
454 }
455
456 switch (field_scope(f, 0)) {
457 case FS_PROCESS: scope = 'P'; break;
458 case FS_SERVICE: scope = 'S'; break;
459 case FS_SYSTEM: scope = 's'; break;
460 case FS_CLUSTER: scope = 'C'; break;
461 default: scope = '?'; break;
462 }
463
464 return chunk_appendf(out, "%c%c%c%c", origin, nature, scope, delim);
465}
466
Simon Horman05ee2132017-01-04 09:37:25 +0100467/* Emits an encoding of the field type as JSON.
468 * Returns non-zero on success, 0 if the buffer is full.
469 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200470int stats_emit_json_field_tags(struct buffer *out, const struct field *f)
Simon Horman05ee2132017-01-04 09:37:25 +0100471{
472 const char *origin, *nature, *scope;
473 int old_len;
474
475 switch (field_origin(f, 0)) {
476 case FO_METRIC: origin = "Metric"; break;
477 case FO_STATUS: origin = "Status"; break;
478 case FO_KEY: origin = "Key"; break;
479 case FO_CONFIG: origin = "Config"; break;
480 case FO_PRODUCT: origin = "Product"; break;
481 default: origin = "Unknown"; break;
482 }
483
484 switch (field_nature(f, 0)) {
485 case FN_GAUGE: nature = "Gauge"; break;
486 case FN_LIMIT: nature = "Limit"; break;
487 case FN_MIN: nature = "Min"; break;
488 case FN_MAX: nature = "Max"; break;
489 case FN_RATE: nature = "Rate"; break;
490 case FN_COUNTER: nature = "Counter"; break;
491 case FN_DURATION: nature = "Duration"; break;
492 case FN_AGE: nature = "Age"; break;
493 case FN_TIME: nature = "Time"; break;
494 case FN_NAME: nature = "Name"; break;
495 case FN_OUTPUT: nature = "Output"; break;
496 case FN_AVG: nature = "Avg"; break;
497 default: nature = "Unknown"; break;
498 }
499
500 switch (field_scope(f, 0)) {
501 case FS_PROCESS: scope = "Process"; break;
502 case FS_SERVICE: scope = "Service"; break;
503 case FS_SYSTEM: scope = "System"; break;
504 case FS_CLUSTER: scope = "Cluster"; break;
505 default: scope = "Unknown"; break;
506 }
507
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200508 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100509 chunk_appendf(out, "\"tags\":{"
510 "\"origin\":\"%s\","
511 "\"nature\":\"%s\","
512 "\"scope\":\"%s\""
513 "}", origin, nature, scope);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200514 return !(old_len == out->data);
Simon Horman05ee2132017-01-04 09:37:25 +0100515}
William Lallemand74c24fb2016-11-21 17:18:36 +0100516
517/* Dump all fields from <stats> into <out> using CSV format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200518static int stats_dump_fields_csv(struct buffer *out,
519 const struct field *stats)
William Lallemand74c24fb2016-11-21 17:18:36 +0100520{
521 int field;
522
523 for (field = 0; field < ST_F_TOTAL_FIELDS; field++) {
524 if (!stats_emit_raw_data_field(out, &stats[field]))
525 return 0;
526 if (!chunk_strcat(out, ","))
527 return 0;
528 }
529 chunk_strcat(out, "\n");
530 return 1;
531}
532
533/* Dump all fields from <stats> into <out> using a typed "field:desc:type:value" format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200534static int stats_dump_fields_typed(struct buffer *out,
535 const struct field *stats)
William Lallemand74c24fb2016-11-21 17:18:36 +0100536{
537 int field;
538
539 for (field = 0; field < ST_F_TOTAL_FIELDS; field++) {
540 if (!stats[field].type)
541 continue;
542
543 chunk_appendf(out, "%c.%u.%u.%d.%s.%u:",
544 stats[ST_F_TYPE].u.u32 == STATS_TYPE_FE ? 'F' :
545 stats[ST_F_TYPE].u.u32 == STATS_TYPE_BE ? 'B' :
546 stats[ST_F_TYPE].u.u32 == STATS_TYPE_SO ? 'L' :
547 stats[ST_F_TYPE].u.u32 == STATS_TYPE_SV ? 'S' :
548 '?',
549 stats[ST_F_IID].u.u32, stats[ST_F_SID].u.u32,
550 field, stat_field_names[field], stats[ST_F_PID].u.u32);
551
552 if (!stats_emit_field_tags(out, &stats[field], ':'))
553 return 0;
554 if (!stats_emit_typed_data_field(out, &stats[field]))
555 return 0;
556 if (!chunk_strcat(out, "\n"))
557 return 0;
558 }
559 return 1;
560}
561
Simon Horman05ee2132017-01-04 09:37:25 +0100562/* Dump all fields from <stats> into <out> using the "show info json" format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200563static int stats_dump_json_info_fields(struct buffer *out,
Simon Horman05ee2132017-01-04 09:37:25 +0100564 const struct field *info)
565{
566 int field;
567 int started = 0;
568
569 if (!chunk_strcat(out, "["))
570 return 0;
571
572 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
573 int old_len;
574
575 if (!field_format(info, field))
576 continue;
577
578 if (started && !chunk_strcat(out, ","))
579 goto err;
580 started = 1;
581
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200582 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100583 chunk_appendf(out,
584 "{\"field\":{\"pos\":%d,\"name\":\"%s\"},"
585 "\"processNum\":%u,",
586 field, info_field_names[field],
587 info[INF_PROCESS_NUM].u.u32);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200588 if (old_len == out->data)
Simon Horman05ee2132017-01-04 09:37:25 +0100589 goto err;
590
591 if (!stats_emit_json_field_tags(out, &info[field]))
592 goto err;
593
594 if (!stats_emit_json_data_field(out, &info[field]))
595 goto err;
596
597 if (!chunk_strcat(out, "}"))
598 goto err;
599 }
600
601 if (!chunk_strcat(out, "]"))
602 goto err;
603 return 1;
604
605err:
606 chunk_reset(out);
607 chunk_appendf(out, "{\"errorStr\":\"output buffer too short\"}");
608 return 0;
609}
610
611/* Dump all fields from <stats> into <out> using a typed "field:desc:type:value" format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200612static int stats_dump_fields_json(struct buffer *out,
613 const struct field *stats,
Simon Horman05ee2132017-01-04 09:37:25 +0100614 int first_stat)
615{
616 int field;
617 int started = 0;
618
619 if (!first_stat && !chunk_strcat(out, ","))
620 return 0;
621 if (!chunk_strcat(out, "["))
622 return 0;
623
624 for (field = 0; field < ST_F_TOTAL_FIELDS; field++) {
625 const char *obj_type;
626 int old_len;
627
628 if (!stats[field].type)
629 continue;
630
631 if (started && !chunk_strcat(out, ","))
632 goto err;
633 started = 1;
634
635 switch (stats[ST_F_TYPE].u.u32) {
636 case STATS_TYPE_FE: obj_type = "Frontend"; break;
637 case STATS_TYPE_BE: obj_type = "Backend"; break;
638 case STATS_TYPE_SO: obj_type = "Listener"; break;
639 case STATS_TYPE_SV: obj_type = "Server"; break;
640 default: obj_type = "Unknown"; break;
641 }
642
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200643 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100644 chunk_appendf(out,
645 "{"
646 "\"objType\":\"%s\","
647 "\"proxyId\":%d,"
648 "\"id\":%d,"
649 "\"field\":{\"pos\":%d,\"name\":\"%s\"},"
650 "\"processNum\":%u,",
651 obj_type, stats[ST_F_IID].u.u32,
652 stats[ST_F_SID].u.u32, field,
653 stat_field_names[field], stats[ST_F_PID].u.u32);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200654 if (old_len == out->data)
Simon Horman05ee2132017-01-04 09:37:25 +0100655 goto err;
656
657 if (!stats_emit_json_field_tags(out, &stats[field]))
658 goto err;
659
660 if (!stats_emit_json_data_field(out, &stats[field]))
661 goto err;
662
663 if (!chunk_strcat(out, "}"))
664 goto err;
665 }
666
667 if (!chunk_strcat(out, "]"))
668 goto err;
669
670 return 1;
671
672err:
673 chunk_reset(out);
674 if (!first_stat)
675 chunk_strcat(out, ",");
676 chunk_appendf(out, "{\"errorStr\":\"output buffer too short\"}");
677 return 0;
678}
679
William Lallemand74c24fb2016-11-21 17:18:36 +0100680/* Dump all fields from <stats> into <out> using the HTML format. A column is
681 * reserved for the checkbox is ST_SHOWADMIN is set in <flags>. Some extra info
682 * are provided if ST_SHLGNDS is present in <flags>.
683 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200684static int stats_dump_fields_html(struct buffer *out,
685 const struct field *stats,
686 unsigned int flags)
William Lallemand74c24fb2016-11-21 17:18:36 +0100687{
Willy Tarreau83061a82018-07-13 11:56:34 +0200688 struct buffer src;
William Lallemand74c24fb2016-11-21 17:18:36 +0100689
690 if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_FE) {
691 chunk_appendf(out,
692 /* name, queue */
693 "<tr class=\"frontend\">");
694
695 if (flags & ST_SHOWADMIN) {
696 /* Column sub-heading for Enable or Disable server */
697 chunk_appendf(out, "<td></td>");
698 }
699
700 chunk_appendf(out,
701 "<td class=ac>"
702 "<a name=\"%s/Frontend\"></a>"
703 "<a class=lfsb href=\"#%s/Frontend\">Frontend</a></td>"
704 "<td colspan=3></td>"
705 "",
706 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_PXNAME));
707
708 chunk_appendf(out,
709 /* sessions rate : current */
710 "<td><u>%s<div class=tips><table class=det>"
711 "<tr><th>Current connection rate:</th><td>%s/s</td></tr>"
712 "<tr><th>Current session rate:</th><td>%s/s</td></tr>"
713 "",
714 U2H(stats[ST_F_RATE].u.u32),
715 U2H(stats[ST_F_CONN_RATE].u.u32),
716 U2H(stats[ST_F_RATE].u.u32));
717
718 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
719 chunk_appendf(out,
720 "<tr><th>Current request rate:</th><td>%s/s</td></tr>",
721 U2H(stats[ST_F_REQ_RATE].u.u32));
722
723 chunk_appendf(out,
724 "</table></div></u></td>"
725 /* sessions rate : max */
726 "<td><u>%s<div class=tips><table class=det>"
727 "<tr><th>Max connection rate:</th><td>%s/s</td></tr>"
728 "<tr><th>Max session rate:</th><td>%s/s</td></tr>"
729 "",
730 U2H(stats[ST_F_RATE_MAX].u.u32),
731 U2H(stats[ST_F_CONN_RATE_MAX].u.u32),
732 U2H(stats[ST_F_RATE_MAX].u.u32));
733
734 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
735 chunk_appendf(out,
736 "<tr><th>Max request rate:</th><td>%s/s</td></tr>",
737 U2H(stats[ST_F_REQ_RATE_MAX].u.u32));
738
739 chunk_appendf(out,
740 "</table></div></u></td>"
741 /* sessions rate : limit */
742 "<td>%s</td>",
743 LIM2A(stats[ST_F_RATE_LIM].u.u32, "-"));
744
745 chunk_appendf(out,
746 /* sessions: current, max, limit, total */
747 "<td>%s</td><td>%s</td><td>%s</td>"
748 "<td><u>%s<div class=tips><table class=det>"
749 "<tr><th>Cum. connections:</th><td>%s</td></tr>"
750 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
751 "",
752 U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SMAX].u.u32), U2H(stats[ST_F_SLIM].u.u32),
753 U2H(stats[ST_F_STOT].u.u64),
754 U2H(stats[ST_F_CONN_TOT].u.u64),
755 U2H(stats[ST_F_STOT].u.u64));
756
757 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
758 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
759 chunk_appendf(out,
760 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
761 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
762 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
763 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
764 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
765 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
766 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
767 "<tr><th>- other responses:</th><td>%s</td></tr>"
768 "<tr><th>Intercepted requests:</th><td>%s</td></tr>"
Willy Tarreaua1214a52018-12-14 14:00:25 +0100769 "<tr><th>Cache lookups:</th><td>%s</td></tr>"
770 "<tr><th>Cache hits:</th><td>%s</td><td>(%d%%)</td></tr>"
Willy Tarreau1b0f85e2018-05-28 15:12:40 +0200771 "<tr><th>Failed hdr rewrites:</th><td>%s</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +0100772 "",
773 U2H(stats[ST_F_REQ_TOT].u.u64),
774 U2H(stats[ST_F_HRSP_1XX].u.u64),
775 U2H(stats[ST_F_HRSP_2XX].u.u64),
776 U2H(stats[ST_F_COMP_RSP].u.u64),
777 stats[ST_F_HRSP_2XX].u.u64 ?
778 (int)(100 * stats[ST_F_COMP_RSP].u.u64 / stats[ST_F_HRSP_2XX].u.u64) : 0,
779 U2H(stats[ST_F_HRSP_3XX].u.u64),
780 U2H(stats[ST_F_HRSP_4XX].u.u64),
781 U2H(stats[ST_F_HRSP_5XX].u.u64),
782 U2H(stats[ST_F_HRSP_OTHER].u.u64),
Willy Tarreau1b0f85e2018-05-28 15:12:40 +0200783 U2H(stats[ST_F_INTERCEPTED].u.u64),
Willy Tarreaua1214a52018-12-14 14:00:25 +0100784 U2H(stats[ST_F_CACHE_LOOKUPS].u.u64),
785 U2H(stats[ST_F_CACHE_HITS].u.u64),
786 stats[ST_F_CACHE_LOOKUPS].u.u64 ?
787 (int)(100 * stats[ST_F_CACHE_HITS].u.u64 / stats[ST_F_CACHE_LOOKUPS].u.u64) : 0,
Willy Tarreau1b0f85e2018-05-28 15:12:40 +0200788 U2H(stats[ST_F_WREW].u.u64));
William Lallemand74c24fb2016-11-21 17:18:36 +0100789 }
790
791 chunk_appendf(out,
792 "</table></div></u></td>"
793 /* sessions: lbtot, lastsess */
794 "<td></td><td></td>"
795 /* bytes : in */
796 "<td>%s</td>"
797 "",
798 U2H(stats[ST_F_BIN].u.u64));
799
800 chunk_appendf(out,
801 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
802 "<td>%s%s<div class=tips><table class=det>"
803 "<tr><th>Response bytes in:</th><td>%s</td></tr>"
804 "<tr><th>Compression in:</th><td>%s</td></tr>"
805 "<tr><th>Compression out:</th><td>%s</td><td>(%d%%)</td></tr>"
806 "<tr><th>Compression bypass:</th><td>%s</td></tr>"
807 "<tr><th>Total bytes saved:</th><td>%s</td><td>(%d%%)</td></tr>"
808 "</table></div>%s</td>",
809 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "<u>":"",
810 U2H(stats[ST_F_BOUT].u.u64),
811 U2H(stats[ST_F_BOUT].u.u64),
812 U2H(stats[ST_F_COMP_IN].u.u64),
813 U2H(stats[ST_F_COMP_OUT].u.u64),
814 stats[ST_F_COMP_IN].u.u64 ? (int)(stats[ST_F_COMP_OUT].u.u64 * 100 / stats[ST_F_COMP_IN].u.u64) : 0,
815 U2H(stats[ST_F_COMP_BYP].u.u64),
816 U2H(stats[ST_F_COMP_IN].u.u64 - stats[ST_F_COMP_OUT].u.u64),
817 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,
818 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "</u>":"");
819
820 chunk_appendf(out,
821 /* denied: req, resp */
822 "<td>%s</td><td>%s</td>"
823 /* errors : request, connect, response */
824 "<td>%s</td><td></td><td></td>"
825 /* warnings: retries, redispatches */
826 "<td></td><td></td>"
827 /* server status : reflect frontend status */
828 "<td class=ac>%s</td>"
829 /* rest of server: nothing */
830 "<td class=ac colspan=8></td></tr>"
831 "",
832 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
833 U2H(stats[ST_F_EREQ].u.u64),
834 field_str(stats, ST_F_STATUS));
835 }
836 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_SO) {
837 chunk_appendf(out, "<tr class=socket>");
838 if (flags & ST_SHOWADMIN) {
839 /* Column sub-heading for Enable or Disable server */
840 chunk_appendf(out, "<td></td>");
841 }
842
843 chunk_appendf(out,
844 /* frontend name, listener name */
845 "<td class=ac><a name=\"%s/+%s\"></a>%s"
846 "<a class=lfsb href=\"#%s/+%s\">%s</a>"
847 "",
848 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME),
849 (flags & ST_SHLGNDS)?"<u>":"",
850 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME), field_str(stats, ST_F_SVNAME));
851
852 if (flags & ST_SHLGNDS) {
853 chunk_appendf(out, "<div class=tips>");
854
855 if (isdigit(*field_str(stats, ST_F_ADDR)))
856 chunk_appendf(out, "IPv4: %s, ", field_str(stats, ST_F_ADDR));
857 else if (*field_str(stats, ST_F_ADDR) == '[')
858 chunk_appendf(out, "IPv6: %s, ", field_str(stats, ST_F_ADDR));
859 else if (*field_str(stats, ST_F_ADDR))
860 chunk_appendf(out, "%s, ", field_str(stats, ST_F_ADDR));
861
862 /* id */
863 chunk_appendf(out, "id: %d</div>", stats[ST_F_SID].u.u32);
864 }
865
866 chunk_appendf(out,
867 /* queue */
868 "%s</td><td colspan=3></td>"
869 /* sessions rate: current, max, limit */
870 "<td colspan=3>&nbsp;</td>"
871 /* sessions: current, max, limit, total, lbtot, lastsess */
872 "<td>%s</td><td>%s</td><td>%s</td>"
873 "<td>%s</td><td>&nbsp;</td><td>&nbsp;</td>"
874 /* bytes: in, out */
875 "<td>%s</td><td>%s</td>"
876 "",
877 (flags & ST_SHLGNDS)?"</u>":"",
878 U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SMAX].u.u32), U2H(stats[ST_F_SLIM].u.u32),
879 U2H(stats[ST_F_STOT].u.u64), U2H(stats[ST_F_BIN].u.u64), U2H(stats[ST_F_BOUT].u.u64));
880
881 chunk_appendf(out,
882 /* denied: req, resp */
883 "<td>%s</td><td>%s</td>"
884 /* errors: request, connect, response */
885 "<td>%s</td><td></td><td></td>"
886 /* warnings: retries, redispatches */
887 "<td></td><td></td>"
888 /* server status: reflect listener status */
889 "<td class=ac>%s</td>"
890 /* rest of server: nothing */
891 "<td class=ac colspan=8></td></tr>"
892 "",
893 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
894 U2H(stats[ST_F_EREQ].u.u64),
895 field_str(stats, ST_F_STATUS));
896 }
897 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_SV) {
898 const char *style;
899
900 /* determine the style to use depending on the server's state,
901 * its health and weight. There isn't a 1-to-1 mapping between
902 * state and styles for the cases where the server is (still)
903 * up. The reason is that we don't want to report nolb and
904 * drain with the same color.
905 */
906
907 if (strcmp(field_str(stats, ST_F_STATUS), "DOWN") == 0 ||
908 strcmp(field_str(stats, ST_F_STATUS), "DOWN (agent)") == 0) {
909 style = "down";
910 }
911 else if (strcmp(field_str(stats, ST_F_STATUS), "DOWN ") == 0) {
912 style = "going_up";
913 }
914 else if (strcmp(field_str(stats, ST_F_STATUS), "NOLB ") == 0) {
915 style = "going_down";
916 }
917 else if (strcmp(field_str(stats, ST_F_STATUS), "NOLB") == 0) {
918 style = "nolb";
919 }
920 else if (strcmp(field_str(stats, ST_F_STATUS), "no check") == 0) {
921 style = "no_check";
922 }
923 else if (!stats[ST_F_CHKFAIL].type ||
924 stats[ST_F_CHECK_HEALTH].u.u32 == stats[ST_F_CHECK_RISE].u.u32 + stats[ST_F_CHECK_FALL].u.u32 - 1) {
925 /* no check or max health = UP */
926 if (stats[ST_F_WEIGHT].u.u32)
927 style = "up";
928 else
929 style = "draining";
930 }
931 else {
932 style = "going_down";
933 }
934
935 if (memcmp(field_str(stats, ST_F_STATUS), "MAINT", 5) == 0)
936 chunk_appendf(out, "<tr class=\"maintain\">");
937 else
938 chunk_appendf(out,
939 "<tr class=\"%s_%s\">",
940 (stats[ST_F_BCK].u.u32) ? "backup" : "active", style);
941
942
943 if (flags & ST_SHOWADMIN)
944 chunk_appendf(out,
David Harrigand3db35a2016-12-30 12:12:49 +0000945 "<td><input class='%s-checkbox' type=\"checkbox\" name=\"s\" value=\"%s\"></td>",
946 field_str(stats, ST_F_PXNAME),
William Lallemand74c24fb2016-11-21 17:18:36 +0100947 field_str(stats, ST_F_SVNAME));
948
949 chunk_appendf(out,
950 "<td class=ac><a name=\"%s/%s\"></a>%s"
951 "<a class=lfsb href=\"#%s/%s\">%s</a>"
952 "",
953 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME),
954 (flags & ST_SHLGNDS) ? "<u>" : "",
955 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME), field_str(stats, ST_F_SVNAME));
956
957 if (flags & ST_SHLGNDS) {
958 chunk_appendf(out, "<div class=tips>");
959
960 if (isdigit(*field_str(stats, ST_F_ADDR)))
961 chunk_appendf(out, "IPv4: %s, ", field_str(stats, ST_F_ADDR));
962 else if (*field_str(stats, ST_F_ADDR) == '[')
963 chunk_appendf(out, "IPv6: %s, ", field_str(stats, ST_F_ADDR));
964 else if (*field_str(stats, ST_F_ADDR))
965 chunk_appendf(out, "%s, ", field_str(stats, ST_F_ADDR));
966
967 /* id */
968 chunk_appendf(out, "id: %d", stats[ST_F_SID].u.u32);
969
970 /* cookie */
971 if (stats[ST_F_COOKIE].type) {
972 chunk_appendf(out, ", cookie: '");
973 chunk_initstr(&src, field_str(stats, ST_F_COOKIE));
974 chunk_htmlencode(out, &src);
975 chunk_appendf(out, "'");
976 }
977
978 chunk_appendf(out, "</div>");
979 }
980
981 chunk_appendf(out,
982 /* queue : current, max, limit */
983 "%s</td><td>%s</td><td>%s</td><td>%s</td>"
984 /* sessions rate : current, max, limit */
985 "<td>%s</td><td>%s</td><td></td>"
986 "",
987 (flags & ST_SHLGNDS) ? "</u>" : "",
988 U2H(stats[ST_F_QCUR].u.u32), U2H(stats[ST_F_QMAX].u.u32), LIM2A(stats[ST_F_QLIMIT].u.u32, "-"),
989 U2H(stats[ST_F_RATE].u.u32), U2H(stats[ST_F_RATE_MAX].u.u32));
990
991 chunk_appendf(out,
992 /* sessions: current, max, limit, total */
Willy Tarreau0ff699e2019-09-08 09:24:56 +0200993 "<td><u>%s<div class=tips>"
994 "<table class=det>"
995 "<tr><th>Current active connections:</th><td>%s</td></tr>"
996 "<tr><th>Current idle connections:</th><td>%s</td></tr>"
997 "<tr><th>Active connections limit:</th><td>%s</td></tr>"
998 "<tr><th>Idle connections limit:</th><td>%s</td></tr>"
999 "</table></div></u>"
1000 "</td><td>%s</td><td>%s</td>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001001 "<td><u>%s<div class=tips><table class=det>"
1002 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
1003 "",
Willy Tarreau0ff699e2019-09-08 09:24:56 +02001004 U2H(stats[ST_F_SCUR].u.u32),
1005 U2H(stats[ST_F_SCUR].u.u32),
1006 U2H(stats[ST_F_SRV_ICUR].u.u32),
1007 LIM2A(stats[ST_F_SLIM].u.u32, "-"),
1008 stats[ST_F_SRV_ILIM].type ? U2H(stats[ST_F_SRV_ILIM].u.u32) : "-",
1009 U2H(stats[ST_F_SMAX].u.u32), LIM2A(stats[ST_F_SLIM].u.u32, "-"),
William Lallemand74c24fb2016-11-21 17:18:36 +01001010 U2H(stats[ST_F_STOT].u.u64),
1011 U2H(stats[ST_F_STOT].u.u64));
1012
1013 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
1014 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
1015 unsigned long long tot;
1016
1017 tot = stats[ST_F_HRSP_OTHER].u.u64;
1018 tot += stats[ST_F_HRSP_1XX].u.u64;
1019 tot += stats[ST_F_HRSP_2XX].u.u64;
1020 tot += stats[ST_F_HRSP_3XX].u.u64;
1021 tot += stats[ST_F_HRSP_4XX].u.u64;
1022 tot += stats[ST_F_HRSP_5XX].u.u64;
1023
1024 chunk_appendf(out,
Willy Tarreauf1573842018-12-14 11:35:36 +01001025 "<tr><th>New connections:</th><td>%s</td></tr>"
1026 "<tr><th>Reused connections:</th><td>%s</td><td>(%d%%)</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001027 "<tr><th>Cum. HTTP responses:</th><td>%s</td></tr>"
1028 "<tr><th>- HTTP 1xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1029 "<tr><th>- HTTP 2xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1030 "<tr><th>- HTTP 3xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1031 "<tr><th>- HTTP 4xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1032 "<tr><th>- HTTP 5xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1033 "<tr><th>- other responses:</th><td>%s</td><td>(%d%%)</td></tr>"
Willy Tarreau1b0f85e2018-05-28 15:12:40 +02001034 "<tr><th>Failed hdr rewrites:</th><td>%s</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001035 "",
Willy Tarreauf1573842018-12-14 11:35:36 +01001036 U2H(stats[ST_F_CONNECT].u.u64),
1037 U2H(stats[ST_F_REUSE].u.u64),
1038 (stats[ST_F_CONNECT].u.u64 + stats[ST_F_REUSE].u.u64) ?
1039 (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 +01001040 U2H(tot),
1041 U2H(stats[ST_F_HRSP_1XX].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_1XX].u.u64 / tot) : 0,
1042 U2H(stats[ST_F_HRSP_2XX].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_2XX].u.u64 / tot) : 0,
1043 U2H(stats[ST_F_HRSP_3XX].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_3XX].u.u64 / tot) : 0,
1044 U2H(stats[ST_F_HRSP_4XX].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_4XX].u.u64 / tot) : 0,
1045 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 +02001046 U2H(stats[ST_F_HRSP_OTHER].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_OTHER].u.u64 / tot) : 0,
1047 U2H(stats[ST_F_WREW].u.u64));
William Lallemand74c24fb2016-11-21 17:18:36 +01001048 }
1049
1050 chunk_appendf(out, "<tr><th colspan=3>Avg over last 1024 success. conn.</th></tr>");
1051 chunk_appendf(out, "<tr><th>- Queue time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_QTIME].u.u32));
1052 chunk_appendf(out, "<tr><th>- Connect time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_CTIME].u.u32));
1053 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
1054 chunk_appendf(out, "<tr><th>- Response time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_RTIME].u.u32));
1055 chunk_appendf(out, "<tr><th>- Total time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_TTIME].u.u32));
1056
1057 chunk_appendf(out,
1058 "</table></div></u></td>"
1059 /* sessions: lbtot, last */
1060 "<td>%s</td><td>%s</td>",
1061 U2H(stats[ST_F_LBTOT].u.u64),
1062 human_time(stats[ST_F_LASTSESS].u.s32, 1));
1063
1064 chunk_appendf(out,
1065 /* bytes : in, out */
1066 "<td>%s</td><td>%s</td>"
1067 /* denied: req, resp */
1068 "<td></td><td>%s</td>"
1069 /* errors : request, connect */
1070 "<td></td><td>%s</td>"
1071 /* errors : response */
1072 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
1073 /* warnings: retries, redispatches */
1074 "<td>%lld</td><td>%lld</td>"
1075 "",
1076 U2H(stats[ST_F_BIN].u.u64), U2H(stats[ST_F_BOUT].u.u64),
1077 U2H(stats[ST_F_DRESP].u.u64),
1078 U2H(stats[ST_F_ECON].u.u64),
1079 U2H(stats[ST_F_ERESP].u.u64),
1080 (long long)stats[ST_F_CLI_ABRT].u.u64,
1081 (long long)stats[ST_F_SRV_ABRT].u.u64,
1082 (long long)stats[ST_F_WRETR].u.u64,
1083 (long long)stats[ST_F_WREDIS].u.u64);
1084
1085 /* status, last change */
1086 chunk_appendf(out, "<td class=ac>");
1087
1088 /* FIXME!!!!
1089 * LASTCHG should contain the last change for *this* server and must be computed
1090 * properly above, as was done below, ie: this server if maint, otherwise ref server
1091 * if tracking. Note that ref is either local or remote depending on tracking.
1092 */
1093
1094
1095 if (memcmp(field_str(stats, ST_F_STATUS), "MAINT", 5) == 0) {
1096 chunk_appendf(out, "%s MAINT", human_time(stats[ST_F_LASTCHG].u.u32, 1));
1097 }
1098 else if (memcmp(field_str(stats, ST_F_STATUS), "no check", 5) == 0) {
1099 chunk_strcat(out, "<i>no check</i>");
1100 }
1101 else {
1102 chunk_appendf(out, "%s %s", human_time(stats[ST_F_LASTCHG].u.u32, 1), field_str(stats, ST_F_STATUS));
1103 if (memcmp(field_str(stats, ST_F_STATUS), "DOWN", 4) == 0) {
1104 if (stats[ST_F_CHECK_HEALTH].u.u32)
1105 chunk_strcat(out, " &uarr;");
1106 }
1107 else if (stats[ST_F_CHECK_HEALTH].u.u32 < stats[ST_F_CHECK_RISE].u.u32 + stats[ST_F_CHECK_FALL].u.u32 - 1)
1108 chunk_strcat(out, " &darr;");
1109 }
1110
1111 if (memcmp(field_str(stats, ST_F_STATUS), "DOWN", 4) == 0 &&
1112 stats[ST_F_AGENT_STATUS].type && !stats[ST_F_AGENT_HEALTH].u.u32) {
1113 chunk_appendf(out,
1114 "</td><td class=ac><u> %s",
1115 field_str(stats, ST_F_AGENT_STATUS));
1116
1117 if (stats[ST_F_AGENT_CODE].type)
1118 chunk_appendf(out, "/%d", stats[ST_F_AGENT_CODE].u.u32);
1119
1120 if (stats[ST_F_AGENT_DURATION].type)
1121 chunk_appendf(out, " in %lums", (long)stats[ST_F_AGENT_DURATION].u.u64);
1122
1123 chunk_appendf(out, "<div class=tips>%s", field_str(stats, ST_F_AGENT_DESC));
1124
1125 if (*field_str(stats, ST_F_LAST_AGT)) {
1126 chunk_appendf(out, ": ");
1127 chunk_initstr(&src, field_str(stats, ST_F_LAST_AGT));
1128 chunk_htmlencode(out, &src);
1129 }
1130 chunk_appendf(out, "</div></u>");
1131 }
1132 else if (stats[ST_F_CHECK_STATUS].type) {
1133 chunk_appendf(out,
1134 "</td><td class=ac><u> %s",
1135 field_str(stats, ST_F_CHECK_STATUS));
1136
1137 if (stats[ST_F_CHECK_CODE].type)
1138 chunk_appendf(out, "/%d", stats[ST_F_CHECK_CODE].u.u32);
1139
1140 if (stats[ST_F_CHECK_DURATION].type)
1141 chunk_appendf(out, " in %lums", (long)stats[ST_F_CHECK_DURATION].u.u64);
1142
1143 chunk_appendf(out, "<div class=tips>%s", field_str(stats, ST_F_CHECK_DESC));
1144
1145 if (*field_str(stats, ST_F_LAST_CHK)) {
1146 chunk_appendf(out, ": ");
1147 chunk_initstr(&src, field_str(stats, ST_F_LAST_CHK));
1148 chunk_htmlencode(out, &src);
1149 }
1150 chunk_appendf(out, "</div></u>");
1151 }
1152 else
1153 chunk_appendf(out, "</td><td>");
1154
1155 chunk_appendf(out,
1156 /* weight */
1157 "</td><td class=ac>%d</td>"
1158 /* act, bck */
1159 "<td class=ac>%s</td><td class=ac>%s</td>"
1160 "",
1161 stats[ST_F_WEIGHT].u.u32,
1162 stats[ST_F_BCK].u.u32 ? "-" : "Y",
1163 stats[ST_F_BCK].u.u32 ? "Y" : "-");
1164
1165 /* check failures: unique, fatal, down time */
1166 if (strcmp(field_str(stats, ST_F_STATUS), "MAINT (resolution)") == 0) {
1167 chunk_appendf(out, "<td class=ac colspan=3>resolution</td>");
1168 }
1169 else if (stats[ST_F_CHKFAIL].type) {
1170 chunk_appendf(out, "<td><u>%lld", (long long)stats[ST_F_CHKFAIL].u.u64);
1171
1172 if (stats[ST_F_HANAFAIL].type)
1173 chunk_appendf(out, "/%lld", (long long)stats[ST_F_HANAFAIL].u.u64);
1174
1175 chunk_appendf(out,
1176 "<div class=tips>Failed Health Checks%s</div></u></td>"
1177 "<td>%lld</td><td>%s</td>"
1178 "",
1179 stats[ST_F_HANAFAIL].type ? "/Health Analyses" : "",
1180 (long long)stats[ST_F_CHKDOWN].u.u64, human_time(stats[ST_F_DOWNTIME].u.u32, 1));
1181 }
1182 else if (strcmp(field_str(stats, ST_F_STATUS), "MAINT") != 0 && field_format(stats, ST_F_TRACKED) == FF_STR) {
1183 /* tracking a server (hence inherited maint would appear as "MAINT (via...)" */
1184 chunk_appendf(out,
1185 "<td class=ac colspan=3><a class=lfsb href=\"#%s\">via %s</a></td>",
1186 field_str(stats, ST_F_TRACKED), field_str(stats, ST_F_TRACKED));
1187 }
1188 else
1189 chunk_appendf(out, "<td colspan=3></td>");
1190
1191 /* throttle */
1192 if (stats[ST_F_THROTTLE].type)
1193 chunk_appendf(out, "<td class=ac>%d %%</td></tr>\n", stats[ST_F_THROTTLE].u.u32);
1194 else
1195 chunk_appendf(out, "<td class=ac>-</td></tr>\n");
1196 }
1197 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_BE) {
1198 chunk_appendf(out, "<tr class=\"backend\">");
1199 if (flags & ST_SHOWADMIN) {
1200 /* Column sub-heading for Enable or Disable server */
1201 chunk_appendf(out, "<td></td>");
1202 }
1203 chunk_appendf(out,
1204 "<td class=ac>"
1205 /* name */
1206 "%s<a name=\"%s/Backend\"></a>"
1207 "<a class=lfsb href=\"#%s/Backend\">Backend</a>"
1208 "",
1209 (flags & ST_SHLGNDS)?"<u>":"",
1210 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_PXNAME));
1211
1212 if (flags & ST_SHLGNDS) {
1213 /* balancing */
1214 chunk_appendf(out, "<div class=tips>balancing: %s",
1215 field_str(stats, ST_F_ALGO));
1216
1217 /* cookie */
1218 if (stats[ST_F_COOKIE].type) {
1219 chunk_appendf(out, ", cookie: '");
1220 chunk_initstr(&src, field_str(stats, ST_F_COOKIE));
1221 chunk_htmlencode(out, &src);
1222 chunk_appendf(out, "'");
1223 }
1224 chunk_appendf(out, "</div>");
1225 }
1226
1227 chunk_appendf(out,
1228 "%s</td>"
1229 /* queue : current, max */
1230 "<td>%s</td><td>%s</td><td></td>"
1231 /* sessions rate : current, max, limit */
1232 "<td>%s</td><td>%s</td><td></td>"
1233 "",
1234 (flags & ST_SHLGNDS)?"</u>":"",
1235 U2H(stats[ST_F_QCUR].u.u32), U2H(stats[ST_F_QMAX].u.u32),
1236 U2H(stats[ST_F_RATE].u.u32), U2H(stats[ST_F_RATE_MAX].u.u32));
1237
1238 chunk_appendf(out,
1239 /* sessions: current, max, limit, total */
1240 "<td>%s</td><td>%s</td><td>%s</td>"
1241 "<td><u>%s<div class=tips><table class=det>"
1242 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
1243 "",
Willy Tarreau8e0f1752016-12-12 15:07:29 +01001244 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 +01001245 U2H(stats[ST_F_STOT].u.u64),
1246 U2H(stats[ST_F_STOT].u.u64));
1247
1248 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
1249 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
1250 chunk_appendf(out,
Willy Tarreauf1573842018-12-14 11:35:36 +01001251 "<tr><th>New connections:</th><td>%s</td></tr>"
1252 "<tr><th>Reused connections:</th><td>%s</td><td>(%d%%)</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001253 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
1254 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
1255 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
1256 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
1257 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
1258 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
1259 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
1260 "<tr><th>- other responses:</th><td>%s</td></tr>"
Willy Tarreaua1214a52018-12-14 14:00:25 +01001261 "<tr><th>Cache lookups:</th><td>%s</td></tr>"
1262 "<tr><th>Cache hits:</th><td>%s</td><td>(%d%%)</td></tr>"
Willy Tarreau1b0f85e2018-05-28 15:12:40 +02001263 "<tr><th>Failed hdr rewrites:</th><td>%s</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001264 "<tr><th colspan=3>Avg over last 1024 success. conn.</th></tr>"
1265 "",
Willy Tarreauf1573842018-12-14 11:35:36 +01001266 U2H(stats[ST_F_CONNECT].u.u64),
1267 U2H(stats[ST_F_REUSE].u.u64),
1268 (stats[ST_F_CONNECT].u.u64 + stats[ST_F_REUSE].u.u64) ?
1269 (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 +01001270 U2H(stats[ST_F_REQ_TOT].u.u64),
1271 U2H(stats[ST_F_HRSP_1XX].u.u64),
1272 U2H(stats[ST_F_HRSP_2XX].u.u64),
1273 U2H(stats[ST_F_COMP_RSP].u.u64),
1274 stats[ST_F_HRSP_2XX].u.u64 ?
1275 (int)(100 * stats[ST_F_COMP_RSP].u.u64 / stats[ST_F_HRSP_2XX].u.u64) : 0,
1276 U2H(stats[ST_F_HRSP_3XX].u.u64),
1277 U2H(stats[ST_F_HRSP_4XX].u.u64),
1278 U2H(stats[ST_F_HRSP_5XX].u.u64),
Willy Tarreaufeead3a2018-12-14 13:48:44 +01001279 U2H(stats[ST_F_HRSP_OTHER].u.u64),
Willy Tarreaua1214a52018-12-14 14:00:25 +01001280 U2H(stats[ST_F_CACHE_LOOKUPS].u.u64),
1281 U2H(stats[ST_F_CACHE_HITS].u.u64),
1282 stats[ST_F_CACHE_LOOKUPS].u.u64 ?
1283 (int)(100 * stats[ST_F_CACHE_HITS].u.u64 / stats[ST_F_CACHE_LOOKUPS].u.u64) : 0,
Willy Tarreaufeead3a2018-12-14 13:48:44 +01001284 U2H(stats[ST_F_WREW].u.u64));
William Lallemand74c24fb2016-11-21 17:18:36 +01001285 }
1286
1287 chunk_appendf(out, "<tr><th>- Queue time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_QTIME].u.u32));
1288 chunk_appendf(out, "<tr><th>- Connect time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_CTIME].u.u32));
1289 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
1290 chunk_appendf(out, "<tr><th>- Response time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_RTIME].u.u32));
1291 chunk_appendf(out, "<tr><th>- Total time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_TTIME].u.u32));
1292
1293 chunk_appendf(out,
1294 "</table></div></u></td>"
1295 /* sessions: lbtot, last */
1296 "<td>%s</td><td>%s</td>"
1297 /* bytes: in */
1298 "<td>%s</td>"
1299 "",
1300 U2H(stats[ST_F_LBTOT].u.u64),
1301 human_time(stats[ST_F_LASTSESS].u.s32, 1),
1302 U2H(stats[ST_F_BIN].u.u64));
1303
1304 chunk_appendf(out,
1305 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
1306 "<td>%s%s<div class=tips><table class=det>"
1307 "<tr><th>Response bytes in:</th><td>%s</td></tr>"
1308 "<tr><th>Compression in:</th><td>%s</td></tr>"
1309 "<tr><th>Compression out:</th><td>%s</td><td>(%d%%)</td></tr>"
1310 "<tr><th>Compression bypass:</th><td>%s</td></tr>"
1311 "<tr><th>Total bytes saved:</th><td>%s</td><td>(%d%%)</td></tr>"
1312 "</table></div>%s</td>",
1313 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "<u>":"",
1314 U2H(stats[ST_F_BOUT].u.u64),
1315 U2H(stats[ST_F_BOUT].u.u64),
1316 U2H(stats[ST_F_COMP_IN].u.u64),
1317 U2H(stats[ST_F_COMP_OUT].u.u64),
1318 stats[ST_F_COMP_IN].u.u64 ? (int)(stats[ST_F_COMP_OUT].u.u64 * 100 / stats[ST_F_COMP_IN].u.u64) : 0,
1319 U2H(stats[ST_F_COMP_BYP].u.u64),
1320 U2H(stats[ST_F_COMP_IN].u.u64 - stats[ST_F_COMP_OUT].u.u64),
1321 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,
1322 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "</u>":"");
1323
1324 chunk_appendf(out,
1325 /* denied: req, resp */
1326 "<td>%s</td><td>%s</td>"
1327 /* errors : request, connect */
1328 "<td></td><td>%s</td>"
1329 /* errors : response */
1330 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
1331 /* warnings: retries, redispatches */
1332 "<td>%lld</td><td>%lld</td>"
1333 /* backend status: reflect backend status (up/down): we display UP
1334 * if the backend has known working servers or if it has no server at
1335 * all (eg: for stats). Then we display the total weight, number of
1336 * active and backups. */
1337 "<td class=ac>%s %s</td><td class=ac>&nbsp;</td><td class=ac>%d</td>"
1338 "<td class=ac>%d</td><td class=ac>%d</td>"
1339 "",
1340 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
1341 U2H(stats[ST_F_ECON].u.u64),
1342 U2H(stats[ST_F_ERESP].u.u64),
1343 (long long)stats[ST_F_CLI_ABRT].u.u64,
1344 (long long)stats[ST_F_SRV_ABRT].u.u64,
1345 (long long)stats[ST_F_WRETR].u.u64, (long long)stats[ST_F_WREDIS].u.u64,
1346 human_time(stats[ST_F_LASTCHG].u.u32, 1),
1347 strcmp(field_str(stats, ST_F_STATUS), "DOWN") ? field_str(stats, ST_F_STATUS) : "<font color=\"red\"><b>DOWN</b></font>",
1348 stats[ST_F_WEIGHT].u.u32,
1349 stats[ST_F_ACT].u.u32, stats[ST_F_BCK].u.u32);
1350
1351 chunk_appendf(out,
1352 /* rest of backend: nothing, down transitions, total downtime, throttle */
1353 "<td class=ac>&nbsp;</td><td>%d</td>"
1354 "<td>%s</td>"
1355 "<td></td>"
1356 "</tr>",
1357 stats[ST_F_CHKDOWN].u.u32,
1358 stats[ST_F_DOWNTIME].type ? human_time(stats[ST_F_DOWNTIME].u.u32, 1) : "&nbsp;");
1359 }
1360 return 1;
1361}
1362
1363int stats_dump_one_line(const struct field *stats, unsigned int flags, struct proxy *px, struct appctx *appctx)
1364{
Simon Horman05ee2132017-01-04 09:37:25 +01001365 int ret;
1366
William Lallemand74c24fb2016-11-21 17:18:36 +01001367 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN))
1368 flags |= ST_SHOWADMIN;
1369
1370 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
Simon Horman05ee2132017-01-04 09:37:25 +01001371 ret = stats_dump_fields_html(&trash, stats, flags);
William Lallemand74c24fb2016-11-21 17:18:36 +01001372 else if (appctx->ctx.stats.flags & STAT_FMT_TYPED)
Simon Horman05ee2132017-01-04 09:37:25 +01001373 ret = stats_dump_fields_typed(&trash, stats);
1374 else if (appctx->ctx.stats.flags & STAT_FMT_JSON)
1375 ret = stats_dump_fields_json(&trash, stats,
1376 !(appctx->ctx.stats.flags &
1377 STAT_STARTED));
William Lallemand74c24fb2016-11-21 17:18:36 +01001378 else
Simon Horman05ee2132017-01-04 09:37:25 +01001379 ret = stats_dump_fields_csv(&trash, stats);
1380
1381 if (ret)
1382 appctx->ctx.stats.flags |= STAT_STARTED;
1383
1384 return ret;
William Lallemand74c24fb2016-11-21 17:18:36 +01001385}
1386
1387/* Fill <stats> with the frontend statistics. <stats> is
1388 * preallocated array of length <len>. The length of the array
1389 * must be at least ST_F_TOTAL_FIELDS. If this length is less then
1390 * this value, the function returns 0, otherwise, it returns 1.
1391 */
1392int stats_fill_fe_stats(struct proxy *px, struct field *stats, int len)
1393{
1394 if (len < ST_F_TOTAL_FIELDS)
1395 return 0;
1396
1397 memset(stats, 0, sizeof(*stats) * len);
1398
1399 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1400 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, "FRONTEND");
1401 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1402 stats[ST_F_SCUR] = mkf_u32(0, px->feconn);
1403 stats[ST_F_SMAX] = mkf_u32(FN_MAX, px->fe_counters.conn_max);
1404 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, px->maxconn);
1405 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, px->fe_counters.cum_sess);
1406 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, px->fe_counters.bytes_in);
1407 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, px->fe_counters.bytes_out);
1408 stats[ST_F_DREQ] = mkf_u64(FN_COUNTER, px->fe_counters.denied_req);
1409 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, px->fe_counters.denied_resp);
1410 stats[ST_F_EREQ] = mkf_u64(FN_COUNTER, px->fe_counters.failed_req);
1411 stats[ST_F_DCON] = mkf_u64(FN_COUNTER, px->fe_counters.denied_conn);
1412 stats[ST_F_DSES] = mkf_u64(FN_COUNTER, px->fe_counters.denied_sess);
1413 stats[ST_F_STATUS] = mkf_str(FO_STATUS, px->state == PR_STREADY ? "OPEN" : px->state == PR_STFULL ? "FULL" : "STOP");
1414 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
1415 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1416 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, 0);
1417 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_FE);
1418 stats[ST_F_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_sess_per_sec));
1419 stats[ST_F_RATE_LIM] = mkf_u32(FO_CONFIG|FN_LIMIT, px->fe_sps_lim);
1420 stats[ST_F_RATE_MAX] = mkf_u32(FN_MAX, px->fe_counters.sps_max);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001421 stats[ST_F_WREW] = mkf_u64(FN_COUNTER, px->fe_counters.failed_rewrites);
William Lallemand74c24fb2016-11-21 17:18:36 +01001422
1423 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
1424 if (px->mode == PR_MODE_HTTP) {
1425 stats[ST_F_HRSP_1XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[1]);
1426 stats[ST_F_HRSP_2XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[2]);
1427 stats[ST_F_HRSP_3XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[3]);
1428 stats[ST_F_HRSP_4XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[4]);
1429 stats[ST_F_HRSP_5XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[5]);
1430 stats[ST_F_HRSP_OTHER] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[0]);
1431 stats[ST_F_INTERCEPTED] = mkf_u64(FN_COUNTER, px->fe_counters.intercepted_req);
Willy Tarreaua1214a52018-12-14 14:00:25 +01001432 stats[ST_F_CACHE_LOOKUPS] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cache_lookups);
1433 stats[ST_F_CACHE_HITS] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cache_hits);
William Lallemand74c24fb2016-11-21 17:18:36 +01001434 }
1435
1436 /* requests : req_rate, req_rate_max, req_tot, */
1437 stats[ST_F_REQ_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_req_per_sec));
1438 stats[ST_F_REQ_RATE_MAX] = mkf_u32(FN_MAX, px->fe_counters.p.http.rps_max);
1439 stats[ST_F_REQ_TOT] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cum_req);
1440
1441 /* compression: in, out, bypassed, responses */
1442 stats[ST_F_COMP_IN] = mkf_u64(FN_COUNTER, px->fe_counters.comp_in);
1443 stats[ST_F_COMP_OUT] = mkf_u64(FN_COUNTER, px->fe_counters.comp_out);
1444 stats[ST_F_COMP_BYP] = mkf_u64(FN_COUNTER, px->fe_counters.comp_byp);
1445 stats[ST_F_COMP_RSP] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.comp_rsp);
1446
1447 /* connections : conn_rate, conn_rate_max, conn_tot, conn_max */
1448 stats[ST_F_CONN_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_conn_per_sec));
1449 stats[ST_F_CONN_RATE_MAX] = mkf_u32(FN_MAX, px->fe_counters.cps_max);
1450 stats[ST_F_CONN_TOT] = mkf_u64(FN_COUNTER, px->fe_counters.cum_conn);
1451
1452 return 1;
1453}
1454
1455/* Dumps a frontend's line to the trash for the current proxy <px> and uses
1456 * the state from stream interface <si>. The caller is responsible for clearing
1457 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
1458 */
1459static int stats_dump_fe_stats(struct stream_interface *si, struct proxy *px)
1460{
1461 struct appctx *appctx = __objt_appctx(si->end);
1462
1463 if (!(px->cap & PR_CAP_FE))
1464 return 0;
1465
1466 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_FE)))
1467 return 0;
1468
1469 if (!stats_fill_fe_stats(px, stats, ST_F_TOTAL_FIELDS))
1470 return 0;
1471
1472 return stats_dump_one_line(stats, 0, px, appctx);
1473}
1474
1475/* Fill <stats> with the listener statistics. <stats> is
1476 * preallocated array of length <len>. The length of the array
1477 * must be at least ST_F_TOTAL_FIELDS. If this length is less
1478 * then this value, the function returns 0, otherwise, it
1479 * returns 1. <flags> can take the value ST_SHLGNDS.
1480 */
1481int stats_fill_li_stats(struct proxy *px, struct listener *l, int flags,
1482 struct field *stats, int len)
1483{
Willy Tarreau83061a82018-07-13 11:56:34 +02001484 struct buffer *out = get_trash_chunk();
William Lallemand74c24fb2016-11-21 17:18:36 +01001485
1486 if (len < ST_F_TOTAL_FIELDS)
1487 return 0;
1488
1489 if (!l->counters)
1490 return 0;
1491
1492 chunk_reset(out);
1493 memset(stats, 0, sizeof(*stats) * len);
1494
1495 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1496 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, l->name);
1497 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1498 stats[ST_F_SCUR] = mkf_u32(0, l->nbconn);
1499 stats[ST_F_SMAX] = mkf_u32(FN_MAX, l->counters->conn_max);
1500 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, l->maxconn);
1501 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, l->counters->cum_conn);
1502 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, l->counters->bytes_in);
1503 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, l->counters->bytes_out);
1504 stats[ST_F_DREQ] = mkf_u64(FN_COUNTER, l->counters->denied_req);
1505 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, l->counters->denied_resp);
1506 stats[ST_F_EREQ] = mkf_u64(FN_COUNTER, l->counters->failed_req);
1507 stats[ST_F_DCON] = mkf_u64(FN_COUNTER, l->counters->denied_conn);
1508 stats[ST_F_DSES] = mkf_u64(FN_COUNTER, l->counters->denied_sess);
Willy Tarreaua8cf66b2019-02-27 16:49:00 +01001509 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 +01001510 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
1511 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1512 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, l->luid);
1513 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_SO);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001514 stats[ST_F_WREW] = mkf_u64(FN_COUNTER, l->counters->failed_rewrites);
William Lallemand74c24fb2016-11-21 17:18:36 +01001515
1516 if (flags & ST_SHLGNDS) {
1517 char str[INET6_ADDRSTRLEN];
1518 int port;
1519
1520 port = get_host_port(&l->addr);
1521 switch (addr_to_str(&l->addr, str, sizeof(str))) {
1522 case AF_INET:
1523 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1524 chunk_appendf(out, "%s:%d", str, port);
1525 break;
1526 case AF_INET6:
1527 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1528 chunk_appendf(out, "[%s]:%d", str, port);
1529 break;
1530 case AF_UNIX:
1531 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, "unix");
1532 break;
1533 case -1:
1534 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1535 chunk_strcat(out, strerror(errno));
1536 break;
1537 default: /* address family not supported */
1538 break;
1539 }
1540 }
1541
1542 return 1;
1543}
1544
1545/* Dumps a line for listener <l> and proxy <px> to the trash and uses the state
1546 * from stream interface <si>, and stats flags <flags>. The caller is responsible
1547 * for clearing the trash if needed. Returns non-zero if it emits anything, zero
1548 * otherwise.
1549 */
1550static int stats_dump_li_stats(struct stream_interface *si, struct proxy *px, struct listener *l, int flags)
1551{
1552 struct appctx *appctx = __objt_appctx(si->end);
1553
1554 if (!stats_fill_li_stats(px, l, flags, stats, ST_F_TOTAL_FIELDS))
1555 return 0;
1556
1557 return stats_dump_one_line(stats, flags, px, appctx);
1558}
1559
1560enum srv_stats_state {
1561 SRV_STATS_STATE_DOWN = 0,
1562 SRV_STATS_STATE_DOWN_AGENT,
1563 SRV_STATS_STATE_GOING_UP,
1564 SRV_STATS_STATE_UP_GOING_DOWN,
1565 SRV_STATS_STATE_UP,
1566 SRV_STATS_STATE_NOLB_GOING_DOWN,
1567 SRV_STATS_STATE_NOLB,
1568 SRV_STATS_STATE_DRAIN_GOING_DOWN,
1569 SRV_STATS_STATE_DRAIN,
1570 SRV_STATS_STATE_DRAIN_AGENT,
1571 SRV_STATS_STATE_NO_CHECK,
1572
1573 SRV_STATS_STATE_COUNT, /* Must be last */
1574};
1575
1576static const char *srv_hlt_st[SRV_STATS_STATE_COUNT] = {
1577 [SRV_STATS_STATE_DOWN] = "DOWN",
1578 [SRV_STATS_STATE_DOWN_AGENT] = "DOWN (agent)",
1579 [SRV_STATS_STATE_GOING_UP] = "DOWN %d/%d",
1580 [SRV_STATS_STATE_UP_GOING_DOWN] = "UP %d/%d",
1581 [SRV_STATS_STATE_UP] = "UP",
1582 [SRV_STATS_STATE_NOLB_GOING_DOWN] = "NOLB %d/%d",
1583 [SRV_STATS_STATE_NOLB] = "NOLB",
1584 [SRV_STATS_STATE_DRAIN_GOING_DOWN] = "DRAIN %d/%d",
1585 [SRV_STATS_STATE_DRAIN] = "DRAIN",
1586 [SRV_STATS_STATE_DRAIN_AGENT] = "DRAIN (agent)",
1587 [SRV_STATS_STATE_NO_CHECK] = "no check"
1588};
1589
1590/* Fill <stats> with the server statistics. <stats> is
1591 * preallocated array of length <len>. The length of the array
1592 * must be at least ST_F_TOTAL_FIELDS. If this length is less
1593 * then this value, the function returns 0, otherwise, it
1594 * returns 1. <flags> can take the value ST_SHLGNDS.
1595 */
1596int stats_fill_sv_stats(struct proxy *px, struct server *sv, int flags,
1597 struct field *stats, int len)
1598{
1599 struct server *via, *ref;
1600 char str[INET6_ADDRSTRLEN];
Willy Tarreau83061a82018-07-13 11:56:34 +02001601 struct buffer *out = get_trash_chunk();
William Lallemand74c24fb2016-11-21 17:18:36 +01001602 enum srv_stats_state state;
1603 char *fld_status;
1604
1605 if (len < ST_F_TOTAL_FIELDS)
1606 return 0;
1607
1608 memset(stats, 0, sizeof(*stats) * len);
1609
1610 /* we have "via" which is the tracked server as described in the configuration,
1611 * and "ref" which is the checked server and the end of the chain.
1612 */
1613 via = sv->track ? sv->track : sv;
1614 ref = via;
1615 while (ref->track)
1616 ref = ref->track;
1617
Emeric Brun52a91d32017-08-31 14:41:55 +02001618 if (sv->cur_state == SRV_ST_RUNNING || sv->cur_state == SRV_ST_STARTING) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001619 if ((ref->check.state & CHK_ST_ENABLED) &&
1620 (ref->check.health < ref->check.rise + ref->check.fall - 1)) {
1621 state = SRV_STATS_STATE_UP_GOING_DOWN;
1622 } else {
1623 state = SRV_STATS_STATE_UP;
1624 }
1625
Emeric Brun52a91d32017-08-31 14:41:55 +02001626 if (sv->cur_admin & SRV_ADMF_DRAIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001627 if (ref->agent.state & CHK_ST_ENABLED)
1628 state = SRV_STATS_STATE_DRAIN_AGENT;
1629 else if (state == SRV_STATS_STATE_UP_GOING_DOWN)
1630 state = SRV_STATS_STATE_DRAIN_GOING_DOWN;
1631 else
1632 state = SRV_STATS_STATE_DRAIN;
1633 }
1634
1635 if (state == SRV_STATS_STATE_UP && !(ref->check.state & CHK_ST_ENABLED)) {
1636 state = SRV_STATS_STATE_NO_CHECK;
1637 }
1638 }
Emeric Brun52a91d32017-08-31 14:41:55 +02001639 else if (sv->cur_state == SRV_ST_STOPPING) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001640 if ((!(sv->check.state & CHK_ST_ENABLED) && !sv->track) ||
1641 (ref->check.health == ref->check.rise + ref->check.fall - 1)) {
1642 state = SRV_STATS_STATE_NOLB;
1643 } else {
1644 state = SRV_STATS_STATE_NOLB_GOING_DOWN;
1645 }
1646 }
1647 else { /* stopped */
1648 if ((ref->agent.state & CHK_ST_ENABLED) && !ref->agent.health) {
1649 state = SRV_STATS_STATE_DOWN_AGENT;
1650 } else if ((ref->check.state & CHK_ST_ENABLED) && !ref->check.health) {
1651 state = SRV_STATS_STATE_DOWN; /* DOWN */
1652 } else if ((ref->agent.state & CHK_ST_ENABLED) || (ref->check.state & CHK_ST_ENABLED)) {
1653 state = SRV_STATS_STATE_GOING_UP;
1654 } else {
1655 state = SRV_STATS_STATE_DOWN; /* DOWN, unchecked */
1656 }
1657 }
1658
1659 chunk_reset(out);
1660
1661 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1662 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, sv->id);
1663 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1664 stats[ST_F_QCUR] = mkf_u32(0, sv->nbpend);
1665 stats[ST_F_QMAX] = mkf_u32(FN_MAX, sv->counters.nbpend_max);
1666 stats[ST_F_SCUR] = mkf_u32(0, sv->cur_sess);
1667 stats[ST_F_SMAX] = mkf_u32(FN_MAX, sv->counters.cur_sess_max);
1668
1669 if (sv->maxconn)
1670 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, sv->maxconn);
1671
Willy Tarreau0ff699e2019-09-08 09:24:56 +02001672 stats[ST_F_SRV_ICUR] = mkf_u32(0, sv->curr_idle_conns);
1673 if (sv->max_idle_conns != -1)
1674 stats[ST_F_SRV_ILIM] = mkf_u32(FO_CONFIG|FN_LIMIT, sv->max_idle_conns);
1675
William Lallemand74c24fb2016-11-21 17:18:36 +01001676 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, sv->counters.cum_sess);
1677 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, sv->counters.bytes_in);
1678 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, sv->counters.bytes_out);
1679 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, sv->counters.failed_secu);
1680 stats[ST_F_ECON] = mkf_u64(FN_COUNTER, sv->counters.failed_conns);
1681 stats[ST_F_ERESP] = mkf_u64(FN_COUNTER, sv->counters.failed_resp);
1682 stats[ST_F_WRETR] = mkf_u64(FN_COUNTER, sv->counters.retries);
1683 stats[ST_F_WREDIS] = mkf_u64(FN_COUNTER, sv->counters.redispatches);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001684 stats[ST_F_WREW] = mkf_u64(FN_COUNTER, sv->counters.failed_rewrites);
Willy Tarreauf1573842018-12-14 11:35:36 +01001685 stats[ST_F_CONNECT] = mkf_u64(FN_COUNTER, sv->counters.connect);
1686 stats[ST_F_REUSE] = mkf_u64(FN_COUNTER, sv->counters.reuse);
William Lallemand74c24fb2016-11-21 17:18:36 +01001687
1688 /* status */
1689 fld_status = chunk_newstr(out);
Emeric Brun52a91d32017-08-31 14:41:55 +02001690 if (sv->cur_admin & SRV_ADMF_RMAINT)
William Lallemand74c24fb2016-11-21 17:18:36 +01001691 chunk_appendf(out, "MAINT (resolution)");
Emeric Brun52a91d32017-08-31 14:41:55 +02001692 else if (sv->cur_admin & SRV_ADMF_IMAINT)
William Lallemand74c24fb2016-11-21 17:18:36 +01001693 chunk_appendf(out, "MAINT (via %s/%s)", via->proxy->id, via->id);
Emeric Brun52a91d32017-08-31 14:41:55 +02001694 else if (sv->cur_admin & SRV_ADMF_MAINT)
William Lallemand74c24fb2016-11-21 17:18:36 +01001695 chunk_appendf(out, "MAINT");
1696 else
1697 chunk_appendf(out,
1698 srv_hlt_st[state],
Emeric Brun52a91d32017-08-31 14:41:55 +02001699 (ref->cur_state != SRV_ST_STOPPED) ? (ref->check.health - ref->check.rise + 1) : (ref->check.health),
1700 (ref->cur_state != SRV_ST_STOPPED) ? (ref->check.fall) : (ref->check.rise));
William Lallemand74c24fb2016-11-21 17:18:36 +01001701
1702 stats[ST_F_STATUS] = mkf_str(FO_STATUS, fld_status);
1703 stats[ST_F_LASTCHG] = mkf_u32(FN_AGE, now.tv_sec - sv->last_change);
Emeric Brun52a91d32017-08-31 14:41:55 +02001704 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 +01001705 stats[ST_F_ACT] = mkf_u32(FO_STATUS, (sv->flags & SRV_F_BACKUP) ? 0 : 1);
1706 stats[ST_F_BCK] = mkf_u32(FO_STATUS, (sv->flags & SRV_F_BACKUP) ? 1 : 0);
1707
1708 /* check failures: unique, fatal; last change, total downtime */
1709 if (sv->check.state & CHK_ST_ENABLED) {
1710 stats[ST_F_CHKFAIL] = mkf_u64(FN_COUNTER, sv->counters.failed_checks);
1711 stats[ST_F_CHKDOWN] = mkf_u64(FN_COUNTER, sv->counters.down_trans);
1712 stats[ST_F_DOWNTIME] = mkf_u32(FN_COUNTER, srv_downtime(sv));
1713 }
1714
1715 if (sv->maxqueue)
1716 stats[ST_F_QLIMIT] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->maxqueue);
1717
1718 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
1719 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1720 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, sv->puid);
1721
Emeric Brun52a91d32017-08-31 14:41:55 +02001722 if (sv->cur_state == SRV_ST_STARTING && !server_is_draining(sv))
William Lallemand74c24fb2016-11-21 17:18:36 +01001723 stats[ST_F_THROTTLE] = mkf_u32(FN_AVG, server_throttle_rate(sv));
1724
1725 stats[ST_F_LBTOT] = mkf_u64(FN_COUNTER, sv->counters.cum_lbconn);
1726
1727 if (sv->track) {
1728 char *fld_track = chunk_newstr(out);
1729
1730 chunk_appendf(out, "%s/%s", sv->track->proxy->id, sv->track->id);
1731 stats[ST_F_TRACKED] = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, fld_track);
1732 }
1733
1734 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_SV);
1735 stats[ST_F_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&sv->sess_per_sec));
1736 stats[ST_F_RATE_MAX] = mkf_u32(FN_MAX, sv->counters.sps_max);
1737
1738 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) {
1739 const char *fld_chksts;
1740
1741 fld_chksts = chunk_newstr(out);
1742 chunk_strcat(out, "* "); // for check in progress
1743 chunk_strcat(out, get_check_status_info(sv->check.status));
1744 if (!(sv->check.state & CHK_ST_INPROGRESS))
1745 fld_chksts += 2; // skip "* "
1746 stats[ST_F_CHECK_STATUS] = mkf_str(FN_OUTPUT, fld_chksts);
1747
1748 if (sv->check.status >= HCHK_STATUS_L57DATA)
1749 stats[ST_F_CHECK_CODE] = mkf_u32(FN_OUTPUT, sv->check.code);
1750
1751 if (sv->check.status >= HCHK_STATUS_CHECKED)
1752 stats[ST_F_CHECK_DURATION] = mkf_u64(FN_DURATION, sv->check.duration);
1753
1754 stats[ST_F_CHECK_DESC] = mkf_str(FN_OUTPUT, get_check_status_description(sv->check.status));
1755 stats[ST_F_LAST_CHK] = mkf_str(FN_OUTPUT, sv->check.desc);
1756 stats[ST_F_CHECK_RISE] = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.rise);
1757 stats[ST_F_CHECK_FALL] = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.fall);
1758 stats[ST_F_CHECK_HEALTH] = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.health);
1759 }
1760
1761 if ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) {
1762 const char *fld_chksts;
1763
1764 fld_chksts = chunk_newstr(out);
1765 chunk_strcat(out, "* "); // for check in progress
1766 chunk_strcat(out, get_check_status_info(sv->agent.status));
1767 if (!(sv->agent.state & CHK_ST_INPROGRESS))
1768 fld_chksts += 2; // skip "* "
1769 stats[ST_F_AGENT_STATUS] = mkf_str(FN_OUTPUT, fld_chksts);
1770
1771 if (sv->agent.status >= HCHK_STATUS_L57DATA)
1772 stats[ST_F_AGENT_CODE] = mkf_u32(FN_OUTPUT, sv->agent.code);
1773
1774 if (sv->agent.status >= HCHK_STATUS_CHECKED)
1775 stats[ST_F_AGENT_DURATION] = mkf_u64(FN_DURATION, sv->agent.duration);
1776
1777 stats[ST_F_AGENT_DESC] = mkf_str(FN_OUTPUT, get_check_status_description(sv->agent.status));
1778 stats[ST_F_LAST_AGT] = mkf_str(FN_OUTPUT, sv->agent.desc);
1779 stats[ST_F_AGENT_RISE] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.rise);
1780 stats[ST_F_AGENT_FALL] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.fall);
1781 stats[ST_F_AGENT_HEALTH] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.health);
1782 }
1783
1784 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
1785 if (px->mode == PR_MODE_HTTP) {
1786 stats[ST_F_HRSP_1XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[1]);
1787 stats[ST_F_HRSP_2XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[2]);
1788 stats[ST_F_HRSP_3XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[3]);
1789 stats[ST_F_HRSP_4XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[4]);
1790 stats[ST_F_HRSP_5XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[5]);
1791 stats[ST_F_HRSP_OTHER] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[0]);
1792 }
1793
1794 if (ref->observe)
1795 stats[ST_F_HANAFAIL] = mkf_u64(FN_COUNTER, sv->counters.failed_hana);
1796
1797 stats[ST_F_CLI_ABRT] = mkf_u64(FN_COUNTER, sv->counters.cli_aborts);
1798 stats[ST_F_SRV_ABRT] = mkf_u64(FN_COUNTER, sv->counters.srv_aborts);
1799 stats[ST_F_LASTSESS] = mkf_s32(FN_AGE, srv_lastsession(sv));
1800
1801 stats[ST_F_QTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.q_time, TIME_STATS_SAMPLES));
1802 stats[ST_F_CTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.c_time, TIME_STATS_SAMPLES));
1803 stats[ST_F_RTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.d_time, TIME_STATS_SAMPLES));
1804 stats[ST_F_TTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.t_time, TIME_STATS_SAMPLES));
1805
1806 if (flags & ST_SHLGNDS) {
1807 switch (addr_to_str(&sv->addr, str, sizeof(str))) {
1808 case AF_INET:
1809 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
Willy Tarreau04276f32017-01-06 17:41:29 +01001810 chunk_appendf(out, "%s:%d", str, sv->svc_port);
William Lallemand74c24fb2016-11-21 17:18:36 +01001811 break;
1812 case AF_INET6:
1813 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
Willy Tarreau04276f32017-01-06 17:41:29 +01001814 chunk_appendf(out, "[%s]:%d", str, sv->svc_port);
William Lallemand74c24fb2016-11-21 17:18:36 +01001815 break;
1816 case AF_UNIX:
1817 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, "unix");
1818 break;
1819 case -1:
1820 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1821 chunk_strcat(out, strerror(errno));
1822 break;
1823 default: /* address family not supported */
1824 break;
1825 }
1826
1827 if (sv->cookie)
1828 stats[ST_F_COOKIE] = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, sv->cookie);
1829 }
1830
1831 return 1;
1832}
1833
1834/* Dumps a line for server <sv> and proxy <px> to the trash and uses the state
1835 * from stream interface <si>, stats flags <flags>, and server state <state>.
1836 * The caller is responsible for clearing the trash if needed. Returns non-zero
1837 * if it emits anything, zero otherwise.
1838 */
1839static int stats_dump_sv_stats(struct stream_interface *si, struct proxy *px, int flags, struct server *sv)
1840{
1841 struct appctx *appctx = __objt_appctx(si->end);
1842
1843 if (!stats_fill_sv_stats(px, sv, flags, stats, ST_F_TOTAL_FIELDS))
1844 return 0;
1845
1846 return stats_dump_one_line(stats, flags, px, appctx);
1847}
1848
1849/* Fill <stats> with the backend statistics. <stats> is
1850 * preallocated array of length <len>. The length of the array
1851 * must be at least ST_F_TOTAL_FIELDS. If this length is less
1852 * then this value, the function returns 0, otherwise, it
1853 * returns 1. <flags> can take the value ST_SHLGNDS.
1854 */
1855int stats_fill_be_stats(struct proxy *px, int flags, struct field *stats, int len)
1856{
1857 if (len < ST_F_TOTAL_FIELDS)
1858 return 0;
1859
1860 memset(stats, 0, sizeof(*stats) * len);
1861
1862 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1863 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, "BACKEND");
1864 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1865 stats[ST_F_QCUR] = mkf_u32(0, px->nbpend);
1866 stats[ST_F_QMAX] = mkf_u32(FN_MAX, px->be_counters.nbpend_max);
Thierry FOURNIER0ff98a42016-12-19 16:50:42 +01001867 stats[ST_F_SCUR] = mkf_u32(0, px->beconn);
William Lallemand74c24fb2016-11-21 17:18:36 +01001868 stats[ST_F_SMAX] = mkf_u32(FN_MAX, px->be_counters.conn_max);
1869 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, px->fullconn);
1870 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, px->be_counters.cum_conn);
1871 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, px->be_counters.bytes_in);
1872 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, px->be_counters.bytes_out);
1873 stats[ST_F_DREQ] = mkf_u64(FN_COUNTER, px->be_counters.denied_req);
1874 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, px->be_counters.denied_resp);
1875 stats[ST_F_ECON] = mkf_u64(FN_COUNTER, px->be_counters.failed_conns);
1876 stats[ST_F_ERESP] = mkf_u64(FN_COUNTER, px->be_counters.failed_resp);
1877 stats[ST_F_WRETR] = mkf_u64(FN_COUNTER, px->be_counters.retries);
1878 stats[ST_F_WREDIS] = mkf_u64(FN_COUNTER, px->be_counters.redispatches);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001879 stats[ST_F_WREW] = mkf_u64(FN_COUNTER, px->be_counters.failed_rewrites);
Willy Tarreauf1573842018-12-14 11:35:36 +01001880 stats[ST_F_CONNECT] = mkf_u64(FN_COUNTER, px->be_counters.connect);
1881 stats[ST_F_REUSE] = mkf_u64(FN_COUNTER, px->be_counters.reuse);
William Lallemand74c24fb2016-11-21 17:18:36 +01001882 stats[ST_F_STATUS] = mkf_str(FO_STATUS, (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" : "DOWN");
1883 stats[ST_F_WEIGHT] = mkf_u32(FN_AVG, (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv);
1884 stats[ST_F_ACT] = mkf_u32(0, px->srv_act);
1885 stats[ST_F_BCK] = mkf_u32(0, px->srv_bck);
1886 stats[ST_F_CHKDOWN] = mkf_u64(FN_COUNTER, px->down_trans);
1887 stats[ST_F_LASTCHG] = mkf_u32(FN_AGE, now.tv_sec - px->last_change);
1888 if (px->srv)
1889 stats[ST_F_DOWNTIME] = mkf_u32(FN_COUNTER, be_downtime(px));
1890
1891 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
1892 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1893 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, 0);
1894 stats[ST_F_LBTOT] = mkf_u64(FN_COUNTER, px->be_counters.cum_lbconn);
1895 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_BE);
1896 stats[ST_F_RATE] = mkf_u32(0, read_freq_ctr(&px->be_sess_per_sec));
1897 stats[ST_F_RATE_MAX] = mkf_u32(0, px->be_counters.sps_max);
1898
1899 if (flags & ST_SHLGNDS) {
1900 if (px->cookie_name)
1901 stats[ST_F_COOKIE] = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, px->cookie_name);
1902 stats[ST_F_ALGO] = mkf_str(FO_CONFIG|FS_SERVICE, backend_lb_algo_str(px->lbprm.algo & BE_LB_ALGO));
1903 }
1904
1905 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
1906 if (px->mode == PR_MODE_HTTP) {
1907 stats[ST_F_REQ_TOT] = mkf_u64(FN_COUNTER, px->be_counters.p.http.cum_req);
1908 stats[ST_F_HRSP_1XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[1]);
1909 stats[ST_F_HRSP_2XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[2]);
1910 stats[ST_F_HRSP_3XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[3]);
1911 stats[ST_F_HRSP_4XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[4]);
1912 stats[ST_F_HRSP_5XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[5]);
1913 stats[ST_F_HRSP_OTHER] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[0]);
Willy Tarreaua1214a52018-12-14 14:00:25 +01001914 stats[ST_F_CACHE_LOOKUPS] = mkf_u64(FN_COUNTER, px->be_counters.p.http.cache_lookups);
1915 stats[ST_F_CACHE_HITS] = mkf_u64(FN_COUNTER, px->be_counters.p.http.cache_hits);
William Lallemand74c24fb2016-11-21 17:18:36 +01001916 }
1917
1918 stats[ST_F_CLI_ABRT] = mkf_u64(FN_COUNTER, px->be_counters.cli_aborts);
1919 stats[ST_F_SRV_ABRT] = mkf_u64(FN_COUNTER, px->be_counters.srv_aborts);
1920
1921 /* compression: in, out, bypassed, responses */
1922 stats[ST_F_COMP_IN] = mkf_u64(FN_COUNTER, px->be_counters.comp_in);
1923 stats[ST_F_COMP_OUT] = mkf_u64(FN_COUNTER, px->be_counters.comp_out);
1924 stats[ST_F_COMP_BYP] = mkf_u64(FN_COUNTER, px->be_counters.comp_byp);
1925 stats[ST_F_COMP_RSP] = mkf_u64(FN_COUNTER, px->be_counters.p.http.comp_rsp);
1926 stats[ST_F_LASTSESS] = mkf_s32(FN_AGE, be_lastsession(px));
1927
1928 stats[ST_F_QTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.q_time, TIME_STATS_SAMPLES));
1929 stats[ST_F_CTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.c_time, TIME_STATS_SAMPLES));
1930 stats[ST_F_RTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.d_time, TIME_STATS_SAMPLES));
1931 stats[ST_F_TTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.t_time, TIME_STATS_SAMPLES));
1932
1933 return 1;
1934}
1935
1936/* Dumps a line for backend <px> to the trash for and uses the state from stream
1937 * interface <si> and stats flags <flags>. The caller is responsible for clearing
1938 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
1939 */
1940static int stats_dump_be_stats(struct stream_interface *si, struct proxy *px, int flags)
1941{
1942 struct appctx *appctx = __objt_appctx(si->end);
1943
1944 if (!(px->cap & PR_CAP_BE))
1945 return 0;
1946
1947 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_BE)))
1948 return 0;
1949
1950 if (!stats_fill_be_stats(px, flags, stats, ST_F_TOTAL_FIELDS))
1951 return 0;
1952
1953 return stats_dump_one_line(stats, flags, px, appctx);
1954}
1955
1956/* Dumps the HTML table header for proxy <px> to the trash for and uses the state from
1957 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
1958 * for clearing the trash if needed.
1959 */
1960static void stats_dump_html_px_hdr(struct stream_interface *si, struct proxy *px, struct uri_auth *uri)
1961{
1962 struct appctx *appctx = __objt_appctx(si->end);
1963 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
1964
1965 if (px->cap & PR_CAP_BE && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
1966 /* A form to enable/disable this proxy servers */
1967
1968 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
1969 scope_txt[0] = 0;
1970 if (appctx->ctx.stats.scope_len) {
Christopher Fauleted7a0662019-01-14 11:07:34 +01001971 const char *scope_ptr = stats_scope_ptr(appctx, si);
1972
William Lallemand74c24fb2016-11-21 17:18:36 +01001973 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Christopher Fauleted7a0662019-01-14 11:07:34 +01001974 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), scope_ptr, appctx->ctx.stats.scope_len);
William Lallemand74c24fb2016-11-21 17:18:36 +01001975 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
1976 }
1977
1978 chunk_appendf(&trash,
1979 "<form method=\"post\">");
1980 }
1981
1982 /* print a new table */
1983 chunk_appendf(&trash,
1984 "<table class=\"tbl\" width=\"100%%\">\n"
1985 "<tr class=\"titre\">"
1986 "<th class=\"pxname\" width=\"10%%\">");
1987
1988 chunk_appendf(&trash,
1989 "<a name=\"%s\"></a>%s"
1990 "<a class=px href=\"#%s\">%s</a>",
1991 px->id,
1992 (uri->flags & ST_SHLGNDS) ? "<u>":"",
1993 px->id, px->id);
1994
1995 if (uri->flags & ST_SHLGNDS) {
1996 /* cap, mode, id */
1997 chunk_appendf(&trash, "<div class=tips>cap: %s, mode: %s, id: %d",
1998 proxy_cap_str(px->cap), proxy_mode_str(px->mode),
1999 px->uuid);
2000 chunk_appendf(&trash, "</div>");
2001 }
2002
2003 chunk_appendf(&trash,
2004 "%s</th>"
2005 "<th class=\"%s\" width=\"90%%\">%s</th>"
2006 "</tr>\n"
2007 "</table>\n"
2008 "<table class=\"tbl\" width=\"100%%\">\n"
2009 "<tr class=\"titre\">",
2010 (uri->flags & ST_SHLGNDS) ? "</u>":"",
2011 px->desc ? "desc" : "empty", px->desc ? px->desc : "");
2012
2013 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
2014 /* Column heading for Enable or Disable server */
David Harrigand3db35a2016-12-30 12:12:49 +00002015 chunk_appendf(&trash,
2016 "<th rowspan=2 width=1><input type=\"checkbox\" \
2017 onclick=\"for(c in document.getElementsByClassName('%s-checkbox')) \
2018 document.getElementsByClassName('%s-checkbox').item(c).checked = this.checked\"></th>",
2019 px->id,
2020 px->id);
William Lallemand74c24fb2016-11-21 17:18:36 +01002021 }
2022
2023 chunk_appendf(&trash,
2024 "<th rowspan=2></th>"
2025 "<th colspan=3>Queue</th>"
2026 "<th colspan=3>Session rate</th><th colspan=6>Sessions</th>"
2027 "<th colspan=2>Bytes</th><th colspan=2>Denied</th>"
2028 "<th colspan=3>Errors</th><th colspan=2>Warnings</th>"
2029 "<th colspan=9>Server</th>"
2030 "</tr>\n"
2031 "<tr class=\"titre\">"
2032 "<th>Cur</th><th>Max</th><th>Limit</th>"
2033 "<th>Cur</th><th>Max</th><th>Limit</th><th>Cur</th><th>Max</th>"
2034 "<th>Limit</th><th>Total</th><th>LbTot</th><th>Last</th><th>In</th><th>Out</th>"
2035 "<th>Req</th><th>Resp</th><th>Req</th><th>Conn</th>"
2036 "<th>Resp</th><th>Retr</th><th>Redis</th>"
2037 "<th>Status</th><th>LastChk</th><th>Wght</th><th>Act</th>"
2038 "<th>Bck</th><th>Chk</th><th>Dwn</th><th>Dwntme</th>"
2039 "<th>Thrtle</th>\n"
2040 "</tr>");
2041}
2042
2043/* Dumps the HTML table trailer for proxy <px> to the trash for and uses the state from
2044 * stream interface <si>. The caller is responsible for clearing the trash if needed.
2045 */
2046static void stats_dump_html_px_end(struct stream_interface *si, struct proxy *px)
2047{
2048 struct appctx *appctx = __objt_appctx(si->end);
2049 chunk_appendf(&trash, "</table>");
2050
2051 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
2052 /* close the form used to enable/disable this proxy servers */
2053 chunk_appendf(&trash,
2054 "Choose the action to perform on the checked servers : "
2055 "<select name=action>"
2056 "<option value=\"\"></option>"
2057 "<option value=\"ready\">Set state to READY</option>"
2058 "<option value=\"drain\">Set state to DRAIN</option>"
2059 "<option value=\"maint\">Set state to MAINT</option>"
2060 "<option value=\"dhlth\">Health: disable checks</option>"
2061 "<option value=\"ehlth\">Health: enable checks</option>"
2062 "<option value=\"hrunn\">Health: force UP</option>"
2063 "<option value=\"hnolb\">Health: force NOLB</option>"
2064 "<option value=\"hdown\">Health: force DOWN</option>"
2065 "<option value=\"dagent\">Agent: disable checks</option>"
2066 "<option value=\"eagent\">Agent: enable checks</option>"
2067 "<option value=\"arunn\">Agent: force UP</option>"
2068 "<option value=\"adown\">Agent: force DOWN</option>"
2069 "<option value=\"shutdown\">Kill Sessions</option>"
2070 "</select>"
2071 "<input type=\"hidden\" name=\"b\" value=\"#%d\">"
2072 "&nbsp;<input type=\"submit\" value=\"Apply\">"
2073 "</form>",
2074 px->uuid);
2075 }
2076
2077 chunk_appendf(&trash, "<p>\n");
2078}
2079
2080/*
2081 * Dumps statistics for a proxy. The output is sent to the stream interface's
2082 * input buffer. Returns 0 if it had to stop dumping data because of lack of
2083 * buffer space, or non-zero if everything completed. This function is used
2084 * both by the CLI and the HTTP entry points, and is able to dump the output
2085 * in HTML or CSV formats. If the later, <uri> must be NULL.
2086 */
Christopher Fauletef779222018-10-31 08:47:01 +01002087int stats_dump_proxy_to_buffer(struct stream_interface *si, struct htx *htx,
2088 struct proxy *px, struct uri_auth *uri)
William Lallemand74c24fb2016-11-21 17:18:36 +01002089{
2090 struct appctx *appctx = __objt_appctx(si->end);
2091 struct stream *s = si_strm(si);
2092 struct channel *rep = si_ic(si);
2093 struct server *sv, *svs; /* server and server-state, server-state=server or server->track */
2094 struct listener *l;
2095 unsigned int flags;
2096
2097 if (uri)
2098 flags = uri->flags;
William Lallemand07a62f72017-05-24 00:57:40 +02002099 else if ((strm_li(s)->bind_conf->level & ACCESS_LVL_MASK) >= ACCESS_LVL_OPER)
William Lallemand74c24fb2016-11-21 17:18:36 +01002100 flags = ST_SHLGNDS | ST_SHNODE | ST_SHDESC;
2101 else
2102 flags = ST_SHNODE | ST_SHDESC;
2103
2104 chunk_reset(&trash);
2105
2106 switch (appctx->ctx.stats.px_st) {
2107 case STAT_PX_ST_INIT:
2108 /* we are on a new proxy */
2109 if (uri && uri->scope) {
2110 /* we have a limited scope, we have to check the proxy name */
2111 struct stat_scope *scope;
2112 int len;
2113
2114 len = strlen(px->id);
2115 scope = uri->scope;
2116
2117 while (scope) {
2118 /* match exact proxy name */
2119 if (scope->px_len == len && !memcmp(px->id, scope->px_id, len))
2120 break;
2121
2122 /* match '.' which means 'self' proxy */
2123 if (!strcmp(scope->px_id, ".") && px == s->be)
2124 break;
2125 scope = scope->next;
2126 }
2127
2128 /* proxy name not found : don't dump anything */
2129 if (scope == NULL)
2130 return 1;
2131 }
2132
2133 /* if the user has requested a limited output and the proxy
2134 * name does not match, skip it.
2135 */
Christopher Fauleted7a0662019-01-14 11:07:34 +01002136 if (appctx->ctx.stats.scope_len) {
2137 const char *scope_ptr = stats_scope_ptr(appctx, si);
2138
2139 if (strnistr(px->id, strlen(px->id), scope_ptr, appctx->ctx.stats.scope_len) == NULL)
2140 return 1;
2141 }
William Lallemand74c24fb2016-11-21 17:18:36 +01002142
2143 if ((appctx->ctx.stats.flags & STAT_BOUND) &&
2144 (appctx->ctx.stats.iid != -1) &&
2145 (px->uuid != appctx->ctx.stats.iid))
2146 return 1;
2147
2148 appctx->ctx.stats.px_st = STAT_PX_ST_TH;
2149 /* fall through */
2150
2151 case STAT_PX_ST_TH:
2152 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
2153 stats_dump_html_px_hdr(si, px, uri);
Christopher Fauletef779222018-10-31 08:47:01 +01002154 if (!stats_putchk(rep, htx, &trash))
2155 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002156 }
2157
2158 appctx->ctx.stats.px_st = STAT_PX_ST_FE;
2159 /* fall through */
2160
2161 case STAT_PX_ST_FE:
2162 /* print the frontend */
2163 if (stats_dump_fe_stats(si, px)) {
Christopher Fauletef779222018-10-31 08:47:01 +01002164 if (!stats_putchk(rep, htx, &trash))
2165 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002166 }
2167
2168 appctx->ctx.stats.l = px->conf.listeners.n;
2169 appctx->ctx.stats.px_st = STAT_PX_ST_LI;
2170 /* fall through */
2171
2172 case STAT_PX_ST_LI:
2173 /* stats.l has been initialized above */
2174 for (; appctx->ctx.stats.l != &px->conf.listeners; appctx->ctx.stats.l = l->by_fe.n) {
Christopher Fauletef779222018-10-31 08:47:01 +01002175 if (htx) {
2176 if (htx_almost_full(htx))
2177 goto full;
2178 }
2179 else {
2180 if (buffer_almost_full(&rep->buf))
2181 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002182 }
2183
2184 l = LIST_ELEM(appctx->ctx.stats.l, struct listener *, by_fe);
2185 if (!l->counters)
2186 continue;
2187
2188 if (appctx->ctx.stats.flags & STAT_BOUND) {
2189 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SO)))
2190 break;
2191
2192 if (appctx->ctx.stats.sid != -1 && l->luid != appctx->ctx.stats.sid)
2193 continue;
2194 }
2195
2196 /* print the frontend */
2197 if (stats_dump_li_stats(si, px, l, flags)) {
Christopher Fauletef779222018-10-31 08:47:01 +01002198 if (!stats_putchk(rep, htx, &trash))
2199 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002200 }
2201 }
2202
2203 appctx->ctx.stats.sv = px->srv; /* may be NULL */
2204 appctx->ctx.stats.px_st = STAT_PX_ST_SV;
2205 /* fall through */
2206
2207 case STAT_PX_ST_SV:
2208 /* stats.sv has been initialized above */
2209 for (; appctx->ctx.stats.sv != NULL; appctx->ctx.stats.sv = sv->next) {
Christopher Fauletef779222018-10-31 08:47:01 +01002210 if (htx) {
2211 if (htx_almost_full(htx))
2212 goto full;
2213 }
2214 else {
2215 if (buffer_almost_full(&rep->buf))
2216 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002217 }
2218
2219 sv = appctx->ctx.stats.sv;
2220
2221 if (appctx->ctx.stats.flags & STAT_BOUND) {
2222 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SV)))
2223 break;
2224
2225 if (appctx->ctx.stats.sid != -1 && sv->puid != appctx->ctx.stats.sid)
2226 continue;
2227 }
2228
2229 svs = sv;
2230 while (svs->track)
2231 svs = svs->track;
2232
2233 /* do not report servers which are DOWN and not changing state */
2234 if ((appctx->ctx.stats.flags & STAT_HIDE_DOWN) &&
Emeric Brun52a91d32017-08-31 14:41:55 +02002235 ((sv->cur_admin & SRV_ADMF_MAINT) || /* server is in maintenance */
2236 (sv->cur_state == SRV_ST_STOPPED && /* server is down */
William Lallemand74c24fb2016-11-21 17:18:36 +01002237 (!((svs->agent.state | svs->check.state) & CHK_ST_ENABLED) ||
2238 ((svs->agent.state & CHK_ST_ENABLED) && !svs->agent.health) ||
2239 ((svs->check.state & CHK_ST_ENABLED) && !svs->check.health))))) {
2240 continue;
2241 }
2242
2243 if (stats_dump_sv_stats(si, px, flags, sv)) {
Christopher Fauletef779222018-10-31 08:47:01 +01002244 if (!stats_putchk(rep, htx, &trash))
2245 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002246 }
2247 } /* for sv */
2248
2249 appctx->ctx.stats.px_st = STAT_PX_ST_BE;
2250 /* fall through */
2251
2252 case STAT_PX_ST_BE:
2253 /* print the backend */
2254 if (stats_dump_be_stats(si, px, flags)) {
Christopher Fauletef779222018-10-31 08:47:01 +01002255 if (!stats_putchk(rep, htx, &trash))
2256 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002257 }
2258
2259 appctx->ctx.stats.px_st = STAT_PX_ST_END;
2260 /* fall through */
2261
2262 case STAT_PX_ST_END:
2263 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
2264 stats_dump_html_px_end(si, px);
Christopher Fauletef779222018-10-31 08:47:01 +01002265 if (!stats_putchk(rep, htx, &trash))
2266 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002267 }
2268
2269 appctx->ctx.stats.px_st = STAT_PX_ST_FIN;
2270 /* fall through */
2271
2272 case STAT_PX_ST_FIN:
2273 return 1;
2274
2275 default:
2276 /* unknown state, we should put an abort() here ! */
2277 return 1;
2278 }
Christopher Fauletef779222018-10-31 08:47:01 +01002279
2280 full:
2281 si_rx_room_blk(si);
2282 return 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01002283}
2284
2285/* Dumps the HTTP stats head block to the trash for and uses the per-uri
2286 * parameters <uri>. The caller is responsible for clearing the trash if needed.
2287 */
2288static void stats_dump_html_head(struct uri_auth *uri)
2289{
2290 /* WARNING! This must fit in the first buffer !!! */
2291 chunk_appendf(&trash,
2292 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n"
2293 "\"http://www.w3.org/TR/html4/loose.dtd\">\n"
2294 "<html><head><title>Statistics Report for " PRODUCT_NAME "%s%s</title>\n"
2295 "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n"
2296 "<style type=\"text/css\"><!--\n"
2297 "body {"
2298 " font-family: arial, helvetica, sans-serif;"
2299 " font-size: 12px;"
2300 " font-weight: normal;"
2301 " color: black;"
2302 " background: white;"
2303 "}\n"
2304 "th,td {"
2305 " font-size: 10px;"
2306 "}\n"
2307 "h1 {"
2308 " font-size: x-large;"
2309 " margin-bottom: 0.5em;"
2310 "}\n"
2311 "h2 {"
2312 " font-family: helvetica, arial;"
2313 " font-size: x-large;"
2314 " font-weight: bold;"
2315 " font-style: italic;"
2316 " color: #6020a0;"
2317 " margin-top: 0em;"
2318 " margin-bottom: 0em;"
2319 "}\n"
2320 "h3 {"
2321 " font-family: helvetica, arial;"
2322 " font-size: 16px;"
2323 " font-weight: bold;"
2324 " color: #b00040;"
2325 " background: #e8e8d0;"
2326 " margin-top: 0em;"
2327 " margin-bottom: 0em;"
2328 "}\n"
2329 "li {"
2330 " margin-top: 0.25em;"
2331 " margin-right: 2em;"
2332 "}\n"
2333 ".hr {margin-top: 0.25em;"
2334 " border-color: black;"
2335 " border-bottom-style: solid;"
2336 "}\n"
2337 ".titre {background: #20D0D0;color: #000000; font-weight: bold; text-align: center;}\n"
2338 ".total {background: #20D0D0;color: #ffff80;}\n"
2339 ".frontend {background: #e8e8d0;}\n"
2340 ".socket {background: #d0d0d0;}\n"
2341 ".backend {background: #e8e8d0;}\n"
2342 ".active_down {background: #ff9090;}\n"
2343 ".active_going_up {background: #ffd020;}\n"
2344 ".active_going_down {background: #ffffa0;}\n"
2345 ".active_up {background: #c0ffc0;}\n"
2346 ".active_nolb {background: #20a0ff;}\n"
2347 ".active_draining {background: #20a0FF;}\n"
2348 ".active_no_check {background: #e0e0e0;}\n"
2349 ".backup_down {background: #ff9090;}\n"
2350 ".backup_going_up {background: #ff80ff;}\n"
2351 ".backup_going_down {background: #c060ff;}\n"
2352 ".backup_up {background: #b0d0ff;}\n"
2353 ".backup_nolb {background: #90b0e0;}\n"
2354 ".backup_draining {background: #cc9900;}\n"
2355 ".backup_no_check {background: #e0e0e0;}\n"
2356 ".maintain {background: #c07820;}\n"
2357 ".rls {letter-spacing: 0.2em; margin-right: 1px;}\n" /* right letter spacing (used for grouping digits) */
2358 "\n"
2359 "a.px:link {color: #ffff40; text-decoration: none;}"
2360 "a.px:visited {color: #ffff40; text-decoration: none;}"
2361 "a.px:hover {color: #ffffff; text-decoration: none;}"
2362 "a.lfsb:link {color: #000000; text-decoration: none;}"
2363 "a.lfsb:visited {color: #000000; text-decoration: none;}"
2364 "a.lfsb:hover {color: #505050; text-decoration: none;}"
2365 "\n"
2366 "table.tbl { border-collapse: collapse; border-style: none;}\n"
2367 "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"
2368 "table.tbl td.ac { text-align: center;}\n"
2369 "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}\n"
2370 "table.tbl th.pxname { background: #b00040; color: #ffff40; font-weight: bold; border-style: solid solid none solid; padding: 2px 3px; white-space: nowrap;}\n"
2371 "table.tbl th.empty { border-style: none; empty-cells: hide; background: white;}\n"
2372 "table.tbl th.desc { background: white; border-style: solid solid none solid; text-align: left; padding: 2px 3px;}\n"
2373 "\n"
2374 "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n"
2375 "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n"
2376 "table.lgd td.noborder { border-style: none; padding: 2px; white-space: nowrap;}\n"
2377 "table.det { border-collapse: collapse; border-style: none; }\n"
2378 "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"
2379 "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"
2380 "u {text-decoration:none; border-bottom: 1px dotted black;}\n"
2381 "div.tips {\n"
2382 " display:block;\n"
2383 " visibility:hidden;\n"
2384 " z-index:2147483647;\n"
2385 " position:absolute;\n"
2386 " padding:2px 4px 3px;\n"
2387 " background:#f0f060; color:#000000;\n"
2388 " border:1px solid #7040c0;\n"
2389 " white-space:nowrap;\n"
2390 " font-style:normal;font-size:11px;font-weight:normal;\n"
2391 " -moz-border-radius:3px;-webkit-border-radius:3px;border-radius:3px;\n"
2392 " -moz-box-shadow:gray 2px 2px 3px;-webkit-box-shadow:gray 2px 2px 3px;box-shadow:gray 2px 2px 3px;\n"
2393 "}\n"
2394 "u:hover div.tips {visibility:visible;}\n"
2395 "-->\n"
2396 "</style></head>\n",
2397 (uri->flags & ST_SHNODE) ? " on " : "",
2398 (uri->flags & ST_SHNODE) ? (uri->node ? uri->node : global.node) : ""
2399 );
2400}
2401
2402/* Dumps the HTML stats information block to the trash for and uses the state from
2403 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
2404 * for clearing the trash if needed.
2405 */
2406static void stats_dump_html_info(struct stream_interface *si, struct uri_auth *uri)
2407{
2408 struct appctx *appctx = __objt_appctx(si->end);
2409 unsigned int up = (now.tv_sec - start_date.tv_sec);
2410 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
Christopher Fauleted7a0662019-01-14 11:07:34 +01002411 const char *scope_ptr = stats_scope_ptr(appctx, si);
Willy Tarreau1713c032019-05-23 12:23:55 +02002412 unsigned long long bps = (unsigned long long)read_freq_ctr(&global.out_32bps) * 32;
2413
2414 /* Turn the bytes per second to bits per second and take care of the
2415 * usual ethernet overhead in order to help figure how far we are from
2416 * interface saturation since it's the only case which usually matters.
2417 * For this we count the total size of an Ethernet frame on the wire
2418 * including preamble and IFG (1538) for the largest TCP segment it
2419 * transports (1448 with TCP timestamps). This is not valid for smaller
2420 * packets (under-estimated), but it gives a reasonably accurate
2421 * estimation of how far we are from uplink saturation.
2422 */
2423 bps = bps * 8 * 1538 / 1448;
William Lallemand74c24fb2016-11-21 17:18:36 +01002424
2425 /* WARNING! this has to fit the first packet too.
2426 * We are around 3.5 kB, add adding entries will
2427 * become tricky if we want to support 4kB buffers !
2428 */
2429 chunk_appendf(&trash,
2430 "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">"
2431 PRODUCT_NAME "%s</a></h1>\n"
2432 "<h2>Statistics Report for pid %d%s%s%s%s</h2>\n"
2433 "<hr width=\"100%%\" class=\"hr\">\n"
2434 "<h3>&gt; General process information</h3>\n"
2435 "<table border=0><tr><td align=\"left\" nowrap width=\"1%%\">\n"
Yves Lafon95317282018-02-26 11:10:37 +01002436 "<p><b>pid = </b> %d (process #%d, nbproc = %d, nbthread = %d)<br>\n"
William Lallemand74c24fb2016-11-21 17:18:36 +01002437 "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n"
2438 "<b>system limits:</b> memmax = %s%s; ulimit-n = %d<br>\n"
2439 "<b>maxsock = </b> %d; <b>maxconn = </b> %d; <b>maxpipes = </b> %d<br>\n"
Willy Tarreau1713c032019-05-23 12:23:55 +02002440 "current conns = %d; current pipes = %d/%d; conn rate = %d/sec; bit rate = %.3f %cbps<br>\n"
William Lallemand74c24fb2016-11-21 17:18:36 +01002441 "Running tasks: %d/%d; idle = %d %%<br>\n"
2442 "</td><td align=\"center\" nowrap>\n"
2443 "<table class=\"lgd\"><tr>\n"
2444 "<td class=\"active_up\">&nbsp;</td><td class=\"noborder\">active UP </td>"
2445 "<td class=\"backup_up\">&nbsp;</td><td class=\"noborder\">backup UP </td>"
2446 "</tr><tr>\n"
2447 "<td class=\"active_going_down\"></td><td class=\"noborder\">active UP, going down </td>"
2448 "<td class=\"backup_going_down\"></td><td class=\"noborder\">backup UP, going down </td>"
2449 "</tr><tr>\n"
2450 "<td class=\"active_going_up\"></td><td class=\"noborder\">active DOWN, going up </td>"
2451 "<td class=\"backup_going_up\"></td><td class=\"noborder\">backup DOWN, going up </td>"
2452 "</tr><tr>\n"
2453 "<td class=\"active_down\"></td><td class=\"noborder\">active or backup DOWN &nbsp;</td>"
2454 "<td class=\"active_no_check\"></td><td class=\"noborder\">not checked </td>"
2455 "</tr><tr>\n"
2456 "<td class=\"maintain\"></td><td class=\"noborder\" colspan=\"3\">active or backup DOWN for maintenance (MAINT) &nbsp;</td>"
2457 "</tr><tr>\n"
2458 "<td class=\"active_draining\"></td><td class=\"noborder\" colspan=\"3\">active or backup SOFT STOPPED for maintenance &nbsp;</td>"
2459 "</tr></table>\n"
2460 "Note: \"NOLB\"/\"DRAIN\" = UP with load-balancing disabled."
2461 "</td>"
2462 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
2463 "<b>Display option:</b><ul style=\"margin-top: 0.25em;\">"
2464 "",
Willy Tarreau909b9d82019-01-04 18:20:32 +01002465 (uri->flags & ST_HIDEVER) ? "" : (stats_version_string),
William Lallemand74c24fb2016-11-21 17:18:36 +01002466 pid, (uri->flags & ST_SHNODE) ? " on " : "",
2467 (uri->flags & ST_SHNODE) ? (uri->node ? uri->node : global.node) : "",
2468 (uri->flags & ST_SHDESC) ? ": " : "",
2469 (uri->flags & ST_SHDESC) ? (uri->desc ? uri->desc : global.desc) : "",
Yves Lafon95317282018-02-26 11:10:37 +01002470 pid, relative_pid, global.nbproc, global.nbthread,
William Lallemand74c24fb2016-11-21 17:18:36 +01002471 up / 86400, (up % 86400) / 3600,
2472 (up % 3600) / 60, (up % 60),
2473 global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited",
2474 global.rlimit_memmax ? " MB" : "",
2475 global.rlimit_nofile,
2476 global.maxsock, global.maxconn, global.maxpipes,
2477 actconn, pipes_used, pipes_used+pipes_free, read_freq_ctr(&global.conn_per_sec),
Willy Tarreau1713c032019-05-23 12:23:55 +02002478 bps >= 1000000000UL ? (bps / 1000000000.0) : bps >= 1000000UL ? (bps / 1000000.0) : (bps / 1000.0),
2479 bps >= 1000000000UL ? 'G' : bps >= 1000000UL ? 'M' : 'k',
Willy Tarreau81036f22019-05-20 19:24:50 +02002480 tasks_run_queue_cur, nb_tasks_cur, ti->idle_pct
William Lallemand74c24fb2016-11-21 17:18:36 +01002481 );
2482
2483 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Christopher Fauleted7a0662019-01-14 11:07:34 +01002484 memcpy(scope_txt, scope_ptr, appctx->ctx.stats.scope_len);
William Lallemand74c24fb2016-11-21 17:18:36 +01002485 scope_txt[appctx->ctx.stats.scope_len] = '\0';
2486
2487 chunk_appendf(&trash,
2488 "<li><form method=\"GET\">Scope : <input value=\"%s\" name=\"" STAT_SCOPE_INPUT_NAME "\" size=\"8\" maxlength=\"%d\" tabindex=\"1\"/></form>\n",
2489 (appctx->ctx.stats.scope_len > 0) ? scope_txt : "",
2490 STAT_SCOPE_TXT_MAXLEN);
2491
2492 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
2493 scope_txt[0] = 0;
2494 if (appctx->ctx.stats.scope_len) {
2495 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Christopher Fauleted7a0662019-01-14 11:07:34 +01002496 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), scope_ptr, appctx->ctx.stats.scope_len);
William Lallemand74c24fb2016-11-21 17:18:36 +01002497 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
2498 }
2499
2500 if (appctx->ctx.stats.flags & STAT_HIDE_DOWN)
2501 chunk_appendf(&trash,
2502 "<li><a href=\"%s%s%s%s\">Show all servers</a><br>\n",
2503 uri->uri_prefix,
2504 "",
2505 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2506 scope_txt);
2507 else
2508 chunk_appendf(&trash,
2509 "<li><a href=\"%s%s%s%s\">Hide 'DOWN' servers</a><br>\n",
2510 uri->uri_prefix,
2511 ";up",
2512 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2513 scope_txt);
2514
2515 if (uri->refresh > 0) {
2516 if (appctx->ctx.stats.flags & STAT_NO_REFRESH)
2517 chunk_appendf(&trash,
2518 "<li><a href=\"%s%s%s%s\">Enable refresh</a><br>\n",
2519 uri->uri_prefix,
2520 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2521 "",
2522 scope_txt);
2523 else
2524 chunk_appendf(&trash,
2525 "<li><a href=\"%s%s%s%s\">Disable refresh</a><br>\n",
2526 uri->uri_prefix,
2527 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2528 ";norefresh",
2529 scope_txt);
2530 }
2531
2532 chunk_appendf(&trash,
2533 "<li><a href=\"%s%s%s%s\">Refresh now</a><br>\n",
2534 uri->uri_prefix,
2535 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2536 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2537 scope_txt);
2538
2539 chunk_appendf(&trash,
2540 "<li><a href=\"%s;csv%s%s\">CSV export</a><br>\n",
2541 uri->uri_prefix,
2542 (uri->refresh > 0) ? ";norefresh" : "",
2543 scope_txt);
2544
2545 chunk_appendf(&trash,
2546 "</ul></td>"
2547 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
2548 "<b>External resources:</b><ul style=\"margin-top: 0.25em;\">\n"
2549 "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>\n"
2550 "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>\n"
2551 "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>\n"
2552 "</ul>"
2553 "</td>"
2554 "</tr></table>\n"
2555 ""
2556 );
2557
2558 if (appctx->ctx.stats.st_code) {
2559 switch (appctx->ctx.stats.st_code) {
2560 case STAT_STATUS_DONE:
2561 chunk_appendf(&trash,
2562 "<p><div class=active_up>"
2563 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2564 "Action processed successfully."
2565 "</div>\n", uri->uri_prefix,
2566 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2567 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2568 scope_txt);
2569 break;
2570 case STAT_STATUS_NONE:
2571 chunk_appendf(&trash,
2572 "<p><div class=active_going_down>"
2573 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2574 "Nothing has changed."
2575 "</div>\n", uri->uri_prefix,
2576 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2577 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2578 scope_txt);
2579 break;
2580 case STAT_STATUS_PART:
2581 chunk_appendf(&trash,
2582 "<p><div class=active_going_down>"
2583 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2584 "Action partially processed.<br>"
2585 "Some server names are probably unknown or ambiguous (duplicated names in the backend)."
2586 "</div>\n", uri->uri_prefix,
2587 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2588 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2589 scope_txt);
2590 break;
2591 case STAT_STATUS_ERRP:
2592 chunk_appendf(&trash,
2593 "<p><div class=active_down>"
2594 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2595 "Action not processed because of invalid parameters."
2596 "<ul>"
2597 "<li>The action is maybe unknown.</li>"
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01002598 "<li>Invalid key parameter (empty or too long).</li>"
William Lallemand74c24fb2016-11-21 17:18:36 +01002599 "<li>The backend name is probably unknown or ambiguous (duplicated names).</li>"
2600 "<li>Some server names are probably unknown or ambiguous (duplicated names in the backend).</li>"
2601 "</ul>"
2602 "</div>\n", uri->uri_prefix,
2603 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2604 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2605 scope_txt);
2606 break;
2607 case STAT_STATUS_EXCD:
2608 chunk_appendf(&trash,
2609 "<p><div class=active_down>"
2610 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2611 "<b>Action not processed : the buffer couldn't store all the data.<br>"
2612 "You should retry with less servers at a time.</b>"
2613 "</div>\n", uri->uri_prefix,
2614 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2615 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2616 scope_txt);
2617 break;
2618 case STAT_STATUS_DENY:
2619 chunk_appendf(&trash,
2620 "<p><div class=active_down>"
2621 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2622 "<b>Action denied.</b>"
2623 "</div>\n", uri->uri_prefix,
2624 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2625 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2626 scope_txt);
2627 break;
Christopher Faulet3c2ecf72019-02-27 16:41:27 +01002628 case STAT_STATUS_IVAL:
2629 chunk_appendf(&trash,
2630 "<p><div class=active_down>"
2631 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2632 "<b>Invalid requests (unsupported method or chunked encoded request).</b>"
2633 "</div>\n", uri->uri_prefix,
2634 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2635 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2636 scope_txt);
2637 break;
William Lallemand74c24fb2016-11-21 17:18:36 +01002638 default:
2639 chunk_appendf(&trash,
2640 "<p><div class=active_no_check>"
2641 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2642 "Unexpected result."
2643 "</div>\n", uri->uri_prefix,
2644 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2645 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2646 scope_txt);
2647 }
2648 chunk_appendf(&trash, "<p>\n");
2649 }
2650}
2651
2652/* Dumps the HTML stats trailer block to the trash. The caller is responsible
2653 * for clearing the trash if needed.
2654 */
2655static void stats_dump_html_end()
2656{
2657 chunk_appendf(&trash, "</body></html>\n");
2658}
2659
Simon Horman05ee2132017-01-04 09:37:25 +01002660/* Dumps the stats JSON header to the trash buffer which. The caller is responsible
2661 * for clearing it if needed.
2662 */
2663static void stats_dump_json_header()
2664{
2665 chunk_strcat(&trash, "[");
2666}
2667
2668
2669/* Dumps the JSON stats trailer block to the trash. The caller is responsible
2670 * for clearing the trash if needed.
2671 */
2672static void stats_dump_json_end()
2673{
2674 chunk_strcat(&trash, "]");
2675}
2676
William Lallemand74c24fb2016-11-21 17:18:36 +01002677/* This function dumps statistics onto the stream interface's read buffer in
2678 * either CSV or HTML format. <uri> contains some HTML-specific parameters that
2679 * are ignored for CSV format (hence <uri> may be NULL there). It returns 0 if
2680 * it had to stop writing data and an I/O is needed, 1 if the dump is finished
2681 * and the stream must be closed, or -1 in case of any error. This function is
2682 * used by both the CLI and the HTTP handlers.
2683 */
Christopher Fauletef779222018-10-31 08:47:01 +01002684static int stats_dump_stat_to_buffer(struct stream_interface *si, struct htx *htx,
2685 struct uri_auth *uri)
William Lallemand74c24fb2016-11-21 17:18:36 +01002686{
2687 struct appctx *appctx = __objt_appctx(si->end);
2688 struct channel *rep = si_ic(si);
2689 struct proxy *px;
2690
2691 chunk_reset(&trash);
2692
2693 switch (appctx->st2) {
2694 case STAT_ST_INIT:
2695 appctx->st2 = STAT_ST_HEAD; /* let's start producing data */
2696 /* fall through */
2697
2698 case STAT_ST_HEAD:
2699 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
2700 stats_dump_html_head(uri);
Simon Horman05ee2132017-01-04 09:37:25 +01002701 else if (appctx->ctx.stats.flags & STAT_FMT_JSON)
Willy Tarreau9d7fb632017-04-11 07:53:04 +02002702 stats_dump_json_header();
William Lallemand74c24fb2016-11-21 17:18:36 +01002703 else if (!(appctx->ctx.stats.flags & STAT_FMT_TYPED))
2704 stats_dump_csv_header();
2705
Christopher Fauletef779222018-10-31 08:47:01 +01002706 if (!stats_putchk(rep, htx, &trash))
2707 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002708
2709 appctx->st2 = STAT_ST_INFO;
2710 /* fall through */
2711
2712 case STAT_ST_INFO:
2713 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
2714 stats_dump_html_info(si, uri);
Christopher Fauletef779222018-10-31 08:47:01 +01002715 if (!stats_putchk(rep, htx, &trash))
2716 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002717 }
2718
Olivier Houchardfbc74e82017-11-24 16:54:05 +01002719 appctx->ctx.stats.px = proxies_list;
William Lallemand74c24fb2016-11-21 17:18:36 +01002720 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
2721 appctx->st2 = STAT_ST_LIST;
2722 /* fall through */
2723
2724 case STAT_ST_LIST:
2725 /* dump proxies */
2726 while (appctx->ctx.stats.px) {
Christopher Fauletef779222018-10-31 08:47:01 +01002727 if (htx) {
2728 if (htx_almost_full(htx))
2729 goto full;
2730 }
2731 else {
2732 if (buffer_almost_full(&rep->buf))
2733 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002734 }
2735
2736 px = appctx->ctx.stats.px;
2737 /* skip the disabled proxies, global frontend and non-networked ones */
2738 if (px->state != PR_STSTOPPED && px->uuid > 0 && (px->cap & (PR_CAP_FE | PR_CAP_BE)))
Christopher Fauletef779222018-10-31 08:47:01 +01002739 if (stats_dump_proxy_to_buffer(si, htx, px, uri) == 0)
William Lallemand74c24fb2016-11-21 17:18:36 +01002740 return 0;
2741
2742 appctx->ctx.stats.px = px->next;
2743 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
2744 }
2745 /* here, we just have reached the last proxy */
2746
2747 appctx->st2 = STAT_ST_END;
2748 /* fall through */
2749
2750 case STAT_ST_END:
Simon Horman05ee2132017-01-04 09:37:25 +01002751 if (appctx->ctx.stats.flags & (STAT_FMT_HTML|STAT_FMT_JSON)) {
2752 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
2753 stats_dump_html_end();
2754 else
2755 stats_dump_json_end();
Christopher Fauletef779222018-10-31 08:47:01 +01002756 if (!stats_putchk(rep, htx, &trash))
2757 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002758 }
2759
2760 appctx->st2 = STAT_ST_FIN;
2761 /* fall through */
2762
2763 case STAT_ST_FIN:
2764 return 1;
2765
2766 default:
2767 /* unknown state ! */
2768 appctx->st2 = STAT_ST_FIN;
2769 return -1;
2770 }
Christopher Fauletef779222018-10-31 08:47:01 +01002771
2772 full:
2773 si_rx_room_blk(si);
2774 return 0;
2775
William Lallemand74c24fb2016-11-21 17:18:36 +01002776}
2777
2778/* We reached the stats page through a POST request. The appctx is
2779 * expected to have already been allocated by the caller.
2780 * Parse the posted data and enable/disable servers if necessary.
2781 * Returns 1 if request was parsed or zero if it needs more data.
2782 */
2783static int stats_process_http_post(struct stream_interface *si)
2784{
2785 struct stream *s = si_strm(si);
2786 struct appctx *appctx = objt_appctx(si->end);
2787
2788 struct proxy *px = NULL;
2789 struct server *sv = NULL;
2790
2791 char key[LINESIZE];
2792 int action = ST_ADM_ACTION_NONE;
2793 int reprocess = 0;
2794
2795 int total_servers = 0;
2796 int altered_servers = 0;
2797
2798 char *first_param, *cur_param, *next_param, *end_params;
2799 char *st_cur_param = NULL;
2800 char *st_next_param = NULL;
2801
Christopher Fauleta3618372018-12-13 21:59:56 +01002802 struct buffer *temp = get_trash_chunk();
William Lallemand74c24fb2016-11-21 17:18:36 +01002803
Christopher Fauleta3618372018-12-13 21:59:56 +01002804 if (IS_HTX_STRM(s)) {
2805 struct htx *htx = htxbuf(&s->req.buf);
2806 struct htx_blk *blk;
Christopher Fauleta3618372018-12-13 21:59:56 +01002807
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01002808 /* we need more data */
2809 if (s->txn->req.msg_state < HTTP_MSG_DONE) {
2810 /* check if we can receive more */
2811 if (htx_free_data_space(htx) <= global.tune.maxrewrite) {
2812 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
2813 goto out;
Christopher Fauleta3618372018-12-13 21:59:56 +01002814 }
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01002815 goto wait;
Christopher Fauleta3618372018-12-13 21:59:56 +01002816 }
William Lallemand74c24fb2016-11-21 17:18:36 +01002817
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01002818 /* The request was fully received. Copy data */
Christopher Faulet8fa60e42019-05-23 11:04:05 +02002819 blk = htx_get_head_blk(htx);
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01002820 while (blk) {
Christopher Fauleta3618372018-12-13 21:59:56 +01002821 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Fauleta3618372018-12-13 21:59:56 +01002822
Christopher Faulet54b5e212019-06-04 10:08:28 +02002823 if (type == HTX_BLK_EOM || type == HTX_BLK_TLR || type == HTX_BLK_EOT)
Christopher Fauleta3618372018-12-13 21:59:56 +01002824 break;
2825 if (type == HTX_BLK_DATA) {
2826 struct ist v = htx_get_blk_value(htx, blk);
2827
2828 if (!chunk_memcat(temp, v.ptr, v.len)) {
2829 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
2830 goto out;
2831 }
2832 }
Christopher Fauleted7a0662019-01-14 11:07:34 +01002833 blk = htx_get_next_blk(htx, blk);
Christopher Fauleta3618372018-12-13 21:59:56 +01002834 }
2835 }
2836 else {
2837 int reql;
2838
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01002839 /* we need more data */
2840 if (s->txn->req.msg_state < HTTP_MSG_DONE) {
2841 /* check if we can receive more */
2842 if (c_room(&s->req) <= global.tune.maxrewrite) {
2843 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
2844 goto out;
2845 }
2846 goto wait;
Christopher Fauleta3618372018-12-13 21:59:56 +01002847 }
Christopher Fauleta3618372018-12-13 21:59:56 +01002848 reql = co_getblk(si_oc(si), temp->area, s->txn->req.body_len,
2849 s->txn->req.eoh + 2);
2850 if (reql <= 0) {
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01002851 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
2852 goto out;
Christopher Fauleta3618372018-12-13 21:59:56 +01002853 }
2854 temp->data = reql;
William Lallemand74c24fb2016-11-21 17:18:36 +01002855 }
2856
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002857 first_param = temp->area;
Christopher Fauleta3618372018-12-13 21:59:56 +01002858 end_params = temp->area + temp->data;
William Lallemand74c24fb2016-11-21 17:18:36 +01002859 cur_param = next_param = end_params;
2860 *end_params = '\0';
2861
2862 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
2863
2864 /*
2865 * Parse the parameters in reverse order to only store the last value.
2866 * From the html form, the backend and the action are at the end.
2867 */
2868 while (cur_param > first_param) {
2869 char *value;
2870 int poffset, plen;
2871
2872 cur_param--;
2873
2874 if ((*cur_param == '&') || (cur_param == first_param)) {
2875 reprocess_servers:
2876 /* Parse the key */
2877 poffset = (cur_param != first_param ? 1 : 0);
2878 plen = next_param - cur_param + (cur_param == first_param ? 1 : 0);
2879 if ((plen > 0) && (plen <= sizeof(key))) {
2880 strncpy(key, cur_param + poffset, plen);
2881 key[plen - 1] = '\0';
2882 } else {
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01002883 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
William Lallemand74c24fb2016-11-21 17:18:36 +01002884 goto out;
2885 }
2886
2887 /* Parse the value */
2888 value = key;
2889 while (*value != '\0' && *value != '=') {
2890 value++;
2891 }
2892 if (*value == '=') {
2893 /* Ok, a value is found, we can mark the end of the key */
2894 *value++ = '\0';
2895 }
2896 if (url_decode(key) < 0 || url_decode(value) < 0)
2897 break;
2898
2899 /* Now we can check the key to see what to do */
2900 if (!px && (strcmp(key, "b") == 0)) {
2901 if ((px = proxy_be_by_name(value)) == NULL) {
2902 /* the backend name is unknown or ambiguous (duplicate names) */
2903 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
2904 goto out;
2905 }
2906 }
2907 else if (!action && (strcmp(key, "action") == 0)) {
2908 if (strcmp(value, "ready") == 0) {
2909 action = ST_ADM_ACTION_READY;
2910 }
2911 else if (strcmp(value, "drain") == 0) {
2912 action = ST_ADM_ACTION_DRAIN;
2913 }
2914 else if (strcmp(value, "maint") == 0) {
2915 action = ST_ADM_ACTION_MAINT;
2916 }
2917 else if (strcmp(value, "shutdown") == 0) {
2918 action = ST_ADM_ACTION_SHUTDOWN;
2919 }
2920 else if (strcmp(value, "dhlth") == 0) {
2921 action = ST_ADM_ACTION_DHLTH;
2922 }
2923 else if (strcmp(value, "ehlth") == 0) {
2924 action = ST_ADM_ACTION_EHLTH;
2925 }
2926 else if (strcmp(value, "hrunn") == 0) {
2927 action = ST_ADM_ACTION_HRUNN;
2928 }
2929 else if (strcmp(value, "hnolb") == 0) {
2930 action = ST_ADM_ACTION_HNOLB;
2931 }
2932 else if (strcmp(value, "hdown") == 0) {
2933 action = ST_ADM_ACTION_HDOWN;
2934 }
2935 else if (strcmp(value, "dagent") == 0) {
2936 action = ST_ADM_ACTION_DAGENT;
2937 }
2938 else if (strcmp(value, "eagent") == 0) {
2939 action = ST_ADM_ACTION_EAGENT;
2940 }
2941 else if (strcmp(value, "arunn") == 0) {
2942 action = ST_ADM_ACTION_ARUNN;
2943 }
2944 else if (strcmp(value, "adown") == 0) {
2945 action = ST_ADM_ACTION_ADOWN;
2946 }
2947 /* else these are the old supported methods */
2948 else if (strcmp(value, "disable") == 0) {
2949 action = ST_ADM_ACTION_DISABLE;
2950 }
2951 else if (strcmp(value, "enable") == 0) {
2952 action = ST_ADM_ACTION_ENABLE;
2953 }
2954 else if (strcmp(value, "stop") == 0) {
2955 action = ST_ADM_ACTION_STOP;
2956 }
2957 else if (strcmp(value, "start") == 0) {
2958 action = ST_ADM_ACTION_START;
2959 }
2960 else {
2961 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
2962 goto out;
2963 }
2964 }
2965 else if (strcmp(key, "s") == 0) {
2966 if (!(px && action)) {
2967 /*
2968 * Indicates that we'll need to reprocess the parameters
2969 * as soon as backend and action are known
2970 */
2971 if (!reprocess) {
2972 st_cur_param = cur_param;
2973 st_next_param = next_param;
2974 }
2975 reprocess = 1;
2976 }
2977 else if ((sv = findserver(px, value)) != NULL) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002978 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
William Lallemand74c24fb2016-11-21 17:18:36 +01002979 switch (action) {
2980 case ST_ADM_ACTION_DISABLE:
Emeric Brun52a91d32017-08-31 14:41:55 +02002981 if (!(sv->cur_admin & SRV_ADMF_FMAINT)) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002982 altered_servers++;
2983 total_servers++;
2984 srv_set_admin_flag(sv, SRV_ADMF_FMAINT, "'disable' on stats page");
2985 }
2986 break;
2987 case ST_ADM_ACTION_ENABLE:
Emeric Brun52a91d32017-08-31 14:41:55 +02002988 if (sv->cur_admin & SRV_ADMF_FMAINT) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002989 altered_servers++;
2990 total_servers++;
2991 srv_clr_admin_flag(sv, SRV_ADMF_FMAINT);
2992 }
2993 break;
2994 case ST_ADM_ACTION_STOP:
Emeric Brun52a91d32017-08-31 14:41:55 +02002995 if (!(sv->cur_admin & SRV_ADMF_FDRAIN)) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002996 srv_set_admin_flag(sv, SRV_ADMF_FDRAIN, "'stop' on stats page");
2997 altered_servers++;
2998 total_servers++;
2999 }
3000 break;
3001 case ST_ADM_ACTION_START:
Emeric Brun52a91d32017-08-31 14:41:55 +02003002 if (sv->cur_admin & SRV_ADMF_FDRAIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01003003 srv_clr_admin_flag(sv, SRV_ADMF_FDRAIN);
3004 altered_servers++;
3005 total_servers++;
3006 }
3007 break;
3008 case ST_ADM_ACTION_DHLTH:
3009 if (sv->check.state & CHK_ST_CONFIGURED) {
3010 sv->check.state &= ~CHK_ST_ENABLED;
3011 altered_servers++;
3012 total_servers++;
3013 }
3014 break;
3015 case ST_ADM_ACTION_EHLTH:
3016 if (sv->check.state & CHK_ST_CONFIGURED) {
3017 sv->check.state |= CHK_ST_ENABLED;
3018 altered_servers++;
3019 total_servers++;
3020 }
3021 break;
3022 case ST_ADM_ACTION_HRUNN:
3023 if (!(sv->track)) {
3024 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02003025 srv_set_running(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003026 altered_servers++;
3027 total_servers++;
3028 }
3029 break;
3030 case ST_ADM_ACTION_HNOLB:
3031 if (!(sv->track)) {
3032 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02003033 srv_set_stopping(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003034 altered_servers++;
3035 total_servers++;
3036 }
3037 break;
3038 case ST_ADM_ACTION_HDOWN:
3039 if (!(sv->track)) {
3040 sv->check.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02003041 srv_set_stopped(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003042 altered_servers++;
3043 total_servers++;
3044 }
3045 break;
3046 case ST_ADM_ACTION_DAGENT:
3047 if (sv->agent.state & CHK_ST_CONFIGURED) {
3048 sv->agent.state &= ~CHK_ST_ENABLED;
3049 altered_servers++;
3050 total_servers++;
3051 }
3052 break;
3053 case ST_ADM_ACTION_EAGENT:
3054 if (sv->agent.state & CHK_ST_CONFIGURED) {
3055 sv->agent.state |= CHK_ST_ENABLED;
3056 altered_servers++;
3057 total_servers++;
3058 }
3059 break;
3060 case ST_ADM_ACTION_ARUNN:
3061 if (sv->agent.state & CHK_ST_ENABLED) {
3062 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02003063 srv_set_running(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003064 altered_servers++;
3065 total_servers++;
3066 }
3067 break;
3068 case ST_ADM_ACTION_ADOWN:
3069 if (sv->agent.state & CHK_ST_ENABLED) {
3070 sv->agent.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02003071 srv_set_stopped(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003072 altered_servers++;
3073 total_servers++;
3074 }
3075 break;
3076 case ST_ADM_ACTION_READY:
3077 srv_adm_set_ready(sv);
3078 altered_servers++;
3079 total_servers++;
3080 break;
3081 case ST_ADM_ACTION_DRAIN:
3082 srv_adm_set_drain(sv);
3083 altered_servers++;
3084 total_servers++;
3085 break;
3086 case ST_ADM_ACTION_MAINT:
3087 srv_adm_set_maint(sv);
3088 altered_servers++;
3089 total_servers++;
3090 break;
3091 case ST_ADM_ACTION_SHUTDOWN:
3092 if (px->state != PR_STSTOPPED) {
3093 struct stream *sess, *sess_bck;
3094
3095 list_for_each_entry_safe(sess, sess_bck, &sv->actconns, by_srv)
3096 if (sess->srv_conn == sv)
3097 stream_shutdown(sess, SF_ERR_KILLED);
3098
3099 altered_servers++;
3100 total_servers++;
3101 }
3102 break;
3103 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003104 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Lallemand74c24fb2016-11-21 17:18:36 +01003105 } else {
3106 /* the server name is unknown or ambiguous (duplicate names) */
3107 total_servers++;
3108 }
3109 }
3110 if (reprocess && px && action) {
3111 /* Now, we know the backend and the action chosen by the user.
3112 * We can safely restart from the first server parameter
3113 * to reprocess them
3114 */
3115 cur_param = st_cur_param;
3116 next_param = st_next_param;
3117 reprocess = 0;
3118 goto reprocess_servers;
3119 }
3120
3121 next_param = cur_param;
3122 }
3123 }
3124
3125 if (total_servers == 0) {
3126 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
3127 }
3128 else if (altered_servers == 0) {
3129 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
3130 }
3131 else if (altered_servers == total_servers) {
3132 appctx->ctx.stats.st_code = STAT_STATUS_DONE;
3133 }
3134 else {
3135 appctx->ctx.stats.st_code = STAT_STATUS_PART;
3136 }
3137 out:
3138 return 1;
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01003139 wait:
3140 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
3141 return 0;
Christopher Fauletef779222018-10-31 08:47:01 +01003142}
3143
3144
3145static int stats_send_htx_headers(struct stream_interface *si, struct htx *htx)
3146{
3147 struct stream *s = si_strm(si);
3148 struct uri_auth *uri = s->be->uri_auth;
3149 struct appctx *appctx = __objt_appctx(si->end);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003150 struct htx_sl *sl;
3151 unsigned int flags;
Christopher Fauletef779222018-10-31 08:47:01 +01003152
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003153 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);
3154 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist("200"), ist("OK"));
3155 if (!sl)
Christopher Fauletef779222018-10-31 08:47:01 +01003156 goto full;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003157 sl->info.res.status = 200;
Christopher Fauletef779222018-10-31 08:47:01 +01003158
Christopher Fauletb829f4c2019-03-29 16:13:55 +01003159 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")))
Christopher Fauletef779222018-10-31 08:47:01 +01003160 goto full;
3161 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
3162 if (!htx_add_header(htx, ist("Content-Type"), ist("text/html")))
3163 goto full;
3164 }
3165 else {
3166 if (!htx_add_header(htx, ist("Content-Type"), ist("text/plain")))
3167 goto full;
3168 }
3169
3170 if (uri->refresh > 0 && !(appctx->ctx.stats.flags & STAT_NO_REFRESH)) {
3171 const char *refresh = U2A(uri->refresh);
3172 if (!htx_add_header(htx, ist("Refresh"), ist2(refresh, strlen(refresh))))
3173 goto full;
3174 }
3175
3176 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
3177 if (!htx_add_header(htx, ist("Transfer-Encoding"), ist("chunked")))
3178 goto full;
3179 }
3180
3181 if (!htx_add_endof(htx, HTX_BLK_EOH))
3182 goto full;
3183
Christopher Faulet1e2d6362019-02-27 16:28:48 +01003184 channel_add_input(&s->res, htx->data);
Christopher Fauletef779222018-10-31 08:47:01 +01003185 return 1;
3186
3187 full:
3188 htx_reset(htx);
3189 si_rx_room_blk(si);
3190 return 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01003191}
3192
Christopher Fauletef779222018-10-31 08:47:01 +01003193
3194static int stats_send_htx_redirect(struct stream_interface *si, struct htx *htx)
3195{
3196 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
3197 struct stream *s = si_strm(si);
3198 struct uri_auth *uri = s->be->uri_auth;
3199 struct appctx *appctx = __objt_appctx(si->end);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003200 struct htx_sl *sl;
3201 unsigned int flags;
Christopher Fauletef779222018-10-31 08:47:01 +01003202
3203 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
3204 scope_txt[0] = 0;
3205 if (appctx->ctx.stats.scope_len) {
Christopher Fauleted7a0662019-01-14 11:07:34 +01003206 const char *scope_ptr = stats_scope_ptr(appctx, si);
3207
Christopher Fauletef779222018-10-31 08:47:01 +01003208 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Christopher Fauleted7a0662019-01-14 11:07:34 +01003209 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), scope_ptr, appctx->ctx.stats.scope_len);
Christopher Fauletef779222018-10-31 08:47:01 +01003210 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
3211 }
3212
3213 /* We don't want to land on the posted stats page because a refresh will
3214 * repost the data. We don't want this to happen on accident so we redirect
3215 * the browse to the stats page with a GET.
3216 */
3217 chunk_printf(&trash, "%s;st=%s%s%s%s",
3218 uri->uri_prefix,
3219 ((appctx->ctx.stats.st_code > STAT_STATUS_INIT) &&
3220 (appctx->ctx.stats.st_code < STAT_STATUS_SIZE) &&
3221 stat_status_codes[appctx->ctx.stats.st_code]) ?
3222 stat_status_codes[appctx->ctx.stats.st_code] :
3223 stat_status_codes[STAT_STATUS_UNKN],
3224 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3225 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3226 scope_txt);
3227
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003228 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_CHNK);
3229 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist("303"), ist("See Other"));
3230 if (!sl)
Christopher Fauletef779222018-10-31 08:47:01 +01003231 goto full;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003232 sl->info.res.status = 303;
Christopher Fauletef779222018-10-31 08:47:01 +01003233
3234 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")) ||
Christopher Fauletef779222018-10-31 08:47:01 +01003235 !htx_add_header(htx, ist("Content-Type"), ist("text/plain")) ||
3236 !htx_add_header(htx, ist("Content-Length"), ist("0")) ||
3237 !htx_add_header(htx, ist("Location"), ist2(trash.area, trash.data)))
3238 goto full;
3239
3240 if (!htx_add_endof(htx, HTX_BLK_EOH))
3241 goto full;
3242
Christopher Faulet1e2d6362019-02-27 16:28:48 +01003243 channel_add_input(&s->res, htx->data);
Christopher Fauletef779222018-10-31 08:47:01 +01003244 return 1;
3245
3246full:
3247 htx_reset(htx);
3248 si_rx_room_blk(si);
3249 return 0;
3250}
William Lallemand74c24fb2016-11-21 17:18:36 +01003251
3252static int stats_send_http_headers(struct stream_interface *si)
3253{
3254 struct stream *s = si_strm(si);
3255 struct uri_auth *uri = s->be->uri_auth;
Willy Tarreau21ff2c42018-09-20 11:01:01 +02003256 struct appctx *appctx = __objt_appctx(si->end);
William Lallemand74c24fb2016-11-21 17:18:36 +01003257
3258 chunk_printf(&trash,
3259 "HTTP/1.1 200 OK\r\n"
3260 "Cache-Control: no-cache\r\n"
3261 "Connection: close\r\n"
3262 "Content-Type: %s\r\n",
3263 (appctx->ctx.stats.flags & STAT_FMT_HTML) ? "text/html" : "text/plain");
3264
3265 if (uri->refresh > 0 && !(appctx->ctx.stats.flags & STAT_NO_REFRESH))
3266 chunk_appendf(&trash, "Refresh: %d\r\n",
3267 uri->refresh);
3268
3269 /* we don't send the CRLF in chunked mode, it will be sent with the first chunk's size */
3270
3271 if (appctx->ctx.stats.flags & STAT_CHUNKED)
3272 chunk_appendf(&trash, "Transfer-Encoding: chunked\r\n");
3273 else
3274 chunk_appendf(&trash, "\r\n");
3275
Willy Tarreau06d80a92017-10-19 14:32:15 +02003276 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01003277 si_rx_room_blk(si);
William Lallemand74c24fb2016-11-21 17:18:36 +01003278 return 0;
3279 }
3280
3281 return 1;
3282}
3283
3284static int stats_send_http_redirect(struct stream_interface *si)
3285{
3286 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
3287 struct stream *s = si_strm(si);
3288 struct uri_auth *uri = s->be->uri_auth;
Willy Tarreau21ff2c42018-09-20 11:01:01 +02003289 struct appctx *appctx = __objt_appctx(si->end);
William Lallemand74c24fb2016-11-21 17:18:36 +01003290
3291 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
3292 scope_txt[0] = 0;
3293 if (appctx->ctx.stats.scope_len) {
3294 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau89faf5d2018-06-07 18:16:48 +02003295 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 +01003296 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
3297 }
3298
3299 /* We don't want to land on the posted stats page because a refresh will
3300 * repost the data. We don't want this to happen on accident so we redirect
3301 * the browse to the stats page with a GET.
3302 */
3303 chunk_printf(&trash,
3304 "HTTP/1.1 303 See Other\r\n"
3305 "Cache-Control: no-cache\r\n"
3306 "Content-Type: text/plain\r\n"
3307 "Connection: close\r\n"
3308 "Location: %s;st=%s%s%s%s\r\n"
3309 "Content-length: 0\r\n"
3310 "\r\n",
3311 uri->uri_prefix,
3312 ((appctx->ctx.stats.st_code > STAT_STATUS_INIT) &&
3313 (appctx->ctx.stats.st_code < STAT_STATUS_SIZE) &&
3314 stat_status_codes[appctx->ctx.stats.st_code]) ?
3315 stat_status_codes[appctx->ctx.stats.st_code] :
3316 stat_status_codes[STAT_STATUS_UNKN],
3317 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3318 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3319 scope_txt);
3320
Willy Tarreau06d80a92017-10-19 14:32:15 +02003321 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01003322 si_rx_room_blk(si);
William Lallemand74c24fb2016-11-21 17:18:36 +01003323 return 0;
3324 }
3325
3326 return 1;
3327}
3328
Simon Horman05ee2132017-01-04 09:37:25 +01003329
William Lallemand74c24fb2016-11-21 17:18:36 +01003330/* This I/O handler runs as an applet embedded in a stream interface. It is
3331 * used to send HTTP stats over a TCP socket. The mechanism is very simple.
3332 * appctx->st0 contains the operation in progress (dump, done). The handler
3333 * automatically unregisters itself once transfer is complete.
3334 */
Christopher Fauletef779222018-10-31 08:47:01 +01003335static void htx_stats_io_handler(struct appctx *appctx)
3336{
3337 struct stream_interface *si = appctx->owner;
3338 struct stream *s = si_strm(si);
3339 struct channel *req = si_oc(si);
3340 struct channel *res = si_ic(si);
3341 struct htx *req_htx, *res_htx;
3342
3343 res_htx = htx_from_buf(&res->buf);
3344
3345 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
3346 goto out;
3347
3348 /* Check if the input buffer is avalaible. */
3349 if (!b_size(&res->buf)) {
3350 si_rx_room_blk(si);
3351 goto out;
3352 }
3353
3354 /* check that the output is not closed */
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003355 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_SHUTR))
3356 appctx->st0 = STAT_HTTP_END;
Christopher Fauletef779222018-10-31 08:47:01 +01003357
3358 /* all states are processed in sequence */
3359 if (appctx->st0 == STAT_HTTP_HEAD) {
3360 if (stats_send_htx_headers(si, res_htx)) {
3361 if (s->txn->meth == HTTP_METH_HEAD)
3362 appctx->st0 = STAT_HTTP_DONE;
3363 else
3364 appctx->st0 = STAT_HTTP_DUMP;
3365 }
3366 }
3367
3368 if (appctx->st0 == STAT_HTTP_DUMP) {
3369 if (stats_dump_stat_to_buffer(si, res_htx, s->be->uri_auth))
3370 appctx->st0 = STAT_HTTP_DONE;
3371 }
3372
3373 if (appctx->st0 == STAT_HTTP_POST) {
3374 if (stats_process_http_post(si))
3375 appctx->st0 = STAT_HTTP_LAST;
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003376 else if (req->flags & CF_SHUTR)
Christopher Fauletef779222018-10-31 08:47:01 +01003377 appctx->st0 = STAT_HTTP_DONE;
3378 }
3379
3380 if (appctx->st0 == STAT_HTTP_LAST) {
3381 if (stats_send_htx_redirect(si, res_htx))
3382 appctx->st0 = STAT_HTTP_DONE;
3383 }
3384
3385 if (appctx->st0 == STAT_HTTP_DONE) {
Christopher Faulet54b5e212019-06-04 10:08:28 +02003386 /* Don't add TLR because mux-h1 will take care of it */
Christopher Fauletef779222018-10-31 08:47:01 +01003387 if (!htx_add_endof(res_htx, HTX_BLK_EOM)) {
3388 si_rx_room_blk(si);
3389 goto out;
3390 }
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003391 channel_add_input(&s->res, 1);
3392 appctx->st0 = STAT_HTTP_END;
Christopher Fauletef779222018-10-31 08:47:01 +01003393 }
3394
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003395 if (appctx->st0 == STAT_HTTP_END) {
3396 if (!(res->flags & CF_SHUTR)) {
Christopher Fauletef779222018-10-31 08:47:01 +01003397 res->flags |= CF_READ_NULL;
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003398 si_shutr(si);
3399 }
3400
3401 /* eat the whole request */
3402 if (co_data(req)) {
3403 req_htx = htx_from_buf(&req->buf);
3404 co_htx_skip(req, req_htx, co_data(req));
3405 htx_to_buf(req_htx, &req->buf);
Christopher Fauletef779222018-10-31 08:47:01 +01003406 }
3407 }
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003408
Christopher Fauletef779222018-10-31 08:47:01 +01003409 out:
3410 /* we have left the request in the buffer for the case where we
3411 * process a POST, and this automatically re-enables activity on
3412 * read. It's better to indicate that we want to stop reading when
3413 * we're sending, so that we know there's at most one direction
3414 * deciding to wake the applet up. It saves it from looping when
3415 * emitting large blocks into small TCP windows.
3416 */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003417 htx_to_buf(res_htx, &res->buf);
3418 if (!channel_is_empty(res))
Christopher Fauletef779222018-10-31 08:47:01 +01003419 si_stop_get(si);
Christopher Fauletef779222018-10-31 08:47:01 +01003420}
3421
3422
3423/* This I/O handler runs as an applet embedded in a stream interface. It is
3424 * used to send HTTP stats over a TCP socket. The mechanism is very simple.
3425 * appctx->st0 contains the operation in progress (dump, done). The handler
3426 * automatically unregisters itself once transfer is complete.
3427 */
William Lallemand74c24fb2016-11-21 17:18:36 +01003428static void http_stats_io_handler(struct appctx *appctx)
3429{
3430 struct stream_interface *si = appctx->owner;
3431 struct stream *s = si_strm(si);
3432 struct channel *req = si_oc(si);
3433 struct channel *res = si_ic(si);
3434
Christopher Fauletef779222018-10-31 08:47:01 +01003435 if (IS_HTX_STRM(s))
3436 return htx_stats_io_handler(appctx);
3437
William Lallemand74c24fb2016-11-21 17:18:36 +01003438 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
3439 goto out;
3440
Joseph Herlant29023ec2018-11-15 13:39:46 -08003441 /* Check if the input buffer is available. */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003442 if (res->buf.size == 0) {
Willy Tarreau4b962a42018-11-15 11:03:21 +01003443 /* already subscribed, we'll be called later once the buffer is
3444 * available.
3445 */
Christopher Fauleta73e59b2016-12-09 17:30:18 +01003446 goto out;
3447 }
3448
William Lallemand74c24fb2016-11-21 17:18:36 +01003449 /* check that the output is not closed */
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003450 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_SHUTR))
3451 appctx->st0 = STAT_HTTP_END;
William Lallemand74c24fb2016-11-21 17:18:36 +01003452
3453 /* all states are processed in sequence */
3454 if (appctx->st0 == STAT_HTTP_HEAD) {
3455 if (stats_send_http_headers(si)) {
3456 if (s->txn->meth == HTTP_METH_HEAD)
3457 appctx->st0 = STAT_HTTP_DONE;
3458 else
3459 appctx->st0 = STAT_HTTP_DUMP;
3460 }
3461 }
3462
3463 if (appctx->st0 == STAT_HTTP_DUMP) {
Willy Tarreau97f538b2018-06-15 19:41:31 +02003464 unsigned int prev_len = ci_data(si_ic(si));
William Lallemand74c24fb2016-11-21 17:18:36 +01003465 unsigned int data_len;
3466 unsigned int last_len;
3467 unsigned int last_fwd = 0;
3468
3469 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
3470 /* One difficulty we're facing is that we must prevent
3471 * the input data from being automatically forwarded to
3472 * the output area. For this, we temporarily disable
3473 * forwarding on the channel.
3474 */
3475 last_fwd = si_ic(si)->to_forward;
3476 si_ic(si)->to_forward = 0;
3477 chunk_printf(&trash, "\r\n000000\r\n");
Willy Tarreau06d80a92017-10-19 14:32:15 +02003478 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01003479 si_rx_room_blk(si);
William Lallemand74c24fb2016-11-21 17:18:36 +01003480 si_ic(si)->to_forward = last_fwd;
3481 goto out;
3482 }
3483 }
3484
Willy Tarreau97f538b2018-06-15 19:41:31 +02003485 data_len = ci_data(si_ic(si));
Christopher Fauletef779222018-10-31 08:47:01 +01003486 if (stats_dump_stat_to_buffer(si, NULL, s->be->uri_auth))
William Lallemand74c24fb2016-11-21 17:18:36 +01003487 appctx->st0 = STAT_HTTP_DONE;
3488
Willy Tarreau97f538b2018-06-15 19:41:31 +02003489 last_len = ci_data(si_ic(si));
William Lallemand74c24fb2016-11-21 17:18:36 +01003490
3491 /* Now we must either adjust or remove the chunk size. This is
3492 * not easy because the chunk size might wrap at the end of the
3493 * buffer, so we pretend we have nothing in the buffer, we write
3494 * the size, then restore the buffer's contents. Note that we can
3495 * only do that because no forwarding is scheduled on the stats
3496 * applet.
3497 */
3498 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
3499 si_ic(si)->total -= (last_len - prev_len);
Willy Tarreau97f538b2018-06-15 19:41:31 +02003500 b_sub(si_ib(si), (last_len - prev_len));
William Lallemand74c24fb2016-11-21 17:18:36 +01003501
3502 if (last_len != data_len) {
3503 chunk_printf(&trash, "\r\n%06x\r\n", (last_len - data_len));
Willy Tarreau06d80a92017-10-19 14:32:15 +02003504 if (ci_putchk(si_ic(si), &trash) == -1)
Willy Tarreaudb398432018-11-15 11:08:52 +01003505 si_rx_room_blk(si);
William Lallemand74c24fb2016-11-21 17:18:36 +01003506
3507 si_ic(si)->total += (last_len - data_len);
Willy Tarreau97f538b2018-06-15 19:41:31 +02003508 b_add(si_ib(si), last_len - data_len);
William Lallemand74c24fb2016-11-21 17:18:36 +01003509 }
3510 /* now re-enable forwarding */
3511 channel_forward(si_ic(si), last_fwd);
3512 }
3513 }
3514
3515 if (appctx->st0 == STAT_HTTP_POST) {
3516 if (stats_process_http_post(si))
3517 appctx->st0 = STAT_HTTP_LAST;
3518 else if (si_oc(si)->flags & CF_SHUTR)
3519 appctx->st0 = STAT_HTTP_DONE;
3520 }
3521
3522 if (appctx->st0 == STAT_HTTP_LAST) {
3523 if (stats_send_http_redirect(si))
3524 appctx->st0 = STAT_HTTP_DONE;
3525 }
3526
3527 if (appctx->st0 == STAT_HTTP_DONE) {
3528 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
3529 chunk_printf(&trash, "\r\n0\r\n\r\n");
Willy Tarreau06d80a92017-10-19 14:32:15 +02003530 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01003531 si_rx_room_blk(si);
William Lallemand74c24fb2016-11-21 17:18:36 +01003532 goto out;
3533 }
3534 }
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003535 appctx->st0 = STAT_HTTP_END;
William Lallemand74c24fb2016-11-21 17:18:36 +01003536 }
3537
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003538 if (appctx->st0 == STAT_HTTP_END) {
3539 if (!(res->flags & CF_SHUTR)) {
William Lallemand74c24fb2016-11-21 17:18:36 +01003540 res->flags |= CF_READ_NULL;
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003541 si_shutr(si);
William Lallemand74c24fb2016-11-21 17:18:36 +01003542 }
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003543
3544 /* eat the whole request */
3545 if (co_data(req))
3546 co_skip(si_oc(si), co_data(si_oc(si)));
William Lallemand74c24fb2016-11-21 17:18:36 +01003547 }
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003548
William Lallemand74c24fb2016-11-21 17:18:36 +01003549 out:
Willy Tarreau055ba4f2018-07-24 17:05:54 +02003550 /* we have left the request in the buffer for the case where we
3551 * process a POST, and this automatically re-enables activity on
3552 * read. It's better to indicate that we want to stop reading when
3553 * we're sending, so that we know there's at most one direction
3554 * deciding to wake the applet up. It saves it from looping when
3555 * emitting large blocks into small TCP windows.
3556 */
3557 if (!channel_is_empty(res))
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01003558 si_stop_get(si);
William Lallemand74c24fb2016-11-21 17:18:36 +01003559}
3560
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003561/* Dump all fields from <info> into <out> using the "show info" format (name: value) */
Willy Tarreau83061a82018-07-13 11:56:34 +02003562static int stats_dump_info_fields(struct buffer *out,
3563 const struct field *info)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003564{
3565 int field;
3566
3567 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
3568 if (!field_format(info, field))
3569 continue;
3570
3571 if (!chunk_appendf(out, "%s: ", info_field_names[field]))
3572 return 0;
3573 if (!stats_emit_raw_data_field(out, &info[field]))
3574 return 0;
3575 if (!chunk_strcat(out, "\n"))
3576 return 0;
3577 }
3578 return 1;
3579}
3580
3581/* Dump all fields from <info> into <out> using the "show info typed" format */
Willy Tarreau83061a82018-07-13 11:56:34 +02003582static int stats_dump_typed_info_fields(struct buffer *out,
3583 const struct field *info)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003584{
3585 int field;
3586
3587 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
3588 if (!field_format(info, field))
3589 continue;
3590
3591 if (!chunk_appendf(out, "%d.%s.%u:", field, info_field_names[field], info[INF_PROCESS_NUM].u.u32))
3592 return 0;
3593 if (!stats_emit_field_tags(out, &info[field], ':'))
3594 return 0;
3595 if (!stats_emit_typed_data_field(out, &info[field]))
3596 return 0;
3597 if (!chunk_strcat(out, "\n"))
3598 return 0;
3599 }
3600 return 1;
3601}
3602
3603/* Fill <info> with HAProxy global info. <info> is preallocated
3604 * array of length <len>. The length of the aray must be
3605 * INF_TOTAL_FIELDS. If this length is less then this value, the
3606 * function returns 0, otherwise, it returns 1.
3607 */
3608int stats_fill_info(struct field *info, int len)
3609{
3610 unsigned int up = (now.tv_sec - start_date.tv_sec);
Willy Tarreau83061a82018-07-13 11:56:34 +02003611 struct buffer *out = get_trash_chunk();
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003612
3613#ifdef USE_OPENSSL
3614 int ssl_sess_rate = read_freq_ctr(&global.ssl_per_sec);
3615 int ssl_key_rate = read_freq_ctr(&global.ssl_fe_keys_per_sec);
3616 int ssl_reuse = 0;
3617
3618 if (ssl_key_rate < ssl_sess_rate) {
3619 /* count the ssl reuse ratio and avoid overflows in both directions */
3620 ssl_reuse = 100 - (100 * ssl_key_rate + (ssl_sess_rate - 1) / 2) / ssl_sess_rate;
3621 }
3622#endif
3623
3624 if (len < INF_TOTAL_FIELDS)
3625 return 0;
3626
3627 chunk_reset(out);
3628 memset(info, 0, sizeof(*info) * len);
3629
3630 info[INF_NAME] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, PRODUCT_NAME);
Willy Tarreau909b9d82019-01-04 18:20:32 +01003631 info[INF_VERSION] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, haproxy_version);
3632 info[INF_RELEASE_DATE] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, haproxy_date);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003633
Yves Lafon95317282018-02-26 11:10:37 +01003634 info[INF_NBTHREAD] = mkf_u32(FO_CONFIG|FS_SERVICE, global.nbthread);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003635 info[INF_NBPROC] = mkf_u32(FO_CONFIG|FS_SERVICE, global.nbproc);
3636 info[INF_PROCESS_NUM] = mkf_u32(FO_KEY, relative_pid);
3637 info[INF_PID] = mkf_u32(FO_STATUS, pid);
3638
3639 info[INF_UPTIME] = mkf_str(FN_DURATION, chunk_newstr(out));
3640 chunk_appendf(out, "%ud %uh%02um%02us", up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60));
3641
3642 info[INF_UPTIME_SEC] = mkf_u32(FN_DURATION, up);
3643 info[INF_MEMMAX_MB] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_memmax);
3644 info[INF_POOL_ALLOC_MB] = mkf_u32(0, (unsigned)(pool_total_allocated() / 1048576L));
3645 info[INF_POOL_USED_MB] = mkf_u32(0, (unsigned)(pool_total_used() / 1048576L));
3646 info[INF_POOL_FAILED] = mkf_u32(FN_COUNTER, pool_total_failures());
3647 info[INF_ULIMIT_N] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_nofile);
3648 info[INF_MAXSOCK] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxsock);
3649 info[INF_MAXCONN] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxconn);
3650 info[INF_HARD_MAXCONN] = mkf_u32(FO_CONFIG|FN_LIMIT, global.hardmaxconn);
3651 info[INF_CURR_CONN] = mkf_u32(0, actconn);
3652 info[INF_CUM_CONN] = mkf_u32(FN_COUNTER, totalconn);
3653 info[INF_CUM_REQ] = mkf_u32(FN_COUNTER, global.req_count);
3654#ifdef USE_OPENSSL
3655 info[INF_MAX_SSL_CONNS] = mkf_u32(FN_MAX, global.maxsslconn);
3656 info[INF_CURR_SSL_CONNS] = mkf_u32(0, sslconns);
3657 info[INF_CUM_SSL_CONNS] = mkf_u32(FN_COUNTER, totalsslconns);
3658#endif
3659 info[INF_MAXPIPES] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxpipes);
3660 info[INF_PIPES_USED] = mkf_u32(0, pipes_used);
3661 info[INF_PIPES_FREE] = mkf_u32(0, pipes_free);
3662 info[INF_CONN_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.conn_per_sec));
3663 info[INF_CONN_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.cps_lim);
3664 info[INF_MAX_CONN_RATE] = mkf_u32(FN_MAX, global.cps_max);
3665 info[INF_SESS_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.sess_per_sec));
3666 info[INF_SESS_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.sps_lim);
3667 info[INF_MAX_SESS_RATE] = mkf_u32(FN_RATE, global.sps_max);
3668
3669#ifdef USE_OPENSSL
3670 info[INF_SSL_RATE] = mkf_u32(FN_RATE, ssl_sess_rate);
3671 info[INF_SSL_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.ssl_lim);
3672 info[INF_MAX_SSL_RATE] = mkf_u32(FN_MAX, global.ssl_max);
3673 info[INF_SSL_FRONTEND_KEY_RATE] = mkf_u32(0, ssl_key_rate);
3674 info[INF_SSL_FRONTEND_MAX_KEY_RATE] = mkf_u32(FN_MAX, global.ssl_fe_keys_max);
3675 info[INF_SSL_FRONTEND_SESSION_REUSE_PCT] = mkf_u32(0, ssl_reuse);
3676 info[INF_SSL_BACKEND_KEY_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.ssl_be_keys_per_sec));
3677 info[INF_SSL_BACKEND_MAX_KEY_RATE] = mkf_u32(FN_MAX, global.ssl_be_keys_max);
3678 info[INF_SSL_CACHE_LOOKUPS] = mkf_u32(FN_COUNTER, global.shctx_lookups);
3679 info[INF_SSL_CACHE_MISSES] = mkf_u32(FN_COUNTER, global.shctx_misses);
3680#endif
3681 info[INF_COMPRESS_BPS_IN] = mkf_u32(FN_RATE, read_freq_ctr(&global.comp_bps_in));
3682 info[INF_COMPRESS_BPS_OUT] = mkf_u32(FN_RATE, read_freq_ctr(&global.comp_bps_out));
3683 info[INF_COMPRESS_BPS_RATE_LIM] = mkf_u32(FO_CONFIG|FN_LIMIT, global.comp_rate_lim);
3684#ifdef USE_ZLIB
3685 info[INF_ZLIB_MEM_USAGE] = mkf_u32(0, zlib_used_memory);
3686 info[INF_MAX_ZLIB_MEM_USAGE] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxzlibmem);
3687#endif
3688 info[INF_TASKS] = mkf_u32(0, nb_tasks_cur);
Christopher Faulet34c5cc92016-12-06 09:15:30 +01003689 info[INF_RUN_QUEUE] = mkf_u32(0, tasks_run_queue_cur);
Willy Tarreau81036f22019-05-20 19:24:50 +02003690 info[INF_IDLE_PCT] = mkf_u32(FN_AVG, ti->idle_pct);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003691 info[INF_NODE] = mkf_str(FO_CONFIG|FN_OUTPUT|FS_SERVICE, global.node);
3692 if (global.desc)
3693 info[INF_DESCRIPTION] = mkf_str(FO_CONFIG|FN_OUTPUT|FS_SERVICE, global.desc);
Willy Tarreau00098ea2018-11-05 14:38:13 +01003694 info[INF_STOPPING] = mkf_u32(0, stopping);
3695 info[INF_JOBS] = mkf_u32(0, jobs);
William Lallemanda7199262018-11-16 16:57:20 +01003696 info[INF_UNSTOPPABLE_JOBS] = mkf_u32(0, unstoppable_jobs);
Willy Tarreau00098ea2018-11-05 14:38:13 +01003697 info[INF_LISTENERS] = mkf_u32(0, listeners);
Willy Tarreau199ad242018-11-05 16:31:22 +01003698 info[INF_ACTIVE_PEERS] = mkf_u32(0, active_peers);
Willy Tarreau2d372c22018-11-05 17:12:27 +01003699 info[INF_CONNECTED_PEERS] = mkf_u32(0, connected_peers);
Willy Tarreau13ef7732018-11-12 07:25:28 +01003700 info[INF_DROPPED_LOGS] = mkf_u32(0, dropped_logs);
Willy Tarreaubeb859a2018-11-22 18:07:59 +01003701 info[INF_BUSY_POLLING] = mkf_u32(0, !!(global.tune.options & GTUNE_BUSY_POLLING));
Baptiste Assmann333939c2019-01-21 08:34:50 +01003702 info[INF_FAILED_RESOLUTIONS] = mkf_u32(0, dns_failed_resolutions);
Willy Tarreau7cf0e452019-05-23 11:39:14 +02003703 info[INF_TOTAL_BYTES_OUT] = mkf_u64(0, global.out_bytes);
3704 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 +01003705
3706 return 1;
3707}
3708
3709/* This function dumps information onto the stream interface's read buffer.
3710 * It returns 0 as long as it does not complete, non-zero upon completion.
3711 * No state is used.
3712 */
3713static int stats_dump_info_to_buffer(struct stream_interface *si)
3714{
3715 struct appctx *appctx = __objt_appctx(si->end);
3716
3717 if (!stats_fill_info(info, INF_TOTAL_FIELDS))
3718 return 0;
3719
3720 chunk_reset(&trash);
3721
3722 if (appctx->ctx.stats.flags & STAT_FMT_TYPED)
3723 stats_dump_typed_info_fields(&trash, info);
Simon Horman05ee2132017-01-04 09:37:25 +01003724 else if (appctx->ctx.stats.flags & STAT_FMT_JSON)
3725 stats_dump_json_info_fields(&trash, info);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003726 else
3727 stats_dump_info_fields(&trash, info);
3728
Willy Tarreau06d80a92017-10-19 14:32:15 +02003729 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01003730 si_rx_room_blk(si);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003731 return 0;
3732 }
3733
3734 return 1;
3735}
3736
Simon Horman6f6bb382017-01-04 09:37:26 +01003737/* This function dumps the schema onto the stream interface's read buffer.
3738 * It returns 0 as long as it does not complete, non-zero upon completion.
3739 * No state is used.
3740 *
3741 * Integer values bouned to the range [-(2**53)+1, (2**53)-1] as
3742 * per the recommendation for interoperable integers in section 6 of RFC 7159.
3743 */
Willy Tarreau83061a82018-07-13 11:56:34 +02003744static void stats_dump_json_schema(struct buffer *out)
Simon Horman6f6bb382017-01-04 09:37:26 +01003745{
3746
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003747 int old_len = out->data;
Simon Horman6f6bb382017-01-04 09:37:26 +01003748
3749 chunk_strcat(out,
3750 "{"
3751 "\"$schema\":\"http://json-schema.org/draft-04/schema#\","
3752 "\"oneOf\":["
3753 "{"
3754 "\"title\":\"Info\","
3755 "\"type\":\"array\","
3756 "\"items\":{"
3757 "\"properties\":{"
3758 "\"title\":\"InfoItem\","
3759 "\"type\":\"object\","
3760 "\"field\":{\"$ref\":\"#/definitions/field\"},"
3761 "\"processNum\":{\"$ref\":\"#/definitions/processNum\"},"
3762 "\"tags\":{\"$ref\":\"#/definitions/tags\"},"
3763 "\"value\":{\"$ref\":\"#/definitions/typedValue\"}"
3764 "},"
3765 "\"required\":[\"field\",\"processNum\",\"tags\","
3766 "\"value\"]"
3767 "}"
3768 "},"
3769 "{"
3770 "\"title\":\"Stat\","
3771 "\"type\":\"array\","
3772 "\"items\":{"
3773 "\"title\":\"InfoItem\","
3774 "\"type\":\"object\","
3775 "\"properties\":{"
3776 "\"objType\":{"
3777 "\"enum\":[\"Frontend\",\"Backend\",\"Listener\","
3778 "\"Server\",\"Unknown\"]"
3779 "},"
3780 "\"proxyId\":{"
3781 "\"type\":\"integer\","
3782 "\"minimum\":0"
3783 "},"
3784 "\"id\":{"
3785 "\"type\":\"integer\","
3786 "\"minimum\":0"
3787 "},"
3788 "\"field\":{\"$ref\":\"#/definitions/field\"},"
3789 "\"processNum\":{\"$ref\":\"#/definitions/processNum\"},"
3790 "\"tags\":{\"$ref\":\"#/definitions/tags\"},"
3791 "\"typedValue\":{\"$ref\":\"#/definitions/typedValue\"}"
3792 "},"
3793 "\"required\":[\"objType\",\"proxyId\",\"id\","
3794 "\"field\",\"processNum\",\"tags\","
3795 "\"value\"]"
3796 "}"
3797 "},"
3798 "{"
3799 "\"title\":\"Error\","
3800 "\"type\":\"object\","
3801 "\"properties\":{"
3802 "\"errorStr\":{"
3803 "\"type\":\"string\""
3804 "},"
3805 "\"required\":[\"errorStr\"]"
3806 "}"
3807 "}"
3808 "],"
3809 "\"definitions\":{"
3810 "\"field\":{"
3811 "\"type\":\"object\","
3812 "\"pos\":{"
3813 "\"type\":\"integer\","
3814 "\"minimum\":0"
3815 "},"
3816 "\"name\":{"
3817 "\"type\":\"string\""
3818 "},"
3819 "\"required\":[\"pos\",\"name\"]"
3820 "},"
3821 "\"processNum\":{"
3822 "\"type\":\"integer\","
3823 "\"minimum\":1"
3824 "},"
3825 "\"tags\":{"
3826 "\"type\":\"object\","
3827 "\"origin\":{"
3828 "\"type\":\"string\","
3829 "\"enum\":[\"Metric\",\"Status\",\"Key\","
3830 "\"Config\",\"Product\",\"Unknown\"]"
3831 "},"
3832 "\"nature\":{"
3833 "\"type\":\"string\","
3834 "\"enum\":[\"Gauge\",\"Limit\",\"Min\",\"Max\","
3835 "\"Rate\",\"Counter\",\"Duration\","
3836 "\"Age\",\"Time\",\"Name\",\"Output\","
3837 "\"Avg\", \"Unknown\"]"
3838 "},"
3839 "\"scope\":{"
3840 "\"type\":\"string\","
3841 "\"enum\":[\"Cluster\",\"Process\",\"Service\","
3842 "\"System\",\"Unknown\"]"
3843 "},"
3844 "\"required\":[\"origin\",\"nature\",\"scope\"]"
3845 "},"
3846 "\"typedValue\":{"
3847 "\"type\":\"object\","
3848 "\"oneOf\":["
3849 "{\"$ref\":\"#/definitions/typedValue/definitions/s32Value\"},"
3850 "{\"$ref\":\"#/definitions/typedValue/definitions/s64Value\"},"
3851 "{\"$ref\":\"#/definitions/typedValue/definitions/u32Value\"},"
3852 "{\"$ref\":\"#/definitions/typedValue/definitions/u64Value\"},"
3853 "{\"$ref\":\"#/definitions/typedValue/definitions/strValue\"}"
3854 "],"
3855 "\"definitions\":{"
3856 "\"s32Value\":{"
3857 "\"properties\":{"
3858 "\"type\":{"
3859 "\"type\":\"string\","
3860 "\"enum\":[\"s32\"]"
3861 "},"
3862 "\"value\":{"
3863 "\"type\":\"integer\","
3864 "\"minimum\":-2147483648,"
3865 "\"maximum\":2147483647"
3866 "}"
3867 "},"
3868 "\"required\":[\"type\",\"value\"]"
3869 "},"
3870 "\"s64Value\":{"
3871 "\"properties\":{"
3872 "\"type\":{"
3873 "\"type\":\"string\","
3874 "\"enum\":[\"s64\"]"
3875 "},"
3876 "\"value\":{"
3877 "\"type\":\"integer\","
3878 "\"minimum\":-9007199254740991,"
3879 "\"maximum\":9007199254740991"
3880 "}"
3881 "},"
3882 "\"required\":[\"type\",\"value\"]"
3883 "},"
3884 "\"u32Value\":{"
3885 "\"properties\":{"
3886 "\"type\":{"
3887 "\"type\":\"string\","
3888 "\"enum\":[\"u32\"]"
3889 "},"
3890 "\"value\":{"
3891 "\"type\":\"integer\","
3892 "\"minimum\":0,"
3893 "\"maximum\":4294967295"
3894 "}"
3895 "},"
3896 "\"required\":[\"type\",\"value\"]"
3897 "},"
3898 "\"u64Value\":{"
3899 "\"properties\":{"
3900 "\"type\":{"
3901 "\"type\":\"string\","
3902 "\"enum\":[\"u64\"]"
3903 "},"
3904 "\"value\":{"
3905 "\"type\":\"integer\","
3906 "\"minimum\":0,"
3907 "\"maximum\":9007199254740991"
3908 "}"
3909 "},"
3910 "\"required\":[\"type\",\"value\"]"
3911 "},"
3912 "\"strValue\":{"
3913 "\"properties\":{"
3914 "\"type\":{"
3915 "\"type\":\"string\","
3916 "\"enum\":[\"str\"]"
3917 "},"
3918 "\"value\":{\"type\":\"string\"}"
3919 "},"
3920 "\"required\":[\"type\",\"value\"]"
3921 "},"
3922 "\"unknownValue\":{"
3923 "\"properties\":{"
3924 "\"type\":{"
3925 "\"type\":\"integer\","
3926 "\"minimum\":0"
3927 "},"
3928 "\"value\":{"
3929 "\"type\":\"string\","
3930 "\"enum\":[\"unknown\"]"
3931 "}"
3932 "},"
3933 "\"required\":[\"type\",\"value\"]"
3934 "}"
3935 "}"
3936 "}"
3937 "}"
3938 "}");
3939
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003940 if (old_len == out->data) {
Simon Horman6f6bb382017-01-04 09:37:26 +01003941 chunk_reset(out);
3942 chunk_appendf(out,
3943 "{\"errorStr\":\"output buffer too short\"}");
3944 }
3945}
3946
3947/* This function dumps the schema onto the stream interface's read buffer.
3948 * It returns 0 as long as it does not complete, non-zero upon completion.
3949 * No state is used.
3950 */
3951static int stats_dump_json_schema_to_buffer(struct stream_interface *si)
3952{
3953 chunk_reset(&trash);
3954
3955 stats_dump_json_schema(&trash);
3956
Willy Tarreau06d80a92017-10-19 14:32:15 +02003957 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01003958 si_rx_room_blk(si);
Simon Horman6f6bb382017-01-04 09:37:26 +01003959 return 0;
3960 }
3961
3962 return 1;
3963}
3964
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02003965static int cli_parse_clear_counters(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau89d467c2016-11-23 11:02:40 +01003966{
3967 struct proxy *px;
3968 struct server *sv;
3969 struct listener *li;
3970 int clrall = 0;
3971
3972 if (strcmp(args[2], "all") == 0)
3973 clrall = 1;
3974
3975 /* check permissions */
3976 if (!cli_has_level(appctx, ACCESS_LVL_OPER) ||
3977 (clrall && !cli_has_level(appctx, ACCESS_LVL_ADMIN)))
3978 return 1;
3979
Olivier Houchardfbc74e82017-11-24 16:54:05 +01003980 for (px = proxies_list; px; px = px->next) {
Willy Tarreau89d467c2016-11-23 11:02:40 +01003981 if (clrall) {
3982 memset(&px->be_counters, 0, sizeof(px->be_counters));
3983 memset(&px->fe_counters, 0, sizeof(px->fe_counters));
3984 }
3985 else {
3986 px->be_counters.conn_max = 0;
3987 px->be_counters.p.http.rps_max = 0;
3988 px->be_counters.sps_max = 0;
3989 px->be_counters.cps_max = 0;
3990 px->be_counters.nbpend_max = 0;
3991
3992 px->fe_counters.conn_max = 0;
3993 px->fe_counters.p.http.rps_max = 0;
3994 px->fe_counters.sps_max = 0;
3995 px->fe_counters.cps_max = 0;
Willy Tarreau89d467c2016-11-23 11:02:40 +01003996 }
3997
3998 for (sv = px->srv; sv; sv = sv->next)
3999 if (clrall)
4000 memset(&sv->counters, 0, sizeof(sv->counters));
4001 else {
4002 sv->counters.cur_sess_max = 0;
4003 sv->counters.nbpend_max = 0;
4004 sv->counters.sps_max = 0;
4005 }
4006
4007 list_for_each_entry(li, &px->conf.listeners, by_fe)
4008 if (li->counters) {
4009 if (clrall)
4010 memset(li->counters, 0, sizeof(*li->counters));
4011 else
4012 li->counters->conn_max = 0;
4013 }
4014 }
4015
4016 global.cps_max = 0;
4017 global.sps_max = 0;
Olivier Houchard00bc3cb2017-10-17 19:23:25 +02004018 global.ssl_max = 0;
4019 global.ssl_fe_keys_max = 0;
4020 global.ssl_be_keys_max = 0;
Willy Tarreaud80cb4e2018-01-20 19:30:13 +01004021
4022 memset(activity, 0, sizeof(activity));
Willy Tarreau89d467c2016-11-23 11:02:40 +01004023 return 1;
4024}
4025
4026
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004027static int cli_parse_show_info(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004028{
Willy Tarreaud25fc792016-12-16 12:33:47 +01004029 appctx->ctx.stats.scope_str = 0;
4030 appctx->ctx.stats.scope_len = 0;
4031 appctx->ctx.stats.flags = 0;
4032
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004033 if (strcmp(args[2], "typed") == 0)
4034 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
Simon Horman05ee2132017-01-04 09:37:25 +01004035 else if (strcmp(args[2], "json") == 0)
4036 appctx->ctx.stats.flags |= STAT_FMT_JSON;
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004037 return 0;
4038}
4039
4040
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004041static int cli_parse_show_stat(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau2b812e22016-11-22 16:18:05 +01004042{
Willy Tarreaud25fc792016-12-16 12:33:47 +01004043 appctx->ctx.stats.scope_str = 0;
4044 appctx->ctx.stats.scope_len = 0;
4045 appctx->ctx.stats.flags = 0;
4046
Willy Tarreau2b812e22016-11-22 16:18:05 +01004047 if (*args[2] && *args[3] && *args[4]) {
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01004048 struct proxy *px;
4049
4050 px = proxy_find_by_name(args[2], 0, 0);
4051 if (px)
4052 appctx->ctx.stats.iid = px->uuid;
4053 else
4054 appctx->ctx.stats.iid = atoi(args[2]);
4055
4056 if (!appctx->ctx.stats.iid) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004057 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01004058 appctx->ctx.cli.msg = "No such proxy.\n";
4059 appctx->st0 = CLI_ST_PRINT;
4060 return 1;
4061 }
4062
Willy Tarreau2b812e22016-11-22 16:18:05 +01004063 appctx->ctx.stats.flags |= STAT_BOUND;
Willy Tarreau2b812e22016-11-22 16:18:05 +01004064 appctx->ctx.stats.type = atoi(args[3]);
4065 appctx->ctx.stats.sid = atoi(args[4]);
4066 if (strcmp(args[5], "typed") == 0)
4067 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
Simon Horman05ee2132017-01-04 09:37:25 +01004068 else if (strcmp(args[5], "json") == 0)
4069 appctx->ctx.stats.flags |= STAT_FMT_JSON;
Willy Tarreau2b812e22016-11-22 16:18:05 +01004070 }
4071 else if (strcmp(args[2], "typed") == 0)
4072 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
Simon Horman05ee2132017-01-04 09:37:25 +01004073 else if (strcmp(args[2], "json") == 0)
4074 appctx->ctx.stats.flags |= STAT_FMT_JSON;
Willy Tarreau2b812e22016-11-22 16:18:05 +01004075
Willy Tarreau2b812e22016-11-22 16:18:05 +01004076 return 0;
4077}
4078
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004079static int cli_io_handler_dump_info(struct appctx *appctx)
4080{
4081 return stats_dump_info_to_buffer(appctx->owner);
4082}
4083
Willy Tarreau2b812e22016-11-22 16:18:05 +01004084/* This I/O handler runs as an applet embedded in a stream interface. It is
4085 * used to send raw stats over a socket.
4086 */
4087static int cli_io_handler_dump_stat(struct appctx *appctx)
4088{
Christopher Fauletef779222018-10-31 08:47:01 +01004089 return stats_dump_stat_to_buffer(appctx->owner, NULL, NULL);
Willy Tarreau2b812e22016-11-22 16:18:05 +01004090}
4091
Simon Horman6f6bb382017-01-04 09:37:26 +01004092static int cli_io_handler_dump_json_schema(struct appctx *appctx)
4093{
4094 return stats_dump_json_schema_to_buffer(appctx->owner);
4095}
4096
Willy Tarreau2b812e22016-11-22 16:18:05 +01004097/* register cli keywords */
4098static struct cli_kw_list cli_kws = {{ },{
Willy Tarreau89d467c2016-11-23 11:02:40 +01004099 { { "clear", "counters", NULL }, "clear counters : clear max statistics counters (add 'all' for all counters)", cli_parse_clear_counters, NULL, NULL },
Willy Tarreaue9038432019-10-09 07:19:02 +02004100 { { "show", "info", NULL }, "show info : report information about the running process [json|typed]", cli_parse_show_info, cli_io_handler_dump_info, NULL },
4101 { { "show", "stat", NULL }, "show stat : report counters for each proxy and server [json|typed]", cli_parse_show_stat, cli_io_handler_dump_stat, NULL },
Simon Horman6f6bb382017-01-04 09:37:26 +01004102 { { "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 +01004103 {{},}
4104}};
4105
Willy Tarreau0108d902018-11-25 19:14:37 +01004106INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
4107
William Lallemand74c24fb2016-11-21 17:18:36 +01004108struct applet http_stats_applet = {
4109 .obj_type = OBJ_TYPE_APPLET,
4110 .name = "<STATS>", /* used for logging */
4111 .fct = http_stats_io_handler,
4112 .release = NULL,
4113};
4114
William Lallemand74c24fb2016-11-21 17:18:36 +01004115/*
4116 * Local variables:
4117 * c-indent-level: 8
4118 * c-basic-offset: 8
4119 * End:
4120 */