blob: dc57e857e4ad938a511a43c2095b0758a3262311 [file] [log] [blame]
William Lallemand74c24fb2016-11-21 17:18:36 +01001/*
2 * Functions dedicated to statistics output and the stats socket
3 *
4 * Copyright 2000-2012 Willy Tarreau <w@1wt.eu>
5 * Copyright 2007-2009 Krzysztof Piotr Oledzki <ole@ans.pl>
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
11 *
12 */
13
14#include <ctype.h>
15#include <errno.h>
16#include <fcntl.h>
17#include <stdio.h>
18#include <stdlib.h>
19#include <string.h>
20#include <pwd.h>
21#include <grp.h>
22
23#include <sys/socket.h>
24#include <sys/stat.h>
25#include <sys/types.h>
26
27#include <common/cfgparse.h>
28#include <common/compat.h>
29#include <common/config.h>
30#include <common/debug.h>
Willy Tarreau35b51c62018-09-10 15:38:55 +020031#include <common/http.h>
Willy Tarreaub96b77e2018-12-11 10:22:41 +010032#include <common/htx.h>
Willy Tarreau0108d902018-11-25 19:14:37 +010033#include <common/initcall.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010034#include <common/memory.h>
35#include <common/mini-clist.h>
36#include <common/standard.h>
37#include <common/ticks.h>
38#include <common/time.h>
39#include <common/uri_auth.h>
40#include <common/version.h>
41#include <common/base64.h>
42
43#include <types/applet.h>
Willy Tarreau2b812e22016-11-22 16:18:05 +010044#include <types/cli.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010045#include <types/global.h>
46#include <types/dns.h>
William Lallemand9ed62032016-11-21 17:49:11 +010047#include <types/stats.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010048
49#include <proto/backend.h>
50#include <proto/channel.h>
51#include <proto/checks.h>
Willy Tarreau2b812e22016-11-22 16:18:05 +010052#include <proto/cli.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010053#include <proto/compression.h>
Baptiste Assmann333939c2019-01-21 08:34:50 +010054#include <proto/dns.h>
William Lallemand9ed62032016-11-21 17:49:11 +010055#include <proto/stats.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010056#include <proto/fd.h>
57#include <proto/freq_ctr.h>
58#include <proto/frontend.h>
Christopher Fauleted7a0662019-01-14 11:07:34 +010059#include <proto/http_htx.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010060#include <proto/log.h>
61#include <proto/pattern.h>
62#include <proto/pipe.h>
63#include <proto/listener.h>
64#include <proto/map.h>
Christopher Fauletef779222018-10-31 08:47:01 +010065#include <proto/proto_http.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010066#include <proto/proxy.h>
67#include <proto/sample.h>
68#include <proto/session.h>
Willy Tarreauc125cef2019-05-10 09:58:43 +020069#include <proto/ssl_sock.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010070#include <proto/stream.h>
71#include <proto/server.h>
72#include <proto/raw_sock.h>
73#include <proto/stream_interface.h>
74#include <proto/task.h>
75
William Lallemand74c24fb2016-11-21 17:18:36 +010076
Christopher Faulet2b9b6782019-02-27 16:42:58 +010077/* status codes available for the stats admin page (strictly 4 chars length) */
78const char *stat_status_codes[STAT_STATUS_SIZE] = {
79 [STAT_STATUS_DENY] = "DENY",
80 [STAT_STATUS_DONE] = "DONE",
81 [STAT_STATUS_ERRP] = "ERRP",
82 [STAT_STATUS_EXCD] = "EXCD",
83 [STAT_STATUS_NONE] = "NONE",
84 [STAT_STATUS_PART] = "PART",
85 [STAT_STATUS_UNKN] = "UNKN",
86 [STAT_STATUS_IVAL] = "IVAL",
87};
88
Willy Tarreau0baac8c2016-11-22 16:36:53 +010089/* These are the field names for each INF_* field position. Please pay attention
90 * to always use the exact same name except that the strings for new names must
91 * be lower case or CamelCase while the enum entries must be upper case.
92 */
93const char *info_field_names[INF_TOTAL_FIELDS] = {
94 [INF_NAME] = "Name",
95 [INF_VERSION] = "Version",
96 [INF_RELEASE_DATE] = "Release_date",
Yves Lafon95317282018-02-26 11:10:37 +010097 [INF_NBTHREAD] = "Nbthread",
Willy Tarreau0baac8c2016-11-22 16:36:53 +010098 [INF_NBPROC] = "Nbproc",
99 [INF_PROCESS_NUM] = "Process_num",
100 [INF_PID] = "Pid",
101 [INF_UPTIME] = "Uptime",
102 [INF_UPTIME_SEC] = "Uptime_sec",
103 [INF_MEMMAX_MB] = "Memmax_MB",
104 [INF_POOL_ALLOC_MB] = "PoolAlloc_MB",
105 [INF_POOL_USED_MB] = "PoolUsed_MB",
106 [INF_POOL_FAILED] = "PoolFailed",
107 [INF_ULIMIT_N] = "Ulimit-n",
108 [INF_MAXSOCK] = "Maxsock",
109 [INF_MAXCONN] = "Maxconn",
110 [INF_HARD_MAXCONN] = "Hard_maxconn",
111 [INF_CURR_CONN] = "CurrConns",
112 [INF_CUM_CONN] = "CumConns",
113 [INF_CUM_REQ] = "CumReq",
114 [INF_MAX_SSL_CONNS] = "MaxSslConns",
115 [INF_CURR_SSL_CONNS] = "CurrSslConns",
116 [INF_CUM_SSL_CONNS] = "CumSslConns",
117 [INF_MAXPIPES] = "Maxpipes",
118 [INF_PIPES_USED] = "PipesUsed",
119 [INF_PIPES_FREE] = "PipesFree",
120 [INF_CONN_RATE] = "ConnRate",
121 [INF_CONN_RATE_LIMIT] = "ConnRateLimit",
122 [INF_MAX_CONN_RATE] = "MaxConnRate",
123 [INF_SESS_RATE] = "SessRate",
124 [INF_SESS_RATE_LIMIT] = "SessRateLimit",
125 [INF_MAX_SESS_RATE] = "MaxSessRate",
126 [INF_SSL_RATE] = "SslRate",
127 [INF_SSL_RATE_LIMIT] = "SslRateLimit",
128 [INF_MAX_SSL_RATE] = "MaxSslRate",
129 [INF_SSL_FRONTEND_KEY_RATE] = "SslFrontendKeyRate",
130 [INF_SSL_FRONTEND_MAX_KEY_RATE] = "SslFrontendMaxKeyRate",
131 [INF_SSL_FRONTEND_SESSION_REUSE_PCT] = "SslFrontendSessionReuse_pct",
132 [INF_SSL_BACKEND_KEY_RATE] = "SslBackendKeyRate",
133 [INF_SSL_BACKEND_MAX_KEY_RATE] = "SslBackendMaxKeyRate",
134 [INF_SSL_CACHE_LOOKUPS] = "SslCacheLookups",
135 [INF_SSL_CACHE_MISSES] = "SslCacheMisses",
136 [INF_COMPRESS_BPS_IN] = "CompressBpsIn",
137 [INF_COMPRESS_BPS_OUT] = "CompressBpsOut",
138 [INF_COMPRESS_BPS_RATE_LIM] = "CompressBpsRateLim",
139 [INF_ZLIB_MEM_USAGE] = "ZlibMemUsage",
140 [INF_MAX_ZLIB_MEM_USAGE] = "MaxZlibMemUsage",
141 [INF_TASKS] = "Tasks",
142 [INF_RUN_QUEUE] = "Run_queue",
143 [INF_IDLE_PCT] = "Idle_pct",
144 [INF_NODE] = "node",
145 [INF_DESCRIPTION] = "description",
Willy Tarreau00098ea2018-11-05 14:38:13 +0100146 [INF_STOPPING] = "Stopping",
147 [INF_JOBS] = "Jobs",
William Lallemanda7199262018-11-16 16:57:20 +0100148 [INF_UNSTOPPABLE_JOBS] = "Unstoppable Jobs",
Willy Tarreau00098ea2018-11-05 14:38:13 +0100149 [INF_LISTENERS] = "Listeners",
Willy Tarreau199ad242018-11-05 16:31:22 +0100150 [INF_ACTIVE_PEERS] = "ActivePeers",
Willy Tarreau2d372c22018-11-05 17:12:27 +0100151 [INF_CONNECTED_PEERS] = "ConnectedPeers",
Willy Tarreau13ef7732018-11-12 07:25:28 +0100152 [INF_DROPPED_LOGS] = "DroppedLogs",
Willy Tarreaubeb859a2018-11-22 18:07:59 +0100153 [INF_BUSY_POLLING] = "BusyPolling",
Baptiste Assmann333939c2019-01-21 08:34:50 +0100154 [INF_FAILED_RESOLUTIONS] = "FailedResolutions",
Willy Tarreau0baac8c2016-11-22 16:36:53 +0100155};
156
William Lallemand74c24fb2016-11-21 17:18:36 +0100157const char *stat_field_names[ST_F_TOTAL_FIELDS] = {
158 [ST_F_PXNAME] = "pxname",
159 [ST_F_SVNAME] = "svname",
160 [ST_F_QCUR] = "qcur",
161 [ST_F_QMAX] = "qmax",
162 [ST_F_SCUR] = "scur",
163 [ST_F_SMAX] = "smax",
164 [ST_F_SLIM] = "slim",
165 [ST_F_STOT] = "stot",
166 [ST_F_BIN] = "bin",
167 [ST_F_BOUT] = "bout",
168 [ST_F_DREQ] = "dreq",
169 [ST_F_DRESP] = "dresp",
170 [ST_F_EREQ] = "ereq",
171 [ST_F_ECON] = "econ",
172 [ST_F_ERESP] = "eresp",
173 [ST_F_WRETR] = "wretr",
174 [ST_F_WREDIS] = "wredis",
175 [ST_F_STATUS] = "status",
176 [ST_F_WEIGHT] = "weight",
177 [ST_F_ACT] = "act",
178 [ST_F_BCK] = "bck",
179 [ST_F_CHKFAIL] = "chkfail",
180 [ST_F_CHKDOWN] = "chkdown",
181 [ST_F_LASTCHG] = "lastchg",
182 [ST_F_DOWNTIME] = "downtime",
183 [ST_F_QLIMIT] = "qlimit",
184 [ST_F_PID] = "pid",
185 [ST_F_IID] = "iid",
186 [ST_F_SID] = "sid",
187 [ST_F_THROTTLE] = "throttle",
188 [ST_F_LBTOT] = "lbtot",
189 [ST_F_TRACKED] = "tracked",
190 [ST_F_TYPE] = "type",
191 [ST_F_RATE] = "rate",
192 [ST_F_RATE_LIM] = "rate_lim",
193 [ST_F_RATE_MAX] = "rate_max",
194 [ST_F_CHECK_STATUS] = "check_status",
195 [ST_F_CHECK_CODE] = "check_code",
196 [ST_F_CHECK_DURATION] = "check_duration",
197 [ST_F_HRSP_1XX] = "hrsp_1xx",
198 [ST_F_HRSP_2XX] = "hrsp_2xx",
199 [ST_F_HRSP_3XX] = "hrsp_3xx",
200 [ST_F_HRSP_4XX] = "hrsp_4xx",
201 [ST_F_HRSP_5XX] = "hrsp_5xx",
202 [ST_F_HRSP_OTHER] = "hrsp_other",
203 [ST_F_HANAFAIL] = "hanafail",
204 [ST_F_REQ_RATE] = "req_rate",
205 [ST_F_REQ_RATE_MAX] = "req_rate_max",
206 [ST_F_REQ_TOT] = "req_tot",
207 [ST_F_CLI_ABRT] = "cli_abrt",
208 [ST_F_SRV_ABRT] = "srv_abrt",
209 [ST_F_COMP_IN] = "comp_in",
210 [ST_F_COMP_OUT] = "comp_out",
211 [ST_F_COMP_BYP] = "comp_byp",
212 [ST_F_COMP_RSP] = "comp_rsp",
213 [ST_F_LASTSESS] = "lastsess",
214 [ST_F_LAST_CHK] = "last_chk",
215 [ST_F_LAST_AGT] = "last_agt",
216 [ST_F_QTIME] = "qtime",
217 [ST_F_CTIME] = "ctime",
218 [ST_F_RTIME] = "rtime",
219 [ST_F_TTIME] = "ttime",
220 [ST_F_AGENT_STATUS] = "agent_status",
221 [ST_F_AGENT_CODE] = "agent_code",
222 [ST_F_AGENT_DURATION] = "agent_duration",
223 [ST_F_CHECK_DESC] = "check_desc",
224 [ST_F_AGENT_DESC] = "agent_desc",
225 [ST_F_CHECK_RISE] = "check_rise",
226 [ST_F_CHECK_FALL] = "check_fall",
227 [ST_F_CHECK_HEALTH] = "check_health",
228 [ST_F_AGENT_RISE] = "agent_rise",
229 [ST_F_AGENT_FALL] = "agent_fall",
230 [ST_F_AGENT_HEALTH] = "agent_health",
231 [ST_F_ADDR] = "addr",
232 [ST_F_COOKIE] = "cookie",
233 [ST_F_MODE] = "mode",
234 [ST_F_ALGO] = "algo",
235 [ST_F_CONN_RATE] = "conn_rate",
236 [ST_F_CONN_RATE_MAX] = "conn_rate_max",
237 [ST_F_CONN_TOT] = "conn_tot",
238 [ST_F_INTERCEPTED] = "intercepted",
239 [ST_F_DCON] = "dcon",
240 [ST_F_DSES] = "dses",
Tim Duesterhus3fd19732018-05-27 20:35:08 +0200241 [ST_F_WREW] = "wrew",
Willy Tarreauf1573842018-12-14 11:35:36 +0100242 [ST_F_CONNECT] = "connect",
243 [ST_F_REUSE] = "reuse",
Willy Tarreaua1214a52018-12-14 14:00:25 +0100244 [ST_F_CACHE_LOOKUPS] = "cache_lookups",
245 [ST_F_CACHE_HITS] = "cache_hits",
William Lallemand74c24fb2016-11-21 17:18:36 +0100246};
247
Willy Tarreau0baac8c2016-11-22 16:36:53 +0100248/* one line of info */
Christopher Faulet1bc04c72017-10-29 20:14:08 +0100249static THREAD_LOCAL struct field info[INF_TOTAL_FIELDS];
William Lallemand74c24fb2016-11-21 17:18:36 +0100250/* one line of stats */
Christopher Faulet1bc04c72017-10-29 20:14:08 +0100251static THREAD_LOCAL struct field stats[ST_F_TOTAL_FIELDS];
William Lallemand74c24fb2016-11-21 17:18:36 +0100252
253
Christopher Fauletef779222018-10-31 08:47:01 +0100254static int stats_putchk(struct channel *chn, struct htx *htx, struct buffer *chk)
255{
256 if (htx) {
Christopher Faulet69fc88c2019-01-07 14:27:53 +0100257 if (chk->data >= channel_htx_recv_max(chn, htx))
258 return 0;
Christopher Fauletef779222018-10-31 08:47:01 +0100259 if (!htx_add_data(htx, ist2(chk->area, chk->data)))
260 return 0;
Christopher Faulet5adbeeb2019-01-02 14:34:39 +0100261 channel_add_input(chn, chk->data);
Christopher Fauletef779222018-10-31 08:47:01 +0100262 chk->data = 0;
Christopher Fauletef779222018-10-31 08:47:01 +0100263 }
264 else {
265 if (ci_putchk(chn, chk) == -1)
266 return 0;
267 }
268 return 1;
269}
Willy Tarreau0baac8c2016-11-22 16:36:53 +0100270
Christopher Fauleted7a0662019-01-14 11:07:34 +0100271static const char *stats_scope_ptr(struct appctx *appctx, struct stream_interface *si)
272{
273 const char *p;
274
275 if (IS_HTX_STRM(si_strm(si))) {
276 struct channel *req = si_oc(si);
277 struct htx *htx = htxbuf(&req->buf);
278 struct ist uri = htx_sl_req_uri(http_find_stline(htx));
279
280 p = uri.ptr;
281 }
282 else
283 p = co_head(si_oc(si));
284
285 return p + appctx->ctx.stats.scope_str;
286}
287
William Lallemand74c24fb2016-11-21 17:18:36 +0100288/*
289 * http_stats_io_handler()
290 * -> stats_dump_stat_to_buffer() // same as above, but used for CSV or HTML
291 * -> stats_dump_csv_header() // emits the CSV headers (same as above)
Simon Horman05ee2132017-01-04 09:37:25 +0100292 * -> stats_dump_json_header() // emits the JSON headers (same as above)
William Lallemand74c24fb2016-11-21 17:18:36 +0100293 * -> stats_dump_html_head() // emits the HTML headers
294 * -> stats_dump_html_info() // emits the equivalent of "show info" at the top
295 * -> stats_dump_proxy_to_buffer() // same as above, valid for CSV and HTML
296 * -> stats_dump_html_px_hdr()
297 * -> stats_dump_fe_stats()
298 * -> stats_dump_li_stats()
299 * -> stats_dump_sv_stats()
300 * -> stats_dump_be_stats()
301 * -> stats_dump_html_px_end()
302 * -> stats_dump_html_end() // emits HTML trailer
Simon Horman05ee2132017-01-04 09:37:25 +0100303 * -> stats_dump_json_end() // emits JSON trailer
William Lallemand74c24fb2016-11-21 17:18:36 +0100304 */
305
306
William Lallemand74c24fb2016-11-21 17:18:36 +0100307/* Dumps the stats CSV header to the trash buffer which. The caller is responsible
308 * for clearing it if needed.
309 * NOTE: Some tools happen to rely on the field position instead of its name,
310 * so please only append new fields at the end, never in the middle.
311 */
312static void stats_dump_csv_header()
313{
314 int field;
315
316 chunk_appendf(&trash, "# ");
317 for (field = 0; field < ST_F_TOTAL_FIELDS; field++)
318 chunk_appendf(&trash, "%s,", stat_field_names[field]);
319
320 chunk_appendf(&trash, "\n");
321}
322
323
324/* Emits a stats field without any surrounding element and properly encoded to
325 * resist CSV output. Returns non-zero on success, 0 if the buffer is full.
326 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200327int stats_emit_raw_data_field(struct buffer *out, const struct field *f)
William Lallemand74c24fb2016-11-21 17:18:36 +0100328{
329 switch (field_format(f, 0)) {
330 case FF_EMPTY: return 1;
331 case FF_S32: return chunk_appendf(out, "%d", f->u.s32);
332 case FF_U32: return chunk_appendf(out, "%u", f->u.u32);
333 case FF_S64: return chunk_appendf(out, "%lld", (long long)f->u.s64);
334 case FF_U64: return chunk_appendf(out, "%llu", (unsigned long long)f->u.u64);
335 case FF_STR: return csv_enc_append(field_str(f, 0), 1, out) != NULL;
336 default: return chunk_appendf(out, "[INCORRECT_FIELD_TYPE_%08x]", f->type);
337 }
338}
339
340/* Emits a stats field prefixed with its type. No CSV encoding is prepared, the
341 * output is supposed to be used on its own line. Returns non-zero on success, 0
342 * if the buffer is full.
343 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200344int stats_emit_typed_data_field(struct buffer *out, const struct field *f)
William Lallemand74c24fb2016-11-21 17:18:36 +0100345{
346 switch (field_format(f, 0)) {
347 case FF_EMPTY: return 1;
348 case FF_S32: return chunk_appendf(out, "s32:%d", f->u.s32);
349 case FF_U32: return chunk_appendf(out, "u32:%u", f->u.u32);
350 case FF_S64: return chunk_appendf(out, "s64:%lld", (long long)f->u.s64);
351 case FF_U64: return chunk_appendf(out, "u64:%llu", (unsigned long long)f->u.u64);
352 case FF_STR: return chunk_appendf(out, "str:%s", field_str(f, 0));
353 default: return chunk_appendf(out, "%08x:?", f->type);
354 }
355}
356
Simon Horman05ee2132017-01-04 09:37:25 +0100357/* Limit JSON integer values to the range [-(2**53)+1, (2**53)-1] as per
358 * the recommendation for interoperable integers in section 6 of RFC 7159.
359 */
360#define JSON_INT_MAX ((1ULL << 53) - 1)
361#define JSON_INT_MIN (0 - JSON_INT_MAX)
362
363/* Emits a stats field value and its type in JSON.
364 * Returns non-zero on success, 0 on error.
365 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200366int stats_emit_json_data_field(struct buffer *out, const struct field *f)
Simon Horman05ee2132017-01-04 09:37:25 +0100367{
368 int old_len;
369 char buf[20];
370 const char *type, *value = buf, *quote = "";
371
372 switch (field_format(f, 0)) {
373 case FF_EMPTY: return 1;
374 case FF_S32: type = "\"s32\"";
375 snprintf(buf, sizeof(buf), "%d", f->u.s32);
376 break;
377 case FF_U32: type = "\"u32\"";
378 snprintf(buf, sizeof(buf), "%u", f->u.u32);
379 break;
380 case FF_S64: type = "\"s64\"";
381 if (f->u.s64 < JSON_INT_MIN || f->u.s64 > JSON_INT_MAX)
382 return 0;
383 type = "\"s64\"";
384 snprintf(buf, sizeof(buf), "%lld", (long long)f->u.s64);
385 break;
386 case FF_U64: if (f->u.u64 > JSON_INT_MAX)
387 return 0;
388 type = "\"u64\"";
389 snprintf(buf, sizeof(buf), "%llu",
390 (unsigned long long) f->u.u64);
391 break;
392 case FF_STR: type = "\"str\"";
393 value = field_str(f, 0);
394 quote = "\"";
395 break;
396 default: snprintf(buf, sizeof(buf), "%u", f->type);
397 type = buf;
398 value = "unknown";
399 quote = "\"";
400 break;
401 }
402
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200403 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100404 chunk_appendf(out, ",\"value\":{\"type\":%s,\"value\":%s%s%s}",
405 type, quote, value, quote);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200406 return !(old_len == out->data);
Simon Horman05ee2132017-01-04 09:37:25 +0100407}
408
William Lallemand74c24fb2016-11-21 17:18:36 +0100409/* Emits an encoding of the field type on 3 characters followed by a delimiter.
410 * Returns non-zero on success, 0 if the buffer is full.
411 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200412int stats_emit_field_tags(struct buffer *out, const struct field *f,
413 char delim)
William Lallemand74c24fb2016-11-21 17:18:36 +0100414{
415 char origin, nature, scope;
416
417 switch (field_origin(f, 0)) {
418 case FO_METRIC: origin = 'M'; break;
419 case FO_STATUS: origin = 'S'; break;
420 case FO_KEY: origin = 'K'; break;
421 case FO_CONFIG: origin = 'C'; break;
422 case FO_PRODUCT: origin = 'P'; break;
423 default: origin = '?'; break;
424 }
425
426 switch (field_nature(f, 0)) {
427 case FN_GAUGE: nature = 'G'; break;
428 case FN_LIMIT: nature = 'L'; break;
429 case FN_MIN: nature = 'm'; break;
430 case FN_MAX: nature = 'M'; break;
431 case FN_RATE: nature = 'R'; break;
432 case FN_COUNTER: nature = 'C'; break;
433 case FN_DURATION: nature = 'D'; break;
434 case FN_AGE: nature = 'A'; break;
435 case FN_TIME: nature = 'T'; break;
436 case FN_NAME: nature = 'N'; break;
437 case FN_OUTPUT: nature = 'O'; break;
438 case FN_AVG: nature = 'a'; break;
439 default: nature = '?'; break;
440 }
441
442 switch (field_scope(f, 0)) {
443 case FS_PROCESS: scope = 'P'; break;
444 case FS_SERVICE: scope = 'S'; break;
445 case FS_SYSTEM: scope = 's'; break;
446 case FS_CLUSTER: scope = 'C'; break;
447 default: scope = '?'; break;
448 }
449
450 return chunk_appendf(out, "%c%c%c%c", origin, nature, scope, delim);
451}
452
Simon Horman05ee2132017-01-04 09:37:25 +0100453/* Emits an encoding of the field type as JSON.
454 * Returns non-zero on success, 0 if the buffer is full.
455 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200456int stats_emit_json_field_tags(struct buffer *out, const struct field *f)
Simon Horman05ee2132017-01-04 09:37:25 +0100457{
458 const char *origin, *nature, *scope;
459 int old_len;
460
461 switch (field_origin(f, 0)) {
462 case FO_METRIC: origin = "Metric"; break;
463 case FO_STATUS: origin = "Status"; break;
464 case FO_KEY: origin = "Key"; break;
465 case FO_CONFIG: origin = "Config"; break;
466 case FO_PRODUCT: origin = "Product"; break;
467 default: origin = "Unknown"; break;
468 }
469
470 switch (field_nature(f, 0)) {
471 case FN_GAUGE: nature = "Gauge"; break;
472 case FN_LIMIT: nature = "Limit"; break;
473 case FN_MIN: nature = "Min"; break;
474 case FN_MAX: nature = "Max"; break;
475 case FN_RATE: nature = "Rate"; break;
476 case FN_COUNTER: nature = "Counter"; break;
477 case FN_DURATION: nature = "Duration"; break;
478 case FN_AGE: nature = "Age"; break;
479 case FN_TIME: nature = "Time"; break;
480 case FN_NAME: nature = "Name"; break;
481 case FN_OUTPUT: nature = "Output"; break;
482 case FN_AVG: nature = "Avg"; break;
483 default: nature = "Unknown"; break;
484 }
485
486 switch (field_scope(f, 0)) {
487 case FS_PROCESS: scope = "Process"; break;
488 case FS_SERVICE: scope = "Service"; break;
489 case FS_SYSTEM: scope = "System"; break;
490 case FS_CLUSTER: scope = "Cluster"; break;
491 default: scope = "Unknown"; break;
492 }
493
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200494 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100495 chunk_appendf(out, "\"tags\":{"
496 "\"origin\":\"%s\","
497 "\"nature\":\"%s\","
498 "\"scope\":\"%s\""
499 "}", origin, nature, scope);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200500 return !(old_len == out->data);
Simon Horman05ee2132017-01-04 09:37:25 +0100501}
William Lallemand74c24fb2016-11-21 17:18:36 +0100502
503/* Dump all fields from <stats> into <out> using CSV format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200504static int stats_dump_fields_csv(struct buffer *out,
505 const struct field *stats)
William Lallemand74c24fb2016-11-21 17:18:36 +0100506{
507 int field;
508
509 for (field = 0; field < ST_F_TOTAL_FIELDS; field++) {
510 if (!stats_emit_raw_data_field(out, &stats[field]))
511 return 0;
512 if (!chunk_strcat(out, ","))
513 return 0;
514 }
515 chunk_strcat(out, "\n");
516 return 1;
517}
518
519/* Dump all fields from <stats> into <out> using a typed "field:desc:type:value" format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200520static int stats_dump_fields_typed(struct buffer *out,
521 const struct field *stats)
William Lallemand74c24fb2016-11-21 17:18:36 +0100522{
523 int field;
524
525 for (field = 0; field < ST_F_TOTAL_FIELDS; field++) {
526 if (!stats[field].type)
527 continue;
528
529 chunk_appendf(out, "%c.%u.%u.%d.%s.%u:",
530 stats[ST_F_TYPE].u.u32 == STATS_TYPE_FE ? 'F' :
531 stats[ST_F_TYPE].u.u32 == STATS_TYPE_BE ? 'B' :
532 stats[ST_F_TYPE].u.u32 == STATS_TYPE_SO ? 'L' :
533 stats[ST_F_TYPE].u.u32 == STATS_TYPE_SV ? 'S' :
534 '?',
535 stats[ST_F_IID].u.u32, stats[ST_F_SID].u.u32,
536 field, stat_field_names[field], stats[ST_F_PID].u.u32);
537
538 if (!stats_emit_field_tags(out, &stats[field], ':'))
539 return 0;
540 if (!stats_emit_typed_data_field(out, &stats[field]))
541 return 0;
542 if (!chunk_strcat(out, "\n"))
543 return 0;
544 }
545 return 1;
546}
547
Simon Horman05ee2132017-01-04 09:37:25 +0100548/* Dump all fields from <stats> into <out> using the "show info json" format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200549static int stats_dump_json_info_fields(struct buffer *out,
Simon Horman05ee2132017-01-04 09:37:25 +0100550 const struct field *info)
551{
552 int field;
553 int started = 0;
554
555 if (!chunk_strcat(out, "["))
556 return 0;
557
558 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
559 int old_len;
560
561 if (!field_format(info, field))
562 continue;
563
564 if (started && !chunk_strcat(out, ","))
565 goto err;
566 started = 1;
567
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200568 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100569 chunk_appendf(out,
570 "{\"field\":{\"pos\":%d,\"name\":\"%s\"},"
571 "\"processNum\":%u,",
572 field, info_field_names[field],
573 info[INF_PROCESS_NUM].u.u32);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200574 if (old_len == out->data)
Simon Horman05ee2132017-01-04 09:37:25 +0100575 goto err;
576
577 if (!stats_emit_json_field_tags(out, &info[field]))
578 goto err;
579
580 if (!stats_emit_json_data_field(out, &info[field]))
581 goto err;
582
583 if (!chunk_strcat(out, "}"))
584 goto err;
585 }
586
587 if (!chunk_strcat(out, "]"))
588 goto err;
589 return 1;
590
591err:
592 chunk_reset(out);
593 chunk_appendf(out, "{\"errorStr\":\"output buffer too short\"}");
594 return 0;
595}
596
597/* Dump all fields from <stats> into <out> using a typed "field:desc:type:value" format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200598static int stats_dump_fields_json(struct buffer *out,
599 const struct field *stats,
Simon Horman05ee2132017-01-04 09:37:25 +0100600 int first_stat)
601{
602 int field;
603 int started = 0;
604
605 if (!first_stat && !chunk_strcat(out, ","))
606 return 0;
607 if (!chunk_strcat(out, "["))
608 return 0;
609
610 for (field = 0; field < ST_F_TOTAL_FIELDS; field++) {
611 const char *obj_type;
612 int old_len;
613
614 if (!stats[field].type)
615 continue;
616
617 if (started && !chunk_strcat(out, ","))
618 goto err;
619 started = 1;
620
621 switch (stats[ST_F_TYPE].u.u32) {
622 case STATS_TYPE_FE: obj_type = "Frontend"; break;
623 case STATS_TYPE_BE: obj_type = "Backend"; break;
624 case STATS_TYPE_SO: obj_type = "Listener"; break;
625 case STATS_TYPE_SV: obj_type = "Server"; break;
626 default: obj_type = "Unknown"; break;
627 }
628
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200629 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100630 chunk_appendf(out,
631 "{"
632 "\"objType\":\"%s\","
633 "\"proxyId\":%d,"
634 "\"id\":%d,"
635 "\"field\":{\"pos\":%d,\"name\":\"%s\"},"
636 "\"processNum\":%u,",
637 obj_type, stats[ST_F_IID].u.u32,
638 stats[ST_F_SID].u.u32, field,
639 stat_field_names[field], stats[ST_F_PID].u.u32);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200640 if (old_len == out->data)
Simon Horman05ee2132017-01-04 09:37:25 +0100641 goto err;
642
643 if (!stats_emit_json_field_tags(out, &stats[field]))
644 goto err;
645
646 if (!stats_emit_json_data_field(out, &stats[field]))
647 goto err;
648
649 if (!chunk_strcat(out, "}"))
650 goto err;
651 }
652
653 if (!chunk_strcat(out, "]"))
654 goto err;
655
656 return 1;
657
658err:
659 chunk_reset(out);
660 if (!first_stat)
661 chunk_strcat(out, ",");
662 chunk_appendf(out, "{\"errorStr\":\"output buffer too short\"}");
663 return 0;
664}
665
William Lallemand74c24fb2016-11-21 17:18:36 +0100666/* Dump all fields from <stats> into <out> using the HTML format. A column is
667 * reserved for the checkbox is ST_SHOWADMIN is set in <flags>. Some extra info
668 * are provided if ST_SHLGNDS is present in <flags>.
669 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200670static int stats_dump_fields_html(struct buffer *out,
671 const struct field *stats,
672 unsigned int flags)
William Lallemand74c24fb2016-11-21 17:18:36 +0100673{
Willy Tarreau83061a82018-07-13 11:56:34 +0200674 struct buffer src;
William Lallemand74c24fb2016-11-21 17:18:36 +0100675
676 if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_FE) {
677 chunk_appendf(out,
678 /* name, queue */
679 "<tr class=\"frontend\">");
680
681 if (flags & ST_SHOWADMIN) {
682 /* Column sub-heading for Enable or Disable server */
683 chunk_appendf(out, "<td></td>");
684 }
685
686 chunk_appendf(out,
687 "<td class=ac>"
688 "<a name=\"%s/Frontend\"></a>"
689 "<a class=lfsb href=\"#%s/Frontend\">Frontend</a></td>"
690 "<td colspan=3></td>"
691 "",
692 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_PXNAME));
693
694 chunk_appendf(out,
695 /* sessions rate : current */
696 "<td><u>%s<div class=tips><table class=det>"
697 "<tr><th>Current connection rate:</th><td>%s/s</td></tr>"
698 "<tr><th>Current session rate:</th><td>%s/s</td></tr>"
699 "",
700 U2H(stats[ST_F_RATE].u.u32),
701 U2H(stats[ST_F_CONN_RATE].u.u32),
702 U2H(stats[ST_F_RATE].u.u32));
703
704 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
705 chunk_appendf(out,
706 "<tr><th>Current request rate:</th><td>%s/s</td></tr>",
707 U2H(stats[ST_F_REQ_RATE].u.u32));
708
709 chunk_appendf(out,
710 "</table></div></u></td>"
711 /* sessions rate : max */
712 "<td><u>%s<div class=tips><table class=det>"
713 "<tr><th>Max connection rate:</th><td>%s/s</td></tr>"
714 "<tr><th>Max session rate:</th><td>%s/s</td></tr>"
715 "",
716 U2H(stats[ST_F_RATE_MAX].u.u32),
717 U2H(stats[ST_F_CONN_RATE_MAX].u.u32),
718 U2H(stats[ST_F_RATE_MAX].u.u32));
719
720 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
721 chunk_appendf(out,
722 "<tr><th>Max request rate:</th><td>%s/s</td></tr>",
723 U2H(stats[ST_F_REQ_RATE_MAX].u.u32));
724
725 chunk_appendf(out,
726 "</table></div></u></td>"
727 /* sessions rate : limit */
728 "<td>%s</td>",
729 LIM2A(stats[ST_F_RATE_LIM].u.u32, "-"));
730
731 chunk_appendf(out,
732 /* sessions: current, max, limit, total */
733 "<td>%s</td><td>%s</td><td>%s</td>"
734 "<td><u>%s<div class=tips><table class=det>"
735 "<tr><th>Cum. connections:</th><td>%s</td></tr>"
736 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
737 "",
738 U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SMAX].u.u32), U2H(stats[ST_F_SLIM].u.u32),
739 U2H(stats[ST_F_STOT].u.u64),
740 U2H(stats[ST_F_CONN_TOT].u.u64),
741 U2H(stats[ST_F_STOT].u.u64));
742
743 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
744 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
745 chunk_appendf(out,
746 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
747 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
748 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
749 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
750 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
751 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
752 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
753 "<tr><th>- other responses:</th><td>%s</td></tr>"
754 "<tr><th>Intercepted requests:</th><td>%s</td></tr>"
Willy Tarreaua1214a52018-12-14 14:00:25 +0100755 "<tr><th>Cache lookups:</th><td>%s</td></tr>"
756 "<tr><th>Cache hits:</th><td>%s</td><td>(%d%%)</td></tr>"
Willy Tarreau1b0f85e2018-05-28 15:12:40 +0200757 "<tr><th>Failed hdr rewrites:</th><td>%s</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +0100758 "",
759 U2H(stats[ST_F_REQ_TOT].u.u64),
760 U2H(stats[ST_F_HRSP_1XX].u.u64),
761 U2H(stats[ST_F_HRSP_2XX].u.u64),
762 U2H(stats[ST_F_COMP_RSP].u.u64),
763 stats[ST_F_HRSP_2XX].u.u64 ?
764 (int)(100 * stats[ST_F_COMP_RSP].u.u64 / stats[ST_F_HRSP_2XX].u.u64) : 0,
765 U2H(stats[ST_F_HRSP_3XX].u.u64),
766 U2H(stats[ST_F_HRSP_4XX].u.u64),
767 U2H(stats[ST_F_HRSP_5XX].u.u64),
768 U2H(stats[ST_F_HRSP_OTHER].u.u64),
Willy Tarreau1b0f85e2018-05-28 15:12:40 +0200769 U2H(stats[ST_F_INTERCEPTED].u.u64),
Willy Tarreaua1214a52018-12-14 14:00:25 +0100770 U2H(stats[ST_F_CACHE_LOOKUPS].u.u64),
771 U2H(stats[ST_F_CACHE_HITS].u.u64),
772 stats[ST_F_CACHE_LOOKUPS].u.u64 ?
773 (int)(100 * stats[ST_F_CACHE_HITS].u.u64 / stats[ST_F_CACHE_LOOKUPS].u.u64) : 0,
Willy Tarreau1b0f85e2018-05-28 15:12:40 +0200774 U2H(stats[ST_F_WREW].u.u64));
William Lallemand74c24fb2016-11-21 17:18:36 +0100775 }
776
777 chunk_appendf(out,
778 "</table></div></u></td>"
779 /* sessions: lbtot, lastsess */
780 "<td></td><td></td>"
781 /* bytes : in */
782 "<td>%s</td>"
783 "",
784 U2H(stats[ST_F_BIN].u.u64));
785
786 chunk_appendf(out,
787 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
788 "<td>%s%s<div class=tips><table class=det>"
789 "<tr><th>Response bytes in:</th><td>%s</td></tr>"
790 "<tr><th>Compression in:</th><td>%s</td></tr>"
791 "<tr><th>Compression out:</th><td>%s</td><td>(%d%%)</td></tr>"
792 "<tr><th>Compression bypass:</th><td>%s</td></tr>"
793 "<tr><th>Total bytes saved:</th><td>%s</td><td>(%d%%)</td></tr>"
794 "</table></div>%s</td>",
795 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "<u>":"",
796 U2H(stats[ST_F_BOUT].u.u64),
797 U2H(stats[ST_F_BOUT].u.u64),
798 U2H(stats[ST_F_COMP_IN].u.u64),
799 U2H(stats[ST_F_COMP_OUT].u.u64),
800 stats[ST_F_COMP_IN].u.u64 ? (int)(stats[ST_F_COMP_OUT].u.u64 * 100 / stats[ST_F_COMP_IN].u.u64) : 0,
801 U2H(stats[ST_F_COMP_BYP].u.u64),
802 U2H(stats[ST_F_COMP_IN].u.u64 - stats[ST_F_COMP_OUT].u.u64),
803 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,
804 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "</u>":"");
805
806 chunk_appendf(out,
807 /* denied: req, resp */
808 "<td>%s</td><td>%s</td>"
809 /* errors : request, connect, response */
810 "<td>%s</td><td></td><td></td>"
811 /* warnings: retries, redispatches */
812 "<td></td><td></td>"
813 /* server status : reflect frontend status */
814 "<td class=ac>%s</td>"
815 /* rest of server: nothing */
816 "<td class=ac colspan=8></td></tr>"
817 "",
818 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
819 U2H(stats[ST_F_EREQ].u.u64),
820 field_str(stats, ST_F_STATUS));
821 }
822 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_SO) {
823 chunk_appendf(out, "<tr class=socket>");
824 if (flags & ST_SHOWADMIN) {
825 /* Column sub-heading for Enable or Disable server */
826 chunk_appendf(out, "<td></td>");
827 }
828
829 chunk_appendf(out,
830 /* frontend name, listener name */
831 "<td class=ac><a name=\"%s/+%s\"></a>%s"
832 "<a class=lfsb href=\"#%s/+%s\">%s</a>"
833 "",
834 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME),
835 (flags & ST_SHLGNDS)?"<u>":"",
836 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME), field_str(stats, ST_F_SVNAME));
837
838 if (flags & ST_SHLGNDS) {
839 chunk_appendf(out, "<div class=tips>");
840
841 if (isdigit(*field_str(stats, ST_F_ADDR)))
842 chunk_appendf(out, "IPv4: %s, ", field_str(stats, ST_F_ADDR));
843 else if (*field_str(stats, ST_F_ADDR) == '[')
844 chunk_appendf(out, "IPv6: %s, ", field_str(stats, ST_F_ADDR));
845 else if (*field_str(stats, ST_F_ADDR))
846 chunk_appendf(out, "%s, ", field_str(stats, ST_F_ADDR));
847
848 /* id */
849 chunk_appendf(out, "id: %d</div>", stats[ST_F_SID].u.u32);
850 }
851
852 chunk_appendf(out,
853 /* queue */
854 "%s</td><td colspan=3></td>"
855 /* sessions rate: current, max, limit */
856 "<td colspan=3>&nbsp;</td>"
857 /* sessions: current, max, limit, total, lbtot, lastsess */
858 "<td>%s</td><td>%s</td><td>%s</td>"
859 "<td>%s</td><td>&nbsp;</td><td>&nbsp;</td>"
860 /* bytes: in, out */
861 "<td>%s</td><td>%s</td>"
862 "",
863 (flags & ST_SHLGNDS)?"</u>":"",
864 U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SMAX].u.u32), U2H(stats[ST_F_SLIM].u.u32),
865 U2H(stats[ST_F_STOT].u.u64), U2H(stats[ST_F_BIN].u.u64), U2H(stats[ST_F_BOUT].u.u64));
866
867 chunk_appendf(out,
868 /* denied: req, resp */
869 "<td>%s</td><td>%s</td>"
870 /* errors: request, connect, response */
871 "<td>%s</td><td></td><td></td>"
872 /* warnings: retries, redispatches */
873 "<td></td><td></td>"
874 /* server status: reflect listener status */
875 "<td class=ac>%s</td>"
876 /* rest of server: nothing */
877 "<td class=ac colspan=8></td></tr>"
878 "",
879 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
880 U2H(stats[ST_F_EREQ].u.u64),
881 field_str(stats, ST_F_STATUS));
882 }
883 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_SV) {
884 const char *style;
885
886 /* determine the style to use depending on the server's state,
887 * its health and weight. There isn't a 1-to-1 mapping between
888 * state and styles for the cases where the server is (still)
889 * up. The reason is that we don't want to report nolb and
890 * drain with the same color.
891 */
892
893 if (strcmp(field_str(stats, ST_F_STATUS), "DOWN") == 0 ||
894 strcmp(field_str(stats, ST_F_STATUS), "DOWN (agent)") == 0) {
895 style = "down";
896 }
897 else if (strcmp(field_str(stats, ST_F_STATUS), "DOWN ") == 0) {
898 style = "going_up";
899 }
900 else if (strcmp(field_str(stats, ST_F_STATUS), "NOLB ") == 0) {
901 style = "going_down";
902 }
903 else if (strcmp(field_str(stats, ST_F_STATUS), "NOLB") == 0) {
904 style = "nolb";
905 }
906 else if (strcmp(field_str(stats, ST_F_STATUS), "no check") == 0) {
907 style = "no_check";
908 }
909 else if (!stats[ST_F_CHKFAIL].type ||
910 stats[ST_F_CHECK_HEALTH].u.u32 == stats[ST_F_CHECK_RISE].u.u32 + stats[ST_F_CHECK_FALL].u.u32 - 1) {
911 /* no check or max health = UP */
912 if (stats[ST_F_WEIGHT].u.u32)
913 style = "up";
914 else
915 style = "draining";
916 }
917 else {
918 style = "going_down";
919 }
920
921 if (memcmp(field_str(stats, ST_F_STATUS), "MAINT", 5) == 0)
922 chunk_appendf(out, "<tr class=\"maintain\">");
923 else
924 chunk_appendf(out,
925 "<tr class=\"%s_%s\">",
926 (stats[ST_F_BCK].u.u32) ? "backup" : "active", style);
927
928
929 if (flags & ST_SHOWADMIN)
930 chunk_appendf(out,
David Harrigand3db35a2016-12-30 12:12:49 +0000931 "<td><input class='%s-checkbox' type=\"checkbox\" name=\"s\" value=\"%s\"></td>",
932 field_str(stats, ST_F_PXNAME),
William Lallemand74c24fb2016-11-21 17:18:36 +0100933 field_str(stats, ST_F_SVNAME));
934
935 chunk_appendf(out,
936 "<td class=ac><a name=\"%s/%s\"></a>%s"
937 "<a class=lfsb href=\"#%s/%s\">%s</a>"
938 "",
939 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME),
940 (flags & ST_SHLGNDS) ? "<u>" : "",
941 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME), field_str(stats, ST_F_SVNAME));
942
943 if (flags & ST_SHLGNDS) {
944 chunk_appendf(out, "<div class=tips>");
945
946 if (isdigit(*field_str(stats, ST_F_ADDR)))
947 chunk_appendf(out, "IPv4: %s, ", field_str(stats, ST_F_ADDR));
948 else if (*field_str(stats, ST_F_ADDR) == '[')
949 chunk_appendf(out, "IPv6: %s, ", field_str(stats, ST_F_ADDR));
950 else if (*field_str(stats, ST_F_ADDR))
951 chunk_appendf(out, "%s, ", field_str(stats, ST_F_ADDR));
952
953 /* id */
954 chunk_appendf(out, "id: %d", stats[ST_F_SID].u.u32);
955
956 /* cookie */
957 if (stats[ST_F_COOKIE].type) {
958 chunk_appendf(out, ", cookie: '");
959 chunk_initstr(&src, field_str(stats, ST_F_COOKIE));
960 chunk_htmlencode(out, &src);
961 chunk_appendf(out, "'");
962 }
963
964 chunk_appendf(out, "</div>");
965 }
966
967 chunk_appendf(out,
968 /* queue : current, max, limit */
969 "%s</td><td>%s</td><td>%s</td><td>%s</td>"
970 /* sessions rate : current, max, limit */
971 "<td>%s</td><td>%s</td><td></td>"
972 "",
973 (flags & ST_SHLGNDS) ? "</u>" : "",
974 U2H(stats[ST_F_QCUR].u.u32), U2H(stats[ST_F_QMAX].u.u32), LIM2A(stats[ST_F_QLIMIT].u.u32, "-"),
975 U2H(stats[ST_F_RATE].u.u32), U2H(stats[ST_F_RATE_MAX].u.u32));
976
977 chunk_appendf(out,
978 /* sessions: current, max, limit, total */
979 "<td>%s</td><td>%s</td><td>%s</td>"
980 "<td><u>%s<div class=tips><table class=det>"
981 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
982 "",
983 U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SMAX].u.u32), LIM2A(stats[ST_F_SLIM].u.u32, "-"),
984 U2H(stats[ST_F_STOT].u.u64),
985 U2H(stats[ST_F_STOT].u.u64));
986
987 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
988 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
989 unsigned long long tot;
990
991 tot = stats[ST_F_HRSP_OTHER].u.u64;
992 tot += stats[ST_F_HRSP_1XX].u.u64;
993 tot += stats[ST_F_HRSP_2XX].u.u64;
994 tot += stats[ST_F_HRSP_3XX].u.u64;
995 tot += stats[ST_F_HRSP_4XX].u.u64;
996 tot += stats[ST_F_HRSP_5XX].u.u64;
997
998 chunk_appendf(out,
Willy Tarreauf1573842018-12-14 11:35:36 +0100999 "<tr><th>New connections:</th><td>%s</td></tr>"
1000 "<tr><th>Reused connections:</th><td>%s</td><td>(%d%%)</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001001 "<tr><th>Cum. HTTP responses:</th><td>%s</td></tr>"
1002 "<tr><th>- HTTP 1xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1003 "<tr><th>- HTTP 2xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1004 "<tr><th>- HTTP 3xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1005 "<tr><th>- HTTP 4xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1006 "<tr><th>- HTTP 5xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
1007 "<tr><th>- other responses:</th><td>%s</td><td>(%d%%)</td></tr>"
Willy Tarreau1b0f85e2018-05-28 15:12:40 +02001008 "<tr><th>Failed hdr rewrites:</th><td>%s</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001009 "",
Willy Tarreauf1573842018-12-14 11:35:36 +01001010 U2H(stats[ST_F_CONNECT].u.u64),
1011 U2H(stats[ST_F_REUSE].u.u64),
1012 (stats[ST_F_CONNECT].u.u64 + stats[ST_F_REUSE].u.u64) ?
1013 (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 +01001014 U2H(tot),
1015 U2H(stats[ST_F_HRSP_1XX].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_1XX].u.u64 / tot) : 0,
1016 U2H(stats[ST_F_HRSP_2XX].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_2XX].u.u64 / tot) : 0,
1017 U2H(stats[ST_F_HRSP_3XX].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_3XX].u.u64 / tot) : 0,
1018 U2H(stats[ST_F_HRSP_4XX].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_4XX].u.u64 / tot) : 0,
1019 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 +02001020 U2H(stats[ST_F_HRSP_OTHER].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_OTHER].u.u64 / tot) : 0,
1021 U2H(stats[ST_F_WREW].u.u64));
William Lallemand74c24fb2016-11-21 17:18:36 +01001022 }
1023
1024 chunk_appendf(out, "<tr><th colspan=3>Avg over last 1024 success. conn.</th></tr>");
1025 chunk_appendf(out, "<tr><th>- Queue time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_QTIME].u.u32));
1026 chunk_appendf(out, "<tr><th>- Connect time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_CTIME].u.u32));
1027 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
1028 chunk_appendf(out, "<tr><th>- Response time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_RTIME].u.u32));
1029 chunk_appendf(out, "<tr><th>- Total time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_TTIME].u.u32));
1030
1031 chunk_appendf(out,
1032 "</table></div></u></td>"
1033 /* sessions: lbtot, last */
1034 "<td>%s</td><td>%s</td>",
1035 U2H(stats[ST_F_LBTOT].u.u64),
1036 human_time(stats[ST_F_LASTSESS].u.s32, 1));
1037
1038 chunk_appendf(out,
1039 /* bytes : in, out */
1040 "<td>%s</td><td>%s</td>"
1041 /* denied: req, resp */
1042 "<td></td><td>%s</td>"
1043 /* errors : request, connect */
1044 "<td></td><td>%s</td>"
1045 /* errors : response */
1046 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
1047 /* warnings: retries, redispatches */
1048 "<td>%lld</td><td>%lld</td>"
1049 "",
1050 U2H(stats[ST_F_BIN].u.u64), U2H(stats[ST_F_BOUT].u.u64),
1051 U2H(stats[ST_F_DRESP].u.u64),
1052 U2H(stats[ST_F_ECON].u.u64),
1053 U2H(stats[ST_F_ERESP].u.u64),
1054 (long long)stats[ST_F_CLI_ABRT].u.u64,
1055 (long long)stats[ST_F_SRV_ABRT].u.u64,
1056 (long long)stats[ST_F_WRETR].u.u64,
1057 (long long)stats[ST_F_WREDIS].u.u64);
1058
1059 /* status, last change */
1060 chunk_appendf(out, "<td class=ac>");
1061
1062 /* FIXME!!!!
1063 * LASTCHG should contain the last change for *this* server and must be computed
1064 * properly above, as was done below, ie: this server if maint, otherwise ref server
1065 * if tracking. Note that ref is either local or remote depending on tracking.
1066 */
1067
1068
1069 if (memcmp(field_str(stats, ST_F_STATUS), "MAINT", 5) == 0) {
1070 chunk_appendf(out, "%s MAINT", human_time(stats[ST_F_LASTCHG].u.u32, 1));
1071 }
1072 else if (memcmp(field_str(stats, ST_F_STATUS), "no check", 5) == 0) {
1073 chunk_strcat(out, "<i>no check</i>");
1074 }
1075 else {
1076 chunk_appendf(out, "%s %s", human_time(stats[ST_F_LASTCHG].u.u32, 1), field_str(stats, ST_F_STATUS));
1077 if (memcmp(field_str(stats, ST_F_STATUS), "DOWN", 4) == 0) {
1078 if (stats[ST_F_CHECK_HEALTH].u.u32)
1079 chunk_strcat(out, " &uarr;");
1080 }
1081 else if (stats[ST_F_CHECK_HEALTH].u.u32 < stats[ST_F_CHECK_RISE].u.u32 + stats[ST_F_CHECK_FALL].u.u32 - 1)
1082 chunk_strcat(out, " &darr;");
1083 }
1084
1085 if (memcmp(field_str(stats, ST_F_STATUS), "DOWN", 4) == 0 &&
1086 stats[ST_F_AGENT_STATUS].type && !stats[ST_F_AGENT_HEALTH].u.u32) {
1087 chunk_appendf(out,
1088 "</td><td class=ac><u> %s",
1089 field_str(stats, ST_F_AGENT_STATUS));
1090
1091 if (stats[ST_F_AGENT_CODE].type)
1092 chunk_appendf(out, "/%d", stats[ST_F_AGENT_CODE].u.u32);
1093
1094 if (stats[ST_F_AGENT_DURATION].type)
1095 chunk_appendf(out, " in %lums", (long)stats[ST_F_AGENT_DURATION].u.u64);
1096
1097 chunk_appendf(out, "<div class=tips>%s", field_str(stats, ST_F_AGENT_DESC));
1098
1099 if (*field_str(stats, ST_F_LAST_AGT)) {
1100 chunk_appendf(out, ": ");
1101 chunk_initstr(&src, field_str(stats, ST_F_LAST_AGT));
1102 chunk_htmlencode(out, &src);
1103 }
1104 chunk_appendf(out, "</div></u>");
1105 }
1106 else if (stats[ST_F_CHECK_STATUS].type) {
1107 chunk_appendf(out,
1108 "</td><td class=ac><u> %s",
1109 field_str(stats, ST_F_CHECK_STATUS));
1110
1111 if (stats[ST_F_CHECK_CODE].type)
1112 chunk_appendf(out, "/%d", stats[ST_F_CHECK_CODE].u.u32);
1113
1114 if (stats[ST_F_CHECK_DURATION].type)
1115 chunk_appendf(out, " in %lums", (long)stats[ST_F_CHECK_DURATION].u.u64);
1116
1117 chunk_appendf(out, "<div class=tips>%s", field_str(stats, ST_F_CHECK_DESC));
1118
1119 if (*field_str(stats, ST_F_LAST_CHK)) {
1120 chunk_appendf(out, ": ");
1121 chunk_initstr(&src, field_str(stats, ST_F_LAST_CHK));
1122 chunk_htmlencode(out, &src);
1123 }
1124 chunk_appendf(out, "</div></u>");
1125 }
1126 else
1127 chunk_appendf(out, "</td><td>");
1128
1129 chunk_appendf(out,
1130 /* weight */
1131 "</td><td class=ac>%d</td>"
1132 /* act, bck */
1133 "<td class=ac>%s</td><td class=ac>%s</td>"
1134 "",
1135 stats[ST_F_WEIGHT].u.u32,
1136 stats[ST_F_BCK].u.u32 ? "-" : "Y",
1137 stats[ST_F_BCK].u.u32 ? "Y" : "-");
1138
1139 /* check failures: unique, fatal, down time */
1140 if (strcmp(field_str(stats, ST_F_STATUS), "MAINT (resolution)") == 0) {
1141 chunk_appendf(out, "<td class=ac colspan=3>resolution</td>");
1142 }
1143 else if (stats[ST_F_CHKFAIL].type) {
1144 chunk_appendf(out, "<td><u>%lld", (long long)stats[ST_F_CHKFAIL].u.u64);
1145
1146 if (stats[ST_F_HANAFAIL].type)
1147 chunk_appendf(out, "/%lld", (long long)stats[ST_F_HANAFAIL].u.u64);
1148
1149 chunk_appendf(out,
1150 "<div class=tips>Failed Health Checks%s</div></u></td>"
1151 "<td>%lld</td><td>%s</td>"
1152 "",
1153 stats[ST_F_HANAFAIL].type ? "/Health Analyses" : "",
1154 (long long)stats[ST_F_CHKDOWN].u.u64, human_time(stats[ST_F_DOWNTIME].u.u32, 1));
1155 }
1156 else if (strcmp(field_str(stats, ST_F_STATUS), "MAINT") != 0 && field_format(stats, ST_F_TRACKED) == FF_STR) {
1157 /* tracking a server (hence inherited maint would appear as "MAINT (via...)" */
1158 chunk_appendf(out,
1159 "<td class=ac colspan=3><a class=lfsb href=\"#%s\">via %s</a></td>",
1160 field_str(stats, ST_F_TRACKED), field_str(stats, ST_F_TRACKED));
1161 }
1162 else
1163 chunk_appendf(out, "<td colspan=3></td>");
1164
1165 /* throttle */
1166 if (stats[ST_F_THROTTLE].type)
1167 chunk_appendf(out, "<td class=ac>%d %%</td></tr>\n", stats[ST_F_THROTTLE].u.u32);
1168 else
1169 chunk_appendf(out, "<td class=ac>-</td></tr>\n");
1170 }
1171 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_BE) {
1172 chunk_appendf(out, "<tr class=\"backend\">");
1173 if (flags & ST_SHOWADMIN) {
1174 /* Column sub-heading for Enable or Disable server */
1175 chunk_appendf(out, "<td></td>");
1176 }
1177 chunk_appendf(out,
1178 "<td class=ac>"
1179 /* name */
1180 "%s<a name=\"%s/Backend\"></a>"
1181 "<a class=lfsb href=\"#%s/Backend\">Backend</a>"
1182 "",
1183 (flags & ST_SHLGNDS)?"<u>":"",
1184 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_PXNAME));
1185
1186 if (flags & ST_SHLGNDS) {
1187 /* balancing */
1188 chunk_appendf(out, "<div class=tips>balancing: %s",
1189 field_str(stats, ST_F_ALGO));
1190
1191 /* cookie */
1192 if (stats[ST_F_COOKIE].type) {
1193 chunk_appendf(out, ", cookie: '");
1194 chunk_initstr(&src, field_str(stats, ST_F_COOKIE));
1195 chunk_htmlencode(out, &src);
1196 chunk_appendf(out, "'");
1197 }
1198 chunk_appendf(out, "</div>");
1199 }
1200
1201 chunk_appendf(out,
1202 "%s</td>"
1203 /* queue : current, max */
1204 "<td>%s</td><td>%s</td><td></td>"
1205 /* sessions rate : current, max, limit */
1206 "<td>%s</td><td>%s</td><td></td>"
1207 "",
1208 (flags & ST_SHLGNDS)?"</u>":"",
1209 U2H(stats[ST_F_QCUR].u.u32), U2H(stats[ST_F_QMAX].u.u32),
1210 U2H(stats[ST_F_RATE].u.u32), U2H(stats[ST_F_RATE_MAX].u.u32));
1211
1212 chunk_appendf(out,
1213 /* sessions: current, max, limit, total */
1214 "<td>%s</td><td>%s</td><td>%s</td>"
1215 "<td><u>%s<div class=tips><table class=det>"
1216 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
1217 "",
Willy Tarreau8e0f1752016-12-12 15:07:29 +01001218 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 +01001219 U2H(stats[ST_F_STOT].u.u64),
1220 U2H(stats[ST_F_STOT].u.u64));
1221
1222 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
1223 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
1224 chunk_appendf(out,
Willy Tarreauf1573842018-12-14 11:35:36 +01001225 "<tr><th>New connections:</th><td>%s</td></tr>"
1226 "<tr><th>Reused connections:</th><td>%s</td><td>(%d%%)</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001227 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
1228 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
1229 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
1230 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
1231 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
1232 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
1233 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
1234 "<tr><th>- other responses:</th><td>%s</td></tr>"
Willy Tarreaua1214a52018-12-14 14:00:25 +01001235 "<tr><th>Cache lookups:</th><td>%s</td></tr>"
1236 "<tr><th>Cache hits:</th><td>%s</td><td>(%d%%)</td></tr>"
Willy Tarreau1b0f85e2018-05-28 15:12:40 +02001237 "<tr><th>Failed hdr rewrites:</th><td>%s</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001238 "<tr><th colspan=3>Avg over last 1024 success. conn.</th></tr>"
1239 "",
Willy Tarreauf1573842018-12-14 11:35:36 +01001240 U2H(stats[ST_F_CONNECT].u.u64),
1241 U2H(stats[ST_F_REUSE].u.u64),
1242 (stats[ST_F_CONNECT].u.u64 + stats[ST_F_REUSE].u.u64) ?
1243 (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 +01001244 U2H(stats[ST_F_REQ_TOT].u.u64),
1245 U2H(stats[ST_F_HRSP_1XX].u.u64),
1246 U2H(stats[ST_F_HRSP_2XX].u.u64),
1247 U2H(stats[ST_F_COMP_RSP].u.u64),
1248 stats[ST_F_HRSP_2XX].u.u64 ?
1249 (int)(100 * stats[ST_F_COMP_RSP].u.u64 / stats[ST_F_HRSP_2XX].u.u64) : 0,
1250 U2H(stats[ST_F_HRSP_3XX].u.u64),
1251 U2H(stats[ST_F_HRSP_4XX].u.u64),
1252 U2H(stats[ST_F_HRSP_5XX].u.u64),
Willy Tarreaufeead3a2018-12-14 13:48:44 +01001253 U2H(stats[ST_F_HRSP_OTHER].u.u64),
Willy Tarreaua1214a52018-12-14 14:00:25 +01001254 U2H(stats[ST_F_CACHE_LOOKUPS].u.u64),
1255 U2H(stats[ST_F_CACHE_HITS].u.u64),
1256 stats[ST_F_CACHE_LOOKUPS].u.u64 ?
1257 (int)(100 * stats[ST_F_CACHE_HITS].u.u64 / stats[ST_F_CACHE_LOOKUPS].u.u64) : 0,
Willy Tarreaufeead3a2018-12-14 13:48:44 +01001258 U2H(stats[ST_F_WREW].u.u64));
William Lallemand74c24fb2016-11-21 17:18:36 +01001259 }
1260
1261 chunk_appendf(out, "<tr><th>- Queue time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_QTIME].u.u32));
1262 chunk_appendf(out, "<tr><th>- Connect time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_CTIME].u.u32));
1263 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
1264 chunk_appendf(out, "<tr><th>- Response time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_RTIME].u.u32));
1265 chunk_appendf(out, "<tr><th>- Total time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_TTIME].u.u32));
1266
1267 chunk_appendf(out,
1268 "</table></div></u></td>"
1269 /* sessions: lbtot, last */
1270 "<td>%s</td><td>%s</td>"
1271 /* bytes: in */
1272 "<td>%s</td>"
1273 "",
1274 U2H(stats[ST_F_LBTOT].u.u64),
1275 human_time(stats[ST_F_LASTSESS].u.s32, 1),
1276 U2H(stats[ST_F_BIN].u.u64));
1277
1278 chunk_appendf(out,
1279 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
1280 "<td>%s%s<div class=tips><table class=det>"
1281 "<tr><th>Response bytes in:</th><td>%s</td></tr>"
1282 "<tr><th>Compression in:</th><td>%s</td></tr>"
1283 "<tr><th>Compression out:</th><td>%s</td><td>(%d%%)</td></tr>"
1284 "<tr><th>Compression bypass:</th><td>%s</td></tr>"
1285 "<tr><th>Total bytes saved:</th><td>%s</td><td>(%d%%)</td></tr>"
1286 "</table></div>%s</td>",
1287 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "<u>":"",
1288 U2H(stats[ST_F_BOUT].u.u64),
1289 U2H(stats[ST_F_BOUT].u.u64),
1290 U2H(stats[ST_F_COMP_IN].u.u64),
1291 U2H(stats[ST_F_COMP_OUT].u.u64),
1292 stats[ST_F_COMP_IN].u.u64 ? (int)(stats[ST_F_COMP_OUT].u.u64 * 100 / stats[ST_F_COMP_IN].u.u64) : 0,
1293 U2H(stats[ST_F_COMP_BYP].u.u64),
1294 U2H(stats[ST_F_COMP_IN].u.u64 - stats[ST_F_COMP_OUT].u.u64),
1295 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,
1296 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "</u>":"");
1297
1298 chunk_appendf(out,
1299 /* denied: req, resp */
1300 "<td>%s</td><td>%s</td>"
1301 /* errors : request, connect */
1302 "<td></td><td>%s</td>"
1303 /* errors : response */
1304 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
1305 /* warnings: retries, redispatches */
1306 "<td>%lld</td><td>%lld</td>"
1307 /* backend status: reflect backend status (up/down): we display UP
1308 * if the backend has known working servers or if it has no server at
1309 * all (eg: for stats). Then we display the total weight, number of
1310 * active and backups. */
1311 "<td class=ac>%s %s</td><td class=ac>&nbsp;</td><td class=ac>%d</td>"
1312 "<td class=ac>%d</td><td class=ac>%d</td>"
1313 "",
1314 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
1315 U2H(stats[ST_F_ECON].u.u64),
1316 U2H(stats[ST_F_ERESP].u.u64),
1317 (long long)stats[ST_F_CLI_ABRT].u.u64,
1318 (long long)stats[ST_F_SRV_ABRT].u.u64,
1319 (long long)stats[ST_F_WRETR].u.u64, (long long)stats[ST_F_WREDIS].u.u64,
1320 human_time(stats[ST_F_LASTCHG].u.u32, 1),
1321 strcmp(field_str(stats, ST_F_STATUS), "DOWN") ? field_str(stats, ST_F_STATUS) : "<font color=\"red\"><b>DOWN</b></font>",
1322 stats[ST_F_WEIGHT].u.u32,
1323 stats[ST_F_ACT].u.u32, stats[ST_F_BCK].u.u32);
1324
1325 chunk_appendf(out,
1326 /* rest of backend: nothing, down transitions, total downtime, throttle */
1327 "<td class=ac>&nbsp;</td><td>%d</td>"
1328 "<td>%s</td>"
1329 "<td></td>"
1330 "</tr>",
1331 stats[ST_F_CHKDOWN].u.u32,
1332 stats[ST_F_DOWNTIME].type ? human_time(stats[ST_F_DOWNTIME].u.u32, 1) : "&nbsp;");
1333 }
1334 return 1;
1335}
1336
1337int stats_dump_one_line(const struct field *stats, unsigned int flags, struct proxy *px, struct appctx *appctx)
1338{
Simon Horman05ee2132017-01-04 09:37:25 +01001339 int ret;
1340
William Lallemand74c24fb2016-11-21 17:18:36 +01001341 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN))
1342 flags |= ST_SHOWADMIN;
1343
1344 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
Simon Horman05ee2132017-01-04 09:37:25 +01001345 ret = stats_dump_fields_html(&trash, stats, flags);
William Lallemand74c24fb2016-11-21 17:18:36 +01001346 else if (appctx->ctx.stats.flags & STAT_FMT_TYPED)
Simon Horman05ee2132017-01-04 09:37:25 +01001347 ret = stats_dump_fields_typed(&trash, stats);
1348 else if (appctx->ctx.stats.flags & STAT_FMT_JSON)
1349 ret = stats_dump_fields_json(&trash, stats,
1350 !(appctx->ctx.stats.flags &
1351 STAT_STARTED));
William Lallemand74c24fb2016-11-21 17:18:36 +01001352 else
Simon Horman05ee2132017-01-04 09:37:25 +01001353 ret = stats_dump_fields_csv(&trash, stats);
1354
1355 if (ret)
1356 appctx->ctx.stats.flags |= STAT_STARTED;
1357
1358 return ret;
William Lallemand74c24fb2016-11-21 17:18:36 +01001359}
1360
1361/* Fill <stats> with the frontend statistics. <stats> is
1362 * preallocated array of length <len>. The length of the array
1363 * must be at least ST_F_TOTAL_FIELDS. If this length is less then
1364 * this value, the function returns 0, otherwise, it returns 1.
1365 */
1366int stats_fill_fe_stats(struct proxy *px, struct field *stats, int len)
1367{
1368 if (len < ST_F_TOTAL_FIELDS)
1369 return 0;
1370
1371 memset(stats, 0, sizeof(*stats) * len);
1372
1373 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1374 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, "FRONTEND");
1375 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1376 stats[ST_F_SCUR] = mkf_u32(0, px->feconn);
1377 stats[ST_F_SMAX] = mkf_u32(FN_MAX, px->fe_counters.conn_max);
1378 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, px->maxconn);
1379 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, px->fe_counters.cum_sess);
1380 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, px->fe_counters.bytes_in);
1381 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, px->fe_counters.bytes_out);
1382 stats[ST_F_DREQ] = mkf_u64(FN_COUNTER, px->fe_counters.denied_req);
1383 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, px->fe_counters.denied_resp);
1384 stats[ST_F_EREQ] = mkf_u64(FN_COUNTER, px->fe_counters.failed_req);
1385 stats[ST_F_DCON] = mkf_u64(FN_COUNTER, px->fe_counters.denied_conn);
1386 stats[ST_F_DSES] = mkf_u64(FN_COUNTER, px->fe_counters.denied_sess);
1387 stats[ST_F_STATUS] = mkf_str(FO_STATUS, px->state == PR_STREADY ? "OPEN" : px->state == PR_STFULL ? "FULL" : "STOP");
1388 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
1389 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1390 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, 0);
1391 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_FE);
1392 stats[ST_F_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_sess_per_sec));
1393 stats[ST_F_RATE_LIM] = mkf_u32(FO_CONFIG|FN_LIMIT, px->fe_sps_lim);
1394 stats[ST_F_RATE_MAX] = mkf_u32(FN_MAX, px->fe_counters.sps_max);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001395 stats[ST_F_WREW] = mkf_u64(FN_COUNTER, px->fe_counters.failed_rewrites);
William Lallemand74c24fb2016-11-21 17:18:36 +01001396
1397 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
1398 if (px->mode == PR_MODE_HTTP) {
1399 stats[ST_F_HRSP_1XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[1]);
1400 stats[ST_F_HRSP_2XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[2]);
1401 stats[ST_F_HRSP_3XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[3]);
1402 stats[ST_F_HRSP_4XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[4]);
1403 stats[ST_F_HRSP_5XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[5]);
1404 stats[ST_F_HRSP_OTHER] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[0]);
1405 stats[ST_F_INTERCEPTED] = mkf_u64(FN_COUNTER, px->fe_counters.intercepted_req);
Willy Tarreaua1214a52018-12-14 14:00:25 +01001406 stats[ST_F_CACHE_LOOKUPS] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cache_lookups);
1407 stats[ST_F_CACHE_HITS] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cache_hits);
William Lallemand74c24fb2016-11-21 17:18:36 +01001408 }
1409
1410 /* requests : req_rate, req_rate_max, req_tot, */
1411 stats[ST_F_REQ_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_req_per_sec));
1412 stats[ST_F_REQ_RATE_MAX] = mkf_u32(FN_MAX, px->fe_counters.p.http.rps_max);
1413 stats[ST_F_REQ_TOT] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cum_req);
1414
1415 /* compression: in, out, bypassed, responses */
1416 stats[ST_F_COMP_IN] = mkf_u64(FN_COUNTER, px->fe_counters.comp_in);
1417 stats[ST_F_COMP_OUT] = mkf_u64(FN_COUNTER, px->fe_counters.comp_out);
1418 stats[ST_F_COMP_BYP] = mkf_u64(FN_COUNTER, px->fe_counters.comp_byp);
1419 stats[ST_F_COMP_RSP] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.comp_rsp);
1420
1421 /* connections : conn_rate, conn_rate_max, conn_tot, conn_max */
1422 stats[ST_F_CONN_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_conn_per_sec));
1423 stats[ST_F_CONN_RATE_MAX] = mkf_u32(FN_MAX, px->fe_counters.cps_max);
1424 stats[ST_F_CONN_TOT] = mkf_u64(FN_COUNTER, px->fe_counters.cum_conn);
1425
1426 return 1;
1427}
1428
1429/* Dumps a frontend's line to the trash for the current proxy <px> and uses
1430 * the state from stream interface <si>. The caller is responsible for clearing
1431 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
1432 */
1433static int stats_dump_fe_stats(struct stream_interface *si, struct proxy *px)
1434{
1435 struct appctx *appctx = __objt_appctx(si->end);
1436
1437 if (!(px->cap & PR_CAP_FE))
1438 return 0;
1439
1440 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_FE)))
1441 return 0;
1442
1443 if (!stats_fill_fe_stats(px, stats, ST_F_TOTAL_FIELDS))
1444 return 0;
1445
1446 return stats_dump_one_line(stats, 0, px, appctx);
1447}
1448
1449/* Fill <stats> with the listener statistics. <stats> is
1450 * preallocated array of length <len>. The length of the array
1451 * must be at least ST_F_TOTAL_FIELDS. If this length is less
1452 * then this value, the function returns 0, otherwise, it
1453 * returns 1. <flags> can take the value ST_SHLGNDS.
1454 */
1455int stats_fill_li_stats(struct proxy *px, struct listener *l, int flags,
1456 struct field *stats, int len)
1457{
Willy Tarreau83061a82018-07-13 11:56:34 +02001458 struct buffer *out = get_trash_chunk();
William Lallemand74c24fb2016-11-21 17:18:36 +01001459
1460 if (len < ST_F_TOTAL_FIELDS)
1461 return 0;
1462
1463 if (!l->counters)
1464 return 0;
1465
1466 chunk_reset(out);
1467 memset(stats, 0, sizeof(*stats) * len);
1468
1469 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1470 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, l->name);
1471 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1472 stats[ST_F_SCUR] = mkf_u32(0, l->nbconn);
1473 stats[ST_F_SMAX] = mkf_u32(FN_MAX, l->counters->conn_max);
1474 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, l->maxconn);
1475 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, l->counters->cum_conn);
1476 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, l->counters->bytes_in);
1477 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, l->counters->bytes_out);
1478 stats[ST_F_DREQ] = mkf_u64(FN_COUNTER, l->counters->denied_req);
1479 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, l->counters->denied_resp);
1480 stats[ST_F_EREQ] = mkf_u64(FN_COUNTER, l->counters->failed_req);
1481 stats[ST_F_DCON] = mkf_u64(FN_COUNTER, l->counters->denied_conn);
1482 stats[ST_F_DSES] = mkf_u64(FN_COUNTER, l->counters->denied_sess);
Willy Tarreaua8cf66b2019-02-27 16:49:00 +01001483 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 +01001484 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
1485 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1486 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, l->luid);
1487 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_SO);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001488 stats[ST_F_WREW] = mkf_u64(FN_COUNTER, l->counters->failed_rewrites);
William Lallemand74c24fb2016-11-21 17:18:36 +01001489
1490 if (flags & ST_SHLGNDS) {
1491 char str[INET6_ADDRSTRLEN];
1492 int port;
1493
1494 port = get_host_port(&l->addr);
1495 switch (addr_to_str(&l->addr, str, sizeof(str))) {
1496 case AF_INET:
1497 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1498 chunk_appendf(out, "%s:%d", str, port);
1499 break;
1500 case AF_INET6:
1501 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1502 chunk_appendf(out, "[%s]:%d", str, port);
1503 break;
1504 case AF_UNIX:
1505 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, "unix");
1506 break;
1507 case -1:
1508 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1509 chunk_strcat(out, strerror(errno));
1510 break;
1511 default: /* address family not supported */
1512 break;
1513 }
1514 }
1515
1516 return 1;
1517}
1518
1519/* Dumps a line for listener <l> and proxy <px> to the trash and uses the state
1520 * from stream interface <si>, and stats flags <flags>. The caller is responsible
1521 * for clearing the trash if needed. Returns non-zero if it emits anything, zero
1522 * otherwise.
1523 */
1524static int stats_dump_li_stats(struct stream_interface *si, struct proxy *px, struct listener *l, int flags)
1525{
1526 struct appctx *appctx = __objt_appctx(si->end);
1527
1528 if (!stats_fill_li_stats(px, l, flags, stats, ST_F_TOTAL_FIELDS))
1529 return 0;
1530
1531 return stats_dump_one_line(stats, flags, px, appctx);
1532}
1533
1534enum srv_stats_state {
1535 SRV_STATS_STATE_DOWN = 0,
1536 SRV_STATS_STATE_DOWN_AGENT,
1537 SRV_STATS_STATE_GOING_UP,
1538 SRV_STATS_STATE_UP_GOING_DOWN,
1539 SRV_STATS_STATE_UP,
1540 SRV_STATS_STATE_NOLB_GOING_DOWN,
1541 SRV_STATS_STATE_NOLB,
1542 SRV_STATS_STATE_DRAIN_GOING_DOWN,
1543 SRV_STATS_STATE_DRAIN,
1544 SRV_STATS_STATE_DRAIN_AGENT,
1545 SRV_STATS_STATE_NO_CHECK,
1546
1547 SRV_STATS_STATE_COUNT, /* Must be last */
1548};
1549
1550static const char *srv_hlt_st[SRV_STATS_STATE_COUNT] = {
1551 [SRV_STATS_STATE_DOWN] = "DOWN",
1552 [SRV_STATS_STATE_DOWN_AGENT] = "DOWN (agent)",
1553 [SRV_STATS_STATE_GOING_UP] = "DOWN %d/%d",
1554 [SRV_STATS_STATE_UP_GOING_DOWN] = "UP %d/%d",
1555 [SRV_STATS_STATE_UP] = "UP",
1556 [SRV_STATS_STATE_NOLB_GOING_DOWN] = "NOLB %d/%d",
1557 [SRV_STATS_STATE_NOLB] = "NOLB",
1558 [SRV_STATS_STATE_DRAIN_GOING_DOWN] = "DRAIN %d/%d",
1559 [SRV_STATS_STATE_DRAIN] = "DRAIN",
1560 [SRV_STATS_STATE_DRAIN_AGENT] = "DRAIN (agent)",
1561 [SRV_STATS_STATE_NO_CHECK] = "no check"
1562};
1563
1564/* Fill <stats> with the server statistics. <stats> is
1565 * preallocated array of length <len>. The length of the array
1566 * must be at least ST_F_TOTAL_FIELDS. If this length is less
1567 * then this value, the function returns 0, otherwise, it
1568 * returns 1. <flags> can take the value ST_SHLGNDS.
1569 */
1570int stats_fill_sv_stats(struct proxy *px, struct server *sv, int flags,
1571 struct field *stats, int len)
1572{
1573 struct server *via, *ref;
1574 char str[INET6_ADDRSTRLEN];
Willy Tarreau83061a82018-07-13 11:56:34 +02001575 struct buffer *out = get_trash_chunk();
William Lallemand74c24fb2016-11-21 17:18:36 +01001576 enum srv_stats_state state;
1577 char *fld_status;
1578
1579 if (len < ST_F_TOTAL_FIELDS)
1580 return 0;
1581
1582 memset(stats, 0, sizeof(*stats) * len);
1583
1584 /* we have "via" which is the tracked server as described in the configuration,
1585 * and "ref" which is the checked server and the end of the chain.
1586 */
1587 via = sv->track ? sv->track : sv;
1588 ref = via;
1589 while (ref->track)
1590 ref = ref->track;
1591
Emeric Brun52a91d32017-08-31 14:41:55 +02001592 if (sv->cur_state == SRV_ST_RUNNING || sv->cur_state == SRV_ST_STARTING) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001593 if ((ref->check.state & CHK_ST_ENABLED) &&
1594 (ref->check.health < ref->check.rise + ref->check.fall - 1)) {
1595 state = SRV_STATS_STATE_UP_GOING_DOWN;
1596 } else {
1597 state = SRV_STATS_STATE_UP;
1598 }
1599
Emeric Brun52a91d32017-08-31 14:41:55 +02001600 if (sv->cur_admin & SRV_ADMF_DRAIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001601 if (ref->agent.state & CHK_ST_ENABLED)
1602 state = SRV_STATS_STATE_DRAIN_AGENT;
1603 else if (state == SRV_STATS_STATE_UP_GOING_DOWN)
1604 state = SRV_STATS_STATE_DRAIN_GOING_DOWN;
1605 else
1606 state = SRV_STATS_STATE_DRAIN;
1607 }
1608
1609 if (state == SRV_STATS_STATE_UP && !(ref->check.state & CHK_ST_ENABLED)) {
1610 state = SRV_STATS_STATE_NO_CHECK;
1611 }
1612 }
Emeric Brun52a91d32017-08-31 14:41:55 +02001613 else if (sv->cur_state == SRV_ST_STOPPING) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001614 if ((!(sv->check.state & CHK_ST_ENABLED) && !sv->track) ||
1615 (ref->check.health == ref->check.rise + ref->check.fall - 1)) {
1616 state = SRV_STATS_STATE_NOLB;
1617 } else {
1618 state = SRV_STATS_STATE_NOLB_GOING_DOWN;
1619 }
1620 }
1621 else { /* stopped */
1622 if ((ref->agent.state & CHK_ST_ENABLED) && !ref->agent.health) {
1623 state = SRV_STATS_STATE_DOWN_AGENT;
1624 } else if ((ref->check.state & CHK_ST_ENABLED) && !ref->check.health) {
1625 state = SRV_STATS_STATE_DOWN; /* DOWN */
1626 } else if ((ref->agent.state & CHK_ST_ENABLED) || (ref->check.state & CHK_ST_ENABLED)) {
1627 state = SRV_STATS_STATE_GOING_UP;
1628 } else {
1629 state = SRV_STATS_STATE_DOWN; /* DOWN, unchecked */
1630 }
1631 }
1632
1633 chunk_reset(out);
1634
1635 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1636 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, sv->id);
1637 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1638 stats[ST_F_QCUR] = mkf_u32(0, sv->nbpend);
1639 stats[ST_F_QMAX] = mkf_u32(FN_MAX, sv->counters.nbpend_max);
1640 stats[ST_F_SCUR] = mkf_u32(0, sv->cur_sess);
1641 stats[ST_F_SMAX] = mkf_u32(FN_MAX, sv->counters.cur_sess_max);
1642
1643 if (sv->maxconn)
1644 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, sv->maxconn);
1645
1646 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, sv->counters.cum_sess);
1647 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, sv->counters.bytes_in);
1648 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, sv->counters.bytes_out);
1649 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, sv->counters.failed_secu);
1650 stats[ST_F_ECON] = mkf_u64(FN_COUNTER, sv->counters.failed_conns);
1651 stats[ST_F_ERESP] = mkf_u64(FN_COUNTER, sv->counters.failed_resp);
1652 stats[ST_F_WRETR] = mkf_u64(FN_COUNTER, sv->counters.retries);
1653 stats[ST_F_WREDIS] = mkf_u64(FN_COUNTER, sv->counters.redispatches);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001654 stats[ST_F_WREW] = mkf_u64(FN_COUNTER, sv->counters.failed_rewrites);
Willy Tarreauf1573842018-12-14 11:35:36 +01001655 stats[ST_F_CONNECT] = mkf_u64(FN_COUNTER, sv->counters.connect);
1656 stats[ST_F_REUSE] = mkf_u64(FN_COUNTER, sv->counters.reuse);
William Lallemand74c24fb2016-11-21 17:18:36 +01001657
1658 /* status */
1659 fld_status = chunk_newstr(out);
Emeric Brun52a91d32017-08-31 14:41:55 +02001660 if (sv->cur_admin & SRV_ADMF_RMAINT)
William Lallemand74c24fb2016-11-21 17:18:36 +01001661 chunk_appendf(out, "MAINT (resolution)");
Emeric Brun52a91d32017-08-31 14:41:55 +02001662 else if (sv->cur_admin & SRV_ADMF_IMAINT)
William Lallemand74c24fb2016-11-21 17:18:36 +01001663 chunk_appendf(out, "MAINT (via %s/%s)", via->proxy->id, via->id);
Emeric Brun52a91d32017-08-31 14:41:55 +02001664 else if (sv->cur_admin & SRV_ADMF_MAINT)
William Lallemand74c24fb2016-11-21 17:18:36 +01001665 chunk_appendf(out, "MAINT");
1666 else
1667 chunk_appendf(out,
1668 srv_hlt_st[state],
Emeric Brun52a91d32017-08-31 14:41:55 +02001669 (ref->cur_state != SRV_ST_STOPPED) ? (ref->check.health - ref->check.rise + 1) : (ref->check.health),
1670 (ref->cur_state != SRV_ST_STOPPED) ? (ref->check.fall) : (ref->check.rise));
William Lallemand74c24fb2016-11-21 17:18:36 +01001671
1672 stats[ST_F_STATUS] = mkf_str(FO_STATUS, fld_status);
1673 stats[ST_F_LASTCHG] = mkf_u32(FN_AGE, now.tv_sec - sv->last_change);
Emeric Brun52a91d32017-08-31 14:41:55 +02001674 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 +01001675 stats[ST_F_ACT] = mkf_u32(FO_STATUS, (sv->flags & SRV_F_BACKUP) ? 0 : 1);
1676 stats[ST_F_BCK] = mkf_u32(FO_STATUS, (sv->flags & SRV_F_BACKUP) ? 1 : 0);
1677
1678 /* check failures: unique, fatal; last change, total downtime */
1679 if (sv->check.state & CHK_ST_ENABLED) {
1680 stats[ST_F_CHKFAIL] = mkf_u64(FN_COUNTER, sv->counters.failed_checks);
1681 stats[ST_F_CHKDOWN] = mkf_u64(FN_COUNTER, sv->counters.down_trans);
1682 stats[ST_F_DOWNTIME] = mkf_u32(FN_COUNTER, srv_downtime(sv));
1683 }
1684
1685 if (sv->maxqueue)
1686 stats[ST_F_QLIMIT] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->maxqueue);
1687
1688 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
1689 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1690 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, sv->puid);
1691
Emeric Brun52a91d32017-08-31 14:41:55 +02001692 if (sv->cur_state == SRV_ST_STARTING && !server_is_draining(sv))
William Lallemand74c24fb2016-11-21 17:18:36 +01001693 stats[ST_F_THROTTLE] = mkf_u32(FN_AVG, server_throttle_rate(sv));
1694
1695 stats[ST_F_LBTOT] = mkf_u64(FN_COUNTER, sv->counters.cum_lbconn);
1696
1697 if (sv->track) {
1698 char *fld_track = chunk_newstr(out);
1699
1700 chunk_appendf(out, "%s/%s", sv->track->proxy->id, sv->track->id);
1701 stats[ST_F_TRACKED] = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, fld_track);
1702 }
1703
1704 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_SV);
1705 stats[ST_F_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&sv->sess_per_sec));
1706 stats[ST_F_RATE_MAX] = mkf_u32(FN_MAX, sv->counters.sps_max);
1707
1708 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) {
1709 const char *fld_chksts;
1710
1711 fld_chksts = chunk_newstr(out);
1712 chunk_strcat(out, "* "); // for check in progress
1713 chunk_strcat(out, get_check_status_info(sv->check.status));
1714 if (!(sv->check.state & CHK_ST_INPROGRESS))
1715 fld_chksts += 2; // skip "* "
1716 stats[ST_F_CHECK_STATUS] = mkf_str(FN_OUTPUT, fld_chksts);
1717
1718 if (sv->check.status >= HCHK_STATUS_L57DATA)
1719 stats[ST_F_CHECK_CODE] = mkf_u32(FN_OUTPUT, sv->check.code);
1720
1721 if (sv->check.status >= HCHK_STATUS_CHECKED)
1722 stats[ST_F_CHECK_DURATION] = mkf_u64(FN_DURATION, sv->check.duration);
1723
1724 stats[ST_F_CHECK_DESC] = mkf_str(FN_OUTPUT, get_check_status_description(sv->check.status));
1725 stats[ST_F_LAST_CHK] = mkf_str(FN_OUTPUT, sv->check.desc);
1726 stats[ST_F_CHECK_RISE] = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.rise);
1727 stats[ST_F_CHECK_FALL] = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.fall);
1728 stats[ST_F_CHECK_HEALTH] = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.health);
1729 }
1730
1731 if ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) {
1732 const char *fld_chksts;
1733
1734 fld_chksts = chunk_newstr(out);
1735 chunk_strcat(out, "* "); // for check in progress
1736 chunk_strcat(out, get_check_status_info(sv->agent.status));
1737 if (!(sv->agent.state & CHK_ST_INPROGRESS))
1738 fld_chksts += 2; // skip "* "
1739 stats[ST_F_AGENT_STATUS] = mkf_str(FN_OUTPUT, fld_chksts);
1740
1741 if (sv->agent.status >= HCHK_STATUS_L57DATA)
1742 stats[ST_F_AGENT_CODE] = mkf_u32(FN_OUTPUT, sv->agent.code);
1743
1744 if (sv->agent.status >= HCHK_STATUS_CHECKED)
1745 stats[ST_F_AGENT_DURATION] = mkf_u64(FN_DURATION, sv->agent.duration);
1746
1747 stats[ST_F_AGENT_DESC] = mkf_str(FN_OUTPUT, get_check_status_description(sv->agent.status));
1748 stats[ST_F_LAST_AGT] = mkf_str(FN_OUTPUT, sv->agent.desc);
1749 stats[ST_F_AGENT_RISE] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.rise);
1750 stats[ST_F_AGENT_FALL] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.fall);
1751 stats[ST_F_AGENT_HEALTH] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.health);
1752 }
1753
1754 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
1755 if (px->mode == PR_MODE_HTTP) {
1756 stats[ST_F_HRSP_1XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[1]);
1757 stats[ST_F_HRSP_2XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[2]);
1758 stats[ST_F_HRSP_3XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[3]);
1759 stats[ST_F_HRSP_4XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[4]);
1760 stats[ST_F_HRSP_5XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[5]);
1761 stats[ST_F_HRSP_OTHER] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[0]);
1762 }
1763
1764 if (ref->observe)
1765 stats[ST_F_HANAFAIL] = mkf_u64(FN_COUNTER, sv->counters.failed_hana);
1766
1767 stats[ST_F_CLI_ABRT] = mkf_u64(FN_COUNTER, sv->counters.cli_aborts);
1768 stats[ST_F_SRV_ABRT] = mkf_u64(FN_COUNTER, sv->counters.srv_aborts);
1769 stats[ST_F_LASTSESS] = mkf_s32(FN_AGE, srv_lastsession(sv));
1770
1771 stats[ST_F_QTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.q_time, TIME_STATS_SAMPLES));
1772 stats[ST_F_CTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.c_time, TIME_STATS_SAMPLES));
1773 stats[ST_F_RTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.d_time, TIME_STATS_SAMPLES));
1774 stats[ST_F_TTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.t_time, TIME_STATS_SAMPLES));
1775
1776 if (flags & ST_SHLGNDS) {
1777 switch (addr_to_str(&sv->addr, str, sizeof(str))) {
1778 case AF_INET:
1779 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
Willy Tarreau04276f32017-01-06 17:41:29 +01001780 chunk_appendf(out, "%s:%d", str, sv->svc_port);
William Lallemand74c24fb2016-11-21 17:18:36 +01001781 break;
1782 case AF_INET6:
1783 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
Willy Tarreau04276f32017-01-06 17:41:29 +01001784 chunk_appendf(out, "[%s]:%d", str, sv->svc_port);
William Lallemand74c24fb2016-11-21 17:18:36 +01001785 break;
1786 case AF_UNIX:
1787 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, "unix");
1788 break;
1789 case -1:
1790 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1791 chunk_strcat(out, strerror(errno));
1792 break;
1793 default: /* address family not supported */
1794 break;
1795 }
1796
1797 if (sv->cookie)
1798 stats[ST_F_COOKIE] = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, sv->cookie);
1799 }
1800
1801 return 1;
1802}
1803
1804/* Dumps a line for server <sv> and proxy <px> to the trash and uses the state
1805 * from stream interface <si>, stats flags <flags>, and server state <state>.
1806 * The caller is responsible for clearing the trash if needed. Returns non-zero
1807 * if it emits anything, zero otherwise.
1808 */
1809static int stats_dump_sv_stats(struct stream_interface *si, struct proxy *px, int flags, struct server *sv)
1810{
1811 struct appctx *appctx = __objt_appctx(si->end);
1812
1813 if (!stats_fill_sv_stats(px, sv, flags, stats, ST_F_TOTAL_FIELDS))
1814 return 0;
1815
1816 return stats_dump_one_line(stats, flags, px, appctx);
1817}
1818
1819/* Fill <stats> with the backend statistics. <stats> is
1820 * preallocated array of length <len>. The length of the array
1821 * must be at least ST_F_TOTAL_FIELDS. If this length is less
1822 * then this value, the function returns 0, otherwise, it
1823 * returns 1. <flags> can take the value ST_SHLGNDS.
1824 */
1825int stats_fill_be_stats(struct proxy *px, int flags, struct field *stats, int len)
1826{
1827 if (len < ST_F_TOTAL_FIELDS)
1828 return 0;
1829
1830 memset(stats, 0, sizeof(*stats) * len);
1831
1832 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1833 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, "BACKEND");
1834 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1835 stats[ST_F_QCUR] = mkf_u32(0, px->nbpend);
1836 stats[ST_F_QMAX] = mkf_u32(FN_MAX, px->be_counters.nbpend_max);
Thierry FOURNIER0ff98a42016-12-19 16:50:42 +01001837 stats[ST_F_SCUR] = mkf_u32(0, px->beconn);
William Lallemand74c24fb2016-11-21 17:18:36 +01001838 stats[ST_F_SMAX] = mkf_u32(FN_MAX, px->be_counters.conn_max);
1839 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, px->fullconn);
1840 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, px->be_counters.cum_conn);
1841 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, px->be_counters.bytes_in);
1842 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, px->be_counters.bytes_out);
1843 stats[ST_F_DREQ] = mkf_u64(FN_COUNTER, px->be_counters.denied_req);
1844 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, px->be_counters.denied_resp);
1845 stats[ST_F_ECON] = mkf_u64(FN_COUNTER, px->be_counters.failed_conns);
1846 stats[ST_F_ERESP] = mkf_u64(FN_COUNTER, px->be_counters.failed_resp);
1847 stats[ST_F_WRETR] = mkf_u64(FN_COUNTER, px->be_counters.retries);
1848 stats[ST_F_WREDIS] = mkf_u64(FN_COUNTER, px->be_counters.redispatches);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001849 stats[ST_F_WREW] = mkf_u64(FN_COUNTER, px->be_counters.failed_rewrites);
Willy Tarreauf1573842018-12-14 11:35:36 +01001850 stats[ST_F_CONNECT] = mkf_u64(FN_COUNTER, px->be_counters.connect);
1851 stats[ST_F_REUSE] = mkf_u64(FN_COUNTER, px->be_counters.reuse);
William Lallemand74c24fb2016-11-21 17:18:36 +01001852 stats[ST_F_STATUS] = mkf_str(FO_STATUS, (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" : "DOWN");
1853 stats[ST_F_WEIGHT] = mkf_u32(FN_AVG, (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv);
1854 stats[ST_F_ACT] = mkf_u32(0, px->srv_act);
1855 stats[ST_F_BCK] = mkf_u32(0, px->srv_bck);
1856 stats[ST_F_CHKDOWN] = mkf_u64(FN_COUNTER, px->down_trans);
1857 stats[ST_F_LASTCHG] = mkf_u32(FN_AGE, now.tv_sec - px->last_change);
1858 if (px->srv)
1859 stats[ST_F_DOWNTIME] = mkf_u32(FN_COUNTER, be_downtime(px));
1860
1861 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
1862 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1863 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, 0);
1864 stats[ST_F_LBTOT] = mkf_u64(FN_COUNTER, px->be_counters.cum_lbconn);
1865 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_BE);
1866 stats[ST_F_RATE] = mkf_u32(0, read_freq_ctr(&px->be_sess_per_sec));
1867 stats[ST_F_RATE_MAX] = mkf_u32(0, px->be_counters.sps_max);
1868
1869 if (flags & ST_SHLGNDS) {
1870 if (px->cookie_name)
1871 stats[ST_F_COOKIE] = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, px->cookie_name);
1872 stats[ST_F_ALGO] = mkf_str(FO_CONFIG|FS_SERVICE, backend_lb_algo_str(px->lbprm.algo & BE_LB_ALGO));
1873 }
1874
1875 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
1876 if (px->mode == PR_MODE_HTTP) {
1877 stats[ST_F_REQ_TOT] = mkf_u64(FN_COUNTER, px->be_counters.p.http.cum_req);
1878 stats[ST_F_HRSP_1XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[1]);
1879 stats[ST_F_HRSP_2XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[2]);
1880 stats[ST_F_HRSP_3XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[3]);
1881 stats[ST_F_HRSP_4XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[4]);
1882 stats[ST_F_HRSP_5XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[5]);
1883 stats[ST_F_HRSP_OTHER] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[0]);
Willy Tarreaua1214a52018-12-14 14:00:25 +01001884 stats[ST_F_CACHE_LOOKUPS] = mkf_u64(FN_COUNTER, px->be_counters.p.http.cache_lookups);
1885 stats[ST_F_CACHE_HITS] = mkf_u64(FN_COUNTER, px->be_counters.p.http.cache_hits);
William Lallemand74c24fb2016-11-21 17:18:36 +01001886 }
1887
1888 stats[ST_F_CLI_ABRT] = mkf_u64(FN_COUNTER, px->be_counters.cli_aborts);
1889 stats[ST_F_SRV_ABRT] = mkf_u64(FN_COUNTER, px->be_counters.srv_aborts);
1890
1891 /* compression: in, out, bypassed, responses */
1892 stats[ST_F_COMP_IN] = mkf_u64(FN_COUNTER, px->be_counters.comp_in);
1893 stats[ST_F_COMP_OUT] = mkf_u64(FN_COUNTER, px->be_counters.comp_out);
1894 stats[ST_F_COMP_BYP] = mkf_u64(FN_COUNTER, px->be_counters.comp_byp);
1895 stats[ST_F_COMP_RSP] = mkf_u64(FN_COUNTER, px->be_counters.p.http.comp_rsp);
1896 stats[ST_F_LASTSESS] = mkf_s32(FN_AGE, be_lastsession(px));
1897
1898 stats[ST_F_QTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.q_time, TIME_STATS_SAMPLES));
1899 stats[ST_F_CTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.c_time, TIME_STATS_SAMPLES));
1900 stats[ST_F_RTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.d_time, TIME_STATS_SAMPLES));
1901 stats[ST_F_TTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.t_time, TIME_STATS_SAMPLES));
1902
1903 return 1;
1904}
1905
1906/* Dumps a line for backend <px> to the trash for and uses the state from stream
1907 * interface <si> and stats flags <flags>. The caller is responsible for clearing
1908 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
1909 */
1910static int stats_dump_be_stats(struct stream_interface *si, struct proxy *px, int flags)
1911{
1912 struct appctx *appctx = __objt_appctx(si->end);
1913
1914 if (!(px->cap & PR_CAP_BE))
1915 return 0;
1916
1917 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_BE)))
1918 return 0;
1919
1920 if (!stats_fill_be_stats(px, flags, stats, ST_F_TOTAL_FIELDS))
1921 return 0;
1922
1923 return stats_dump_one_line(stats, flags, px, appctx);
1924}
1925
1926/* Dumps the HTML table header for proxy <px> to the trash for and uses the state from
1927 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
1928 * for clearing the trash if needed.
1929 */
1930static void stats_dump_html_px_hdr(struct stream_interface *si, struct proxy *px, struct uri_auth *uri)
1931{
1932 struct appctx *appctx = __objt_appctx(si->end);
1933 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
1934
1935 if (px->cap & PR_CAP_BE && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
1936 /* A form to enable/disable this proxy servers */
1937
1938 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
1939 scope_txt[0] = 0;
1940 if (appctx->ctx.stats.scope_len) {
Christopher Fauleted7a0662019-01-14 11:07:34 +01001941 const char *scope_ptr = stats_scope_ptr(appctx, si);
1942
William Lallemand74c24fb2016-11-21 17:18:36 +01001943 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Christopher Fauleted7a0662019-01-14 11:07:34 +01001944 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), scope_ptr, appctx->ctx.stats.scope_len);
William Lallemand74c24fb2016-11-21 17:18:36 +01001945 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
1946 }
1947
1948 chunk_appendf(&trash,
1949 "<form method=\"post\">");
1950 }
1951
1952 /* print a new table */
1953 chunk_appendf(&trash,
1954 "<table class=\"tbl\" width=\"100%%\">\n"
1955 "<tr class=\"titre\">"
1956 "<th class=\"pxname\" width=\"10%%\">");
1957
1958 chunk_appendf(&trash,
1959 "<a name=\"%s\"></a>%s"
1960 "<a class=px href=\"#%s\">%s</a>",
1961 px->id,
1962 (uri->flags & ST_SHLGNDS) ? "<u>":"",
1963 px->id, px->id);
1964
1965 if (uri->flags & ST_SHLGNDS) {
1966 /* cap, mode, id */
1967 chunk_appendf(&trash, "<div class=tips>cap: %s, mode: %s, id: %d",
1968 proxy_cap_str(px->cap), proxy_mode_str(px->mode),
1969 px->uuid);
1970 chunk_appendf(&trash, "</div>");
1971 }
1972
1973 chunk_appendf(&trash,
1974 "%s</th>"
1975 "<th class=\"%s\" width=\"90%%\">%s</th>"
1976 "</tr>\n"
1977 "</table>\n"
1978 "<table class=\"tbl\" width=\"100%%\">\n"
1979 "<tr class=\"titre\">",
1980 (uri->flags & ST_SHLGNDS) ? "</u>":"",
1981 px->desc ? "desc" : "empty", px->desc ? px->desc : "");
1982
1983 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
1984 /* Column heading for Enable or Disable server */
David Harrigand3db35a2016-12-30 12:12:49 +00001985 chunk_appendf(&trash,
1986 "<th rowspan=2 width=1><input type=\"checkbox\" \
1987 onclick=\"for(c in document.getElementsByClassName('%s-checkbox')) \
1988 document.getElementsByClassName('%s-checkbox').item(c).checked = this.checked\"></th>",
1989 px->id,
1990 px->id);
William Lallemand74c24fb2016-11-21 17:18:36 +01001991 }
1992
1993 chunk_appendf(&trash,
1994 "<th rowspan=2></th>"
1995 "<th colspan=3>Queue</th>"
1996 "<th colspan=3>Session rate</th><th colspan=6>Sessions</th>"
1997 "<th colspan=2>Bytes</th><th colspan=2>Denied</th>"
1998 "<th colspan=3>Errors</th><th colspan=2>Warnings</th>"
1999 "<th colspan=9>Server</th>"
2000 "</tr>\n"
2001 "<tr class=\"titre\">"
2002 "<th>Cur</th><th>Max</th><th>Limit</th>"
2003 "<th>Cur</th><th>Max</th><th>Limit</th><th>Cur</th><th>Max</th>"
2004 "<th>Limit</th><th>Total</th><th>LbTot</th><th>Last</th><th>In</th><th>Out</th>"
2005 "<th>Req</th><th>Resp</th><th>Req</th><th>Conn</th>"
2006 "<th>Resp</th><th>Retr</th><th>Redis</th>"
2007 "<th>Status</th><th>LastChk</th><th>Wght</th><th>Act</th>"
2008 "<th>Bck</th><th>Chk</th><th>Dwn</th><th>Dwntme</th>"
2009 "<th>Thrtle</th>\n"
2010 "</tr>");
2011}
2012
2013/* Dumps the HTML table trailer for proxy <px> to the trash for and uses the state from
2014 * stream interface <si>. The caller is responsible for clearing the trash if needed.
2015 */
2016static void stats_dump_html_px_end(struct stream_interface *si, struct proxy *px)
2017{
2018 struct appctx *appctx = __objt_appctx(si->end);
2019 chunk_appendf(&trash, "</table>");
2020
2021 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
2022 /* close the form used to enable/disable this proxy servers */
2023 chunk_appendf(&trash,
2024 "Choose the action to perform on the checked servers : "
2025 "<select name=action>"
2026 "<option value=\"\"></option>"
2027 "<option value=\"ready\">Set state to READY</option>"
2028 "<option value=\"drain\">Set state to DRAIN</option>"
2029 "<option value=\"maint\">Set state to MAINT</option>"
2030 "<option value=\"dhlth\">Health: disable checks</option>"
2031 "<option value=\"ehlth\">Health: enable checks</option>"
2032 "<option value=\"hrunn\">Health: force UP</option>"
2033 "<option value=\"hnolb\">Health: force NOLB</option>"
2034 "<option value=\"hdown\">Health: force DOWN</option>"
2035 "<option value=\"dagent\">Agent: disable checks</option>"
2036 "<option value=\"eagent\">Agent: enable checks</option>"
2037 "<option value=\"arunn\">Agent: force UP</option>"
2038 "<option value=\"adown\">Agent: force DOWN</option>"
2039 "<option value=\"shutdown\">Kill Sessions</option>"
2040 "</select>"
2041 "<input type=\"hidden\" name=\"b\" value=\"#%d\">"
2042 "&nbsp;<input type=\"submit\" value=\"Apply\">"
2043 "</form>",
2044 px->uuid);
2045 }
2046
2047 chunk_appendf(&trash, "<p>\n");
2048}
2049
2050/*
2051 * Dumps statistics for a proxy. The output is sent to the stream interface's
2052 * input buffer. Returns 0 if it had to stop dumping data because of lack of
2053 * buffer space, or non-zero if everything completed. This function is used
2054 * both by the CLI and the HTTP entry points, and is able to dump the output
2055 * in HTML or CSV formats. If the later, <uri> must be NULL.
2056 */
Christopher Fauletef779222018-10-31 08:47:01 +01002057int stats_dump_proxy_to_buffer(struct stream_interface *si, struct htx *htx,
2058 struct proxy *px, struct uri_auth *uri)
William Lallemand74c24fb2016-11-21 17:18:36 +01002059{
2060 struct appctx *appctx = __objt_appctx(si->end);
2061 struct stream *s = si_strm(si);
2062 struct channel *rep = si_ic(si);
2063 struct server *sv, *svs; /* server and server-state, server-state=server or server->track */
2064 struct listener *l;
2065 unsigned int flags;
2066
2067 if (uri)
2068 flags = uri->flags;
William Lallemand07a62f72017-05-24 00:57:40 +02002069 else if ((strm_li(s)->bind_conf->level & ACCESS_LVL_MASK) >= ACCESS_LVL_OPER)
William Lallemand74c24fb2016-11-21 17:18:36 +01002070 flags = ST_SHLGNDS | ST_SHNODE | ST_SHDESC;
2071 else
2072 flags = ST_SHNODE | ST_SHDESC;
2073
2074 chunk_reset(&trash);
2075
2076 switch (appctx->ctx.stats.px_st) {
2077 case STAT_PX_ST_INIT:
2078 /* we are on a new proxy */
2079 if (uri && uri->scope) {
2080 /* we have a limited scope, we have to check the proxy name */
2081 struct stat_scope *scope;
2082 int len;
2083
2084 len = strlen(px->id);
2085 scope = uri->scope;
2086
2087 while (scope) {
2088 /* match exact proxy name */
2089 if (scope->px_len == len && !memcmp(px->id, scope->px_id, len))
2090 break;
2091
2092 /* match '.' which means 'self' proxy */
2093 if (!strcmp(scope->px_id, ".") && px == s->be)
2094 break;
2095 scope = scope->next;
2096 }
2097
2098 /* proxy name not found : don't dump anything */
2099 if (scope == NULL)
2100 return 1;
2101 }
2102
2103 /* if the user has requested a limited output and the proxy
2104 * name does not match, skip it.
2105 */
Christopher Fauleted7a0662019-01-14 11:07:34 +01002106 if (appctx->ctx.stats.scope_len) {
2107 const char *scope_ptr = stats_scope_ptr(appctx, si);
2108
2109 if (strnistr(px->id, strlen(px->id), scope_ptr, appctx->ctx.stats.scope_len) == NULL)
2110 return 1;
2111 }
William Lallemand74c24fb2016-11-21 17:18:36 +01002112
2113 if ((appctx->ctx.stats.flags & STAT_BOUND) &&
2114 (appctx->ctx.stats.iid != -1) &&
2115 (px->uuid != appctx->ctx.stats.iid))
2116 return 1;
2117
2118 appctx->ctx.stats.px_st = STAT_PX_ST_TH;
2119 /* fall through */
2120
2121 case STAT_PX_ST_TH:
2122 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
2123 stats_dump_html_px_hdr(si, px, uri);
Christopher Fauletef779222018-10-31 08:47:01 +01002124 if (!stats_putchk(rep, htx, &trash))
2125 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002126 }
2127
2128 appctx->ctx.stats.px_st = STAT_PX_ST_FE;
2129 /* fall through */
2130
2131 case STAT_PX_ST_FE:
2132 /* print the frontend */
2133 if (stats_dump_fe_stats(si, px)) {
Christopher Fauletef779222018-10-31 08:47:01 +01002134 if (!stats_putchk(rep, htx, &trash))
2135 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002136 }
2137
2138 appctx->ctx.stats.l = px->conf.listeners.n;
2139 appctx->ctx.stats.px_st = STAT_PX_ST_LI;
2140 /* fall through */
2141
2142 case STAT_PX_ST_LI:
2143 /* stats.l has been initialized above */
2144 for (; appctx->ctx.stats.l != &px->conf.listeners; appctx->ctx.stats.l = l->by_fe.n) {
Christopher Fauletef779222018-10-31 08:47:01 +01002145 if (htx) {
2146 if (htx_almost_full(htx))
2147 goto full;
2148 }
2149 else {
2150 if (buffer_almost_full(&rep->buf))
2151 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002152 }
2153
2154 l = LIST_ELEM(appctx->ctx.stats.l, struct listener *, by_fe);
2155 if (!l->counters)
2156 continue;
2157
2158 if (appctx->ctx.stats.flags & STAT_BOUND) {
2159 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SO)))
2160 break;
2161
2162 if (appctx->ctx.stats.sid != -1 && l->luid != appctx->ctx.stats.sid)
2163 continue;
2164 }
2165
2166 /* print the frontend */
2167 if (stats_dump_li_stats(si, px, l, flags)) {
Christopher Fauletef779222018-10-31 08:47:01 +01002168 if (!stats_putchk(rep, htx, &trash))
2169 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002170 }
2171 }
2172
2173 appctx->ctx.stats.sv = px->srv; /* may be NULL */
2174 appctx->ctx.stats.px_st = STAT_PX_ST_SV;
2175 /* fall through */
2176
2177 case STAT_PX_ST_SV:
2178 /* stats.sv has been initialized above */
2179 for (; appctx->ctx.stats.sv != NULL; appctx->ctx.stats.sv = sv->next) {
Christopher Fauletef779222018-10-31 08:47:01 +01002180 if (htx) {
2181 if (htx_almost_full(htx))
2182 goto full;
2183 }
2184 else {
2185 if (buffer_almost_full(&rep->buf))
2186 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002187 }
2188
2189 sv = appctx->ctx.stats.sv;
2190
2191 if (appctx->ctx.stats.flags & STAT_BOUND) {
2192 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SV)))
2193 break;
2194
2195 if (appctx->ctx.stats.sid != -1 && sv->puid != appctx->ctx.stats.sid)
2196 continue;
2197 }
2198
2199 svs = sv;
2200 while (svs->track)
2201 svs = svs->track;
2202
2203 /* do not report servers which are DOWN and not changing state */
2204 if ((appctx->ctx.stats.flags & STAT_HIDE_DOWN) &&
Emeric Brun52a91d32017-08-31 14:41:55 +02002205 ((sv->cur_admin & SRV_ADMF_MAINT) || /* server is in maintenance */
2206 (sv->cur_state == SRV_ST_STOPPED && /* server is down */
William Lallemand74c24fb2016-11-21 17:18:36 +01002207 (!((svs->agent.state | svs->check.state) & CHK_ST_ENABLED) ||
2208 ((svs->agent.state & CHK_ST_ENABLED) && !svs->agent.health) ||
2209 ((svs->check.state & CHK_ST_ENABLED) && !svs->check.health))))) {
2210 continue;
2211 }
2212
2213 if (stats_dump_sv_stats(si, px, flags, sv)) {
Christopher Fauletef779222018-10-31 08:47:01 +01002214 if (!stats_putchk(rep, htx, &trash))
2215 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002216 }
2217 } /* for sv */
2218
2219 appctx->ctx.stats.px_st = STAT_PX_ST_BE;
2220 /* fall through */
2221
2222 case STAT_PX_ST_BE:
2223 /* print the backend */
2224 if (stats_dump_be_stats(si, px, flags)) {
Christopher Fauletef779222018-10-31 08:47:01 +01002225 if (!stats_putchk(rep, htx, &trash))
2226 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002227 }
2228
2229 appctx->ctx.stats.px_st = STAT_PX_ST_END;
2230 /* fall through */
2231
2232 case STAT_PX_ST_END:
2233 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
2234 stats_dump_html_px_end(si, px);
Christopher Fauletef779222018-10-31 08:47:01 +01002235 if (!stats_putchk(rep, htx, &trash))
2236 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002237 }
2238
2239 appctx->ctx.stats.px_st = STAT_PX_ST_FIN;
2240 /* fall through */
2241
2242 case STAT_PX_ST_FIN:
2243 return 1;
2244
2245 default:
2246 /* unknown state, we should put an abort() here ! */
2247 return 1;
2248 }
Christopher Fauletef779222018-10-31 08:47:01 +01002249
2250 full:
2251 si_rx_room_blk(si);
2252 return 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01002253}
2254
2255/* Dumps the HTTP stats head block to the trash for and uses the per-uri
2256 * parameters <uri>. The caller is responsible for clearing the trash if needed.
2257 */
2258static void stats_dump_html_head(struct uri_auth *uri)
2259{
2260 /* WARNING! This must fit in the first buffer !!! */
2261 chunk_appendf(&trash,
2262 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n"
2263 "\"http://www.w3.org/TR/html4/loose.dtd\">\n"
2264 "<html><head><title>Statistics Report for " PRODUCT_NAME "%s%s</title>\n"
2265 "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n"
2266 "<style type=\"text/css\"><!--\n"
2267 "body {"
2268 " font-family: arial, helvetica, sans-serif;"
2269 " font-size: 12px;"
2270 " font-weight: normal;"
2271 " color: black;"
2272 " background: white;"
2273 "}\n"
2274 "th,td {"
2275 " font-size: 10px;"
2276 "}\n"
2277 "h1 {"
2278 " font-size: x-large;"
2279 " margin-bottom: 0.5em;"
2280 "}\n"
2281 "h2 {"
2282 " font-family: helvetica, arial;"
2283 " font-size: x-large;"
2284 " font-weight: bold;"
2285 " font-style: italic;"
2286 " color: #6020a0;"
2287 " margin-top: 0em;"
2288 " margin-bottom: 0em;"
2289 "}\n"
2290 "h3 {"
2291 " font-family: helvetica, arial;"
2292 " font-size: 16px;"
2293 " font-weight: bold;"
2294 " color: #b00040;"
2295 " background: #e8e8d0;"
2296 " margin-top: 0em;"
2297 " margin-bottom: 0em;"
2298 "}\n"
2299 "li {"
2300 " margin-top: 0.25em;"
2301 " margin-right: 2em;"
2302 "}\n"
2303 ".hr {margin-top: 0.25em;"
2304 " border-color: black;"
2305 " border-bottom-style: solid;"
2306 "}\n"
2307 ".titre {background: #20D0D0;color: #000000; font-weight: bold; text-align: center;}\n"
2308 ".total {background: #20D0D0;color: #ffff80;}\n"
2309 ".frontend {background: #e8e8d0;}\n"
2310 ".socket {background: #d0d0d0;}\n"
2311 ".backend {background: #e8e8d0;}\n"
2312 ".active_down {background: #ff9090;}\n"
2313 ".active_going_up {background: #ffd020;}\n"
2314 ".active_going_down {background: #ffffa0;}\n"
2315 ".active_up {background: #c0ffc0;}\n"
2316 ".active_nolb {background: #20a0ff;}\n"
2317 ".active_draining {background: #20a0FF;}\n"
2318 ".active_no_check {background: #e0e0e0;}\n"
2319 ".backup_down {background: #ff9090;}\n"
2320 ".backup_going_up {background: #ff80ff;}\n"
2321 ".backup_going_down {background: #c060ff;}\n"
2322 ".backup_up {background: #b0d0ff;}\n"
2323 ".backup_nolb {background: #90b0e0;}\n"
2324 ".backup_draining {background: #cc9900;}\n"
2325 ".backup_no_check {background: #e0e0e0;}\n"
2326 ".maintain {background: #c07820;}\n"
2327 ".rls {letter-spacing: 0.2em; margin-right: 1px;}\n" /* right letter spacing (used for grouping digits) */
2328 "\n"
2329 "a.px:link {color: #ffff40; text-decoration: none;}"
2330 "a.px:visited {color: #ffff40; text-decoration: none;}"
2331 "a.px:hover {color: #ffffff; text-decoration: none;}"
2332 "a.lfsb:link {color: #000000; text-decoration: none;}"
2333 "a.lfsb:visited {color: #000000; text-decoration: none;}"
2334 "a.lfsb:hover {color: #505050; text-decoration: none;}"
2335 "\n"
2336 "table.tbl { border-collapse: collapse; border-style: none;}\n"
2337 "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"
2338 "table.tbl td.ac { text-align: center;}\n"
2339 "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}\n"
2340 "table.tbl th.pxname { background: #b00040; color: #ffff40; font-weight: bold; border-style: solid solid none solid; padding: 2px 3px; white-space: nowrap;}\n"
2341 "table.tbl th.empty { border-style: none; empty-cells: hide; background: white;}\n"
2342 "table.tbl th.desc { background: white; border-style: solid solid none solid; text-align: left; padding: 2px 3px;}\n"
2343 "\n"
2344 "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n"
2345 "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n"
2346 "table.lgd td.noborder { border-style: none; padding: 2px; white-space: nowrap;}\n"
2347 "table.det { border-collapse: collapse; border-style: none; }\n"
2348 "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"
2349 "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"
2350 "u {text-decoration:none; border-bottom: 1px dotted black;}\n"
2351 "div.tips {\n"
2352 " display:block;\n"
2353 " visibility:hidden;\n"
2354 " z-index:2147483647;\n"
2355 " position:absolute;\n"
2356 " padding:2px 4px 3px;\n"
2357 " background:#f0f060; color:#000000;\n"
2358 " border:1px solid #7040c0;\n"
2359 " white-space:nowrap;\n"
2360 " font-style:normal;font-size:11px;font-weight:normal;\n"
2361 " -moz-border-radius:3px;-webkit-border-radius:3px;border-radius:3px;\n"
2362 " -moz-box-shadow:gray 2px 2px 3px;-webkit-box-shadow:gray 2px 2px 3px;box-shadow:gray 2px 2px 3px;\n"
2363 "}\n"
2364 "u:hover div.tips {visibility:visible;}\n"
2365 "-->\n"
2366 "</style></head>\n",
2367 (uri->flags & ST_SHNODE) ? " on " : "",
2368 (uri->flags & ST_SHNODE) ? (uri->node ? uri->node : global.node) : ""
2369 );
2370}
2371
2372/* Dumps the HTML stats information block to the trash for and uses the state from
2373 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
2374 * for clearing the trash if needed.
2375 */
2376static void stats_dump_html_info(struct stream_interface *si, struct uri_auth *uri)
2377{
2378 struct appctx *appctx = __objt_appctx(si->end);
2379 unsigned int up = (now.tv_sec - start_date.tv_sec);
2380 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
Christopher Fauleted7a0662019-01-14 11:07:34 +01002381 const char *scope_ptr = stats_scope_ptr(appctx, si);
William Lallemand74c24fb2016-11-21 17:18:36 +01002382
2383 /* WARNING! this has to fit the first packet too.
2384 * We are around 3.5 kB, add adding entries will
2385 * become tricky if we want to support 4kB buffers !
2386 */
2387 chunk_appendf(&trash,
2388 "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">"
2389 PRODUCT_NAME "%s</a></h1>\n"
2390 "<h2>Statistics Report for pid %d%s%s%s%s</h2>\n"
2391 "<hr width=\"100%%\" class=\"hr\">\n"
2392 "<h3>&gt; General process information</h3>\n"
2393 "<table border=0><tr><td align=\"left\" nowrap width=\"1%%\">\n"
Yves Lafon95317282018-02-26 11:10:37 +01002394 "<p><b>pid = </b> %d (process #%d, nbproc = %d, nbthread = %d)<br>\n"
William Lallemand74c24fb2016-11-21 17:18:36 +01002395 "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n"
2396 "<b>system limits:</b> memmax = %s%s; ulimit-n = %d<br>\n"
2397 "<b>maxsock = </b> %d; <b>maxconn = </b> %d; <b>maxpipes = </b> %d<br>\n"
2398 "current conns = %d; current pipes = %d/%d; conn rate = %d/sec<br>\n"
2399 "Running tasks: %d/%d; idle = %d %%<br>\n"
2400 "</td><td align=\"center\" nowrap>\n"
2401 "<table class=\"lgd\"><tr>\n"
2402 "<td class=\"active_up\">&nbsp;</td><td class=\"noborder\">active UP </td>"
2403 "<td class=\"backup_up\">&nbsp;</td><td class=\"noborder\">backup UP </td>"
2404 "</tr><tr>\n"
2405 "<td class=\"active_going_down\"></td><td class=\"noborder\">active UP, going down </td>"
2406 "<td class=\"backup_going_down\"></td><td class=\"noborder\">backup UP, going down </td>"
2407 "</tr><tr>\n"
2408 "<td class=\"active_going_up\"></td><td class=\"noborder\">active DOWN, going up </td>"
2409 "<td class=\"backup_going_up\"></td><td class=\"noborder\">backup DOWN, going up </td>"
2410 "</tr><tr>\n"
2411 "<td class=\"active_down\"></td><td class=\"noborder\">active or backup DOWN &nbsp;</td>"
2412 "<td class=\"active_no_check\"></td><td class=\"noborder\">not checked </td>"
2413 "</tr><tr>\n"
2414 "<td class=\"maintain\"></td><td class=\"noborder\" colspan=\"3\">active or backup DOWN for maintenance (MAINT) &nbsp;</td>"
2415 "</tr><tr>\n"
2416 "<td class=\"active_draining\"></td><td class=\"noborder\" colspan=\"3\">active or backup SOFT STOPPED for maintenance &nbsp;</td>"
2417 "</tr></table>\n"
2418 "Note: \"NOLB\"/\"DRAIN\" = UP with load-balancing disabled."
2419 "</td>"
2420 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
2421 "<b>Display option:</b><ul style=\"margin-top: 0.25em;\">"
2422 "",
Willy Tarreau909b9d82019-01-04 18:20:32 +01002423 (uri->flags & ST_HIDEVER) ? "" : (stats_version_string),
William Lallemand74c24fb2016-11-21 17:18:36 +01002424 pid, (uri->flags & ST_SHNODE) ? " on " : "",
2425 (uri->flags & ST_SHNODE) ? (uri->node ? uri->node : global.node) : "",
2426 (uri->flags & ST_SHDESC) ? ": " : "",
2427 (uri->flags & ST_SHDESC) ? (uri->desc ? uri->desc : global.desc) : "",
Yves Lafon95317282018-02-26 11:10:37 +01002428 pid, relative_pid, global.nbproc, global.nbthread,
William Lallemand74c24fb2016-11-21 17:18:36 +01002429 up / 86400, (up % 86400) / 3600,
2430 (up % 3600) / 60, (up % 60),
2431 global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited",
2432 global.rlimit_memmax ? " MB" : "",
2433 global.rlimit_nofile,
2434 global.maxsock, global.maxconn, global.maxpipes,
2435 actconn, pipes_used, pipes_used+pipes_free, read_freq_ctr(&global.conn_per_sec),
Christopher Faulet34c5cc92016-12-06 09:15:30 +01002436 tasks_run_queue_cur, nb_tasks_cur, idle_pct
William Lallemand74c24fb2016-11-21 17:18:36 +01002437 );
2438
2439 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Christopher Fauleted7a0662019-01-14 11:07:34 +01002440 memcpy(scope_txt, scope_ptr, appctx->ctx.stats.scope_len);
William Lallemand74c24fb2016-11-21 17:18:36 +01002441 scope_txt[appctx->ctx.stats.scope_len] = '\0';
2442
2443 chunk_appendf(&trash,
2444 "<li><form method=\"GET\">Scope : <input value=\"%s\" name=\"" STAT_SCOPE_INPUT_NAME "\" size=\"8\" maxlength=\"%d\" tabindex=\"1\"/></form>\n",
2445 (appctx->ctx.stats.scope_len > 0) ? scope_txt : "",
2446 STAT_SCOPE_TXT_MAXLEN);
2447
2448 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
2449 scope_txt[0] = 0;
2450 if (appctx->ctx.stats.scope_len) {
2451 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Christopher Fauleted7a0662019-01-14 11:07:34 +01002452 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), scope_ptr, appctx->ctx.stats.scope_len);
William Lallemand74c24fb2016-11-21 17:18:36 +01002453 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
2454 }
2455
2456 if (appctx->ctx.stats.flags & STAT_HIDE_DOWN)
2457 chunk_appendf(&trash,
2458 "<li><a href=\"%s%s%s%s\">Show all servers</a><br>\n",
2459 uri->uri_prefix,
2460 "",
2461 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2462 scope_txt);
2463 else
2464 chunk_appendf(&trash,
2465 "<li><a href=\"%s%s%s%s\">Hide 'DOWN' servers</a><br>\n",
2466 uri->uri_prefix,
2467 ";up",
2468 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2469 scope_txt);
2470
2471 if (uri->refresh > 0) {
2472 if (appctx->ctx.stats.flags & STAT_NO_REFRESH)
2473 chunk_appendf(&trash,
2474 "<li><a href=\"%s%s%s%s\">Enable refresh</a><br>\n",
2475 uri->uri_prefix,
2476 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2477 "",
2478 scope_txt);
2479 else
2480 chunk_appendf(&trash,
2481 "<li><a href=\"%s%s%s%s\">Disable refresh</a><br>\n",
2482 uri->uri_prefix,
2483 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2484 ";norefresh",
2485 scope_txt);
2486 }
2487
2488 chunk_appendf(&trash,
2489 "<li><a href=\"%s%s%s%s\">Refresh now</a><br>\n",
2490 uri->uri_prefix,
2491 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2492 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2493 scope_txt);
2494
2495 chunk_appendf(&trash,
2496 "<li><a href=\"%s;csv%s%s\">CSV export</a><br>\n",
2497 uri->uri_prefix,
2498 (uri->refresh > 0) ? ";norefresh" : "",
2499 scope_txt);
2500
2501 chunk_appendf(&trash,
2502 "</ul></td>"
2503 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
2504 "<b>External resources:</b><ul style=\"margin-top: 0.25em;\">\n"
2505 "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>\n"
2506 "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>\n"
2507 "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>\n"
2508 "</ul>"
2509 "</td>"
2510 "</tr></table>\n"
2511 ""
2512 );
2513
2514 if (appctx->ctx.stats.st_code) {
2515 switch (appctx->ctx.stats.st_code) {
2516 case STAT_STATUS_DONE:
2517 chunk_appendf(&trash,
2518 "<p><div class=active_up>"
2519 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2520 "Action processed successfully."
2521 "</div>\n", uri->uri_prefix,
2522 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2523 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2524 scope_txt);
2525 break;
2526 case STAT_STATUS_NONE:
2527 chunk_appendf(&trash,
2528 "<p><div class=active_going_down>"
2529 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2530 "Nothing has changed."
2531 "</div>\n", uri->uri_prefix,
2532 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2533 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2534 scope_txt);
2535 break;
2536 case STAT_STATUS_PART:
2537 chunk_appendf(&trash,
2538 "<p><div class=active_going_down>"
2539 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2540 "Action partially processed.<br>"
2541 "Some server names are probably unknown or ambiguous (duplicated names in the backend)."
2542 "</div>\n", uri->uri_prefix,
2543 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2544 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2545 scope_txt);
2546 break;
2547 case STAT_STATUS_ERRP:
2548 chunk_appendf(&trash,
2549 "<p><div class=active_down>"
2550 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2551 "Action not processed because of invalid parameters."
2552 "<ul>"
2553 "<li>The action is maybe unknown.</li>"
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01002554 "<li>Invalid key parameter (empty or too long).</li>"
William Lallemand74c24fb2016-11-21 17:18:36 +01002555 "<li>The backend name is probably unknown or ambiguous (duplicated names).</li>"
2556 "<li>Some server names are probably unknown or ambiguous (duplicated names in the backend).</li>"
2557 "</ul>"
2558 "</div>\n", uri->uri_prefix,
2559 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2560 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2561 scope_txt);
2562 break;
2563 case STAT_STATUS_EXCD:
2564 chunk_appendf(&trash,
2565 "<p><div class=active_down>"
2566 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2567 "<b>Action not processed : the buffer couldn't store all the data.<br>"
2568 "You should retry with less servers at a time.</b>"
2569 "</div>\n", uri->uri_prefix,
2570 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2571 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2572 scope_txt);
2573 break;
2574 case STAT_STATUS_DENY:
2575 chunk_appendf(&trash,
2576 "<p><div class=active_down>"
2577 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2578 "<b>Action denied.</b>"
2579 "</div>\n", uri->uri_prefix,
2580 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2581 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2582 scope_txt);
2583 break;
Christopher Faulet3c2ecf72019-02-27 16:41:27 +01002584 case STAT_STATUS_IVAL:
2585 chunk_appendf(&trash,
2586 "<p><div class=active_down>"
2587 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2588 "<b>Invalid requests (unsupported method or chunked encoded request).</b>"
2589 "</div>\n", uri->uri_prefix,
2590 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2591 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2592 scope_txt);
2593 break;
William Lallemand74c24fb2016-11-21 17:18:36 +01002594 default:
2595 chunk_appendf(&trash,
2596 "<p><div class=active_no_check>"
2597 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2598 "Unexpected result."
2599 "</div>\n", uri->uri_prefix,
2600 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2601 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2602 scope_txt);
2603 }
2604 chunk_appendf(&trash, "<p>\n");
2605 }
2606}
2607
2608/* Dumps the HTML stats trailer block to the trash. The caller is responsible
2609 * for clearing the trash if needed.
2610 */
2611static void stats_dump_html_end()
2612{
2613 chunk_appendf(&trash, "</body></html>\n");
2614}
2615
Simon Horman05ee2132017-01-04 09:37:25 +01002616/* Dumps the stats JSON header to the trash buffer which. The caller is responsible
2617 * for clearing it if needed.
2618 */
2619static void stats_dump_json_header()
2620{
2621 chunk_strcat(&trash, "[");
2622}
2623
2624
2625/* Dumps the JSON stats trailer block to the trash. The caller is responsible
2626 * for clearing the trash if needed.
2627 */
2628static void stats_dump_json_end()
2629{
2630 chunk_strcat(&trash, "]");
2631}
2632
William Lallemand74c24fb2016-11-21 17:18:36 +01002633/* This function dumps statistics onto the stream interface's read buffer in
2634 * either CSV or HTML format. <uri> contains some HTML-specific parameters that
2635 * are ignored for CSV format (hence <uri> may be NULL there). It returns 0 if
2636 * it had to stop writing data and an I/O is needed, 1 if the dump is finished
2637 * and the stream must be closed, or -1 in case of any error. This function is
2638 * used by both the CLI and the HTTP handlers.
2639 */
Christopher Fauletef779222018-10-31 08:47:01 +01002640static int stats_dump_stat_to_buffer(struct stream_interface *si, struct htx *htx,
2641 struct uri_auth *uri)
William Lallemand74c24fb2016-11-21 17:18:36 +01002642{
2643 struct appctx *appctx = __objt_appctx(si->end);
2644 struct channel *rep = si_ic(si);
2645 struct proxy *px;
2646
2647 chunk_reset(&trash);
2648
2649 switch (appctx->st2) {
2650 case STAT_ST_INIT:
2651 appctx->st2 = STAT_ST_HEAD; /* let's start producing data */
2652 /* fall through */
2653
2654 case STAT_ST_HEAD:
2655 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
2656 stats_dump_html_head(uri);
Simon Horman05ee2132017-01-04 09:37:25 +01002657 else if (appctx->ctx.stats.flags & STAT_FMT_JSON)
Willy Tarreau9d7fb632017-04-11 07:53:04 +02002658 stats_dump_json_header();
William Lallemand74c24fb2016-11-21 17:18:36 +01002659 else if (!(appctx->ctx.stats.flags & STAT_FMT_TYPED))
2660 stats_dump_csv_header();
2661
Christopher Fauletef779222018-10-31 08:47:01 +01002662 if (!stats_putchk(rep, htx, &trash))
2663 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002664
2665 appctx->st2 = STAT_ST_INFO;
2666 /* fall through */
2667
2668 case STAT_ST_INFO:
2669 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
2670 stats_dump_html_info(si, uri);
Christopher Fauletef779222018-10-31 08:47:01 +01002671 if (!stats_putchk(rep, htx, &trash))
2672 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002673 }
2674
Olivier Houchardfbc74e82017-11-24 16:54:05 +01002675 appctx->ctx.stats.px = proxies_list;
William Lallemand74c24fb2016-11-21 17:18:36 +01002676 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
2677 appctx->st2 = STAT_ST_LIST;
2678 /* fall through */
2679
2680 case STAT_ST_LIST:
2681 /* dump proxies */
2682 while (appctx->ctx.stats.px) {
Christopher Fauletef779222018-10-31 08:47:01 +01002683 if (htx) {
2684 if (htx_almost_full(htx))
2685 goto full;
2686 }
2687 else {
2688 if (buffer_almost_full(&rep->buf))
2689 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002690 }
2691
2692 px = appctx->ctx.stats.px;
2693 /* skip the disabled proxies, global frontend and non-networked ones */
2694 if (px->state != PR_STSTOPPED && px->uuid > 0 && (px->cap & (PR_CAP_FE | PR_CAP_BE)))
Christopher Fauletef779222018-10-31 08:47:01 +01002695 if (stats_dump_proxy_to_buffer(si, htx, px, uri) == 0)
William Lallemand74c24fb2016-11-21 17:18:36 +01002696 return 0;
2697
2698 appctx->ctx.stats.px = px->next;
2699 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
2700 }
2701 /* here, we just have reached the last proxy */
2702
2703 appctx->st2 = STAT_ST_END;
2704 /* fall through */
2705
2706 case STAT_ST_END:
Simon Horman05ee2132017-01-04 09:37:25 +01002707 if (appctx->ctx.stats.flags & (STAT_FMT_HTML|STAT_FMT_JSON)) {
2708 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
2709 stats_dump_html_end();
2710 else
2711 stats_dump_json_end();
Christopher Fauletef779222018-10-31 08:47:01 +01002712 if (!stats_putchk(rep, htx, &trash))
2713 goto full;
William Lallemand74c24fb2016-11-21 17:18:36 +01002714 }
2715
2716 appctx->st2 = STAT_ST_FIN;
2717 /* fall through */
2718
2719 case STAT_ST_FIN:
2720 return 1;
2721
2722 default:
2723 /* unknown state ! */
2724 appctx->st2 = STAT_ST_FIN;
2725 return -1;
2726 }
Christopher Fauletef779222018-10-31 08:47:01 +01002727
2728 full:
2729 si_rx_room_blk(si);
2730 return 0;
2731
William Lallemand74c24fb2016-11-21 17:18:36 +01002732}
2733
2734/* We reached the stats page through a POST request. The appctx is
2735 * expected to have already been allocated by the caller.
2736 * Parse the posted data and enable/disable servers if necessary.
2737 * Returns 1 if request was parsed or zero if it needs more data.
2738 */
2739static int stats_process_http_post(struct stream_interface *si)
2740{
2741 struct stream *s = si_strm(si);
2742 struct appctx *appctx = objt_appctx(si->end);
2743
2744 struct proxy *px = NULL;
2745 struct server *sv = NULL;
2746
2747 char key[LINESIZE];
2748 int action = ST_ADM_ACTION_NONE;
2749 int reprocess = 0;
2750
2751 int total_servers = 0;
2752 int altered_servers = 0;
2753
2754 char *first_param, *cur_param, *next_param, *end_params;
2755 char *st_cur_param = NULL;
2756 char *st_next_param = NULL;
2757
Christopher Fauleta3618372018-12-13 21:59:56 +01002758 struct buffer *temp = get_trash_chunk();
William Lallemand74c24fb2016-11-21 17:18:36 +01002759
Christopher Fauleta3618372018-12-13 21:59:56 +01002760 if (IS_HTX_STRM(s)) {
2761 struct htx *htx = htxbuf(&s->req.buf);
2762 struct htx_blk *blk;
Christopher Fauleta3618372018-12-13 21:59:56 +01002763
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01002764 /* we need more data */
2765 if (s->txn->req.msg_state < HTTP_MSG_DONE) {
2766 /* check if we can receive more */
2767 if (htx_free_data_space(htx) <= global.tune.maxrewrite) {
2768 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
2769 goto out;
Christopher Fauleta3618372018-12-13 21:59:56 +01002770 }
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01002771 goto wait;
Christopher Fauleta3618372018-12-13 21:59:56 +01002772 }
William Lallemand74c24fb2016-11-21 17:18:36 +01002773
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01002774 /* The request was fully received. Copy data */
2775 blk = htx_get_head_blk(htx);
2776 while (blk) {
Christopher Fauleta3618372018-12-13 21:59:56 +01002777 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Fauleta3618372018-12-13 21:59:56 +01002778
2779 if (type == HTX_BLK_EOM || type == HTX_BLK_EOD)
2780 break;
2781 if (type == HTX_BLK_DATA) {
2782 struct ist v = htx_get_blk_value(htx, blk);
2783
2784 if (!chunk_memcat(temp, v.ptr, v.len)) {
2785 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
2786 goto out;
2787 }
2788 }
Christopher Fauleted7a0662019-01-14 11:07:34 +01002789 blk = htx_get_next_blk(htx, blk);
Christopher Fauleta3618372018-12-13 21:59:56 +01002790 }
2791 }
2792 else {
2793 int reql;
2794
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01002795 /* we need more data */
2796 if (s->txn->req.msg_state < HTTP_MSG_DONE) {
2797 /* check if we can receive more */
2798 if (c_room(&s->req) <= global.tune.maxrewrite) {
2799 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
2800 goto out;
2801 }
2802 goto wait;
Christopher Fauleta3618372018-12-13 21:59:56 +01002803 }
Christopher Fauleta3618372018-12-13 21:59:56 +01002804 reql = co_getblk(si_oc(si), temp->area, s->txn->req.body_len,
2805 s->txn->req.eoh + 2);
2806 if (reql <= 0) {
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01002807 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
2808 goto out;
Christopher Fauleta3618372018-12-13 21:59:56 +01002809 }
2810 temp->data = reql;
William Lallemand74c24fb2016-11-21 17:18:36 +01002811 }
2812
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002813 first_param = temp->area;
Christopher Fauleta3618372018-12-13 21:59:56 +01002814 end_params = temp->area + temp->data;
William Lallemand74c24fb2016-11-21 17:18:36 +01002815 cur_param = next_param = end_params;
2816 *end_params = '\0';
2817
2818 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
2819
2820 /*
2821 * Parse the parameters in reverse order to only store the last value.
2822 * From the html form, the backend and the action are at the end.
2823 */
2824 while (cur_param > first_param) {
2825 char *value;
2826 int poffset, plen;
2827
2828 cur_param--;
2829
2830 if ((*cur_param == '&') || (cur_param == first_param)) {
2831 reprocess_servers:
2832 /* Parse the key */
2833 poffset = (cur_param != first_param ? 1 : 0);
2834 plen = next_param - cur_param + (cur_param == first_param ? 1 : 0);
2835 if ((plen > 0) && (plen <= sizeof(key))) {
2836 strncpy(key, cur_param + poffset, plen);
2837 key[plen - 1] = '\0';
2838 } else {
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01002839 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
William Lallemand74c24fb2016-11-21 17:18:36 +01002840 goto out;
2841 }
2842
2843 /* Parse the value */
2844 value = key;
2845 while (*value != '\0' && *value != '=') {
2846 value++;
2847 }
2848 if (*value == '=') {
2849 /* Ok, a value is found, we can mark the end of the key */
2850 *value++ = '\0';
2851 }
2852 if (url_decode(key) < 0 || url_decode(value) < 0)
2853 break;
2854
2855 /* Now we can check the key to see what to do */
2856 if (!px && (strcmp(key, "b") == 0)) {
2857 if ((px = proxy_be_by_name(value)) == NULL) {
2858 /* the backend name is unknown or ambiguous (duplicate names) */
2859 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
2860 goto out;
2861 }
2862 }
2863 else if (!action && (strcmp(key, "action") == 0)) {
2864 if (strcmp(value, "ready") == 0) {
2865 action = ST_ADM_ACTION_READY;
2866 }
2867 else if (strcmp(value, "drain") == 0) {
2868 action = ST_ADM_ACTION_DRAIN;
2869 }
2870 else if (strcmp(value, "maint") == 0) {
2871 action = ST_ADM_ACTION_MAINT;
2872 }
2873 else if (strcmp(value, "shutdown") == 0) {
2874 action = ST_ADM_ACTION_SHUTDOWN;
2875 }
2876 else if (strcmp(value, "dhlth") == 0) {
2877 action = ST_ADM_ACTION_DHLTH;
2878 }
2879 else if (strcmp(value, "ehlth") == 0) {
2880 action = ST_ADM_ACTION_EHLTH;
2881 }
2882 else if (strcmp(value, "hrunn") == 0) {
2883 action = ST_ADM_ACTION_HRUNN;
2884 }
2885 else if (strcmp(value, "hnolb") == 0) {
2886 action = ST_ADM_ACTION_HNOLB;
2887 }
2888 else if (strcmp(value, "hdown") == 0) {
2889 action = ST_ADM_ACTION_HDOWN;
2890 }
2891 else if (strcmp(value, "dagent") == 0) {
2892 action = ST_ADM_ACTION_DAGENT;
2893 }
2894 else if (strcmp(value, "eagent") == 0) {
2895 action = ST_ADM_ACTION_EAGENT;
2896 }
2897 else if (strcmp(value, "arunn") == 0) {
2898 action = ST_ADM_ACTION_ARUNN;
2899 }
2900 else if (strcmp(value, "adown") == 0) {
2901 action = ST_ADM_ACTION_ADOWN;
2902 }
2903 /* else these are the old supported methods */
2904 else if (strcmp(value, "disable") == 0) {
2905 action = ST_ADM_ACTION_DISABLE;
2906 }
2907 else if (strcmp(value, "enable") == 0) {
2908 action = ST_ADM_ACTION_ENABLE;
2909 }
2910 else if (strcmp(value, "stop") == 0) {
2911 action = ST_ADM_ACTION_STOP;
2912 }
2913 else if (strcmp(value, "start") == 0) {
2914 action = ST_ADM_ACTION_START;
2915 }
2916 else {
2917 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
2918 goto out;
2919 }
2920 }
2921 else if (strcmp(key, "s") == 0) {
2922 if (!(px && action)) {
2923 /*
2924 * Indicates that we'll need to reprocess the parameters
2925 * as soon as backend and action are known
2926 */
2927 if (!reprocess) {
2928 st_cur_param = cur_param;
2929 st_next_param = next_param;
2930 }
2931 reprocess = 1;
2932 }
2933 else if ((sv = findserver(px, value)) != NULL) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002934 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
William Lallemand74c24fb2016-11-21 17:18:36 +01002935 switch (action) {
2936 case ST_ADM_ACTION_DISABLE:
Emeric Brun52a91d32017-08-31 14:41:55 +02002937 if (!(sv->cur_admin & SRV_ADMF_FMAINT)) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002938 altered_servers++;
2939 total_servers++;
2940 srv_set_admin_flag(sv, SRV_ADMF_FMAINT, "'disable' on stats page");
2941 }
2942 break;
2943 case ST_ADM_ACTION_ENABLE:
Emeric Brun52a91d32017-08-31 14:41:55 +02002944 if (sv->cur_admin & SRV_ADMF_FMAINT) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002945 altered_servers++;
2946 total_servers++;
2947 srv_clr_admin_flag(sv, SRV_ADMF_FMAINT);
2948 }
2949 break;
2950 case ST_ADM_ACTION_STOP:
Emeric Brun52a91d32017-08-31 14:41:55 +02002951 if (!(sv->cur_admin & SRV_ADMF_FDRAIN)) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002952 srv_set_admin_flag(sv, SRV_ADMF_FDRAIN, "'stop' on stats page");
2953 altered_servers++;
2954 total_servers++;
2955 }
2956 break;
2957 case ST_ADM_ACTION_START:
Emeric Brun52a91d32017-08-31 14:41:55 +02002958 if (sv->cur_admin & SRV_ADMF_FDRAIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002959 srv_clr_admin_flag(sv, SRV_ADMF_FDRAIN);
2960 altered_servers++;
2961 total_servers++;
2962 }
2963 break;
2964 case ST_ADM_ACTION_DHLTH:
2965 if (sv->check.state & CHK_ST_CONFIGURED) {
2966 sv->check.state &= ~CHK_ST_ENABLED;
2967 altered_servers++;
2968 total_servers++;
2969 }
2970 break;
2971 case ST_ADM_ACTION_EHLTH:
2972 if (sv->check.state & CHK_ST_CONFIGURED) {
2973 sv->check.state |= CHK_ST_ENABLED;
2974 altered_servers++;
2975 total_servers++;
2976 }
2977 break;
2978 case ST_ADM_ACTION_HRUNN:
2979 if (!(sv->track)) {
2980 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02002981 srv_set_running(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01002982 altered_servers++;
2983 total_servers++;
2984 }
2985 break;
2986 case ST_ADM_ACTION_HNOLB:
2987 if (!(sv->track)) {
2988 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02002989 srv_set_stopping(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01002990 altered_servers++;
2991 total_servers++;
2992 }
2993 break;
2994 case ST_ADM_ACTION_HDOWN:
2995 if (!(sv->track)) {
2996 sv->check.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02002997 srv_set_stopped(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01002998 altered_servers++;
2999 total_servers++;
3000 }
3001 break;
3002 case ST_ADM_ACTION_DAGENT:
3003 if (sv->agent.state & CHK_ST_CONFIGURED) {
3004 sv->agent.state &= ~CHK_ST_ENABLED;
3005 altered_servers++;
3006 total_servers++;
3007 }
3008 break;
3009 case ST_ADM_ACTION_EAGENT:
3010 if (sv->agent.state & CHK_ST_CONFIGURED) {
3011 sv->agent.state |= CHK_ST_ENABLED;
3012 altered_servers++;
3013 total_servers++;
3014 }
3015 break;
3016 case ST_ADM_ACTION_ARUNN:
3017 if (sv->agent.state & CHK_ST_ENABLED) {
3018 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02003019 srv_set_running(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003020 altered_servers++;
3021 total_servers++;
3022 }
3023 break;
3024 case ST_ADM_ACTION_ADOWN:
3025 if (sv->agent.state & CHK_ST_ENABLED) {
3026 sv->agent.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02003027 srv_set_stopped(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01003028 altered_servers++;
3029 total_servers++;
3030 }
3031 break;
3032 case ST_ADM_ACTION_READY:
3033 srv_adm_set_ready(sv);
3034 altered_servers++;
3035 total_servers++;
3036 break;
3037 case ST_ADM_ACTION_DRAIN:
3038 srv_adm_set_drain(sv);
3039 altered_servers++;
3040 total_servers++;
3041 break;
3042 case ST_ADM_ACTION_MAINT:
3043 srv_adm_set_maint(sv);
3044 altered_servers++;
3045 total_servers++;
3046 break;
3047 case ST_ADM_ACTION_SHUTDOWN:
3048 if (px->state != PR_STSTOPPED) {
3049 struct stream *sess, *sess_bck;
3050
3051 list_for_each_entry_safe(sess, sess_bck, &sv->actconns, by_srv)
3052 if (sess->srv_conn == sv)
3053 stream_shutdown(sess, SF_ERR_KILLED);
3054
3055 altered_servers++;
3056 total_servers++;
3057 }
3058 break;
3059 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003060 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Lallemand74c24fb2016-11-21 17:18:36 +01003061 } else {
3062 /* the server name is unknown or ambiguous (duplicate names) */
3063 total_servers++;
3064 }
3065 }
3066 if (reprocess && px && action) {
3067 /* Now, we know the backend and the action chosen by the user.
3068 * We can safely restart from the first server parameter
3069 * to reprocess them
3070 */
3071 cur_param = st_cur_param;
3072 next_param = st_next_param;
3073 reprocess = 0;
3074 goto reprocess_servers;
3075 }
3076
3077 next_param = cur_param;
3078 }
3079 }
3080
3081 if (total_servers == 0) {
3082 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
3083 }
3084 else if (altered_servers == 0) {
3085 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
3086 }
3087 else if (altered_servers == total_servers) {
3088 appctx->ctx.stats.st_code = STAT_STATUS_DONE;
3089 }
3090 else {
3091 appctx->ctx.stats.st_code = STAT_STATUS_PART;
3092 }
3093 out:
3094 return 1;
Christopher Faulet2f9a41d2019-02-27 15:30:57 +01003095 wait:
3096 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
3097 return 0;
Christopher Fauletef779222018-10-31 08:47:01 +01003098}
3099
3100
3101static int stats_send_htx_headers(struct stream_interface *si, struct htx *htx)
3102{
3103 struct stream *s = si_strm(si);
3104 struct uri_auth *uri = s->be->uri_auth;
3105 struct appctx *appctx = __objt_appctx(si->end);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003106 struct htx_sl *sl;
3107 unsigned int flags;
Christopher Fauletef779222018-10-31 08:47:01 +01003108
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003109 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);
3110 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist("200"), ist("OK"));
3111 if (!sl)
Christopher Fauletef779222018-10-31 08:47:01 +01003112 goto full;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003113 sl->info.res.status = 200;
Christopher Fauletef779222018-10-31 08:47:01 +01003114
Christopher Fauletb829f4c2019-03-29 16:13:55 +01003115 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")))
Christopher Fauletef779222018-10-31 08:47:01 +01003116 goto full;
3117 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
3118 if (!htx_add_header(htx, ist("Content-Type"), ist("text/html")))
3119 goto full;
3120 }
3121 else {
3122 if (!htx_add_header(htx, ist("Content-Type"), ist("text/plain")))
3123 goto full;
3124 }
3125
3126 if (uri->refresh > 0 && !(appctx->ctx.stats.flags & STAT_NO_REFRESH)) {
3127 const char *refresh = U2A(uri->refresh);
3128 if (!htx_add_header(htx, ist("Refresh"), ist2(refresh, strlen(refresh))))
3129 goto full;
3130 }
3131
3132 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
3133 if (!htx_add_header(htx, ist("Transfer-Encoding"), ist("chunked")))
3134 goto full;
3135 }
3136
3137 if (!htx_add_endof(htx, HTX_BLK_EOH))
3138 goto full;
3139
Christopher Faulet1e2d6362019-02-27 16:28:48 +01003140 channel_add_input(&s->res, htx->data);
Christopher Fauletef779222018-10-31 08:47:01 +01003141 return 1;
3142
3143 full:
3144 htx_reset(htx);
3145 si_rx_room_blk(si);
3146 return 0;
William Lallemand74c24fb2016-11-21 17:18:36 +01003147}
3148
Christopher Fauletef779222018-10-31 08:47:01 +01003149
3150static int stats_send_htx_redirect(struct stream_interface *si, struct htx *htx)
3151{
3152 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
3153 struct stream *s = si_strm(si);
3154 struct uri_auth *uri = s->be->uri_auth;
3155 struct appctx *appctx = __objt_appctx(si->end);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003156 struct htx_sl *sl;
3157 unsigned int flags;
Christopher Fauletef779222018-10-31 08:47:01 +01003158
3159 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
3160 scope_txt[0] = 0;
3161 if (appctx->ctx.stats.scope_len) {
Christopher Fauleted7a0662019-01-14 11:07:34 +01003162 const char *scope_ptr = stats_scope_ptr(appctx, si);
3163
Christopher Fauletef779222018-10-31 08:47:01 +01003164 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Christopher Fauleted7a0662019-01-14 11:07:34 +01003165 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), scope_ptr, appctx->ctx.stats.scope_len);
Christopher Fauletef779222018-10-31 08:47:01 +01003166 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
3167 }
3168
3169 /* We don't want to land on the posted stats page because a refresh will
3170 * repost the data. We don't want this to happen on accident so we redirect
3171 * the browse to the stats page with a GET.
3172 */
3173 chunk_printf(&trash, "%s;st=%s%s%s%s",
3174 uri->uri_prefix,
3175 ((appctx->ctx.stats.st_code > STAT_STATUS_INIT) &&
3176 (appctx->ctx.stats.st_code < STAT_STATUS_SIZE) &&
3177 stat_status_codes[appctx->ctx.stats.st_code]) ?
3178 stat_status_codes[appctx->ctx.stats.st_code] :
3179 stat_status_codes[STAT_STATUS_UNKN],
3180 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3181 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3182 scope_txt);
3183
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003184 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_CHNK);
3185 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist("303"), ist("See Other"));
3186 if (!sl)
Christopher Fauletef779222018-10-31 08:47:01 +01003187 goto full;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003188 sl->info.res.status = 303;
Christopher Fauletef779222018-10-31 08:47:01 +01003189
3190 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")) ||
Christopher Fauletef779222018-10-31 08:47:01 +01003191 !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 */
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003311 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_SHUTR))
3312 appctx->st0 = STAT_HTTP_END;
Christopher Fauletef779222018-10-31 08:47:01 +01003313
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;
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003332 else if (req->flags & CF_SHUTR)
Christopher Fauletef779222018-10-31 08:47:01 +01003333 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 }
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003347 channel_add_input(&s->res, 1);
3348 appctx->st0 = STAT_HTTP_END;
Christopher Fauletef779222018-10-31 08:47:01 +01003349 }
3350
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003351 if (appctx->st0 == STAT_HTTP_END) {
3352 if (!(res->flags & CF_SHUTR)) {
Christopher Fauletef779222018-10-31 08:47:01 +01003353 res->flags |= CF_READ_NULL;
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003354 si_shutr(si);
3355 }
3356
3357 /* eat the whole request */
3358 if (co_data(req)) {
3359 req_htx = htx_from_buf(&req->buf);
3360 co_htx_skip(req, req_htx, co_data(req));
3361 htx_to_buf(req_htx, &req->buf);
Christopher Fauletef779222018-10-31 08:47:01 +01003362 }
3363 }
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003364
Christopher Fauletef779222018-10-31 08:47:01 +01003365 out:
3366 /* we have left the request in the buffer for the case where we
3367 * process a POST, and this automatically re-enables activity on
3368 * read. It's better to indicate that we want to stop reading when
3369 * we're sending, so that we know there's at most one direction
3370 * deciding to wake the applet up. It saves it from looping when
3371 * emitting large blocks into small TCP windows.
3372 */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003373 htx_to_buf(res_htx, &res->buf);
3374 if (!channel_is_empty(res))
Christopher Fauletef779222018-10-31 08:47:01 +01003375 si_stop_get(si);
Christopher Fauletef779222018-10-31 08:47:01 +01003376}
3377
3378
3379/* This I/O handler runs as an applet embedded in a stream interface. It is
3380 * used to send HTTP stats over a TCP socket. The mechanism is very simple.
3381 * appctx->st0 contains the operation in progress (dump, done). The handler
3382 * automatically unregisters itself once transfer is complete.
3383 */
William Lallemand74c24fb2016-11-21 17:18:36 +01003384static void http_stats_io_handler(struct appctx *appctx)
3385{
3386 struct stream_interface *si = appctx->owner;
3387 struct stream *s = si_strm(si);
3388 struct channel *req = si_oc(si);
3389 struct channel *res = si_ic(si);
3390
Christopher Fauletef779222018-10-31 08:47:01 +01003391 if (IS_HTX_STRM(s))
3392 return htx_stats_io_handler(appctx);
3393
William Lallemand74c24fb2016-11-21 17:18:36 +01003394 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
3395 goto out;
3396
Joseph Herlant29023ec2018-11-15 13:39:46 -08003397 /* Check if the input buffer is available. */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003398 if (res->buf.size == 0) {
Willy Tarreau4b962a42018-11-15 11:03:21 +01003399 /* already subscribed, we'll be called later once the buffer is
3400 * available.
3401 */
Christopher Fauleta73e59b2016-12-09 17:30:18 +01003402 goto out;
3403 }
3404
William Lallemand74c24fb2016-11-21 17:18:36 +01003405 /* check that the output is not closed */
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003406 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_SHUTR))
3407 appctx->st0 = STAT_HTTP_END;
William Lallemand74c24fb2016-11-21 17:18:36 +01003408
3409 /* all states are processed in sequence */
3410 if (appctx->st0 == STAT_HTTP_HEAD) {
3411 if (stats_send_http_headers(si)) {
3412 if (s->txn->meth == HTTP_METH_HEAD)
3413 appctx->st0 = STAT_HTTP_DONE;
3414 else
3415 appctx->st0 = STAT_HTTP_DUMP;
3416 }
3417 }
3418
3419 if (appctx->st0 == STAT_HTTP_DUMP) {
Willy Tarreau97f538b2018-06-15 19:41:31 +02003420 unsigned int prev_len = ci_data(si_ic(si));
William Lallemand74c24fb2016-11-21 17:18:36 +01003421 unsigned int data_len;
3422 unsigned int last_len;
3423 unsigned int last_fwd = 0;
3424
3425 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
3426 /* One difficulty we're facing is that we must prevent
3427 * the input data from being automatically forwarded to
3428 * the output area. For this, we temporarily disable
3429 * forwarding on the channel.
3430 */
3431 last_fwd = si_ic(si)->to_forward;
3432 si_ic(si)->to_forward = 0;
3433 chunk_printf(&trash, "\r\n000000\r\n");
Willy Tarreau06d80a92017-10-19 14:32:15 +02003434 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01003435 si_rx_room_blk(si);
William Lallemand74c24fb2016-11-21 17:18:36 +01003436 si_ic(si)->to_forward = last_fwd;
3437 goto out;
3438 }
3439 }
3440
Willy Tarreau97f538b2018-06-15 19:41:31 +02003441 data_len = ci_data(si_ic(si));
Christopher Fauletef779222018-10-31 08:47:01 +01003442 if (stats_dump_stat_to_buffer(si, NULL, s->be->uri_auth))
William Lallemand74c24fb2016-11-21 17:18:36 +01003443 appctx->st0 = STAT_HTTP_DONE;
3444
Willy Tarreau97f538b2018-06-15 19:41:31 +02003445 last_len = ci_data(si_ic(si));
William Lallemand74c24fb2016-11-21 17:18:36 +01003446
3447 /* Now we must either adjust or remove the chunk size. This is
3448 * not easy because the chunk size might wrap at the end of the
3449 * buffer, so we pretend we have nothing in the buffer, we write
3450 * the size, then restore the buffer's contents. Note that we can
3451 * only do that because no forwarding is scheduled on the stats
3452 * applet.
3453 */
3454 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
3455 si_ic(si)->total -= (last_len - prev_len);
Willy Tarreau97f538b2018-06-15 19:41:31 +02003456 b_sub(si_ib(si), (last_len - prev_len));
William Lallemand74c24fb2016-11-21 17:18:36 +01003457
3458 if (last_len != data_len) {
3459 chunk_printf(&trash, "\r\n%06x\r\n", (last_len - data_len));
Willy Tarreau06d80a92017-10-19 14:32:15 +02003460 if (ci_putchk(si_ic(si), &trash) == -1)
Willy Tarreaudb398432018-11-15 11:08:52 +01003461 si_rx_room_blk(si);
William Lallemand74c24fb2016-11-21 17:18:36 +01003462
3463 si_ic(si)->total += (last_len - data_len);
Willy Tarreau97f538b2018-06-15 19:41:31 +02003464 b_add(si_ib(si), last_len - data_len);
William Lallemand74c24fb2016-11-21 17:18:36 +01003465 }
3466 /* now re-enable forwarding */
3467 channel_forward(si_ic(si), last_fwd);
3468 }
3469 }
3470
3471 if (appctx->st0 == STAT_HTTP_POST) {
3472 if (stats_process_http_post(si))
3473 appctx->st0 = STAT_HTTP_LAST;
3474 else if (si_oc(si)->flags & CF_SHUTR)
3475 appctx->st0 = STAT_HTTP_DONE;
3476 }
3477
3478 if (appctx->st0 == STAT_HTTP_LAST) {
3479 if (stats_send_http_redirect(si))
3480 appctx->st0 = STAT_HTTP_DONE;
3481 }
3482
3483 if (appctx->st0 == STAT_HTTP_DONE) {
3484 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
3485 chunk_printf(&trash, "\r\n0\r\n\r\n");
Willy Tarreau06d80a92017-10-19 14:32:15 +02003486 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01003487 si_rx_room_blk(si);
William Lallemand74c24fb2016-11-21 17:18:36 +01003488 goto out;
3489 }
3490 }
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003491 appctx->st0 = STAT_HTTP_END;
William Lallemand74c24fb2016-11-21 17:18:36 +01003492 }
3493
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003494 if (appctx->st0 == STAT_HTTP_END) {
3495 if (!(res->flags & CF_SHUTR)) {
William Lallemand74c24fb2016-11-21 17:18:36 +01003496 res->flags |= CF_READ_NULL;
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003497 si_shutr(si);
William Lallemand74c24fb2016-11-21 17:18:36 +01003498 }
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003499
3500 /* eat the whole request */
3501 if (co_data(req))
3502 co_skip(si_oc(si), co_data(si_oc(si)));
William Lallemand74c24fb2016-11-21 17:18:36 +01003503 }
Christopher Faulet3a78aa62019-02-27 16:19:48 +01003504
William Lallemand74c24fb2016-11-21 17:18:36 +01003505 out:
Willy Tarreau055ba4f2018-07-24 17:05:54 +02003506 /* we have left the request in the buffer for the case where we
3507 * process a POST, and this automatically re-enables activity on
3508 * read. It's better to indicate that we want to stop reading when
3509 * we're sending, so that we know there's at most one direction
3510 * deciding to wake the applet up. It saves it from looping when
3511 * emitting large blocks into small TCP windows.
3512 */
3513 if (!channel_is_empty(res))
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01003514 si_stop_get(si);
William Lallemand74c24fb2016-11-21 17:18:36 +01003515}
3516
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003517/* Dump all fields from <info> into <out> using the "show info" format (name: value) */
Willy Tarreau83061a82018-07-13 11:56:34 +02003518static int stats_dump_info_fields(struct buffer *out,
3519 const struct field *info)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003520{
3521 int field;
3522
3523 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
3524 if (!field_format(info, field))
3525 continue;
3526
3527 if (!chunk_appendf(out, "%s: ", info_field_names[field]))
3528 return 0;
3529 if (!stats_emit_raw_data_field(out, &info[field]))
3530 return 0;
3531 if (!chunk_strcat(out, "\n"))
3532 return 0;
3533 }
3534 return 1;
3535}
3536
3537/* Dump all fields from <info> into <out> using the "show info typed" format */
Willy Tarreau83061a82018-07-13 11:56:34 +02003538static int stats_dump_typed_info_fields(struct buffer *out,
3539 const struct field *info)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003540{
3541 int field;
3542
3543 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
3544 if (!field_format(info, field))
3545 continue;
3546
3547 if (!chunk_appendf(out, "%d.%s.%u:", field, info_field_names[field], info[INF_PROCESS_NUM].u.u32))
3548 return 0;
3549 if (!stats_emit_field_tags(out, &info[field], ':'))
3550 return 0;
3551 if (!stats_emit_typed_data_field(out, &info[field]))
3552 return 0;
3553 if (!chunk_strcat(out, "\n"))
3554 return 0;
3555 }
3556 return 1;
3557}
3558
3559/* Fill <info> with HAProxy global info. <info> is preallocated
3560 * array of length <len>. The length of the aray must be
3561 * INF_TOTAL_FIELDS. If this length is less then this value, the
3562 * function returns 0, otherwise, it returns 1.
3563 */
3564int stats_fill_info(struct field *info, int len)
3565{
3566 unsigned int up = (now.tv_sec - start_date.tv_sec);
Willy Tarreau83061a82018-07-13 11:56:34 +02003567 struct buffer *out = get_trash_chunk();
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003568
3569#ifdef USE_OPENSSL
3570 int ssl_sess_rate = read_freq_ctr(&global.ssl_per_sec);
3571 int ssl_key_rate = read_freq_ctr(&global.ssl_fe_keys_per_sec);
3572 int ssl_reuse = 0;
3573
3574 if (ssl_key_rate < ssl_sess_rate) {
3575 /* count the ssl reuse ratio and avoid overflows in both directions */
3576 ssl_reuse = 100 - (100 * ssl_key_rate + (ssl_sess_rate - 1) / 2) / ssl_sess_rate;
3577 }
3578#endif
3579
3580 if (len < INF_TOTAL_FIELDS)
3581 return 0;
3582
3583 chunk_reset(out);
3584 memset(info, 0, sizeof(*info) * len);
3585
3586 info[INF_NAME] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, PRODUCT_NAME);
Willy Tarreau909b9d82019-01-04 18:20:32 +01003587 info[INF_VERSION] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, haproxy_version);
3588 info[INF_RELEASE_DATE] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, haproxy_date);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003589
Yves Lafon95317282018-02-26 11:10:37 +01003590 info[INF_NBTHREAD] = mkf_u32(FO_CONFIG|FS_SERVICE, global.nbthread);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003591 info[INF_NBPROC] = mkf_u32(FO_CONFIG|FS_SERVICE, global.nbproc);
3592 info[INF_PROCESS_NUM] = mkf_u32(FO_KEY, relative_pid);
3593 info[INF_PID] = mkf_u32(FO_STATUS, pid);
3594
3595 info[INF_UPTIME] = mkf_str(FN_DURATION, chunk_newstr(out));
3596 chunk_appendf(out, "%ud %uh%02um%02us", up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60));
3597
3598 info[INF_UPTIME_SEC] = mkf_u32(FN_DURATION, up);
3599 info[INF_MEMMAX_MB] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_memmax);
3600 info[INF_POOL_ALLOC_MB] = mkf_u32(0, (unsigned)(pool_total_allocated() / 1048576L));
3601 info[INF_POOL_USED_MB] = mkf_u32(0, (unsigned)(pool_total_used() / 1048576L));
3602 info[INF_POOL_FAILED] = mkf_u32(FN_COUNTER, pool_total_failures());
3603 info[INF_ULIMIT_N] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_nofile);
3604 info[INF_MAXSOCK] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxsock);
3605 info[INF_MAXCONN] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxconn);
3606 info[INF_HARD_MAXCONN] = mkf_u32(FO_CONFIG|FN_LIMIT, global.hardmaxconn);
3607 info[INF_CURR_CONN] = mkf_u32(0, actconn);
3608 info[INF_CUM_CONN] = mkf_u32(FN_COUNTER, totalconn);
3609 info[INF_CUM_REQ] = mkf_u32(FN_COUNTER, global.req_count);
3610#ifdef USE_OPENSSL
3611 info[INF_MAX_SSL_CONNS] = mkf_u32(FN_MAX, global.maxsslconn);
3612 info[INF_CURR_SSL_CONNS] = mkf_u32(0, sslconns);
3613 info[INF_CUM_SSL_CONNS] = mkf_u32(FN_COUNTER, totalsslconns);
3614#endif
3615 info[INF_MAXPIPES] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxpipes);
3616 info[INF_PIPES_USED] = mkf_u32(0, pipes_used);
3617 info[INF_PIPES_FREE] = mkf_u32(0, pipes_free);
3618 info[INF_CONN_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.conn_per_sec));
3619 info[INF_CONN_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.cps_lim);
3620 info[INF_MAX_CONN_RATE] = mkf_u32(FN_MAX, global.cps_max);
3621 info[INF_SESS_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.sess_per_sec));
3622 info[INF_SESS_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.sps_lim);
3623 info[INF_MAX_SESS_RATE] = mkf_u32(FN_RATE, global.sps_max);
3624
3625#ifdef USE_OPENSSL
3626 info[INF_SSL_RATE] = mkf_u32(FN_RATE, ssl_sess_rate);
3627 info[INF_SSL_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.ssl_lim);
3628 info[INF_MAX_SSL_RATE] = mkf_u32(FN_MAX, global.ssl_max);
3629 info[INF_SSL_FRONTEND_KEY_RATE] = mkf_u32(0, ssl_key_rate);
3630 info[INF_SSL_FRONTEND_MAX_KEY_RATE] = mkf_u32(FN_MAX, global.ssl_fe_keys_max);
3631 info[INF_SSL_FRONTEND_SESSION_REUSE_PCT] = mkf_u32(0, ssl_reuse);
3632 info[INF_SSL_BACKEND_KEY_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.ssl_be_keys_per_sec));
3633 info[INF_SSL_BACKEND_MAX_KEY_RATE] = mkf_u32(FN_MAX, global.ssl_be_keys_max);
3634 info[INF_SSL_CACHE_LOOKUPS] = mkf_u32(FN_COUNTER, global.shctx_lookups);
3635 info[INF_SSL_CACHE_MISSES] = mkf_u32(FN_COUNTER, global.shctx_misses);
3636#endif
3637 info[INF_COMPRESS_BPS_IN] = mkf_u32(FN_RATE, read_freq_ctr(&global.comp_bps_in));
3638 info[INF_COMPRESS_BPS_OUT] = mkf_u32(FN_RATE, read_freq_ctr(&global.comp_bps_out));
3639 info[INF_COMPRESS_BPS_RATE_LIM] = mkf_u32(FO_CONFIG|FN_LIMIT, global.comp_rate_lim);
3640#ifdef USE_ZLIB
3641 info[INF_ZLIB_MEM_USAGE] = mkf_u32(0, zlib_used_memory);
3642 info[INF_MAX_ZLIB_MEM_USAGE] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxzlibmem);
3643#endif
3644 info[INF_TASKS] = mkf_u32(0, nb_tasks_cur);
Christopher Faulet34c5cc92016-12-06 09:15:30 +01003645 info[INF_RUN_QUEUE] = mkf_u32(0, tasks_run_queue_cur);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003646 info[INF_IDLE_PCT] = mkf_u32(FN_AVG, idle_pct);
3647 info[INF_NODE] = mkf_str(FO_CONFIG|FN_OUTPUT|FS_SERVICE, global.node);
3648 if (global.desc)
3649 info[INF_DESCRIPTION] = mkf_str(FO_CONFIG|FN_OUTPUT|FS_SERVICE, global.desc);
Willy Tarreau00098ea2018-11-05 14:38:13 +01003650 info[INF_STOPPING] = mkf_u32(0, stopping);
3651 info[INF_JOBS] = mkf_u32(0, jobs);
William Lallemanda7199262018-11-16 16:57:20 +01003652 info[INF_UNSTOPPABLE_JOBS] = mkf_u32(0, unstoppable_jobs);
Willy Tarreau00098ea2018-11-05 14:38:13 +01003653 info[INF_LISTENERS] = mkf_u32(0, listeners);
Willy Tarreau199ad242018-11-05 16:31:22 +01003654 info[INF_ACTIVE_PEERS] = mkf_u32(0, active_peers);
Willy Tarreau2d372c22018-11-05 17:12:27 +01003655 info[INF_CONNECTED_PEERS] = mkf_u32(0, connected_peers);
Willy Tarreau13ef7732018-11-12 07:25:28 +01003656 info[INF_DROPPED_LOGS] = mkf_u32(0, dropped_logs);
Willy Tarreaubeb859a2018-11-22 18:07:59 +01003657 info[INF_BUSY_POLLING] = mkf_u32(0, !!(global.tune.options & GTUNE_BUSY_POLLING));
Baptiste Assmann333939c2019-01-21 08:34:50 +01003658 info[INF_FAILED_RESOLUTIONS] = mkf_u32(0, dns_failed_resolutions);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003659
3660 return 1;
3661}
3662
3663/* This function dumps information onto the stream interface's read buffer.
3664 * It returns 0 as long as it does not complete, non-zero upon completion.
3665 * No state is used.
3666 */
3667static int stats_dump_info_to_buffer(struct stream_interface *si)
3668{
3669 struct appctx *appctx = __objt_appctx(si->end);
3670
3671 if (!stats_fill_info(info, INF_TOTAL_FIELDS))
3672 return 0;
3673
3674 chunk_reset(&trash);
3675
3676 if (appctx->ctx.stats.flags & STAT_FMT_TYPED)
3677 stats_dump_typed_info_fields(&trash, info);
Simon Horman05ee2132017-01-04 09:37:25 +01003678 else if (appctx->ctx.stats.flags & STAT_FMT_JSON)
3679 stats_dump_json_info_fields(&trash, info);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003680 else
3681 stats_dump_info_fields(&trash, info);
3682
Willy Tarreau06d80a92017-10-19 14:32:15 +02003683 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01003684 si_rx_room_blk(si);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003685 return 0;
3686 }
3687
3688 return 1;
3689}
3690
Simon Horman6f6bb382017-01-04 09:37:26 +01003691/* This function dumps the schema onto the stream interface's read buffer.
3692 * It returns 0 as long as it does not complete, non-zero upon completion.
3693 * No state is used.
3694 *
3695 * Integer values bouned to the range [-(2**53)+1, (2**53)-1] as
3696 * per the recommendation for interoperable integers in section 6 of RFC 7159.
3697 */
Willy Tarreau83061a82018-07-13 11:56:34 +02003698static void stats_dump_json_schema(struct buffer *out)
Simon Horman6f6bb382017-01-04 09:37:26 +01003699{
3700
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003701 int old_len = out->data;
Simon Horman6f6bb382017-01-04 09:37:26 +01003702
3703 chunk_strcat(out,
3704 "{"
3705 "\"$schema\":\"http://json-schema.org/draft-04/schema#\","
3706 "\"oneOf\":["
3707 "{"
3708 "\"title\":\"Info\","
3709 "\"type\":\"array\","
3710 "\"items\":{"
3711 "\"properties\":{"
3712 "\"title\":\"InfoItem\","
3713 "\"type\":\"object\","
3714 "\"field\":{\"$ref\":\"#/definitions/field\"},"
3715 "\"processNum\":{\"$ref\":\"#/definitions/processNum\"},"
3716 "\"tags\":{\"$ref\":\"#/definitions/tags\"},"
3717 "\"value\":{\"$ref\":\"#/definitions/typedValue\"}"
3718 "},"
3719 "\"required\":[\"field\",\"processNum\",\"tags\","
3720 "\"value\"]"
3721 "}"
3722 "},"
3723 "{"
3724 "\"title\":\"Stat\","
3725 "\"type\":\"array\","
3726 "\"items\":{"
3727 "\"title\":\"InfoItem\","
3728 "\"type\":\"object\","
3729 "\"properties\":{"
3730 "\"objType\":{"
3731 "\"enum\":[\"Frontend\",\"Backend\",\"Listener\","
3732 "\"Server\",\"Unknown\"]"
3733 "},"
3734 "\"proxyId\":{"
3735 "\"type\":\"integer\","
3736 "\"minimum\":0"
3737 "},"
3738 "\"id\":{"
3739 "\"type\":\"integer\","
3740 "\"minimum\":0"
3741 "},"
3742 "\"field\":{\"$ref\":\"#/definitions/field\"},"
3743 "\"processNum\":{\"$ref\":\"#/definitions/processNum\"},"
3744 "\"tags\":{\"$ref\":\"#/definitions/tags\"},"
3745 "\"typedValue\":{\"$ref\":\"#/definitions/typedValue\"}"
3746 "},"
3747 "\"required\":[\"objType\",\"proxyId\",\"id\","
3748 "\"field\",\"processNum\",\"tags\","
3749 "\"value\"]"
3750 "}"
3751 "},"
3752 "{"
3753 "\"title\":\"Error\","
3754 "\"type\":\"object\","
3755 "\"properties\":{"
3756 "\"errorStr\":{"
3757 "\"type\":\"string\""
3758 "},"
3759 "\"required\":[\"errorStr\"]"
3760 "}"
3761 "}"
3762 "],"
3763 "\"definitions\":{"
3764 "\"field\":{"
3765 "\"type\":\"object\","
3766 "\"pos\":{"
3767 "\"type\":\"integer\","
3768 "\"minimum\":0"
3769 "},"
3770 "\"name\":{"
3771 "\"type\":\"string\""
3772 "},"
3773 "\"required\":[\"pos\",\"name\"]"
3774 "},"
3775 "\"processNum\":{"
3776 "\"type\":\"integer\","
3777 "\"minimum\":1"
3778 "},"
3779 "\"tags\":{"
3780 "\"type\":\"object\","
3781 "\"origin\":{"
3782 "\"type\":\"string\","
3783 "\"enum\":[\"Metric\",\"Status\",\"Key\","
3784 "\"Config\",\"Product\",\"Unknown\"]"
3785 "},"
3786 "\"nature\":{"
3787 "\"type\":\"string\","
3788 "\"enum\":[\"Gauge\",\"Limit\",\"Min\",\"Max\","
3789 "\"Rate\",\"Counter\",\"Duration\","
3790 "\"Age\",\"Time\",\"Name\",\"Output\","
3791 "\"Avg\", \"Unknown\"]"
3792 "},"
3793 "\"scope\":{"
3794 "\"type\":\"string\","
3795 "\"enum\":[\"Cluster\",\"Process\",\"Service\","
3796 "\"System\",\"Unknown\"]"
3797 "},"
3798 "\"required\":[\"origin\",\"nature\",\"scope\"]"
3799 "},"
3800 "\"typedValue\":{"
3801 "\"type\":\"object\","
3802 "\"oneOf\":["
3803 "{\"$ref\":\"#/definitions/typedValue/definitions/s32Value\"},"
3804 "{\"$ref\":\"#/definitions/typedValue/definitions/s64Value\"},"
3805 "{\"$ref\":\"#/definitions/typedValue/definitions/u32Value\"},"
3806 "{\"$ref\":\"#/definitions/typedValue/definitions/u64Value\"},"
3807 "{\"$ref\":\"#/definitions/typedValue/definitions/strValue\"}"
3808 "],"
3809 "\"definitions\":{"
3810 "\"s32Value\":{"
3811 "\"properties\":{"
3812 "\"type\":{"
3813 "\"type\":\"string\","
3814 "\"enum\":[\"s32\"]"
3815 "},"
3816 "\"value\":{"
3817 "\"type\":\"integer\","
3818 "\"minimum\":-2147483648,"
3819 "\"maximum\":2147483647"
3820 "}"
3821 "},"
3822 "\"required\":[\"type\",\"value\"]"
3823 "},"
3824 "\"s64Value\":{"
3825 "\"properties\":{"
3826 "\"type\":{"
3827 "\"type\":\"string\","
3828 "\"enum\":[\"s64\"]"
3829 "},"
3830 "\"value\":{"
3831 "\"type\":\"integer\","
3832 "\"minimum\":-9007199254740991,"
3833 "\"maximum\":9007199254740991"
3834 "}"
3835 "},"
3836 "\"required\":[\"type\",\"value\"]"
3837 "},"
3838 "\"u32Value\":{"
3839 "\"properties\":{"
3840 "\"type\":{"
3841 "\"type\":\"string\","
3842 "\"enum\":[\"u32\"]"
3843 "},"
3844 "\"value\":{"
3845 "\"type\":\"integer\","
3846 "\"minimum\":0,"
3847 "\"maximum\":4294967295"
3848 "}"
3849 "},"
3850 "\"required\":[\"type\",\"value\"]"
3851 "},"
3852 "\"u64Value\":{"
3853 "\"properties\":{"
3854 "\"type\":{"
3855 "\"type\":\"string\","
3856 "\"enum\":[\"u64\"]"
3857 "},"
3858 "\"value\":{"
3859 "\"type\":\"integer\","
3860 "\"minimum\":0,"
3861 "\"maximum\":9007199254740991"
3862 "}"
3863 "},"
3864 "\"required\":[\"type\",\"value\"]"
3865 "},"
3866 "\"strValue\":{"
3867 "\"properties\":{"
3868 "\"type\":{"
3869 "\"type\":\"string\","
3870 "\"enum\":[\"str\"]"
3871 "},"
3872 "\"value\":{\"type\":\"string\"}"
3873 "},"
3874 "\"required\":[\"type\",\"value\"]"
3875 "},"
3876 "\"unknownValue\":{"
3877 "\"properties\":{"
3878 "\"type\":{"
3879 "\"type\":\"integer\","
3880 "\"minimum\":0"
3881 "},"
3882 "\"value\":{"
3883 "\"type\":\"string\","
3884 "\"enum\":[\"unknown\"]"
3885 "}"
3886 "},"
3887 "\"required\":[\"type\",\"value\"]"
3888 "}"
3889 "}"
3890 "}"
3891 "}"
3892 "}");
3893
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003894 if (old_len == out->data) {
Simon Horman6f6bb382017-01-04 09:37:26 +01003895 chunk_reset(out);
3896 chunk_appendf(out,
3897 "{\"errorStr\":\"output buffer too short\"}");
3898 }
3899}
3900
3901/* This function dumps the schema onto the stream interface's read buffer.
3902 * It returns 0 as long as it does not complete, non-zero upon completion.
3903 * No state is used.
3904 */
3905static int stats_dump_json_schema_to_buffer(struct stream_interface *si)
3906{
3907 chunk_reset(&trash);
3908
3909 stats_dump_json_schema(&trash);
3910
Willy Tarreau06d80a92017-10-19 14:32:15 +02003911 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01003912 si_rx_room_blk(si);
Simon Horman6f6bb382017-01-04 09:37:26 +01003913 return 0;
3914 }
3915
3916 return 1;
3917}
3918
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02003919static int cli_parse_clear_counters(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau89d467c2016-11-23 11:02:40 +01003920{
3921 struct proxy *px;
3922 struct server *sv;
3923 struct listener *li;
3924 int clrall = 0;
3925
3926 if (strcmp(args[2], "all") == 0)
3927 clrall = 1;
3928
3929 /* check permissions */
3930 if (!cli_has_level(appctx, ACCESS_LVL_OPER) ||
3931 (clrall && !cli_has_level(appctx, ACCESS_LVL_ADMIN)))
3932 return 1;
3933
Olivier Houchardfbc74e82017-11-24 16:54:05 +01003934 for (px = proxies_list; px; px = px->next) {
Willy Tarreau89d467c2016-11-23 11:02:40 +01003935 if (clrall) {
3936 memset(&px->be_counters, 0, sizeof(px->be_counters));
3937 memset(&px->fe_counters, 0, sizeof(px->fe_counters));
3938 }
3939 else {
3940 px->be_counters.conn_max = 0;
3941 px->be_counters.p.http.rps_max = 0;
3942 px->be_counters.sps_max = 0;
3943 px->be_counters.cps_max = 0;
3944 px->be_counters.nbpend_max = 0;
3945
3946 px->fe_counters.conn_max = 0;
3947 px->fe_counters.p.http.rps_max = 0;
3948 px->fe_counters.sps_max = 0;
3949 px->fe_counters.cps_max = 0;
Willy Tarreau89d467c2016-11-23 11:02:40 +01003950 }
3951
3952 for (sv = px->srv; sv; sv = sv->next)
3953 if (clrall)
3954 memset(&sv->counters, 0, sizeof(sv->counters));
3955 else {
3956 sv->counters.cur_sess_max = 0;
3957 sv->counters.nbpend_max = 0;
3958 sv->counters.sps_max = 0;
3959 }
3960
3961 list_for_each_entry(li, &px->conf.listeners, by_fe)
3962 if (li->counters) {
3963 if (clrall)
3964 memset(li->counters, 0, sizeof(*li->counters));
3965 else
3966 li->counters->conn_max = 0;
3967 }
3968 }
3969
3970 global.cps_max = 0;
3971 global.sps_max = 0;
Olivier Houchard00bc3cb2017-10-17 19:23:25 +02003972 global.ssl_max = 0;
3973 global.ssl_fe_keys_max = 0;
3974 global.ssl_be_keys_max = 0;
Willy Tarreaud80cb4e2018-01-20 19:30:13 +01003975
3976 memset(activity, 0, sizeof(activity));
Willy Tarreau89d467c2016-11-23 11:02:40 +01003977 return 1;
3978}
3979
3980
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02003981static int cli_parse_show_info(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003982{
Willy Tarreaud25fc792016-12-16 12:33:47 +01003983 appctx->ctx.stats.scope_str = 0;
3984 appctx->ctx.stats.scope_len = 0;
3985 appctx->ctx.stats.flags = 0;
3986
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003987 if (strcmp(args[2], "typed") == 0)
3988 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
Simon Horman05ee2132017-01-04 09:37:25 +01003989 else if (strcmp(args[2], "json") == 0)
3990 appctx->ctx.stats.flags |= STAT_FMT_JSON;
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003991 return 0;
3992}
3993
3994
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02003995static int cli_parse_show_stat(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau2b812e22016-11-22 16:18:05 +01003996{
Willy Tarreaud25fc792016-12-16 12:33:47 +01003997 appctx->ctx.stats.scope_str = 0;
3998 appctx->ctx.stats.scope_len = 0;
3999 appctx->ctx.stats.flags = 0;
4000
Willy Tarreau2b812e22016-11-22 16:18:05 +01004001 if (*args[2] && *args[3] && *args[4]) {
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01004002 struct proxy *px;
4003
4004 px = proxy_find_by_name(args[2], 0, 0);
4005 if (px)
4006 appctx->ctx.stats.iid = px->uuid;
4007 else
4008 appctx->ctx.stats.iid = atoi(args[2]);
4009
4010 if (!appctx->ctx.stats.iid) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004011 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01004012 appctx->ctx.cli.msg = "No such proxy.\n";
4013 appctx->st0 = CLI_ST_PRINT;
4014 return 1;
4015 }
4016
Willy Tarreau2b812e22016-11-22 16:18:05 +01004017 appctx->ctx.stats.flags |= STAT_BOUND;
Willy Tarreau2b812e22016-11-22 16:18:05 +01004018 appctx->ctx.stats.type = atoi(args[3]);
4019 appctx->ctx.stats.sid = atoi(args[4]);
4020 if (strcmp(args[5], "typed") == 0)
4021 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
Simon Horman05ee2132017-01-04 09:37:25 +01004022 else if (strcmp(args[5], "json") == 0)
4023 appctx->ctx.stats.flags |= STAT_FMT_JSON;
Willy Tarreau2b812e22016-11-22 16:18:05 +01004024 }
4025 else if (strcmp(args[2], "typed") == 0)
4026 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
Simon Horman05ee2132017-01-04 09:37:25 +01004027 else if (strcmp(args[2], "json") == 0)
4028 appctx->ctx.stats.flags |= STAT_FMT_JSON;
Willy Tarreau2b812e22016-11-22 16:18:05 +01004029
Willy Tarreau2b812e22016-11-22 16:18:05 +01004030 return 0;
4031}
4032
Willy Tarreau0baac8c2016-11-22 16:36:53 +01004033static int cli_io_handler_dump_info(struct appctx *appctx)
4034{
4035 return stats_dump_info_to_buffer(appctx->owner);
4036}
4037
Willy Tarreau2b812e22016-11-22 16:18:05 +01004038/* This I/O handler runs as an applet embedded in a stream interface. It is
4039 * used to send raw stats over a socket.
4040 */
4041static int cli_io_handler_dump_stat(struct appctx *appctx)
4042{
Christopher Fauletef779222018-10-31 08:47:01 +01004043 return stats_dump_stat_to_buffer(appctx->owner, NULL, NULL);
Willy Tarreau2b812e22016-11-22 16:18:05 +01004044}
4045
Simon Horman6f6bb382017-01-04 09:37:26 +01004046static int cli_io_handler_dump_json_schema(struct appctx *appctx)
4047{
4048 return stats_dump_json_schema_to_buffer(appctx->owner);
4049}
4050
Willy Tarreau2b812e22016-11-22 16:18:05 +01004051/* register cli keywords */
4052static struct cli_kw_list cli_kws = {{ },{
Willy Tarreau89d467c2016-11-23 11:02:40 +01004053 { { "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 +01004054 { { "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 +01004055 { { "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 +01004056 { { "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 +01004057 {{},}
4058}};
4059
Willy Tarreau0108d902018-11-25 19:14:37 +01004060INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
4061
William Lallemand74c24fb2016-11-21 17:18:36 +01004062struct applet http_stats_applet = {
4063 .obj_type = OBJ_TYPE_APPLET,
4064 .name = "<STATS>", /* used for logging */
4065 .fct = http_stats_io_handler,
4066 .release = NULL,
4067};
4068
William Lallemand74c24fb2016-11-21 17:18:36 +01004069/*
4070 * Local variables:
4071 * c-indent-level: 8
4072 * c-basic-offset: 8
4073 * End:
4074 */