blob: c0c1b35a7619db3b8b564541c380d850f4b6b167 [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 */
270int stats_emit_raw_data_field(struct chunk *out, const struct field *f)
271{
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 */
287int stats_emit_typed_data_field(struct chunk *out, const struct field *f)
288{
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 */
309int stats_emit_json_data_field(struct chunk *out, const struct field *f)
310{
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
346 old_len = out->len;
347 chunk_appendf(out, ",\"value\":{\"type\":%s,\"value\":%s%s%s}",
348 type, quote, value, quote);
349 return !(old_len == out->len);
350}
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 */
355int stats_emit_field_tags(struct chunk *out, const struct field *f, char delim)
356{
357 char origin, nature, scope;
358
359 switch (field_origin(f, 0)) {
360 case FO_METRIC: origin = 'M'; break;
361 case FO_STATUS: origin = 'S'; break;
362 case FO_KEY: origin = 'K'; break;
363 case FO_CONFIG: origin = 'C'; break;
364 case FO_PRODUCT: origin = 'P'; break;
365 default: origin = '?'; break;
366 }
367
368 switch (field_nature(f, 0)) {
369 case FN_GAUGE: nature = 'G'; break;
370 case FN_LIMIT: nature = 'L'; break;
371 case FN_MIN: nature = 'm'; break;
372 case FN_MAX: nature = 'M'; break;
373 case FN_RATE: nature = 'R'; break;
374 case FN_COUNTER: nature = 'C'; break;
375 case FN_DURATION: nature = 'D'; break;
376 case FN_AGE: nature = 'A'; break;
377 case FN_TIME: nature = 'T'; break;
378 case FN_NAME: nature = 'N'; break;
379 case FN_OUTPUT: nature = 'O'; break;
380 case FN_AVG: nature = 'a'; break;
381 default: nature = '?'; break;
382 }
383
384 switch (field_scope(f, 0)) {
385 case FS_PROCESS: scope = 'P'; break;
386 case FS_SERVICE: scope = 'S'; break;
387 case FS_SYSTEM: scope = 's'; break;
388 case FS_CLUSTER: scope = 'C'; break;
389 default: scope = '?'; break;
390 }
391
392 return chunk_appendf(out, "%c%c%c%c", origin, nature, scope, delim);
393}
394
Simon Horman05ee2132017-01-04 09:37:25 +0100395/* Emits an encoding of the field type as JSON.
396 * Returns non-zero on success, 0 if the buffer is full.
397 */
398int stats_emit_json_field_tags(struct chunk *out, const struct field *f)
399{
400 const char *origin, *nature, *scope;
401 int old_len;
402
403 switch (field_origin(f, 0)) {
404 case FO_METRIC: origin = "Metric"; break;
405 case FO_STATUS: origin = "Status"; break;
406 case FO_KEY: origin = "Key"; break;
407 case FO_CONFIG: origin = "Config"; break;
408 case FO_PRODUCT: origin = "Product"; break;
409 default: origin = "Unknown"; break;
410 }
411
412 switch (field_nature(f, 0)) {
413 case FN_GAUGE: nature = "Gauge"; break;
414 case FN_LIMIT: nature = "Limit"; break;
415 case FN_MIN: nature = "Min"; break;
416 case FN_MAX: nature = "Max"; break;
417 case FN_RATE: nature = "Rate"; break;
418 case FN_COUNTER: nature = "Counter"; break;
419 case FN_DURATION: nature = "Duration"; break;
420 case FN_AGE: nature = "Age"; break;
421 case FN_TIME: nature = "Time"; break;
422 case FN_NAME: nature = "Name"; break;
423 case FN_OUTPUT: nature = "Output"; break;
424 case FN_AVG: nature = "Avg"; break;
425 default: nature = "Unknown"; break;
426 }
427
428 switch (field_scope(f, 0)) {
429 case FS_PROCESS: scope = "Process"; break;
430 case FS_SERVICE: scope = "Service"; break;
431 case FS_SYSTEM: scope = "System"; break;
432 case FS_CLUSTER: scope = "Cluster"; break;
433 default: scope = "Unknown"; break;
434 }
435
436 old_len = out->len;
437 chunk_appendf(out, "\"tags\":{"
438 "\"origin\":\"%s\","
439 "\"nature\":\"%s\","
440 "\"scope\":\"%s\""
441 "}", origin, nature, scope);
442 return !(old_len == out->len);
443}
William Lallemand74c24fb2016-11-21 17:18:36 +0100444
445/* Dump all fields from <stats> into <out> using CSV format */
446static int stats_dump_fields_csv(struct chunk *out, const struct field *stats)
447{
448 int field;
449
450 for (field = 0; field < ST_F_TOTAL_FIELDS; field++) {
451 if (!stats_emit_raw_data_field(out, &stats[field]))
452 return 0;
453 if (!chunk_strcat(out, ","))
454 return 0;
455 }
456 chunk_strcat(out, "\n");
457 return 1;
458}
459
460/* Dump all fields from <stats> into <out> using a typed "field:desc:type:value" format */
461static int stats_dump_fields_typed(struct chunk *out, const struct field *stats)
462{
463 int field;
464
465 for (field = 0; field < ST_F_TOTAL_FIELDS; field++) {
466 if (!stats[field].type)
467 continue;
468
469 chunk_appendf(out, "%c.%u.%u.%d.%s.%u:",
470 stats[ST_F_TYPE].u.u32 == STATS_TYPE_FE ? 'F' :
471 stats[ST_F_TYPE].u.u32 == STATS_TYPE_BE ? 'B' :
472 stats[ST_F_TYPE].u.u32 == STATS_TYPE_SO ? 'L' :
473 stats[ST_F_TYPE].u.u32 == STATS_TYPE_SV ? 'S' :
474 '?',
475 stats[ST_F_IID].u.u32, stats[ST_F_SID].u.u32,
476 field, stat_field_names[field], stats[ST_F_PID].u.u32);
477
478 if (!stats_emit_field_tags(out, &stats[field], ':'))
479 return 0;
480 if (!stats_emit_typed_data_field(out, &stats[field]))
481 return 0;
482 if (!chunk_strcat(out, "\n"))
483 return 0;
484 }
485 return 1;
486}
487
Simon Horman05ee2132017-01-04 09:37:25 +0100488/* Dump all fields from <stats> into <out> using the "show info json" format */
489static int stats_dump_json_info_fields(struct chunk *out,
490 const struct field *info)
491{
492 int field;
493 int started = 0;
494
495 if (!chunk_strcat(out, "["))
496 return 0;
497
498 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
499 int old_len;
500
501 if (!field_format(info, field))
502 continue;
503
504 if (started && !chunk_strcat(out, ","))
505 goto err;
506 started = 1;
507
508 old_len = out->len;
509 chunk_appendf(out,
510 "{\"field\":{\"pos\":%d,\"name\":\"%s\"},"
511 "\"processNum\":%u,",
512 field, info_field_names[field],
513 info[INF_PROCESS_NUM].u.u32);
514 if (old_len == out->len)
515 goto err;
516
517 if (!stats_emit_json_field_tags(out, &info[field]))
518 goto err;
519
520 if (!stats_emit_json_data_field(out, &info[field]))
521 goto err;
522
523 if (!chunk_strcat(out, "}"))
524 goto err;
525 }
526
527 if (!chunk_strcat(out, "]"))
528 goto err;
529 return 1;
530
531err:
532 chunk_reset(out);
533 chunk_appendf(out, "{\"errorStr\":\"output buffer too short\"}");
534 return 0;
535}
536
537/* Dump all fields from <stats> into <out> using a typed "field:desc:type:value" format */
538static int stats_dump_fields_json(struct chunk *out, const struct field *stats,
539 int first_stat)
540{
541 int field;
542 int started = 0;
543
544 if (!first_stat && !chunk_strcat(out, ","))
545 return 0;
546 if (!chunk_strcat(out, "["))
547 return 0;
548
549 for (field = 0; field < ST_F_TOTAL_FIELDS; field++) {
550 const char *obj_type;
551 int old_len;
552
553 if (!stats[field].type)
554 continue;
555
556 if (started && !chunk_strcat(out, ","))
557 goto err;
558 started = 1;
559
560 switch (stats[ST_F_TYPE].u.u32) {
561 case STATS_TYPE_FE: obj_type = "Frontend"; break;
562 case STATS_TYPE_BE: obj_type = "Backend"; break;
563 case STATS_TYPE_SO: obj_type = "Listener"; break;
564 case STATS_TYPE_SV: obj_type = "Server"; break;
565 default: obj_type = "Unknown"; break;
566 }
567
568 old_len = out->len;
569 chunk_appendf(out,
570 "{"
571 "\"objType\":\"%s\","
572 "\"proxyId\":%d,"
573 "\"id\":%d,"
574 "\"field\":{\"pos\":%d,\"name\":\"%s\"},"
575 "\"processNum\":%u,",
576 obj_type, stats[ST_F_IID].u.u32,
577 stats[ST_F_SID].u.u32, field,
578 stat_field_names[field], stats[ST_F_PID].u.u32);
579 if (old_len == out->len)
580 goto err;
581
582 if (!stats_emit_json_field_tags(out, &stats[field]))
583 goto err;
584
585 if (!stats_emit_json_data_field(out, &stats[field]))
586 goto err;
587
588 if (!chunk_strcat(out, "}"))
589 goto err;
590 }
591
592 if (!chunk_strcat(out, "]"))
593 goto err;
594
595 return 1;
596
597err:
598 chunk_reset(out);
599 if (!first_stat)
600 chunk_strcat(out, ",");
601 chunk_appendf(out, "{\"errorStr\":\"output buffer too short\"}");
602 return 0;
603}
604
William Lallemand74c24fb2016-11-21 17:18:36 +0100605/* Dump all fields from <stats> into <out> using the HTML format. A column is
606 * reserved for the checkbox is ST_SHOWADMIN is set in <flags>. Some extra info
607 * are provided if ST_SHLGNDS is present in <flags>.
608 */
609static int stats_dump_fields_html(struct chunk *out, const struct field *stats, unsigned int flags)
610{
611 struct chunk src;
612
613 if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_FE) {
614 chunk_appendf(out,
615 /* name, queue */
616 "<tr class=\"frontend\">");
617
618 if (flags & ST_SHOWADMIN) {
619 /* Column sub-heading for Enable or Disable server */
620 chunk_appendf(out, "<td></td>");
621 }
622
623 chunk_appendf(out,
624 "<td class=ac>"
625 "<a name=\"%s/Frontend\"></a>"
626 "<a class=lfsb href=\"#%s/Frontend\">Frontend</a></td>"
627 "<td colspan=3></td>"
628 "",
629 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_PXNAME));
630
631 chunk_appendf(out,
632 /* sessions rate : current */
633 "<td><u>%s<div class=tips><table class=det>"
634 "<tr><th>Current connection rate:</th><td>%s/s</td></tr>"
635 "<tr><th>Current session rate:</th><td>%s/s</td></tr>"
636 "",
637 U2H(stats[ST_F_RATE].u.u32),
638 U2H(stats[ST_F_CONN_RATE].u.u32),
639 U2H(stats[ST_F_RATE].u.u32));
640
641 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
642 chunk_appendf(out,
643 "<tr><th>Current request rate:</th><td>%s/s</td></tr>",
644 U2H(stats[ST_F_REQ_RATE].u.u32));
645
646 chunk_appendf(out,
647 "</table></div></u></td>"
648 /* sessions rate : max */
649 "<td><u>%s<div class=tips><table class=det>"
650 "<tr><th>Max connection rate:</th><td>%s/s</td></tr>"
651 "<tr><th>Max session rate:</th><td>%s/s</td></tr>"
652 "",
653 U2H(stats[ST_F_RATE_MAX].u.u32),
654 U2H(stats[ST_F_CONN_RATE_MAX].u.u32),
655 U2H(stats[ST_F_RATE_MAX].u.u32));
656
657 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
658 chunk_appendf(out,
659 "<tr><th>Max request rate:</th><td>%s/s</td></tr>",
660 U2H(stats[ST_F_REQ_RATE_MAX].u.u32));
661
662 chunk_appendf(out,
663 "</table></div></u></td>"
664 /* sessions rate : limit */
665 "<td>%s</td>",
666 LIM2A(stats[ST_F_RATE_LIM].u.u32, "-"));
667
668 chunk_appendf(out,
669 /* sessions: current, max, limit, total */
670 "<td>%s</td><td>%s</td><td>%s</td>"
671 "<td><u>%s<div class=tips><table class=det>"
672 "<tr><th>Cum. connections:</th><td>%s</td></tr>"
673 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
674 "",
675 U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SMAX].u.u32), U2H(stats[ST_F_SLIM].u.u32),
676 U2H(stats[ST_F_STOT].u.u64),
677 U2H(stats[ST_F_CONN_TOT].u.u64),
678 U2H(stats[ST_F_STOT].u.u64));
679
680 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
681 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
682 chunk_appendf(out,
683 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
684 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
685 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
686 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
687 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
688 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
689 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
690 "<tr><th>- other responses:</th><td>%s</td></tr>"
691 "<tr><th>Intercepted requests:</th><td>%s</td></tr>"
Willy Tarreau1b0f85e2018-05-28 15:12:40 +0200692 "<tr><th>Failed hdr rewrites:</th><td>%s</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +0100693 "",
694 U2H(stats[ST_F_REQ_TOT].u.u64),
695 U2H(stats[ST_F_HRSP_1XX].u.u64),
696 U2H(stats[ST_F_HRSP_2XX].u.u64),
697 U2H(stats[ST_F_COMP_RSP].u.u64),
698 stats[ST_F_HRSP_2XX].u.u64 ?
699 (int)(100 * stats[ST_F_COMP_RSP].u.u64 / stats[ST_F_HRSP_2XX].u.u64) : 0,
700 U2H(stats[ST_F_HRSP_3XX].u.u64),
701 U2H(stats[ST_F_HRSP_4XX].u.u64),
702 U2H(stats[ST_F_HRSP_5XX].u.u64),
703 U2H(stats[ST_F_HRSP_OTHER].u.u64),
Willy Tarreau1b0f85e2018-05-28 15:12:40 +0200704 U2H(stats[ST_F_INTERCEPTED].u.u64),
705 U2H(stats[ST_F_WREW].u.u64));
William Lallemand74c24fb2016-11-21 17:18:36 +0100706 }
707
708 chunk_appendf(out,
709 "</table></div></u></td>"
710 /* sessions: lbtot, lastsess */
711 "<td></td><td></td>"
712 /* bytes : in */
713 "<td>%s</td>"
714 "",
715 U2H(stats[ST_F_BIN].u.u64));
716
717 chunk_appendf(out,
718 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
719 "<td>%s%s<div class=tips><table class=det>"
720 "<tr><th>Response bytes in:</th><td>%s</td></tr>"
721 "<tr><th>Compression in:</th><td>%s</td></tr>"
722 "<tr><th>Compression out:</th><td>%s</td><td>(%d%%)</td></tr>"
723 "<tr><th>Compression bypass:</th><td>%s</td></tr>"
724 "<tr><th>Total bytes saved:</th><td>%s</td><td>(%d%%)</td></tr>"
725 "</table></div>%s</td>",
726 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "<u>":"",
727 U2H(stats[ST_F_BOUT].u.u64),
728 U2H(stats[ST_F_BOUT].u.u64),
729 U2H(stats[ST_F_COMP_IN].u.u64),
730 U2H(stats[ST_F_COMP_OUT].u.u64),
731 stats[ST_F_COMP_IN].u.u64 ? (int)(stats[ST_F_COMP_OUT].u.u64 * 100 / stats[ST_F_COMP_IN].u.u64) : 0,
732 U2H(stats[ST_F_COMP_BYP].u.u64),
733 U2H(stats[ST_F_COMP_IN].u.u64 - stats[ST_F_COMP_OUT].u.u64),
734 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,
735 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "</u>":"");
736
737 chunk_appendf(out,
738 /* denied: req, resp */
739 "<td>%s</td><td>%s</td>"
740 /* errors : request, connect, response */
741 "<td>%s</td><td></td><td></td>"
742 /* warnings: retries, redispatches */
743 "<td></td><td></td>"
744 /* server status : reflect frontend status */
745 "<td class=ac>%s</td>"
746 /* rest of server: nothing */
747 "<td class=ac colspan=8></td></tr>"
748 "",
749 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
750 U2H(stats[ST_F_EREQ].u.u64),
751 field_str(stats, ST_F_STATUS));
752 }
753 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_SO) {
754 chunk_appendf(out, "<tr class=socket>");
755 if (flags & ST_SHOWADMIN) {
756 /* Column sub-heading for Enable or Disable server */
757 chunk_appendf(out, "<td></td>");
758 }
759
760 chunk_appendf(out,
761 /* frontend name, listener name */
762 "<td class=ac><a name=\"%s/+%s\"></a>%s"
763 "<a class=lfsb href=\"#%s/+%s\">%s</a>"
764 "",
765 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME),
766 (flags & ST_SHLGNDS)?"<u>":"",
767 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME), field_str(stats, ST_F_SVNAME));
768
769 if (flags & ST_SHLGNDS) {
770 chunk_appendf(out, "<div class=tips>");
771
772 if (isdigit(*field_str(stats, ST_F_ADDR)))
773 chunk_appendf(out, "IPv4: %s, ", field_str(stats, ST_F_ADDR));
774 else if (*field_str(stats, ST_F_ADDR) == '[')
775 chunk_appendf(out, "IPv6: %s, ", field_str(stats, ST_F_ADDR));
776 else if (*field_str(stats, ST_F_ADDR))
777 chunk_appendf(out, "%s, ", field_str(stats, ST_F_ADDR));
778
779 /* id */
780 chunk_appendf(out, "id: %d</div>", stats[ST_F_SID].u.u32);
781 }
782
783 chunk_appendf(out,
784 /* queue */
785 "%s</td><td colspan=3></td>"
786 /* sessions rate: current, max, limit */
787 "<td colspan=3>&nbsp;</td>"
788 /* sessions: current, max, limit, total, lbtot, lastsess */
789 "<td>%s</td><td>%s</td><td>%s</td>"
790 "<td>%s</td><td>&nbsp;</td><td>&nbsp;</td>"
791 /* bytes: in, out */
792 "<td>%s</td><td>%s</td>"
793 "",
794 (flags & ST_SHLGNDS)?"</u>":"",
795 U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SMAX].u.u32), U2H(stats[ST_F_SLIM].u.u32),
796 U2H(stats[ST_F_STOT].u.u64), U2H(stats[ST_F_BIN].u.u64), U2H(stats[ST_F_BOUT].u.u64));
797
798 chunk_appendf(out,
799 /* denied: req, resp */
800 "<td>%s</td><td>%s</td>"
801 /* errors: request, connect, response */
802 "<td>%s</td><td></td><td></td>"
803 /* warnings: retries, redispatches */
804 "<td></td><td></td>"
805 /* server status: reflect listener status */
806 "<td class=ac>%s</td>"
807 /* rest of server: nothing */
808 "<td class=ac colspan=8></td></tr>"
809 "",
810 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
811 U2H(stats[ST_F_EREQ].u.u64),
812 field_str(stats, ST_F_STATUS));
813 }
814 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_SV) {
815 const char *style;
816
817 /* determine the style to use depending on the server's state,
818 * its health and weight. There isn't a 1-to-1 mapping between
819 * state and styles for the cases where the server is (still)
820 * up. The reason is that we don't want to report nolb and
821 * drain with the same color.
822 */
823
824 if (strcmp(field_str(stats, ST_F_STATUS), "DOWN") == 0 ||
825 strcmp(field_str(stats, ST_F_STATUS), "DOWN (agent)") == 0) {
826 style = "down";
827 }
828 else if (strcmp(field_str(stats, ST_F_STATUS), "DOWN ") == 0) {
829 style = "going_up";
830 }
831 else if (strcmp(field_str(stats, ST_F_STATUS), "NOLB ") == 0) {
832 style = "going_down";
833 }
834 else if (strcmp(field_str(stats, ST_F_STATUS), "NOLB") == 0) {
835 style = "nolb";
836 }
837 else if (strcmp(field_str(stats, ST_F_STATUS), "no check") == 0) {
838 style = "no_check";
839 }
840 else if (!stats[ST_F_CHKFAIL].type ||
841 stats[ST_F_CHECK_HEALTH].u.u32 == stats[ST_F_CHECK_RISE].u.u32 + stats[ST_F_CHECK_FALL].u.u32 - 1) {
842 /* no check or max health = UP */
843 if (stats[ST_F_WEIGHT].u.u32)
844 style = "up";
845 else
846 style = "draining";
847 }
848 else {
849 style = "going_down";
850 }
851
852 if (memcmp(field_str(stats, ST_F_STATUS), "MAINT", 5) == 0)
853 chunk_appendf(out, "<tr class=\"maintain\">");
854 else
855 chunk_appendf(out,
856 "<tr class=\"%s_%s\">",
857 (stats[ST_F_BCK].u.u32) ? "backup" : "active", style);
858
859
860 if (flags & ST_SHOWADMIN)
861 chunk_appendf(out,
David Harrigand3db35a2016-12-30 12:12:49 +0000862 "<td><input class='%s-checkbox' type=\"checkbox\" name=\"s\" value=\"%s\"></td>",
863 field_str(stats, ST_F_PXNAME),
William Lallemand74c24fb2016-11-21 17:18:36 +0100864 field_str(stats, ST_F_SVNAME));
865
866 chunk_appendf(out,
867 "<td class=ac><a name=\"%s/%s\"></a>%s"
868 "<a class=lfsb href=\"#%s/%s\">%s</a>"
869 "",
870 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME),
871 (flags & ST_SHLGNDS) ? "<u>" : "",
872 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_SVNAME), field_str(stats, ST_F_SVNAME));
873
874 if (flags & ST_SHLGNDS) {
875 chunk_appendf(out, "<div class=tips>");
876
877 if (isdigit(*field_str(stats, ST_F_ADDR)))
878 chunk_appendf(out, "IPv4: %s, ", field_str(stats, ST_F_ADDR));
879 else if (*field_str(stats, ST_F_ADDR) == '[')
880 chunk_appendf(out, "IPv6: %s, ", field_str(stats, ST_F_ADDR));
881 else if (*field_str(stats, ST_F_ADDR))
882 chunk_appendf(out, "%s, ", field_str(stats, ST_F_ADDR));
883
884 /* id */
885 chunk_appendf(out, "id: %d", stats[ST_F_SID].u.u32);
886
887 /* cookie */
888 if (stats[ST_F_COOKIE].type) {
889 chunk_appendf(out, ", cookie: '");
890 chunk_initstr(&src, field_str(stats, ST_F_COOKIE));
891 chunk_htmlencode(out, &src);
892 chunk_appendf(out, "'");
893 }
894
895 chunk_appendf(out, "</div>");
896 }
897
898 chunk_appendf(out,
899 /* queue : current, max, limit */
900 "%s</td><td>%s</td><td>%s</td><td>%s</td>"
901 /* sessions rate : current, max, limit */
902 "<td>%s</td><td>%s</td><td></td>"
903 "",
904 (flags & ST_SHLGNDS) ? "</u>" : "",
905 U2H(stats[ST_F_QCUR].u.u32), U2H(stats[ST_F_QMAX].u.u32), LIM2A(stats[ST_F_QLIMIT].u.u32, "-"),
906 U2H(stats[ST_F_RATE].u.u32), U2H(stats[ST_F_RATE_MAX].u.u32));
907
908 chunk_appendf(out,
909 /* sessions: current, max, limit, total */
910 "<td>%s</td><td>%s</td><td>%s</td>"
911 "<td><u>%s<div class=tips><table class=det>"
912 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
913 "",
914 U2H(stats[ST_F_SCUR].u.u32), U2H(stats[ST_F_SMAX].u.u32), LIM2A(stats[ST_F_SLIM].u.u32, "-"),
915 U2H(stats[ST_F_STOT].u.u64),
916 U2H(stats[ST_F_STOT].u.u64));
917
918 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
919 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
920 unsigned long long tot;
921
922 tot = stats[ST_F_HRSP_OTHER].u.u64;
923 tot += stats[ST_F_HRSP_1XX].u.u64;
924 tot += stats[ST_F_HRSP_2XX].u.u64;
925 tot += stats[ST_F_HRSP_3XX].u.u64;
926 tot += stats[ST_F_HRSP_4XX].u.u64;
927 tot += stats[ST_F_HRSP_5XX].u.u64;
928
929 chunk_appendf(out,
930 "<tr><th>Cum. HTTP responses:</th><td>%s</td></tr>"
931 "<tr><th>- HTTP 1xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
932 "<tr><th>- HTTP 2xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
933 "<tr><th>- HTTP 3xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
934 "<tr><th>- HTTP 4xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
935 "<tr><th>- HTTP 5xx responses:</th><td>%s</td><td>(%d%%)</td></tr>"
936 "<tr><th>- other responses:</th><td>%s</td><td>(%d%%)</td></tr>"
Willy Tarreau1b0f85e2018-05-28 15:12:40 +0200937 "<tr><th>Failed hdr rewrites:</th><td>%s</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +0100938 "",
939 U2H(tot),
940 U2H(stats[ST_F_HRSP_1XX].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_1XX].u.u64 / tot) : 0,
941 U2H(stats[ST_F_HRSP_2XX].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_2XX].u.u64 / tot) : 0,
942 U2H(stats[ST_F_HRSP_3XX].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_3XX].u.u64 / tot) : 0,
943 U2H(stats[ST_F_HRSP_4XX].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_4XX].u.u64 / tot) : 0,
944 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 +0200945 U2H(stats[ST_F_HRSP_OTHER].u.u64), tot ? (int)(100 * stats[ST_F_HRSP_OTHER].u.u64 / tot) : 0,
946 U2H(stats[ST_F_WREW].u.u64));
William Lallemand74c24fb2016-11-21 17:18:36 +0100947 }
948
949 chunk_appendf(out, "<tr><th colspan=3>Avg over last 1024 success. conn.</th></tr>");
950 chunk_appendf(out, "<tr><th>- Queue time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_QTIME].u.u32));
951 chunk_appendf(out, "<tr><th>- Connect time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_CTIME].u.u32));
952 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
953 chunk_appendf(out, "<tr><th>- Response time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_RTIME].u.u32));
954 chunk_appendf(out, "<tr><th>- Total time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_TTIME].u.u32));
955
956 chunk_appendf(out,
957 "</table></div></u></td>"
958 /* sessions: lbtot, last */
959 "<td>%s</td><td>%s</td>",
960 U2H(stats[ST_F_LBTOT].u.u64),
961 human_time(stats[ST_F_LASTSESS].u.s32, 1));
962
963 chunk_appendf(out,
964 /* bytes : in, out */
965 "<td>%s</td><td>%s</td>"
966 /* denied: req, resp */
967 "<td></td><td>%s</td>"
968 /* errors : request, connect */
969 "<td></td><td>%s</td>"
970 /* errors : response */
971 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
972 /* warnings: retries, redispatches */
973 "<td>%lld</td><td>%lld</td>"
974 "",
975 U2H(stats[ST_F_BIN].u.u64), U2H(stats[ST_F_BOUT].u.u64),
976 U2H(stats[ST_F_DRESP].u.u64),
977 U2H(stats[ST_F_ECON].u.u64),
978 U2H(stats[ST_F_ERESP].u.u64),
979 (long long)stats[ST_F_CLI_ABRT].u.u64,
980 (long long)stats[ST_F_SRV_ABRT].u.u64,
981 (long long)stats[ST_F_WRETR].u.u64,
982 (long long)stats[ST_F_WREDIS].u.u64);
983
984 /* status, last change */
985 chunk_appendf(out, "<td class=ac>");
986
987 /* FIXME!!!!
988 * LASTCHG should contain the last change for *this* server and must be computed
989 * properly above, as was done below, ie: this server if maint, otherwise ref server
990 * if tracking. Note that ref is either local or remote depending on tracking.
991 */
992
993
994 if (memcmp(field_str(stats, ST_F_STATUS), "MAINT", 5) == 0) {
995 chunk_appendf(out, "%s MAINT", human_time(stats[ST_F_LASTCHG].u.u32, 1));
996 }
997 else if (memcmp(field_str(stats, ST_F_STATUS), "no check", 5) == 0) {
998 chunk_strcat(out, "<i>no check</i>");
999 }
1000 else {
1001 chunk_appendf(out, "%s %s", human_time(stats[ST_F_LASTCHG].u.u32, 1), field_str(stats, ST_F_STATUS));
1002 if (memcmp(field_str(stats, ST_F_STATUS), "DOWN", 4) == 0) {
1003 if (stats[ST_F_CHECK_HEALTH].u.u32)
1004 chunk_strcat(out, " &uarr;");
1005 }
1006 else if (stats[ST_F_CHECK_HEALTH].u.u32 < stats[ST_F_CHECK_RISE].u.u32 + stats[ST_F_CHECK_FALL].u.u32 - 1)
1007 chunk_strcat(out, " &darr;");
1008 }
1009
1010 if (memcmp(field_str(stats, ST_F_STATUS), "DOWN", 4) == 0 &&
1011 stats[ST_F_AGENT_STATUS].type && !stats[ST_F_AGENT_HEALTH].u.u32) {
1012 chunk_appendf(out,
1013 "</td><td class=ac><u> %s",
1014 field_str(stats, ST_F_AGENT_STATUS));
1015
1016 if (stats[ST_F_AGENT_CODE].type)
1017 chunk_appendf(out, "/%d", stats[ST_F_AGENT_CODE].u.u32);
1018
1019 if (stats[ST_F_AGENT_DURATION].type)
1020 chunk_appendf(out, " in %lums", (long)stats[ST_F_AGENT_DURATION].u.u64);
1021
1022 chunk_appendf(out, "<div class=tips>%s", field_str(stats, ST_F_AGENT_DESC));
1023
1024 if (*field_str(stats, ST_F_LAST_AGT)) {
1025 chunk_appendf(out, ": ");
1026 chunk_initstr(&src, field_str(stats, ST_F_LAST_AGT));
1027 chunk_htmlencode(out, &src);
1028 }
1029 chunk_appendf(out, "</div></u>");
1030 }
1031 else if (stats[ST_F_CHECK_STATUS].type) {
1032 chunk_appendf(out,
1033 "</td><td class=ac><u> %s",
1034 field_str(stats, ST_F_CHECK_STATUS));
1035
1036 if (stats[ST_F_CHECK_CODE].type)
1037 chunk_appendf(out, "/%d", stats[ST_F_CHECK_CODE].u.u32);
1038
1039 if (stats[ST_F_CHECK_DURATION].type)
1040 chunk_appendf(out, " in %lums", (long)stats[ST_F_CHECK_DURATION].u.u64);
1041
1042 chunk_appendf(out, "<div class=tips>%s", field_str(stats, ST_F_CHECK_DESC));
1043
1044 if (*field_str(stats, ST_F_LAST_CHK)) {
1045 chunk_appendf(out, ": ");
1046 chunk_initstr(&src, field_str(stats, ST_F_LAST_CHK));
1047 chunk_htmlencode(out, &src);
1048 }
1049 chunk_appendf(out, "</div></u>");
1050 }
1051 else
1052 chunk_appendf(out, "</td><td>");
1053
1054 chunk_appendf(out,
1055 /* weight */
1056 "</td><td class=ac>%d</td>"
1057 /* act, bck */
1058 "<td class=ac>%s</td><td class=ac>%s</td>"
1059 "",
1060 stats[ST_F_WEIGHT].u.u32,
1061 stats[ST_F_BCK].u.u32 ? "-" : "Y",
1062 stats[ST_F_BCK].u.u32 ? "Y" : "-");
1063
1064 /* check failures: unique, fatal, down time */
1065 if (strcmp(field_str(stats, ST_F_STATUS), "MAINT (resolution)") == 0) {
1066 chunk_appendf(out, "<td class=ac colspan=3>resolution</td>");
1067 }
1068 else if (stats[ST_F_CHKFAIL].type) {
1069 chunk_appendf(out, "<td><u>%lld", (long long)stats[ST_F_CHKFAIL].u.u64);
1070
1071 if (stats[ST_F_HANAFAIL].type)
1072 chunk_appendf(out, "/%lld", (long long)stats[ST_F_HANAFAIL].u.u64);
1073
1074 chunk_appendf(out,
1075 "<div class=tips>Failed Health Checks%s</div></u></td>"
1076 "<td>%lld</td><td>%s</td>"
1077 "",
1078 stats[ST_F_HANAFAIL].type ? "/Health Analyses" : "",
1079 (long long)stats[ST_F_CHKDOWN].u.u64, human_time(stats[ST_F_DOWNTIME].u.u32, 1));
1080 }
1081 else if (strcmp(field_str(stats, ST_F_STATUS), "MAINT") != 0 && field_format(stats, ST_F_TRACKED) == FF_STR) {
1082 /* tracking a server (hence inherited maint would appear as "MAINT (via...)" */
1083 chunk_appendf(out,
1084 "<td class=ac colspan=3><a class=lfsb href=\"#%s\">via %s</a></td>",
1085 field_str(stats, ST_F_TRACKED), field_str(stats, ST_F_TRACKED));
1086 }
1087 else
1088 chunk_appendf(out, "<td colspan=3></td>");
1089
1090 /* throttle */
1091 if (stats[ST_F_THROTTLE].type)
1092 chunk_appendf(out, "<td class=ac>%d %%</td></tr>\n", stats[ST_F_THROTTLE].u.u32);
1093 else
1094 chunk_appendf(out, "<td class=ac>-</td></tr>\n");
1095 }
1096 else if (stats[ST_F_TYPE].u.u32 == STATS_TYPE_BE) {
1097 chunk_appendf(out, "<tr class=\"backend\">");
1098 if (flags & ST_SHOWADMIN) {
1099 /* Column sub-heading for Enable or Disable server */
1100 chunk_appendf(out, "<td></td>");
1101 }
1102 chunk_appendf(out,
1103 "<td class=ac>"
1104 /* name */
1105 "%s<a name=\"%s/Backend\"></a>"
1106 "<a class=lfsb href=\"#%s/Backend\">Backend</a>"
1107 "",
1108 (flags & ST_SHLGNDS)?"<u>":"",
1109 field_str(stats, ST_F_PXNAME), field_str(stats, ST_F_PXNAME));
1110
1111 if (flags & ST_SHLGNDS) {
1112 /* balancing */
1113 chunk_appendf(out, "<div class=tips>balancing: %s",
1114 field_str(stats, ST_F_ALGO));
1115
1116 /* cookie */
1117 if (stats[ST_F_COOKIE].type) {
1118 chunk_appendf(out, ", cookie: '");
1119 chunk_initstr(&src, field_str(stats, ST_F_COOKIE));
1120 chunk_htmlencode(out, &src);
1121 chunk_appendf(out, "'");
1122 }
1123 chunk_appendf(out, "</div>");
1124 }
1125
1126 chunk_appendf(out,
1127 "%s</td>"
1128 /* queue : current, max */
1129 "<td>%s</td><td>%s</td><td></td>"
1130 /* sessions rate : current, max, limit */
1131 "<td>%s</td><td>%s</td><td></td>"
1132 "",
1133 (flags & ST_SHLGNDS)?"</u>":"",
1134 U2H(stats[ST_F_QCUR].u.u32), U2H(stats[ST_F_QMAX].u.u32),
1135 U2H(stats[ST_F_RATE].u.u32), U2H(stats[ST_F_RATE_MAX].u.u32));
1136
1137 chunk_appendf(out,
1138 /* sessions: current, max, limit, total */
1139 "<td>%s</td><td>%s</td><td>%s</td>"
1140 "<td><u>%s<div class=tips><table class=det>"
1141 "<tr><th>Cum. sessions:</th><td>%s</td></tr>"
1142 "",
Willy Tarreau8e0f1752016-12-12 15:07:29 +01001143 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 +01001144 U2H(stats[ST_F_STOT].u.u64),
1145 U2H(stats[ST_F_STOT].u.u64));
1146
1147 /* http response (via hover): 1xx, 2xx, 3xx, 4xx, 5xx, other */
1148 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0) {
1149 chunk_appendf(out,
1150 "<tr><th>Cum. HTTP requests:</th><td>%s</td></tr>"
1151 "<tr><th>- HTTP 1xx responses:</th><td>%s</td></tr>"
1152 "<tr><th>- HTTP 2xx responses:</th><td>%s</td></tr>"
1153 "<tr><th>&nbsp;&nbsp;Compressed 2xx:</th><td>%s</td><td>(%d%%)</td></tr>"
1154 "<tr><th>- HTTP 3xx responses:</th><td>%s</td></tr>"
1155 "<tr><th>- HTTP 4xx responses:</th><td>%s</td></tr>"
1156 "<tr><th>- HTTP 5xx responses:</th><td>%s</td></tr>"
1157 "<tr><th>- other responses:</th><td>%s</td></tr>"
Willy Tarreau1b0f85e2018-05-28 15:12:40 +02001158 "<tr><th>Failed hdr rewrites:</th><td>%s</td></tr>"
William Lallemand74c24fb2016-11-21 17:18:36 +01001159 "<tr><th colspan=3>Avg over last 1024 success. conn.</th></tr>"
1160 "",
1161 U2H(stats[ST_F_REQ_TOT].u.u64),
1162 U2H(stats[ST_F_HRSP_1XX].u.u64),
1163 U2H(stats[ST_F_HRSP_2XX].u.u64),
1164 U2H(stats[ST_F_COMP_RSP].u.u64),
1165 stats[ST_F_HRSP_2XX].u.u64 ?
1166 (int)(100 * stats[ST_F_COMP_RSP].u.u64 / stats[ST_F_HRSP_2XX].u.u64) : 0,
1167 U2H(stats[ST_F_HRSP_3XX].u.u64),
1168 U2H(stats[ST_F_HRSP_4XX].u.u64),
1169 U2H(stats[ST_F_HRSP_5XX].u.u64),
Willy Tarreau1b0f85e2018-05-28 15:12:40 +02001170 U2H(stats[ST_F_WREW].u.u64),
Willy Tarreauae9bea02016-11-25 14:44:52 +01001171 U2H(stats[ST_F_HRSP_OTHER].u.u64));
William Lallemand74c24fb2016-11-21 17:18:36 +01001172 }
1173
1174 chunk_appendf(out, "<tr><th>- Queue time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_QTIME].u.u32));
1175 chunk_appendf(out, "<tr><th>- Connect time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_CTIME].u.u32));
1176 if (strcmp(field_str(stats, ST_F_MODE), "http") == 0)
1177 chunk_appendf(out, "<tr><th>- Response time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_RTIME].u.u32));
1178 chunk_appendf(out, "<tr><th>- Total time:</th><td>%s</td><td>ms</td></tr>", U2H(stats[ST_F_TTIME].u.u32));
1179
1180 chunk_appendf(out,
1181 "</table></div></u></td>"
1182 /* sessions: lbtot, last */
1183 "<td>%s</td><td>%s</td>"
1184 /* bytes: in */
1185 "<td>%s</td>"
1186 "",
1187 U2H(stats[ST_F_LBTOT].u.u64),
1188 human_time(stats[ST_F_LASTSESS].u.s32, 1),
1189 U2H(stats[ST_F_BIN].u.u64));
1190
1191 chunk_appendf(out,
1192 /* bytes:out + compression stats (via hover): comp_in, comp_out, comp_byp */
1193 "<td>%s%s<div class=tips><table class=det>"
1194 "<tr><th>Response bytes in:</th><td>%s</td></tr>"
1195 "<tr><th>Compression in:</th><td>%s</td></tr>"
1196 "<tr><th>Compression out:</th><td>%s</td><td>(%d%%)</td></tr>"
1197 "<tr><th>Compression bypass:</th><td>%s</td></tr>"
1198 "<tr><th>Total bytes saved:</th><td>%s</td><td>(%d%%)</td></tr>"
1199 "</table></div>%s</td>",
1200 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "<u>":"",
1201 U2H(stats[ST_F_BOUT].u.u64),
1202 U2H(stats[ST_F_BOUT].u.u64),
1203 U2H(stats[ST_F_COMP_IN].u.u64),
1204 U2H(stats[ST_F_COMP_OUT].u.u64),
1205 stats[ST_F_COMP_IN].u.u64 ? (int)(stats[ST_F_COMP_OUT].u.u64 * 100 / stats[ST_F_COMP_IN].u.u64) : 0,
1206 U2H(stats[ST_F_COMP_BYP].u.u64),
1207 U2H(stats[ST_F_COMP_IN].u.u64 - stats[ST_F_COMP_OUT].u.u64),
1208 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,
1209 (stats[ST_F_COMP_IN].u.u64 || stats[ST_F_COMP_BYP].u.u64) ? "</u>":"");
1210
1211 chunk_appendf(out,
1212 /* denied: req, resp */
1213 "<td>%s</td><td>%s</td>"
1214 /* errors : request, connect */
1215 "<td></td><td>%s</td>"
1216 /* errors : response */
1217 "<td><u>%s<div class=tips>Connection resets during transfers: %lld client, %lld server</div></u></td>"
1218 /* warnings: retries, redispatches */
1219 "<td>%lld</td><td>%lld</td>"
1220 /* backend status: reflect backend status (up/down): we display UP
1221 * if the backend has known working servers or if it has no server at
1222 * all (eg: for stats). Then we display the total weight, number of
1223 * active and backups. */
1224 "<td class=ac>%s %s</td><td class=ac>&nbsp;</td><td class=ac>%d</td>"
1225 "<td class=ac>%d</td><td class=ac>%d</td>"
1226 "",
1227 U2H(stats[ST_F_DREQ].u.u64), U2H(stats[ST_F_DRESP].u.u64),
1228 U2H(stats[ST_F_ECON].u.u64),
1229 U2H(stats[ST_F_ERESP].u.u64),
1230 (long long)stats[ST_F_CLI_ABRT].u.u64,
1231 (long long)stats[ST_F_SRV_ABRT].u.u64,
1232 (long long)stats[ST_F_WRETR].u.u64, (long long)stats[ST_F_WREDIS].u.u64,
1233 human_time(stats[ST_F_LASTCHG].u.u32, 1),
1234 strcmp(field_str(stats, ST_F_STATUS), "DOWN") ? field_str(stats, ST_F_STATUS) : "<font color=\"red\"><b>DOWN</b></font>",
1235 stats[ST_F_WEIGHT].u.u32,
1236 stats[ST_F_ACT].u.u32, stats[ST_F_BCK].u.u32);
1237
1238 chunk_appendf(out,
1239 /* rest of backend: nothing, down transitions, total downtime, throttle */
1240 "<td class=ac>&nbsp;</td><td>%d</td>"
1241 "<td>%s</td>"
1242 "<td></td>"
1243 "</tr>",
1244 stats[ST_F_CHKDOWN].u.u32,
1245 stats[ST_F_DOWNTIME].type ? human_time(stats[ST_F_DOWNTIME].u.u32, 1) : "&nbsp;");
1246 }
1247 return 1;
1248}
1249
1250int stats_dump_one_line(const struct field *stats, unsigned int flags, struct proxy *px, struct appctx *appctx)
1251{
Simon Horman05ee2132017-01-04 09:37:25 +01001252 int ret;
1253
William Lallemand74c24fb2016-11-21 17:18:36 +01001254 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN))
1255 flags |= ST_SHOWADMIN;
1256
1257 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
Simon Horman05ee2132017-01-04 09:37:25 +01001258 ret = stats_dump_fields_html(&trash, stats, flags);
William Lallemand74c24fb2016-11-21 17:18:36 +01001259 else if (appctx->ctx.stats.flags & STAT_FMT_TYPED)
Simon Horman05ee2132017-01-04 09:37:25 +01001260 ret = stats_dump_fields_typed(&trash, stats);
1261 else if (appctx->ctx.stats.flags & STAT_FMT_JSON)
1262 ret = stats_dump_fields_json(&trash, stats,
1263 !(appctx->ctx.stats.flags &
1264 STAT_STARTED));
William Lallemand74c24fb2016-11-21 17:18:36 +01001265 else
Simon Horman05ee2132017-01-04 09:37:25 +01001266 ret = stats_dump_fields_csv(&trash, stats);
1267
1268 if (ret)
1269 appctx->ctx.stats.flags |= STAT_STARTED;
1270
1271 return ret;
William Lallemand74c24fb2016-11-21 17:18:36 +01001272}
1273
1274/* Fill <stats> with the frontend statistics. <stats> is
1275 * preallocated array of length <len>. The length of the array
1276 * must be at least ST_F_TOTAL_FIELDS. If this length is less then
1277 * this value, the function returns 0, otherwise, it returns 1.
1278 */
1279int stats_fill_fe_stats(struct proxy *px, struct field *stats, int len)
1280{
1281 if (len < ST_F_TOTAL_FIELDS)
1282 return 0;
1283
1284 memset(stats, 0, sizeof(*stats) * len);
1285
1286 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1287 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, "FRONTEND");
1288 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1289 stats[ST_F_SCUR] = mkf_u32(0, px->feconn);
1290 stats[ST_F_SMAX] = mkf_u32(FN_MAX, px->fe_counters.conn_max);
1291 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, px->maxconn);
1292 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, px->fe_counters.cum_sess);
1293 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, px->fe_counters.bytes_in);
1294 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, px->fe_counters.bytes_out);
1295 stats[ST_F_DREQ] = mkf_u64(FN_COUNTER, px->fe_counters.denied_req);
1296 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, px->fe_counters.denied_resp);
1297 stats[ST_F_EREQ] = mkf_u64(FN_COUNTER, px->fe_counters.failed_req);
1298 stats[ST_F_DCON] = mkf_u64(FN_COUNTER, px->fe_counters.denied_conn);
1299 stats[ST_F_DSES] = mkf_u64(FN_COUNTER, px->fe_counters.denied_sess);
1300 stats[ST_F_STATUS] = mkf_str(FO_STATUS, px->state == PR_STREADY ? "OPEN" : px->state == PR_STFULL ? "FULL" : "STOP");
1301 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
1302 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1303 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, 0);
1304 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_FE);
1305 stats[ST_F_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_sess_per_sec));
1306 stats[ST_F_RATE_LIM] = mkf_u32(FO_CONFIG|FN_LIMIT, px->fe_sps_lim);
1307 stats[ST_F_RATE_MAX] = mkf_u32(FN_MAX, px->fe_counters.sps_max);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001308 stats[ST_F_WREW] = mkf_u64(FN_COUNTER, px->fe_counters.failed_rewrites);
William Lallemand74c24fb2016-11-21 17:18:36 +01001309
1310 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
1311 if (px->mode == PR_MODE_HTTP) {
1312 stats[ST_F_HRSP_1XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[1]);
1313 stats[ST_F_HRSP_2XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[2]);
1314 stats[ST_F_HRSP_3XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[3]);
1315 stats[ST_F_HRSP_4XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[4]);
1316 stats[ST_F_HRSP_5XX] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[5]);
1317 stats[ST_F_HRSP_OTHER] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.rsp[0]);
1318 stats[ST_F_INTERCEPTED] = mkf_u64(FN_COUNTER, px->fe_counters.intercepted_req);
1319 }
1320
1321 /* requests : req_rate, req_rate_max, req_tot, */
1322 stats[ST_F_REQ_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_req_per_sec));
1323 stats[ST_F_REQ_RATE_MAX] = mkf_u32(FN_MAX, px->fe_counters.p.http.rps_max);
1324 stats[ST_F_REQ_TOT] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.cum_req);
1325
1326 /* compression: in, out, bypassed, responses */
1327 stats[ST_F_COMP_IN] = mkf_u64(FN_COUNTER, px->fe_counters.comp_in);
1328 stats[ST_F_COMP_OUT] = mkf_u64(FN_COUNTER, px->fe_counters.comp_out);
1329 stats[ST_F_COMP_BYP] = mkf_u64(FN_COUNTER, px->fe_counters.comp_byp);
1330 stats[ST_F_COMP_RSP] = mkf_u64(FN_COUNTER, px->fe_counters.p.http.comp_rsp);
1331
1332 /* connections : conn_rate, conn_rate_max, conn_tot, conn_max */
1333 stats[ST_F_CONN_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&px->fe_conn_per_sec));
1334 stats[ST_F_CONN_RATE_MAX] = mkf_u32(FN_MAX, px->fe_counters.cps_max);
1335 stats[ST_F_CONN_TOT] = mkf_u64(FN_COUNTER, px->fe_counters.cum_conn);
1336
1337 return 1;
1338}
1339
1340/* Dumps a frontend's line to the trash for the current proxy <px> and uses
1341 * the state from stream interface <si>. The caller is responsible for clearing
1342 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
1343 */
1344static int stats_dump_fe_stats(struct stream_interface *si, struct proxy *px)
1345{
1346 struct appctx *appctx = __objt_appctx(si->end);
1347
1348 if (!(px->cap & PR_CAP_FE))
1349 return 0;
1350
1351 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_FE)))
1352 return 0;
1353
1354 if (!stats_fill_fe_stats(px, stats, ST_F_TOTAL_FIELDS))
1355 return 0;
1356
1357 return stats_dump_one_line(stats, 0, px, appctx);
1358}
1359
1360/* Fill <stats> with the listener statistics. <stats> is
1361 * preallocated array of length <len>. The length of the array
1362 * must be at least ST_F_TOTAL_FIELDS. If this length is less
1363 * then this value, the function returns 0, otherwise, it
1364 * returns 1. <flags> can take the value ST_SHLGNDS.
1365 */
1366int stats_fill_li_stats(struct proxy *px, struct listener *l, int flags,
1367 struct field *stats, int len)
1368{
1369 struct chunk *out = get_trash_chunk();
1370
1371 if (len < ST_F_TOTAL_FIELDS)
1372 return 0;
1373
1374 if (!l->counters)
1375 return 0;
1376
1377 chunk_reset(out);
1378 memset(stats, 0, sizeof(*stats) * len);
1379
1380 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1381 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, l->name);
1382 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1383 stats[ST_F_SCUR] = mkf_u32(0, l->nbconn);
1384 stats[ST_F_SMAX] = mkf_u32(FN_MAX, l->counters->conn_max);
1385 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, l->maxconn);
1386 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, l->counters->cum_conn);
1387 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, l->counters->bytes_in);
1388 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, l->counters->bytes_out);
1389 stats[ST_F_DREQ] = mkf_u64(FN_COUNTER, l->counters->denied_req);
1390 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, l->counters->denied_resp);
1391 stats[ST_F_EREQ] = mkf_u64(FN_COUNTER, l->counters->failed_req);
1392 stats[ST_F_DCON] = mkf_u64(FN_COUNTER, l->counters->denied_conn);
1393 stats[ST_F_DSES] = mkf_u64(FN_COUNTER, l->counters->denied_sess);
1394 stats[ST_F_STATUS] = mkf_str(FO_STATUS, (l->nbconn < l->maxconn) ? (l->state == LI_LIMITED) ? "WAITING" : "OPEN" : "FULL");
1395 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
1396 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1397 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, l->luid);
1398 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_SO);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001399 stats[ST_F_WREW] = mkf_u64(FN_COUNTER, l->counters->failed_rewrites);
William Lallemand74c24fb2016-11-21 17:18:36 +01001400
1401 if (flags & ST_SHLGNDS) {
1402 char str[INET6_ADDRSTRLEN];
1403 int port;
1404
1405 port = get_host_port(&l->addr);
1406 switch (addr_to_str(&l->addr, str, sizeof(str))) {
1407 case AF_INET:
1408 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1409 chunk_appendf(out, "%s:%d", str, port);
1410 break;
1411 case AF_INET6:
1412 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1413 chunk_appendf(out, "[%s]:%d", str, port);
1414 break;
1415 case AF_UNIX:
1416 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, "unix");
1417 break;
1418 case -1:
1419 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1420 chunk_strcat(out, strerror(errno));
1421 break;
1422 default: /* address family not supported */
1423 break;
1424 }
1425 }
1426
1427 return 1;
1428}
1429
1430/* Dumps a line for listener <l> and proxy <px> to the trash and uses the state
1431 * from stream interface <si>, and stats flags <flags>. The caller is responsible
1432 * for clearing the trash if needed. Returns non-zero if it emits anything, zero
1433 * otherwise.
1434 */
1435static int stats_dump_li_stats(struct stream_interface *si, struct proxy *px, struct listener *l, int flags)
1436{
1437 struct appctx *appctx = __objt_appctx(si->end);
1438
1439 if (!stats_fill_li_stats(px, l, flags, stats, ST_F_TOTAL_FIELDS))
1440 return 0;
1441
1442 return stats_dump_one_line(stats, flags, px, appctx);
1443}
1444
1445enum srv_stats_state {
1446 SRV_STATS_STATE_DOWN = 0,
1447 SRV_STATS_STATE_DOWN_AGENT,
1448 SRV_STATS_STATE_GOING_UP,
1449 SRV_STATS_STATE_UP_GOING_DOWN,
1450 SRV_STATS_STATE_UP,
1451 SRV_STATS_STATE_NOLB_GOING_DOWN,
1452 SRV_STATS_STATE_NOLB,
1453 SRV_STATS_STATE_DRAIN_GOING_DOWN,
1454 SRV_STATS_STATE_DRAIN,
1455 SRV_STATS_STATE_DRAIN_AGENT,
1456 SRV_STATS_STATE_NO_CHECK,
1457
1458 SRV_STATS_STATE_COUNT, /* Must be last */
1459};
1460
1461static const char *srv_hlt_st[SRV_STATS_STATE_COUNT] = {
1462 [SRV_STATS_STATE_DOWN] = "DOWN",
1463 [SRV_STATS_STATE_DOWN_AGENT] = "DOWN (agent)",
1464 [SRV_STATS_STATE_GOING_UP] = "DOWN %d/%d",
1465 [SRV_STATS_STATE_UP_GOING_DOWN] = "UP %d/%d",
1466 [SRV_STATS_STATE_UP] = "UP",
1467 [SRV_STATS_STATE_NOLB_GOING_DOWN] = "NOLB %d/%d",
1468 [SRV_STATS_STATE_NOLB] = "NOLB",
1469 [SRV_STATS_STATE_DRAIN_GOING_DOWN] = "DRAIN %d/%d",
1470 [SRV_STATS_STATE_DRAIN] = "DRAIN",
1471 [SRV_STATS_STATE_DRAIN_AGENT] = "DRAIN (agent)",
1472 [SRV_STATS_STATE_NO_CHECK] = "no check"
1473};
1474
1475/* Fill <stats> with the server statistics. <stats> is
1476 * preallocated array of length <len>. The length of the array
1477 * must be at least ST_F_TOTAL_FIELDS. If this length is less
1478 * then this value, the function returns 0, otherwise, it
1479 * returns 1. <flags> can take the value ST_SHLGNDS.
1480 */
1481int stats_fill_sv_stats(struct proxy *px, struct server *sv, int flags,
1482 struct field *stats, int len)
1483{
1484 struct server *via, *ref;
1485 char str[INET6_ADDRSTRLEN];
1486 struct chunk *out = get_trash_chunk();
1487 enum srv_stats_state state;
1488 char *fld_status;
1489
1490 if (len < ST_F_TOTAL_FIELDS)
1491 return 0;
1492
1493 memset(stats, 0, sizeof(*stats) * len);
1494
1495 /* we have "via" which is the tracked server as described in the configuration,
1496 * and "ref" which is the checked server and the end of the chain.
1497 */
1498 via = sv->track ? sv->track : sv;
1499 ref = via;
1500 while (ref->track)
1501 ref = ref->track;
1502
Emeric Brun52a91d32017-08-31 14:41:55 +02001503 if (sv->cur_state == SRV_ST_RUNNING || sv->cur_state == SRV_ST_STARTING) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001504 if ((ref->check.state & CHK_ST_ENABLED) &&
1505 (ref->check.health < ref->check.rise + ref->check.fall - 1)) {
1506 state = SRV_STATS_STATE_UP_GOING_DOWN;
1507 } else {
1508 state = SRV_STATS_STATE_UP;
1509 }
1510
Emeric Brun52a91d32017-08-31 14:41:55 +02001511 if (sv->cur_admin & SRV_ADMF_DRAIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001512 if (ref->agent.state & CHK_ST_ENABLED)
1513 state = SRV_STATS_STATE_DRAIN_AGENT;
1514 else if (state == SRV_STATS_STATE_UP_GOING_DOWN)
1515 state = SRV_STATS_STATE_DRAIN_GOING_DOWN;
1516 else
1517 state = SRV_STATS_STATE_DRAIN;
1518 }
1519
1520 if (state == SRV_STATS_STATE_UP && !(ref->check.state & CHK_ST_ENABLED)) {
1521 state = SRV_STATS_STATE_NO_CHECK;
1522 }
1523 }
Emeric Brun52a91d32017-08-31 14:41:55 +02001524 else if (sv->cur_state == SRV_ST_STOPPING) {
William Lallemand74c24fb2016-11-21 17:18:36 +01001525 if ((!(sv->check.state & CHK_ST_ENABLED) && !sv->track) ||
1526 (ref->check.health == ref->check.rise + ref->check.fall - 1)) {
1527 state = SRV_STATS_STATE_NOLB;
1528 } else {
1529 state = SRV_STATS_STATE_NOLB_GOING_DOWN;
1530 }
1531 }
1532 else { /* stopped */
1533 if ((ref->agent.state & CHK_ST_ENABLED) && !ref->agent.health) {
1534 state = SRV_STATS_STATE_DOWN_AGENT;
1535 } else if ((ref->check.state & CHK_ST_ENABLED) && !ref->check.health) {
1536 state = SRV_STATS_STATE_DOWN; /* DOWN */
1537 } else if ((ref->agent.state & CHK_ST_ENABLED) || (ref->check.state & CHK_ST_ENABLED)) {
1538 state = SRV_STATS_STATE_GOING_UP;
1539 } else {
1540 state = SRV_STATS_STATE_DOWN; /* DOWN, unchecked */
1541 }
1542 }
1543
1544 chunk_reset(out);
1545
1546 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1547 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, sv->id);
1548 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1549 stats[ST_F_QCUR] = mkf_u32(0, sv->nbpend);
1550 stats[ST_F_QMAX] = mkf_u32(FN_MAX, sv->counters.nbpend_max);
1551 stats[ST_F_SCUR] = mkf_u32(0, sv->cur_sess);
1552 stats[ST_F_SMAX] = mkf_u32(FN_MAX, sv->counters.cur_sess_max);
1553
1554 if (sv->maxconn)
1555 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, sv->maxconn);
1556
1557 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, sv->counters.cum_sess);
1558 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, sv->counters.bytes_in);
1559 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, sv->counters.bytes_out);
1560 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, sv->counters.failed_secu);
1561 stats[ST_F_ECON] = mkf_u64(FN_COUNTER, sv->counters.failed_conns);
1562 stats[ST_F_ERESP] = mkf_u64(FN_COUNTER, sv->counters.failed_resp);
1563 stats[ST_F_WRETR] = mkf_u64(FN_COUNTER, sv->counters.retries);
1564 stats[ST_F_WREDIS] = mkf_u64(FN_COUNTER, sv->counters.redispatches);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001565 stats[ST_F_WREW] = mkf_u64(FN_COUNTER, sv->counters.failed_rewrites);
William Lallemand74c24fb2016-11-21 17:18:36 +01001566
1567 /* status */
1568 fld_status = chunk_newstr(out);
Emeric Brun52a91d32017-08-31 14:41:55 +02001569 if (sv->cur_admin & SRV_ADMF_RMAINT)
William Lallemand74c24fb2016-11-21 17:18:36 +01001570 chunk_appendf(out, "MAINT (resolution)");
Emeric Brun52a91d32017-08-31 14:41:55 +02001571 else if (sv->cur_admin & SRV_ADMF_IMAINT)
William Lallemand74c24fb2016-11-21 17:18:36 +01001572 chunk_appendf(out, "MAINT (via %s/%s)", via->proxy->id, via->id);
Emeric Brun52a91d32017-08-31 14:41:55 +02001573 else if (sv->cur_admin & SRV_ADMF_MAINT)
William Lallemand74c24fb2016-11-21 17:18:36 +01001574 chunk_appendf(out, "MAINT");
1575 else
1576 chunk_appendf(out,
1577 srv_hlt_st[state],
Emeric Brun52a91d32017-08-31 14:41:55 +02001578 (ref->cur_state != SRV_ST_STOPPED) ? (ref->check.health - ref->check.rise + 1) : (ref->check.health),
1579 (ref->cur_state != SRV_ST_STOPPED) ? (ref->check.fall) : (ref->check.rise));
William Lallemand74c24fb2016-11-21 17:18:36 +01001580
1581 stats[ST_F_STATUS] = mkf_str(FO_STATUS, fld_status);
1582 stats[ST_F_LASTCHG] = mkf_u32(FN_AGE, now.tv_sec - sv->last_change);
Emeric Brun52a91d32017-08-31 14:41:55 +02001583 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 +01001584 stats[ST_F_ACT] = mkf_u32(FO_STATUS, (sv->flags & SRV_F_BACKUP) ? 0 : 1);
1585 stats[ST_F_BCK] = mkf_u32(FO_STATUS, (sv->flags & SRV_F_BACKUP) ? 1 : 0);
1586
1587 /* check failures: unique, fatal; last change, total downtime */
1588 if (sv->check.state & CHK_ST_ENABLED) {
1589 stats[ST_F_CHKFAIL] = mkf_u64(FN_COUNTER, sv->counters.failed_checks);
1590 stats[ST_F_CHKDOWN] = mkf_u64(FN_COUNTER, sv->counters.down_trans);
1591 stats[ST_F_DOWNTIME] = mkf_u32(FN_COUNTER, srv_downtime(sv));
1592 }
1593
1594 if (sv->maxqueue)
1595 stats[ST_F_QLIMIT] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->maxqueue);
1596
1597 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
1598 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1599 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, sv->puid);
1600
Emeric Brun52a91d32017-08-31 14:41:55 +02001601 if (sv->cur_state == SRV_ST_STARTING && !server_is_draining(sv))
William Lallemand74c24fb2016-11-21 17:18:36 +01001602 stats[ST_F_THROTTLE] = mkf_u32(FN_AVG, server_throttle_rate(sv));
1603
1604 stats[ST_F_LBTOT] = mkf_u64(FN_COUNTER, sv->counters.cum_lbconn);
1605
1606 if (sv->track) {
1607 char *fld_track = chunk_newstr(out);
1608
1609 chunk_appendf(out, "%s/%s", sv->track->proxy->id, sv->track->id);
1610 stats[ST_F_TRACKED] = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, fld_track);
1611 }
1612
1613 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_SV);
1614 stats[ST_F_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&sv->sess_per_sec));
1615 stats[ST_F_RATE_MAX] = mkf_u32(FN_MAX, sv->counters.sps_max);
1616
1617 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) {
1618 const char *fld_chksts;
1619
1620 fld_chksts = chunk_newstr(out);
1621 chunk_strcat(out, "* "); // for check in progress
1622 chunk_strcat(out, get_check_status_info(sv->check.status));
1623 if (!(sv->check.state & CHK_ST_INPROGRESS))
1624 fld_chksts += 2; // skip "* "
1625 stats[ST_F_CHECK_STATUS] = mkf_str(FN_OUTPUT, fld_chksts);
1626
1627 if (sv->check.status >= HCHK_STATUS_L57DATA)
1628 stats[ST_F_CHECK_CODE] = mkf_u32(FN_OUTPUT, sv->check.code);
1629
1630 if (sv->check.status >= HCHK_STATUS_CHECKED)
1631 stats[ST_F_CHECK_DURATION] = mkf_u64(FN_DURATION, sv->check.duration);
1632
1633 stats[ST_F_CHECK_DESC] = mkf_str(FN_OUTPUT, get_check_status_description(sv->check.status));
1634 stats[ST_F_LAST_CHK] = mkf_str(FN_OUTPUT, sv->check.desc);
1635 stats[ST_F_CHECK_RISE] = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.rise);
1636 stats[ST_F_CHECK_FALL] = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.fall);
1637 stats[ST_F_CHECK_HEALTH] = mkf_u32(FO_CONFIG|FS_SERVICE, ref->check.health);
1638 }
1639
1640 if ((sv->agent.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) == CHK_ST_ENABLED) {
1641 const char *fld_chksts;
1642
1643 fld_chksts = chunk_newstr(out);
1644 chunk_strcat(out, "* "); // for check in progress
1645 chunk_strcat(out, get_check_status_info(sv->agent.status));
1646 if (!(sv->agent.state & CHK_ST_INPROGRESS))
1647 fld_chksts += 2; // skip "* "
1648 stats[ST_F_AGENT_STATUS] = mkf_str(FN_OUTPUT, fld_chksts);
1649
1650 if (sv->agent.status >= HCHK_STATUS_L57DATA)
1651 stats[ST_F_AGENT_CODE] = mkf_u32(FN_OUTPUT, sv->agent.code);
1652
1653 if (sv->agent.status >= HCHK_STATUS_CHECKED)
1654 stats[ST_F_AGENT_DURATION] = mkf_u64(FN_DURATION, sv->agent.duration);
1655
1656 stats[ST_F_AGENT_DESC] = mkf_str(FN_OUTPUT, get_check_status_description(sv->agent.status));
1657 stats[ST_F_LAST_AGT] = mkf_str(FN_OUTPUT, sv->agent.desc);
1658 stats[ST_F_AGENT_RISE] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.rise);
1659 stats[ST_F_AGENT_FALL] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.fall);
1660 stats[ST_F_AGENT_HEALTH] = mkf_u32(FO_CONFIG|FS_SERVICE, sv->agent.health);
1661 }
1662
1663 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
1664 if (px->mode == PR_MODE_HTTP) {
1665 stats[ST_F_HRSP_1XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[1]);
1666 stats[ST_F_HRSP_2XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[2]);
1667 stats[ST_F_HRSP_3XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[3]);
1668 stats[ST_F_HRSP_4XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[4]);
1669 stats[ST_F_HRSP_5XX] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[5]);
1670 stats[ST_F_HRSP_OTHER] = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[0]);
1671 }
1672
1673 if (ref->observe)
1674 stats[ST_F_HANAFAIL] = mkf_u64(FN_COUNTER, sv->counters.failed_hana);
1675
1676 stats[ST_F_CLI_ABRT] = mkf_u64(FN_COUNTER, sv->counters.cli_aborts);
1677 stats[ST_F_SRV_ABRT] = mkf_u64(FN_COUNTER, sv->counters.srv_aborts);
1678 stats[ST_F_LASTSESS] = mkf_s32(FN_AGE, srv_lastsession(sv));
1679
1680 stats[ST_F_QTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.q_time, TIME_STATS_SAMPLES));
1681 stats[ST_F_CTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.c_time, TIME_STATS_SAMPLES));
1682 stats[ST_F_RTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.d_time, TIME_STATS_SAMPLES));
1683 stats[ST_F_TTIME] = mkf_u32(FN_AVG, swrate_avg(sv->counters.t_time, TIME_STATS_SAMPLES));
1684
1685 if (flags & ST_SHLGNDS) {
1686 switch (addr_to_str(&sv->addr, str, sizeof(str))) {
1687 case AF_INET:
1688 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
Willy Tarreau04276f32017-01-06 17:41:29 +01001689 chunk_appendf(out, "%s:%d", str, sv->svc_port);
William Lallemand74c24fb2016-11-21 17:18:36 +01001690 break;
1691 case AF_INET6:
1692 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
Willy Tarreau04276f32017-01-06 17:41:29 +01001693 chunk_appendf(out, "[%s]:%d", str, sv->svc_port);
William Lallemand74c24fb2016-11-21 17:18:36 +01001694 break;
1695 case AF_UNIX:
1696 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, "unix");
1697 break;
1698 case -1:
1699 stats[ST_F_ADDR] = mkf_str(FO_CONFIG|FS_SERVICE, chunk_newstr(out));
1700 chunk_strcat(out, strerror(errno));
1701 break;
1702 default: /* address family not supported */
1703 break;
1704 }
1705
1706 if (sv->cookie)
1707 stats[ST_F_COOKIE] = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, sv->cookie);
1708 }
1709
1710 return 1;
1711}
1712
1713/* Dumps a line for server <sv> and proxy <px> to the trash and uses the state
1714 * from stream interface <si>, stats flags <flags>, and server state <state>.
1715 * The caller is responsible for clearing the trash if needed. Returns non-zero
1716 * if it emits anything, zero otherwise.
1717 */
1718static int stats_dump_sv_stats(struct stream_interface *si, struct proxy *px, int flags, struct server *sv)
1719{
1720 struct appctx *appctx = __objt_appctx(si->end);
1721
1722 if (!stats_fill_sv_stats(px, sv, flags, stats, ST_F_TOTAL_FIELDS))
1723 return 0;
1724
1725 return stats_dump_one_line(stats, flags, px, appctx);
1726}
1727
1728/* Fill <stats> with the backend statistics. <stats> is
1729 * preallocated array of length <len>. The length of the array
1730 * must be at least ST_F_TOTAL_FIELDS. If this length is less
1731 * then this value, the function returns 0, otherwise, it
1732 * returns 1. <flags> can take the value ST_SHLGNDS.
1733 */
1734int stats_fill_be_stats(struct proxy *px, int flags, struct field *stats, int len)
1735{
1736 if (len < ST_F_TOTAL_FIELDS)
1737 return 0;
1738
1739 memset(stats, 0, sizeof(*stats) * len);
1740
1741 stats[ST_F_PXNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, px->id);
1742 stats[ST_F_SVNAME] = mkf_str(FO_KEY|FN_NAME|FS_SERVICE, "BACKEND");
1743 stats[ST_F_MODE] = mkf_str(FO_CONFIG|FS_SERVICE, proxy_mode_str(px->mode));
1744 stats[ST_F_QCUR] = mkf_u32(0, px->nbpend);
1745 stats[ST_F_QMAX] = mkf_u32(FN_MAX, px->be_counters.nbpend_max);
Thierry FOURNIER0ff98a42016-12-19 16:50:42 +01001746 stats[ST_F_SCUR] = mkf_u32(0, px->beconn);
William Lallemand74c24fb2016-11-21 17:18:36 +01001747 stats[ST_F_SMAX] = mkf_u32(FN_MAX, px->be_counters.conn_max);
1748 stats[ST_F_SLIM] = mkf_u32(FO_CONFIG|FN_LIMIT, px->fullconn);
1749 stats[ST_F_STOT] = mkf_u64(FN_COUNTER, px->be_counters.cum_conn);
1750 stats[ST_F_BIN] = mkf_u64(FN_COUNTER, px->be_counters.bytes_in);
1751 stats[ST_F_BOUT] = mkf_u64(FN_COUNTER, px->be_counters.bytes_out);
1752 stats[ST_F_DREQ] = mkf_u64(FN_COUNTER, px->be_counters.denied_req);
1753 stats[ST_F_DRESP] = mkf_u64(FN_COUNTER, px->be_counters.denied_resp);
1754 stats[ST_F_ECON] = mkf_u64(FN_COUNTER, px->be_counters.failed_conns);
1755 stats[ST_F_ERESP] = mkf_u64(FN_COUNTER, px->be_counters.failed_resp);
1756 stats[ST_F_WRETR] = mkf_u64(FN_COUNTER, px->be_counters.retries);
1757 stats[ST_F_WREDIS] = mkf_u64(FN_COUNTER, px->be_counters.redispatches);
Tim Duesterhus3fd19732018-05-27 20:35:08 +02001758 stats[ST_F_WREW] = mkf_u64(FN_COUNTER, px->be_counters.failed_rewrites);
William Lallemand74c24fb2016-11-21 17:18:36 +01001759 stats[ST_F_STATUS] = mkf_str(FO_STATUS, (px->lbprm.tot_weight > 0 || !px->srv) ? "UP" : "DOWN");
1760 stats[ST_F_WEIGHT] = mkf_u32(FN_AVG, (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv);
1761 stats[ST_F_ACT] = mkf_u32(0, px->srv_act);
1762 stats[ST_F_BCK] = mkf_u32(0, px->srv_bck);
1763 stats[ST_F_CHKDOWN] = mkf_u64(FN_COUNTER, px->down_trans);
1764 stats[ST_F_LASTCHG] = mkf_u32(FN_AGE, now.tv_sec - px->last_change);
1765 if (px->srv)
1766 stats[ST_F_DOWNTIME] = mkf_u32(FN_COUNTER, be_downtime(px));
1767
1768 stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
1769 stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
1770 stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, 0);
1771 stats[ST_F_LBTOT] = mkf_u64(FN_COUNTER, px->be_counters.cum_lbconn);
1772 stats[ST_F_TYPE] = mkf_u32(FO_CONFIG|FS_SERVICE, STATS_TYPE_BE);
1773 stats[ST_F_RATE] = mkf_u32(0, read_freq_ctr(&px->be_sess_per_sec));
1774 stats[ST_F_RATE_MAX] = mkf_u32(0, px->be_counters.sps_max);
1775
1776 if (flags & ST_SHLGNDS) {
1777 if (px->cookie_name)
1778 stats[ST_F_COOKIE] = mkf_str(FO_CONFIG|FN_NAME|FS_SERVICE, px->cookie_name);
1779 stats[ST_F_ALGO] = mkf_str(FO_CONFIG|FS_SERVICE, backend_lb_algo_str(px->lbprm.algo & BE_LB_ALGO));
1780 }
1781
1782 /* http response: 1xx, 2xx, 3xx, 4xx, 5xx, other */
1783 if (px->mode == PR_MODE_HTTP) {
1784 stats[ST_F_REQ_TOT] = mkf_u64(FN_COUNTER, px->be_counters.p.http.cum_req);
1785 stats[ST_F_HRSP_1XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[1]);
1786 stats[ST_F_HRSP_2XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[2]);
1787 stats[ST_F_HRSP_3XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[3]);
1788 stats[ST_F_HRSP_4XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[4]);
1789 stats[ST_F_HRSP_5XX] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[5]);
1790 stats[ST_F_HRSP_OTHER] = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[0]);
William Lallemand74c24fb2016-11-21 17:18:36 +01001791 }
1792
1793 stats[ST_F_CLI_ABRT] = mkf_u64(FN_COUNTER, px->be_counters.cli_aborts);
1794 stats[ST_F_SRV_ABRT] = mkf_u64(FN_COUNTER, px->be_counters.srv_aborts);
1795
1796 /* compression: in, out, bypassed, responses */
1797 stats[ST_F_COMP_IN] = mkf_u64(FN_COUNTER, px->be_counters.comp_in);
1798 stats[ST_F_COMP_OUT] = mkf_u64(FN_COUNTER, px->be_counters.comp_out);
1799 stats[ST_F_COMP_BYP] = mkf_u64(FN_COUNTER, px->be_counters.comp_byp);
1800 stats[ST_F_COMP_RSP] = mkf_u64(FN_COUNTER, px->be_counters.p.http.comp_rsp);
1801 stats[ST_F_LASTSESS] = mkf_s32(FN_AGE, be_lastsession(px));
1802
1803 stats[ST_F_QTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.q_time, TIME_STATS_SAMPLES));
1804 stats[ST_F_CTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.c_time, TIME_STATS_SAMPLES));
1805 stats[ST_F_RTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.d_time, TIME_STATS_SAMPLES));
1806 stats[ST_F_TTIME] = mkf_u32(FN_AVG, swrate_avg(px->be_counters.t_time, TIME_STATS_SAMPLES));
1807
1808 return 1;
1809}
1810
1811/* Dumps a line for backend <px> to the trash for and uses the state from stream
1812 * interface <si> and stats flags <flags>. The caller is responsible for clearing
1813 * the trash if needed. Returns non-zero if it emits anything, zero otherwise.
1814 */
1815static int stats_dump_be_stats(struct stream_interface *si, struct proxy *px, int flags)
1816{
1817 struct appctx *appctx = __objt_appctx(si->end);
1818
1819 if (!(px->cap & PR_CAP_BE))
1820 return 0;
1821
1822 if ((appctx->ctx.stats.flags & STAT_BOUND) && !(appctx->ctx.stats.type & (1 << STATS_TYPE_BE)))
1823 return 0;
1824
1825 if (!stats_fill_be_stats(px, flags, stats, ST_F_TOTAL_FIELDS))
1826 return 0;
1827
1828 return stats_dump_one_line(stats, flags, px, appctx);
1829}
1830
1831/* Dumps the HTML table header for proxy <px> to the trash for and uses the state from
1832 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
1833 * for clearing the trash if needed.
1834 */
1835static void stats_dump_html_px_hdr(struct stream_interface *si, struct proxy *px, struct uri_auth *uri)
1836{
1837 struct appctx *appctx = __objt_appctx(si->end);
1838 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
1839
1840 if (px->cap & PR_CAP_BE && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
1841 /* A form to enable/disable this proxy servers */
1842
1843 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
1844 scope_txt[0] = 0;
1845 if (appctx->ctx.stats.scope_len) {
1846 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau89faf5d2018-06-07 18:16:48 +02001847 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 +01001848 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
1849 }
1850
1851 chunk_appendf(&trash,
1852 "<form method=\"post\">");
1853 }
1854
1855 /* print a new table */
1856 chunk_appendf(&trash,
1857 "<table class=\"tbl\" width=\"100%%\">\n"
1858 "<tr class=\"titre\">"
1859 "<th class=\"pxname\" width=\"10%%\">");
1860
1861 chunk_appendf(&trash,
1862 "<a name=\"%s\"></a>%s"
1863 "<a class=px href=\"#%s\">%s</a>",
1864 px->id,
1865 (uri->flags & ST_SHLGNDS) ? "<u>":"",
1866 px->id, px->id);
1867
1868 if (uri->flags & ST_SHLGNDS) {
1869 /* cap, mode, id */
1870 chunk_appendf(&trash, "<div class=tips>cap: %s, mode: %s, id: %d",
1871 proxy_cap_str(px->cap), proxy_mode_str(px->mode),
1872 px->uuid);
1873 chunk_appendf(&trash, "</div>");
1874 }
1875
1876 chunk_appendf(&trash,
1877 "%s</th>"
1878 "<th class=\"%s\" width=\"90%%\">%s</th>"
1879 "</tr>\n"
1880 "</table>\n"
1881 "<table class=\"tbl\" width=\"100%%\">\n"
1882 "<tr class=\"titre\">",
1883 (uri->flags & ST_SHLGNDS) ? "</u>":"",
1884 px->desc ? "desc" : "empty", px->desc ? px->desc : "");
1885
1886 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
1887 /* Column heading for Enable or Disable server */
David Harrigand3db35a2016-12-30 12:12:49 +00001888 chunk_appendf(&trash,
1889 "<th rowspan=2 width=1><input type=\"checkbox\" \
1890 onclick=\"for(c in document.getElementsByClassName('%s-checkbox')) \
1891 document.getElementsByClassName('%s-checkbox').item(c).checked = this.checked\"></th>",
1892 px->id,
1893 px->id);
William Lallemand74c24fb2016-11-21 17:18:36 +01001894 }
1895
1896 chunk_appendf(&trash,
1897 "<th rowspan=2></th>"
1898 "<th colspan=3>Queue</th>"
1899 "<th colspan=3>Session rate</th><th colspan=6>Sessions</th>"
1900 "<th colspan=2>Bytes</th><th colspan=2>Denied</th>"
1901 "<th colspan=3>Errors</th><th colspan=2>Warnings</th>"
1902 "<th colspan=9>Server</th>"
1903 "</tr>\n"
1904 "<tr class=\"titre\">"
1905 "<th>Cur</th><th>Max</th><th>Limit</th>"
1906 "<th>Cur</th><th>Max</th><th>Limit</th><th>Cur</th><th>Max</th>"
1907 "<th>Limit</th><th>Total</th><th>LbTot</th><th>Last</th><th>In</th><th>Out</th>"
1908 "<th>Req</th><th>Resp</th><th>Req</th><th>Conn</th>"
1909 "<th>Resp</th><th>Retr</th><th>Redis</th>"
1910 "<th>Status</th><th>LastChk</th><th>Wght</th><th>Act</th>"
1911 "<th>Bck</th><th>Chk</th><th>Dwn</th><th>Dwntme</th>"
1912 "<th>Thrtle</th>\n"
1913 "</tr>");
1914}
1915
1916/* Dumps the HTML table trailer for proxy <px> to the trash for and uses the state from
1917 * stream interface <si>. The caller is responsible for clearing the trash if needed.
1918 */
1919static void stats_dump_html_px_end(struct stream_interface *si, struct proxy *px)
1920{
1921 struct appctx *appctx = __objt_appctx(si->end);
1922 chunk_appendf(&trash, "</table>");
1923
1924 if ((px->cap & PR_CAP_BE) && px->srv && (appctx->ctx.stats.flags & STAT_ADMIN)) {
1925 /* close the form used to enable/disable this proxy servers */
1926 chunk_appendf(&trash,
1927 "Choose the action to perform on the checked servers : "
1928 "<select name=action>"
1929 "<option value=\"\"></option>"
1930 "<option value=\"ready\">Set state to READY</option>"
1931 "<option value=\"drain\">Set state to DRAIN</option>"
1932 "<option value=\"maint\">Set state to MAINT</option>"
1933 "<option value=\"dhlth\">Health: disable checks</option>"
1934 "<option value=\"ehlth\">Health: enable checks</option>"
1935 "<option value=\"hrunn\">Health: force UP</option>"
1936 "<option value=\"hnolb\">Health: force NOLB</option>"
1937 "<option value=\"hdown\">Health: force DOWN</option>"
1938 "<option value=\"dagent\">Agent: disable checks</option>"
1939 "<option value=\"eagent\">Agent: enable checks</option>"
1940 "<option value=\"arunn\">Agent: force UP</option>"
1941 "<option value=\"adown\">Agent: force DOWN</option>"
1942 "<option value=\"shutdown\">Kill Sessions</option>"
1943 "</select>"
1944 "<input type=\"hidden\" name=\"b\" value=\"#%d\">"
1945 "&nbsp;<input type=\"submit\" value=\"Apply\">"
1946 "</form>",
1947 px->uuid);
1948 }
1949
1950 chunk_appendf(&trash, "<p>\n");
1951}
1952
1953/*
1954 * Dumps statistics for a proxy. The output is sent to the stream interface's
1955 * input buffer. Returns 0 if it had to stop dumping data because of lack of
1956 * buffer space, or non-zero if everything completed. This function is used
1957 * both by the CLI and the HTTP entry points, and is able to dump the output
1958 * in HTML or CSV formats. If the later, <uri> must be NULL.
1959 */
1960int stats_dump_proxy_to_buffer(struct stream_interface *si, struct proxy *px, struct uri_auth *uri)
1961{
1962 struct appctx *appctx = __objt_appctx(si->end);
1963 struct stream *s = si_strm(si);
1964 struct channel *rep = si_ic(si);
1965 struct server *sv, *svs; /* server and server-state, server-state=server or server->track */
1966 struct listener *l;
1967 unsigned int flags;
1968
1969 if (uri)
1970 flags = uri->flags;
William Lallemand07a62f72017-05-24 00:57:40 +02001971 else if ((strm_li(s)->bind_conf->level & ACCESS_LVL_MASK) >= ACCESS_LVL_OPER)
William Lallemand74c24fb2016-11-21 17:18:36 +01001972 flags = ST_SHLGNDS | ST_SHNODE | ST_SHDESC;
1973 else
1974 flags = ST_SHNODE | ST_SHDESC;
1975
1976 chunk_reset(&trash);
1977
1978 switch (appctx->ctx.stats.px_st) {
1979 case STAT_PX_ST_INIT:
1980 /* we are on a new proxy */
1981 if (uri && uri->scope) {
1982 /* we have a limited scope, we have to check the proxy name */
1983 struct stat_scope *scope;
1984 int len;
1985
1986 len = strlen(px->id);
1987 scope = uri->scope;
1988
1989 while (scope) {
1990 /* match exact proxy name */
1991 if (scope->px_len == len && !memcmp(px->id, scope->px_id, len))
1992 break;
1993
1994 /* match '.' which means 'self' proxy */
1995 if (!strcmp(scope->px_id, ".") && px == s->be)
1996 break;
1997 scope = scope->next;
1998 }
1999
2000 /* proxy name not found : don't dump anything */
2001 if (scope == NULL)
2002 return 1;
2003 }
2004
2005 /* if the user has requested a limited output and the proxy
2006 * name does not match, skip it.
2007 */
2008 if (appctx->ctx.stats.scope_len &&
Willy Tarreau89faf5d2018-06-07 18:16:48 +02002009 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 +01002010 return 1;
2011
2012 if ((appctx->ctx.stats.flags & STAT_BOUND) &&
2013 (appctx->ctx.stats.iid != -1) &&
2014 (px->uuid != appctx->ctx.stats.iid))
2015 return 1;
2016
2017 appctx->ctx.stats.px_st = STAT_PX_ST_TH;
2018 /* fall through */
2019
2020 case STAT_PX_ST_TH:
2021 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
2022 stats_dump_html_px_hdr(si, px, uri);
Willy Tarreau06d80a92017-10-19 14:32:15 +02002023 if (ci_putchk(rep, &trash) == -1) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002024 si_applet_cant_put(si);
2025 return 0;
2026 }
2027 }
2028
2029 appctx->ctx.stats.px_st = STAT_PX_ST_FE;
2030 /* fall through */
2031
2032 case STAT_PX_ST_FE:
2033 /* print the frontend */
2034 if (stats_dump_fe_stats(si, px)) {
Willy Tarreau06d80a92017-10-19 14:32:15 +02002035 if (ci_putchk(rep, &trash) == -1) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002036 si_applet_cant_put(si);
2037 return 0;
2038 }
2039 }
2040
2041 appctx->ctx.stats.l = px->conf.listeners.n;
2042 appctx->ctx.stats.px_st = STAT_PX_ST_LI;
2043 /* fall through */
2044
2045 case STAT_PX_ST_LI:
2046 /* stats.l has been initialized above */
2047 for (; appctx->ctx.stats.l != &px->conf.listeners; appctx->ctx.stats.l = l->by_fe.n) {
2048 if (buffer_almost_full(rep->buf)) {
2049 si_applet_cant_put(si);
2050 return 0;
2051 }
2052
2053 l = LIST_ELEM(appctx->ctx.stats.l, struct listener *, by_fe);
2054 if (!l->counters)
2055 continue;
2056
2057 if (appctx->ctx.stats.flags & STAT_BOUND) {
2058 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SO)))
2059 break;
2060
2061 if (appctx->ctx.stats.sid != -1 && l->luid != appctx->ctx.stats.sid)
2062 continue;
2063 }
2064
2065 /* print the frontend */
2066 if (stats_dump_li_stats(si, px, l, flags)) {
Willy Tarreau06d80a92017-10-19 14:32:15 +02002067 if (ci_putchk(rep, &trash) == -1) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002068 si_applet_cant_put(si);
2069 return 0;
2070 }
2071 }
2072 }
2073
2074 appctx->ctx.stats.sv = px->srv; /* may be NULL */
2075 appctx->ctx.stats.px_st = STAT_PX_ST_SV;
2076 /* fall through */
2077
2078 case STAT_PX_ST_SV:
2079 /* stats.sv has been initialized above */
2080 for (; appctx->ctx.stats.sv != NULL; appctx->ctx.stats.sv = sv->next) {
2081 if (buffer_almost_full(rep->buf)) {
2082 si_applet_cant_put(si);
2083 return 0;
2084 }
2085
2086 sv = appctx->ctx.stats.sv;
2087
2088 if (appctx->ctx.stats.flags & STAT_BOUND) {
2089 if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SV)))
2090 break;
2091
2092 if (appctx->ctx.stats.sid != -1 && sv->puid != appctx->ctx.stats.sid)
2093 continue;
2094 }
2095
2096 svs = sv;
2097 while (svs->track)
2098 svs = svs->track;
2099
2100 /* do not report servers which are DOWN and not changing state */
2101 if ((appctx->ctx.stats.flags & STAT_HIDE_DOWN) &&
Emeric Brun52a91d32017-08-31 14:41:55 +02002102 ((sv->cur_admin & SRV_ADMF_MAINT) || /* server is in maintenance */
2103 (sv->cur_state == SRV_ST_STOPPED && /* server is down */
William Lallemand74c24fb2016-11-21 17:18:36 +01002104 (!((svs->agent.state | svs->check.state) & CHK_ST_ENABLED) ||
2105 ((svs->agent.state & CHK_ST_ENABLED) && !svs->agent.health) ||
2106 ((svs->check.state & CHK_ST_ENABLED) && !svs->check.health))))) {
2107 continue;
2108 }
2109
2110 if (stats_dump_sv_stats(si, px, flags, sv)) {
Willy Tarreau06d80a92017-10-19 14:32:15 +02002111 if (ci_putchk(rep, &trash) == -1) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002112 si_applet_cant_put(si);
2113 return 0;
2114 }
2115 }
2116 } /* for sv */
2117
2118 appctx->ctx.stats.px_st = STAT_PX_ST_BE;
2119 /* fall through */
2120
2121 case STAT_PX_ST_BE:
2122 /* print the backend */
2123 if (stats_dump_be_stats(si, px, flags)) {
Willy Tarreau06d80a92017-10-19 14:32:15 +02002124 if (ci_putchk(rep, &trash) == -1) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002125 si_applet_cant_put(si);
2126 return 0;
2127 }
2128 }
2129
2130 appctx->ctx.stats.px_st = STAT_PX_ST_END;
2131 /* fall through */
2132
2133 case STAT_PX_ST_END:
2134 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
2135 stats_dump_html_px_end(si, px);
Willy Tarreau06d80a92017-10-19 14:32:15 +02002136 if (ci_putchk(rep, &trash) == -1) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002137 si_applet_cant_put(si);
2138 return 0;
2139 }
2140 }
2141
2142 appctx->ctx.stats.px_st = STAT_PX_ST_FIN;
2143 /* fall through */
2144
2145 case STAT_PX_ST_FIN:
2146 return 1;
2147
2148 default:
2149 /* unknown state, we should put an abort() here ! */
2150 return 1;
2151 }
2152}
2153
2154/* Dumps the HTTP stats head block to the trash for and uses the per-uri
2155 * parameters <uri>. The caller is responsible for clearing the trash if needed.
2156 */
2157static void stats_dump_html_head(struct uri_auth *uri)
2158{
2159 /* WARNING! This must fit in the first buffer !!! */
2160 chunk_appendf(&trash,
2161 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n"
2162 "\"http://www.w3.org/TR/html4/loose.dtd\">\n"
2163 "<html><head><title>Statistics Report for " PRODUCT_NAME "%s%s</title>\n"
2164 "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n"
2165 "<style type=\"text/css\"><!--\n"
2166 "body {"
2167 " font-family: arial, helvetica, sans-serif;"
2168 " font-size: 12px;"
2169 " font-weight: normal;"
2170 " color: black;"
2171 " background: white;"
2172 "}\n"
2173 "th,td {"
2174 " font-size: 10px;"
2175 "}\n"
2176 "h1 {"
2177 " font-size: x-large;"
2178 " margin-bottom: 0.5em;"
2179 "}\n"
2180 "h2 {"
2181 " font-family: helvetica, arial;"
2182 " font-size: x-large;"
2183 " font-weight: bold;"
2184 " font-style: italic;"
2185 " color: #6020a0;"
2186 " margin-top: 0em;"
2187 " margin-bottom: 0em;"
2188 "}\n"
2189 "h3 {"
2190 " font-family: helvetica, arial;"
2191 " font-size: 16px;"
2192 " font-weight: bold;"
2193 " color: #b00040;"
2194 " background: #e8e8d0;"
2195 " margin-top: 0em;"
2196 " margin-bottom: 0em;"
2197 "}\n"
2198 "li {"
2199 " margin-top: 0.25em;"
2200 " margin-right: 2em;"
2201 "}\n"
2202 ".hr {margin-top: 0.25em;"
2203 " border-color: black;"
2204 " border-bottom-style: solid;"
2205 "}\n"
2206 ".titre {background: #20D0D0;color: #000000; font-weight: bold; text-align: center;}\n"
2207 ".total {background: #20D0D0;color: #ffff80;}\n"
2208 ".frontend {background: #e8e8d0;}\n"
2209 ".socket {background: #d0d0d0;}\n"
2210 ".backend {background: #e8e8d0;}\n"
2211 ".active_down {background: #ff9090;}\n"
2212 ".active_going_up {background: #ffd020;}\n"
2213 ".active_going_down {background: #ffffa0;}\n"
2214 ".active_up {background: #c0ffc0;}\n"
2215 ".active_nolb {background: #20a0ff;}\n"
2216 ".active_draining {background: #20a0FF;}\n"
2217 ".active_no_check {background: #e0e0e0;}\n"
2218 ".backup_down {background: #ff9090;}\n"
2219 ".backup_going_up {background: #ff80ff;}\n"
2220 ".backup_going_down {background: #c060ff;}\n"
2221 ".backup_up {background: #b0d0ff;}\n"
2222 ".backup_nolb {background: #90b0e0;}\n"
2223 ".backup_draining {background: #cc9900;}\n"
2224 ".backup_no_check {background: #e0e0e0;}\n"
2225 ".maintain {background: #c07820;}\n"
2226 ".rls {letter-spacing: 0.2em; margin-right: 1px;}\n" /* right letter spacing (used for grouping digits) */
2227 "\n"
2228 "a.px:link {color: #ffff40; text-decoration: none;}"
2229 "a.px:visited {color: #ffff40; text-decoration: none;}"
2230 "a.px:hover {color: #ffffff; text-decoration: none;}"
2231 "a.lfsb:link {color: #000000; text-decoration: none;}"
2232 "a.lfsb:visited {color: #000000; text-decoration: none;}"
2233 "a.lfsb:hover {color: #505050; text-decoration: none;}"
2234 "\n"
2235 "table.tbl { border-collapse: collapse; border-style: none;}\n"
2236 "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"
2237 "table.tbl td.ac { text-align: center;}\n"
2238 "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}\n"
2239 "table.tbl th.pxname { background: #b00040; color: #ffff40; font-weight: bold; border-style: solid solid none solid; padding: 2px 3px; white-space: nowrap;}\n"
2240 "table.tbl th.empty { border-style: none; empty-cells: hide; background: white;}\n"
2241 "table.tbl th.desc { background: white; border-style: solid solid none solid; text-align: left; padding: 2px 3px;}\n"
2242 "\n"
2243 "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n"
2244 "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n"
2245 "table.lgd td.noborder { border-style: none; padding: 2px; white-space: nowrap;}\n"
2246 "table.det { border-collapse: collapse; border-style: none; }\n"
2247 "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"
2248 "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"
2249 "u {text-decoration:none; border-bottom: 1px dotted black;}\n"
2250 "div.tips {\n"
2251 " display:block;\n"
2252 " visibility:hidden;\n"
2253 " z-index:2147483647;\n"
2254 " position:absolute;\n"
2255 " padding:2px 4px 3px;\n"
2256 " background:#f0f060; color:#000000;\n"
2257 " border:1px solid #7040c0;\n"
2258 " white-space:nowrap;\n"
2259 " font-style:normal;font-size:11px;font-weight:normal;\n"
2260 " -moz-border-radius:3px;-webkit-border-radius:3px;border-radius:3px;\n"
2261 " -moz-box-shadow:gray 2px 2px 3px;-webkit-box-shadow:gray 2px 2px 3px;box-shadow:gray 2px 2px 3px;\n"
2262 "}\n"
2263 "u:hover div.tips {visibility:visible;}\n"
2264 "-->\n"
2265 "</style></head>\n",
2266 (uri->flags & ST_SHNODE) ? " on " : "",
2267 (uri->flags & ST_SHNODE) ? (uri->node ? uri->node : global.node) : ""
2268 );
2269}
2270
2271/* Dumps the HTML stats information block to the trash for and uses the state from
2272 * stream interface <si> and per-uri parameters <uri>. The caller is responsible
2273 * for clearing the trash if needed.
2274 */
2275static void stats_dump_html_info(struct stream_interface *si, struct uri_auth *uri)
2276{
2277 struct appctx *appctx = __objt_appctx(si->end);
2278 unsigned int up = (now.tv_sec - start_date.tv_sec);
2279 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
2280
2281 /* WARNING! this has to fit the first packet too.
2282 * We are around 3.5 kB, add adding entries will
2283 * become tricky if we want to support 4kB buffers !
2284 */
2285 chunk_appendf(&trash,
2286 "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">"
2287 PRODUCT_NAME "%s</a></h1>\n"
2288 "<h2>Statistics Report for pid %d%s%s%s%s</h2>\n"
2289 "<hr width=\"100%%\" class=\"hr\">\n"
2290 "<h3>&gt; General process information</h3>\n"
2291 "<table border=0><tr><td align=\"left\" nowrap width=\"1%%\">\n"
Yves Lafon95317282018-02-26 11:10:37 +01002292 "<p><b>pid = </b> %d (process #%d, nbproc = %d, nbthread = %d)<br>\n"
William Lallemand74c24fb2016-11-21 17:18:36 +01002293 "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n"
2294 "<b>system limits:</b> memmax = %s%s; ulimit-n = %d<br>\n"
2295 "<b>maxsock = </b> %d; <b>maxconn = </b> %d; <b>maxpipes = </b> %d<br>\n"
2296 "current conns = %d; current pipes = %d/%d; conn rate = %d/sec<br>\n"
2297 "Running tasks: %d/%d; idle = %d %%<br>\n"
2298 "</td><td align=\"center\" nowrap>\n"
2299 "<table class=\"lgd\"><tr>\n"
2300 "<td class=\"active_up\">&nbsp;</td><td class=\"noborder\">active UP </td>"
2301 "<td class=\"backup_up\">&nbsp;</td><td class=\"noborder\">backup UP </td>"
2302 "</tr><tr>\n"
2303 "<td class=\"active_going_down\"></td><td class=\"noborder\">active UP, going down </td>"
2304 "<td class=\"backup_going_down\"></td><td class=\"noborder\">backup UP, going down </td>"
2305 "</tr><tr>\n"
2306 "<td class=\"active_going_up\"></td><td class=\"noborder\">active DOWN, going up </td>"
2307 "<td class=\"backup_going_up\"></td><td class=\"noborder\">backup DOWN, going up </td>"
2308 "</tr><tr>\n"
2309 "<td class=\"active_down\"></td><td class=\"noborder\">active or backup DOWN &nbsp;</td>"
2310 "<td class=\"active_no_check\"></td><td class=\"noborder\">not checked </td>"
2311 "</tr><tr>\n"
2312 "<td class=\"maintain\"></td><td class=\"noborder\" colspan=\"3\">active or backup DOWN for maintenance (MAINT) &nbsp;</td>"
2313 "</tr><tr>\n"
2314 "<td class=\"active_draining\"></td><td class=\"noborder\" colspan=\"3\">active or backup SOFT STOPPED for maintenance &nbsp;</td>"
2315 "</tr></table>\n"
2316 "Note: \"NOLB\"/\"DRAIN\" = UP with load-balancing disabled."
2317 "</td>"
2318 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
2319 "<b>Display option:</b><ul style=\"margin-top: 0.25em;\">"
2320 "",
2321 (uri->flags & ST_HIDEVER) ? "" : (STATS_VERSION_STRING),
2322 pid, (uri->flags & ST_SHNODE) ? " on " : "",
2323 (uri->flags & ST_SHNODE) ? (uri->node ? uri->node : global.node) : "",
2324 (uri->flags & ST_SHDESC) ? ": " : "",
2325 (uri->flags & ST_SHDESC) ? (uri->desc ? uri->desc : global.desc) : "",
Yves Lafon95317282018-02-26 11:10:37 +01002326 pid, relative_pid, global.nbproc, global.nbthread,
William Lallemand74c24fb2016-11-21 17:18:36 +01002327 up / 86400, (up % 86400) / 3600,
2328 (up % 3600) / 60, (up % 60),
2329 global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited",
2330 global.rlimit_memmax ? " MB" : "",
2331 global.rlimit_nofile,
2332 global.maxsock, global.maxconn, global.maxpipes,
2333 actconn, pipes_used, pipes_used+pipes_free, read_freq_ctr(&global.conn_per_sec),
Christopher Faulet34c5cc92016-12-06 09:15:30 +01002334 tasks_run_queue_cur, nb_tasks_cur, idle_pct
William Lallemand74c24fb2016-11-21 17:18:36 +01002335 );
2336
2337 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Willy Tarreau89faf5d2018-06-07 18:16:48 +02002338 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 +01002339 scope_txt[appctx->ctx.stats.scope_len] = '\0';
2340
2341 chunk_appendf(&trash,
2342 "<li><form method=\"GET\">Scope : <input value=\"%s\" name=\"" STAT_SCOPE_INPUT_NAME "\" size=\"8\" maxlength=\"%d\" tabindex=\"1\"/></form>\n",
2343 (appctx->ctx.stats.scope_len > 0) ? scope_txt : "",
2344 STAT_SCOPE_TXT_MAXLEN);
2345
2346 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
2347 scope_txt[0] = 0;
2348 if (appctx->ctx.stats.scope_len) {
2349 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau89faf5d2018-06-07 18:16:48 +02002350 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 +01002351 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
2352 }
2353
2354 if (appctx->ctx.stats.flags & STAT_HIDE_DOWN)
2355 chunk_appendf(&trash,
2356 "<li><a href=\"%s%s%s%s\">Show all servers</a><br>\n",
2357 uri->uri_prefix,
2358 "",
2359 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2360 scope_txt);
2361 else
2362 chunk_appendf(&trash,
2363 "<li><a href=\"%s%s%s%s\">Hide 'DOWN' servers</a><br>\n",
2364 uri->uri_prefix,
2365 ";up",
2366 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2367 scope_txt);
2368
2369 if (uri->refresh > 0) {
2370 if (appctx->ctx.stats.flags & STAT_NO_REFRESH)
2371 chunk_appendf(&trash,
2372 "<li><a href=\"%s%s%s%s\">Enable refresh</a><br>\n",
2373 uri->uri_prefix,
2374 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2375 "",
2376 scope_txt);
2377 else
2378 chunk_appendf(&trash,
2379 "<li><a href=\"%s%s%s%s\">Disable refresh</a><br>\n",
2380 uri->uri_prefix,
2381 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2382 ";norefresh",
2383 scope_txt);
2384 }
2385
2386 chunk_appendf(&trash,
2387 "<li><a href=\"%s%s%s%s\">Refresh now</a><br>\n",
2388 uri->uri_prefix,
2389 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2390 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2391 scope_txt);
2392
2393 chunk_appendf(&trash,
2394 "<li><a href=\"%s;csv%s%s\">CSV export</a><br>\n",
2395 uri->uri_prefix,
2396 (uri->refresh > 0) ? ";norefresh" : "",
2397 scope_txt);
2398
2399 chunk_appendf(&trash,
2400 "</ul></td>"
2401 "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">"
2402 "<b>External resources:</b><ul style=\"margin-top: 0.25em;\">\n"
2403 "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>\n"
2404 "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>\n"
2405 "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>\n"
2406 "</ul>"
2407 "</td>"
2408 "</tr></table>\n"
2409 ""
2410 );
2411
2412 if (appctx->ctx.stats.st_code) {
2413 switch (appctx->ctx.stats.st_code) {
2414 case STAT_STATUS_DONE:
2415 chunk_appendf(&trash,
2416 "<p><div class=active_up>"
2417 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2418 "Action processed successfully."
2419 "</div>\n", uri->uri_prefix,
2420 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2421 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2422 scope_txt);
2423 break;
2424 case STAT_STATUS_NONE:
2425 chunk_appendf(&trash,
2426 "<p><div class=active_going_down>"
2427 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2428 "Nothing has changed."
2429 "</div>\n", uri->uri_prefix,
2430 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2431 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2432 scope_txt);
2433 break;
2434 case STAT_STATUS_PART:
2435 chunk_appendf(&trash,
2436 "<p><div class=active_going_down>"
2437 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2438 "Action partially processed.<br>"
2439 "Some server names are probably unknown or ambiguous (duplicated names in the backend)."
2440 "</div>\n", uri->uri_prefix,
2441 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2442 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2443 scope_txt);
2444 break;
2445 case STAT_STATUS_ERRP:
2446 chunk_appendf(&trash,
2447 "<p><div class=active_down>"
2448 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2449 "Action not processed because of invalid parameters."
2450 "<ul>"
2451 "<li>The action is maybe unknown.</li>"
2452 "<li>The backend name is probably unknown or ambiguous (duplicated names).</li>"
2453 "<li>Some server names are probably unknown or ambiguous (duplicated names in the backend).</li>"
2454 "</ul>"
2455 "</div>\n", uri->uri_prefix,
2456 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2457 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2458 scope_txt);
2459 break;
2460 case STAT_STATUS_EXCD:
2461 chunk_appendf(&trash,
2462 "<p><div class=active_down>"
2463 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2464 "<b>Action not processed : the buffer couldn't store all the data.<br>"
2465 "You should retry with less servers at a time.</b>"
2466 "</div>\n", uri->uri_prefix,
2467 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2468 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2469 scope_txt);
2470 break;
2471 case STAT_STATUS_DENY:
2472 chunk_appendf(&trash,
2473 "<p><div class=active_down>"
2474 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2475 "<b>Action denied.</b>"
2476 "</div>\n", uri->uri_prefix,
2477 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2478 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2479 scope_txt);
2480 break;
2481 default:
2482 chunk_appendf(&trash,
2483 "<p><div class=active_no_check>"
2484 "<a class=lfsb href=\"%s%s%s%s\" title=\"Remove this message\">[X]</a> "
2485 "Unexpected result."
2486 "</div>\n", uri->uri_prefix,
2487 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
2488 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
2489 scope_txt);
2490 }
2491 chunk_appendf(&trash, "<p>\n");
2492 }
2493}
2494
2495/* Dumps the HTML stats trailer block to the trash. The caller is responsible
2496 * for clearing the trash if needed.
2497 */
2498static void stats_dump_html_end()
2499{
2500 chunk_appendf(&trash, "</body></html>\n");
2501}
2502
Simon Horman05ee2132017-01-04 09:37:25 +01002503/* Dumps the stats JSON header to the trash buffer which. The caller is responsible
2504 * for clearing it if needed.
2505 */
2506static void stats_dump_json_header()
2507{
2508 chunk_strcat(&trash, "[");
2509}
2510
2511
2512/* Dumps the JSON stats trailer block to the trash. The caller is responsible
2513 * for clearing the trash if needed.
2514 */
2515static void stats_dump_json_end()
2516{
2517 chunk_strcat(&trash, "]");
2518}
2519
William Lallemand74c24fb2016-11-21 17:18:36 +01002520/* This function dumps statistics onto the stream interface's read buffer in
2521 * either CSV or HTML format. <uri> contains some HTML-specific parameters that
2522 * are ignored for CSV format (hence <uri> may be NULL there). It returns 0 if
2523 * it had to stop writing data and an I/O is needed, 1 if the dump is finished
2524 * and the stream must be closed, or -1 in case of any error. This function is
2525 * used by both the CLI and the HTTP handlers.
2526 */
Willy Tarreau2b812e22016-11-22 16:18:05 +01002527static int stats_dump_stat_to_buffer(struct stream_interface *si, struct uri_auth *uri)
William Lallemand74c24fb2016-11-21 17:18:36 +01002528{
2529 struct appctx *appctx = __objt_appctx(si->end);
2530 struct channel *rep = si_ic(si);
2531 struct proxy *px;
2532
2533 chunk_reset(&trash);
2534
2535 switch (appctx->st2) {
2536 case STAT_ST_INIT:
2537 appctx->st2 = STAT_ST_HEAD; /* let's start producing data */
2538 /* fall through */
2539
2540 case STAT_ST_HEAD:
2541 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
2542 stats_dump_html_head(uri);
Simon Horman05ee2132017-01-04 09:37:25 +01002543 else if (appctx->ctx.stats.flags & STAT_FMT_JSON)
Willy Tarreau9d7fb632017-04-11 07:53:04 +02002544 stats_dump_json_header();
William Lallemand74c24fb2016-11-21 17:18:36 +01002545 else if (!(appctx->ctx.stats.flags & STAT_FMT_TYPED))
2546 stats_dump_csv_header();
2547
Willy Tarreau06d80a92017-10-19 14:32:15 +02002548 if (ci_putchk(rep, &trash) == -1) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002549 si_applet_cant_put(si);
2550 return 0;
2551 }
2552
2553 appctx->st2 = STAT_ST_INFO;
2554 /* fall through */
2555
2556 case STAT_ST_INFO:
2557 if (appctx->ctx.stats.flags & STAT_FMT_HTML) {
2558 stats_dump_html_info(si, uri);
Willy Tarreau06d80a92017-10-19 14:32:15 +02002559 if (ci_putchk(rep, &trash) == -1) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002560 si_applet_cant_put(si);
2561 return 0;
2562 }
2563 }
2564
Olivier Houchardfbc74e82017-11-24 16:54:05 +01002565 appctx->ctx.stats.px = proxies_list;
William Lallemand74c24fb2016-11-21 17:18:36 +01002566 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
2567 appctx->st2 = STAT_ST_LIST;
2568 /* fall through */
2569
2570 case STAT_ST_LIST:
2571 /* dump proxies */
2572 while (appctx->ctx.stats.px) {
2573 if (buffer_almost_full(rep->buf)) {
2574 si_applet_cant_put(si);
2575 return 0;
2576 }
2577
2578 px = appctx->ctx.stats.px;
2579 /* skip the disabled proxies, global frontend and non-networked ones */
2580 if (px->state != PR_STSTOPPED && px->uuid > 0 && (px->cap & (PR_CAP_FE | PR_CAP_BE)))
2581 if (stats_dump_proxy_to_buffer(si, px, uri) == 0)
2582 return 0;
2583
2584 appctx->ctx.stats.px = px->next;
2585 appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
2586 }
2587 /* here, we just have reached the last proxy */
2588
2589 appctx->st2 = STAT_ST_END;
2590 /* fall through */
2591
2592 case STAT_ST_END:
Simon Horman05ee2132017-01-04 09:37:25 +01002593 if (appctx->ctx.stats.flags & (STAT_FMT_HTML|STAT_FMT_JSON)) {
2594 if (appctx->ctx.stats.flags & STAT_FMT_HTML)
2595 stats_dump_html_end();
2596 else
2597 stats_dump_json_end();
Willy Tarreau06d80a92017-10-19 14:32:15 +02002598 if (ci_putchk(rep, &trash) == -1) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002599 si_applet_cant_put(si);
2600 return 0;
2601 }
2602 }
2603
2604 appctx->st2 = STAT_ST_FIN;
2605 /* fall through */
2606
2607 case STAT_ST_FIN:
2608 return 1;
2609
2610 default:
2611 /* unknown state ! */
2612 appctx->st2 = STAT_ST_FIN;
2613 return -1;
2614 }
2615}
2616
2617/* We reached the stats page through a POST request. The appctx is
2618 * expected to have already been allocated by the caller.
2619 * Parse the posted data and enable/disable servers if necessary.
2620 * Returns 1 if request was parsed or zero if it needs more data.
2621 */
2622static int stats_process_http_post(struct stream_interface *si)
2623{
2624 struct stream *s = si_strm(si);
2625 struct appctx *appctx = objt_appctx(si->end);
2626
2627 struct proxy *px = NULL;
2628 struct server *sv = NULL;
2629
2630 char key[LINESIZE];
2631 int action = ST_ADM_ACTION_NONE;
2632 int reprocess = 0;
2633
2634 int total_servers = 0;
2635 int altered_servers = 0;
2636
2637 char *first_param, *cur_param, *next_param, *end_params;
2638 char *st_cur_param = NULL;
2639 char *st_next_param = NULL;
2640
2641 struct chunk *temp;
2642 int reql;
2643
2644 temp = get_trash_chunk();
2645 if (temp->size < s->txn->req.body_len) {
2646 /* too large request */
2647 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
2648 goto out;
2649 }
2650
Willy Tarreau06d80a92017-10-19 14:32:15 +02002651 reql = co_getblk(si_oc(si), temp->str, s->txn->req.body_len, s->txn->req.eoh + 2);
William Lallemand74c24fb2016-11-21 17:18:36 +01002652 if (reql <= 0) {
2653 /* we need more data */
2654 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
2655 return 0;
2656 }
2657
2658 first_param = temp->str;
2659 end_params = temp->str + reql;
2660 cur_param = next_param = end_params;
2661 *end_params = '\0';
2662
2663 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
2664
2665 /*
2666 * Parse the parameters in reverse order to only store the last value.
2667 * From the html form, the backend and the action are at the end.
2668 */
2669 while (cur_param > first_param) {
2670 char *value;
2671 int poffset, plen;
2672
2673 cur_param--;
2674
2675 if ((*cur_param == '&') || (cur_param == first_param)) {
2676 reprocess_servers:
2677 /* Parse the key */
2678 poffset = (cur_param != first_param ? 1 : 0);
2679 plen = next_param - cur_param + (cur_param == first_param ? 1 : 0);
2680 if ((plen > 0) && (plen <= sizeof(key))) {
2681 strncpy(key, cur_param + poffset, plen);
2682 key[plen - 1] = '\0';
2683 } else {
2684 appctx->ctx.stats.st_code = STAT_STATUS_EXCD;
2685 goto out;
2686 }
2687
2688 /* Parse the value */
2689 value = key;
2690 while (*value != '\0' && *value != '=') {
2691 value++;
2692 }
2693 if (*value == '=') {
2694 /* Ok, a value is found, we can mark the end of the key */
2695 *value++ = '\0';
2696 }
2697 if (url_decode(key) < 0 || url_decode(value) < 0)
2698 break;
2699
2700 /* Now we can check the key to see what to do */
2701 if (!px && (strcmp(key, "b") == 0)) {
2702 if ((px = proxy_be_by_name(value)) == NULL) {
2703 /* the backend name is unknown or ambiguous (duplicate names) */
2704 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
2705 goto out;
2706 }
2707 }
2708 else if (!action && (strcmp(key, "action") == 0)) {
2709 if (strcmp(value, "ready") == 0) {
2710 action = ST_ADM_ACTION_READY;
2711 }
2712 else if (strcmp(value, "drain") == 0) {
2713 action = ST_ADM_ACTION_DRAIN;
2714 }
2715 else if (strcmp(value, "maint") == 0) {
2716 action = ST_ADM_ACTION_MAINT;
2717 }
2718 else if (strcmp(value, "shutdown") == 0) {
2719 action = ST_ADM_ACTION_SHUTDOWN;
2720 }
2721 else if (strcmp(value, "dhlth") == 0) {
2722 action = ST_ADM_ACTION_DHLTH;
2723 }
2724 else if (strcmp(value, "ehlth") == 0) {
2725 action = ST_ADM_ACTION_EHLTH;
2726 }
2727 else if (strcmp(value, "hrunn") == 0) {
2728 action = ST_ADM_ACTION_HRUNN;
2729 }
2730 else if (strcmp(value, "hnolb") == 0) {
2731 action = ST_ADM_ACTION_HNOLB;
2732 }
2733 else if (strcmp(value, "hdown") == 0) {
2734 action = ST_ADM_ACTION_HDOWN;
2735 }
2736 else if (strcmp(value, "dagent") == 0) {
2737 action = ST_ADM_ACTION_DAGENT;
2738 }
2739 else if (strcmp(value, "eagent") == 0) {
2740 action = ST_ADM_ACTION_EAGENT;
2741 }
2742 else if (strcmp(value, "arunn") == 0) {
2743 action = ST_ADM_ACTION_ARUNN;
2744 }
2745 else if (strcmp(value, "adown") == 0) {
2746 action = ST_ADM_ACTION_ADOWN;
2747 }
2748 /* else these are the old supported methods */
2749 else if (strcmp(value, "disable") == 0) {
2750 action = ST_ADM_ACTION_DISABLE;
2751 }
2752 else if (strcmp(value, "enable") == 0) {
2753 action = ST_ADM_ACTION_ENABLE;
2754 }
2755 else if (strcmp(value, "stop") == 0) {
2756 action = ST_ADM_ACTION_STOP;
2757 }
2758 else if (strcmp(value, "start") == 0) {
2759 action = ST_ADM_ACTION_START;
2760 }
2761 else {
2762 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
2763 goto out;
2764 }
2765 }
2766 else if (strcmp(key, "s") == 0) {
2767 if (!(px && action)) {
2768 /*
2769 * Indicates that we'll need to reprocess the parameters
2770 * as soon as backend and action are known
2771 */
2772 if (!reprocess) {
2773 st_cur_param = cur_param;
2774 st_next_param = next_param;
2775 }
2776 reprocess = 1;
2777 }
2778 else if ((sv = findserver(px, value)) != NULL) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002779 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
William Lallemand74c24fb2016-11-21 17:18:36 +01002780 switch (action) {
2781 case ST_ADM_ACTION_DISABLE:
Emeric Brun52a91d32017-08-31 14:41:55 +02002782 if (!(sv->cur_admin & SRV_ADMF_FMAINT)) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002783 altered_servers++;
2784 total_servers++;
2785 srv_set_admin_flag(sv, SRV_ADMF_FMAINT, "'disable' on stats page");
2786 }
2787 break;
2788 case ST_ADM_ACTION_ENABLE:
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_clr_admin_flag(sv, SRV_ADMF_FMAINT);
2793 }
2794 break;
2795 case ST_ADM_ACTION_STOP:
Emeric Brun52a91d32017-08-31 14:41:55 +02002796 if (!(sv->cur_admin & SRV_ADMF_FDRAIN)) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002797 srv_set_admin_flag(sv, SRV_ADMF_FDRAIN, "'stop' on stats page");
2798 altered_servers++;
2799 total_servers++;
2800 }
2801 break;
2802 case ST_ADM_ACTION_START:
Emeric Brun52a91d32017-08-31 14:41:55 +02002803 if (sv->cur_admin & SRV_ADMF_FDRAIN) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002804 srv_clr_admin_flag(sv, SRV_ADMF_FDRAIN);
2805 altered_servers++;
2806 total_servers++;
2807 }
2808 break;
2809 case ST_ADM_ACTION_DHLTH:
2810 if (sv->check.state & CHK_ST_CONFIGURED) {
2811 sv->check.state &= ~CHK_ST_ENABLED;
2812 altered_servers++;
2813 total_servers++;
2814 }
2815 break;
2816 case ST_ADM_ACTION_EHLTH:
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_HRUNN:
2824 if (!(sv->track)) {
2825 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02002826 srv_set_running(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01002827 altered_servers++;
2828 total_servers++;
2829 }
2830 break;
2831 case ST_ADM_ACTION_HNOLB:
2832 if (!(sv->track)) {
2833 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02002834 srv_set_stopping(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01002835 altered_servers++;
2836 total_servers++;
2837 }
2838 break;
2839 case ST_ADM_ACTION_HDOWN:
2840 if (!(sv->track)) {
2841 sv->check.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02002842 srv_set_stopped(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01002843 altered_servers++;
2844 total_servers++;
2845 }
2846 break;
2847 case ST_ADM_ACTION_DAGENT:
2848 if (sv->agent.state & CHK_ST_CONFIGURED) {
2849 sv->agent.state &= ~CHK_ST_ENABLED;
2850 altered_servers++;
2851 total_servers++;
2852 }
2853 break;
2854 case ST_ADM_ACTION_EAGENT:
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_ARUNN:
2862 if (sv->agent.state & CHK_ST_ENABLED) {
2863 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02002864 srv_set_running(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01002865 altered_servers++;
2866 total_servers++;
2867 }
2868 break;
2869 case ST_ADM_ACTION_ADOWN:
2870 if (sv->agent.state & CHK_ST_ENABLED) {
2871 sv->agent.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02002872 srv_set_stopped(sv, "changed from Web interface", NULL);
William Lallemand74c24fb2016-11-21 17:18:36 +01002873 altered_servers++;
2874 total_servers++;
2875 }
2876 break;
2877 case ST_ADM_ACTION_READY:
2878 srv_adm_set_ready(sv);
2879 altered_servers++;
2880 total_servers++;
2881 break;
2882 case ST_ADM_ACTION_DRAIN:
2883 srv_adm_set_drain(sv);
2884 altered_servers++;
2885 total_servers++;
2886 break;
2887 case ST_ADM_ACTION_MAINT:
2888 srv_adm_set_maint(sv);
2889 altered_servers++;
2890 total_servers++;
2891 break;
2892 case ST_ADM_ACTION_SHUTDOWN:
2893 if (px->state != PR_STSTOPPED) {
2894 struct stream *sess, *sess_bck;
2895
2896 list_for_each_entry_safe(sess, sess_bck, &sv->actconns, by_srv)
2897 if (sess->srv_conn == sv)
2898 stream_shutdown(sess, SF_ERR_KILLED);
2899
2900 altered_servers++;
2901 total_servers++;
2902 }
2903 break;
2904 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002905 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Lallemand74c24fb2016-11-21 17:18:36 +01002906 } else {
2907 /* the server name is unknown or ambiguous (duplicate names) */
2908 total_servers++;
2909 }
2910 }
2911 if (reprocess && px && action) {
2912 /* Now, we know the backend and the action chosen by the user.
2913 * We can safely restart from the first server parameter
2914 * to reprocess them
2915 */
2916 cur_param = st_cur_param;
2917 next_param = st_next_param;
2918 reprocess = 0;
2919 goto reprocess_servers;
2920 }
2921
2922 next_param = cur_param;
2923 }
2924 }
2925
2926 if (total_servers == 0) {
2927 appctx->ctx.stats.st_code = STAT_STATUS_NONE;
2928 }
2929 else if (altered_servers == 0) {
2930 appctx->ctx.stats.st_code = STAT_STATUS_ERRP;
2931 }
2932 else if (altered_servers == total_servers) {
2933 appctx->ctx.stats.st_code = STAT_STATUS_DONE;
2934 }
2935 else {
2936 appctx->ctx.stats.st_code = STAT_STATUS_PART;
2937 }
2938 out:
2939 return 1;
2940}
2941
2942
2943static int stats_send_http_headers(struct stream_interface *si)
2944{
2945 struct stream *s = si_strm(si);
2946 struct uri_auth *uri = s->be->uri_auth;
2947 struct appctx *appctx = objt_appctx(si->end);
2948
2949 chunk_printf(&trash,
2950 "HTTP/1.1 200 OK\r\n"
2951 "Cache-Control: no-cache\r\n"
2952 "Connection: close\r\n"
2953 "Content-Type: %s\r\n",
2954 (appctx->ctx.stats.flags & STAT_FMT_HTML) ? "text/html" : "text/plain");
2955
2956 if (uri->refresh > 0 && !(appctx->ctx.stats.flags & STAT_NO_REFRESH))
2957 chunk_appendf(&trash, "Refresh: %d\r\n",
2958 uri->refresh);
2959
2960 /* we don't send the CRLF in chunked mode, it will be sent with the first chunk's size */
2961
2962 if (appctx->ctx.stats.flags & STAT_CHUNKED)
2963 chunk_appendf(&trash, "Transfer-Encoding: chunked\r\n");
2964 else
2965 chunk_appendf(&trash, "\r\n");
2966
Willy Tarreau06d80a92017-10-19 14:32:15 +02002967 if (ci_putchk(si_ic(si), &trash) == -1) {
William Lallemand74c24fb2016-11-21 17:18:36 +01002968 si_applet_cant_put(si);
2969 return 0;
2970 }
2971
2972 return 1;
2973}
2974
2975static int stats_send_http_redirect(struct stream_interface *si)
2976{
2977 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
2978 struct stream *s = si_strm(si);
2979 struct uri_auth *uri = s->be->uri_auth;
2980 struct appctx *appctx = objt_appctx(si->end);
2981
2982 /* scope_txt = search pattern + search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
2983 scope_txt[0] = 0;
2984 if (appctx->ctx.stats.scope_len) {
2985 strcpy(scope_txt, STAT_SCOPE_PATTERN);
Willy Tarreau89faf5d2018-06-07 18:16:48 +02002986 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 +01002987 scope_txt[strlen(STAT_SCOPE_PATTERN) + appctx->ctx.stats.scope_len] = 0;
2988 }
2989
2990 /* We don't want to land on the posted stats page because a refresh will
2991 * repost the data. We don't want this to happen on accident so we redirect
2992 * the browse to the stats page with a GET.
2993 */
2994 chunk_printf(&trash,
2995 "HTTP/1.1 303 See Other\r\n"
2996 "Cache-Control: no-cache\r\n"
2997 "Content-Type: text/plain\r\n"
2998 "Connection: close\r\n"
2999 "Location: %s;st=%s%s%s%s\r\n"
3000 "Content-length: 0\r\n"
3001 "\r\n",
3002 uri->uri_prefix,
3003 ((appctx->ctx.stats.st_code > STAT_STATUS_INIT) &&
3004 (appctx->ctx.stats.st_code < STAT_STATUS_SIZE) &&
3005 stat_status_codes[appctx->ctx.stats.st_code]) ?
3006 stat_status_codes[appctx->ctx.stats.st_code] :
3007 stat_status_codes[STAT_STATUS_UNKN],
3008 (appctx->ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3009 (appctx->ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3010 scope_txt);
3011
Willy Tarreau06d80a92017-10-19 14:32:15 +02003012 if (ci_putchk(si_ic(si), &trash) == -1) {
William Lallemand74c24fb2016-11-21 17:18:36 +01003013 si_applet_cant_put(si);
3014 return 0;
3015 }
3016
3017 return 1;
3018}
3019
Simon Horman05ee2132017-01-04 09:37:25 +01003020
William Lallemand74c24fb2016-11-21 17:18:36 +01003021/* This I/O handler runs as an applet embedded in a stream interface. It is
3022 * used to send HTTP stats over a TCP socket. The mechanism is very simple.
3023 * appctx->st0 contains the operation in progress (dump, done). The handler
3024 * automatically unregisters itself once transfer is complete.
3025 */
3026static void http_stats_io_handler(struct appctx *appctx)
3027{
3028 struct stream_interface *si = appctx->owner;
3029 struct stream *s = si_strm(si);
3030 struct channel *req = si_oc(si);
3031 struct channel *res = si_ic(si);
3032
3033 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
3034 goto out;
3035
Christopher Fauleta73e59b2016-12-09 17:30:18 +01003036 /* Check if the input buffer is avalaible. */
3037 if (res->buf->size == 0) {
3038 si_applet_cant_put(si);
3039 goto out;
3040 }
3041
William Lallemand74c24fb2016-11-21 17:18:36 +01003042 /* check that the output is not closed */
3043 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW))
3044 appctx->st0 = STAT_HTTP_DONE;
3045
3046 /* all states are processed in sequence */
3047 if (appctx->st0 == STAT_HTTP_HEAD) {
3048 if (stats_send_http_headers(si)) {
3049 if (s->txn->meth == HTTP_METH_HEAD)
3050 appctx->st0 = STAT_HTTP_DONE;
3051 else
3052 appctx->st0 = STAT_HTTP_DUMP;
3053 }
3054 }
3055
3056 if (appctx->st0 == STAT_HTTP_DUMP) {
Willy Tarreau97f538b2018-06-15 19:41:31 +02003057 unsigned int prev_len = ci_data(si_ic(si));
William Lallemand74c24fb2016-11-21 17:18:36 +01003058 unsigned int data_len;
3059 unsigned int last_len;
3060 unsigned int last_fwd = 0;
3061
3062 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
3063 /* One difficulty we're facing is that we must prevent
3064 * the input data from being automatically forwarded to
3065 * the output area. For this, we temporarily disable
3066 * forwarding on the channel.
3067 */
3068 last_fwd = si_ic(si)->to_forward;
3069 si_ic(si)->to_forward = 0;
3070 chunk_printf(&trash, "\r\n000000\r\n");
Willy Tarreau06d80a92017-10-19 14:32:15 +02003071 if (ci_putchk(si_ic(si), &trash) == -1) {
William Lallemand74c24fb2016-11-21 17:18:36 +01003072 si_applet_cant_put(si);
3073 si_ic(si)->to_forward = last_fwd;
3074 goto out;
3075 }
3076 }
3077
Willy Tarreau97f538b2018-06-15 19:41:31 +02003078 data_len = ci_data(si_ic(si));
William Lallemand74c24fb2016-11-21 17:18:36 +01003079 if (stats_dump_stat_to_buffer(si, s->be->uri_auth))
3080 appctx->st0 = STAT_HTTP_DONE;
3081
Willy Tarreau97f538b2018-06-15 19:41:31 +02003082 last_len = ci_data(si_ic(si));
William Lallemand74c24fb2016-11-21 17:18:36 +01003083
3084 /* Now we must either adjust or remove the chunk size. This is
3085 * not easy because the chunk size might wrap at the end of the
3086 * buffer, so we pretend we have nothing in the buffer, we write
3087 * the size, then restore the buffer's contents. Note that we can
3088 * only do that because no forwarding is scheduled on the stats
3089 * applet.
3090 */
3091 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
3092 si_ic(si)->total -= (last_len - prev_len);
Willy Tarreau97f538b2018-06-15 19:41:31 +02003093 b_sub(si_ib(si), (last_len - prev_len));
William Lallemand74c24fb2016-11-21 17:18:36 +01003094
3095 if (last_len != data_len) {
3096 chunk_printf(&trash, "\r\n%06x\r\n", (last_len - data_len));
Willy Tarreau06d80a92017-10-19 14:32:15 +02003097 if (ci_putchk(si_ic(si), &trash) == -1)
William Lallemand74c24fb2016-11-21 17:18:36 +01003098 si_applet_cant_put(si);
3099
3100 si_ic(si)->total += (last_len - data_len);
Willy Tarreau97f538b2018-06-15 19:41:31 +02003101 b_add(si_ib(si), last_len - data_len);
William Lallemand74c24fb2016-11-21 17:18:36 +01003102 }
3103 /* now re-enable forwarding */
3104 channel_forward(si_ic(si), last_fwd);
3105 }
3106 }
3107
3108 if (appctx->st0 == STAT_HTTP_POST) {
3109 if (stats_process_http_post(si))
3110 appctx->st0 = STAT_HTTP_LAST;
3111 else if (si_oc(si)->flags & CF_SHUTR)
3112 appctx->st0 = STAT_HTTP_DONE;
3113 }
3114
3115 if (appctx->st0 == STAT_HTTP_LAST) {
3116 if (stats_send_http_redirect(si))
3117 appctx->st0 = STAT_HTTP_DONE;
3118 }
3119
3120 if (appctx->st0 == STAT_HTTP_DONE) {
3121 if (appctx->ctx.stats.flags & STAT_CHUNKED) {
3122 chunk_printf(&trash, "\r\n0\r\n\r\n");
Willy Tarreau06d80a92017-10-19 14:32:15 +02003123 if (ci_putchk(si_ic(si), &trash) == -1) {
William Lallemand74c24fb2016-11-21 17:18:36 +01003124 si_applet_cant_put(si);
3125 goto out;
3126 }
3127 }
3128 /* eat the whole request */
Willy Tarreau97f538b2018-06-15 19:41:31 +02003129 co_skip(si_oc(si), co_data(si_oc(si)));
William Lallemand74c24fb2016-11-21 17:18:36 +01003130 res->flags |= CF_READ_NULL;
3131 si_shutr(si);
3132 }
3133
3134 if ((res->flags & CF_SHUTR) && (si->state == SI_ST_EST))
3135 si_shutw(si);
3136
3137 if (appctx->st0 == STAT_HTTP_DONE) {
3138 if ((req->flags & CF_SHUTW) && (si->state == SI_ST_EST)) {
3139 si_shutr(si);
3140 res->flags |= CF_READ_NULL;
3141 }
3142 }
3143 out:
3144 /* just to make gcc happy */ ;
3145}
3146
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003147/* Dump all fields from <info> into <out> using the "show info" format (name: value) */
3148static int stats_dump_info_fields(struct chunk *out, const struct field *info)
3149{
3150 int field;
3151
3152 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
3153 if (!field_format(info, field))
3154 continue;
3155
3156 if (!chunk_appendf(out, "%s: ", info_field_names[field]))
3157 return 0;
3158 if (!stats_emit_raw_data_field(out, &info[field]))
3159 return 0;
3160 if (!chunk_strcat(out, "\n"))
3161 return 0;
3162 }
3163 return 1;
3164}
3165
3166/* Dump all fields from <info> into <out> using the "show info typed" format */
3167static int stats_dump_typed_info_fields(struct chunk *out, const struct field *info)
3168{
3169 int field;
3170
3171 for (field = 0; field < INF_TOTAL_FIELDS; field++) {
3172 if (!field_format(info, field))
3173 continue;
3174
3175 if (!chunk_appendf(out, "%d.%s.%u:", field, info_field_names[field], info[INF_PROCESS_NUM].u.u32))
3176 return 0;
3177 if (!stats_emit_field_tags(out, &info[field], ':'))
3178 return 0;
3179 if (!stats_emit_typed_data_field(out, &info[field]))
3180 return 0;
3181 if (!chunk_strcat(out, "\n"))
3182 return 0;
3183 }
3184 return 1;
3185}
3186
3187/* Fill <info> with HAProxy global info. <info> is preallocated
3188 * array of length <len>. The length of the aray must be
3189 * INF_TOTAL_FIELDS. If this length is less then this value, the
3190 * function returns 0, otherwise, it returns 1.
3191 */
3192int stats_fill_info(struct field *info, int len)
3193{
3194 unsigned int up = (now.tv_sec - start_date.tv_sec);
3195 struct chunk *out = get_trash_chunk();
3196
3197#ifdef USE_OPENSSL
3198 int ssl_sess_rate = read_freq_ctr(&global.ssl_per_sec);
3199 int ssl_key_rate = read_freq_ctr(&global.ssl_fe_keys_per_sec);
3200 int ssl_reuse = 0;
3201
3202 if (ssl_key_rate < ssl_sess_rate) {
3203 /* count the ssl reuse ratio and avoid overflows in both directions */
3204 ssl_reuse = 100 - (100 * ssl_key_rate + (ssl_sess_rate - 1) / 2) / ssl_sess_rate;
3205 }
3206#endif
3207
3208 if (len < INF_TOTAL_FIELDS)
3209 return 0;
3210
3211 chunk_reset(out);
3212 memset(info, 0, sizeof(*info) * len);
3213
3214 info[INF_NAME] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, PRODUCT_NAME);
3215 info[INF_VERSION] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, HAPROXY_VERSION);
3216 info[INF_RELEASE_DATE] = mkf_str(FO_PRODUCT|FN_OUTPUT|FS_SERVICE, HAPROXY_DATE);
3217
Yves Lafon95317282018-02-26 11:10:37 +01003218 info[INF_NBTHREAD] = mkf_u32(FO_CONFIG|FS_SERVICE, global.nbthread);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003219 info[INF_NBPROC] = mkf_u32(FO_CONFIG|FS_SERVICE, global.nbproc);
3220 info[INF_PROCESS_NUM] = mkf_u32(FO_KEY, relative_pid);
3221 info[INF_PID] = mkf_u32(FO_STATUS, pid);
3222
3223 info[INF_UPTIME] = mkf_str(FN_DURATION, chunk_newstr(out));
3224 chunk_appendf(out, "%ud %uh%02um%02us", up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60));
3225
3226 info[INF_UPTIME_SEC] = mkf_u32(FN_DURATION, up);
3227 info[INF_MEMMAX_MB] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_memmax);
3228 info[INF_POOL_ALLOC_MB] = mkf_u32(0, (unsigned)(pool_total_allocated() / 1048576L));
3229 info[INF_POOL_USED_MB] = mkf_u32(0, (unsigned)(pool_total_used() / 1048576L));
3230 info[INF_POOL_FAILED] = mkf_u32(FN_COUNTER, pool_total_failures());
3231 info[INF_ULIMIT_N] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_nofile);
3232 info[INF_MAXSOCK] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxsock);
3233 info[INF_MAXCONN] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxconn);
3234 info[INF_HARD_MAXCONN] = mkf_u32(FO_CONFIG|FN_LIMIT, global.hardmaxconn);
3235 info[INF_CURR_CONN] = mkf_u32(0, actconn);
3236 info[INF_CUM_CONN] = mkf_u32(FN_COUNTER, totalconn);
3237 info[INF_CUM_REQ] = mkf_u32(FN_COUNTER, global.req_count);
3238#ifdef USE_OPENSSL
3239 info[INF_MAX_SSL_CONNS] = mkf_u32(FN_MAX, global.maxsslconn);
3240 info[INF_CURR_SSL_CONNS] = mkf_u32(0, sslconns);
3241 info[INF_CUM_SSL_CONNS] = mkf_u32(FN_COUNTER, totalsslconns);
3242#endif
3243 info[INF_MAXPIPES] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxpipes);
3244 info[INF_PIPES_USED] = mkf_u32(0, pipes_used);
3245 info[INF_PIPES_FREE] = mkf_u32(0, pipes_free);
3246 info[INF_CONN_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.conn_per_sec));
3247 info[INF_CONN_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.cps_lim);
3248 info[INF_MAX_CONN_RATE] = mkf_u32(FN_MAX, global.cps_max);
3249 info[INF_SESS_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.sess_per_sec));
3250 info[INF_SESS_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.sps_lim);
3251 info[INF_MAX_SESS_RATE] = mkf_u32(FN_RATE, global.sps_max);
3252
3253#ifdef USE_OPENSSL
3254 info[INF_SSL_RATE] = mkf_u32(FN_RATE, ssl_sess_rate);
3255 info[INF_SSL_RATE_LIMIT] = mkf_u32(FO_CONFIG|FN_LIMIT, global.ssl_lim);
3256 info[INF_MAX_SSL_RATE] = mkf_u32(FN_MAX, global.ssl_max);
3257 info[INF_SSL_FRONTEND_KEY_RATE] = mkf_u32(0, ssl_key_rate);
3258 info[INF_SSL_FRONTEND_MAX_KEY_RATE] = mkf_u32(FN_MAX, global.ssl_fe_keys_max);
3259 info[INF_SSL_FRONTEND_SESSION_REUSE_PCT] = mkf_u32(0, ssl_reuse);
3260 info[INF_SSL_BACKEND_KEY_RATE] = mkf_u32(FN_RATE, read_freq_ctr(&global.ssl_be_keys_per_sec));
3261 info[INF_SSL_BACKEND_MAX_KEY_RATE] = mkf_u32(FN_MAX, global.ssl_be_keys_max);
3262 info[INF_SSL_CACHE_LOOKUPS] = mkf_u32(FN_COUNTER, global.shctx_lookups);
3263 info[INF_SSL_CACHE_MISSES] = mkf_u32(FN_COUNTER, global.shctx_misses);
3264#endif
3265 info[INF_COMPRESS_BPS_IN] = mkf_u32(FN_RATE, read_freq_ctr(&global.comp_bps_in));
3266 info[INF_COMPRESS_BPS_OUT] = mkf_u32(FN_RATE, read_freq_ctr(&global.comp_bps_out));
3267 info[INF_COMPRESS_BPS_RATE_LIM] = mkf_u32(FO_CONFIG|FN_LIMIT, global.comp_rate_lim);
3268#ifdef USE_ZLIB
3269 info[INF_ZLIB_MEM_USAGE] = mkf_u32(0, zlib_used_memory);
3270 info[INF_MAX_ZLIB_MEM_USAGE] = mkf_u32(FO_CONFIG|FN_LIMIT, global.maxzlibmem);
3271#endif
3272 info[INF_TASKS] = mkf_u32(0, nb_tasks_cur);
Christopher Faulet34c5cc92016-12-06 09:15:30 +01003273 info[INF_RUN_QUEUE] = mkf_u32(0, tasks_run_queue_cur);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003274 info[INF_IDLE_PCT] = mkf_u32(FN_AVG, idle_pct);
3275 info[INF_NODE] = mkf_str(FO_CONFIG|FN_OUTPUT|FS_SERVICE, global.node);
3276 if (global.desc)
3277 info[INF_DESCRIPTION] = mkf_str(FO_CONFIG|FN_OUTPUT|FS_SERVICE, global.desc);
3278
3279 return 1;
3280}
3281
3282/* This function dumps information onto the stream interface's read buffer.
3283 * It returns 0 as long as it does not complete, non-zero upon completion.
3284 * No state is used.
3285 */
3286static int stats_dump_info_to_buffer(struct stream_interface *si)
3287{
3288 struct appctx *appctx = __objt_appctx(si->end);
3289
3290 if (!stats_fill_info(info, INF_TOTAL_FIELDS))
3291 return 0;
3292
3293 chunk_reset(&trash);
3294
3295 if (appctx->ctx.stats.flags & STAT_FMT_TYPED)
3296 stats_dump_typed_info_fields(&trash, info);
Simon Horman05ee2132017-01-04 09:37:25 +01003297 else if (appctx->ctx.stats.flags & STAT_FMT_JSON)
3298 stats_dump_json_info_fields(&trash, info);
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003299 else
3300 stats_dump_info_fields(&trash, info);
3301
Willy Tarreau06d80a92017-10-19 14:32:15 +02003302 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003303 si_applet_cant_put(si);
3304 return 0;
3305 }
3306
3307 return 1;
3308}
3309
Simon Horman6f6bb382017-01-04 09:37:26 +01003310/* This function dumps the schema onto the stream interface's read buffer.
3311 * It returns 0 as long as it does not complete, non-zero upon completion.
3312 * No state is used.
3313 *
3314 * Integer values bouned to the range [-(2**53)+1, (2**53)-1] as
3315 * per the recommendation for interoperable integers in section 6 of RFC 7159.
3316 */
3317static void stats_dump_json_schema(struct chunk *out)
3318{
3319
3320 int old_len = out->len;
3321
3322 chunk_strcat(out,
3323 "{"
3324 "\"$schema\":\"http://json-schema.org/draft-04/schema#\","
3325 "\"oneOf\":["
3326 "{"
3327 "\"title\":\"Info\","
3328 "\"type\":\"array\","
3329 "\"items\":{"
3330 "\"properties\":{"
3331 "\"title\":\"InfoItem\","
3332 "\"type\":\"object\","
3333 "\"field\":{\"$ref\":\"#/definitions/field\"},"
3334 "\"processNum\":{\"$ref\":\"#/definitions/processNum\"},"
3335 "\"tags\":{\"$ref\":\"#/definitions/tags\"},"
3336 "\"value\":{\"$ref\":\"#/definitions/typedValue\"}"
3337 "},"
3338 "\"required\":[\"field\",\"processNum\",\"tags\","
3339 "\"value\"]"
3340 "}"
3341 "},"
3342 "{"
3343 "\"title\":\"Stat\","
3344 "\"type\":\"array\","
3345 "\"items\":{"
3346 "\"title\":\"InfoItem\","
3347 "\"type\":\"object\","
3348 "\"properties\":{"
3349 "\"objType\":{"
3350 "\"enum\":[\"Frontend\",\"Backend\",\"Listener\","
3351 "\"Server\",\"Unknown\"]"
3352 "},"
3353 "\"proxyId\":{"
3354 "\"type\":\"integer\","
3355 "\"minimum\":0"
3356 "},"
3357 "\"id\":{"
3358 "\"type\":\"integer\","
3359 "\"minimum\":0"
3360 "},"
3361 "\"field\":{\"$ref\":\"#/definitions/field\"},"
3362 "\"processNum\":{\"$ref\":\"#/definitions/processNum\"},"
3363 "\"tags\":{\"$ref\":\"#/definitions/tags\"},"
3364 "\"typedValue\":{\"$ref\":\"#/definitions/typedValue\"}"
3365 "},"
3366 "\"required\":[\"objType\",\"proxyId\",\"id\","
3367 "\"field\",\"processNum\",\"tags\","
3368 "\"value\"]"
3369 "}"
3370 "},"
3371 "{"
3372 "\"title\":\"Error\","
3373 "\"type\":\"object\","
3374 "\"properties\":{"
3375 "\"errorStr\":{"
3376 "\"type\":\"string\""
3377 "},"
3378 "\"required\":[\"errorStr\"]"
3379 "}"
3380 "}"
3381 "],"
3382 "\"definitions\":{"
3383 "\"field\":{"
3384 "\"type\":\"object\","
3385 "\"pos\":{"
3386 "\"type\":\"integer\","
3387 "\"minimum\":0"
3388 "},"
3389 "\"name\":{"
3390 "\"type\":\"string\""
3391 "},"
3392 "\"required\":[\"pos\",\"name\"]"
3393 "},"
3394 "\"processNum\":{"
3395 "\"type\":\"integer\","
3396 "\"minimum\":1"
3397 "},"
3398 "\"tags\":{"
3399 "\"type\":\"object\","
3400 "\"origin\":{"
3401 "\"type\":\"string\","
3402 "\"enum\":[\"Metric\",\"Status\",\"Key\","
3403 "\"Config\",\"Product\",\"Unknown\"]"
3404 "},"
3405 "\"nature\":{"
3406 "\"type\":\"string\","
3407 "\"enum\":[\"Gauge\",\"Limit\",\"Min\",\"Max\","
3408 "\"Rate\",\"Counter\",\"Duration\","
3409 "\"Age\",\"Time\",\"Name\",\"Output\","
3410 "\"Avg\", \"Unknown\"]"
3411 "},"
3412 "\"scope\":{"
3413 "\"type\":\"string\","
3414 "\"enum\":[\"Cluster\",\"Process\",\"Service\","
3415 "\"System\",\"Unknown\"]"
3416 "},"
3417 "\"required\":[\"origin\",\"nature\",\"scope\"]"
3418 "},"
3419 "\"typedValue\":{"
3420 "\"type\":\"object\","
3421 "\"oneOf\":["
3422 "{\"$ref\":\"#/definitions/typedValue/definitions/s32Value\"},"
3423 "{\"$ref\":\"#/definitions/typedValue/definitions/s64Value\"},"
3424 "{\"$ref\":\"#/definitions/typedValue/definitions/u32Value\"},"
3425 "{\"$ref\":\"#/definitions/typedValue/definitions/u64Value\"},"
3426 "{\"$ref\":\"#/definitions/typedValue/definitions/strValue\"}"
3427 "],"
3428 "\"definitions\":{"
3429 "\"s32Value\":{"
3430 "\"properties\":{"
3431 "\"type\":{"
3432 "\"type\":\"string\","
3433 "\"enum\":[\"s32\"]"
3434 "},"
3435 "\"value\":{"
3436 "\"type\":\"integer\","
3437 "\"minimum\":-2147483648,"
3438 "\"maximum\":2147483647"
3439 "}"
3440 "},"
3441 "\"required\":[\"type\",\"value\"]"
3442 "},"
3443 "\"s64Value\":{"
3444 "\"properties\":{"
3445 "\"type\":{"
3446 "\"type\":\"string\","
3447 "\"enum\":[\"s64\"]"
3448 "},"
3449 "\"value\":{"
3450 "\"type\":\"integer\","
3451 "\"minimum\":-9007199254740991,"
3452 "\"maximum\":9007199254740991"
3453 "}"
3454 "},"
3455 "\"required\":[\"type\",\"value\"]"
3456 "},"
3457 "\"u32Value\":{"
3458 "\"properties\":{"
3459 "\"type\":{"
3460 "\"type\":\"string\","
3461 "\"enum\":[\"u32\"]"
3462 "},"
3463 "\"value\":{"
3464 "\"type\":\"integer\","
3465 "\"minimum\":0,"
3466 "\"maximum\":4294967295"
3467 "}"
3468 "},"
3469 "\"required\":[\"type\",\"value\"]"
3470 "},"
3471 "\"u64Value\":{"
3472 "\"properties\":{"
3473 "\"type\":{"
3474 "\"type\":\"string\","
3475 "\"enum\":[\"u64\"]"
3476 "},"
3477 "\"value\":{"
3478 "\"type\":\"integer\","
3479 "\"minimum\":0,"
3480 "\"maximum\":9007199254740991"
3481 "}"
3482 "},"
3483 "\"required\":[\"type\",\"value\"]"
3484 "},"
3485 "\"strValue\":{"
3486 "\"properties\":{"
3487 "\"type\":{"
3488 "\"type\":\"string\","
3489 "\"enum\":[\"str\"]"
3490 "},"
3491 "\"value\":{\"type\":\"string\"}"
3492 "},"
3493 "\"required\":[\"type\",\"value\"]"
3494 "},"
3495 "\"unknownValue\":{"
3496 "\"properties\":{"
3497 "\"type\":{"
3498 "\"type\":\"integer\","
3499 "\"minimum\":0"
3500 "},"
3501 "\"value\":{"
3502 "\"type\":\"string\","
3503 "\"enum\":[\"unknown\"]"
3504 "}"
3505 "},"
3506 "\"required\":[\"type\",\"value\"]"
3507 "}"
3508 "}"
3509 "}"
3510 "}"
3511 "}");
3512
3513 if (old_len == out->len) {
3514 chunk_reset(out);
3515 chunk_appendf(out,
3516 "{\"errorStr\":\"output buffer too short\"}");
3517 }
3518}
3519
3520/* This function dumps the schema onto the stream interface's read buffer.
3521 * It returns 0 as long as it does not complete, non-zero upon completion.
3522 * No state is used.
3523 */
3524static int stats_dump_json_schema_to_buffer(struct stream_interface *si)
3525{
3526 chunk_reset(&trash);
3527
3528 stats_dump_json_schema(&trash);
3529
Willy Tarreau06d80a92017-10-19 14:32:15 +02003530 if (ci_putchk(si_ic(si), &trash) == -1) {
Simon Horman6f6bb382017-01-04 09:37:26 +01003531 si_applet_cant_put(si);
3532 return 0;
3533 }
3534
3535 return 1;
3536}
3537
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02003538static int cli_parse_clear_counters(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau89d467c2016-11-23 11:02:40 +01003539{
3540 struct proxy *px;
3541 struct server *sv;
3542 struct listener *li;
3543 int clrall = 0;
3544
3545 if (strcmp(args[2], "all") == 0)
3546 clrall = 1;
3547
3548 /* check permissions */
3549 if (!cli_has_level(appctx, ACCESS_LVL_OPER) ||
3550 (clrall && !cli_has_level(appctx, ACCESS_LVL_ADMIN)))
3551 return 1;
3552
Olivier Houchardfbc74e82017-11-24 16:54:05 +01003553 for (px = proxies_list; px; px = px->next) {
Willy Tarreau89d467c2016-11-23 11:02:40 +01003554 if (clrall) {
3555 memset(&px->be_counters, 0, sizeof(px->be_counters));
3556 memset(&px->fe_counters, 0, sizeof(px->fe_counters));
3557 }
3558 else {
3559 px->be_counters.conn_max = 0;
3560 px->be_counters.p.http.rps_max = 0;
3561 px->be_counters.sps_max = 0;
3562 px->be_counters.cps_max = 0;
3563 px->be_counters.nbpend_max = 0;
3564
3565 px->fe_counters.conn_max = 0;
3566 px->fe_counters.p.http.rps_max = 0;
3567 px->fe_counters.sps_max = 0;
3568 px->fe_counters.cps_max = 0;
Willy Tarreau89d467c2016-11-23 11:02:40 +01003569 }
3570
3571 for (sv = px->srv; sv; sv = sv->next)
3572 if (clrall)
3573 memset(&sv->counters, 0, sizeof(sv->counters));
3574 else {
3575 sv->counters.cur_sess_max = 0;
3576 sv->counters.nbpend_max = 0;
3577 sv->counters.sps_max = 0;
3578 }
3579
3580 list_for_each_entry(li, &px->conf.listeners, by_fe)
3581 if (li->counters) {
3582 if (clrall)
3583 memset(li->counters, 0, sizeof(*li->counters));
3584 else
3585 li->counters->conn_max = 0;
3586 }
3587 }
3588
3589 global.cps_max = 0;
3590 global.sps_max = 0;
Olivier Houchard00bc3cb2017-10-17 19:23:25 +02003591 global.ssl_max = 0;
3592 global.ssl_fe_keys_max = 0;
3593 global.ssl_be_keys_max = 0;
Willy Tarreaud80cb4e2018-01-20 19:30:13 +01003594
3595 memset(activity, 0, sizeof(activity));
Willy Tarreau89d467c2016-11-23 11:02:40 +01003596 return 1;
3597}
3598
3599
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02003600static int cli_parse_show_info(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003601{
Willy Tarreaud25fc792016-12-16 12:33:47 +01003602 appctx->ctx.stats.scope_str = 0;
3603 appctx->ctx.stats.scope_len = 0;
3604 appctx->ctx.stats.flags = 0;
3605
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003606 if (strcmp(args[2], "typed") == 0)
3607 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
Simon Horman05ee2132017-01-04 09:37:25 +01003608 else if (strcmp(args[2], "json") == 0)
3609 appctx->ctx.stats.flags |= STAT_FMT_JSON;
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003610 return 0;
3611}
3612
3613
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02003614static int cli_parse_show_stat(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau2b812e22016-11-22 16:18:05 +01003615{
Willy Tarreaud25fc792016-12-16 12:33:47 +01003616 appctx->ctx.stats.scope_str = 0;
3617 appctx->ctx.stats.scope_len = 0;
3618 appctx->ctx.stats.flags = 0;
3619
Willy Tarreau2b812e22016-11-22 16:18:05 +01003620 if (*args[2] && *args[3] && *args[4]) {
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01003621 struct proxy *px;
3622
3623 px = proxy_find_by_name(args[2], 0, 0);
3624 if (px)
3625 appctx->ctx.stats.iid = px->uuid;
3626 else
3627 appctx->ctx.stats.iid = atoi(args[2]);
3628
3629 if (!appctx->ctx.stats.iid) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02003630 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreaua1b1ed52016-11-25 08:50:58 +01003631 appctx->ctx.cli.msg = "No such proxy.\n";
3632 appctx->st0 = CLI_ST_PRINT;
3633 return 1;
3634 }
3635
Willy Tarreau2b812e22016-11-22 16:18:05 +01003636 appctx->ctx.stats.flags |= STAT_BOUND;
Willy Tarreau2b812e22016-11-22 16:18:05 +01003637 appctx->ctx.stats.type = atoi(args[3]);
3638 appctx->ctx.stats.sid = atoi(args[4]);
3639 if (strcmp(args[5], "typed") == 0)
3640 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
Simon Horman05ee2132017-01-04 09:37:25 +01003641 else if (strcmp(args[5], "json") == 0)
3642 appctx->ctx.stats.flags |= STAT_FMT_JSON;
Willy Tarreau2b812e22016-11-22 16:18:05 +01003643 }
3644 else if (strcmp(args[2], "typed") == 0)
3645 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
Simon Horman05ee2132017-01-04 09:37:25 +01003646 else if (strcmp(args[2], "json") == 0)
3647 appctx->ctx.stats.flags |= STAT_FMT_JSON;
Willy Tarreau2b812e22016-11-22 16:18:05 +01003648
Willy Tarreau2b812e22016-11-22 16:18:05 +01003649 return 0;
3650}
3651
Willy Tarreau0baac8c2016-11-22 16:36:53 +01003652static int cli_io_handler_dump_info(struct appctx *appctx)
3653{
3654 return stats_dump_info_to_buffer(appctx->owner);
3655}
3656
Willy Tarreau2b812e22016-11-22 16:18:05 +01003657/* This I/O handler runs as an applet embedded in a stream interface. It is
3658 * used to send raw stats over a socket.
3659 */
3660static int cli_io_handler_dump_stat(struct appctx *appctx)
3661{
3662 return stats_dump_stat_to_buffer(appctx->owner, NULL);
3663}
3664
Simon Horman6f6bb382017-01-04 09:37:26 +01003665static int cli_io_handler_dump_json_schema(struct appctx *appctx)
3666{
3667 return stats_dump_json_schema_to_buffer(appctx->owner);
3668}
3669
Willy Tarreau2b812e22016-11-22 16:18:05 +01003670/* register cli keywords */
3671static struct cli_kw_list cli_kws = {{ },{
Willy Tarreau89d467c2016-11-23 11:02:40 +01003672 { { "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 +01003673 { { "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 +01003674 { { "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 +01003675 { { "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 +01003676 {{},}
3677}};
3678
William Lallemand74c24fb2016-11-21 17:18:36 +01003679struct applet http_stats_applet = {
3680 .obj_type = OBJ_TYPE_APPLET,
3681 .name = "<STATS>", /* used for logging */
3682 .fct = http_stats_io_handler,
3683 .release = NULL,
3684};
3685
Willy Tarreau2b812e22016-11-22 16:18:05 +01003686__attribute__((constructor))
3687static void __stat_init(void)
3688{
3689 cli_register_kw(&cli_kws);
3690}
3691
William Lallemand74c24fb2016-11-21 17:18:36 +01003692/*
3693 * Local variables:
3694 * c-indent-level: 8
3695 * c-basic-offset: 8
3696 * End:
3697 */