blob: 0574260d997ee926faa1b70e59663267b0a55d71 [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",
William Lallemanda7199262018-11-16 16:57:20 +0100135 [INF_UNSTOPPABLE_JOBS] = "Unstoppable Jobs",
Willy Tarreau00098ea2018-11-05 14:38:13 +0100136 [INF_LISTENERS] = "Listeners",
Willy Tarreau199ad242018-11-05 16:31:22 +0100137 [INF_ACTIVE_PEERS] = "ActivePeers",
Willy Tarreau2d372c22018-11-05 17:12:27 +0100138 [INF_CONNECTED_PEERS] = "ConnectedPeers",
Willy Tarreau13ef7732018-11-12 07:25:28 +0100139 [INF_DROPPED_LOGS] = "DroppedLogs",
Willy Tarreau0baac8c2016-11-22 16:36:53 +0100140};
141
William Lallemand74c24fb2016-11-21 17:18:36 +0100142const char *stat_field_names[ST_F_TOTAL_FIELDS] = {
143 [ST_F_PXNAME] = "pxname",
144 [ST_F_SVNAME] = "svname",
145 [ST_F_QCUR] = "qcur",
146 [ST_F_QMAX] = "qmax",
147 [ST_F_SCUR] = "scur",
148 [ST_F_SMAX] = "smax",
149 [ST_F_SLIM] = "slim",
150 [ST_F_STOT] = "stot",
151 [ST_F_BIN] = "bin",
152 [ST_F_BOUT] = "bout",
153 [ST_F_DREQ] = "dreq",
154 [ST_F_DRESP] = "dresp",
155 [ST_F_EREQ] = "ereq",
156 [ST_F_ECON] = "econ",
157 [ST_F_ERESP] = "eresp",
158 [ST_F_WRETR] = "wretr",
159 [ST_F_WREDIS] = "wredis",
160 [ST_F_STATUS] = "status",
161 [ST_F_WEIGHT] = "weight",
162 [ST_F_ACT] = "act",
163 [ST_F_BCK] = "bck",
164 [ST_F_CHKFAIL] = "chkfail",
165 [ST_F_CHKDOWN] = "chkdown",
166 [ST_F_LASTCHG] = "lastchg",
167 [ST_F_DOWNTIME] = "downtime",
168 [ST_F_QLIMIT] = "qlimit",
169 [ST_F_PID] = "pid",
170 [ST_F_IID] = "iid",
171 [ST_F_SID] = "sid",
172 [ST_F_THROTTLE] = "throttle",
173 [ST_F_LBTOT] = "lbtot",
174 [ST_F_TRACKED] = "tracked",
175 [ST_F_TYPE] = "type",
176 [ST_F_RATE] = "rate",
177 [ST_F_RATE_LIM] = "rate_lim",
178 [ST_F_RATE_MAX] = "rate_max",
179 [ST_F_CHECK_STATUS] = "check_status",
180 [ST_F_CHECK_CODE] = "check_code",
181 [ST_F_CHECK_DURATION] = "check_duration",
182 [ST_F_HRSP_1XX] = "hrsp_1xx",
183 [ST_F_HRSP_2XX] = "hrsp_2xx",
184 [ST_F_HRSP_3XX] = "hrsp_3xx",
185 [ST_F_HRSP_4XX] = "hrsp_4xx",
186 [ST_F_HRSP_5XX] = "hrsp_5xx",
187 [ST_F_HRSP_OTHER] = "hrsp_other",
188 [ST_F_HANAFAIL] = "hanafail",
189 [ST_F_REQ_RATE] = "req_rate",
190 [ST_F_REQ_RATE_MAX] = "req_rate_max",
191 [ST_F_REQ_TOT] = "req_tot",
192 [ST_F_CLI_ABRT] = "cli_abrt",
193 [ST_F_SRV_ABRT] = "srv_abrt",
194 [ST_F_COMP_IN] = "comp_in",
195 [ST_F_COMP_OUT] = "comp_out",
196 [ST_F_COMP_BYP] = "comp_byp",
197 [ST_F_COMP_RSP] = "comp_rsp",
198 [ST_F_LASTSESS] = "lastsess",
199 [ST_F_LAST_CHK] = "last_chk",
200 [ST_F_LAST_AGT] = "last_agt",
201 [ST_F_QTIME] = "qtime",
202 [ST_F_CTIME] = "ctime",
203 [ST_F_RTIME] = "rtime",
204 [ST_F_TTIME] = "ttime",
205 [ST_F_AGENT_STATUS] = "agent_status",
206 [ST_F_AGENT_CODE] = "agent_code",
207 [ST_F_AGENT_DURATION] = "agent_duration",
208 [ST_F_CHECK_DESC] = "check_desc",
209 [ST_F_AGENT_DESC] = "agent_desc",
210 [ST_F_CHECK_RISE] = "check_rise",
211 [ST_F_CHECK_FALL] = "check_fall",
212 [ST_F_CHECK_HEALTH] = "check_health",
213 [ST_F_AGENT_RISE] = "agent_rise",
214 [ST_F_AGENT_FALL] = "agent_fall",
215 [ST_F_AGENT_HEALTH] = "agent_health",
216 [ST_F_ADDR] = "addr",
217 [ST_F_COOKIE] = "cookie",
218 [ST_F_MODE] = "mode",
219 [ST_F_ALGO] = "algo",
220 [ST_F_CONN_RATE] = "conn_rate",
221 [ST_F_CONN_RATE_MAX] = "conn_rate_max",
222 [ST_F_CONN_TOT] = "conn_tot",
223 [ST_F_INTERCEPTED] = "intercepted",
224 [ST_F_DCON] = "dcon",
225 [ST_F_DSES] = "dses",
Tim Duesterhus3fd19732018-05-27 20:35:08 +0200226 [ST_F_WREW] = "wrew",
William Lallemand74c24fb2016-11-21 17:18:36 +0100227};
228
Willy Tarreau0baac8c2016-11-22 16:36:53 +0100229/* one line of info */
Christopher Faulet1bc04c72017-10-29 20:14:08 +0100230static THREAD_LOCAL struct field info[INF_TOTAL_FIELDS];
William Lallemand74c24fb2016-11-21 17:18:36 +0100231/* one line of stats */
Christopher Faulet1bc04c72017-10-29 20:14:08 +0100232static THREAD_LOCAL struct field stats[ST_F_TOTAL_FIELDS];
William Lallemand74c24fb2016-11-21 17:18:36 +0100233
234
Willy Tarreau0baac8c2016-11-22 16:36:53 +0100235
William Lallemand74c24fb2016-11-21 17:18:36 +0100236/*
237 * http_stats_io_handler()
238 * -> stats_dump_stat_to_buffer() // same as above, but used for CSV or HTML
239 * -> stats_dump_csv_header() // emits the CSV headers (same as above)
Simon Horman05ee2132017-01-04 09:37:25 +0100240 * -> stats_dump_json_header() // emits the JSON headers (same as above)
William Lallemand74c24fb2016-11-21 17:18:36 +0100241 * -> stats_dump_html_head() // emits the HTML headers
242 * -> stats_dump_html_info() // emits the equivalent of "show info" at the top
243 * -> stats_dump_proxy_to_buffer() // same as above, valid for CSV and HTML
244 * -> stats_dump_html_px_hdr()
245 * -> stats_dump_fe_stats()
246 * -> stats_dump_li_stats()
247 * -> stats_dump_sv_stats()
248 * -> stats_dump_be_stats()
249 * -> stats_dump_html_px_end()
250 * -> stats_dump_html_end() // emits HTML trailer
Simon Horman05ee2132017-01-04 09:37:25 +0100251 * -> stats_dump_json_end() // emits JSON trailer
William Lallemand74c24fb2016-11-21 17:18:36 +0100252 */
253
254
255extern const char *stat_status_codes[];
256
257/* Dumps the stats CSV header to the trash buffer which. The caller is responsible
258 * for clearing it if needed.
259 * NOTE: Some tools happen to rely on the field position instead of its name,
260 * so please only append new fields at the end, never in the middle.
261 */
262static void stats_dump_csv_header()
263{
264 int field;
265
266 chunk_appendf(&trash, "# ");
267 for (field = 0; field < ST_F_TOTAL_FIELDS; field++)
268 chunk_appendf(&trash, "%s,", stat_field_names[field]);
269
270 chunk_appendf(&trash, "\n");
271}
272
273
274/* Emits a stats field without any surrounding element and properly encoded to
275 * resist CSV output. Returns non-zero on success, 0 if the buffer is full.
276 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200277int stats_emit_raw_data_field(struct buffer *out, const struct field *f)
William Lallemand74c24fb2016-11-21 17:18:36 +0100278{
279 switch (field_format(f, 0)) {
280 case FF_EMPTY: return 1;
281 case FF_S32: return chunk_appendf(out, "%d", f->u.s32);
282 case FF_U32: return chunk_appendf(out, "%u", f->u.u32);
283 case FF_S64: return chunk_appendf(out, "%lld", (long long)f->u.s64);
284 case FF_U64: return chunk_appendf(out, "%llu", (unsigned long long)f->u.u64);
285 case FF_STR: return csv_enc_append(field_str(f, 0), 1, out) != NULL;
286 default: return chunk_appendf(out, "[INCORRECT_FIELD_TYPE_%08x]", f->type);
287 }
288}
289
290/* Emits a stats field prefixed with its type. No CSV encoding is prepared, the
291 * output is supposed to be used on its own line. Returns non-zero on success, 0
292 * if the buffer is full.
293 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200294int stats_emit_typed_data_field(struct buffer *out, const struct field *f)
William Lallemand74c24fb2016-11-21 17:18:36 +0100295{
296 switch (field_format(f, 0)) {
297 case FF_EMPTY: return 1;
298 case FF_S32: return chunk_appendf(out, "s32:%d", f->u.s32);
299 case FF_U32: return chunk_appendf(out, "u32:%u", f->u.u32);
300 case FF_S64: return chunk_appendf(out, "s64:%lld", (long long)f->u.s64);
301 case FF_U64: return chunk_appendf(out, "u64:%llu", (unsigned long long)f->u.u64);
302 case FF_STR: return chunk_appendf(out, "str:%s", field_str(f, 0));
303 default: return chunk_appendf(out, "%08x:?", f->type);
304 }
305}
306
Simon Horman05ee2132017-01-04 09:37:25 +0100307/* Limit JSON integer values to the range [-(2**53)+1, (2**53)-1] as per
308 * the recommendation for interoperable integers in section 6 of RFC 7159.
309 */
310#define JSON_INT_MAX ((1ULL << 53) - 1)
311#define JSON_INT_MIN (0 - JSON_INT_MAX)
312
313/* Emits a stats field value and its type in JSON.
314 * Returns non-zero on success, 0 on error.
315 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200316int stats_emit_json_data_field(struct buffer *out, const struct field *f)
Simon Horman05ee2132017-01-04 09:37:25 +0100317{
318 int old_len;
319 char buf[20];
320 const char *type, *value = buf, *quote = "";
321
322 switch (field_format(f, 0)) {
323 case FF_EMPTY: return 1;
324 case FF_S32: type = "\"s32\"";
325 snprintf(buf, sizeof(buf), "%d", f->u.s32);
326 break;
327 case FF_U32: type = "\"u32\"";
328 snprintf(buf, sizeof(buf), "%u", f->u.u32);
329 break;
330 case FF_S64: type = "\"s64\"";
331 if (f->u.s64 < JSON_INT_MIN || f->u.s64 > JSON_INT_MAX)
332 return 0;
333 type = "\"s64\"";
334 snprintf(buf, sizeof(buf), "%lld", (long long)f->u.s64);
335 break;
336 case FF_U64: if (f->u.u64 > JSON_INT_MAX)
337 return 0;
338 type = "\"u64\"";
339 snprintf(buf, sizeof(buf), "%llu",
340 (unsigned long long) f->u.u64);
341 break;
342 case FF_STR: type = "\"str\"";
343 value = field_str(f, 0);
344 quote = "\"";
345 break;
346 default: snprintf(buf, sizeof(buf), "%u", f->type);
347 type = buf;
348 value = "unknown";
349 quote = "\"";
350 break;
351 }
352
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200353 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100354 chunk_appendf(out, ",\"value\":{\"type\":%s,\"value\":%s%s%s}",
355 type, quote, value, quote);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200356 return !(old_len == out->data);
Simon Horman05ee2132017-01-04 09:37:25 +0100357}
358
William Lallemand74c24fb2016-11-21 17:18:36 +0100359/* Emits an encoding of the field type on 3 characters followed by a delimiter.
360 * Returns non-zero on success, 0 if the buffer is full.
361 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200362int stats_emit_field_tags(struct buffer *out, const struct field *f,
363 char delim)
William Lallemand74c24fb2016-11-21 17:18:36 +0100364{
365 char origin, nature, scope;
366
367 switch (field_origin(f, 0)) {
368 case FO_METRIC: origin = 'M'; break;
369 case FO_STATUS: origin = 'S'; break;
370 case FO_KEY: origin = 'K'; break;
371 case FO_CONFIG: origin = 'C'; break;
372 case FO_PRODUCT: origin = 'P'; break;
373 default: origin = '?'; break;
374 }
375
376 switch (field_nature(f, 0)) {
377 case FN_GAUGE: nature = 'G'; break;
378 case FN_LIMIT: nature = 'L'; break;
379 case FN_MIN: nature = 'm'; break;
380 case FN_MAX: nature = 'M'; break;
381 case FN_RATE: nature = 'R'; break;
382 case FN_COUNTER: nature = 'C'; break;
383 case FN_DURATION: nature = 'D'; break;
384 case FN_AGE: nature = 'A'; break;
385 case FN_TIME: nature = 'T'; break;
386 case FN_NAME: nature = 'N'; break;
387 case FN_OUTPUT: nature = 'O'; break;
388 case FN_AVG: nature = 'a'; break;
389 default: nature = '?'; break;
390 }
391
392 switch (field_scope(f, 0)) {
393 case FS_PROCESS: scope = 'P'; break;
394 case FS_SERVICE: scope = 'S'; break;
395 case FS_SYSTEM: scope = 's'; break;
396 case FS_CLUSTER: scope = 'C'; break;
397 default: scope = '?'; break;
398 }
399
400 return chunk_appendf(out, "%c%c%c%c", origin, nature, scope, delim);
401}
402
Simon Horman05ee2132017-01-04 09:37:25 +0100403/* Emits an encoding of the field type as JSON.
404 * Returns non-zero on success, 0 if the buffer is full.
405 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200406int stats_emit_json_field_tags(struct buffer *out, const struct field *f)
Simon Horman05ee2132017-01-04 09:37:25 +0100407{
408 const char *origin, *nature, *scope;
409 int old_len;
410
411 switch (field_origin(f, 0)) {
412 case FO_METRIC: origin = "Metric"; break;
413 case FO_STATUS: origin = "Status"; break;
414 case FO_KEY: origin = "Key"; break;
415 case FO_CONFIG: origin = "Config"; break;
416 case FO_PRODUCT: origin = "Product"; break;
417 default: origin = "Unknown"; break;
418 }
419
420 switch (field_nature(f, 0)) {
421 case FN_GAUGE: nature = "Gauge"; break;
422 case FN_LIMIT: nature = "Limit"; break;
423 case FN_MIN: nature = "Min"; break;
424 case FN_MAX: nature = "Max"; break;
425 case FN_RATE: nature = "Rate"; break;
426 case FN_COUNTER: nature = "Counter"; break;
427 case FN_DURATION: nature = "Duration"; break;
428 case FN_AGE: nature = "Age"; break;
429 case FN_TIME: nature = "Time"; break;
430 case FN_NAME: nature = "Name"; break;
431 case FN_OUTPUT: nature = "Output"; break;
432 case FN_AVG: nature = "Avg"; break;
433 default: nature = "Unknown"; break;
434 }
435
436 switch (field_scope(f, 0)) {
437 case FS_PROCESS: scope = "Process"; break;
438 case FS_SERVICE: scope = "Service"; break;
439 case FS_SYSTEM: scope = "System"; break;
440 case FS_CLUSTER: scope = "Cluster"; break;
441 default: scope = "Unknown"; break;
442 }
443
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200444 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100445 chunk_appendf(out, "\"tags\":{"
446 "\"origin\":\"%s\","
447 "\"nature\":\"%s\","
448 "\"scope\":\"%s\""
449 "}", origin, nature, scope);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200450 return !(old_len == out->data);
Simon Horman05ee2132017-01-04 09:37:25 +0100451}
William Lallemand74c24fb2016-11-21 17:18:36 +0100452
453/* Dump all fields from <stats> into <out> using CSV format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200454static int stats_dump_fields_csv(struct buffer *out,
455 const struct field *stats)
William Lallemand74c24fb2016-11-21 17:18:36 +0100456{
457 int field;
458
459 for (field = 0; field < ST_F_TOTAL_FIELDS; field++) {
460 if (!stats_emit_raw_data_field(out, &stats[field]))
461 return 0;
462 if (!chunk_strcat(out, ","))
463 return 0;
464 }
465 chunk_strcat(out, "\n");
466 return 1;
467}
468
469/* Dump all fields from <stats> into <out> using a typed "field:desc:type:value" format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200470static int stats_dump_fields_typed(struct buffer *out,
471 const struct field *stats)
William Lallemand74c24fb2016-11-21 17:18:36 +0100472{
473 int field;
474
475 for (field = 0; field < ST_F_TOTAL_FIELDS; field++) {
476 if (!stats[field].type)
477 continue;
478
479 chunk_appendf(out, "%c.%u.%u.%d.%s.%u:",
480 stats[ST_F_TYPE].u.u32 == STATS_TYPE_FE ? 'F' :
481 stats[ST_F_TYPE].u.u32 == STATS_TYPE_BE ? 'B' :
482 stats[ST_F_TYPE].u.u32 == STATS_TYPE_SO ? 'L' :
483 stats[ST_F_TYPE].u.u32 == STATS_TYPE_SV ? 'S' :
484 '?',
485 stats[ST_F_IID].u.u32, stats[ST_F_SID].u.u32,
486 field, stat_field_names[field], stats[ST_F_PID].u.u32);
487
488 if (!stats_emit_field_tags(out, &stats[field], ':'))
489 return 0;
490 if (!stats_emit_typed_data_field(out, &stats[field]))
491 return 0;
492 if (!chunk_strcat(out, "\n"))
493 return 0;
494 }
495 return 1;
496}
497
Simon Horman05ee2132017-01-04 09:37:25 +0100498/* Dump all fields from <stats> into <out> using the "show info json" format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200499static int stats_dump_json_info_fields(struct buffer *out,
Simon Horman05ee2132017-01-04 09:37:25 +0100500 const struct field *info)
501{
502 int field;
503 int started = 0;
504
505 if (!chunk_strcat(out, "["))
506 return 0;
507
508 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
509 int old_len;
510
511 if (!field_format(info, field))
512 continue;
513
514 if (started && !chunk_strcat(out, ","))
515 goto err;
516 started = 1;
517
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200518 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100519 chunk_appendf(out,
520 "{\"field\":{\"pos\":%d,\"name\":\"%s\"},"
521 "\"processNum\":%u,",
522 field, info_field_names[field],
523 info[INF_PROCESS_NUM].u.u32);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200524 if (old_len == out->data)
Simon Horman05ee2132017-01-04 09:37:25 +0100525 goto err;
526
527 if (!stats_emit_json_field_tags(out, &info[field]))
528 goto err;
529
530 if (!stats_emit_json_data_field(out, &info[field]))
531 goto err;
532
533 if (!chunk_strcat(out, "}"))
534 goto err;
535 }
536
537 if (!chunk_strcat(out, "]"))
538 goto err;
539 return 1;
540
541err:
542 chunk_reset(out);
543 chunk_appendf(out, "{\"errorStr\":\"output buffer too short\"}");
544 return 0;
545}
546
547/* Dump all fields from <stats> into <out> using a typed "field:desc:type:value" format */
Willy Tarreau83061a82018-07-13 11:56:34 +0200548static int stats_dump_fields_json(struct buffer *out,
549 const struct field *stats,
Simon Horman05ee2132017-01-04 09:37:25 +0100550 int first_stat)
551{
552 int field;
553 int started = 0;
554
555 if (!first_stat && !chunk_strcat(out, ","))
556 return 0;
557 if (!chunk_strcat(out, "["))
558 return 0;
559
560 for (field = 0; field < ST_F_TOTAL_FIELDS; field++) {
561 const char *obj_type;
562 int old_len;
563
564 if (!stats[field].type)
565 continue;
566
567 if (started && !chunk_strcat(out, ","))
568 goto err;
569 started = 1;
570
571 switch (stats[ST_F_TYPE].u.u32) {
572 case STATS_TYPE_FE: obj_type = "Frontend"; break;
573 case STATS_TYPE_BE: obj_type = "Backend"; break;
574 case STATS_TYPE_SO: obj_type = "Listener"; break;
575 case STATS_TYPE_SV: obj_type = "Server"; break;
576 default: obj_type = "Unknown"; break;
577 }
578
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200579 old_len = out->data;
Simon Horman05ee2132017-01-04 09:37:25 +0100580 chunk_appendf(out,
581 "{"
582 "\"objType\":\"%s\","
583 "\"proxyId\":%d,"
584 "\"id\":%d,"
585 "\"field\":{\"pos\":%d,\"name\":\"%s\"},"
586 "\"processNum\":%u,",
587 obj_type, stats[ST_F_IID].u.u32,
588 stats[ST_F_SID].u.u32, field,
589 stat_field_names[field], stats[ST_F_PID].u.u32);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200590 if (old_len == out->data)
Simon Horman05ee2132017-01-04 09:37:25 +0100591 goto err;
592
593 if (!stats_emit_json_field_tags(out, &stats[field]))
594 goto err;
595
596 if (!stats_emit_json_data_field(out, &stats[field]))
597 goto err;
598
599 if (!chunk_strcat(out, "}"))
600 goto err;
601 }
602
603 if (!chunk_strcat(out, "]"))
604 goto err;
605
606 return 1;
607
608err:
609 chunk_reset(out);
610 if (!first_stat)
611 chunk_strcat(out, ",");
612 chunk_appendf(out, "{\"errorStr\":\"output buffer too short\"}");
613 return 0;
614}
615
William Lallemand74c24fb2016-11-21 17:18:36 +0100616/* Dump all fields from <stats> into <out> using the HTML format. A column is
617 * reserved for the checkbox is ST_SHOWADMIN is set in <flags>. Some extra info
618 * are provided if ST_SHLGNDS is present in <flags>.
619 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200620static int stats_dump_fields_html(struct buffer *out,
621 const struct field *stats,
622 unsigned int flags)
William Lallemand74c24fb2016-11-21 17:18:36 +0100623{
Willy Tarreau83061a82018-07-13 11:56:34 +0200624 struct buffer src;
William Lallemand74c24fb2016-11-21 17:18:36 +0100625
626 if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_FE) {
627 chunk_appendf(out,
628 /* name, queue */
629 "<tr class=\"frontend\">");
630
631 if (flags & ST_SHOWADMIN) {
632 /* Column sub-heading for Enable or Disable server */
633 chunk_appendf(out, "<td></td>");
634 }
635
636 chunk_appendf(out,
637 "<td class=ac>"
638 "<a name=\"%s/Frontend\"></a>"
639 "<a class=lfsb href=\"#%s/Frontend\">Frontend</a></td>"
640 "<td colspan=3></td>"
641 "",
642 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_PXNAME));
643
644 chunk_appendf(out,
645 /* sessions rate : current */
646 "<td><u>%s<div class=tips><table class=det>"
647 "<tr><th>Current connection rate:</th><td>%s/s</td></tr>"
648 "<tr><th>Current session rate:</th><td>%s/s</td></tr>"
649 "",
650 U2H(stats[ST_F_RATE].u.u32),
651 U2H(stats[ST_F_CONN_RATE].u.u32),
652 U2H(stats[ST_F_RATE].u.u32));
653
654 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
655 chunk_appendf(out,
656 "<tr><th>Current request rate:</th><td>%s/s</td></tr>",
657 U2H(stats[ST_F_REQ_RATE].u.u32));
658
659 chunk_appendf(out,
660 "</table></div></u></td>"
661 /* sessions rate : max */
662 "<td><u>%s<div class=tips><table class=det>"
663 "<tr><th>Max connection rate:</th><td>%s/s</td></tr>"
664 "<tr><th>Max session rate:</th><td>%s/s</td></tr>"
665 "",
666 U2H(stats[ST_F_RATE_MAX].u.u32),
667 U2H(stats[ST_F_CONN_RATE_MAX].u.u32),
668 U2H(stats[ST_F_RATE_MAX].u.u32));
669
670 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
671 chunk_appendf(out,
672 "<tr><th>Max request rate:</th><td>%s/s</td></tr>",
673 U2H(stats[ST_F_REQ_RATE_MAX].u.u32));
674
675 chunk_appendf(out,
676 "</table></div></u></td>"
677 /* sessions rate : limit */
678 "<td>%s</td>",
679 LIM2A(stats[ST_F_RATE_LIM].u.u32, "-"));
680
681 chunk_appendf(out,
682 /* sessions: current, max, limit, total */
683 "<td>%s</td><td>%s</td><td>%s</td>"
684 "<td><u>%s<div class=tips><table class=det>"
685 "<tr><th>Cum. connections:</th><td>%s</td></tr>"
686 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
687 "",
688 U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SMAX].u.u32), U2H(stats[ST_F_SLIM].u.u32),
689 U2H(stats[ST_F_STOT].u.u64),
690 U2H(stats[ST_F_CONN_TOT].u.u64),
691 U2H(stats[ST_F_STOT].u.u64));
692
693 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
694 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
695 chunk_appendf(out,
696 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
697 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
698 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
699 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
700 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
701 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
702 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
703 "<tr><th>- other responses:</th><td>%s</td></tr>"
704 "<tr><th>Intercepted requests:</th><td>%s</td></tr>"
Willy Tarreau1b0f85e2018-05-28 15:12:40 +0200705 "<tr><th>Failed hdr rewrites:</th><td>%s</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +0100706 "",
707 U2H(stats[ST_F_REQ_TOT].u.u64),
708 U2H(stats[ST_F_HRSP_1XX].u.u64),
709 U2H(stats[ST_F_HRSP_2XX].u.u64),
710 U2H(stats[ST_F_COMP_RSP].u.u64),
711 stats[ST_F_HRSP_2XX].u.u64 ?
712 (int)(100 * stats[ST_F_COMP_RSP].u.u64 / stats[ST_F_HRSP_2XX].u.u64) : 0,
713 U2H(stats[ST_F_HRSP_3XX].u.u64),
714 U2H(stats[ST_F_HRSP_4XX].u.u64),
715 U2H(stats[ST_F_HRSP_5XX].u.u64),
716 U2H(stats[ST_F_HRSP_OTHER].u.u64),
Willy Tarreau1b0f85e2018-05-28 15:12:40 +0200717 U2H(stats[ST_F_INTERCEPTED].u.u64),
718 U2H(stats[ST_F_WREW].u.u64));
William Lallemand74c24fb2016-11-21 17:18:36 +0100719 }
720
721 chunk_appendf(out,
722 "</table></div></u></td>"
723 /* sessions: lbtot, lastsess */
724 "<td></td><td></td>"
725 /* bytes : in */
726 "<td>%s</td>"
727 "",
728 U2H(stats[ST_F_BIN].u.u64));
729
730 chunk_appendf(out,
731 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
732 "<td>%s%s<div class=tips><table class=det>"
733 "<tr><th>Response bytes in:</th><td>%s</td></tr>"
734 "<tr><th>Compression in:</th><td>%s</td></tr>"
735 "<tr><th>Compression out:</th><td>%s</td><td>(%d%%)</td></tr>"
736 "<tr><th>Compression bypass:</th><td>%s</td></tr>"
737 "<tr><th>Total bytes saved:</th><td>%s</td><td>(%d%%)</td></tr>"
738 "</table></div>%s</td>",
739 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "<u>":"",
740 U2H(stats[ST_F_BOUT].u.u64),
741 U2H(stats[ST_F_BOUT].u.u64),
742 U2H(stats[ST_F_COMP_IN].u.u64),
743 U2H(stats[ST_F_COMP_OUT].u.u64),
744 stats[ST_F_COMP_IN].u.u64 ? (int)(stats[ST_F_COMP_OUT].u.u64 * 100 / stats[ST_F_COMP_IN].u.u64) : 0,
745 U2H(stats[ST_F_COMP_BYP].u.u64),
746 U2H(stats[ST_F_COMP_IN].u.u64 - stats[ST_F_COMP_OUT].u.u64),
747 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,
748 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "</u>":"");
749
750 chunk_appendf(out,
751 /* denied: req, resp */
752 "<td>%s</td><td>%s</td>"
753 /* errors : request, connect, response */
754 "<td>%s</td><td></td><td></td>"
755 /* warnings: retries, redispatches */
756 "<td></td><td></td>"
757 /* server status : reflect frontend status */
758 "<td class=ac>%s</td>"
759 /* rest of server: nothing */
760 "<td class=ac colspan=8></td></tr>"
761 "",
762 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
763 U2H(stats[ST_F_EREQ].u.u64),
764 field_str(stats, ST_F_STATUS));
765 }
766 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_SO) {
767 chunk_appendf(out, "<tr class=socket>");
768 if (flags & ST_SHOWADMIN) {
769 /* Column sub-heading for Enable or Disable server */
770 chunk_appendf(out, "<td></td>");
771 }
772
773 chunk_appendf(out,
774 /* frontend name, listener name */
775 "<td class=ac><a name=\"%s/+%s\"></a>%s"
776 "<a class=lfsb href=\"#%s/+%s\">%s</a>"
777 "",
778 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME),
779 (flags & ST_SHLGNDS)?"<u>":"",
780 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME), field_str(stats, ST_F_SVNAME));
781
782 if (flags & ST_SHLGNDS) {
783 chunk_appendf(out, "<div class=tips>");
784
785 if (isdigit(*field_str(stats, ST_F_ADDR)))
786 chunk_appendf(out, "IPv4: %s, ", field_str(stats, ST_F_ADDR));
787 else if (*field_str(stats, ST_F_ADDR) == '[')
788 chunk_appendf(out, "IPv6: %s, ", field_str(stats, ST_F_ADDR));
789 else if (*field_str(stats, ST_F_ADDR))
790 chunk_appendf(out, "%s, ", field_str(stats, ST_F_ADDR));
791
792 /* id */
793 chunk_appendf(out, "id: %d</div>", stats[ST_F_SID].u.u32);
794 }
795
796 chunk_appendf(out,
797 /* queue */
798 "%s</td><td colspan=3></td>"
799 /* sessions rate: current, max, limit */
800 "<td colspan=3>&nbsp;</td>"
801 /* sessions: current, max, limit, total, lbtot, lastsess */
802 "<td>%s</td><td>%s</td><td>%s</td>"
803 "<td>%s</td><td>&nbsp;</td><td>&nbsp;</td>"
804 /* bytes: in, out */
805 "<td>%s</td><td>%s</td>"
806 "",
807 (flags & ST_SHLGNDS)?"</u>":"",
808 U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SMAX].u.u32), U2H(stats[ST_F_SLIM].u.u32),
809 U2H(stats[ST_F_STOT].u.u64), U2H(stats[ST_F_BIN].u.u64), U2H(stats[ST_F_BOUT].u.u64));
810
811 chunk_appendf(out,
812 /* denied: req, resp */
813 "<td>%s</td><td>%s</td>"
814 /* errors: request, connect, response */
815 "<td>%s</td><td></td><td></td>"
816 /* warnings: retries, redispatches */
817 "<td></td><td></td>"
818 /* server status: reflect listener status */
819 "<td class=ac>%s</td>"
820 /* rest of server: nothing */
821 "<td class=ac colspan=8></td></tr>"
822 "",
823 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
824 U2H(stats[ST_F_EREQ].u.u64),
825 field_str(stats, ST_F_STATUS));
826 }
827 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_SV) {
828 const char *style;
829
830 /* determine the style to use depending on the server's state,
831 * its health and weight. There isn't a 1-to-1 mapping between
832 * state and styles for the cases where the server is (still)
833 * up. The reason is that we don't want to report nolb and
834 * drain with the same color.
835 */
836
837 if (strcmp(field_str(stats, ST_F_STATUS), "DOWN") == 0 ||
838 strcmp(field_str(stats, ST_F_STATUS), "DOWN (agent)") == 0) {
839 style = "down";
840 }
841 else if (strcmp(field_str(stats, ST_F_STATUS), "DOWN ") == 0) {
842 style = "going_up";
843 }
844 else if (strcmp(field_str(stats, ST_F_STATUS), "NOLB ") == 0) {
845 style = "going_down";
846 }
847 else if (strcmp(field_str(stats, ST_F_STATUS), "NOLB") == 0) {
848 style = "nolb";
849 }
850 else if (strcmp(field_str(stats, ST_F_STATUS), "no check") == 0) {
851 style = "no_check";
852 }
853 else if (!stats[ST_F_CHKFAIL].type ||
854 stats[ST_F_CHECK_HEALTH].u.u32 == stats[ST_F_CHECK_RISE].u.u32 + stats[ST_F_CHECK_FALL].u.u32 - 1) {
855 /* no check or max health = UP */
856 if (stats[ST_F_WEIGHT].u.u32)
857 style = "up";
858 else
859 style = "draining";
860 }
861 else {
862 style = "going_down";
863 }
864
865 if (memcmp(field_str(stats, ST_F_STATUS), "MAINT", 5) == 0)
866 chunk_appendf(out, "<tr class=\"maintain\">");
867 else
868 chunk_appendf(out,
869 "<tr class=\"%s_%s\">",
870 (stats[ST_F_BCK].u.u32) ? "backup" : "active", style);
871
872
873 if (flags & ST_SHOWADMIN)
874 chunk_appendf(out,
David Harrigand3db35a2016-12-30 12:12:49 +0000875 "<td><input class='%s-checkbox' type=\"checkbox\" name=\"s\" value=\"%s\"></td>",
876 field_str(stats, ST_F_PXNAME),
William Lallemand74c24fb2016-11-21 17:18:36 +0100877 field_str(stats, ST_F_SVNAME));
878
879 chunk_appendf(out,
880 "<td class=ac><a name=\"%s/%s\"></a>%s"
881 "<a class=lfsb href=\"#%s/%s\">%s</a>"
882 "",
883 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME),
884 (flags & ST_SHLGNDS) ? "<u>" : "",
885 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME), field_str(stats, ST_F_SVNAME));
886
887 if (flags & ST_SHLGNDS) {
888 chunk_appendf(out, "<div class=tips>");
889
890 if (isdigit(*field_str(stats, ST_F_ADDR)))
891 chunk_appendf(out, "IPv4: %s, ", field_str(stats, ST_F_ADDR));
892 else if (*field_str(stats, ST_F_ADDR) == '[')
893 chunk_appendf(out, "IPv6: %s, ", field_str(stats, ST_F_ADDR));
894 else if (*field_str(stats, ST_F_ADDR))
895 chunk_appendf(out, "%s, ", field_str(stats, ST_F_ADDR));
896
897 /* id */
898 chunk_appendf(out, "id: %d", stats[ST_F_SID].u.u32);
899
900 /* cookie */
901 if (stats[ST_F_COOKIE].type) {
902 chunk_appendf(out, ", cookie: '");
903 chunk_initstr(&src, field_str(stats, ST_F_COOKIE));
904 chunk_htmlencode(out, &src);
905 chunk_appendf(out, "'");
906 }
907
908 chunk_appendf(out, "</div>");
909 }
910
911 chunk_appendf(out,
912 /* queue : current, max, limit */
913 "%s</td><td>%s</td><td>%s</td><td>%s</td>"
914 /* sessions rate : current, max, limit */
915 "<td>%s</td><td>%s</td><td></td>"
916 "",
917 (flags & ST_SHLGNDS) ? "</u>" : "",
918 U2H(stats[ST_F_QCUR].u.u32), U2H(stats[ST_F_QMAX].u.u32), LIM2A(stats[ST_F_QLIMIT].u.u32, "-"),
919 U2H(stats[ST_F_RATE].u.u32), U2H(stats[ST_F_RATE_MAX].u.u32));
920
921 chunk_appendf(out,
922 /* sessions: current, max, limit, total */
923 "<td>%s</td><td>%s</td><td>%s</td>"
924 "<td><u>%s<div class=tips><table class=det>"
925 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
926 "",
927 U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SMAX].u.u32), LIM2A(stats[ST_F_SLIM].u.u32, "-"),
928 U2H(stats[ST_F_STOT].u.u64),
929 U2H(stats[ST_F_STOT].u.u64));
930
931 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
932 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
933 unsigned long long tot;
934
935 tot = stats[ST_F_HRSP_OTHER].u.u64;
936 tot += stats[ST_F_HRSP_1XX].u.u64;
937 tot += stats[ST_F_HRSP_2XX].u.u64;
938 tot += stats[ST_F_HRSP_3XX].u.u64;
939 tot += stats[ST_F_HRSP_4XX].u.u64;
940 tot += stats[ST_F_HRSP_5XX].u.u64;
941
942 chunk_appendf(out,
943 "<tr><th>Cum. HTTP responses:</th><td>%s</td></tr>"
944 "<tr><th>- HTTP 1xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
945 "<tr><th>- HTTP 2xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
946 "<tr><th>- HTTP 3xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
947 "<tr><th>- HTTP 4xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
948 "<tr><th>- HTTP 5xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
949 "<tr><th>- other responses:</th><td>%s</td><td>(%d%%)</td></tr>"
Willy Tarreau1b0f85e2018-05-28 15:12:40 +0200950 "<tr><th>Failed hdr rewrites:</th><td>%s</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +0100951 "",
952 U2H(tot),
953 U2H(stats[ST_F_HRSP_1XX].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_1XX].u.u64 / tot) : 0,
954 U2H(stats[ST_F_HRSP_2XX].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_2XX].u.u64 / tot) : 0,
955 U2H(stats[ST_F_HRSP_3XX].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_3XX].u.u64 / tot) : 0,
956 U2H(stats[ST_F_HRSP_4XX].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_4XX].u.u64 / tot) : 0,
957 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 +0200958 U2H(stats[ST_F_HRSP_OTHER].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_OTHER].u.u64 / tot) : 0,
959 U2H(stats[ST_F_WREW].u.u64));
William Lallemand74c24fb2016-11-21 17:18:36 +0100960 }
961
962 chunk_appendf(out, "<tr><th colspan=3>Avg over last 1024 success. conn.</th></tr>");
963 chunk_appendf(out, "<tr><th>- Queue time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_QTIME].u.u32));
964 chunk_appendf(out, "<tr><th>- Connect time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_CTIME].u.u32));
965 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
966 chunk_appendf(out, "<tr><th>- Response time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_RTIME].u.u32));
967 chunk_appendf(out, "<tr><th>- Total time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_TTIME].u.u32));
968
969 chunk_appendf(out,
970 "</table></div></u></td>"
971 /* sessions: lbtot, last */
972 "<td>%s</td><td>%s</td>",
973 U2H(stats[ST_F_LBTOT].u.u64),
974 human_time(stats[ST_F_LASTSESS].u.s32, 1));
975
976 chunk_appendf(out,
977 /* bytes : in, out */
978 "<td>%s</td><td>%s</td>"
979 /* denied: req, resp */
980 "<td></td><td>%s</td>"
981 /* errors : request, connect */
982 "<td></td><td>%s</td>"
983 /* errors : response */
984 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
985 /* warnings: retries, redispatches */
986 "<td>%lld</td><td>%lld</td>"
987 "",
988 U2H(stats[ST_F_BIN].u.u64), U2H(stats[ST_F_BOUT].u.u64),
989 U2H(stats[ST_F_DRESP].u.u64),
990 U2H(stats[ST_F_ECON].u.u64),
991 U2H(stats[ST_F_ERESP].u.u64),
992 (long long)stats[ST_F_CLI_ABRT].u.u64,
993 (long long)stats[ST_F_SRV_ABRT].u.u64,
994 (long long)stats[ST_F_WRETR].u.u64,
995 (long long)stats[ST_F_WREDIS].u.u64);
996
997 /* status, last change */
998 chunk_appendf(out, "<td class=ac>");
999
1000 /* FIXME!!!!
1001 * LASTCHG should contain the last change for *this* server and must be computed
1002 * properly above, as was done below, ie: this server if maint, otherwise ref server
1003 * if tracking. Note that ref is either local or remote depending on tracking.
1004 */
1005
1006
1007 if (memcmp(field_str(stats, ST_F_STATUS), "MAINT", 5) == 0) {
1008 chunk_appendf(out, "%s MAINT", human_time(stats[ST_F_LASTCHG].u.u32, 1));
1009 }
1010 else if (memcmp(field_str(stats, ST_F_STATUS), "no check", 5) == 0) {
1011 chunk_strcat(out, "<i>no check</i>");
1012 }
1013 else {
1014 chunk_appendf(out, "%s %s", human_time(stats[ST_F_LASTCHG].u.u32, 1), field_str(stats, ST_F_STATUS));
1015 if (memcmp(field_str(stats, ST_F_STATUS), "DOWN", 4) == 0) {
1016 if (stats[ST_F_CHECK_HEALTH].u.u32)
1017 chunk_strcat(out, " &uarr;");
1018 }
1019 else if (stats[ST_F_CHECK_HEALTH].u.u32 < stats[ST_F_CHECK_RISE].u.u32 + stats[ST_F_CHECK_FALL].u.u32 - 1)
1020 chunk_strcat(out, " &darr;");
1021 }
1022
1023 if (memcmp(field_str(stats, ST_F_STATUS), "DOWN", 4) == 0 &&
1024 stats[ST_F_AGENT_STATUS].type && !stats[ST_F_AGENT_HEALTH].u.u32) {
1025 chunk_appendf(out,
1026 "</td><td class=ac><u> %s",
1027 field_str(stats, ST_F_AGENT_STATUS));
1028
1029 if (stats[ST_F_AGENT_CODE].type)
1030 chunk_appendf(out, "/%d", stats[ST_F_AGENT_CODE].u.u32);
1031
1032 if (stats[ST_F_AGENT_DURATION].type)
1033 chunk_appendf(out, " in %lums", (long)stats[ST_F_AGENT_DURATION].u.u64);
1034
1035 chunk_appendf(out, "<div class=tips>%s", field_str(stats, ST_F_AGENT_DESC));
1036
1037 if (*field_str(stats, ST_F_LAST_AGT)) {
1038 chunk_appendf(out, ": ");
1039 chunk_initstr(&src, field_str(stats, ST_F_LAST_AGT));
1040 chunk_htmlencode(out, &src);
1041 }
1042 chunk_appendf(out, "</div></u>");
1043 }
1044 else if (stats[ST_F_CHECK_STATUS].type) {
1045 chunk_appendf(out,
1046 "</td><td class=ac><u> %s",
1047 field_str(stats, ST_F_CHECK_STATUS));
1048
1049 if (stats[ST_F_CHECK_CODE].type)
1050 chunk_appendf(out, "/%d", stats[ST_F_CHECK_CODE].u.u32);
1051
1052 if (stats[ST_F_CHECK_DURATION].type)
1053 chunk_appendf(out, " in %lums", (long)stats[ST_F_CHECK_DURATION].u.u64);
1054
1055 chunk_appendf(out, "<div class=tips>%s", field_str(stats, ST_F_CHECK_DESC));
1056
1057 if (*field_str(stats, ST_F_LAST_CHK)) {
1058 chunk_appendf(out, ": ");
1059 chunk_initstr(&src, field_str(stats, ST_F_LAST_CHK));
1060 chunk_htmlencode(out, &src);
1061 }
1062 chunk_appendf(out, "</div></u>");
1063 }
1064 else
1065 chunk_appendf(out, "</td><td>");
1066
1067 chunk_appendf(out,
1068 /* weight */
1069 "</td><td class=ac>%d</td>"
1070 /* act, bck */
1071 "<td class=ac>%s</td><td class=ac>%s</td>"
1072 "",
1073 stats[ST_F_WEIGHT].u.u32,
1074 stats[ST_F_BCK].u.u32 ? "-" : "Y",
1075 stats[ST_F_BCK].u.u32 ? "Y" : "-");
1076
1077 /* check failures: unique, fatal, down time */
1078 if (strcmp(field_str(stats, ST_F_STATUS), "MAINT (resolution)") == 0) {
1079 chunk_appendf(out, "<td class=ac colspan=3>resolution</td>");
1080 }
1081 else if (stats[ST_F_CHKFAIL].type) {
1082 chunk_appendf(out, "<td><u>%lld", (long long)stats[ST_F_CHKFAIL].u.u64);
1083
1084 if (stats[ST_F_HANAFAIL].type)
1085 chunk_appendf(out, "/%lld", (long long)stats[ST_F_HANAFAIL].u.u64);
1086
1087 chunk_appendf(out,
1088 "<div class=tips>Failed Health Checks%s</div></u></td>"
1089 "<td>%lld</td><td>%s</td>"
1090 "",
1091 stats[ST_F_HANAFAIL].type ? "/Health Analyses" : "",
1092 (long long)stats[ST_F_CHKDOWN].u.u64, human_time(stats[ST_F_DOWNTIME].u.u32, 1));
1093 }
1094 else if (strcmp(field_str(stats, ST_F_STATUS), "MAINT") != 0 && field_format(stats, ST_F_TRACKED) == FF_STR) {
1095 /* tracking a server (hence inherited maint would appear as "MAINT (via...)" */
1096 chunk_appendf(out,
1097 "<td class=ac colspan=3><a class=lfsb href=\"#%s\">via %s</a></td>",
1098 field_str(stats, ST_F_TRACKED), field_str(stats, ST_F_TRACKED));
1099 }
1100 else
1101 chunk_appendf(out, "<td colspan=3></td>");
1102
1103 /* throttle */
1104 if (stats[ST_F_THROTTLE].type)
1105 chunk_appendf(out, "<td class=ac>%d %%</td></tr>\n", stats[ST_F_THROTTLE].u.u32);
1106 else
1107 chunk_appendf(out, "<td class=ac>-</td></tr>\n");
1108 }
1109 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_BE) {
1110 chunk_appendf(out, "<tr class=\"backend\">");
1111 if (flags & ST_SHOWADMIN) {
1112 /* Column sub-heading for Enable or Disable server */
1113 chunk_appendf(out, "<td></td>");
1114 }
1115 chunk_appendf(out,
1116 "<td class=ac>"
1117 /* name */
1118 "%s<a name=\"%s/Backend\"></a>"
1119 "<a class=lfsb href=\"#%s/Backend\">Backend</a>"
1120 "",
1121 (flags & ST_SHLGNDS)?"<u>":"",
1122 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_PXNAME));
1123
1124 if (flags & ST_SHLGNDS) {
1125 /* balancing */
1126 chunk_appendf(out, "<div class=tips>balancing: %s",
1127 field_str(stats, ST_F_ALGO));
1128
1129 /* cookie */
1130 if (stats[ST_F_COOKIE].type) {
1131 chunk_appendf(out, ", cookie: '");
1132 chunk_initstr(&src, field_str(stats, ST_F_COOKIE));
1133 chunk_htmlencode(out, &src);
1134 chunk_appendf(out, "'");
1135 }
1136 chunk_appendf(out, "</div>");
1137 }
1138
1139 chunk_appendf(out,
1140 "%s</td>"
1141 /* queue : current, max */
1142 "<td>%s</td><td>%s</td><td></td>"
1143 /* sessions rate : current, max, limit */
1144 "<td>%s</td><td>%s</td><td></td>"
1145 "",
1146 (flags & ST_SHLGNDS)?"</u>":"",
1147 U2H(stats[ST_F_QCUR].u.u32), U2H(stats[ST_F_QMAX].u.u32),
1148 U2H(stats[ST_F_RATE].u.u32), U2H(stats[ST_F_RATE_MAX].u.u32));
1149
1150 chunk_appendf(out,
1151 /* sessions: current, max, limit, total */
1152 "<td>%s</td><td>%s</td><td>%s</td>"
1153 "<td><u>%s<div class=tips><table class=det>"
1154 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
1155 "",
Willy Tarreau8e0f1752016-12-12 15:07:29 +01001156 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 +01001157 U2H(stats[ST_F_STOT].u.u64),
1158 U2H(stats[ST_F_STOT].u.u64));
1159
1160 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
1161 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
1162 chunk_appendf(out,
1163 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
1164 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
1165 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
1166 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
1167 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
1168 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
1169 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
1170 "<tr><th>- other responses:</th><td>%s</td></tr>"
Willy Tarreau1b0f85e2018-05-28 15:12:40 +02001171 "<tr><th>Failed hdr rewrites:</th><td>%s</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001172 "<tr><th colspan=3>Avg over last 1024 success. conn.</th></tr>"
1173 "",
1174 U2H(stats[ST_F_REQ_TOT].u.u64),
1175 U2H(stats[ST_F_HRSP_1XX].u.u64),
1176 U2H(stats[ST_F_HRSP_2XX].u.u64),
1177 U2H(stats[ST_F_COMP_RSP].u.u64),
1178 stats[ST_F_HRSP_2XX].u.u64 ?
1179 (int)(100 * stats[ST_F_COMP_RSP].u.u64 / stats[ST_F_HRSP_2XX].u.u64) : 0,
1180 U2H(stats[ST_F_HRSP_3XX].u.u64),
1181 U2H(stats[ST_F_HRSP_4XX].u.u64),
1182 U2H(stats[ST_F_HRSP_5XX].u.u64),
Willy Tarreau1b0f85e2018-05-28 15:12:40 +02001183 U2H(stats[ST_F_WREW].u.u64),
Willy Tarreauae9bea02016-11-25 14:44:52 +01001184 U2H(stats[ST_F_HRSP_OTHER].u.u64));
William Lallemand74c24fb2016-11-21 17:18:36 +01001185 }
1186
1187 chunk_appendf(out, "<tr><th>- Queue time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_QTIME].u.u32));
1188 chunk_appendf(out, "<tr><th>- Connect time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_CTIME].u.u32));
1189 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
1190 chunk_appendf(out, "<tr><th>- Response time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_RTIME].u.u32));
1191 chunk_appendf(out, "<tr><th>- Total time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_TTIME].u.u32));
1192
1193 chunk_appendf(out,
1194 "</table></div></u></td>"
1195 /* sessions: lbtot, last */
1196 "<td>%s</td><td>%s</td>"
1197 /* bytes: in */
1198 "<td>%s</td>"
1199 "",
1200 U2H(stats[ST_F_LBTOT].u.u64),
1201 human_time(stats[ST_F_LASTSESS].u.s32, 1),
1202 U2H(stats[ST_F_BIN].u.u64));
1203
1204 chunk_appendf(out,
1205 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
1206 "<td>%s%s<div class=tips><table class=det>"
1207 "<tr><th>Response bytes in:</th><td>%s</td></tr>"
1208 "<tr><th>Compression in:</th><td>%s</td></tr>"
1209 "<tr><th>Compression out:</th><td>%s</td><td>(%d%%)</td></tr>"
1210 "<tr><th>Compression bypass:</th><td>%s</td></tr>"
1211 "<tr><th>Total bytes saved:</th><td>%s</td><td>(%d%%)</td></tr>"
1212 "</table></div>%s</td>",
1213 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "<u>":"",
1214 U2H(stats[ST_F_BOUT].u.u64),
1215 U2H(stats[ST_F_BOUT].u.u64),
1216 U2H(stats[ST_F_COMP_IN].u.u64),
1217 U2H(stats[ST_F_COMP_OUT].u.u64),
1218 stats[ST_F_COMP_IN].u.u64 ? (int)(stats[ST_F_COMP_OUT].u.u64 * 100 / stats[ST_F_COMP_IN].u.u64) : 0,
1219 U2H(stats[ST_F_COMP_BYP].u.u64),
1220 U2H(stats[ST_F_COMP_IN].u.u64 - stats[ST_F_COMP_OUT].u.u64),
1221 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,
1222 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "</u>":"");
1223
1224 chunk_appendf(out,
1225 /* denied: req, resp */
1226 "<td>%s</td><td>%s</td>"
1227 /* errors : request, connect */
1228 "<td></td><td>%s</td>"
1229 /* errors : response */
1230 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
1231 /* warnings: retries, redispatches */
1232 "<td>%lld</td><td>%lld</td>"
1233 /* backend status: reflect backend status (up/down): we display UP
1234 * if the backend has known working servers or if it has no server at
1235 * all (eg: for stats). Then we display the total weight, number of
1236 * active and backups. */
1237 "<td class=ac>%s %s</td><td class=ac>&nbsp;</td><td class=ac>%d</td>"
1238 "<td class=ac>%d</td><td class=ac>%d</td>"
1239 "",
1240 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
1241 U2H(stats[ST_F_ECON].u.u64),
1242 U2H(stats[ST_F_ERESP].u.u64),
1243 (long long)stats[ST_F_CLI_ABRT].u.u64,
1244 (long long)stats[ST_F_SRV_ABRT].u.u64,
1245 (long long)stats[ST_F_WRETR].u.u64, (long long)stats[ST_F_WREDIS].u.u64,
1246 human_time(stats[ST_F_LASTCHG].u.u32, 1),
1247 strcmp(field_str(stats, ST_F_STATUS), "DOWN") ? field_str(stats, ST_F_STATUS) : "<font color=\"red\"><b>DOWN</b></font>",
1248 stats[ST_F_WEIGHT].u.u32,
1249 stats[ST_F_ACT].u.u32, stats[ST_F_BCK].u.u32);
1250
1251 chunk_appendf(out,
1252 /* rest of backend: nothing, down transitions, total downtime, throttle */
1253 "<td class=ac>&nbsp;</td><td>%d</td>"
1254 "<td>%s</td>"
1255 "<td></td>"
1256 "</tr>",
1257 stats[ST_F_CHKDOWN].u.u32,
1258 stats[ST_F_DOWNTIME].type ? human_time(stats[ST_F_DOWNTIME].u.u32, 1) : "&nbsp;");
1259 }
1260 return 1;
1261}
1262
1263int stats_dump_one_line(const struct field *stats, unsigned int flags, struct proxy *px, struct appctx *appctx)
1264{
Simon Horman05ee2132017-01-04 09:37:25 +01001265 int ret;
1266
William Lallemand74c24fb2016-11-21 17:18:36 +01001267 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN))
1268 flags |= ST_SHOWADMIN;
1269
1270 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
Simon Horman05ee2132017-01-04 09:37:25 +01001271 ret = stats_dump_fields_html(&trash, stats, flags);
William Lallemand74c24fb2016-11-21 17:18:36 +01001272 else if (appctx->ctx.stats.flags & STAT_FMT_TYPED)
Simon Horman05ee2132017-01-04 09:37:25 +01001273 ret = stats_dump_fields_typed(&trash, stats);
1274 else if (appctx->ctx.stats.flags & STAT_FMT_JSON)
1275 ret = stats_dump_fields_json(&trash, stats,
1276 !(appctx->ctx.stats.flags &
1277 STAT_STARTED));
William Lallemand74c24fb2016-11-21 17:18:36 +01001278 else
Simon Horman05ee2132017-01-04 09:37:25 +01001279 ret = stats_dump_fields_csv(&trash, stats);
1280
1281 if (ret)
1282 appctx->ctx.stats.flags |= STAT_STARTED;
1283
1284 return ret;
William Lallemand74c24fb2016-11-21 17:18:36 +01001285}
1286
1287/* Fill <stats> with the frontend statistics. <stats> is
1288 * preallocated array of length <len>. The length of the array
1289 * must be at least ST_F_TOTAL_FIELDS. If this length is less then
1290 * this value, the function returns 0, otherwise, it returns 1.
1291 */
1292int stats_fill_fe_stats(struct proxy *px, struct field *stats, int len)
1293{
1294 if (len < ST_F_TOTAL_FIELDS)
1295 return 0;
1296
1297 memset(stats, 0, sizeof(*stats) * len);
1298
1299 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1300 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, "FRONTEND");
1301 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1302 stats[ST_F_SCUR] = mkf_u32(0, px->feconn);
1303 stats[ST_F_SMAX] = mkf_u32(FN_MAX, px->fe_counters.conn_max);
1304 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, px->maxconn);
1305 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, px->fe_counters.cum_sess);
1306 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, px->fe_counters.bytes_in);
1307 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, px->fe_counters.bytes_out);
1308 stats[ST_F_DREQ] = mkf_u64(FN_COUNTER, px->fe_counters.denied_req);
1309 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, px->fe_counters.denied_resp);
1310 stats[ST_F_EREQ] = mkf_u64(FN_COUNTER, px->fe_counters.failed_req);
1311 stats[ST_F_DCON] = mkf_u64(FN_COUNTER, px->fe_counters.denied_conn);
1312 stats[ST_F_DSES] = mkf_u64(FN_COUNTER, px->fe_counters.denied_sess);
1313 stats[ST_F_STATUS] = mkf_str(FO_STATUS, px->state == PR_STREADY ? "OPEN" : px->state == PR_STFULL ? "FULL" : "STOP");
1314 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
1315 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1316 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, 0);
1317 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_FE);
1318 stats[ST_F_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_sess_per_sec));
1319 stats[ST_F_RATE_LIM] = mkf_u32(FO_CONFIG|FN_LIMIT, px->fe_sps_lim);
1320 stats[ST_F_RATE_MAX] = mkf_u32(FN_MAX, px->fe_counters.sps_max);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001321 stats[ST_F_WREW] = mkf_u64(FN_COUNTER, px->fe_counters.failed_rewrites);
William Lallemand74c24fb2016-11-21 17:18:36 +01001322
1323 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
1324 if (px->mode == PR_MODE_HTTP) {
1325 stats[ST_F_HRSP_1XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[1]);
1326 stats[ST_F_HRSP_2XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[2]);
1327 stats[ST_F_HRSP_3XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[3]);
1328 stats[ST_F_HRSP_4XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[4]);
1329 stats[ST_F_HRSP_5XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[5]);
1330 stats[ST_F_HRSP_OTHER] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[0]);
1331 stats[ST_F_INTERCEPTED] = mkf_u64(FN_COUNTER, px->fe_counters.intercepted_req);
1332 }
1333
1334 /* requests : req_rate, req_rate_max, req_tot, */
1335 stats[ST_F_REQ_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_req_per_sec));
1336 stats[ST_F_REQ_RATE_MAX] = mkf_u32(FN_MAX, px->fe_counters.p.http.rps_max);
1337 stats[ST_F_REQ_TOT] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cum_req);
1338
1339 /* compression: in, out, bypassed, responses */
1340 stats[ST_F_COMP_IN] = mkf_u64(FN_COUNTER, px->fe_counters.comp_in);
1341 stats[ST_F_COMP_OUT] = mkf_u64(FN_COUNTER, px->fe_counters.comp_out);
1342 stats[ST_F_COMP_BYP] = mkf_u64(FN_COUNTER, px->fe_counters.comp_byp);
1343 stats[ST_F_COMP_RSP] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.comp_rsp);
1344
1345 /* connections : conn_rate, conn_rate_max, conn_tot, conn_max */
1346 stats[ST_F_CONN_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_conn_per_sec));
1347 stats[ST_F_CONN_RATE_MAX] = mkf_u32(FN_MAX, px->fe_counters.cps_max);
1348 stats[ST_F_CONN_TOT] = mkf_u64(FN_COUNTER, px->fe_counters.cum_conn);
1349
1350 return 1;
1351}
1352
1353/* Dumps a frontend's line to the trash for the current proxy <px> and uses
1354 * the state from stream interface <si>. The caller is responsible for clearing
1355 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
1356 */
1357static int stats_dump_fe_stats(struct stream_interface *si, struct proxy *px)
1358{
1359 struct appctx *appctx = __objt_appctx(si->end);
1360
1361 if (!(px->cap & PR_CAP_FE))
1362 return 0;
1363
1364 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_FE)))
1365 return 0;
1366
1367 if (!stats_fill_fe_stats(px, stats, ST_F_TOTAL_FIELDS))
1368 return 0;
1369
1370 return stats_dump_one_line(stats, 0, px, appctx);
1371}
1372
1373/* Fill <stats> with the listener statistics. <stats> is
1374 * preallocated array of length <len>. The length of the array
1375 * must be at least ST_F_TOTAL_FIELDS. If this length is less
1376 * then this value, the function returns 0, otherwise, it
1377 * returns 1. <flags> can take the value ST_SHLGNDS.
1378 */
1379int stats_fill_li_stats(struct proxy *px, struct listener *l, int flags,
1380 struct field *stats, int len)
1381{
Willy Tarreau83061a82018-07-13 11:56:34 +02001382 struct buffer *out = get_trash_chunk();
William Lallemand74c24fb2016-11-21 17:18:36 +01001383
1384 if (len < ST_F_TOTAL_FIELDS)
1385 return 0;
1386
1387 if (!l->counters)
1388 return 0;
1389
1390 chunk_reset(out);
1391 memset(stats, 0, sizeof(*stats) * len);
1392
1393 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1394 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, l->name);
1395 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1396 stats[ST_F_SCUR] = mkf_u32(0, l->nbconn);
1397 stats[ST_F_SMAX] = mkf_u32(FN_MAX, l->counters->conn_max);
1398 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, l->maxconn);
1399 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, l->counters->cum_conn);
1400 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, l->counters->bytes_in);
1401 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, l->counters->bytes_out);
1402 stats[ST_F_DREQ] = mkf_u64(FN_COUNTER, l->counters->denied_req);
1403 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, l->counters->denied_resp);
1404 stats[ST_F_EREQ] = mkf_u64(FN_COUNTER, l->counters->failed_req);
1405 stats[ST_F_DCON] = mkf_u64(FN_COUNTER, l->counters->denied_conn);
1406 stats[ST_F_DSES] = mkf_u64(FN_COUNTER, l->counters->denied_sess);
1407 stats[ST_F_STATUS] = mkf_str(FO_STATUS, (l->nbconn < l->maxconn) ? (l->state == LI_LIMITED) ? "WAITING" : "OPEN" : "FULL");
1408 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
1409 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1410 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, l->luid);
1411 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_SO);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001412 stats[ST_F_WREW] = mkf_u64(FN_COUNTER, l->counters->failed_rewrites);
William Lallemand74c24fb2016-11-21 17:18:36 +01001413
1414 if (flags & ST_SHLGNDS) {
1415 char str[INET6_ADDRSTRLEN];
1416 int port;
1417
1418 port = get_host_port(&l->addr);
1419 switch (addr_to_str(&l->addr, str, sizeof(str))) {
1420 case AF_INET:
1421 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1422 chunk_appendf(out, "%s:%d", str, port);
1423 break;
1424 case AF_INET6:
1425 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1426 chunk_appendf(out, "[%s]:%d", str, port);
1427 break;
1428 case AF_UNIX:
1429 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, "unix");
1430 break;
1431 case -1:
1432 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1433 chunk_strcat(out, strerror(errno));
1434 break;
1435 default: /* address family not supported */
1436 break;
1437 }
1438 }
1439
1440 return 1;
1441}
1442
1443/* Dumps a line for listener <l> and proxy <px> to the trash and uses the state
1444 * from stream interface <si>, and stats flags <flags>. The caller is responsible
1445 * for clearing the trash if needed. Returns non-zero if it emits anything, zero
1446 * otherwise.
1447 */
1448static int stats_dump_li_stats(struct stream_interface *si, struct proxy *px, struct listener *l, int flags)
1449{
1450 struct appctx *appctx = __objt_appctx(si->end);
1451
1452 if (!stats_fill_li_stats(px, l, flags, stats, ST_F_TOTAL_FIELDS))
1453 return 0;
1454
1455 return stats_dump_one_line(stats, flags, px, appctx);
1456}
1457
1458enum srv_stats_state {
1459 SRV_STATS_STATE_DOWN = 0,
1460 SRV_STATS_STATE_DOWN_AGENT,
1461 SRV_STATS_STATE_GOING_UP,
1462 SRV_STATS_STATE_UP_GOING_DOWN,
1463 SRV_STATS_STATE_UP,
1464 SRV_STATS_STATE_NOLB_GOING_DOWN,
1465 SRV_STATS_STATE_NOLB,
1466 SRV_STATS_STATE_DRAIN_GOING_DOWN,
1467 SRV_STATS_STATE_DRAIN,
1468 SRV_STATS_STATE_DRAIN_AGENT,
1469 SRV_STATS_STATE_NO_CHECK,
1470
1471 SRV_STATS_STATE_COUNT, /* Must be last */
1472};
1473
1474static const char *srv_hlt_st[SRV_STATS_STATE_COUNT] = {
1475 [SRV_STATS_STATE_DOWN] = "DOWN",
1476 [SRV_STATS_STATE_DOWN_AGENT] = "DOWN (agent)",
1477 [SRV_STATS_STATE_GOING_UP] = "DOWN %d/%d",
1478 [SRV_STATS_STATE_UP_GOING_DOWN] = "UP %d/%d",
1479 [SRV_STATS_STATE_UP] = "UP",
1480 [SRV_STATS_STATE_NOLB_GOING_DOWN] = "NOLB %d/%d",
1481 [SRV_STATS_STATE_NOLB] = "NOLB",
1482 [SRV_STATS_STATE_DRAIN_GOING_DOWN] = "DRAIN %d/%d",
1483 [SRV_STATS_STATE_DRAIN] = "DRAIN",
1484 [SRV_STATS_STATE_DRAIN_AGENT] = "DRAIN (agent)",
1485 [SRV_STATS_STATE_NO_CHECK] = "no check"
1486};
1487
1488/* Fill <stats> with the server statistics. <stats> is
1489 * preallocated array of length <len>. The length of the array
1490 * must be at least ST_F_TOTAL_FIELDS. If this length is less
1491 * then this value, the function returns 0, otherwise, it
1492 * returns 1. <flags> can take the value ST_SHLGNDS.
1493 */
1494int stats_fill_sv_stats(struct proxy *px, struct server *sv, int flags,
1495 struct field *stats, int len)
1496{
1497 struct server *via, *ref;
1498 char str[INET6_ADDRSTRLEN];
Willy Tarreau83061a82018-07-13 11:56:34 +02001499 struct buffer *out = get_trash_chunk();
William Lallemand74c24fb2016-11-21 17:18:36 +01001500 enum srv_stats_state state;
1501 char *fld_status;
1502
1503 if (len < ST_F_TOTAL_FIELDS)
1504 return 0;
1505
1506 memset(stats, 0, sizeof(*stats) * len);
1507
1508 /* we have "via" which is the tracked server as described in the configuration,
1509 * and "ref" which is the checked server and the end of the chain.
1510 */
1511 via = sv->track ? sv->track : sv;
1512 ref = via;
1513 while (ref->track)
1514 ref = ref->track;
1515
Emeric Brun52a91d32017-08-31 14:41:55 +02001516 if (sv->cur_state == SRV_ST_RUNNING || sv->cur_state == SRV_ST_STARTING) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001517 if ((ref->check.state & CHK_ST_ENABLED) &&
1518 (ref->check.health < ref->check.rise + ref->check.fall - 1)) {
1519 state = SRV_STATS_STATE_UP_GOING_DOWN;
1520 } else {
1521 state = SRV_STATS_STATE_UP;
1522 }
1523
Emeric Brun52a91d32017-08-31 14:41:55 +02001524 if (sv->cur_admin & SRV_ADMF_DRAIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001525 if (ref->agent.state & CHK_ST_ENABLED)
1526 state = SRV_STATS_STATE_DRAIN_AGENT;
1527 else if (state == SRV_STATS_STATE_UP_GOING_DOWN)
1528 state = SRV_STATS_STATE_DRAIN_GOING_DOWN;
1529 else
1530 state = SRV_STATS_STATE_DRAIN;
1531 }
1532
1533 if (state == SRV_STATS_STATE_UP && !(ref->check.state & CHK_ST_ENABLED)) {
1534 state = SRV_STATS_STATE_NO_CHECK;
1535 }
1536 }
Emeric Brun52a91d32017-08-31 14:41:55 +02001537 else if (sv->cur_state == SRV_ST_STOPPING) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001538 if ((!(sv->check.state & CHK_ST_ENABLED) && !sv->track) ||
1539 (ref->check.health == ref->check.rise + ref->check.fall - 1)) {
1540 state = SRV_STATS_STATE_NOLB;
1541 } else {
1542 state = SRV_STATS_STATE_NOLB_GOING_DOWN;
1543 }
1544 }
1545 else { /* stopped */
1546 if ((ref->agent.state & CHK_ST_ENABLED) && !ref->agent.health) {
1547 state = SRV_STATS_STATE_DOWN_AGENT;
1548 } else if ((ref->check.state & CHK_ST_ENABLED) && !ref->check.health) {
1549 state = SRV_STATS_STATE_DOWN; /* DOWN */
1550 } else if ((ref->agent.state & CHK_ST_ENABLED) || (ref->check.state & CHK_ST_ENABLED)) {
1551 state = SRV_STATS_STATE_GOING_UP;
1552 } else {
1553 state = SRV_STATS_STATE_DOWN; /* DOWN, unchecked */
1554 }
1555 }
1556
1557 chunk_reset(out);
1558
1559 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1560 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, sv->id);
1561 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1562 stats[ST_F_QCUR] = mkf_u32(0, sv->nbpend);
1563 stats[ST_F_QMAX] = mkf_u32(FN_MAX, sv->counters.nbpend_max);
1564 stats[ST_F_SCUR] = mkf_u32(0, sv->cur_sess);
1565 stats[ST_F_SMAX] = mkf_u32(FN_MAX, sv->counters.cur_sess_max);
1566
1567 if (sv->maxconn)
1568 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, sv->maxconn);
1569
1570 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, sv->counters.cum_sess);
1571 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, sv->counters.bytes_in);
1572 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, sv->counters.bytes_out);
1573 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, sv->counters.failed_secu);
1574 stats[ST_F_ECON] = mkf_u64(FN_COUNTER, sv->counters.failed_conns);
1575 stats[ST_F_ERESP] = mkf_u64(FN_COUNTER, sv->counters.failed_resp);
1576 stats[ST_F_WRETR] = mkf_u64(FN_COUNTER, sv->counters.retries);
1577 stats[ST_F_WREDIS] = mkf_u64(FN_COUNTER, sv->counters.redispatches);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001578 stats[ST_F_WREW] = mkf_u64(FN_COUNTER, sv->counters.failed_rewrites);
William Lallemand74c24fb2016-11-21 17:18:36 +01001579
1580 /* status */
1581 fld_status = chunk_newstr(out);
Emeric Brun52a91d32017-08-31 14:41:55 +02001582 if (sv->cur_admin & SRV_ADMF_RMAINT)
William Lallemand74c24fb2016-11-21 17:18:36 +01001583 chunk_appendf(out, "MAINT (resolution)");
Emeric Brun52a91d32017-08-31 14:41:55 +02001584 else if (sv->cur_admin & SRV_ADMF_IMAINT)
William Lallemand74c24fb2016-11-21 17:18:36 +01001585 chunk_appendf(out, "MAINT (via %s/%s)", via->proxy->id, via->id);
Emeric Brun52a91d32017-08-31 14:41:55 +02001586 else if (sv->cur_admin & SRV_ADMF_MAINT)
William Lallemand74c24fb2016-11-21 17:18:36 +01001587 chunk_appendf(out, "MAINT");
1588 else
1589 chunk_appendf(out,
1590 srv_hlt_st[state],
Emeric Brun52a91d32017-08-31 14:41:55 +02001591 (ref->cur_state != SRV_ST_STOPPED) ? (ref->check.health - ref->check.rise + 1) : (ref->check.health),
1592 (ref->cur_state != SRV_ST_STOPPED) ? (ref->check.fall) : (ref->check.rise));
William Lallemand74c24fb2016-11-21 17:18:36 +01001593
1594 stats[ST_F_STATUS] = mkf_str(FO_STATUS, fld_status);
1595 stats[ST_F_LASTCHG] = mkf_u32(FN_AGE, now.tv_sec - sv->last_change);
Emeric Brun52a91d32017-08-31 14:41:55 +02001596 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 +01001597 stats[ST_F_ACT] = mkf_u32(FO_STATUS, (sv->flags & SRV_F_BACKUP) ? 0 : 1);
1598 stats[ST_F_BCK] = mkf_u32(FO_STATUS, (sv->flags & SRV_F_BACKUP) ? 1 : 0);
1599
1600 /* check failures: unique, fatal; last change, total downtime */
1601 if (sv->check.state & CHK_ST_ENABLED) {
1602 stats[ST_F_CHKFAIL] = mkf_u64(FN_COUNTER, sv->counters.failed_checks);
1603 stats[ST_F_CHKDOWN] = mkf_u64(FN_COUNTER, sv->counters.down_trans);
1604 stats[ST_F_DOWNTIME] = mkf_u32(FN_COUNTER, srv_downtime(sv));
1605 }
1606
1607 if (sv->maxqueue)
1608 stats[ST_F_QLIMIT] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->maxqueue);
1609
1610 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
1611 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1612 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, sv->puid);
1613
Emeric Brun52a91d32017-08-31 14:41:55 +02001614 if (sv->cur_state == SRV_ST_STARTING && !server_is_draining(sv))
William Lallemand74c24fb2016-11-21 17:18:36 +01001615 stats[ST_F_THROTTLE] = mkf_u32(FN_AVG, server_throttle_rate(sv));
1616
1617 stats[ST_F_LBTOT] = mkf_u64(FN_COUNTER, sv->counters.cum_lbconn);
1618
1619 if (sv->track) {
1620 char *fld_track = chunk_newstr(out);
1621
1622 chunk_appendf(out, "%s/%s", sv->track->proxy->id, sv->track->id);
1623 stats[ST_F_TRACKED] = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, fld_track);
1624 }
1625
1626 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_SV);
1627 stats[ST_F_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&sv->sess_per_sec));
1628 stats[ST_F_RATE_MAX] = mkf_u32(FN_MAX, sv->counters.sps_max);
1629
1630 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) {
1631 const char *fld_chksts;
1632
1633 fld_chksts = chunk_newstr(out);
1634 chunk_strcat(out, "* "); // for check in progress
1635 chunk_strcat(out, get_check_status_info(sv->check.status));
1636 if (!(sv->check.state & CHK_ST_INPROGRESS))
1637 fld_chksts += 2; // skip "* "
1638 stats[ST_F_CHECK_STATUS] = mkf_str(FN_OUTPUT, fld_chksts);
1639
1640 if (sv->check.status >= HCHK_STATUS_L57DATA)
1641 stats[ST_F_CHECK_CODE] = mkf_u32(FN_OUTPUT, sv->check.code);
1642
1643 if (sv->check.status >= HCHK_STATUS_CHECKED)
1644 stats[ST_F_CHECK_DURATION] = mkf_u64(FN_DURATION, sv->check.duration);
1645
1646 stats[ST_F_CHECK_DESC] = mkf_str(FN_OUTPUT, get_check_status_description(sv->check.status));
1647 stats[ST_F_LAST_CHK] = mkf_str(FN_OUTPUT, sv->check.desc);
1648 stats[ST_F_CHECK_RISE] = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.rise);
1649 stats[ST_F_CHECK_FALL] = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.fall);
1650 stats[ST_F_CHECK_HEALTH] = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.health);
1651 }
1652
1653 if ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) {
1654 const char *fld_chksts;
1655
1656 fld_chksts = chunk_newstr(out);
1657 chunk_strcat(out, "* "); // for check in progress
1658 chunk_strcat(out, get_check_status_info(sv->agent.status));
1659 if (!(sv->agent.state & CHK_ST_INPROGRESS))
1660 fld_chksts += 2; // skip "* "
1661 stats[ST_F_AGENT_STATUS] = mkf_str(FN_OUTPUT, fld_chksts);
1662
1663 if (sv->agent.status >= HCHK_STATUS_L57DATA)
1664 stats[ST_F_AGENT_CODE] = mkf_u32(FN_OUTPUT, sv->agent.code);
1665
1666 if (sv->agent.status >= HCHK_STATUS_CHECKED)
1667 stats[ST_F_AGENT_DURATION] = mkf_u64(FN_DURATION, sv->agent.duration);
1668
1669 stats[ST_F_AGENT_DESC] = mkf_str(FN_OUTPUT, get_check_status_description(sv->agent.status));
1670 stats[ST_F_LAST_AGT] = mkf_str(FN_OUTPUT, sv->agent.desc);
1671 stats[ST_F_AGENT_RISE] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.rise);
1672 stats[ST_F_AGENT_FALL] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.fall);
1673 stats[ST_F_AGENT_HEALTH] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.health);
1674 }
1675
1676 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
1677 if (px->mode == PR_MODE_HTTP) {
1678 stats[ST_F_HRSP_1XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[1]);
1679 stats[ST_F_HRSP_2XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[2]);
1680 stats[ST_F_HRSP_3XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[3]);
1681 stats[ST_F_HRSP_4XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[4]);
1682 stats[ST_F_HRSP_5XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[5]);
1683 stats[ST_F_HRSP_OTHER] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[0]);
1684 }
1685
1686 if (ref->observe)
1687 stats[ST_F_HANAFAIL] = mkf_u64(FN_COUNTER, sv->counters.failed_hana);
1688
1689 stats[ST_F_CLI_ABRT] = mkf_u64(FN_COUNTER, sv->counters.cli_aborts);
1690 stats[ST_F_SRV_ABRT] = mkf_u64(FN_COUNTER, sv->counters.srv_aborts);
1691 stats[ST_F_LASTSESS] = mkf_s32(FN_AGE, srv_lastsession(sv));
1692
1693 stats[ST_F_QTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.q_time, TIME_STATS_SAMPLES));
1694 stats[ST_F_CTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.c_time, TIME_STATS_SAMPLES));
1695 stats[ST_F_RTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.d_time, TIME_STATS_SAMPLES));
1696 stats[ST_F_TTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.t_time, TIME_STATS_SAMPLES));
1697
1698 if (flags & ST_SHLGNDS) {
1699 switch (addr_to_str(&sv->addr, str, sizeof(str))) {
1700 case AF_INET:
1701 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
Willy Tarreau04276f32017-01-06 17:41:29 +01001702 chunk_appendf(out, "%s:%d", str, sv->svc_port);
William Lallemand74c24fb2016-11-21 17:18:36 +01001703 break;
1704 case AF_INET6:
1705 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
Willy Tarreau04276f32017-01-06 17:41:29 +01001706 chunk_appendf(out, "[%s]:%d", str, sv->svc_port);
William Lallemand74c24fb2016-11-21 17:18:36 +01001707 break;
1708 case AF_UNIX:
1709 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, "unix");
1710 break;
1711 case -1:
1712 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1713 chunk_strcat(out, strerror(errno));
1714 break;
1715 default: /* address family not supported */
1716 break;
1717 }
1718
1719 if (sv->cookie)
1720 stats[ST_F_COOKIE] = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, sv->cookie);
1721 }
1722
1723 return 1;
1724}
1725
1726/* Dumps a line for server <sv> and proxy <px> to the trash and uses the state
1727 * from stream interface <si>, stats flags <flags>, and server state <state>.
1728 * The caller is responsible for clearing the trash if needed. Returns non-zero
1729 * if it emits anything, zero otherwise.
1730 */
1731static int stats_dump_sv_stats(struct stream_interface *si, struct proxy *px, int flags, struct server *sv)
1732{
1733 struct appctx *appctx = __objt_appctx(si->end);
1734
1735 if (!stats_fill_sv_stats(px, sv, flags, stats, ST_F_TOTAL_FIELDS))
1736 return 0;
1737
1738 return stats_dump_one_line(stats, flags, px, appctx);
1739}
1740
1741/* Fill <stats> with the backend statistics. <stats> is
1742 * preallocated array of length <len>. The length of the array
1743 * must be at least ST_F_TOTAL_FIELDS. If this length is less
1744 * then this value, the function returns 0, otherwise, it
1745 * returns 1. <flags> can take the value ST_SHLGNDS.
1746 */
1747int stats_fill_be_stats(struct proxy *px, int flags, struct field *stats, int len)
1748{
1749 if (len < ST_F_TOTAL_FIELDS)
1750 return 0;
1751
1752 memset(stats, 0, sizeof(*stats) * len);
1753
1754 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1755 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, "BACKEND");
1756 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1757 stats[ST_F_QCUR] = mkf_u32(0, px->nbpend);
1758 stats[ST_F_QMAX] = mkf_u32(FN_MAX, px->be_counters.nbpend_max);
Thierry FOURNIER0ff98a42016-12-19 16:50:42 +01001759 stats[ST_F_SCUR] = mkf_u32(0, px->beconn);
William Lallemand74c24fb2016-11-21 17:18:36 +01001760 stats[ST_F_SMAX] = mkf_u32(FN_MAX, px->be_counters.conn_max);
1761 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, px->fullconn);
1762 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, px->be_counters.cum_conn);
1763 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, px->be_counters.bytes_in);
1764 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, px->be_counters.bytes_out);
1765 stats[ST_F_DREQ] = mkf_u64(FN_COUNTER, px->be_counters.denied_req);
1766 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, px->be_counters.denied_resp);
1767 stats[ST_F_ECON] = mkf_u64(FN_COUNTER, px->be_counters.failed_conns);
1768 stats[ST_F_ERESP] = mkf_u64(FN_COUNTER, px->be_counters.failed_resp);
1769 stats[ST_F_WRETR] = mkf_u64(FN_COUNTER, px->be_counters.retries);
1770 stats[ST_F_WREDIS] = mkf_u64(FN_COUNTER, px->be_counters.redispatches);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001771 stats[ST_F_WREW] = mkf_u64(FN_COUNTER, px->be_counters.failed_rewrites);
William Lallemand74c24fb2016-11-21 17:18:36 +01001772 stats[ST_F_STATUS] = mkf_str(FO_STATUS, (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" : "DOWN");
1773 stats[ST_F_WEIGHT] = mkf_u32(FN_AVG, (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv);
1774 stats[ST_F_ACT] = mkf_u32(0, px->srv_act);
1775 stats[ST_F_BCK] = mkf_u32(0, px->srv_bck);
1776 stats[ST_F_CHKDOWN] = mkf_u64(FN_COUNTER, px->down_trans);
1777 stats[ST_F_LASTCHG] = mkf_u32(FN_AGE, now.tv_sec - px->last_change);
1778 if (px->srv)
1779 stats[ST_F_DOWNTIME] = mkf_u32(FN_COUNTER, be_downtime(px));
1780
1781 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
1782 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1783 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, 0);
1784 stats[ST_F_LBTOT] = mkf_u64(FN_COUNTER, px->be_counters.cum_lbconn);
1785 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_BE);
1786 stats[ST_F_RATE] = mkf_u32(0, read_freq_ctr(&px->be_sess_per_sec));
1787 stats[ST_F_RATE_MAX] = mkf_u32(0, px->be_counters.sps_max);
1788
1789 if (flags & ST_SHLGNDS) {
1790 if (px->cookie_name)
1791 stats[ST_F_COOKIE] = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, px->cookie_name);
1792 stats[ST_F_ALGO] = mkf_str(FO_CONFIG|FS_SERVICE, backend_lb_algo_str(px->lbprm.algo & BE_LB_ALGO));
1793 }
1794
1795 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
1796 if (px->mode == PR_MODE_HTTP) {
1797 stats[ST_F_REQ_TOT] = mkf_u64(FN_COUNTER, px->be_counters.p.http.cum_req);
1798 stats[ST_F_HRSP_1XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[1]);
1799 stats[ST_F_HRSP_2XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[2]);
1800 stats[ST_F_HRSP_3XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[3]);
1801 stats[ST_F_HRSP_4XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[4]);
1802 stats[ST_F_HRSP_5XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[5]);
1803 stats[ST_F_HRSP_OTHER] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[0]);
William Lallemand74c24fb2016-11-21 17:18:36 +01001804 }
1805
1806 stats[ST_F_CLI_ABRT] = mkf_u64(FN_COUNTER, px->be_counters.cli_aborts);
1807 stats[ST_F_SRV_ABRT] = mkf_u64(FN_COUNTER, px->be_counters.srv_aborts);
1808
1809 /* compression: in, out, bypassed, responses */
1810 stats[ST_F_COMP_IN] = mkf_u64(FN_COUNTER, px->be_counters.comp_in);
1811 stats[ST_F_COMP_OUT] = mkf_u64(FN_COUNTER, px->be_counters.comp_out);
1812 stats[ST_F_COMP_BYP] = mkf_u64(FN_COUNTER, px->be_counters.comp_byp);
1813 stats[ST_F_COMP_RSP] = mkf_u64(FN_COUNTER, px->be_counters.p.http.comp_rsp);
1814 stats[ST_F_LASTSESS] = mkf_s32(FN_AGE, be_lastsession(px));
1815
1816 stats[ST_F_QTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.q_time, TIME_STATS_SAMPLES));
1817 stats[ST_F_CTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.c_time, TIME_STATS_SAMPLES));
1818 stats[ST_F_RTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.d_time, TIME_STATS_SAMPLES));
1819 stats[ST_F_TTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.t_time, TIME_STATS_SAMPLES));
1820
1821 return 1;
1822}
1823
1824/* Dumps a line for backend <px> to the trash for and uses the state from stream
1825 * interface <si> and stats flags <flags>. The caller is responsible for clearing
1826 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
1827 */
1828static int stats_dump_be_stats(struct stream_interface *si, struct proxy *px, int flags)
1829{
1830 struct appctx *appctx = __objt_appctx(si->end);
1831
1832 if (!(px->cap & PR_CAP_BE))
1833 return 0;
1834
1835 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_BE)))
1836 return 0;
1837
1838 if (!stats_fill_be_stats(px, flags, stats, ST_F_TOTAL_FIELDS))
1839 return 0;
1840
1841 return stats_dump_one_line(stats, flags, px, appctx);
1842}
1843
1844/* Dumps the HTML table header for proxy <px> to the trash for and uses the state from
1845 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
1846 * for clearing the trash if needed.
1847 */
1848static void stats_dump_html_px_hdr(struct stream_interface *si, struct proxy *px, struct uri_auth *uri)
1849{
1850 struct appctx *appctx = __objt_appctx(si->end);
1851 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
1852
1853 if (px->cap & PR_CAP_BE && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
1854 /* A form to enable/disable this proxy servers */
1855
1856 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
1857 scope_txt[0] = 0;
1858 if (appctx->ctx.stats.scope_len) {
1859 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau89faf5d2018-06-07 18:16:48 +02001860 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 +01001861 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
1862 }
1863
1864 chunk_appendf(&trash,
1865 "<form method=\"post\">");
1866 }
1867
1868 /* print a new table */
1869 chunk_appendf(&trash,
1870 "<table class=\"tbl\" width=\"100%%\">\n"
1871 "<tr class=\"titre\">"
1872 "<th class=\"pxname\" width=\"10%%\">");
1873
1874 chunk_appendf(&trash,
1875 "<a name=\"%s\"></a>%s"
1876 "<a class=px href=\"#%s\">%s</a>",
1877 px->id,
1878 (uri->flags & ST_SHLGNDS) ? "<u>":"",
1879 px->id, px->id);
1880
1881 if (uri->flags & ST_SHLGNDS) {
1882 /* cap, mode, id */
1883 chunk_appendf(&trash, "<div class=tips>cap: %s, mode: %s, id: %d",
1884 proxy_cap_str(px->cap), proxy_mode_str(px->mode),
1885 px->uuid);
1886 chunk_appendf(&trash, "</div>");
1887 }
1888
1889 chunk_appendf(&trash,
1890 "%s</th>"
1891 "<th class=\"%s\" width=\"90%%\">%s</th>"
1892 "</tr>\n"
1893 "</table>\n"
1894 "<table class=\"tbl\" width=\"100%%\">\n"
1895 "<tr class=\"titre\">",
1896 (uri->flags & ST_SHLGNDS) ? "</u>":"",
1897 px->desc ? "desc" : "empty", px->desc ? px->desc : "");
1898
1899 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
1900 /* Column heading for Enable or Disable server */
David Harrigand3db35a2016-12-30 12:12:49 +00001901 chunk_appendf(&trash,
1902 "<th rowspan=2 width=1><input type=\"checkbox\" \
1903 onclick=\"for(c in document.getElementsByClassName('%s-checkbox')) \
1904 document.getElementsByClassName('%s-checkbox').item(c).checked = this.checked\"></th>",
1905 px->id,
1906 px->id);
William Lallemand74c24fb2016-11-21 17:18:36 +01001907 }
1908
1909 chunk_appendf(&trash,
1910 "<th rowspan=2></th>"
1911 "<th colspan=3>Queue</th>"
1912 "<th colspan=3>Session rate</th><th colspan=6>Sessions</th>"
1913 "<th colspan=2>Bytes</th><th colspan=2>Denied</th>"
1914 "<th colspan=3>Errors</th><th colspan=2>Warnings</th>"
1915 "<th colspan=9>Server</th>"
1916 "</tr>\n"
1917 "<tr class=\"titre\">"
1918 "<th>Cur</th><th>Max</th><th>Limit</th>"
1919 "<th>Cur</th><th>Max</th><th>Limit</th><th>Cur</th><th>Max</th>"
1920 "<th>Limit</th><th>Total</th><th>LbTot</th><th>Last</th><th>In</th><th>Out</th>"
1921 "<th>Req</th><th>Resp</th><th>Req</th><th>Conn</th>"
1922 "<th>Resp</th><th>Retr</th><th>Redis</th>"
1923 "<th>Status</th><th>LastChk</th><th>Wght</th><th>Act</th>"
1924 "<th>Bck</th><th>Chk</th><th>Dwn</th><th>Dwntme</th>"
1925 "<th>Thrtle</th>\n"
1926 "</tr>");
1927}
1928
1929/* Dumps the HTML table trailer for proxy <px> to the trash for and uses the state from
1930 * stream interface <si>. The caller is responsible for clearing the trash if needed.
1931 */
1932static void stats_dump_html_px_end(struct stream_interface *si, struct proxy *px)
1933{
1934 struct appctx *appctx = __objt_appctx(si->end);
1935 chunk_appendf(&trash, "</table>");
1936
1937 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
1938 /* close the form used to enable/disable this proxy servers */
1939 chunk_appendf(&trash,
1940 "Choose the action to perform on the checked servers : "
1941 "<select name=action>"
1942 "<option value=\"\"></option>"
1943 "<option value=\"ready\">Set state to READY</option>"
1944 "<option value=\"drain\">Set state to DRAIN</option>"
1945 "<option value=\"maint\">Set state to MAINT</option>"
1946 "<option value=\"dhlth\">Health: disable checks</option>"
1947 "<option value=\"ehlth\">Health: enable checks</option>"
1948 "<option value=\"hrunn\">Health: force UP</option>"
1949 "<option value=\"hnolb\">Health: force NOLB</option>"
1950 "<option value=\"hdown\">Health: force DOWN</option>"
1951 "<option value=\"dagent\">Agent: disable checks</option>"
1952 "<option value=\"eagent\">Agent: enable checks</option>"
1953 "<option value=\"arunn\">Agent: force UP</option>"
1954 "<option value=\"adown\">Agent: force DOWN</option>"
1955 "<option value=\"shutdown\">Kill Sessions</option>"
1956 "</select>"
1957 "<input type=\"hidden\" name=\"b\" value=\"#%d\">"
1958 "&nbsp;<input type=\"submit\" value=\"Apply\">"
1959 "</form>",
1960 px->uuid);
1961 }
1962
1963 chunk_appendf(&trash, "<p>\n");
1964}
1965
1966/*
1967 * Dumps statistics for a proxy. The output is sent to the stream interface's
1968 * input buffer. Returns 0 if it had to stop dumping data because of lack of
1969 * buffer space, or non-zero if everything completed. This function is used
1970 * both by the CLI and the HTTP entry points, and is able to dump the output
1971 * in HTML or CSV formats. If the later, <uri> must be NULL.
1972 */
1973int stats_dump_proxy_to_buffer(struct stream_interface *si, struct proxy *px, struct uri_auth *uri)
1974{
1975 struct appctx *appctx = __objt_appctx(si->end);
1976 struct stream *s = si_strm(si);
1977 struct channel *rep = si_ic(si);
1978 struct server *sv, *svs; /* server and server-state, server-state=server or server->track */
1979 struct listener *l;
1980 unsigned int flags;
1981
1982 if (uri)
1983 flags = uri->flags;
William Lallemand07a62f72017-05-24 00:57:40 +02001984 else if ((strm_li(s)->bind_conf->level & ACCESS_LVL_MASK) >= ACCESS_LVL_OPER)
William Lallemand74c24fb2016-11-21 17:18:36 +01001985 flags = ST_SHLGNDS | ST_SHNODE | ST_SHDESC;
1986 else
1987 flags = ST_SHNODE | ST_SHDESC;
1988
1989 chunk_reset(&trash);
1990
1991 switch (appctx->ctx.stats.px_st) {
1992 case STAT_PX_ST_INIT:
1993 /* we are on a new proxy */
1994 if (uri && uri->scope) {
1995 /* we have a limited scope, we have to check the proxy name */
1996 struct stat_scope *scope;
1997 int len;
1998
1999 len = strlen(px->id);
2000 scope = uri->scope;
2001
2002 while (scope) {
2003 /* match exact proxy name */
2004 if (scope->px_len == len && !memcmp(px->id, scope->px_id, len))
2005 break;
2006
2007 /* match '.' which means 'self' proxy */
2008 if (!strcmp(scope->px_id, ".") && px == s->be)
2009 break;
2010 scope = scope->next;
2011 }
2012
2013 /* proxy name not found : don't dump anything */
2014 if (scope == NULL)
2015 return 1;
2016 }
2017
2018 /* if the user has requested a limited output and the proxy
2019 * name does not match, skip it.
2020 */
2021 if (appctx->ctx.stats.scope_len &&
Willy Tarreau89faf5d2018-06-07 18:16:48 +02002022 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 +01002023 return 1;
2024
2025 if ((appctx->ctx.stats.flags & STAT_BOUND) &&
2026 (appctx->ctx.stats.iid != -1) &&
2027 (px->uuid != appctx->ctx.stats.iid))
2028 return 1;
2029
2030 appctx->ctx.stats.px_st = STAT_PX_ST_TH;
2031 /* fall through */
2032
2033 case STAT_PX_ST_TH:
2034 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
2035 stats_dump_html_px_hdr(si, px, uri);
Willy Tarreau06d80a92017-10-19 14:32:15 +02002036 if (ci_putchk(rep, &trash) == -1) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01002037 si_cant_put(si);
William Lallemand74c24fb2016-11-21 17:18:36 +01002038 return 0;
2039 }
2040 }
2041
2042 appctx->ctx.stats.px_st = STAT_PX_ST_FE;
2043 /* fall through */
2044
2045 case STAT_PX_ST_FE:
2046 /* print the frontend */
2047 if (stats_dump_fe_stats(si, px)) {
Willy Tarreau06d80a92017-10-19 14:32:15 +02002048 if (ci_putchk(rep, &trash) == -1) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01002049 si_cant_put(si);
William Lallemand74c24fb2016-11-21 17:18:36 +01002050 return 0;
2051 }
2052 }
2053
2054 appctx->ctx.stats.l = px->conf.listeners.n;
2055 appctx->ctx.stats.px_st = STAT_PX_ST_LI;
2056 /* fall through */
2057
2058 case STAT_PX_ST_LI:
2059 /* stats.l has been initialized above */
2060 for (; appctx->ctx.stats.l != &px->conf.listeners; appctx->ctx.stats.l = l->by_fe.n) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002061 if (buffer_almost_full(&rep->buf)) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01002062 si_cant_put(si);
William Lallemand74c24fb2016-11-21 17:18:36 +01002063 return 0;
2064 }
2065
2066 l = LIST_ELEM(appctx->ctx.stats.l, struct listener *, by_fe);
2067 if (!l->counters)
2068 continue;
2069
2070 if (appctx->ctx.stats.flags & STAT_BOUND) {
2071 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SO)))
2072 break;
2073
2074 if (appctx->ctx.stats.sid != -1 && l->luid != appctx->ctx.stats.sid)
2075 continue;
2076 }
2077
2078 /* print the frontend */
2079 if (stats_dump_li_stats(si, px, l, flags)) {
Willy Tarreau06d80a92017-10-19 14:32:15 +02002080 if (ci_putchk(rep, &trash) == -1) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01002081 si_cant_put(si);
William Lallemand74c24fb2016-11-21 17:18:36 +01002082 return 0;
2083 }
2084 }
2085 }
2086
2087 appctx->ctx.stats.sv = px->srv; /* may be NULL */
2088 appctx->ctx.stats.px_st = STAT_PX_ST_SV;
2089 /* fall through */
2090
2091 case STAT_PX_ST_SV:
2092 /* stats.sv has been initialized above */
2093 for (; appctx->ctx.stats.sv != NULL; appctx->ctx.stats.sv = sv->next) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002094 if (buffer_almost_full(&rep->buf)) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01002095 si_cant_put(si);
William Lallemand74c24fb2016-11-21 17:18:36 +01002096 return 0;
2097 }
2098
2099 sv = appctx->ctx.stats.sv;
2100
2101 if (appctx->ctx.stats.flags & STAT_BOUND) {
2102 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SV)))
2103 break;
2104
2105 if (appctx->ctx.stats.sid != -1 && sv->puid != appctx->ctx.stats.sid)
2106 continue;
2107 }
2108
2109 svs = sv;
2110 while (svs->track)
2111 svs = svs->track;
2112
2113 /* do not report servers which are DOWN and not changing state */
2114 if ((appctx->ctx.stats.flags & STAT_HIDE_DOWN) &&
Emeric Brun52a91d32017-08-31 14:41:55 +02002115 ((sv->cur_admin & SRV_ADMF_MAINT) || /* server is in maintenance */
2116 (sv->cur_state == SRV_ST_STOPPED && /* server is down */
William Lallemand74c24fb2016-11-21 17:18:36 +01002117 (!((svs->agent.state | svs->check.state) & CHK_ST_ENABLED) ||
2118 ((svs->agent.state & CHK_ST_ENABLED) && !svs->agent.health) ||
2119 ((svs->check.state & CHK_ST_ENABLED) && !svs->check.health))))) {
2120 continue;
2121 }
2122
2123 if (stats_dump_sv_stats(si, px, flags, sv)) {
Willy Tarreau06d80a92017-10-19 14:32:15 +02002124 if (ci_putchk(rep, &trash) == -1) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01002125 si_cant_put(si);
William Lallemand74c24fb2016-11-21 17:18:36 +01002126 return 0;
2127 }
2128 }
2129 } /* for sv */
2130
2131 appctx->ctx.stats.px_st = STAT_PX_ST_BE;
2132 /* fall through */
2133
2134 case STAT_PX_ST_BE:
2135 /* print the backend */
2136 if (stats_dump_be_stats(si, px, flags)) {
Willy Tarreau06d80a92017-10-19 14:32:15 +02002137 if (ci_putchk(rep, &trash) == -1) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01002138 si_cant_put(si);
William Lallemand74c24fb2016-11-21 17:18:36 +01002139 return 0;
2140 }
2141 }
2142
2143 appctx->ctx.stats.px_st = STAT_PX_ST_END;
2144 /* fall through */
2145
2146 case STAT_PX_ST_END:
2147 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
2148 stats_dump_html_px_end(si, px);
Willy Tarreau06d80a92017-10-19 14:32:15 +02002149 if (ci_putchk(rep, &trash) == -1) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01002150 si_cant_put(si);
William Lallemand74c24fb2016-11-21 17:18:36 +01002151 return 0;
2152 }
2153 }
2154
2155 appctx->ctx.stats.px_st = STAT_PX_ST_FIN;
2156 /* fall through */
2157
2158 case STAT_PX_ST_FIN:
2159 return 1;
2160
2161 default:
2162 /* unknown state, we should put an abort() here ! */
2163 return 1;
2164 }
2165}
2166
2167/* Dumps the HTTP stats head block to the trash for and uses the per-uri
2168 * parameters <uri>. The caller is responsible for clearing the trash if needed.
2169 */
2170static void stats_dump_html_head(struct uri_auth *uri)
2171{
2172 /* WARNING! This must fit in the first buffer !!! */
2173 chunk_appendf(&trash,
2174 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n"
2175 "\"http://www.w3.org/TR/html4/loose.dtd\">\n"
2176 "<html><head><title>Statistics Report for " PRODUCT_NAME "%s%s</title>\n"
2177 "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n"
2178 "<style type=\"text/css\"><!--\n"
2179 "body {"
2180 " font-family: arial, helvetica, sans-serif;"
2181 " font-size: 12px;"
2182 " font-weight: normal;"
2183 " color: black;"
2184 " background: white;"
2185 "}\n"
2186 "th,td {"
2187 " font-size: 10px;"
2188 "}\n"
2189 "h1 {"
2190 " font-size: x-large;"
2191 " margin-bottom: 0.5em;"
2192 "}\n"
2193 "h2 {"
2194 " font-family: helvetica, arial;"
2195 " font-size: x-large;"
2196 " font-weight: bold;"
2197 " font-style: italic;"
2198 " color: #6020a0;"
2199 " margin-top: 0em;"
2200 " margin-bottom: 0em;"
2201 "}\n"
2202 "h3 {"
2203 " font-family: helvetica, arial;"
2204 " font-size: 16px;"
2205 " font-weight: bold;"
2206 " color: #b00040;"
2207 " background: #e8e8d0;"
2208 " margin-top: 0em;"
2209 " margin-bottom: 0em;"
2210 "}\n"
2211 "li {"
2212 " margin-top: 0.25em;"
2213 " margin-right: 2em;"
2214 "}\n"
2215 ".hr {margin-top: 0.25em;"
2216 " border-color: black;"
2217 " border-bottom-style: solid;"
2218 "}\n"
2219 ".titre {background: #20D0D0;color: #000000; font-weight: bold; text-align: center;}\n"
2220 ".total {background: #20D0D0;color: #ffff80;}\n"
2221 ".frontend {background: #e8e8d0;}\n"
2222 ".socket {background: #d0d0d0;}\n"
2223 ".backend {background: #e8e8d0;}\n"
2224 ".active_down {background: #ff9090;}\n"
2225 ".active_going_up {background: #ffd020;}\n"
2226 ".active_going_down {background: #ffffa0;}\n"
2227 ".active_up {background: #c0ffc0;}\n"
2228 ".active_nolb {background: #20a0ff;}\n"
2229 ".active_draining {background: #20a0FF;}\n"
2230 ".active_no_check {background: #e0e0e0;}\n"
2231 ".backup_down {background: #ff9090;}\n"
2232 ".backup_going_up {background: #ff80ff;}\n"
2233 ".backup_going_down {background: #c060ff;}\n"
2234 ".backup_up {background: #b0d0ff;}\n"
2235 ".backup_nolb {background: #90b0e0;}\n"
2236 ".backup_draining {background: #cc9900;}\n"
2237 ".backup_no_check {background: #e0e0e0;}\n"
2238 ".maintain {background: #c07820;}\n"
2239 ".rls {letter-spacing: 0.2em; margin-right: 1px;}\n" /* right letter spacing (used for grouping digits) */
2240 "\n"
2241 "a.px:link {color: #ffff40; text-decoration: none;}"
2242 "a.px:visited {color: #ffff40; text-decoration: none;}"
2243 "a.px:hover {color: #ffffff; text-decoration: none;}"
2244 "a.lfsb:link {color: #000000; text-decoration: none;}"
2245 "a.lfsb:visited {color: #000000; text-decoration: none;}"
2246 "a.lfsb:hover {color: #505050; text-decoration: none;}"
2247 "\n"
2248 "table.tbl { border-collapse: collapse; border-style: none;}\n"
2249 "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"
2250 "table.tbl td.ac { text-align: center;}\n"
2251 "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}\n"
2252 "table.tbl th.pxname { background: #b00040; color: #ffff40; font-weight: bold; border-style: solid solid none solid; padding: 2px 3px; white-space: nowrap;}\n"
2253 "table.tbl th.empty { border-style: none; empty-cells: hide; background: white;}\n"
2254 "table.tbl th.desc { background: white; border-style: solid solid none solid; text-align: left; padding: 2px 3px;}\n"
2255 "\n"
2256 "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n"
2257 "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n"
2258 "table.lgd td.noborder { border-style: none; padding: 2px; white-space: nowrap;}\n"
2259 "table.det { border-collapse: collapse; border-style: none; }\n"
2260 "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"
2261 "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"
2262 "u {text-decoration:none; border-bottom: 1px dotted black;}\n"
2263 "div.tips {\n"
2264 " display:block;\n"
2265 " visibility:hidden;\n"
2266 " z-index:2147483647;\n"
2267 " position:absolute;\n"
2268 " padding:2px 4px 3px;\n"
2269 " background:#f0f060; color:#000000;\n"
2270 " border:1px solid #7040c0;\n"
2271 " white-space:nowrap;\n"
2272 " font-style:normal;font-size:11px;font-weight:normal;\n"
2273 " -moz-border-radius:3px;-webkit-border-radius:3px;border-radius:3px;\n"
2274 " -moz-box-shadow:gray 2px 2px 3px;-webkit-box-shadow:gray 2px 2px 3px;box-shadow:gray 2px 2px 3px;\n"
2275 "}\n"
2276 "u:hover div.tips {visibility:visible;}\n"
2277 "-->\n"
2278 "</style></head>\n",
2279 (uri->flags & ST_SHNODE) ? " on " : "",
2280 (uri->flags & ST_SHNODE) ? (uri->node ? uri->node : global.node) : ""
2281 );
2282}
2283
2284/* Dumps the HTML stats information block to the trash for and uses the state from
2285 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
2286 * for clearing the trash if needed.
2287 */
2288static void stats_dump_html_info(struct stream_interface *si, struct uri_auth *uri)
2289{
2290 struct appctx *appctx = __objt_appctx(si->end);
2291 unsigned int up = (now.tv_sec - start_date.tv_sec);
2292 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
2293
2294 /* WARNING! this has to fit the first packet too.
2295 * We are around 3.5 kB, add adding entries will
2296 * become tricky if we want to support 4kB buffers !
2297 */
2298 chunk_appendf(&trash,
2299 "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">"
2300 PRODUCT_NAME "%s</a></h1>\n"
2301 "<h2>Statistics Report for pid %d%s%s%s%s</h2>\n"
2302 "<hr width=\"100%%\" class=\"hr\">\n"
2303 "<h3>&gt; General process information</h3>\n"
2304 "<table border=0><tr><td align=\"left\" nowrap width=\"1%%\">\n"
Yves Lafon95317282018-02-26 11:10:37 +01002305 "<p><b>pid = </b> %d (process #%d, nbproc = %d, nbthread = %d)<br>\n"
William Lallemand74c24fb2016-11-21 17:18:36 +01002306 "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n"
2307 "<b>system limits:</b> memmax = %s%s; ulimit-n = %d<br>\n"
2308 "<b>maxsock = </b> %d; <b>maxconn = </b> %d; <b>maxpipes = </b> %d<br>\n"
2309 "current conns = %d; current pipes = %d/%d; conn rate = %d/sec<br>\n"
2310 "Running tasks: %d/%d; idle = %d %%<br>\n"
2311 "</td><td align=\"center\" nowrap>\n"
2312 "<table class=\"lgd\"><tr>\n"
2313 "<td class=\"active_up\">&nbsp;</td><td class=\"noborder\">active UP </td>"
2314 "<td class=\"backup_up\">&nbsp;</td><td class=\"noborder\">backup UP </td>"
2315 "</tr><tr>\n"
2316 "<td class=\"active_going_down\"></td><td class=\"noborder\">active UP, going down </td>"
2317 "<td class=\"backup_going_down\"></td><td class=\"noborder\">backup UP, going down </td>"
2318 "</tr><tr>\n"
2319 "<td class=\"active_going_up\"></td><td class=\"noborder\">active DOWN, going up </td>"
2320 "<td class=\"backup_going_up\"></td><td class=\"noborder\">backup DOWN, going up </td>"
2321 "</tr><tr>\n"
2322 "<td class=\"active_down\"></td><td class=\"noborder\">active or backup DOWN &nbsp;</td>"
2323 "<td class=\"active_no_check\"></td><td class=\"noborder\">not checked </td>"
2324 "</tr><tr>\n"
2325 "<td class=\"maintain\"></td><td class=\"noborder\" colspan=\"3\">active or backup DOWN for maintenance (MAINT) &nbsp;</td>"
2326 "</tr><tr>\n"
2327 "<td class=\"active_draining\"></td><td class=\"noborder\" colspan=\"3\">active or backup SOFT STOPPED for maintenance &nbsp;</td>"
2328 "</tr></table>\n"
2329 "Note: \"NOLB\"/\"DRAIN\" = UP with load-balancing disabled."
2330 "</td>"
2331 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
2332 "<b>Display option:</b><ul style=\"margin-top: 0.25em;\">"
2333 "",
2334 (uri->flags & ST_HIDEVER) ? "" : (STATS_VERSION_STRING),
2335 pid, (uri->flags & ST_SHNODE) ? " on " : "",
2336 (uri->flags & ST_SHNODE) ? (uri->node ? uri->node : global.node) : "",
2337 (uri->flags & ST_SHDESC) ? ": " : "",
2338 (uri->flags & ST_SHDESC) ? (uri->desc ? uri->desc : global.desc) : "",
Yves Lafon95317282018-02-26 11:10:37 +01002339 pid, relative_pid, global.nbproc, global.nbthread,
William Lallemand74c24fb2016-11-21 17:18:36 +01002340 up / 86400, (up % 86400) / 3600,
2341 (up % 3600) / 60, (up % 60),
2342 global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited",
2343 global.rlimit_memmax ? " MB" : "",
2344 global.rlimit_nofile,
2345 global.maxsock, global.maxconn, global.maxpipes,
2346 actconn, pipes_used, pipes_used+pipes_free, read_freq_ctr(&global.conn_per_sec),
Christopher Faulet34c5cc92016-12-06 09:15:30 +01002347 tasks_run_queue_cur, nb_tasks_cur, idle_pct
William Lallemand74c24fb2016-11-21 17:18:36 +01002348 );
2349
2350 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Willy Tarreau89faf5d2018-06-07 18:16:48 +02002351 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 +01002352 scope_txt[appctx->ctx.stats.scope_len] = '\0';
2353
2354 chunk_appendf(&trash,
2355 "<li><form method=\"GET\">Scope : <input value=\"%s\" name=\"" STAT_SCOPE_INPUT_NAME "\" size=\"8\" maxlength=\"%d\" tabindex=\"1\"/></form>\n",
2356 (appctx->ctx.stats.scope_len > 0) ? scope_txt : "",
2357 STAT_SCOPE_TXT_MAXLEN);
2358
2359 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
2360 scope_txt[0] = 0;
2361 if (appctx->ctx.stats.scope_len) {
2362 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau89faf5d2018-06-07 18:16:48 +02002363 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 +01002364 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
2365 }
2366
2367 if (appctx->ctx.stats.flags & STAT_HIDE_DOWN)
2368 chunk_appendf(&trash,
2369 "<li><a href=\"%s%s%s%s\">Show all servers</a><br>\n",
2370 uri->uri_prefix,
2371 "",
2372 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2373 scope_txt);
2374 else
2375 chunk_appendf(&trash,
2376 "<li><a href=\"%s%s%s%s\">Hide 'DOWN' servers</a><br>\n",
2377 uri->uri_prefix,
2378 ";up",
2379 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2380 scope_txt);
2381
2382 if (uri->refresh > 0) {
2383 if (appctx->ctx.stats.flags & STAT_NO_REFRESH)
2384 chunk_appendf(&trash,
2385 "<li><a href=\"%s%s%s%s\">Enable refresh</a><br>\n",
2386 uri->uri_prefix,
2387 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2388 "",
2389 scope_txt);
2390 else
2391 chunk_appendf(&trash,
2392 "<li><a href=\"%s%s%s%s\">Disable refresh</a><br>\n",
2393 uri->uri_prefix,
2394 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2395 ";norefresh",
2396 scope_txt);
2397 }
2398
2399 chunk_appendf(&trash,
2400 "<li><a href=\"%s%s%s%s\">Refresh now</a><br>\n",
2401 uri->uri_prefix,
2402 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2403 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2404 scope_txt);
2405
2406 chunk_appendf(&trash,
2407 "<li><a href=\"%s;csv%s%s\">CSV export</a><br>\n",
2408 uri->uri_prefix,
2409 (uri->refresh > 0) ? ";norefresh" : "",
2410 scope_txt);
2411
2412 chunk_appendf(&trash,
2413 "</ul></td>"
2414 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
2415 "<b>External resources:</b><ul style=\"margin-top: 0.25em;\">\n"
2416 "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>\n"
2417 "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>\n"
2418 "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>\n"
2419 "</ul>"
2420 "</td>"
2421 "</tr></table>\n"
2422 ""
2423 );
2424
2425 if (appctx->ctx.stats.st_code) {
2426 switch (appctx->ctx.stats.st_code) {
2427 case STAT_STATUS_DONE:
2428 chunk_appendf(&trash,
2429 "<p><div class=active_up>"
2430 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2431 "Action processed successfully."
2432 "</div>\n", uri->uri_prefix,
2433 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2434 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2435 scope_txt);
2436 break;
2437 case STAT_STATUS_NONE:
2438 chunk_appendf(&trash,
2439 "<p><div class=active_going_down>"
2440 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2441 "Nothing has changed."
2442 "</div>\n", uri->uri_prefix,
2443 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2444 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2445 scope_txt);
2446 break;
2447 case STAT_STATUS_PART:
2448 chunk_appendf(&trash,
2449 "<p><div class=active_going_down>"
2450 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2451 "Action partially processed.<br>"
2452 "Some server names are probably unknown or ambiguous (duplicated names in the backend)."
2453 "</div>\n", uri->uri_prefix,
2454 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2455 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2456 scope_txt);
2457 break;
2458 case STAT_STATUS_ERRP:
2459 chunk_appendf(&trash,
2460 "<p><div class=active_down>"
2461 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2462 "Action not processed because of invalid parameters."
2463 "<ul>"
2464 "<li>The action is maybe unknown.</li>"
2465 "<li>The backend name is probably unknown or ambiguous (duplicated names).</li>"
2466 "<li>Some server names are probably unknown or ambiguous (duplicated names in the backend).</li>"
2467 "</ul>"
2468 "</div>\n", uri->uri_prefix,
2469 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2470 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2471 scope_txt);
2472 break;
2473 case STAT_STATUS_EXCD:
2474 chunk_appendf(&trash,
2475 "<p><div class=active_down>"
2476 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2477 "<b>Action not processed : the buffer couldn't store all the data.<br>"
2478 "You should retry with less servers at a time.</b>"
2479 "</div>\n", uri->uri_prefix,
2480 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2481 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2482 scope_txt);
2483 break;
2484 case STAT_STATUS_DENY:
2485 chunk_appendf(&trash,
2486 "<p><div class=active_down>"
2487 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2488 "<b>Action denied.</b>"
2489 "</div>\n", uri->uri_prefix,
2490 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2491 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2492 scope_txt);
2493 break;
2494 default:
2495 chunk_appendf(&trash,
2496 "<p><div class=active_no_check>"
2497 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2498 "Unexpected result."
2499 "</div>\n", uri->uri_prefix,
2500 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2501 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2502 scope_txt);
2503 }
2504 chunk_appendf(&trash, "<p>\n");
2505 }
2506}
2507
2508/* Dumps the HTML stats trailer block to the trash. The caller is responsible
2509 * for clearing the trash if needed.
2510 */
2511static void stats_dump_html_end()
2512{
2513 chunk_appendf(&trash, "</body></html>\n");
2514}
2515
Simon Horman05ee2132017-01-04 09:37:25 +01002516/* Dumps the stats JSON header to the trash buffer which. The caller is responsible
2517 * for clearing it if needed.
2518 */
2519static void stats_dump_json_header()
2520{
2521 chunk_strcat(&trash, "[");
2522}
2523
2524
2525/* Dumps the JSON stats trailer block to the trash. The caller is responsible
2526 * for clearing the trash if needed.
2527 */
2528static void stats_dump_json_end()
2529{
2530 chunk_strcat(&trash, "]");
2531}
2532
William Lallemand74c24fb2016-11-21 17:18:36 +01002533/* This function dumps statistics onto the stream interface's read buffer in
2534 * either CSV or HTML format. <uri> contains some HTML-specific parameters that
2535 * are ignored for CSV format (hence <uri> may be NULL there). It returns 0 if
2536 * it had to stop writing data and an I/O is needed, 1 if the dump is finished
2537 * and the stream must be closed, or -1 in case of any error. This function is
2538 * used by both the CLI and the HTTP handlers.
2539 */
Willy Tarreau2b812e22016-11-22 16:18:05 +01002540static int stats_dump_stat_to_buffer(struct stream_interface *si, struct uri_auth *uri)
William Lallemand74c24fb2016-11-21 17:18:36 +01002541{
2542 struct appctx *appctx = __objt_appctx(si->end);
2543 struct channel *rep = si_ic(si);
2544 struct proxy *px;
2545
2546 chunk_reset(&trash);
2547
2548 switch (appctx->st2) {
2549 case STAT_ST_INIT:
2550 appctx->st2 = STAT_ST_HEAD; /* let's start producing data */
2551 /* fall through */
2552
2553 case STAT_ST_HEAD:
2554 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
2555 stats_dump_html_head(uri);
Simon Horman05ee2132017-01-04 09:37:25 +01002556 else if (appctx->ctx.stats.flags & STAT_FMT_JSON)
Willy Tarreau9d7fb632017-04-11 07:53:04 +02002557 stats_dump_json_header();
William Lallemand74c24fb2016-11-21 17:18:36 +01002558 else if (!(appctx->ctx.stats.flags & STAT_FMT_TYPED))
2559 stats_dump_csv_header();
2560
Willy Tarreau06d80a92017-10-19 14:32:15 +02002561 if (ci_putchk(rep, &trash) == -1) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01002562 si_cant_put(si);
William Lallemand74c24fb2016-11-21 17:18:36 +01002563 return 0;
2564 }
2565
2566 appctx->st2 = STAT_ST_INFO;
2567 /* fall through */
2568
2569 case STAT_ST_INFO:
2570 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
2571 stats_dump_html_info(si, uri);
Willy Tarreau06d80a92017-10-19 14:32:15 +02002572 if (ci_putchk(rep, &trash) == -1) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01002573 si_cant_put(si);
William Lallemand74c24fb2016-11-21 17:18:36 +01002574 return 0;
2575 }
2576 }
2577
Olivier Houchardfbc74e82017-11-24 16:54:05 +01002578 appctx->ctx.stats.px = proxies_list;
William Lallemand74c24fb2016-11-21 17:18:36 +01002579 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
2580 appctx->st2 = STAT_ST_LIST;
2581 /* fall through */
2582
2583 case STAT_ST_LIST:
2584 /* dump proxies */
2585 while (appctx->ctx.stats.px) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002586 if (buffer_almost_full(&rep->buf)) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01002587 si_cant_put(si);
William Lallemand74c24fb2016-11-21 17:18:36 +01002588 return 0;
2589 }
2590
2591 px = appctx->ctx.stats.px;
2592 /* skip the disabled proxies, global frontend and non-networked ones */
2593 if (px->state != PR_STSTOPPED && px->uuid > 0 && (px->cap & (PR_CAP_FE | PR_CAP_BE)))
2594 if (stats_dump_proxy_to_buffer(si, px, uri) == 0)
2595 return 0;
2596
2597 appctx->ctx.stats.px = px->next;
2598 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
2599 }
2600 /* here, we just have reached the last proxy */
2601
2602 appctx->st2 = STAT_ST_END;
2603 /* fall through */
2604
2605 case STAT_ST_END:
Simon Horman05ee2132017-01-04 09:37:25 +01002606 if (appctx->ctx.stats.flags & (STAT_FMT_HTML|STAT_FMT_JSON)) {
2607 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
2608 stats_dump_html_end();
2609 else
2610 stats_dump_json_end();
Willy Tarreau06d80a92017-10-19 14:32:15 +02002611 if (ci_putchk(rep, &trash) == -1) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01002612 si_cant_put(si);
William Lallemand74c24fb2016-11-21 17:18:36 +01002613 return 0;
2614 }
2615 }
2616
2617 appctx->st2 = STAT_ST_FIN;
2618 /* fall through */
2619
2620 case STAT_ST_FIN:
2621 return 1;
2622
2623 default:
2624 /* unknown state ! */
2625 appctx->st2 = STAT_ST_FIN;
2626 return -1;
2627 }
2628}
2629
2630/* We reached the stats page through a POST request. The appctx is
2631 * expected to have already been allocated by the caller.
2632 * Parse the posted data and enable/disable servers if necessary.
2633 * Returns 1 if request was parsed or zero if it needs more data.
2634 */
2635static int stats_process_http_post(struct stream_interface *si)
2636{
2637 struct stream *s = si_strm(si);
2638 struct appctx *appctx = objt_appctx(si->end);
2639
2640 struct proxy *px = NULL;
2641 struct server *sv = NULL;
2642
2643 char key[LINESIZE];
2644 int action = ST_ADM_ACTION_NONE;
2645 int reprocess = 0;
2646
2647 int total_servers = 0;
2648 int altered_servers = 0;
2649
2650 char *first_param, *cur_param, *next_param, *end_params;
2651 char *st_cur_param = NULL;
2652 char *st_next_param = NULL;
2653
Willy Tarreau83061a82018-07-13 11:56:34 +02002654 struct buffer *temp;
William Lallemand74c24fb2016-11-21 17:18:36 +01002655 int reql;
2656
2657 temp = get_trash_chunk();
2658 if (temp->size < s->txn->req.body_len) {
2659 /* too large request */
2660 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
2661 goto out;
2662 }
2663
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002664 reql = co_getblk(si_oc(si), temp->area, s->txn->req.body_len,
2665 s->txn->req.eoh + 2);
William Lallemand74c24fb2016-11-21 17:18:36 +01002666 if (reql <= 0) {
2667 /* we need more data */
2668 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
2669 return 0;
2670 }
2671
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002672 first_param = temp->area;
2673 end_params = temp->area + reql;
William Lallemand74c24fb2016-11-21 17:18:36 +01002674 cur_param = next_param = end_params;
2675 *end_params = '\0';
2676
2677 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
2678
2679 /*
2680 * Parse the parameters in reverse order to only store the last value.
2681 * From the html form, the backend and the action are at the end.
2682 */
2683 while (cur_param > first_param) {
2684 char *value;
2685 int poffset, plen;
2686
2687 cur_param--;
2688
2689 if ((*cur_param == '&') || (cur_param == first_param)) {
2690 reprocess_servers:
2691 /* Parse the key */
2692 poffset = (cur_param != first_param ? 1 : 0);
2693 plen = next_param - cur_param + (cur_param == first_param ? 1 : 0);
2694 if ((plen > 0) && (plen <= sizeof(key))) {
2695 strncpy(key, cur_param + poffset, plen);
2696 key[plen - 1] = '\0';
2697 } else {
2698 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
2699 goto out;
2700 }
2701
2702 /* Parse the value */
2703 value = key;
2704 while (*value != '\0' && *value != '=') {
2705 value++;
2706 }
2707 if (*value == '=') {
2708 /* Ok, a value is found, we can mark the end of the key */
2709 *value++ = '\0';
2710 }
2711 if (url_decode(key) < 0 || url_decode(value) < 0)
2712 break;
2713
2714 /* Now we can check the key to see what to do */
2715 if (!px && (strcmp(key, "b") == 0)) {
2716 if ((px = proxy_be_by_name(value)) == NULL) {
2717 /* the backend name is unknown or ambiguous (duplicate names) */
2718 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
2719 goto out;
2720 }
2721 }
2722 else if (!action && (strcmp(key, "action") == 0)) {
2723 if (strcmp(value, "ready") == 0) {
2724 action = ST_ADM_ACTION_READY;
2725 }
2726 else if (strcmp(value, "drain") == 0) {
2727 action = ST_ADM_ACTION_DRAIN;
2728 }
2729 else if (strcmp(value, "maint") == 0) {
2730 action = ST_ADM_ACTION_MAINT;
2731 }
2732 else if (strcmp(value, "shutdown") == 0) {
2733 action = ST_ADM_ACTION_SHUTDOWN;
2734 }
2735 else if (strcmp(value, "dhlth") == 0) {
2736 action = ST_ADM_ACTION_DHLTH;
2737 }
2738 else if (strcmp(value, "ehlth") == 0) {
2739 action = ST_ADM_ACTION_EHLTH;
2740 }
2741 else if (strcmp(value, "hrunn") == 0) {
2742 action = ST_ADM_ACTION_HRUNN;
2743 }
2744 else if (strcmp(value, "hnolb") == 0) {
2745 action = ST_ADM_ACTION_HNOLB;
2746 }
2747 else if (strcmp(value, "hdown") == 0) {
2748 action = ST_ADM_ACTION_HDOWN;
2749 }
2750 else if (strcmp(value, "dagent") == 0) {
2751 action = ST_ADM_ACTION_DAGENT;
2752 }
2753 else if (strcmp(value, "eagent") == 0) {
2754 action = ST_ADM_ACTION_EAGENT;
2755 }
2756 else if (strcmp(value, "arunn") == 0) {
2757 action = ST_ADM_ACTION_ARUNN;
2758 }
2759 else if (strcmp(value, "adown") == 0) {
2760 action = ST_ADM_ACTION_ADOWN;
2761 }
2762 /* else these are the old supported methods */
2763 else if (strcmp(value, "disable") == 0) {
2764 action = ST_ADM_ACTION_DISABLE;
2765 }
2766 else if (strcmp(value, "enable") == 0) {
2767 action = ST_ADM_ACTION_ENABLE;
2768 }
2769 else if (strcmp(value, "stop") == 0) {
2770 action = ST_ADM_ACTION_STOP;
2771 }
2772 else if (strcmp(value, "start") == 0) {
2773 action = ST_ADM_ACTION_START;
2774 }
2775 else {
2776 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
2777 goto out;
2778 }
2779 }
2780 else if (strcmp(key, "s") == 0) {
2781 if (!(px && action)) {
2782 /*
2783 * Indicates that we'll need to reprocess the parameters
2784 * as soon as backend and action are known
2785 */
2786 if (!reprocess) {
2787 st_cur_param = cur_param;
2788 st_next_param = next_param;
2789 }
2790 reprocess = 1;
2791 }
2792 else if ((sv = findserver(px, value)) != NULL) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002793 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
William Lallemand74c24fb2016-11-21 17:18:36 +01002794 switch (action) {
2795 case ST_ADM_ACTION_DISABLE:
Emeric Brun52a91d32017-08-31 14:41:55 +02002796 if (!(sv->cur_admin & SRV_ADMF_FMAINT)) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002797 altered_servers++;
2798 total_servers++;
2799 srv_set_admin_flag(sv, SRV_ADMF_FMAINT, "'disable' on stats page");
2800 }
2801 break;
2802 case ST_ADM_ACTION_ENABLE:
Emeric Brun52a91d32017-08-31 14:41:55 +02002803 if (sv->cur_admin & SRV_ADMF_FMAINT) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002804 altered_servers++;
2805 total_servers++;
2806 srv_clr_admin_flag(sv, SRV_ADMF_FMAINT);
2807 }
2808 break;
2809 case ST_ADM_ACTION_STOP:
Emeric Brun52a91d32017-08-31 14:41:55 +02002810 if (!(sv->cur_admin & SRV_ADMF_FDRAIN)) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002811 srv_set_admin_flag(sv, SRV_ADMF_FDRAIN, "'stop' on stats page");
2812 altered_servers++;
2813 total_servers++;
2814 }
2815 break;
2816 case ST_ADM_ACTION_START:
Emeric Brun52a91d32017-08-31 14:41:55 +02002817 if (sv->cur_admin & SRV_ADMF_FDRAIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002818 srv_clr_admin_flag(sv, SRV_ADMF_FDRAIN);
2819 altered_servers++;
2820 total_servers++;
2821 }
2822 break;
2823 case ST_ADM_ACTION_DHLTH:
2824 if (sv->check.state & CHK_ST_CONFIGURED) {
2825 sv->check.state &= ~CHK_ST_ENABLED;
2826 altered_servers++;
2827 total_servers++;
2828 }
2829 break;
2830 case ST_ADM_ACTION_EHLTH:
2831 if (sv->check.state & CHK_ST_CONFIGURED) {
2832 sv->check.state |= CHK_ST_ENABLED;
2833 altered_servers++;
2834 total_servers++;
2835 }
2836 break;
2837 case ST_ADM_ACTION_HRUNN:
2838 if (!(sv->track)) {
2839 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02002840 srv_set_running(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01002841 altered_servers++;
2842 total_servers++;
2843 }
2844 break;
2845 case ST_ADM_ACTION_HNOLB:
2846 if (!(sv->track)) {
2847 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02002848 srv_set_stopping(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01002849 altered_servers++;
2850 total_servers++;
2851 }
2852 break;
2853 case ST_ADM_ACTION_HDOWN:
2854 if (!(sv->track)) {
2855 sv->check.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02002856 srv_set_stopped(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01002857 altered_servers++;
2858 total_servers++;
2859 }
2860 break;
2861 case ST_ADM_ACTION_DAGENT:
2862 if (sv->agent.state & CHK_ST_CONFIGURED) {
2863 sv->agent.state &= ~CHK_ST_ENABLED;
2864 altered_servers++;
2865 total_servers++;
2866 }
2867 break;
2868 case ST_ADM_ACTION_EAGENT:
2869 if (sv->agent.state & CHK_ST_CONFIGURED) {
2870 sv->agent.state |= CHK_ST_ENABLED;
2871 altered_servers++;
2872 total_servers++;
2873 }
2874 break;
2875 case ST_ADM_ACTION_ARUNN:
2876 if (sv->agent.state & CHK_ST_ENABLED) {
2877 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02002878 srv_set_running(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01002879 altered_servers++;
2880 total_servers++;
2881 }
2882 break;
2883 case ST_ADM_ACTION_ADOWN:
2884 if (sv->agent.state & CHK_ST_ENABLED) {
2885 sv->agent.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02002886 srv_set_stopped(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01002887 altered_servers++;
2888 total_servers++;
2889 }
2890 break;
2891 case ST_ADM_ACTION_READY:
2892 srv_adm_set_ready(sv);
2893 altered_servers++;
2894 total_servers++;
2895 break;
2896 case ST_ADM_ACTION_DRAIN:
2897 srv_adm_set_drain(sv);
2898 altered_servers++;
2899 total_servers++;
2900 break;
2901 case ST_ADM_ACTION_MAINT:
2902 srv_adm_set_maint(sv);
2903 altered_servers++;
2904 total_servers++;
2905 break;
2906 case ST_ADM_ACTION_SHUTDOWN:
2907 if (px->state != PR_STSTOPPED) {
2908 struct stream *sess, *sess_bck;
2909
2910 list_for_each_entry_safe(sess, sess_bck, &sv->actconns, by_srv)
2911 if (sess->srv_conn == sv)
2912 stream_shutdown(sess, SF_ERR_KILLED);
2913
2914 altered_servers++;
2915 total_servers++;
2916 }
2917 break;
2918 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002919 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Lallemand74c24fb2016-11-21 17:18:36 +01002920 } else {
2921 /* the server name is unknown or ambiguous (duplicate names) */
2922 total_servers++;
2923 }
2924 }
2925 if (reprocess && px && action) {
2926 /* Now, we know the backend and the action chosen by the user.
2927 * We can safely restart from the first server parameter
2928 * to reprocess them
2929 */
2930 cur_param = st_cur_param;
2931 next_param = st_next_param;
2932 reprocess = 0;
2933 goto reprocess_servers;
2934 }
2935
2936 next_param = cur_param;
2937 }
2938 }
2939
2940 if (total_servers == 0) {
2941 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
2942 }
2943 else if (altered_servers == 0) {
2944 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
2945 }
2946 else if (altered_servers == total_servers) {
2947 appctx->ctx.stats.st_code = STAT_STATUS_DONE;
2948 }
2949 else {
2950 appctx->ctx.stats.st_code = STAT_STATUS_PART;
2951 }
2952 out:
2953 return 1;
2954}
2955
2956
2957static int stats_send_http_headers(struct stream_interface *si)
2958{
2959 struct stream *s = si_strm(si);
2960 struct uri_auth *uri = s->be->uri_auth;
Willy Tarreau21ff2c42018-09-20 11:01:01 +02002961 struct appctx *appctx = __objt_appctx(si->end);
William Lallemand74c24fb2016-11-21 17:18:36 +01002962
2963 chunk_printf(&trash,
2964 "HTTP/1.1 200 OK\r\n"
2965 "Cache-Control: no-cache\r\n"
2966 "Connection: close\r\n"
2967 "Content-Type: %s\r\n",
2968 (appctx->ctx.stats.flags & STAT_FMT_HTML) ? "text/html" : "text/plain");
2969
2970 if (uri->refresh > 0 && !(appctx->ctx.stats.flags & STAT_NO_REFRESH))
2971 chunk_appendf(&trash, "Refresh: %d\r\n",
2972 uri->refresh);
2973
2974 /* we don't send the CRLF in chunked mode, it will be sent with the first chunk's size */
2975
2976 if (appctx->ctx.stats.flags & STAT_CHUNKED)
2977 chunk_appendf(&trash, "Transfer-Encoding: chunked\r\n");
2978 else
2979 chunk_appendf(&trash, "\r\n");
2980
Willy Tarreau06d80a92017-10-19 14:32:15 +02002981 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01002982 si_cant_put(si);
William Lallemand74c24fb2016-11-21 17:18:36 +01002983 return 0;
2984 }
2985
2986 return 1;
2987}
2988
2989static int stats_send_http_redirect(struct stream_interface *si)
2990{
2991 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
2992 struct stream *s = si_strm(si);
2993 struct uri_auth *uri = s->be->uri_auth;
Willy Tarreau21ff2c42018-09-20 11:01:01 +02002994 struct appctx *appctx = __objt_appctx(si->end);
William Lallemand74c24fb2016-11-21 17:18:36 +01002995
2996 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
2997 scope_txt[0] = 0;
2998 if (appctx->ctx.stats.scope_len) {
2999 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau89faf5d2018-06-07 18:16:48 +02003000 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 +01003001 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
3002 }
3003
3004 /* We don't want to land on the posted stats page because a refresh will
3005 * repost the data. We don't want this to happen on accident so we redirect
3006 * the browse to the stats page with a GET.
3007 */
3008 chunk_printf(&trash,
3009 "HTTP/1.1 303 See Other\r\n"
3010 "Cache-Control: no-cache\r\n"
3011 "Content-Type: text/plain\r\n"
3012 "Connection: close\r\n"
3013 "Location: %s;st=%s%s%s%s\r\n"
3014 "Content-length: 0\r\n"
3015 "\r\n",
3016 uri->uri_prefix,
3017 ((appctx->ctx.stats.st_code > STAT_STATUS_INIT) &&
3018 (appctx->ctx.stats.st_code < STAT_STATUS_SIZE) &&
3019 stat_status_codes[appctx->ctx.stats.st_code]) ?
3020 stat_status_codes[appctx->ctx.stats.st_code] :
3021 stat_status_codes[STAT_STATUS_UNKN],
3022 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3023 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3024 scope_txt);
3025
Willy Tarreau06d80a92017-10-19 14:32:15 +02003026 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01003027 si_cant_put(si);
William Lallemand74c24fb2016-11-21 17:18:36 +01003028 return 0;
3029 }
3030
3031 return 1;
3032}
3033
Simon Horman05ee2132017-01-04 09:37:25 +01003034
William Lallemand74c24fb2016-11-21 17:18:36 +01003035/* This I/O handler runs as an applet embedded in a stream interface. It is
3036 * used to send HTTP stats over a TCP socket. The mechanism is very simple.
3037 * appctx->st0 contains the operation in progress (dump, done). The handler
3038 * automatically unregisters itself once transfer is complete.
3039 */
3040static void http_stats_io_handler(struct appctx *appctx)
3041{
3042 struct stream_interface *si = appctx->owner;
3043 struct stream *s = si_strm(si);
3044 struct channel *req = si_oc(si);
3045 struct channel *res = si_ic(si);
3046
3047 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
3048 goto out;
3049
Christopher Fauleta73e59b2016-12-09 17:30:18 +01003050 /* Check if the input buffer is avalaible. */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003051 if (res->buf.size == 0) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01003052 si_cant_put(si);
Christopher Fauleta73e59b2016-12-09 17:30:18 +01003053 goto out;
3054 }
3055
William Lallemand74c24fb2016-11-21 17:18:36 +01003056 /* check that the output is not closed */
3057 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW))
3058 appctx->st0 = STAT_HTTP_DONE;
3059
3060 /* all states are processed in sequence */
3061 if (appctx->st0 == STAT_HTTP_HEAD) {
3062 if (stats_send_http_headers(si)) {
3063 if (s->txn->meth == HTTP_METH_HEAD)
3064 appctx->st0 = STAT_HTTP_DONE;
3065 else
3066 appctx->st0 = STAT_HTTP_DUMP;
3067 }
3068 }
3069
3070 if (appctx->st0 == STAT_HTTP_DUMP) {
Willy Tarreau97f538b2018-06-15 19:41:31 +02003071 unsigned int prev_len = ci_data(si_ic(si));
William Lallemand74c24fb2016-11-21 17:18:36 +01003072 unsigned int data_len;
3073 unsigned int last_len;
3074 unsigned int last_fwd = 0;
3075
3076 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
3077 /* One difficulty we're facing is that we must prevent
3078 * the input data from being automatically forwarded to
3079 * the output area. For this, we temporarily disable
3080 * forwarding on the channel.
3081 */
3082 last_fwd = si_ic(si)->to_forward;
3083 si_ic(si)->to_forward = 0;
3084 chunk_printf(&trash, "\r\n000000\r\n");
Willy Tarreau06d80a92017-10-19 14:32:15 +02003085 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01003086 si_cant_put(si);
William Lallemand74c24fb2016-11-21 17:18:36 +01003087 si_ic(si)->to_forward = last_fwd;
3088 goto out;
3089 }
3090 }
3091
Willy Tarreau97f538b2018-06-15 19:41:31 +02003092 data_len = ci_data(si_ic(si));
William Lallemand74c24fb2016-11-21 17:18:36 +01003093 if (stats_dump_stat_to_buffer(si, s->be->uri_auth))
3094 appctx->st0 = STAT_HTTP_DONE;
3095
Willy Tarreau97f538b2018-06-15 19:41:31 +02003096 last_len = ci_data(si_ic(si));
William Lallemand74c24fb2016-11-21 17:18:36 +01003097
3098 /* Now we must either adjust or remove the chunk size. This is
3099 * not easy because the chunk size might wrap at the end of the
3100 * buffer, so we pretend we have nothing in the buffer, we write
3101 * the size, then restore the buffer's contents. Note that we can
3102 * only do that because no forwarding is scheduled on the stats
3103 * applet.
3104 */
3105 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
3106 si_ic(si)->total -= (last_len - prev_len);
Willy Tarreau97f538b2018-06-15 19:41:31 +02003107 b_sub(si_ib(si), (last_len - prev_len));
William Lallemand74c24fb2016-11-21 17:18:36 +01003108
3109 if (last_len != data_len) {
3110 chunk_printf(&trash, "\r\n%06x\r\n", (last_len - data_len));
Willy Tarreau06d80a92017-10-19 14:32:15 +02003111 if (ci_putchk(si_ic(si), &trash) == -1)
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01003112 si_cant_put(si);
William Lallemand74c24fb2016-11-21 17:18:36 +01003113
3114 si_ic(si)->total += (last_len - data_len);
Willy Tarreau97f538b2018-06-15 19:41:31 +02003115 b_add(si_ib(si), last_len - data_len);
William Lallemand74c24fb2016-11-21 17:18:36 +01003116 }
3117 /* now re-enable forwarding */
3118 channel_forward(si_ic(si), last_fwd);
3119 }
3120 }
3121
3122 if (appctx->st0 == STAT_HTTP_POST) {
3123 if (stats_process_http_post(si))
3124 appctx->st0 = STAT_HTTP_LAST;
3125 else if (si_oc(si)->flags & CF_SHUTR)
3126 appctx->st0 = STAT_HTTP_DONE;
3127 }
3128
3129 if (appctx->st0 == STAT_HTTP_LAST) {
3130 if (stats_send_http_redirect(si))
3131 appctx->st0 = STAT_HTTP_DONE;
3132 }
3133
3134 if (appctx->st0 == STAT_HTTP_DONE) {
3135 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
3136 chunk_printf(&trash, "\r\n0\r\n\r\n");
Willy Tarreau06d80a92017-10-19 14:32:15 +02003137 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01003138 si_cant_put(si);
William Lallemand74c24fb2016-11-21 17:18:36 +01003139 goto out;
3140 }
3141 }
3142 /* eat the whole request */
Willy Tarreau97f538b2018-06-15 19:41:31 +02003143 co_skip(si_oc(si), co_data(si_oc(si)));
William Lallemand74c24fb2016-11-21 17:18:36 +01003144 res->flags |= CF_READ_NULL;
3145 si_shutr(si);
3146 }
3147
3148 if ((res->flags & CF_SHUTR) && (si->state == SI_ST_EST))
3149 si_shutw(si);
3150
3151 if (appctx->st0 == STAT_HTTP_DONE) {
3152 if ((req->flags & CF_SHUTW) && (si->state == SI_ST_EST)) {
3153 si_shutr(si);
3154 res->flags |= CF_READ_NULL;
3155 }
3156 }
3157 out:
Willy Tarreau055ba4f2018-07-24 17:05:54 +02003158 /* we have left the request in the buffer for the case where we
3159 * process a POST, and this automatically re-enables activity on
3160 * read. It's better to indicate that we want to stop reading when
3161 * we're sending, so that we know there's at most one direction
3162 * deciding to wake the applet up. It saves it from looping when
3163 * emitting large blocks into small TCP windows.
3164 */
3165 if (!channel_is_empty(res))
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01003166 si_stop_get(si);
William Lallemand74c24fb2016-11-21 17:18:36 +01003167}
3168
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003169/* Dump all fields from <info> into <out> using the "show info" format (name: value) */
Willy Tarreau83061a82018-07-13 11:56:34 +02003170static int stats_dump_info_fields(struct buffer *out,
3171 const struct field *info)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003172{
3173 int field;
3174
3175 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
3176 if (!field_format(info, field))
3177 continue;
3178
3179 if (!chunk_appendf(out, "%s: ", info_field_names[field]))
3180 return 0;
3181 if (!stats_emit_raw_data_field(out, &info[field]))
3182 return 0;
3183 if (!chunk_strcat(out, "\n"))
3184 return 0;
3185 }
3186 return 1;
3187}
3188
3189/* Dump all fields from <info> into <out> using the "show info typed" format */
Willy Tarreau83061a82018-07-13 11:56:34 +02003190static int stats_dump_typed_info_fields(struct buffer *out,
3191 const struct field *info)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003192{
3193 int field;
3194
3195 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
3196 if (!field_format(info, field))
3197 continue;
3198
3199 if (!chunk_appendf(out, "%d.%s.%u:", field, info_field_names[field], info[INF_PROCESS_NUM].u.u32))
3200 return 0;
3201 if (!stats_emit_field_tags(out, &info[field], ':'))
3202 return 0;
3203 if (!stats_emit_typed_data_field(out, &info[field]))
3204 return 0;
3205 if (!chunk_strcat(out, "\n"))
3206 return 0;
3207 }
3208 return 1;
3209}
3210
3211/* Fill <info> with HAProxy global info. <info> is preallocated
3212 * array of length <len>. The length of the aray must be
3213 * INF_TOTAL_FIELDS. If this length is less then this value, the
3214 * function returns 0, otherwise, it returns 1.
3215 */
3216int stats_fill_info(struct field *info, int len)
3217{
3218 unsigned int up = (now.tv_sec - start_date.tv_sec);
Willy Tarreau83061a82018-07-13 11:56:34 +02003219 struct buffer *out = get_trash_chunk();
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003220
3221#ifdef USE_OPENSSL
3222 int ssl_sess_rate = read_freq_ctr(&global.ssl_per_sec);
3223 int ssl_key_rate = read_freq_ctr(&global.ssl_fe_keys_per_sec);
3224 int ssl_reuse = 0;
3225
3226 if (ssl_key_rate < ssl_sess_rate) {
3227 /* count the ssl reuse ratio and avoid overflows in both directions */
3228 ssl_reuse = 100 - (100 * ssl_key_rate + (ssl_sess_rate - 1) / 2) / ssl_sess_rate;
3229 }
3230#endif
3231
3232 if (len < INF_TOTAL_FIELDS)
3233 return 0;
3234
3235 chunk_reset(out);
3236 memset(info, 0, sizeof(*info) * len);
3237
3238 info[INF_NAME] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, PRODUCT_NAME);
3239 info[INF_VERSION] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, HAPROXY_VERSION);
3240 info[INF_RELEASE_DATE] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, HAPROXY_DATE);
3241
Yves Lafon95317282018-02-26 11:10:37 +01003242 info[INF_NBTHREAD] = mkf_u32(FO_CONFIG|FS_SERVICE, global.nbthread);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003243 info[INF_NBPROC] = mkf_u32(FO_CONFIG|FS_SERVICE, global.nbproc);
3244 info[INF_PROCESS_NUM] = mkf_u32(FO_KEY, relative_pid);
3245 info[INF_PID] = mkf_u32(FO_STATUS, pid);
3246
3247 info[INF_UPTIME] = mkf_str(FN_DURATION, chunk_newstr(out));
3248 chunk_appendf(out, "%ud %uh%02um%02us", up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60));
3249
3250 info[INF_UPTIME_SEC] = mkf_u32(FN_DURATION, up);
3251 info[INF_MEMMAX_MB] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_memmax);
3252 info[INF_POOL_ALLOC_MB] = mkf_u32(0, (unsigned)(pool_total_allocated() / 1048576L));
3253 info[INF_POOL_USED_MB] = mkf_u32(0, (unsigned)(pool_total_used() / 1048576L));
3254 info[INF_POOL_FAILED] = mkf_u32(FN_COUNTER, pool_total_failures());
3255 info[INF_ULIMIT_N] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_nofile);
3256 info[INF_MAXSOCK] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxsock);
3257 info[INF_MAXCONN] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxconn);
3258 info[INF_HARD_MAXCONN] = mkf_u32(FO_CONFIG|FN_LIMIT, global.hardmaxconn);
3259 info[INF_CURR_CONN] = mkf_u32(0, actconn);
3260 info[INF_CUM_CONN] = mkf_u32(FN_COUNTER, totalconn);
3261 info[INF_CUM_REQ] = mkf_u32(FN_COUNTER, global.req_count);
3262#ifdef USE_OPENSSL
3263 info[INF_MAX_SSL_CONNS] = mkf_u32(FN_MAX, global.maxsslconn);
3264 info[INF_CURR_SSL_CONNS] = mkf_u32(0, sslconns);
3265 info[INF_CUM_SSL_CONNS] = mkf_u32(FN_COUNTER, totalsslconns);
3266#endif
3267 info[INF_MAXPIPES] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxpipes);
3268 info[INF_PIPES_USED] = mkf_u32(0, pipes_used);
3269 info[INF_PIPES_FREE] = mkf_u32(0, pipes_free);
3270 info[INF_CONN_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.conn_per_sec));
3271 info[INF_CONN_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.cps_lim);
3272 info[INF_MAX_CONN_RATE] = mkf_u32(FN_MAX, global.cps_max);
3273 info[INF_SESS_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.sess_per_sec));
3274 info[INF_SESS_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.sps_lim);
3275 info[INF_MAX_SESS_RATE] = mkf_u32(FN_RATE, global.sps_max);
3276
3277#ifdef USE_OPENSSL
3278 info[INF_SSL_RATE] = mkf_u32(FN_RATE, ssl_sess_rate);
3279 info[INF_SSL_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.ssl_lim);
3280 info[INF_MAX_SSL_RATE] = mkf_u32(FN_MAX, global.ssl_max);
3281 info[INF_SSL_FRONTEND_KEY_RATE] = mkf_u32(0, ssl_key_rate);
3282 info[INF_SSL_FRONTEND_MAX_KEY_RATE] = mkf_u32(FN_MAX, global.ssl_fe_keys_max);
3283 info[INF_SSL_FRONTEND_SESSION_REUSE_PCT] = mkf_u32(0, ssl_reuse);
3284 info[INF_SSL_BACKEND_KEY_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.ssl_be_keys_per_sec));
3285 info[INF_SSL_BACKEND_MAX_KEY_RATE] = mkf_u32(FN_MAX, global.ssl_be_keys_max);
3286 info[INF_SSL_CACHE_LOOKUPS] = mkf_u32(FN_COUNTER, global.shctx_lookups);
3287 info[INF_SSL_CACHE_MISSES] = mkf_u32(FN_COUNTER, global.shctx_misses);
3288#endif
3289 info[INF_COMPRESS_BPS_IN] = mkf_u32(FN_RATE, read_freq_ctr(&global.comp_bps_in));
3290 info[INF_COMPRESS_BPS_OUT] = mkf_u32(FN_RATE, read_freq_ctr(&global.comp_bps_out));
3291 info[INF_COMPRESS_BPS_RATE_LIM] = mkf_u32(FO_CONFIG|FN_LIMIT, global.comp_rate_lim);
3292#ifdef USE_ZLIB
3293 info[INF_ZLIB_MEM_USAGE] = mkf_u32(0, zlib_used_memory);
3294 info[INF_MAX_ZLIB_MEM_USAGE] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxzlibmem);
3295#endif
3296 info[INF_TASKS] = mkf_u32(0, nb_tasks_cur);
Christopher Faulet34c5cc92016-12-06 09:15:30 +01003297 info[INF_RUN_QUEUE] = mkf_u32(0, tasks_run_queue_cur);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003298 info[INF_IDLE_PCT] = mkf_u32(FN_AVG, idle_pct);
3299 info[INF_NODE] = mkf_str(FO_CONFIG|FN_OUTPUT|FS_SERVICE, global.node);
3300 if (global.desc)
3301 info[INF_DESCRIPTION] = mkf_str(FO_CONFIG|FN_OUTPUT|FS_SERVICE, global.desc);
Willy Tarreau00098ea2018-11-05 14:38:13 +01003302 info[INF_STOPPING] = mkf_u32(0, stopping);
3303 info[INF_JOBS] = mkf_u32(0, jobs);
William Lallemanda7199262018-11-16 16:57:20 +01003304 info[INF_UNSTOPPABLE_JOBS] = mkf_u32(0, unstoppable_jobs);
Willy Tarreau00098ea2018-11-05 14:38:13 +01003305 info[INF_LISTENERS] = mkf_u32(0, listeners);
Willy Tarreau199ad242018-11-05 16:31:22 +01003306 info[INF_ACTIVE_PEERS] = mkf_u32(0, active_peers);
Willy Tarreau2d372c22018-11-05 17:12:27 +01003307 info[INF_CONNECTED_PEERS] = mkf_u32(0, connected_peers);
Willy Tarreau13ef7732018-11-12 07:25:28 +01003308 info[INF_DROPPED_LOGS] = mkf_u32(0, dropped_logs);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003309
3310 return 1;
3311}
3312
3313/* This function dumps information onto the stream interface's read buffer.
3314 * It returns 0 as long as it does not complete, non-zero upon completion.
3315 * No state is used.
3316 */
3317static int stats_dump_info_to_buffer(struct stream_interface *si)
3318{
3319 struct appctx *appctx = __objt_appctx(si->end);
3320
3321 if (!stats_fill_info(info, INF_TOTAL_FIELDS))
3322 return 0;
3323
3324 chunk_reset(&trash);
3325
3326 if (appctx->ctx.stats.flags & STAT_FMT_TYPED)
3327 stats_dump_typed_info_fields(&trash, info);
Simon Horman05ee2132017-01-04 09:37:25 +01003328 else if (appctx->ctx.stats.flags & STAT_FMT_JSON)
3329 stats_dump_json_info_fields(&trash, info);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003330 else
3331 stats_dump_info_fields(&trash, info);
3332
Willy Tarreau06d80a92017-10-19 14:32:15 +02003333 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01003334 si_cant_put(si);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003335 return 0;
3336 }
3337
3338 return 1;
3339}
3340
Simon Horman6f6bb382017-01-04 09:37:26 +01003341/* This function dumps the schema onto the stream interface's read buffer.
3342 * It returns 0 as long as it does not complete, non-zero upon completion.
3343 * No state is used.
3344 *
3345 * Integer values bouned to the range [-(2**53)+1, (2**53)-1] as
3346 * per the recommendation for interoperable integers in section 6 of RFC 7159.
3347 */
Willy Tarreau83061a82018-07-13 11:56:34 +02003348static void stats_dump_json_schema(struct buffer *out)
Simon Horman6f6bb382017-01-04 09:37:26 +01003349{
3350
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003351 int old_len = out->data;
Simon Horman6f6bb382017-01-04 09:37:26 +01003352
3353 chunk_strcat(out,
3354 "{"
3355 "\"$schema\":\"http://json-schema.org/draft-04/schema#\","
3356 "\"oneOf\":["
3357 "{"
3358 "\"title\":\"Info\","
3359 "\"type\":\"array\","
3360 "\"items\":{"
3361 "\"properties\":{"
3362 "\"title\":\"InfoItem\","
3363 "\"type\":\"object\","
3364 "\"field\":{\"$ref\":\"#/definitions/field\"},"
3365 "\"processNum\":{\"$ref\":\"#/definitions/processNum\"},"
3366 "\"tags\":{\"$ref\":\"#/definitions/tags\"},"
3367 "\"value\":{\"$ref\":\"#/definitions/typedValue\"}"
3368 "},"
3369 "\"required\":[\"field\",\"processNum\",\"tags\","
3370 "\"value\"]"
3371 "}"
3372 "},"
3373 "{"
3374 "\"title\":\"Stat\","
3375 "\"type\":\"array\","
3376 "\"items\":{"
3377 "\"title\":\"InfoItem\","
3378 "\"type\":\"object\","
3379 "\"properties\":{"
3380 "\"objType\":{"
3381 "\"enum\":[\"Frontend\",\"Backend\",\"Listener\","
3382 "\"Server\",\"Unknown\"]"
3383 "},"
3384 "\"proxyId\":{"
3385 "\"type\":\"integer\","
3386 "\"minimum\":0"
3387 "},"
3388 "\"id\":{"
3389 "\"type\":\"integer\","
3390 "\"minimum\":0"
3391 "},"
3392 "\"field\":{\"$ref\":\"#/definitions/field\"},"
3393 "\"processNum\":{\"$ref\":\"#/definitions/processNum\"},"
3394 "\"tags\":{\"$ref\":\"#/definitions/tags\"},"
3395 "\"typedValue\":{\"$ref\":\"#/definitions/typedValue\"}"
3396 "},"
3397 "\"required\":[\"objType\",\"proxyId\",\"id\","
3398 "\"field\",\"processNum\",\"tags\","
3399 "\"value\"]"
3400 "}"
3401 "},"
3402 "{"
3403 "\"title\":\"Error\","
3404 "\"type\":\"object\","
3405 "\"properties\":{"
3406 "\"errorStr\":{"
3407 "\"type\":\"string\""
3408 "},"
3409 "\"required\":[\"errorStr\"]"
3410 "}"
3411 "}"
3412 "],"
3413 "\"definitions\":{"
3414 "\"field\":{"
3415 "\"type\":\"object\","
3416 "\"pos\":{"
3417 "\"type\":\"integer\","
3418 "\"minimum\":0"
3419 "},"
3420 "\"name\":{"
3421 "\"type\":\"string\""
3422 "},"
3423 "\"required\":[\"pos\",\"name\"]"
3424 "},"
3425 "\"processNum\":{"
3426 "\"type\":\"integer\","
3427 "\"minimum\":1"
3428 "},"
3429 "\"tags\":{"
3430 "\"type\":\"object\","
3431 "\"origin\":{"
3432 "\"type\":\"string\","
3433 "\"enum\":[\"Metric\",\"Status\",\"Key\","
3434 "\"Config\",\"Product\",\"Unknown\"]"
3435 "},"
3436 "\"nature\":{"
3437 "\"type\":\"string\","
3438 "\"enum\":[\"Gauge\",\"Limit\",\"Min\",\"Max\","
3439 "\"Rate\",\"Counter\",\"Duration\","
3440 "\"Age\",\"Time\",\"Name\",\"Output\","
3441 "\"Avg\", \"Unknown\"]"
3442 "},"
3443 "\"scope\":{"
3444 "\"type\":\"string\","
3445 "\"enum\":[\"Cluster\",\"Process\",\"Service\","
3446 "\"System\",\"Unknown\"]"
3447 "},"
3448 "\"required\":[\"origin\",\"nature\",\"scope\"]"
3449 "},"
3450 "\"typedValue\":{"
3451 "\"type\":\"object\","
3452 "\"oneOf\":["
3453 "{\"$ref\":\"#/definitions/typedValue/definitions/s32Value\"},"
3454 "{\"$ref\":\"#/definitions/typedValue/definitions/s64Value\"},"
3455 "{\"$ref\":\"#/definitions/typedValue/definitions/u32Value\"},"
3456 "{\"$ref\":\"#/definitions/typedValue/definitions/u64Value\"},"
3457 "{\"$ref\":\"#/definitions/typedValue/definitions/strValue\"}"
3458 "],"
3459 "\"definitions\":{"
3460 "\"s32Value\":{"
3461 "\"properties\":{"
3462 "\"type\":{"
3463 "\"type\":\"string\","
3464 "\"enum\":[\"s32\"]"
3465 "},"
3466 "\"value\":{"
3467 "\"type\":\"integer\","
3468 "\"minimum\":-2147483648,"
3469 "\"maximum\":2147483647"
3470 "}"
3471 "},"
3472 "\"required\":[\"type\",\"value\"]"
3473 "},"
3474 "\"s64Value\":{"
3475 "\"properties\":{"
3476 "\"type\":{"
3477 "\"type\":\"string\","
3478 "\"enum\":[\"s64\"]"
3479 "},"
3480 "\"value\":{"
3481 "\"type\":\"integer\","
3482 "\"minimum\":-9007199254740991,"
3483 "\"maximum\":9007199254740991"
3484 "}"
3485 "},"
3486 "\"required\":[\"type\",\"value\"]"
3487 "},"
3488 "\"u32Value\":{"
3489 "\"properties\":{"
3490 "\"type\":{"
3491 "\"type\":\"string\","
3492 "\"enum\":[\"u32\"]"
3493 "},"
3494 "\"value\":{"
3495 "\"type\":\"integer\","
3496 "\"minimum\":0,"
3497 "\"maximum\":4294967295"
3498 "}"
3499 "},"
3500 "\"required\":[\"type\",\"value\"]"
3501 "},"
3502 "\"u64Value\":{"
3503 "\"properties\":{"
3504 "\"type\":{"
3505 "\"type\":\"string\","
3506 "\"enum\":[\"u64\"]"
3507 "},"
3508 "\"value\":{"
3509 "\"type\":\"integer\","
3510 "\"minimum\":0,"
3511 "\"maximum\":9007199254740991"
3512 "}"
3513 "},"
3514 "\"required\":[\"type\",\"value\"]"
3515 "},"
3516 "\"strValue\":{"
3517 "\"properties\":{"
3518 "\"type\":{"
3519 "\"type\":\"string\","
3520 "\"enum\":[\"str\"]"
3521 "},"
3522 "\"value\":{\"type\":\"string\"}"
3523 "},"
3524 "\"required\":[\"type\",\"value\"]"
3525 "},"
3526 "\"unknownValue\":{"
3527 "\"properties\":{"
3528 "\"type\":{"
3529 "\"type\":\"integer\","
3530 "\"minimum\":0"
3531 "},"
3532 "\"value\":{"
3533 "\"type\":\"string\","
3534 "\"enum\":[\"unknown\"]"
3535 "}"
3536 "},"
3537 "\"required\":[\"type\",\"value\"]"
3538 "}"
3539 "}"
3540 "}"
3541 "}"
3542 "}");
3543
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003544 if (old_len == out->data) {
Simon Horman6f6bb382017-01-04 09:37:26 +01003545 chunk_reset(out);
3546 chunk_appendf(out,
3547 "{\"errorStr\":\"output buffer too short\"}");
3548 }
3549}
3550
3551/* This function dumps the schema onto the stream interface's read buffer.
3552 * It returns 0 as long as it does not complete, non-zero upon completion.
3553 * No state is used.
3554 */
3555static int stats_dump_json_schema_to_buffer(struct stream_interface *si)
3556{
3557 chunk_reset(&trash);
3558
3559 stats_dump_json_schema(&trash);
3560
Willy Tarreau06d80a92017-10-19 14:32:15 +02003561 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01003562 si_cant_put(si);
Simon Horman6f6bb382017-01-04 09:37:26 +01003563 return 0;
3564 }
3565
3566 return 1;
3567}
3568
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02003569static int cli_parse_clear_counters(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau89d467c2016-11-23 11:02:40 +01003570{
3571 struct proxy *px;
3572 struct server *sv;
3573 struct listener *li;
3574 int clrall = 0;
3575
3576 if (strcmp(args[2], "all") == 0)
3577 clrall = 1;
3578
3579 /* check permissions */
3580 if (!cli_has_level(appctx, ACCESS_LVL_OPER) ||
3581 (clrall && !cli_has_level(appctx, ACCESS_LVL_ADMIN)))
3582 return 1;
3583
Olivier Houchardfbc74e82017-11-24 16:54:05 +01003584 for (px = proxies_list; px; px = px->next) {
Willy Tarreau89d467c2016-11-23 11:02:40 +01003585 if (clrall) {
3586 memset(&px->be_counters, 0, sizeof(px->be_counters));
3587 memset(&px->fe_counters, 0, sizeof(px->fe_counters));
3588 }
3589 else {
3590 px->be_counters.conn_max = 0;
3591 px->be_counters.p.http.rps_max = 0;
3592 px->be_counters.sps_max = 0;
3593 px->be_counters.cps_max = 0;
3594 px->be_counters.nbpend_max = 0;
3595
3596 px->fe_counters.conn_max = 0;
3597 px->fe_counters.p.http.rps_max = 0;
3598 px->fe_counters.sps_max = 0;
3599 px->fe_counters.cps_max = 0;
Willy Tarreau89d467c2016-11-23 11:02:40 +01003600 }
3601
3602 for (sv = px->srv; sv; sv = sv->next)
3603 if (clrall)
3604 memset(&sv->counters, 0, sizeof(sv->counters));
3605 else {
3606 sv->counters.cur_sess_max = 0;
3607 sv->counters.nbpend_max = 0;
3608 sv->counters.sps_max = 0;
3609 }
3610
3611 list_for_each_entry(li, &px->conf.listeners, by_fe)
3612 if (li->counters) {
3613 if (clrall)
3614 memset(li->counters, 0, sizeof(*li->counters));
3615 else
3616 li->counters->conn_max = 0;
3617 }
3618 }
3619
3620 global.cps_max = 0;
3621 global.sps_max = 0;
Olivier Houchard00bc3cb2017-10-17 19:23:25 +02003622 global.ssl_max = 0;
3623 global.ssl_fe_keys_max = 0;
3624 global.ssl_be_keys_max = 0;
Willy Tarreaud80cb4e2018-01-20 19:30:13 +01003625
3626 memset(activity, 0, sizeof(activity));
Willy Tarreau89d467c2016-11-23 11:02:40 +01003627 return 1;
3628}
3629
3630
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02003631static int cli_parse_show_info(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003632{
Willy Tarreaud25fc792016-12-16 12:33:47 +01003633 appctx->ctx.stats.scope_str = 0;
3634 appctx->ctx.stats.scope_len = 0;
3635 appctx->ctx.stats.flags = 0;
3636
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003637 if (strcmp(args[2], "typed") == 0)
3638 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
Simon Horman05ee2132017-01-04 09:37:25 +01003639 else if (strcmp(args[2], "json") == 0)
3640 appctx->ctx.stats.flags |= STAT_FMT_JSON;
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003641 return 0;
3642}
3643
3644
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02003645static int cli_parse_show_stat(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau2b812e22016-11-22 16:18:05 +01003646{
Willy Tarreaud25fc792016-12-16 12:33:47 +01003647 appctx->ctx.stats.scope_str = 0;
3648 appctx->ctx.stats.scope_len = 0;
3649 appctx->ctx.stats.flags = 0;
3650
Willy Tarreau2b812e22016-11-22 16:18:05 +01003651 if (*args[2] && *args[3] && *args[4]) {
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01003652 struct proxy *px;
3653
3654 px = proxy_find_by_name(args[2], 0, 0);
3655 if (px)
3656 appctx->ctx.stats.iid = px->uuid;
3657 else
3658 appctx->ctx.stats.iid = atoi(args[2]);
3659
3660 if (!appctx->ctx.stats.iid) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02003661 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01003662 appctx->ctx.cli.msg = "No such proxy.\n";
3663 appctx->st0 = CLI_ST_PRINT;
3664 return 1;
3665 }
3666
Willy Tarreau2b812e22016-11-22 16:18:05 +01003667 appctx->ctx.stats.flags |= STAT_BOUND;
Willy Tarreau2b812e22016-11-22 16:18:05 +01003668 appctx->ctx.stats.type = atoi(args[3]);
3669 appctx->ctx.stats.sid = atoi(args[4]);
3670 if (strcmp(args[5], "typed") == 0)
3671 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
Simon Horman05ee2132017-01-04 09:37:25 +01003672 else if (strcmp(args[5], "json") == 0)
3673 appctx->ctx.stats.flags |= STAT_FMT_JSON;
Willy Tarreau2b812e22016-11-22 16:18:05 +01003674 }
3675 else if (strcmp(args[2], "typed") == 0)
3676 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
Simon Horman05ee2132017-01-04 09:37:25 +01003677 else if (strcmp(args[2], "json") == 0)
3678 appctx->ctx.stats.flags |= STAT_FMT_JSON;
Willy Tarreau2b812e22016-11-22 16:18:05 +01003679
Willy Tarreau2b812e22016-11-22 16:18:05 +01003680 return 0;
3681}
3682
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003683static int cli_io_handler_dump_info(struct appctx *appctx)
3684{
3685 return stats_dump_info_to_buffer(appctx->owner);
3686}
3687
Willy Tarreau2b812e22016-11-22 16:18:05 +01003688/* This I/O handler runs as an applet embedded in a stream interface. It is
3689 * used to send raw stats over a socket.
3690 */
3691static int cli_io_handler_dump_stat(struct appctx *appctx)
3692{
3693 return stats_dump_stat_to_buffer(appctx->owner, NULL);
3694}
3695
Simon Horman6f6bb382017-01-04 09:37:26 +01003696static int cli_io_handler_dump_json_schema(struct appctx *appctx)
3697{
3698 return stats_dump_json_schema_to_buffer(appctx->owner);
3699}
3700
Willy Tarreau2b812e22016-11-22 16:18:05 +01003701/* register cli keywords */
3702static struct cli_kw_list cli_kws = {{ },{
Willy Tarreau89d467c2016-11-23 11:02:40 +01003703 { { "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 +01003704 { { "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 +01003705 { { "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 +01003706 { { "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 +01003707 {{},}
3708}};
3709
William Lallemand74c24fb2016-11-21 17:18:36 +01003710struct applet http_stats_applet = {
3711 .obj_type = OBJ_TYPE_APPLET,
3712 .name = "<STATS>", /* used for logging */
3713 .fct = http_stats_io_handler,
3714 .release = NULL,
3715};
3716
Willy Tarreau2b812e22016-11-22 16:18:05 +01003717__attribute__((constructor))
3718static void __stat_init(void)
3719{
3720 cli_register_kw(&cli_kws);
3721}
3722
William Lallemand74c24fb2016-11-21 17:18:36 +01003723/*
3724 * Local variables:
3725 * c-indent-level: 8
3726 * c-basic-offset: 8
3727 * End:
3728 */