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