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