blob: b3935410f7f3b8cae0eb61cefb47a051b9788716 [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>
William Lallemand9ed62032016-11-21 17:49:11 +010054#include <proto/stats.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010055#include <proto/fd.h>
56#include <proto/freq_ctr.h>
57#include <proto/frontend.h>
Christopher Fauleted7a0662019-01-14 11:07:34 +010058#include <proto/http_htx.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010059#include <proto/log.h>
60#include <proto/pattern.h>
61#include <proto/pipe.h>
62#include <proto/listener.h>
63#include <proto/map.h>
Christopher Fauletef779222018-10-31 08:47:01 +010064#include <proto/proto_http.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010065#include <proto/proxy.h>
66#include <proto/sample.h>
67#include <proto/session.h>
68#include <proto/stream.h>
69#include <proto/server.h>
70#include <proto/raw_sock.h>
71#include <proto/stream_interface.h>
72#include <proto/task.h>
73
74#ifdef USE_OPENSSL
75#include <proto/ssl_sock.h>
76#include <types/ssl_sock.h>
77#endif
78
79
Willy Tarreau0baac8c2016-11-22 16:36:53 +010080/* These are the field names for each INF_* field position. Please pay attention
81 * to always use the exact same name except that the strings for new names must
82 * be lower case or CamelCase while the enum entries must be upper case.
83 */
84const char *info_field_names[INF_TOTAL_FIELDS] = {
85 [INF_NAME] = "Name",
86 [INF_VERSION] = "Version",
87 [INF_RELEASE_DATE] = "Release_date",
Yves Lafon95317282018-02-26 11:10:37 +010088 [INF_NBTHREAD] = "Nbthread",
Willy Tarreau0baac8c2016-11-22 16:36:53 +010089 [INF_NBPROC] = "Nbproc",
90 [INF_PROCESS_NUM] = "Process_num",
91 [INF_PID] = "Pid",
92 [INF_UPTIME] = "Uptime",
93 [INF_UPTIME_SEC] = "Uptime_sec",
94 [INF_MEMMAX_MB] = "Memmax_MB",
95 [INF_POOL_ALLOC_MB] = "PoolAlloc_MB",
96 [INF_POOL_USED_MB] = "PoolUsed_MB",
97 [INF_POOL_FAILED] = "PoolFailed",
98 [INF_ULIMIT_N] = "Ulimit-n",
99 [INF_MAXSOCK] = "Maxsock",
100 [INF_MAXCONN] = "Maxconn",
101 [INF_HARD_MAXCONN] = "Hard_maxconn",
102 [INF_CURR_CONN] = "CurrConns",
103 [INF_CUM_CONN] = "CumConns",
104 [INF_CUM_REQ] = "CumReq",
105 [INF_MAX_SSL_CONNS] = "MaxSslConns",
106 [INF_CURR_SSL_CONNS] = "CurrSslConns",
107 [INF_CUM_SSL_CONNS] = "CumSslConns",
108 [INF_MAXPIPES] = "Maxpipes",
109 [INF_PIPES_USED] = "PipesUsed",
110 [INF_PIPES_FREE] = "PipesFree",
111 [INF_CONN_RATE] = "ConnRate",
112 [INF_CONN_RATE_LIMIT] = "ConnRateLimit",
113 [INF_MAX_CONN_RATE] = "MaxConnRate",
114 [INF_SESS_RATE] = "SessRate",
115 [INF_SESS_RATE_LIMIT] = "SessRateLimit",
116 [INF_MAX_SESS_RATE] = "MaxSessRate",
117 [INF_SSL_RATE] = "SslRate",
118 [INF_SSL_RATE_LIMIT] = "SslRateLimit",
119 [INF_MAX_SSL_RATE] = "MaxSslRate",
120 [INF_SSL_FRONTEND_KEY_RATE] = "SslFrontendKeyRate",
121 [INF_SSL_FRONTEND_MAX_KEY_RATE] = "SslFrontendMaxKeyRate",
122 [INF_SSL_FRONTEND_SESSION_REUSE_PCT] = "SslFrontendSessionReuse_pct",
123 [INF_SSL_BACKEND_KEY_RATE] = "SslBackendKeyRate",
124 [INF_SSL_BACKEND_MAX_KEY_RATE] = "SslBackendMaxKeyRate",
125 [INF_SSL_CACHE_LOOKUPS] = "SslCacheLookups",
126 [INF_SSL_CACHE_MISSES] = "SslCacheMisses",
127 [INF_COMPRESS_BPS_IN] = "CompressBpsIn",
128 [INF_COMPRESS_BPS_OUT] = "CompressBpsOut",
129 [INF_COMPRESS_BPS_RATE_LIM] = "CompressBpsRateLim",
130 [INF_ZLIB_MEM_USAGE] = "ZlibMemUsage",
131 [INF_MAX_ZLIB_MEM_USAGE] = "MaxZlibMemUsage",
132 [INF_TASKS] = "Tasks",
133 [INF_RUN_QUEUE] = "Run_queue",
134 [INF_IDLE_PCT] = "Idle_pct",
135 [INF_NODE] = "node",
136 [INF_DESCRIPTION] = "description",
Willy Tarreau00098ea2018-11-05 14:38:13 +0100137 [INF_STOPPING] = "Stopping",
138 [INF_JOBS] = "Jobs",
William Lallemanda7199262018-11-16 16:57:20 +0100139 [INF_UNSTOPPABLE_JOBS] = "Unstoppable Jobs",
Willy Tarreau00098ea2018-11-05 14:38:13 +0100140 [INF_LISTENERS] = "Listeners",
Willy Tarreau199ad242018-11-05 16:31:22 +0100141 [INF_ACTIVE_PEERS] = "ActivePeers",
Willy Tarreau2d372c22018-11-05 17:12:27 +0100142 [INF_CONNECTED_PEERS] = "ConnectedPeers",
Willy Tarreau13ef7732018-11-12 07:25:28 +0100143 [INF_DROPPED_LOGS] = "DroppedLogs",
Willy Tarreaubeb859a2018-11-22 18:07:59 +0100144 [INF_BUSY_POLLING] = "BusyPolling",
Willy Tarreau0baac8c2016-11-22 16:36:53 +0100145};
146
William Lallemand74c24fb2016-11-21 17:18:36 +0100147const char *stat_field_names[ST_F_TOTAL_FIELDS] = {
148 [ST_F_PXNAME] = "pxname",
149 [ST_F_SVNAME] = "svname",
150 [ST_F_QCUR] = "qcur",
151 [ST_F_QMAX] = "qmax",
152 [ST_F_SCUR] = "scur",
153 [ST_F_SMAX] = "smax",
154 [ST_F_SLIM] = "slim",
155 [ST_F_STOT] = "stot",
156 [ST_F_BIN] = "bin",
157 [ST_F_BOUT] = "bout",
158 [ST_F_DREQ] = "dreq",
159 [ST_F_DRESP] = "dresp",
160 [ST_F_EREQ] = "ereq",
161 [ST_F_ECON] = "econ",
162 [ST_F_ERESP] = "eresp",
163 [ST_F_WRETR] = "wretr",
164 [ST_F_WREDIS] = "wredis",
165 [ST_F_STATUS] = "status",
166 [ST_F_WEIGHT] = "weight",
167 [ST_F_ACT] = "act",
168 [ST_F_BCK] = "bck",
169 [ST_F_CHKFAIL] = "chkfail",
170 [ST_F_CHKDOWN] = "chkdown",
171 [ST_F_LASTCHG] = "lastchg",
172 [ST_F_DOWNTIME] = "downtime",
173 [ST_F_QLIMIT] = "qlimit",
174 [ST_F_PID] = "pid",
175 [ST_F_IID] = "iid",
176 [ST_F_SID] = "sid",
177 [ST_F_THROTTLE] = "throttle",
178 [ST_F_LBTOT] = "lbtot",
179 [ST_F_TRACKED] = "tracked",
180 [ST_F_TYPE] = "type",
181 [ST_F_RATE] = "rate",
182 [ST_F_RATE_LIM] = "rate_lim",
183 [ST_F_RATE_MAX] = "rate_max",
184 [ST_F_CHECK_STATUS] = "check_status",
185 [ST_F_CHECK_CODE] = "check_code",
186 [ST_F_CHECK_DURATION] = "check_duration",
187 [ST_F_HRSP_1XX] = "hrsp_1xx",
188 [ST_F_HRSP_2XX] = "hrsp_2xx",
189 [ST_F_HRSP_3XX] = "hrsp_3xx",
190 [ST_F_HRSP_4XX] = "hrsp_4xx",
191 [ST_F_HRSP_5XX] = "hrsp_5xx",
192 [ST_F_HRSP_OTHER] = "hrsp_other",
193 [ST_F_HANAFAIL] = "hanafail",
194 [ST_F_REQ_RATE] = "req_rate",
195 [ST_F_REQ_RATE_MAX] = "req_rate_max",
196 [ST_F_REQ_TOT] = "req_tot",
197 [ST_F_CLI_ABRT] = "cli_abrt",
198 [ST_F_SRV_ABRT] = "srv_abrt",
199 [ST_F_COMP_IN] = "comp_in",
200 [ST_F_COMP_OUT] = "comp_out",
201 [ST_F_COMP_BYP] = "comp_byp",
202 [ST_F_COMP_RSP] = "comp_rsp",
203 [ST_F_LASTSESS] = "lastsess",
204 [ST_F_LAST_CHK] = "last_chk",
205 [ST_F_LAST_AGT] = "last_agt",
206 [ST_F_QTIME] = "qtime",
207 [ST_F_CTIME] = "ctime",
208 [ST_F_RTIME] = "rtime",
209 [ST_F_TTIME] = "ttime",
210 [ST_F_AGENT_STATUS] = "agent_status",
211 [ST_F_AGENT_CODE] = "agent_code",
212 [ST_F_AGENT_DURATION] = "agent_duration",
213 [ST_F_CHECK_DESC] = "check_desc",
214 [ST_F_AGENT_DESC] = "agent_desc",
215 [ST_F_CHECK_RISE] = "check_rise",
216 [ST_F_CHECK_FALL] = "check_fall",
217 [ST_F_CHECK_HEALTH] = "check_health",
218 [ST_F_AGENT_RISE] = "agent_rise",
219 [ST_F_AGENT_FALL] = "agent_fall",
220 [ST_F_AGENT_HEALTH] = "agent_health",
221 [ST_F_ADDR] = "addr",
222 [ST_F_COOKIE] = "cookie",
223 [ST_F_MODE] = "mode",
224 [ST_F_ALGO] = "algo",
225 [ST_F_CONN_RATE] = "conn_rate",
226 [ST_F_CONN_RATE_MAX] = "conn_rate_max",
227 [ST_F_CONN_TOT] = "conn_tot",
228 [ST_F_INTERCEPTED] = "intercepted",
229 [ST_F_DCON] = "dcon",
230 [ST_F_DSES] = "dses",
Tim Duesterhus3fd19732018-05-27 20:35:08 +0200231 [ST_F_WREW] = "wrew",
Willy Tarreauf1573842018-12-14 11:35:36 +0100232 [ST_F_CONNECT] = "connect",
233 [ST_F_REUSE] = "reuse",
Willy Tarreaua1214a52018-12-14 14:00:25 +0100234 [ST_F_CACHE_LOOKUPS] = "cache_lookups",
235 [ST_F_CACHE_HITS] = "cache_hits",
William Lallemand74c24fb2016-11-21 17:18:36 +0100236};
237
Willy Tarreau0baac8c2016-11-22 16:36:53 +0100238/* one line of info */
Christopher Faulet1bc04c72017-10-29 20:14:08 +0100239static THREAD_LOCAL struct field info[INF_TOTAL_FIELDS];
William Lallemand74c24fb2016-11-21 17:18:36 +0100240/* one line of stats */
Christopher Faulet1bc04c72017-10-29 20:14:08 +0100241static THREAD_LOCAL struct field stats[ST_F_TOTAL_FIELDS];
William Lallemand74c24fb2016-11-21 17:18:36 +0100242
243
Christopher Fauletef779222018-10-31 08:47:01 +0100244static int stats_putchk(struct channel *chn, struct htx *htx, struct buffer *chk)
245{
246 if (htx) {
Christopher Faulet69fc88c2019-01-07 14:27:53 +0100247 if (chk->data >= channel_htx_recv_max(chn, htx))
248 return 0;
Christopher Fauletef779222018-10-31 08:47:01 +0100249 if (!htx_add_data(htx, ist2(chk->area, chk->data)))
250 return 0;
Christopher Faulet5adbeeb2019-01-02 14:34:39 +0100251 channel_add_input(chn, chk->data);
Christopher Fauletef779222018-10-31 08:47:01 +0100252 chk->data = 0;
Christopher Fauletef779222018-10-31 08:47:01 +0100253 }
254 else {
255 if (ci_putchk(chn, chk) == -1)
256 return 0;
257 }
258 return 1;
259}
Willy Tarreau0baac8c2016-11-22 16:36:53 +0100260
Christopher Fauleted7a0662019-01-14 11:07:34 +0100261static const char *stats_scope_ptr(struct appctx *appctx, struct stream_interface *si)
262{
263 const char *p;
264
265 if (IS_HTX_STRM(si_strm(si))) {
266 struct channel *req = si_oc(si);
267 struct htx *htx = htxbuf(&req->buf);
268 struct ist uri = htx_sl_req_uri(http_find_stline(htx));
269
270 p = uri.ptr;
271 }
272 else
273 p = co_head(si_oc(si));
274
275 return p + appctx->ctx.stats.scope_str;
276}
277
William Lallemand74c24fb2016-11-21 17:18:36 +0100278/*
279 * http_stats_io_handler()
280 * -> stats_dump_stat_to_buffer() // same as above, but used for CSV or HTML
281 * -> stats_dump_csv_header() // emits the CSV headers (same as above)
Simon Horman05ee2132017-01-04 09:37:25 +0100282 * -> stats_dump_json_header() // emits the JSON headers (same as above)
William Lallemand74c24fb2016-11-21 17:18:36 +0100283 * -> stats_dump_html_head() // emits the HTML headers
284 * -> stats_dump_html_info() // emits the equivalent of "show info" at the top
285 * -> stats_dump_proxy_to_buffer() // same as above, valid for CSV and HTML
286 * -> stats_dump_html_px_hdr()
287 * -> stats_dump_fe_stats()
288 * -> stats_dump_li_stats()
289 * -> stats_dump_sv_stats()
290 * -> stats_dump_be_stats()
291 * -> stats_dump_html_px_end()
292 * -> stats_dump_html_end() // emits HTML trailer
Simon Horman05ee2132017-01-04 09:37:25 +0100293 * -> stats_dump_json_end() // emits JSON trailer
William Lallemand74c24fb2016-11-21 17:18:36 +0100294 */
295
296
297extern const char *stat_status_codes[];
298
299/* Dumps the stats CSV header to the trash buffer which. The caller is responsible
300 * for clearing it if needed.
301 * NOTE: Some tools happen to rely on the field position instead of its name,
302 * so please only append new fields at the end, never in the middle.
303 */
304static void stats_dump_csv_header()
305{
306 int field;
307
308 chunk_appendf(&trash, "# ");
309 for (field = 0; field < ST_F_TOTAL_FIELDS; field++)
310 chunk_appendf(&trash, "%s,", stat_field_names[field]);
311
312 chunk_appendf(&trash, "\n");
313}
314
315
316/* Emits a stats field without any surrounding element and properly encoded to
317 * resist CSV output. Returns non-zero on success, 0 if the buffer is full.
318 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200319int stats_emit_raw_data_field(struct buffer *out, const struct field *f)
William Lallemand74c24fb2016-11-21 17:18:36 +0100320{
321 switch (field_format(f, 0)) {
322 case FF_EMPTY: return 1;
323 case FF_S32: return chunk_appendf(out, "%d", f->u.s32);
324 case FF_U32: return chunk_appendf(out, "%u", f->u.u32);
325 case FF_S64: return chunk_appendf(out, "%lld", (long long)f->u.s64);
326 case FF_U64: return chunk_appendf(out, "%llu", (unsigned long long)f->u.u64);
327 case FF_STR: return csv_enc_append(field_str(f, 0), 1, out) != NULL;
328 default: return chunk_appendf(out, "[INCORRECT_FIELD_TYPE_%08x]", f->type);
329 }
330}
331
332/* Emits a stats field prefixed with its type. No CSV encoding is prepared, the
333 * output is supposed to be used on its own line. Returns non-zero on success, 0
334 * if the buffer is full.
335 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200336int stats_emit_typed_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, "s32:%d", f->u.s32);
341 case FF_U32: return chunk_appendf(out, "u32:%u", f->u.u32);
342 case FF_S64: return chunk_appendf(out, "s64:%lld", (long long)f->u.s64);
343 case FF_U64: return chunk_appendf(out, "u64:%llu", (unsigned long long)f->u.u64);
344 case FF_STR: return chunk_appendf(out, "str:%s", field_str(f, 0));
345 default: return chunk_appendf(out, "%08x:?", f->type);
346 }
347}
348
Simon Horman05ee2132017-01-04 09:37:25 +0100349/* Limit JSON integer values to the range [-(2**53)+1, (2**53)-1] as per
350 * the recommendation for interoperable integers in section 6 of RFC 7159.
351 */
352#define JSON_INT_MAX ((1ULL << 53) - 1)
353#define JSON_INT_MIN (0 - JSON_INT_MAX)
354
355/* Emits a stats field value and its type in JSON.
356 * Returns non-zero on success, 0 on error.
357 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200358int stats_emit_json_data_field(struct buffer *out, const struct field *f)
Simon Horman05ee2132017-01-04 09:37:25 +0100359{
360 int old_len;
361 char buf[20];
362 const char *type, *value = buf, *quote = "";
363
364 switch (field_format(f, 0)) {
365 case FF_EMPTY: return 1;
366 case FF_S32: type = "\"s32\"";
367 snprintf(buf, sizeof(buf), "%d", f->u.s32);
368 break;
369 case FF_U32: type = "\"u32\"";
370 snprintf(buf, sizeof(buf), "%u", f->u.u32);
371 break;
372 case FF_S64: type = "\"s64\"";
373 if (f->u.s64 < JSON_INT_MIN || f->u.s64 > JSON_INT_MAX)
374 return 0;
375 type = "\"s64\"";
376 snprintf(buf, sizeof(buf), "%lld", (long long)f->u.s64);
377 break;
378 case FF_U64: if (f->u.u64 > JSON_INT_MAX)
379 return 0;
380 type = "\"u64\"";
381 snprintf(buf, sizeof(buf), "%llu",
382 (unsigned long long) f->u.u64);
383 break;
384 case FF_STR: type = "\"str\"";
385 value = field_str(f, 0);
386 quote = "\"";
387 break;
388 default: snprintf(buf, sizeof(buf), "%u", f->type);
389 type = buf;
390 value = "unknown";
391 quote = "\"";
392 break;
393 }
394
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200395 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100396 chunk_appendf(out, ",\"value\":{\"type\":%s,\"value\":%s%s%s}",
397 type, quote, value, quote);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200398 return !(old_len == out->data);
Simon Horman05ee2132017-01-04 09:37:25 +0100399}
400
William Lallemand74c24fb2016-11-21 17:18:36 +0100401/* Emits an encoding of the field type on 3 characters followed by a delimiter.
402 * Returns non-zero on success, 0 if the buffer is full.
403 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200404int stats_emit_field_tags(struct buffer *out, const struct field *f,
405 char delim)
William Lallemand74c24fb2016-11-21 17:18:36 +0100406{
407 char origin, nature, scope;
408
409 switch (field_origin(f, 0)) {
410 case FO_METRIC: origin = 'M'; break;
411 case FO_STATUS: origin = 'S'; break;
412 case FO_KEY: origin = 'K'; break;
413 case FO_CONFIG: origin = 'C'; break;
414 case FO_PRODUCT: origin = 'P'; break;
415 default: origin = '?'; break;
416 }
417
418 switch (field_nature(f, 0)) {
419 case FN_GAUGE: nature = 'G'; break;
420 case FN_LIMIT: nature = 'L'; break;
421 case FN_MIN: nature = 'm'; break;
422 case FN_MAX: nature = 'M'; break;
423 case FN_RATE: nature = 'R'; break;
424 case FN_COUNTER: nature = 'C'; break;
425 case FN_DURATION: nature = 'D'; break;
426 case FN_AGE: nature = 'A'; break;
427 case FN_TIME: nature = 'T'; break;
428 case FN_NAME: nature = 'N'; break;
429 case FN_OUTPUT: nature = 'O'; break;
430 case FN_AVG: nature = 'a'; break;
431 default: nature = '?'; break;
432 }
433
434 switch (field_scope(f, 0)) {
435 case FS_PROCESS: scope = 'P'; break;
436 case FS_SERVICE: scope = 'S'; break;
437 case FS_SYSTEM: scope = 's'; break;
438 case FS_CLUSTER: scope = 'C'; break;
439 default: scope = '?'; break;
440 }
441
442 return chunk_appendf(out, "%c%c%c%c", origin, nature, scope, delim);
443}
444
Simon Horman05ee2132017-01-04 09:37:25 +0100445/* Emits an encoding of the field type as JSON.
446 * Returns non-zero on success, 0 if the buffer is full.
447 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200448int stats_emit_json_field_tags(struct buffer *out, const struct field *f)
Simon Horman05ee2132017-01-04 09:37:25 +0100449{
450 const char *origin, *nature, *scope;
451 int old_len;
452
453 switch (field_origin(f, 0)) {
454 case FO_METRIC: origin = "Metric"; break;
455 case FO_STATUS: origin = "Status"; break;
456 case FO_KEY: origin = "Key"; break;
457 case FO_CONFIG: origin = "Config"; break;
458 case FO_PRODUCT: origin = "Product"; break;
459 default: origin = "Unknown"; break;
460 }
461
462 switch (field_nature(f, 0)) {
463 case FN_GAUGE: nature = "Gauge"; break;
464 case FN_LIMIT: nature = "Limit"; break;
465 case FN_MIN: nature = "Min"; break;
466 case FN_MAX: nature = "Max"; break;
467 case FN_RATE: nature = "Rate"; break;
468 case FN_COUNTER: nature = "Counter"; break;
469 case FN_DURATION: nature = "Duration"; break;
470 case FN_AGE: nature = "Age"; break;
471 case FN_TIME: nature = "Time"; break;
472 case FN_NAME: nature = "Name"; break;
473 case FN_OUTPUT: nature = "Output"; break;
474 case FN_AVG: nature = "Avg"; break;
475 default: nature = "Unknown"; break;
476 }
477
478 switch (field_scope(f, 0)) {
479 case FS_PROCESS: scope = "Process"; break;
480 case FS_SERVICE: scope = "Service"; break;
481 case FS_SYSTEM: scope = "System"; break;
482 case FS_CLUSTER: scope = "Cluster"; break;
483 default: scope = "Unknown"; break;
484 }
485
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200486 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100487 chunk_appendf(out, "\"tags\":{"
488 "\"origin\":\"%s\","
489 "\"nature\":\"%s\","
490 "\"scope\":\"%s\""
491 "}", origin, nature, scope);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200492 return !(old_len == out->data);
Simon Horman05ee2132017-01-04 09:37:25 +0100493}
William Lallemand74c24fb2016-11-21 17:18:36 +0100494
495/* Dump all fields from <stats> into <out> using CSV format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200496static int stats_dump_fields_csv(struct buffer *out,
497 const struct field *stats)
William Lallemand74c24fb2016-11-21 17:18:36 +0100498{
499 int field;
500
501 for (field = 0; field < ST_F_TOTAL_FIELDS; field++) {
502 if (!stats_emit_raw_data_field(out, &stats[field]))
503 return 0;
504 if (!chunk_strcat(out, ","))
505 return 0;
506 }
507 chunk_strcat(out, "\n");
508 return 1;
509}
510
511/* Dump all fields from <stats> into <out> using a typed "field:desc:type:value" format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200512static int stats_dump_fields_typed(struct buffer *out,
513 const struct field *stats)
William Lallemand74c24fb2016-11-21 17:18:36 +0100514{
515 int field;
516
517 for (field = 0; field < ST_F_TOTAL_FIELDS; field++) {
518 if (!stats[field].type)
519 continue;
520
521 chunk_appendf(out, "%c.%u.%u.%d.%s.%u:",
522 stats[ST_F_TYPE].u.u32 == STATS_TYPE_FE ? 'F' :
523 stats[ST_F_TYPE].u.u32 == STATS_TYPE_BE ? 'B' :
524 stats[ST_F_TYPE].u.u32 == STATS_TYPE_SO ? 'L' :
525 stats[ST_F_TYPE].u.u32 == STATS_TYPE_SV ? 'S' :
526 '?',
527 stats[ST_F_IID].u.u32, stats[ST_F_SID].u.u32,
528 field, stat_field_names[field], stats[ST_F_PID].u.u32);
529
530 if (!stats_emit_field_tags(out, &stats[field], ':'))
531 return 0;
532 if (!stats_emit_typed_data_field(out, &stats[field]))
533 return 0;
534 if (!chunk_strcat(out, "\n"))
535 return 0;
536 }
537 return 1;
538}
539
Simon Horman05ee2132017-01-04 09:37:25 +0100540/* Dump all fields from <stats> into <out> using the "show info json" format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200541static int stats_dump_json_info_fields(struct buffer *out,
Simon Horman05ee2132017-01-04 09:37:25 +0100542 const struct field *info)
543{
544 int field;
545 int started = 0;
546
547 if (!chunk_strcat(out, "["))
548 return 0;
549
550 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
551 int old_len;
552
553 if (!field_format(info, field))
554 continue;
555
556 if (started && !chunk_strcat(out, ","))
557 goto err;
558 started = 1;
559
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200560 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100561 chunk_appendf(out,
562 "{\"field\":{\"pos\":%d,\"name\":\"%s\"},"
563 "\"processNum\":%u,",
564 field, info_field_names[field],
565 info[INF_PROCESS_NUM].u.u32);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200566 if (old_len == out->data)
Simon Horman05ee2132017-01-04 09:37:25 +0100567 goto err;
568
569 if (!stats_emit_json_field_tags(out, &info[field]))
570 goto err;
571
572 if (!stats_emit_json_data_field(out, &info[field]))
573 goto err;
574
575 if (!chunk_strcat(out, "}"))
576 goto err;
577 }
578
579 if (!chunk_strcat(out, "]"))
580 goto err;
581 return 1;
582
583err:
584 chunk_reset(out);
585 chunk_appendf(out, "{\"errorStr\":\"output buffer too short\"}");
586 return 0;
587}
588
589/* Dump all fields from <stats> into <out> using a typed "field:desc:type:value" format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200590static int stats_dump_fields_json(struct buffer *out,
591 const struct field *stats,
Simon Horman05ee2132017-01-04 09:37:25 +0100592 int first_stat)
593{
594 int field;
595 int started = 0;
596
597 if (!first_stat && !chunk_strcat(out, ","))
598 return 0;
599 if (!chunk_strcat(out, "["))
600 return 0;
601
602 for (field = 0; field < ST_F_TOTAL_FIELDS; field++) {
603 const char *obj_type;
604 int old_len;
605
606 if (!stats[field].type)
607 continue;
608
609 if (started && !chunk_strcat(out, ","))
610 goto err;
611 started = 1;
612
613 switch (stats[ST_F_TYPE].u.u32) {
614 case STATS_TYPE_FE: obj_type = "Frontend"; break;
615 case STATS_TYPE_BE: obj_type = "Backend"; break;
616 case STATS_TYPE_SO: obj_type = "Listener"; break;
617 case STATS_TYPE_SV: obj_type = "Server"; break;
618 default: obj_type = "Unknown"; break;
619 }
620
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200621 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100622 chunk_appendf(out,
623 "{"
624 "\"objType\":\"%s\","
625 "\"proxyId\":%d,"
626 "\"id\":%d,"
627 "\"field\":{\"pos\":%d,\"name\":\"%s\"},"
628 "\"processNum\":%u,",
629 obj_type, stats[ST_F_IID].u.u32,
630 stats[ST_F_SID].u.u32, field,
631 stat_field_names[field], stats[ST_F_PID].u.u32);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200632 if (old_len == out->data)
Simon Horman05ee2132017-01-04 09:37:25 +0100633 goto err;
634
635 if (!stats_emit_json_field_tags(out, &stats[field]))
636 goto err;
637
638 if (!stats_emit_json_data_field(out, &stats[field]))
639 goto err;
640
641 if (!chunk_strcat(out, "}"))
642 goto err;
643 }
644
645 if (!chunk_strcat(out, "]"))
646 goto err;
647
648 return 1;
649
650err:
651 chunk_reset(out);
652 if (!first_stat)
653 chunk_strcat(out, ",");
654 chunk_appendf(out, "{\"errorStr\":\"output buffer too short\"}");
655 return 0;
656}
657
William Lallemand74c24fb2016-11-21 17:18:36 +0100658/* Dump all fields from <stats> into <out> using the HTML format. A column is
659 * reserved for the checkbox is ST_SHOWADMIN is set in <flags>. Some extra info
660 * are provided if ST_SHLGNDS is present in <flags>.
661 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200662static int stats_dump_fields_html(struct buffer *out,
663 const struct field *stats,
664 unsigned int flags)
William Lallemand74c24fb2016-11-21 17:18:36 +0100665{
Willy Tarreau83061a82018-07-13 11:56:34 +0200666 struct buffer src;
William Lallemand74c24fb2016-11-21 17:18:36 +0100667
668 if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_FE) {
669 chunk_appendf(out,
670 /* name, queue */
671 "<tr class=\"frontend\">");
672
673 if (flags & ST_SHOWADMIN) {
674 /* Column sub-heading for Enable or Disable server */
675 chunk_appendf(out, "<td></td>");
676 }
677
678 chunk_appendf(out,
679 "<td class=ac>"
680 "<a name=\"%s/Frontend\"></a>"
681 "<a class=lfsb href=\"#%s/Frontend\">Frontend</a></td>"
682 "<td colspan=3></td>"
683 "",
684 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_PXNAME));
685
686 chunk_appendf(out,
687 /* sessions rate : current */
688 "<td><u>%s<div class=tips><table class=det>"
689 "<tr><th>Current connection rate:</th><td>%s/s</td></tr>"
690 "<tr><th>Current session rate:</th><td>%s/s</td></tr>"
691 "",
692 U2H(stats[ST_F_RATE].u.u32),
693 U2H(stats[ST_F_CONN_RATE].u.u32),
694 U2H(stats[ST_F_RATE].u.u32));
695
696 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
697 chunk_appendf(out,
698 "<tr><th>Current request rate:</th><td>%s/s</td></tr>",
699 U2H(stats[ST_F_REQ_RATE].u.u32));
700
701 chunk_appendf(out,
702 "</table></div></u></td>"
703 /* sessions rate : max */
704 "<td><u>%s<div class=tips><table class=det>"
705 "<tr><th>Max connection rate:</th><td>%s/s</td></tr>"
706 "<tr><th>Max session rate:</th><td>%s/s</td></tr>"
707 "",
708 U2H(stats[ST_F_RATE_MAX].u.u32),
709 U2H(stats[ST_F_CONN_RATE_MAX].u.u32),
710 U2H(stats[ST_F_RATE_MAX].u.u32));
711
712 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
713 chunk_appendf(out,
714 "<tr><th>Max request rate:</th><td>%s/s</td></tr>",
715 U2H(stats[ST_F_REQ_RATE_MAX].u.u32));
716
717 chunk_appendf(out,
718 "</table></div></u></td>"
719 /* sessions rate : limit */
720 "<td>%s</td>",
721 LIM2A(stats[ST_F_RATE_LIM].u.u32, "-"));
722
723 chunk_appendf(out,
724 /* sessions: current, max, limit, total */
725 "<td>%s</td><td>%s</td><td>%s</td>"
726 "<td><u>%s<div class=tips><table class=det>"
727 "<tr><th>Cum. connections:</th><td>%s</td></tr>"
728 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
729 "",
730 U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SMAX].u.u32), U2H(stats[ST_F_SLIM].u.u32),
731 U2H(stats[ST_F_STOT].u.u64),
732 U2H(stats[ST_F_CONN_TOT].u.u64),
733 U2H(stats[ST_F_STOT].u.u64));
734
735 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
736 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
737 chunk_appendf(out,
738 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
739 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
740 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
741 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
742 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
743 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
744 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
745 "<tr><th>- other responses:</th><td>%s</td></tr>"
746 "<tr><th>Intercepted requests:</th><td>%s</td></tr>"
Willy Tarreaua1214a52018-12-14 14:00:25 +0100747 "<tr><th>Cache lookups:</th><td>%s</td></tr>"
748 "<tr><th>Cache hits:</th><td>%s</td><td>(%d%%)</td></tr>"
Willy Tarreau1b0f85e2018-05-28 15:12:40 +0200749 "<tr><th>Failed hdr rewrites:</th><td>%s</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +0100750 "",
751 U2H(stats[ST_F_REQ_TOT].u.u64),
752 U2H(stats[ST_F_HRSP_1XX].u.u64),
753 U2H(stats[ST_F_HRSP_2XX].u.u64),
754 U2H(stats[ST_F_COMP_RSP].u.u64),
755 stats[ST_F_HRSP_2XX].u.u64 ?
756 (int)(100 * stats[ST_F_COMP_RSP].u.u64 / stats[ST_F_HRSP_2XX].u.u64) : 0,
757 U2H(stats[ST_F_HRSP_3XX].u.u64),
758 U2H(stats[ST_F_HRSP_4XX].u.u64),
759 U2H(stats[ST_F_HRSP_5XX].u.u64),
760 U2H(stats[ST_F_HRSP_OTHER].u.u64),
Willy Tarreau1b0f85e2018-05-28 15:12:40 +0200761 U2H(stats[ST_F_INTERCEPTED].u.u64),
Willy Tarreaua1214a52018-12-14 14:00:25 +0100762 U2H(stats[ST_F_CACHE_LOOKUPS].u.u64),
763 U2H(stats[ST_F_CACHE_HITS].u.u64),
764 stats[ST_F_CACHE_LOOKUPS].u.u64 ?
765 (int)(100 * stats[ST_F_CACHE_HITS].u.u64 / stats[ST_F_CACHE_LOOKUPS].u.u64) : 0,
Willy Tarreau1b0f85e2018-05-28 15:12:40 +0200766 U2H(stats[ST_F_WREW].u.u64));
William Lallemand74c24fb2016-11-21 17:18:36 +0100767 }
768
769 chunk_appendf(out,
770 "</table></div></u></td>"
771 /* sessions: lbtot, lastsess */
772 "<td></td><td></td>"
773 /* bytes : in */
774 "<td>%s</td>"
775 "",
776 U2H(stats[ST_F_BIN].u.u64));
777
778 chunk_appendf(out,
779 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
780 "<td>%s%s<div class=tips><table class=det>"
781 "<tr><th>Response bytes in:</th><td>%s</td></tr>"
782 "<tr><th>Compression in:</th><td>%s</td></tr>"
783 "<tr><th>Compression out:</th><td>%s</td><td>(%d%%)</td></tr>"
784 "<tr><th>Compression bypass:</th><td>%s</td></tr>"
785 "<tr><th>Total bytes saved:</th><td>%s</td><td>(%d%%)</td></tr>"
786 "</table></div>%s</td>",
787 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "<u>":"",
788 U2H(stats[ST_F_BOUT].u.u64),
789 U2H(stats[ST_F_BOUT].u.u64),
790 U2H(stats[ST_F_COMP_IN].u.u64),
791 U2H(stats[ST_F_COMP_OUT].u.u64),
792 stats[ST_F_COMP_IN].u.u64 ? (int)(stats[ST_F_COMP_OUT].u.u64 * 100 / stats[ST_F_COMP_IN].u.u64) : 0,
793 U2H(stats[ST_F_COMP_BYP].u.u64),
794 U2H(stats[ST_F_COMP_IN].u.u64 - stats[ST_F_COMP_OUT].u.u64),
795 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,
796 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "</u>":"");
797
798 chunk_appendf(out,
799 /* denied: req, resp */
800 "<td>%s</td><td>%s</td>"
801 /* errors : request, connect, response */
802 "<td>%s</td><td></td><td></td>"
803 /* warnings: retries, redispatches */
804 "<td></td><td></td>"
805 /* server status : reflect frontend status */
806 "<td class=ac>%s</td>"
807 /* rest of server: nothing */
808 "<td class=ac colspan=8></td></tr>"
809 "",
810 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
811 U2H(stats[ST_F_EREQ].u.u64),
812 field_str(stats, ST_F_STATUS));
813 }
814 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_SO) {
815 chunk_appendf(out, "<tr class=socket>");
816 if (flags & ST_SHOWADMIN) {
817 /* Column sub-heading for Enable or Disable server */
818 chunk_appendf(out, "<td></td>");
819 }
820
821 chunk_appendf(out,
822 /* frontend name, listener name */
823 "<td class=ac><a name=\"%s/+%s\"></a>%s"
824 "<a class=lfsb href=\"#%s/+%s\">%s</a>"
825 "",
826 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME),
827 (flags & ST_SHLGNDS)?"<u>":"",
828 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME), field_str(stats, ST_F_SVNAME));
829
830 if (flags & ST_SHLGNDS) {
831 chunk_appendf(out, "<div class=tips>");
832
833 if (isdigit(*field_str(stats, ST_F_ADDR)))
834 chunk_appendf(out, "IPv4: %s, ", field_str(stats, ST_F_ADDR));
835 else if (*field_str(stats, ST_F_ADDR) == '[')
836 chunk_appendf(out, "IPv6: %s, ", field_str(stats, ST_F_ADDR));
837 else if (*field_str(stats, ST_F_ADDR))
838 chunk_appendf(out, "%s, ", field_str(stats, ST_F_ADDR));
839
840 /* id */
841 chunk_appendf(out, "id: %d</div>", stats[ST_F_SID].u.u32);
842 }
843
844 chunk_appendf(out,
845 /* queue */
846 "%s</td><td colspan=3></td>"
847 /* sessions rate: current, max, limit */
848 "<td colspan=3>&nbsp;</td>"
849 /* sessions: current, max, limit, total, lbtot, lastsess */
850 "<td>%s</td><td>%s</td><td>%s</td>"
851 "<td>%s</td><td>&nbsp;</td><td>&nbsp;</td>"
852 /* bytes: in, out */
853 "<td>%s</td><td>%s</td>"
854 "",
855 (flags & ST_SHLGNDS)?"</u>":"",
856 U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SMAX].u.u32), U2H(stats[ST_F_SLIM].u.u32),
857 U2H(stats[ST_F_STOT].u.u64), U2H(stats[ST_F_BIN].u.u64), U2H(stats[ST_F_BOUT].u.u64));
858
859 chunk_appendf(out,
860 /* denied: req, resp */
861 "<td>%s</td><td>%s</td>"
862 /* errors: request, connect, response */
863 "<td>%s</td><td></td><td></td>"
864 /* warnings: retries, redispatches */
865 "<td></td><td></td>"
866 /* server status: reflect listener status */
867 "<td class=ac>%s</td>"
868 /* rest of server: nothing */
869 "<td class=ac colspan=8></td></tr>"
870 "",
871 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
872 U2H(stats[ST_F_EREQ].u.u64),
873 field_str(stats, ST_F_STATUS));
874 }
875 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_SV) {
876 const char *style;
877
878 /* determine the style to use depending on the server's state,
879 * its health and weight. There isn't a 1-to-1 mapping between
880 * state and styles for the cases where the server is (still)
881 * up. The reason is that we don't want to report nolb and
882 * drain with the same color.
883 */
884
885 if (strcmp(field_str(stats, ST_F_STATUS), "DOWN") == 0 ||
886 strcmp(field_str(stats, ST_F_STATUS), "DOWN (agent)") == 0) {
887 style = "down";
888 }
889 else if (strcmp(field_str(stats, ST_F_STATUS), "DOWN ") == 0) {
890 style = "going_up";
891 }
892 else if (strcmp(field_str(stats, ST_F_STATUS), "NOLB ") == 0) {
893 style = "going_down";
894 }
895 else if (strcmp(field_str(stats, ST_F_STATUS), "NOLB") == 0) {
896 style = "nolb";
897 }
898 else if (strcmp(field_str(stats, ST_F_STATUS), "no check") == 0) {
899 style = "no_check";
900 }
901 else if (!stats[ST_F_CHKFAIL].type ||
902 stats[ST_F_CHECK_HEALTH].u.u32 == stats[ST_F_CHECK_RISE].u.u32 + stats[ST_F_CHECK_FALL].u.u32 - 1) {
903 /* no check or max health = UP */
904 if (stats[ST_F_WEIGHT].u.u32)
905 style = "up";
906 else
907 style = "draining";
908 }
909 else {
910 style = "going_down";
911 }
912
913 if (memcmp(field_str(stats, ST_F_STATUS), "MAINT", 5) == 0)
914 chunk_appendf(out, "<tr class=\"maintain\">");
915 else
916 chunk_appendf(out,
917 "<tr class=\"%s_%s\">",
918 (stats[ST_F_BCK].u.u32) ? "backup" : "active", style);
919
920
921 if (flags & ST_SHOWADMIN)
922 chunk_appendf(out,
David Harrigand3db35a2016-12-30 12:12:49 +0000923 "<td><input class='%s-checkbox' type=\"checkbox\" name=\"s\" value=\"%s\"></td>",
924 field_str(stats, ST_F_PXNAME),
William Lallemand74c24fb2016-11-21 17:18:36 +0100925 field_str(stats, ST_F_SVNAME));
926
927 chunk_appendf(out,
928 "<td class=ac><a name=\"%s/%s\"></a>%s"
929 "<a class=lfsb href=\"#%s/%s\">%s</a>"
930 "",
931 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME),
932 (flags & ST_SHLGNDS) ? "<u>" : "",
933 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME), field_str(stats, ST_F_SVNAME));
934
935 if (flags & ST_SHLGNDS) {
936 chunk_appendf(out, "<div class=tips>");
937
938 if (isdigit(*field_str(stats, ST_F_ADDR)))
939 chunk_appendf(out, "IPv4: %s, ", field_str(stats, ST_F_ADDR));
940 else if (*field_str(stats, ST_F_ADDR) == '[')
941 chunk_appendf(out, "IPv6: %s, ", field_str(stats, ST_F_ADDR));
942 else if (*field_str(stats, ST_F_ADDR))
943 chunk_appendf(out, "%s, ", field_str(stats, ST_F_ADDR));
944
945 /* id */
946 chunk_appendf(out, "id: %d", stats[ST_F_SID].u.u32);
947
948 /* cookie */
949 if (stats[ST_F_COOKIE].type) {
950 chunk_appendf(out, ", cookie: '");
951 chunk_initstr(&src, field_str(stats, ST_F_COOKIE));
952 chunk_htmlencode(out, &src);
953 chunk_appendf(out, "'");
954 }
955
956 chunk_appendf(out, "</div>");
957 }
958
959 chunk_appendf(out,
960 /* queue : current, max, limit */
961 "%s</td><td>%s</td><td>%s</td><td>%s</td>"
962 /* sessions rate : current, max, limit */
963 "<td>%s</td><td>%s</td><td></td>"
964 "",
965 (flags & ST_SHLGNDS) ? "</u>" : "",
966 U2H(stats[ST_F_QCUR].u.u32), U2H(stats[ST_F_QMAX].u.u32), LIM2A(stats[ST_F_QLIMIT].u.u32, "-"),
967 U2H(stats[ST_F_RATE].u.u32), U2H(stats[ST_F_RATE_MAX].u.u32));
968
969 chunk_appendf(out,
970 /* sessions: current, max, limit, total */
971 "<td>%s</td><td>%s</td><td>%s</td>"
972 "<td><u>%s<div class=tips><table class=det>"
973 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
974 "",
975 U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SMAX].u.u32), LIM2A(stats[ST_F_SLIM].u.u32, "-"),
976 U2H(stats[ST_F_STOT].u.u64),
977 U2H(stats[ST_F_STOT].u.u64));
978
979 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
980 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
981 unsigned long long tot;
982
983 tot = stats[ST_F_HRSP_OTHER].u.u64;
984 tot += stats[ST_F_HRSP_1XX].u.u64;
985 tot += stats[ST_F_HRSP_2XX].u.u64;
986 tot += stats[ST_F_HRSP_3XX].u.u64;
987 tot += stats[ST_F_HRSP_4XX].u.u64;
988 tot += stats[ST_F_HRSP_5XX].u.u64;
989
990 chunk_appendf(out,
Willy Tarreauf1573842018-12-14 11:35:36 +0100991 "<tr><th>New connections:</th><td>%s</td></tr>"
992 "<tr><th>Reused connections:</th><td>%s</td><td>(%d%%)</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +0100993 "<tr><th>Cum. HTTP responses:</th><td>%s</td></tr>"
994 "<tr><th>- HTTP 1xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
995 "<tr><th>- HTTP 2xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
996 "<tr><th>- HTTP 3xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
997 "<tr><th>- HTTP 4xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
998 "<tr><th>- HTTP 5xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
999 "<tr><th>- other responses:</th><td>%s</td><td>(%d%%)</td></tr>"
Willy Tarreau1b0f85e2018-05-28 15:12:40 +02001000 "<tr><th>Failed hdr rewrites:</th><td>%s</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001001 "",
Willy Tarreauf1573842018-12-14 11:35:36 +01001002 U2H(stats[ST_F_CONNECT].u.u64),
1003 U2H(stats[ST_F_REUSE].u.u64),
1004 (stats[ST_F_CONNECT].u.u64 + stats[ST_F_REUSE].u.u64) ?
1005 (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 +01001006 U2H(tot),
1007 U2H(stats[ST_F_HRSP_1XX].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_1XX].u.u64 / tot) : 0,
1008 U2H(stats[ST_F_HRSP_2XX].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_2XX].u.u64 / tot) : 0,
1009 U2H(stats[ST_F_HRSP_3XX].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_3XX].u.u64 / tot) : 0,
1010 U2H(stats[ST_F_HRSP_4XX].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_4XX].u.u64 / tot) : 0,
1011 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 +02001012 U2H(stats[ST_F_HRSP_OTHER].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_OTHER].u.u64 / tot) : 0,
1013 U2H(stats[ST_F_WREW].u.u64));
William Lallemand74c24fb2016-11-21 17:18:36 +01001014 }
1015
1016 chunk_appendf(out, "<tr><th colspan=3>Avg over last 1024 success. conn.</th></tr>");
1017 chunk_appendf(out, "<tr><th>- Queue time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_QTIME].u.u32));
1018 chunk_appendf(out, "<tr><th>- Connect time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_CTIME].u.u32));
1019 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
1020 chunk_appendf(out, "<tr><th>- Response time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_RTIME].u.u32));
1021 chunk_appendf(out, "<tr><th>- Total time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_TTIME].u.u32));
1022
1023 chunk_appendf(out,
1024 "</table></div></u></td>"
1025 /* sessions: lbtot, last */
1026 "<td>%s</td><td>%s</td>",
1027 U2H(stats[ST_F_LBTOT].u.u64),
1028 human_time(stats[ST_F_LASTSESS].u.s32, 1));
1029
1030 chunk_appendf(out,
1031 /* bytes : in, out */
1032 "<td>%s</td><td>%s</td>"
1033 /* denied: req, resp */
1034 "<td></td><td>%s</td>"
1035 /* errors : request, connect */
1036 "<td></td><td>%s</td>"
1037 /* errors : response */
1038 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
1039 /* warnings: retries, redispatches */
1040 "<td>%lld</td><td>%lld</td>"
1041 "",
1042 U2H(stats[ST_F_BIN].u.u64), U2H(stats[ST_F_BOUT].u.u64),
1043 U2H(stats[ST_F_DRESP].u.u64),
1044 U2H(stats[ST_F_ECON].u.u64),
1045 U2H(stats[ST_F_ERESP].u.u64),
1046 (long long)stats[ST_F_CLI_ABRT].u.u64,
1047 (long long)stats[ST_F_SRV_ABRT].u.u64,
1048 (long long)stats[ST_F_WRETR].u.u64,
1049 (long long)stats[ST_F_WREDIS].u.u64);
1050
1051 /* status, last change */
1052 chunk_appendf(out, "<td class=ac>");
1053
1054 /* FIXME!!!!
1055 * LASTCHG should contain the last change for *this* server and must be computed
1056 * properly above, as was done below, ie: this server if maint, otherwise ref server
1057 * if tracking. Note that ref is either local or remote depending on tracking.
1058 */
1059
1060
1061 if (memcmp(field_str(stats, ST_F_STATUS), "MAINT", 5) == 0) {
1062 chunk_appendf(out, "%s MAINT", human_time(stats[ST_F_LASTCHG].u.u32, 1));
1063 }
1064 else if (memcmp(field_str(stats, ST_F_STATUS), "no check", 5) == 0) {
1065 chunk_strcat(out, "<i>no check</i>");
1066 }
1067 else {
1068 chunk_appendf(out, "%s %s", human_time(stats[ST_F_LASTCHG].u.u32, 1), field_str(stats, ST_F_STATUS));
1069 if (memcmp(field_str(stats, ST_F_STATUS), "DOWN", 4) == 0) {
1070 if (stats[ST_F_CHECK_HEALTH].u.u32)
1071 chunk_strcat(out, " &uarr;");
1072 }
1073 else if (stats[ST_F_CHECK_HEALTH].u.u32 < stats[ST_F_CHECK_RISE].u.u32 + stats[ST_F_CHECK_FALL].u.u32 - 1)
1074 chunk_strcat(out, " &darr;");
1075 }
1076
1077 if (memcmp(field_str(stats, ST_F_STATUS), "DOWN", 4) == 0 &&
1078 stats[ST_F_AGENT_STATUS].type && !stats[ST_F_AGENT_HEALTH].u.u32) {
1079 chunk_appendf(out,
1080 "</td><td class=ac><u> %s",
1081 field_str(stats, ST_F_AGENT_STATUS));
1082
1083 if (stats[ST_F_AGENT_CODE].type)
1084 chunk_appendf(out, "/%d", stats[ST_F_AGENT_CODE].u.u32);
1085
1086 if (stats[ST_F_AGENT_DURATION].type)
1087 chunk_appendf(out, " in %lums", (long)stats[ST_F_AGENT_DURATION].u.u64);
1088
1089 chunk_appendf(out, "<div class=tips>%s", field_str(stats, ST_F_AGENT_DESC));
1090
1091 if (*field_str(stats, ST_F_LAST_AGT)) {
1092 chunk_appendf(out, ": ");
1093 chunk_initstr(&src, field_str(stats, ST_F_LAST_AGT));
1094 chunk_htmlencode(out, &src);
1095 }
1096 chunk_appendf(out, "</div></u>");
1097 }
1098 else if (stats[ST_F_CHECK_STATUS].type) {
1099 chunk_appendf(out,
1100 "</td><td class=ac><u> %s",
1101 field_str(stats, ST_F_CHECK_STATUS));
1102
1103 if (stats[ST_F_CHECK_CODE].type)
1104 chunk_appendf(out, "/%d", stats[ST_F_CHECK_CODE].u.u32);
1105
1106 if (stats[ST_F_CHECK_DURATION].type)
1107 chunk_appendf(out, " in %lums", (long)stats[ST_F_CHECK_DURATION].u.u64);
1108
1109 chunk_appendf(out, "<div class=tips>%s", field_str(stats, ST_F_CHECK_DESC));
1110
1111 if (*field_str(stats, ST_F_LAST_CHK)) {
1112 chunk_appendf(out, ": ");
1113 chunk_initstr(&src, field_str(stats, ST_F_LAST_CHK));
1114 chunk_htmlencode(out, &src);
1115 }
1116 chunk_appendf(out, "</div></u>");
1117 }
1118 else
1119 chunk_appendf(out, "</td><td>");
1120
1121 chunk_appendf(out,
1122 /* weight */
1123 "</td><td class=ac>%d</td>"
1124 /* act, bck */
1125 "<td class=ac>%s</td><td class=ac>%s</td>"
1126 "",
1127 stats[ST_F_WEIGHT].u.u32,
1128 stats[ST_F_BCK].u.u32 ? "-" : "Y",
1129 stats[ST_F_BCK].u.u32 ? "Y" : "-");
1130
1131 /* check failures: unique, fatal, down time */
1132 if (strcmp(field_str(stats, ST_F_STATUS), "MAINT (resolution)") == 0) {
1133 chunk_appendf(out, "<td class=ac colspan=3>resolution</td>");
1134 }
1135 else if (stats[ST_F_CHKFAIL].type) {
1136 chunk_appendf(out, "<td><u>%lld", (long long)stats[ST_F_CHKFAIL].u.u64);
1137
1138 if (stats[ST_F_HANAFAIL].type)
1139 chunk_appendf(out, "/%lld", (long long)stats[ST_F_HANAFAIL].u.u64);
1140
1141 chunk_appendf(out,
1142 "<div class=tips>Failed Health Checks%s</div></u></td>"
1143 "<td>%lld</td><td>%s</td>"
1144 "",
1145 stats[ST_F_HANAFAIL].type ? "/Health Analyses" : "",
1146 (long long)stats[ST_F_CHKDOWN].u.u64, human_time(stats[ST_F_DOWNTIME].u.u32, 1));
1147 }
1148 else if (strcmp(field_str(stats, ST_F_STATUS), "MAINT") != 0 && field_format(stats, ST_F_TRACKED) == FF_STR) {
1149 /* tracking a server (hence inherited maint would appear as "MAINT (via...)" */
1150 chunk_appendf(out,
1151 "<td class=ac colspan=3><a class=lfsb href=\"#%s\">via %s</a></td>",
1152 field_str(stats, ST_F_TRACKED), field_str(stats, ST_F_TRACKED));
1153 }
1154 else
1155 chunk_appendf(out, "<td colspan=3></td>");
1156
1157 /* throttle */
1158 if (stats[ST_F_THROTTLE].type)
1159 chunk_appendf(out, "<td class=ac>%d %%</td></tr>\n", stats[ST_F_THROTTLE].u.u32);
1160 else
1161 chunk_appendf(out, "<td class=ac>-</td></tr>\n");
1162 }
1163 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_BE) {
1164 chunk_appendf(out, "<tr class=\"backend\">");
1165 if (flags & ST_SHOWADMIN) {
1166 /* Column sub-heading for Enable or Disable server */
1167 chunk_appendf(out, "<td></td>");
1168 }
1169 chunk_appendf(out,
1170 "<td class=ac>"
1171 /* name */
1172 "%s<a name=\"%s/Backend\"></a>"
1173 "<a class=lfsb href=\"#%s/Backend\">Backend</a>"
1174 "",
1175 (flags & ST_SHLGNDS)?"<u>":"",
1176 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_PXNAME));
1177
1178 if (flags & ST_SHLGNDS) {
1179 /* balancing */
1180 chunk_appendf(out, "<div class=tips>balancing: %s",
1181 field_str(stats, ST_F_ALGO));
1182
1183 /* cookie */
1184 if (stats[ST_F_COOKIE].type) {
1185 chunk_appendf(out, ", cookie: '");
1186 chunk_initstr(&src, field_str(stats, ST_F_COOKIE));
1187 chunk_htmlencode(out, &src);
1188 chunk_appendf(out, "'");
1189 }
1190 chunk_appendf(out, "</div>");
1191 }
1192
1193 chunk_appendf(out,
1194 "%s</td>"
1195 /* queue : current, max */
1196 "<td>%s</td><td>%s</td><td></td>"
1197 /* sessions rate : current, max, limit */
1198 "<td>%s</td><td>%s</td><td></td>"
1199 "",
1200 (flags & ST_SHLGNDS)?"</u>":"",
1201 U2H(stats[ST_F_QCUR].u.u32), U2H(stats[ST_F_QMAX].u.u32),
1202 U2H(stats[ST_F_RATE].u.u32), U2H(stats[ST_F_RATE_MAX].u.u32));
1203
1204 chunk_appendf(out,
1205 /* sessions: current, max, limit, total */
1206 "<td>%s</td><td>%s</td><td>%s</td>"
1207 "<td><u>%s<div class=tips><table class=det>"
1208 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
1209 "",
Willy Tarreau8e0f1752016-12-12 15:07:29 +01001210 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 +01001211 U2H(stats[ST_F_STOT].u.u64),
1212 U2H(stats[ST_F_STOT].u.u64));
1213
1214 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
1215 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
1216 chunk_appendf(out,
Willy Tarreauf1573842018-12-14 11:35:36 +01001217 "<tr><th>New connections:</th><td>%s</td></tr>"
1218 "<tr><th>Reused connections:</th><td>%s</td><td>(%d%%)</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001219 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
1220 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
1221 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
1222 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
1223 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
1224 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
1225 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
1226 "<tr><th>- other responses:</th><td>%s</td></tr>"
Willy Tarreaua1214a52018-12-14 14:00:25 +01001227 "<tr><th>Cache lookups:</th><td>%s</td></tr>"
1228 "<tr><th>Cache hits:</th><td>%s</td><td>(%d%%)</td></tr>"
Willy Tarreau1b0f85e2018-05-28 15:12:40 +02001229 "<tr><th>Failed hdr rewrites:</th><td>%s</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001230 "<tr><th colspan=3>Avg over last 1024 success. conn.</th></tr>"
1231 "",
Willy Tarreauf1573842018-12-14 11:35:36 +01001232 U2H(stats[ST_F_CONNECT].u.u64),
1233 U2H(stats[ST_F_REUSE].u.u64),
1234 (stats[ST_F_CONNECT].u.u64 + stats[ST_F_REUSE].u.u64) ?
1235 (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 +01001236 U2H(stats[ST_F_REQ_TOT].u.u64),
1237 U2H(stats[ST_F_HRSP_1XX].u.u64),
1238 U2H(stats[ST_F_HRSP_2XX].u.u64),
1239 U2H(stats[ST_F_COMP_RSP].u.u64),
1240 stats[ST_F_HRSP_2XX].u.u64 ?
1241 (int)(100 * stats[ST_F_COMP_RSP].u.u64 / stats[ST_F_HRSP_2XX].u.u64) : 0,
1242 U2H(stats[ST_F_HRSP_3XX].u.u64),
1243 U2H(stats[ST_F_HRSP_4XX].u.u64),
1244 U2H(stats[ST_F_HRSP_5XX].u.u64),
Willy Tarreaufeead3a2018-12-14 13:48:44 +01001245 U2H(stats[ST_F_HRSP_OTHER].u.u64),
Willy Tarreaua1214a52018-12-14 14:00:25 +01001246 U2H(stats[ST_F_CACHE_LOOKUPS].u.u64),
1247 U2H(stats[ST_F_CACHE_HITS].u.u64),
1248 stats[ST_F_CACHE_LOOKUPS].u.u64 ?
1249 (int)(100 * stats[ST_F_CACHE_HITS].u.u64 / stats[ST_F_CACHE_LOOKUPS].u.u64) : 0,
Willy Tarreaufeead3a2018-12-14 13:48:44 +01001250 U2H(stats[ST_F_WREW].u.u64));
William Lallemand74c24fb2016-11-21 17:18:36 +01001251 }
1252
1253 chunk_appendf(out, "<tr><th>- Queue time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_QTIME].u.u32));
1254 chunk_appendf(out, "<tr><th>- Connect time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_CTIME].u.u32));
1255 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
1256 chunk_appendf(out, "<tr><th>- Response time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_RTIME].u.u32));
1257 chunk_appendf(out, "<tr><th>- Total time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_TTIME].u.u32));
1258
1259 chunk_appendf(out,
1260 "</table></div></u></td>"
1261 /* sessions: lbtot, last */
1262 "<td>%s</td><td>%s</td>"
1263 /* bytes: in */
1264 "<td>%s</td>"
1265 "",
1266 U2H(stats[ST_F_LBTOT].u.u64),
1267 human_time(stats[ST_F_LASTSESS].u.s32, 1),
1268 U2H(stats[ST_F_BIN].u.u64));
1269
1270 chunk_appendf(out,
1271 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
1272 "<td>%s%s<div class=tips><table class=det>"
1273 "<tr><th>Response bytes in:</th><td>%s</td></tr>"
1274 "<tr><th>Compression in:</th><td>%s</td></tr>"
1275 "<tr><th>Compression out:</th><td>%s</td><td>(%d%%)</td></tr>"
1276 "<tr><th>Compression bypass:</th><td>%s</td></tr>"
1277 "<tr><th>Total bytes saved:</th><td>%s</td><td>(%d%%)</td></tr>"
1278 "</table></div>%s</td>",
1279 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "<u>":"",
1280 U2H(stats[ST_F_BOUT].u.u64),
1281 U2H(stats[ST_F_BOUT].u.u64),
1282 U2H(stats[ST_F_COMP_IN].u.u64),
1283 U2H(stats[ST_F_COMP_OUT].u.u64),
1284 stats[ST_F_COMP_IN].u.u64 ? (int)(stats[ST_F_COMP_OUT].u.u64 * 100 / stats[ST_F_COMP_IN].u.u64) : 0,
1285 U2H(stats[ST_F_COMP_BYP].u.u64),
1286 U2H(stats[ST_F_COMP_IN].u.u64 - stats[ST_F_COMP_OUT].u.u64),
1287 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,
1288 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "</u>":"");
1289
1290 chunk_appendf(out,
1291 /* denied: req, resp */
1292 "<td>%s</td><td>%s</td>"
1293 /* errors : request, connect */
1294 "<td></td><td>%s</td>"
1295 /* errors : response */
1296 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
1297 /* warnings: retries, redispatches */
1298 "<td>%lld</td><td>%lld</td>"
1299 /* backend status: reflect backend status (up/down): we display UP
1300 * if the backend has known working servers or if it has no server at
1301 * all (eg: for stats). Then we display the total weight, number of
1302 * active and backups. */
1303 "<td class=ac>%s %s</td><td class=ac>&nbsp;</td><td class=ac>%d</td>"
1304 "<td class=ac>%d</td><td class=ac>%d</td>"
1305 "",
1306 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
1307 U2H(stats[ST_F_ECON].u.u64),
1308 U2H(stats[ST_F_ERESP].u.u64),
1309 (long long)stats[ST_F_CLI_ABRT].u.u64,
1310 (long long)stats[ST_F_SRV_ABRT].u.u64,
1311 (long long)stats[ST_F_WRETR].u.u64, (long long)stats[ST_F_WREDIS].u.u64,
1312 human_time(stats[ST_F_LASTCHG].u.u32, 1),
1313 strcmp(field_str(stats, ST_F_STATUS), "DOWN") ? field_str(stats, ST_F_STATUS) : "<font color=\"red\"><b>DOWN</b></font>",
1314 stats[ST_F_WEIGHT].u.u32,
1315 stats[ST_F_ACT].u.u32, stats[ST_F_BCK].u.u32);
1316
1317 chunk_appendf(out,
1318 /* rest of backend: nothing, down transitions, total downtime, throttle */
1319 "<td class=ac>&nbsp;</td><td>%d</td>"
1320 "<td>%s</td>"
1321 "<td></td>"
1322 "</tr>",
1323 stats[ST_F_CHKDOWN].u.u32,
1324 stats[ST_F_DOWNTIME].type ? human_time(stats[ST_F_DOWNTIME].u.u32, 1) : "&nbsp;");
1325 }
1326 return 1;
1327}
1328
1329int stats_dump_one_line(const struct field *stats, unsigned int flags, struct proxy *px, struct appctx *appctx)
1330{
Simon Horman05ee2132017-01-04 09:37:25 +01001331 int ret;
1332
William Lallemand74c24fb2016-11-21 17:18:36 +01001333 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN))
1334 flags |= ST_SHOWADMIN;
1335
1336 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
Simon Horman05ee2132017-01-04 09:37:25 +01001337 ret = stats_dump_fields_html(&trash, stats, flags);
William Lallemand74c24fb2016-11-21 17:18:36 +01001338 else if (appctx->ctx.stats.flags & STAT_FMT_TYPED)
Simon Horman05ee2132017-01-04 09:37:25 +01001339 ret = stats_dump_fields_typed(&trash, stats);
1340 else if (appctx->ctx.stats.flags & STAT_FMT_JSON)
1341 ret = stats_dump_fields_json(&trash, stats,
1342 !(appctx->ctx.stats.flags &
1343 STAT_STARTED));
William Lallemand74c24fb2016-11-21 17:18:36 +01001344 else
Simon Horman05ee2132017-01-04 09:37:25 +01001345 ret = stats_dump_fields_csv(&trash, stats);
1346
1347 if (ret)
1348 appctx->ctx.stats.flags |= STAT_STARTED;
1349
1350 return ret;
William Lallemand74c24fb2016-11-21 17:18:36 +01001351}
1352
1353/* Fill <stats> with the frontend statistics. <stats> is
1354 * preallocated array of length <len>. The length of the array
1355 * must be at least ST_F_TOTAL_FIELDS. If this length is less then
1356 * this value, the function returns 0, otherwise, it returns 1.
1357 */
1358int stats_fill_fe_stats(struct proxy *px, struct field *stats, int len)
1359{
1360 if (len < ST_F_TOTAL_FIELDS)
1361 return 0;
1362
1363 memset(stats, 0, sizeof(*stats) * len);
1364
1365 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1366 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, "FRONTEND");
1367 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1368 stats[ST_F_SCUR] = mkf_u32(0, px->feconn);
1369 stats[ST_F_SMAX] = mkf_u32(FN_MAX, px->fe_counters.conn_max);
1370 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, px->maxconn);
1371 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, px->fe_counters.cum_sess);
1372 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, px->fe_counters.bytes_in);
1373 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, px->fe_counters.bytes_out);
1374 stats[ST_F_DREQ] = mkf_u64(FN_COUNTER, px->fe_counters.denied_req);
1375 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, px->fe_counters.denied_resp);
1376 stats[ST_F_EREQ] = mkf_u64(FN_COUNTER, px->fe_counters.failed_req);
1377 stats[ST_F_DCON] = mkf_u64(FN_COUNTER, px->fe_counters.denied_conn);
1378 stats[ST_F_DSES] = mkf_u64(FN_COUNTER, px->fe_counters.denied_sess);
1379 stats[ST_F_STATUS] = mkf_str(FO_STATUS, px->state == PR_STREADY ? "OPEN" : px->state == PR_STFULL ? "FULL" : "STOP");
1380 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
1381 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1382 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, 0);
1383 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_FE);
1384 stats[ST_F_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_sess_per_sec));
1385 stats[ST_F_RATE_LIM] = mkf_u32(FO_CONFIG|FN_LIMIT, px->fe_sps_lim);
1386 stats[ST_F_RATE_MAX] = mkf_u32(FN_MAX, px->fe_counters.sps_max);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001387 stats[ST_F_WREW] = mkf_u64(FN_COUNTER, px->fe_counters.failed_rewrites);
William Lallemand74c24fb2016-11-21 17:18:36 +01001388
1389 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
1390 if (px->mode == PR_MODE_HTTP) {
1391 stats[ST_F_HRSP_1XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[1]);
1392 stats[ST_F_HRSP_2XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[2]);
1393 stats[ST_F_HRSP_3XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[3]);
1394 stats[ST_F_HRSP_4XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[4]);
1395 stats[ST_F_HRSP_5XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[5]);
1396 stats[ST_F_HRSP_OTHER] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[0]);
1397 stats[ST_F_INTERCEPTED] = mkf_u64(FN_COUNTER, px->fe_counters.intercepted_req);
Willy Tarreaua1214a52018-12-14 14:00:25 +01001398 stats[ST_F_CACHE_LOOKUPS] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cache_lookups);
1399 stats[ST_F_CACHE_HITS] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cache_hits);
William Lallemand74c24fb2016-11-21 17:18:36 +01001400 }
1401
1402 /* requests : req_rate, req_rate_max, req_tot, */
1403 stats[ST_F_REQ_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_req_per_sec));
1404 stats[ST_F_REQ_RATE_MAX] = mkf_u32(FN_MAX, px->fe_counters.p.http.rps_max);
1405 stats[ST_F_REQ_TOT] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cum_req);
1406
1407 /* compression: in, out, bypassed, responses */
1408 stats[ST_F_COMP_IN] = mkf_u64(FN_COUNTER, px->fe_counters.comp_in);
1409 stats[ST_F_COMP_OUT] = mkf_u64(FN_COUNTER, px->fe_counters.comp_out);
1410 stats[ST_F_COMP_BYP] = mkf_u64(FN_COUNTER, px->fe_counters.comp_byp);
1411 stats[ST_F_COMP_RSP] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.comp_rsp);
1412
1413 /* connections : conn_rate, conn_rate_max, conn_tot, conn_max */
1414 stats[ST_F_CONN_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_conn_per_sec));
1415 stats[ST_F_CONN_RATE_MAX] = mkf_u32(FN_MAX, px->fe_counters.cps_max);
1416 stats[ST_F_CONN_TOT] = mkf_u64(FN_COUNTER, px->fe_counters.cum_conn);
1417
1418 return 1;
1419}
1420
1421/* Dumps a frontend's line to the trash for the current proxy <px> and uses
1422 * the state from stream interface <si>. The caller is responsible for clearing
1423 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
1424 */
1425static int stats_dump_fe_stats(struct stream_interface *si, struct proxy *px)
1426{
1427 struct appctx *appctx = __objt_appctx(si->end);
1428
1429 if (!(px->cap & PR_CAP_FE))
1430 return 0;
1431
1432 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_FE)))
1433 return 0;
1434
1435 if (!stats_fill_fe_stats(px, stats, ST_F_TOTAL_FIELDS))
1436 return 0;
1437
1438 return stats_dump_one_line(stats, 0, px, appctx);
1439}
1440
1441/* Fill <stats> with the listener statistics. <stats> is
1442 * preallocated array of length <len>. The length of the array
1443 * must be at least ST_F_TOTAL_FIELDS. If this length is less
1444 * then this value, the function returns 0, otherwise, it
1445 * returns 1. <flags> can take the value ST_SHLGNDS.
1446 */
1447int stats_fill_li_stats(struct proxy *px, struct listener *l, int flags,
1448 struct field *stats, int len)
1449{
Willy Tarreau83061a82018-07-13 11:56:34 +02001450 struct buffer *out = get_trash_chunk();
William Lallemand74c24fb2016-11-21 17:18:36 +01001451
1452 if (len < ST_F_TOTAL_FIELDS)
1453 return 0;
1454
1455 if (!l->counters)
1456 return 0;
1457
1458 chunk_reset(out);
1459 memset(stats, 0, sizeof(*stats) * len);
1460
1461 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1462 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, l->name);
1463 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1464 stats[ST_F_SCUR] = mkf_u32(0, l->nbconn);
1465 stats[ST_F_SMAX] = mkf_u32(FN_MAX, l->counters->conn_max);
1466 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, l->maxconn);
1467 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, l->counters->cum_conn);
1468 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, l->counters->bytes_in);
1469 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, l->counters->bytes_out);
1470 stats[ST_F_DREQ] = mkf_u64(FN_COUNTER, l->counters->denied_req);
1471 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, l->counters->denied_resp);
1472 stats[ST_F_EREQ] = mkf_u64(FN_COUNTER, l->counters->failed_req);
1473 stats[ST_F_DCON] = mkf_u64(FN_COUNTER, l->counters->denied_conn);
1474 stats[ST_F_DSES] = mkf_u64(FN_COUNTER, l->counters->denied_sess);
Willy Tarreaua8cf66b2019-02-27 16:49:00 +01001475 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 +01001476 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
1477 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1478 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, l->luid);
1479 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_SO);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001480 stats[ST_F_WREW] = mkf_u64(FN_COUNTER, l->counters->failed_rewrites);
William Lallemand74c24fb2016-11-21 17:18:36 +01001481
1482 if (flags & ST_SHLGNDS) {
1483 char str[INET6_ADDRSTRLEN];
1484 int port;
1485
1486 port = get_host_port(&l->addr);
1487 switch (addr_to_str(&l->addr, str, sizeof(str))) {
1488 case AF_INET:
1489 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1490 chunk_appendf(out, "%s:%d", str, port);
1491 break;
1492 case AF_INET6:
1493 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1494 chunk_appendf(out, "[%s]:%d", str, port);
1495 break;
1496 case AF_UNIX:
1497 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, "unix");
1498 break;
1499 case -1:
1500 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1501 chunk_strcat(out, strerror(errno));
1502 break;
1503 default: /* address family not supported */
1504 break;
1505 }
1506 }
1507
1508 return 1;
1509}
1510
1511/* Dumps a line for listener <l> and proxy <px> to the trash and uses the state
1512 * from stream interface <si>, and stats flags <flags>. The caller is responsible
1513 * for clearing the trash if needed. Returns non-zero if it emits anything, zero
1514 * otherwise.
1515 */
1516static int stats_dump_li_stats(struct stream_interface *si, struct proxy *px, struct listener *l, int flags)
1517{
1518 struct appctx *appctx = __objt_appctx(si->end);
1519
1520 if (!stats_fill_li_stats(px, l, flags, stats, ST_F_TOTAL_FIELDS))
1521 return 0;
1522
1523 return stats_dump_one_line(stats, flags, px, appctx);
1524}
1525
1526enum srv_stats_state {
1527 SRV_STATS_STATE_DOWN = 0,
1528 SRV_STATS_STATE_DOWN_AGENT,
1529 SRV_STATS_STATE_GOING_UP,
1530 SRV_STATS_STATE_UP_GOING_DOWN,
1531 SRV_STATS_STATE_UP,
1532 SRV_STATS_STATE_NOLB_GOING_DOWN,
1533 SRV_STATS_STATE_NOLB,
1534 SRV_STATS_STATE_DRAIN_GOING_DOWN,
1535 SRV_STATS_STATE_DRAIN,
1536 SRV_STATS_STATE_DRAIN_AGENT,
1537 SRV_STATS_STATE_NO_CHECK,
1538
1539 SRV_STATS_STATE_COUNT, /* Must be last */
1540};
1541
1542static const char *srv_hlt_st[SRV_STATS_STATE_COUNT] = {
1543 [SRV_STATS_STATE_DOWN] = "DOWN",
1544 [SRV_STATS_STATE_DOWN_AGENT] = "DOWN (agent)",
1545 [SRV_STATS_STATE_GOING_UP] = "DOWN %d/%d",
1546 [SRV_STATS_STATE_UP_GOING_DOWN] = "UP %d/%d",
1547 [SRV_STATS_STATE_UP] = "UP",
1548 [SRV_STATS_STATE_NOLB_GOING_DOWN] = "NOLB %d/%d",
1549 [SRV_STATS_STATE_NOLB] = "NOLB",
1550 [SRV_STATS_STATE_DRAIN_GOING_DOWN] = "DRAIN %d/%d",
1551 [SRV_STATS_STATE_DRAIN] = "DRAIN",
1552 [SRV_STATS_STATE_DRAIN_AGENT] = "DRAIN (agent)",
1553 [SRV_STATS_STATE_NO_CHECK] = "no check"
1554};
1555
1556/* Fill <stats> with the server statistics. <stats> is
1557 * preallocated array of length <len>. The length of the array
1558 * must be at least ST_F_TOTAL_FIELDS. If this length is less
1559 * then this value, the function returns 0, otherwise, it
1560 * returns 1. <flags> can take the value ST_SHLGNDS.
1561 */
1562int stats_fill_sv_stats(struct proxy *px, struct server *sv, int flags,
1563 struct field *stats, int len)
1564{
1565 struct server *via, *ref;
1566 char str[INET6_ADDRSTRLEN];
Willy Tarreau83061a82018-07-13 11:56:34 +02001567 struct buffer *out = get_trash_chunk();
William Lallemand74c24fb2016-11-21 17:18:36 +01001568 enum srv_stats_state state;
1569 char *fld_status;
1570
1571 if (len < ST_F_TOTAL_FIELDS)
1572 return 0;
1573
1574 memset(stats, 0, sizeof(*stats) * len);
1575
1576 /* we have "via" which is the tracked server as described in the configuration,
1577 * and "ref" which is the checked server and the end of the chain.
1578 */
1579 via = sv->track ? sv->track : sv;
1580 ref = via;
1581 while (ref->track)
1582 ref = ref->track;
1583
Emeric Brun52a91d32017-08-31 14:41:55 +02001584 if (sv->cur_state == SRV_ST_RUNNING || sv->cur_state == SRV_ST_STARTING) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001585 if ((ref->check.state & CHK_ST_ENABLED) &&
1586 (ref->check.health < ref->check.rise + ref->check.fall - 1)) {
1587 state = SRV_STATS_STATE_UP_GOING_DOWN;
1588 } else {
1589 state = SRV_STATS_STATE_UP;
1590 }
1591
Emeric Brun52a91d32017-08-31 14:41:55 +02001592 if (sv->cur_admin & SRV_ADMF_DRAIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001593 if (ref->agent.state & CHK_ST_ENABLED)
1594 state = SRV_STATS_STATE_DRAIN_AGENT;
1595 else if (state == SRV_STATS_STATE_UP_GOING_DOWN)
1596 state = SRV_STATS_STATE_DRAIN_GOING_DOWN;
1597 else
1598 state = SRV_STATS_STATE_DRAIN;
1599 }
1600
1601 if (state == SRV_STATS_STATE_UP && !(ref->check.state & CHK_ST_ENABLED)) {
1602 state = SRV_STATS_STATE_NO_CHECK;
1603 }
1604 }
Emeric Brun52a91d32017-08-31 14:41:55 +02001605 else if (sv->cur_state == SRV_ST_STOPPING) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001606 if ((!(sv->check.state & CHK_ST_ENABLED) && !sv->track) ||
1607 (ref->check.health == ref->check.rise + ref->check.fall - 1)) {
1608 state = SRV_STATS_STATE_NOLB;
1609 } else {
1610 state = SRV_STATS_STATE_NOLB_GOING_DOWN;
1611 }
1612 }
1613 else { /* stopped */
1614 if ((ref->agent.state & CHK_ST_ENABLED) && !ref->agent.health) {
1615 state = SRV_STATS_STATE_DOWN_AGENT;
1616 } else if ((ref->check.state & CHK_ST_ENABLED) && !ref->check.health) {
1617 state = SRV_STATS_STATE_DOWN; /* DOWN */
1618 } else if ((ref->agent.state & CHK_ST_ENABLED) || (ref->check.state & CHK_ST_ENABLED)) {
1619 state = SRV_STATS_STATE_GOING_UP;
1620 } else {
1621 state = SRV_STATS_STATE_DOWN; /* DOWN, unchecked */
1622 }
1623 }
1624
1625 chunk_reset(out);
1626
1627 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1628 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, sv->id);
1629 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1630 stats[ST_F_QCUR] = mkf_u32(0, sv->nbpend);
1631 stats[ST_F_QMAX] = mkf_u32(FN_MAX, sv->counters.nbpend_max);
1632 stats[ST_F_SCUR] = mkf_u32(0, sv->cur_sess);
1633 stats[ST_F_SMAX] = mkf_u32(FN_MAX, sv->counters.cur_sess_max);
1634
1635 if (sv->maxconn)
1636 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, sv->maxconn);
1637
1638 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, sv->counters.cum_sess);
1639 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, sv->counters.bytes_in);
1640 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, sv->counters.bytes_out);
1641 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, sv->counters.failed_secu);
1642 stats[ST_F_ECON] = mkf_u64(FN_COUNTER, sv->counters.failed_conns);
1643 stats[ST_F_ERESP] = mkf_u64(FN_COUNTER, sv->counters.failed_resp);
1644 stats[ST_F_WRETR] = mkf_u64(FN_COUNTER, sv->counters.retries);
1645 stats[ST_F_WREDIS] = mkf_u64(FN_COUNTER, sv->counters.redispatches);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001646 stats[ST_F_WREW] = mkf_u64(FN_COUNTER, sv->counters.failed_rewrites);
Willy Tarreauf1573842018-12-14 11:35:36 +01001647 stats[ST_F_CONNECT] = mkf_u64(FN_COUNTER, sv->counters.connect);
1648 stats[ST_F_REUSE] = mkf_u64(FN_COUNTER, sv->counters.reuse);
William Lallemand74c24fb2016-11-21 17:18:36 +01001649
1650 /* status */
1651 fld_status = chunk_newstr(out);
Emeric Brun52a91d32017-08-31 14:41:55 +02001652 if (sv->cur_admin & SRV_ADMF_RMAINT)
William Lallemand74c24fb2016-11-21 17:18:36 +01001653 chunk_appendf(out, "MAINT (resolution)");
Emeric Brun52a91d32017-08-31 14:41:55 +02001654 else if (sv->cur_admin & SRV_ADMF_IMAINT)
William Lallemand74c24fb2016-11-21 17:18:36 +01001655 chunk_appendf(out, "MAINT (via %s/%s)", via->proxy->id, via->id);
Emeric Brun52a91d32017-08-31 14:41:55 +02001656 else if (sv->cur_admin & SRV_ADMF_MAINT)
William Lallemand74c24fb2016-11-21 17:18:36 +01001657 chunk_appendf(out, "MAINT");
1658 else
1659 chunk_appendf(out,
1660 srv_hlt_st[state],
Emeric Brun52a91d32017-08-31 14:41:55 +02001661 (ref->cur_state != SRV_ST_STOPPED) ? (ref->check.health - ref->check.rise + 1) : (ref->check.health),
1662 (ref->cur_state != SRV_ST_STOPPED) ? (ref->check.fall) : (ref->check.rise));
William Lallemand74c24fb2016-11-21 17:18:36 +01001663
1664 stats[ST_F_STATUS] = mkf_str(FO_STATUS, fld_status);
1665 stats[ST_F_LASTCHG] = mkf_u32(FN_AGE, now.tv_sec - sv->last_change);
Emeric Brun52a91d32017-08-31 14:41:55 +02001666 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 +01001667 stats[ST_F_ACT] = mkf_u32(FO_STATUS, (sv->flags & SRV_F_BACKUP) ? 0 : 1);
1668 stats[ST_F_BCK] = mkf_u32(FO_STATUS, (sv->flags & SRV_F_BACKUP) ? 1 : 0);
1669
1670 /* check failures: unique, fatal; last change, total downtime */
1671 if (sv->check.state & CHK_ST_ENABLED) {
1672 stats[ST_F_CHKFAIL] = mkf_u64(FN_COUNTER, sv->counters.failed_checks);
1673 stats[ST_F_CHKDOWN] = mkf_u64(FN_COUNTER, sv->counters.down_trans);
1674 stats[ST_F_DOWNTIME] = mkf_u32(FN_COUNTER, srv_downtime(sv));
1675 }
1676
1677 if (sv->maxqueue)
1678 stats[ST_F_QLIMIT] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->maxqueue);
1679
1680 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
1681 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1682 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, sv->puid);
1683
Emeric Brun52a91d32017-08-31 14:41:55 +02001684 if (sv->cur_state == SRV_ST_STARTING && !server_is_draining(sv))
William Lallemand74c24fb2016-11-21 17:18:36 +01001685 stats[ST_F_THROTTLE] = mkf_u32(FN_AVG, server_throttle_rate(sv));
1686
1687 stats[ST_F_LBTOT] = mkf_u64(FN_COUNTER, sv->counters.cum_lbconn);
1688
1689 if (sv->track) {
1690 char *fld_track = chunk_newstr(out);
1691
1692 chunk_appendf(out, "%s/%s", sv->track->proxy->id, sv->track->id);
1693 stats[ST_F_TRACKED] = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, fld_track);
1694 }
1695
1696 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_SV);
1697 stats[ST_F_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&sv->sess_per_sec));
1698 stats[ST_F_RATE_MAX] = mkf_u32(FN_MAX, sv->counters.sps_max);
1699
1700 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) {
1701 const char *fld_chksts;
1702
1703 fld_chksts = chunk_newstr(out);
1704 chunk_strcat(out, "* "); // for check in progress
1705 chunk_strcat(out, get_check_status_info(sv->check.status));
1706 if (!(sv->check.state & CHK_ST_INPROGRESS))
1707 fld_chksts += 2; // skip "* "
1708 stats[ST_F_CHECK_STATUS] = mkf_str(FN_OUTPUT, fld_chksts);
1709
1710 if (sv->check.status >= HCHK_STATUS_L57DATA)
1711 stats[ST_F_CHECK_CODE] = mkf_u32(FN_OUTPUT, sv->check.code);
1712
1713 if (sv->check.status >= HCHK_STATUS_CHECKED)
1714 stats[ST_F_CHECK_DURATION] = mkf_u64(FN_DURATION, sv->check.duration);
1715
1716 stats[ST_F_CHECK_DESC] = mkf_str(FN_OUTPUT, get_check_status_description(sv->check.status));
1717 stats[ST_F_LAST_CHK] = mkf_str(FN_OUTPUT, sv->check.desc);
1718 stats[ST_F_CHECK_RISE] = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.rise);
1719 stats[ST_F_CHECK_FALL] = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.fall);
1720 stats[ST_F_CHECK_HEALTH] = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.health);
1721 }
1722
1723 if ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) {
1724 const char *fld_chksts;
1725
1726 fld_chksts = chunk_newstr(out);
1727 chunk_strcat(out, "* "); // for check in progress
1728 chunk_strcat(out, get_check_status_info(sv->agent.status));
1729 if (!(sv->agent.state & CHK_ST_INPROGRESS))
1730 fld_chksts += 2; // skip "* "
1731 stats[ST_F_AGENT_STATUS] = mkf_str(FN_OUTPUT, fld_chksts);
1732
1733 if (sv->agent.status >= HCHK_STATUS_L57DATA)
1734 stats[ST_F_AGENT_CODE] = mkf_u32(FN_OUTPUT, sv->agent.code);
1735
1736 if (sv->agent.status >= HCHK_STATUS_CHECKED)
1737 stats[ST_F_AGENT_DURATION] = mkf_u64(FN_DURATION, sv->agent.duration);
1738
1739 stats[ST_F_AGENT_DESC] = mkf_str(FN_OUTPUT, get_check_status_description(sv->agent.status));
1740 stats[ST_F_LAST_AGT] = mkf_str(FN_OUTPUT, sv->agent.desc);
1741 stats[ST_F_AGENT_RISE] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.rise);
1742 stats[ST_F_AGENT_FALL] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.fall);
1743 stats[ST_F_AGENT_HEALTH] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.health);
1744 }
1745
1746 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
1747 if (px->mode == PR_MODE_HTTP) {
1748 stats[ST_F_HRSP_1XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[1]);
1749 stats[ST_F_HRSP_2XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[2]);
1750 stats[ST_F_HRSP_3XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[3]);
1751 stats[ST_F_HRSP_4XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[4]);
1752 stats[ST_F_HRSP_5XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[5]);
1753 stats[ST_F_HRSP_OTHER] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[0]);
1754 }
1755
1756 if (ref->observe)
1757 stats[ST_F_HANAFAIL] = mkf_u64(FN_COUNTER, sv->counters.failed_hana);
1758
1759 stats[ST_F_CLI_ABRT] = mkf_u64(FN_COUNTER, sv->counters.cli_aborts);
1760 stats[ST_F_SRV_ABRT] = mkf_u64(FN_COUNTER, sv->counters.srv_aborts);
1761 stats[ST_F_LASTSESS] = mkf_s32(FN_AGE, srv_lastsession(sv));
1762
1763 stats[ST_F_QTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.q_time, TIME_STATS_SAMPLES));
1764 stats[ST_F_CTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.c_time, TIME_STATS_SAMPLES));
1765 stats[ST_F_RTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.d_time, TIME_STATS_SAMPLES));
1766 stats[ST_F_TTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.t_time, TIME_STATS_SAMPLES));
1767
1768 if (flags & ST_SHLGNDS) {
1769 switch (addr_to_str(&sv->addr, str, sizeof(str))) {
1770 case AF_INET:
1771 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
Willy Tarreau04276f32017-01-06 17:41:29 +01001772 chunk_appendf(out, "%s:%d", str, sv->svc_port);
William Lallemand74c24fb2016-11-21 17:18:36 +01001773 break;
1774 case AF_INET6:
1775 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
Willy Tarreau04276f32017-01-06 17:41:29 +01001776 chunk_appendf(out, "[%s]:%d", str, sv->svc_port);
William Lallemand74c24fb2016-11-21 17:18:36 +01001777 break;
1778 case AF_UNIX:
1779 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, "unix");
1780 break;
1781 case -1:
1782 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1783 chunk_strcat(out, strerror(errno));
1784 break;
1785 default: /* address family not supported */
1786 break;
1787 }
1788
1789 if (sv->cookie)
1790 stats[ST_F_COOKIE] = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, sv->cookie);
1791 }
1792
1793 return 1;
1794}
1795
1796/* Dumps a line for server <sv> and proxy <px> to the trash and uses the state
1797 * from stream interface <si>, stats flags <flags>, and server state <state>.
1798 * The caller is responsible for clearing the trash if needed. Returns non-zero
1799 * if it emits anything, zero otherwise.
1800 */
1801static int stats_dump_sv_stats(struct stream_interface *si, struct proxy *px, int flags, struct server *sv)
1802{
1803 struct appctx *appctx = __objt_appctx(si->end);
1804
1805 if (!stats_fill_sv_stats(px, sv, flags, stats, ST_F_TOTAL_FIELDS))
1806 return 0;
1807
1808 return stats_dump_one_line(stats, flags, px, appctx);
1809}
1810
1811/* Fill <stats> with the backend statistics. <stats> is
1812 * preallocated array of length <len>. The length of the array
1813 * must be at least ST_F_TOTAL_FIELDS. If this length is less
1814 * then this value, the function returns 0, otherwise, it
1815 * returns 1. <flags> can take the value ST_SHLGNDS.
1816 */
1817int stats_fill_be_stats(struct proxy *px, int flags, struct field *stats, int len)
1818{
1819 if (len < ST_F_TOTAL_FIELDS)
1820 return 0;
1821
1822 memset(stats, 0, sizeof(*stats) * len);
1823
1824 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1825 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, "BACKEND");
1826 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1827 stats[ST_F_QCUR] = mkf_u32(0, px->nbpend);
1828 stats[ST_F_QMAX] = mkf_u32(FN_MAX, px->be_counters.nbpend_max);
Thierry FOURNIER0ff98a42016-12-19 16:50:42 +01001829 stats[ST_F_SCUR] = mkf_u32(0, px->beconn);
William Lallemand74c24fb2016-11-21 17:18:36 +01001830 stats[ST_F_SMAX] = mkf_u32(FN_MAX, px->be_counters.conn_max);
1831 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, px->fullconn);
1832 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, px->be_counters.cum_conn);
1833 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, px->be_counters.bytes_in);
1834 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, px->be_counters.bytes_out);
1835 stats[ST_F_DREQ] = mkf_u64(FN_COUNTER, px->be_counters.denied_req);
1836 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, px->be_counters.denied_resp);
1837 stats[ST_F_ECON] = mkf_u64(FN_COUNTER, px->be_counters.failed_conns);
1838 stats[ST_F_ERESP] = mkf_u64(FN_COUNTER, px->be_counters.failed_resp);
1839 stats[ST_F_WRETR] = mkf_u64(FN_COUNTER, px->be_counters.retries);
1840 stats[ST_F_WREDIS] = mkf_u64(FN_COUNTER, px->be_counters.redispatches);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001841 stats[ST_F_WREW] = mkf_u64(FN_COUNTER, px->be_counters.failed_rewrites);
Willy Tarreauf1573842018-12-14 11:35:36 +01001842 stats[ST_F_CONNECT] = mkf_u64(FN_COUNTER, px->be_counters.connect);
1843 stats[ST_F_REUSE] = mkf_u64(FN_COUNTER, px->be_counters.reuse);
William Lallemand74c24fb2016-11-21 17:18:36 +01001844 stats[ST_F_STATUS] = mkf_str(FO_STATUS, (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" : "DOWN");
1845 stats[ST_F_WEIGHT] = mkf_u32(FN_AVG, (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv);
1846 stats[ST_F_ACT] = mkf_u32(0, px->srv_act);
1847 stats[ST_F_BCK] = mkf_u32(0, px->srv_bck);
1848 stats[ST_F_CHKDOWN] = mkf_u64(FN_COUNTER, px->down_trans);
1849 stats[ST_F_LASTCHG] = mkf_u32(FN_AGE, now.tv_sec - px->last_change);
1850 if (px->srv)
1851 stats[ST_F_DOWNTIME] = mkf_u32(FN_COUNTER, be_downtime(px));
1852
1853 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
1854 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1855 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, 0);
1856 stats[ST_F_LBTOT] = mkf_u64(FN_COUNTER, px->be_counters.cum_lbconn);
1857 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_BE);
1858 stats[ST_F_RATE] = mkf_u32(0, read_freq_ctr(&px->be_sess_per_sec));
1859 stats[ST_F_RATE_MAX] = mkf_u32(0, px->be_counters.sps_max);
1860
1861 if (flags & ST_SHLGNDS) {
1862 if (px->cookie_name)
1863 stats[ST_F_COOKIE] = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, px->cookie_name);
1864 stats[ST_F_ALGO] = mkf_str(FO_CONFIG|FS_SERVICE, backend_lb_algo_str(px->lbprm.algo & BE_LB_ALGO));
1865 }
1866
1867 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
1868 if (px->mode == PR_MODE_HTTP) {
1869 stats[ST_F_REQ_TOT] = mkf_u64(FN_COUNTER, px->be_counters.p.http.cum_req);
1870 stats[ST_F_HRSP_1XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[1]);
1871 stats[ST_F_HRSP_2XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[2]);
1872 stats[ST_F_HRSP_3XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[3]);
1873 stats[ST_F_HRSP_4XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[4]);
1874 stats[ST_F_HRSP_5XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[5]);
1875 stats[ST_F_HRSP_OTHER] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[0]);
Willy Tarreaua1214a52018-12-14 14:00:25 +01001876 stats[ST_F_CACHE_LOOKUPS] = mkf_u64(FN_COUNTER, px->be_counters.p.http.cache_lookups);
1877 stats[ST_F_CACHE_HITS] = mkf_u64(FN_COUNTER, px->be_counters.p.http.cache_hits);
William Lallemand74c24fb2016-11-21 17:18:36 +01001878 }
1879
1880 stats[ST_F_CLI_ABRT] = mkf_u64(FN_COUNTER, px->be_counters.cli_aborts);
1881 stats[ST_F_SRV_ABRT] = mkf_u64(FN_COUNTER, px->be_counters.srv_aborts);
1882
1883 /* compression: in, out, bypassed, responses */
1884 stats[ST_F_COMP_IN] = mkf_u64(FN_COUNTER, px->be_counters.comp_in);
1885 stats[ST_F_COMP_OUT] = mkf_u64(FN_COUNTER, px->be_counters.comp_out);
1886 stats[ST_F_COMP_BYP] = mkf_u64(FN_COUNTER, px->be_counters.comp_byp);
1887 stats[ST_F_COMP_RSP] = mkf_u64(FN_COUNTER, px->be_counters.p.http.comp_rsp);
1888 stats[ST_F_LASTSESS] = mkf_s32(FN_AGE, be_lastsession(px));
1889
1890 stats[ST_F_QTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.q_time, TIME_STATS_SAMPLES));
1891 stats[ST_F_CTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.c_time, TIME_STATS_SAMPLES));
1892 stats[ST_F_RTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.d_time, TIME_STATS_SAMPLES));
1893 stats[ST_F_TTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.t_time, TIME_STATS_SAMPLES));
1894
1895 return 1;
1896}
1897
1898/* Dumps a line for backend <px> to the trash for and uses the state from stream
1899 * interface <si> and stats flags <flags>. The caller is responsible for clearing
1900 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
1901 */
1902static int stats_dump_be_stats(struct stream_interface *si, struct proxy *px, int flags)
1903{
1904 struct appctx *appctx = __objt_appctx(si->end);
1905
1906 if (!(px->cap & PR_CAP_BE))
1907 return 0;
1908
1909 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_BE)))
1910 return 0;
1911
1912 if (!stats_fill_be_stats(px, flags, stats, ST_F_TOTAL_FIELDS))
1913 return 0;
1914
1915 return stats_dump_one_line(stats, flags, px, appctx);
1916}
1917
1918/* Dumps the HTML table header for proxy <px> to the trash for and uses the state from
1919 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
1920 * for clearing the trash if needed.
1921 */
1922static void stats_dump_html_px_hdr(struct stream_interface *si, struct proxy *px, struct uri_auth *uri)
1923{
1924 struct appctx *appctx = __objt_appctx(si->end);
1925 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
1926
1927 if (px->cap & PR_CAP_BE && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
1928 /* A form to enable/disable this proxy servers */
1929
1930 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
1931 scope_txt[0] = 0;
1932 if (appctx->ctx.stats.scope_len) {
Christopher Fauleted7a0662019-01-14 11:07:34 +01001933 const char *scope_ptr = stats_scope_ptr(appctx, si);
1934
William Lallemand74c24fb2016-11-21 17:18:36 +01001935 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Christopher Fauleted7a0662019-01-14 11:07:34 +01001936 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), scope_ptr, appctx->ctx.stats.scope_len);
William Lallemand74c24fb2016-11-21 17:18:36 +01001937 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
1938 }
1939
1940 chunk_appendf(&trash,
1941 "<form method=\"post\">");
1942 }
1943
1944 /* print a new table */
1945 chunk_appendf(&trash,
1946 "<table class=\"tbl\" width=\"100%%\">\n"
1947 "<tr class=\"titre\">"
1948 "<th class=\"pxname\" width=\"10%%\">");
1949
1950 chunk_appendf(&trash,
1951 "<a name=\"%s\"></a>%s"
1952 "<a class=px href=\"#%s\">%s</a>",
1953 px->id,
1954 (uri->flags & ST_SHLGNDS) ? "<u>":"",
1955 px->id, px->id);
1956
1957 if (uri->flags & ST_SHLGNDS) {
1958 /* cap, mode, id */
1959 chunk_appendf(&trash, "<div class=tips>cap: %s, mode: %s, id: %d",
1960 proxy_cap_str(px->cap), proxy_mode_str(px->mode),
1961 px->uuid);
1962 chunk_appendf(&trash, "</div>");
1963 }
1964
1965 chunk_appendf(&trash,
1966 "%s</th>"
1967 "<th class=\"%s\" width=\"90%%\">%s</th>"
1968 "</tr>\n"
1969 "</table>\n"
1970 "<table class=\"tbl\" width=\"100%%\">\n"
1971 "<tr class=\"titre\">",
1972 (uri->flags & ST_SHLGNDS) ? "</u>":"",
1973 px->desc ? "desc" : "empty", px->desc ? px->desc : "");
1974
1975 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
1976 /* Column heading for Enable or Disable server */
David Harrigand3db35a2016-12-30 12:12:49 +00001977 chunk_appendf(&trash,
1978 "<th rowspan=2 width=1><input type=\"checkbox\" \
1979 onclick=\"for(c in document.getElementsByClassName('%s-checkbox')) \
1980 document.getElementsByClassName('%s-checkbox').item(c).checked = this.checked\"></th>",
1981 px->id,
1982 px->id);
William Lallemand74c24fb2016-11-21 17:18:36 +01001983 }
1984
1985 chunk_appendf(&trash,
1986 "<th rowspan=2></th>"
1987 "<th colspan=3>Queue</th>"
1988 "<th colspan=3>Session rate</th><th colspan=6>Sessions</th>"
1989 "<th colspan=2>Bytes</th><th colspan=2>Denied</th>"
1990 "<th colspan=3>Errors</th><th colspan=2>Warnings</th>"
1991 "<th colspan=9>Server</th>"
1992 "</tr>\n"
1993 "<tr class=\"titre\">"
1994 "<th>Cur</th><th>Max</th><th>Limit</th>"
1995 "<th>Cur</th><th>Max</th><th>Limit</th><th>Cur</th><th>Max</th>"
1996 "<th>Limit</th><th>Total</th><th>LbTot</th><th>Last</th><th>In</th><th>Out</th>"
1997 "<th>Req</th><th>Resp</th><th>Req</th><th>Conn</th>"
1998 "<th>Resp</th><th>Retr</th><th>Redis</th>"
1999 "<th>Status</th><th>LastChk</th><th>Wght</th><th>Act</th>"
2000 "<th>Bck</th><th>Chk</th><th>Dwn</th><th>Dwntme</th>"
2001 "<th>Thrtle</th>\n"
2002 "</tr>");
2003}
2004
2005/* Dumps the HTML table trailer for proxy <px> to the trash for and uses the state from
2006 * stream interface <si>. The caller is responsible for clearing the trash if needed.
2007 */
2008static void stats_dump_html_px_end(struct stream_interface *si, struct proxy *px)
2009{
2010 struct appctx *appctx = __objt_appctx(si->end);
2011 chunk_appendf(&trash, "</table>");
2012
2013 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
2014 /* close the form used to enable/disable this proxy servers */
2015 chunk_appendf(&trash,
2016 "Choose the action to perform on the checked servers : "
2017 "<select name=action>"
2018 "<option value=\"\"></option>"
2019 "<option value=\"ready\">Set state to READY</option>"
2020 "<option value=\"drain\">Set state to DRAIN</option>"
2021 "<option value=\"maint\">Set state to MAINT</option>"
2022 "<option value=\"dhlth\">Health: disable checks</option>"
2023 "<option value=\"ehlth\">Health: enable checks</option>"
2024 "<option value=\"hrunn\">Health: force UP</option>"
2025 "<option value=\"hnolb\">Health: force NOLB</option>"
2026 "<option value=\"hdown\">Health: force DOWN</option>"
2027 "<option value=\"dagent\">Agent: disable checks</option>"
2028 "<option value=\"eagent\">Agent: enable checks</option>"
2029 "<option value=\"arunn\">Agent: force UP</option>"
2030 "<option value=\"adown\">Agent: force DOWN</option>"
2031 "<option value=\"shutdown\">Kill Sessions</option>"
2032 "</select>"
2033 "<input type=\"hidden\" name=\"b\" value=\"#%d\">"
2034 "&nbsp;<input type=\"submit\" value=\"Apply\">"
2035 "</form>",
2036 px->uuid);
2037 }
2038
2039 chunk_appendf(&trash, "<p>\n");
2040}
2041
2042/*
2043 * Dumps statistics for a proxy. The output is sent to the stream interface's
2044 * input buffer. Returns 0 if it had to stop dumping data because of lack of
2045 * buffer space, or non-zero if everything completed. This function is used
2046 * both by the CLI and the HTTP entry points, and is able to dump the output
2047 * in HTML or CSV formats. If the later, <uri> must be NULL.
2048 */
Christopher Fauletef779222018-10-31 08:47:01 +01002049int stats_dump_proxy_to_buffer(struct stream_interface *si, struct htx *htx,
2050 struct proxy *px, struct uri_auth *uri)
William Lallemand74c24fb2016-11-21 17:18:36 +01002051{
2052 struct appctx *appctx = __objt_appctx(si->end);
2053 struct stream *s = si_strm(si);
2054 struct channel *rep = si_ic(si);
2055 struct server *sv, *svs; /* server and server-state, server-state=server or server->track */
2056 struct listener *l;
2057 unsigned int flags;
2058
2059 if (uri)
2060 flags = uri->flags;
William Lallemand07a62f72017-05-24 00:57:40 +02002061 else if ((strm_li(s)->bind_conf->level & ACCESS_LVL_MASK) >= ACCESS_LVL_OPER)
William Lallemand74c24fb2016-11-21 17:18:36 +01002062 flags = ST_SHLGNDS | ST_SHNODE | ST_SHDESC;
2063 else
2064 flags = ST_SHNODE | ST_SHDESC;
2065
2066 chunk_reset(&trash);
2067
2068 switch (appctx->ctx.stats.px_st) {
2069 case STAT_PX_ST_INIT:
2070 /* we are on a new proxy */
2071 if (uri && uri->scope) {
2072 /* we have a limited scope, we have to check the proxy name */
2073 struct stat_scope *scope;
2074 int len;
2075
2076 len = strlen(px->id);
2077 scope = uri->scope;
2078
2079 while (scope) {
2080 /* match exact proxy name */
2081 if (scope->px_len == len && !memcmp(px->id, scope->px_id, len))
2082 break;
2083
2084 /* match '.' which means 'self' proxy */
2085 if (!strcmp(scope->px_id, ".") && px == s->be)
2086 break;
2087 scope = scope->next;
2088 }
2089
2090 /* proxy name not found : don't dump anything */
2091 if (scope == NULL)
2092 return 1;
2093 }
2094
2095 /* if the user has requested a limited output and the proxy
2096 * name does not match, skip it.
2097 */
Christopher Fauleted7a0662019-01-14 11:07:34 +01002098 if (appctx->ctx.stats.scope_len) {
2099 const char *scope_ptr = stats_scope_ptr(appctx, si);
2100
2101 if (strnistr(px->id, strlen(px->id), scope_ptr, appctx->ctx.stats.scope_len) == NULL)
2102 return 1;
2103 }
William Lallemand74c24fb2016-11-21 17:18:36 +01002104
2105 if ((appctx->ctx.stats.flags & STAT_BOUND) &&
2106 (appctx->ctx.stats.iid != -1) &&
2107 (px->uuid != appctx->ctx.stats.iid))
2108 return 1;
2109
2110 appctx->ctx.stats.px_st = STAT_PX_ST_TH;
2111 /* fall through */
2112
2113 case STAT_PX_ST_TH:
2114 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
2115 stats_dump_html_px_hdr(si, px, uri);
Christopher Fauletef779222018-10-31 08:47:01 +01002116 if (!stats_putchk(rep, htx, &trash))
2117 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002118 }
2119
2120 appctx->ctx.stats.px_st = STAT_PX_ST_FE;
2121 /* fall through */
2122
2123 case STAT_PX_ST_FE:
2124 /* print the frontend */
2125 if (stats_dump_fe_stats(si, px)) {
Christopher Fauletef779222018-10-31 08:47:01 +01002126 if (!stats_putchk(rep, htx, &trash))
2127 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002128 }
2129
2130 appctx->ctx.stats.l = px->conf.listeners.n;
2131 appctx->ctx.stats.px_st = STAT_PX_ST_LI;
2132 /* fall through */
2133
2134 case STAT_PX_ST_LI:
2135 /* stats.l has been initialized above */
2136 for (; appctx->ctx.stats.l != &px->conf.listeners; appctx->ctx.stats.l = l->by_fe.n) {
Christopher Fauletef779222018-10-31 08:47:01 +01002137 if (htx) {
2138 if (htx_almost_full(htx))
2139 goto full;
2140 }
2141 else {
2142 if (buffer_almost_full(&rep->buf))
2143 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002144 }
2145
2146 l = LIST_ELEM(appctx->ctx.stats.l, struct listener *, by_fe);
2147 if (!l->counters)
2148 continue;
2149
2150 if (appctx->ctx.stats.flags & STAT_BOUND) {
2151 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SO)))
2152 break;
2153
2154 if (appctx->ctx.stats.sid != -1 && l->luid != appctx->ctx.stats.sid)
2155 continue;
2156 }
2157
2158 /* print the frontend */
2159 if (stats_dump_li_stats(si, px, l, flags)) {
Christopher Fauletef779222018-10-31 08:47:01 +01002160 if (!stats_putchk(rep, htx, &trash))
2161 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002162 }
2163 }
2164
2165 appctx->ctx.stats.sv = px->srv; /* may be NULL */
2166 appctx->ctx.stats.px_st = STAT_PX_ST_SV;
2167 /* fall through */
2168
2169 case STAT_PX_ST_SV:
2170 /* stats.sv has been initialized above */
2171 for (; appctx->ctx.stats.sv != NULL; appctx->ctx.stats.sv = sv->next) {
Christopher Fauletef779222018-10-31 08:47:01 +01002172 if (htx) {
2173 if (htx_almost_full(htx))
2174 goto full;
2175 }
2176 else {
2177 if (buffer_almost_full(&rep->buf))
2178 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002179 }
2180
2181 sv = appctx->ctx.stats.sv;
2182
2183 if (appctx->ctx.stats.flags & STAT_BOUND) {
2184 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SV)))
2185 break;
2186
2187 if (appctx->ctx.stats.sid != -1 && sv->puid != appctx->ctx.stats.sid)
2188 continue;
2189 }
2190
2191 svs = sv;
2192 while (svs->track)
2193 svs = svs->track;
2194
2195 /* do not report servers which are DOWN and not changing state */
2196 if ((appctx->ctx.stats.flags & STAT_HIDE_DOWN) &&
Emeric Brun52a91d32017-08-31 14:41:55 +02002197 ((sv->cur_admin & SRV_ADMF_MAINT) || /* server is in maintenance */
2198 (sv->cur_state == SRV_ST_STOPPED && /* server is down */
William Lallemand74c24fb2016-11-21 17:18:36 +01002199 (!((svs->agent.state | svs->check.state) & CHK_ST_ENABLED) ||
2200 ((svs->agent.state & CHK_ST_ENABLED) && !svs->agent.health) ||
2201 ((svs->check.state & CHK_ST_ENABLED) && !svs->check.health))))) {
2202 continue;
2203 }
2204
2205 if (stats_dump_sv_stats(si, px, flags, sv)) {
Christopher Fauletef779222018-10-31 08:47:01 +01002206 if (!stats_putchk(rep, htx, &trash))
2207 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002208 }
2209 } /* for sv */
2210
2211 appctx->ctx.stats.px_st = STAT_PX_ST_BE;
2212 /* fall through */
2213
2214 case STAT_PX_ST_BE:
2215 /* print the backend */
2216 if (stats_dump_be_stats(si, px, flags)) {
Christopher Fauletef779222018-10-31 08:47:01 +01002217 if (!stats_putchk(rep, htx, &trash))
2218 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002219 }
2220
2221 appctx->ctx.stats.px_st = STAT_PX_ST_END;
2222 /* fall through */
2223
2224 case STAT_PX_ST_END:
2225 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
2226 stats_dump_html_px_end(si, px);
Christopher Fauletef779222018-10-31 08:47:01 +01002227 if (!stats_putchk(rep, htx, &trash))
2228 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002229 }
2230
2231 appctx->ctx.stats.px_st = STAT_PX_ST_FIN;
2232 /* fall through */
2233
2234 case STAT_PX_ST_FIN:
2235 return 1;
2236
2237 default:
2238 /* unknown state, we should put an abort() here ! */
2239 return 1;
2240 }
Christopher Fauletef779222018-10-31 08:47:01 +01002241
2242 full:
2243 si_rx_room_blk(si);
2244 return 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01002245}
2246
2247/* Dumps the HTTP stats head block to the trash for and uses the per-uri
2248 * parameters <uri>. The caller is responsible for clearing the trash if needed.
2249 */
2250static void stats_dump_html_head(struct uri_auth *uri)
2251{
2252 /* WARNING! This must fit in the first buffer !!! */
2253 chunk_appendf(&trash,
2254 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n"
2255 "\"http://www.w3.org/TR/html4/loose.dtd\">\n"
2256 "<html><head><title>Statistics Report for " PRODUCT_NAME "%s%s</title>\n"
2257 "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n"
2258 "<style type=\"text/css\"><!--\n"
2259 "body {"
2260 " font-family: arial, helvetica, sans-serif;"
2261 " font-size: 12px;"
2262 " font-weight: normal;"
2263 " color: black;"
2264 " background: white;"
2265 "}\n"
2266 "th,td {"
2267 " font-size: 10px;"
2268 "}\n"
2269 "h1 {"
2270 " font-size: x-large;"
2271 " margin-bottom: 0.5em;"
2272 "}\n"
2273 "h2 {"
2274 " font-family: helvetica, arial;"
2275 " font-size: x-large;"
2276 " font-weight: bold;"
2277 " font-style: italic;"
2278 " color: #6020a0;"
2279 " margin-top: 0em;"
2280 " margin-bottom: 0em;"
2281 "}\n"
2282 "h3 {"
2283 " font-family: helvetica, arial;"
2284 " font-size: 16px;"
2285 " font-weight: bold;"
2286 " color: #b00040;"
2287 " background: #e8e8d0;"
2288 " margin-top: 0em;"
2289 " margin-bottom: 0em;"
2290 "}\n"
2291 "li {"
2292 " margin-top: 0.25em;"
2293 " margin-right: 2em;"
2294 "}\n"
2295 ".hr {margin-top: 0.25em;"
2296 " border-color: black;"
2297 " border-bottom-style: solid;"
2298 "}\n"
2299 ".titre {background: #20D0D0;color: #000000; font-weight: bold; text-align: center;}\n"
2300 ".total {background: #20D0D0;color: #ffff80;}\n"
2301 ".frontend {background: #e8e8d0;}\n"
2302 ".socket {background: #d0d0d0;}\n"
2303 ".backend {background: #e8e8d0;}\n"
2304 ".active_down {background: #ff9090;}\n"
2305 ".active_going_up {background: #ffd020;}\n"
2306 ".active_going_down {background: #ffffa0;}\n"
2307 ".active_up {background: #c0ffc0;}\n"
2308 ".active_nolb {background: #20a0ff;}\n"
2309 ".active_draining {background: #20a0FF;}\n"
2310 ".active_no_check {background: #e0e0e0;}\n"
2311 ".backup_down {background: #ff9090;}\n"
2312 ".backup_going_up {background: #ff80ff;}\n"
2313 ".backup_going_down {background: #c060ff;}\n"
2314 ".backup_up {background: #b0d0ff;}\n"
2315 ".backup_nolb {background: #90b0e0;}\n"
2316 ".backup_draining {background: #cc9900;}\n"
2317 ".backup_no_check {background: #e0e0e0;}\n"
2318 ".maintain {background: #c07820;}\n"
2319 ".rls {letter-spacing: 0.2em; margin-right: 1px;}\n" /* right letter spacing (used for grouping digits) */
2320 "\n"
2321 "a.px:link {color: #ffff40; text-decoration: none;}"
2322 "a.px:visited {color: #ffff40; text-decoration: none;}"
2323 "a.px:hover {color: #ffffff; text-decoration: none;}"
2324 "a.lfsb:link {color: #000000; text-decoration: none;}"
2325 "a.lfsb:visited {color: #000000; text-decoration: none;}"
2326 "a.lfsb:hover {color: #505050; text-decoration: none;}"
2327 "\n"
2328 "table.tbl { border-collapse: collapse; border-style: none;}\n"
2329 "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"
2330 "table.tbl td.ac { text-align: center;}\n"
2331 "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}\n"
2332 "table.tbl th.pxname { background: #b00040; color: #ffff40; font-weight: bold; border-style: solid solid none solid; padding: 2px 3px; white-space: nowrap;}\n"
2333 "table.tbl th.empty { border-style: none; empty-cells: hide; background: white;}\n"
2334 "table.tbl th.desc { background: white; border-style: solid solid none solid; text-align: left; padding: 2px 3px;}\n"
2335 "\n"
2336 "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n"
2337 "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n"
2338 "table.lgd td.noborder { border-style: none; padding: 2px; white-space: nowrap;}\n"
2339 "table.det { border-collapse: collapse; border-style: none; }\n"
2340 "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"
2341 "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"
2342 "u {text-decoration:none; border-bottom: 1px dotted black;}\n"
2343 "div.tips {\n"
2344 " display:block;\n"
2345 " visibility:hidden;\n"
2346 " z-index:2147483647;\n"
2347 " position:absolute;\n"
2348 " padding:2px 4px 3px;\n"
2349 " background:#f0f060; color:#000000;\n"
2350 " border:1px solid #7040c0;\n"
2351 " white-space:nowrap;\n"
2352 " font-style:normal;font-size:11px;font-weight:normal;\n"
2353 " -moz-border-radius:3px;-webkit-border-radius:3px;border-radius:3px;\n"
2354 " -moz-box-shadow:gray 2px 2px 3px;-webkit-box-shadow:gray 2px 2px 3px;box-shadow:gray 2px 2px 3px;\n"
2355 "}\n"
2356 "u:hover div.tips {visibility:visible;}\n"
2357 "-->\n"
2358 "</style></head>\n",
2359 (uri->flags & ST_SHNODE) ? " on " : "",
2360 (uri->flags & ST_SHNODE) ? (uri->node ? uri->node : global.node) : ""
2361 );
2362}
2363
2364/* Dumps the HTML stats information block to the trash for and uses the state from
2365 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
2366 * for clearing the trash if needed.
2367 */
2368static void stats_dump_html_info(struct stream_interface *si, struct uri_auth *uri)
2369{
2370 struct appctx *appctx = __objt_appctx(si->end);
2371 unsigned int up = (now.tv_sec - start_date.tv_sec);
2372 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
Christopher Fauleted7a0662019-01-14 11:07:34 +01002373 const char *scope_ptr = stats_scope_ptr(appctx, si);
William Lallemand74c24fb2016-11-21 17:18:36 +01002374
2375 /* WARNING! this has to fit the first packet too.
2376 * We are around 3.5 kB, add adding entries will
2377 * become tricky if we want to support 4kB buffers !
2378 */
2379 chunk_appendf(&trash,
2380 "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">"
2381 PRODUCT_NAME "%s</a></h1>\n"
2382 "<h2>Statistics Report for pid %d%s%s%s%s</h2>\n"
2383 "<hr width=\"100%%\" class=\"hr\">\n"
2384 "<h3>&gt; General process information</h3>\n"
2385 "<table border=0><tr><td align=\"left\" nowrap width=\"1%%\">\n"
Yves Lafon95317282018-02-26 11:10:37 +01002386 "<p><b>pid = </b> %d (process #%d, nbproc = %d, nbthread = %d)<br>\n"
William Lallemand74c24fb2016-11-21 17:18:36 +01002387 "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n"
2388 "<b>system limits:</b> memmax = %s%s; ulimit-n = %d<br>\n"
2389 "<b>maxsock = </b> %d; <b>maxconn = </b> %d; <b>maxpipes = </b> %d<br>\n"
2390 "current conns = %d; current pipes = %d/%d; conn rate = %d/sec<br>\n"
2391 "Running tasks: %d/%d; idle = %d %%<br>\n"
2392 "</td><td align=\"center\" nowrap>\n"
2393 "<table class=\"lgd\"><tr>\n"
2394 "<td class=\"active_up\">&nbsp;</td><td class=\"noborder\">active UP </td>"
2395 "<td class=\"backup_up\">&nbsp;</td><td class=\"noborder\">backup UP </td>"
2396 "</tr><tr>\n"
2397 "<td class=\"active_going_down\"></td><td class=\"noborder\">active UP, going down </td>"
2398 "<td class=\"backup_going_down\"></td><td class=\"noborder\">backup UP, going down </td>"
2399 "</tr><tr>\n"
2400 "<td class=\"active_going_up\"></td><td class=\"noborder\">active DOWN, going up </td>"
2401 "<td class=\"backup_going_up\"></td><td class=\"noborder\">backup DOWN, going up </td>"
2402 "</tr><tr>\n"
2403 "<td class=\"active_down\"></td><td class=\"noborder\">active or backup DOWN &nbsp;</td>"
2404 "<td class=\"active_no_check\"></td><td class=\"noborder\">not checked </td>"
2405 "</tr><tr>\n"
2406 "<td class=\"maintain\"></td><td class=\"noborder\" colspan=\"3\">active or backup DOWN for maintenance (MAINT) &nbsp;</td>"
2407 "</tr><tr>\n"
2408 "<td class=\"active_draining\"></td><td class=\"noborder\" colspan=\"3\">active or backup SOFT STOPPED for maintenance &nbsp;</td>"
2409 "</tr></table>\n"
2410 "Note: \"NOLB\"/\"DRAIN\" = UP with load-balancing disabled."
2411 "</td>"
2412 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
2413 "<b>Display option:</b><ul style=\"margin-top: 0.25em;\">"
2414 "",
Willy Tarreau909b9d82019-01-04 18:20:32 +01002415 (uri->flags & ST_HIDEVER) ? "" : (stats_version_string),
William Lallemand74c24fb2016-11-21 17:18:36 +01002416 pid, (uri->flags & ST_SHNODE) ? " on " : "",
2417 (uri->flags & ST_SHNODE) ? (uri->node ? uri->node : global.node) : "",
2418 (uri->flags & ST_SHDESC) ? ": " : "",
2419 (uri->flags & ST_SHDESC) ? (uri->desc ? uri->desc : global.desc) : "",
Yves Lafon95317282018-02-26 11:10:37 +01002420 pid, relative_pid, global.nbproc, global.nbthread,
William Lallemand74c24fb2016-11-21 17:18:36 +01002421 up / 86400, (up % 86400) / 3600,
2422 (up % 3600) / 60, (up % 60),
2423 global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited",
2424 global.rlimit_memmax ? " MB" : "",
2425 global.rlimit_nofile,
2426 global.maxsock, global.maxconn, global.maxpipes,
2427 actconn, pipes_used, pipes_used+pipes_free, read_freq_ctr(&global.conn_per_sec),
Christopher Faulet34c5cc92016-12-06 09:15:30 +01002428 tasks_run_queue_cur, nb_tasks_cur, idle_pct
William Lallemand74c24fb2016-11-21 17:18:36 +01002429 );
2430
2431 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Christopher Fauleted7a0662019-01-14 11:07:34 +01002432 memcpy(scope_txt, scope_ptr, appctx->ctx.stats.scope_len);
William Lallemand74c24fb2016-11-21 17:18:36 +01002433 scope_txt[appctx->ctx.stats.scope_len] = '\0';
2434
2435 chunk_appendf(&trash,
2436 "<li><form method=\"GET\">Scope : <input value=\"%s\" name=\"" STAT_SCOPE_INPUT_NAME "\" size=\"8\" maxlength=\"%d\" tabindex=\"1\"/></form>\n",
2437 (appctx->ctx.stats.scope_len > 0) ? scope_txt : "",
2438 STAT_SCOPE_TXT_MAXLEN);
2439
2440 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
2441 scope_txt[0] = 0;
2442 if (appctx->ctx.stats.scope_len) {
2443 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Christopher Fauleted7a0662019-01-14 11:07:34 +01002444 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), scope_ptr, appctx->ctx.stats.scope_len);
William Lallemand74c24fb2016-11-21 17:18:36 +01002445 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
2446 }
2447
2448 if (appctx->ctx.stats.flags & STAT_HIDE_DOWN)
2449 chunk_appendf(&trash,
2450 "<li><a href=\"%s%s%s%s\">Show all servers</a><br>\n",
2451 uri->uri_prefix,
2452 "",
2453 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2454 scope_txt);
2455 else
2456 chunk_appendf(&trash,
2457 "<li><a href=\"%s%s%s%s\">Hide 'DOWN' servers</a><br>\n",
2458 uri->uri_prefix,
2459 ";up",
2460 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2461 scope_txt);
2462
2463 if (uri->refresh > 0) {
2464 if (appctx->ctx.stats.flags & STAT_NO_REFRESH)
2465 chunk_appendf(&trash,
2466 "<li><a href=\"%s%s%s%s\">Enable refresh</a><br>\n",
2467 uri->uri_prefix,
2468 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2469 "",
2470 scope_txt);
2471 else
2472 chunk_appendf(&trash,
2473 "<li><a href=\"%s%s%s%s\">Disable refresh</a><br>\n",
2474 uri->uri_prefix,
2475 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2476 ";norefresh",
2477 scope_txt);
2478 }
2479
2480 chunk_appendf(&trash,
2481 "<li><a href=\"%s%s%s%s\">Refresh now</a><br>\n",
2482 uri->uri_prefix,
2483 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2484 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2485 scope_txt);
2486
2487 chunk_appendf(&trash,
2488 "<li><a href=\"%s;csv%s%s\">CSV export</a><br>\n",
2489 uri->uri_prefix,
2490 (uri->refresh > 0) ? ";norefresh" : "",
2491 scope_txt);
2492
2493 chunk_appendf(&trash,
2494 "</ul></td>"
2495 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
2496 "<b>External resources:</b><ul style=\"margin-top: 0.25em;\">\n"
2497 "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>\n"
2498 "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>\n"
2499 "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>\n"
2500 "</ul>"
2501 "</td>"
2502 "</tr></table>\n"
2503 ""
2504 );
2505
2506 if (appctx->ctx.stats.st_code) {
2507 switch (appctx->ctx.stats.st_code) {
2508 case STAT_STATUS_DONE:
2509 chunk_appendf(&trash,
2510 "<p><div class=active_up>"
2511 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2512 "Action processed successfully."
2513 "</div>\n", uri->uri_prefix,
2514 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2515 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2516 scope_txt);
2517 break;
2518 case STAT_STATUS_NONE:
2519 chunk_appendf(&trash,
2520 "<p><div class=active_going_down>"
2521 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2522 "Nothing has changed."
2523 "</div>\n", uri->uri_prefix,
2524 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2525 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2526 scope_txt);
2527 break;
2528 case STAT_STATUS_PART:
2529 chunk_appendf(&trash,
2530 "<p><div class=active_going_down>"
2531 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2532 "Action partially processed.<br>"
2533 "Some server names are probably unknown or ambiguous (duplicated names in the backend)."
2534 "</div>\n", 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 break;
2539 case STAT_STATUS_ERRP:
2540 chunk_appendf(&trash,
2541 "<p><div class=active_down>"
2542 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2543 "Action not processed because of invalid parameters."
2544 "<ul>"
2545 "<li>The action is maybe unknown.</li>"
2546 "<li>The backend name is probably unknown or ambiguous (duplicated names).</li>"
2547 "<li>Some server names are probably unknown or ambiguous (duplicated names in the backend).</li>"
2548 "</ul>"
2549 "</div>\n", uri->uri_prefix,
2550 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2551 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2552 scope_txt);
2553 break;
2554 case STAT_STATUS_EXCD:
2555 chunk_appendf(&trash,
2556 "<p><div class=active_down>"
2557 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2558 "<b>Action not processed : the buffer couldn't store all the data.<br>"
2559 "You should retry with less servers at a time.</b>"
2560 "</div>\n", uri->uri_prefix,
2561 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2562 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2563 scope_txt);
2564 break;
2565 case STAT_STATUS_DENY:
2566 chunk_appendf(&trash,
2567 "<p><div class=active_down>"
2568 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2569 "<b>Action denied.</b>"
2570 "</div>\n", uri->uri_prefix,
2571 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2572 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2573 scope_txt);
2574 break;
2575 default:
2576 chunk_appendf(&trash,
2577 "<p><div class=active_no_check>"
2578 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2579 "Unexpected result."
2580 "</div>\n", uri->uri_prefix,
2581 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2582 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2583 scope_txt);
2584 }
2585 chunk_appendf(&trash, "<p>\n");
2586 }
2587}
2588
2589/* Dumps the HTML stats trailer block to the trash. The caller is responsible
2590 * for clearing the trash if needed.
2591 */
2592static void stats_dump_html_end()
2593{
2594 chunk_appendf(&trash, "</body></html>\n");
2595}
2596
Simon Horman05ee2132017-01-04 09:37:25 +01002597/* Dumps the stats JSON header to the trash buffer which. The caller is responsible
2598 * for clearing it if needed.
2599 */
2600static void stats_dump_json_header()
2601{
2602 chunk_strcat(&trash, "[");
2603}
2604
2605
2606/* Dumps the JSON stats trailer block to the trash. The caller is responsible
2607 * for clearing the trash if needed.
2608 */
2609static void stats_dump_json_end()
2610{
2611 chunk_strcat(&trash, "]");
2612}
2613
William Lallemand74c24fb2016-11-21 17:18:36 +01002614/* This function dumps statistics onto the stream interface's read buffer in
2615 * either CSV or HTML format. <uri> contains some HTML-specific parameters that
2616 * are ignored for CSV format (hence <uri> may be NULL there). It returns 0 if
2617 * it had to stop writing data and an I/O is needed, 1 if the dump is finished
2618 * and the stream must be closed, or -1 in case of any error. This function is
2619 * used by both the CLI and the HTTP handlers.
2620 */
Christopher Fauletef779222018-10-31 08:47:01 +01002621static int stats_dump_stat_to_buffer(struct stream_interface *si, struct htx *htx,
2622 struct uri_auth *uri)
William Lallemand74c24fb2016-11-21 17:18:36 +01002623{
2624 struct appctx *appctx = __objt_appctx(si->end);
2625 struct channel *rep = si_ic(si);
2626 struct proxy *px;
2627
2628 chunk_reset(&trash);
2629
2630 switch (appctx->st2) {
2631 case STAT_ST_INIT:
2632 appctx->st2 = STAT_ST_HEAD; /* let's start producing data */
2633 /* fall through */
2634
2635 case STAT_ST_HEAD:
2636 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
2637 stats_dump_html_head(uri);
Simon Horman05ee2132017-01-04 09:37:25 +01002638 else if (appctx->ctx.stats.flags & STAT_FMT_JSON)
Willy Tarreau9d7fb632017-04-11 07:53:04 +02002639 stats_dump_json_header();
William Lallemand74c24fb2016-11-21 17:18:36 +01002640 else if (!(appctx->ctx.stats.flags & STAT_FMT_TYPED))
2641 stats_dump_csv_header();
2642
Christopher Fauletef779222018-10-31 08:47:01 +01002643 if (!stats_putchk(rep, htx, &trash))
2644 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002645
2646 appctx->st2 = STAT_ST_INFO;
2647 /* fall through */
2648
2649 case STAT_ST_INFO:
2650 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
2651 stats_dump_html_info(si, uri);
Christopher Fauletef779222018-10-31 08:47:01 +01002652 if (!stats_putchk(rep, htx, &trash))
2653 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002654 }
2655
Olivier Houchardfbc74e82017-11-24 16:54:05 +01002656 appctx->ctx.stats.px = proxies_list;
William Lallemand74c24fb2016-11-21 17:18:36 +01002657 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
2658 appctx->st2 = STAT_ST_LIST;
2659 /* fall through */
2660
2661 case STAT_ST_LIST:
2662 /* dump proxies */
2663 while (appctx->ctx.stats.px) {
Christopher Fauletef779222018-10-31 08:47:01 +01002664 if (htx) {
2665 if (htx_almost_full(htx))
2666 goto full;
2667 }
2668 else {
2669 if (buffer_almost_full(&rep->buf))
2670 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002671 }
2672
2673 px = appctx->ctx.stats.px;
2674 /* skip the disabled proxies, global frontend and non-networked ones */
2675 if (px->state != PR_STSTOPPED && px->uuid > 0 && (px->cap & (PR_CAP_FE | PR_CAP_BE)))
Christopher Fauletef779222018-10-31 08:47:01 +01002676 if (stats_dump_proxy_to_buffer(si, htx, px, uri) == 0)
William Lallemand74c24fb2016-11-21 17:18:36 +01002677 return 0;
2678
2679 appctx->ctx.stats.px = px->next;
2680 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
2681 }
2682 /* here, we just have reached the last proxy */
2683
2684 appctx->st2 = STAT_ST_END;
2685 /* fall through */
2686
2687 case STAT_ST_END:
Simon Horman05ee2132017-01-04 09:37:25 +01002688 if (appctx->ctx.stats.flags & (STAT_FMT_HTML|STAT_FMT_JSON)) {
2689 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
2690 stats_dump_html_end();
2691 else
2692 stats_dump_json_end();
Christopher Fauletef779222018-10-31 08:47:01 +01002693 if (!stats_putchk(rep, htx, &trash))
2694 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002695 }
2696
2697 appctx->st2 = STAT_ST_FIN;
2698 /* fall through */
2699
2700 case STAT_ST_FIN:
2701 return 1;
2702
2703 default:
2704 /* unknown state ! */
2705 appctx->st2 = STAT_ST_FIN;
2706 return -1;
2707 }
Christopher Fauletef779222018-10-31 08:47:01 +01002708
2709 full:
2710 si_rx_room_blk(si);
2711 return 0;
2712
William Lallemand74c24fb2016-11-21 17:18:36 +01002713}
2714
2715/* We reached the stats page through a POST request. The appctx is
2716 * expected to have already been allocated by the caller.
2717 * Parse the posted data and enable/disable servers if necessary.
2718 * Returns 1 if request was parsed or zero if it needs more data.
2719 */
2720static int stats_process_http_post(struct stream_interface *si)
2721{
2722 struct stream *s = si_strm(si);
2723 struct appctx *appctx = objt_appctx(si->end);
2724
2725 struct proxy *px = NULL;
2726 struct server *sv = NULL;
2727
2728 char key[LINESIZE];
2729 int action = ST_ADM_ACTION_NONE;
2730 int reprocess = 0;
2731
2732 int total_servers = 0;
2733 int altered_servers = 0;
2734
2735 char *first_param, *cur_param, *next_param, *end_params;
2736 char *st_cur_param = NULL;
2737 char *st_next_param = NULL;
2738
Christopher Fauleta3618372018-12-13 21:59:56 +01002739 struct buffer *temp = get_trash_chunk();
William Lallemand74c24fb2016-11-21 17:18:36 +01002740
Christopher Fauleta3618372018-12-13 21:59:56 +01002741 if (IS_HTX_STRM(s)) {
2742 struct htx *htx = htxbuf(&s->req.buf);
2743 struct htx_blk *blk;
2744 size_t count = co_data(&s->req);
2745
Christopher Fauleted7a0662019-01-14 11:07:34 +01002746 /* we need more data */
2747 if (htx->extra || htx->data > count) {
2748 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
2749 return 0;
2750 }
2751
2752 /* Skip the headers */
Christopher Fauleta3618372018-12-13 21:59:56 +01002753 blk = htx_get_head_blk(htx);
2754 while (count && blk) {
2755 enum htx_blk_type type = htx_get_blk_type(blk);
2756 uint32_t sz = htx_get_blksz(blk);
2757
2758 if (sz > count) {
2759 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
2760 return 0;
2761 }
2762
2763 count -= sz;
Christopher Fauleted7a0662019-01-14 11:07:34 +01002764 blk = htx_get_next_blk(htx, blk);
Christopher Fauleta3618372018-12-13 21:59:56 +01002765
2766 if (type == HTX_BLK_EOH)
2767 break;
2768 }
2769
William Lallemand74c24fb2016-11-21 17:18:36 +01002770 /* too large request */
Christopher Fauleted7a0662019-01-14 11:07:34 +01002771 if (count > b_size(temp)) {
Christopher Fauleta3618372018-12-13 21:59:56 +01002772 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
2773 goto out;
2774 }
William Lallemand74c24fb2016-11-21 17:18:36 +01002775
Christopher Fauleta3618372018-12-13 21:59:56 +01002776 while (count && blk) {
2777 enum htx_blk_type type = htx_get_blk_type(blk);
2778 uint32_t sz = htx_get_blksz(blk);
2779
2780 if (type == HTX_BLK_EOM || type == HTX_BLK_EOD)
2781 break;
2782 if (type == HTX_BLK_DATA) {
2783 struct ist v = htx_get_blk_value(htx, blk);
2784
2785 if (!chunk_memcat(temp, v.ptr, v.len)) {
2786 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
2787 goto out;
2788 }
2789 }
2790
2791 count -= sz;
Christopher Fauleted7a0662019-01-14 11:07:34 +01002792 blk = htx_get_next_blk(htx, blk);
Christopher Fauleta3618372018-12-13 21:59:56 +01002793 }
2794 }
2795 else {
2796 int reql;
2797
2798 if (temp->size < s->txn->req.body_len) {
2799 /* too large request */
2800 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
2801 goto out;
2802 }
2803
2804 reql = co_getblk(si_oc(si), temp->area, s->txn->req.body_len,
2805 s->txn->req.eoh + 2);
2806 if (reql <= 0) {
2807 /* we need more data */
2808 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
2809 return 0;
2810 }
2811 temp->data = reql;
William Lallemand74c24fb2016-11-21 17:18:36 +01002812 }
2813
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002814 first_param = temp->area;
Christopher Fauleta3618372018-12-13 21:59:56 +01002815 end_params = temp->area + temp->data;
William Lallemand74c24fb2016-11-21 17:18:36 +01002816 cur_param = next_param = end_params;
2817 *end_params = '\0';
2818
2819 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
2820
2821 /*
2822 * Parse the parameters in reverse order to only store the last value.
2823 * From the html form, the backend and the action are at the end.
2824 */
2825 while (cur_param > first_param) {
2826 char *value;
2827 int poffset, plen;
2828
2829 cur_param--;
2830
2831 if ((*cur_param == '&') || (cur_param == first_param)) {
2832 reprocess_servers:
2833 /* Parse the key */
2834 poffset = (cur_param != first_param ? 1 : 0);
2835 plen = next_param - cur_param + (cur_param == first_param ? 1 : 0);
2836 if ((plen > 0) && (plen <= sizeof(key))) {
2837 strncpy(key, cur_param + poffset, plen);
2838 key[plen - 1] = '\0';
2839 } else {
2840 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
2841 goto out;
2842 }
2843
2844 /* Parse the value */
2845 value = key;
2846 while (*value != '\0' && *value != '=') {
2847 value++;
2848 }
2849 if (*value == '=') {
2850 /* Ok, a value is found, we can mark the end of the key */
2851 *value++ = '\0';
2852 }
2853 if (url_decode(key) < 0 || url_decode(value) < 0)
2854 break;
2855
2856 /* Now we can check the key to see what to do */
2857 if (!px && (strcmp(key, "b") == 0)) {
2858 if ((px = proxy_be_by_name(value)) == NULL) {
2859 /* the backend name is unknown or ambiguous (duplicate names) */
2860 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
2861 goto out;
2862 }
2863 }
2864 else if (!action && (strcmp(key, "action") == 0)) {
2865 if (strcmp(value, "ready") == 0) {
2866 action = ST_ADM_ACTION_READY;
2867 }
2868 else if (strcmp(value, "drain") == 0) {
2869 action = ST_ADM_ACTION_DRAIN;
2870 }
2871 else if (strcmp(value, "maint") == 0) {
2872 action = ST_ADM_ACTION_MAINT;
2873 }
2874 else if (strcmp(value, "shutdown") == 0) {
2875 action = ST_ADM_ACTION_SHUTDOWN;
2876 }
2877 else if (strcmp(value, "dhlth") == 0) {
2878 action = ST_ADM_ACTION_DHLTH;
2879 }
2880 else if (strcmp(value, "ehlth") == 0) {
2881 action = ST_ADM_ACTION_EHLTH;
2882 }
2883 else if (strcmp(value, "hrunn") == 0) {
2884 action = ST_ADM_ACTION_HRUNN;
2885 }
2886 else if (strcmp(value, "hnolb") == 0) {
2887 action = ST_ADM_ACTION_HNOLB;
2888 }
2889 else if (strcmp(value, "hdown") == 0) {
2890 action = ST_ADM_ACTION_HDOWN;
2891 }
2892 else if (strcmp(value, "dagent") == 0) {
2893 action = ST_ADM_ACTION_DAGENT;
2894 }
2895 else if (strcmp(value, "eagent") == 0) {
2896 action = ST_ADM_ACTION_EAGENT;
2897 }
2898 else if (strcmp(value, "arunn") == 0) {
2899 action = ST_ADM_ACTION_ARUNN;
2900 }
2901 else if (strcmp(value, "adown") == 0) {
2902 action = ST_ADM_ACTION_ADOWN;
2903 }
2904 /* else these are the old supported methods */
2905 else if (strcmp(value, "disable") == 0) {
2906 action = ST_ADM_ACTION_DISABLE;
2907 }
2908 else if (strcmp(value, "enable") == 0) {
2909 action = ST_ADM_ACTION_ENABLE;
2910 }
2911 else if (strcmp(value, "stop") == 0) {
2912 action = ST_ADM_ACTION_STOP;
2913 }
2914 else if (strcmp(value, "start") == 0) {
2915 action = ST_ADM_ACTION_START;
2916 }
2917 else {
2918 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
2919 goto out;
2920 }
2921 }
2922 else if (strcmp(key, "s") == 0) {
2923 if (!(px && action)) {
2924 /*
2925 * Indicates that we'll need to reprocess the parameters
2926 * as soon as backend and action are known
2927 */
2928 if (!reprocess) {
2929 st_cur_param = cur_param;
2930 st_next_param = next_param;
2931 }
2932 reprocess = 1;
2933 }
2934 else if ((sv = findserver(px, value)) != NULL) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002935 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
William Lallemand74c24fb2016-11-21 17:18:36 +01002936 switch (action) {
2937 case ST_ADM_ACTION_DISABLE:
Emeric Brun52a91d32017-08-31 14:41:55 +02002938 if (!(sv->cur_admin & SRV_ADMF_FMAINT)) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002939 altered_servers++;
2940 total_servers++;
2941 srv_set_admin_flag(sv, SRV_ADMF_FMAINT, "'disable' on stats page");
2942 }
2943 break;
2944 case ST_ADM_ACTION_ENABLE:
Emeric Brun52a91d32017-08-31 14:41:55 +02002945 if (sv->cur_admin & SRV_ADMF_FMAINT) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002946 altered_servers++;
2947 total_servers++;
2948 srv_clr_admin_flag(sv, SRV_ADMF_FMAINT);
2949 }
2950 break;
2951 case ST_ADM_ACTION_STOP:
Emeric Brun52a91d32017-08-31 14:41:55 +02002952 if (!(sv->cur_admin & SRV_ADMF_FDRAIN)) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002953 srv_set_admin_flag(sv, SRV_ADMF_FDRAIN, "'stop' on stats page");
2954 altered_servers++;
2955 total_servers++;
2956 }
2957 break;
2958 case ST_ADM_ACTION_START:
Emeric Brun52a91d32017-08-31 14:41:55 +02002959 if (sv->cur_admin & SRV_ADMF_FDRAIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002960 srv_clr_admin_flag(sv, SRV_ADMF_FDRAIN);
2961 altered_servers++;
2962 total_servers++;
2963 }
2964 break;
2965 case ST_ADM_ACTION_DHLTH:
2966 if (sv->check.state & CHK_ST_CONFIGURED) {
2967 sv->check.state &= ~CHK_ST_ENABLED;
2968 altered_servers++;
2969 total_servers++;
2970 }
2971 break;
2972 case ST_ADM_ACTION_EHLTH:
2973 if (sv->check.state & CHK_ST_CONFIGURED) {
2974 sv->check.state |= CHK_ST_ENABLED;
2975 altered_servers++;
2976 total_servers++;
2977 }
2978 break;
2979 case ST_ADM_ACTION_HRUNN:
2980 if (!(sv->track)) {
2981 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02002982 srv_set_running(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01002983 altered_servers++;
2984 total_servers++;
2985 }
2986 break;
2987 case ST_ADM_ACTION_HNOLB:
2988 if (!(sv->track)) {
2989 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02002990 srv_set_stopping(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01002991 altered_servers++;
2992 total_servers++;
2993 }
2994 break;
2995 case ST_ADM_ACTION_HDOWN:
2996 if (!(sv->track)) {
2997 sv->check.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02002998 srv_set_stopped(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01002999 altered_servers++;
3000 total_servers++;
3001 }
3002 break;
3003 case ST_ADM_ACTION_DAGENT:
3004 if (sv->agent.state & CHK_ST_CONFIGURED) {
3005 sv->agent.state &= ~CHK_ST_ENABLED;
3006 altered_servers++;
3007 total_servers++;
3008 }
3009 break;
3010 case ST_ADM_ACTION_EAGENT:
3011 if (sv->agent.state & CHK_ST_CONFIGURED) {
3012 sv->agent.state |= CHK_ST_ENABLED;
3013 altered_servers++;
3014 total_servers++;
3015 }
3016 break;
3017 case ST_ADM_ACTION_ARUNN:
3018 if (sv->agent.state & CHK_ST_ENABLED) {
3019 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02003020 srv_set_running(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003021 altered_servers++;
3022 total_servers++;
3023 }
3024 break;
3025 case ST_ADM_ACTION_ADOWN:
3026 if (sv->agent.state & CHK_ST_ENABLED) {
3027 sv->agent.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02003028 srv_set_stopped(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003029 altered_servers++;
3030 total_servers++;
3031 }
3032 break;
3033 case ST_ADM_ACTION_READY:
3034 srv_adm_set_ready(sv);
3035 altered_servers++;
3036 total_servers++;
3037 break;
3038 case ST_ADM_ACTION_DRAIN:
3039 srv_adm_set_drain(sv);
3040 altered_servers++;
3041 total_servers++;
3042 break;
3043 case ST_ADM_ACTION_MAINT:
3044 srv_adm_set_maint(sv);
3045 altered_servers++;
3046 total_servers++;
3047 break;
3048 case ST_ADM_ACTION_SHUTDOWN:
3049 if (px->state != PR_STSTOPPED) {
3050 struct stream *sess, *sess_bck;
3051
3052 list_for_each_entry_safe(sess, sess_bck, &sv->actconns, by_srv)
3053 if (sess->srv_conn == sv)
3054 stream_shutdown(sess, SF_ERR_KILLED);
3055
3056 altered_servers++;
3057 total_servers++;
3058 }
3059 break;
3060 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003061 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Lallemand74c24fb2016-11-21 17:18:36 +01003062 } else {
3063 /* the server name is unknown or ambiguous (duplicate names) */
3064 total_servers++;
3065 }
3066 }
3067 if (reprocess && px && action) {
3068 /* Now, we know the backend and the action chosen by the user.
3069 * We can safely restart from the first server parameter
3070 * to reprocess them
3071 */
3072 cur_param = st_cur_param;
3073 next_param = st_next_param;
3074 reprocess = 0;
3075 goto reprocess_servers;
3076 }
3077
3078 next_param = cur_param;
3079 }
3080 }
3081
3082 if (total_servers == 0) {
3083 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
3084 }
3085 else if (altered_servers == 0) {
3086 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
3087 }
3088 else if (altered_servers == total_servers) {
3089 appctx->ctx.stats.st_code = STAT_STATUS_DONE;
3090 }
3091 else {
3092 appctx->ctx.stats.st_code = STAT_STATUS_PART;
3093 }
3094 out:
3095 return 1;
Christopher Fauletef779222018-10-31 08:47:01 +01003096}
3097
3098
3099static int stats_send_htx_headers(struct stream_interface *si, struct htx *htx)
3100{
3101 struct stream *s = si_strm(si);
3102 struct uri_auth *uri = s->be->uri_auth;
3103 struct appctx *appctx = __objt_appctx(si->end);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003104 struct htx_sl *sl;
3105 unsigned int flags;
Christopher Fauletef779222018-10-31 08:47:01 +01003106
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003107 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);
3108 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist("200"), ist("OK"));
3109 if (!sl)
Christopher Fauletef779222018-10-31 08:47:01 +01003110 goto full;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003111 sl->info.res.status = 200;
Christopher Fauletef779222018-10-31 08:47:01 +01003112
3113 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")) ||
3114 !htx_add_header(htx, ist("Connection"), ist("close")))
3115 goto full;
3116 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
3117 if (!htx_add_header(htx, ist("Content-Type"), ist("text/html")))
3118 goto full;
3119 }
3120 else {
3121 if (!htx_add_header(htx, ist("Content-Type"), ist("text/plain")))
3122 goto full;
3123 }
3124
3125 if (uri->refresh > 0 && !(appctx->ctx.stats.flags & STAT_NO_REFRESH)) {
3126 const char *refresh = U2A(uri->refresh);
3127 if (!htx_add_header(htx, ist("Refresh"), ist2(refresh, strlen(refresh))))
3128 goto full;
3129 }
3130
3131 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
3132 if (!htx_add_header(htx, ist("Transfer-Encoding"), ist("chunked")))
3133 goto full;
3134 }
3135
3136 if (!htx_add_endof(htx, HTX_BLK_EOH))
3137 goto full;
3138
Christopher Faulet1e2d6362019-02-27 16:28:48 +01003139 channel_add_input(&s->res, htx->data);
Christopher Fauletef779222018-10-31 08:47:01 +01003140 return 1;
3141
3142 full:
3143 htx_reset(htx);
3144 si_rx_room_blk(si);
3145 return 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01003146}
3147
Christopher Fauletef779222018-10-31 08:47:01 +01003148
3149static int stats_send_htx_redirect(struct stream_interface *si, struct htx *htx)
3150{
3151 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
3152 struct stream *s = si_strm(si);
3153 struct uri_auth *uri = s->be->uri_auth;
3154 struct appctx *appctx = __objt_appctx(si->end);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003155 struct htx_sl *sl;
3156 unsigned int flags;
Christopher Fauletef779222018-10-31 08:47:01 +01003157
3158 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
3159 scope_txt[0] = 0;
3160 if (appctx->ctx.stats.scope_len) {
Christopher Fauleted7a0662019-01-14 11:07:34 +01003161 const char *scope_ptr = stats_scope_ptr(appctx, si);
3162
Christopher Fauletef779222018-10-31 08:47:01 +01003163 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Christopher Fauleted7a0662019-01-14 11:07:34 +01003164 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), scope_ptr, appctx->ctx.stats.scope_len);
Christopher Fauletef779222018-10-31 08:47:01 +01003165 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
3166 }
3167
3168 /* We don't want to land on the posted stats page because a refresh will
3169 * repost the data. We don't want this to happen on accident so we redirect
3170 * the browse to the stats page with a GET.
3171 */
3172 chunk_printf(&trash, "%s;st=%s%s%s%s",
3173 uri->uri_prefix,
3174 ((appctx->ctx.stats.st_code > STAT_STATUS_INIT) &&
3175 (appctx->ctx.stats.st_code < STAT_STATUS_SIZE) &&
3176 stat_status_codes[appctx->ctx.stats.st_code]) ?
3177 stat_status_codes[appctx->ctx.stats.st_code] :
3178 stat_status_codes[STAT_STATUS_UNKN],
3179 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3180 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3181 scope_txt);
3182
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003183 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_CHNK);
3184 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist("303"), ist("See Other"));
3185 if (!sl)
Christopher Fauletef779222018-10-31 08:47:01 +01003186 goto full;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003187 sl->info.res.status = 303;
Christopher Fauletef779222018-10-31 08:47:01 +01003188
3189 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")) ||
3190 !htx_add_header(htx, ist("Connection"), ist("close")) ||
3191 !htx_add_header(htx, ist("Content-Type"), ist("text/plain")) ||
3192 !htx_add_header(htx, ist("Content-Length"), ist("0")) ||
3193 !htx_add_header(htx, ist("Location"), ist2(trash.area, trash.data)))
3194 goto full;
3195
3196 if (!htx_add_endof(htx, HTX_BLK_EOH))
3197 goto full;
3198
Christopher Faulet1e2d6362019-02-27 16:28:48 +01003199 channel_add_input(&s->res, htx->data);
Christopher Fauletef779222018-10-31 08:47:01 +01003200 return 1;
3201
3202full:
3203 htx_reset(htx);
3204 si_rx_room_blk(si);
3205 return 0;
3206}
William Lallemand74c24fb2016-11-21 17:18:36 +01003207
3208static int stats_send_http_headers(struct stream_interface *si)
3209{
3210 struct stream *s = si_strm(si);
3211 struct uri_auth *uri = s->be->uri_auth;
Willy Tarreau21ff2c42018-09-20 11:01:01 +02003212 struct appctx *appctx = __objt_appctx(si->end);
William Lallemand74c24fb2016-11-21 17:18:36 +01003213
3214 chunk_printf(&trash,
3215 "HTTP/1.1 200 OK\r\n"
3216 "Cache-Control: no-cache\r\n"
3217 "Connection: close\r\n"
3218 "Content-Type: %s\r\n",
3219 (appctx->ctx.stats.flags & STAT_FMT_HTML) ? "text/html" : "text/plain");
3220
3221 if (uri->refresh > 0 && !(appctx->ctx.stats.flags & STAT_NO_REFRESH))
3222 chunk_appendf(&trash, "Refresh: %d\r\n",
3223 uri->refresh);
3224
3225 /* we don't send the CRLF in chunked mode, it will be sent with the first chunk's size */
3226
3227 if (appctx->ctx.stats.flags & STAT_CHUNKED)
3228 chunk_appendf(&trash, "Transfer-Encoding: chunked\r\n");
3229 else
3230 chunk_appendf(&trash, "\r\n");
3231
Willy Tarreau06d80a92017-10-19 14:32:15 +02003232 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01003233 si_rx_room_blk(si);
William Lallemand74c24fb2016-11-21 17:18:36 +01003234 return 0;
3235 }
3236
3237 return 1;
3238}
3239
3240static int stats_send_http_redirect(struct stream_interface *si)
3241{
3242 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
3243 struct stream *s = si_strm(si);
3244 struct uri_auth *uri = s->be->uri_auth;
Willy Tarreau21ff2c42018-09-20 11:01:01 +02003245 struct appctx *appctx = __objt_appctx(si->end);
William Lallemand74c24fb2016-11-21 17:18:36 +01003246
3247 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
3248 scope_txt[0] = 0;
3249 if (appctx->ctx.stats.scope_len) {
3250 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau89faf5d2018-06-07 18:16:48 +02003251 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 +01003252 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
3253 }
3254
3255 /* We don't want to land on the posted stats page because a refresh will
3256 * repost the data. We don't want this to happen on accident so we redirect
3257 * the browse to the stats page with a GET.
3258 */
3259 chunk_printf(&trash,
3260 "HTTP/1.1 303 See Other\r\n"
3261 "Cache-Control: no-cache\r\n"
3262 "Content-Type: text/plain\r\n"
3263 "Connection: close\r\n"
3264 "Location: %s;st=%s%s%s%s\r\n"
3265 "Content-length: 0\r\n"
3266 "\r\n",
3267 uri->uri_prefix,
3268 ((appctx->ctx.stats.st_code > STAT_STATUS_INIT) &&
3269 (appctx->ctx.stats.st_code < STAT_STATUS_SIZE) &&
3270 stat_status_codes[appctx->ctx.stats.st_code]) ?
3271 stat_status_codes[appctx->ctx.stats.st_code] :
3272 stat_status_codes[STAT_STATUS_UNKN],
3273 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3274 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3275 scope_txt);
3276
Willy Tarreau06d80a92017-10-19 14:32:15 +02003277 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01003278 si_rx_room_blk(si);
William Lallemand74c24fb2016-11-21 17:18:36 +01003279 return 0;
3280 }
3281
3282 return 1;
3283}
3284
Simon Horman05ee2132017-01-04 09:37:25 +01003285
William Lallemand74c24fb2016-11-21 17:18:36 +01003286/* This I/O handler runs as an applet embedded in a stream interface. It is
3287 * used to send HTTP stats over a TCP socket. The mechanism is very simple.
3288 * appctx->st0 contains the operation in progress (dump, done). The handler
3289 * automatically unregisters itself once transfer is complete.
3290 */
Christopher Fauletef779222018-10-31 08:47:01 +01003291static void htx_stats_io_handler(struct appctx *appctx)
3292{
3293 struct stream_interface *si = appctx->owner;
3294 struct stream *s = si_strm(si);
3295 struct channel *req = si_oc(si);
3296 struct channel *res = si_ic(si);
3297 struct htx *req_htx, *res_htx;
3298
3299 res_htx = htx_from_buf(&res->buf);
3300
3301 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
3302 goto out;
3303
3304 /* Check if the input buffer is avalaible. */
3305 if (!b_size(&res->buf)) {
3306 si_rx_room_blk(si);
3307 goto out;
3308 }
3309
3310 /* check that the output is not closed */
3311 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW))
3312 appctx->st0 = STAT_HTTP_DONE;
3313
3314 /* all states are processed in sequence */
3315 if (appctx->st0 == STAT_HTTP_HEAD) {
3316 if (stats_send_htx_headers(si, res_htx)) {
3317 if (s->txn->meth == HTTP_METH_HEAD)
3318 appctx->st0 = STAT_HTTP_DONE;
3319 else
3320 appctx->st0 = STAT_HTTP_DUMP;
3321 }
3322 }
3323
3324 if (appctx->st0 == STAT_HTTP_DUMP) {
3325 if (stats_dump_stat_to_buffer(si, res_htx, s->be->uri_auth))
3326 appctx->st0 = STAT_HTTP_DONE;
3327 }
3328
3329 if (appctx->st0 == STAT_HTTP_POST) {
3330 if (stats_process_http_post(si))
3331 appctx->st0 = STAT_HTTP_LAST;
3332 else if (si_oc(si)->flags & CF_SHUTR)
3333 appctx->st0 = STAT_HTTP_DONE;
3334 }
3335
3336 if (appctx->st0 == STAT_HTTP_LAST) {
3337 if (stats_send_htx_redirect(si, res_htx))
3338 appctx->st0 = STAT_HTTP_DONE;
3339 }
3340
3341 if (appctx->st0 == STAT_HTTP_DONE) {
Christopher Fauletb1b08212018-11-21 13:51:07 +01003342 /* Don't add EOD and TLR because mux-h1 will take care of it */
Christopher Fauletef779222018-10-31 08:47:01 +01003343 if (!htx_add_endof(res_htx, HTX_BLK_EOM)) {
3344 si_rx_room_blk(si);
3345 goto out;
3346 }
3347
3348 /* eat the whole request */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003349 req_htx = htxbuf(&req->buf);
3350 htx_reset(req_htx);
3351 htx_to_buf(req_htx, &req->buf);
Christopher Faulet9b95d312018-11-19 21:58:10 +01003352 co_set_data(req, 0);
Christopher Fauletef779222018-10-31 08:47:01 +01003353 res->flags |= CF_READ_NULL;
3354 si_shutr(si);
3355 }
3356
3357 if ((res->flags & CF_SHUTR) && (si->state == SI_ST_EST))
3358 si_shutw(si);
3359
3360 if (appctx->st0 == STAT_HTTP_DONE) {
3361 if ((req->flags & CF_SHUTW) && (si->state == SI_ST_EST)) {
3362 si_shutr(si);
3363 res->flags |= CF_READ_NULL;
3364 }
3365 }
3366 out:
3367 /* we have left the request in the buffer for the case where we
3368 * process a POST, and this automatically re-enables activity on
3369 * read. It's better to indicate that we want to stop reading when
3370 * we're sending, so that we know there's at most one direction
3371 * deciding to wake the applet up. It saves it from looping when
3372 * emitting large blocks into small TCP windows.
3373 */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003374 htx_to_buf(res_htx, &res->buf);
3375 if (!channel_is_empty(res))
Christopher Fauletef779222018-10-31 08:47:01 +01003376 si_stop_get(si);
Christopher Fauletef779222018-10-31 08:47:01 +01003377}
3378
3379
3380/* This I/O handler runs as an applet embedded in a stream interface. It is
3381 * used to send HTTP stats over a TCP socket. The mechanism is very simple.
3382 * appctx->st0 contains the operation in progress (dump, done). The handler
3383 * automatically unregisters itself once transfer is complete.
3384 */
William Lallemand74c24fb2016-11-21 17:18:36 +01003385static void http_stats_io_handler(struct appctx *appctx)
3386{
3387 struct stream_interface *si = appctx->owner;
3388 struct stream *s = si_strm(si);
3389 struct channel *req = si_oc(si);
3390 struct channel *res = si_ic(si);
3391
Christopher Fauletef779222018-10-31 08:47:01 +01003392 if (IS_HTX_STRM(s))
3393 return htx_stats_io_handler(appctx);
3394
William Lallemand74c24fb2016-11-21 17:18:36 +01003395 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
3396 goto out;
3397
Joseph Herlant29023ec2018-11-15 13:39:46 -08003398 /* Check if the input buffer is available. */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003399 if (res->buf.size == 0) {
Willy Tarreau4b962a42018-11-15 11:03:21 +01003400 /* already subscribed, we'll be called later once the buffer is
3401 * available.
3402 */
Christopher Fauleta73e59b2016-12-09 17:30:18 +01003403 goto out;
3404 }
3405
William Lallemand74c24fb2016-11-21 17:18:36 +01003406 /* check that the output is not closed */
3407 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW))
3408 appctx->st0 = STAT_HTTP_DONE;
3409
3410 /* all states are processed in sequence */
3411 if (appctx->st0 == STAT_HTTP_HEAD) {
3412 if (stats_send_http_headers(si)) {
3413 if (s->txn->meth == HTTP_METH_HEAD)
3414 appctx->st0 = STAT_HTTP_DONE;
3415 else
3416 appctx->st0 = STAT_HTTP_DUMP;
3417 }
3418 }
3419
3420 if (appctx->st0 == STAT_HTTP_DUMP) {
Willy Tarreau97f538b2018-06-15 19:41:31 +02003421 unsigned int prev_len = ci_data(si_ic(si));
William Lallemand74c24fb2016-11-21 17:18:36 +01003422 unsigned int data_len;
3423 unsigned int last_len;
3424 unsigned int last_fwd = 0;
3425
3426 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
3427 /* One difficulty we're facing is that we must prevent
3428 * the input data from being automatically forwarded to
3429 * the output area. For this, we temporarily disable
3430 * forwarding on the channel.
3431 */
3432 last_fwd = si_ic(si)->to_forward;
3433 si_ic(si)->to_forward = 0;
3434 chunk_printf(&trash, "\r\n000000\r\n");
Willy Tarreau06d80a92017-10-19 14:32:15 +02003435 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01003436 si_rx_room_blk(si);
William Lallemand74c24fb2016-11-21 17:18:36 +01003437 si_ic(si)->to_forward = last_fwd;
3438 goto out;
3439 }
3440 }
3441
Willy Tarreau97f538b2018-06-15 19:41:31 +02003442 data_len = ci_data(si_ic(si));
Christopher Fauletef779222018-10-31 08:47:01 +01003443 if (stats_dump_stat_to_buffer(si, NULL, s->be->uri_auth))
William Lallemand74c24fb2016-11-21 17:18:36 +01003444 appctx->st0 = STAT_HTTP_DONE;
3445
Willy Tarreau97f538b2018-06-15 19:41:31 +02003446 last_len = ci_data(si_ic(si));
William Lallemand74c24fb2016-11-21 17:18:36 +01003447
3448 /* Now we must either adjust or remove the chunk size. This is
3449 * not easy because the chunk size might wrap at the end of the
3450 * buffer, so we pretend we have nothing in the buffer, we write
3451 * the size, then restore the buffer's contents. Note that we can
3452 * only do that because no forwarding is scheduled on the stats
3453 * applet.
3454 */
3455 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
3456 si_ic(si)->total -= (last_len - prev_len);
Willy Tarreau97f538b2018-06-15 19:41:31 +02003457 b_sub(si_ib(si), (last_len - prev_len));
William Lallemand74c24fb2016-11-21 17:18:36 +01003458
3459 if (last_len != data_len) {
3460 chunk_printf(&trash, "\r\n%06x\r\n", (last_len - data_len));
Willy Tarreau06d80a92017-10-19 14:32:15 +02003461 if (ci_putchk(si_ic(si), &trash) == -1)
Willy Tarreaudb398432018-11-15 11:08:52 +01003462 si_rx_room_blk(si);
William Lallemand74c24fb2016-11-21 17:18:36 +01003463
3464 si_ic(si)->total += (last_len - data_len);
Willy Tarreau97f538b2018-06-15 19:41:31 +02003465 b_add(si_ib(si), last_len - data_len);
William Lallemand74c24fb2016-11-21 17:18:36 +01003466 }
3467 /* now re-enable forwarding */
3468 channel_forward(si_ic(si), last_fwd);
3469 }
3470 }
3471
3472 if (appctx->st0 == STAT_HTTP_POST) {
3473 if (stats_process_http_post(si))
3474 appctx->st0 = STAT_HTTP_LAST;
3475 else if (si_oc(si)->flags & CF_SHUTR)
3476 appctx->st0 = STAT_HTTP_DONE;
3477 }
3478
3479 if (appctx->st0 == STAT_HTTP_LAST) {
3480 if (stats_send_http_redirect(si))
3481 appctx->st0 = STAT_HTTP_DONE;
3482 }
3483
3484 if (appctx->st0 == STAT_HTTP_DONE) {
3485 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
3486 chunk_printf(&trash, "\r\n0\r\n\r\n");
Willy Tarreau06d80a92017-10-19 14:32:15 +02003487 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01003488 si_rx_room_blk(si);
William Lallemand74c24fb2016-11-21 17:18:36 +01003489 goto out;
3490 }
3491 }
3492 /* eat the whole request */
Willy Tarreau97f538b2018-06-15 19:41:31 +02003493 co_skip(si_oc(si), co_data(si_oc(si)));
William Lallemand74c24fb2016-11-21 17:18:36 +01003494 res->flags |= CF_READ_NULL;
3495 si_shutr(si);
3496 }
3497
3498 if ((res->flags & CF_SHUTR) && (si->state == SI_ST_EST))
3499 si_shutw(si);
3500
3501 if (appctx->st0 == STAT_HTTP_DONE) {
3502 if ((req->flags & CF_SHUTW) && (si->state == SI_ST_EST)) {
3503 si_shutr(si);
3504 res->flags |= CF_READ_NULL;
3505 }
3506 }
3507 out:
Willy Tarreau055ba4f2018-07-24 17:05:54 +02003508 /* we have left the request in the buffer for the case where we
3509 * process a POST, and this automatically re-enables activity on
3510 * read. It's better to indicate that we want to stop reading when
3511 * we're sending, so that we know there's at most one direction
3512 * deciding to wake the applet up. It saves it from looping when
3513 * emitting large blocks into small TCP windows.
3514 */
3515 if (!channel_is_empty(res))
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01003516 si_stop_get(si);
William Lallemand74c24fb2016-11-21 17:18:36 +01003517}
3518
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003519/* Dump all fields from <info> into <out> using the "show info" format (name: value) */
Willy Tarreau83061a82018-07-13 11:56:34 +02003520static int stats_dump_info_fields(struct buffer *out,
3521 const struct field *info)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003522{
3523 int field;
3524
3525 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
3526 if (!field_format(info, field))
3527 continue;
3528
3529 if (!chunk_appendf(out, "%s: ", info_field_names[field]))
3530 return 0;
3531 if (!stats_emit_raw_data_field(out, &info[field]))
3532 return 0;
3533 if (!chunk_strcat(out, "\n"))
3534 return 0;
3535 }
3536 return 1;
3537}
3538
3539/* Dump all fields from <info> into <out> using the "show info typed" format */
Willy Tarreau83061a82018-07-13 11:56:34 +02003540static int stats_dump_typed_info_fields(struct buffer *out,
3541 const struct field *info)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003542{
3543 int field;
3544
3545 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
3546 if (!field_format(info, field))
3547 continue;
3548
3549 if (!chunk_appendf(out, "%d.%s.%u:", field, info_field_names[field], info[INF_PROCESS_NUM].u.u32))
3550 return 0;
3551 if (!stats_emit_field_tags(out, &info[field], ':'))
3552 return 0;
3553 if (!stats_emit_typed_data_field(out, &info[field]))
3554 return 0;
3555 if (!chunk_strcat(out, "\n"))
3556 return 0;
3557 }
3558 return 1;
3559}
3560
3561/* Fill <info> with HAProxy global info. <info> is preallocated
3562 * array of length <len>. The length of the aray must be
3563 * INF_TOTAL_FIELDS. If this length is less then this value, the
3564 * function returns 0, otherwise, it returns 1.
3565 */
3566int stats_fill_info(struct field *info, int len)
3567{
3568 unsigned int up = (now.tv_sec - start_date.tv_sec);
Willy Tarreau83061a82018-07-13 11:56:34 +02003569 struct buffer *out = get_trash_chunk();
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003570
3571#ifdef USE_OPENSSL
3572 int ssl_sess_rate = read_freq_ctr(&global.ssl_per_sec);
3573 int ssl_key_rate = read_freq_ctr(&global.ssl_fe_keys_per_sec);
3574 int ssl_reuse = 0;
3575
3576 if (ssl_key_rate < ssl_sess_rate) {
3577 /* count the ssl reuse ratio and avoid overflows in both directions */
3578 ssl_reuse = 100 - (100 * ssl_key_rate + (ssl_sess_rate - 1) / 2) / ssl_sess_rate;
3579 }
3580#endif
3581
3582 if (len < INF_TOTAL_FIELDS)
3583 return 0;
3584
3585 chunk_reset(out);
3586 memset(info, 0, sizeof(*info) * len);
3587
3588 info[INF_NAME] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, PRODUCT_NAME);
Willy Tarreau909b9d82019-01-04 18:20:32 +01003589 info[INF_VERSION] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, haproxy_version);
3590 info[INF_RELEASE_DATE] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, haproxy_date);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003591
Yves Lafon95317282018-02-26 11:10:37 +01003592 info[INF_NBTHREAD] = mkf_u32(FO_CONFIG|FS_SERVICE, global.nbthread);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003593 info[INF_NBPROC] = mkf_u32(FO_CONFIG|FS_SERVICE, global.nbproc);
3594 info[INF_PROCESS_NUM] = mkf_u32(FO_KEY, relative_pid);
3595 info[INF_PID] = mkf_u32(FO_STATUS, pid);
3596
3597 info[INF_UPTIME] = mkf_str(FN_DURATION, chunk_newstr(out));
3598 chunk_appendf(out, "%ud %uh%02um%02us", up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60));
3599
3600 info[INF_UPTIME_SEC] = mkf_u32(FN_DURATION, up);
3601 info[INF_MEMMAX_MB] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_memmax);
3602 info[INF_POOL_ALLOC_MB] = mkf_u32(0, (unsigned)(pool_total_allocated() / 1048576L));
3603 info[INF_POOL_USED_MB] = mkf_u32(0, (unsigned)(pool_total_used() / 1048576L));
3604 info[INF_POOL_FAILED] = mkf_u32(FN_COUNTER, pool_total_failures());
3605 info[INF_ULIMIT_N] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_nofile);
3606 info[INF_MAXSOCK] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxsock);
3607 info[INF_MAXCONN] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxconn);
3608 info[INF_HARD_MAXCONN] = mkf_u32(FO_CONFIG|FN_LIMIT, global.hardmaxconn);
3609 info[INF_CURR_CONN] = mkf_u32(0, actconn);
3610 info[INF_CUM_CONN] = mkf_u32(FN_COUNTER, totalconn);
3611 info[INF_CUM_REQ] = mkf_u32(FN_COUNTER, global.req_count);
3612#ifdef USE_OPENSSL
3613 info[INF_MAX_SSL_CONNS] = mkf_u32(FN_MAX, global.maxsslconn);
3614 info[INF_CURR_SSL_CONNS] = mkf_u32(0, sslconns);
3615 info[INF_CUM_SSL_CONNS] = mkf_u32(FN_COUNTER, totalsslconns);
3616#endif
3617 info[INF_MAXPIPES] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxpipes);
3618 info[INF_PIPES_USED] = mkf_u32(0, pipes_used);
3619 info[INF_PIPES_FREE] = mkf_u32(0, pipes_free);
3620 info[INF_CONN_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.conn_per_sec));
3621 info[INF_CONN_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.cps_lim);
3622 info[INF_MAX_CONN_RATE] = mkf_u32(FN_MAX, global.cps_max);
3623 info[INF_SESS_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.sess_per_sec));
3624 info[INF_SESS_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.sps_lim);
3625 info[INF_MAX_SESS_RATE] = mkf_u32(FN_RATE, global.sps_max);
3626
3627#ifdef USE_OPENSSL
3628 info[INF_SSL_RATE] = mkf_u32(FN_RATE, ssl_sess_rate);
3629 info[INF_SSL_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.ssl_lim);
3630 info[INF_MAX_SSL_RATE] = mkf_u32(FN_MAX, global.ssl_max);
3631 info[INF_SSL_FRONTEND_KEY_RATE] = mkf_u32(0, ssl_key_rate);
3632 info[INF_SSL_FRONTEND_MAX_KEY_RATE] = mkf_u32(FN_MAX, global.ssl_fe_keys_max);
3633 info[INF_SSL_FRONTEND_SESSION_REUSE_PCT] = mkf_u32(0, ssl_reuse);
3634 info[INF_SSL_BACKEND_KEY_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.ssl_be_keys_per_sec));
3635 info[INF_SSL_BACKEND_MAX_KEY_RATE] = mkf_u32(FN_MAX, global.ssl_be_keys_max);
3636 info[INF_SSL_CACHE_LOOKUPS] = mkf_u32(FN_COUNTER, global.shctx_lookups);
3637 info[INF_SSL_CACHE_MISSES] = mkf_u32(FN_COUNTER, global.shctx_misses);
3638#endif
3639 info[INF_COMPRESS_BPS_IN] = mkf_u32(FN_RATE, read_freq_ctr(&global.comp_bps_in));
3640 info[INF_COMPRESS_BPS_OUT] = mkf_u32(FN_RATE, read_freq_ctr(&global.comp_bps_out));
3641 info[INF_COMPRESS_BPS_RATE_LIM] = mkf_u32(FO_CONFIG|FN_LIMIT, global.comp_rate_lim);
3642#ifdef USE_ZLIB
3643 info[INF_ZLIB_MEM_USAGE] = mkf_u32(0, zlib_used_memory);
3644 info[INF_MAX_ZLIB_MEM_USAGE] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxzlibmem);
3645#endif
3646 info[INF_TASKS] = mkf_u32(0, nb_tasks_cur);
Christopher Faulet34c5cc92016-12-06 09:15:30 +01003647 info[INF_RUN_QUEUE] = mkf_u32(0, tasks_run_queue_cur);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003648 info[INF_IDLE_PCT] = mkf_u32(FN_AVG, idle_pct);
3649 info[INF_NODE] = mkf_str(FO_CONFIG|FN_OUTPUT|FS_SERVICE, global.node);
3650 if (global.desc)
3651 info[INF_DESCRIPTION] = mkf_str(FO_CONFIG|FN_OUTPUT|FS_SERVICE, global.desc);
Willy Tarreau00098ea2018-11-05 14:38:13 +01003652 info[INF_STOPPING] = mkf_u32(0, stopping);
3653 info[INF_JOBS] = mkf_u32(0, jobs);
William Lallemanda7199262018-11-16 16:57:20 +01003654 info[INF_UNSTOPPABLE_JOBS] = mkf_u32(0, unstoppable_jobs);
Willy Tarreau00098ea2018-11-05 14:38:13 +01003655 info[INF_LISTENERS] = mkf_u32(0, listeners);
Willy Tarreau199ad242018-11-05 16:31:22 +01003656 info[INF_ACTIVE_PEERS] = mkf_u32(0, active_peers);
Willy Tarreau2d372c22018-11-05 17:12:27 +01003657 info[INF_CONNECTED_PEERS] = mkf_u32(0, connected_peers);
Willy Tarreau13ef7732018-11-12 07:25:28 +01003658 info[INF_DROPPED_LOGS] = mkf_u32(0, dropped_logs);
Willy Tarreaubeb859a2018-11-22 18:07:59 +01003659 info[INF_BUSY_POLLING] = mkf_u32(0, !!(global.tune.options & GTUNE_BUSY_POLLING));
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003660
3661 return 1;
3662}
3663
3664/* This function dumps information onto the stream interface's read buffer.
3665 * It returns 0 as long as it does not complete, non-zero upon completion.
3666 * No state is used.
3667 */
3668static int stats_dump_info_to_buffer(struct stream_interface *si)
3669{
3670 struct appctx *appctx = __objt_appctx(si->end);
3671
3672 if (!stats_fill_info(info, INF_TOTAL_FIELDS))
3673 return 0;
3674
3675 chunk_reset(&trash);
3676
3677 if (appctx->ctx.stats.flags & STAT_FMT_TYPED)
3678 stats_dump_typed_info_fields(&trash, info);
Simon Horman05ee2132017-01-04 09:37:25 +01003679 else if (appctx->ctx.stats.flags & STAT_FMT_JSON)
3680 stats_dump_json_info_fields(&trash, info);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003681 else
3682 stats_dump_info_fields(&trash, info);
3683
Willy Tarreau06d80a92017-10-19 14:32:15 +02003684 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01003685 si_rx_room_blk(si);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003686 return 0;
3687 }
3688
3689 return 1;
3690}
3691
Simon Horman6f6bb382017-01-04 09:37:26 +01003692/* This function dumps the schema onto the stream interface's read buffer.
3693 * It returns 0 as long as it does not complete, non-zero upon completion.
3694 * No state is used.
3695 *
3696 * Integer values bouned to the range [-(2**53)+1, (2**53)-1] as
3697 * per the recommendation for interoperable integers in section 6 of RFC 7159.
3698 */
Willy Tarreau83061a82018-07-13 11:56:34 +02003699static void stats_dump_json_schema(struct buffer *out)
Simon Horman6f6bb382017-01-04 09:37:26 +01003700{
3701
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003702 int old_len = out->data;
Simon Horman6f6bb382017-01-04 09:37:26 +01003703
3704 chunk_strcat(out,
3705 "{"
3706 "\"$schema\":\"http://json-schema.org/draft-04/schema#\","
3707 "\"oneOf\":["
3708 "{"
3709 "\"title\":\"Info\","
3710 "\"type\":\"array\","
3711 "\"items\":{"
3712 "\"properties\":{"
3713 "\"title\":\"InfoItem\","
3714 "\"type\":\"object\","
3715 "\"field\":{\"$ref\":\"#/definitions/field\"},"
3716 "\"processNum\":{\"$ref\":\"#/definitions/processNum\"},"
3717 "\"tags\":{\"$ref\":\"#/definitions/tags\"},"
3718 "\"value\":{\"$ref\":\"#/definitions/typedValue\"}"
3719 "},"
3720 "\"required\":[\"field\",\"processNum\",\"tags\","
3721 "\"value\"]"
3722 "}"
3723 "},"
3724 "{"
3725 "\"title\":\"Stat\","
3726 "\"type\":\"array\","
3727 "\"items\":{"
3728 "\"title\":\"InfoItem\","
3729 "\"type\":\"object\","
3730 "\"properties\":{"
3731 "\"objType\":{"
3732 "\"enum\":[\"Frontend\",\"Backend\",\"Listener\","
3733 "\"Server\",\"Unknown\"]"
3734 "},"
3735 "\"proxyId\":{"
3736 "\"type\":\"integer\","
3737 "\"minimum\":0"
3738 "},"
3739 "\"id\":{"
3740 "\"type\":\"integer\","
3741 "\"minimum\":0"
3742 "},"
3743 "\"field\":{\"$ref\":\"#/definitions/field\"},"
3744 "\"processNum\":{\"$ref\":\"#/definitions/processNum\"},"
3745 "\"tags\":{\"$ref\":\"#/definitions/tags\"},"
3746 "\"typedValue\":{\"$ref\":\"#/definitions/typedValue\"}"
3747 "},"
3748 "\"required\":[\"objType\",\"proxyId\",\"id\","
3749 "\"field\",\"processNum\",\"tags\","
3750 "\"value\"]"
3751 "}"
3752 "},"
3753 "{"
3754 "\"title\":\"Error\","
3755 "\"type\":\"object\","
3756 "\"properties\":{"
3757 "\"errorStr\":{"
3758 "\"type\":\"string\""
3759 "},"
3760 "\"required\":[\"errorStr\"]"
3761 "}"
3762 "}"
3763 "],"
3764 "\"definitions\":{"
3765 "\"field\":{"
3766 "\"type\":\"object\","
3767 "\"pos\":{"
3768 "\"type\":\"integer\","
3769 "\"minimum\":0"
3770 "},"
3771 "\"name\":{"
3772 "\"type\":\"string\""
3773 "},"
3774 "\"required\":[\"pos\",\"name\"]"
3775 "},"
3776 "\"processNum\":{"
3777 "\"type\":\"integer\","
3778 "\"minimum\":1"
3779 "},"
3780 "\"tags\":{"
3781 "\"type\":\"object\","
3782 "\"origin\":{"
3783 "\"type\":\"string\","
3784 "\"enum\":[\"Metric\",\"Status\",\"Key\","
3785 "\"Config\",\"Product\",\"Unknown\"]"
3786 "},"
3787 "\"nature\":{"
3788 "\"type\":\"string\","
3789 "\"enum\":[\"Gauge\",\"Limit\",\"Min\",\"Max\","
3790 "\"Rate\",\"Counter\",\"Duration\","
3791 "\"Age\",\"Time\",\"Name\",\"Output\","
3792 "\"Avg\", \"Unknown\"]"
3793 "},"
3794 "\"scope\":{"
3795 "\"type\":\"string\","
3796 "\"enum\":[\"Cluster\",\"Process\",\"Service\","
3797 "\"System\",\"Unknown\"]"
3798 "},"
3799 "\"required\":[\"origin\",\"nature\",\"scope\"]"
3800 "},"
3801 "\"typedValue\":{"
3802 "\"type\":\"object\","
3803 "\"oneOf\":["
3804 "{\"$ref\":\"#/definitions/typedValue/definitions/s32Value\"},"
3805 "{\"$ref\":\"#/definitions/typedValue/definitions/s64Value\"},"
3806 "{\"$ref\":\"#/definitions/typedValue/definitions/u32Value\"},"
3807 "{\"$ref\":\"#/definitions/typedValue/definitions/u64Value\"},"
3808 "{\"$ref\":\"#/definitions/typedValue/definitions/strValue\"}"
3809 "],"
3810 "\"definitions\":{"
3811 "\"s32Value\":{"
3812 "\"properties\":{"
3813 "\"type\":{"
3814 "\"type\":\"string\","
3815 "\"enum\":[\"s32\"]"
3816 "},"
3817 "\"value\":{"
3818 "\"type\":\"integer\","
3819 "\"minimum\":-2147483648,"
3820 "\"maximum\":2147483647"
3821 "}"
3822 "},"
3823 "\"required\":[\"type\",\"value\"]"
3824 "},"
3825 "\"s64Value\":{"
3826 "\"properties\":{"
3827 "\"type\":{"
3828 "\"type\":\"string\","
3829 "\"enum\":[\"s64\"]"
3830 "},"
3831 "\"value\":{"
3832 "\"type\":\"integer\","
3833 "\"minimum\":-9007199254740991,"
3834 "\"maximum\":9007199254740991"
3835 "}"
3836 "},"
3837 "\"required\":[\"type\",\"value\"]"
3838 "},"
3839 "\"u32Value\":{"
3840 "\"properties\":{"
3841 "\"type\":{"
3842 "\"type\":\"string\","
3843 "\"enum\":[\"u32\"]"
3844 "},"
3845 "\"value\":{"
3846 "\"type\":\"integer\","
3847 "\"minimum\":0,"
3848 "\"maximum\":4294967295"
3849 "}"
3850 "},"
3851 "\"required\":[\"type\",\"value\"]"
3852 "},"
3853 "\"u64Value\":{"
3854 "\"properties\":{"
3855 "\"type\":{"
3856 "\"type\":\"string\","
3857 "\"enum\":[\"u64\"]"
3858 "},"
3859 "\"value\":{"
3860 "\"type\":\"integer\","
3861 "\"minimum\":0,"
3862 "\"maximum\":9007199254740991"
3863 "}"
3864 "},"
3865 "\"required\":[\"type\",\"value\"]"
3866 "},"
3867 "\"strValue\":{"
3868 "\"properties\":{"
3869 "\"type\":{"
3870 "\"type\":\"string\","
3871 "\"enum\":[\"str\"]"
3872 "},"
3873 "\"value\":{\"type\":\"string\"}"
3874 "},"
3875 "\"required\":[\"type\",\"value\"]"
3876 "},"
3877 "\"unknownValue\":{"
3878 "\"properties\":{"
3879 "\"type\":{"
3880 "\"type\":\"integer\","
3881 "\"minimum\":0"
3882 "},"
3883 "\"value\":{"
3884 "\"type\":\"string\","
3885 "\"enum\":[\"unknown\"]"
3886 "}"
3887 "},"
3888 "\"required\":[\"type\",\"value\"]"
3889 "}"
3890 "}"
3891 "}"
3892 "}"
3893 "}");
3894
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003895 if (old_len == out->data) {
Simon Horman6f6bb382017-01-04 09:37:26 +01003896 chunk_reset(out);
3897 chunk_appendf(out,
3898 "{\"errorStr\":\"output buffer too short\"}");
3899 }
3900}
3901
3902/* This function dumps the schema onto the stream interface's read buffer.
3903 * It returns 0 as long as it does not complete, non-zero upon completion.
3904 * No state is used.
3905 */
3906static int stats_dump_json_schema_to_buffer(struct stream_interface *si)
3907{
3908 chunk_reset(&trash);
3909
3910 stats_dump_json_schema(&trash);
3911
Willy Tarreau06d80a92017-10-19 14:32:15 +02003912 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01003913 si_rx_room_blk(si);
Simon Horman6f6bb382017-01-04 09:37:26 +01003914 return 0;
3915 }
3916
3917 return 1;
3918}
3919
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02003920static int cli_parse_clear_counters(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau89d467c2016-11-23 11:02:40 +01003921{
3922 struct proxy *px;
3923 struct server *sv;
3924 struct listener *li;
3925 int clrall = 0;
3926
3927 if (strcmp(args[2], "all") == 0)
3928 clrall = 1;
3929
3930 /* check permissions */
3931 if (!cli_has_level(appctx, ACCESS_LVL_OPER) ||
3932 (clrall && !cli_has_level(appctx, ACCESS_LVL_ADMIN)))
3933 return 1;
3934
Olivier Houchardfbc74e82017-11-24 16:54:05 +01003935 for (px = proxies_list; px; px = px->next) {
Willy Tarreau89d467c2016-11-23 11:02:40 +01003936 if (clrall) {
3937 memset(&px->be_counters, 0, sizeof(px->be_counters));
3938 memset(&px->fe_counters, 0, sizeof(px->fe_counters));
3939 }
3940 else {
3941 px->be_counters.conn_max = 0;
3942 px->be_counters.p.http.rps_max = 0;
3943 px->be_counters.sps_max = 0;
3944 px->be_counters.cps_max = 0;
3945 px->be_counters.nbpend_max = 0;
3946
3947 px->fe_counters.conn_max = 0;
3948 px->fe_counters.p.http.rps_max = 0;
3949 px->fe_counters.sps_max = 0;
3950 px->fe_counters.cps_max = 0;
Willy Tarreau89d467c2016-11-23 11:02:40 +01003951 }
3952
3953 for (sv = px->srv; sv; sv = sv->next)
3954 if (clrall)
3955 memset(&sv->counters, 0, sizeof(sv->counters));
3956 else {
3957 sv->counters.cur_sess_max = 0;
3958 sv->counters.nbpend_max = 0;
3959 sv->counters.sps_max = 0;
3960 }
3961
3962 list_for_each_entry(li, &px->conf.listeners, by_fe)
3963 if (li->counters) {
3964 if (clrall)
3965 memset(li->counters, 0, sizeof(*li->counters));
3966 else
3967 li->counters->conn_max = 0;
3968 }
3969 }
3970
3971 global.cps_max = 0;
3972 global.sps_max = 0;
Olivier Houchard00bc3cb2017-10-17 19:23:25 +02003973 global.ssl_max = 0;
3974 global.ssl_fe_keys_max = 0;
3975 global.ssl_be_keys_max = 0;
Willy Tarreaud80cb4e2018-01-20 19:30:13 +01003976
3977 memset(activity, 0, sizeof(activity));
Willy Tarreau89d467c2016-11-23 11:02:40 +01003978 return 1;
3979}
3980
3981
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02003982static int cli_parse_show_info(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003983{
Willy Tarreaud25fc792016-12-16 12:33:47 +01003984 appctx->ctx.stats.scope_str = 0;
3985 appctx->ctx.stats.scope_len = 0;
3986 appctx->ctx.stats.flags = 0;
3987
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003988 if (strcmp(args[2], "typed") == 0)
3989 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
Simon Horman05ee2132017-01-04 09:37:25 +01003990 else if (strcmp(args[2], "json") == 0)
3991 appctx->ctx.stats.flags |= STAT_FMT_JSON;
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003992 return 0;
3993}
3994
3995
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02003996static int cli_parse_show_stat(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau2b812e22016-11-22 16:18:05 +01003997{
Willy Tarreaud25fc792016-12-16 12:33:47 +01003998 appctx->ctx.stats.scope_str = 0;
3999 appctx->ctx.stats.scope_len = 0;
4000 appctx->ctx.stats.flags = 0;
4001
Willy Tarreau2b812e22016-11-22 16:18:05 +01004002 if (*args[2] && *args[3] && *args[4]) {
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01004003 struct proxy *px;
4004
4005 px = proxy_find_by_name(args[2], 0, 0);
4006 if (px)
4007 appctx->ctx.stats.iid = px->uuid;
4008 else
4009 appctx->ctx.stats.iid = atoi(args[2]);
4010
4011 if (!appctx->ctx.stats.iid) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004012 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01004013 appctx->ctx.cli.msg = "No such proxy.\n";
4014 appctx->st0 = CLI_ST_PRINT;
4015 return 1;
4016 }
4017
Willy Tarreau2b812e22016-11-22 16:18:05 +01004018 appctx->ctx.stats.flags |= STAT_BOUND;
Willy Tarreau2b812e22016-11-22 16:18:05 +01004019 appctx->ctx.stats.type = atoi(args[3]);
4020 appctx->ctx.stats.sid = atoi(args[4]);
4021 if (strcmp(args[5], "typed") == 0)
4022 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
Simon Horman05ee2132017-01-04 09:37:25 +01004023 else if (strcmp(args[5], "json") == 0)
4024 appctx->ctx.stats.flags |= STAT_FMT_JSON;
Willy Tarreau2b812e22016-11-22 16:18:05 +01004025 }
4026 else if (strcmp(args[2], "typed") == 0)
4027 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
Simon Horman05ee2132017-01-04 09:37:25 +01004028 else if (strcmp(args[2], "json") == 0)
4029 appctx->ctx.stats.flags |= STAT_FMT_JSON;
Willy Tarreau2b812e22016-11-22 16:18:05 +01004030
Willy Tarreau2b812e22016-11-22 16:18:05 +01004031 return 0;
4032}
4033
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004034static int cli_io_handler_dump_info(struct appctx *appctx)
4035{
4036 return stats_dump_info_to_buffer(appctx->owner);
4037}
4038
Willy Tarreau2b812e22016-11-22 16:18:05 +01004039/* This I/O handler runs as an applet embedded in a stream interface. It is
4040 * used to send raw stats over a socket.
4041 */
4042static int cli_io_handler_dump_stat(struct appctx *appctx)
4043{
Christopher Fauletef779222018-10-31 08:47:01 +01004044 return stats_dump_stat_to_buffer(appctx->owner, NULL, NULL);
Willy Tarreau2b812e22016-11-22 16:18:05 +01004045}
4046
Simon Horman6f6bb382017-01-04 09:37:26 +01004047static int cli_io_handler_dump_json_schema(struct appctx *appctx)
4048{
4049 return stats_dump_json_schema_to_buffer(appctx->owner);
4050}
4051
Willy Tarreau2b812e22016-11-22 16:18:05 +01004052/* register cli keywords */
4053static struct cli_kw_list cli_kws = {{ },{
Willy Tarreau89d467c2016-11-23 11:02:40 +01004054 { { "clear", "counters", NULL }, "clear counters : clear max statistics counters (add 'all' for all counters)", cli_parse_clear_counters, NULL, NULL },
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004055 { { "show", "info", NULL }, "show info : report information about the running process", cli_parse_show_info, cli_io_handler_dump_info, NULL },
Willy Tarreau2b812e22016-11-22 16:18:05 +01004056 { { "show", "stat", NULL }, "show stat : report counters for each proxy and server", cli_parse_show_stat, cli_io_handler_dump_stat, NULL },
Simon Horman6f6bb382017-01-04 09:37:26 +01004057 { { "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 +01004058 {{},}
4059}};
4060
Willy Tarreau0108d902018-11-25 19:14:37 +01004061INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
4062
William Lallemand74c24fb2016-11-21 17:18:36 +01004063struct applet http_stats_applet = {
4064 .obj_type = OBJ_TYPE_APPLET,
4065 .name = "<STATS>", /* used for logging */
4066 .fct = http_stats_io_handler,
4067 .release = NULL,
4068};
4069
William Lallemand74c24fb2016-11-21 17:18:36 +01004070/*
4071 * Local variables:
4072 * c-indent-level: 8
4073 * c-basic-offset: 8
4074 * End:
4075 */