blob: 7ac18d0d9fea9c88a6e4503635cacee3086139b5 [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>
William Lallemand74c24fb2016-11-21 17:18:36 +010032#include <common/memory.h>
33#include <common/mini-clist.h>
34#include <common/standard.h>
35#include <common/ticks.h>
36#include <common/time.h>
37#include <common/uri_auth.h>
38#include <common/version.h>
39#include <common/base64.h>
40
41#include <types/applet.h>
Willy Tarreau2b812e22016-11-22 16:18:05 +010042#include <types/cli.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010043#include <types/global.h>
44#include <types/dns.h>
William Lallemand9ed62032016-11-21 17:49:11 +010045#include <types/stats.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010046
47#include <proto/backend.h>
48#include <proto/channel.h>
49#include <proto/checks.h>
Willy Tarreau2b812e22016-11-22 16:18:05 +010050#include <proto/cli.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010051#include <proto/compression.h>
William Lallemand9ed62032016-11-21 17:49:11 +010052#include <proto/stats.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010053#include <proto/fd.h>
54#include <proto/freq_ctr.h>
55#include <proto/frontend.h>
56#include <proto/log.h>
57#include <proto/pattern.h>
58#include <proto/pipe.h>
59#include <proto/listener.h>
60#include <proto/map.h>
William Lallemand74c24fb2016-11-21 17:18:36 +010061#include <proto/proxy.h>
62#include <proto/sample.h>
63#include <proto/session.h>
64#include <proto/stream.h>
65#include <proto/server.h>
66#include <proto/raw_sock.h>
67#include <proto/stream_interface.h>
68#include <proto/task.h>
69
70#ifdef USE_OPENSSL
71#include <proto/ssl_sock.h>
72#include <types/ssl_sock.h>
73#endif
74
75
Willy Tarreau0baac8c2016-11-22 16:36:53 +010076/* These are the field names for each INF_* field position. Please pay attention
77 * to always use the exact same name except that the strings for new names must
78 * be lower case or CamelCase while the enum entries must be upper case.
79 */
80const char *info_field_names[INF_TOTAL_FIELDS] = {
81 [INF_NAME] = "Name",
82 [INF_VERSION] = "Version",
83 [INF_RELEASE_DATE] = "Release_date",
Yves Lafon95317282018-02-26 11:10:37 +010084 [INF_NBTHREAD] = "Nbthread",
Willy Tarreau0baac8c2016-11-22 16:36:53 +010085 [INF_NBPROC] = "Nbproc",
86 [INF_PROCESS_NUM] = "Process_num",
87 [INF_PID] = "Pid",
88 [INF_UPTIME] = "Uptime",
89 [INF_UPTIME_SEC] = "Uptime_sec",
90 [INF_MEMMAX_MB] = "Memmax_MB",
91 [INF_POOL_ALLOC_MB] = "PoolAlloc_MB",
92 [INF_POOL_USED_MB] = "PoolUsed_MB",
93 [INF_POOL_FAILED] = "PoolFailed",
94 [INF_ULIMIT_N] = "Ulimit-n",
95 [INF_MAXSOCK] = "Maxsock",
96 [INF_MAXCONN] = "Maxconn",
97 [INF_HARD_MAXCONN] = "Hard_maxconn",
98 [INF_CURR_CONN] = "CurrConns",
99 [INF_CUM_CONN] = "CumConns",
100 [INF_CUM_REQ] = "CumReq",
101 [INF_MAX_SSL_CONNS] = "MaxSslConns",
102 [INF_CURR_SSL_CONNS] = "CurrSslConns",
103 [INF_CUM_SSL_CONNS] = "CumSslConns",
104 [INF_MAXPIPES] = "Maxpipes",
105 [INF_PIPES_USED] = "PipesUsed",
106 [INF_PIPES_FREE] = "PipesFree",
107 [INF_CONN_RATE] = "ConnRate",
108 [INF_CONN_RATE_LIMIT] = "ConnRateLimit",
109 [INF_MAX_CONN_RATE] = "MaxConnRate",
110 [INF_SESS_RATE] = "SessRate",
111 [INF_SESS_RATE_LIMIT] = "SessRateLimit",
112 [INF_MAX_SESS_RATE] = "MaxSessRate",
113 [INF_SSL_RATE] = "SslRate",
114 [INF_SSL_RATE_LIMIT] = "SslRateLimit",
115 [INF_MAX_SSL_RATE] = "MaxSslRate",
116 [INF_SSL_FRONTEND_KEY_RATE] = "SslFrontendKeyRate",
117 [INF_SSL_FRONTEND_MAX_KEY_RATE] = "SslFrontendMaxKeyRate",
118 [INF_SSL_FRONTEND_SESSION_REUSE_PCT] = "SslFrontendSessionReuse_pct",
119 [INF_SSL_BACKEND_KEY_RATE] = "SslBackendKeyRate",
120 [INF_SSL_BACKEND_MAX_KEY_RATE] = "SslBackendMaxKeyRate",
121 [INF_SSL_CACHE_LOOKUPS] = "SslCacheLookups",
122 [INF_SSL_CACHE_MISSES] = "SslCacheMisses",
123 [INF_COMPRESS_BPS_IN] = "CompressBpsIn",
124 [INF_COMPRESS_BPS_OUT] = "CompressBpsOut",
125 [INF_COMPRESS_BPS_RATE_LIM] = "CompressBpsRateLim",
126 [INF_ZLIB_MEM_USAGE] = "ZlibMemUsage",
127 [INF_MAX_ZLIB_MEM_USAGE] = "MaxZlibMemUsage",
128 [INF_TASKS] = "Tasks",
129 [INF_RUN_QUEUE] = "Run_queue",
130 [INF_IDLE_PCT] = "Idle_pct",
131 [INF_NODE] = "node",
132 [INF_DESCRIPTION] = "description",
Willy Tarreau00098ea2018-11-05 14:38:13 +0100133 [INF_STOPPING] = "Stopping",
134 [INF_JOBS] = "Jobs",
135 [INF_LISTENERS] = "Listeners",
Willy Tarreau199ad242018-11-05 16:31:22 +0100136 [INF_ACTIVE_PEERS] = "ActivePeers",
Willy Tarreau2d372c22018-11-05 17:12:27 +0100137 [INF_CONNECTED_PEERS] = "ConnectedPeers",
Willy Tarreau0baac8c2016-11-22 16:36:53 +0100138};
139
William Lallemand74c24fb2016-11-21 17:18:36 +0100140const char *stat_field_names[ST_F_TOTAL_FIELDS] = {
141 [ST_F_PXNAME] = "pxname",
142 [ST_F_SVNAME] = "svname",
143 [ST_F_QCUR] = "qcur",
144 [ST_F_QMAX] = "qmax",
145 [ST_F_SCUR] = "scur",
146 [ST_F_SMAX] = "smax",
147 [ST_F_SLIM] = "slim",
148 [ST_F_STOT] = "stot",
149 [ST_F_BIN] = "bin",
150 [ST_F_BOUT] = "bout",
151 [ST_F_DREQ] = "dreq",
152 [ST_F_DRESP] = "dresp",
153 [ST_F_EREQ] = "ereq",
154 [ST_F_ECON] = "econ",
155 [ST_F_ERESP] = "eresp",
156 [ST_F_WRETR] = "wretr",
157 [ST_F_WREDIS] = "wredis",
158 [ST_F_STATUS] = "status",
159 [ST_F_WEIGHT] = "weight",
160 [ST_F_ACT] = "act",
161 [ST_F_BCK] = "bck",
162 [ST_F_CHKFAIL] = "chkfail",
163 [ST_F_CHKDOWN] = "chkdown",
164 [ST_F_LASTCHG] = "lastchg",
165 [ST_F_DOWNTIME] = "downtime",
166 [ST_F_QLIMIT] = "qlimit",
167 [ST_F_PID] = "pid",
168 [ST_F_IID] = "iid",
169 [ST_F_SID] = "sid",
170 [ST_F_THROTTLE] = "throttle",
171 [ST_F_LBTOT] = "lbtot",
172 [ST_F_TRACKED] = "tracked",
173 [ST_F_TYPE] = "type",
174 [ST_F_RATE] = "rate",
175 [ST_F_RATE_LIM] = "rate_lim",
176 [ST_F_RATE_MAX] = "rate_max",
177 [ST_F_CHECK_STATUS] = "check_status",
178 [ST_F_CHECK_CODE] = "check_code",
179 [ST_F_CHECK_DURATION] = "check_duration",
180 [ST_F_HRSP_1XX] = "hrsp_1xx",
181 [ST_F_HRSP_2XX] = "hrsp_2xx",
182 [ST_F_HRSP_3XX] = "hrsp_3xx",
183 [ST_F_HRSP_4XX] = "hrsp_4xx",
184 [ST_F_HRSP_5XX] = "hrsp_5xx",
185 [ST_F_HRSP_OTHER] = "hrsp_other",
186 [ST_F_HANAFAIL] = "hanafail",
187 [ST_F_REQ_RATE] = "req_rate",
188 [ST_F_REQ_RATE_MAX] = "req_rate_max",
189 [ST_F_REQ_TOT] = "req_tot",
190 [ST_F_CLI_ABRT] = "cli_abrt",
191 [ST_F_SRV_ABRT] = "srv_abrt",
192 [ST_F_COMP_IN] = "comp_in",
193 [ST_F_COMP_OUT] = "comp_out",
194 [ST_F_COMP_BYP] = "comp_byp",
195 [ST_F_COMP_RSP] = "comp_rsp",
196 [ST_F_LASTSESS] = "lastsess",
197 [ST_F_LAST_CHK] = "last_chk",
198 [ST_F_LAST_AGT] = "last_agt",
199 [ST_F_QTIME] = "qtime",
200 [ST_F_CTIME] = "ctime",
201 [ST_F_RTIME] = "rtime",
202 [ST_F_TTIME] = "ttime",
203 [ST_F_AGENT_STATUS] = "agent_status",
204 [ST_F_AGENT_CODE] = "agent_code",
205 [ST_F_AGENT_DURATION] = "agent_duration",
206 [ST_F_CHECK_DESC] = "check_desc",
207 [ST_F_AGENT_DESC] = "agent_desc",
208 [ST_F_CHECK_RISE] = "check_rise",
209 [ST_F_CHECK_FALL] = "check_fall",
210 [ST_F_CHECK_HEALTH] = "check_health",
211 [ST_F_AGENT_RISE] = "agent_rise",
212 [ST_F_AGENT_FALL] = "agent_fall",
213 [ST_F_AGENT_HEALTH] = "agent_health",
214 [ST_F_ADDR] = "addr",
215 [ST_F_COOKIE] = "cookie",
216 [ST_F_MODE] = "mode",
217 [ST_F_ALGO] = "algo",
218 [ST_F_CONN_RATE] = "conn_rate",
219 [ST_F_CONN_RATE_MAX] = "conn_rate_max",
220 [ST_F_CONN_TOT] = "conn_tot",
221 [ST_F_INTERCEPTED] = "intercepted",
222 [ST_F_DCON] = "dcon",
223 [ST_F_DSES] = "dses",
Tim Duesterhus3fd19732018-05-27 20:35:08 +0200224 [ST_F_WREW] = "wrew",
William Lallemand74c24fb2016-11-21 17:18:36 +0100225};
226
Willy Tarreau0baac8c2016-11-22 16:36:53 +0100227/* one line of info */
Christopher Faulet1bc04c72017-10-29 20:14:08 +0100228static THREAD_LOCAL struct field info[INF_TOTAL_FIELDS];
William Lallemand74c24fb2016-11-21 17:18:36 +0100229/* one line of stats */
Christopher Faulet1bc04c72017-10-29 20:14:08 +0100230static THREAD_LOCAL struct field stats[ST_F_TOTAL_FIELDS];
William Lallemand74c24fb2016-11-21 17:18:36 +0100231
232
Willy Tarreau0baac8c2016-11-22 16:36:53 +0100233
William Lallemand74c24fb2016-11-21 17:18:36 +0100234/*
235 * http_stats_io_handler()
236 * -> stats_dump_stat_to_buffer() // same as above, but used for CSV or HTML
237 * -> stats_dump_csv_header() // emits the CSV headers (same as above)
Simon Horman05ee2132017-01-04 09:37:25 +0100238 * -> stats_dump_json_header() // emits the JSON headers (same as above)
William Lallemand74c24fb2016-11-21 17:18:36 +0100239 * -> stats_dump_html_head() // emits the HTML headers
240 * -> stats_dump_html_info() // emits the equivalent of "show info" at the top
241 * -> stats_dump_proxy_to_buffer() // same as above, valid for CSV and HTML
242 * -> stats_dump_html_px_hdr()
243 * -> stats_dump_fe_stats()
244 * -> stats_dump_li_stats()
245 * -> stats_dump_sv_stats()
246 * -> stats_dump_be_stats()
247 * -> stats_dump_html_px_end()
248 * -> stats_dump_html_end() // emits HTML trailer
Simon Horman05ee2132017-01-04 09:37:25 +0100249 * -> stats_dump_json_end() // emits JSON trailer
William Lallemand74c24fb2016-11-21 17:18:36 +0100250 */
251
252
253extern const char *stat_status_codes[];
254
255/* Dumps the stats CSV header to the trash buffer which. The caller is responsible
256 * for clearing it if needed.
257 * NOTE: Some tools happen to rely on the field position instead of its name,
258 * so please only append new fields at the end, never in the middle.
259 */
260static void stats_dump_csv_header()
261{
262 int field;
263
264 chunk_appendf(&trash, "# ");
265 for (field = 0; field < ST_F_TOTAL_FIELDS; field++)
266 chunk_appendf(&trash, "%s,", stat_field_names[field]);
267
268 chunk_appendf(&trash, "\n");
269}
270
271
272/* Emits a stats field without any surrounding element and properly encoded to
273 * resist CSV output. Returns non-zero on success, 0 if the buffer is full.
274 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200275int stats_emit_raw_data_field(struct buffer *out, const struct field *f)
William Lallemand74c24fb2016-11-21 17:18:36 +0100276{
277 switch (field_format(f, 0)) {
278 case FF_EMPTY: return 1;
279 case FF_S32: return chunk_appendf(out, "%d", f->u.s32);
280 case FF_U32: return chunk_appendf(out, "%u", f->u.u32);
281 case FF_S64: return chunk_appendf(out, "%lld", (long long)f->u.s64);
282 case FF_U64: return chunk_appendf(out, "%llu", (unsigned long long)f->u.u64);
283 case FF_STR: return csv_enc_append(field_str(f, 0), 1, out) != NULL;
284 default: return chunk_appendf(out, "[INCORRECT_FIELD_TYPE_%08x]", f->type);
285 }
286}
287
288/* Emits a stats field prefixed with its type. No CSV encoding is prepared, the
289 * output is supposed to be used on its own line. Returns non-zero on success, 0
290 * if the buffer is full.
291 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200292int stats_emit_typed_data_field(struct buffer *out, const struct field *f)
William Lallemand74c24fb2016-11-21 17:18:36 +0100293{
294 switch (field_format(f, 0)) {
295 case FF_EMPTY: return 1;
296 case FF_S32: return chunk_appendf(out, "s32:%d", f->u.s32);
297 case FF_U32: return chunk_appendf(out, "u32:%u", f->u.u32);
298 case FF_S64: return chunk_appendf(out, "s64:%lld", (long long)f->u.s64);
299 case FF_U64: return chunk_appendf(out, "u64:%llu", (unsigned long long)f->u.u64);
300 case FF_STR: return chunk_appendf(out, "str:%s", field_str(f, 0));
301 default: return chunk_appendf(out, "%08x:?", f->type);
302 }
303}
304
Simon Horman05ee2132017-01-04 09:37:25 +0100305/* Limit JSON integer values to the range [-(2**53)+1, (2**53)-1] as per
306 * the recommendation for interoperable integers in section 6 of RFC 7159.
307 */
308#define JSON_INT_MAX ((1ULL << 53) - 1)
309#define JSON_INT_MIN (0 - JSON_INT_MAX)
310
311/* Emits a stats field value and its type in JSON.
312 * Returns non-zero on success, 0 on error.
313 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200314int stats_emit_json_data_field(struct buffer *out, const struct field *f)
Simon Horman05ee2132017-01-04 09:37:25 +0100315{
316 int old_len;
317 char buf[20];
318 const char *type, *value = buf, *quote = "";
319
320 switch (field_format(f, 0)) {
321 case FF_EMPTY: return 1;
322 case FF_S32: type = "\"s32\"";
323 snprintf(buf, sizeof(buf), "%d", f->u.s32);
324 break;
325 case FF_U32: type = "\"u32\"";
326 snprintf(buf, sizeof(buf), "%u", f->u.u32);
327 break;
328 case FF_S64: type = "\"s64\"";
329 if (f->u.s64 < JSON_INT_MIN || f->u.s64 > JSON_INT_MAX)
330 return 0;
331 type = "\"s64\"";
332 snprintf(buf, sizeof(buf), "%lld", (long long)f->u.s64);
333 break;
334 case FF_U64: if (f->u.u64 > JSON_INT_MAX)
335 return 0;
336 type = "\"u64\"";
337 snprintf(buf, sizeof(buf), "%llu",
338 (unsigned long long) f->u.u64);
339 break;
340 case FF_STR: type = "\"str\"";
341 value = field_str(f, 0);
342 quote = "\"";
343 break;
344 default: snprintf(buf, sizeof(buf), "%u", f->type);
345 type = buf;
346 value = "unknown";
347 quote = "\"";
348 break;
349 }
350
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200351 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100352 chunk_appendf(out, ",\"value\":{\"type\":%s,\"value\":%s%s%s}",
353 type, quote, value, quote);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200354 return !(old_len == out->data);
Simon Horman05ee2132017-01-04 09:37:25 +0100355}
356
William Lallemand74c24fb2016-11-21 17:18:36 +0100357/* Emits an encoding of the field type on 3 characters followed by a delimiter.
358 * Returns non-zero on success, 0 if the buffer is full.
359 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200360int stats_emit_field_tags(struct buffer *out, const struct field *f,
361 char delim)
William Lallemand74c24fb2016-11-21 17:18:36 +0100362{
363 char origin, nature, scope;
364
365 switch (field_origin(f, 0)) {
366 case FO_METRIC: origin = 'M'; break;
367 case FO_STATUS: origin = 'S'; break;
368 case FO_KEY: origin = 'K'; break;
369 case FO_CONFIG: origin = 'C'; break;
370 case FO_PRODUCT: origin = 'P'; break;
371 default: origin = '?'; break;
372 }
373
374 switch (field_nature(f, 0)) {
375 case FN_GAUGE: nature = 'G'; break;
376 case FN_LIMIT: nature = 'L'; break;
377 case FN_MIN: nature = 'm'; break;
378 case FN_MAX: nature = 'M'; break;
379 case FN_RATE: nature = 'R'; break;
380 case FN_COUNTER: nature = 'C'; break;
381 case FN_DURATION: nature = 'D'; break;
382 case FN_AGE: nature = 'A'; break;
383 case FN_TIME: nature = 'T'; break;
384 case FN_NAME: nature = 'N'; break;
385 case FN_OUTPUT: nature = 'O'; break;
386 case FN_AVG: nature = 'a'; break;
387 default: nature = '?'; break;
388 }
389
390 switch (field_scope(f, 0)) {
391 case FS_PROCESS: scope = 'P'; break;
392 case FS_SERVICE: scope = 'S'; break;
393 case FS_SYSTEM: scope = 's'; break;
394 case FS_CLUSTER: scope = 'C'; break;
395 default: scope = '?'; break;
396 }
397
398 return chunk_appendf(out, "%c%c%c%c", origin, nature, scope, delim);
399}
400
Simon Horman05ee2132017-01-04 09:37:25 +0100401/* Emits an encoding of the field type as JSON.
402 * Returns non-zero on success, 0 if the buffer is full.
403 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200404int stats_emit_json_field_tags(struct buffer *out, const struct field *f)
Simon Horman05ee2132017-01-04 09:37:25 +0100405{
406 const char *origin, *nature, *scope;
407 int old_len;
408
409 switch (field_origin(f, 0)) {
410 case FO_METRIC: origin = "Metric"; break;
411 case FO_STATUS: origin = "Status"; break;
412 case FO_KEY: origin = "Key"; break;
413 case FO_CONFIG: origin = "Config"; break;
414 case FO_PRODUCT: origin = "Product"; break;
415 default: origin = "Unknown"; break;
416 }
417
418 switch (field_nature(f, 0)) {
419 case FN_GAUGE: nature = "Gauge"; break;
420 case FN_LIMIT: nature = "Limit"; break;
421 case FN_MIN: nature = "Min"; break;
422 case FN_MAX: nature = "Max"; break;
423 case FN_RATE: nature = "Rate"; break;
424 case FN_COUNTER: nature = "Counter"; break;
425 case FN_DURATION: nature = "Duration"; break;
426 case FN_AGE: nature = "Age"; break;
427 case FN_TIME: nature = "Time"; break;
428 case FN_NAME: nature = "Name"; break;
429 case FN_OUTPUT: nature = "Output"; break;
430 case FN_AVG: nature = "Avg"; break;
431 default: nature = "Unknown"; break;
432 }
433
434 switch (field_scope(f, 0)) {
435 case FS_PROCESS: scope = "Process"; break;
436 case FS_SERVICE: scope = "Service"; break;
437 case FS_SYSTEM: scope = "System"; break;
438 case FS_CLUSTER: scope = "Cluster"; break;
439 default: scope = "Unknown"; break;
440 }
441
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200442 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100443 chunk_appendf(out, "\"tags\":{"
444 "\"origin\":\"%s\","
445 "\"nature\":\"%s\","
446 "\"scope\":\"%s\""
447 "}", origin, nature, scope);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200448 return !(old_len == out->data);
Simon Horman05ee2132017-01-04 09:37:25 +0100449}
William Lallemand74c24fb2016-11-21 17:18:36 +0100450
451/* Dump all fields from <stats> into <out> using CSV format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200452static int stats_dump_fields_csv(struct buffer *out,
453 const struct field *stats)
William Lallemand74c24fb2016-11-21 17:18:36 +0100454{
455 int field;
456
457 for (field = 0; field < ST_F_TOTAL_FIELDS; field++) {
458 if (!stats_emit_raw_data_field(out, &stats[field]))
459 return 0;
460 if (!chunk_strcat(out, ","))
461 return 0;
462 }
463 chunk_strcat(out, "\n");
464 return 1;
465}
466
467/* Dump all fields from <stats> into <out> using a typed "field:desc:type:value" format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200468static int stats_dump_fields_typed(struct buffer *out,
469 const struct field *stats)
William Lallemand74c24fb2016-11-21 17:18:36 +0100470{
471 int field;
472
473 for (field = 0; field < ST_F_TOTAL_FIELDS; field++) {
474 if (!stats[field].type)
475 continue;
476
477 chunk_appendf(out, "%c.%u.%u.%d.%s.%u:",
478 stats[ST_F_TYPE].u.u32 == STATS_TYPE_FE ? 'F' :
479 stats[ST_F_TYPE].u.u32 == STATS_TYPE_BE ? 'B' :
480 stats[ST_F_TYPE].u.u32 == STATS_TYPE_SO ? 'L' :
481 stats[ST_F_TYPE].u.u32 == STATS_TYPE_SV ? 'S' :
482 '?',
483 stats[ST_F_IID].u.u32, stats[ST_F_SID].u.u32,
484 field, stat_field_names[field], stats[ST_F_PID].u.u32);
485
486 if (!stats_emit_field_tags(out, &stats[field], ':'))
487 return 0;
488 if (!stats_emit_typed_data_field(out, &stats[field]))
489 return 0;
490 if (!chunk_strcat(out, "\n"))
491 return 0;
492 }
493 return 1;
494}
495
Simon Horman05ee2132017-01-04 09:37:25 +0100496/* Dump all fields from <stats> into <out> using the "show info json" format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200497static int stats_dump_json_info_fields(struct buffer *out,
Simon Horman05ee2132017-01-04 09:37:25 +0100498 const struct field *info)
499{
500 int field;
501 int started = 0;
502
503 if (!chunk_strcat(out, "["))
504 return 0;
505
506 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
507 int old_len;
508
509 if (!field_format(info, field))
510 continue;
511
512 if (started && !chunk_strcat(out, ","))
513 goto err;
514 started = 1;
515
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200516 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100517 chunk_appendf(out,
518 "{\"field\":{\"pos\":%d,\"name\":\"%s\"},"
519 "\"processNum\":%u,",
520 field, info_field_names[field],
521 info[INF_PROCESS_NUM].u.u32);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200522 if (old_len == out->data)
Simon Horman05ee2132017-01-04 09:37:25 +0100523 goto err;
524
525 if (!stats_emit_json_field_tags(out, &info[field]))
526 goto err;
527
528 if (!stats_emit_json_data_field(out, &info[field]))
529 goto err;
530
531 if (!chunk_strcat(out, "}"))
532 goto err;
533 }
534
535 if (!chunk_strcat(out, "]"))
536 goto err;
537 return 1;
538
539err:
540 chunk_reset(out);
541 chunk_appendf(out, "{\"errorStr\":\"output buffer too short\"}");
542 return 0;
543}
544
545/* Dump all fields from <stats> into <out> using a typed "field:desc:type:value" format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200546static int stats_dump_fields_json(struct buffer *out,
547 const struct field *stats,
Simon Horman05ee2132017-01-04 09:37:25 +0100548 int first_stat)
549{
550 int field;
551 int started = 0;
552
553 if (!first_stat && !chunk_strcat(out, ","))
554 return 0;
555 if (!chunk_strcat(out, "["))
556 return 0;
557
558 for (field = 0; field < ST_F_TOTAL_FIELDS; field++) {
559 const char *obj_type;
560 int old_len;
561
562 if (!stats[field].type)
563 continue;
564
565 if (started && !chunk_strcat(out, ","))
566 goto err;
567 started = 1;
568
569 switch (stats[ST_F_TYPE].u.u32) {
570 case STATS_TYPE_FE: obj_type = "Frontend"; break;
571 case STATS_TYPE_BE: obj_type = "Backend"; break;
572 case STATS_TYPE_SO: obj_type = "Listener"; break;
573 case STATS_TYPE_SV: obj_type = "Server"; break;
574 default: obj_type = "Unknown"; break;
575 }
576
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200577 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100578 chunk_appendf(out,
579 "{"
580 "\"objType\":\"%s\","
581 "\"proxyId\":%d,"
582 "\"id\":%d,"
583 "\"field\":{\"pos\":%d,\"name\":\"%s\"},"
584 "\"processNum\":%u,",
585 obj_type, stats[ST_F_IID].u.u32,
586 stats[ST_F_SID].u.u32, field,
587 stat_field_names[field], stats[ST_F_PID].u.u32);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200588 if (old_len == out->data)
Simon Horman05ee2132017-01-04 09:37:25 +0100589 goto err;
590
591 if (!stats_emit_json_field_tags(out, &stats[field]))
592 goto err;
593
594 if (!stats_emit_json_data_field(out, &stats[field]))
595 goto err;
596
597 if (!chunk_strcat(out, "}"))
598 goto err;
599 }
600
601 if (!chunk_strcat(out, "]"))
602 goto err;
603
604 return 1;
605
606err:
607 chunk_reset(out);
608 if (!first_stat)
609 chunk_strcat(out, ",");
610 chunk_appendf(out, "{\"errorStr\":\"output buffer too short\"}");
611 return 0;
612}
613
William Lallemand74c24fb2016-11-21 17:18:36 +0100614/* Dump all fields from <stats> into <out> using the HTML format. A column is
615 * reserved for the checkbox is ST_SHOWADMIN is set in <flags>. Some extra info
616 * are provided if ST_SHLGNDS is present in <flags>.
617 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200618static int stats_dump_fields_html(struct buffer *out,
619 const struct field *stats,
620 unsigned int flags)
William Lallemand74c24fb2016-11-21 17:18:36 +0100621{
Willy Tarreau83061a82018-07-13 11:56:34 +0200622 struct buffer src;
William Lallemand74c24fb2016-11-21 17:18:36 +0100623
624 if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_FE) {
625 chunk_appendf(out,
626 /* name, queue */
627 "<tr class=\"frontend\">");
628
629 if (flags & ST_SHOWADMIN) {
630 /* Column sub-heading for Enable or Disable server */
631 chunk_appendf(out, "<td></td>");
632 }
633
634 chunk_appendf(out,
635 "<td class=ac>"
636 "<a name=\"%s/Frontend\"></a>"
637 "<a class=lfsb href=\"#%s/Frontend\">Frontend</a></td>"
638 "<td colspan=3></td>"
639 "",
640 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_PXNAME));
641
642 chunk_appendf(out,
643 /* sessions rate : current */
644 "<td><u>%s<div class=tips><table class=det>"
645 "<tr><th>Current connection rate:</th><td>%s/s</td></tr>"
646 "<tr><th>Current session rate:</th><td>%s/s</td></tr>"
647 "",
648 U2H(stats[ST_F_RATE].u.u32),
649 U2H(stats[ST_F_CONN_RATE].u.u32),
650 U2H(stats[ST_F_RATE].u.u32));
651
652 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
653 chunk_appendf(out,
654 "<tr><th>Current request rate:</th><td>%s/s</td></tr>",
655 U2H(stats[ST_F_REQ_RATE].u.u32));
656
657 chunk_appendf(out,
658 "</table></div></u></td>"
659 /* sessions rate : max */
660 "<td><u>%s<div class=tips><table class=det>"
661 "<tr><th>Max connection rate:</th><td>%s/s</td></tr>"
662 "<tr><th>Max session rate:</th><td>%s/s</td></tr>"
663 "",
664 U2H(stats[ST_F_RATE_MAX].u.u32),
665 U2H(stats[ST_F_CONN_RATE_MAX].u.u32),
666 U2H(stats[ST_F_RATE_MAX].u.u32));
667
668 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
669 chunk_appendf(out,
670 "<tr><th>Max request rate:</th><td>%s/s</td></tr>",
671 U2H(stats[ST_F_REQ_RATE_MAX].u.u32));
672
673 chunk_appendf(out,
674 "</table></div></u></td>"
675 /* sessions rate : limit */
676 "<td>%s</td>",
677 LIM2A(stats[ST_F_RATE_LIM].u.u32, "-"));
678
679 chunk_appendf(out,
680 /* sessions: current, max, limit, total */
681 "<td>%s</td><td>%s</td><td>%s</td>"
682 "<td><u>%s<div class=tips><table class=det>"
683 "<tr><th>Cum. connections:</th><td>%s</td></tr>"
684 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
685 "",
686 U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SMAX].u.u32), U2H(stats[ST_F_SLIM].u.u32),
687 U2H(stats[ST_F_STOT].u.u64),
688 U2H(stats[ST_F_CONN_TOT].u.u64),
689 U2H(stats[ST_F_STOT].u.u64));
690
691 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
692 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
693 chunk_appendf(out,
694 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
695 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
696 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
697 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
698 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
699 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
700 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
701 "<tr><th>- other responses:</th><td>%s</td></tr>"
702 "<tr><th>Intercepted requests:</th><td>%s</td></tr>"
Willy Tarreau1b0f85e2018-05-28 15:12:40 +0200703 "<tr><th>Failed hdr rewrites:</th><td>%s</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +0100704 "",
705 U2H(stats[ST_F_REQ_TOT].u.u64),
706 U2H(stats[ST_F_HRSP_1XX].u.u64),
707 U2H(stats[ST_F_HRSP_2XX].u.u64),
708 U2H(stats[ST_F_COMP_RSP].u.u64),
709 stats[ST_F_HRSP_2XX].u.u64 ?
710 (int)(100 * stats[ST_F_COMP_RSP].u.u64 / stats[ST_F_HRSP_2XX].u.u64) : 0,
711 U2H(stats[ST_F_HRSP_3XX].u.u64),
712 U2H(stats[ST_F_HRSP_4XX].u.u64),
713 U2H(stats[ST_F_HRSP_5XX].u.u64),
714 U2H(stats[ST_F_HRSP_OTHER].u.u64),
Willy Tarreau1b0f85e2018-05-28 15:12:40 +0200715 U2H(stats[ST_F_INTERCEPTED].u.u64),
716 U2H(stats[ST_F_WREW].u.u64));
William Lallemand74c24fb2016-11-21 17:18:36 +0100717 }
718
719 chunk_appendf(out,
720 "</table></div></u></td>"
721 /* sessions: lbtot, lastsess */
722 "<td></td><td></td>"
723 /* bytes : in */
724 "<td>%s</td>"
725 "",
726 U2H(stats[ST_F_BIN].u.u64));
727
728 chunk_appendf(out,
729 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
730 "<td>%s%s<div class=tips><table class=det>"
731 "<tr><th>Response bytes in:</th><td>%s</td></tr>"
732 "<tr><th>Compression in:</th><td>%s</td></tr>"
733 "<tr><th>Compression out:</th><td>%s</td><td>(%d%%)</td></tr>"
734 "<tr><th>Compression bypass:</th><td>%s</td></tr>"
735 "<tr><th>Total bytes saved:</th><td>%s</td><td>(%d%%)</td></tr>"
736 "</table></div>%s</td>",
737 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "<u>":"",
738 U2H(stats[ST_F_BOUT].u.u64),
739 U2H(stats[ST_F_BOUT].u.u64),
740 U2H(stats[ST_F_COMP_IN].u.u64),
741 U2H(stats[ST_F_COMP_OUT].u.u64),
742 stats[ST_F_COMP_IN].u.u64 ? (int)(stats[ST_F_COMP_OUT].u.u64 * 100 / stats[ST_F_COMP_IN].u.u64) : 0,
743 U2H(stats[ST_F_COMP_BYP].u.u64),
744 U2H(stats[ST_F_COMP_IN].u.u64 - stats[ST_F_COMP_OUT].u.u64),
745 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,
746 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "</u>":"");
747
748 chunk_appendf(out,
749 /* denied: req, resp */
750 "<td>%s</td><td>%s</td>"
751 /* errors : request, connect, response */
752 "<td>%s</td><td></td><td></td>"
753 /* warnings: retries, redispatches */
754 "<td></td><td></td>"
755 /* server status : reflect frontend status */
756 "<td class=ac>%s</td>"
757 /* rest of server: nothing */
758 "<td class=ac colspan=8></td></tr>"
759 "",
760 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
761 U2H(stats[ST_F_EREQ].u.u64),
762 field_str(stats, ST_F_STATUS));
763 }
764 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_SO) {
765 chunk_appendf(out, "<tr class=socket>");
766 if (flags & ST_SHOWADMIN) {
767 /* Column sub-heading for Enable or Disable server */
768 chunk_appendf(out, "<td></td>");
769 }
770
771 chunk_appendf(out,
772 /* frontend name, listener name */
773 "<td class=ac><a name=\"%s/+%s\"></a>%s"
774 "<a class=lfsb href=\"#%s/+%s\">%s</a>"
775 "",
776 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME),
777 (flags & ST_SHLGNDS)?"<u>":"",
778 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME), field_str(stats, ST_F_SVNAME));
779
780 if (flags & ST_SHLGNDS) {
781 chunk_appendf(out, "<div class=tips>");
782
783 if (isdigit(*field_str(stats, ST_F_ADDR)))
784 chunk_appendf(out, "IPv4: %s, ", field_str(stats, ST_F_ADDR));
785 else if (*field_str(stats, ST_F_ADDR) == '[')
786 chunk_appendf(out, "IPv6: %s, ", field_str(stats, ST_F_ADDR));
787 else if (*field_str(stats, ST_F_ADDR))
788 chunk_appendf(out, "%s, ", field_str(stats, ST_F_ADDR));
789
790 /* id */
791 chunk_appendf(out, "id: %d</div>", stats[ST_F_SID].u.u32);
792 }
793
794 chunk_appendf(out,
795 /* queue */
796 "%s</td><td colspan=3></td>"
797 /* sessions rate: current, max, limit */
798 "<td colspan=3>&nbsp;</td>"
799 /* sessions: current, max, limit, total, lbtot, lastsess */
800 "<td>%s</td><td>%s</td><td>%s</td>"
801 "<td>%s</td><td>&nbsp;</td><td>&nbsp;</td>"
802 /* bytes: in, out */
803 "<td>%s</td><td>%s</td>"
804 "",
805 (flags & ST_SHLGNDS)?"</u>":"",
806 U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SMAX].u.u32), U2H(stats[ST_F_SLIM].u.u32),
807 U2H(stats[ST_F_STOT].u.u64), U2H(stats[ST_F_BIN].u.u64), U2H(stats[ST_F_BOUT].u.u64));
808
809 chunk_appendf(out,
810 /* denied: req, resp */
811 "<td>%s</td><td>%s</td>"
812 /* errors: request, connect, response */
813 "<td>%s</td><td></td><td></td>"
814 /* warnings: retries, redispatches */
815 "<td></td><td></td>"
816 /* server status: reflect listener status */
817 "<td class=ac>%s</td>"
818 /* rest of server: nothing */
819 "<td class=ac colspan=8></td></tr>"
820 "",
821 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
822 U2H(stats[ST_F_EREQ].u.u64),
823 field_str(stats, ST_F_STATUS));
824 }
825 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_SV) {
826 const char *style;
827
828 /* determine the style to use depending on the server's state,
829 * its health and weight. There isn't a 1-to-1 mapping between
830 * state and styles for the cases where the server is (still)
831 * up. The reason is that we don't want to report nolb and
832 * drain with the same color.
833 */
834
835 if (strcmp(field_str(stats, ST_F_STATUS), "DOWN") == 0 ||
836 strcmp(field_str(stats, ST_F_STATUS), "DOWN (agent)") == 0) {
837 style = "down";
838 }
839 else if (strcmp(field_str(stats, ST_F_STATUS), "DOWN ") == 0) {
840 style = "going_up";
841 }
842 else if (strcmp(field_str(stats, ST_F_STATUS), "NOLB ") == 0) {
843 style = "going_down";
844 }
845 else if (strcmp(field_str(stats, ST_F_STATUS), "NOLB") == 0) {
846 style = "nolb";
847 }
848 else if (strcmp(field_str(stats, ST_F_STATUS), "no check") == 0) {
849 style = "no_check";
850 }
851 else if (!stats[ST_F_CHKFAIL].type ||
852 stats[ST_F_CHECK_HEALTH].u.u32 == stats[ST_F_CHECK_RISE].u.u32 + stats[ST_F_CHECK_FALL].u.u32 - 1) {
853 /* no check or max health = UP */
854 if (stats[ST_F_WEIGHT].u.u32)
855 style = "up";
856 else
857 style = "draining";
858 }
859 else {
860 style = "going_down";
861 }
862
863 if (memcmp(field_str(stats, ST_F_STATUS), "MAINT", 5) == 0)
864 chunk_appendf(out, "<tr class=\"maintain\">");
865 else
866 chunk_appendf(out,
867 "<tr class=\"%s_%s\">",
868 (stats[ST_F_BCK].u.u32) ? "backup" : "active", style);
869
870
871 if (flags & ST_SHOWADMIN)
872 chunk_appendf(out,
David Harrigand3db35a2016-12-30 12:12:49 +0000873 "<td><input class='%s-checkbox' type=\"checkbox\" name=\"s\" value=\"%s\"></td>",
874 field_str(stats, ST_F_PXNAME),
William Lallemand74c24fb2016-11-21 17:18:36 +0100875 field_str(stats, ST_F_SVNAME));
876
877 chunk_appendf(out,
878 "<td class=ac><a name=\"%s/%s\"></a>%s"
879 "<a class=lfsb href=\"#%s/%s\">%s</a>"
880 "",
881 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME),
882 (flags & ST_SHLGNDS) ? "<u>" : "",
883 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME), field_str(stats, ST_F_SVNAME));
884
885 if (flags & ST_SHLGNDS) {
886 chunk_appendf(out, "<div class=tips>");
887
888 if (isdigit(*field_str(stats, ST_F_ADDR)))
889 chunk_appendf(out, "IPv4: %s, ", field_str(stats, ST_F_ADDR));
890 else if (*field_str(stats, ST_F_ADDR) == '[')
891 chunk_appendf(out, "IPv6: %s, ", field_str(stats, ST_F_ADDR));
892 else if (*field_str(stats, ST_F_ADDR))
893 chunk_appendf(out, "%s, ", field_str(stats, ST_F_ADDR));
894
895 /* id */
896 chunk_appendf(out, "id: %d", stats[ST_F_SID].u.u32);
897
898 /* cookie */
899 if (stats[ST_F_COOKIE].type) {
900 chunk_appendf(out, ", cookie: '");
901 chunk_initstr(&src, field_str(stats, ST_F_COOKIE));
902 chunk_htmlencode(out, &src);
903 chunk_appendf(out, "'");
904 }
905
906 chunk_appendf(out, "</div>");
907 }
908
909 chunk_appendf(out,
910 /* queue : current, max, limit */
911 "%s</td><td>%s</td><td>%s</td><td>%s</td>"
912 /* sessions rate : current, max, limit */
913 "<td>%s</td><td>%s</td><td></td>"
914 "",
915 (flags & ST_SHLGNDS) ? "</u>" : "",
916 U2H(stats[ST_F_QCUR].u.u32), U2H(stats[ST_F_QMAX].u.u32), LIM2A(stats[ST_F_QLIMIT].u.u32, "-"),
917 U2H(stats[ST_F_RATE].u.u32), U2H(stats[ST_F_RATE_MAX].u.u32));
918
919 chunk_appendf(out,
920 /* sessions: current, max, limit, total */
921 "<td>%s</td><td>%s</td><td>%s</td>"
922 "<td><u>%s<div class=tips><table class=det>"
923 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
924 "",
925 U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SMAX].u.u32), LIM2A(stats[ST_F_SLIM].u.u32, "-"),
926 U2H(stats[ST_F_STOT].u.u64),
927 U2H(stats[ST_F_STOT].u.u64));
928
929 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
930 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
931 unsigned long long tot;
932
933 tot = stats[ST_F_HRSP_OTHER].u.u64;
934 tot += stats[ST_F_HRSP_1XX].u.u64;
935 tot += stats[ST_F_HRSP_2XX].u.u64;
936 tot += stats[ST_F_HRSP_3XX].u.u64;
937 tot += stats[ST_F_HRSP_4XX].u.u64;
938 tot += stats[ST_F_HRSP_5XX].u.u64;
939
940 chunk_appendf(out,
941 "<tr><th>Cum. HTTP responses:</th><td>%s</td></tr>"
942 "<tr><th>- HTTP 1xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
943 "<tr><th>- HTTP 2xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
944 "<tr><th>- HTTP 3xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
945 "<tr><th>- HTTP 4xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
946 "<tr><th>- HTTP 5xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
947 "<tr><th>- other responses:</th><td>%s</td><td>(%d%%)</td></tr>"
Willy Tarreau1b0f85e2018-05-28 15:12:40 +0200948 "<tr><th>Failed hdr rewrites:</th><td>%s</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +0100949 "",
950 U2H(tot),
951 U2H(stats[ST_F_HRSP_1XX].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_1XX].u.u64 / tot) : 0,
952 U2H(stats[ST_F_HRSP_2XX].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_2XX].u.u64 / tot) : 0,
953 U2H(stats[ST_F_HRSP_3XX].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_3XX].u.u64 / tot) : 0,
954 U2H(stats[ST_F_HRSP_4XX].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_4XX].u.u64 / tot) : 0,
955 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 +0200956 U2H(stats[ST_F_HRSP_OTHER].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_OTHER].u.u64 / tot) : 0,
957 U2H(stats[ST_F_WREW].u.u64));
William Lallemand74c24fb2016-11-21 17:18:36 +0100958 }
959
960 chunk_appendf(out, "<tr><th colspan=3>Avg over last 1024 success. conn.</th></tr>");
961 chunk_appendf(out, "<tr><th>- Queue time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_QTIME].u.u32));
962 chunk_appendf(out, "<tr><th>- Connect time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_CTIME].u.u32));
963 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
964 chunk_appendf(out, "<tr><th>- Response time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_RTIME].u.u32));
965 chunk_appendf(out, "<tr><th>- Total time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_TTIME].u.u32));
966
967 chunk_appendf(out,
968 "</table></div></u></td>"
969 /* sessions: lbtot, last */
970 "<td>%s</td><td>%s</td>",
971 U2H(stats[ST_F_LBTOT].u.u64),
972 human_time(stats[ST_F_LASTSESS].u.s32, 1));
973
974 chunk_appendf(out,
975 /* bytes : in, out */
976 "<td>%s</td><td>%s</td>"
977 /* denied: req, resp */
978 "<td></td><td>%s</td>"
979 /* errors : request, connect */
980 "<td></td><td>%s</td>"
981 /* errors : response */
982 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
983 /* warnings: retries, redispatches */
984 "<td>%lld</td><td>%lld</td>"
985 "",
986 U2H(stats[ST_F_BIN].u.u64), U2H(stats[ST_F_BOUT].u.u64),
987 U2H(stats[ST_F_DRESP].u.u64),
988 U2H(stats[ST_F_ECON].u.u64),
989 U2H(stats[ST_F_ERESP].u.u64),
990 (long long)stats[ST_F_CLI_ABRT].u.u64,
991 (long long)stats[ST_F_SRV_ABRT].u.u64,
992 (long long)stats[ST_F_WRETR].u.u64,
993 (long long)stats[ST_F_WREDIS].u.u64);
994
995 /* status, last change */
996 chunk_appendf(out, "<td class=ac>");
997
998 /* FIXME!!!!
999 * LASTCHG should contain the last change for *this* server and must be computed
1000 * properly above, as was done below, ie: this server if maint, otherwise ref server
1001 * if tracking. Note that ref is either local or remote depending on tracking.
1002 */
1003
1004
1005 if (memcmp(field_str(stats, ST_F_STATUS), "MAINT", 5) == 0) {
1006 chunk_appendf(out, "%s MAINT", human_time(stats[ST_F_LASTCHG].u.u32, 1));
1007 }
1008 else if (memcmp(field_str(stats, ST_F_STATUS), "no check", 5) == 0) {
1009 chunk_strcat(out, "<i>no check</i>");
1010 }
1011 else {
1012 chunk_appendf(out, "%s %s", human_time(stats[ST_F_LASTCHG].u.u32, 1), field_str(stats, ST_F_STATUS));
1013 if (memcmp(field_str(stats, ST_F_STATUS), "DOWN", 4) == 0) {
1014 if (stats[ST_F_CHECK_HEALTH].u.u32)
1015 chunk_strcat(out, " &uarr;");
1016 }
1017 else if (stats[ST_F_CHECK_HEALTH].u.u32 < stats[ST_F_CHECK_RISE].u.u32 + stats[ST_F_CHECK_FALL].u.u32 - 1)
1018 chunk_strcat(out, " &darr;");
1019 }
1020
1021 if (memcmp(field_str(stats, ST_F_STATUS), "DOWN", 4) == 0 &&
1022 stats[ST_F_AGENT_STATUS].type && !stats[ST_F_AGENT_HEALTH].u.u32) {
1023 chunk_appendf(out,
1024 "</td><td class=ac><u> %s",
1025 field_str(stats, ST_F_AGENT_STATUS));
1026
1027 if (stats[ST_F_AGENT_CODE].type)
1028 chunk_appendf(out, "/%d", stats[ST_F_AGENT_CODE].u.u32);
1029
1030 if (stats[ST_F_AGENT_DURATION].type)
1031 chunk_appendf(out, " in %lums", (long)stats[ST_F_AGENT_DURATION].u.u64);
1032
1033 chunk_appendf(out, "<div class=tips>%s", field_str(stats, ST_F_AGENT_DESC));
1034
1035 if (*field_str(stats, ST_F_LAST_AGT)) {
1036 chunk_appendf(out, ": ");
1037 chunk_initstr(&src, field_str(stats, ST_F_LAST_AGT));
1038 chunk_htmlencode(out, &src);
1039 }
1040 chunk_appendf(out, "</div></u>");
1041 }
1042 else if (stats[ST_F_CHECK_STATUS].type) {
1043 chunk_appendf(out,
1044 "</td><td class=ac><u> %s",
1045 field_str(stats, ST_F_CHECK_STATUS));
1046
1047 if (stats[ST_F_CHECK_CODE].type)
1048 chunk_appendf(out, "/%d", stats[ST_F_CHECK_CODE].u.u32);
1049
1050 if (stats[ST_F_CHECK_DURATION].type)
1051 chunk_appendf(out, " in %lums", (long)stats[ST_F_CHECK_DURATION].u.u64);
1052
1053 chunk_appendf(out, "<div class=tips>%s", field_str(stats, ST_F_CHECK_DESC));
1054
1055 if (*field_str(stats, ST_F_LAST_CHK)) {
1056 chunk_appendf(out, ": ");
1057 chunk_initstr(&src, field_str(stats, ST_F_LAST_CHK));
1058 chunk_htmlencode(out, &src);
1059 }
1060 chunk_appendf(out, "</div></u>");
1061 }
1062 else
1063 chunk_appendf(out, "</td><td>");
1064
1065 chunk_appendf(out,
1066 /* weight */
1067 "</td><td class=ac>%d</td>"
1068 /* act, bck */
1069 "<td class=ac>%s</td><td class=ac>%s</td>"
1070 "",
1071 stats[ST_F_WEIGHT].u.u32,
1072 stats[ST_F_BCK].u.u32 ? "-" : "Y",
1073 stats[ST_F_BCK].u.u32 ? "Y" : "-");
1074
1075 /* check failures: unique, fatal, down time */
1076 if (strcmp(field_str(stats, ST_F_STATUS), "MAINT (resolution)") == 0) {
1077 chunk_appendf(out, "<td class=ac colspan=3>resolution</td>");
1078 }
1079 else if (stats[ST_F_CHKFAIL].type) {
1080 chunk_appendf(out, "<td><u>%lld", (long long)stats[ST_F_CHKFAIL].u.u64);
1081
1082 if (stats[ST_F_HANAFAIL].type)
1083 chunk_appendf(out, "/%lld", (long long)stats[ST_F_HANAFAIL].u.u64);
1084
1085 chunk_appendf(out,
1086 "<div class=tips>Failed Health Checks%s</div></u></td>"
1087 "<td>%lld</td><td>%s</td>"
1088 "",
1089 stats[ST_F_HANAFAIL].type ? "/Health Analyses" : "",
1090 (long long)stats[ST_F_CHKDOWN].u.u64, human_time(stats[ST_F_DOWNTIME].u.u32, 1));
1091 }
1092 else if (strcmp(field_str(stats, ST_F_STATUS), "MAINT") != 0 && field_format(stats, ST_F_TRACKED) == FF_STR) {
1093 /* tracking a server (hence inherited maint would appear as "MAINT (via...)" */
1094 chunk_appendf(out,
1095 "<td class=ac colspan=3><a class=lfsb href=\"#%s\">via %s</a></td>",
1096 field_str(stats, ST_F_TRACKED), field_str(stats, ST_F_TRACKED));
1097 }
1098 else
1099 chunk_appendf(out, "<td colspan=3></td>");
1100
1101 /* throttle */
1102 if (stats[ST_F_THROTTLE].type)
1103 chunk_appendf(out, "<td class=ac>%d %%</td></tr>\n", stats[ST_F_THROTTLE].u.u32);
1104 else
1105 chunk_appendf(out, "<td class=ac>-</td></tr>\n");
1106 }
1107 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_BE) {
1108 chunk_appendf(out, "<tr class=\"backend\">");
1109 if (flags & ST_SHOWADMIN) {
1110 /* Column sub-heading for Enable or Disable server */
1111 chunk_appendf(out, "<td></td>");
1112 }
1113 chunk_appendf(out,
1114 "<td class=ac>"
1115 /* name */
1116 "%s<a name=\"%s/Backend\"></a>"
1117 "<a class=lfsb href=\"#%s/Backend\">Backend</a>"
1118 "",
1119 (flags & ST_SHLGNDS)?"<u>":"",
1120 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_PXNAME));
1121
1122 if (flags & ST_SHLGNDS) {
1123 /* balancing */
1124 chunk_appendf(out, "<div class=tips>balancing: %s",
1125 field_str(stats, ST_F_ALGO));
1126
1127 /* cookie */
1128 if (stats[ST_F_COOKIE].type) {
1129 chunk_appendf(out, ", cookie: '");
1130 chunk_initstr(&src, field_str(stats, ST_F_COOKIE));
1131 chunk_htmlencode(out, &src);
1132 chunk_appendf(out, "'");
1133 }
1134 chunk_appendf(out, "</div>");
1135 }
1136
1137 chunk_appendf(out,
1138 "%s</td>"
1139 /* queue : current, max */
1140 "<td>%s</td><td>%s</td><td></td>"
1141 /* sessions rate : current, max, limit */
1142 "<td>%s</td><td>%s</td><td></td>"
1143 "",
1144 (flags & ST_SHLGNDS)?"</u>":"",
1145 U2H(stats[ST_F_QCUR].u.u32), U2H(stats[ST_F_QMAX].u.u32),
1146 U2H(stats[ST_F_RATE].u.u32), U2H(stats[ST_F_RATE_MAX].u.u32));
1147
1148 chunk_appendf(out,
1149 /* sessions: current, max, limit, total */
1150 "<td>%s</td><td>%s</td><td>%s</td>"
1151 "<td><u>%s<div class=tips><table class=det>"
1152 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
1153 "",
Willy Tarreau8e0f1752016-12-12 15:07:29 +01001154 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 +01001155 U2H(stats[ST_F_STOT].u.u64),
1156 U2H(stats[ST_F_STOT].u.u64));
1157
1158 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
1159 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
1160 chunk_appendf(out,
1161 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
1162 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
1163 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
1164 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
1165 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
1166 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
1167 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
1168 "<tr><th>- other responses:</th><td>%s</td></tr>"
Willy Tarreau1b0f85e2018-05-28 15:12:40 +02001169 "<tr><th>Failed hdr rewrites:</th><td>%s</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001170 "<tr><th colspan=3>Avg over last 1024 success. conn.</th></tr>"
1171 "",
1172 U2H(stats[ST_F_REQ_TOT].u.u64),
1173 U2H(stats[ST_F_HRSP_1XX].u.u64),
1174 U2H(stats[ST_F_HRSP_2XX].u.u64),
1175 U2H(stats[ST_F_COMP_RSP].u.u64),
1176 stats[ST_F_HRSP_2XX].u.u64 ?
1177 (int)(100 * stats[ST_F_COMP_RSP].u.u64 / stats[ST_F_HRSP_2XX].u.u64) : 0,
1178 U2H(stats[ST_F_HRSP_3XX].u.u64),
1179 U2H(stats[ST_F_HRSP_4XX].u.u64),
1180 U2H(stats[ST_F_HRSP_5XX].u.u64),
Willy Tarreau1b0f85e2018-05-28 15:12:40 +02001181 U2H(stats[ST_F_WREW].u.u64),
Willy Tarreauae9bea02016-11-25 14:44:52 +01001182 U2H(stats[ST_F_HRSP_OTHER].u.u64));
William Lallemand74c24fb2016-11-21 17:18:36 +01001183 }
1184
1185 chunk_appendf(out, "<tr><th>- Queue time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_QTIME].u.u32));
1186 chunk_appendf(out, "<tr><th>- Connect time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_CTIME].u.u32));
1187 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
1188 chunk_appendf(out, "<tr><th>- Response time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_RTIME].u.u32));
1189 chunk_appendf(out, "<tr><th>- Total time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_TTIME].u.u32));
1190
1191 chunk_appendf(out,
1192 "</table></div></u></td>"
1193 /* sessions: lbtot, last */
1194 "<td>%s</td><td>%s</td>"
1195 /* bytes: in */
1196 "<td>%s</td>"
1197 "",
1198 U2H(stats[ST_F_LBTOT].u.u64),
1199 human_time(stats[ST_F_LASTSESS].u.s32, 1),
1200 U2H(stats[ST_F_BIN].u.u64));
1201
1202 chunk_appendf(out,
1203 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
1204 "<td>%s%s<div class=tips><table class=det>"
1205 "<tr><th>Response bytes in:</th><td>%s</td></tr>"
1206 "<tr><th>Compression in:</th><td>%s</td></tr>"
1207 "<tr><th>Compression out:</th><td>%s</td><td>(%d%%)</td></tr>"
1208 "<tr><th>Compression bypass:</th><td>%s</td></tr>"
1209 "<tr><th>Total bytes saved:</th><td>%s</td><td>(%d%%)</td></tr>"
1210 "</table></div>%s</td>",
1211 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "<u>":"",
1212 U2H(stats[ST_F_BOUT].u.u64),
1213 U2H(stats[ST_F_BOUT].u.u64),
1214 U2H(stats[ST_F_COMP_IN].u.u64),
1215 U2H(stats[ST_F_COMP_OUT].u.u64),
1216 stats[ST_F_COMP_IN].u.u64 ? (int)(stats[ST_F_COMP_OUT].u.u64 * 100 / stats[ST_F_COMP_IN].u.u64) : 0,
1217 U2H(stats[ST_F_COMP_BYP].u.u64),
1218 U2H(stats[ST_F_COMP_IN].u.u64 - stats[ST_F_COMP_OUT].u.u64),
1219 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,
1220 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "</u>":"");
1221
1222 chunk_appendf(out,
1223 /* denied: req, resp */
1224 "<td>%s</td><td>%s</td>"
1225 /* errors : request, connect */
1226 "<td></td><td>%s</td>"
1227 /* errors : response */
1228 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
1229 /* warnings: retries, redispatches */
1230 "<td>%lld</td><td>%lld</td>"
1231 /* backend status: reflect backend status (up/down): we display UP
1232 * if the backend has known working servers or if it has no server at
1233 * all (eg: for stats). Then we display the total weight, number of
1234 * active and backups. */
1235 "<td class=ac>%s %s</td><td class=ac>&nbsp;</td><td class=ac>%d</td>"
1236 "<td class=ac>%d</td><td class=ac>%d</td>"
1237 "",
1238 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
1239 U2H(stats[ST_F_ECON].u.u64),
1240 U2H(stats[ST_F_ERESP].u.u64),
1241 (long long)stats[ST_F_CLI_ABRT].u.u64,
1242 (long long)stats[ST_F_SRV_ABRT].u.u64,
1243 (long long)stats[ST_F_WRETR].u.u64, (long long)stats[ST_F_WREDIS].u.u64,
1244 human_time(stats[ST_F_LASTCHG].u.u32, 1),
1245 strcmp(field_str(stats, ST_F_STATUS), "DOWN") ? field_str(stats, ST_F_STATUS) : "<font color=\"red\"><b>DOWN</b></font>",
1246 stats[ST_F_WEIGHT].u.u32,
1247 stats[ST_F_ACT].u.u32, stats[ST_F_BCK].u.u32);
1248
1249 chunk_appendf(out,
1250 /* rest of backend: nothing, down transitions, total downtime, throttle */
1251 "<td class=ac>&nbsp;</td><td>%d</td>"
1252 "<td>%s</td>"
1253 "<td></td>"
1254 "</tr>",
1255 stats[ST_F_CHKDOWN].u.u32,
1256 stats[ST_F_DOWNTIME].type ? human_time(stats[ST_F_DOWNTIME].u.u32, 1) : "&nbsp;");
1257 }
1258 return 1;
1259}
1260
1261int stats_dump_one_line(const struct field *stats, unsigned int flags, struct proxy *px, struct appctx *appctx)
1262{
Simon Horman05ee2132017-01-04 09:37:25 +01001263 int ret;
1264
William Lallemand74c24fb2016-11-21 17:18:36 +01001265 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN))
1266 flags |= ST_SHOWADMIN;
1267
1268 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
Simon Horman05ee2132017-01-04 09:37:25 +01001269 ret = stats_dump_fields_html(&trash, stats, flags);
William Lallemand74c24fb2016-11-21 17:18:36 +01001270 else if (appctx->ctx.stats.flags & STAT_FMT_TYPED)
Simon Horman05ee2132017-01-04 09:37:25 +01001271 ret = stats_dump_fields_typed(&trash, stats);
1272 else if (appctx->ctx.stats.flags & STAT_FMT_JSON)
1273 ret = stats_dump_fields_json(&trash, stats,
1274 !(appctx->ctx.stats.flags &
1275 STAT_STARTED));
William Lallemand74c24fb2016-11-21 17:18:36 +01001276 else
Simon Horman05ee2132017-01-04 09:37:25 +01001277 ret = stats_dump_fields_csv(&trash, stats);
1278
1279 if (ret)
1280 appctx->ctx.stats.flags |= STAT_STARTED;
1281
1282 return ret;
William Lallemand74c24fb2016-11-21 17:18:36 +01001283}
1284
1285/* Fill <stats> with the frontend statistics. <stats> is
1286 * preallocated array of length <len>. The length of the array
1287 * must be at least ST_F_TOTAL_FIELDS. If this length is less then
1288 * this value, the function returns 0, otherwise, it returns 1.
1289 */
1290int stats_fill_fe_stats(struct proxy *px, struct field *stats, int len)
1291{
1292 if (len < ST_F_TOTAL_FIELDS)
1293 return 0;
1294
1295 memset(stats, 0, sizeof(*stats) * len);
1296
1297 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1298 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, "FRONTEND");
1299 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1300 stats[ST_F_SCUR] = mkf_u32(0, px->feconn);
1301 stats[ST_F_SMAX] = mkf_u32(FN_MAX, px->fe_counters.conn_max);
1302 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, px->maxconn);
1303 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, px->fe_counters.cum_sess);
1304 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, px->fe_counters.bytes_in);
1305 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, px->fe_counters.bytes_out);
1306 stats[ST_F_DREQ] = mkf_u64(FN_COUNTER, px->fe_counters.denied_req);
1307 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, px->fe_counters.denied_resp);
1308 stats[ST_F_EREQ] = mkf_u64(FN_COUNTER, px->fe_counters.failed_req);
1309 stats[ST_F_DCON] = mkf_u64(FN_COUNTER, px->fe_counters.denied_conn);
1310 stats[ST_F_DSES] = mkf_u64(FN_COUNTER, px->fe_counters.denied_sess);
1311 stats[ST_F_STATUS] = mkf_str(FO_STATUS, px->state == PR_STREADY ? "OPEN" : px->state == PR_STFULL ? "FULL" : "STOP");
1312 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
1313 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1314 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, 0);
1315 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_FE);
1316 stats[ST_F_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_sess_per_sec));
1317 stats[ST_F_RATE_LIM] = mkf_u32(FO_CONFIG|FN_LIMIT, px->fe_sps_lim);
1318 stats[ST_F_RATE_MAX] = mkf_u32(FN_MAX, px->fe_counters.sps_max);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001319 stats[ST_F_WREW] = mkf_u64(FN_COUNTER, px->fe_counters.failed_rewrites);
William Lallemand74c24fb2016-11-21 17:18:36 +01001320
1321 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
1322 if (px->mode == PR_MODE_HTTP) {
1323 stats[ST_F_HRSP_1XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[1]);
1324 stats[ST_F_HRSP_2XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[2]);
1325 stats[ST_F_HRSP_3XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[3]);
1326 stats[ST_F_HRSP_4XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[4]);
1327 stats[ST_F_HRSP_5XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[5]);
1328 stats[ST_F_HRSP_OTHER] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[0]);
1329 stats[ST_F_INTERCEPTED] = mkf_u64(FN_COUNTER, px->fe_counters.intercepted_req);
1330 }
1331
1332 /* requests : req_rate, req_rate_max, req_tot, */
1333 stats[ST_F_REQ_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_req_per_sec));
1334 stats[ST_F_REQ_RATE_MAX] = mkf_u32(FN_MAX, px->fe_counters.p.http.rps_max);
1335 stats[ST_F_REQ_TOT] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cum_req);
1336
1337 /* compression: in, out, bypassed, responses */
1338 stats[ST_F_COMP_IN] = mkf_u64(FN_COUNTER, px->fe_counters.comp_in);
1339 stats[ST_F_COMP_OUT] = mkf_u64(FN_COUNTER, px->fe_counters.comp_out);
1340 stats[ST_F_COMP_BYP] = mkf_u64(FN_COUNTER, px->fe_counters.comp_byp);
1341 stats[ST_F_COMP_RSP] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.comp_rsp);
1342
1343 /* connections : conn_rate, conn_rate_max, conn_tot, conn_max */
1344 stats[ST_F_CONN_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_conn_per_sec));
1345 stats[ST_F_CONN_RATE_MAX] = mkf_u32(FN_MAX, px->fe_counters.cps_max);
1346 stats[ST_F_CONN_TOT] = mkf_u64(FN_COUNTER, px->fe_counters.cum_conn);
1347
1348 return 1;
1349}
1350
1351/* Dumps a frontend's line to the trash for the current proxy <px> and uses
1352 * the state from stream interface <si>. The caller is responsible for clearing
1353 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
1354 */
1355static int stats_dump_fe_stats(struct stream_interface *si, struct proxy *px)
1356{
1357 struct appctx *appctx = __objt_appctx(si->end);
1358
1359 if (!(px->cap & PR_CAP_FE))
1360 return 0;
1361
1362 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_FE)))
1363 return 0;
1364
1365 if (!stats_fill_fe_stats(px, stats, ST_F_TOTAL_FIELDS))
1366 return 0;
1367
1368 return stats_dump_one_line(stats, 0, px, appctx);
1369}
1370
1371/* Fill <stats> with the listener statistics. <stats> is
1372 * preallocated array of length <len>. The length of the array
1373 * must be at least ST_F_TOTAL_FIELDS. If this length is less
1374 * then this value, the function returns 0, otherwise, it
1375 * returns 1. <flags> can take the value ST_SHLGNDS.
1376 */
1377int stats_fill_li_stats(struct proxy *px, struct listener *l, int flags,
1378 struct field *stats, int len)
1379{
Willy Tarreau83061a82018-07-13 11:56:34 +02001380 struct buffer *out = get_trash_chunk();
William Lallemand74c24fb2016-11-21 17:18:36 +01001381
1382 if (len < ST_F_TOTAL_FIELDS)
1383 return 0;
1384
1385 if (!l->counters)
1386 return 0;
1387
1388 chunk_reset(out);
1389 memset(stats, 0, sizeof(*stats) * len);
1390
1391 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1392 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, l->name);
1393 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1394 stats[ST_F_SCUR] = mkf_u32(0, l->nbconn);
1395 stats[ST_F_SMAX] = mkf_u32(FN_MAX, l->counters->conn_max);
1396 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, l->maxconn);
1397 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, l->counters->cum_conn);
1398 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, l->counters->bytes_in);
1399 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, l->counters->bytes_out);
1400 stats[ST_F_DREQ] = mkf_u64(FN_COUNTER, l->counters->denied_req);
1401 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, l->counters->denied_resp);
1402 stats[ST_F_EREQ] = mkf_u64(FN_COUNTER, l->counters->failed_req);
1403 stats[ST_F_DCON] = mkf_u64(FN_COUNTER, l->counters->denied_conn);
1404 stats[ST_F_DSES] = mkf_u64(FN_COUNTER, l->counters->denied_sess);
1405 stats[ST_F_STATUS] = mkf_str(FO_STATUS, (l->nbconn < l->maxconn) ? (l->state == LI_LIMITED) ? "WAITING" : "OPEN" : "FULL");
1406 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
1407 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1408 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, l->luid);
1409 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_SO);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001410 stats[ST_F_WREW] = mkf_u64(FN_COUNTER, l->counters->failed_rewrites);
William Lallemand74c24fb2016-11-21 17:18:36 +01001411
1412 if (flags & ST_SHLGNDS) {
1413 char str[INET6_ADDRSTRLEN];
1414 int port;
1415
1416 port = get_host_port(&l->addr);
1417 switch (addr_to_str(&l->addr, str, sizeof(str))) {
1418 case AF_INET:
1419 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1420 chunk_appendf(out, "%s:%d", str, port);
1421 break;
1422 case AF_INET6:
1423 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1424 chunk_appendf(out, "[%s]:%d", str, port);
1425 break;
1426 case AF_UNIX:
1427 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, "unix");
1428 break;
1429 case -1:
1430 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1431 chunk_strcat(out, strerror(errno));
1432 break;
1433 default: /* address family not supported */
1434 break;
1435 }
1436 }
1437
1438 return 1;
1439}
1440
1441/* Dumps a line for listener <l> and proxy <px> to the trash and uses the state
1442 * from stream interface <si>, and stats flags <flags>. The caller is responsible
1443 * for clearing the trash if needed. Returns non-zero if it emits anything, zero
1444 * otherwise.
1445 */
1446static int stats_dump_li_stats(struct stream_interface *si, struct proxy *px, struct listener *l, int flags)
1447{
1448 struct appctx *appctx = __objt_appctx(si->end);
1449
1450 if (!stats_fill_li_stats(px, l, flags, stats, ST_F_TOTAL_FIELDS))
1451 return 0;
1452
1453 return stats_dump_one_line(stats, flags, px, appctx);
1454}
1455
1456enum srv_stats_state {
1457 SRV_STATS_STATE_DOWN = 0,
1458 SRV_STATS_STATE_DOWN_AGENT,
1459 SRV_STATS_STATE_GOING_UP,
1460 SRV_STATS_STATE_UP_GOING_DOWN,
1461 SRV_STATS_STATE_UP,
1462 SRV_STATS_STATE_NOLB_GOING_DOWN,
1463 SRV_STATS_STATE_NOLB,
1464 SRV_STATS_STATE_DRAIN_GOING_DOWN,
1465 SRV_STATS_STATE_DRAIN,
1466 SRV_STATS_STATE_DRAIN_AGENT,
1467 SRV_STATS_STATE_NO_CHECK,
1468
1469 SRV_STATS_STATE_COUNT, /* Must be last */
1470};
1471
1472static const char *srv_hlt_st[SRV_STATS_STATE_COUNT] = {
1473 [SRV_STATS_STATE_DOWN] = "DOWN",
1474 [SRV_STATS_STATE_DOWN_AGENT] = "DOWN (agent)",
1475 [SRV_STATS_STATE_GOING_UP] = "DOWN %d/%d",
1476 [SRV_STATS_STATE_UP_GOING_DOWN] = "UP %d/%d",
1477 [SRV_STATS_STATE_UP] = "UP",
1478 [SRV_STATS_STATE_NOLB_GOING_DOWN] = "NOLB %d/%d",
1479 [SRV_STATS_STATE_NOLB] = "NOLB",
1480 [SRV_STATS_STATE_DRAIN_GOING_DOWN] = "DRAIN %d/%d",
1481 [SRV_STATS_STATE_DRAIN] = "DRAIN",
1482 [SRV_STATS_STATE_DRAIN_AGENT] = "DRAIN (agent)",
1483 [SRV_STATS_STATE_NO_CHECK] = "no check"
1484};
1485
1486/* Fill <stats> with the server statistics. <stats> is
1487 * preallocated array of length <len>. The length of the array
1488 * must be at least ST_F_TOTAL_FIELDS. If this length is less
1489 * then this value, the function returns 0, otherwise, it
1490 * returns 1. <flags> can take the value ST_SHLGNDS.
1491 */
1492int stats_fill_sv_stats(struct proxy *px, struct server *sv, int flags,
1493 struct field *stats, int len)
1494{
1495 struct server *via, *ref;
1496 char str[INET6_ADDRSTRLEN];
Willy Tarreau83061a82018-07-13 11:56:34 +02001497 struct buffer *out = get_trash_chunk();
William Lallemand74c24fb2016-11-21 17:18:36 +01001498 enum srv_stats_state state;
1499 char *fld_status;
1500
1501 if (len < ST_F_TOTAL_FIELDS)
1502 return 0;
1503
1504 memset(stats, 0, sizeof(*stats) * len);
1505
1506 /* we have "via" which is the tracked server as described in the configuration,
1507 * and "ref" which is the checked server and the end of the chain.
1508 */
1509 via = sv->track ? sv->track : sv;
1510 ref = via;
1511 while (ref->track)
1512 ref = ref->track;
1513
Emeric Brun52a91d32017-08-31 14:41:55 +02001514 if (sv->cur_state == SRV_ST_RUNNING || sv->cur_state == SRV_ST_STARTING) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001515 if ((ref->check.state & CHK_ST_ENABLED) &&
1516 (ref->check.health < ref->check.rise + ref->check.fall - 1)) {
1517 state = SRV_STATS_STATE_UP_GOING_DOWN;
1518 } else {
1519 state = SRV_STATS_STATE_UP;
1520 }
1521
Emeric Brun52a91d32017-08-31 14:41:55 +02001522 if (sv->cur_admin & SRV_ADMF_DRAIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001523 if (ref->agent.state & CHK_ST_ENABLED)
1524 state = SRV_STATS_STATE_DRAIN_AGENT;
1525 else if (state == SRV_STATS_STATE_UP_GOING_DOWN)
1526 state = SRV_STATS_STATE_DRAIN_GOING_DOWN;
1527 else
1528 state = SRV_STATS_STATE_DRAIN;
1529 }
1530
1531 if (state == SRV_STATS_STATE_UP && !(ref->check.state & CHK_ST_ENABLED)) {
1532 state = SRV_STATS_STATE_NO_CHECK;
1533 }
1534 }
Emeric Brun52a91d32017-08-31 14:41:55 +02001535 else if (sv->cur_state == SRV_ST_STOPPING) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001536 if ((!(sv->check.state & CHK_ST_ENABLED) && !sv->track) ||
1537 (ref->check.health == ref->check.rise + ref->check.fall - 1)) {
1538 state = SRV_STATS_STATE_NOLB;
1539 } else {
1540 state = SRV_STATS_STATE_NOLB_GOING_DOWN;
1541 }
1542 }
1543 else { /* stopped */
1544 if ((ref->agent.state & CHK_ST_ENABLED) && !ref->agent.health) {
1545 state = SRV_STATS_STATE_DOWN_AGENT;
1546 } else if ((ref->check.state & CHK_ST_ENABLED) && !ref->check.health) {
1547 state = SRV_STATS_STATE_DOWN; /* DOWN */
1548 } else if ((ref->agent.state & CHK_ST_ENABLED) || (ref->check.state & CHK_ST_ENABLED)) {
1549 state = SRV_STATS_STATE_GOING_UP;
1550 } else {
1551 state = SRV_STATS_STATE_DOWN; /* DOWN, unchecked */
1552 }
1553 }
1554
1555 chunk_reset(out);
1556
1557 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1558 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, sv->id);
1559 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1560 stats[ST_F_QCUR] = mkf_u32(0, sv->nbpend);
1561 stats[ST_F_QMAX] = mkf_u32(FN_MAX, sv->counters.nbpend_max);
1562 stats[ST_F_SCUR] = mkf_u32(0, sv->cur_sess);
1563 stats[ST_F_SMAX] = mkf_u32(FN_MAX, sv->counters.cur_sess_max);
1564
1565 if (sv->maxconn)
1566 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, sv->maxconn);
1567
1568 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, sv->counters.cum_sess);
1569 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, sv->counters.bytes_in);
1570 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, sv->counters.bytes_out);
1571 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, sv->counters.failed_secu);
1572 stats[ST_F_ECON] = mkf_u64(FN_COUNTER, sv->counters.failed_conns);
1573 stats[ST_F_ERESP] = mkf_u64(FN_COUNTER, sv->counters.failed_resp);
1574 stats[ST_F_WRETR] = mkf_u64(FN_COUNTER, sv->counters.retries);
1575 stats[ST_F_WREDIS] = mkf_u64(FN_COUNTER, sv->counters.redispatches);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001576 stats[ST_F_WREW] = mkf_u64(FN_COUNTER, sv->counters.failed_rewrites);
William Lallemand74c24fb2016-11-21 17:18:36 +01001577
1578 /* status */
1579 fld_status = chunk_newstr(out);
Emeric Brun52a91d32017-08-31 14:41:55 +02001580 if (sv->cur_admin & SRV_ADMF_RMAINT)
William Lallemand74c24fb2016-11-21 17:18:36 +01001581 chunk_appendf(out, "MAINT (resolution)");
Emeric Brun52a91d32017-08-31 14:41:55 +02001582 else if (sv->cur_admin & SRV_ADMF_IMAINT)
William Lallemand74c24fb2016-11-21 17:18:36 +01001583 chunk_appendf(out, "MAINT (via %s/%s)", via->proxy->id, via->id);
Emeric Brun52a91d32017-08-31 14:41:55 +02001584 else if (sv->cur_admin & SRV_ADMF_MAINT)
William Lallemand74c24fb2016-11-21 17:18:36 +01001585 chunk_appendf(out, "MAINT");
1586 else
1587 chunk_appendf(out,
1588 srv_hlt_st[state],
Emeric Brun52a91d32017-08-31 14:41:55 +02001589 (ref->cur_state != SRV_ST_STOPPED) ? (ref->check.health - ref->check.rise + 1) : (ref->check.health),
1590 (ref->cur_state != SRV_ST_STOPPED) ? (ref->check.fall) : (ref->check.rise));
William Lallemand74c24fb2016-11-21 17:18:36 +01001591
1592 stats[ST_F_STATUS] = mkf_str(FO_STATUS, fld_status);
1593 stats[ST_F_LASTCHG] = mkf_u32(FN_AGE, now.tv_sec - sv->last_change);
Emeric Brun52a91d32017-08-31 14:41:55 +02001594 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 +01001595 stats[ST_F_ACT] = mkf_u32(FO_STATUS, (sv->flags & SRV_F_BACKUP) ? 0 : 1);
1596 stats[ST_F_BCK] = mkf_u32(FO_STATUS, (sv->flags & SRV_F_BACKUP) ? 1 : 0);
1597
1598 /* check failures: unique, fatal; last change, total downtime */
1599 if (sv->check.state & CHK_ST_ENABLED) {
1600 stats[ST_F_CHKFAIL] = mkf_u64(FN_COUNTER, sv->counters.failed_checks);
1601 stats[ST_F_CHKDOWN] = mkf_u64(FN_COUNTER, sv->counters.down_trans);
1602 stats[ST_F_DOWNTIME] = mkf_u32(FN_COUNTER, srv_downtime(sv));
1603 }
1604
1605 if (sv->maxqueue)
1606 stats[ST_F_QLIMIT] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->maxqueue);
1607
1608 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
1609 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1610 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, sv->puid);
1611
Emeric Brun52a91d32017-08-31 14:41:55 +02001612 if (sv->cur_state == SRV_ST_STARTING && !server_is_draining(sv))
William Lallemand74c24fb2016-11-21 17:18:36 +01001613 stats[ST_F_THROTTLE] = mkf_u32(FN_AVG, server_throttle_rate(sv));
1614
1615 stats[ST_F_LBTOT] = mkf_u64(FN_COUNTER, sv->counters.cum_lbconn);
1616
1617 if (sv->track) {
1618 char *fld_track = chunk_newstr(out);
1619
1620 chunk_appendf(out, "%s/%s", sv->track->proxy->id, sv->track->id);
1621 stats[ST_F_TRACKED] = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, fld_track);
1622 }
1623
1624 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_SV);
1625 stats[ST_F_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&sv->sess_per_sec));
1626 stats[ST_F_RATE_MAX] = mkf_u32(FN_MAX, sv->counters.sps_max);
1627
1628 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) {
1629 const char *fld_chksts;
1630
1631 fld_chksts = chunk_newstr(out);
1632 chunk_strcat(out, "* "); // for check in progress
1633 chunk_strcat(out, get_check_status_info(sv->check.status));
1634 if (!(sv->check.state & CHK_ST_INPROGRESS))
1635 fld_chksts += 2; // skip "* "
1636 stats[ST_F_CHECK_STATUS] = mkf_str(FN_OUTPUT, fld_chksts);
1637
1638 if (sv->check.status >= HCHK_STATUS_L57DATA)
1639 stats[ST_F_CHECK_CODE] = mkf_u32(FN_OUTPUT, sv->check.code);
1640
1641 if (sv->check.status >= HCHK_STATUS_CHECKED)
1642 stats[ST_F_CHECK_DURATION] = mkf_u64(FN_DURATION, sv->check.duration);
1643
1644 stats[ST_F_CHECK_DESC] = mkf_str(FN_OUTPUT, get_check_status_description(sv->check.status));
1645 stats[ST_F_LAST_CHK] = mkf_str(FN_OUTPUT, sv->check.desc);
1646 stats[ST_F_CHECK_RISE] = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.rise);
1647 stats[ST_F_CHECK_FALL] = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.fall);
1648 stats[ST_F_CHECK_HEALTH] = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.health);
1649 }
1650
1651 if ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) {
1652 const char *fld_chksts;
1653
1654 fld_chksts = chunk_newstr(out);
1655 chunk_strcat(out, "* "); // for check in progress
1656 chunk_strcat(out, get_check_status_info(sv->agent.status));
1657 if (!(sv->agent.state & CHK_ST_INPROGRESS))
1658 fld_chksts += 2; // skip "* "
1659 stats[ST_F_AGENT_STATUS] = mkf_str(FN_OUTPUT, fld_chksts);
1660
1661 if (sv->agent.status >= HCHK_STATUS_L57DATA)
1662 stats[ST_F_AGENT_CODE] = mkf_u32(FN_OUTPUT, sv->agent.code);
1663
1664 if (sv->agent.status >= HCHK_STATUS_CHECKED)
1665 stats[ST_F_AGENT_DURATION] = mkf_u64(FN_DURATION, sv->agent.duration);
1666
1667 stats[ST_F_AGENT_DESC] = mkf_str(FN_OUTPUT, get_check_status_description(sv->agent.status));
1668 stats[ST_F_LAST_AGT] = mkf_str(FN_OUTPUT, sv->agent.desc);
1669 stats[ST_F_AGENT_RISE] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.rise);
1670 stats[ST_F_AGENT_FALL] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.fall);
1671 stats[ST_F_AGENT_HEALTH] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.health);
1672 }
1673
1674 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
1675 if (px->mode == PR_MODE_HTTP) {
1676 stats[ST_F_HRSP_1XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[1]);
1677 stats[ST_F_HRSP_2XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[2]);
1678 stats[ST_F_HRSP_3XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[3]);
1679 stats[ST_F_HRSP_4XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[4]);
1680 stats[ST_F_HRSP_5XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[5]);
1681 stats[ST_F_HRSP_OTHER] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[0]);
1682 }
1683
1684 if (ref->observe)
1685 stats[ST_F_HANAFAIL] = mkf_u64(FN_COUNTER, sv->counters.failed_hana);
1686
1687 stats[ST_F_CLI_ABRT] = mkf_u64(FN_COUNTER, sv->counters.cli_aborts);
1688 stats[ST_F_SRV_ABRT] = mkf_u64(FN_COUNTER, sv->counters.srv_aborts);
1689 stats[ST_F_LASTSESS] = mkf_s32(FN_AGE, srv_lastsession(sv));
1690
1691 stats[ST_F_QTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.q_time, TIME_STATS_SAMPLES));
1692 stats[ST_F_CTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.c_time, TIME_STATS_SAMPLES));
1693 stats[ST_F_RTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.d_time, TIME_STATS_SAMPLES));
1694 stats[ST_F_TTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.t_time, TIME_STATS_SAMPLES));
1695
1696 if (flags & ST_SHLGNDS) {
1697 switch (addr_to_str(&sv->addr, str, sizeof(str))) {
1698 case AF_INET:
1699 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
Willy Tarreau04276f32017-01-06 17:41:29 +01001700 chunk_appendf(out, "%s:%d", str, sv->svc_port);
William Lallemand74c24fb2016-11-21 17:18:36 +01001701 break;
1702 case AF_INET6:
1703 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
Willy Tarreau04276f32017-01-06 17:41:29 +01001704 chunk_appendf(out, "[%s]:%d", str, sv->svc_port);
William Lallemand74c24fb2016-11-21 17:18:36 +01001705 break;
1706 case AF_UNIX:
1707 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, "unix");
1708 break;
1709 case -1:
1710 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1711 chunk_strcat(out, strerror(errno));
1712 break;
1713 default: /* address family not supported */
1714 break;
1715 }
1716
1717 if (sv->cookie)
1718 stats[ST_F_COOKIE] = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, sv->cookie);
1719 }
1720
1721 return 1;
1722}
1723
1724/* Dumps a line for server <sv> and proxy <px> to the trash and uses the state
1725 * from stream interface <si>, stats flags <flags>, and server state <state>.
1726 * The caller is responsible for clearing the trash if needed. Returns non-zero
1727 * if it emits anything, zero otherwise.
1728 */
1729static int stats_dump_sv_stats(struct stream_interface *si, struct proxy *px, int flags, struct server *sv)
1730{
1731 struct appctx *appctx = __objt_appctx(si->end);
1732
1733 if (!stats_fill_sv_stats(px, sv, flags, stats, ST_F_TOTAL_FIELDS))
1734 return 0;
1735
1736 return stats_dump_one_line(stats, flags, px, appctx);
1737}
1738
1739/* Fill <stats> with the backend statistics. <stats> is
1740 * preallocated array of length <len>. The length of the array
1741 * must be at least ST_F_TOTAL_FIELDS. If this length is less
1742 * then this value, the function returns 0, otherwise, it
1743 * returns 1. <flags> can take the value ST_SHLGNDS.
1744 */
1745int stats_fill_be_stats(struct proxy *px, int flags, struct field *stats, int len)
1746{
1747 if (len < ST_F_TOTAL_FIELDS)
1748 return 0;
1749
1750 memset(stats, 0, sizeof(*stats) * len);
1751
1752 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1753 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, "BACKEND");
1754 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1755 stats[ST_F_QCUR] = mkf_u32(0, px->nbpend);
1756 stats[ST_F_QMAX] = mkf_u32(FN_MAX, px->be_counters.nbpend_max);
Thierry FOURNIER0ff98a42016-12-19 16:50:42 +01001757 stats[ST_F_SCUR] = mkf_u32(0, px->beconn);
William Lallemand74c24fb2016-11-21 17:18:36 +01001758 stats[ST_F_SMAX] = mkf_u32(FN_MAX, px->be_counters.conn_max);
1759 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, px->fullconn);
1760 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, px->be_counters.cum_conn);
1761 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, px->be_counters.bytes_in);
1762 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, px->be_counters.bytes_out);
1763 stats[ST_F_DREQ] = mkf_u64(FN_COUNTER, px->be_counters.denied_req);
1764 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, px->be_counters.denied_resp);
1765 stats[ST_F_ECON] = mkf_u64(FN_COUNTER, px->be_counters.failed_conns);
1766 stats[ST_F_ERESP] = mkf_u64(FN_COUNTER, px->be_counters.failed_resp);
1767 stats[ST_F_WRETR] = mkf_u64(FN_COUNTER, px->be_counters.retries);
1768 stats[ST_F_WREDIS] = mkf_u64(FN_COUNTER, px->be_counters.redispatches);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001769 stats[ST_F_WREW] = mkf_u64(FN_COUNTER, px->be_counters.failed_rewrites);
William Lallemand74c24fb2016-11-21 17:18:36 +01001770 stats[ST_F_STATUS] = mkf_str(FO_STATUS, (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" : "DOWN");
1771 stats[ST_F_WEIGHT] = mkf_u32(FN_AVG, (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv);
1772 stats[ST_F_ACT] = mkf_u32(0, px->srv_act);
1773 stats[ST_F_BCK] = mkf_u32(0, px->srv_bck);
1774 stats[ST_F_CHKDOWN] = mkf_u64(FN_COUNTER, px->down_trans);
1775 stats[ST_F_LASTCHG] = mkf_u32(FN_AGE, now.tv_sec - px->last_change);
1776 if (px->srv)
1777 stats[ST_F_DOWNTIME] = mkf_u32(FN_COUNTER, be_downtime(px));
1778
1779 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
1780 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1781 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, 0);
1782 stats[ST_F_LBTOT] = mkf_u64(FN_COUNTER, px->be_counters.cum_lbconn);
1783 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_BE);
1784 stats[ST_F_RATE] = mkf_u32(0, read_freq_ctr(&px->be_sess_per_sec));
1785 stats[ST_F_RATE_MAX] = mkf_u32(0, px->be_counters.sps_max);
1786
1787 if (flags & ST_SHLGNDS) {
1788 if (px->cookie_name)
1789 stats[ST_F_COOKIE] = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, px->cookie_name);
1790 stats[ST_F_ALGO] = mkf_str(FO_CONFIG|FS_SERVICE, backend_lb_algo_str(px->lbprm.algo & BE_LB_ALGO));
1791 }
1792
1793 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
1794 if (px->mode == PR_MODE_HTTP) {
1795 stats[ST_F_REQ_TOT] = mkf_u64(FN_COUNTER, px->be_counters.p.http.cum_req);
1796 stats[ST_F_HRSP_1XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[1]);
1797 stats[ST_F_HRSP_2XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[2]);
1798 stats[ST_F_HRSP_3XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[3]);
1799 stats[ST_F_HRSP_4XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[4]);
1800 stats[ST_F_HRSP_5XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[5]);
1801 stats[ST_F_HRSP_OTHER] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[0]);
William Lallemand74c24fb2016-11-21 17:18:36 +01001802 }
1803
1804 stats[ST_F_CLI_ABRT] = mkf_u64(FN_COUNTER, px->be_counters.cli_aborts);
1805 stats[ST_F_SRV_ABRT] = mkf_u64(FN_COUNTER, px->be_counters.srv_aborts);
1806
1807 /* compression: in, out, bypassed, responses */
1808 stats[ST_F_COMP_IN] = mkf_u64(FN_COUNTER, px->be_counters.comp_in);
1809 stats[ST_F_COMP_OUT] = mkf_u64(FN_COUNTER, px->be_counters.comp_out);
1810 stats[ST_F_COMP_BYP] = mkf_u64(FN_COUNTER, px->be_counters.comp_byp);
1811 stats[ST_F_COMP_RSP] = mkf_u64(FN_COUNTER, px->be_counters.p.http.comp_rsp);
1812 stats[ST_F_LASTSESS] = mkf_s32(FN_AGE, be_lastsession(px));
1813
1814 stats[ST_F_QTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.q_time, TIME_STATS_SAMPLES));
1815 stats[ST_F_CTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.c_time, TIME_STATS_SAMPLES));
1816 stats[ST_F_RTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.d_time, TIME_STATS_SAMPLES));
1817 stats[ST_F_TTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.t_time, TIME_STATS_SAMPLES));
1818
1819 return 1;
1820}
1821
1822/* Dumps a line for backend <px> to the trash for and uses the state from stream
1823 * interface <si> and stats flags <flags>. The caller is responsible for clearing
1824 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
1825 */
1826static int stats_dump_be_stats(struct stream_interface *si, struct proxy *px, int flags)
1827{
1828 struct appctx *appctx = __objt_appctx(si->end);
1829
1830 if (!(px->cap & PR_CAP_BE))
1831 return 0;
1832
1833 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_BE)))
1834 return 0;
1835
1836 if (!stats_fill_be_stats(px, flags, stats, ST_F_TOTAL_FIELDS))
1837 return 0;
1838
1839 return stats_dump_one_line(stats, flags, px, appctx);
1840}
1841
1842/* Dumps the HTML table header for proxy <px> to the trash for and uses the state from
1843 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
1844 * for clearing the trash if needed.
1845 */
1846static void stats_dump_html_px_hdr(struct stream_interface *si, struct proxy *px, struct uri_auth *uri)
1847{
1848 struct appctx *appctx = __objt_appctx(si->end);
1849 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
1850
1851 if (px->cap & PR_CAP_BE && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
1852 /* A form to enable/disable this proxy servers */
1853
1854 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
1855 scope_txt[0] = 0;
1856 if (appctx->ctx.stats.scope_len) {
1857 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau89faf5d2018-06-07 18:16:48 +02001858 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 +01001859 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
1860 }
1861
1862 chunk_appendf(&trash,
1863 "<form method=\"post\">");
1864 }
1865
1866 /* print a new table */
1867 chunk_appendf(&trash,
1868 "<table class=\"tbl\" width=\"100%%\">\n"
1869 "<tr class=\"titre\">"
1870 "<th class=\"pxname\" width=\"10%%\">");
1871
1872 chunk_appendf(&trash,
1873 "<a name=\"%s\"></a>%s"
1874 "<a class=px href=\"#%s\">%s</a>",
1875 px->id,
1876 (uri->flags & ST_SHLGNDS) ? "<u>":"",
1877 px->id, px->id);
1878
1879 if (uri->flags & ST_SHLGNDS) {
1880 /* cap, mode, id */
1881 chunk_appendf(&trash, "<div class=tips>cap: %s, mode: %s, id: %d",
1882 proxy_cap_str(px->cap), proxy_mode_str(px->mode),
1883 px->uuid);
1884 chunk_appendf(&trash, "</div>");
1885 }
1886
1887 chunk_appendf(&trash,
1888 "%s</th>"
1889 "<th class=\"%s\" width=\"90%%\">%s</th>"
1890 "</tr>\n"
1891 "</table>\n"
1892 "<table class=\"tbl\" width=\"100%%\">\n"
1893 "<tr class=\"titre\">",
1894 (uri->flags & ST_SHLGNDS) ? "</u>":"",
1895 px->desc ? "desc" : "empty", px->desc ? px->desc : "");
1896
1897 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
1898 /* Column heading for Enable or Disable server */
David Harrigand3db35a2016-12-30 12:12:49 +00001899 chunk_appendf(&trash,
1900 "<th rowspan=2 width=1><input type=\"checkbox\" \
1901 onclick=\"for(c in document.getElementsByClassName('%s-checkbox')) \
1902 document.getElementsByClassName('%s-checkbox').item(c).checked = this.checked\"></th>",
1903 px->id,
1904 px->id);
William Lallemand74c24fb2016-11-21 17:18:36 +01001905 }
1906
1907 chunk_appendf(&trash,
1908 "<th rowspan=2></th>"
1909 "<th colspan=3>Queue</th>"
1910 "<th colspan=3>Session rate</th><th colspan=6>Sessions</th>"
1911 "<th colspan=2>Bytes</th><th colspan=2>Denied</th>"
1912 "<th colspan=3>Errors</th><th colspan=2>Warnings</th>"
1913 "<th colspan=9>Server</th>"
1914 "</tr>\n"
1915 "<tr class=\"titre\">"
1916 "<th>Cur</th><th>Max</th><th>Limit</th>"
1917 "<th>Cur</th><th>Max</th><th>Limit</th><th>Cur</th><th>Max</th>"
1918 "<th>Limit</th><th>Total</th><th>LbTot</th><th>Last</th><th>In</th><th>Out</th>"
1919 "<th>Req</th><th>Resp</th><th>Req</th><th>Conn</th>"
1920 "<th>Resp</th><th>Retr</th><th>Redis</th>"
1921 "<th>Status</th><th>LastChk</th><th>Wght</th><th>Act</th>"
1922 "<th>Bck</th><th>Chk</th><th>Dwn</th><th>Dwntme</th>"
1923 "<th>Thrtle</th>\n"
1924 "</tr>");
1925}
1926
1927/* Dumps the HTML table trailer for proxy <px> to the trash for and uses the state from
1928 * stream interface <si>. The caller is responsible for clearing the trash if needed.
1929 */
1930static void stats_dump_html_px_end(struct stream_interface *si, struct proxy *px)
1931{
1932 struct appctx *appctx = __objt_appctx(si->end);
1933 chunk_appendf(&trash, "</table>");
1934
1935 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
1936 /* close the form used to enable/disable this proxy servers */
1937 chunk_appendf(&trash,
1938 "Choose the action to perform on the checked servers : "
1939 "<select name=action>"
1940 "<option value=\"\"></option>"
1941 "<option value=\"ready\">Set state to READY</option>"
1942 "<option value=\"drain\">Set state to DRAIN</option>"
1943 "<option value=\"maint\">Set state to MAINT</option>"
1944 "<option value=\"dhlth\">Health: disable checks</option>"
1945 "<option value=\"ehlth\">Health: enable checks</option>"
1946 "<option value=\"hrunn\">Health: force UP</option>"
1947 "<option value=\"hnolb\">Health: force NOLB</option>"
1948 "<option value=\"hdown\">Health: force DOWN</option>"
1949 "<option value=\"dagent\">Agent: disable checks</option>"
1950 "<option value=\"eagent\">Agent: enable checks</option>"
1951 "<option value=\"arunn\">Agent: force UP</option>"
1952 "<option value=\"adown\">Agent: force DOWN</option>"
1953 "<option value=\"shutdown\">Kill Sessions</option>"
1954 "</select>"
1955 "<input type=\"hidden\" name=\"b\" value=\"#%d\">"
1956 "&nbsp;<input type=\"submit\" value=\"Apply\">"
1957 "</form>",
1958 px->uuid);
1959 }
1960
1961 chunk_appendf(&trash, "<p>\n");
1962}
1963
1964/*
1965 * Dumps statistics for a proxy. The output is sent to the stream interface's
1966 * input buffer. Returns 0 if it had to stop dumping data because of lack of
1967 * buffer space, or non-zero if everything completed. This function is used
1968 * both by the CLI and the HTTP entry points, and is able to dump the output
1969 * in HTML or CSV formats. If the later, <uri> must be NULL.
1970 */
1971int stats_dump_proxy_to_buffer(struct stream_interface *si, struct proxy *px, struct uri_auth *uri)
1972{
1973 struct appctx *appctx = __objt_appctx(si->end);
1974 struct stream *s = si_strm(si);
1975 struct channel *rep = si_ic(si);
1976 struct server *sv, *svs; /* server and server-state, server-state=server or server->track */
1977 struct listener *l;
1978 unsigned int flags;
1979
1980 if (uri)
1981 flags = uri->flags;
William Lallemand07a62f72017-05-24 00:57:40 +02001982 else if ((strm_li(s)->bind_conf->level & ACCESS_LVL_MASK) >= ACCESS_LVL_OPER)
William Lallemand74c24fb2016-11-21 17:18:36 +01001983 flags = ST_SHLGNDS | ST_SHNODE | ST_SHDESC;
1984 else
1985 flags = ST_SHNODE | ST_SHDESC;
1986
1987 chunk_reset(&trash);
1988
1989 switch (appctx->ctx.stats.px_st) {
1990 case STAT_PX_ST_INIT:
1991 /* we are on a new proxy */
1992 if (uri && uri->scope) {
1993 /* we have a limited scope, we have to check the proxy name */
1994 struct stat_scope *scope;
1995 int len;
1996
1997 len = strlen(px->id);
1998 scope = uri->scope;
1999
2000 while (scope) {
2001 /* match exact proxy name */
2002 if (scope->px_len == len && !memcmp(px->id, scope->px_id, len))
2003 break;
2004
2005 /* match '.' which means 'self' proxy */
2006 if (!strcmp(scope->px_id, ".") && px == s->be)
2007 break;
2008 scope = scope->next;
2009 }
2010
2011 /* proxy name not found : don't dump anything */
2012 if (scope == NULL)
2013 return 1;
2014 }
2015
2016 /* if the user has requested a limited output and the proxy
2017 * name does not match, skip it.
2018 */
2019 if (appctx->ctx.stats.scope_len &&
Willy Tarreau89faf5d2018-06-07 18:16:48 +02002020 strnistr(px->id, strlen(px->id), co_head(si_oc(si)) + appctx->ctx.stats.scope_str, appctx->ctx.stats.scope_len) == NULL)
William Lallemand74c24fb2016-11-21 17:18:36 +01002021 return 1;
2022
2023 if ((appctx->ctx.stats.flags & STAT_BOUND) &&
2024 (appctx->ctx.stats.iid != -1) &&
2025 (px->uuid != appctx->ctx.stats.iid))
2026 return 1;
2027
2028 appctx->ctx.stats.px_st = STAT_PX_ST_TH;
2029 /* fall through */
2030
2031 case STAT_PX_ST_TH:
2032 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
2033 stats_dump_html_px_hdr(si, px, uri);
Willy Tarreau06d80a92017-10-19 14:32:15 +02002034 if (ci_putchk(rep, &trash) == -1) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002035 si_applet_cant_put(si);
2036 return 0;
2037 }
2038 }
2039
2040 appctx->ctx.stats.px_st = STAT_PX_ST_FE;
2041 /* fall through */
2042
2043 case STAT_PX_ST_FE:
2044 /* print the frontend */
2045 if (stats_dump_fe_stats(si, px)) {
Willy Tarreau06d80a92017-10-19 14:32:15 +02002046 if (ci_putchk(rep, &trash) == -1) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002047 si_applet_cant_put(si);
2048 return 0;
2049 }
2050 }
2051
2052 appctx->ctx.stats.l = px->conf.listeners.n;
2053 appctx->ctx.stats.px_st = STAT_PX_ST_LI;
2054 /* fall through */
2055
2056 case STAT_PX_ST_LI:
2057 /* stats.l has been initialized above */
2058 for (; appctx->ctx.stats.l != &px->conf.listeners; appctx->ctx.stats.l = l->by_fe.n) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002059 if (buffer_almost_full(&rep->buf)) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002060 si_applet_cant_put(si);
2061 return 0;
2062 }
2063
2064 l = LIST_ELEM(appctx->ctx.stats.l, struct listener *, by_fe);
2065 if (!l->counters)
2066 continue;
2067
2068 if (appctx->ctx.stats.flags & STAT_BOUND) {
2069 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SO)))
2070 break;
2071
2072 if (appctx->ctx.stats.sid != -1 && l->luid != appctx->ctx.stats.sid)
2073 continue;
2074 }
2075
2076 /* print the frontend */
2077 if (stats_dump_li_stats(si, px, l, flags)) {
Willy Tarreau06d80a92017-10-19 14:32:15 +02002078 if (ci_putchk(rep, &trash) == -1) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002079 si_applet_cant_put(si);
2080 return 0;
2081 }
2082 }
2083 }
2084
2085 appctx->ctx.stats.sv = px->srv; /* may be NULL */
2086 appctx->ctx.stats.px_st = STAT_PX_ST_SV;
2087 /* fall through */
2088
2089 case STAT_PX_ST_SV:
2090 /* stats.sv has been initialized above */
2091 for (; appctx->ctx.stats.sv != NULL; appctx->ctx.stats.sv = sv->next) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002092 if (buffer_almost_full(&rep->buf)) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002093 si_applet_cant_put(si);
2094 return 0;
2095 }
2096
2097 sv = appctx->ctx.stats.sv;
2098
2099 if (appctx->ctx.stats.flags & STAT_BOUND) {
2100 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SV)))
2101 break;
2102
2103 if (appctx->ctx.stats.sid != -1 && sv->puid != appctx->ctx.stats.sid)
2104 continue;
2105 }
2106
2107 svs = sv;
2108 while (svs->track)
2109 svs = svs->track;
2110
2111 /* do not report servers which are DOWN and not changing state */
2112 if ((appctx->ctx.stats.flags & STAT_HIDE_DOWN) &&
Emeric Brun52a91d32017-08-31 14:41:55 +02002113 ((sv->cur_admin & SRV_ADMF_MAINT) || /* server is in maintenance */
2114 (sv->cur_state == SRV_ST_STOPPED && /* server is down */
William Lallemand74c24fb2016-11-21 17:18:36 +01002115 (!((svs->agent.state | svs->check.state) & CHK_ST_ENABLED) ||
2116 ((svs->agent.state & CHK_ST_ENABLED) && !svs->agent.health) ||
2117 ((svs->check.state & CHK_ST_ENABLED) && !svs->check.health))))) {
2118 continue;
2119 }
2120
2121 if (stats_dump_sv_stats(si, px, flags, sv)) {
Willy Tarreau06d80a92017-10-19 14:32:15 +02002122 if (ci_putchk(rep, &trash) == -1) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002123 si_applet_cant_put(si);
2124 return 0;
2125 }
2126 }
2127 } /* for sv */
2128
2129 appctx->ctx.stats.px_st = STAT_PX_ST_BE;
2130 /* fall through */
2131
2132 case STAT_PX_ST_BE:
2133 /* print the backend */
2134 if (stats_dump_be_stats(si, px, flags)) {
Willy Tarreau06d80a92017-10-19 14:32:15 +02002135 if (ci_putchk(rep, &trash) == -1) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002136 si_applet_cant_put(si);
2137 return 0;
2138 }
2139 }
2140
2141 appctx->ctx.stats.px_st = STAT_PX_ST_END;
2142 /* fall through */
2143
2144 case STAT_PX_ST_END:
2145 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
2146 stats_dump_html_px_end(si, px);
Willy Tarreau06d80a92017-10-19 14:32:15 +02002147 if (ci_putchk(rep, &trash) == -1) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002148 si_applet_cant_put(si);
2149 return 0;
2150 }
2151 }
2152
2153 appctx->ctx.stats.px_st = STAT_PX_ST_FIN;
2154 /* fall through */
2155
2156 case STAT_PX_ST_FIN:
2157 return 1;
2158
2159 default:
2160 /* unknown state, we should put an abort() here ! */
2161 return 1;
2162 }
2163}
2164
2165/* Dumps the HTTP stats head block to the trash for and uses the per-uri
2166 * parameters <uri>. The caller is responsible for clearing the trash if needed.
2167 */
2168static void stats_dump_html_head(struct uri_auth *uri)
2169{
2170 /* WARNING! This must fit in the first buffer !!! */
2171 chunk_appendf(&trash,
2172 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n"
2173 "\"http://www.w3.org/TR/html4/loose.dtd\">\n"
2174 "<html><head><title>Statistics Report for " PRODUCT_NAME "%s%s</title>\n"
2175 "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n"
2176 "<style type=\"text/css\"><!--\n"
2177 "body {"
2178 " font-family: arial, helvetica, sans-serif;"
2179 " font-size: 12px;"
2180 " font-weight: normal;"
2181 " color: black;"
2182 " background: white;"
2183 "}\n"
2184 "th,td {"
2185 " font-size: 10px;"
2186 "}\n"
2187 "h1 {"
2188 " font-size: x-large;"
2189 " margin-bottom: 0.5em;"
2190 "}\n"
2191 "h2 {"
2192 " font-family: helvetica, arial;"
2193 " font-size: x-large;"
2194 " font-weight: bold;"
2195 " font-style: italic;"
2196 " color: #6020a0;"
2197 " margin-top: 0em;"
2198 " margin-bottom: 0em;"
2199 "}\n"
2200 "h3 {"
2201 " font-family: helvetica, arial;"
2202 " font-size: 16px;"
2203 " font-weight: bold;"
2204 " color: #b00040;"
2205 " background: #e8e8d0;"
2206 " margin-top: 0em;"
2207 " margin-bottom: 0em;"
2208 "}\n"
2209 "li {"
2210 " margin-top: 0.25em;"
2211 " margin-right: 2em;"
2212 "}\n"
2213 ".hr {margin-top: 0.25em;"
2214 " border-color: black;"
2215 " border-bottom-style: solid;"
2216 "}\n"
2217 ".titre {background: #20D0D0;color: #000000; font-weight: bold; text-align: center;}\n"
2218 ".total {background: #20D0D0;color: #ffff80;}\n"
2219 ".frontend {background: #e8e8d0;}\n"
2220 ".socket {background: #d0d0d0;}\n"
2221 ".backend {background: #e8e8d0;}\n"
2222 ".active_down {background: #ff9090;}\n"
2223 ".active_going_up {background: #ffd020;}\n"
2224 ".active_going_down {background: #ffffa0;}\n"
2225 ".active_up {background: #c0ffc0;}\n"
2226 ".active_nolb {background: #20a0ff;}\n"
2227 ".active_draining {background: #20a0FF;}\n"
2228 ".active_no_check {background: #e0e0e0;}\n"
2229 ".backup_down {background: #ff9090;}\n"
2230 ".backup_going_up {background: #ff80ff;}\n"
2231 ".backup_going_down {background: #c060ff;}\n"
2232 ".backup_up {background: #b0d0ff;}\n"
2233 ".backup_nolb {background: #90b0e0;}\n"
2234 ".backup_draining {background: #cc9900;}\n"
2235 ".backup_no_check {background: #e0e0e0;}\n"
2236 ".maintain {background: #c07820;}\n"
2237 ".rls {letter-spacing: 0.2em; margin-right: 1px;}\n" /* right letter spacing (used for grouping digits) */
2238 "\n"
2239 "a.px:link {color: #ffff40; text-decoration: none;}"
2240 "a.px:visited {color: #ffff40; text-decoration: none;}"
2241 "a.px:hover {color: #ffffff; text-decoration: none;}"
2242 "a.lfsb:link {color: #000000; text-decoration: none;}"
2243 "a.lfsb:visited {color: #000000; text-decoration: none;}"
2244 "a.lfsb:hover {color: #505050; text-decoration: none;}"
2245 "\n"
2246 "table.tbl { border-collapse: collapse; border-style: none;}\n"
2247 "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"
2248 "table.tbl td.ac { text-align: center;}\n"
2249 "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}\n"
2250 "table.tbl th.pxname { background: #b00040; color: #ffff40; font-weight: bold; border-style: solid solid none solid; padding: 2px 3px; white-space: nowrap;}\n"
2251 "table.tbl th.empty { border-style: none; empty-cells: hide; background: white;}\n"
2252 "table.tbl th.desc { background: white; border-style: solid solid none solid; text-align: left; padding: 2px 3px;}\n"
2253 "\n"
2254 "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n"
2255 "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n"
2256 "table.lgd td.noborder { border-style: none; padding: 2px; white-space: nowrap;}\n"
2257 "table.det { border-collapse: collapse; border-style: none; }\n"
2258 "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"
2259 "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"
2260 "u {text-decoration:none; border-bottom: 1px dotted black;}\n"
2261 "div.tips {\n"
2262 " display:block;\n"
2263 " visibility:hidden;\n"
2264 " z-index:2147483647;\n"
2265 " position:absolute;\n"
2266 " padding:2px 4px 3px;\n"
2267 " background:#f0f060; color:#000000;\n"
2268 " border:1px solid #7040c0;\n"
2269 " white-space:nowrap;\n"
2270 " font-style:normal;font-size:11px;font-weight:normal;\n"
2271 " -moz-border-radius:3px;-webkit-border-radius:3px;border-radius:3px;\n"
2272 " -moz-box-shadow:gray 2px 2px 3px;-webkit-box-shadow:gray 2px 2px 3px;box-shadow:gray 2px 2px 3px;\n"
2273 "}\n"
2274 "u:hover div.tips {visibility:visible;}\n"
2275 "-->\n"
2276 "</style></head>\n",
2277 (uri->flags & ST_SHNODE) ? " on " : "",
2278 (uri->flags & ST_SHNODE) ? (uri->node ? uri->node : global.node) : ""
2279 );
2280}
2281
2282/* Dumps the HTML stats information block to the trash for and uses the state from
2283 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
2284 * for clearing the trash if needed.
2285 */
2286static void stats_dump_html_info(struct stream_interface *si, struct uri_auth *uri)
2287{
2288 struct appctx *appctx = __objt_appctx(si->end);
2289 unsigned int up = (now.tv_sec - start_date.tv_sec);
2290 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
2291
2292 /* WARNING! this has to fit the first packet too.
2293 * We are around 3.5 kB, add adding entries will
2294 * become tricky if we want to support 4kB buffers !
2295 */
2296 chunk_appendf(&trash,
2297 "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">"
2298 PRODUCT_NAME "%s</a></h1>\n"
2299 "<h2>Statistics Report for pid %d%s%s%s%s</h2>\n"
2300 "<hr width=\"100%%\" class=\"hr\">\n"
2301 "<h3>&gt; General process information</h3>\n"
2302 "<table border=0><tr><td align=\"left\" nowrap width=\"1%%\">\n"
Yves Lafon95317282018-02-26 11:10:37 +01002303 "<p><b>pid = </b> %d (process #%d, nbproc = %d, nbthread = %d)<br>\n"
William Lallemand74c24fb2016-11-21 17:18:36 +01002304 "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n"
2305 "<b>system limits:</b> memmax = %s%s; ulimit-n = %d<br>\n"
2306 "<b>maxsock = </b> %d; <b>maxconn = </b> %d; <b>maxpipes = </b> %d<br>\n"
2307 "current conns = %d; current pipes = %d/%d; conn rate = %d/sec<br>\n"
2308 "Running tasks: %d/%d; idle = %d %%<br>\n"
2309 "</td><td align=\"center\" nowrap>\n"
2310 "<table class=\"lgd\"><tr>\n"
2311 "<td class=\"active_up\">&nbsp;</td><td class=\"noborder\">active UP </td>"
2312 "<td class=\"backup_up\">&nbsp;</td><td class=\"noborder\">backup UP </td>"
2313 "</tr><tr>\n"
2314 "<td class=\"active_going_down\"></td><td class=\"noborder\">active UP, going down </td>"
2315 "<td class=\"backup_going_down\"></td><td class=\"noborder\">backup UP, going down </td>"
2316 "</tr><tr>\n"
2317 "<td class=\"active_going_up\"></td><td class=\"noborder\">active DOWN, going up </td>"
2318 "<td class=\"backup_going_up\"></td><td class=\"noborder\">backup DOWN, going up </td>"
2319 "</tr><tr>\n"
2320 "<td class=\"active_down\"></td><td class=\"noborder\">active or backup DOWN &nbsp;</td>"
2321 "<td class=\"active_no_check\"></td><td class=\"noborder\">not checked </td>"
2322 "</tr><tr>\n"
2323 "<td class=\"maintain\"></td><td class=\"noborder\" colspan=\"3\">active or backup DOWN for maintenance (MAINT) &nbsp;</td>"
2324 "</tr><tr>\n"
2325 "<td class=\"active_draining\"></td><td class=\"noborder\" colspan=\"3\">active or backup SOFT STOPPED for maintenance &nbsp;</td>"
2326 "</tr></table>\n"
2327 "Note: \"NOLB\"/\"DRAIN\" = UP with load-balancing disabled."
2328 "</td>"
2329 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
2330 "<b>Display option:</b><ul style=\"margin-top: 0.25em;\">"
2331 "",
2332 (uri->flags & ST_HIDEVER) ? "" : (STATS_VERSION_STRING),
2333 pid, (uri->flags & ST_SHNODE) ? " on " : "",
2334 (uri->flags & ST_SHNODE) ? (uri->node ? uri->node : global.node) : "",
2335 (uri->flags & ST_SHDESC) ? ": " : "",
2336 (uri->flags & ST_SHDESC) ? (uri->desc ? uri->desc : global.desc) : "",
Yves Lafon95317282018-02-26 11:10:37 +01002337 pid, relative_pid, global.nbproc, global.nbthread,
William Lallemand74c24fb2016-11-21 17:18:36 +01002338 up / 86400, (up % 86400) / 3600,
2339 (up % 3600) / 60, (up % 60),
2340 global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited",
2341 global.rlimit_memmax ? " MB" : "",
2342 global.rlimit_nofile,
2343 global.maxsock, global.maxconn, global.maxpipes,
2344 actconn, pipes_used, pipes_used+pipes_free, read_freq_ctr(&global.conn_per_sec),
Christopher Faulet34c5cc92016-12-06 09:15:30 +01002345 tasks_run_queue_cur, nb_tasks_cur, idle_pct
William Lallemand74c24fb2016-11-21 17:18:36 +01002346 );
2347
2348 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Willy Tarreau89faf5d2018-06-07 18:16:48 +02002349 memcpy(scope_txt, co_head(si_oc(si)) + appctx->ctx.stats.scope_str, appctx->ctx.stats.scope_len);
William Lallemand74c24fb2016-11-21 17:18:36 +01002350 scope_txt[appctx->ctx.stats.scope_len] = '\0';
2351
2352 chunk_appendf(&trash,
2353 "<li><form method=\"GET\">Scope : <input value=\"%s\" name=\"" STAT_SCOPE_INPUT_NAME "\" size=\"8\" maxlength=\"%d\" tabindex=\"1\"/></form>\n",
2354 (appctx->ctx.stats.scope_len > 0) ? scope_txt : "",
2355 STAT_SCOPE_TXT_MAXLEN);
2356
2357 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
2358 scope_txt[0] = 0;
2359 if (appctx->ctx.stats.scope_len) {
2360 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau89faf5d2018-06-07 18:16:48 +02002361 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 +01002362 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
2363 }
2364
2365 if (appctx->ctx.stats.flags & STAT_HIDE_DOWN)
2366 chunk_appendf(&trash,
2367 "<li><a href=\"%s%s%s%s\">Show all servers</a><br>\n",
2368 uri->uri_prefix,
2369 "",
2370 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2371 scope_txt);
2372 else
2373 chunk_appendf(&trash,
2374 "<li><a href=\"%s%s%s%s\">Hide 'DOWN' servers</a><br>\n",
2375 uri->uri_prefix,
2376 ";up",
2377 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2378 scope_txt);
2379
2380 if (uri->refresh > 0) {
2381 if (appctx->ctx.stats.flags & STAT_NO_REFRESH)
2382 chunk_appendf(&trash,
2383 "<li><a href=\"%s%s%s%s\">Enable refresh</a><br>\n",
2384 uri->uri_prefix,
2385 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2386 "",
2387 scope_txt);
2388 else
2389 chunk_appendf(&trash,
2390 "<li><a href=\"%s%s%s%s\">Disable refresh</a><br>\n",
2391 uri->uri_prefix,
2392 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2393 ";norefresh",
2394 scope_txt);
2395 }
2396
2397 chunk_appendf(&trash,
2398 "<li><a href=\"%s%s%s%s\">Refresh now</a><br>\n",
2399 uri->uri_prefix,
2400 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2401 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2402 scope_txt);
2403
2404 chunk_appendf(&trash,
2405 "<li><a href=\"%s;csv%s%s\">CSV export</a><br>\n",
2406 uri->uri_prefix,
2407 (uri->refresh > 0) ? ";norefresh" : "",
2408 scope_txt);
2409
2410 chunk_appendf(&trash,
2411 "</ul></td>"
2412 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
2413 "<b>External resources:</b><ul style=\"margin-top: 0.25em;\">\n"
2414 "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>\n"
2415 "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>\n"
2416 "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>\n"
2417 "</ul>"
2418 "</td>"
2419 "</tr></table>\n"
2420 ""
2421 );
2422
2423 if (appctx->ctx.stats.st_code) {
2424 switch (appctx->ctx.stats.st_code) {
2425 case STAT_STATUS_DONE:
2426 chunk_appendf(&trash,
2427 "<p><div class=active_up>"
2428 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2429 "Action processed successfully."
2430 "</div>\n", uri->uri_prefix,
2431 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2432 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2433 scope_txt);
2434 break;
2435 case STAT_STATUS_NONE:
2436 chunk_appendf(&trash,
2437 "<p><div class=active_going_down>"
2438 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2439 "Nothing has changed."
2440 "</div>\n", uri->uri_prefix,
2441 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2442 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2443 scope_txt);
2444 break;
2445 case STAT_STATUS_PART:
2446 chunk_appendf(&trash,
2447 "<p><div class=active_going_down>"
2448 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2449 "Action partially processed.<br>"
2450 "Some server names are probably unknown or ambiguous (duplicated names in the backend)."
2451 "</div>\n", uri->uri_prefix,
2452 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2453 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2454 scope_txt);
2455 break;
2456 case STAT_STATUS_ERRP:
2457 chunk_appendf(&trash,
2458 "<p><div class=active_down>"
2459 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2460 "Action not processed because of invalid parameters."
2461 "<ul>"
2462 "<li>The action is maybe unknown.</li>"
2463 "<li>The backend name is probably unknown or ambiguous (duplicated names).</li>"
2464 "<li>Some server names are probably unknown or ambiguous (duplicated names in the backend).</li>"
2465 "</ul>"
2466 "</div>\n", uri->uri_prefix,
2467 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2468 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2469 scope_txt);
2470 break;
2471 case STAT_STATUS_EXCD:
2472 chunk_appendf(&trash,
2473 "<p><div class=active_down>"
2474 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2475 "<b>Action not processed : the buffer couldn't store all the data.<br>"
2476 "You should retry with less servers at a time.</b>"
2477 "</div>\n", uri->uri_prefix,
2478 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2479 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2480 scope_txt);
2481 break;
2482 case STAT_STATUS_DENY:
2483 chunk_appendf(&trash,
2484 "<p><div class=active_down>"
2485 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2486 "<b>Action denied.</b>"
2487 "</div>\n", uri->uri_prefix,
2488 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2489 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2490 scope_txt);
2491 break;
2492 default:
2493 chunk_appendf(&trash,
2494 "<p><div class=active_no_check>"
2495 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2496 "Unexpected result."
2497 "</div>\n", uri->uri_prefix,
2498 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2499 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2500 scope_txt);
2501 }
2502 chunk_appendf(&trash, "<p>\n");
2503 }
2504}
2505
2506/* Dumps the HTML stats trailer block to the trash. The caller is responsible
2507 * for clearing the trash if needed.
2508 */
2509static void stats_dump_html_end()
2510{
2511 chunk_appendf(&trash, "</body></html>\n");
2512}
2513
Simon Horman05ee2132017-01-04 09:37:25 +01002514/* Dumps the stats JSON header to the trash buffer which. The caller is responsible
2515 * for clearing it if needed.
2516 */
2517static void stats_dump_json_header()
2518{
2519 chunk_strcat(&trash, "[");
2520}
2521
2522
2523/* Dumps the JSON stats trailer block to the trash. The caller is responsible
2524 * for clearing the trash if needed.
2525 */
2526static void stats_dump_json_end()
2527{
2528 chunk_strcat(&trash, "]");
2529}
2530
William Lallemand74c24fb2016-11-21 17:18:36 +01002531/* This function dumps statistics onto the stream interface's read buffer in
2532 * either CSV or HTML format. <uri> contains some HTML-specific parameters that
2533 * are ignored for CSV format (hence <uri> may be NULL there). It returns 0 if
2534 * it had to stop writing data and an I/O is needed, 1 if the dump is finished
2535 * and the stream must be closed, or -1 in case of any error. This function is
2536 * used by both the CLI and the HTTP handlers.
2537 */
Willy Tarreau2b812e22016-11-22 16:18:05 +01002538static int stats_dump_stat_to_buffer(struct stream_interface *si, struct uri_auth *uri)
William Lallemand74c24fb2016-11-21 17:18:36 +01002539{
2540 struct appctx *appctx = __objt_appctx(si->end);
2541 struct channel *rep = si_ic(si);
2542 struct proxy *px;
2543
2544 chunk_reset(&trash);
2545
2546 switch (appctx->st2) {
2547 case STAT_ST_INIT:
2548 appctx->st2 = STAT_ST_HEAD; /* let's start producing data */
2549 /* fall through */
2550
2551 case STAT_ST_HEAD:
2552 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
2553 stats_dump_html_head(uri);
Simon Horman05ee2132017-01-04 09:37:25 +01002554 else if (appctx->ctx.stats.flags & STAT_FMT_JSON)
Willy Tarreau9d7fb632017-04-11 07:53:04 +02002555 stats_dump_json_header();
William Lallemand74c24fb2016-11-21 17:18:36 +01002556 else if (!(appctx->ctx.stats.flags & STAT_FMT_TYPED))
2557 stats_dump_csv_header();
2558
Willy Tarreau06d80a92017-10-19 14:32:15 +02002559 if (ci_putchk(rep, &trash) == -1) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002560 si_applet_cant_put(si);
2561 return 0;
2562 }
2563
2564 appctx->st2 = STAT_ST_INFO;
2565 /* fall through */
2566
2567 case STAT_ST_INFO:
2568 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
2569 stats_dump_html_info(si, uri);
Willy Tarreau06d80a92017-10-19 14:32:15 +02002570 if (ci_putchk(rep, &trash) == -1) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002571 si_applet_cant_put(si);
2572 return 0;
2573 }
2574 }
2575
Olivier Houchardfbc74e82017-11-24 16:54:05 +01002576 appctx->ctx.stats.px = proxies_list;
William Lallemand74c24fb2016-11-21 17:18:36 +01002577 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
2578 appctx->st2 = STAT_ST_LIST;
2579 /* fall through */
2580
2581 case STAT_ST_LIST:
2582 /* dump proxies */
2583 while (appctx->ctx.stats.px) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002584 if (buffer_almost_full(&rep->buf)) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002585 si_applet_cant_put(si);
2586 return 0;
2587 }
2588
2589 px = appctx->ctx.stats.px;
2590 /* skip the disabled proxies, global frontend and non-networked ones */
2591 if (px->state != PR_STSTOPPED && px->uuid > 0 && (px->cap & (PR_CAP_FE | PR_CAP_BE)))
2592 if (stats_dump_proxy_to_buffer(si, px, uri) == 0)
2593 return 0;
2594
2595 appctx->ctx.stats.px = px->next;
2596 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
2597 }
2598 /* here, we just have reached the last proxy */
2599
2600 appctx->st2 = STAT_ST_END;
2601 /* fall through */
2602
2603 case STAT_ST_END:
Simon Horman05ee2132017-01-04 09:37:25 +01002604 if (appctx->ctx.stats.flags & (STAT_FMT_HTML|STAT_FMT_JSON)) {
2605 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
2606 stats_dump_html_end();
2607 else
2608 stats_dump_json_end();
Willy Tarreau06d80a92017-10-19 14:32:15 +02002609 if (ci_putchk(rep, &trash) == -1) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002610 si_applet_cant_put(si);
2611 return 0;
2612 }
2613 }
2614
2615 appctx->st2 = STAT_ST_FIN;
2616 /* fall through */
2617
2618 case STAT_ST_FIN:
2619 return 1;
2620
2621 default:
2622 /* unknown state ! */
2623 appctx->st2 = STAT_ST_FIN;
2624 return -1;
2625 }
2626}
2627
2628/* We reached the stats page through a POST request. The appctx is
2629 * expected to have already been allocated by the caller.
2630 * Parse the posted data and enable/disable servers if necessary.
2631 * Returns 1 if request was parsed or zero if it needs more data.
2632 */
2633static int stats_process_http_post(struct stream_interface *si)
2634{
2635 struct stream *s = si_strm(si);
2636 struct appctx *appctx = objt_appctx(si->end);
2637
2638 struct proxy *px = NULL;
2639 struct server *sv = NULL;
2640
2641 char key[LINESIZE];
2642 int action = ST_ADM_ACTION_NONE;
2643 int reprocess = 0;
2644
2645 int total_servers = 0;
2646 int altered_servers = 0;
2647
2648 char *first_param, *cur_param, *next_param, *end_params;
2649 char *st_cur_param = NULL;
2650 char *st_next_param = NULL;
2651
Willy Tarreau83061a82018-07-13 11:56:34 +02002652 struct buffer *temp;
William Lallemand74c24fb2016-11-21 17:18:36 +01002653 int reql;
2654
2655 temp = get_trash_chunk();
2656 if (temp->size < s->txn->req.body_len) {
2657 /* too large request */
2658 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
2659 goto out;
2660 }
2661
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002662 reql = co_getblk(si_oc(si), temp->area, s->txn->req.body_len,
2663 s->txn->req.eoh + 2);
William Lallemand74c24fb2016-11-21 17:18:36 +01002664 if (reql <= 0) {
2665 /* we need more data */
2666 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
2667 return 0;
2668 }
2669
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002670 first_param = temp->area;
2671 end_params = temp->area + reql;
William Lallemand74c24fb2016-11-21 17:18:36 +01002672 cur_param = next_param = end_params;
2673 *end_params = '\0';
2674
2675 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
2676
2677 /*
2678 * Parse the parameters in reverse order to only store the last value.
2679 * From the html form, the backend and the action are at the end.
2680 */
2681 while (cur_param > first_param) {
2682 char *value;
2683 int poffset, plen;
2684
2685 cur_param--;
2686
2687 if ((*cur_param == '&') || (cur_param == first_param)) {
2688 reprocess_servers:
2689 /* Parse the key */
2690 poffset = (cur_param != first_param ? 1 : 0);
2691 plen = next_param - cur_param + (cur_param == first_param ? 1 : 0);
2692 if ((plen > 0) && (plen <= sizeof(key))) {
2693 strncpy(key, cur_param + poffset, plen);
2694 key[plen - 1] = '\0';
2695 } else {
2696 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
2697 goto out;
2698 }
2699
2700 /* Parse the value */
2701 value = key;
2702 while (*value != '\0' && *value != '=') {
2703 value++;
2704 }
2705 if (*value == '=') {
2706 /* Ok, a value is found, we can mark the end of the key */
2707 *value++ = '\0';
2708 }
2709 if (url_decode(key) < 0 || url_decode(value) < 0)
2710 break;
2711
2712 /* Now we can check the key to see what to do */
2713 if (!px && (strcmp(key, "b") == 0)) {
2714 if ((px = proxy_be_by_name(value)) == NULL) {
2715 /* the backend name is unknown or ambiguous (duplicate names) */
2716 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
2717 goto out;
2718 }
2719 }
2720 else if (!action && (strcmp(key, "action") == 0)) {
2721 if (strcmp(value, "ready") == 0) {
2722 action = ST_ADM_ACTION_READY;
2723 }
2724 else if (strcmp(value, "drain") == 0) {
2725 action = ST_ADM_ACTION_DRAIN;
2726 }
2727 else if (strcmp(value, "maint") == 0) {
2728 action = ST_ADM_ACTION_MAINT;
2729 }
2730 else if (strcmp(value, "shutdown") == 0) {
2731 action = ST_ADM_ACTION_SHUTDOWN;
2732 }
2733 else if (strcmp(value, "dhlth") == 0) {
2734 action = ST_ADM_ACTION_DHLTH;
2735 }
2736 else if (strcmp(value, "ehlth") == 0) {
2737 action = ST_ADM_ACTION_EHLTH;
2738 }
2739 else if (strcmp(value, "hrunn") == 0) {
2740 action = ST_ADM_ACTION_HRUNN;
2741 }
2742 else if (strcmp(value, "hnolb") == 0) {
2743 action = ST_ADM_ACTION_HNOLB;
2744 }
2745 else if (strcmp(value, "hdown") == 0) {
2746 action = ST_ADM_ACTION_HDOWN;
2747 }
2748 else if (strcmp(value, "dagent") == 0) {
2749 action = ST_ADM_ACTION_DAGENT;
2750 }
2751 else if (strcmp(value, "eagent") == 0) {
2752 action = ST_ADM_ACTION_EAGENT;
2753 }
2754 else if (strcmp(value, "arunn") == 0) {
2755 action = ST_ADM_ACTION_ARUNN;
2756 }
2757 else if (strcmp(value, "adown") == 0) {
2758 action = ST_ADM_ACTION_ADOWN;
2759 }
2760 /* else these are the old supported methods */
2761 else if (strcmp(value, "disable") == 0) {
2762 action = ST_ADM_ACTION_DISABLE;
2763 }
2764 else if (strcmp(value, "enable") == 0) {
2765 action = ST_ADM_ACTION_ENABLE;
2766 }
2767 else if (strcmp(value, "stop") == 0) {
2768 action = ST_ADM_ACTION_STOP;
2769 }
2770 else if (strcmp(value, "start") == 0) {
2771 action = ST_ADM_ACTION_START;
2772 }
2773 else {
2774 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
2775 goto out;
2776 }
2777 }
2778 else if (strcmp(key, "s") == 0) {
2779 if (!(px && action)) {
2780 /*
2781 * Indicates that we'll need to reprocess the parameters
2782 * as soon as backend and action are known
2783 */
2784 if (!reprocess) {
2785 st_cur_param = cur_param;
2786 st_next_param = next_param;
2787 }
2788 reprocess = 1;
2789 }
2790 else if ((sv = findserver(px, value)) != NULL) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002791 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
William Lallemand74c24fb2016-11-21 17:18:36 +01002792 switch (action) {
2793 case ST_ADM_ACTION_DISABLE:
Emeric Brun52a91d32017-08-31 14:41:55 +02002794 if (!(sv->cur_admin & SRV_ADMF_FMAINT)) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002795 altered_servers++;
2796 total_servers++;
2797 srv_set_admin_flag(sv, SRV_ADMF_FMAINT, "'disable' on stats page");
2798 }
2799 break;
2800 case ST_ADM_ACTION_ENABLE:
Emeric Brun52a91d32017-08-31 14:41:55 +02002801 if (sv->cur_admin & SRV_ADMF_FMAINT) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002802 altered_servers++;
2803 total_servers++;
2804 srv_clr_admin_flag(sv, SRV_ADMF_FMAINT);
2805 }
2806 break;
2807 case ST_ADM_ACTION_STOP:
Emeric Brun52a91d32017-08-31 14:41:55 +02002808 if (!(sv->cur_admin & SRV_ADMF_FDRAIN)) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002809 srv_set_admin_flag(sv, SRV_ADMF_FDRAIN, "'stop' on stats page");
2810 altered_servers++;
2811 total_servers++;
2812 }
2813 break;
2814 case ST_ADM_ACTION_START:
Emeric Brun52a91d32017-08-31 14:41:55 +02002815 if (sv->cur_admin & SRV_ADMF_FDRAIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002816 srv_clr_admin_flag(sv, SRV_ADMF_FDRAIN);
2817 altered_servers++;
2818 total_servers++;
2819 }
2820 break;
2821 case ST_ADM_ACTION_DHLTH:
2822 if (sv->check.state & CHK_ST_CONFIGURED) {
2823 sv->check.state &= ~CHK_ST_ENABLED;
2824 altered_servers++;
2825 total_servers++;
2826 }
2827 break;
2828 case ST_ADM_ACTION_EHLTH:
2829 if (sv->check.state & CHK_ST_CONFIGURED) {
2830 sv->check.state |= CHK_ST_ENABLED;
2831 altered_servers++;
2832 total_servers++;
2833 }
2834 break;
2835 case ST_ADM_ACTION_HRUNN:
2836 if (!(sv->track)) {
2837 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02002838 srv_set_running(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01002839 altered_servers++;
2840 total_servers++;
2841 }
2842 break;
2843 case ST_ADM_ACTION_HNOLB:
2844 if (!(sv->track)) {
2845 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02002846 srv_set_stopping(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01002847 altered_servers++;
2848 total_servers++;
2849 }
2850 break;
2851 case ST_ADM_ACTION_HDOWN:
2852 if (!(sv->track)) {
2853 sv->check.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02002854 srv_set_stopped(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01002855 altered_servers++;
2856 total_servers++;
2857 }
2858 break;
2859 case ST_ADM_ACTION_DAGENT:
2860 if (sv->agent.state & CHK_ST_CONFIGURED) {
2861 sv->agent.state &= ~CHK_ST_ENABLED;
2862 altered_servers++;
2863 total_servers++;
2864 }
2865 break;
2866 case ST_ADM_ACTION_EAGENT:
2867 if (sv->agent.state & CHK_ST_CONFIGURED) {
2868 sv->agent.state |= CHK_ST_ENABLED;
2869 altered_servers++;
2870 total_servers++;
2871 }
2872 break;
2873 case ST_ADM_ACTION_ARUNN:
2874 if (sv->agent.state & CHK_ST_ENABLED) {
2875 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02002876 srv_set_running(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01002877 altered_servers++;
2878 total_servers++;
2879 }
2880 break;
2881 case ST_ADM_ACTION_ADOWN:
2882 if (sv->agent.state & CHK_ST_ENABLED) {
2883 sv->agent.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02002884 srv_set_stopped(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01002885 altered_servers++;
2886 total_servers++;
2887 }
2888 break;
2889 case ST_ADM_ACTION_READY:
2890 srv_adm_set_ready(sv);
2891 altered_servers++;
2892 total_servers++;
2893 break;
2894 case ST_ADM_ACTION_DRAIN:
2895 srv_adm_set_drain(sv);
2896 altered_servers++;
2897 total_servers++;
2898 break;
2899 case ST_ADM_ACTION_MAINT:
2900 srv_adm_set_maint(sv);
2901 altered_servers++;
2902 total_servers++;
2903 break;
2904 case ST_ADM_ACTION_SHUTDOWN:
2905 if (px->state != PR_STSTOPPED) {
2906 struct stream *sess, *sess_bck;
2907
2908 list_for_each_entry_safe(sess, sess_bck, &sv->actconns, by_srv)
2909 if (sess->srv_conn == sv)
2910 stream_shutdown(sess, SF_ERR_KILLED);
2911
2912 altered_servers++;
2913 total_servers++;
2914 }
2915 break;
2916 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002917 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Lallemand74c24fb2016-11-21 17:18:36 +01002918 } else {
2919 /* the server name is unknown or ambiguous (duplicate names) */
2920 total_servers++;
2921 }
2922 }
2923 if (reprocess && px && action) {
2924 /* Now, we know the backend and the action chosen by the user.
2925 * We can safely restart from the first server parameter
2926 * to reprocess them
2927 */
2928 cur_param = st_cur_param;
2929 next_param = st_next_param;
2930 reprocess = 0;
2931 goto reprocess_servers;
2932 }
2933
2934 next_param = cur_param;
2935 }
2936 }
2937
2938 if (total_servers == 0) {
2939 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
2940 }
2941 else if (altered_servers == 0) {
2942 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
2943 }
2944 else if (altered_servers == total_servers) {
2945 appctx->ctx.stats.st_code = STAT_STATUS_DONE;
2946 }
2947 else {
2948 appctx->ctx.stats.st_code = STAT_STATUS_PART;
2949 }
2950 out:
2951 return 1;
2952}
2953
2954
2955static int stats_send_http_headers(struct stream_interface *si)
2956{
2957 struct stream *s = si_strm(si);
2958 struct uri_auth *uri = s->be->uri_auth;
Willy Tarreau21ff2c42018-09-20 11:01:01 +02002959 struct appctx *appctx = __objt_appctx(si->end);
William Lallemand74c24fb2016-11-21 17:18:36 +01002960
2961 chunk_printf(&trash,
2962 "HTTP/1.1 200 OK\r\n"
2963 "Cache-Control: no-cache\r\n"
2964 "Connection: close\r\n"
2965 "Content-Type: %s\r\n",
2966 (appctx->ctx.stats.flags & STAT_FMT_HTML) ? "text/html" : "text/plain");
2967
2968 if (uri->refresh > 0 && !(appctx->ctx.stats.flags & STAT_NO_REFRESH))
2969 chunk_appendf(&trash, "Refresh: %d\r\n",
2970 uri->refresh);
2971
2972 /* we don't send the CRLF in chunked mode, it will be sent with the first chunk's size */
2973
2974 if (appctx->ctx.stats.flags & STAT_CHUNKED)
2975 chunk_appendf(&trash, "Transfer-Encoding: chunked\r\n");
2976 else
2977 chunk_appendf(&trash, "\r\n");
2978
Willy Tarreau06d80a92017-10-19 14:32:15 +02002979 if (ci_putchk(si_ic(si), &trash) == -1) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002980 si_applet_cant_put(si);
2981 return 0;
2982 }
2983
2984 return 1;
2985}
2986
2987static int stats_send_http_redirect(struct stream_interface *si)
2988{
2989 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
2990 struct stream *s = si_strm(si);
2991 struct uri_auth *uri = s->be->uri_auth;
Willy Tarreau21ff2c42018-09-20 11:01:01 +02002992 struct appctx *appctx = __objt_appctx(si->end);
William Lallemand74c24fb2016-11-21 17:18:36 +01002993
2994 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
2995 scope_txt[0] = 0;
2996 if (appctx->ctx.stats.scope_len) {
2997 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau89faf5d2018-06-07 18:16:48 +02002998 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 +01002999 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
3000 }
3001
3002 /* We don't want to land on the posted stats page because a refresh will
3003 * repost the data. We don't want this to happen on accident so we redirect
3004 * the browse to the stats page with a GET.
3005 */
3006 chunk_printf(&trash,
3007 "HTTP/1.1 303 See Other\r\n"
3008 "Cache-Control: no-cache\r\n"
3009 "Content-Type: text/plain\r\n"
3010 "Connection: close\r\n"
3011 "Location: %s;st=%s%s%s%s\r\n"
3012 "Content-length: 0\r\n"
3013 "\r\n",
3014 uri->uri_prefix,
3015 ((appctx->ctx.stats.st_code > STAT_STATUS_INIT) &&
3016 (appctx->ctx.stats.st_code < STAT_STATUS_SIZE) &&
3017 stat_status_codes[appctx->ctx.stats.st_code]) ?
3018 stat_status_codes[appctx->ctx.stats.st_code] :
3019 stat_status_codes[STAT_STATUS_UNKN],
3020 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3021 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3022 scope_txt);
3023
Willy Tarreau06d80a92017-10-19 14:32:15 +02003024 if (ci_putchk(si_ic(si), &trash) == -1) {
William Lallemand74c24fb2016-11-21 17:18:36 +01003025 si_applet_cant_put(si);
3026 return 0;
3027 }
3028
3029 return 1;
3030}
3031
Simon Horman05ee2132017-01-04 09:37:25 +01003032
William Lallemand74c24fb2016-11-21 17:18:36 +01003033/* This I/O handler runs as an applet embedded in a stream interface. It is
3034 * used to send HTTP stats over a TCP socket. The mechanism is very simple.
3035 * appctx->st0 contains the operation in progress (dump, done). The handler
3036 * automatically unregisters itself once transfer is complete.
3037 */
3038static void http_stats_io_handler(struct appctx *appctx)
3039{
3040 struct stream_interface *si = appctx->owner;
3041 struct stream *s = si_strm(si);
3042 struct channel *req = si_oc(si);
3043 struct channel *res = si_ic(si);
3044
3045 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
3046 goto out;
3047
Christopher Fauleta73e59b2016-12-09 17:30:18 +01003048 /* Check if the input buffer is avalaible. */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003049 if (res->buf.size == 0) {
Christopher Fauleta73e59b2016-12-09 17:30:18 +01003050 si_applet_cant_put(si);
3051 goto out;
3052 }
3053
William Lallemand74c24fb2016-11-21 17:18:36 +01003054 /* check that the output is not closed */
3055 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW))
3056 appctx->st0 = STAT_HTTP_DONE;
3057
3058 /* all states are processed in sequence */
3059 if (appctx->st0 == STAT_HTTP_HEAD) {
3060 if (stats_send_http_headers(si)) {
3061 if (s->txn->meth == HTTP_METH_HEAD)
3062 appctx->st0 = STAT_HTTP_DONE;
3063 else
3064 appctx->st0 = STAT_HTTP_DUMP;
3065 }
3066 }
3067
3068 if (appctx->st0 == STAT_HTTP_DUMP) {
Willy Tarreau97f538b2018-06-15 19:41:31 +02003069 unsigned int prev_len = ci_data(si_ic(si));
William Lallemand74c24fb2016-11-21 17:18:36 +01003070 unsigned int data_len;
3071 unsigned int last_len;
3072 unsigned int last_fwd = 0;
3073
3074 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
3075 /* One difficulty we're facing is that we must prevent
3076 * the input data from being automatically forwarded to
3077 * the output area. For this, we temporarily disable
3078 * forwarding on the channel.
3079 */
3080 last_fwd = si_ic(si)->to_forward;
3081 si_ic(si)->to_forward = 0;
3082 chunk_printf(&trash, "\r\n000000\r\n");
Willy Tarreau06d80a92017-10-19 14:32:15 +02003083 if (ci_putchk(si_ic(si), &trash) == -1) {
William Lallemand74c24fb2016-11-21 17:18:36 +01003084 si_applet_cant_put(si);
3085 si_ic(si)->to_forward = last_fwd;
3086 goto out;
3087 }
3088 }
3089
Willy Tarreau97f538b2018-06-15 19:41:31 +02003090 data_len = ci_data(si_ic(si));
William Lallemand74c24fb2016-11-21 17:18:36 +01003091 if (stats_dump_stat_to_buffer(si, s->be->uri_auth))
3092 appctx->st0 = STAT_HTTP_DONE;
3093
Willy Tarreau97f538b2018-06-15 19:41:31 +02003094 last_len = ci_data(si_ic(si));
William Lallemand74c24fb2016-11-21 17:18:36 +01003095
3096 /* Now we must either adjust or remove the chunk size. This is
3097 * not easy because the chunk size might wrap at the end of the
3098 * buffer, so we pretend we have nothing in the buffer, we write
3099 * the size, then restore the buffer's contents. Note that we can
3100 * only do that because no forwarding is scheduled on the stats
3101 * applet.
3102 */
3103 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
3104 si_ic(si)->total -= (last_len - prev_len);
Willy Tarreau97f538b2018-06-15 19:41:31 +02003105 b_sub(si_ib(si), (last_len - prev_len));
William Lallemand74c24fb2016-11-21 17:18:36 +01003106
3107 if (last_len != data_len) {
3108 chunk_printf(&trash, "\r\n%06x\r\n", (last_len - data_len));
Willy Tarreau06d80a92017-10-19 14:32:15 +02003109 if (ci_putchk(si_ic(si), &trash) == -1)
William Lallemand74c24fb2016-11-21 17:18:36 +01003110 si_applet_cant_put(si);
3111
3112 si_ic(si)->total += (last_len - data_len);
Willy Tarreau97f538b2018-06-15 19:41:31 +02003113 b_add(si_ib(si), last_len - data_len);
William Lallemand74c24fb2016-11-21 17:18:36 +01003114 }
3115 /* now re-enable forwarding */
3116 channel_forward(si_ic(si), last_fwd);
3117 }
3118 }
3119
3120 if (appctx->st0 == STAT_HTTP_POST) {
3121 if (stats_process_http_post(si))
3122 appctx->st0 = STAT_HTTP_LAST;
3123 else if (si_oc(si)->flags & CF_SHUTR)
3124 appctx->st0 = STAT_HTTP_DONE;
3125 }
3126
3127 if (appctx->st0 == STAT_HTTP_LAST) {
3128 if (stats_send_http_redirect(si))
3129 appctx->st0 = STAT_HTTP_DONE;
3130 }
3131
3132 if (appctx->st0 == STAT_HTTP_DONE) {
3133 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
3134 chunk_printf(&trash, "\r\n0\r\n\r\n");
Willy Tarreau06d80a92017-10-19 14:32:15 +02003135 if (ci_putchk(si_ic(si), &trash) == -1) {
William Lallemand74c24fb2016-11-21 17:18:36 +01003136 si_applet_cant_put(si);
3137 goto out;
3138 }
3139 }
3140 /* eat the whole request */
Willy Tarreau97f538b2018-06-15 19:41:31 +02003141 co_skip(si_oc(si), co_data(si_oc(si)));
William Lallemand74c24fb2016-11-21 17:18:36 +01003142 res->flags |= CF_READ_NULL;
3143 si_shutr(si);
3144 }
3145
3146 if ((res->flags & CF_SHUTR) && (si->state == SI_ST_EST))
3147 si_shutw(si);
3148
3149 if (appctx->st0 == STAT_HTTP_DONE) {
3150 if ((req->flags & CF_SHUTW) && (si->state == SI_ST_EST)) {
3151 si_shutr(si);
3152 res->flags |= CF_READ_NULL;
3153 }
3154 }
3155 out:
Willy Tarreau055ba4f2018-07-24 17:05:54 +02003156 /* we have left the request in the buffer for the case where we
3157 * process a POST, and this automatically re-enables activity on
3158 * read. It's better to indicate that we want to stop reading when
3159 * we're sending, so that we know there's at most one direction
3160 * deciding to wake the applet up. It saves it from looping when
3161 * emitting large blocks into small TCP windows.
3162 */
3163 if (!channel_is_empty(res))
3164 si_applet_stop_get(si);
William Lallemand74c24fb2016-11-21 17:18:36 +01003165}
3166
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003167/* Dump all fields from <info> into <out> using the "show info" format (name: value) */
Willy Tarreau83061a82018-07-13 11:56:34 +02003168static int stats_dump_info_fields(struct buffer *out,
3169 const struct field *info)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003170{
3171 int field;
3172
3173 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
3174 if (!field_format(info, field))
3175 continue;
3176
3177 if (!chunk_appendf(out, "%s: ", info_field_names[field]))
3178 return 0;
3179 if (!stats_emit_raw_data_field(out, &info[field]))
3180 return 0;
3181 if (!chunk_strcat(out, "\n"))
3182 return 0;
3183 }
3184 return 1;
3185}
3186
3187/* Dump all fields from <info> into <out> using the "show info typed" format */
Willy Tarreau83061a82018-07-13 11:56:34 +02003188static int stats_dump_typed_info_fields(struct buffer *out,
3189 const struct field *info)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003190{
3191 int field;
3192
3193 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
3194 if (!field_format(info, field))
3195 continue;
3196
3197 if (!chunk_appendf(out, "%d.%s.%u:", field, info_field_names[field], info[INF_PROCESS_NUM].u.u32))
3198 return 0;
3199 if (!stats_emit_field_tags(out, &info[field], ':'))
3200 return 0;
3201 if (!stats_emit_typed_data_field(out, &info[field]))
3202 return 0;
3203 if (!chunk_strcat(out, "\n"))
3204 return 0;
3205 }
3206 return 1;
3207}
3208
3209/* Fill <info> with HAProxy global info. <info> is preallocated
3210 * array of length <len>. The length of the aray must be
3211 * INF_TOTAL_FIELDS. If this length is less then this value, the
3212 * function returns 0, otherwise, it returns 1.
3213 */
3214int stats_fill_info(struct field *info, int len)
3215{
3216 unsigned int up = (now.tv_sec - start_date.tv_sec);
Willy Tarreau83061a82018-07-13 11:56:34 +02003217 struct buffer *out = get_trash_chunk();
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003218
3219#ifdef USE_OPENSSL
3220 int ssl_sess_rate = read_freq_ctr(&global.ssl_per_sec);
3221 int ssl_key_rate = read_freq_ctr(&global.ssl_fe_keys_per_sec);
3222 int ssl_reuse = 0;
3223
3224 if (ssl_key_rate < ssl_sess_rate) {
3225 /* count the ssl reuse ratio and avoid overflows in both directions */
3226 ssl_reuse = 100 - (100 * ssl_key_rate + (ssl_sess_rate - 1) / 2) / ssl_sess_rate;
3227 }
3228#endif
3229
3230 if (len < INF_TOTAL_FIELDS)
3231 return 0;
3232
3233 chunk_reset(out);
3234 memset(info, 0, sizeof(*info) * len);
3235
3236 info[INF_NAME] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, PRODUCT_NAME);
3237 info[INF_VERSION] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, HAPROXY_VERSION);
3238 info[INF_RELEASE_DATE] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, HAPROXY_DATE);
3239
Yves Lafon95317282018-02-26 11:10:37 +01003240 info[INF_NBTHREAD] = mkf_u32(FO_CONFIG|FS_SERVICE, global.nbthread);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003241 info[INF_NBPROC] = mkf_u32(FO_CONFIG|FS_SERVICE, global.nbproc);
3242 info[INF_PROCESS_NUM] = mkf_u32(FO_KEY, relative_pid);
3243 info[INF_PID] = mkf_u32(FO_STATUS, pid);
3244
3245 info[INF_UPTIME] = mkf_str(FN_DURATION, chunk_newstr(out));
3246 chunk_appendf(out, "%ud %uh%02um%02us", up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60));
3247
3248 info[INF_UPTIME_SEC] = mkf_u32(FN_DURATION, up);
3249 info[INF_MEMMAX_MB] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_memmax);
3250 info[INF_POOL_ALLOC_MB] = mkf_u32(0, (unsigned)(pool_total_allocated() / 1048576L));
3251 info[INF_POOL_USED_MB] = mkf_u32(0, (unsigned)(pool_total_used() / 1048576L));
3252 info[INF_POOL_FAILED] = mkf_u32(FN_COUNTER, pool_total_failures());
3253 info[INF_ULIMIT_N] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_nofile);
3254 info[INF_MAXSOCK] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxsock);
3255 info[INF_MAXCONN] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxconn);
3256 info[INF_HARD_MAXCONN] = mkf_u32(FO_CONFIG|FN_LIMIT, global.hardmaxconn);
3257 info[INF_CURR_CONN] = mkf_u32(0, actconn);
3258 info[INF_CUM_CONN] = mkf_u32(FN_COUNTER, totalconn);
3259 info[INF_CUM_REQ] = mkf_u32(FN_COUNTER, global.req_count);
3260#ifdef USE_OPENSSL
3261 info[INF_MAX_SSL_CONNS] = mkf_u32(FN_MAX, global.maxsslconn);
3262 info[INF_CURR_SSL_CONNS] = mkf_u32(0, sslconns);
3263 info[INF_CUM_SSL_CONNS] = mkf_u32(FN_COUNTER, totalsslconns);
3264#endif
3265 info[INF_MAXPIPES] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxpipes);
3266 info[INF_PIPES_USED] = mkf_u32(0, pipes_used);
3267 info[INF_PIPES_FREE] = mkf_u32(0, pipes_free);
3268 info[INF_CONN_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.conn_per_sec));
3269 info[INF_CONN_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.cps_lim);
3270 info[INF_MAX_CONN_RATE] = mkf_u32(FN_MAX, global.cps_max);
3271 info[INF_SESS_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.sess_per_sec));
3272 info[INF_SESS_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.sps_lim);
3273 info[INF_MAX_SESS_RATE] = mkf_u32(FN_RATE, global.sps_max);
3274
3275#ifdef USE_OPENSSL
3276 info[INF_SSL_RATE] = mkf_u32(FN_RATE, ssl_sess_rate);
3277 info[INF_SSL_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.ssl_lim);
3278 info[INF_MAX_SSL_RATE] = mkf_u32(FN_MAX, global.ssl_max);
3279 info[INF_SSL_FRONTEND_KEY_RATE] = mkf_u32(0, ssl_key_rate);
3280 info[INF_SSL_FRONTEND_MAX_KEY_RATE] = mkf_u32(FN_MAX, global.ssl_fe_keys_max);
3281 info[INF_SSL_FRONTEND_SESSION_REUSE_PCT] = mkf_u32(0, ssl_reuse);
3282 info[INF_SSL_BACKEND_KEY_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.ssl_be_keys_per_sec));
3283 info[INF_SSL_BACKEND_MAX_KEY_RATE] = mkf_u32(FN_MAX, global.ssl_be_keys_max);
3284 info[INF_SSL_CACHE_LOOKUPS] = mkf_u32(FN_COUNTER, global.shctx_lookups);
3285 info[INF_SSL_CACHE_MISSES] = mkf_u32(FN_COUNTER, global.shctx_misses);
3286#endif
3287 info[INF_COMPRESS_BPS_IN] = mkf_u32(FN_RATE, read_freq_ctr(&global.comp_bps_in));
3288 info[INF_COMPRESS_BPS_OUT] = mkf_u32(FN_RATE, read_freq_ctr(&global.comp_bps_out));
3289 info[INF_COMPRESS_BPS_RATE_LIM] = mkf_u32(FO_CONFIG|FN_LIMIT, global.comp_rate_lim);
3290#ifdef USE_ZLIB
3291 info[INF_ZLIB_MEM_USAGE] = mkf_u32(0, zlib_used_memory);
3292 info[INF_MAX_ZLIB_MEM_USAGE] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxzlibmem);
3293#endif
3294 info[INF_TASKS] = mkf_u32(0, nb_tasks_cur);
Christopher Faulet34c5cc92016-12-06 09:15:30 +01003295 info[INF_RUN_QUEUE] = mkf_u32(0, tasks_run_queue_cur);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003296 info[INF_IDLE_PCT] = mkf_u32(FN_AVG, idle_pct);
3297 info[INF_NODE] = mkf_str(FO_CONFIG|FN_OUTPUT|FS_SERVICE, global.node);
3298 if (global.desc)
3299 info[INF_DESCRIPTION] = mkf_str(FO_CONFIG|FN_OUTPUT|FS_SERVICE, global.desc);
Willy Tarreau00098ea2018-11-05 14:38:13 +01003300 info[INF_STOPPING] = mkf_u32(0, stopping);
3301 info[INF_JOBS] = mkf_u32(0, jobs);
3302 info[INF_LISTENERS] = mkf_u32(0, listeners);
Willy Tarreau199ad242018-11-05 16:31:22 +01003303 info[INF_ACTIVE_PEERS] = mkf_u32(0, active_peers);
Willy Tarreau2d372c22018-11-05 17:12:27 +01003304 info[INF_CONNECTED_PEERS] = mkf_u32(0, connected_peers);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003305
3306 return 1;
3307}
3308
3309/* This function dumps information onto the stream interface's read buffer.
3310 * It returns 0 as long as it does not complete, non-zero upon completion.
3311 * No state is used.
3312 */
3313static int stats_dump_info_to_buffer(struct stream_interface *si)
3314{
3315 struct appctx *appctx = __objt_appctx(si->end);
3316
3317 if (!stats_fill_info(info, INF_TOTAL_FIELDS))
3318 return 0;
3319
3320 chunk_reset(&trash);
3321
3322 if (appctx->ctx.stats.flags & STAT_FMT_TYPED)
3323 stats_dump_typed_info_fields(&trash, info);
Simon Horman05ee2132017-01-04 09:37:25 +01003324 else if (appctx->ctx.stats.flags & STAT_FMT_JSON)
3325 stats_dump_json_info_fields(&trash, info);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003326 else
3327 stats_dump_info_fields(&trash, info);
3328
Willy Tarreau06d80a92017-10-19 14:32:15 +02003329 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003330 si_applet_cant_put(si);
3331 return 0;
3332 }
3333
3334 return 1;
3335}
3336
Simon Horman6f6bb382017-01-04 09:37:26 +01003337/* This function dumps the schema onto the stream interface's read buffer.
3338 * It returns 0 as long as it does not complete, non-zero upon completion.
3339 * No state is used.
3340 *
3341 * Integer values bouned to the range [-(2**53)+1, (2**53)-1] as
3342 * per the recommendation for interoperable integers in section 6 of RFC 7159.
3343 */
Willy Tarreau83061a82018-07-13 11:56:34 +02003344static void stats_dump_json_schema(struct buffer *out)
Simon Horman6f6bb382017-01-04 09:37:26 +01003345{
3346
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003347 int old_len = out->data;
Simon Horman6f6bb382017-01-04 09:37:26 +01003348
3349 chunk_strcat(out,
3350 "{"
3351 "\"$schema\":\"http://json-schema.org/draft-04/schema#\","
3352 "\"oneOf\":["
3353 "{"
3354 "\"title\":\"Info\","
3355 "\"type\":\"array\","
3356 "\"items\":{"
3357 "\"properties\":{"
3358 "\"title\":\"InfoItem\","
3359 "\"type\":\"object\","
3360 "\"field\":{\"$ref\":\"#/definitions/field\"},"
3361 "\"processNum\":{\"$ref\":\"#/definitions/processNum\"},"
3362 "\"tags\":{\"$ref\":\"#/definitions/tags\"},"
3363 "\"value\":{\"$ref\":\"#/definitions/typedValue\"}"
3364 "},"
3365 "\"required\":[\"field\",\"processNum\",\"tags\","
3366 "\"value\"]"
3367 "}"
3368 "},"
3369 "{"
3370 "\"title\":\"Stat\","
3371 "\"type\":\"array\","
3372 "\"items\":{"
3373 "\"title\":\"InfoItem\","
3374 "\"type\":\"object\","
3375 "\"properties\":{"
3376 "\"objType\":{"
3377 "\"enum\":[\"Frontend\",\"Backend\",\"Listener\","
3378 "\"Server\",\"Unknown\"]"
3379 "},"
3380 "\"proxyId\":{"
3381 "\"type\":\"integer\","
3382 "\"minimum\":0"
3383 "},"
3384 "\"id\":{"
3385 "\"type\":\"integer\","
3386 "\"minimum\":0"
3387 "},"
3388 "\"field\":{\"$ref\":\"#/definitions/field\"},"
3389 "\"processNum\":{\"$ref\":\"#/definitions/processNum\"},"
3390 "\"tags\":{\"$ref\":\"#/definitions/tags\"},"
3391 "\"typedValue\":{\"$ref\":\"#/definitions/typedValue\"}"
3392 "},"
3393 "\"required\":[\"objType\",\"proxyId\",\"id\","
3394 "\"field\",\"processNum\",\"tags\","
3395 "\"value\"]"
3396 "}"
3397 "},"
3398 "{"
3399 "\"title\":\"Error\","
3400 "\"type\":\"object\","
3401 "\"properties\":{"
3402 "\"errorStr\":{"
3403 "\"type\":\"string\""
3404 "},"
3405 "\"required\":[\"errorStr\"]"
3406 "}"
3407 "}"
3408 "],"
3409 "\"definitions\":{"
3410 "\"field\":{"
3411 "\"type\":\"object\","
3412 "\"pos\":{"
3413 "\"type\":\"integer\","
3414 "\"minimum\":0"
3415 "},"
3416 "\"name\":{"
3417 "\"type\":\"string\""
3418 "},"
3419 "\"required\":[\"pos\",\"name\"]"
3420 "},"
3421 "\"processNum\":{"
3422 "\"type\":\"integer\","
3423 "\"minimum\":1"
3424 "},"
3425 "\"tags\":{"
3426 "\"type\":\"object\","
3427 "\"origin\":{"
3428 "\"type\":\"string\","
3429 "\"enum\":[\"Metric\",\"Status\",\"Key\","
3430 "\"Config\",\"Product\",\"Unknown\"]"
3431 "},"
3432 "\"nature\":{"
3433 "\"type\":\"string\","
3434 "\"enum\":[\"Gauge\",\"Limit\",\"Min\",\"Max\","
3435 "\"Rate\",\"Counter\",\"Duration\","
3436 "\"Age\",\"Time\",\"Name\",\"Output\","
3437 "\"Avg\", \"Unknown\"]"
3438 "},"
3439 "\"scope\":{"
3440 "\"type\":\"string\","
3441 "\"enum\":[\"Cluster\",\"Process\",\"Service\","
3442 "\"System\",\"Unknown\"]"
3443 "},"
3444 "\"required\":[\"origin\",\"nature\",\"scope\"]"
3445 "},"
3446 "\"typedValue\":{"
3447 "\"type\":\"object\","
3448 "\"oneOf\":["
3449 "{\"$ref\":\"#/definitions/typedValue/definitions/s32Value\"},"
3450 "{\"$ref\":\"#/definitions/typedValue/definitions/s64Value\"},"
3451 "{\"$ref\":\"#/definitions/typedValue/definitions/u32Value\"},"
3452 "{\"$ref\":\"#/definitions/typedValue/definitions/u64Value\"},"
3453 "{\"$ref\":\"#/definitions/typedValue/definitions/strValue\"}"
3454 "],"
3455 "\"definitions\":{"
3456 "\"s32Value\":{"
3457 "\"properties\":{"
3458 "\"type\":{"
3459 "\"type\":\"string\","
3460 "\"enum\":[\"s32\"]"
3461 "},"
3462 "\"value\":{"
3463 "\"type\":\"integer\","
3464 "\"minimum\":-2147483648,"
3465 "\"maximum\":2147483647"
3466 "}"
3467 "},"
3468 "\"required\":[\"type\",\"value\"]"
3469 "},"
3470 "\"s64Value\":{"
3471 "\"properties\":{"
3472 "\"type\":{"
3473 "\"type\":\"string\","
3474 "\"enum\":[\"s64\"]"
3475 "},"
3476 "\"value\":{"
3477 "\"type\":\"integer\","
3478 "\"minimum\":-9007199254740991,"
3479 "\"maximum\":9007199254740991"
3480 "}"
3481 "},"
3482 "\"required\":[\"type\",\"value\"]"
3483 "},"
3484 "\"u32Value\":{"
3485 "\"properties\":{"
3486 "\"type\":{"
3487 "\"type\":\"string\","
3488 "\"enum\":[\"u32\"]"
3489 "},"
3490 "\"value\":{"
3491 "\"type\":\"integer\","
3492 "\"minimum\":0,"
3493 "\"maximum\":4294967295"
3494 "}"
3495 "},"
3496 "\"required\":[\"type\",\"value\"]"
3497 "},"
3498 "\"u64Value\":{"
3499 "\"properties\":{"
3500 "\"type\":{"
3501 "\"type\":\"string\","
3502 "\"enum\":[\"u64\"]"
3503 "},"
3504 "\"value\":{"
3505 "\"type\":\"integer\","
3506 "\"minimum\":0,"
3507 "\"maximum\":9007199254740991"
3508 "}"
3509 "},"
3510 "\"required\":[\"type\",\"value\"]"
3511 "},"
3512 "\"strValue\":{"
3513 "\"properties\":{"
3514 "\"type\":{"
3515 "\"type\":\"string\","
3516 "\"enum\":[\"str\"]"
3517 "},"
3518 "\"value\":{\"type\":\"string\"}"
3519 "},"
3520 "\"required\":[\"type\",\"value\"]"
3521 "},"
3522 "\"unknownValue\":{"
3523 "\"properties\":{"
3524 "\"type\":{"
3525 "\"type\":\"integer\","
3526 "\"minimum\":0"
3527 "},"
3528 "\"value\":{"
3529 "\"type\":\"string\","
3530 "\"enum\":[\"unknown\"]"
3531 "}"
3532 "},"
3533 "\"required\":[\"type\",\"value\"]"
3534 "}"
3535 "}"
3536 "}"
3537 "}"
3538 "}");
3539
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003540 if (old_len == out->data) {
Simon Horman6f6bb382017-01-04 09:37:26 +01003541 chunk_reset(out);
3542 chunk_appendf(out,
3543 "{\"errorStr\":\"output buffer too short\"}");
3544 }
3545}
3546
3547/* This function dumps the schema onto the stream interface's read buffer.
3548 * It returns 0 as long as it does not complete, non-zero upon completion.
3549 * No state is used.
3550 */
3551static int stats_dump_json_schema_to_buffer(struct stream_interface *si)
3552{
3553 chunk_reset(&trash);
3554
3555 stats_dump_json_schema(&trash);
3556
Willy Tarreau06d80a92017-10-19 14:32:15 +02003557 if (ci_putchk(si_ic(si), &trash) == -1) {
Simon Horman6f6bb382017-01-04 09:37:26 +01003558 si_applet_cant_put(si);
3559 return 0;
3560 }
3561
3562 return 1;
3563}
3564
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02003565static int cli_parse_clear_counters(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau89d467c2016-11-23 11:02:40 +01003566{
3567 struct proxy *px;
3568 struct server *sv;
3569 struct listener *li;
3570 int clrall = 0;
3571
3572 if (strcmp(args[2], "all") == 0)
3573 clrall = 1;
3574
3575 /* check permissions */
3576 if (!cli_has_level(appctx, ACCESS_LVL_OPER) ||
3577 (clrall && !cli_has_level(appctx, ACCESS_LVL_ADMIN)))
3578 return 1;
3579
Olivier Houchardfbc74e82017-11-24 16:54:05 +01003580 for (px = proxies_list; px; px = px->next) {
Willy Tarreau89d467c2016-11-23 11:02:40 +01003581 if (clrall) {
3582 memset(&px->be_counters, 0, sizeof(px->be_counters));
3583 memset(&px->fe_counters, 0, sizeof(px->fe_counters));
3584 }
3585 else {
3586 px->be_counters.conn_max = 0;
3587 px->be_counters.p.http.rps_max = 0;
3588 px->be_counters.sps_max = 0;
3589 px->be_counters.cps_max = 0;
3590 px->be_counters.nbpend_max = 0;
3591
3592 px->fe_counters.conn_max = 0;
3593 px->fe_counters.p.http.rps_max = 0;
3594 px->fe_counters.sps_max = 0;
3595 px->fe_counters.cps_max = 0;
Willy Tarreau89d467c2016-11-23 11:02:40 +01003596 }
3597
3598 for (sv = px->srv; sv; sv = sv->next)
3599 if (clrall)
3600 memset(&sv->counters, 0, sizeof(sv->counters));
3601 else {
3602 sv->counters.cur_sess_max = 0;
3603 sv->counters.nbpend_max = 0;
3604 sv->counters.sps_max = 0;
3605 }
3606
3607 list_for_each_entry(li, &px->conf.listeners, by_fe)
3608 if (li->counters) {
3609 if (clrall)
3610 memset(li->counters, 0, sizeof(*li->counters));
3611 else
3612 li->counters->conn_max = 0;
3613 }
3614 }
3615
3616 global.cps_max = 0;
3617 global.sps_max = 0;
Olivier Houchard00bc3cb2017-10-17 19:23:25 +02003618 global.ssl_max = 0;
3619 global.ssl_fe_keys_max = 0;
3620 global.ssl_be_keys_max = 0;
Willy Tarreaud80cb4e2018-01-20 19:30:13 +01003621
3622 memset(activity, 0, sizeof(activity));
Willy Tarreau89d467c2016-11-23 11:02:40 +01003623 return 1;
3624}
3625
3626
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02003627static int cli_parse_show_info(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003628{
Willy Tarreaud25fc792016-12-16 12:33:47 +01003629 appctx->ctx.stats.scope_str = 0;
3630 appctx->ctx.stats.scope_len = 0;
3631 appctx->ctx.stats.flags = 0;
3632
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003633 if (strcmp(args[2], "typed") == 0)
3634 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
Simon Horman05ee2132017-01-04 09:37:25 +01003635 else if (strcmp(args[2], "json") == 0)
3636 appctx->ctx.stats.flags |= STAT_FMT_JSON;
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003637 return 0;
3638}
3639
3640
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02003641static int cli_parse_show_stat(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau2b812e22016-11-22 16:18:05 +01003642{
Willy Tarreaud25fc792016-12-16 12:33:47 +01003643 appctx->ctx.stats.scope_str = 0;
3644 appctx->ctx.stats.scope_len = 0;
3645 appctx->ctx.stats.flags = 0;
3646
Willy Tarreau2b812e22016-11-22 16:18:05 +01003647 if (*args[2] && *args[3] && *args[4]) {
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01003648 struct proxy *px;
3649
3650 px = proxy_find_by_name(args[2], 0, 0);
3651 if (px)
3652 appctx->ctx.stats.iid = px->uuid;
3653 else
3654 appctx->ctx.stats.iid = atoi(args[2]);
3655
3656 if (!appctx->ctx.stats.iid) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02003657 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01003658 appctx->ctx.cli.msg = "No such proxy.\n";
3659 appctx->st0 = CLI_ST_PRINT;
3660 return 1;
3661 }
3662
Willy Tarreau2b812e22016-11-22 16:18:05 +01003663 appctx->ctx.stats.flags |= STAT_BOUND;
Willy Tarreau2b812e22016-11-22 16:18:05 +01003664 appctx->ctx.stats.type = atoi(args[3]);
3665 appctx->ctx.stats.sid = atoi(args[4]);
3666 if (strcmp(args[5], "typed") == 0)
3667 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
Simon Horman05ee2132017-01-04 09:37:25 +01003668 else if (strcmp(args[5], "json") == 0)
3669 appctx->ctx.stats.flags |= STAT_FMT_JSON;
Willy Tarreau2b812e22016-11-22 16:18:05 +01003670 }
3671 else if (strcmp(args[2], "typed") == 0)
3672 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
Simon Horman05ee2132017-01-04 09:37:25 +01003673 else if (strcmp(args[2], "json") == 0)
3674 appctx->ctx.stats.flags |= STAT_FMT_JSON;
Willy Tarreau2b812e22016-11-22 16:18:05 +01003675
Willy Tarreau2b812e22016-11-22 16:18:05 +01003676 return 0;
3677}
3678
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003679static int cli_io_handler_dump_info(struct appctx *appctx)
3680{
3681 return stats_dump_info_to_buffer(appctx->owner);
3682}
3683
Willy Tarreau2b812e22016-11-22 16:18:05 +01003684/* This I/O handler runs as an applet embedded in a stream interface. It is
3685 * used to send raw stats over a socket.
3686 */
3687static int cli_io_handler_dump_stat(struct appctx *appctx)
3688{
3689 return stats_dump_stat_to_buffer(appctx->owner, NULL);
3690}
3691
Simon Horman6f6bb382017-01-04 09:37:26 +01003692static int cli_io_handler_dump_json_schema(struct appctx *appctx)
3693{
3694 return stats_dump_json_schema_to_buffer(appctx->owner);
3695}
3696
Willy Tarreau2b812e22016-11-22 16:18:05 +01003697/* register cli keywords */
3698static struct cli_kw_list cli_kws = {{ },{
Willy Tarreau89d467c2016-11-23 11:02:40 +01003699 { { "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 +01003700 { { "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 +01003701 { { "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 +01003702 { { "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 +01003703 {{},}
3704}};
3705
William Lallemand74c24fb2016-11-21 17:18:36 +01003706struct applet http_stats_applet = {
3707 .obj_type = OBJ_TYPE_APPLET,
3708 .name = "<STATS>", /* used for logging */
3709 .fct = http_stats_io_handler,
3710 .release = NULL,
3711};
3712
Willy Tarreau2b812e22016-11-22 16:18:05 +01003713__attribute__((constructor))
3714static void __stat_init(void)
3715{
3716 cli_register_kw(&cli_kws);
3717}
3718
William Lallemand74c24fb2016-11-21 17:18:36 +01003719/*
3720 * Local variables:
3721 * c-indent-level: 8
3722 * c-basic-offset: 8
3723 * End:
3724 */